Debian Codegraph @

src/Relation/Binary/Construct/On.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Many properties which hold for `_∼_` also hold for `_∼_ on f`
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Relation.Binary.Construct.On where
10
11 open import Data.Product.Base using (_,_)
12 open import Function.Base using (_on_; _∘_)
13 open import Induction.WellFounded using (WellFounded; Acc; acc)
14 open import Level using (Level)
15 open import Relation.Binary.Core using (Rel; _⇒_)
16 open import Relation.Binary.Bundles
17 using (Preorder; Setoid; DecSetoid; Poset; DecPoset; StrictPartialOrder
18 ; TotalOrder; DecTotalOrder; StrictTotalOrder)
19 open import Relation.Binary.Structures
20 using (IsEquivalence; IsDecEquivalence; IsPreorder; IsPartialOrder
21 ; IsDecPartialOrder; IsStrictPartialOrder; IsTotalOrder
22 ; IsDecTotalOrder; IsStrictTotalOrder)
23 open import Relation.Binary.Definitions
24 using (Reflexive; Irreflexive; Symmetric; Transitive; Antisymmetric
25 ; Asymmetric; Decidable; Total; Trichotomous; _Respects_; _Respects₂_)
26
27 private
28 variable
29 a b p ℓ ℓ₁ ℓ₂ : Level
30 A : Set a
31 B : Set b
32
33 ------------------------------------------------------------------------
34 -- Definitions
35
36 module _ (f : B → A) where
37
38 implies : (≈ : Rel A ℓ₁) (∼ : Rel A ℓ₂) →
39 ≈ ⇒ ∼ → (≈ on f) ⇒ (∼ on f)
40 implies _ _ impl = impl
41
42 reflexive : (∼ : Rel A ℓ) → Reflexive ∼ → Reflexive (∼ on f)
43 reflexive _ refl = refl
44
45 irreflexive : (≈ : Rel A ℓ₁) (∼ : Rel A ℓ₂) →
46 Irreflexive ≈ ∼ → Irreflexive (≈ on f) (∼ on f)
47 irreflexive _ _ irrefl = irrefl
48
49 symmetric : (∼ : Rel A ℓ) → Symmetric ∼ → Symmetric (∼ on f)
50 symmetric _ sym = sym
51
52 transitive : (∼ : Rel A ℓ) → Transitive ∼ → Transitive (∼ on f)
53 transitive _ trans = trans
54
55 antisymmetric : (≈ : Rel A ℓ₁) (≤ : Rel A ℓ₂) →
56 Antisymmetric ≈ ≤ → Antisymmetric (≈ on f) (≤ on f)
57 antisymmetric _ _ antisym = antisym
58
59 asymmetric : (< : Rel A ℓ) → Asymmetric < → Asymmetric (< on f)
60 asymmetric _ asym = asym
61
62 respects : (∼ : Rel A ℓ) (P : A → Set p) →
63 P Respects ∼ → (P ∘ f) Respects (∼ on f)
64 respects _ _ resp = resp
65
66 respects₂ : (∼₁ : Rel A ℓ₁) (∼₂ : Rel A ℓ₂) →
67 ∼₁ Respects₂ ∼₂ → (∼₁ on f) Respects₂ (∼₂ on f)
68 respects₂ _ _ (resp₁ , resp₂) = (resp₁ , resp₂)
69
70 decidable : (∼ : Rel A ℓ) → Decidable ∼ → Decidable (∼ on f)
71 decidable _ dec x y = dec (f x) (f y)
72
73 total : (∼ : Rel A ℓ) → Total ∼ → Total (∼ on f)
74 total _ tot x y = tot (f x) (f y)
75
76 trichotomous : (≈ : Rel A ℓ₁) (< : Rel A ℓ₂) →
77 Trichotomous ≈ < → Trichotomous (≈ on f) (< on f)
78 trichotomous _ _ compare x y = compare (f x) (f y)
79
80 accessible : ∀ {∼ : Rel A ℓ} {x} → Acc ∼ (f x) → Acc (∼ on f) x
81 accessible (acc rs) = acc (λ fy<fx → accessible (rs fy<fx))
82
83 wellFounded : {∼ : Rel A ℓ} → WellFounded ∼ → WellFounded (∼ on f)
84 wellFounded wf x = accessible (wf (f x))
85
86 ------------------------------------------------------------------------
87 -- Structures
88
89 module _ (f : B → A) {≈ : Rel A ℓ₁} where
90
91 isEquivalence : IsEquivalence ≈ →
92 IsEquivalence (≈ on f)
93 isEquivalence eq = record
94 { refl = reflexive f ≈ Eq.refl
95 ; sym = symmetric f ≈ Eq.sym
96 ; trans = transitive f ≈ Eq.trans
97 } where module Eq = IsEquivalence eq
98
99 isDecEquivalence : IsDecEquivalence ≈ →
100 IsDecEquivalence (≈ on f)
101 isDecEquivalence dec = record
102 { isEquivalence = isEquivalence Dec.isEquivalence
103 ; _≟_ = decidable f ≈ Dec._≟_
104 } where module Dec = IsDecEquivalence dec
105
106 module _ (f : B → A) {≈ : Rel A ℓ₁} {∼ : Rel A ℓ₂} where
107
108 isPreorder : IsPreorder ≈ ∼ → IsPreorder (≈ on f) (∼ on f)
109 isPreorder pre = record
110 { isEquivalence = isEquivalence f Pre.isEquivalence
111 ; reflexive = implies f ≈ ∼ Pre.reflexive
112 ; trans = transitive f ∼ Pre.trans
113 } where module Pre = IsPreorder pre
114
115 isPartialOrder : IsPartialOrder ≈ ∼ →
116 IsPartialOrder (≈ on f) (∼ on f)
117 isPartialOrder po = record
118 { isPreorder = isPreorder Po.isPreorder
119 ; antisym = antisymmetric f ≈ ∼ Po.antisym
120 } where module Po = IsPartialOrder po
121
122 isDecPartialOrder : IsDecPartialOrder ≈ ∼ →
123 IsDecPartialOrder (≈ on f) (∼ on f)
124 isDecPartialOrder dpo = record
125 { isPartialOrder = isPartialOrder DPO.isPartialOrder
126 ; _≟_ = decidable f _ DPO._≟_
127 ; _≤?_ = decidable f _ DPO._≤?_
128 } where module DPO = IsDecPartialOrder dpo
129
130 isStrictPartialOrder : IsStrictPartialOrder ≈ ∼ →
131 IsStrictPartialOrder (≈ on f) (∼ on f)
132 isStrictPartialOrder spo = record
133 { isEquivalence = isEquivalence f Spo.isEquivalence
134 ; irrefl = irreflexive f ≈ ∼ Spo.irrefl
135 ; trans = transitive f ∼ Spo.trans
136 ; <-resp-≈ = respects₂ f ∼ ≈ Spo.<-resp-≈
137 } where module Spo = IsStrictPartialOrder spo
138
139 isTotalOrder : IsTotalOrder ≈ ∼ →
140 IsTotalOrder (≈ on f) (∼ on f)
141 isTotalOrder to = record
142 { isPartialOrder = isPartialOrder To.isPartialOrder
143 ; total = total f ∼ To.total
144 } where module To = IsTotalOrder to
145
146 isDecTotalOrder : IsDecTotalOrder ≈ ∼ →
147 IsDecTotalOrder (≈ on f) (∼ on f)
148 isDecTotalOrder dec = record
149 { isTotalOrder = isTotalOrder Dec.isTotalOrder
150 ; _≟_ = decidable f ≈ Dec._≟_
151 ; _≤?_ = decidable f ∼ Dec._≤?_
152 } where module Dec = IsDecTotalOrder dec
153
154 isStrictTotalOrder : IsStrictTotalOrder ≈ ∼ →
155 IsStrictTotalOrder (≈ on f) (∼ on f)
156 isStrictTotalOrder sto = record
157 { isStrictPartialOrder = isStrictPartialOrder Sto.isStrictPartialOrder
158 ; compare = trichotomous _ _ _ Sto.compare
159 } where module Sto = IsStrictTotalOrder sto
160
161 ------------------------------------------------------------------------
162 -- Bundles
163
164 preorder : (P : Preorder a ℓ₁ ℓ₂) →
165 (B → Preorder.Carrier P) →
166 Preorder _ _ _
167 preorder P f = record
168 { isPreorder = isPreorder f (Preorder.isPreorder P)
169 }
170
171 setoid : (S : Setoid a ℓ) →
172 (B → Setoid.Carrier S) →
173 Setoid _ _
174 setoid S f = record
175 { isEquivalence = isEquivalence f (Setoid.isEquivalence S)
176 }
177
178 decSetoid : (D : DecSetoid a ℓ) →
179 (B → DecSetoid.Carrier D) →
180 DecSetoid _ _
181 decSetoid D f = record
182 { isDecEquivalence = isDecEquivalence f (DecSetoid.isDecEquivalence D)
183 }
184
185 poset : ∀ (P : Poset a ℓ₁ ℓ₂) →
186 (B → Poset.Carrier P) →
187 Poset _ _ _
188 poset P f = record
189 { isPartialOrder = isPartialOrder f (Poset.isPartialOrder P)
190 }
191
192 decPoset : (D : DecPoset a ℓ₁ ℓ₂) →
193 (B → DecPoset.Carrier D) →
194 DecPoset _ _ _
195 decPoset D f = record
196 { isDecPartialOrder =
197 isDecPartialOrder f (DecPoset.isDecPartialOrder D)
198 }
199
200 strictPartialOrder : (S : StrictPartialOrder a ℓ₁ ℓ₂) →
201 (B → StrictPartialOrder.Carrier S) →
202 StrictPartialOrder _ _ _
203 strictPartialOrder S f = record
204 { isStrictPartialOrder =
205 isStrictPartialOrder f (StrictPartialOrder.isStrictPartialOrder S)
206 }
207
208 totalOrder : (T : TotalOrder a ℓ₁ ℓ₂) →
209 (B → TotalOrder.Carrier T) →
210 TotalOrder _ _ _
211 totalOrder T f = record
212 { isTotalOrder = isTotalOrder f (TotalOrder.isTotalOrder T)
213 }
214
215 decTotalOrder : (D : DecTotalOrder a ℓ₁ ℓ₂) →
216 (B → DecTotalOrder.Carrier D) →
217 DecTotalOrder _ _ _
218 decTotalOrder D f = record
219 { isDecTotalOrder = isDecTotalOrder f (DecTotalOrder.isDecTotalOrder D)
220 }
221
222 strictTotalOrder : (S : StrictTotalOrder a ℓ₁ ℓ₂) →
223 (B → StrictTotalOrder.Carrier S) →
224 StrictTotalOrder _ _ _
225 strictTotalOrder S f = record
226 { isStrictTotalOrder =
227 isStrictTotalOrder f (StrictTotalOrder.isStrictTotalOrder S)
228 }