Mercurial > hg > Members > kono > Proof > automaton
annotate automaton-in-agda/src/regular-language.agda @ 410:db02b6938e04
StarProp and Ntrace
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 22 Nov 2023 17:07:01 +0900 |
parents | 4e4acdc43dee |
children | 207e6c4e155c |
rev | line source |
---|---|
405 | 1 {-# OPTIONS --cubical-compatible --safe #-} |
2 | |
65 | 3 module regular-language where |
4 | |
5 open import Level renaming ( suc to Suc ; zero to Zero ) | |
6 open import Data.List | |
7 open import Data.Nat hiding ( _≟_ ) | |
70 | 8 open import Data.Fin hiding ( _+_ ) |
72 | 9 open import Data.Empty |
101 | 10 open import Data.Unit |
65 | 11 open import Data.Product |
12 -- open import Data.Maybe | |
13 open import Relation.Nullary | |
14 open import Relation.Binary.PropositionalEquality hiding ( [_] ) | |
15 open import logic | |
70 | 16 open import nat |
65 | 17 open import automaton |
18 | |
19 language : { Σ : Set } → Set | |
20 language {Σ} = List Σ → Bool | |
21 | |
22 language-L : { Σ : Set } → Set | |
23 language-L {Σ} = List (List Σ) | |
24 | |
25 Union : {Σ : Set} → ( A B : language {Σ} ) → language {Σ} | |
330 | 26 Union {Σ} A B x = A x \/ B x |
65 | 27 |
330 | 28 split : {Σ : Set} → (x y : language {Σ} ) → language {Σ} |
65 | 29 split x y [] = x [] /\ y [] |
30 split x y (h ∷ t) = (x [] /\ y (h ∷ t)) \/ | |
31 split (λ t1 → x ( h ∷ t1 )) (λ t2 → y t2 ) t | |
32 | |
33 Concat : {Σ : Set} → ( A B : language {Σ} ) → language {Σ} | |
34 Concat {Σ} A B = split A B | |
35 | |
405 | 36 -- {-# TERMINATING #-} |
37 -- Star1 : {Σ : Set} → ( A : language {Σ} ) → language {Σ} | |
38 -- Star1 {Σ} A [] = true | |
39 -- Star0 {Σ} A (h ∷ t) = split A ( Star1 {Σ} A ) (h ∷ t) | |
383
708570e55a91
add regex2 (we need source reorganization)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
330
diff
changeset
|
40 |
401 | 41 -- Terminating version of Star1 |
42 -- | |
384 | 43 repeat : {Σ : Set} → (x : List Σ → Bool) → (y : List Σ ) → Bool |
401 | 44 repeat2 : {Σ : Set} → (x : List Σ → Bool) → (pre y : List Σ ) → Bool |
410 | 45 repeat2 x pre [] = true |
401 | 46 repeat2 x pre (h ∷ y) = |
47 (x (pre ++ (h ∷ [])) /\ repeat x y ) | |
48 \/ repeat2 x (pre ++ (h ∷ [])) y | |
49 | |
384 | 50 repeat {Σ} x [] = true |
401 | 51 repeat {Σ} x (h ∷ y) = repeat2 x [] (h ∷ y) |
383
708570e55a91
add regex2 (we need source reorganization)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
330
diff
changeset
|
52 |
384 | 53 Star : {Σ : Set} → (x : List Σ → Bool) → (y : List Σ ) → Bool |
54 Star {Σ} x y = repeat x y | |
65 | 55 |
406 | 56 -- We have to prove definitions of Concat and Star are equivalent to the set theoretic definitions |
57 | |
58 -- 1. A ∪ B = { x | x ∈ A \/ x ∈ B } | |
59 -- 2. A ∘ B = { x | ∃ y ∈ A, z ∈ B, x = y ++ z } | |
60 -- 3. A* = { x | ∃ n ∈ ℕ, y1, y2, ..., yn ∈ A, x = y1 ++ y2 ++ ... ++ yn } | |
61 | |
62 -- lemma-Union : {Σ : Set} → ( A B : language {Σ} ) → ( x : List Σ ) → Union A B x ≡ ( A x \/ B x ) | |
63 -- lemma-Union = ? | |
64 | |
65 -- lemma-Concat : {Σ : Set} → ( A B : language {Σ} ) → ( x : List Σ ) | |
66 -- → Concat A B x ≡ ( ∃ λ y → ∃ λ z → A y /\ B z /\ x ≡ y ++ z ) | |
67 -- lemma-Concat = ? | |
68 | |
141 | 69 open import automaton-ex |
70 | |
87 | 71 test-AB→split : {Σ : Set} → {A B : List In2 → Bool} → split A B ( i0 ∷ i1 ∷ i0 ∷ [] ) ≡ ( |
69 | 72 ( A [] /\ B ( i0 ∷ i1 ∷ i0 ∷ [] ) ) \/ |
73 ( A ( i0 ∷ [] ) /\ B ( i1 ∷ i0 ∷ [] ) ) \/ | |
74 ( A ( i0 ∷ i1 ∷ [] ) /\ B ( i0 ∷ [] ) ) \/ | |
75 ( A ( i0 ∷ i1 ∷ i0 ∷ [] ) /\ B [] ) | |
76 ) | |
87 | 77 test-AB→split {_} {A} {B} = refl |
69 | 78 |
266 | 79 star-nil : {Σ : Set} → ( A : language {Σ} ) → Star A [] ≡ true |
80 star-nil A = refl | |
81 | |
267 | 82 open Automaton |
268 | 83 open import finiteSet |
84 open import finiteSetUtil | |
267 | 85 |
86 record RegularLanguage ( Σ : Set ) : Set (Suc Zero) where | |
87 field | |
268 | 88 states : Set |
89 astart : states | |
90 afin : FiniteSet states | |
267 | 91 automaton : Automaton states Σ |
92 contain : List Σ → Bool | |
93 contain x = accept automaton astart x | |
94 | |
268 | 95 open RegularLanguage |
96 | |
97 isRegular : {Σ : Set} → (A : language {Σ} ) → ( x : List Σ ) → (r : RegularLanguage Σ ) → Set | |
98 isRegular A x r = A x ≡ contain r x | |
99 | |
267 | 100 RegularLanguage-is-language : { Σ : Set } → RegularLanguage Σ → language {Σ} |
101 RegularLanguage-is-language {Σ} R = RegularLanguage.contain R | |
102 | |
103 RegularLanguage-is-language' : { Σ : Set } → RegularLanguage Σ → List Σ → Bool | |
104 RegularLanguage-is-language' {Σ} R x = accept (automaton R) (astart R) x where | |
105 open RegularLanguage | |
106 | |
107 -- a language is implemented by an automaton | |
65 | 108 |
126 | 109 -- postulate |
110 -- fin-× : {A B : Set} → { a b : ℕ } → FiniteSet A {a} → FiniteSet B {b} → FiniteSet (A × B) {a * b} | |
73 | 111 |
65 | 112 M-Union : {Σ : Set} → (A B : RegularLanguage Σ ) → RegularLanguage Σ |
113 M-Union {Σ} A B = record { | |
114 states = states A × states B | |
115 ; astart = ( astart A , astart B ) | |
268 | 116 ; afin = fin-× (afin A) (afin B) |
65 | 117 ; automaton = record { |
118 δ = λ q x → ( δ (automaton A) (proj₁ q) x , δ (automaton B) (proj₂ q) x ) | |
119 ; aend = λ q → ( aend (automaton A) (proj₁ q) \/ aend (automaton B) (proj₂ q) ) | |
120 } | |
141 | 121 } |
65 | 122 |
123 closed-in-union : {Σ : Set} → (A B : RegularLanguage Σ ) → ( x : List Σ ) → isRegular (Union (contain A) (contain B)) x ( M-Union A B ) | |
124 closed-in-union A B [] = lemma where | |
125 lemma : aend (automaton A) (astart A) \/ aend (automaton B) (astart B) ≡ | |
126 aend (automaton A) (astart A) \/ aend (automaton B) (astart B) | |
127 lemma = refl | |
128 closed-in-union {Σ} A B ( h ∷ t ) = lemma1 t ((δ (automaton A) (astart A) h)) ((δ (automaton B) (astart B) h)) where | |
129 lemma1 : (t : List Σ) → (qa : states A ) → (qb : states B ) → | |
130 accept (automaton A) qa t \/ accept (automaton B) qb t | |
131 ≡ accept (automaton (M-Union A B)) (qa , qb) t | |
132 lemma1 [] qa qb = refl | |
133 lemma1 (h ∷ t ) qa qb = lemma1 t ((δ (automaton A) qa h)) ((δ (automaton B) qb h)) | |
134 | |
406 | 135 |
136 | |
137 record Split {Σ : Set} (A : List Σ → Bool ) ( B : List Σ → Bool ) (x : List Σ ) : Set where | |
138 field | |
139 sp0 sp1 : List Σ | |
140 sp-concat : sp0 ++ sp1 ≡ x | |
141 prop0 : A sp0 ≡ true | |
142 prop1 : B sp1 ≡ true | |
143 | |
144 open Split | |
145 | |
408 | 146 AB→split1 : {Σ : Set} → (A B : List Σ → Bool ) → ( x y : List Σ ) → {z : List Σ} → A x ≡ true → B y ≡ true → z ≡ x ++ y → Split A B z |
147 AB→split1 {Σ} A B x y {z} ax by z=xy = record { sp0 = x ; sp1 = y ; sp-concat = sym z=xy ; prop0 = ax ; prop1 = by } | |
148 | |
406 | 149 list-empty++ : {Σ : Set} → (x y : List Σ) → x ++ y ≡ [] → (x ≡ [] ) ∧ (y ≡ [] ) |
408 | 150 list-empty++ [] [] _ = record { proj1 = refl ; proj2 = refl } |
406 | 151 list-empty++ [] (x ∷ y) () |
152 list-empty++ (x ∷ x₁) y () | |
153 | |
154 open _∧_ | |
155 | |
156 open import Relation.Binary.PropositionalEquality hiding ( [_] ) | |
157 | |
158 c-split-lemma : {Σ : Set} → (A B : List Σ → Bool ) → (h : Σ) → ( t : List Σ ) → split A B (h ∷ t ) ≡ true | |
159 → ( ¬ (A [] ≡ true )) ∨ ( ¬ (B ( h ∷ t ) ≡ true) ) | |
160 → split (λ t1 → A (h ∷ t1)) B t ≡ true | |
161 c-split-lemma {Σ} A B h t eq p = sym ( begin | |
162 true | |
163 ≡⟨ sym eq ⟩ | |
164 split A B (h ∷ t ) | |
165 ≡⟨⟩ | |
166 A [] /\ B (h ∷ t) \/ split (λ t1 → A (h ∷ t1)) B t | |
167 ≡⟨ cong ( λ k → k \/ split (λ t1 → A (h ∷ t1)) B t ) (lemma-p p ) ⟩ | |
168 false \/ split (λ t1 → A (h ∷ t1)) B t | |
169 ≡⟨ bool-or-1 refl ⟩ | |
170 split (λ t1 → A (h ∷ t1)) B t | |
171 ∎ ) where | |
172 open ≡-Reasoning | |
173 lemma-p : ( ¬ (A [] ≡ true )) ∨ ( ¬ (B ( h ∷ t ) ≡ true) ) → A [] /\ B (h ∷ t) ≡ false | |
174 lemma-p (case1 ¬A ) = bool-and-1 ( ¬-bool-t ¬A ) | |
175 lemma-p (case2 ¬B ) = bool-and-2 ( ¬-bool-t ¬B ) | |
176 | |
177 split→AB : {Σ : Set} → (A B : List Σ → Bool ) → ( x : List Σ ) → split A B x ≡ true → Split A B x | |
178 split→AB {Σ} A B [] eq with bool-≡-? (A []) true | bool-≡-? (B []) true | |
179 split→AB {Σ} A B [] eq | yes eqa | yes eqb = | |
180 record { sp0 = [] ; sp1 = [] ; sp-concat = refl ; prop0 = eqa ; prop1 = eqb } | |
181 split→AB {Σ} A B [] eq | yes p | no ¬p = ⊥-elim (lemma-∧-1 eq (¬-bool-t ¬p )) | |
182 split→AB {Σ} A B [] eq | no ¬p | t = ⊥-elim (lemma-∧-0 eq (¬-bool-t ¬p )) | |
183 split→AB {Σ} A B (h ∷ t ) eq with bool-≡-? (A []) true | bool-≡-? (B (h ∷ t )) true | |
184 ... | yes px | yes py = record { sp0 = [] ; sp1 = h ∷ t ; sp-concat = refl ; prop0 = px ; prop1 = py } | |
185 ... | no px | _ with split→AB (λ t1 → A ( h ∷ t1 )) B t (c-split-lemma A B h t eq (case1 px) ) | |
186 ... | S = record { sp0 = h ∷ sp0 S ; sp1 = sp1 S ; sp-concat = cong ( λ k → h ∷ k) (sp-concat S) ; prop0 = prop0 S ; prop1 = prop1 S } | |
187 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) ) | |
188 ... | S = record { sp0 = h ∷ sp0 S ; sp1 = sp1 S ; sp-concat = cong ( λ k → h ∷ k) (sp-concat S) ; prop0 = prop0 S ; prop1 = prop1 S } | |
189 | |
190 AB→split : {Σ : Set} → (A B : List Σ → Bool ) → ( x y : List Σ ) → A x ≡ true → B y ≡ true → split A B (x ++ y ) ≡ true | |
191 AB→split {Σ} A B [] [] eqa eqb = begin | |
408 | 192 split A B [] ≡⟨⟩ |
193 A [] /\ B [] ≡⟨ cong₂ (λ j k → j /\ k ) eqa eqb ⟩ | |
406 | 194 true |
195 ∎ where open ≡-Reasoning | |
196 AB→split {Σ} A B [] (h ∷ y ) eqa eqb = begin | |
408 | 197 split A B (h ∷ y ) ≡⟨⟩ |
198 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 ⟩ | |
199 true /\ true \/ split (λ t1 → A (h ∷ t1)) B y ≡⟨⟩ | |
200 true \/ split (λ t1 → A (h ∷ t1)) B y ≡⟨⟩ | |
201 true ∎ where open ≡-Reasoning | |
406 | 202 AB→split {Σ} A B (h ∷ t) y eqa eqb = begin |
408 | 203 split A B ((h ∷ t) ++ y) ≡⟨⟩ |
406 | 204 A [] /\ B (h ∷ t ++ y) \/ split (λ t1 → A (h ∷ t1)) B (t ++ y) |
205 ≡⟨ cong ( λ k → A [] /\ B (h ∷ t ++ y) \/ k ) (AB→split {Σ} (λ t1 → A (h ∷ t1)) B t y eqa eqb ) ⟩ | |
408 | 206 A [] /\ B (h ∷ t ++ y) \/ true ≡⟨ bool-or-3 ⟩ |
207 true ∎ where open ≡-Reasoning | |
208 | |
406 | 209 |
409 | 210 split→AB1 : {Σ : Set} → (A B : List Σ → Bool ) → ( x : List Σ ) → Split A B x → split A B x ≡ true |
211 split→AB1 {Σ} A B x S = subst (λ k → split A B k ≡ true ) (sp-concat S) ( AB→split A B _ _ (prop0 S) (prop1 S) ) | |
212 | |
408 | 213 |
410 | 214 -- low of exclude middle of Split A B x |
215 lemma-concat : {Σ : Set} → ( A B : language {Σ} ) → (x : List Σ) → Split A B x ∨ ( ¬ Split A B x ) | |
216 lemma-concat {Σ} A B x with split A B x in eq | |
217 ... | true = case1 (split→AB A B x eq ) | |
218 ... | false = case2 (λ p → ¬-bool eq (split→AB1 A B x p )) | |
219 | |
220 -- Concat : {Σ : Set} → ( A B : language {Σ} ) → language {Σ} | |
221 -- Concat {Σ} A B = split A B | |
222 | |
223 Concat' : {Σ : Set} → ( A B : language {Σ} ) → (x : List Σ) → Set | |
224 Concat' {Σ} A B = λ x → Split A B x | |
225 | |
226 record StarProp {Σ : Set} (A : List Σ → Bool ) (x : List Σ ) : Set where | |
227 field | |
228 spn : List ( List Σ ) | |
229 spn-concat : foldr (λ k → k ++_ ) [] spn ≡ x | |
230 propn : foldr (λ k → λ j → A k /\ j ) true spn ≡ true | |
231 | |
232 open StarProp | |
233 | |
234 Star→StarProp : {Σ : Set} → ( A : language {Σ} ) → (x : List Σ) → Star A x ≡ true → StarProp A x | |
235 Star→StarProp = ? | |
236 | |
237 StarProp→Star : {Σ : Set} → ( A : language {Σ} ) → (x : List Σ) → StarProp A x → Star A x ≡ true | |
238 StarProp→Star {Σ} A x sp = subst (λ k → Star A k ≡ true ) (spsx (spn sp) refl) ( sps1 (spn sp) refl ) where | |
239 spsx : (y : List ( List Σ ) ) → spn sp ≡ y → foldr (λ k → k ++_ ) [] y ≡ x | |
240 spsx y refl = spn-concat sp | |
241 sps1 : (y : List ( List Σ ) ) → spn sp ≡ y → Star A (foldr (λ k → k ++_ ) [] y) ≡ true | |
242 sps1 = ? | |
243 | |
244 | |
245 lemma-starprop : {Σ : Set} → ( A : language {Σ} ) → (x : List Σ) → StarProp A x ∨ ( ¬ StarProp A x ) | |
246 lemma-starprop = ? | |
247 |