| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Unary relations |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Relation.Unary where |
| 10 |
|
| 11 |
open import Data.Empty using (⊥) |
| 12 |
open import Data.Unit.Base using (⊤) |
| 13 |
open import Data.Product.Base using (_×_; _,_; Σ-syntax; ∃; uncurry; swap) |
| 14 |
open import Data.Sum.Base using (_⊎_; [_,_]) |
| 15 |
open import Function.Base using (_∘_; _|>_) |
| 16 |
open import Level using (Level; _⊔_; 0ℓ; suc; Lift) |
| 17 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_) |
| 18 |
open import Relation.Nullary as Nullary using (¬_; Dec; True) |
| 19 |
|
| 20 |
private |
| 21 |
variable |
| 22 |
a b c ℓ ℓ₁ ℓ₂ : Level |
| 23 |
A : Set a |
| 24 |
B : Set b |
| 25 |
C : Set c |
| 26 |
|
| 27 |
------------------------------------------------------------------------ |
| 28 |
-- Definition |
| 29 |
|
| 30 |
-- Unary relations are known as predicates and `Pred A ℓ` can be viewed |
| 31 |
-- as some property that elements of type A might satisfy. |
| 32 |
|
| 33 |
-- Consequently `P : Pred A ℓ` can also be seen as a subset of A |
| 34 |
-- containing all the elements of A that satisfy property P. This view |
| 35 |
-- informs much of the notation used below. |
| 36 |
|
| 37 |
Pred : ∀ {a} → Set a → (ℓ : Level) → Set (a ⊔ suc ℓ) |
| 38 |
Pred A ℓ = A → Set ℓ |
| 39 |
|
| 40 |
------------------------------------------------------------------------ |
| 41 |
-- Special sets |
| 42 |
|
| 43 |
-- The empty set. |
| 44 |
-- Explicitly not level polymorphic as this often causes unsolved metas; |
| 45 |
-- see `Relation.Unary.Polymorphic` for a level-polymorphic version. |
| 46 |
|
| 47 |
∅ : Pred A 0ℓ |
| 48 |
∅ = λ _ → ⊥ |
| 49 |
|
| 50 |
-- The singleton set. |
| 51 |
|
| 52 |
{_} : A → Pred A _ |
| 53 |
{ x } = x ≡_ |
| 54 |
|
| 55 |
-- The universal set. |
| 56 |
-- Explicitly not level polymorphic (see comments for `∅` for more details) |
| 57 |
|
| 58 |
U : Pred A 0ℓ |
| 59 |
U = λ _ → ⊤ |
| 60 |
|
| 61 |
------------------------------------------------------------------------ |
| 62 |
-- Membership |
| 63 |
|
| 64 |
infix 4 _∈_ _∉_ |
| 65 |
|
| 66 |
_∈_ : A → Pred A ℓ → Set _ |
| 67 |
x ∈ P = P x |
| 68 |
|
| 69 |
_∉_ : A → Pred A ℓ → Set _ |
| 70 |
x ∉ P = ¬ x ∈ P |
| 71 |
|
| 72 |
------------------------------------------------------------------------ |
| 73 |
-- Subset relations |
| 74 |
|
| 75 |
infix 4 _⊆_ _⊇_ _⊈_ _⊉_ _⊂_ _⊃_ _⊄_ _⊅_ _≐_ _≐′_ |
| 76 |
|
| 77 |
_⊆_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 78 |
P ⊆ Q = ∀ {x} → x ∈ P → x ∈ Q |
| 79 |
|
| 80 |
_⊇_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 81 |
P ⊇ Q = Q ⊆ P |
| 82 |
|
| 83 |
_⊈_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 84 |
P ⊈ Q = ¬ (P ⊆ Q) |
| 85 |
|
| 86 |
_⊉_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 87 |
P ⊉ Q = ¬ (P ⊇ Q) |
| 88 |
|
| 89 |
_⊂_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 90 |
P ⊂ Q = P ⊆ Q × Q ⊈ P |
| 91 |
|
| 92 |
_⊃_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 93 |
P ⊃ Q = Q ⊂ P |
| 94 |
|
| 95 |
_⊄_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 96 |
P ⊄ Q = ¬ (P ⊂ Q) |
| 97 |
|
| 98 |
_⊅_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 99 |
P ⊅ Q = ¬ (P ⊃ Q) |
| 100 |
|
| 101 |
_≐_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 102 |
P ≐ Q = (P ⊆ Q) × (Q ⊆ P) |
| 103 |
|
| 104 |
-- The following primed variants of _⊆_ can be used when 'x' can't |
| 105 |
-- be inferred from 'x ∈ P'. |
| 106 |
|
| 107 |
infix 4 _⊆′_ _⊇′_ _⊈′_ _⊉′_ _⊂′_ _⊃′_ _⊄′_ _⊅′_ |
| 108 |
|
| 109 |
_⊆′_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 110 |
P ⊆′ Q = ∀ x → x ∈ P → x ∈ Q |
| 111 |
|
| 112 |
_⊇′_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 113 |
Q ⊇′ P = P ⊆′ Q |
| 114 |
|
| 115 |
_⊈′_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 116 |
P ⊈′ Q = ¬ (P ⊆′ Q) |
| 117 |
|
| 118 |
_⊉′_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 119 |
P ⊉′ Q = ¬ (P ⊇′ Q) |
| 120 |
|
| 121 |
_⊂′_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 122 |
P ⊂′ Q = P ⊆′ Q × Q ⊈′ P |
| 123 |
|
| 124 |
_⊃′_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 125 |
P ⊃′ Q = Q ⊂′ P |
| 126 |
|
| 127 |
_⊄′_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 128 |
P ⊄′ Q = ¬ (P ⊂′ Q) |
| 129 |
|
| 130 |
_⊅′_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 131 |
P ⊅′ Q = ¬ (P ⊃′ Q) |
| 132 |
|
| 133 |
_≐′_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 134 |
P ≐′ Q = (P ⊆′ Q) × (Q ⊆′ P) |
| 135 |
|
| 136 |
------------------------------------------------------------------------ |
| 137 |
-- Properties of sets |
| 138 |
|
| 139 |
infix 10 Satisfiable Universal IUniversal |
| 140 |
|
| 141 |
-- Emptiness - no element satisfies P. |
| 142 |
|
| 143 |
Empty : Pred A ℓ → Set _ |
| 144 |
Empty P = ∀ x → x ∉ P |
| 145 |
|
| 146 |
-- Satisfiable - at least one element satisfies P. |
| 147 |
|
| 148 |
Satisfiable : Pred A ℓ → Set _ |
| 149 |
Satisfiable P = ∃ λ x → x ∈ P |
| 150 |
|
| 151 |
syntax Satisfiable P = ∃⟨ P ⟩ |
| 152 |
|
| 153 |
-- Universality - all elements satisfy P. |
| 154 |
|
| 155 |
Universal : Pred A ℓ → Set _ |
| 156 |
Universal P = ∀ x → x ∈ P |
| 157 |
|
| 158 |
syntax Universal P = Π[ P ] |
| 159 |
|
| 160 |
-- Implicit universality - all elements satisfy P. |
| 161 |
|
| 162 |
IUniversal : Pred A ℓ → Set _ |
| 163 |
IUniversal P = ∀ {x} → x ∈ P |
| 164 |
|
| 165 |
syntax IUniversal P = ∀[ P ] |
| 166 |
|
| 167 |
-- Irrelevance - any two proofs that an element satifies P are |
| 168 |
-- indistinguishable. |
| 169 |
|
| 170 |
Irrelevant : Pred A ℓ → Set _ |
| 171 |
Irrelevant P = ∀ {x} → Nullary.Irrelevant (P x) |
| 172 |
|
| 173 |
-- Recomputability - we can rebuild a relevant proof given an |
| 174 |
-- irrelevant one. |
| 175 |
|
| 176 |
Recomputable : Pred A ℓ → Set _ |
| 177 |
Recomputable P = ∀ {x} → Nullary.Recomputable (P x) |
| 178 |
|
| 179 |
-- Stability - instances of P are stable wrt double negation |
| 180 |
|
| 181 |
Stable : Pred A ℓ → Set _ |
| 182 |
Stable P = ∀ x → Nullary.Stable (P x) |
| 183 |
|
| 184 |
-- Weak Decidability |
| 185 |
|
| 186 |
WeaklyDecidable : Pred A ℓ → Set _ |
| 187 |
WeaklyDecidable P = ∀ x → Nullary.WeaklyDecidable (P x) |
| 188 |
|
| 189 |
-- Decidability - it is possible to determine if an arbitrary element |
| 190 |
-- satisfies P. |
| 191 |
|
| 192 |
Decidable : Pred A ℓ → Set _ |
| 193 |
Decidable P = ∀ x → Dec (P x) |
| 194 |
|
| 195 |
-- Erasure: A decidable predicate gives rise to another one, more |
| 196 |
-- amenable to η-expansion |
| 197 |
|
| 198 |
⌊_⌋ : {P : Pred A ℓ} → Decidable P → Pred A ℓ |
| 199 |
⌊ P? ⌋ a = Lift _ (True (P? a)) |
| 200 |
|
| 201 |
------------------------------------------------------------------------ |
| 202 |
-- Operations on sets |
| 203 |
|
| 204 |
infix 10 ⋃ ⋂ |
| 205 |
infixr 9 _⊢_ |
| 206 |
infixr 8 _⇒_ |
| 207 |
infixr 7 _∩_ |
| 208 |
infixr 6 _∪_ |
| 209 |
infixr 6 _∖_ |
| 210 |
infix 4 _≬_ _⊥_ _⊥′_ |
| 211 |
|
| 212 |
-- Complement. |
| 213 |
|
| 214 |
∁ : Pred A ℓ → Pred A ℓ |
| 215 |
∁ P = λ x → x ∉ P |
| 216 |
|
| 217 |
-- Implication. |
| 218 |
|
| 219 |
_⇒_ : Pred A ℓ₁ → Pred A ℓ₂ → Pred A _ |
| 220 |
P ⇒ Q = λ x → x ∈ P → x ∈ Q |
| 221 |
|
| 222 |
-- Union. |
| 223 |
|
| 224 |
_∪_ : Pred A ℓ₁ → Pred A ℓ₂ → Pred A _ |
| 225 |
P ∪ Q = λ x → x ∈ P ⊎ x ∈ Q |
| 226 |
|
| 227 |
-- Intersection. |
| 228 |
|
| 229 |
_∩_ : Pred A ℓ₁ → Pred A ℓ₂ → Pred A _ |
| 230 |
P ∩ Q = λ x → x ∈ P × x ∈ Q |
| 231 |
|
| 232 |
-- Difference. |
| 233 |
|
| 234 |
_∖_ : Pred A ℓ₁ → Pred A ℓ₂ → Pred A _ |
| 235 |
P ∖ Q = λ x → x ∈ P × x ∉ Q |
| 236 |
|
| 237 |
-- Infinitary union. |
| 238 |
|
| 239 |
⋃ : ∀ {i} (I : Set i) → (I → Pred A ℓ) → Pred A _ |
| 240 |
⋃ I P = λ x → Σ[ i ∈ I ] P i x |
| 241 |
|
| 242 |
syntax ⋃ I (λ i → P) = ⋃[ i ∶ I ] P |
| 243 |
|
| 244 |
-- Infinitary intersection. |
| 245 |
|
| 246 |
⋂ : ∀ {i} (I : Set i) → (I → Pred A ℓ) → Pred A _ |
| 247 |
⋂ I P = λ x → (i : I) → P i x |
| 248 |
|
| 249 |
syntax ⋂ I (λ i → P) = ⋂[ i ∶ I ] P |
| 250 |
|
| 251 |
-- Positive version of non-disjointness, dual to inclusion. |
| 252 |
|
| 253 |
_≬_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 254 |
P ≬ Q = ∃ λ x → x ∈ P × x ∈ Q |
| 255 |
|
| 256 |
-- Disjoint |
| 257 |
|
| 258 |
_⊥_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 259 |
P ⊥ Q = P ∩ Q ⊆ ∅ |
| 260 |
|
| 261 |
_⊥′_ : Pred A ℓ₁ → Pred A ℓ₂ → Set _ |
| 262 |
P ⊥′ Q = P ∩ Q ⊆′ ∅ |
| 263 |
|
| 264 |
-- Update. |
| 265 |
|
| 266 |
_⊢_ : (A → B) → Pred B ℓ → Pred A ℓ |
| 267 |
f ⊢ P = λ x → P (f x) |
| 268 |
|
| 269 |
------------------------------------------------------------------------ |
| 270 |
-- Predicate combinators |
| 271 |
|
| 272 |
-- These differ from the set operations above, as the carrier set of the |
| 273 |
-- resulting predicates are not the same as the carrier set of the |
| 274 |
-- component predicates. |
| 275 |
|
| 276 |
infixr 2 _⟨×⟩_ |
| 277 |
infixr 2 _⟨⊙⟩_ |
| 278 |
infixr 1 _⟨⊎⟩_ |
| 279 |
infixr 0 _⟨→⟩_ |
| 280 |
infixl 9 _⟨·⟩_ |
| 281 |
infix 10 _~ |
| 282 |
infixr 9 _⟨∘⟩_ |
| 283 |
infixr 2 _//_ _\\_ |
| 284 |
|
| 285 |
-- Product. |
| 286 |
|
| 287 |
_⟨×⟩_ : Pred A ℓ₁ → Pred B ℓ₂ → Pred (A × B) _ |
| 288 |
(P ⟨×⟩ Q) (x , y) = x ∈ P × y ∈ Q |
| 289 |
|
| 290 |
-- Sum over one element. |
| 291 |
|
| 292 |
_⟨⊎⟩_ : Pred A ℓ → Pred B ℓ → Pred (A ⊎ B) _ |
| 293 |
P ⟨⊎⟩ Q = [ P , Q ] |
| 294 |
|
| 295 |
-- Sum over two elements. |
| 296 |
|
| 297 |
_⟨⊙⟩_ : Pred A ℓ₁ → Pred B ℓ₂ → Pred (A × B) _ |
| 298 |
(P ⟨⊙⟩ Q) (x , y) = x ∈ P ⊎ y ∈ Q |
| 299 |
|
| 300 |
-- Implication. |
| 301 |
|
| 302 |
_⟨→⟩_ : Pred A ℓ₁ → Pred B ℓ₂ → Pred (A → B) _ |
| 303 |
(P ⟨→⟩ Q) f = P ⊆ Q ∘ f |
| 304 |
|
| 305 |
-- Product. |
| 306 |
|
| 307 |
_⟨·⟩_ : (P : Pred A ℓ₁) (Q : Pred B ℓ₂) → |
| 308 |
(P ⟨×⟩ (P ⟨→⟩ Q)) ⊆ Q ∘ uncurry _|>_ |
| 309 |
(P ⟨·⟩ Q) (p , f) = f p |
| 310 |
|
| 311 |
-- Converse. |
| 312 |
|
| 313 |
_~ : Pred (A × B) ℓ → Pred (B × A) ℓ |
| 314 |
P ~ = P ∘ swap |
| 315 |
|
| 316 |
-- Composition. |
| 317 |
|
| 318 |
_⟨∘⟩_ : Pred (A × B) ℓ₁ → Pred (B × C) ℓ₂ → Pred (A × C) _ |
| 319 |
(P ⟨∘⟩ Q) (x , z) = ∃ λ y → (x , y) ∈ P × (y , z) ∈ Q |
| 320 |
|
| 321 |
-- Post-division. |
| 322 |
|
| 323 |
_//_ : Pred (A × C) ℓ₁ → Pred (B × C) ℓ₂ → Pred (A × B) _ |
| 324 |
(P // Q) (x , y) = Q ∘ (y ,_) ⊆ P ∘ (x ,_) |
| 325 |
|
| 326 |
-- Pre-division. |
| 327 |
|
| 328 |
_\\_ : Pred (A × C) ℓ₁ → Pred (A × B) ℓ₂ → Pred (B × C) _ |
| 329 |
P \\ Q = (P ~ // Q ~) ~ |