Mercurial > hg > Members > kono > Proof > automaton
view agda/regular-language.agda @ 99:aca3f1349913
...
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 11 Nov 2019 21:30:04 +0900 |
parents | f196e739b4bf |
children | 0b1b9a28a707 |
line wrap: on
line source
module regular-language where open import Level renaming ( suc to Suc ; zero to Zero ) open import Data.List open import Data.Nat hiding ( _≟_ ) open import Data.Fin hiding ( _+_ ) open import Data.Empty open import Data.Product -- open import Data.Maybe open import Relation.Nullary open import Relation.Binary.PropositionalEquality hiding ( [_] ) open import logic open import nat open import automaton open import finiteSet language : { Σ : Set } → Set language {Σ} = List Σ → Bool language-L : { Σ : Set } → Set language-L {Σ} = List (List Σ) open Automaton record RegularLanguage ( Σ : Set ) : Set (Suc Zero) where field states : Set astart : states aℕ : ℕ afin : FiniteSet states {aℕ} automaton : Automaton states Σ contain : List Σ → Bool contain x = accept automaton astart x Union : {Σ : Set} → ( A B : language {Σ} ) → language {Σ} Union {Σ} A B x = (A x ) \/ (B x) split : {Σ : Set} → (List Σ → Bool) → ( List Σ → Bool) → List Σ → Bool split x y [] = x [] /\ y [] split x y (h ∷ t) = (x [] /\ y (h ∷ t)) \/ split (λ t1 → x ( h ∷ t1 )) (λ t2 → y t2 ) t Concat : {Σ : Set} → ( A B : language {Σ} ) → language {Σ} Concat {Σ} A B = split A B {-# TERMINATING #-} Star : {Σ : Set} → ( A : language {Σ} ) → language {Σ} Star {Σ} A = split A ( Star {Σ} A ) test-AB→split : {Σ : Set} → {A B : List In2 → Bool} → split A B ( i0 ∷ i1 ∷ i0 ∷ [] ) ≡ ( ( A [] /\ B ( i0 ∷ i1 ∷ i0 ∷ [] ) ) \/ ( A ( i0 ∷ [] ) /\ B ( i1 ∷ i0 ∷ [] ) ) \/ ( A ( i0 ∷ i1 ∷ [] ) /\ B ( i0 ∷ [] ) ) \/ ( A ( i0 ∷ i1 ∷ i0 ∷ [] ) /\ B [] ) ) test-AB→split {_} {A} {B} = refl open RegularLanguage isRegular : {Σ : Set} → (A : language {Σ} ) → ( x : List Σ ) → (r : RegularLanguage Σ ) → Set isRegular A x r = A x ≡ contain r x postulate fin-× : {A B : Set} → { a b : ℕ } → FiniteSet A {a} → FiniteSet B {b} → FiniteSet (A × B) {a * b} M-Union : {Σ : Set} → (A B : RegularLanguage Σ ) → RegularLanguage Σ M-Union {Σ} A B = record { states = states A × states B ; astart = ( astart A , astart B ) ; aℕ = aℕ A * aℕ B ; afin = fin-× (afin A) (afin B) ; automaton = record { δ = λ q x → ( δ (automaton A) (proj₁ q) x , δ (automaton B) (proj₂ q) x ) ; aend = λ q → ( aend (automaton A) (proj₁ q) \/ aend (automaton B) (proj₂ q) ) } } closed-in-union : {Σ : Set} → (A B : RegularLanguage Σ ) → ( x : List Σ ) → isRegular (Union (contain A) (contain B)) x ( M-Union A B ) closed-in-union A B [] = lemma where lemma : aend (automaton A) (astart A) \/ aend (automaton B) (astart B) ≡ aend (automaton A) (astart A) \/ aend (automaton B) (astart B) lemma = refl closed-in-union {Σ} A B ( h ∷ t ) = lemma1 t ((δ (automaton A) (astart A) h)) ((δ (automaton B) (astart B) h)) where lemma1 : (t : List Σ) → (qa : states A ) → (qb : states B ) → accept (automaton A) qa t \/ accept (automaton B) qb t ≡ accept (automaton (M-Union A B)) (qa , qb) t lemma1 [] qa qb = refl lemma1 (h ∷ t ) qa qb = lemma1 t ((δ (automaton A) qa h)) ((δ (automaton B) qb h)) -- M-Concat : {Σ : Set} → (A B : RegularLanguage Σ ) → RegularLanguage Σ -- M-Concat {Σ} A B = record { -- states = states A ∨ states B -- ; astart = case1 (astart A ) -- ; automaton = record { -- δ = {!!} -- ; aend = {!!} -- } -- } -- -- closed-in-concat : {Σ : Set} → (A B : RegularLanguage Σ ) → ( x : List Σ ) → isRegular (Concat (contain A) (contain B)) x ( M-Concat A B ) -- closed-in-concat = {!!} open import nfa open import sbconst2 open FiniteSet open import Data.Nat.Properties hiding ( _≟_ ) open import Relation.Binary as B hiding (Decidable) postulate fin-∨ : {A B : Set} → { a b : ℕ } → FiniteSet A {a} → FiniteSet B {b} → FiniteSet (A ∨ B) {a + b} fin→ : {A : Set} → { a : ℕ } → FiniteSet A {a} → FiniteSet (A → Bool ) {exp 2 a} Concat-NFA : {Σ : Set} → (A B : RegularLanguage Σ ) → NAutomaton (states A ∨ states B) Σ Concat-NFA {Σ} A B = record { Nδ = δnfa ; Nend = nend } module Concat-NFA where δnfa : states A ∨ states B → Σ → states A ∨ states B → Bool δnfa (case1 q) i (case1 q₁) = equal? (afin A) (δ (automaton A) q i) q₁ δnfa (case1 qa) i (case2 qb) = (aend (automaton A) qa ) /\ (equal? (afin B) qb (δ (automaton B) (astart B) i) ) δnfa (case2 q) i (case2 q₁) = equal? (afin B) (δ (automaton B) q i) q₁ δnfa _ i _ = false nend : states A ∨ states B → Bool nend (case2 q) = aend (automaton B) q nend (case1 q) = aend (automaton A) q /\ aend (automaton B) (astart B) -- empty B case Concat-NFA-start : {Σ : Set} → (A B : RegularLanguage Σ ) → states A ∨ states B → Bool Concat-NFA-start A B q = equal? (fin-∨ (afin A) (afin B)) (case1 (astart A)) q M-Concat : {Σ : Set} → (A B : RegularLanguage Σ ) → RegularLanguage Σ M-Concat {Σ} A B = record { states = states A ∨ states B → Bool ; astart = Concat-NFA-start A B ; aℕ = finℕ finf ; afin = finf ; automaton = subset-construction fin (Concat-NFA A B) (case1 (astart A)) } where fin : FiniteSet (states A ∨ states B ) {aℕ A + aℕ B} fin = fin-∨ (afin A) (afin B) finf : FiniteSet (states A ∨ states B → Bool ) finf = fin→ fin record Split {Σ : Set} (A : List Σ → Bool ) ( B : List Σ → Bool ) (x : List Σ ) : Set where field sp0 : List Σ sp1 : List Σ sp-concat : sp0 ++ sp1 ≡ x prop0 : A sp0 ≡ true prop1 : B sp1 ≡ true open Split list-empty++ : {Σ : Set} → (x y : List Σ) → x ++ y ≡ [] → (x ≡ [] ) ∧ (y ≡ [] ) list-empty++ [] [] refl = record { proj1 = refl ; proj2 = refl } list-empty++ [] (x ∷ y) () list-empty++ (x ∷ x₁) y () open _∧_ open import Relation.Binary.PropositionalEquality hiding ( [_] ) c-split-lemma : {Σ : Set} → (A B : List Σ → Bool ) → (h : Σ) → ( t : List Σ ) → split A B (h ∷ t ) ≡ true → ( ¬ (A [] ≡ true )) ∨ ( ¬ (B ( h ∷ t ) ≡ true) ) → split (λ t1 → A (h ∷ t1)) B t ≡ true c-split-lemma {Σ} A B h t eq (case1 ¬p ) = sym ( begin true ≡⟨ sym eq ⟩ split A B (h ∷ t ) ≡⟨⟩ A [] /\ B (h ∷ t) \/ split (λ t1 → A (h ∷ t1)) B t ≡⟨ cong ( λ k → k \/ split (λ t1 → A (h ∷ t1)) B t ) (bool-and-1 (¬-bool-t ¬p)) ⟩ false \/ split (λ t1 → A (h ∷ t1)) B t ≡⟨ bool-or-1 refl ⟩ split (λ t1 → A (h ∷ t1)) B t ∎ ) where open ≡-Reasoning c-split-lemma {Σ} A B h t eq (case2 ¬p ) = sym ( begin true ≡⟨ sym eq ⟩ split A B (h ∷ t ) ≡⟨⟩ A [] /\ B (h ∷ t) \/ split (λ t1 → A (h ∷ t1)) B t ≡⟨ cong ( λ k → k \/ split (λ t1 → A (h ∷ t1)) B t ) (bool-and-2 (¬-bool-t ¬p)) ⟩ false \/ split (λ t1 → A (h ∷ t1)) B t ≡⟨ bool-or-1 refl ⟩ split (λ t1 → A (h ∷ t1)) B t ∎ ) where open ≡-Reasoning split→AB : {Σ : Set} → (A B : List Σ → Bool ) → ( x : List Σ ) → split A B x ≡ true → Split A B x split→AB {Σ} A B [] eq with bool-≡-? (A []) true | bool-≡-? (B []) true split→AB {Σ} A B [] eq | yes eqa | yes eqb = record { sp0 = [] ; sp1 = [] ; sp-concat = refl ; prop0 = eqa ; prop1 = eqb } split→AB {Σ} A B [] eq | yes p | no ¬p = ⊥-elim (lemma-∧-1 eq (¬-bool-t ¬p )) split→AB {Σ} A B [] eq | no ¬p | t = ⊥-elim (lemma-∧-0 eq (¬-bool-t ¬p )) split→AB {Σ} A B (h ∷ t ) eq with bool-≡-? (A []) true | bool-≡-? (B (h ∷ t )) true ... | yes px | yes py = record { sp0 = [] ; sp1 = h ∷ t ; sp-concat = refl ; prop0 = px ; prop1 = py } ... | no px | _ with split→AB (λ t1 → A ( h ∷ t1 )) B t (c-split-lemma A B h t eq (case1 px) ) ... | S = record { sp0 = h ∷ sp0 S ; sp1 = sp1 S ; sp-concat = cong ( λ k → h ∷ k) (sp-concat S) ; prop0 = prop0 S ; prop1 = prop1 S } split→AB {Σ} A B (h ∷ t ) eq | _ | no px with split→AB (λ t1 → A ( h ∷ t1 )) B t (c-split-lemma A B h t eq (case2 px) ) ... | S = record { sp0 = h ∷ sp0 S ; sp1 = sp1 S ; sp-concat = cong ( λ k → h ∷ k) (sp-concat S) ; prop0 = prop0 S ; prop1 = prop1 S } AB→split : {Σ : Set} → (A B : List Σ → Bool ) → ( x y : List Σ ) → A x ≡ true → B y ≡ true → split A B (x ++ y ) ≡ true AB→split {Σ} A B [] [] eqa eqb = begin split A B [] ≡⟨⟩ A [] /\ B [] ≡⟨ cong₂ (λ j k → j /\ k ) eqa eqb ⟩ true ∎ where open ≡-Reasoning AB→split {Σ} A B [] (h ∷ y ) eqa eqb = begin split A B (h ∷ y ) ≡⟨⟩ A [] /\ B (h ∷ y) \/ split (λ t1 → A (h ∷ t1)) B y ≡⟨ cong₂ (λ j k → j /\ k \/ split (λ t1 → A (h ∷ t1)) B y ) eqa eqb ⟩ true /\ true \/ split (λ t1 → A (h ∷ t1)) B y ≡⟨⟩ true \/ split (λ t1 → A (h ∷ t1)) B y ≡⟨⟩ true ∎ where open ≡-Reasoning AB→split {Σ} A B (h ∷ t) y eqa eqb = begin split A B ((h ∷ t) ++ y) ≡⟨⟩ A [] /\ B (h ∷ t ++ y) \/ split (λ t1 → A (h ∷ t1)) B (t ++ y) ≡⟨ cong ( λ k → A [] /\ B (h ∷ t ++ y) \/ k ) ( begin split (λ t1 → A (h ∷ t1)) B (t ++ y) ≡⟨ AB→split {Σ} (λ t1 → A (h ∷ t1)) B t y eqa eqb ⟩ true ∎ ) ⟩ A [] /\ B (h ∷ t ++ y) \/ true ≡⟨ bool-or-3 ⟩ true ∎ where open ≡-Reasoning open NAutomaton open import Data.List.Properties closed-in-concat : {Σ : Set} → (A B : RegularLanguage Σ ) → ( x : List Σ ) → isRegular (Concat (contain A) (contain B)) x ( M-Concat A B ) closed-in-concat {Σ} A B x = ≡-Bool-func closed-in-concat→ closed-in-concat← where finab = (fin-∨ (afin A) (afin B)) NFA = (Concat-NFA A B) abmove : (q : states A ∨ states B) → (h : Σ ) → states A ∨ states B abmove (case1 q) h = case1 (δ (automaton A) q h) abmove (case2 q) h = case2 (δ (automaton B) q h) lemma-nmove-ab : (q : states A ∨ states B) → (h : Σ ) → Nδ NFA q h (abmove q h) ≡ true lemma-nmove-ab (case1 q) _ = equal?-refl (afin A) lemma-nmove-ab (case2 q) _ = equal?-refl (afin B) nmove : (q : states A ∨ states B) (nq : states A ∨ states B → Bool ) → (nq q ≡ true) → ( h : Σ ) → exists finab (λ qn → nq qn /\ Nδ NFA qn h (abmove q h)) ≡ true nmove (case1 q) nq nqt h = found finab (case1 q) ( bool-and-tt nqt (lemma-nmove-ab (case1 q) h) ) nmove (case2 q) nq nqt h = found finab (case2 q) ( bool-and-tt nqt (lemma-nmove-ab (case2 q) h) ) where acceptB : (z : List Σ) → (q : states B) → (nq : states A ∨ states B → Bool ) → (nq (case2 q) ≡ true) → ( accept (automaton B) q z ≡ true ) → Naccept NFA finab nq z ≡ true acceptB [] q nq nqt fb = lemma8 where lemma8 : exists finab ( λ q → nq q /\ Nend NFA q ) ≡ true lemma8 = found finab (case2 q) ( bool-and-tt nqt fb ) acceptB (h ∷ t ) q nq nq=q fb = acceptB t (δ (automaton B) q h) (Nmoves NFA finab nq h) (nmove (case2 q) nq nq=q h) fb acceptA : (y z : List Σ) → (q : states A) → (nq : states A ∨ states B → Bool ) → (nq (case1 q) ≡ true) → ( accept (automaton A) q y ≡ true ) → ( accept (automaton B) (astart B) z ≡ true ) → Naccept NFA finab nq (y ++ z) ≡ true acceptA [] [] q nq nqt fa fb = found finab (case1 q) (bool-and-tt nqt (bool-and-tt fa fb )) acceptA [] (h ∷ z) q nq nq=q fa fb = acceptB z nextb (Nmoves NFA finab nq h) lemma70 fb where nextb : states B nextb = δ (automaton B) (astart B) h lemma70 : exists finab (λ qn → nq qn /\ Nδ NFA qn h (case2 nextb)) ≡ true lemma70 = found finab (case1 q) ( bool-and-tt nq=q (bool-and-tt fa (lemma-nmove-ab (case2 (astart B)) h) )) acceptA (h ∷ t) z q nq nq=q fa fb = acceptA t z (δ (automaton A) q h) (Nmoves NFA finab nq h) (nmove (case1 q) nq nq=q h) fa fb where acceptAB : Split (contain A) (contain B) x → Naccept NFA finab (equal? finab (case1 (astart A))) x ≡ true acceptAB S = subst ( λ k → Naccept NFA finab (equal? finab (case1 (astart A))) k ≡ true ) ( sp-concat S ) (acceptA (sp0 S) (sp1 S) (astart A) (equal? finab (case1 (astart A))) (equal?-refl finab) (prop0 S) (prop1 S) ) closed-in-concat→ : Concat (contain A) (contain B) x ≡ true → contain (M-Concat A B) x ≡ true closed-in-concat→ concat with split→AB (contain A) (contain B) x concat ... | S = begin accept (subset-construction finab NFA (case1 (astart A))) (Concat-NFA-start A B ) x ≡⟨ ≡-Bool-func (subset-construction-lemma← finab NFA (case1 (astart A)) x ) (subset-construction-lemma→ finab NFA (case1 (astart A)) x ) ⟩ Naccept NFA finab (equal? finab (case1 (astart A))) x ≡⟨ acceptAB S ⟩ true ∎ where open ≡-Reasoning open Found contain-B : ( h : Σ) → ( t : List Σ ) → ( nq : states A ∨ states B → Bool ) → (qb : states B) → ( nq (case2 qb) ≡ true ) → Naccept NFA finab nq (h ∷ t) ≡ true → accept (automaton B) (δ (automaton B) (astart B) h) t ≡ true contain-B = {!!} condition-A : (nq : states A ∨ states B → Bool ) → (qa : states A) → (q : states A ∨ states B) → Set condition-A nq qa q = ( nq q ≡ true → q ≡ case1 qa ) ∨ ( (nq (case1 qa) /\ aend (automaton A) qa /\ nq (case2 (astart B))) ≡ true ) contain-A : (x : List Σ) → (nq : states A ∨ states B → Bool ) → Naccept NFA finab nq x ≡ true → (qa : states A ) → (nq (case1 qa) ≡ true) → ( (q : states A ∨ states B ) → condition-A nq qa q ) → split (accept (automaton A) qa ) (contain B) x ≡ true contain-A [] nq fn qa qat cond with found← finab fn ... | S with cond (found-q S) ... | case2 end = {!!} ... | case1 qa=q with qa=q (bool-∧→tt-0 (found-p S)) ... | refl = bool-∧→tt-1 (found-p S) contain-A (h ∷ t) nq fn qa qat cond with bool-≡-? ((aend (automaton A) qa) /\ accept (automaton B) (δ (automaton B) (astart B) h) t ) true ... | yes eq = bool-or-41 eq ... | no ne = bool-or-31 (contain-A t (Nmoves NFA finab nq h) fn (δ (automaton A) qa h) (nmove (case1 qa) nq qat h) lemma11-1 ) where lemma11-1 : (q : states A ∨ states B) → condition-A (λ q → exists finab (λ qn → nq qn /\ Nδ NFA qn h q)) (δ (automaton A) qa h) q lemma11-1 q with cond q ... | case2 end = {!!} ... | case1 qa=q = case1 (lemma11 q) where lemma11 : (q : states A ∨ states B) → exists finab (λ qn → nq qn /\ Nδ NFA qn h q) ≡ true → q ≡ case1 (δ (automaton A) qa h) lemma11 q ex = lemma17 lemma19 where lemma17 : exists finab (λ qn → nq qn /\ Nδ NFA (case1 qa) h q) ≡ true → q ≡ case1 (δ (automaton A) qa h) lemma17 en with found← finab en ... | S with found-q S | found-p S | {!!} -- qa=q (bool-∧→tt-0 ( found-p S))(found-q S) ... | (case1 qq) | e1 | eq = lemma21 q refl where lemma20 : nq (case1 qa) /\ Concat-NFA.δnfa A B (case1 qa) h q ≡ true lemma20 = {!!} lemma21 : (qq : states A ∨ states B) → qq ≡ q → q ≡ case1 (δ (automaton A) qa h) lemma21 (case1 x) refl = cong ( λ k → case1 k ) (sym lemma23 ) where lemma23 : (δ (automaton A) qa h) ≡ x lemma23 = equal→refl (afin A) ( bool-∧→tt-1 e1) lemma21 (case2 x) refl with bool-≡-? (aend (automaton A) qa) true ... | no not1 = ⊥-elim ( not1 ( bool-∧→tt-0 ( bool-∧→tt-1 e1))) ... | yes a-end = ⊥-elim ( ne (bool-and-tt a-end lemma25 ) ) where lemma24 : aend (automaton A) qa /\ (equal? (afin B) x (δ (automaton B) (astart B) h)) ≡ true lemma24 = bool-∧→tt-1 e1 lemma25 : accept (automaton B) (δ (automaton B) (astart B) h) t ≡ true lemma25 = contain-B h t nq (astart B) {!!} fn lemma18 : (qn : states A ∨ states B) → nq qn /\ Nδ NFA qn h q ≡ true → nq qn /\ Nδ NFA (case1 qa) h q ≡ true lemma18 qn eq = begin nq qn /\ Nδ NFA (case1 qa) h q ≡⟨ cong ( λ k → nq qn /\ Nδ NFA k h q ) (sym {!!}) ⟩ nq qn /\ Nδ NFA qn h q ≡⟨ eq ⟩ true ∎ where open ≡-Reasoning lemma19 : exists finab (λ qn → nq qn /\ Nδ NFA (case1 qa) h q) ≡ true lemma19 = found finab fq ( (lemma18 fq (found-p ( found← finab ex )))) where fq = found-q ( found← finab ex ) lemma14 : split (λ t1 → accept (automaton A) qa (h ∷ t1)) (contain B) t ≡ true lemma14 = contain-A t (Nmoves NFA finab nq h) fn (δ (automaton A) qa h) (nmove (case1 qa) nq qat h) lemma11-1 lemma10 : Naccept NFA finab (equal? finab (case1 (astart A))) x ≡ true → split (contain A) (contain B) x ≡ true lemma10 CC = contain-A x (Concat-NFA-start A B ) CC (astart A) (equal?-refl finab) lemma12-1 where lemma12-1 : (q : states A ∨ states B) → condition-A (Concat-NFA-start A B) (astart A) q lemma12-1 q = case1 (λ eq → sym ( equal→refl finab eq ) ) closed-in-concat← : contain (M-Concat A B) x ≡ true → Concat (contain A) (contain B) x ≡ true closed-in-concat← C with subset-construction-lemma← finab NFA (case1 (astart A)) x C ... | CC = lemma10 CC