Skip to content

Commit 1edf72c

Browse files
derives_preds
1 parent b121dec commit 1edf72c

File tree

2 files changed

+125
-18
lines changed

2 files changed

+125
-18
lines changed

Validator/Expr/Regex.lean

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -505,19 +505,19 @@ theorem map_first (p: σ -> α -> Bool) (r: Regex σ) (a: α):
505505
simp only [Regex.map, Regex.first]
506506
| or r1 r2 ih1 ih2 =>
507507
simp only [Regex.map, Regex.first]
508-
simp_all only [Regex.or.injEq]
508+
simp only [Regex.or.injEq]
509509
apply And.intro
510510
· exact ih1
511511
· exact ih2
512512
| concat r1 r2 ih1 ih2 =>
513513
simp only [Regex.map, Regex.first]
514-
simp_all only [Regex.concat.injEq]
514+
simp only [Regex.concat.injEq]
515515
apply And.intro
516516
· exact ih1
517517
· exact ih2
518518
| star r1 ih1 =>
519519
simp only [Regex.map, Regex.first]
520-
simp_all only [Regex.star.injEq]
520+
simp only [Regex.star.injEq]
521521
exact ih1
522522

523523
theorem map_nullable (p: σ -> α -> Bool) (r: Regex σ) (a: α):
@@ -531,10 +531,12 @@ theorem map_nullable (p: σ -> α -> Bool) (r: Regex σ) (a: α):
531531
simp only [Regex.map, Regex.nullable]
532532
| or r1 r2 ih1 ih2 =>
533533
simp only [Regex.map, Regex.nullable]
534-
simp_all only
534+
rw [ih1]
535+
rw [ih2]
535536
| concat r1 r2 ih1 ih2 =>
536537
simp only [Regex.map, Regex.nullable]
537-
simp_all only
538+
rw [ih1]
539+
rw [ih2]
538540
| star r1 ih1 =>
539541
simp only [Regex.map, Regex.nullable]
540542

@@ -587,6 +589,9 @@ def Regex.smartDerive2 {σ: Type} (r: Regex (σ × Bool)): Regex σ :=
587589
| star r1 =>
588590
smartConcat (derive2 r1) (smartStar (first r1))
589591

592+
def Regexes.map (rs: List.Vector (Regex α) l) (f: α -> β): List.Vector (Regex β) l :=
593+
List.Vector.map (fun r => Regex.map r f) rs
594+
590595
def Regex.derives (p: σ -> α -> Bool) (rs: List.Vector (Regex σ) l) (a: α): List.Vector (Regex σ) l :=
591596
List.Vector.map (fun r => derive p r a) rs
592597

Validator/Expr/Symbol.lean

Lines changed: 115 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,32 @@ abbrev Symbols σ l := List.Vector σ l
5555
def RegexID.add {n: Nat} (m: Nat) (r: RegexID n): RegexID (n + m) :=
5656
Regex.map r (fun s => (Fin.mk s.val (by omega)))
5757

58-
def RegexID.cast (r: RegexID n) (h: n = m): RegexID m := by
59-
rw [<- h]
60-
exact r
58+
def RegexID.cast (r: RegexID n) (h: n = m): RegexID m :=
59+
match h with
60+
| Eq.refl _ => r
6161

6262
def RegexID.casts (rs: List.Vector (RegexID n) l) (h: n = m): List.Vector (RegexID m) l :=
6363
List.Vector.map (fun r => RegexID.cast r h) rs
6464

65-
def RegexID.add_or (r: RegexID (n + num r1 + num r2)): RegexID (n + num (Regex.or r1 r2)) :=
66-
have h : (n + num r1 + num r2) = (n + num (Regex.or r1 r2)) := by
65+
abbrev RegexID.add_assoc (r: RegexID (n + num r1 + num r2)): RegexID (n + (num r1 + num r2)) :=
66+
have h : (n + num r1 + num r2) = n + (num r1 + num r2) := by
6767
rw [<- Nat.add_assoc]
6868
RegexID.cast r h
6969

70+
def RegexID.add_or (r: RegexID (n + num r1 + num r2)): RegexID (n + num (Regex.or r1 r2)) :=
71+
RegexID.add_assoc r
72+
7073
def RegexID.add_concat (r: RegexID (n + num r1 + num r2)): RegexID (n + num (Regex.concat r1 r2)) :=
71-
have h : (n + num r1 + num r2) = (n + num (Regex.concat r1 r2)) := by
72-
rw [<- Nat.add_assoc]
73-
RegexID.cast r h
74+
RegexID.add_assoc r
7475

75-
def Symbols.cast (xs: Symbols σ n) (h: n = m): Symbols σ m := by
76-
rw [<- h]
77-
exact xs
76+
def Symbols.cast (xs: Symbols σ n) (h: n = m): Symbols σ m :=
77+
match h with
78+
| Eq.refl _ => xs
7879

7980
theorem List.Vector.toList_cast_is_toList (xs: List.Vector σ n):
8081
List.Vector.toList xs = List.Vector.toList (Symbols.cast xs h) := by
8182
rcases xs with ⟨xs, hxs⟩
82-
simp [Symbols.cast, _root_.cast, List.Vector.toList]
83+
simp [Symbols.cast, List.Vector.toList]
8384
subst h hxs
8485
simp only
8586

@@ -96,7 +97,7 @@ def Symbols.add_or (xs: Symbols σ (n + num r1 + num r2)): Symbols σ (n + num (
9697
rw [<- Nat.add_assoc]
9798
Symbols.cast xs h
9899

99-
def Symbols.add_concat (xs: Symbols σ (n + num r1 + num r2)): Symbols σ (n + num (Regex.or r1 r2)) :=
100+
def Symbols.add_concat (xs: Symbols σ (n + num r1 + num r2)): Symbols σ (n + num (Regex.concat r1 r2)) :=
100101
have h : (n + num r1 + num r2) = (n + num (Regex.concat r1 r2)) := by
101102
rw [<- Nat.add_assoc]
102103
Symbols.cast xs h
@@ -770,6 +771,32 @@ theorem extractFrom_replaceFrom_is_fmap (r: Regex α) (f: α -> β):
770771
def derive {σ: Type} {α: Type} (p: σ -> α -> Bool) (r: Regex σ) (a: α): Regex σ :=
771772
Regex.derive2 (replaceFrom (extractFrom r).1 (List.Vector.map (fun s => (s, p s a)) (extractFrom r).2))
772773

774+
def derive' {σ: Type} {α: Type} (p: σ -> α -> Bool) (r: Regex σ) (a: α): Regex σ :=
775+
let (r', symbols): (RegexID (num r) × Symbols σ (num r)) := extractFrom r
776+
let pred_results: List.Vector Bool (num r) := List.Vector.map (fun s => p s a) symbols
777+
let replaces: List.Vector (σ × Bool) (num r) := Vec.zip symbols pred_results
778+
let replaced: Regex (σ × Bool) := replaceFrom r' replaces
779+
Regex.derive2 replaced
780+
781+
theorem zip_map {α: Type} {β: Type} (f: α -> β) (xs: List.Vector α l):
782+
(List.Vector.map (fun x => (x, f x)) xs) =
783+
(Vec.zip xs (List.Vector.map f xs)) := by
784+
simp only [Vec.zip, List.Vector.map]
785+
cases xs with
786+
| mk xs h =>
787+
simp only
788+
congr
789+
rw [← List.zip_eq_zipWith]
790+
rw [← List.map_prod_left_eq_zip]
791+
792+
theorem Symbol_derive_is_derive'
793+
{σ: Type} {α: Type} (p: σ -> α -> Bool) (r: Regex σ) (a: α):
794+
Symbol.derive p r a = Symbol.derive' p r a := by
795+
unfold Symbol.derive
796+
unfold Symbol.derive'
797+
simp only
798+
rw [zip_map]
799+
773800
theorem Symbol_derive_is_Regex_derive
774801
{σ: Type} {α: Type} (p: σ -> α -> Bool) (r: Regex σ) (a: α):
775802
Symbol.derive p r a = Regex.derive p r a := by
@@ -793,3 +820,78 @@ def derives {σ: Type} {α: Type} (p: σ -> α -> Bool) (rs: List.Vector (Regex
793820
let replaces: List.Vector (σ × Bool) (nums rs) := Vec.zip symbols pred_results
794821
let replaced: List.Vector (Regex (σ × Bool)) l := replacesFrom rs' replaces
795822
Regex.derives2 replaced
823+
824+
theorem replacesFrom_cast_both (rs: List.Vector (RegexID n) l) (ss: Symbols σ n) (h: n = m):
825+
replacesFrom rs ss =
826+
replacesFrom (RegexID.casts rs h) (Symbols.cast ss h) := by
827+
sorry
828+
829+
theorem extractsFrom_replacesFrom_is_fmap (rs: List.Vector (Regex α) l) (f: α -> β):
830+
Regexes.map rs f = replacesFrom (extractsFrom rs).1 (List.Vector.map f (extractsFrom rs).2) := by
831+
unfold extractsFrom
832+
simp only
833+
unfold Regexes.map
834+
generalize_proofs hnumrs
835+
rw [map_cast]
836+
rw [<- replacesFrom_cast_both]
837+
sorry
838+
839+
theorem Symbol_derives_is_fmap
840+
{σ: Type} {α: Type} (p: σ -> α -> Bool) (rs: List.Vector (Regex σ) l) (a: α):
841+
Symbol.derives p rs a = List.Vector.map (fun r => Symbol.derive p r a) rs := by
842+
unfold Symbol.derives
843+
simp only
844+
unfold Regex.derives2
845+
unfold Symbol.derive
846+
nth_rewrite 2 [<- List.Vector.map_map]
847+
nth_rewrite 1 [<- List.Vector.map_map]
848+
apply (congrArg (List.Vector.map Regex.derive2))
849+
rw [<- zip_map]
850+
-- rewrites under the closure
851+
simp only [<- extractFrom_replaceFrom_is_fmap]
852+
rw [<- extractsFrom_replacesFrom_is_fmap]
853+
unfold Regexes.map
854+
simp only [List.Vector.map_map]
855+
856+
theorem Symbol_derives_is_Regex_derives
857+
{σ: Type} {α: Type} (p: σ -> α -> Bool) (r: List.Vector (Regex σ) l) (a: α):
858+
Symbol.derives p r a = Regex.derives p r a := by
859+
rw [Symbol_derives_is_fmap]
860+
unfold Symbol.derive
861+
unfold Regex.derives
862+
congr
863+
funext r
864+
rw [<- extractFrom_replaceFrom_is_fmap]
865+
rw [Regex.derive_is_derive2]
866+
867+
def leave
868+
(rs: List.Vector (Regex σ) l)
869+
(ns: List.Vector Bool (Symbol.nums rs))
870+
: (List.Vector (Regex σ) l) :=
871+
let replaces: List.Vector (σ × Bool) (nums rs) := Vec.zip (Symbol.extractsFrom rs).2 ns
872+
let replaced: List.Vector (Regex (σ × Bool)) l := replacesFrom (Symbol.extractsFrom rs).1 replaces
873+
Regex.derives2 replaced
874+
875+
def enter (rs: List.Vector (Regex σ) l): Symbols σ (nums rs) :=
876+
let (_, symbols): (List.Vector (RegexID (nums rs)) l × Symbols σ (nums rs)) := Symbol.extractsFrom rs
877+
symbols
878+
879+
-- derives_preds unlike derives takes a predicate that works out the full vector of predicates.
880+
-- This gives the predicate control over the evaluation order of α, for example α is a tree, we can first evaluate the same label, before traversing down.
881+
def derives_preds {σ: Type} {α: Type}
882+
(ps: {n: Nat} -> List.Vector σ n -> α -> List.Vector Bool n) (rs: List.Vector (Regex σ) l) (a: α): List.Vector (Regex σ) l :=
883+
let symbols: Symbols σ (nums rs) := enter rs
884+
let pred_results: List.Vector Bool (nums rs) := ps symbols a
885+
leave rs pred_results
886+
887+
theorem Symbol_derive_is_derive_preds
888+
{σ: Type} {α: Type} (ps: {n: Nat} -> List.Vector σ n -> α -> List.Vector Bool n) (rs: List.Vector (Regex σ) l) (a: α)
889+
(h: ∀ {n': Nat} (xs: List.Vector σ n') (a: α), ps xs a = List.Vector.map (fun x => (ps (Vec.singleton x) a).head) xs):
890+
Symbol.derives (fun s a => (ps (Vec.singleton s) a).head) rs a = Symbol.derives_preds ps rs a := by
891+
unfold derives
892+
simp only
893+
rw [<- h]
894+
unfold derives_preds
895+
unfold leave
896+
unfold enter
897+
simp only

0 commit comments

Comments
 (0)