Skip to content

Commit ef56402

Browse files
move replace function into its own file
1 parent f69b1ca commit ef56402

File tree

3 files changed

+144
-136
lines changed

3 files changed

+144
-136
lines changed

Validator/Regex.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ import Validator.Regex.Num
77
import Validator.Regex.Pair
88
import Validator.Regex.Regex
99
import Validator.Regex.RegexID
10+
import Validator.Regex.Replace
1011
import Validator.Regex.Smart
1112
import Validator.Regex.Symbol

Validator/Regex/Replace.lean

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
import Validator.Regex.Num
2+
import Validator.Regex.Regex
3+
import Validator.Regex.RegexID
4+
5+
namespace Symbol
6+
7+
def replace (r: RegexID n) (xs: Vec σ l) (h: n <= l): Regex σ :=
8+
match r with
9+
| Regex.emptyset => Regex.emptyset
10+
| Regex.emptystr => Regex.emptystr
11+
| Regex.symbol s => Regex.symbol (Vec.get xs (Fin.mk s.val (by
12+
cases s
13+
simp only
14+
omega
15+
)))
16+
| Regex.or r1 r2 =>
17+
Regex.or (replace r1 xs h) (replace r2 xs h)
18+
| Regex.concat r1 r2 =>
19+
Regex.concat (replace r1 xs h) (replace r2 xs h)
20+
| Regex.star r1 =>
21+
Regex.star (replace r1 xs h)
22+
23+
def replaceFrom (r: RegexID n) (xs: Vec σ n): Regex σ :=
24+
replace r xs (le_refl n)
25+
26+
def replacesFrom (rs: Vec (RegexID n) l) (xs: Vec σ n): Vec (Regex σ) l :=
27+
Vec.map rs (fun r => replaceFrom r xs)
28+
29+
30+
theorem replace_cast_both (r: RegexID n) (xs: Vec σ n) (h: n = l):
31+
replace r xs (by omega) = replace (RegexID.cast r h) (Vec.cast xs h) (by omega) := by
32+
subst h
33+
simp only [Vec.cast_rfl]
34+
rfl
35+
36+
theorem replaceFrom_cast_both (r: RegexID n) (xs: Vec σ n) (h: n = l):
37+
replaceFrom r xs = replaceFrom (RegexID.cast r h) (Vec.cast xs h) := by
38+
unfold replaceFrom
39+
rw [replace_cast_both]
40+
41+
theorem replace_cast_symbols (r: RegexID n) (xs: Vec σ n) (h: n = l):
42+
replace r xs (by omega) = replace r (Vec.cast xs h) (by omega) := by
43+
subst h
44+
simp only [Vec.cast_rfl]
45+
46+
theorem replace_nil_is_map_id (r: Regex (Fin 0)):
47+
replace r Vec.nil (by simp) = Regex.map r id := by
48+
induction r with
49+
| emptyset =>
50+
simp only [replace, Regex.map]
51+
| emptystr =>
52+
simp only [replace, Regex.map]
53+
| symbol s =>
54+
nomatch s
55+
| or r1 r2 ih1 ih2 =>
56+
simp only [replace, Regex.map, Regex.or.injEq]
57+
rw [ih1]
58+
rw [ih2]
59+
apply And.intro rfl rfl
60+
| concat r1 r2 ih1 ih2 =>
61+
simp only [replace, Regex.map, Regex.concat.injEq]
62+
rw [ih1]
63+
rw [ih2]
64+
apply And.intro rfl rfl
65+
| star r1 ih1 =>
66+
simp only [replace, Regex.map]
67+
rw [ih1]
68+
69+
theorem replace_take (r: RegexID n) (xs: Vec σ (n + l)):
70+
replace r (Vec.take n xs) (by omega) = replace r xs (by omega):= by
71+
induction r with
72+
| emptyset =>
73+
simp only [replace]
74+
| emptystr =>
75+
simp only [replace]
76+
| symbol s =>
77+
generalize_proofs h1 h2 at *
78+
simp only [replace]
79+
obtain ⟨s, hs⟩ := s
80+
simp only [Regex.symbol.injEq]
81+
generalize_proofs h3 h4
82+
rw [Vec.take_get]
83+
omega
84+
| or r1 r2 ih1 ih2 =>
85+
simp only [replace, Regex.or.injEq]
86+
generalize_proofs h1 h2 at *
87+
rw [<- ih1]
88+
rw [<- ih2]
89+
apply And.intro rfl rfl
90+
| concat r1 r2 ih1 ih2 =>
91+
simp only [replace, Regex.concat.injEq]
92+
generalize_proofs h1 h2 at *
93+
rw [<- ih1]
94+
rw [<- ih2]
95+
apply And.intro rfl rfl
96+
| star r1 ih1 =>
97+
simp only [replace]
98+
generalize_proofs h1 at *
99+
rw [<- ih1]
100+
101+
theorem replace_regexid_add (r: RegexID n) (xs: Vec σ (n + l)):
102+
replace r xs (by omega) = replace (RegexID.add l r) xs (by omega):= by
103+
generalize_proofs h1 h2
104+
induction r with
105+
| emptyset =>
106+
simp only [replace, RegexID.add, Regex.map]
107+
| emptystr =>
108+
simp only [replace, RegexID.add, Regex.map]
109+
| symbol s =>
110+
generalize_proofs h1 h2 at *
111+
simp only [replace, RegexID.add, Regex.map, Fin.coe_castLE]
112+
| or r1 r2 ih1 ih2 =>
113+
simp only [replace, RegexID.add, Regex.map, Regex.or.injEq]
114+
generalize_proofs h1 h2 at *
115+
rw [ih1]
116+
rw [ih2]
117+
apply And.intro rfl rfl
118+
| concat r1 r2 ih1 ih2 =>
119+
simp only [replace, RegexID.add, Regex.map, Regex.concat.injEq]
120+
generalize_proofs h1 h2 at *
121+
rw [ih1]
122+
rw [ih2]
123+
apply And.intro rfl rfl
124+
| star r1 ih1 =>
125+
simp only [replace, RegexID.add, Regex.map, Regex.star.injEq]
126+
generalize_proofs h1 h2 at *
127+
rw [ih1]
128+
rfl
129+
130+
theorem replacesFrom_cast_both (rs: Vec (RegexID n) l) (ss: Vec σ n) (h: n = m):
131+
replacesFrom rs ss =
132+
replacesFrom (RegexID.casts rs h) (Vec.cast ss h) := by
133+
subst h
134+
simp only [Vec.cast_rfl]
135+
simp only [RegexID.casts_rfl]
136+
137+
theorem replacesFrom_cons (rs: Vec (RegexID n) l) (xs: Vec σ n):
138+
replacesFrom (Vec.cons r rs) xs
139+
= Vec.cons (replaceFrom r xs) (replacesFrom rs xs)
140+
:= by
141+
simp only [replacesFrom]
142+
simp only [Vec.map]

Validator/Regex/Symbol.lean

Lines changed: 1 addition & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Validator.Regex.Num
66
import Validator.Regex.Pair
77
import Validator.Regex.Regex
88
import Validator.Regex.RegexID
9+
import Validator.Regex.Replace
910

1011
-- I want to define a map function over a regular expression:
1112

@@ -36,25 +37,6 @@ import Validator.Regex.RegexID
3637

3738
namespace Symbol
3839

39-
def replace (r: RegexID n) (xs: Vec σ l) (h: n <= l): Regex σ :=
40-
match r with
41-
| Regex.emptyset => Regex.emptyset
42-
| Regex.emptystr => Regex.emptystr
43-
| Regex.symbol s => Regex.symbol (Vec.get xs (Fin.mk s.val (by
44-
cases s
45-
simp only
46-
omega
47-
)))
48-
| Regex.or r1 r2 =>
49-
Regex.or (replace r1 xs h) (replace r2 xs h)
50-
| Regex.concat r1 r2 =>
51-
Regex.concat (replace r1 xs h) (replace r2 xs h)
52-
| Regex.star r1 =>
53-
Regex.star (replace r1 xs h)
54-
55-
def replaceFrom (r: RegexID n) (xs: Vec σ n): Regex σ :=
56-
replace r xs (le_refl n)
57-
5840
def derive {σ: Type} {α: Type} (Φ: σ -> α -> Bool) (r: Regex σ) (a: α): Regex σ :=
5941
Regex.Pair.derive
6042
(replaceFrom
@@ -72,9 +54,6 @@ def derive' {σ: Type} {α: Type} (Φ: σ -> α -> Bool) (r: Regex σ) (a: α):
7254
let replaced: Regex (σ × Bool) := replaceFrom r' replaces
7355
Regex.Pair.derive replaced
7456

75-
def replacesFrom (rs: Vec (RegexID n) l) (xs: Vec σ n): Vec (Regex σ) l :=
76-
Vec.map rs (fun r => replaceFrom r xs)
77-
7857
def derives {σ: Type} {α: Type} (Φ: σ -> α -> Bool) (rs: Vec (Regex σ) l) (a: α): Vec (Regex σ) l :=
7958
let (rs', symbols): (Vec (RegexID (Symbol.nums rs)) l × Vec σ (Symbol.nums rs)) := Symbol.extractsFrom rs
8059
let pred_results: Vec Bool (Symbol.nums rs) := Vec.map symbols (fun s => Φ s a)
@@ -137,106 +116,6 @@ def derive_closure' {σ: Type}
137116
)
138117
)
139118

140-
theorem replace_cast_both (r: RegexID n) (xs: Vec σ n) (h: n = l):
141-
replace r xs (by omega) = replace (RegexID.cast r h) (Vec.cast xs h) (by omega) := by
142-
subst h
143-
simp only [Vec.cast_rfl]
144-
rfl
145-
146-
theorem replaceFrom_cast_both (r: RegexID n) (xs: Vec σ n) (h: n = l):
147-
replaceFrom r xs = replaceFrom (RegexID.cast r h) (Vec.cast xs h) := by
148-
unfold replaceFrom
149-
rw [replace_cast_both]
150-
151-
theorem replace_cast_symbols (r: RegexID n) (xs: Vec σ n) (h: n = l):
152-
replace r xs (by omega) = replace r (Vec.cast xs h) (by omega) := by
153-
subst h
154-
simp only [Vec.cast_rfl]
155-
156-
theorem replace_nil_is_map_id (r: Regex (Fin 0)):
157-
replace r Vec.nil (by simp) = Regex.map r id := by
158-
induction r with
159-
| emptyset =>
160-
simp only [replace, Regex.map]
161-
| emptystr =>
162-
simp only [replace, Regex.map]
163-
| symbol s =>
164-
nomatch s
165-
| or r1 r2 ih1 ih2 =>
166-
simp only [replace, Regex.map, Regex.or.injEq]
167-
rw [ih1]
168-
rw [ih2]
169-
apply And.intro rfl rfl
170-
| concat r1 r2 ih1 ih2 =>
171-
simp only [replace, Regex.map, Regex.concat.injEq]
172-
rw [ih1]
173-
rw [ih2]
174-
apply And.intro rfl rfl
175-
| star r1 ih1 =>
176-
simp only [replace, Regex.map]
177-
rw [ih1]
178-
179-
theorem replace_take (r: RegexID n) (xs: Vec σ (n + l)):
180-
replace r (Vec.take n xs) (by omega) = replace r xs (by omega):= by
181-
induction r with
182-
| emptyset =>
183-
simp only [replace]
184-
| emptystr =>
185-
simp only [replace]
186-
| symbol s =>
187-
generalize_proofs h1 h2 at *
188-
simp only [replace]
189-
obtain ⟨s, hs⟩ := s
190-
simp only [Regex.symbol.injEq]
191-
generalize_proofs h3 h4
192-
rw [Vec.take_get]
193-
omega
194-
| or r1 r2 ih1 ih2 =>
195-
simp only [replace, Regex.or.injEq]
196-
generalize_proofs h1 h2 at *
197-
rw [<- ih1]
198-
rw [<- ih2]
199-
apply And.intro rfl rfl
200-
| concat r1 r2 ih1 ih2 =>
201-
simp only [replace, Regex.concat.injEq]
202-
generalize_proofs h1 h2 at *
203-
rw [<- ih1]
204-
rw [<- ih2]
205-
apply And.intro rfl rfl
206-
| star r1 ih1 =>
207-
simp only [replace]
208-
generalize_proofs h1 at *
209-
rw [<- ih1]
210-
211-
theorem replace_regexid_add (r: RegexID n) (xs: Vec σ (n + l)):
212-
replace r xs (by omega) = replace (RegexID.add l r) xs (by omega):= by
213-
generalize_proofs h1 h2
214-
induction r with
215-
| emptyset =>
216-
simp only [replace, RegexID.add, Regex.map]
217-
| emptystr =>
218-
simp only [replace, RegexID.add, Regex.map]
219-
| symbol s =>
220-
generalize_proofs h1 h2 at *
221-
simp only [replace, RegexID.add, Regex.map, Fin.coe_castLE]
222-
| or r1 r2 ih1 ih2 =>
223-
simp only [replace, RegexID.add, Regex.map, Regex.or.injEq]
224-
generalize_proofs h1 h2 at *
225-
rw [ih1]
226-
rw [ih2]
227-
apply And.intro rfl rfl
228-
| concat r1 r2 ih1 ih2 =>
229-
simp only [replace, RegexID.add, Regex.map, Regex.concat.injEq]
230-
generalize_proofs h1 h2 at *
231-
rw [ih1]
232-
rw [ih2]
233-
apply And.intro rfl rfl
234-
| star r1 ih1 =>
235-
simp only [replace, RegexID.add, Regex.map, Regex.star.injEq]
236-
generalize_proofs h1 h2 at *
237-
rw [ih1]
238-
rfl
239-
240119
theorem extract_replaceFrom_is_id (r: Regex σ) (acc: Vec σ l):
241120
r = replaceFrom (extract r acc).1 (extract r acc).2 := by
242121
simp only [replaceFrom]
@@ -455,20 +334,6 @@ theorem Symbol_derive_is_Regex_derive
455334
rw [<- extractFrom_replaceFrom_is_fmap]
456335
rw [Regex.Pair.derive_is_pair_derive]
457336

458-
theorem replacesFrom_cast_both (rs: Vec (RegexID n) l) (ss: Vec σ n) (h: n = m):
459-
replacesFrom rs ss =
460-
replacesFrom (RegexID.casts rs h) (Vec.cast ss h) := by
461-
subst h
462-
simp only [Vec.cast_rfl]
463-
simp only [RegexID.casts_rfl]
464-
465-
theorem replacesFrom_cons (rs: Vec (RegexID n) l) (xs: Vec σ n):
466-
replacesFrom (Vec.cons r rs) xs
467-
= Vec.cons (replaceFrom r xs) (replacesFrom rs xs)
468-
:= by
469-
simp only [replacesFrom]
470-
simp only [Vec.map]
471-
472337
theorem replaceFrom_append {r: Regex α} {xs: Vec α (n + Symbol.num r)} {acc: Vec α n} {ys: Vec α m}:
473338
replaceFrom (RegexID.add m (extract r acc).1) (Vec.append xs ys)
474339
= replaceFrom (extract r acc).1 xs := by

0 commit comments

Comments
 (0)