comparison automaton-in-agda/src/fin.agda @ 294:248711134141

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 29 Dec 2021 19:08:28 +0900
parents 8992ecc40eb1
children 16e47a3c4eda
comparison
equal deleted inserted replaced
293:8992ecc40eb1 294:248711134141
171 fin-dup-in-list>n : {n : ℕ } → (qs : List (Fin n)) → (len> : length qs > n ) → FDup-in-list n qs 171 fin-dup-in-list>n : {n : ℕ } → (qs : List (Fin n)) → (len> : length qs > n ) → FDup-in-list n qs
172 fin-dup-in-list>n {zero} [] () 172 fin-dup-in-list>n {zero} [] ()
173 fin-dup-in-list>n {zero} (() ∷ qs) lt 173 fin-dup-in-list>n {zero} (() ∷ qs) lt
174 fin-dup-in-list>n {suc n} qs lt = fdup-phase0 where 174 fin-dup-in-list>n {suc n} qs lt = fdup-phase0 where
175 open import Level using ( Level ) 175 open import Level using ( Level )
176 -- make a dup from one level below
176 fdup+1 : (qs : List (Fin (suc n))) (i : Fin n) → fin-dup-in-list (fromℕ< a<sa ) qs ≡ false 177 fdup+1 : (qs : List (Fin (suc n))) (i : Fin n) → fin-dup-in-list (fromℕ< a<sa ) qs ≡ false
177 → fin-dup-in-list i (list-less qs) ≡ true → FDup-in-list (suc n) qs 178 → fin-dup-in-list i (list-less qs) ≡ true → FDup-in-list (suc n) qs
178 fdup+1 qs i ne p = record { dup = fin+1 i ; is-dup = f1-phase1 qs p (case1 ne) } where 179 fdup+1 qs i ne p = record { dup = fin+1 i ; is-dup = f1-phase1 qs p (case1 ne) } where
180 -- we have two loops on the max element and the current level. The disjuction means the phases may differ.
179 f1-phase2 : (qs : List (Fin (suc n)) ) → fin-phase2 i (list-less qs) ≡ true 181 f1-phase2 : (qs : List (Fin (suc n)) ) → fin-phase2 i (list-less qs) ≡ true
180 → (fin-phase1 (fromℕ< a<sa) qs ≡ false ) ∨ (fin-phase2 (fromℕ< a<sa) qs ≡ false) 182 → (fin-phase1 (fromℕ< a<sa) qs ≡ false ) ∨ (fin-phase2 (fromℕ< a<sa) qs ≡ false)
181 → fin-phase2 (fin+1 i) qs ≡ true 183 → fin-phase2 (fin+1 i) qs ≡ true
182 f1-phase2 (x ∷ qs) p (case1 q1) with <-fcmp (fromℕ< a<sa) x 184 f1-phase2 (x ∷ qs) p (case1 q1) with <-fcmp (fromℕ< a<sa) x
183 ... | tri< a ¬b ¬c = ⊥-elim ( nat-≤> a (subst (λ k → toℕ x < suc k ) (sym fin<asa) (fin≤n _ ))) 185 ... | tri< a ¬b ¬c = ⊥-elim ( nat-≤> a (subst (λ k → toℕ x < suc k ) (sym fin<asa) (fin≤n _ )))
184 f1-phase2 (x ∷ qs) p (case1 q1) | tri≈ ¬a b ¬c with <-fcmp (fin+1 i) x 186 f1-phase2 (x ∷ qs) p (case1 q1) | tri≈ ¬a b ¬c with <-fcmp (fin+1 i) x
185 ... | tri< a ¬b ¬c₁ = f1-phase2 qs p (case2 q1) 187 ... | tri< a ¬b ¬c₁ = f1-phase2 qs p (case2 q1)
186 ... | tri≈ ¬a₁ b₁ ¬c₁ = refl 188 ... | tri≈ ¬a₁ b₁ ¬c₁ = refl
187 ... | tri> ¬a₁ ¬b c = f1-phase2 qs p (case2 q1) 189 ... | tri> ¬a₁ ¬b c = f1-phase2 qs p (case2 q1)
190 -- two fcmp is only different in the size of Fin, but to develop both f1-phase and list-less both fcmps are required
188 f1-phase2 (x ∷ qs) p (case1 q1) | tri> ¬a ¬b c with <-fcmp i (fromℕ< (≤-trans c (fin≤n (fromℕ< a<sa)))) | <-fcmp (fin+1 i) x 191 f1-phase2 (x ∷ qs) p (case1 q1) | tri> ¬a ¬b c with <-fcmp i (fromℕ< (≤-trans c (fin≤n (fromℕ< a<sa)))) | <-fcmp (fin+1 i) x
189 ... | tri< a ¬b₁ ¬c | tri< a₁ ¬b₂ ¬c₁ = f1-phase2 qs p (case1 q1) 192 ... | tri< a ¬b₁ ¬c | tri< a₁ ¬b₂ ¬c₁ = f1-phase2 qs p (case1 q1)
190 ... | tri< a ¬b₁ ¬c | tri≈ ¬a₁ b ¬c₁ = ⊥-elim ( ¬a₁ (subst₂ (λ j k → j < k) (sym fin+1-toℕ) (toℕ-fromℕ< _) a )) 193 ... | tri< a ¬b₁ ¬c | tri≈ ¬a₁ b ¬c₁ = ⊥-elim ( ¬a₁ (subst₂ (λ j k → j < k) (sym fin+1-toℕ) (toℕ-fromℕ< _) a ))
191 ... | tri< a ¬b₁ ¬c | tri> ¬a₁ ¬b₂ c₁ = ⊥-elim ( ¬a₁ (subst₂ (λ j k → j < k) (sym fin+1-toℕ) (toℕ-fromℕ< _) a )) 194 ... | tri< a ¬b₁ ¬c | tri> ¬a₁ ¬b₂ c₁ = ⊥-elim ( ¬a₁ (subst₂ (λ j k → j < k) (sym fin+1-toℕ) (toℕ-fromℕ< _) a ))
192 ... | tri≈ ¬a₁ b ¬c | tri< a ¬b₁ ¬c₁ = ⊥-elim ( ¬a₁ (subst₂ (λ j k → j < k) fin+1-toℕ (sym (toℕ-fromℕ< _)) a )) 195 ... | tri≈ ¬a₁ b ¬c | tri< a ¬b₁ ¬c₁ = ⊥-elim ( ¬a₁ (subst₂ (λ j k → j < k) fin+1-toℕ (sym (toℕ-fromℕ< _)) a ))
247 ... | tri> ¬a₁ ¬b₁ c₁ | tri> ¬a₂ ¬b₂ c₂ = f1-phase1 qs p (case2 q1) 250 ... | tri> ¬a₁ ¬b₁ c₁ | tri> ¬a₂ ¬b₂ c₂ = f1-phase1 qs p (case2 q1)
248 fdup-phase0 : FDup-in-list (suc n) qs 251 fdup-phase0 : FDup-in-list (suc n) qs
249 fdup-phase0 with fin-dup-in-list (fromℕ< a<sa) qs | inspect (fin-dup-in-list (fromℕ< a<sa)) qs 252 fdup-phase0 with fin-dup-in-list (fromℕ< a<sa) qs | inspect (fin-dup-in-list (fromℕ< a<sa)) qs
250 ... | true | record { eq = eq } = record { dup = fromℕ< a<sa ; is-dup = eq } 253 ... | true | record { eq = eq } = record { dup = fromℕ< a<sa ; is-dup = eq }
251 ... | false | record { eq = ne } = fdup+1 qs (FDup-in-list.dup fdup) ne (FDup-in-list.is-dup fdup) where 254 ... | false | record { eq = ne } = fdup+1 qs (FDup-in-list.dup fdup) ne (FDup-in-list.is-dup fdup) where
255 -- if no dup in the max element, the list without the element is only one length shorter
252 fless : (qs : List (Fin (suc n))) → length qs > suc n → fin-dup-in-list (fromℕ< a<sa) qs ≡ false → n < length (list-less qs) 256 fless : (qs : List (Fin (suc n))) → length qs > suc n → fin-dup-in-list (fromℕ< a<sa) qs ≡ false → n < length (list-less qs)
253 fless qs lt p = fl-phase1 n qs lt p where 257 fless qs lt p = fl-phase1 n qs lt p where
254 fl-phase2 : (n1 : ℕ) (qs : List (Fin (suc n))) → length qs > n1 → fin-phase2 (fromℕ< a<sa) qs ≡ false → n1 < length (list-less qs) 258 fl-phase2 : (n1 : ℕ) (qs : List (Fin (suc n))) → length qs > n1 → fin-phase2 (fromℕ< a<sa) qs ≡ false → n1 < length (list-less qs)
255 fl-phase2 zero (x ∷ qs) (s≤s lt) p with <-fcmp (fromℕ< a<sa) x 259 fl-phase2 zero (x ∷ qs) (s≤s lt) p with <-fcmp (fromℕ< a<sa) x
256 ... | tri< a ¬b ¬c = ⊥-elim ( nat-≤> a (subst (λ k → toℕ x < suc k ) (sym fin<asa) (fin≤n _ ))) 260 ... | tri< a ¬b ¬c = ⊥-elim ( nat-≤> a (subst (λ k → toℕ x < suc k ) (sym fin<asa) (fin≤n _ )))
265 ... | tri> ¬a ¬b c = s≤s z≤n 269 ... | tri> ¬a ¬b c = s≤s z≤n
266 fl-phase1 (suc n1) (x ∷ qs) (s≤s lt) p with <-fcmp (fromℕ< a<sa) x 270 fl-phase1 (suc n1) (x ∷ qs) (s≤s lt) p with <-fcmp (fromℕ< a<sa) x
267 ... | tri< a ¬b ¬c = ⊥-elim ( nat-≤> a (subst (λ k → toℕ x < suc k ) (sym fin<asa) (fin≤n _ ))) 271 ... | tri< a ¬b ¬c = ⊥-elim ( nat-≤> a (subst (λ k → toℕ x < suc k ) (sym fin<asa) (fin≤n _ )))
268 ... | tri≈ ¬a b ¬c = fl-phase2 (suc n1) qs lt p 272 ... | tri≈ ¬a b ¬c = fl-phase2 (suc n1) qs lt p
269 ... | tri> ¬a ¬b c = s≤s ( fl-phase1 n1 qs lt p ) 273 ... | tri> ¬a ¬b c = s≤s ( fl-phase1 n1 qs lt p )
274 -- if the list without the max element is only one length shorter, we can recurse
270 fdup : FDup-in-list n (list-less qs) 275 fdup : FDup-in-list n (list-less qs)
271 fdup = fin-dup-in-list>n (list-less qs) (fless qs lt ne) 276 fdup = fin-dup-in-list>n (list-less qs) (fless qs lt ne)