Skip to content

Commit 52b919e

Browse files
new operators
Adds predicate and not operators in Language Switches out char for predicate in Regex Update definition of star in Language
1 parent 908edda commit 52b919e

File tree

3 files changed

+106
-192
lines changed

3 files changed

+106
-192
lines changed

Katydid/Regex/Commutes.lean

+7-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def denote {α: Type} (r: Regex α): Language.Lang α :=
1010
match r with
1111
| Regex.emptyset => Language.emptyset
1212
| Regex.emptystr => Language.emptystr
13-
| Regex.char a => Language.char a
13+
| Regex.pred p => Language.pred p
1414
| Regex.or x y => Language.or (denote x) (denote y)
1515
| Regex.concat x y => Language.concat (denote x) (denote y)
1616
| Regex.star x => Language.star (denote x)
@@ -27,11 +27,7 @@ private theorem decide_prop (P: Prop) [dP: Decidable P]:
2727
simp only [iff_false]
2828
exact hp
2929

30-
private theorem decide_eq (x y: α) [dα: DecidableEq α]:
31-
(x == y) = true <-> (x = y) := by
32-
apply beq_iff_eq
33-
34-
def denote_onlyif {α: Type} [DecidableEq α] (condition: Prop) [dcond: Decidable condition] (r: Regex α):
30+
def denote_onlyif {α: Type} (condition: Prop) [dcond: Decidable condition] (r: Regex α):
3531
denote (Regex.onlyif condition r) = Language.onlyif condition (denote r) := by
3632
unfold Language.onlyif
3733
unfold Regex.onlyif
@@ -69,9 +65,9 @@ theorem null_commutes {α: Type} (r: Regex α):
6965
rw [Language.null_emptystr]
7066
unfold Regex.null
7167
simp only
72-
| char a =>
68+
| pred p =>
7369
unfold denote
74-
rw [Language.null_char]
70+
rw [Language.null_pred]
7571
unfold Regex.null
7672
apply Bool.false_eq_true
7773
| or p q ihp ihq =>
@@ -94,7 +90,7 @@ theorem null_commutes {α: Type} (r: Regex α):
9490
unfold Regex.null
9591
simp only
9692

97-
theorem derive_commutes {α: Type} [dα: DecidableEq α] (r: Regex α) (x: α):
93+
theorem derive_commutes {α: Type} (r: Regex α) (x: α):
9894
denote (Regex.derive r x) = Language.derive (denote r) x := by
9995
induction r with
10096
| emptyset =>
@@ -103,13 +99,12 @@ theorem derive_commutes {α: Type} [dα: DecidableEq α] (r: Regex α) (x: α):
10399
| emptystr =>
104100
simp only [denote]
105101
rw [Language.derive_emptystr]
106-
| char a =>
102+
| pred p =>
107103
simp only [denote]
108-
rw [Language.derive_char]
104+
rw [Language.derive_pred]
109105
unfold Regex.derive
110106
rw [denote_onlyif]
111107
simp only [denote]
112-
rw [beq_iff_eq]
113108
| or p q ihp ihq =>
114109
simp only [denote]
115110
rw [Language.derive_or]

Katydid/Regex/Language.lean

+89-172
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,42 @@ def universal {α: Type} : Lang α :=
1313
fun _ => True
1414

1515
def emptystr {α: Type} : Lang α :=
16-
fun w => w = []
16+
fun xs => xs = []
1717

18-
def char {α: Type} (a : α): Lang α :=
19-
fun w => w = [a]
18+
def char {α: Type} (x : α): Lang α :=
19+
fun xs => xs = [x]
20+
21+
def pred {α: Type} (p : α -> Prop): Lang α :=
22+
fun xs => ∃ x, xs = [x] /\ p x
2023

2124
-- onlyif is used as an and to make derive char not require an if statement
2225
-- (derive (char c) a) w <-> (onlyif (a = c) emptystr)
23-
def onlyif {α: Type} (cond : Prop) (P : Lang α) : Lang α :=
24-
fun w => cond /\ P w
26+
def onlyif {α: Type} (cond : Prop) (R : Lang α) : Lang α :=
27+
fun xs => cond /\ R xs
2528

2629
def or {α: Type} (P : Lang α) (Q : Lang α) : Lang α :=
27-
fun w => P w \/ Q w
30+
fun xs => P xs \/ Q xs
2831

2932
def and {α: Type} (P : Lang α) (Q : Lang α) : Lang α :=
30-
fun w => P w /\ Q w
33+
fun xs => P xs /\ Q xs
3134

3235
def concat {α: Type} (P : Lang α) (Q : Lang α) : Lang α :=
3336
fun (xs : List α) =>
34-
∃ (ys : List α) (zs : List α), P ys /\ Q zs /\ xs = (ys ++ zs)
37+
∃ (xs1 : List α) (xs2 : List α), P xs1 /\ Q xs2 /\ xs = (xs1 ++ xs2)
3538

36-
inductive All {α: Type} (P : α -> Prop) : (List α -> Prop) where
37-
| nil : All P []
38-
| cons : ∀ {x xs} (_px : P x) (_pxs : All P xs), All P (x :: xs)
39+
inductive star {α: Type} (R: Lang α): Lang α where
40+
| zero: star R []
41+
| more: ∀ (x: α) (xs1 xs2 xs: List α),
42+
xs = (x::xs1) ++ xs2
43+
-> R (x::xs1)
44+
-> star R xs2
45+
-> star R xs
3946

40-
def star {α: Type} (R : Lang α) : Lang α :=
41-
fun (xs : List α) =>
42-
∃ (xss : List (List α)), All R xss /\ xs = (List.flatten xss)
47+
def not {α: Type} (R: Lang α): Lang α :=
48+
fun xs => (Not (R xs))
4349

4450
-- attribute [simp] allows these definitions to be unfolded when using the simp tactic.
45-
attribute [simp] universal emptyset emptystr char onlyif or and concat star
51+
attribute [simp] universal emptyset emptystr char onlyif or and concat
4652

4753
example: Lang α := universal
4854
example: Lang α := emptystr
@@ -154,6 +160,18 @@ def null_char {α: Type} {c: α}:
154160
null (char c) = False := by
155161
rw [null_iff_char]
156162

163+
def null_iff_pred {α: Type} {p: α -> Prop}:
164+
null (pred p) <-> False :=
165+
Iff.intro nofun nofun
166+
167+
def not_null_if_pred {α: Type} {p: α -> Prop}:
168+
null (pred p) -> False :=
169+
nofun
170+
171+
def null_pred {α: Type} {p: α -> Prop}:
172+
null (pred p) = False := by
173+
rw [null_iff_pred]
174+
157175
def null_or {α: Type} {P Q: Lang α}:
158176
null (or P Q) = ((null P) \/ (null Q)) :=
159177
rfl
@@ -181,22 +199,27 @@ def null_concat {α: Type} {P Q: Lang α}:
181199
null (concat P Q) = ((null P) /\ (null Q)) := by
182200
rw [null_iff_concat]
183201

184-
def null_star {α: Type} {P: Lang α}:
185-
null (star P) = True := by
186-
simp
187-
exists []
188-
apply And.intro
189-
· exact All.nil
190-
· intro l hl
191-
cases hl
202+
def null_if_star {α: Type} {R: Lang α}:
203+
null (star R) :=
204+
star.zero
192205

193-
def null_iff_star {α: Type} {P: Lang α}:
194-
null (star P) <-> True := by
195-
rw [null_star]
206+
def null_iff_star {α: Type} {R: Lang α}:
207+
null (star R) <-> True :=
208+
Iff.intro
209+
(fun _ => True.intro)
210+
(fun _ => star.zero)
211+
212+
def null_star {α: Type} {R: Lang α}:
213+
null (star R) = True := by
214+
rw [null_iff_star]
215+
216+
def null_not {α: Type} {R: Lang α}:
217+
null (not R) = null (Not ∘ R) :=
218+
rfl
196219

197-
def null_if_star {α: Type} {P: Lang α}:
198-
null (star P) :=
199-
null_iff_star.mpr True.intro
220+
def null_iff_not {α: Type} {R: Lang α}:
221+
null (not R) <-> null (Not ∘ R) := by
222+
rw [null_not]
200223

201224
def derive_emptyset {α: Type} {a: α}:
202225
(derive emptyset a) = emptyset :=
@@ -233,6 +256,32 @@ def derive_char {α: Type} [DecidableEq α] {a: α} {c: α}:
233256
funext
234257
rw [derive_iff_char]
235258

259+
def derive_iff_pred {α: Type} {p: α -> Prop} [dp: DecidablePred p] {x: α} {xs: List α}:
260+
(derive (pred p) x) xs <-> (onlyif (p x) emptystr) xs := by
261+
simp only [derive, derives, singleton_append]
262+
simp only [onlyif, emptystr]
263+
refine Iff.intro ?toFun ?invFun
264+
case toFun =>
265+
intro D
266+
match D with
267+
| Exists.intro x' D =>
268+
simp only [cons.injEq] at D
269+
match D with
270+
| And.intro (And.intro hxx' hxs) hpx =>
271+
rw [<- hxx'] at hpx
272+
exact And.intro hpx hxs
273+
case invFun =>
274+
intro ⟨ hpx , hxs ⟩
275+
unfold pred
276+
exists x
277+
simp only [cons.injEq, true_and]
278+
exact And.intro hxs hpx
279+
280+
def derive_pred {α: Type} {p: α -> Prop} [DecidablePred p] {x: α}:
281+
(derive (pred p) x) = (onlyif (p x) emptystr) := by
282+
funext
283+
rw [derive_iff_pred]
284+
236285
def derive_or {α: Type} {a: α} {P Q: Lang α}:
237286
(derive (or P Q) a) = (or (derive P a) (derive Q a)) :=
238287
rfl
@@ -313,147 +362,6 @@ def derive_concat {α: Type} {x: α} {P Q: Lang α}:
313362
funext
314363
rw [derive_iff_concat]
315364

316-
inductive starplus {α: Type} (R: Lang α): Lang α where
317-
| zero: starplus R []
318-
| more: ∀ (ps qs w: List α),
319-
w = ps ++ qs
320-
-> R ps
321-
-> starplus R qs
322-
-> starplus R w
323-
324-
theorem star_is_starplus: ∀ {α: Type} (R: Lang α) (xs: List α),
325-
star R xs -> starplus R xs := by
326-
intro α R xs
327-
unfold star
328-
intro s
329-
match s with
330-
| Exists.intro ws (And.intro sl sr) =>
331-
clear s
332-
rw [sr]
333-
clear sr
334-
clear xs
335-
induction ws with
336-
| nil =>
337-
simp
338-
exact starplus.zero
339-
| cons w' ws' ih =>
340-
cases sl with
341-
| cons rw allrw =>
342-
have hr := ih allrw
343-
refine starplus.more ?ps ?qs ?w ?psqs ?rps ?rqs
344-
· exact w'
345-
· exact ws'.flatten
346-
· simp
347-
· assumption
348-
· assumption
349-
350-
theorem starplus_is_star: ∀ {α: Type} (R: Lang α) (xs: List α),
351-
starplus R xs -> star R xs := by
352-
intro α R xs
353-
intro hs
354-
induction xs with
355-
| nil =>
356-
simp
357-
exists []
358-
apply And.intro ?_ (by simp)
359-
apply All.nil
360-
| cons x' xs' ih =>
361-
cases hs with
362-
| more ps qs _ xxspsqs Rps sRqs =>
363-
unfold star
364-
have hxs := list_split_cons (x' :: xs')
365-
cases hxs with
366-
| inl h =>
367-
contradiction
368-
| inr h =>
369-
cases h with
370-
| intro x h =>
371-
cases h with
372-
| intro xs h =>
373-
cases h with
374-
| intro ys h =>
375-
rw [h]
376-
have hys := list_split_flatten ys
377-
cases hys with
378-
| intro yss hyss =>
379-
sorry
380-
381-
inductive starcons {α: Type} (R: Lang α): Lang α where
382-
| zero: starcons R []
383-
| more: ∀ (p: α) (ps qs w: List α),
384-
w = (p::ps) ++ qs
385-
-> R (p::ps)
386-
-> starcons R qs
387-
-> starcons R w
388-
389-
def stara {α: Type} (R : Lang α) (ws : List (List α)) (allr: All R ws): star R ws.flatten := by
390-
unfold star
391-
exists ws
392-
393-
theorem star_is_starcons: ∀ {α: Type} (R: Lang α) (xs: List α),
394-
star R xs <-> starcons R xs := by
395-
intro α R xs
396-
refine Iff.intro ?toFun ?invFun
397-
case toFun =>
398-
intro s
399-
match s with
400-
| Exists.intro ws (And.intro sl sr) =>
401-
cases sl with
402-
| nil =>
403-
rw [sr]
404-
exact starcons.zero
405-
| cons rx1 rx2 =>
406-
rename_i xs1 xs2
407-
cases xs with
408-
| nil =>
409-
exact starcons.zero
410-
| cons x' xs' =>
411-
cases xs1 with
412-
| nil =>
413-
sorry
414-
| cons xs1' xss1' =>
415-
refine starcons.more x' ?ps ?qs ?ws' ?hw ?hr ?hsr
416-
· exact xss1'
417-
· exact xs2.flatten
418-
· sorry
419-
· sorry
420-
· sorry
421-
case invFun =>
422-
intro hs
423-
induction hs with
424-
| zero =>
425-
unfold star
426-
exists []
427-
simp
428-
exact All.nil
429-
| more x' ys' zs' xs hsplit hr hrs ih =>
430-
have hxs := list_split_cons xs
431-
cases hxs with
432-
| inl h =>
433-
rw [h]
434-
simp
435-
exists []
436-
simp
437-
apply All.nil
438-
| inr h =>
439-
cases h with
440-
| intro x h =>
441-
cases h with
442-
| intro ys h =>
443-
cases h with
444-
| intro zs h =>
445-
rw [hsplit] at h
446-
rw [hsplit]
447-
unfold star
448-
exists [x' :: ys', zs']
449-
refine And.intro ?_ (by simp)
450-
refine All.cons hr ?_
451-
cases ih with
452-
| intro ws h2 =>
453-
cases h2 with
454-
| intro h2l h2r =>
455-
sorry
456-
457365
def derive_iff_star {α: Type} {x: α} {R: Lang α} {xs: List α}:
458366
(derive (star R) x) xs <-> (concat (derive R x) (star R)) xs := by
459367
refine Iff.intro ?toFun ?invFun
@@ -462,7 +370,16 @@ def derive_iff_star {α: Type} {x: α} {R: Lang α} {xs: List α}:
462370
case invFun =>
463371
sorry
464372

465-
def derive_star {α: Type} {a: α} {P: Lang α}:
466-
(derive (star P) a) = (concat (derive P a) (star P)) := by
373+
def derive_star {α: Type} {x: α} {R: Lang α}:
374+
(derive (star R) x) = (concat (derive R x) (star R)) := by
467375
funext
468376
rw [derive_iff_star]
377+
378+
def derive_not {α: Type} {x: α} {R: Lang α}:
379+
(derive (not R) x) = Not ∘ (derive R x) :=
380+
rfl
381+
382+
def derive_iff_not {α: Type} {x: α} {R: Lang α} {xs: List α}:
383+
(derive (not R) x) xs <-> Not ((derive R x) xs) := by
384+
rw [derive_not]
385+
rfl

0 commit comments

Comments
 (0)