annotate agda/regex.agda @ 6:8e8bca295494

regex wrote
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 15 Aug 2018 11:59:36 +0900
parents f3de819eb3d0
children f1fbe6603066
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
0
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
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
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
29 field
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
30 state : ℕ
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
31 move : Maybe Σ → List ℕ
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
32 RSTend : Bool
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
33
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
34 open RST
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
35
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
36 record RNFA ( Σ : Set )
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
37 : Set where
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
38 field
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
39 Rstates : List ( RST Σ )
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
40 Rstart : RST Σ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
41 Rend : RST Σ
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
42 Rnum : ℕ
5
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
43
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
44 open RNFA
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
45
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
46 mergeℕ : List ℕ → List ℕ → List ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
47 mergeℕ [] L = L
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
48 mergeℕ (h ∷ t ) L with member h L
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
49 ... | true = mergeℕ t L
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
50 ... | false = h ∷ mergeℕ t L
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
51
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
52 generateRNFA : { Σ : Set } → ( Regex Σ ) → (_≟_ : ( q q' : Σ ) → Dec ( q ≡ q' ) ) → RNFA Σ
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
53 generateRNFA {Σ} regex _≟_ = generate regex (
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
54 record { Rstates = [] ; Rstart = record { state = 0 ; move = λ q → [] ; RSTend = true } ;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
55 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
56 where
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
57 literal : Maybe Σ → Σ → ℕ → List ℕ
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
58 literal nothing _ _ = []
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
59 literal (just q) q' n with q ≟ q'
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
60 ... | yes _ = [ n ]
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
61 ... | no _ = []
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
62 generate : ( Regex Σ ) → RNFA Σ → RNFA Σ
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
63 generate (x *) R = record R' { Rstart = record (Rstart R') { move = move0 } ;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
64 Rend = record (Rend R') { move = move1 } }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
65 where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
66 R' : RNFA Σ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
67 R' = generate x R
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
68 move0 : Maybe Σ → List ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
69 move0 nothing = mergeℕ (move (Rstart R') nothing) [ state (Rend R') ]
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
70 move0 (just x) = move (Rstart R') (just x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
71 move1 : Maybe Σ → List ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
72 move1 nothing = mergeℕ (move (Rstart R') nothing) ( state (Rend R') ∷ state (Rend R') ∷ [] )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
73 move1 (just x) = move (Rstart R') (just x )
5
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
74 generate (x & y) R = record R1 { Rend = Rend R2 ;
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
75 Rstates = Rstates R1 ++ [ Rend R1 ] ++ [ Rstart R2 ] ++ Rstates R2 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
76 where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
77 R2 : RNFA Σ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
78 R2 = generate y R
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
79 R1 : RNFA Σ
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
80 R1 = generate x ( record R2 { Rstart = Rstart R2 ; Rend = Rstart R2 } )
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
81 generate (x || y) R = record R1 { Rstart = Rstart R1 ; Rend = Rend R2 ;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
82 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
83 where
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
84 R1 : RNFA Σ
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
85 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
86 S1 : RST Σ
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
87 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
88 R2 : RNFA Σ
5
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
89 R2 = generate y ( record R1 { Rstart = S1 ; Rend = S1 } )
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
90 move0 : Maybe Σ → List ℕ
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
91 move0 nothing = mergeℕ (move (Rstart R1) nothing) [ state (Rstart R2) ]
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
92 move0 (just x) = move (Rstart R1) (just x )
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
93 move1 : Maybe Σ → List ℕ
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
94 move1 nothing = mergeℕ (move (Rend R1) nothing) [ state (Rend R2) ]
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
95 move1 (just x) = move (Rend R1) (just x )
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
96 generate < x > R = record R {
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
97 Rnum = Rnum R + 1 ;
5
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
98 Rstart = record {
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
99 state = Rnum R
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
100 ; move = λ q → literal q x ( state (Rstart R) )
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
101 ; RSTend = false
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
102 } ;
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
103 Rstates = Rstart R ∷ Rstates R }
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
104
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
105 moveRegex : { Σ : Set } → List (RST Σ) → ( RST Σ → ℕ ) → RST Σ → Maybe Σ → List (RST Σ)
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
106 moveRegex { Σ} L sid s i = toRST ( move s i )
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
107 where
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
108 toRST : List ℕ → List ( RST Σ )
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
109 toRST [] = []
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
110 toRST (h ∷ t ) with findSbyNum h L sid
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
111 ... | nothing = toRST t
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
112 ... | just s = s ∷ toRST t
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
113
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
114
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
115 regex2nfa : (regex : Regex In2 ) → εAutomaton (RST In2) In2
2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
116 regex2nfa regex = record {
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
117 εδ = moveRegex ( Rstates G ) state
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
118 ; εid = λ s → state s
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
119 ; allState = Rstates G
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
120 ; εstart = Rstart G
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
121 ; εend = λ s → RSTend s }
6
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
122 where
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
123 G : RNFA In2
8e8bca295494 regex wrote
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 5
diff changeset
124 G = generateRNFA regex ieq
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
125