Skip to content

Commit 2ab1aba

Browse files
authored
Merge pull request #92 from VariantSync/isetops-fixed
Union, Intersection, Set Difference and Further Operations on Indexed Sets
2 parents 1f766b5 + d873e07 commit 2ab1aba

File tree

1 file changed

+197
-5
lines changed

1 file changed

+197
-5
lines changed

src/Vatras/Data/IndexedSet.lagda.md

Lines changed: 197 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module Eq = IsEquivalence isEquivalence
4545
## Definitions
4646

4747
```agda
48-
variable
48+
private variable
4949
iℓ jℓ kℓ : Level
5050
5151
-- An index can just be any set (of any universe, which is why it looks so complicated).
@@ -130,6 +130,19 @@ _⊢_≡ⁱ_ : ∀ {I : Set iℓ} (A : IndexedSet I) → I → I → Set ℓ
130130
A ⊢ i ≡ⁱ j = A i ≈ A j
131131
```
132132

133+
## Inverse Operations
134+
135+
```agda
136+
_∉_ : ∀ {I : Set iℓ} → Carrier → IndexedSet I → Set (ℓ ⊔ iℓ)
137+
a ∉ A = ∀ i → ¬ (a ≈ A i)
138+
139+
Disjoint : ∀ {I : Set iℓ} {J : Set jℓ} (A : IndexedSet I) (B : IndexedSet J) → Set (ℓ ⊔ iℓ ⊔ jℓ)
140+
Disjoint A B = ∀ i → A i ∉ B
141+
142+
Disjoint-flip : ∀ {I : Set iℓ} {J : Set jℓ} {A : IndexedSet I} {B : IndexedSet J} → Disjoint A B → Disjoint B A
143+
Disjoint-flip disjointAB j i Bj≈Ai = disjointAB i j (Eq.sym Bj≈Ai)
144+
```
145+
133146
## Singletons
134147

135148
```agda
@@ -164,11 +177,13 @@ We now prove the following theorems:
164177
⊆-refl i = i , Eq.refl
165178
166179
-- There is no antisymmetry definition in Relation.Binary.Indexed.Heterogeneous.Definition. Adding that to the standard library would be good and a low hanging fruit.
167-
⊆-antisym : ∀ {I : Set iℓ} {J : Set jℓ} → Antisym (_⊆_ {i₁ = I}) (_⊆_ {i₁ = J}) (_≅_)
180+
-- ⊆-antisym : ∀ {I : Set iℓ} {J : Set jℓ} → Antisym (_⊆_ {i₁ = I}) (_⊆_ {i₁ = J}) (_≅_)
181+
⊆-antisym : ∀ {I : Set iℓ} {J : Set jℓ} {A : IndexedSet I} {B : IndexedSet J} → A ⊆ B → B ⊆ A → A ≅ B
168182
⊆-antisym l r = l , r
169183
170184
-- There are no generalized transitivity, symmetry and antisymmetry definitions which allow different levels in Relation.Binary.Indexed.Heterogeneous.Definition . Adding that to the standard library would be good and a low hanging fruit.
171-
⊆-trans : Transitive (IndexedSet {iℓ}) _⊆_
185+
-- ⊆-trans : Transitive (IndexedSet {iℓ}) _⊆_
186+
⊆-trans : ∀ {I : Set iℓ} {J : Set jℓ} {K : Set kℓ} {A : IndexedSet I} {B : IndexedSet J} {C : IndexedSet K} → A ⊆ B → B ⊆ C → A ⊆ C
172187
⊆-trans A⊆B B⊆C i =
173188
-- This proof looks resembles state monad bind >>=.
174189
-- interesting... 🤔
@@ -179,10 +194,10 @@ We now prove the following theorems:
179194
≅-refl : Reflexive (IndexedSet {iℓ}) _≅_
180195
≅-refl = ⊆-refl , ⊆-refl
181196
182-
≅-sym : Symmetric (IndexedSet {iℓ}) _≅_
197+
≅-sym : ∀ {I : Set iℓ} {J : Set jℓ} {A : IndexedSet I} {B : IndexedSet J} → A ≅ B → B ≅ A
183198
≅-sym (l , r) = r , l
184199
185-
≅-trans : Transitive (IndexedSet {iℓ}) _≅_
200+
≅-trans : ∀ {I : Set iℓ} {J : Set jℓ} {K : Set kℓ} {A : IndexedSet I} {B : IndexedSet J} {C : IndexedSet K} → A ≅ B → B ≅ C → A ≅ C
186201
≅-trans (A⊆B , B⊆A) (B⊆C , C⊆B) =
187202
⊆-trans A⊆B B⊆C
188203
, ⊆-trans C⊆B B⊆A
@@ -625,6 +640,183 @@ singleton-set-is-nonempty : (A : 𝟙 iℓ) → nonempty A
625640
singleton-set-is-nonempty _ = tt
626641
```
627642

643+
## Operations
644+
645+
```agda
646+
module _ where
647+
open import Data.Sum using (_⊎_; inj₁; inj₂)
648+
private variable
649+
α : Set iℓ
650+
β : Set jℓ
651+
652+
{-|
653+
Indexed Set Union (Or):
654+
We can create the union of two indexed sets by accepting either of the input indices.
655+
-}
656+
infix 21 _⨆_
657+
_⨆_ : IndexedSet α → IndexedSet β → IndexedSet (α ⊎ β)
658+
(A ⨆ B) (inj₁ a) = A a
659+
(A ⨆ B) (inj₂ b) = B b
660+
661+
{-|
662+
Indexed Set Intersection (And):
663+
The set intersection should contain only elements that are both in A _and_ B.
664+
This means that an element is in the intersection if and only if there is an
665+
index for both A and B that both point to the element.
666+
Hence, the index set of the intersection set can be modelled as the type of
667+
all indices from A that point to elements that also B points to.
668+
-}
669+
infix 21 _⨅_
670+
_⨅_ : (A : IndexedSet α) → (B : IndexedSet β) → IndexedSet (Σ[ a ∈ α ] A a ∈ B)
671+
(A ⨅ B) (a , b , eq) = A a -- We could also pick B b here.
672+
673+
{-|
674+
Indexed Set Difference:
675+
We can remove all elements pointed to by B from an indexed set A by restricting the set of indices
676+
to those indices that do not point to elements in B.
677+
Hence, the index set of the difference set can be modelled as the type of
678+
all indices from A that point to elements that are not pointed at by B.
679+
-}
680+
infix 21 _∖_ -- use \setminus to create ∖ with Agda Input Mode in Emacs
681+
_∖_ : (A : IndexedSet α) → (B : IndexedSet β) → IndexedSet (Σ[ a ∈ α ] (A a ∉ B))
682+
(A ∖ B) (a , Aa∉B) = A a
683+
684+
module _ where
685+
open import Data.Empty using (⊥-elim)
686+
private variable
687+
γ : Set kℓ
688+
A : IndexedSet α
689+
B : IndexedSet β
690+
C : IndexedSet γ
691+
692+
-- ⨆ properties
693+
694+
⊆-⨆ : A ⊆ A ⨆ B
695+
⊆-⨆ i = inj₁ i , Eq.refl
696+
697+
⨆-⊆ : B ⊆ A → A ⨆ B ⊆ A
698+
⨆-⊆ _ (inj₁ a) = a , Eq.refl
699+
⨆-⊆ B⊆A (inj₂ b) = B⊆A b
700+
701+
⨆-idemp : A ⨆ A ≅ A
702+
⨆-idemp = ⨆-⊆ ⊆-refl , ⊆-⨆
703+
704+
⨆-comm-⊆ : A ⨆ B ⊆ B ⨆ A
705+
⨆-comm-⊆ (inj₁ a) = inj₂ a , Eq.refl
706+
⨆-comm-⊆ (inj₂ b) = inj₁ b , Eq.refl
707+
708+
⨆-comm : A ⨆ B ≅ B ⨆ A
709+
⨆-comm = ⨆-comm-⊆ , ⨆-comm-⊆
710+
711+
⨆-assoc-⊆ : (A ⨆ B) ⨆ C ⊆ A ⨆ (B ⨆ C)
712+
⨆-assoc-⊆ (inj₁ (inj₁ a)) = ⊆-⨆ a
713+
⨆-assoc-⊆ (inj₁ (inj₂ b)) = inj₂ (inj₁ b) , Eq.refl
714+
⨆-assoc-⊆ (inj₂ c) = inj₂ (inj₂ c) , Eq.refl
715+
716+
⨆-assoc-⊇ : A ⨆ (B ⨆ C) ⊆ (A ⨆ B) ⨆ C
717+
⨆-assoc-⊇ (inj₁ a) = inj₁ (inj₁ a) , Eq.refl
718+
⨆-assoc-⊇ (inj₂ (inj₁ b)) = inj₁ (inj₂ b) , Eq.refl
719+
⨆-assoc-⊇ (inj₂ (inj₂ c)) = inj₂ c , Eq.refl
720+
721+
⨆-assoc : (A ⨆ B) ⨆ C ≅ A ⨆ (B ⨆ C)
722+
⨆-assoc = ⨆-assoc-⊆ , ⨆-assoc-⊇
723+
724+
⨆-idʳ : A ⨆ 𝟘 ≅ A
725+
⨆-idʳ = ⨆-⊆ 𝟘⊆A , ⊆-⨆
726+
727+
⨆-idˡ : 𝟘 ⨆ A ≅ A
728+
⨆-idˡ = ≅-trans ⨆-comm ⨆-idʳ
729+
730+
-- ⨅ properties
731+
732+
⨅-⊆ : A ⨅ B ⊆ A
733+
⨅-⊆ (a₁ , _ ) = a₁ , Eq.refl
734+
735+
⊆-⨅ : A ⊆ B → A ⊆ A ⨅ B
736+
⊆-⨅ A⊆B a = (a , A⊆B a) , Eq.refl
737+
738+
⨅-idemp : A ⨅ A ≅ A
739+
⨅-idemp = ⨅-⊆ , ⊆-⨅ ⊆-refl
740+
741+
⨅-comm-⊆ : A ⨅ B ⊆ B ⨅ A
742+
⨅-comm-⊆ (a , b , Aa≈Bb) = (b , a , Eq.sym Aa≈Bb) , Aa≈Bb
743+
744+
⨅-comm : A ⨅ B ≅ B ⨅ A
745+
⨅-comm = ⨅-comm-⊆ , ⨅-comm-⊆
746+
747+
⨅-assoc-⊆ : (A ⨅ B) ⨅ C ⊆ A ⨅ (B ⨅ C)
748+
⨅-assoc-⊆ ((a , b , Aa≈Bb) , c , Aa≈Cc) = (a , (b , c , Eq.trans (Eq.sym Aa≈Bb) Aa≈Cc) , Aa≈Bb) , Eq.refl
749+
750+
⨅-assoc-⊇ : A ⨅ (B ⨅ C) ⊆ (A ⨅ B) ⨅ C
751+
⨅-assoc-⊇ (a , (b , c , Bb≈Cc) , Aa≈Bb) = ((a , b , Aa≈Bb) , c , Eq.trans Aa≈Bb Bb≈Cc) , Eq.refl
752+
753+
⨅-assoc : (A ⨅ B) ⨅ C ≅ A ⨅ (B ⨅ C)
754+
⨅-assoc = ⨅-assoc-⊆ , ⨅-assoc-⊇
755+
756+
⨅-zeroˡ : 𝟘 ⨅ A ≅ 𝟘
757+
⨅-zeroˡ = ⨅-⊆ , ⊆-⨅ 𝟘⊆A
758+
759+
⨅-zeroʳ : A ⨅ 𝟘 ≅ 𝟘
760+
⨅-zeroʳ = ≅-trans ⨅-comm ⨅-zeroˡ
761+
762+
-- "A ⨅ B ≅ 𝟘" and "Disjoint A B" are equivalent propositions.
763+
-- "A ⨅ B ≅ 𝟘" is the canonical way of saying that two sets are disjoint.
764+
-- "Disjoint A B" is a direct way of saying that for indexed sets.
765+
766+
⨅-empty→Disjoint : A ⨅ B ≅ 𝟘 → Disjoint A B
767+
⨅-empty→Disjoint (A⨅B⊆𝟘 , 𝟘⊆A⨅B) a b Aa≈Bb with A⨅B⊆𝟘 (a , b , Aa≈Bb)
768+
... | ()
769+
770+
Disjoint→⨅-empty : Disjoint A B → A ⨅ B ≅ 𝟘
771+
proj₁ (Disjoint→⨅-empty disjointAB) (a , b , Aa≈Bb) = ⊥-elim (disjointAB a b Aa≈Bb)
772+
proj₂ (Disjoint→⨅-empty disjointAB) = 𝟘⊆A
773+
774+
-- ∖ properties
775+
776+
∖-⊆ : A ∖ B ⊆ A
777+
∖-⊆ (a , Aa∉B) = a , Eq.refl
778+
779+
⊆-∖ : A ⨅ B ≅ 𝟘 → A ⊆ (A ∖ B)
780+
⊆-∖ A⨅B≅𝟘 a = (a , ⨅-empty→Disjoint A⨅B≅𝟘 a) , Eq.refl
781+
782+
≅-∖ : A ⨅ B ≅ 𝟘 → A ≅ (A ∖ B)
783+
≅-∖ A⨅B≅𝟘 = ⊆-∖ A⨅B≅𝟘 , ∖-⊆
784+
785+
∖-id : A ∖ 𝟘 ≅ A
786+
∖-id = ∖-⊆ , ⊆-∖ ⨅-zeroʳ
787+
788+
∖-zero-⊆ : 𝟘 ∖ A ⊆ 𝟘
789+
∖-zero-⊆ ()
790+
791+
∖-zero : 𝟘 ∖ A ≅ 𝟘
792+
∖-zero = ∖-zero-⊆ , 𝟘⊆A
793+
794+
-- combined properties
795+
796+
⨆-distrib-⨅-⊆ : A ⨆ (B ⨅ C) ⊆ (A ⨆ B) ⨅ (A ⨆ C)
797+
⨆-distrib-⨅-⊆ (inj₁ a) = (inj₁ a , ⊆-⨆ a) , Eq.refl
798+
⨆-distrib-⨅-⊆ (inj₂ (b , c , Bb≈Cc)) = (inj₂ b , inj₂ c , Bb≈Cc) , Eq.refl
799+
800+
⨆-distrib-⨅-⊇ : (A ⨆ B) ⨅ (A ⨆ C) ⊆ A ⨆ (B ⨅ C)
801+
⨆-distrib-⨅-⊇ (inj₁ a , _) = inj₁ a , Eq.refl
802+
⨆-distrib-⨅-⊇ (inj₂ b , inj₁ a , Bb≈Aa) = inj₁ a , Bb≈Aa
803+
⨆-distrib-⨅-⊇ (inj₂ b , inj₂ c , Bb≈Cc) = inj₂ (b , c , Bb≈Cc) , Eq.refl
804+
805+
⨆-distrib-⨅ : A ⨆ (B ⨅ C) ≅ (A ⨆ B) ⨅ (A ⨆ C)
806+
⨆-distrib-⨅ = ⨆-distrib-⨅-⊆ , ⨆-distrib-⨅-⊇
807+
808+
⨅-distrib-⨆-⊆ : A ⨅ (B ⨆ C) ⊆ (A ⨅ B) ⨆ (A ⨅ C)
809+
⨅-distrib-⨆-⊆ (a , inj₁ b , Aa≈Bb) = inj₁ (a , b , Aa≈Bb) , Eq.refl
810+
⨅-distrib-⨆-⊆ (a , inj₂ c , Aa≈Cc) = inj₂ (a , c , Aa≈Cc) , Eq.refl
811+
812+
⨅-distrib-⨆-⊇ : (A ⨅ B) ⨆ (A ⨅ C) ⊆ A ⨅ (B ⨆ C)
813+
⨅-distrib-⨆-⊇ (inj₁ (a , b , Aa≈Bb)) = (a , inj₁ b , Aa≈Bb) , Eq.refl
814+
⨅-distrib-⨆-⊇ (inj₂ (a , c , Aa≈Cc)) = (a , inj₂ c , Aa≈Cc) , Eq.refl
815+
816+
⨅-distrib-⨆ : A ⨅ (B ⨆ C) ≅ (A ⨅ B) ⨆ (A ⨅ C)
817+
⨅-distrib-⨆ = ⨅-distrib-⨆-⊆ , ⨅-distrib-⨆-⊇
818+
```
819+
628820
## Further Properties
629821

630822
### Reindexing

0 commit comments

Comments
 (0)