module public import Regula.Input public import Regula.Reach public section namespace Regula.Closed open Lean (Name) namespace Input variable (i : Input) inductive Reaches : Name → Name → Prop | refl (c : Name) : Reaches c c | step {c u e : Name} (d : Decl) : d ∈ i.decls → d.name = c → u ∈ d.uses → Reaches u e → Reaches c e def AxiomOf (c a : Name) : Prop := i.Reaches c a ∧ ∃ d ∈ i.decls, d.name = a ∧ d.kind = .axiom end Input structure Tables (i : Input) where index : Std.HashMap Name Decl mem_index : ∀ n d, index[n]? = some d ↔ d ∈ i.decls ∧ d.name = n claimed : List Decl mem_claimed : ∀ d, d ∈ claimed ↔ d ∈ i.decls ∧ d.module ∈ i.modules axiomReach : List (Name × Std.HashSet Name) mem_axiomReach : ∀ c a, (∃ s, (a, s) ∈ axiomReach ∧ s.contains c = true) ↔ i.AxiomOf c a namespace Input variable (i : Input) theorem mem_indexFold (l : List Decl) (hu : (l.map (·.name)).Nodup) (init : Std.HashMap Name Decl) (n : Name) (d : Decl) : (l.foldl (fun m d => m.insert d.name d) init)[n]? = some d ↔ (d ∈ l ∧ d.name = n) ∨ (n ∉ l.map (·.name) ∧ init[n]? = some d) := by induction l generalizing init with | nil => simp | cons a l ih => simp only [List.map_cons, List.nodup_cons] at hu simp only [List.foldl_cons, ih hu.2, Std.HashMap.getElem?_insert, List.mem_cons, List.map_cons, not_or] by_cases han : a.name = n · subst han have hnot : a.name ∈ l.map (·.name) → False := hu.1 constructor · rintro (⟨hd, hn⟩ | ⟨-, h⟩) · exact .inl ⟨.inr hd, hn⟩ · simp only [beq_self_eq_true, ↓reduceIte, Option.some.injEq] at h subst h exact .inl ⟨.inl rfl, rfl⟩ · rintro (⟨rfl | hd, hn⟩ | ⟨h, -⟩) · exact .inr ⟨hnot, by simp⟩ · exact .inl ⟨hd, hn⟩ · exact absurd rfl h.1 · have hb : (a.name == n) = false := by simpa using han simp only [hb, Bool.false_eq_true, ↓reduceIte] constructor · rintro (⟨hd, hn⟩ | ⟨h1, h2⟩) · exact .inl ⟨.inr hd, hn⟩ · exact .inr ⟨⟨fun h => han h.symm, h1⟩, h2⟩ · rintro (⟨rfl | hd, hn⟩ | ⟨h1, h2⟩) · exact absurd hn han · exact .inl ⟨hd, hn⟩ · exact .inr ⟨h1.2, h2⟩ def index : Std.HashMap Name Decl := i.decls.foldl (fun m d => m.insert d.name d) (Std.HashMap.emptyWithCapacity i.decls.length) theorem index_eq_some (n : Name) (d : Decl) : i.index[n]? = some d ↔ d ∈ i.decls ∧ d.name = n := by rw [index, mem_indexFold _ i.unique] simp def claimedDecls : List Decl := let claimed := setOf i.modules i.decls.filter (claimed.contains ·.module) theorem mem_claimedDecls (d : Decl) : d ∈ i.claimedDecls ↔ d ∈ i.decls ∧ d.module ∈ i.modules := by simp only [claimedDecls, List.mem_filter, mem_setOf] def declIdMapGo (decls : Array Decl) : Nat → Nat → Std.HashMap Name Nat → Std.HashMap Name Nat | 0, _, m => m | fuel + 1, idx, m => if h : idx < decls.size then declIdMapGo decls fuel (idx + 1) (m.insert decls[idx].name idx) else m def declIdMap (decls : Array Decl) : Std.HashMap Name Nat := declIdMapGo decls decls.size 0 (Std.HashMap.emptyWithCapacity decls.size) theorem list_nodup_getElem_inj {α : Type} : ∀ {l : List α}, l.Nodup → ∀ {j₁ j₂ : Nat} (h₁ : j₁ < l.length) (h₂ : j₂ < l.length), l[j₁] = l[j₂] → j₁ = j₂ | [], _, _, _, h₁, _, _ => by cases h₁ | a :: l, hnd, j₁, j₂, h₁, h₂, heq => by rw [List.nodup_cons] at hnd cases j₁ with | zero => cases j₂ with | zero => rfl | succ j₂ => simp only [List.getElem_cons_zero, List.getElem_cons_succ] at heq exact absurd (heq ▸ List.getElem_mem (by simpa using h₂)) hnd.1 | succ j₁ => cases j₂ with | zero => simp only [List.getElem_cons_zero, List.getElem_cons_succ] at heq exact absurd (heq.symm ▸ List.getElem_mem (by simpa using h₁)) hnd.1 | succ j₂ => simp only [List.getElem_cons_succ] at heq exact congrArg Nat.succ (list_nodup_getElem_inj hnd.2 (by simpa using h₁) (by simpa using h₂) heq) theorem decls_name_inj {decls : Array Decl} (hu : (decls.toList.map (·.name)).Nodup) {j₁ j₂ : Nat} (h₁ : j₁ < decls.size) (h₂ : j₂ < decls.size) (heq : decls[j₁].name = decls[j₂].name) : j₁ = j₂ := by have h1' : j₁ < (decls.toList.map (·.name)).length := by simpa using h₁ have h2' : j₂ < (decls.toList.map (·.name)).length := by simpa using h₂ exact list_nodup_getElem_inj hu h1' h2' (by simpa using heq) theorem mem_declIdMapGo (decls : Array Decl) (hu : (decls.toList.map (·.name)).Nodup) (fuel idx : Nat) (m : Std.HashMap Name Nat) (n : Name) (k : Nat) : (declIdMapGo decls fuel idx m)[n]? = some k ↔ (∃ hk : k < decls.size, idx ≤ k ∧ k < idx + fuel ∧ decls[k].name = n) ∨ ((∀ j (hj : j < decls.size), idx ≤ j → j < idx + fuel → decls[j].name ≠ n) ∧ m[n]? = some k) := by induction fuel generalizing idx m with | zero => simp only [declIdMapGo] constructor · intro hm exact .inr ⟨fun _ _ h1 h2 => by omega, hm⟩ · rintro (⟨_, h1, h2, _⟩ | ⟨_, hm⟩) · omega · exact hm | succ fuel ih => unfold declIdMapGo split · rename_i hidx rw [ih (idx + 1) (m.insert decls[idx].name idx), Std.HashMap.getElem?_insert] by_cases hcur : decls[idx].name = n · have hbeq : (decls[idx].name == n) = true := by simp [hcur] simp only [hbeq, ↓reduceIte, Option.some.injEq] constructor · rintro (⟨hk, hge, hlt, hname⟩ | ⟨_, rfl⟩) · exact .inl ⟨hk, by omega, by omega, hname⟩ · exact .inl ⟨hidx, Nat.le_refl _, by omega, hcur⟩ · rintro (⟨hk, hge, hlt, hname⟩ | ⟨hall, _⟩) · have hkeq : k = idx := decls_name_inj hu hk hidx (hname.trans hcur.symm) subst hkeq exact .inr ⟨fun j hj hj1 _ hjn => by have := decls_name_inj hu hj hidx (hjn.trans hcur.symm) omega, rfl⟩ · exact absurd hcur (hall idx hidx (Nat.le_refl _) (by omega)) · have hbeq : (decls[idx].name == n) = false := beq_false_of_ne hcur simp only [hbeq, Bool.false_eq_true, ↓reduceIte] constructor · rintro (⟨hk, hge, hlt, hname⟩ | ⟨hall, hm⟩) · exact .inl ⟨hk, by omega, by omega, hname⟩ · refine .inr ⟨fun j hj hj1 hj2 => ?_, hm⟩ by_cases hji : j = idx · subst hji; exact hcur · exact hall j hj (by omega) (by omega) · rintro (⟨hk, hge, hlt, hname⟩ | ⟨hall, hm⟩) · have hki : k ≠ idx := fun h => hcur (h ▸ hname) exact .inl ⟨hk, by omega, by omega, hname⟩ · exact .inr ⟨fun j hj hj1 hj2 => hall j hj (by omega) (by omega), hm⟩ · rename_i hidx constructor · intro hm exact .inr ⟨fun j hj hj1 _ => by omega, hm⟩ · rintro (⟨hk, hge, _, _⟩ | ⟨_, hm⟩) · omega · exact hm theorem mem_declIdMap (decls : Array Decl) (hu : (decls.toList.map (·.name)).Nodup) (n : Name) (k : Nat) : (declIdMap decls)[n]? = some k ↔ ∃ hk : k < decls.size, decls[k].name = n := by rw [declIdMap, mem_declIdMapGo decls hu decls.size 0 _ n k] simp only [Nat.zero_le, Nat.zero_add, true_and, Std.HashMap.getElem?_emptyWithCapacity, reduceCtorEq, and_false, or_false] constructor · rintro ⟨hk, _, hn⟩; exact ⟨hk, hn⟩ · rintro ⟨hk, hn⟩; exact ⟨hk, hk, hn⟩ def resolveUsesStep (idOf : Std.HashMap Name Nat) (acc : Array Nat) (u : Name) : Array Nat := match idOf[u]? with | some uid => acc.push uid | none => acc def resolveUses (idOf : Std.HashMap Name Nat) (uses : List Name) : Array Nat := uses.foldl (resolveUsesStep idOf) #[] theorem mem_resolveUsesFold (idOf : Std.HashMap Name Nat) (uses : List Name) (acc : Array Nat) (uid : Nat) : uid ∈ uses.foldl (resolveUsesStep idOf) acc ↔ (∃ u ∈ uses, idOf[u]? = some uid) ∨ uid ∈ acc := by induction uses generalizing acc with | nil => simp | cons u us ih => simp only [List.foldl_cons, ih, List.mem_cons, resolveUsesStep] cases hget : idOf[u]? with | none => constructor · rintro (⟨w, hw, h⟩ | h) · exact .inl ⟨w, .inr hw, h⟩ · exact .inr h · rintro (⟨w, rfl | hw, h⟩ | h) · rw [hget] at h; cases h · exact .inl ⟨w, hw, h⟩ · exact .inr h | some v => simp only [Array.mem_push] constructor · rintro (⟨w, hw, h⟩ | (h | rfl)) · exact .inl ⟨w, .inr hw, h⟩ · exact .inr h · exact .inl ⟨u, .inl rfl, hget⟩ · rintro (⟨w, rfl | hw, h⟩ | h) · rw [hget] at h; cases h; exact .inr (.inr rfl) · exact .inl ⟨w, hw, h⟩ · exact .inr (.inl h) theorem mem_resolveUses (idOf : Std.HashMap Name Nat) (uses : List Name) (uid : Nat) : uid ∈ resolveUses idOf uses ↔ ∃ u ∈ uses, idOf[u]? = some uid := by simpa [resolveUses] using mem_resolveUsesFold idOf uses #[] uid def forwardEdges (decls : Array Decl) (idOf : Std.HashMap Name Nat) : Array (Array Nat) := mapArrayPar 4096 (fun d => resolveUses idOf d.uses) decls def addRevEdges (rev : Array (Array Nat)) (src : Nat) (us : Array Nat) : Array (Array Nat) := us.foldl (init := rev) fun r u => r.modify u (·.push src) theorem size_addRevEdgesFold (src : Nat) (us : List Nat) (rev : Array (Array Nat)) : (us.foldl (fun r u => r.modify u (·.push src)) rev).size = rev.size := by induction us generalizing rev with | nil => rfl | cons u us ih => simp only [List.foldl_cons, ih, Array.size_modify] theorem size_addRevEdges (rev : Array (Array Nat)) (src : Nat) (us : Array Nat) : (addRevEdges rev src us).size = rev.size := by simpa [addRevEdges, ← Array.foldl_toList] using size_addRevEdgesFold src us.toList rev theorem mem_addRevEdgesFold (src : Nat) (us : List Nat) (rev : Array (Array Nat)) {u : Nat} (hu : u < rev.size) (c : Nat) : c ∈ (us.foldl (fun r v => r.modify v (·.push src)) rev)[u]'(by rw [size_addRevEdgesFold]; exact hu) ↔ (u ∈ us ∧ c = src) ∨ c ∈ rev[u] := by induction us generalizing rev with | nil => simp | cons v vs ih => simp only [List.foldl_cons, List.mem_cons] have hu' : u < (rev.modify v (·.push src)).size := by simpa [Array.size_modify] using hu rw [ih (rev.modify v (·.push src)) hu'] by_cases hvu : v = u · subst hvu have hget : (rev.modify v (·.push src))[v]'hu' = rev[v].push src := by simp [Array.getElem_modify] rw [hget, Array.mem_push] constructor · rintro (⟨h1, rfl⟩ | (h | rfl)) · exact .inl ⟨.inr h1, rfl⟩ · exact .inr h · exact .inl ⟨.inl rfl, rfl⟩ · rintro (⟨heq | h1, rfl⟩ | h) · exact .inr (.inr rfl) · exact .inl ⟨h1, rfl⟩ · exact .inr (.inl h) · have hget : (rev.modify v (·.push src))[u]'hu' = rev[u] := by rw [Array.getElem_modify] simp [hvu] rw [hget] constructor · rintro (⟨h1, rfl⟩ | h) · exact .inl ⟨.inr h1, rfl⟩ · exact .inr h · rintro (⟨h1 | h1, rfl⟩ | h) · exact absurd h1.symm hvu · exact .inl ⟨h1, rfl⟩ · exact .inr h theorem mem_addRevEdges (rev : Array (Array Nat)) (src : Nat) (us : Array Nat) {u : Nat} (hu : u < rev.size) (c : Nat) : c ∈ (addRevEdges rev src us)[u]'(by rw [size_addRevEdges]; exact hu) ↔ (u ∈ us ∧ c = src) ∨ c ∈ rev[u] := by have h := mem_addRevEdgesFold src us.toList rev hu c simpa [addRevEdges, ← Array.foldl_toList, Array.mem_toList_iff] using h def reverseEdgesGo (fwd : Array (Array Nat)) : Nat → Nat → Array (Array Nat) → Array (Array Nat) | 0, _, rev => rev | fuel + 1, idx, rev => if h : idx < fwd.size then reverseEdgesGo fwd fuel (idx + 1) (addRevEdges rev idx fwd[idx]) else rev def reverseEdges (fwd : Array (Array Nat)) : Array (Array Nat) := reverseEdgesGo fwd fwd.size 0 (Array.replicate fwd.size #[]) theorem size_reverseEdgesGo (fwd : Array (Array Nat)) (fuel idx : Nat) (rev : Array (Array Nat)) : (reverseEdgesGo fwd fuel idx rev).size = rev.size := by induction fuel generalizing idx rev with | zero => rfl | succ fuel ih => unfold reverseEdgesGo split · rw [ih, size_addRevEdges] · rfl theorem size_reverseEdges (fwd : Array (Array Nat)) : (reverseEdges fwd).size = fwd.size := by rw [reverseEdges, size_reverseEdgesGo, Array.size_replicate] theorem mem_reverseEdgesGo (fwd : Array (Array Nat)) (fuel idx : Nat) (rev : Array (Array Nat)) {u : Nat} (hu : u < rev.size) (c : Nat) : c ∈ (reverseEdgesGo fwd fuel idx rev)[u]'(by rw [size_reverseEdgesGo]; exact hu) ↔ (∃ hc : c < fwd.size, idx ≤ c ∧ c < idx + fuel ∧ u ∈ fwd[c]) ∨ c ∈ rev[u] := by induction fuel generalizing idx rev with | zero => simp only [reverseEdgesGo] constructor · exact .inr · rintro (⟨_, h1, h2, _⟩ | h) · omega · exact h | succ fuel ih => unfold reverseEdgesGo split · rename_i hidx have hu' : u < (addRevEdges rev idx fwd[idx]).size := by rw [size_addRevEdges]; exact hu rw [ih (idx + 1) (addRevEdges rev idx fwd[idx]) hu', mem_addRevEdges rev idx fwd[idx] hu c] constructor · rintro (⟨hc, hge, hlt, hm⟩ | (⟨hm, rfl⟩ | h)) · exact .inl ⟨hc, by omega, by omega, hm⟩ · exact .inl ⟨hidx, Nat.le_refl _, by omega, hm⟩ · exact .inr h · rintro (⟨hc, hge, hlt, hm⟩ | h) · by_cases hci : c = idx · subst hci; exact .inr (.inl ⟨hm, rfl⟩) · exact .inl ⟨hc, by omega, by omega, hm⟩ · exact .inr (.inr h) · rename_i hidx constructor · exact .inr · rintro (⟨hc, hge, _, _⟩ | h) · omega · exact h theorem mem_reverseEdges (fwd : Array (Array Nat)) {u : Nat} (hu : u < fwd.size) (c : Nat) : c ∈ (reverseEdges fwd)[u]'(by rw [size_reverseEdges]; exact hu) ↔ ∃ hc : c < fwd.size, u ∈ fwd[c] := by have hu0 : u < (Array.replicate fwd.size (#[] : Array Nat)).size := by simpa [Array.size_replicate] using hu have h := mem_reverseEdgesGo fwd fwd.size 0 (Array.replicate fwd.size #[]) hu0 c simp only [Nat.zero_le, Nat.zero_add, true_and, Array.getElem_replicate, Array.not_mem_empty, or_false] at h have hdef : (reverseEdges fwd)[u]'(by rw [size_reverseEdges]; exact hu) = (reverseEdgesGo fwd fwd.size 0 (Array.replicate fwd.size #[]))[u]'(by rw [size_reverseEdgesGo]; exact hu0) := rfl rw [hdef] refine h.trans ⟨?_, ?_⟩ · rintro ⟨hc, _, hm⟩; exact ⟨hc, hm⟩ · rintro ⟨hc, hm⟩; exact ⟨hc, hc, hm⟩ inductive ReachNat (rev : Array (Array Nat)) (s : Nat) : Nat → Prop | refl : ReachNat rev s s | step {b c : Nat} : ReachNat rev s b → (hb : b < rev.size) → c ∈ rev[b] → c < rev.size → ReachNat rev s c def pushUnseenStep (acc : Array Bool × Array Nat) (c : Nat) : Array Bool × Array Nat := let (s, w) := acc if h : c < s.size then if s[c] then (s, w) else (s.set c true, w.push c) else (s, w) def pushUnseenNat (seen : Array Bool) (work : Array Nat) (xs : Array Nat) : Array Bool × Array Nat := xs.foldl pushUnseenStep (seen, work) def reachGoNat (rev : Array (Array Nat)) : Nat → Array Bool → Array Nat → Array Bool | 0, seen, _ => seen | k + 1, seen, work => if h : work.size = 0 then seen else let u := work[work.size - 1] let rest := work.pop if hu : u < rev.size then let (seen', work') := pushUnseenNat seen rest rev[u] reachGoNat rev k seen' work' else reachGoNat rev k seen rest def reachNat (rev : Array (Array Nat)) (seed : Nat) : Array Bool := let seen0 := Array.replicate rev.size false if h : seed < seen0.size then reachGoNat rev rev.size (seen0.set seed true) #[seed] else seen0 def countFalse (seen : Array Bool) : Nat := seen.toList.count false theorem countFalse_set_true {seen : Array Bool} {c : Nat} (hc : c < seen.size) (hf : seen[c] = false) : countFalse (seen.set c true) + 1 = countFalse seen := by unfold countFalse rw [Array.toList_set] have hlen : c < seen.toList.length := by simpa using hc have hget : seen.toList[c] = false := by simpa using hf clear hc hf generalize seen.toList = l at hlen hget ⊢ induction l generalizing c with | nil => cases hlen | cons a l ih => cases c with | zero => simp only [List.getElem_cons_zero] at hget subst hget simp [List.set] | succ c => simp only [List.getElem_cons_succ] at hget have hlen' : c < l.length := by simpa using hlen have hrec := ih hlen' hget simp only [List.set, List.count_cons] omega theorem countFalse_replicate (n : Nat) : countFalse (Array.replicate n false) = n := by unfold countFalse rw [Array.toList_replicate] induction n with | zero => rfl | succ n ih => rw [List.replicate_succ, List.count_cons_self, ih] theorem size_pushUnseenFold (xs : List Nat) (seen : Array Bool) (work : Array Nat) : (xs.foldl pushUnseenStep (seen, work)).1.size = seen.size := by induction xs generalizing seen work with | nil => rfl | cons c cs ih => simp only [List.foldl_cons, pushUnseenStep] split · split · exact ih seen work · rw [ih, Array.size_set] · exact ih seen work theorem size_pushUnseenNat (seen : Array Bool) (work : Array Nat) (xs : Array Nat) : (pushUnseenNat seen work xs).1.size = seen.size := by simpa [pushUnseenNat, ← Array.foldl_toList] using size_pushUnseenFold xs.toList seen work theorem pushUnseenFold_props (xs : List Nat) (seen : Array Bool) (work : Array Nat) : (∀ (j : Nat) (hj : j < seen.size), seen[j] = true → (xs.foldl pushUnseenStep (seen, work)).1[j]'(by rw [size_pushUnseenFold]; exact hj) = true) ∧ (∀ (c : Nat), c ∈ xs → ∀ (hcs : c < seen.size), (xs.foldl pushUnseenStep (seen, work)).1[c]'(by rw [size_pushUnseenFold]; exact hcs) = true) ∧ (∀ x ∈ work, x ∈ (xs.foldl pushUnseenStep (seen, work)).2) ∧ (∀ (j : Nat) (hj : j < seen.size), seen[j] = false → (xs.foldl pushUnseenStep (seen, work)).1[j]'(by rw [size_pushUnseenFold]; exact hj) = true → j ∈ (xs.foldl pushUnseenStep (seen, work)).2) ∧ (∀ x ∈ (xs.foldl pushUnseenStep (seen, work)).2, x ∈ work ∨ (x ∈ xs ∧ x < seen.size)) ∧ (xs.foldl pushUnseenStep (seen, work)).2.size + countFalse (xs.foldl pushUnseenStep (seen, work)).1 = work.size + countFalse seen := by induction xs generalizing seen work with | nil => simp only [List.foldl_nil] refine ⟨fun _ _ h => h, fun _ hc => (by cases hc), fun _ h => h, fun _ _ h1 h2 => (by rw [h1] at h2; cases h2), fun _ h => .inl h, trivial⟩ | cons c cs ih => simp only [List.foldl_cons, pushUnseenStep] by_cases hcs : c < seen.size · by_cases hsc : seen[c] = true · simp only [hcs, ↓reduceDIte, hsc, ↓reduceIte] obtain ⟨hmono, hmem, hwsub, hnew, hwmem, hmeas⟩ := ih seen work refine ⟨hmono, fun d hd hds => ?_, hwsub, hnew, fun x hx => ?_, hmeas⟩ · rcases List.mem_cons.mp hd with heq | hd · subst heq; exact hmono d hcs hsc · exact hmem d hd hds · rcases hwmem x hx with hx | ⟨hx, hxs⟩ · exact .inl hx · exact .inr ⟨List.mem_cons_of_mem _ hx, hxs⟩ · have hscf : seen[c] = false := by cases hb : seen[c] <;> simp_all simp only [hcs, ↓reduceDIte, hscf, Bool.false_eq_true, ↓reduceIte] have hsz1 : (seen.set c true hcs).size = seen.size := Array.size_set hcs obtain ⟨hmono, hmem, hwsub, hnew, hwmem, hmeas⟩ := ih (seen.set c true hcs) (work.push c) have hcf := countFalse_set_true hcs hscf refine ⟨?_, ?_, ?_, ?_, ?_, ?_⟩ · intro j hj hjs have hj1 : j < (seen.set c true hcs).size := hsz1.symm ▸ hj have hset : (seen.set c true hcs)[j]'hj1 = true := by rw [Array.getElem_set] split · rfl · exact hjs exact hmono j hj1 hset · intro d hd hds rcases List.mem_cons.mp hd with heq | hd · subst heq have hc1 : d < (seen.set d true hcs).size := hsz1.symm ▸ hcs exact hmono d hc1 (by simp) · exact hmem d hd (hsz1.symm ▸ hds) · intro x hx exact hwsub x (Array.mem_push.mpr (.inl hx)) · intro j hj hjf hjt by_cases hjc : j = c · subst hjc exact hwsub j (Array.mem_push.mpr (.inr rfl)) · have hj1 : j < (seen.set c true hcs).size := hsz1.symm ▸ hj have hj1f : (seen.set c true hcs)[j]'hj1 = false := by rw [Array.getElem_set] simp [Ne.symm hjc, hjf] exact hnew j hj1 hj1f hjt · intro x hx rcases hwmem x hx with hx | ⟨hx, hxs⟩ · rcases Array.mem_push.mp hx with hx | rfl · exact .inl hx · exact .inr ⟨List.mem_cons_self, hcs⟩ · exact .inr ⟨List.mem_cons_of_mem _ hx, hsz1 ▸ hxs⟩ · rw [hmeas, Array.size_push] omega · simp only [hcs, ↓reduceDIte] obtain ⟨hmono, hmem, hwsub, hnew, hwmem, hmeas⟩ := ih seen work refine ⟨hmono, fun d hd hds => ?_, hwsub, hnew, fun x hx => ?_, hmeas⟩ · rcases List.mem_cons.mp hd with heq | hd · subst heq; exact absurd hds hcs · exact hmem d hd hds · rcases hwmem x hx with hx | ⟨hx, hxs⟩ · exact .inl hx · exact .inr ⟨List.mem_cons_of_mem _ hx, hxs⟩ theorem pushUnseenNat_spec (seen : Array Bool) (work : Array Nat) (xs : Array Nat) : (∀ (j : Nat) (hj : j < seen.size), seen[j] = true → (pushUnseenNat seen work xs).1[j]'(by rw [size_pushUnseenNat]; exact hj) = true) ∧ (∀ (c : Nat), c ∈ xs → ∀ (hcs : c < seen.size), (pushUnseenNat seen work xs).1[c]'(by rw [size_pushUnseenNat]; exact hcs) = true) ∧ (∀ x ∈ work, x ∈ (pushUnseenNat seen work xs).2) ∧ (∀ (j : Nat) (hj : j < seen.size), seen[j] = false → (pushUnseenNat seen work xs).1[j]'(by rw [size_pushUnseenNat]; exact hj) = true → j ∈ (pushUnseenNat seen work xs).2) ∧ (∀ x ∈ (pushUnseenNat seen work xs).2, x ∈ work ∨ (x ∈ xs ∧ x < seen.size)) ∧ (pushUnseenNat seen work xs).2.size + countFalse (pushUnseenNat seen work xs).1 = work.size + countFalse seen := by have h := pushUnseenFold_props xs.toList seen work simpa [pushUnseenNat, ← Array.foldl_toList, Array.mem_toList_iff] using h theorem mem_work_pop_iff {work : Array Nat} (hpos : 0 < work.size) (x : Nat) : x ∈ work ↔ x ∈ work.pop ∨ x = work[work.size - 1] := by simp only [Array.mem_iff_getElem, Array.size_pop, Array.getElem_pop] constructor · rintro ⟨idx, hlt, rfl⟩ by_cases hlast : idx = work.size - 1 · subst hlast; exact .inr rfl · exact .inl ⟨idx, by omega, rfl⟩ · rintro (⟨idx, hlt, rfl⟩ | rfl) · exact ⟨idx, by omega, rfl⟩ · exact ⟨work.size - 1, by omega, rfl⟩ theorem size_reachGoNat (rev : Array (Array Nat)) (k : Nat) (seen : Array Bool) (work : Array Nat) : (reachGoNat rev k seen work).size = seen.size := by induction k generalizing seen work with | zero => rfl | succ k ih => unfold reachGoNat split · rfl · dsimp only split · rw [ih, size_pushUnseenNat] · exact ih seen work.pop theorem reachGoNat_mono (rev : Array (Array Nat)) (k : Nat) (seen : Array Bool) (work : Array Nat) {j : Nat} (hj : j < seen.size) (h : seen[j] = true) : (reachGoNat rev k seen work)[j]'(by rw [size_reachGoNat]; exact hj) = true := by induction k generalizing seen work with | zero => simpa [reachGoNat] | succ k ih => unfold reachGoNat split · simpa · dsimp only split · have hp := pushUnseenNat_spec seen work.pop rev[work[work.size - 1]] exact ih _ _ (by rw [size_pushUnseenNat]; exact hj) (hp.1 j hj h) · exact ih seen work.pop hj h theorem reachGoNat_sound (rev : Array (Array Nat)) (P : Nat → Prop) (hclosed : ∀ (b : Nat) (hb : b < rev.size), P b → ∀ c ∈ rev[b], c < rev.size → P c) (k : Nat) (seen : Array Bool) (work : Array Nat) (hsz : seen.size = rev.size) (hs : ∀ (j : Nat) (hj : j < seen.size), seen[j] = true → P j) (hw : ∀ u ∈ work, P u) {j : Nat} (hj : j < seen.size) (h : (reachGoNat rev k seen work)[j]'(by rw [size_reachGoNat]; exact hj) = true) : P j := by induction k generalizing seen work with | zero => exact hs j hj (by simpa [reachGoNat] using h) | succ k ih => unfold reachGoNat at h split at h · exact hs j hj h · rename_i hne dsimp only at h have hpos : 0 < work.size := Nat.pos_of_ne_zero hne have huMem : work[work.size - 1]'(by omega) ∈ work := Array.getElem_mem (by omega) have hpu : P (work[work.size - 1]'(by omega)) := hw _ huMem have hpop : ∀ x ∈ work.pop, P x := fun x hx => hw x ((mem_work_pop_iff hpos x).mpr (.inl hx)) split at h · rename_i hu have hp := pushUnseenNat_spec seen work.pop rev[work[work.size - 1]] have hsz' : (pushUnseenNat seen work.pop rev[work[work.size - 1]]).1.size = seen.size := size_pushUnseenNat seen work.pop rev[work[work.size - 1]] refine ih _ _ (hsz'.trans hsz) ?_ ?_ (hsz'.symm ▸ hj) h · intro x hx hxt by_cases hxs : seen[x]'(hsz' ▸ hx) = true · exact hs x (hsz' ▸ hx) hxs · have hxsf : seen[x]'(hsz' ▸ hx) = false := by cases hb : seen[x]'(hsz' ▸ hx) <;> simp_all have hxwork := hp.2.2.2.1 x (hsz' ▸ hx) hxsf hxt rcases hp.2.2.2.2.1 x hxwork with hxpop | ⟨hxrev, hxlt⟩ · exact hpop x hxpop · exact hclosed _ hu hpu x hxrev (hsz ▸ hxlt) · intro x hx rcases hp.2.2.2.2.1 x hx with hxpop | ⟨hxrev, hxlt⟩ · exact hpop x hxpop · exact hclosed _ hu hpu x hxrev (hsz ▸ hxlt) · exact ih seen work.pop hsz hs hpop hj h theorem reachGoNat_closed (rev : Array (Array Nat)) (k : Nat) (seen : Array Bool) (work : Array Nat) (hsz : seen.size = rev.size) (hk : work.size + countFalse seen ≤ k) (hc : ∀ (b : Nat) (hb : b < rev.size), seen[b]'(hsz.symm ▸ hb) = true → b ∉ work → ∀ c ∈ rev[b], ∀ hc' : c < rev.size, seen[c]'(hsz.symm ▸ hc') = true) : ∀ (b : Nat) (hb : b < rev.size), (reachGoNat rev k seen work)[b]'(by rw [size_reachGoNat, hsz]; exact hb) = true → ∀ c ∈ rev[b], ∀ hc' : c < rev.size, (reachGoNat rev k seen work)[c]'(by rw [size_reachGoNat, hsz]; exact hc') = true := by induction k generalizing seen work with | zero => have hw0 : work.size = 0 := by omega have hwnot : ∀ x, x ∉ work := fun x hx => by have hpos : 0 < work.size := Array.size_pos_of_mem hx omega intro b hb hbt c hcm hc' simpa [reachGoNat] using hc b hb (by simpa [reachGoNat] using hbt) (hwnot b) c hcm hc' | succ k ih => unfold reachGoNat split · rename_i hw0 have hwnot : ∀ x, x ∉ work := fun x hx => by have hpos : 0 < work.size := Array.size_pos_of_mem hx omega intro b hb hbt c hcm hc' exact hc b hb hbt (hwnot b) c hcm hc' · rename_i hne dsimp only have hpos : 0 < work.size := Nat.pos_of_ne_zero hne have hpopSz : work.pop.size = work.size - 1 := Array.size_pop split · rename_i hu let u := work[work.size - 1]'(by omega) have hp := pushUnseenNat_spec seen work.pop rev[u] have hsz1 : (pushUnseenNat seen work.pop rev[u]).1.size = seen.size := size_pushUnseenNat seen work.pop rev[u] have hsz' : (pushUnseenNat seen work.pop rev[u]).1.size = rev.size := hsz1.trans hsz have hk' : (pushUnseenNat seen work.pop rev[u]).2.size + countFalse (pushUnseenNat seen work.pop rev[u]).1 ≤ k := by have hm := hp.2.2.2.2.2 omega refine ih (pushUnseenNat seen work.pop rev[u]).1 (pushUnseenNat seen work.pop rev[u]).2 hsz' hk' ?_ intro b hb hbt hbnot c hcm hc' by_cases hbu : b = u · subst hbu exact hp.2.1 c hcm (hsz.symm ▸ hc') · have hbnotpop : b ∉ work.pop := fun h => hbnot (hp.2.2.1 b h) have hbnotwork : b ∉ work := fun h => ((mem_work_pop_iff hpos b).mp h).elim hbnotpop hbu have hbseen : seen[b]'(hsz.symm ▸ hb) = true := by cases hsb : seen[b]'(hsz.symm ▸ hb) with | true => rfl | false => exact absurd (hp.2.2.2.1 b (hsz.symm ▸ hb) hsb hbt) hbnot exact hp.1 c (hsz.symm ▸ hc') (hc b hb hbseen hbnotwork c hcm hc') · rename_i hu refine ih seen work.pop hsz (by omega) ?_ intro b hb hbt hbnotpop c hcm hc' have hbu : b ≠ work[work.size - 1]'(by omega) := fun h => hu (h ▸ hb) have hbnotwork : b ∉ work := fun h => ((mem_work_pop_iff hpos b).mp h).elim hbnotpop hbu exact hc b hb hbt hbnotwork c hcm hc' theorem size_reachNat (rev : Array (Array Nat)) (seed : Nat) : (reachNat rev seed).size = rev.size := by unfold reachNat dsimp only split · rw [size_reachGoNat, Array.size_set, Array.size_replicate] · rw [Array.size_replicate] theorem mem_reachNat (rev : Array (Array Nat)) {seed j : Nat} (hseed : seed < rev.size) (hj : j < rev.size) : (reachNat rev seed)[j]'(by rw [size_reachNat]; exact hj) = true ↔ ReachNat rev seed j := by unfold reachNat have hseed0 : seed < (Array.replicate rev.size false).size := by simpa [Array.size_replicate] using hseed simp only [hseed0, ↓reduceDIte] let seen0 := (Array.replicate rev.size false).set seed true have hsz0 : seen0.size = rev.size := by simp [seen0] change (reachGoNat rev rev.size seen0 #[seed])[j]'(by rw [size_reachGoNat, hsz0]; exact hj) = true ↔ ReachNat rev seed j constructor · intro h refine reachGoNat_sound rev (ReachNat rev seed) (fun b hb hr c hc hclt => .step hr hb hc hclt) rev.size seen0 #[seed] hsz0 ?_ ?_ (hsz0.symm ▸ hj) h · intro x hx hxt simp only [seen0, Array.getElem_set, Array.getElem_replicate] at hxt split at hxt · rename_i heq; subst heq; exact .refl · cases hxt · intro u hu have : u = seed := by simpa using hu subst this exact .refl · intro hr have hclosed := reachGoNat_closed rev rev.size seen0 #[seed] hsz0 (by have hcf : countFalse seen0 + 1 = rev.size := by have h1 := countFalse_set_true (seen := Array.replicate rev.size false) hseed0 (by simp) rw [countFalse_replicate] at h1 exact h1 have hws : (#[seed] : Array Nat).size = 1 := rfl omega) (fun b hb hbt hbnot => by simp only [seen0, Array.getElem_set, Array.getElem_replicate] at hbt split at hbt · rename_i heq subst heq exact absurd (by simp) hbnot · cases hbt) have hseedIn : (reachGoNat rev rev.size seen0 #[seed])[seed]'(by rw [size_reachGoNat, hsz0]; exact hseed) = true := reachGoNat_mono rev rev.size seen0 #[seed] (hsz0.symm ▸ hseed) (by simp [seen0]) induction hr with | refl => exact hseedIn | step _ hb hcm hc' ih => exact hclosed _ hb (ih hb) _ hcm hc' def boolArrToHashSetGo (decls : Array Decl) (seen : Array Bool) : Nat → Nat → Std.HashSet Name → Std.HashSet Name | 0, _, s => s | fuel + 1, idx, s => if h1 : idx < seen.size then if h2 : idx < decls.size then let s' := if seen[idx] then s.insert decls[idx].name else s boolArrToHashSetGo decls seen fuel (idx + 1) s' else s else s def boolArrToHashSet (decls : Array Decl) (seen : Array Bool) : Std.HashSet Name := let cnt := seen.foldl (init := 0) fun acc b => if b then acc + 1 else acc boolArrToHashSetGo decls seen (min decls.size seen.size) 0 (Std.HashSet.emptyWithCapacity cnt) theorem mem_boolArrToHashSetGo (decls : Array Decl) (seen : Array Bool) (fuel idx : Nat) (s : Std.HashSet Name) (c : Name) : (boolArrToHashSetGo decls seen fuel idx s).contains c = true ↔ (∃ (j : Nat) (h1 : j < seen.size) (h2 : j < decls.size), idx ≤ j ∧ j < idx + fuel ∧ seen[j] = true ∧ decls[j].name = c) ∨ s.contains c = true := by induction fuel generalizing idx s with | zero => simp only [boolArrToHashSetGo] constructor · exact .inr · rintro (⟨_, _, _, h1, h2, _, _⟩ | h) · omega · exact h | succ fuel ih => unfold boolArrToHashSetGo split · rename_i h1 split · rename_i h2 dsimp only rw [ih (idx + 1)] by_cases hcur : seen[idx] = true · simp only [hcur, ↓reduceIte, Std.HashSet.contains_insert, Bool.or_eq_true, beq_iff_eq] constructor · rintro (⟨j, hj1, hj2, hge, hlt, hst, hnm⟩ | (rfl | hs)) · exact .inl ⟨j, hj1, hj2, by omega, by omega, hst, hnm⟩ · exact .inl ⟨idx, h1, h2, Nat.le_refl _, by omega, hcur, rfl⟩ · exact .inr hs · rintro (⟨j, hj1, hj2, hge, hlt, hst, hnm⟩ | hs) · by_cases hji : j = idx · subst hji; exact .inr (.inl hnm) · exact .inl ⟨j, hj1, hj2, by omega, by omega, hst, hnm⟩ · exact .inr (.inr hs) · have hcf : seen[idx] = false := by cases hb : seen[idx] <;> simp_all simp only [hcf, Bool.false_eq_true, ↓reduceIte] constructor · rintro (⟨j, hj1, hj2, hge, hlt, hst, hnm⟩ | hs) · exact .inl ⟨j, hj1, hj2, by omega, by omega, hst, hnm⟩ · exact .inr hs · rintro (⟨j, hj1, hj2, hge, hlt, hst, hnm⟩ | hs) · have hji : j ≠ idx := fun h => hcur (h ▸ hst) exact .inl ⟨j, hj1, hj2, by omega, by omega, hst, hnm⟩ · exact .inr hs · rename_i h2 constructor · exact .inr · rintro (⟨_, _, hj2, hge, _, _, _⟩ | hs) · omega · exact hs · rename_i h1 constructor · exact .inr · rintro (⟨_, hj1, _, hge, _, _, _⟩ | hs) · omega · exact hs theorem mem_boolArrToHashSet (decls : Array Decl) (seen : Array Bool) (c : Name) : (boolArrToHashSet decls seen).contains c = true ↔ ∃ (j : Nat) (h1 : j < seen.size) (h2 : j < decls.size), seen[j] = true ∧ decls[j].name = c := by unfold boolArrToHashSet dsimp only rw [mem_boolArrToHashSetGo] simp only [Nat.zero_le, Nat.zero_add, true_and, Std.HashSet.contains_emptyWithCapacity, Bool.false_eq_true, or_false] constructor · rintro ⟨j, h1, h2, _, hst, hnm⟩; exact ⟨j, h1, h2, hst, hnm⟩ · rintro ⟨j, h1, h2, hst, hnm⟩; exact ⟨j, h1, h2, Nat.lt_min.mpr ⟨h2, h1⟩, hst, hnm⟩ def axiomIndicesGo (decls : Array Decl) : Nat → Nat → List (Name × Nat) → List (Name × Nat) | 0, _, acc => acc.reverse | fuel + 1, idx, acc => if h : idx < decls.size then let acc' := if decls[idx].kind == .axiom then (decls[idx].name, idx) :: acc else acc axiomIndicesGo decls fuel (idx + 1) acc' else acc.reverse def axiomIndices (decls : Array Decl) : List (Name × Nat) := axiomIndicesGo decls decls.size 0 [] theorem mem_axiomIndicesGo (decls : Array Decl) (fuel idx : Nat) (acc : List (Name × Nat)) (a : Name) (k : Nat) : (a, k) ∈ axiomIndicesGo decls fuel idx acc ↔ (∃ hk : k < decls.size, idx ≤ k ∧ k < idx + fuel ∧ decls[k].name = a ∧ decls[k].kind = .axiom) ∨ (a, k) ∈ acc := by induction fuel generalizing idx acc with | zero => simp only [axiomIndicesGo, List.mem_reverse] constructor · exact .inr · rintro (⟨_, h1, h2, _, _⟩ | h) · omega · exact h | succ fuel ih => unfold axiomIndicesGo split · rename_i hidx dsimp only rw [ih (idx + 1)] by_cases hax : decls[idx].kind = .axiom · have hb : (decls[idx].kind == .axiom) = true := by simp [hax] simp only [hb, ↓reduceIte, List.mem_cons, Prod.mk.injEq] constructor · rintro (⟨hk, hge, hlt, hnm, hkd⟩ | (⟨rfl, rfl⟩ | h)) · exact .inl ⟨hk, by omega, by omega, hnm, hkd⟩ · exact .inl ⟨hidx, Nat.le_refl _, by omega, rfl, hax⟩ · exact .inr h · rintro (⟨hk, hge, hlt, hnm, hkd⟩ | h) · by_cases hki : k = idx · subst hki; exact .inr (.inl ⟨hnm.symm, rfl⟩) · exact .inl ⟨hk, by omega, by omega, hnm, hkd⟩ · exact .inr (.inr h) · have hb : (decls[idx].kind == .axiom) = false := beq_false_of_ne hax simp only [hb, Bool.false_eq_true, ↓reduceIte] constructor · rintro (⟨hk, hge, hlt, hnm, hkd⟩ | h) · exact .inl ⟨hk, by omega, by omega, hnm, hkd⟩ · exact .inr h · rintro (⟨hk, hge, hlt, hnm, hkd⟩ | h) · have hki : k ≠ idx := fun h => hax (h ▸ hkd) exact .inl ⟨hk, by omega, by omega, hnm, hkd⟩ · exact .inr h · rename_i hidx simp only [List.mem_reverse] constructor · exact .inr · rintro (⟨hk, hge, _, _, _⟩ | h) · omega · exact h theorem mem_axiomIndices (decls : Array Decl) (a : Name) (k : Nat) : (a, k) ∈ axiomIndices decls ↔ ∃ hk : k < decls.size, decls[k].name = a ∧ decls[k].kind = .axiom := by rw [axiomIndices, mem_axiomIndicesGo] simp only [Nat.zero_le, Nat.zero_add, true_and, List.not_mem_nil, or_false] constructor · rintro ⟨hk, _, hnm, hkd⟩; exact ⟨hk, hnm, hkd⟩ · rintro ⟨hk, hnm, hkd⟩; exact ⟨hk, hk, hnm, hkd⟩ def axiomReachSeq : List (Name × Std.HashSet Name) := let decls := i.decls.toArray let idOf := declIdMap decls let fwd := decls.map (fun d => resolveUses idOf d.uses) let rev := reverseEdges fwd (axiomIndices decls).map fun (aName, aIdx) => (aName, boolArrToHashSet decls (reachNat rev aIdx)) def axiomReach : List (Name × Std.HashSet Name) := let decls := i.decls.toArray let idOf := declIdMap decls let fwd := forwardEdges decls idOf let rev := reverseEdges fwd let tasks := (axiomIndices decls).map fun (aName, aIdx) => Task.spawn (prio := .default) fun _ => (aName, boolArrToHashSet decls (reachNat rev aIdx)) tasks.map Task.get theorem axiomReach_eq_seq : i.axiomReach = i.axiomReachSeq := by unfold axiomReach axiomReachSeq forwardEdges dsimp only rw [map_task_spawn_get, mapArrayPar_eq_map] theorem mem_axiomReach (c a : Name) : (∃ s, (a, s) ∈ i.axiomReach ∧ s.contains c = true) ↔ i.AxiomOf c a := by rw [axiomReach_eq_seq] let decls := i.decls.toArray let idOf := declIdMap decls let fwd := decls.map (fun d => resolveUses idOf d.uses) let rev := reverseEdges fwd have hnodup : (decls.toList.map (·.name)).Nodup := by simpa [decls] using i.unique have hfwdSz : fwd.size = decls.size := Array.size_map have hrevSz : rev.size = decls.size := (size_reverseEdges fwd).trans hfwdSz have hrev_iff : ∀ {u j : Nat} (hu : u < rev.size) (hj : j < rev.size), j ∈ rev[u] ↔ (decls[u]'(hrevSz ▸ hu)).name ∈ (decls[j]'(hrevSz ▸ hj)).uses := by intro u j hu hj have huf : u < fwd.size := hfwdSz.symm ▸ hrevSz ▸ hu have hjf : j < fwd.size := hfwdSz.symm ▸ hrevSz ▸ hj have hjd : j < decls.size := hrevSz ▸ hj have hud : u < decls.size := hrevSz ▸ hu rw [mem_reverseEdges fwd huf j] constructor · rintro ⟨hc, hm⟩ have hm' : u ∈ resolveUses idOf (decls[j]'hjd).uses := by simpa [fwd, Array.getElem_map] using hm obtain ⟨uname, hun, hid⟩ := (mem_resolveUses idOf _ u).mp hm' obtain ⟨hu', hname⟩ := (mem_declIdMap decls hnodup uname u).mp hid exact hname ▸ hun · intro hun refine ⟨hjf, ?_⟩ simpa [fwd, Array.getElem_map] using (mem_resolveUses idOf (decls[j]'hjd).uses u).mpr ⟨(decls[u]'hud).name, hun, (mem_declIdMap decls hnodup _ u).mpr ⟨hud, rfl⟩⟩ have hreach_to : ∀ {aIdx cIdx : Nat} (ha : aIdx < rev.size) (hc : cIdx < rev.size), ReachNat rev aIdx cIdx → i.Reaches (decls[cIdx]'(hrevSz ▸ hc)).name (decls[aIdx]'(hrevSz ▸ ha)).name := by intro aIdx cIdx ha hc hr induction hr with | refl => exact .refl _ | step _ hb hcm hc' ih => have huse := (hrev_iff hb hc').mp hcm exact .step (decls[_]'(hrevSz ▸ hc')) (by simp [decls]) rfl huse (ih hb) have hreach_from : ∀ {x y : Name}, i.Reaches x y → ∀ {yIdx : Nat} (hy : yIdx < rev.size), (decls[yIdx]'(hrevSz ▸ hy)).name = y → ∃ (xIdx : Nat) (hx : xIdx < rev.size), (decls[xIdx]'(hrevSz ▸ hx)).name = x ∧ ReachNat rev yIdx xIdx := by intro x y hr induction hr with | refl y => intro yIdx hy hyn exact ⟨yIdx, hy, hyn, .refl⟩ | step d hd hdn hu hrest ih => intro yIdx hy hyn obtain ⟨e, he, hen⟩ := i.closed d hd _ hu obtain ⟨uIdx, huIdx, hde⟩ := (Array.mem_iff_getElem (xs := decls)).mp (by simpa [decls] using he) have huRev : uIdx < rev.size := hrevSz.symm ▸ huIdx have hru : ReachNat rev yIdx uIdx := by obtain ⟨uIdx', hu', hname', hr'⟩ := ih hy hyn have heq : uIdx' = uIdx := decls_name_inj hnodup (hrevSz ▸ hu') huIdx (by rw [hname', ← hen, hde]) exact heq ▸ hr' obtain ⟨dIdx, hdIdx, hdd⟩ := (Array.mem_iff_getElem (xs := decls)).mp (by simpa [decls] using hd) have hdRev : dIdx < rev.size := hrevSz.symm ▸ hdIdx have hmem : dIdx ∈ rev[uIdx]'huRev := (hrev_iff huRev hdRev).mpr (by rw [hdd, hde, hen]; exact hu) exact ⟨dIdx, hdRev, by rw [hdd, hdn], .step hru huRev hmem hdRev⟩ change (∃ s, (a, s) ∈ (axiomIndices decls).map (fun x => (x.1, boolArrToHashSet decls (reachNat rev x.2))) ∧ s.contains c = true) ↔ i.AxiomOf c a simp only [List.mem_map, Prod.mk.injEq] constructor · rintro ⟨s, ⟨⟨aName, aIdx⟩, hmemAx, haEq, rfl⟩, hc⟩ subst haEq obtain ⟨haIdx, haName, haKind⟩ := (mem_axiomIndices decls aName aIdx).mp hmemAx obtain ⟨cIdx, hcSeen, hcDecl, hct, hcName⟩ := (mem_boolArrToHashSet decls _ c).mp hc have haRev : aIdx < rev.size := hrevSz.symm ▸ haIdx have hcRev : cIdx < rev.size := hrevSz.symm ▸ hcDecl have hreach := (mem_reachNat rev haRev hcRev).mp hct refine ⟨?_, decls[aIdx]'haIdx, by simp [decls], haName, haKind⟩ have hr := hreach_to haRev hcRev hreach simpa [haName, hcName] using hr · rintro ⟨hr, d, hd, hda, hdk⟩ obtain ⟨aIdx, haIdx, hdd⟩ := (Array.mem_iff_getElem (xs := decls)).mp (by simpa [decls] using hd) have haRev : aIdx < rev.size := hrevSz.symm ▸ haIdx obtain ⟨cIdx, hcRev, hcName, hrNat⟩ := hreach_from hr haRev (by rw [hdd, hda]) refine ⟨boolArrToHashSet decls (reachNat rev aIdx), ⟨(a, aIdx), (mem_axiomIndices decls a aIdx).mpr ⟨haIdx, by rw [hdd, hda], by rw [hdd, hdk]⟩, rfl, rfl⟩, ?_⟩ have hcSeen : cIdx < (reachNat rev aIdx).size := by rw [size_reachNat]; exact hcRev exact (mem_boolArrToHashSet decls _ c).mpr ⟨cIdx, hcSeen, hrevSz ▸ hcRev, (mem_reachNat rev haRev hcRev).mpr hrNat, hcName⟩ def kernelNames : List Name := [`Nat, `String, `Nat.zero, `Nat.succ, `String.ofList, `List.cons, `List.nil, `Char, `Char.ofNat, `Bool.true, `Bool.false] def cone (seeds : List Name) : Std.HashSet Name := let index := i.index reach (fun n => ((index[n]?).map (·.uses)).getD []) (i.decls.map (·.uses.length)).sum (i.claimedDecls.map (·.name) ++ seeds) def tables : Tables i where index := i.index mem_index := i.index_eq_some claimed := i.claimedDecls mem_claimed := i.mem_claimedDecls axiomReach := i.axiomReach mem_axiomReach := i.mem_axiomReach end Input namespace Tables variable {i : Input} (t : Tables i) def axiomSet (c : Name) : List Name := t.axiomReach.filterMap fun p => if p.2.contains c then some p.1 else none theorem mem_axiomSet (c a : Name) : a ∈ t.axiomSet c ↔ i.AxiomOf c a := by rw [← t.mem_axiomReach c a] simp only [axiomSet, List.mem_filterMap] constructor · rintro ⟨⟨b, s⟩, hm, hif⟩ split at hif · rename_i hc simp only [Option.some.injEq] at hif subst hif exact ⟨s, hm, hc⟩ · cases hif · rintro ⟨s, hm, hc⟩ exact ⟨(a, s), hm, by simp [hc]⟩ end Tables structure Prepared where input : Input tables : Tables input end Regula.Closed