annotate automaton-in-agda/src/derive.agda @ 340:b818fbd86d0b

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 10 Jul 2023 14:28:39 +0900
parents 40f7b6c3d276
children 9120a5872a5b
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
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
10 open import automaton
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
11 open import logic
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
12 open import regex
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
13
274
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 272
diff changeset
14 -- whether a regex accepts empty input
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 272
diff changeset
15 --
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
16 empty? : Regex Σ → Bool
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
17 empty? ε = true
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
18 empty? φ = false
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
19 empty? (x *) = true
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
20 empty? (x & y) = empty? x /\ empty? y
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
21 empty? (x || y) = empty? x \/ empty? y
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
22 empty? < x > = false
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
23
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
24 derivative : Regex Σ → Σ → Regex Σ
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
25 derivative ε s = φ
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
26 derivative φ s = φ
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
27 derivative (x *) s with derivative x s
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
28 ... | ε = x *
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 ... | t = t & (x *)
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
31 derivative (x & y) s with empty? x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
32 ... | true with derivative x s | derivative y s
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
33 ... | ε | φ = φ
335
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
34 ... | ε | t = t || y
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
35 ... | φ | t = t
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
36 ... | x1 | φ = x1 & y
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
37 ... | x1 | y1 = (x1 & y) || y1
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
38 derivative (x & y) s | false with derivative x s
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
39 ... | ε = y
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
40 ... | φ = φ
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
41 ... | t = t & y
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
42 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
43 ... | φ | y1 = y1
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
44 ... | x1 | φ = x1
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
45 ... | x1 | y1 = x1 || y1
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
46 derivative < x > s with eq? x s
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
47 ... | yes _ = ε
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
48 ... | no _ = φ
138
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 45
diff changeset
49
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
50 data regex-states (x : Regex Σ ) : Regex Σ → Set where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
51 unit : regex-states x x
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
52 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
53
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
54 record Derivative (x : Regex Σ ) : Set where
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
55 field
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
56 state : Regex Σ
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
57 is-derived : regex-states x state
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
58
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
59 open Derivative
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
60
335
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
61 derive-step : (r : Regex Σ) (d0 : Derivative r) → (s : Σ) → regex-states r (derivative (state d0) s)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
62 derive-step r d0 s = derive (is-derived d0) s
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
63
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
64 regex→automaton : (r : Regex Σ) → Automaton (Derivative r) Σ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
65 regex→automaton r = record { δ = λ d s → record { state = derivative (state d) s ; is-derived = derive-step r d s}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
66 ; aend = λ d → empty? (state d) }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
67
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
68 regex-match : (r : Regex Σ) → (List Σ) → Bool
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
69 regex-match ex is = accept ( regex→automaton ex ) record { state = ex ; is-derived = unit } is
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
70
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
71 -- open import nfa
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
72 open import Data.Nat
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
73 -- open import Data.Nat hiding ( _<_ ; _>_ )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
74 -- open import Data.Fin hiding ( _<_ )
336
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 335
diff changeset
75 open import nat
335
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
76 open import finiteSetUtil
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
77 open FiniteSet
270
dd98e7e5d4a5 derive worked but finiteness is difficult
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 183
diff changeset
78 open import Data.Fin hiding (_<_)
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
79
336
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 335
diff changeset
80 -- finiteness of derivative
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 335
diff changeset
81 -- term generate x & y for each * and & only once
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 335
diff changeset
82 -- rank : Regex → ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 335
diff changeset
83 -- r₀ & r₁ ... r
337
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 336
diff changeset
84 -- generated state is a subset of the term set
141
b3f05cd08d24 clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 138
diff changeset
85
271
5e066b730d73 regex cmp
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 270
diff changeset
86 open import Relation.Binary.Definitions
5e066b730d73 regex cmp
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 270
diff changeset
87
336
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 335
diff changeset
88 rank : (r : Regex Σ) → ℕ
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 335
diff changeset
89 rank ε = 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 335
diff changeset
90 rank φ = 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 335
diff changeset
91 rank (r *) = suc (rank r)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 335
diff changeset
92 rank (r & r₁) = suc (max (rank r) (rank r₁))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 335
diff changeset
93 rank (r || r₁) = max (rank r) (rank r₁)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 335
diff changeset
94 rank < x > = 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 335
diff changeset
95
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 335
diff changeset
96 data SB : (r : Regex Σ) → (rn : ℕ) → Set where
339
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 338
diff changeset
97 sbε : (r : Regex Σ) → SB ε 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 338
diff changeset
98 sbφ : (r : Regex Σ) → SB φ 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 338
diff changeset
99 sb<> : (s : Σ) → SB < s > 0
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 338
diff changeset
100 sb| : (x y : Regex Σ) → (xn yn xy : ℕ) → SB x xn → SB y yn → xy ≡ max xn yn → SB (x || y) xy
336
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 335
diff changeset
101 sb* : (x : Regex Σ) → (xn : ℕ) → SB (x & (x *)) (suc xn)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 335
diff changeset
102 sb& : (x y : Regex Σ) → (xn yn : ℕ) → xn < yn → SB (x & y) (suc yn)
271
5e066b730d73 regex cmp
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 270
diff changeset
103
338
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
104 SBTA : (r : Regex Σ) → Automaton (SB r (rank r) → Bool) Σ
335
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
105 SBTA = ?
271
5e066b730d73 regex cmp
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 270
diff changeset
106
338
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
107 open import bijection using ( InjectiveF ; Is )
336
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 335
diff changeset
108
338
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
109 finSBTA : (r : Regex Σ) → FiniteSet (SB r (rank r) → Bool)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
110 finSBTA r = fin→ ( fb00 (rank r) r refl ) where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
111 fb00 : (n : ℕ ) → (r : Regex Σ) → rank r ≡ n → FiniteSet (SB r (rank r))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
112 fb00 zero ε eq = ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
113 fb00 zero φ eq = ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
114 fb00 zero (r || r₁) eq = ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
115 fb00 zero < x > eq = ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
116 fb00 (suc n) (r *) eq = ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
117 fb00 (suc n) (r & r₁) eq = ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
118 fb00 (suc n) (r || r₁) eq = ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
119
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
120 injSB : (r : Regex Σ) → InjectiveF (Derivative r) (SB r (rank r) → Bool)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
121 injSB r = record { f = fb01 (rank r) r refl ; inject = ? } where
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
122 fb01 : (n : ℕ ) → (r : Regex Σ) → rank r ≡ n → Derivative r → SB r (rank r) → Bool
340
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 339
diff changeset
123 fb01 n r eq d sb with is-derived d
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 339
diff changeset
124 fb01 n r eq d sb | unit = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 339
diff changeset
125 fb01 zero .ε eq d (sbε r) | derive {y} ss i = true
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 339
diff changeset
126 fb01 zero .φ eq d (sbφ r) | derive {y} ss i = false
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 339
diff changeset
127 fb01 zero .(< s >) eq d (sb<> s) | derive {y} ss i = ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 339
diff changeset
128 fb01 zero .(x || y) eq d (sb| x y xn yn .(rank (x || y)) sb sb₁ x₁) | derive {y₂} ss i = ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 339
diff changeset
129 fb01 (suc n) .(x || y) eq d (sb| x y xn yn .(rank (x || y)) sb sb₁ x₁) | derive {y₂} ss i = ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 339
diff changeset
130 fb01 (suc n) .(x & (x *)) eq d (sb* x .(max (rank x) (rank (x *)))) | derive {y₂} ss i = ?
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 339
diff changeset
131 fb01 (suc n) .(x & y) eq d (sb& x y xn .(max (rank x) (rank y)) x₁) | derive {y₂} ss i = ?
338
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
132
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
133 finite-derivative : (r : Regex Σ) → FiniteSet (Derivative r)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
134 finite-derivative r = inject-fin (finSBTA r) (injSB r) ?
335
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
135
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
136
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 330
diff changeset
137
338
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 337
diff changeset
138