annotate agda/regex.agda @ 7:f1fbe6603066

εAutomaton using Tree
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 15 Aug 2018 14:37:11 +0900
parents 8e8bca295494
children cdf75ae6f0c1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 module regex where
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 open import Level renaming ( suc to succ ; zero to Zero )
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
4 -- open import Data.Fin
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
5 open import Data.Nat
7
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
6 -- open import Data.List
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
7 open import Data.Maybe
0
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 open import Data.Bool using ( Bool ; true ; false )
2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
9 open import Relation.Binary.PropositionalEquality hiding ( [_] )
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
10 open import Relation.Nullary using (¬_; Dec; yes; no)
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
11
0
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 data Regex ( Σ : Set ) : Set where
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 _* : Regex Σ → Regex Σ
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 _&_ : Regex Σ → Regex Σ → Regex Σ
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
16 _||_ : Regex Σ → Regex Σ → Regex Σ
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
17 <_> : Σ → Regex Σ
0
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 -- data In2 : Set where
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 -- a : In2
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 -- b : In2
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 open import automaton
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
26
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
27 record RST ( Σ : Set )
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
28 : Set where
7
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
29 inductive
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
30 field
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
31 state : ℕ
7
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
32 move : Maybe Σ → Tree (RST Σ)
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
33 RSTend : Bool
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
34
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
35 open RST
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
36
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
37 record RNFA ( Σ : Set )
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
38 : Set where
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
39 field
7
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
40 Rstates : Tree ( RST Σ )
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
41 Rstart : RST Σ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
42 Rend : RST Σ
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
43 Rnum : ℕ
5
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
44
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
45 open RNFA
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
46
7
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
47 [_] : { Σ : Set} → RST Σ → Tree (RST Σ )
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
48 [ x ] = leaf ( state x ) x
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
49
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
50 [] : { Σ : Set} → Tree (RST Σ )
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
51 [] = empty
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
52
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
53
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
54 infixr 5 _++_
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
55
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
56 _++_ : { Σ : Set} → Tree (RST Σ ) → Tree (RST Σ ) → Tree (RST Σ )
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
57 empty ++ t = t
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
58 leaf n e ++ t = insertT n e t
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
59 node n e left right ++ t =
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
60 left ++ ( insertT n e (right ++ t ) )
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
61
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
62 generateRNFA : { Σ : Set } → ( Regex Σ ) → (_≟_ : ( q q' : Σ ) → Dec ( q ≡ q' ) ) → RNFA Σ
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
63 generateRNFA {Σ} regex _≟_ = generate regex (
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
64 record { Rstates = [] ; Rstart = record { state = 0 ; move = λ q → [] ; RSTend = true } ;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
65 Rend = record { state = 0 ; move = λ q → [] ; RSTend = true } ; Rnum = 1 } )
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
66 where
7
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
67 literal : Maybe Σ → Σ → ℕ → Tree (RST Σ)
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
68 literal nothing q' n = []
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
69 literal (just q) q' n with q ≟ q'
7
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
70 ... | yes _ = [ record { state = n ; move = λ i → empty ; RSTend = true } ]
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
71 ... | no _ = []
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
72 generate : ( Regex Σ ) → RNFA Σ → RNFA Σ
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
73 generate (x *) R = record R' { Rstart = record (Rstart R') { move = move0 } ;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
74 Rend = record (Rend R') { move = move1 } }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
75 where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
76 R' : RNFA Σ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
77 R' = generate x R
7
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
78 move0 : Maybe Σ → Tree (RST Σ)
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
79 move0 (just x) = move (Rstart R') (just x )
7
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
80 move0 nothing = move (Rstart R') nothing ++ [ Rend R' ]
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
81 move1 : Maybe Σ → Tree (RST Σ)
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
82 move1 (just x) = move (Rstart R') (just x )
7
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
83 move1 nothing = move (Rstart R') nothing ++ [ Rstart R' ] ++ [ Rend R' ]
5
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
84 generate (x & y) R = record R1 { Rend = Rend R2 ;
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
85 Rstates = Rstates R1 ++ [ Rend R1 ] ++ [ Rstart R2 ] ++ Rstates R2 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
86 where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
87 R2 : RNFA Σ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
88 R2 = generate y R
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
89 R1 : RNFA Σ
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
90 R1 = generate x ( record R2 { Rstart = Rstart R2 ; Rend = Rstart R2 } )
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
91 generate (x || y) R = record R1 { Rstart = Rstart R1 ; Rend = Rend R2 ;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
92 Rstates = [ Rstart R1 ] ++ Rstates R1 ++ [ Rend R1 ] ++ [ Rstart R2 ] ++ Rstates R2 ++ [ Rend R2 ] }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
93 where
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
94 R1 : RNFA Σ
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
95 R1 = generate x ( record R { Rnum = Rnum R + 1 } )
5
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
96 S1 : RST Σ
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
97 S1 = record { state = Rnum R ; RSTend = RSTend (Rend R) ; move = λ q → [] }
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
98 R2 : RNFA Σ
5
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
99 R2 = generate y ( record R1 { Rstart = S1 ; Rend = S1 } )
7
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
100 move0 : Maybe Σ → Tree (RST Σ)
5
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
101 move0 (just x) = move (Rstart R1) (just x )
7
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
102 move0 nothing = move (Rstart R1) nothing ++ [ Rstart R2 ]
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
103 move1 : Maybe Σ → Tree (RST Σ)
5
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
104 move1 (just x) = move (Rend R1) (just x )
7
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
105 move1 nothing = move (Rend R1) nothing ++ [ Rend R2 ]
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
106 generate < x > R = record R {
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
107 Rnum = Rnum R + 1 ;
5
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
108 Rstart = record {
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
109 state = Rnum R
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
110 ; move = λ q → literal q x ( state (Rstart R) )
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
111 ; RSTend = false
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
112 } ;
7
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
113 Rstates = [ Rstart R ] ++ Rstates R
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
114 }
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
115
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
116 regex2nfa : (regex : Regex In2 ) → εAutomaton (RST In2) In2
2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
117 regex2nfa regex = record {
7
f1fbe6603066 εAutomaton using Tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 6
diff changeset
118 εδ = move
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
119 ; εid = λ s → state s
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
120 ; allState = Rstates G
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
121 ; εstart = Rstart G
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
122 ; εend = λ s → RSTend s }
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
123 where
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
124 G : RNFA In2
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
125 G = generateRNFA regex ieq
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
126