Debian Codegraph @

src/Data/Fin/Subset.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Subsets of finite sets
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Fin.Subset where
10
11 open import Algebra.Core using (Op₁; Op₂)
12 open import Data.Bool using (not; _∧_; _∨_; _≟_)
13 open import Data.Fin.Base using (Fin; zero; suc)
14 open import Data.List.Base using (List; foldr; foldl)
15 open import Data.Nat.Base using (ℕ)
16 open import Data.Product.Base using (∃; _×_)
17 open import Data.Vec.Base hiding (foldr; foldl)
18 open import Function.Base using (flip)
19 open import Relation.Nullary
20
21 private
22 variable
23 n : ℕ
24
25 ------------------------------------------------------------------------
26 -- Definitions
27
28 -- Partitions a finite set into two parts, the inside and the outside.
29 -- Note that it would be great to shorten these to `in` and `out` but
30 -- `in` is a keyword (e.g. let ... in ...)
31
32 -- Sides.
33
34 open import Data.Bool.Base public
35 using () renaming (Bool to Side; true to inside; false to outside)
36
37 -- Subset
38
39 Subset : ℕ → Set
40 Subset = Vec Side
41
42 ------------------------------------------------------------------------
43 -- Special subsets
44
45 -- The empty subset
46 ⊥ : Subset n
47 ⊥ = replicate _ outside
48
49 -- The full subset
50 ⊤ : Subset n
51 ⊤ = replicate _ inside
52
53 -- A singleton subset, containing just the given element.
54 ⁅_⁆ : Fin n → Subset n
55 ⁅ zero ⁆ = inside ∷ ⊥
56 ⁅ suc i ⁆ = outside ∷ ⁅ i ⁆
57
58 ------------------------------------------------------------------------
59 -- Membership and subset predicates
60
61 infix 4 _∈_ _∉_ _⊆_ _⊈_ _⊂_ _⊄_
62
63 _∈_ : Fin n → Subset n → Set
64 x ∈ p = p [ x ]= inside
65
66 _∉_ : Fin n → Subset n → Set
67 x ∉ p = ¬ (x ∈ p)
68
69 _⊆_ : Subset n → Subset n → Set
70 p ⊆ q = ∀ {x} → x ∈ p → x ∈ q
71
72 _⊈_ : Subset n → Subset n → Set
73 p ⊈ q = ¬ (p ⊆ q)
74
75 _⊂_ : Subset n → Subset n → Set
76 p ⊂ q = p ⊆ q × ∃ (λ x → x ∈ q × x ∉ p)
77
78 _⊄_ : Subset n → Subset n → Set
79 p ⊄ q = ¬ (p ⊂ q)
80
81 _⊇_ : Subset n → Subset n → Set
82 _⊇_ = flip _⊆_
83
84 _⊉_ : Subset n → Subset n → Set
85 _⊉_ = flip _⊈_
86
87 _⊃_ : Subset n → Subset n → Set
88 _⊃_ = flip _⊂_
89
90 _⊅_ : Subset n → Subset n → Set
91 _⊅_ = flip _⊄_
92
93 ------------------------------------------------------------------------
94 -- Set operations
95
96 infixr 7 _∩_
97 infixr 6 _∪_
98 infixl 5 _─_ _-_
99
100 -- Complement
101 ∁ : Op₁ (Subset n)
102 ∁ p = map not p
103
104 -- Intersection
105 _∩_ : Op₂ (Subset n)
106 p ∩ q = zipWith _∧_ p q
107
108 -- Union
109 _∪_ : Op₂ (Subset n)
110 p ∪ q = zipWith _∨_ p q
111
112 -- Difference
113 _─_ : Op₂ (Subset n)
114 p ─ q = zipWith diff p q
115 where
116 diff : Side → Side → Side
117 diff x inside = outside
118 diff x outside = x
119
120 -- N-ary union
121 ⋃ : List (Subset n) → Subset n
122 ⋃ = foldr _∪_ ⊥
123
124 -- N-ary intersection
125 ⋂ : List (Subset n) → Subset n
126 ⋂ = foldr _∩_ ⊤
127
128 -- Element removal
129 _-_ : Subset n → Fin n → Subset n
130 p - x = p ─ ⁅ x ⁆
131
132 -- Size
133 ∣_∣ : Subset n → ℕ
134 ∣ p ∣ = count (_≟ inside) p
135
136 ------------------------------------------------------------------------
137 -- Properties
138
139 Nonempty : ∀ (p : Subset n) → Set
140 Nonempty p = ∃ λ f → f ∈ p
141
142 Empty : ∀ (p : Subset n) → Set
143 Empty p = ¬ Nonempty p
144
145 Lift : ∀ {ℓ} → (Fin n → Set ℓ) → (Subset n → Set ℓ)
146 Lift P p = ∀ {x} → x ∈ p → P x