Debian Codegraph @

src/Data/List/Relation/Binary/Lex/Strict.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Lexicographic ordering of lists
5 ------------------------------------------------------------------------
6
7 -- The definitions of lexicographic ordering used here are suitable if
8 -- the argument order is a strict partial order.
9
10 {-# OPTIONS --cubical-compatible --safe #-}
11
12 module Data.List.Relation.Binary.Lex.Strict where
13
14 open import Function.Base using (_∘_; id)
15 open import Data.Product.Base using (_,_)
16 open import Data.Sum.Base using (inj₁; inj₂)
17 open import Data.List.Base using (List; []; _∷_)
18 open import Level using (_⊔_)
19 open import Relation.Nullary using (yes; no; ¬_)
20 open import Relation.Binary.Core using (Rel; _⇒_)
21 open import Relation.Binary.Bundles
22 using (StrictPartialOrder; StrictTotalOrder; Preorder; Poset; DecPoset; DecTotalOrder)
23 open import Relation.Binary.Structures
24 using (IsEquivalence; IsStrictPartialOrder; IsStrictTotalOrder; IsPreorder; IsPartialOrder; IsDecPartialOrder; IsTotalOrder; IsDecTotalOrder)
25 open import Relation.Binary.Definitions
26 using (Irreflexive; Symmetric; _Respects₂_; Total; Asymmetric; Antisymmetric; Transitive; Trichotomous; Decidable; tri≈; tri<; tri>)
27 open import Relation.Binary.Consequences
28 open import Data.List.Relation.Binary.Pointwise as Pointwise
29 using (Pointwise; []; _∷_; head; tail)
30
31 import Data.List.Relation.Binary.Lex as Core
32
33 ------------------------------------------------------------------------
34 -- Re-exporting core definitions
35
36 open Core public
37 using (Lex-<; Lex-≤; base; halt; this; next; ¬≤-this; ¬≤-next)
38
39 ------------------------------------------------------------------------
40 -- Strict lexicographic ordering.
41
42 module _ {a ℓ₁ ℓ₂} {A : Set a} where
43
44 -- Properties
45
46 module _ {_≈_ : Rel A ℓ₁} {_≺_ : Rel A ℓ₂} where
47
48 private
49 _≋_ = Pointwise _≈_
50 _<_ = Lex-< _≈_ _≺_
51
52 xs≮[] : ∀ {xs} → ¬ xs < []
53 xs≮[] (base ())
54
55 ¬[]<[] : ¬ [] < []
56 ¬[]<[] = xs≮[]
57
58 <-irreflexive : Irreflexive _≈_ _≺_ → Irreflexive _≋_ _<_
59 <-irreflexive irr (x≈y ∷ xs≋ys) (this x<y) = irr x≈y x<y
60 <-irreflexive irr (x≈y ∷ xs≋ys) (next _ xs⊴ys) =
61 <-irreflexive irr xs≋ys xs⊴ys
62
63 <-asymmetric : Symmetric _≈_ → _≺_ Respects₂ _≈_ → Asymmetric _≺_ →
64 Asymmetric _<_
65 <-asymmetric sym resp as = asym
66 where
67 irrefl : Irreflexive _≈_ _≺_
68 irrefl = asym⇒irr resp sym as
69
70 asym : Asymmetric _<_
71 asym (base bot) _ = bot
72 asym (this x<y) (this y<x) = as x<y y<x
73 asym (this x<y) (next y≈x ys⊴xs) = irrefl (sym y≈x) x<y
74 asym (next x≈y xs⊴ys) (this y<x) = irrefl (sym x≈y) y<x
75 asym (next x≈y xs⊴ys) (next y≈x ys⊴xs) = asym xs⊴ys ys⊴xs
76
77 <-antisymmetric : Symmetric _≈_ → Irreflexive _≈_ _≺_ →
78 Asymmetric _≺_ → Antisymmetric _≋_ _<_
79 <-antisymmetric = Core.antisymmetric
80
81 <-transitive : IsEquivalence _≈_ → _≺_ Respects₂ _≈_ →
82 Transitive _≺_ → Transitive _<_
83 <-transitive = Core.transitive
84
85 <-compare : Symmetric _≈_ → Trichotomous _≈_ _≺_ →
86 Trichotomous _≋_ _<_
87 <-compare sym tri [] [] = tri≈ ¬[]<[] [] ¬[]<[]
88 <-compare sym tri [] (y ∷ ys) = tri< halt (λ()) (λ())
89 <-compare sym tri (x ∷ xs) [] = tri> (λ()) (λ()) halt
90 <-compare sym tri (x ∷ xs) (y ∷ ys) with tri x y
91 ... | tri< x<y x≉y y≮x =
92 tri< (this x<y) (x≉y ∘ head) (¬≤-this (x≉y ∘ sym) y≮x)
93 ... | tri> x≮y x≉y y<x =
94 tri> (¬≤-this x≉y x≮y) (x≉y ∘ head) (this y<x)
95 ... | tri≈ x≮y x≈y y≮x with <-compare sym tri xs ys
96 ... | tri< xs<ys xs≉ys ys≮xs =
97 tri< (next x≈y xs<ys) (xs≉ys ∘ tail) (¬≤-next y≮x ys≮xs)
98 ... | tri≈ xs≮ys xs≈ys ys≮xs =
99 tri≈ (¬≤-next x≮y xs≮ys) (x≈y ∷ xs≈ys) (¬≤-next y≮x ys≮xs)
100 ... | tri> xs≮ys xs≉ys ys<xs =
101 tri> (¬≤-next x≮y xs≮ys) (xs≉ys ∘ tail) (next (sym x≈y) ys<xs)
102
103 <-decidable : Decidable _≈_ → Decidable _≺_ → Decidable _<_
104 <-decidable = Core.decidable (no id)
105
106 <-respects₂ : IsEquivalence _≈_ → _≺_ Respects₂ _≈_ →
107 _<_ Respects₂ _≋_
108 <-respects₂ = Core.respects₂
109
110 <-isStrictPartialOrder : IsStrictPartialOrder _≈_ _≺_ →
111 IsStrictPartialOrder _≋_ _<_
112 <-isStrictPartialOrder spo = record
113 { isEquivalence = Pointwise.isEquivalence isEquivalence
114 ; irrefl = <-irreflexive irrefl
115 ; trans = Core.transitive isEquivalence <-resp-≈ trans
116 ; <-resp-≈ = Core.respects₂ isEquivalence <-resp-≈
117 } where open IsStrictPartialOrder spo
118
119 <-isStrictTotalOrder : IsStrictTotalOrder _≈_ _≺_ →
120 IsStrictTotalOrder _≋_ _<_
121 <-isStrictTotalOrder sto = record
122 { isStrictPartialOrder = <-isStrictPartialOrder isStrictPartialOrder
123 ; compare = <-compare Eq.sym compare
124 } where open IsStrictTotalOrder sto
125
126 <-strictPartialOrder : ∀ {a ℓ₁ ℓ₂} → StrictPartialOrder a ℓ₁ ℓ₂ →
127 StrictPartialOrder _ _ _
128 <-strictPartialOrder spo = record
129 { isStrictPartialOrder = <-isStrictPartialOrder isStrictPartialOrder
130 } where open StrictPartialOrder spo
131
132 <-strictTotalOrder : ∀ {a ℓ₁ ℓ₂} → StrictTotalOrder a ℓ₁ ℓ₂ →
133 StrictTotalOrder _ _ _
134 <-strictTotalOrder sto = record
135 { isStrictTotalOrder = <-isStrictTotalOrder isStrictTotalOrder
136 } where open StrictTotalOrder sto
137
138 ------------------------------------------------------------------------
139 -- Non-strict lexicographic ordering.
140
141 module _ {a ℓ₁ ℓ₂} {A : Set a} where
142
143 -- Properties
144
145 ≤-reflexive : (_≈_ : Rel A ℓ₁) (_≺_ : Rel A ℓ₂) →
146 Pointwise _≈_ ⇒ Lex-≤ _≈_ _≺_
147 ≤-reflexive _≈_ _≺_ [] = base _
148 ≤-reflexive _≈_ _≺_ (x≈y ∷ xs≈ys) =
149 next x≈y (≤-reflexive _≈_ _≺_ xs≈ys)
150
151 module _ {_≈_ : Rel A ℓ₁} {_≺_ : Rel A ℓ₂} where
152
153 private
154 _≋_ = Pointwise _≈_
155 _≤_ = Lex-≤ _≈_ _≺_
156
157 ≤-antisymmetric : Symmetric _≈_ → Irreflexive _≈_ _≺_ →
158 Asymmetric _≺_ → Antisymmetric _≋_ _≤_
159 ≤-antisymmetric = Core.antisymmetric
160
161 ≤-transitive : IsEquivalence _≈_ → _≺_ Respects₂ _≈_ →
162 Transitive _≺_ → Transitive _≤_
163 ≤-transitive = Core.transitive
164
165 -- Note that trichotomy is an unnecessarily strong precondition for
166 -- the following lemma.
167
168 ≤-total : Symmetric _≈_ → Trichotomous _≈_ _≺_ → Total _≤_
169 ≤-total _ _ [] [] = inj₁ (base _)
170 ≤-total _ _ [] (x ∷ xs) = inj₁ halt
171 ≤-total _ _ (x ∷ xs) [] = inj₂ halt
172 ≤-total sym tri (x ∷ xs) (y ∷ ys) with tri x y
173 ... | tri< x<y _ _ = inj₁ (this x<y)
174 ... | tri> _ _ y<x = inj₂ (this y<x)
175 ... | tri≈ _ x≈y _ with ≤-total sym tri xs ys
176 ... | inj₁ xs≲ys = inj₁ (next x≈y xs≲ys)
177 ... | inj₂ ys≲xs = inj₂ (next (sym x≈y) ys≲xs)
178
179 ≤-decidable : Decidable _≈_ → Decidable _≺_ → Decidable _≤_
180 ≤-decidable = Core.decidable (yes _)
181
182 ≤-respects₂ : IsEquivalence _≈_ → _≺_ Respects₂ _≈_ →
183 _≤_ Respects₂ _≋_
184 ≤-respects₂ = Core.respects₂
185
186 ≤-isPreorder : IsEquivalence _≈_ → Transitive _≺_ →
187 _≺_ Respects₂ _≈_ → IsPreorder _≋_ _≤_
188 ≤-isPreorder eq tr resp = record
189 { isEquivalence = Pointwise.isEquivalence eq
190 ; reflexive = ≤-reflexive _≈_ _≺_
191 ; trans = Core.transitive eq resp tr
192 }
193
194 ≤-isPartialOrder : IsStrictPartialOrder _≈_ _≺_ →
195 IsPartialOrder _≋_ _≤_
196 ≤-isPartialOrder spo = record
197 { isPreorder = ≤-isPreorder isEquivalence trans <-resp-≈
198 ; antisym = Core.antisymmetric Eq.sym irrefl asym
199 }
200 where open IsStrictPartialOrder spo
201
202 ≤-isDecPartialOrder : IsStrictTotalOrder _≈_ _≺_ →
203 IsDecPartialOrder _≋_ _≤_
204 ≤-isDecPartialOrder sto = record
205 { isPartialOrder = ≤-isPartialOrder isStrictPartialOrder
206 ; _≟_ = Pointwise.decidable _≟_
207 ; _≤?_ = ≤-decidable _≟_ _<?_
208 } where open IsStrictTotalOrder sto
209
210 ≤-isTotalOrder : IsStrictTotalOrder _≈_ _≺_ → IsTotalOrder _≋_ _≤_
211 ≤-isTotalOrder sto = record
212 { isPartialOrder = ≤-isPartialOrder isStrictPartialOrder
213 ; total = ≤-total Eq.sym compare
214 }
215 where open IsStrictTotalOrder sto
216
217 ≤-isDecTotalOrder : IsStrictTotalOrder _≈_ _≺_ →
218 IsDecTotalOrder _≋_ _≤_
219 ≤-isDecTotalOrder sto = record
220 { isTotalOrder = ≤-isTotalOrder sto
221 ; _≟_ = Pointwise.decidable _≟_
222 ; _≤?_ = ≤-decidable _≟_ _<?_
223 }
224 where open IsStrictTotalOrder sto
225
226 ≤-preorder : ∀ {a ℓ₁ ℓ₂} → Preorder a ℓ₁ ℓ₂ → Preorder _ _ _
227 ≤-preorder pre = record
228 { isPreorder = ≤-isPreorder isEquivalence trans ∼-resp-≈
229 } where open Preorder pre
230
231 ≤-partialOrder : ∀ {a ℓ₁ ℓ₂} → StrictPartialOrder a ℓ₁ ℓ₂ → Poset _ _ _
232 ≤-partialOrder spo = record
233 { isPartialOrder = ≤-isPartialOrder isStrictPartialOrder
234 } where open StrictPartialOrder spo
235
236 ≤-decPoset : ∀ {a ℓ₁ ℓ₂} → StrictTotalOrder a ℓ₁ ℓ₂ →
237 DecPoset _ _ _
238 ≤-decPoset sto = record
239 { isDecPartialOrder = ≤-isDecPartialOrder isStrictTotalOrder
240 } where open StrictTotalOrder sto
241
242
243 ≤-decTotalOrder : ∀ {a ℓ₁ ℓ₂} → StrictTotalOrder a ℓ₁ ℓ₂ →
244 DecTotalOrder _ _ _
245 ≤-decTotalOrder sto = record
246 { isDecTotalOrder = ≤-isDecTotalOrder isStrictTotalOrder
247 } where open StrictTotalOrder sto