Mercurial > hg > Members > kono > Proof > automaton
view automaton-in-agda/src/fin.agda @ 286:f49c6d768e19
...
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 28 Dec 2021 02:43:07 +0900 |
parents | 6e85b8b0d8db |
children | ce16779e72a5 |
line wrap: on
line source
{-# OPTIONS --allow-unsolved-metas #-} module fin where open import Data.Fin hiding (_<_ ; _≤_ ; _>_ ; _+_ ) open import Data.Fin.Properties hiding (≤-trans ; <-trans ; ≤-refl ) renaming ( <-cmp to <-fcmp ) open import Data.Nat open import Data.Nat.Properties open import logic open import nat open import Relation.Binary.PropositionalEquality -- toℕ<n fin<n : {n : ℕ} {f : Fin n} → toℕ f < n fin<n {_} {zero} = s≤s z≤n fin<n {suc n} {suc f} = s≤s (fin<n {n} {f}) -- toℕ≤n fin≤n : {n : ℕ} (f : Fin (suc n)) → toℕ f ≤ n fin≤n {_} zero = z≤n fin≤n {suc n} (suc f) = s≤s (fin≤n {n} f) pred<n : {n : ℕ} {f : Fin (suc n)} → n > 0 → Data.Nat.pred (toℕ f) < n pred<n {suc n} {zero} (s≤s z≤n) = s≤s z≤n pred<n {suc n} {suc f} (s≤s z≤n) = fin<n fin<asa : {n : ℕ} → toℕ (fromℕ< {n} a<sa) ≡ n fin<asa = toℕ-fromℕ< nat.a<sa -- fromℕ<-toℕ toℕ→from : {n : ℕ} {x : Fin (suc n)} → toℕ x ≡ n → fromℕ n ≡ x toℕ→from {0} {zero} refl = refl toℕ→from {suc n} {suc x} eq = cong (λ k → suc k ) ( toℕ→from {n} {x} (cong (λ k → Data.Nat.pred k ) eq )) 0≤fmax : {n : ℕ } → (# 0) Data.Fin.≤ fromℕ< {n} a<sa 0≤fmax = subst (λ k → 0 ≤ k ) (sym (toℕ-fromℕ< a<sa)) z≤n 0<fmax : {n : ℕ } → (# 0) Data.Fin.< fromℕ< {suc n} a<sa 0<fmax = subst (λ k → 0 < k ) (sym (toℕ-fromℕ< a<sa)) (s≤s z≤n) -- toℕ-injective i=j : {n : ℕ} (i j : Fin n) → toℕ i ≡ toℕ j → i ≡ j i=j {suc n} zero zero refl = refl i=j {suc n} (suc i) (suc j) eq = cong ( λ k → suc k ) ( i=j i j (cong ( λ k → Data.Nat.pred k ) eq) ) -- raise 1 fin+1 : { n : ℕ } → Fin n → Fin (suc n) fin+1 zero = zero fin+1 (suc x) = suc (fin+1 x) open import Data.Nat.Properties as NatP hiding ( _≟_ ) fin+1≤ : { i n : ℕ } → (a : i < n) → fin+1 (fromℕ< a) ≡ fromℕ< (<-trans a a<sa) fin+1≤ {0} {suc i} (s≤s z≤n) = refl fin+1≤ {suc n} {suc (suc i)} (s≤s (s≤s a)) = cong (λ k → suc k ) ( fin+1≤ {n} {suc i} (s≤s a) ) fin+1-toℕ : { n : ℕ } → { x : Fin n} → toℕ (fin+1 x) ≡ toℕ x fin+1-toℕ {suc n} {zero} = refl fin+1-toℕ {suc n} {suc x} = cong (λ k → suc k ) (fin+1-toℕ {n} {x}) open import Relation.Nullary open import Data.Empty fin-1 : { n : ℕ } → (x : Fin (suc n)) → ¬ (x ≡ zero ) → Fin n fin-1 zero ne = ⊥-elim (ne refl ) fin-1 {n} (suc x) ne = x fin-1-sx : { n : ℕ } → (x : Fin n) → fin-1 (suc x) (λ ()) ≡ x fin-1-sx zero = refl fin-1-sx (suc x) = refl fin-1-xs : { n : ℕ } → (x : Fin (suc n)) → (ne : ¬ (x ≡ zero )) → suc (fin-1 x ne ) ≡ x fin-1-xs zero ne = ⊥-elim ( ne refl ) fin-1-xs (suc x) ne = refl -- suc-injective -- suc-eq : {n : ℕ } {x y : Fin n} → Fin.suc x ≡ Fin.suc y → x ≡ y -- suc-eq {n} {x} {y} eq = subst₂ (λ j k → j ≡ k ) {!!} {!!} (cong (λ k → Data.Fin.pred k ) eq ) -- this is refl lemma3 : {a b : ℕ } → (lt : a < b ) → fromℕ< (s≤s lt) ≡ suc (fromℕ< lt) lemma3 (s≤s lt) = refl -- fromℕ<-toℕ lemma12 : {n m : ℕ } → (n<m : n < m ) → (f : Fin m ) → toℕ f ≡ n → f ≡ fromℕ< n<m lemma12 {zero} {suc m} (s≤s z≤n) zero refl = refl lemma12 {suc n} {suc m} (s≤s n<m) (suc f) refl = cong suc ( lemma12 {n} {m} n<m f refl ) open import Relation.Binary.HeterogeneousEquality as HE using (_≅_ ) -- <-irrelevant <-nat=irr : {i j n : ℕ } → ( i ≡ j ) → {i<n : i < n } → {j<n : j < n } → i<n ≅ j<n <-nat=irr {zero} {zero} {suc n} refl {s≤s z≤n} {s≤s z≤n} = HE.refl <-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 ) lemma8 : {i j n : ℕ } → ( i ≡ j ) → {i<n : i < n } → {j<n : j < n } → i<n ≅ j<n lemma8 {zero} {zero} {suc n} refl {s≤s z≤n} {s≤s z≤n} = HE.refl 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 ) -- fromℕ<-irrelevant lemma10 : {n i j : ℕ } → ( i ≡ j ) → {i<n : i < n } → {j<n : j < n } → fromℕ< i<n ≡ fromℕ< j<n lemma10 {n} refl = HE.≅-to-≡ (HE.cong (λ k → fromℕ< k ) (lemma8 refl )) lemma31 : {a b c : ℕ } → { a<b : a < b } { b<c : b < c } { a<c : a < c } → NatP.<-trans a<b b<c ≡ a<c lemma31 {a} {b} {c} {a<b} {b<c} {a<c} = HE.≅-to-≡ (lemma8 refl) -- toℕ-fromℕ< lemma11 : {n m : ℕ } {x : Fin n } → (n<m : n < m ) → toℕ (fromℕ< (NatP.<-trans (toℕ<n x) n<m)) ≡ toℕ x lemma11 {n} {m} {x} n<m = begin toℕ (fromℕ< (NatP.<-trans (toℕ<n x) n<m)) ≡⟨ toℕ-fromℕ< _ ⟩ toℕ x ∎ where open ≡-Reasoning x<y→fin-1 : {n : ℕ } → { x y : Fin (suc n)} → toℕ x < toℕ y → Fin n x<y→fin-1 {n} {x} {y} lt = fromℕ< (≤-trans lt (fin≤n _ )) x<y→fin-1-eq : {n : ℕ } → { x y : Fin (suc n)} → (lt : toℕ x < toℕ y ) → toℕ x ≡ toℕ (x<y→fin-1 lt ) x<y→fin-1-eq {n} {x} {y} lt = sym ( begin toℕ (fromℕ< (≤-trans lt (fin≤n y)) ) ≡⟨ toℕ-fromℕ< _ ⟩ toℕ x ∎ ) where open ≡-Reasoning open import Data.List open import Relation.Binary.Definitions fin-phase2 : { n : ℕ } (q : Fin n) (qs : List (Fin n) ) → Bool fin-phase2 q [] = false fin-phase2 q (x ∷ qs) with <-fcmp q x ... | tri< a ¬b ¬c = fin-phase2 q qs ... | tri≈ ¬a b ¬c = true ... | tri> ¬a ¬b c = fin-phase2 q qs fin-phase1 : { n : ℕ } (q : Fin n) (qs : List (Fin n) ) → Bool fin-phase1 q [] = false fin-phase1 q (x ∷ qs) with <-fcmp q x ... | tri< a ¬b ¬c = fin-phase1 q qs ... | tri≈ ¬a b ¬c = fin-phase2 q qs ... | tri> ¬a ¬b c = fin-phase1 q qs fin-dup-in-list : { n : ℕ} (q : Fin n) (qs : List (Fin n) ) → Bool fin-dup-in-list {n} q qs = fin-phase1 q qs record FDup-in-list (n : ℕ ) (qs : List (Fin n)) : Set where field dup : Fin n is-dup : fin-dup-in-list dup qs ≡ true list-less : {n : ℕ } → List (Fin (suc n)) → List (Fin n) list-less [] = [] list-less {n} (i ∷ ls) with NatP.<-cmp (toℕ i) n ... | tri< a ¬b ¬c = fromℕ< a ∷ list-less ls ... | tri≈ ¬a b ¬c = list-less ls ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> (fin≤n i) c ) record NList (n : ℕ) (qs : List (Fin (suc n))) : Set where field ls : List (Fin n) lseq : list-less qs ≡ ls ls< : (length ls ≡ length qs) ∨ (suc (length ls) ≡ length qs) fin-dup-in-list>n : {n : ℕ } → (qs : List (Fin n)) → (len> : length qs > n ) → FDup-in-list n qs fin-dup-in-list>n {zero} [] () fin-dup-in-list>n {zero} (() ∷ qs) lt fin-dup-in-list>n {suc n} qs lt = fdup-phase0 where open import Level using ( Level ) fdup+1 : (qs : List (Fin (suc n))) (i : Fin n) → fin-dup-in-list i (list-less qs) ≡ true → fin-dup-in-list (fin+1 i) qs ≡ true fdup+1 qs i p = f1-phase1 qs p where f1-phase2 : (qs : List (Fin (suc n)) ) → fin-phase2 i (list-less qs) ≡ true → fin-phase2 (fin+1 i) qs ≡ true f1-phase2 (x ∷ qs) p with NatP.<-cmp (toℕ x) n f1-phase2 (x ∷ qs) p | tri< a ¬b ¬c with <-fcmp (fin+1 i) x ... | tri< a₁ ¬b₁ ¬c₁ = f1-phase2 qs {!!} ... | tri≈ ¬a b ¬c₁ = refl ... | tri> ¬a ¬b₁ c = f1-phase2 qs {!!} f1-phase2 (x ∷ qs) p | tri≈ ¬a b ¬c = {!!} f1-phase2 (x ∷ qs) p | tri> ¬a ¬b c = ⊥-elim ( nat-≤> (fin≤n x) c ) f1-phase1 : (qs : List (Fin (suc n)) ) → fin-phase1 i (list-less qs) ≡ true → fin-phase1 (fin+1 i) qs ≡ true f1-phase1 [] () f1-phase1 (x ∷ qs) p with <-fcmp (fin+1 i) x ... | tri< a ¬b ¬c = f1-phase1 qs {!!} ... | tri≈ ¬a b ¬c = f1-phase2 qs {!!} ... | tri> ¬a ¬b c = f1-phase1 qs {!!} fdup-phase2 : (qs : List (Fin (suc n)) ) → ( fin-phase2 (fromℕ< a<sa ) qs ≡ true ) ∨ NList n qs fdup-phase2 [] = case2 record { ls = [] ; lseq = refl ; ls< = case1 refl } fdup-phase2 (x ∷ qs) with <-fcmp (fromℕ< a<sa) x ... | tri< a ¬b ¬c = ⊥-elim ( nat-≤> a (subst (λ k → toℕ x < suc k) (sym fin<asa) fin<n )) fdup-phase2 (x ∷ qs) | tri≈ ¬a b ¬c = case1 refl fdup-phase2 (x ∷ qs) | tri> ¬a ¬b c with fdup-phase2 qs ... | case1 p = case1 p ... | case2 nlist = case2 record { ls = x<y→fin-1 c ∷ NList.ls nlist ; lseq = fdup01 ; ls< = case1 {!!} } where fdup01 : list-less (x ∷ qs) ≡ x<y→fin-1 c ∷ NList.ls nlist fdup01 with NatP.<-cmp (toℕ x) n ... | tri< a ¬b ¬c = begin fromℕ< a ∷ list-less qs ≡⟨ cong₂ (λ j k → j ∷ k ) (lemma10 refl) (NList.lseq nlist) ⟩ fromℕ< (≤-trans c (fin≤n (fromℕ< a<sa))) ∷ NList.ls nlist ∎ where open ≡-Reasoning ... | tri≈ ¬a b ¬c = ⊥-elim ( nat-≡< b (subst (λ k → toℕ x < k ) fin<asa c )) ... | tri> ¬a ¬b c = ⊥-elim ( nat-≤> (fin≤n x) c ) fdup-phase1 : (qs : List (Fin (suc n)) ) → (fin-phase1 (fromℕ< a<sa) qs ≡ true) ∨ NList n qs fdup-phase1 [] = case2 record { ls = [] ; lseq = refl ; ls< = case1 refl } fdup-phase1 (x ∷ qs) with <-fcmp (fromℕ< a<sa) x fdup-phase1 (x ∷ qs) | tri< a ¬b ¬c = ⊥-elim ( nat-≤> a (subst (λ k → toℕ x < suc k) (sym fin<asa) fin<n )) fdup-phase1 (x ∷ qs) | tri≈ ¬a b ¬c with fdup-phase2 qs ... | case1 p = case1 p ... | case2 nlist = case2 record { ls = NList.ls nlist ; lseq = {!!} ; ls< = case2 {!!} } where fdup03 : list-less (x ∷ qs) ≡ NList.ls nlist fdup03 = {!!} fdup06 : suc (length (NList.ls nlist)) ≡ length (x ∷ qs) fdup06 = {!!} fdup-phase1 (x ∷ qs) | tri> ¬a ¬b c with fdup-phase1 qs ... | case1 p = case1 p ... | case2 nlist = case2 record { ls = x<y→fin-1 c ∷ NList.ls nlist ; lseq = {!!} ; ls< = case1 fdup5 } where fdup5 : length (x<y→fin-1 c ∷ NList.ls nlist) ≡ length (x ∷ qs) fdup5 = {!!} fdup-phase0 : FDup-in-list (suc n) qs fdup-phase0 with fdup-phase1 qs ... | case1 dup = record { dup = fromℕ< a<sa ; is-dup = dup } ... | case2 nlist = record { dup = fin+1 (FDup-in-list.dup fdup) ; is-dup = fdup+1 qs (FDup-in-list.dup fdup) (FDup-in-list.is-dup fdup) } where fdup04 : (length (NList.ls nlist) ≡ length qs) ∨ (suc (length (NList.ls nlist)) ≡ length qs) → length (list-less qs) > n fdup04 (case1 eq) = px≤py ( begin suc (suc n) ≤⟨ lt ⟩ length qs ≡⟨ sym eq ⟩ length (NList.ls nlist) ≡⟨ cong (λ k → length k) (sym (NList.lseq nlist )) ⟩ length (list-less qs) ≤⟨ refl-≤s ⟩ suc (length (list-less qs)) ∎ ) where open ≤-Reasoning fdup04 (case2 eq) = px≤py ( begin suc (suc n) ≤⟨ lt ⟩ length qs ≡⟨ sym eq ⟩ suc (length (NList.ls nlist)) ≡⟨ cong (λ k → suc (length k)) (sym (NList.lseq nlist )) ⟩ suc (length (list-less qs)) ∎ ) where open ≤-Reasoning fdup : FDup-in-list n (list-less qs) fdup = fin-dup-in-list>n (list-less qs) ( fdup04 (NList.ls< nlist) )