annotate a04/lecture.ind @ 69:f124fceba460

subset construction
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 06 Nov 2019 17:18:58 +0900
parents a7f09c9a2c7a
children fba1cd54581d
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 例えば、ドアの鍵がテンキーだったら、次に何を入れるかには自由度がある。
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 record NAutomaton ( Q : Set ) ( Σ : Set )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 : Set where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 field
69
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
12 Nδ : Q → Σ → Q → Bool
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
13 Nend : Q → Bool
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 これを非決定性オートマトンという。Agda では同じ名前を使いまわしすることはできない(monomorpphism)ので、nを付けた。
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16
69
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
17 <a href="../agda/nfa.agda"> nfa.agda </a>
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
69
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
19 状態の受理と遷移は以下のようになる。
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 Nmoves : { Q : Set } { Σ : Set }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 → NAutomaton Q Σ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 → {n : ℕ } → FiniteSet Q {n}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 → ( Q → Bool ) → Σ → Q → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 Nmoves {Q} { Σ} M fin Qs s q =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 exists fin ( λ qn → (Qs qn ∧ ( Nδ M qn s q ) ))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27
69
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
28 Naccept : { Q : Set } { Σ : Set }
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 → NAutomaton Q Σ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 → {n : ℕ } → FiniteSet Q {n}
69
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
31 → (Nstart : Q → Bool) → List Σ → Bool
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
32 Naccept M fin sb [] = exists fin ( λ q → sb q ∧ Nend M q )
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
33 Naccept M fin sb (i ∷ t ) = Naccept M fin ( Nmoves M fin sb i ) t
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
34
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
35 次の状態は状態の集合(List)になる。次の次の状態は、可能な状態の次の状態の集合を合わせた(union)ものになる。
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
36 しかし、List で定義すると少し複雑になる。
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
37
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
38 部分集合を使うと簡単になる。Q の部分集合は Q → Bool で true になるものであるとする。
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
39
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 --例題
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 transition3 : States1 → In2 → States1 → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 transition3 sr i0 sr = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 transition3 sr i1 ss = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 transition3 sr i1 sr = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 transition3 ss i0 sr = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 transition3 ss i1 st = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 transition3 st i0 sr = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 transition3 st i1 st = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 transition3 _ _ _ = false
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 fin1 : States1 → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54 fin1 st = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 fin1 ss = false
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 fin1 sr = false
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 start1 : States1 → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 start1 sr = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 start1 _ = false
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 am2 : NAutomaton States1 In2
69
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
63 am2 = record { Nδ = transition3 ; Nend = fin1}
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
64
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
65 example2-1 = Naccept am2 finState1 start1 ( i0 ∷ i1 ∷ i0 ∷ [] )
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
66 example2-2 = Naccept am2 finState1 start1 ( i1 ∷ i1 ∷ i1 ∷ [] )
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67
69
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
68 fin0 : States1 → Bool
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
69 fin0 st = false
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
70 fin0 ss = false
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
71 fin0 sr = false
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72
69
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
73 test0 : Bool
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
74 test0 = exists finState1 fin0
37
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75
69
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
76 test1 : Bool
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
77 test1 = exists finState1 fin1
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
78
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
79 test2 = Nmoves am2 finState1 start1
f124fceba460 subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 37
diff changeset
80