annotate automaton-in-agda/src/derive.agda @ 274:1c8ed1220489

fixes
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sat, 18 Dec 2021 16:27:46 +0900
parents f60c1041ae8e
children 407684f806e4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
1 {-# OPTIONS --allow-unsolved-metas #-}
36
9558d870e8ae add cfg and derive
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
44
aa15eff1aeb3 seprate finite
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
3 open import Relation.Binary.PropositionalEquality hiding ( [_] )
aa15eff1aeb3 seprate finite
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
4 open import Relation.Nullary using (¬_; Dec; yes; no)
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
5 open import Data.List hiding ( [_] )
271
5e066b730d73 regex cmp
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 270
diff changeset
6 open import finiteSet
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
7
271
5e066b730d73 regex cmp
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 270
diff changeset
8 module derive ( Σ : Set) ( fin : FiniteSet Σ ) ( eq? : (x y : Σ) → Dec (x ≡ y)) where
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
9
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
10 -- open import nfa
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
11 open import Data.Nat
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
12 -- open import Data.Nat hiding ( _<_ ; _>_ )
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
13 -- open import Data.Fin hiding ( _<_ )
270
dd98e7e5d4a5 derive worked but finiteness is difficult
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
14 open import finiteSetUtil
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
15 open import automaton
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
16 open import logic
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
17 open import regex
271
5e066b730d73 regex cmp
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 270
diff changeset
18 open FiniteSet
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
19
274
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 272
diff changeset
20 -- whether a regex accepts empty input
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 272
diff changeset
21 --
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
22 empty? : Regex Σ → Bool
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
23 empty? ε = true
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
24 empty? φ = false
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
25 empty? (x *) = true
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
26 empty? (x & y) = empty? x /\ empty? y
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
27 empty? (x || y) = empty? x \/ empty? y
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
28 empty? < x > = false
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
29
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
30 derivative : Regex Σ → Σ → Regex Σ
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
31 derivative ε s = φ
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
32 derivative φ s = φ
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
33 derivative (x *) s with derivative x s
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
34 ... | ε = x *
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
35 ... | φ = φ
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
36 ... | t = t & (x *)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
37 derivative (x & y) s with empty? x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
38 ... | true with derivative x s | derivative y s
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
39 ... | ε | φ = φ
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
40 ... | ε | t = y || t
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
41 ... | φ | t = t
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
42 ... | x1 | φ = x1 & y
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
43 ... | x1 | y1 = (x1 & y) || y1
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
44 derivative (x & y) s | false with derivative x s
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
45 ... | ε = y
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
46 ... | φ = φ
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
47 ... | t = t & y
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
48 derivative (x || y) s with derivative x s | derivative y s
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
49 ... | φ | y1 = y1
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
50 ... | x1 | φ = x1
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
51 ... | x1 | y1 = x1 || y1
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
52 derivative < x > s with eq? x s
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
53 ... | yes _ = ε
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
54 ... | no _ = φ
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
55
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
56 data regex-states (x : Regex Σ ) : Regex Σ → Set where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
57 unit : regex-states x x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
58 derive : { y : Regex Σ } → regex-states x y → (s : Σ) → regex-states x ( derivative y s )
44
aa15eff1aeb3 seprate finite
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 36
diff changeset
59
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
60 record Derivative (x : Regex Σ ) : Set where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
61 field
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
62 state : Regex Σ
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
63 is-derived : regex-states x state
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
64
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
65 open Derivative
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
66
270
dd98e7e5d4a5 derive worked but finiteness is difficult
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
67 open import Data.Fin hiding (_<_)
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
68
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
69 -- derivative generates (x & y) || ... form. y and x part is a substerm of original regex
270
dd98e7e5d4a5 derive worked but finiteness is difficult
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
70 -- since subterm is finite, only finite number of state is generated for each operator
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
71 -- this does not work, becuase it depends on input sequences
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
72 -- finite-derivative : (r : Regex Σ) → FiniteSet Σ → FiniteSet (Derivative r)
270
dd98e7e5d4a5 derive worked but finiteness is difficult
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
73 -- order : Regex Σ → ℕ
dd98e7e5d4a5 derive worked but finiteness is difficult
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
74 -- decline-derive : (x : Regex Σ ) (i : Σ ) → 0 < order x → order (derivative x i) < order x
dd98e7e5d4a5 derive worked but finiteness is difficult
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
75 -- is not so easy
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
76 -- in case of automaton, number of derivative is limited by iteration of input length, so it is finite.
270
dd98e7e5d4a5 derive worked but finiteness is difficult
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
77 -- so we cannot say derived automaton is finite i.e. regex-match is regular language now
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
78
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
79 regex→automaton : (r : Regex Σ) → Automaton (Derivative r) Σ
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
80 regex→automaton r = record { δ = λ d s → record { state = derivative (state d) s ; is-derived = derive-step d s} ; aend = λ d → empty? (state d) } where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
81 derive-step : (d0 : Derivative r) → (s : Σ) → regex-states r (derivative (state d0) s)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
82 derive-step d0 s = derive (is-derived d0) s
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
83
270
dd98e7e5d4a5 derive worked but finiteness is difficult
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
84 regex-match : (r : Regex Σ) → (List Σ) → Bool
dd98e7e5d4a5 derive worked but finiteness is difficult
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
85 regex-match ex is = accept ( regex→automaton ex ) record { state = ex ; is-derived = unit } is
271
5e066b730d73 regex cmp
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 270
diff changeset
86
5e066b730d73 regex cmp
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 270
diff changeset
87 open import Relation.Binary.Definitions
5e066b730d73 regex cmp
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 270
diff changeset
88
272
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
89 cmp-regex : (x y : Regex Σ) → Dec ( x ≡ y )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
90 cmp-regex ε ε = yes refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
91 cmp-regex ε φ = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
92 cmp-regex ε (y *) = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
93 cmp-regex ε (y & y₁) = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
94 cmp-regex ε (y || y₁) = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
95 cmp-regex ε < x > = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
96 cmp-regex φ ε = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
97 cmp-regex φ φ = yes refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
98 cmp-regex φ (y *) = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
99 cmp-regex φ (y & y₁) = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
100 cmp-regex φ (y || y₁) = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
101 cmp-regex φ < x > = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
102 cmp-regex (x *) ε = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
103 cmp-regex (x *) φ = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
104 cmp-regex (x *) (y *) with cmp-regex x y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
105 ... | yes refl = yes refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
106 ... | no ne = no (λ x → ne (cmp1 x)) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
107 cmp1 : (x *) ≡ (y *) → x ≡ y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
108 cmp1 refl = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
109 cmp-regex (x *) (y & y₁) = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
110 cmp-regex (x *) (y || y₁) = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
111 cmp-regex (x *) < x₁ > = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
112 cmp-regex (x & x₁) ε = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
113 cmp-regex (x & x₁) φ = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
114 cmp-regex (x & x₁) (y *) = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
115 cmp-regex (x & x₁) (y & y₁) with cmp-regex x y | cmp-regex x₁ y₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
116 ... | yes refl | yes refl = yes refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
117 ... | no ne | _ = no (λ x → ne (cmp1 x)) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
118 cmp1 : x & x₁ ≡ y & y₁ → x ≡ y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
119 cmp1 refl = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
120 ... | _ | no ne = no (λ x → ne (cmp1 x)) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
121 cmp1 : x & x₁ ≡ y & y₁ → x₁ ≡ y₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
122 cmp1 refl = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
123 cmp-regex (x & x₁) (y || y₁) = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
124 cmp-regex (x & x₁) < x₂ > = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
125 cmp-regex (x || x₁) ε = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
126 cmp-regex (x || x₁) φ = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
127 cmp-regex (x || x₁) (y *) = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
128 cmp-regex (x || x₁) (y & y₁) = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
129 cmp-regex (x || x₁) (y || y₁) with cmp-regex x y | cmp-regex x₁ y₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
130 ... | yes refl | yes refl = yes refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
131 ... | no ne | _ = no (λ x → ne (cmp1 x)) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
132 cmp1 : x || x₁ ≡ y || y₁ → x ≡ y
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
133 cmp1 refl = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
134 ... | _ | no ne = no (λ x → ne (cmp1 x)) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
135 cmp1 : x || x₁ ≡ y || y₁ → x₁ ≡ y₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
136 cmp1 refl = refl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
137 cmp-regex (x || x₁) < x₂ > = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
138 cmp-regex < x > ε = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
139 cmp-regex < x > φ = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
140 cmp-regex < x > (y *) = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
141 cmp-regex < x > (y & y₁) = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
142 cmp-regex < x > (y || y₁) = no (λ ())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
143 cmp-regex < x > < x₁ > with equal? fin x x₁ | inspect ( equal? fin x ) x₁
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
144 ... | false | record { eq = eq } = no (λ x → ¬-bool eq (cmp1 x)) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
145 cmp1 : < x > ≡ < x₁ > → equal? fin x x₁ ≡ true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
146 cmp1 refl = equal?-refl fin
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
147 ... | true | record { eq = eq } with equal→refl fin eq
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
148 ... | refl = yes refl
271
5e066b730d73 regex cmp
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 270
diff changeset
149
272
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
150 insert : List (Regex Σ) → (Regex Σ) → List (Regex Σ)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
151 insert [] k = k ∷ []
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
152 insert (x ∷ t) k with cmp-regex k x
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
153 ... | no n = x ∷ (insert t k)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
154 ... | yes y = x ∷ t
271
5e066b730d73 regex cmp
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 270
diff changeset
155
272
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
156 regex-derive : List (Regex Σ) → List (Regex Σ)
271
5e066b730d73 regex cmp
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 270
diff changeset
157 regex-derive t = regex-derive0 t t where
272
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
158 regex-derive1 : Regex Σ → List Σ → List (Regex Σ) → List (Regex Σ)
271
5e066b730d73 regex cmp
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 270
diff changeset
159 regex-derive1 x [] t = t
5e066b730d73 regex cmp
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 270
diff changeset
160 regex-derive1 x (i ∷ t) r = regex-derive1 x t (insert r (derivative x i))
272
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
161 regex-derive0 : List (Regex Σ) → List (Regex Σ) → List (Regex Σ)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
162 regex-derive0 [] t = t
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 271
diff changeset
163 regex-derive0 (x ∷ r) t = regex-derive0 r (regex-derive1 x (to-list fin (λ _ → true)) t)
271
5e066b730d73 regex cmp
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 270
diff changeset
164
274
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 272
diff changeset
165 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 272
diff changeset
166 -- if (Derivative r is finite, regex→automaton is finite
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 272
diff changeset
167 --
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 272
diff changeset
168 drive-is-finite : (r : Regex Σ) → FiniteSet (Derivative r)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 272
diff changeset
169 drive-is-finite ε = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 272
diff changeset
170 drive-is-finite φ = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 272
diff changeset
171 drive-is-finite (r *) = {!!} where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 272
diff changeset
172 d1 : FiniteSet (Derivative r )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 272
diff changeset
173 d1 = drive-is-finite r
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 272
diff changeset
174 drive-is-finite (r & r₁) = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 272
diff changeset
175 drive-is-finite (r || r₁) = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 272
diff changeset
176 drive-is-finite < x > = {!!}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 272
diff changeset
177