431
|
1 open import Level
|
|
2 open import Ordinals
|
|
3 module OrdUtil {n : Level} (O : Ordinals {n} ) where
|
|
4
|
|
5 open import logic
|
|
6 open import nat
|
|
7 open import Data.Nat renaming ( zero to Zero ; suc to Suc ; ℕ to Nat ; _⊔_ to _n⊔_ )
|
|
8 open import Data.Empty
|
|
9 open import Relation.Binary.PropositionalEquality
|
|
10 open import Relation.Nullary
|
|
11 open import Relation.Binary hiding (_⇔_)
|
|
12
|
|
13 open Ordinals.Ordinals O
|
|
14 open Ordinals.IsOrdinals isOrdinal
|
|
15 open Ordinals.IsNext isNext
|
|
16
|
|
17 o<-dom : { x y : Ordinal } → x o< y → Ordinal
|
|
18 o<-dom {x} _ = x
|
|
19
|
|
20 o<-cod : { x y : Ordinal } → x o< y → Ordinal
|
|
21 o<-cod {_} {y} _ = y
|
|
22
|
|
23 o<-subst : {Z X z x : Ordinal } → Z o< X → Z ≡ z → X ≡ x → z o< x
|
|
24 o<-subst df refl refl = df
|
|
25
|
|
26 o<¬≡ : { ox oy : Ordinal } → ox ≡ oy → ox o< oy → ⊥
|
|
27 o<¬≡ {ox} {oy} eq lt with trio< ox oy
|
|
28 o<¬≡ {ox} {oy} eq lt | tri< a ¬b ¬c = ¬b eq
|
|
29 o<¬≡ {ox} {oy} eq lt | tri≈ ¬a b ¬c = ¬a lt
|
|
30 o<¬≡ {ox} {oy} eq lt | tri> ¬a ¬b c = ¬b eq
|
|
31
|
|
32 o<> : {x y : Ordinal } → y o< x → x o< y → ⊥
|
|
33 o<> {ox} {oy} lt tl with trio< ox oy
|
|
34 o<> {ox} {oy} lt tl | tri< a ¬b ¬c = ¬c lt
|
|
35 o<> {ox} {oy} lt tl | tri≈ ¬a b ¬c = ¬a tl
|
|
36 o<> {ox} {oy} lt tl | tri> ¬a ¬b c = ¬a tl
|
|
37
|
|
38 osuc-< : { x y : Ordinal } → y o< osuc x → x o< y → ⊥
|
|
39 osuc-< {x} {y} y<ox x<y with osuc-≡< y<ox
|
|
40 osuc-< {x} {y} y<ox x<y | case1 refl = o<¬≡ refl x<y
|
|
41 osuc-< {x} {y} y<ox x<y | case2 y<x = o<> x<y y<x
|
|
42
|
|
43 osucc : {ox oy : Ordinal } → oy o< ox → osuc oy o< osuc ox
|
|
44 ---- y < osuc y < x < osuc x
|
|
45 ---- y < osuc y = x < osuc x
|
|
46 ---- y < osuc y > x < osuc x -> y = x ∨ x < y → ⊥
|
|
47 osucc {ox} {oy} oy<ox with trio< (osuc oy) ox
|
|
48 osucc {ox} {oy} oy<ox | tri< a ¬b ¬c = ordtrans a <-osuc
|
|
49 osucc {ox} {oy} oy<ox | tri≈ ¬a refl ¬c = <-osuc
|
|
50 osucc {ox} {oy} oy<ox | tri> ¬a ¬b c with osuc-≡< c
|
|
51 osucc {ox} {oy} oy<ox | tri> ¬a ¬b c | case1 eq = ⊥-elim (o<¬≡ (sym eq) oy<ox)
|
|
52 osucc {ox} {oy} oy<ox | tri> ¬a ¬b c | case2 lt = ⊥-elim (o<> lt oy<ox)
|
|
53
|
|
54 osucprev : {ox oy : Ordinal } → osuc oy o< osuc ox → oy o< ox
|
|
55 osucprev {ox} {oy} oy<ox with trio< oy ox
|
|
56 osucprev {ox} {oy} oy<ox | tri< a ¬b ¬c = a
|
|
57 osucprev {ox} {oy} oy<ox | tri≈ ¬a b ¬c = ⊥-elim (o<¬≡ (cong (λ k → osuc k) b) oy<ox )
|
|
58 osucprev {ox} {oy} oy<ox | tri> ¬a ¬b c = ⊥-elim (o<> (osucc c) oy<ox )
|
|
59
|
|
60 open _∧_
|
|
61
|
|
62 osuc2 : ( x y : Ordinal ) → ( osuc x o< osuc (osuc y )) ⇔ (x o< osuc y)
|
|
63 proj2 (osuc2 x y) lt = osucc lt
|
|
64 proj1 (osuc2 x y) ox<ooy with osuc-≡< ox<ooy
|
|
65 proj1 (osuc2 x y) ox<ooy | case1 ox=oy = o<-subst <-osuc refl ox=oy
|
|
66 proj1 (osuc2 x y) ox<ooy | case2 ox<oy = ordtrans <-osuc ox<oy
|
|
67
|
538
|
68 o≡? : (x y : Ordinal) → Dec ( x ≡ y )
|
|
69 o≡? x y with trio< x y
|
|
70 ... | tri< a ¬b ¬c = no ¬b
|
|
71 ... | tri≈ ¬a b ¬c = yes b
|
|
72 ... | tri> ¬a ¬b c = no ¬b
|
|
73
|
431
|
74 _o≤_ : Ordinal → Ordinal → Set n
|
|
75 a o≤ b = a o< osuc b -- (a ≡ b) ∨ ( a o< b )
|
|
76
|
|
77
|
|
78 xo<ab : {oa ob : Ordinal } → ( {ox : Ordinal } → ox o< oa → ox o< ob ) → oa o< osuc ob
|
|
79 xo<ab {oa} {ob} a→b with trio< oa ob
|
|
80 xo<ab {oa} {ob} a→b | tri< a ¬b ¬c = ordtrans a <-osuc
|
|
81 xo<ab {oa} {ob} a→b | tri≈ ¬a refl ¬c = <-osuc
|
|
82 xo<ab {oa} {ob} a→b | tri> ¬a ¬b c = ⊥-elim ( o<¬≡ refl (a→b c ) )
|
|
83
|
|
84 maxα : Ordinal → Ordinal → Ordinal
|
|
85 maxα x y with trio< x y
|
|
86 maxα x y | tri< a ¬b ¬c = y
|
|
87 maxα x y | tri> ¬a ¬b c = x
|
|
88 maxα x y | tri≈ ¬a refl ¬c = x
|
|
89
|
|
90 omin : Ordinal → Ordinal → Ordinal
|
|
91 omin x y with trio< x y
|
|
92 omin x y | tri< a ¬b ¬c = x
|
|
93 omin x y | tri> ¬a ¬b c = y
|
|
94 omin x y | tri≈ ¬a refl ¬c = x
|
|
95
|
|
96 min1 : {x y z : Ordinal } → z o< x → z o< y → z o< omin x y
|
|
97 min1 {x} {y} {z} z<x z<y with trio< x y
|
|
98 min1 {x} {y} {z} z<x z<y | tri< a ¬b ¬c = z<x
|
|
99 min1 {x} {y} {z} z<x z<y | tri≈ ¬a refl ¬c = z<x
|
|
100 min1 {x} {y} {z} z<x z<y | tri> ¬a ¬b c = z<y
|
|
101
|
|
102 --
|
|
103 -- max ( osuc x , osuc y )
|
|
104 --
|
|
105
|
|
106 omax : ( x y : Ordinal ) → Ordinal
|
|
107 omax x y with trio< x y
|
|
108 omax x y | tri< a ¬b ¬c = osuc y
|
|
109 omax x y | tri> ¬a ¬b c = osuc x
|
|
110 omax x y | tri≈ ¬a refl ¬c = osuc x
|
|
111
|
|
112 omax< : ( x y : Ordinal ) → x o< y → osuc y ≡ omax x y
|
|
113 omax< x y lt with trio< x y
|
|
114 omax< x y lt | tri< a ¬b ¬c = refl
|
|
115 omax< x y lt | tri≈ ¬a b ¬c = ⊥-elim (¬a lt )
|
|
116 omax< x y lt | tri> ¬a ¬b c = ⊥-elim (¬a lt )
|
|
117
|
|
118 omax≤ : ( x y : Ordinal ) → x o≤ y → osuc y ≡ omax x y
|
|
119 omax≤ x y le with trio< x y
|
|
120 omax≤ x y le | tri< a ¬b ¬c = refl
|
|
121 omax≤ x y le | tri≈ ¬a refl ¬c = refl
|
|
122 omax≤ x y le | tri> ¬a ¬b c with osuc-≡< le
|
|
123 omax≤ x y le | tri> ¬a ¬b c | case1 eq = ⊥-elim (¬b eq)
|
|
124 omax≤ x y le | tri> ¬a ¬b c | case2 x<y = ⊥-elim (¬a x<y)
|
|
125
|
|
126 omax≡ : ( x y : Ordinal ) → x ≡ y → osuc y ≡ omax x y
|
|
127 omax≡ x y eq with trio< x y
|
|
128 omax≡ x y eq | tri< a ¬b ¬c = ⊥-elim (¬b eq )
|
|
129 omax≡ x y eq | tri≈ ¬a refl ¬c = refl
|
|
130 omax≡ x y eq | tri> ¬a ¬b c = ⊥-elim (¬b eq )
|
|
131
|
|
132 omax-x : ( x y : Ordinal ) → x o< omax x y
|
|
133 omax-x x y with trio< x y
|
|
134 omax-x x y | tri< a ¬b ¬c = ordtrans a <-osuc
|
|
135 omax-x x y | tri> ¬a ¬b c = <-osuc
|
|
136 omax-x x y | tri≈ ¬a refl ¬c = <-osuc
|
|
137
|
|
138 omax-y : ( x y : Ordinal ) → y o< omax x y
|
|
139 omax-y x y with trio< x y
|
|
140 omax-y x y | tri< a ¬b ¬c = <-osuc
|
|
141 omax-y x y | tri> ¬a ¬b c = ordtrans c <-osuc
|
|
142 omax-y x y | tri≈ ¬a refl ¬c = <-osuc
|
|
143
|
|
144 omxx : ( x : Ordinal ) → omax x x ≡ osuc x
|
|
145 omxx x with trio< x x
|
|
146 omxx x | tri< a ¬b ¬c = ⊥-elim (¬b refl )
|
|
147 omxx x | tri> ¬a ¬b c = ⊥-elim (¬b refl )
|
|
148 omxx x | tri≈ ¬a refl ¬c = refl
|
|
149
|
|
150 omxxx : ( x : Ordinal ) → omax x (omax x x ) ≡ osuc (osuc x)
|
|
151 omxxx x = trans ( cong ( λ k → omax x k ) (omxx x )) (sym ( omax< x (osuc x) <-osuc ))
|
|
152
|
|
153 open _∧_
|
|
154
|
|
155 o≤-refl : { i j : Ordinal } → i ≡ j → i o≤ j
|
|
156 o≤-refl {i} {j} eq = subst (λ k → i o< osuc k ) eq <-osuc
|
|
157 OrdTrans : Transitive _o≤_
|
|
158 OrdTrans a≤b b≤c with osuc-≡< a≤b | osuc-≡< b≤c
|
|
159 OrdTrans a≤b b≤c | case1 refl | case1 refl = <-osuc
|
|
160 OrdTrans a≤b b≤c | case1 refl | case2 a≤c = ordtrans a≤c <-osuc
|
|
161 OrdTrans a≤b b≤c | case2 a≤c | case1 refl = ordtrans a≤c <-osuc
|
|
162 OrdTrans a≤b b≤c | case2 a<b | case2 b<c = ordtrans (ordtrans a<b b<c) <-osuc
|
|
163
|
|
164 OrdPreorder : Preorder n n n
|
|
165 OrdPreorder = record { Carrier = Ordinal
|
|
166 ; _≈_ = _≡_
|
|
167 ; _∼_ = _o≤_
|
|
168 ; isPreorder = record {
|
|
169 isEquivalence = record { refl = refl ; sym = sym ; trans = trans }
|
|
170 ; reflexive = o≤-refl
|
|
171 ; trans = OrdTrans
|
|
172 }
|
|
173 }
|
|
174
|
|
175 FExists : {m l : Level} → ( ψ : Ordinal → Set m )
|
|
176 → {p : Set l} ( P : { y : Ordinal } → ψ y → ¬ p )
|
|
177 → (exists : ¬ (∀ y → ¬ ( ψ y ) ))
|
|
178 → ¬ p
|
|
179 FExists {m} {l} ψ {p} P = contra-position ( λ p y ψy → P {y} ψy p )
|
|
180
|
|
181 nexto∅ : {x : Ordinal} → o∅ o< next x
|
|
182 nexto∅ {x} with trio< o∅ x
|
|
183 nexto∅ {x} | tri< a ¬b ¬c = ordtrans a x<nx
|
|
184 nexto∅ {x} | tri≈ ¬a b ¬c = subst (λ k → k o< next x) (sym b) x<nx
|
|
185 nexto∅ {x} | tri> ¬a ¬b c = ⊥-elim ( ¬x<0 c )
|
|
186
|
|
187 next< : {x y z : Ordinal} → x o< next z → y o< next x → y o< next z
|
|
188 next< {x} {y} {z} x<nz y<nx with trio< y (next z)
|
|
189 next< {x} {y} {z} x<nz y<nx | tri< a ¬b ¬c = a
|
|
190 next< {x} {y} {z} x<nz y<nx | tri≈ ¬a b ¬c = ⊥-elim (¬nx<nx x<nz (subst (λ k → k o< next x) b y<nx)
|
|
191 (λ w nz=ow → o<¬≡ nz=ow (subst₂ (λ j k → j o< k ) (sym nz=ow) nz=ow (osuc<nx (subst (λ k → w o< k ) (sym nz=ow) <-osuc) ))))
|
|
192 next< {x} {y} {z} x<nz y<nx | tri> ¬a ¬b c = ⊥-elim (¬nx<nx x<nz (ordtrans c y<nx )
|
|
193 (λ w nz=ow → o<¬≡ (sym nz=ow) (osuc<nx (subst (λ k → w o< k ) (sym nz=ow) <-osuc ))))
|
|
194
|
|
195 osuc< : {x y : Ordinal} → osuc x ≡ y → x o< y
|
|
196 osuc< {x} {y} refl = <-osuc
|
|
197
|
|
198 nexto=n : {x y : Ordinal} → x o< next (osuc y) → x o< next y
|
|
199 nexto=n {x} {y} x<noy = next< (osuc<nx x<nx) x<noy
|
|
200
|
|
201 nexto≡ : {x : Ordinal} → next x ≡ next (osuc x)
|
|
202 nexto≡ {x} with trio< (next x) (next (osuc x) )
|
|
203 -- next x o< next (osuc x ) -> osuc x o< next x o< next (osuc x) -> next x ≡ osuc z -> z o o< next x -> osuc z o< next x -> next x o< next x
|
|
204 nexto≡ {x} | tri< a ¬b ¬c = ⊥-elim (¬nx<nx (osuc<nx x<nx ) a
|
|
205 (λ z eq → o<¬≡ (sym eq) (osuc<nx (osuc< (sym eq)))))
|
|
206 nexto≡ {x} | tri≈ ¬a b ¬c = b
|
|
207 -- next (osuc x) o< next x -> osuc x o< next (osuc x) o< next x -> next (osuc x) ≡ osuc z -> z o o< next (osuc x) ...
|
|
208 nexto≡ {x} | tri> ¬a ¬b c = ⊥-elim (¬nx<nx (ordtrans <-osuc x<nx) c
|
|
209 (λ z eq → o<¬≡ (sym eq) (osuc<nx (osuc< (sym eq)))))
|
|
210
|
|
211 next-is-limit : {x y : Ordinal} → ¬ (next x ≡ osuc y)
|
|
212 next-is-limit {x} {y} eq = o<¬≡ (sym eq) (osuc<nx y<nx) where
|
|
213 y<nx : y o< next x
|
|
214 y<nx = osuc< (sym eq)
|
|
215
|
|
216 omax<next : {x y : Ordinal} → x o< y → omax x y o< next y
|
|
217 omax<next {x} {y} x<y = subst (λ k → k o< next y ) (omax< _ _ x<y ) (osuc<nx x<nx)
|
|
218
|
|
219 x<ny→≡next : {x y : Ordinal} → x o< y → y o< next x → next x ≡ next y
|
|
220 x<ny→≡next {x} {y} x<y y<nx with trio< (next x) (next y)
|
|
221 x<ny→≡next {x} {y} x<y y<nx | tri< a ¬b ¬c = -- x < y < next x < next y ∧ next x = osuc z
|
|
222 ⊥-elim ( ¬nx<nx y<nx a (λ z eq → o<¬≡ (sym eq) (osuc<nx (subst (λ k → z o< k ) (sym eq) <-osuc ))))
|
|
223 x<ny→≡next {x} {y} x<y y<nx | tri≈ ¬a b ¬c = b
|
|
224 x<ny→≡next {x} {y} x<y y<nx | tri> ¬a ¬b c = -- x < y < next y < next x
|
|
225 ⊥-elim ( ¬nx<nx (ordtrans x<y x<nx) c (λ z eq → o<¬≡ (sym eq) (osuc<nx (subst (λ k → z o< k ) (sym eq) <-osuc ))))
|
|
226
|
|
227 ≤next : {x y : Ordinal} → x o≤ y → next x o≤ next y
|
|
228 ≤next {x} {y} x≤y with trio< (next x) y
|
|
229 ≤next {x} {y} x≤y | tri< a ¬b ¬c = ordtrans a (ordtrans x<nx <-osuc )
|
|
230 ≤next {x} {y} x≤y | tri≈ ¬a refl ¬c = (ordtrans x<nx <-osuc )
|
|
231 ≤next {x} {y} x≤y | tri> ¬a ¬b c with osuc-≡< x≤y
|
|
232 ≤next {x} {y} x≤y | tri> ¬a ¬b c | case1 refl = o≤-refl refl -- x = y < next x
|
|
233 ≤next {x} {y} x≤y | tri> ¬a ¬b c | case2 x<y = o≤-refl (x<ny→≡next x<y c) -- x ≤ y < next x
|
|
234
|
|
235 x<ny→≤next : {x y : Ordinal} → x o< next y → next x o≤ next y
|
|
236 x<ny→≤next {x} {y} x<ny with trio< x y
|
|
237 x<ny→≤next {x} {y} x<ny | tri< a ¬b ¬c = ≤next (ordtrans a <-osuc )
|
|
238 x<ny→≤next {x} {y} x<ny | tri≈ ¬a refl ¬c = o≤-refl refl
|
|
239 x<ny→≤next {x} {y} x<ny | tri> ¬a ¬b c = o≤-refl (sym ( x<ny→≡next c x<ny ))
|
|
240
|
|
241 omax<nomax : {x y : Ordinal} → omax x y o< next (omax x y )
|
|
242 omax<nomax {x} {y} with trio< x y
|
|
243 omax<nomax {x} {y} | tri< a ¬b ¬c = subst (λ k → osuc y o< k ) nexto≡ (osuc<nx x<nx )
|
|
244 omax<nomax {x} {y} | tri≈ ¬a refl ¬c = subst (λ k → osuc x o< k ) nexto≡ (osuc<nx x<nx )
|
|
245 omax<nomax {x} {y} | tri> ¬a ¬b c = subst (λ k → osuc x o< k ) nexto≡ (osuc<nx x<nx )
|
|
246
|
|
247 omax<nx : {x y z : Ordinal} → x o< next z → y o< next z → omax x y o< next z
|
|
248 omax<nx {x} {y} {z} x<nz y<nz with trio< x y
|
|
249 omax<nx {x} {y} {z} x<nz y<nz | tri< a ¬b ¬c = osuc<nx y<nz
|
|
250 omax<nx {x} {y} {z} x<nz y<nz | tri≈ ¬a refl ¬c = osuc<nx y<nz
|
|
251 omax<nx {x} {y} {z} x<nz y<nz | tri> ¬a ¬b c = osuc<nx x<nz
|
|
252
|
|
253 nn<omax : {x nx ny : Ordinal} → x o< next nx → x o< next ny → x o< next (omax nx ny)
|
|
254 nn<omax {x} {nx} {ny} xnx xny with trio< nx ny
|
|
255 nn<omax {x} {nx} {ny} xnx xny | tri< a ¬b ¬c = subst (λ k → x o< k ) nexto≡ xny
|
|
256 nn<omax {x} {nx} {ny} xnx xny | tri≈ ¬a refl ¬c = subst (λ k → x o< k ) nexto≡ xny
|
|
257 nn<omax {x} {nx} {ny} xnx xny | tri> ¬a ¬b c = subst (λ k → x o< k ) nexto≡ xnx
|
|
258
|
|
259 record OrdinalSubset (maxordinal : Ordinal) : Set (suc n) where
|
|
260 field
|
|
261 os→ : (x : Ordinal) → x o< maxordinal → Ordinal
|
|
262 os← : Ordinal → Ordinal
|
|
263 os←limit : (x : Ordinal) → os← x o< maxordinal
|
|
264 os-iso← : {x : Ordinal} → os→ (os← x) (os←limit x) ≡ x
|
|
265 os-iso→ : {x : Ordinal} → (lt : x o< maxordinal ) → os← (os→ x lt) ≡ x
|
|
266
|
|
267 module o≤-Reasoning {n : Level} (O : Ordinals {n} ) where
|
|
268
|
|
269 -- open inOrdinal O
|
|
270
|
|
271 resp-o< : _o<_ Respects₂ _≡_
|
|
272 resp-o< = resp₂ _o<_
|
|
273
|
|
274 trans1 : {i j k : Ordinal} → i o< j → j o< osuc k → i o< k
|
|
275 trans1 {i} {j} {k} i<j j<ok with osuc-≡< j<ok
|
|
276 trans1 {i} {j} {k} i<j j<ok | case1 refl = i<j
|
|
277 trans1 {i} {j} {k} i<j j<ok | case2 j<k = ordtrans i<j j<k
|
|
278
|
|
279 trans2 : {i j k : Ordinal} → i o< osuc j → j o< k → i o< k
|
|
280 trans2 {i} {j} {k} i<oj j<k with osuc-≡< i<oj
|
|
281 trans2 {i} {j} {k} i<oj j<k | case1 refl = j<k
|
|
282 trans2 {i} {j} {k} i<oj j<k | case2 i<j = ordtrans i<j j<k
|
|
283
|
|
284 open import Relation.Binary.Reasoning.Base.Triple
|
|
285 (Preorder.isPreorder OrdPreorder)
|
|
286 ordtrans --<-trans
|
|
287 (resp₂ _o<_) --(resp₂ _<_)
|
|
288 (λ x → ordtrans x <-osuc ) --<⇒≤
|
|
289 trans1 --<-transˡ
|
|
290 trans2 --<-transʳ
|
|
291 public
|
|
292 -- hiding (_≈⟨_⟩_)
|
|
293
|