Skip to content

Commit d153f47

Browse files
committed
Add matches'_foldl_sum helper and progress Kleene's theorem
1 parent 294432b commit d153f47

1 file changed

Lines changed: 40 additions & 4 deletions

File tree

TraceTheory/TraceTheory/Computability.lean

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import Mathlib.Algebra.BigOperators.Group.Finset.Defs
12
import Mathlib.Computability.EpsilonNFA
23
import Mathlib.Computability.RegularExpressions
4+
import Mathlib.Data.Finset.Sort
35
import Mathlib.Data.Fintype.Option
46

57
open Classical Computability
@@ -665,7 +667,7 @@ section Kleene
665667
open RegularExpression
666668

667669
variable {σ : Type*} [Fintype σ] [DecidableEq σ]
668-
variable {α : Type*} [Fintype α] [DecidableEq α]
670+
variable {α : Type*} [Fintype α] [DecidableEq α] [LinearOrder α]
669671
variable {M : εNFA α (ExtendedState σ)}
670672

671673
local notation "n" => Fintype.card (ExtendedState σ)
@@ -679,8 +681,8 @@ noncomputable def directRegex (i j : Fin n) : RegularExpression α :=
679681
let s_i := e.symm i
680682
let s_j := e.symm j
681683
let char_transitions : RegularExpression α :=
682-
(Finset.univ.toList).foldl (fun acc a =>
683-
if s_j ∈ M.step s_i (some a) then acc + (char a) else acc
684+
Finset.univ.sort.foldl (fun acc a =>
685+
acc + (if s_j ∈ M.step s_i (some a) then char a else 0)
684686
) 0
685687
let epsilon_transitions : RegularExpression α :=
686688
if s_j ∈ M.step s_i none ∨ i = j then 1 else 0
@@ -701,6 +703,30 @@ noncomputable def pathRegex : ℕ → Fin n → Fin n → RegularExpression α
701703
else
702704
pathRegex k i j
703705

706+
omit [Fintype α] [DecidableEq α] [LinearOrder α] in
707+
theorem matches'_foldl_sum (L : List α) (f : α → RegularExpression α) :
708+
(L.foldl (fun acc a => acc + f a) 0).matches' =
709+
((⋃ x ∈ L, (f x).matches') : Language α) := by
710+
let g := fun (acc : RegularExpression α) (a : α) => acc + f a
711+
let u : Language α := (⋃ x ∈ L, (f x).matches')
712+
have h : ∀ acc, (L.foldl g acc).matches' = acc.matches' + u := by
713+
dsimp [u]
714+
induction L with
715+
| nil =>
716+
intro acc
717+
simp
718+
apply Set.empty_subset
719+
| cons a L' ih =>
720+
intro acc
721+
dsimp [g]
722+
rw [ih, matches'_add, add_assoc]
723+
apply congr_arg
724+
simp
725+
rfl
726+
specialize h 0
727+
simp [g, u] at h
728+
exact h
729+
704730
variable (M) in
705731
/-- A path in the NFA restricted to intermediate states < k. -/
706732
inductive IsRestrictedPath (k : ℕ) : Fin n → Fin n → List α → Prop
@@ -722,7 +748,17 @@ noncomputable def toRegex (M : εNFA a σ) : RegularExpression α :=
722748

723749
theorem isRestrictedPath_iff_isPath {i j : Fin n} {x : List α} :
724750
IsRestrictedPath M n i j x ↔ M.IsPath (e.symm i) (e.symm j) x := by
725-
sorry
751+
constructor
752+
· intro h
753+
induction h with
754+
| direct i' j' x' h_match =>
755+
dsimp [directRegex] at h_match
756+
simp only [matches'_foldl_sum] at h_match
757+
simp only [Language.mem_add] at h_match
758+
sorry
759+
| trans k i' j' x₁ x₂ h₁ hlt h₂ ih₁ ih₂ =>
760+
sorry
761+
· sorry
726762

727763
theorem accepts_toRegex (M : εNFA a σ) : (toRegex M).matches' = M.accepts := by
728764
sorry

0 commit comments

Comments
 (0)