Skip to content

Commit 69fbc94

Browse files
move RegexID into its own file
1 parent 5d3b31c commit 69fbc94

File tree

4 files changed

+203
-202
lines changed

4 files changed

+203
-202
lines changed

Validator/Regex.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ import Validator.Regex.Map
55
import Validator.Regex.Num
66
import Validator.Regex.Pair
77
import Validator.Regex.Regex
8+
import Validator.Regex.RegexID
89
import Validator.Regex.Smart
910
import Validator.Regex.Symbol

Validator/Regex/Num.lean

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,12 @@ theorem nums_map_map:
103103
rw [Vec.foldl_assoc]
104104
rw [ih]
105105
rw [num_map]
106+
107+
theorem nums_cons_is_add:
108+
nums (Vec.cons x xs) = num x + nums xs
109+
:= by
110+
simp only [nums]
111+
simp only [Vec.map]
112+
simp only [Vec.foldl]
113+
nth_rewrite 1 [Nat.add_comm]
114+
rw [Vec.foldl_assoc]

Validator/Regex/RegexID.lean

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
import Validator.Regex.Regex
2+
import Validator.Regex.Num
3+
4+
namespace Symbol
5+
6+
abbrev RegexID n := Regex (Fin n)
7+
8+
def RegexID.add {n: Nat} (m: Nat) (r: RegexID n): RegexID (n + m) :=
9+
Regex.map r (fun s => (Fin.castLE (by omega) s))
10+
11+
def RegexID.cast (r: RegexID n) (h: n = m): RegexID m :=
12+
match h with
13+
| Eq.refl _ => r
14+
15+
def RegexID.castLE {n: Nat} (r: RegexID n) (h : n ≤ m): RegexID m :=
16+
Regex.map r (fun s => (Fin.castLE h s))
17+
18+
def RegexID.cast_map (r: RegexID n) (h: n = m): RegexID m :=
19+
Regex.map r (fun s => Fin.castLE (by omega) s)
20+
21+
def RegexID.casts (rs: Vec (RegexID n) l) (h: n = m): Vec (RegexID m) l :=
22+
Vec.map rs (fun r => RegexID.cast r h)
23+
24+
def RegexID.casts_rw (rs: Vec (RegexID n) l) (h: n = m): Vec (RegexID m) l := by
25+
subst h
26+
exact rs
27+
28+
def RegexID.castsLE (rs: Vec (RegexID n) l) (h: n ≤ m): Vec (RegexID m) l :=
29+
Vec.map rs (fun r => RegexID.castLE r h)
30+
31+
abbrev RegexID.add_assoc (r: RegexID (n + Symbol.num r1 + Symbol.num r2)): RegexID (n + (Symbol.num r1 + Symbol.num r2)) :=
32+
have h : (n + Symbol.num r1 + Symbol.num r2) = n + (Symbol.num r1 + Symbol.num r2) := by
33+
rw [<- Nat.add_assoc]
34+
RegexID.cast r h
35+
36+
def RegexID.add_or (r: RegexID (n + Symbol.num r1 + Symbol.num r2)): RegexID (n + Symbol.num (Regex.or r1 r2)) :=
37+
RegexID.add_assoc r
38+
39+
def RegexID.add_concat (r: RegexID (n + Symbol.num r1 + Symbol.num r2)): RegexID (n + Symbol.num (Regex.concat r1 r2)) :=
40+
RegexID.add_assoc r
41+
42+
theorem RegexID.cast_rfl (r: RegexID n): RegexID.cast r rfl = r := by
43+
rfl
44+
45+
theorem RegexID.cast_rfl' (r: RegexID n) (h: n = n): RegexID.cast r h = r := by
46+
rfl
47+
48+
theorem RegexID.cast_map_rfl (r: RegexID n): RegexID.cast_map r rfl = r := by
49+
unfold RegexID.cast_map
50+
simp only [Fin.castLE_refl]
51+
rw [Regex.map_id]
52+
53+
theorem RegexID.cast_is_cast_map (r: RegexID n) (h: n = m):
54+
RegexID.cast r h = RegexID.cast_map r h := by
55+
subst h
56+
rw [RegexID.cast_rfl]
57+
rw [RegexID.cast_map_rfl]
58+
59+
theorem RegexID.cons_cast:
60+
Vec (RegexID (nacc + Symbol.nums (Vec.cons x xs))) n
61+
= Vec (RegexID (nacc + Symbol.num x + Symbol.nums xs)) n := by
62+
simp only [Symbol.nums]
63+
simp only [Vec.map]
64+
simp only [Vec.foldl]
65+
nth_rewrite 2 [Nat.add_comm]
66+
rw [Vec.foldl_assoc]
67+
ac_rfl
68+
69+
theorem RegexID.casts_rfl {n} {xs : Vec (RegexID n) l} {h : n = n} : RegexID.casts xs h = xs := by
70+
induction xs with
71+
| nil =>
72+
unfold RegexID.casts
73+
rfl
74+
| @cons l x xs ih =>
75+
simp only [casts] at *
76+
simp only [Vec.map]
77+
rw [ih]
78+
rfl
79+
80+
theorem RegexID.casts_symm:
81+
RegexID.casts rs1 h = rs2
82+
<->
83+
RegexID.casts rs2 (Eq.symm h) = rs1 := by
84+
apply Iff.intro
85+
case mp =>
86+
intro h
87+
rw [<- h]
88+
unfold RegexID.casts
89+
rw [Vec.map_map]
90+
simp only [RegexID.cast_is_cast_map]
91+
unfold RegexID.cast_map
92+
simp only [Regex.map_map]
93+
rename_i h_1
94+
subst h h_1
95+
simp_all only [Fin.castLE_refl]
96+
simp only [Regex.map_id]
97+
simp only [Vec.map_id]
98+
case mpr =>
99+
intro h
100+
rw [<- h]
101+
unfold RegexID.casts
102+
rw [Vec.map_map]
103+
simp only [RegexID.cast_is_cast_map]
104+
unfold RegexID.cast_map
105+
simp only [Regex.map_map]
106+
rename_i h_1
107+
subst h h_1
108+
simp_all only [Fin.castLE_refl]
109+
simp only [Regex.map_id]
110+
simp only [Vec.map_id]
111+
112+
theorem RegexID.cast_lift_cons {x: RegexID n} {h: n = m} {xs: Vec (RegexID n) l}:
113+
Vec.cons (RegexID.cast x h) (RegexID.casts xs h)
114+
= RegexID.casts (Vec.cons x xs) h := by
115+
simp only [RegexID.casts]
116+
simp only [Vec.map]
117+
118+
theorem RegexID.castLE_lift_cons {x: RegexID n} {h: n ≤ m} {xs: Vec (RegexID n) l}:
119+
Vec.cons (RegexID.castLE x h) (RegexID.castsLE xs h)
120+
= RegexID.castsLE (Vec.cons x xs) h := by
121+
simp only [RegexID.castsLE]
122+
simp only [Vec.map]
123+
124+
theorem RegexID.castLE_id {h: n ≤ n}:
125+
(RegexID.castLE r h) = r := by
126+
simp only [RegexID.castLE]
127+
simp_all only [Fin.castLE_refl]
128+
simp_all only [le_refl]
129+
rw [Regex.map_id]
130+
131+
theorem RegexID.castLE_casts_lift_cons {x: RegexID n1} {h1: n1 ≤ k} {h2: n2 = k} {xs: Vec (RegexID n2) l}:
132+
Vec.cons (RegexID.castLE x h1) (RegexID.casts xs h2)
133+
= RegexID.castsLE (Vec.cons (RegexID.castLE x (by omega)) xs) (by omega) := by
134+
simp only [RegexID.casts]
135+
simp only [RegexID.cast_is_cast_map]
136+
simp only [RegexID.cast_map]
137+
simp only [RegexID.castsLE]
138+
subst h2
139+
simp_all only [Fin.castLE_refl]
140+
simp only [Vec.map]
141+
simp only [Regex.map_id]
142+
generalize_proofs h2
143+
simp only [Vec.map_id]
144+
rw [RegexID.castLE_id]
145+
congr
146+
· induction xs with
147+
| nil =>
148+
simp only [Vec.map_nil]
149+
| @cons l x xs ih =>
150+
simp only [Vec.map]
151+
rw [<- ih]
152+
rw [RegexID.castLE_id]
153+
154+
theorem RegexID.add_zero:
155+
(RegexID.add 0 r) = r := by
156+
simp only [Nat.add_zero, RegexID.add]
157+
simp_all only [Fin.castLE_refl]
158+
simp only [Regex.map_id]
159+
160+
theorem RegexID.casts_casts (xs: Vec (RegexID n1) l) (h12: n1 = n2) (h23: n2 = n3):
161+
RegexID.casts (RegexID.casts xs h12) h23 = RegexID.casts xs (by omega) := by
162+
subst h12 h23
163+
simp only [RegexID.casts_rfl]
164+
165+
theorem RegexID.add_cast_is_castLE (r: RegexID n) (h: n = m):
166+
(RegexID.add k (RegexID.cast r h)) = RegexID.castLE r (by omega) := by
167+
simp only [RegexID.add, RegexID.cast]
168+
subst h
169+
simp_all only
170+
rfl
171+
172+
theorem RegexID.add_is_castLE (r: RegexID n):
173+
(RegexID.add k r) = RegexID.castLE r (by omega) := by
174+
simp only [RegexID.add, RegexID.castLE]
175+
176+
theorem RegexID.casts_is_casts_rw:
177+
RegexID.casts xs h =
178+
RegexID.casts_rw xs h := by
179+
subst h
180+
simp only [RegexID.casts]
181+
simp only [RegexID.cast]
182+
simp only [RegexID.casts_rw]
183+
rw [Vec.map_id]

0 commit comments

Comments
 (0)