annotate a05/lecture.ind @ 83:92f396c3a1d7

add end function
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 09 Nov 2019 00:05:05 +0900
parents a7f09c9a2c7a
children a3fb231feeb9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 -title: 正規表現
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 --受理集合と演算
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 automaton で受理されるのは文字列。あるautomatonnは受理される文字列の集合を決める。
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 文字列の集合への演算を考えよう
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 x 文字列そのもの
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 x+y 文字列の結合
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 * 文字列の繰り返し
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 x|y 文字列の選択
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 これらを使って文字列の集合を決めると、それは文字列に対するパターンマッチとなる。
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 これを正規表現という。
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 ---正規表現の例
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 + は省略しても良いことにしよう。a+b+c の代わりに、
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 abc
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 文字集合の要素を全部選択したものを . と書くことにしよう。(Shell では?が多い。* は .* を意味する)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 .*abc
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 .*abcabc
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 曖昧さを避けるために()を使う。
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 (abc|bcd)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 .*(abc|bcd)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 --問題5.1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 egrep / Perl などを使って、これらのパターンに対するテストプログラムを作成せよ。C で書くとどうなるか?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 C の regcomp を使ってみよ。
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 Option : 実行時間を測定してみよう。
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 --正規表現を表すデータ構造
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 再帰的な構文木を表すには data を使うことができる。
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 data Regex ( Σ : Set ) : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 _* : Regex Σ → Regex Σ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 _&_ : Regex Σ → Regex Σ → Regex Σ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 _||_ : Regex Σ → Regex Σ → Regex Σ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 <_> : Σ → Regex Σ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 List Σ を用いて < 文字列 > とすることもできるが、基本的なデータ構造をなるべく簡単にすることにしよう。
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 <a href="../agda/regex1.agda"> regex1.agda </a>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 上の例題は、
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 < a > & < b > & < c >
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 any = a || b || c
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 ( any * ) & ( < a > & < b > & < c > )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 ( any * ) & ( < a > & < b > & < c > & < a > & < b > & < c > )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 ( < a > & < b > & < c > ) || ( < b > & < c > & < d > )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 ( any * ) & ( < a > & < b > & < c > || < b > & < c > & < d > )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 となる。
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 --正規言語
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 ある正規表現が受け付ける文字列の集合を正規言語という。これは文字列の集合全体 ( List Σ )の部分集合になる。
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 部分集合は Bool への関数として表すことができる。
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 List Σ → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 正規言語は以下の論理式で表すことができる。
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 regular-language : {Σ : Set} → Regex Σ → List Σ → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 Regex Σはdataなので場合分けとなる。
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 例えば、
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 _‖_ : {Σ : Set} ( x y : List Σ → Bool) → List Σ → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 x ‖ y = λ s → x s ∨ y s
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 regular-language (x || y) f = ( regular-language x f ) ‖ ( regular-language y f )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 -- < a >
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 もし、Σがデータなら (例えば In2 )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 regular-language < h > f (i0 ∷ [] ) = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 regular-language < h > f (i1 ∷ [] ) = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 regular-language < h > f _ = false
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 で良い。そうっでないなら、
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 cmp : (x y : Σ )→ Dec ( x ≡ y )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 みたいなのがあれば、
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108 regular-language < h > (h1 ∷ [] ) with cmp h h1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 ... | yes _ = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 ... | no _ = false
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 regular-language < h > _ = false
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 と書ける。Dec は、条件分岐を理由付きで得るためのもの。理由がないと証明できない。
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 in Relation.Nullary
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
116
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 ¬_ : ∀ {ℓ} → Set ℓ → Set ℓ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 ¬ P = P → ⊥
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
119
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 -- Decidable relations.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 data Dec {p} (P : Set p) : Set p where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 yes : ( p : P) → Dec P
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 no : (¬p : ¬ P) → Dec P
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
125
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 --Finite Set
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
127
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 有限集合 Fin n は、
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
129
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 test1 : Fin 2 → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 test1 0 → true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 test1 1 → true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 test1 2 → true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
134
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 などのように使いたい。0,1,2 は Data.Nat なのでだめである。Agda には Data.Fin が用意されている。
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
136
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 data Fin : ℕ → Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 zero : {n : ℕ} → Fin (suc n)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
139 suc : {n : ℕ} (i : Fin n) → Fin (suc n)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
140
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
141 実際に test1 がどのようになるかを Agda で確認すると、
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
142
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 test4 : Fin 2 → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
144 test4 zero = { }0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 test4 (suc zero) = { }1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
146 test4 (suc (suc ()))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
147
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
148 ここで () はあり得ない入力を表す。あり得ないので body は空である。
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
149
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 (Agda での否定を思い出そう)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
151
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 --Finite Set の構成
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
153
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 <a href="../agda/nfa.agda"> nfa.agda </a>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
155
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
156 record FiniteSet ( Q : Set ) { n : ℕ }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
157 : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 Q←F : Fin n → Q
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
160 F←Q : Q → Fin n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
161 finiso→ : (q : Q) → Q←F ( F←Q q ) ≡ q
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
162 finiso← : (f : Fin n ) → F←Q ( Q←F f ) ≡ f
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
163 lt0 : (n : ℕ) → n Data.Nat.≤ n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
164 lt0 zero = z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
165 lt0 (suc n) = s≤s (lt0 n)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
166 lt2 : {m n : ℕ} → m < n → m Data.Nat.≤ n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
167 lt2 {zero} lt = z≤n
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
168 lt2 {suc m} {zero} ()
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
169 lt2 {suc m} {suc n} (s≤s lt) = s≤s (lt2 lt)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
170 exists : ( Q → Bool ) → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
171 exists p = exists1 n (lt0 n) p where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 exists1 : (m : ℕ ) → m Data.Nat.≤ n → ( Q → Bool ) → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
173 exists1 zero _ _ = false
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
174 exists1 ( suc m ) m<n p = p (Q←F (fromℕ≤ {m} {n} m<n)) ∨ exists1 m (lt2 m<n) p
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
175
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
176 finState1 : FiniteSet States1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 finState1 = record {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
178 Q←F = Q←F
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
179 ; F←Q = F←Q
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
180 ; finiso→ = finiso→
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
181 ; finiso← = finiso←
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
182 } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
183 Q←F : Fin 3 → States1
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
184 Q←F zero = sr
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
185 Q←F (suc zero) = ss
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
186 Q←F (suc (suc zero)) = st
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
187 Q←F (suc (suc (suc ())))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 F←Q : States1 → Fin 3
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
189 F←Q sr = zero
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
190 F←Q ss = suc (zero)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 F←Q st = suc ( suc zero )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 finiso→ : (q : States1) → Q←F (F←Q q) ≡ q
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 finiso→ sr = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 finiso→ ss = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
195 finiso→ st = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
196 finiso← : (f : Fin 3) → F←Q (Q←F f) ≡ f
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 finiso← zero = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
198 finiso← (suc zero) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
199 finiso← (suc (suc zero)) = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
200 finiso← (suc (suc (suc ())))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
201
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
202
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
203
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
204 -- x & y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
205
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
206 これは
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
208 split : {Σ : Set} → (List Σ → Bool)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
209 → ( List Σ → Bool) → List Σ → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
210
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
211 があれば、
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
212
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
213 _・_ : {Σ : Set} → ( x y : List Σ → Bool) → List Σ → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
214 x ・ y = λ z → split x y z
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
215
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
216 regular-language (x & y) f = ( regular-language x f ) ・ ( regular-language y f )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
217
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
218 -- x & y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
219
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 repeat : {Σ : Set} → (List Σ → Bool) → List Σ → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 repeat x [] = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
222 repeat {Σ} x ( h ∷ t ) = split x (repeat {Σ} x) ( h ∷ t )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
223
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
224 regular-language (x *) f = repeat ( regular-language x f )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
225
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
226
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
227 --split
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
228
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
229 split : {Σ : Set} → (List Σ → Bool)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
230 → ( List Σ → Bool) → List Σ → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
231 split x y [] = x [] ∧ y []
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
232 split x y (h ∷ t) = (x [] ∧ y (h ∷ t)) ∨
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
233 split (λ t1 → x ( ( h ∷ [] ) ++ t1 )) (λ t2 → y t2 ) t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
234
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
235
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
236 --問題5.2 - 5.7
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
237
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
238 いくつかの正規表現に関する例題
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
239
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
240 <a href="../exercise/002.html"> 問題5.2 - 5.7 </a> <!--- Exercise 5.2 --->
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
241
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
242 <a href=""> </a> <!--- Exercise 5.3 --->
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
243 <a href=""> </a> <!--- Exercise 5.4 --->
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
244 <a href=""> </a> <!--- Exercise 5.5 --->
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
245 <a href=""> </a> <!--- Exercise 5.6 --->
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
246 <a href=""> </a> <!--- Exercise 5.7 --->
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
247
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
248
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
249
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
250
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
251
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
252
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
253
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
254
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
255
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
256
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
257
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
258
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
259
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
260
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
261