| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Properties of binary relations |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
-- The contents of this module should be accessed via `Relation.Binary`. |
| 8 |
|
| 9 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 10 |
|
| 11 |
module Relation.Binary.Definitions where |
| 12 |
|
| 13 |
open import Agda.Builtin.Equality using (_≡_) |
| 14 |
open import Data.Product.Base using (_×_; ∃-syntax) |
| 15 |
open import Data.Sum.Base using (_⊎_) |
| 16 |
open import Function.Base using (_on_; flip) |
| 17 |
open import Level using (Level; _⊔_; suc) |
| 18 |
open import Relation.Binary.Core |
| 19 |
open import Relation.Nullary as Nullary using (¬_; Dec) |
| 20 |
|
| 21 |
private |
| 22 |
variable |
| 23 |
a b c ℓ ℓ₁ ℓ₂ ℓ₃ : Level |
| 24 |
A : Set a |
| 25 |
B : Set b |
| 26 |
C : Set c |
| 27 |
|
| 28 |
------------------------------------------------------------------------ |
| 29 |
-- Definitions |
| 30 |
------------------------------------------------------------------------ |
| 31 |
|
| 32 |
-- Reflexivity - defined without an underlying equality. It could |
| 33 |
-- alternatively be defined as `_≈_ ⇒ _∼_` for some equality `_≈_`. |
| 34 |
|
| 35 |
-- Confusingly the convention in the library is to use the name "refl" |
| 36 |
-- for proofs of Reflexive and `reflexive` for proofs of type `_≈_ ⇒ _∼_`, |
| 37 |
-- e.g. in the definition of `IsEquivalence` later in this file. This |
| 38 |
-- convention is a legacy from the early days of the library. |
| 39 |
|
| 40 |
Reflexive : Rel A ℓ → Set _ |
| 41 |
Reflexive _∼_ = ∀ {x} → x ∼ x |
| 42 |
|
| 43 |
-- Generalised symmetry. |
| 44 |
|
| 45 |
Sym : REL A B ℓ₁ → REL B A ℓ₂ → Set _ |
| 46 |
Sym P Q = P ⇒ flip Q |
| 47 |
|
| 48 |
-- Symmetry. |
| 49 |
|
| 50 |
Symmetric : Rel A ℓ → Set _ |
| 51 |
Symmetric _∼_ = Sym _∼_ _∼_ |
| 52 |
|
| 53 |
-- Generalised transitivity. |
| 54 |
|
| 55 |
Trans : REL A B ℓ₁ → REL B C ℓ₂ → REL A C ℓ₃ → Set _ |
| 56 |
Trans P Q R = ∀ {i j k} → P i j → Q j k → R i k |
| 57 |
|
| 58 |
RightTrans : REL A B ℓ₁ → REL B B ℓ₂ → Set _ |
| 59 |
RightTrans R S = Trans R S R |
| 60 |
|
| 61 |
LeftTrans : REL A A ℓ₁ → REL A B ℓ₂ → Set _ |
| 62 |
LeftTrans S R = Trans S R R |
| 63 |
|
| 64 |
-- A flipped variant of generalised transitivity. |
| 65 |
|
| 66 |
TransFlip : REL A B ℓ₁ → REL B C ℓ₂ → REL A C ℓ₃ → Set _ |
| 67 |
TransFlip P Q R = ∀ {i j k} → Q j k → P i j → R i k |
| 68 |
|
| 69 |
-- Transitivity. |
| 70 |
|
| 71 |
Transitive : Rel A ℓ → Set _ |
| 72 |
Transitive _∼_ = Trans _∼_ _∼_ _∼_ |
| 73 |
|
| 74 |
-- Generalised antisymmetry |
| 75 |
|
| 76 |
Antisym : REL A B ℓ₁ → REL B A ℓ₂ → REL A B ℓ₃ → Set _ |
| 77 |
Antisym R S E = ∀ {i j} → R i j → S j i → E i j |
| 78 |
|
| 79 |
-- Antisymmetry. |
| 80 |
|
| 81 |
Antisymmetric : Rel A ℓ₁ → Rel A ℓ₂ → Set _ |
| 82 |
Antisymmetric _≈_ _≤_ = Antisym _≤_ _≤_ _≈_ |
| 83 |
|
| 84 |
-- Irreflexivity - this is defined terms of the underlying equality. |
| 85 |
|
| 86 |
Irreflexive : REL A B ℓ₁ → REL A B ℓ₂ → Set _ |
| 87 |
Irreflexive _≈_ _<_ = ∀ {x y} → x ≈ y → ¬ (x < y) |
| 88 |
|
| 89 |
-- Asymmetry. |
| 90 |
|
| 91 |
Asymmetric : Rel A ℓ → Set _ |
| 92 |
Asymmetric _<_ = ∀ {x y} → x < y → ¬ (y < x) |
| 93 |
|
| 94 |
-- Density |
| 95 |
|
| 96 |
Dense : Rel A ℓ → Set _ |
| 97 |
Dense _<_ = ∀ {x y} → x < y → ∃[ z ] x < z × z < y |
| 98 |
|
| 99 |
-- Generalised connex - at least one of the two relations holds. |
| 100 |
|
| 101 |
Connex : REL A B ℓ₁ → REL B A ℓ₂ → Set _ |
| 102 |
Connex P Q = ∀ x y → P x y ⊎ Q y x |
| 103 |
|
| 104 |
-- Totality. |
| 105 |
|
| 106 |
Total : Rel A ℓ → Set _ |
| 107 |
Total _∼_ = Connex _∼_ _∼_ |
| 108 |
|
| 109 |
-- Generalised trichotomy - exactly one of three types has a witness. |
| 110 |
|
| 111 |
data Tri (A : Set a) (B : Set b) (C : Set c) : Set (a ⊔ b ⊔ c) where |
| 112 |
tri< : ( a : A) (¬b : ¬ B) (¬c : ¬ C) → Tri A B C |
| 113 |
tri≈ : (¬a : ¬ A) ( b : B) (¬c : ¬ C) → Tri A B C |
| 114 |
tri> : (¬a : ¬ A) (¬b : ¬ B) ( c : C) → Tri A B C |
| 115 |
|
| 116 |
-- Trichotomy. |
| 117 |
|
| 118 |
Trichotomous : Rel A ℓ₁ → Rel A ℓ₂ → Set _ |
| 119 |
Trichotomous _≈_ _<_ = ∀ x y → Tri (x < y) (x ≈ y) (x > y) |
| 120 |
where _>_ = flip _<_ |
| 121 |
|
| 122 |
-- Generalised maximum element. |
| 123 |
|
| 124 |
Max : REL A B ℓ → B → Set _ |
| 125 |
Max _≤_ T = ∀ x → x ≤ T |
| 126 |
|
| 127 |
-- Maximum element. |
| 128 |
|
| 129 |
Maximum : Rel A ℓ → A → Set _ |
| 130 |
Maximum = Max |
| 131 |
|
| 132 |
-- Generalised minimum element. |
| 133 |
|
| 134 |
Min : REL A B ℓ → A → Set _ |
| 135 |
Min R = Max (flip R) |
| 136 |
|
| 137 |
-- Minimum element. |
| 138 |
|
| 139 |
Minimum : Rel A ℓ → A → Set _ |
| 140 |
Minimum = Min |
| 141 |
|
| 142 |
-- Definitions for apartness relations |
| 143 |
|
| 144 |
-- Note that Cotransitive's arguments are permuted with respect to Transitive's. |
| 145 |
Cotransitive : Rel A ℓ → Set _ |
| 146 |
Cotransitive _#_ = ∀ {x y} → x # y → ∀ z → (x # z) ⊎ (z # y) |
| 147 |
|
| 148 |
Tight : Rel A ℓ₁ → Rel A ℓ₂ → Set _ |
| 149 |
Tight _≈_ _#_ = ∀ x y → (¬ x # y → x ≈ y) × (x ≈ y → ¬ x # y) |
| 150 |
|
| 151 |
-- Properties of order morphisms, aka order-preserving maps |
| 152 |
|
| 153 |
Monotonic₁ : Rel A ℓ₁ → Rel B ℓ₂ → (A → B) → Set _ |
| 154 |
Monotonic₁ _≤_ _⊑_ f = f Preserves _≤_ ⟶ _⊑_ |
| 155 |
|
| 156 |
Antitonic₁ : Rel A ℓ₁ → Rel B ℓ₂ → (A → B) → Set _ |
| 157 |
Antitonic₁ _≤_ = Monotonic₁ (flip _≤_) |
| 158 |
|
| 159 |
LeftMonotonic : Rel B ℓ₁ → Rel C ℓ₂ → (A → B → C) → Set _ |
| 160 |
LeftMonotonic _≤_ _⊑_ _∙_ = ∀ x → Monotonic₁ _≤_ _⊑_ (x ∙_) |
| 161 |
|
| 162 |
RightMonotonic : Rel A ℓ₁ → Rel C ℓ₂ → (A → B → C) → Set _ |
| 163 |
RightMonotonic _≤_ _⊑_ _∙_ = ∀ y → Monotonic₁ _≤_ _⊑_ (_∙ y) |
| 164 |
|
| 165 |
Monotonic₂ : Rel A ℓ₁ → Rel B ℓ₂ → Rel C ℓ₃ → (A → B → C) → Set _ |
| 166 |
Monotonic₂ _≤_ _⊑_ _≼_ ∙ = ∙ Preserves₂ _≤_ ⟶ _⊑_ ⟶ _≼_ |
| 167 |
|
| 168 |
MonotonicAntitonic : Rel A ℓ₁ → Rel B ℓ₂ → Rel C ℓ₃ → (A → B → C) → Set _ |
| 169 |
MonotonicAntitonic _≤_ _⊑_ = Monotonic₂ _≤_ (flip _⊑_) |
| 170 |
|
| 171 |
AntitonicMonotonic : Rel A ℓ₁ → Rel B ℓ₂ → Rel C ℓ₃ → (A → B → C) → Set _ |
| 172 |
AntitonicMonotonic _≤_ = Monotonic₂ (flip _≤_) |
| 173 |
|
| 174 |
Antitonic₂ : Rel A ℓ₁ → Rel B ℓ₂ → Rel C ℓ₃ → (A → B → C) → Set _ |
| 175 |
Antitonic₂ _≤_ _⊑_ = Monotonic₂ (flip _≤_) (flip _⊑_) |
| 176 |
|
| 177 |
Adjoint : Rel A ℓ₁ → Rel B ℓ₂ → (A → B) → (B → A) → Set _ |
| 178 |
Adjoint _≤_ _⊑_ f g = ∀ {x y} → (f x ⊑ y → x ≤ g y) × (x ≤ g y → f x ⊑ y) |
| 179 |
|
| 180 |
-- Unary relations respecting a binary relation. |
| 181 |
|
| 182 |
_⟶_Respects_ : (A → Set ℓ₁) → (B → Set ℓ₂) → REL A B ℓ₃ → Set _ |
| 183 |
P ⟶ Q Respects _∼_ = ∀ {x y} → x ∼ y → P x → Q y |
| 184 |
|
| 185 |
-- Unary relation respects a binary relation. |
| 186 |
|
| 187 |
_Respects_ : (A → Set ℓ₁) → Rel A ℓ₂ → Set _ |
| 188 |
P Respects _∼_ = P ⟶ P Respects _∼_ |
| 189 |
|
| 190 |
-- Right respecting - relatedness is preserved on the right by equality. |
| 191 |
|
| 192 |
_Respectsʳ_ : REL A B ℓ₁ → Rel B ℓ₂ → Set _ |
| 193 |
_∼_ Respectsʳ _≈_ = ∀ {x} → (x ∼_) Respects _≈_ |
| 194 |
|
| 195 |
-- Left respecting - relatedness is preserved on the left by equality. |
| 196 |
|
| 197 |
_Respectsˡ_ : REL A B ℓ₁ → Rel A ℓ₂ → Set _ |
| 198 |
P Respectsˡ _∼_ = ∀ {y} → (flip P y) Respects _∼_ |
| 199 |
|
| 200 |
-- Respecting - relatedness is preserved on both sides by equality |
| 201 |
|
| 202 |
_Respects₂_ : Rel A ℓ₁ → Rel A ℓ₂ → Set _ |
| 203 |
P Respects₂ _∼_ = (P Respectsʳ _∼_) × (P Respectsˡ _∼_) |
| 204 |
|
| 205 |
-- Substitutivity - any two related elements satisfy exactly the same |
| 206 |
-- set of unary relations. Note that only the various derivatives |
| 207 |
-- of propositional equality can satisfy this property. |
| 208 |
|
| 209 |
Substitutive : Rel A ℓ₁ → (ℓ₂ : Level) → Set _ |
| 210 |
Substitutive {A = A} _∼_ p = (P : A → Set p) → P Respects _∼_ |
| 211 |
|
| 212 |
-- Irrelevancy - all proofs that a given pair of elements are related |
| 213 |
-- are indistinguishable. |
| 214 |
|
| 215 |
Irrelevant : REL A B ℓ → Set _ |
| 216 |
Irrelevant _∼_ = ∀ {x y} → Nullary.Irrelevant (x ∼ y) |
| 217 |
|
| 218 |
-- Recomputability - we can rebuild a relevant proof given an |
| 219 |
-- irrelevant one. |
| 220 |
|
| 221 |
Recomputable : REL A B ℓ → Set _ |
| 222 |
Recomputable _∼_ = ∀ {x y} → Nullary.Recomputable (x ∼ y) |
| 223 |
|
| 224 |
-- Stability |
| 225 |
|
| 226 |
Stable : REL A B ℓ → Set _ |
| 227 |
Stable _∼_ = ∀ x y → Nullary.Stable (x ∼ y) |
| 228 |
|
| 229 |
-- Weak decidability - it is sometimes possible to determine if a given |
| 230 |
-- pair of elements are related. |
| 231 |
|
| 232 |
WeaklyDecidable : REL A B ℓ → Set _ |
| 233 |
WeaklyDecidable _∼_ = ∀ x y → Nullary.WeaklyDecidable (x ∼ y) |
| 234 |
|
| 235 |
-- Decidability - it is possible to determine whether a given pair of |
| 236 |
-- elements are related. |
| 237 |
|
| 238 |
Decidable : REL A B ℓ → Set _ |
| 239 |
Decidable _∼_ = ∀ x y → Dec (x ∼ y) |
| 240 |
|
| 241 |
-- Propositional equality is decidable for the type. |
| 242 |
|
| 243 |
DecidableEquality : (A : Set a) → Set _ |
| 244 |
DecidableEquality A = Decidable {A = A} _≡_ |
| 245 |
|
| 246 |
-- Universal - all pairs of elements are related |
| 247 |
|
| 248 |
Universal : REL A B ℓ → Set _ |
| 249 |
Universal _∼_ = ∀ x y → x ∼ y |
| 250 |
|
| 251 |
-- Empty - no elements are related |
| 252 |
|
| 253 |
Empty : REL A B ℓ → Set _ |
| 254 |
Empty _∼_ = ∀ {x y} → ¬ (x ∼ y) |
| 255 |
|
| 256 |
-- Non-emptiness - at least one pair of elements are related. |
| 257 |
|
| 258 |
record NonEmpty {A : Set a} {B : Set b} |
| 259 |
(T : REL A B ℓ) : Set (a ⊔ b ⊔ ℓ) where |
| 260 |
constructor nonEmpty |
| 261 |
field |
| 262 |
{x} : A |
| 263 |
{y} : B |
| 264 |
proof : T x y |