Mercurial > hg > Members > kono > Proof > automaton
annotate agda/nfa-list.agda @ 63:abfeed0c61b5
...
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 30 Oct 2019 12:07:29 +0900 |
parents | aa15eff1aeb3 |
children |
rev | line source |
---|---|
24 | 1 module nfa-list where |
0 | 2 |
3 open import Level renaming ( suc to succ ; zero to Zero ) | |
2 | 4 open import Data.Nat |
13
02b4ecc9972c
start exp version of subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
5 open import Data.List |
02b4ecc9972c
start exp version of subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
6 open import Data.Maybe |
0 | 7 open import Data.Bool using ( Bool ; true ; false ) |
3 | 8 open import Relation.Binary.PropositionalEquality hiding ( [_] ) |
9 | 9 open import Relation.Nullary using (¬_; Dec; yes; no) |
0 | 10 |
11 data States1 : Set where | |
1 | 12 sr : States1 |
13 ss : States1 | |
14 st : States1 | |
0 | 15 |
16 data In2 : Set where | |
17 i0 : In2 | |
18 i1 : In2 | |
19 | |
13
02b4ecc9972c
start exp version of subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
20 transition1 : States1 → In2 → States1 |
1 | 21 transition1 sr i0 = sr |
22 transition1 sr i1 = ss | |
23 transition1 ss i0 = sr | |
24 transition1 ss i1 = st | |
25 transition1 st i0 = sr | |
26 transition1 st i1 = st | |
0 | 27 |
13
02b4ecc9972c
start exp version of subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
28 fin1 : States1 → Bool |
1 | 29 fin1 st = true |
0 | 30 fin1 _ = false |
31 | |
2 | 32 s1id : States1 → ℕ |
33 s1id sr = 0 | |
34 s1id ss = 1 | |
35 s1id st = 2 | |
0 | 36 |
13
02b4ecc9972c
start exp version of subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
37 record NAutomaton ( Q : Set ) ( Σ : Set ) |
0 | 38 : Set where |
1 | 39 field |
40 nδ : Q → Σ → List Q | |
2 | 41 sid : Q → ℕ |
1 | 42 nstart : Q |
13
02b4ecc9972c
start exp version of subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
43 nend : Q → Bool |
1 | 44 |
45 open NAutomaton | |
0 | 46 |
13
02b4ecc9972c
start exp version of subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
47 insert : { Q : Set } { Σ : Set } → ( NAutomaton Q Σ ) → List Q → Q → List Q |
2 | 48 insert M [] q = q ∷ [] |
49 insert M ( q ∷ T ) q' with (sid M q ) ≟ (sid M q') | |
50 ... | yes _ = insert M T q' | |
51 ... | no _ = q ∷ (insert M T q' ) | |
0 | 52 |
13
02b4ecc9972c
start exp version of subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
53 merge : { Q : Set } { Σ : Set } → ( NAutomaton Q Σ ) → List Q → List Q → List Q |
2 | 54 merge M L1 [] = L1 |
13
02b4ecc9972c
start exp version of subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
55 merge M L1 ( H ∷ L ) = insert M (merge M L1 L ) H |
0 | 56 |
13
02b4ecc9972c
start exp version of subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
57 Nmoves : { Q : Set } { Σ : Set } |
02b4ecc9972c
start exp version of subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
58 → NAutomaton Q Σ |
0 | 59 → List Q → List Σ → List Q |
1 | 60 Nmoves {Q} { Σ} M q L = Nmoves1 q L [] |
0 | 61 where |
62 Nmoves1 : (q : List Q ) ( L : List Σ ) → List Q → List Q | |
63 Nmoves1 q [] _ = q | |
64 Nmoves1 [] (_ ∷ L) LQ = Nmoves1 LQ L [] | |
2 | 65 Nmoves1 (q ∷ T ) (H ∷ L) LQ = Nmoves1 T (H ∷ L) ( merge M ( nδ M q H ) LQ ) |
0 | 66 |
67 | |
13
02b4ecc9972c
start exp version of subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
68 Naccept : { Q : Set } { Σ : Set } |
02b4ecc9972c
start exp version of subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
69 → NAutomaton Q Σ |
0 | 70 → List Σ → Bool |
1 | 71 Naccept {Q} { Σ} M L = |
72 checkAccept ( Nmoves M ((nstart M) ∷ [] ) L ) | |
0 | 73 where |
74 checkAccept : (q : List Q ) → Bool | |
75 checkAccept [] = false | |
1 | 76 checkAccept ( H ∷ L ) with (nend M) H |
0 | 77 ... | true = true |
78 ... | false = checkAccept L | |
79 | |
80 | |
13
02b4ecc9972c
start exp version of subset construction
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
9
diff
changeset
|
81 transition2 : States1 → In2 → List States1 |
1 | 82 transition2 sr i0 = (sr ∷ []) |
83 transition2 sr i1 = (ss ∷ sr ∷ [] ) | |
84 transition2 ss i0 = (sr ∷ []) | |
85 transition2 ss i1 = (st ∷ []) | |
86 transition2 st i0 = (sr ∷ []) | |
87 transition2 st i1 = (st ∷ []) | |
0 | 88 |
1 | 89 am2 : NAutomaton States1 In2 |
2 | 90 am2 = record { nδ = transition2 ; sid = s1id ; nstart = sr ; nend = fin1} |
0 | 91 |
92 | |
93 example2-1 = Naccept am2 ( i0 ∷ i1 ∷ i0 ∷ [] ) | |
94 example2-2 = Naccept am2 ( i1 ∷ i1 ∷ i1 ∷ [] ) | |
95 |