Theorem: Regex.VM.upper_bound_exploreεClosure
Proof state before:
case inr
nfa : NFA
pos : String.Pos
next : SparseSet nfa.nodes.size
currentSave : Array (Option String.Pos)
matched : Option (Array (Option String.Pos))
saveSlots : Data.Vec (Array (Option String.Pos)) nfa.nodes.size
target : Fin nfa.nodes.size
stack : Array (StackEntry nfa.nodes.size)
matched' : Option (Array (Option String.Pos))
next' : SparseSet nfa.nodes.size
saveSlots' : Data.Vec (Array (Option String.Pos)) nfa.nodes.size
i : Fin nfa.nodes.size
inv : UpperInvExploreεClosure nfa i target stack
h✝ : target ∉ next
h :
  (let next' := next.insert target;
    match hn : nfa[target] with
    | Node.epsilon target' =>
      let_fun isLt := ⋯;
      exploreεClosure nfa pos next' currentSave matched saveSlots ⟨target', isLt⟩ stack
    | Node.split target₁ target₂ =>
      let_fun isLt := ⋯;
      exploreεClosure nfa pos next' currentSave matched saveSlots ⟨target₁, ⋯⟩
        (stack.push (StackEntry.explore ⟨target₂, ⋯⟩))
    | Node.save offset target' =>
      let_fun isLt := ⋯;
      if h : offset < currentSave.size then
        let nextSave := currentSave.set ⟨offset, h⟩ (some pos);
        let stack' := stack.push (StackEntry.restore currentSave);
        exploreεClosure nfa pos next' nextSave matched saveSlots ⟨target', isLt⟩ stack'
      else exploreεClosure nfa pos next' currentSave matched saveSlots ⟨target', isLt⟩ stack
    | Node.done =>
      let matched' := HOrElse.hOrElse matched fun x => some currentSave;
      let saveSlots' := saveSlots.set ↑target ⋯ currentSave;
      εClosure nfa pos next' currentSave matched' saveSlots' stack
    | Node.char c next =>
      let saveSlots' := saveSlots.set ↑target ⋯ currentSave;
      εClosure nfa pos next' currentSave matched saveSlots' stack
    | Node.sparse cs next =>
      let saveSlots' := saveSlots.set ↑target ⋯ currentSave;
      εClosure nfa pos next' currentSave matched saveSlots' stack
    | Node.fail => εClosure nfa pos next' currentSave matched saveSlots stack) =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i next next'
Tactic:
next =>
  suffices UpperBoundεClosure nfa i (next.insert target) next' by
    intro j hj
    cases this j hj with
    | inl hnext =>
      cases SparseSet.eq_or_mem_of_mem_insert hnext with
      | inl heq => exact .inr (heq ▸ inv.1)
      | inr hnext => exact .inl hnext
    | inr hcls => exact .inr hcls
  simp at h
  split at h
  next target' hn =>
    have isLt := nfa.inBounds' target hn
    have inv' : UpperInvExploreεClosure nfa i ⟨target', isLt⟩ stack :=
      ⟨εClosure_snoc inv.1 (by simp [εStep, Node.εStep, hn]), inv.2⟩
    exact upper_bound_exploreεClosure h inv'
  next target₁ target₂ hn =>
    have isLt := nfa.inBounds' target hn
    have inv' : UpperInvExploreεClosure nfa i ⟨target₁, isLt.1⟩ (stack.push (.explore ⟨target₂, isLt.2⟩)) := by
      refine ⟨εClosure_snoc inv.1 (by simp [εStep, Node.εStep, hn]), ?_⟩
      intro j hj
      cases (Array.mem_push ..).mp hj with
      | inl hj => exact inv.2 j hj
      | inr hj =>
        simp at hj
        exact εClosure_snoc inv.1 (by simp [εStep, Node.εStep, hn, hj])
    exact upper_bound_exploreεClosure h inv'
  next _ target' hn =>
    have isLt := nfa.inBounds' target hn
    split at h
    next =>
      have inv' : UpperInvExploreεClosure nfa i ⟨target', isLt⟩ (stack.push (.restore currentSave)) := by
        refine ⟨εClosure_snoc inv.1 (by simp [εStep, Node.εStep, hn]), ?_⟩
        intro j hj
        cases (Array.mem_push ..).mp hj with
        | inl hj => exact inv.2 j hj
        | inr hj => contradiction
      exact upper_bound_exploreεClosure h inv'
    next =>
      have inv' : UpperInvExploreεClosure nfa i ⟨target', isLt⟩ stack :=
        ⟨εClosure_snoc inv.1 (by simp [εStep, Node.εStep, hn]), inv.2⟩
      exact upper_bound_exploreεClosure h inv'
  next hn => exact upper_bound_εClosure h inv.2
  next hn => exact upper_bound_εClosure h inv.2
  next hn => exact upper_bound_εClosure h inv.2
  next hn => exact upper_bound_εClosure h inv.2
Proof state after:
no goals