Mercurial > hg > Members > kono > Proof > automaton
view agda/regular-language.agda @ 101:37a38f1d8d0d
...
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 12 Nov 2019 18:54:44 +0900 |
parents | 0b1b9a28a707 |
children | 137d39e3dc7d |
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.Unit 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) ) 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 data AB-state (nq : states A ∨ states B → Bool ) (qa : states A) (x : List Σ) : Set (Level.suc Level.zero) where state-A : ((q : states A ∨ states B ) → nq q ≡ true → q ≡ case1 qa ) → AB-state nq qa x state-AB : ((q : states A ∨ states B ) → ( nq q ≡ true ) → ( (q ≡ case1 qa) ∨ ({qb : states B} → nq (case2 qb) ≡ true → ( accept (automaton B) qb x ≡ false ) ))) → AB-state nq qa x open AB-state contain-A : (x : List Σ) → (nq : states A ∨ states B → Bool ) → (fn : Naccept NFA finab nq x ≡ true ) → (qa : states A ) → AB-state nq qa x → split (accept (automaton A) qa ) (contain B) x ≡ true contain-A [] nq fn qa cond with found← finab fn | found-q (found← finab fn) | cond | inspect found-q (found← finab fn) contain-A [] nq fn qa cond | S | s | state-A nq=t | record { eq = refl } with nq=t (found-q S) (bool-∧→tt-0 (found-p S)) ... | refl = bool-∧→tt-1 (found-p S) contain-A [] nq fn qa cond | S | s | state-AB cond-b | _ with cond-b (found-q S) (bool-∧→tt-0 (found-p S)) contain-A [] nq fn qa cond | S | s | state-AB cond-b | _ | case1 refl = bool-∧→tt-1 (found-p S) contain-A [] nq fn qa cond | S | case2 qb | state-AB cond-b | record { eq = refl } | case2 accept-B = ⊥-elim ( lemma11 accept-B ) where lemma11 : ( nq (case2 qb ) ≡ true → aend (automaton B) qb ≡ false ) → ⊥ lemma11 accept-B = ¬-bool ( accept-B (bool-∧→tt-0 (found-p S))) (bool-∧→tt-1 (found-p S )) contain-A [] nq fn qa cond | S | case1 qa' | state-AB cond-b | record { eq = refl } | case2 accept-B = {!!} contain-A (h ∷ t) nq fn qa 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) {!!} ) where 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) {!!} where 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