163
|
1 {-# OPTIONS --allow-unsolved-metas #-}
|
|
2
|
|
3 module fin where
|
|
4
|
283
|
5 open import Data.Fin hiding (_<_ ; _≤_ ; _>_ ; _+_ )
|
284
|
6 open import Data.Fin.Properties hiding (≤-trans ; <-trans ; ≤-refl ) renaming ( <-cmp to <-fcmp )
|
163
|
7 open import Data.Nat
|
284
|
8 open import Data.Nat.Properties
|
163
|
9 open import logic
|
|
10 open import nat
|
|
11 open import Relation.Binary.PropositionalEquality
|
|
12
|
|
13
|
|
14 -- toℕ<n
|
|
15 fin<n : {n : ℕ} {f : Fin n} → toℕ f < n
|
|
16 fin<n {_} {zero} = s≤s z≤n
|
|
17 fin<n {suc n} {suc f} = s≤s (fin<n {n} {f})
|
|
18
|
|
19 -- toℕ≤n
|
|
20 fin≤n : {n : ℕ} (f : Fin (suc n)) → toℕ f ≤ n
|
|
21 fin≤n {_} zero = z≤n
|
|
22 fin≤n {suc n} (suc f) = s≤s (fin≤n {n} f)
|
|
23
|
|
24 pred<n : {n : ℕ} {f : Fin (suc n)} → n > 0 → Data.Nat.pred (toℕ f) < n
|
|
25 pred<n {suc n} {zero} (s≤s z≤n) = s≤s z≤n
|
|
26 pred<n {suc n} {suc f} (s≤s z≤n) = fin<n
|
|
27
|
|
28 fin<asa : {n : ℕ} → toℕ (fromℕ< {n} a<sa) ≡ n
|
|
29 fin<asa = toℕ-fromℕ< nat.a<sa
|
|
30
|
|
31 -- fromℕ<-toℕ
|
|
32 toℕ→from : {n : ℕ} {x : Fin (suc n)} → toℕ x ≡ n → fromℕ n ≡ x
|
|
33 toℕ→from {0} {zero} refl = refl
|
|
34 toℕ→from {suc n} {suc x} eq = cong (λ k → suc k ) ( toℕ→from {n} {x} (cong (λ k → Data.Nat.pred k ) eq ))
|
|
35
|
|
36 0≤fmax : {n : ℕ } → (# 0) Data.Fin.≤ fromℕ< {n} a<sa
|
|
37 0≤fmax = subst (λ k → 0 ≤ k ) (sym (toℕ-fromℕ< a<sa)) z≤n
|
|
38
|
|
39 0<fmax : {n : ℕ } → (# 0) Data.Fin.< fromℕ< {suc n} a<sa
|
|
40 0<fmax = subst (λ k → 0 < k ) (sym (toℕ-fromℕ< a<sa)) (s≤s z≤n)
|
|
41
|
|
42 -- toℕ-injective
|
|
43 i=j : {n : ℕ} (i j : Fin n) → toℕ i ≡ toℕ j → i ≡ j
|
|
44 i=j {suc n} zero zero refl = refl
|
|
45 i=j {suc n} (suc i) (suc j) eq = cong ( λ k → suc k ) ( i=j i j (cong ( λ k → Data.Nat.pred k ) eq) )
|
|
46
|
|
47 -- raise 1
|
|
48 fin+1 : { n : ℕ } → Fin n → Fin (suc n)
|
|
49 fin+1 zero = zero
|
|
50 fin+1 (suc x) = suc (fin+1 x)
|
|
51
|
|
52 open import Data.Nat.Properties as NatP hiding ( _≟_ )
|
|
53
|
|
54 fin+1≤ : { i n : ℕ } → (a : i < n) → fin+1 (fromℕ< a) ≡ fromℕ< (<-trans a a<sa)
|
|
55 fin+1≤ {0} {suc i} (s≤s z≤n) = refl
|
|
56 fin+1≤ {suc n} {suc (suc i)} (s≤s (s≤s a)) = cong (λ k → suc k ) ( fin+1≤ {n} {suc i} (s≤s a) )
|
|
57
|
|
58 fin+1-toℕ : { n : ℕ } → { x : Fin n} → toℕ (fin+1 x) ≡ toℕ x
|
|
59 fin+1-toℕ {suc n} {zero} = refl
|
|
60 fin+1-toℕ {suc n} {suc x} = cong (λ k → suc k ) (fin+1-toℕ {n} {x})
|
|
61
|
|
62 open import Relation.Nullary
|
|
63 open import Data.Empty
|
|
64
|
|
65 fin-1 : { n : ℕ } → (x : Fin (suc n)) → ¬ (x ≡ zero ) → Fin n
|
|
66 fin-1 zero ne = ⊥-elim (ne refl )
|
|
67 fin-1 {n} (suc x) ne = x
|
|
68
|
|
69 fin-1-sx : { n : ℕ } → (x : Fin n) → fin-1 (suc x) (λ ()) ≡ x
|
|
70 fin-1-sx zero = refl
|
|
71 fin-1-sx (suc x) = refl
|
|
72
|
|
73 fin-1-xs : { n : ℕ } → (x : Fin (suc n)) → (ne : ¬ (x ≡ zero )) → suc (fin-1 x ne ) ≡ x
|
|
74 fin-1-xs zero ne = ⊥-elim ( ne refl )
|
|
75 fin-1-xs (suc x) ne = refl
|
|
76
|
|
77 -- suc-injective
|
|
78 -- suc-eq : {n : ℕ } {x y : Fin n} → Fin.suc x ≡ Fin.suc y → x ≡ y
|
|
79 -- suc-eq {n} {x} {y} eq = subst₂ (λ j k → j ≡ k ) {!!} {!!} (cong (λ k → Data.Fin.pred k ) eq )
|
|
80
|
|
81 -- this is refl
|
|
82 lemma3 : {a b : ℕ } → (lt : a < b ) → fromℕ< (s≤s lt) ≡ suc (fromℕ< lt)
|
|
83 lemma3 (s≤s lt) = refl
|
|
84
|
|
85 -- fromℕ<-toℕ
|
|
86 lemma12 : {n m : ℕ } → (n<m : n < m ) → (f : Fin m ) → toℕ f ≡ n → f ≡ fromℕ< n<m
|
|
87 lemma12 {zero} {suc m} (s≤s z≤n) zero refl = refl
|
|
88 lemma12 {suc n} {suc m} (s≤s n<m) (suc f) refl = cong suc ( lemma12 {n} {m} n<m f refl )
|
|
89
|
|
90 open import Relation.Binary.HeterogeneousEquality as HE using (_≅_ )
|
|
91
|
|
92 -- <-irrelevant
|
|
93 <-nat=irr : {i j n : ℕ } → ( i ≡ j ) → {i<n : i < n } → {j<n : j < n } → i<n ≅ j<n
|
|
94 <-nat=irr {zero} {zero} {suc n} refl {s≤s z≤n} {s≤s z≤n} = HE.refl
|
|
95 <-nat=irr {suc i} {suc i} {suc n} refl {s≤s i<n} {s≤s j<n} = HE.cong (λ k → s≤s k ) ( <-nat=irr {i} {i} {n} refl )
|
|
96
|
|
97 lemma8 : {i j n : ℕ } → ( i ≡ j ) → {i<n : i < n } → {j<n : j < n } → i<n ≅ j<n
|
|
98 lemma8 {zero} {zero} {suc n} refl {s≤s z≤n} {s≤s z≤n} = HE.refl
|
|
99 lemma8 {suc i} {suc i} {suc n} refl {s≤s i<n} {s≤s j<n} = HE.cong (λ k → s≤s k ) ( lemma8 {i} {i} {n} refl )
|
|
100
|
|
101 -- fromℕ<-irrelevant
|
|
102 lemma10 : {n i j : ℕ } → ( i ≡ j ) → {i<n : i < n } → {j<n : j < n } → fromℕ< i<n ≡ fromℕ< j<n
|
|
103 lemma10 {n} refl = HE.≅-to-≡ (HE.cong (λ k → fromℕ< k ) (lemma8 refl ))
|
|
104
|
|
105 lemma31 : {a b c : ℕ } → { a<b : a < b } { b<c : b < c } { a<c : a < c } → NatP.<-trans a<b b<c ≡ a<c
|
|
106 lemma31 {a} {b} {c} {a<b} {b<c} {a<c} = HE.≅-to-≡ (lemma8 refl)
|
|
107
|
|
108 -- toℕ-fromℕ<
|
|
109 lemma11 : {n m : ℕ } {x : Fin n } → (n<m : n < m ) → toℕ (fromℕ< (NatP.<-trans (toℕ<n x) n<m)) ≡ toℕ x
|
|
110 lemma11 {n} {m} {x} n<m = begin
|
|
111 toℕ (fromℕ< (NatP.<-trans (toℕ<n x) n<m))
|
|
112 ≡⟨ toℕ-fromℕ< _ ⟩
|
|
113 toℕ x
|
|
114 ∎ where
|
|
115 open ≡-Reasoning
|
|
116
|
284
|
117 x<y→fin-1 : {n : ℕ } → { x y : Fin (suc n)} → toℕ x < toℕ y → Fin n
|
|
118 x<y→fin-1 {n} {x} {y} lt = fromℕ< (≤-trans lt (fin≤n _ ))
|
|
119
|
|
120 x<y→fin-1-eq : {n : ℕ } → { x y : Fin (suc n)} → (lt : toℕ x < toℕ y ) → toℕ x ≡ toℕ (x<y→fin-1 lt )
|
|
121 x<y→fin-1-eq {n} {x} {y} lt = sym ( begin
|
|
122 toℕ (fromℕ< (≤-trans lt (fin≤n y)) ) ≡⟨ toℕ-fromℕ< _ ⟩
|
|
123 toℕ x ∎ ) where open ≡-Reasoning
|
|
124
|
283
|
125 open import Data.List
|
|
126 open import Relation.Binary.Definitions
|
|
127
|
|
128 fin-phase2 : { n : ℕ } (q : Fin n) (qs : List (Fin n) ) → Bool
|
|
129 fin-phase2 q [] = false
|
|
130 fin-phase2 q (x ∷ qs) with <-fcmp q x
|
|
131 ... | tri< a ¬b ¬c = fin-phase2 q qs
|
|
132 ... | tri≈ ¬a b ¬c = true
|
|
133 ... | tri> ¬a ¬b c = fin-phase2 q qs
|
|
134 fin-phase1 : { n : ℕ } (q : Fin n) (qs : List (Fin n) ) → Bool
|
|
135 fin-phase1 q [] = false
|
|
136 fin-phase1 q (x ∷ qs) with <-fcmp q x
|
|
137 ... | tri< a ¬b ¬c = fin-phase1 q qs
|
|
138 ... | tri≈ ¬a b ¬c = fin-phase2 q qs
|
|
139 ... | tri> ¬a ¬b c = fin-phase1 q qs
|
|
140
|
|
141 fin-dup-in-list : { n : ℕ} (q : Fin n) (qs : List (Fin n) ) → Bool
|
|
142 fin-dup-in-list {n} q qs = fin-phase1 q qs
|
|
143
|
|
144 record FDup-in-list (n : ℕ ) (qs : List (Fin n)) : Set where
|
|
145 field
|
|
146 dup : Fin n
|
|
147 is-dup : fin-dup-in-list dup qs ≡ true
|
|
148
|
|
149 list-less : {n : ℕ } → List (Fin (suc n)) → List (Fin n)
|
|
150 list-less [] = []
|
|
151 list-less {n} (i ∷ ls) with NatP.<-cmp (toℕ i) n
|
|
152 ... | tri< a ¬b ¬c = fromℕ< a ∷ list-less ls
|
|
153 ... | tri≈ ¬a b ¬c = list-less ls
|
|
154 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> (fin≤n i) c )
|
|
155
|
285
|
156 record NList (n : ℕ) (qs : List (Fin (suc n))) : Set where
|
283
|
157 field
|
|
158 ls : List (Fin n)
|
|
159 lseq : list-less qs ≡ ls
|
286
|
160 ls< : (length ls ≡ length qs) ∨ (suc (length ls) ≡ length qs)
|
284
|
161
|
283
|
162 fin-dup-in-list>n : {n : ℕ } → (qs : List (Fin n)) → (len> : length qs > n ) → FDup-in-list n qs
|
|
163 fin-dup-in-list>n {zero} [] ()
|
|
164 fin-dup-in-list>n {zero} (() ∷ qs) lt
|
|
165 fin-dup-in-list>n {suc n} qs lt = fdup-phase0 where
|
284
|
166 open import Level using ( Level )
|
287
|
167 fdup+1 : (qs : List (Fin (suc n))) (i : Fin n) → fin-dup-in-list i (list-less qs) ≡ true → FDup-in-list (suc n) qs
|
|
168 fdup+1 qs i p with fin-dup-in-list (fromℕ< a<sa ) qs | inspect (fin-dup-in-list (fromℕ< a<sa )) qs
|
|
169 ... | true | record {eq = eq } = record { dup = fromℕ< a<sa ; is-dup = eq }
|
|
170 ... | false | record {eq = ne } = f1-phase1 qs p where
|
|
171 f1-phase2 : (qs : List (Fin (suc n)) ) → fin-phase2 i (list-less qs) ≡ true → {!!}
|
|
172 f1-phase2 (x ∷ qs) p with <-fcmp (fin+1 i) x
|
|
173 f1-phase2 (x ∷ qs) p | tri< a ¬b ¬c with NatP.<-cmp (toℕ x) n
|
286
|
174 ... | tri< a₁ ¬b₁ ¬c₁ = f1-phase2 qs {!!}
|
287
|
175 ... | tri≈ ¬a b ¬c₁ = f1-phase2 qs {!!}
|
|
176 ... | tri> ¬a ¬b₁ c = {!!} -- ⊥-elim ( nat-<> fin<n c )
|
|
177 f1-phase2 (x ∷ qs) p | tri≈ ¬a b ¬c = refl
|
|
178 f1-phase2 (x ∷ qs) p | tri> ¬a ¬b c = f1-phase2 qs {!!}
|
|
179 f1-phase1 : (qs : List (Fin (suc n)) ) → fin-phase1 i (list-less qs) ≡ true → {!!}
|
283
|
180 f1-phase1 [] ()
|
|
181 f1-phase1 (x ∷ qs) p with <-fcmp (fin+1 i) x
|
|
182 ... | tri< a ¬b ¬c = f1-phase1 qs {!!}
|
|
183 ... | tri≈ ¬a b ¬c = f1-phase2 qs {!!}
|
|
184 ... | tri> ¬a ¬b c = f1-phase1 qs {!!}
|
285
|
185 fdup-phase2 : (qs : List (Fin (suc n)) )
|
|
186 → ( fin-phase2 (fromℕ< a<sa ) qs ≡ true ) ∨ NList n qs
|
286
|
187 fdup-phase2 [] = case2 record { ls = [] ; lseq = refl ; ls< = case1 refl }
|
285
|
188 fdup-phase2 (x ∷ qs) with <-fcmp (fromℕ< a<sa) x
|
284
|
189 ... | tri< a ¬b ¬c = ⊥-elim ( nat-≤> a (subst (λ k → toℕ x < suc k) (sym fin<asa) fin<n ))
|
285
|
190 fdup-phase2 (x ∷ qs) | tri≈ ¬a b ¬c = case1 refl
|
|
191 fdup-phase2 (x ∷ qs) | tri> ¬a ¬b c with fdup-phase2 qs
|
283
|
192 ... | case1 p = case1 p
|
286
|
193 ... | case2 nlist = case2 record { ls = x<y→fin-1 c ∷ NList.ls nlist ; lseq = fdup01 ; ls< = case1 {!!} } where
|
285
|
194 fdup01 : list-less (x ∷ qs) ≡ x<y→fin-1 c ∷ NList.ls nlist
|
|
195 fdup01 with NatP.<-cmp (toℕ x) n
|
|
196 ... | tri< a ¬b ¬c = begin
|
|
197 fromℕ< a ∷ list-less qs ≡⟨ cong₂ (λ j k → j ∷ k ) (lemma10 refl) (NList.lseq nlist) ⟩
|
|
198 fromℕ< (≤-trans c (fin≤n (fromℕ< a<sa))) ∷ NList.ls nlist ∎ where open ≡-Reasoning
|
|
199 ... | tri≈ ¬a b ¬c = ⊥-elim ( nat-≡< b (subst (λ k → toℕ x < k ) fin<asa c ))
|
|
200 ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> (fin≤n x) c )
|
|
201 fdup-phase1 : (qs : List (Fin (suc n)) ) → (fin-phase1 (fromℕ< a<sa) qs ≡ true) ∨ NList n qs
|
286
|
202 fdup-phase1 [] = case2 record { ls = [] ; lseq = refl ; ls< = case1 refl }
|
285
|
203 fdup-phase1 (x ∷ qs) with <-fcmp (fromℕ< a<sa) x
|
|
204 fdup-phase1 (x ∷ qs) | tri< a ¬b ¬c = ⊥-elim ( nat-≤> a (subst (λ k → toℕ x < suc k) (sym fin<asa) fin<n ))
|
|
205 fdup-phase1 (x ∷ qs) | tri≈ ¬a b ¬c with fdup-phase2 qs
|
283
|
206 ... | case1 p = case1 p
|
286
|
207 ... | case2 nlist = case2 record { ls = NList.ls nlist ; lseq = {!!} ; ls< = case2 {!!} } where
|
285
|
208 fdup03 : list-less (x ∷ qs) ≡ NList.ls nlist
|
|
209 fdup03 = {!!}
|
286
|
210 fdup06 : suc (length (NList.ls nlist)) ≡ length (x ∷ qs)
|
|
211 fdup06 = {!!}
|
285
|
212 fdup-phase1 (x ∷ qs) | tri> ¬a ¬b c with fdup-phase1 qs
|
283
|
213 ... | case1 p = case1 p
|
286
|
214 ... | case2 nlist = case2 record { ls = x<y→fin-1 c ∷ NList.ls nlist ; lseq = {!!} ; ls< = case1 fdup5 } where
|
|
215 fdup5 : length (x<y→fin-1 c ∷ NList.ls nlist) ≡ length (x ∷ qs)
|
|
216 fdup5 = {!!}
|
283
|
217 fdup-phase0 : FDup-in-list (suc n) qs
|
285
|
218 fdup-phase0 with fdup-phase1 qs
|
283
|
219 ... | case1 dup = record { dup = fromℕ< a<sa ; is-dup = dup }
|
287
|
220 ... | case2 nlist = fdup+1 qs (FDup-in-list.dup fdup) (FDup-in-list.is-dup fdup) where
|
286
|
221 fdup04 : (length (NList.ls nlist) ≡ length qs) ∨ (suc (length (NList.ls nlist)) ≡ length qs) → length (list-less qs) > n
|
|
222 fdup04 (case1 eq) = px≤py ( begin
|
|
223 suc (suc n) ≤⟨ lt ⟩
|
|
224 length qs ≡⟨ sym eq ⟩
|
|
225 length (NList.ls nlist) ≡⟨ cong (λ k → length k) (sym (NList.lseq nlist )) ⟩
|
|
226 length (list-less qs) ≤⟨ refl-≤s ⟩
|
|
227 suc (length (list-less qs)) ∎ ) where open ≤-Reasoning
|
|
228 fdup04 (case2 eq) = px≤py ( begin
|
|
229 suc (suc n) ≤⟨ lt ⟩
|
|
230 length qs ≡⟨ sym eq ⟩
|
|
231 suc (length (NList.ls nlist)) ≡⟨ cong (λ k → suc (length k)) (sym (NList.lseq nlist )) ⟩
|
|
232 suc (length (list-less qs)) ∎ ) where open ≤-Reasoning
|
283
|
233 fdup : FDup-in-list n (list-less qs)
|
286
|
234 fdup = fin-dup-in-list>n (list-less qs) ( fdup04 (NList.ls< nlist) )
|