annotate automaton-in-agda/src/regular-language.agda @ 285:6e85b8b0d8db

remove ls<n
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 28 Dec 2021 00:28:29 +0900
parents 8006cbd87b20
children 407684f806e4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
65
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 module regular-language where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 open import Level renaming ( suc to Suc ; zero to Zero )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 open import Data.List
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 open import Data.Nat hiding ( _≟_ )
70
702ce92c45ab add concat
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 69
diff changeset
6 open import Data.Fin hiding ( _+_ )
72
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 71
diff changeset
7 open import Data.Empty
101
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 100
diff changeset
8 open import Data.Unit
65
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 open import Data.Product
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 -- open import Data.Maybe
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 open import Relation.Nullary
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 open import Relation.Binary.PropositionalEquality hiding ( [_] )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 open import logic
70
702ce92c45ab add concat
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 69
diff changeset
14 open import nat
65
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 open import automaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 language : { Σ : Set } → Set
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 language {Σ} = List Σ → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 language-L : { Σ : Set } → Set
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 language-L {Σ} = List (List Σ)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 Union : {Σ : Set} → ( A B : language {Σ} ) → language {Σ}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 Union {Σ} A B x = (A x ) \/ (B x)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 split : {Σ : Set} → (List Σ → Bool)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 → ( List Σ → Bool) → List Σ → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 split x y [] = x [] /\ y []
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 split x y (h ∷ t) = (x [] /\ y (h ∷ t)) \/
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 split (λ t1 → x ( h ∷ t1 )) (λ t2 → y t2 ) t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 Concat : {Σ : Set} → ( A B : language {Σ} ) → language {Σ}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 Concat {Σ} A B = split A B
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 {-# TERMINATING #-}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 Star : {Σ : Set} → ( A : language {Σ} ) → language {Σ}
266
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
37 Star {Σ} A [] = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
38 Star {Σ} A (h ∷ t) = split A ( Star {Σ} A ) (h ∷ t)
65
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
40 open import automaton-ex
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
41
87
217ef727574a reverse direction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 86
diff changeset
42 test-AB→split : {Σ : Set} → {A B : List In2 → Bool} → split A B ( i0 ∷ i1 ∷ i0 ∷ [] ) ≡ (
69
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 65
diff changeset
43 ( A [] /\ B ( i0 ∷ i1 ∷ i0 ∷ [] ) ) \/
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 65
diff changeset
44 ( A ( i0 ∷ [] ) /\ B ( i1 ∷ i0 ∷ [] ) ) \/
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 65
diff changeset
45 ( A ( i0 ∷ i1 ∷ [] ) /\ B ( i0 ∷ [] ) ) \/
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 65
diff changeset
46 ( A ( i0 ∷ i1 ∷ i0 ∷ [] ) /\ B [] )
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 65
diff changeset
47 )
87
217ef727574a reverse direction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 86
diff changeset
48 test-AB→split {_} {A} {B} = refl
69
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 65
diff changeset
49
266
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
50 star-nil : {Σ : Set} → ( A : language {Σ} ) → Star A [] ≡ true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
51 star-nil A = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
52
267
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 266
diff changeset
53 open Automaton
268
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 267
diff changeset
54 open import finiteSet
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 267
diff changeset
55 open import finiteSetUtil
267
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 266
diff changeset
56
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 266
diff changeset
57 record RegularLanguage ( Σ : Set ) : Set (Suc Zero) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 266
diff changeset
58 field
268
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 267
diff changeset
59 states : Set
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 267
diff changeset
60 astart : states
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 267
diff changeset
61 afin : FiniteSet states
267
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 266
diff changeset
62 automaton : Automaton states Σ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 266
diff changeset
63 contain : List Σ → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 266
diff changeset
64 contain x = accept automaton astart x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 266
diff changeset
65
268
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 267
diff changeset
66 open RegularLanguage
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 267
diff changeset
67
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 267
diff changeset
68 isRegular : {Σ : Set} → (A : language {Σ} ) → ( x : List Σ ) → (r : RegularLanguage Σ ) → Set
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 267
diff changeset
69 isRegular A x r = A x ≡ contain r x
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 267
diff changeset
70
267
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 266
diff changeset
71 RegularLanguage-is-language : { Σ : Set } → RegularLanguage Σ → language {Σ}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 266
diff changeset
72 RegularLanguage-is-language {Σ} R = RegularLanguage.contain R
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 266
diff changeset
73
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 266
diff changeset
74 RegularLanguage-is-language' : { Σ : Set } → RegularLanguage Σ → List Σ → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 266
diff changeset
75 RegularLanguage-is-language' {Σ} R x = accept (automaton R) (astart R) x where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 266
diff changeset
76 open RegularLanguage
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 266
diff changeset
77
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 266
diff changeset
78 -- a language is implemented by an automaton
65
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79
126
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
80 -- postulate
a79e2c2e1642 finite done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 121
diff changeset
81 -- fin-× : {A B : Set} → { a b : ℕ } → FiniteSet A {a} → FiniteSet B {b} → FiniteSet (A × B) {a * b}
73
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 72
diff changeset
82
65
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 M-Union : {Σ : Set} → (A B : RegularLanguage Σ ) → RegularLanguage Σ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 M-Union {Σ} A B = record {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 states = states A × states B
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 ; astart = ( astart A , astart B )
268
8006cbd87b20 fix concat dfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 267
diff changeset
87 ; afin = fin-× (afin A) (afin B)
65
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 ; automaton = record {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 δ = λ q x → ( δ (automaton A) (proj₁ q) x , δ (automaton B) (proj₂ q) x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 ; aend = λ q → ( aend (automaton A) (proj₁ q) \/ aend (automaton B) (proj₂ q) )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 }
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
92 }
65
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 closed-in-union : {Σ : Set} → (A B : RegularLanguage Σ ) → ( x : List Σ ) → isRegular (Union (contain A) (contain B)) x ( M-Union A B )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 closed-in-union A B [] = lemma where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 lemma : aend (automaton A) (astart A) \/ aend (automaton B) (astart B) ≡
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 aend (automaton A) (astart A) \/ aend (automaton B) (astart B)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 lemma = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 closed-in-union {Σ} A B ( h ∷ t ) = lemma1 t ((δ (automaton A) (astart A) h)) ((δ (automaton B) (astart B) h)) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 lemma1 : (t : List Σ) → (qa : states A ) → (qb : states B ) →
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 accept (automaton A) qa t \/ accept (automaton B) qb t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 ≡ accept (automaton (M-Union A B)) (qa , qb) t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 lemma1 [] qa qb = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 lemma1 (h ∷ t ) qa qb = lemma1 t ((δ (automaton A) qa h)) ((δ (automaton B) qb h))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105