annotate agda/regex.agda @ 5:f3de819eb3d0

on going ...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 15 Aug 2018 11:25:46 +0900
parents 843f155846fb
children 8e8bca295494
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 Σ
0
e5aa1cf746cb automaton lecture
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 <_> : List Σ → Regex Σ
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
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
57 listcase : Maybe Σ → List Σ → ℕ → List ℕ
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
58 listcase nothing _ _ = []
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
59 listcase (just q) [] n = []
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
60 listcase (just q) ( q' ∷ t ) n with q ≟ q'
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
61 ... | yes _ = [ n ]
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
62 ... | no _ = listcase (just q) t n
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
63 generate : ( Regex Σ ) → RNFA Σ → RNFA Σ
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
64 generate (x *) R = record R' { Rstart = record (Rstart R') { move = move0 } ;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
65 Rend = record (Rend R') { move = move1 } }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
66 where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
67 R' : RNFA Σ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
68 R' = generate x R
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
69 move0 : Maybe Σ → List ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
70 move0 nothing = mergeℕ (move (Rstart R') nothing) [ state (Rend R') ]
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
71 move0 (just x) = move (Rstart R') (just x )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
72 move1 : Maybe Σ → List ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
73 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
74 move1 (just x) = move (Rstart R') (just x )
5
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
75 generate (x & y) R = record R1 { Rend = Rend R2 ;
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
76 Rstates = Rstates R1 ++ [ Rend R1 ] ++ [ Rstart R2 ] ++ Rstates R2 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
77 where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
78 R2 : RNFA Σ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
79 R2 = generate y R
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
80 R1 : RNFA Σ
5
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
81 R1 = generate x ( record R2 { Rstart = record (Rstart R1) { move = move0 } ; Rend = record (Rend R1) { move = move1 } } )
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
82 move0 : Maybe Σ → List ℕ
5
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
83 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
84 move0 (just x) = move (Rstart R1) (just x )
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
85 move1 : Maybe Σ → List ℕ
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
86 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
87 move1 (just x) = move (Rend R1) (just x )
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
88 generate (x || y) R = record R1 { Rstart = Rstart R1 ; Rend = Rend R2 ;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
89 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
90 where
5
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
91 S1 : RST Σ
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
92 S1 = record { state = Rnum R ; RSTend = RSTend R ; move = λ q → [] }
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
93 R1 = generate x ( record R { Rnum = Rnum R +1 } )
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
94 R2 = generate y ( record R1 { Rstart = S1 ; Rend = S1 } )
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
95 move0 : Maybe Σ → List ℕ
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
96 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
97 move0 (just x) = move (Rstart R1) (just x )
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
98 move1 : Maybe Σ → List ℕ
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
99 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
100 move1 (just x) = move (Rend R1) (just x )
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
101 generate < x > R = record R {
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
102 Rnum = Rnum R + 1 ;
5
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
103 Rstart = record {
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
104 state = Rnum R
4
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 3
diff changeset
105 ; move = λ q → listcase q x ( state (Rstart R) )
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
106 ; RSTend = false
5
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
107 }
f3de819eb3d0 on going ...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 4
diff changeset
108 Rstates = Rstart R ∷ Rstate R }
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
109
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
110 regex2nfa : (regex : Regex In2 ) → εAutomaton (RST In2) In2
2
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
111 regex2nfa regex = record {
3
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
112 εδ = λ s i → {!!} -- move s i
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
113 ; εid = λ s → state s
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
114 ; allState = Rstates ( generateRNFA regex ieq )
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
115 ; εstart = {!!} -- Rstart ( generateRNFA regex ieq )
3ac6311293ec εAutomaton
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 2
diff changeset
116 ; εend = λ s → RSTend s }
1
3c6de7cf2a95 nfa worked
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
117