Theorem: Regex.VM.upper_bound_exploreεClosure (RegexCorrectness/VM/Auxiliary.lean)

State before:
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
h : exploreεClosure nfa pos next currentSave matched saveSlots target stack = (matched', next', saveSlots')
inv : UpperInvExploreεClosure nfa i target stack
⊢ UpperBoundεClosure nfa i next next'
Tactic:
unfold exploreεClosure at h
State after:
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 :
  (if target ∈ next then εClosure nfa pos next currentSave matched saveSlots stack
    else
      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'

State before:
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 :
  (if target ∈ next then εClosure nfa pos next currentSave matched saveSlots stack
    else
      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:
split at h
State after:
case inl
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 : εClosure nfa pos next currentSave matched saveSlots stack = (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i next next'

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'

State before:
case inl
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 : εClosure nfa pos next currentSave matched saveSlots stack = (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i next next'

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 => exact upper_bound_εClosure h inv.2
State after:
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'

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
State after:
no goals

State before:
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 : εClosure nfa pos next currentSave matched saveSlots stack = (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i next next'
Tactic:
exact upper_bound_εClosure h inv.2
State after:
no goals

State before:
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:
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
State after:
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.insert target) next'

State before:
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.insert target) next'
Tactic:
simp at h
State after:
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 :
  (match hn : nfa[↑target] with
    | Node.epsilon target' =>
      exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack
    | Node.split target₁ target₂ =>
      exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁, ⋯⟩
        (stack.push (StackEntry.explore ⟨target₂, ⋯⟩))
    | Node.save offset target' =>
      if h : offset < currentSave.size then
        exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset, ⋯⟩ (some pos)) matched saveSlots
          ⟨target', ⋯⟩ (stack.push (StackEntry.restore currentSave))
      else exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack
    | Node.done =>
      εClosure nfa pos (next.insert target) currentSave (HOrElse.hOrElse matched fun x => some currentSave)
        (saveSlots.set ↑target ⋯ currentSave) stack
    | Node.char c next_1 =>
      εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack
    | Node.sparse cs next_1 =>
      εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack
    | Node.fail => εClosure nfa pos (next.insert target) currentSave matched saveSlots stack) =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

State before:
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 :
  (match hn : nfa[↑target] with
    | Node.epsilon target' =>
      exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack
    | Node.split target₁ target₂ =>
      exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁, ⋯⟩
        (stack.push (StackEntry.explore ⟨target₂, ⋯⟩))
    | Node.save offset target' =>
      if h : offset < currentSave.size then
        exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset, ⋯⟩ (some pos)) matched saveSlots
          ⟨target', ⋯⟩ (stack.push (StackEntry.restore currentSave))
      else exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack
    | Node.done =>
      εClosure nfa pos (next.insert target) currentSave (HOrElse.hOrElse matched fun x => some currentSave)
        (saveSlots.set ↑target ⋯ currentSave) stack
    | Node.char c next_1 =>
      εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack
    | Node.sparse cs next_1 =>
      εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack
    | Node.fail => εClosure nfa pos (next.insert target) currentSave matched saveSlots stack) =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
split at h
State after:
case h_1
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
target'✝ : ℕ
heq✝ : nfa[↑target] = Node.epsilon target'✝
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target'✝, ⋯⟩ stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_2
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
target₁✝ target₂✝ : ℕ
heq✝ : nfa[↑target] = Node.split target₁✝ target₂✝
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁✝, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂✝, ⋯⟩)) =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_3
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
offset✝ target'✝ : ℕ
heq✝ : nfa[↑target] = Node.save offset✝ target'✝
h :
  (if h : offset✝ < currentSave.size then
      exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots
        ⟨target'✝, ⋯⟩ (stack.push (StackEntry.restore currentSave))
    else exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target'✝, ⋯⟩ stack) =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_4
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
heq✝ : nfa[↑target] = Node.done
h :
  εClosure nfa pos (next.insert target) currentSave (HOrElse.hOrElse matched fun x => some currentSave)
      (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_5
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
c✝ : Char
next✝ : ℕ
heq✝ : nfa[↑target] = Node.char c✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_6
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
cs✝ : Data.Classes
next✝ : ℕ
heq✝ : nfa[↑target] = Node.sparse cs✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_7
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
heq✝ : nfa[↑target] = Node.fail
h : εClosure nfa pos (next.insert target) currentSave matched saveSlots stack = (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

State before:
case h_1
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
target'✝ : ℕ
heq✝ : nfa[↑target] = Node.epsilon target'✝
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target'✝, ⋯⟩ stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_2
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
target₁✝ target₂✝ : ℕ
heq✝ : nfa[↑target] = Node.split target₁✝ target₂✝
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁✝, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂✝, ⋯⟩)) =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_3
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
offset✝ target'✝ : ℕ
heq✝ : nfa[↑target] = Node.save offset✝ target'✝
h :
  (if h : offset✝ < currentSave.size then
      exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots
        ⟨target'✝, ⋯⟩ (stack.push (StackEntry.restore currentSave))
    else exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target'✝, ⋯⟩ stack) =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_4
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
heq✝ : nfa[↑target] = Node.done
h :
  εClosure nfa pos (next.insert target) currentSave (HOrElse.hOrElse matched fun x => some currentSave)
      (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_5
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
c✝ : Char
next✝ : ℕ
heq✝ : nfa[↑target] = Node.char c✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_6
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
cs✝ : Data.Classes
next✝ : ℕ
heq✝ : nfa[↑target] = Node.sparse cs✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_7
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
heq✝ : nfa[↑target] = Node.fail
h : εClosure nfa pos (next.insert target) currentSave matched saveSlots stack = (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
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'
State after:
case h_2
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
target₁✝ target₂✝ : ℕ
heq✝ : nfa[↑target] = Node.split target₁✝ target₂✝
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁✝, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂✝, ⋯⟩)) =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_3
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
offset✝ target'✝ : ℕ
heq✝ : nfa[↑target] = Node.save offset✝ target'✝
h :
  (if h : offset✝ < currentSave.size then
      exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots
        ⟨target'✝, ⋯⟩ (stack.push (StackEntry.restore currentSave))
    else exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target'✝, ⋯⟩ stack) =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_4
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
heq✝ : nfa[↑target] = Node.done
h :
  εClosure nfa pos (next.insert target) currentSave (HOrElse.hOrElse matched fun x => some currentSave)
      (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_5
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
c✝ : Char
next✝ : ℕ
heq✝ : nfa[↑target] = Node.char c✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_6
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
cs✝ : Data.Classes
next✝ : ℕ
heq✝ : nfa[↑target] = Node.sparse cs✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_7
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
heq✝ : nfa[↑target] = Node.fail
h : εClosure nfa pos (next.insert target) currentSave matched saveSlots stack = (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

State before:
case h_2
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
target₁✝ target₂✝ : ℕ
heq✝ : nfa[↑target] = Node.split target₁✝ target₂✝
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁✝, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂✝, ⋯⟩)) =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_3
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
offset✝ target'✝ : ℕ
heq✝ : nfa[↑target] = Node.save offset✝ target'✝
h :
  (if h : offset✝ < currentSave.size then
      exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots
        ⟨target'✝, ⋯⟩ (stack.push (StackEntry.restore currentSave))
    else exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target'✝, ⋯⟩ stack) =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_4
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
heq✝ : nfa[↑target] = Node.done
h :
  εClosure nfa pos (next.insert target) currentSave (HOrElse.hOrElse matched fun x => some currentSave)
      (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_5
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
c✝ : Char
next✝ : ℕ
heq✝ : nfa[↑target] = Node.char c✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_6
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
cs✝ : Data.Classes
next✝ : ℕ
heq✝ : nfa[↑target] = Node.sparse cs✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_7
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
heq✝ : nfa[↑target] = Node.fail
h : εClosure nfa pos (next.insert target) currentSave matched saveSlots stack = (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
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'
State after:
case h_3
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
offset✝ target'✝ : ℕ
heq✝ : nfa[↑target] = Node.save offset✝ target'✝
h :
  (if h : offset✝ < currentSave.size then
      exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots
        ⟨target'✝, ⋯⟩ (stack.push (StackEntry.restore currentSave))
    else exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target'✝, ⋯⟩ stack) =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_4
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
heq✝ : nfa[↑target] = Node.done
h :
  εClosure nfa pos (next.insert target) currentSave (HOrElse.hOrElse matched fun x => some currentSave)
      (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_5
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
c✝ : Char
next✝ : ℕ
heq✝ : nfa[↑target] = Node.char c✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_6
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
cs✝ : Data.Classes
next✝ : ℕ
heq✝ : nfa[↑target] = Node.sparse cs✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_7
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
heq✝ : nfa[↑target] = Node.fail
h : εClosure nfa pos (next.insert target) currentSave matched saveSlots stack = (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

State before:
case h_3
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
offset✝ target'✝ : ℕ
heq✝ : nfa[↑target] = Node.save offset✝ target'✝
h :
  (if h : offset✝ < currentSave.size then
      exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots
        ⟨target'✝, ⋯⟩ (stack.push (StackEntry.restore currentSave))
    else exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target'✝, ⋯⟩ stack) =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_4
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
heq✝ : nfa[↑target] = Node.done
h :
  εClosure nfa pos (next.insert target) currentSave (HOrElse.hOrElse matched fun x => some currentSave)
      (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_5
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
c✝ : Char
next✝ : ℕ
heq✝ : nfa[↑target] = Node.char c✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_6
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
cs✝ : Data.Classes
next✝ : ℕ
heq✝ : nfa[↑target] = Node.sparse cs✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_7
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
heq✝ : nfa[↑target] = Node.fail
h : εClosure nfa pos (next.insert target) currentSave matched saveSlots stack = (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
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'
State after:
case h_4
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
heq✝ : nfa[↑target] = Node.done
h :
  εClosure nfa pos (next.insert target) currentSave (HOrElse.hOrElse matched fun x => some currentSave)
      (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_5
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
c✝ : Char
next✝ : ℕ
heq✝ : nfa[↑target] = Node.char c✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_6
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
cs✝ : Data.Classes
next✝ : ℕ
heq✝ : nfa[↑target] = Node.sparse cs✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_7
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
heq✝ : nfa[↑target] = Node.fail
h : εClosure nfa pos (next.insert target) currentSave matched saveSlots stack = (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

State before:
case h_4
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
heq✝ : nfa[↑target] = Node.done
h :
  εClosure nfa pos (next.insert target) currentSave (HOrElse.hOrElse matched fun x => some currentSave)
      (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_5
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
c✝ : Char
next✝ : ℕ
heq✝ : nfa[↑target] = Node.char c✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_6
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
cs✝ : Data.Classes
next✝ : ℕ
heq✝ : nfa[↑target] = Node.sparse cs✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_7
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
heq✝ : nfa[↑target] = Node.fail
h : εClosure nfa pos (next.insert target) currentSave matched saveSlots stack = (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
next hn => exact upper_bound_εClosure h inv.2
State after:
case h_5
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
c✝ : Char
next✝ : ℕ
heq✝ : nfa[↑target] = Node.char c✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_6
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
cs✝ : Data.Classes
next✝ : ℕ
heq✝ : nfa[↑target] = Node.sparse cs✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_7
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
heq✝ : nfa[↑target] = Node.fail
h : εClosure nfa pos (next.insert target) currentSave matched saveSlots stack = (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

State before:
case h_5
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
c✝ : Char
next✝ : ℕ
heq✝ : nfa[↑target] = Node.char c✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_6
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
cs✝ : Data.Classes
next✝ : ℕ
heq✝ : nfa[↑target] = Node.sparse cs✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_7
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
heq✝ : nfa[↑target] = Node.fail
h : εClosure nfa pos (next.insert target) currentSave matched saveSlots stack = (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
next hn => exact upper_bound_εClosure h inv.2
State after:
case h_6
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
cs✝ : Data.Classes
next✝ : ℕ
heq✝ : nfa[↑target] = Node.sparse cs✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_7
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
heq✝ : nfa[↑target] = Node.fail
h : εClosure nfa pos (next.insert target) currentSave matched saveSlots stack = (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

State before:
case h_6
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
cs✝ : Data.Classes
next✝ : ℕ
heq✝ : nfa[↑target] = Node.sparse cs✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

case h_7
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
heq✝ : nfa[↑target] = Node.fail
h : εClosure nfa pos (next.insert target) currentSave matched saveSlots stack = (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
next hn => exact upper_bound_εClosure h inv.2
State after:
case h_7
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
heq✝ : nfa[↑target] = Node.fail
h : εClosure nfa pos (next.insert target) currentSave matched saveSlots stack = (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

State before:
case h_7
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
heq✝ : nfa[↑target] = Node.fail
h : εClosure nfa pos (next.insert target) currentSave matched saveSlots stack = (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
next hn => exact upper_bound_εClosure h inv.2
State after:
no goals

State before:
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')
this : UpperBoundεClosure nfa i (next.insert target) next'
⊢ UpperBoundεClosure nfa i next next'
Tactic:
intro j hj
State after:
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')
this : UpperBoundεClosure nfa i (next.insert target) next'
j : Fin nfa.nodes.size
hj : j ∈ next'
⊢ j ∈ next ∨ ↑j ∈ nfa.εClosure ↑i

State before:
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')
this : UpperBoundεClosure nfa i (next.insert target) next'
j : Fin nfa.nodes.size
hj : j ∈ next'
⊢ j ∈ next ∨ ↑j ∈ nfa.εClosure ↑i
Tactic:
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
State after:
no goals

State before:
case inl
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')
this : UpperBoundεClosure nfa i (next.insert target) next'
j : Fin nfa.nodes.size
hj : j ∈ next'
hnext : j ∈ next.insert target
⊢ j ∈ next ∨ ↑j ∈ nfa.εClosure ↑i
Tactic:
cases SparseSet.eq_or_mem_of_mem_insert hnext with
| inl heq => exact .inr (heq ▸ inv.1)
| inr hnext => exact .inl hnext
State after:
no goals

State before:
case inl.inl
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')
this : UpperBoundεClosure nfa i (next.insert target) next'
j : Fin nfa.nodes.size
hj : j ∈ next'
hnext : j ∈ next.insert target
heq : j = target
⊢ j ∈ next ∨ ↑j ∈ nfa.εClosure ↑i
Tactic:
exact .inr (heq ▸ inv.1)
State after:
no goals

State before:
case inl.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')
this : UpperBoundεClosure nfa i (next.insert target) next'
j : Fin nfa.nodes.size
hj : j ∈ next'
hnext✝ : j ∈ next.insert target
hnext : j ∈ next
⊢ j ∈ next ∨ ↑j ∈ nfa.εClosure ↑i
Tactic:
exact .inl hnext
State after:
no goals

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')
this : UpperBoundεClosure nfa i (next.insert target) next'
j : Fin nfa.nodes.size
hj : j ∈ next'
hcls : ↑j ∈ nfa.εClosure ↑i
⊢ j ∈ next ∨ ↑j ∈ nfa.εClosure ↑i
Tactic:
exact .inr hcls
State after:
no goals

State before:
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
target' : ℕ
hn : nfa[↑target] = Node.epsilon target'
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
have isLt := nfa.inBounds' target hn
State after:
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
target' : ℕ
hn : nfa[↑target] = Node.epsilon target'
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack =
    (matched', next', saveSlots')
isLt : (Node.epsilon target').inBounds nfa.nodes.size
⊢ UpperBoundεClosure nfa i (next.insert target) next'

State before:
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
target' : ℕ
hn : nfa[↑target] = Node.epsilon target'
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack =
    (matched', next', saveSlots')
isLt : (Node.epsilon target').inBounds nfa.nodes.size
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
have inv' : UpperInvExploreεClosure nfa i ⟨target', isLt⟩ stack :=
  ⟨εClosure_snoc inv.1 (by simp [εStep, Node.εStep, hn]), inv.2⟩
State after:
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
target' : ℕ
hn : nfa[↑target] = Node.epsilon target'
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack =
    (matched', next', saveSlots')
isLt : (Node.epsilon target').inBounds nfa.nodes.size
inv' : UpperInvExploreεClosure nfa i ⟨target', isLt⟩ stack
⊢ UpperBoundεClosure nfa i (next.insert target) next'

State before:
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
target' : ℕ
hn : nfa[↑target] = Node.epsilon target'
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack =
    (matched', next', saveSlots')
isLt : (Node.epsilon target').inBounds nfa.nodes.size
inv' : UpperInvExploreεClosure nfa i ⟨target', isLt⟩ stack
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
exact upper_bound_exploreεClosure h inv'
State after:
no goals

State before:
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
target' : ℕ
hn : nfa[↑target] = Node.epsilon target'
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack =
    (matched', next', saveSlots')
isLt : (Node.epsilon target').inBounds nfa.nodes.size
⊢ ↑⟨target', isLt⟩ ∈ nfa.εStep ↑target
Tactic:
simp [εStep, Node.εStep, hn]
State after:
no goals

State before:
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
target₁ target₂ : ℕ
hn : nfa[↑target] = Node.split target₁ target₂
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂, ⋯⟩)) =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
have isLt := nfa.inBounds' target hn
State after:
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
target₁ target₂ : ℕ
hn : nfa[↑target] = Node.split target₁ target₂
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂, ⋯⟩)) =
    (matched', next', saveSlots')
isLt : (Node.split target₁ target₂).inBounds nfa.nodes.size
⊢ UpperBoundεClosure nfa i (next.insert target) next'

State before:
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
target₁ target₂ : ℕ
hn : nfa[↑target] = Node.split target₁ target₂
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂, ⋯⟩)) =
    (matched', next', saveSlots')
isLt : (Node.split target₁ target₂).inBounds nfa.nodes.size
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
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])
State after:
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
target₁ target₂ : ℕ
hn : nfa[↑target] = Node.split target₁ target₂
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂, ⋯⟩)) =
    (matched', next', saveSlots')
isLt : (Node.split target₁ target₂).inBounds nfa.nodes.size
inv' : UpperInvExploreεClosure nfa i ⟨target₁, ⋯⟩ (stack.push (StackEntry.explore ⟨target₂, ⋯⟩))
⊢ UpperBoundεClosure nfa i (next.insert target) next'

State before:
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
target₁ target₂ : ℕ
hn : nfa[↑target] = Node.split target₁ target₂
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂, ⋯⟩)) =
    (matched', next', saveSlots')
isLt : (Node.split target₁ target₂).inBounds nfa.nodes.size
inv' : UpperInvExploreεClosure nfa i ⟨target₁, ⋯⟩ (stack.push (StackEntry.explore ⟨target₂, ⋯⟩))
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
exact upper_bound_exploreεClosure h inv'
State after:
no goals

State before:
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
target₁ target₂ : ℕ
hn : nfa[↑target] = Node.split target₁ target₂
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂, ⋯⟩)) =
    (matched', next', saveSlots')
isLt : (Node.split target₁ target₂).inBounds nfa.nodes.size
⊢ UpperInvExploreεClosure nfa i ⟨target₁, ⋯⟩ (stack.push (StackEntry.explore ⟨target₂, ⋯⟩))
Tactic:
refine ⟨εClosure_snoc inv.1 (by simp [εStep, Node.εStep, hn]), ?_⟩
State after:
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
target₁ target₂ : ℕ
hn : nfa[↑target] = Node.split target₁ target₂
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂, ⋯⟩)) =
    (matched', next', saveSlots')
isLt : (Node.split target₁ target₂).inBounds nfa.nodes.size
⊢ ∀ (j : Fin nfa.nodes.size), StackEntry.explore j ∈ stack.push (StackEntry.explore ⟨target₂, ⋯⟩) → ↑j ∈ nfa.εClosure ↑i

State before:
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
target₁ target₂ : ℕ
hn : nfa[↑target] = Node.split target₁ target₂
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂, ⋯⟩)) =
    (matched', next', saveSlots')
isLt : (Node.split target₁ target₂).inBounds nfa.nodes.size
⊢ ∀ (j : Fin nfa.nodes.size), StackEntry.explore j ∈ stack.push (StackEntry.explore ⟨target₂, ⋯⟩) → ↑j ∈ nfa.εClosure ↑i
Tactic:
intro j hj
State after:
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
target₁ target₂ : ℕ
hn : nfa[↑target] = Node.split target₁ target₂
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂, ⋯⟩)) =
    (matched', next', saveSlots')
isLt : (Node.split target₁ target₂).inBounds nfa.nodes.size
j : Fin nfa.nodes.size
hj : StackEntry.explore j ∈ stack.push (StackEntry.explore ⟨target₂, ⋯⟩)
⊢ ↑j ∈ nfa.εClosure ↑i

State before:
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
target₁ target₂ : ℕ
hn : nfa[↑target] = Node.split target₁ target₂
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂, ⋯⟩)) =
    (matched', next', saveSlots')
isLt : (Node.split target₁ target₂).inBounds nfa.nodes.size
j : Fin nfa.nodes.size
hj : StackEntry.explore j ∈ stack.push (StackEntry.explore ⟨target₂, ⋯⟩)
⊢ ↑j ∈ nfa.εClosure ↑i
Tactic:
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])
State after:
no goals

State before:
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
target₁ target₂ : ℕ
hn : nfa[↑target] = Node.split target₁ target₂
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂, ⋯⟩)) =
    (matched', next', saveSlots')
isLt : (Node.split target₁ target₂).inBounds nfa.nodes.size
⊢ ↑⟨target₁, ⋯⟩ ∈ nfa.εStep ↑target
Tactic:
simp [εStep, Node.εStep, hn]
State after:
no goals

State before:
case inl
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
target₁ target₂ : ℕ
hn : nfa[↑target] = Node.split target₁ target₂
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂, ⋯⟩)) =
    (matched', next', saveSlots')
isLt : (Node.split target₁ target₂).inBounds nfa.nodes.size
j : Fin nfa.nodes.size
hj✝ : StackEntry.explore j ∈ stack.push (StackEntry.explore ⟨target₂, ⋯⟩)
hj : StackEntry.explore j ∈ stack
⊢ ↑j ∈ nfa.εClosure ↑i
Tactic:
exact inv.2 j hj
State after:
no goals

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
target₁ target₂ : ℕ
hn : nfa[↑target] = Node.split target₁ target₂
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂, ⋯⟩)) =
    (matched', next', saveSlots')
isLt : (Node.split target₁ target₂).inBounds nfa.nodes.size
j : Fin nfa.nodes.size
hj✝ : StackEntry.explore j ∈ stack.push (StackEntry.explore ⟨target₂, ⋯⟩)
hj : StackEntry.explore j = StackEntry.explore ⟨target₂, ⋯⟩
⊢ ↑j ∈ nfa.εClosure ↑i
Tactic:
simp at hj
State after:
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
target₁ target₂ : ℕ
hn : nfa[↑target] = Node.split target₁ target₂
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂, ⋯⟩)) =
    (matched', next', saveSlots')
isLt : (Node.split target₁ target₂).inBounds nfa.nodes.size
j : Fin nfa.nodes.size
hj✝ : StackEntry.explore j ∈ stack.push (StackEntry.explore ⟨target₂, ⋯⟩)
hj : j = ⟨target₂, ⋯⟩
⊢ ↑j ∈ nfa.εClosure ↑i

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
target₁ target₂ : ℕ
hn : nfa[↑target] = Node.split target₁ target₂
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂, ⋯⟩)) =
    (matched', next', saveSlots')
isLt : (Node.split target₁ target₂).inBounds nfa.nodes.size
j : Fin nfa.nodes.size
hj✝ : StackEntry.explore j ∈ stack.push (StackEntry.explore ⟨target₂, ⋯⟩)
hj : j = ⟨target₂, ⋯⟩
⊢ ↑j ∈ nfa.εClosure ↑i
Tactic:
exact εClosure_snoc inv.1 (by simp [εStep, Node.εStep, hn, hj])
State after:
no goals

State before:
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
target₁ target₂ : ℕ
hn : nfa[↑target] = Node.split target₁ target₂
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target₁, ⋯⟩
      (stack.push (StackEntry.explore ⟨target₂, ⋯⟩)) =
    (matched', next', saveSlots')
isLt : (Node.split target₁ target₂).inBounds nfa.nodes.size
j : Fin nfa.nodes.size
hj✝ : StackEntry.explore j ∈ stack.push (StackEntry.explore ⟨target₂, ⋯⟩)
hj : j = ⟨target₂, ⋯⟩
⊢ ↑j ∈ nfa.εStep ↑target
Tactic:
simp [εStep, Node.εStep, hn, hj]
State after:
no goals

State before:
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
h :
  (if h : offset✝ < currentSave.size then
      exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots
        ⟨target', ⋯⟩ (stack.push (StackEntry.restore currentSave))
    else exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack) =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
have isLt := nfa.inBounds' target hn
State after:
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
h :
  (if h : offset✝ < currentSave.size then
      exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots
        ⟨target', ⋯⟩ (stack.push (StackEntry.restore currentSave))
    else exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack) =
    (matched', next', saveSlots')
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
⊢ UpperBoundεClosure nfa i (next.insert target) next'

State before:
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
h :
  (if h : offset✝ < currentSave.size then
      exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots
        ⟨target', ⋯⟩ (stack.push (StackEntry.restore currentSave))
    else exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack) =
    (matched', next', saveSlots')
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
split at h
State after:
case inl
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots ⟨target', ⋯⟩
      (stack.push (StackEntry.restore currentSave)) =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : ¬offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

State before:
case inl
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots ⟨target', ⋯⟩
      (stack.push (StackEntry.restore currentSave)) =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : ¬offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
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'
State after:
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : ¬offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'

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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : ¬offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
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'
State after:
no goals

State before:
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots ⟨target', ⋯⟩
      (stack.push (StackEntry.restore currentSave)) =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
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
State after:
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots ⟨target', ⋯⟩
      (stack.push (StackEntry.restore currentSave)) =
    (matched', next', saveSlots')
inv' : UpperInvExploreεClosure nfa i ⟨target', isLt⟩ (stack.push (StackEntry.restore currentSave))
⊢ UpperBoundεClosure nfa i (next.insert target) next'

State before:
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots ⟨target', ⋯⟩
      (stack.push (StackEntry.restore currentSave)) =
    (matched', next', saveSlots')
inv' : UpperInvExploreεClosure nfa i ⟨target', isLt⟩ (stack.push (StackEntry.restore currentSave))
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
exact upper_bound_exploreεClosure h inv'
State after:
no goals

State before:
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots ⟨target', ⋯⟩
      (stack.push (StackEntry.restore currentSave)) =
    (matched', next', saveSlots')
⊢ UpperInvExploreεClosure nfa i ⟨target', isLt⟩ (stack.push (StackEntry.restore currentSave))
Tactic:
refine ⟨εClosure_snoc inv.1 (by simp [εStep, Node.εStep, hn]), ?_⟩
State after:
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots ⟨target', ⋯⟩
      (stack.push (StackEntry.restore currentSave)) =
    (matched', next', saveSlots')
⊢ ∀ (j : Fin nfa.nodes.size), StackEntry.explore j ∈ stack.push (StackEntry.restore currentSave) → ↑j ∈ nfa.εClosure ↑i

State before:
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots ⟨target', ⋯⟩
      (stack.push (StackEntry.restore currentSave)) =
    (matched', next', saveSlots')
⊢ ∀ (j : Fin nfa.nodes.size), StackEntry.explore j ∈ stack.push (StackEntry.restore currentSave) → ↑j ∈ nfa.εClosure ↑i
Tactic:
intro j hj
State after:
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots ⟨target', ⋯⟩
      (stack.push (StackEntry.restore currentSave)) =
    (matched', next', saveSlots')
j : Fin nfa.nodes.size
hj : StackEntry.explore j ∈ stack.push (StackEntry.restore currentSave)
⊢ ↑j ∈ nfa.εClosure ↑i

State before:
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots ⟨target', ⋯⟩
      (stack.push (StackEntry.restore currentSave)) =
    (matched', next', saveSlots')
j : Fin nfa.nodes.size
hj : StackEntry.explore j ∈ stack.push (StackEntry.restore currentSave)
⊢ ↑j ∈ nfa.εClosure ↑i
Tactic:
cases (Array.mem_push ..).mp hj with
| inl hj => exact inv.2 j hj
| inr hj => contradiction
State after:
no goals

State before:
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots ⟨target', ⋯⟩
      (stack.push (StackEntry.restore currentSave)) =
    (matched', next', saveSlots')
⊢ ↑⟨target', isLt⟩ ∈ nfa.εStep ↑target
Tactic:
simp [εStep, Node.εStep, hn]
State after:
no goals

State before:
case inl
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots ⟨target', ⋯⟩
      (stack.push (StackEntry.restore currentSave)) =
    (matched', next', saveSlots')
j : Fin nfa.nodes.size
hj✝ : StackEntry.explore j ∈ stack.push (StackEntry.restore currentSave)
hj : StackEntry.explore j ∈ stack
⊢ ↑j ∈ nfa.εClosure ↑i
Tactic:
exact inv.2 j hj
State after:
no goals

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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) (currentSave.set ⟨offset✝, ⋯⟩ (some pos)) matched saveSlots ⟨target', ⋯⟩
      (stack.push (StackEntry.restore currentSave)) =
    (matched', next', saveSlots')
j : Fin nfa.nodes.size
hj✝ : StackEntry.explore j ∈ stack.push (StackEntry.restore currentSave)
hj : StackEntry.explore j = StackEntry.restore currentSave
⊢ ↑j ∈ nfa.εClosure ↑i
Tactic:
contradiction
State after:
no goals

State before:
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : ¬offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
have inv' : UpperInvExploreεClosure nfa i ⟨target', isLt⟩ stack :=
  ⟨εClosure_snoc inv.1 (by simp [εStep, Node.εStep, hn]), inv.2⟩
State after:
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : ¬offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack =
    (matched', next', saveSlots')
inv' : UpperInvExploreεClosure nfa i ⟨target', isLt⟩ stack
⊢ UpperBoundεClosure nfa i (next.insert target) next'

State before:
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : ¬offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack =
    (matched', next', saveSlots')
inv' : UpperInvExploreεClosure nfa i ⟨target', isLt⟩ stack
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
exact upper_bound_exploreεClosure h inv'
State after:
no goals

State before:
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
offset✝ target' : ℕ
hn : nfa[↑target] = Node.save offset✝ target'
isLt : (Node.save offset✝ target').inBounds nfa.nodes.size
h✝ : ¬offset✝ < currentSave.size
h :
  exploreεClosure nfa pos (next.insert target) currentSave matched saveSlots ⟨target', ⋯⟩ stack =
    (matched', next', saveSlots')
⊢ ↑⟨target', isLt⟩ ∈ nfa.εStep ↑target
Tactic:
simp [εStep, Node.εStep, hn]
State after:
no goals

State before:
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
hn : nfa[↑target] = Node.done
h :
  εClosure nfa pos (next.insert target) currentSave (HOrElse.hOrElse matched fun x => some currentSave)
      (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
exact upper_bound_εClosure h inv.2
State after:
no goals

State before:
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
c✝ : Char
next✝ : ℕ
hn : nfa[↑target] = Node.char c✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
exact upper_bound_εClosure h inv.2
State after:
no goals

State before:
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
cs✝ : Data.Classes
next✝ : ℕ
hn : nfa[↑target] = Node.sparse cs✝ next✝
h :
  εClosure nfa pos (next.insert target) currentSave matched (saveSlots.set ↑target ⋯ currentSave) stack =
    (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
exact upper_bound_εClosure h inv.2
State after:
no goals

State before:
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
hn : nfa[↑target] = Node.fail
h : εClosure nfa pos (next.insert target) currentSave matched saveSlots stack = (matched', next', saveSlots')
⊢ UpperBoundεClosure nfa i (next.insert target) next'
Tactic:
exact upper_bound_εClosure h inv.2
State after:
no goals