diff --git a/lean/PEqualNP/Outside/V_meta_126329__k10.lean b/lean/PEqualNP/Outside/V_meta_126329__k10.lean new file mode 100644 index 000000000000..663f8c6e3257 --- /dev/null +++ b/lean/PEqualNP/Outside/V_meta_126329__k10.lean @@ -0,0 +1,4958 @@ +import PEqualNP.Defs + +namespace PEqualNP.Outside.V_meta_126329__k10 +open PEqualNP.CircuitLowerBounds PEqualNP.MetaComplexity + +def full64 : Nat := 18446744073709551615 + +def varTabs : List Nat := + [12297829382473034410, 14757395258967641292, 17361641481138401520, + 18374966859414961920, 18446462603027742720, 18446744069414584320] + +def minComp (mask x : Nat) : Nat := + let nx := x ^^^ mask + if x < nx then x else nx + +def normZ (u : Bool) (Z w : Nat) : Nat := + let wz := w &&& Z + if u then minComp Z wz else wz + +def canon64 (w : Nat) : Nat := + minComp full64 (w &&& full64) + +def trivW (_u : Bool) (Z w : Nat) : Bool := + let wz := w &&& Z + wz == 0 || wz == Z + +def opsCount (u : Bool) : Nat := + if u then 4 else 2 + +def opU (u : Bool) (d a b : Nat) : Nat := + if d = 0 then a &&& b + else if d = 1 then a ||| b + else if !u then 0 + else if d = 2 then a &&& (b ^^^ full64) + else (a ^^^ full64) &&& b + +def flipOp (u : Bool) (d : Nat) : Nat := + if !u then d else if d = 2 then 3 else if d = 3 then 2 else d + +def stepVal (u : Bool) (Z d a b : Nat) : Nat := + normZ u Z (opU u d a b) + +def pairFail (u : Bool) (S : List Nat) (p q : Nat) : Bool := + if u then S.all (fun w => w.testBit p == w.testBit q) + else S.all (fun w => !(w.testBit p) || w.testBit q) + +def of6 (b0 b1 b2 b3 b4 b5 : Bool) : Fin 6 → Bool := + fun i => + match i.val with + | 0 => b0 + | 1 => b1 + | 2 => b2 + | 3 => b3 + | 4 => b4 + | _ => b5 + +set_option maxHeartbeats 0 in +theorem of6_ti (b0 b1 b2 b3 b4 b5 : Bool) : + PEqualNP.MetaComplexity.tableIndex (of6 b0 b1 b2 b3 b4 b5) < 64 ∧ + ∀ i : Fin 6, + (varTabs.getD i.val 0).testBit (PEqualNP.MetaComplexity.tableIndex (of6 b0 b1 b2 b3 b4 b5)) = + of6 b0 b1 b2 b3 b4 b5 i := by + revert b0 b1 b2 b3 b4 b5 + decide +kernel + +theorem varTabs_ti (x : Fin 6 → Bool) : + PEqualNP.MetaComplexity.tableIndex x < 64 ∧ + ∀ i : Fin 6, (varTabs.getD i.val 0).testBit (PEqualNP.MetaComplexity.tableIndex x) = x i := by + have hx : x = of6 (x 0) (x 1) (x 2) (x 3) (x 4) (x 5) := by + funext i + fin_cases i <;> rfl + rw [hx] + exact of6_ti (x 0) (x 1) (x 2) (x 3) (x 4) (x 5) + +def fOf (T : Nat) (x : Fin 6 → Bool) : Bool := + T.testBit (PEqualNP.MetaComplexity.tableIndex x) + +def row6 (p : Nat) (i : Fin 6) : Bool := + (varTabs.getD i.val 0).testBit p + +set_option maxHeartbeats 0 in +theorem row6_ti : ∀ p : Fin 64, PEqualNP.MetaComplexity.tableIndex (row6 p) = p.val := by + decide +kernel + +theorem full64_tb (p : Nat) (hp : p < 64) : full64.testBit p = true := by + have hf : ∀ q : Fin 64, full64.testBit q.val = true := by decide +kernel + exact hf ⟨p, hp⟩ + +theorem full64_eq : full64 = 2 ^ 64 - 1 := rfl + +theorem varTabs_lt : ∀ i, i ∈ varTabs → i < 2 ^ 64 := by decide +kernel + +theorem zero_lt64 : (0 : Nat) < 2 ^ 64 := by decide + +theorem testBit_and_full {a p : Nat} (hp : p < 64) : (a &&& full64).testBit p = a.testBit p := by + rw [Nat.testBit_and, full64_tb p hp, Bool.and_true] + +theorem testBit_xor_full {a p : Nat} (hp : p < 64) : (a ^^^ full64).testBit p = !(a.testBit p) := by + rw [Nat.testBit_xor, full64_tb p hp] + cases a.testBit p <;> rfl + +theorem and_full_lt (a : Nat) : (a &&& full64) < 2 ^ 64 := by + have hfl : full64 < 2 ^ 64 := by decide + have : a &&& full64 ≤ full64 := Nat.and_le_right + omega + +theorem lt64_of_xor {a : Nat} (ha : a < 2 ^ 64) : (a ^^^ full64) < 2 ^ 64 := by + have h1 : full64 < 2 ^ 64 := by decide + exact Nat.xor_lt_two_pow ha h1 + +theorem and_lt64 {a b : Nat} (hb : b < 2 ^ 64) : (a &&& b) < 2 ^ 64 := by + have := Nat.and_le_right (n := a) (m := b) + omega + +theorem or_lt64 {a b : Nat} (ha : a < 2 ^ 64) (hb : b < 2 ^ 64) : (a ||| b) < 2 ^ 64 := + Nat.or_lt_two_pow ha hb + +theorem getD_varTabs_lt (i : Nat) : varTabs.getD i 0 < 2 ^ 64 := by + have : ∀ j : Fin 7, varTabs.getD j.val 0 < 2 ^ 64 := by decide +kernel + rcases Nat.lt_or_ge i 7 with h | h + · exact this ⟨i, h⟩ + · have : varTabs.getD i 0 = 0 := by + simp [List.getD, List.getElem?_eq_none (by simp [varTabs]; omega : varTabs.length ≤ i)] + rw [this]; exact zero_lt64 + +def evalGateNat (g : Gate) (ws : List Nat) : Nat := + match g.op with + | GateOp.and => g.inputs.foldl (fun acc j => acc &&& ws.getD j 0) full64 + | GateOp.or => g.inputs.foldl (fun acc j => acc ||| ws.getD j 0) 0 + | GateOp.not => + match g.inputs with + | [] => full64 + | j :: _ => (ws.getD j 0) ^^^ full64 + | _ => 0 + +def evalWiresNat (gs : List Gate) (init : List Nat) : List Nat := + gs.foldl (fun ws g => ws ++ [evalGateNat g ws]) init + +def toGate (t : Nat × Nat × Nat) : Gate := + if t.1 = 0 then ⟨GateOp.and, [t.2.1, t.2.2]⟩ + else if t.1 = 1 then ⟨GateOp.or, [t.2.1, t.2.2]⟩ + else if t.1 = 2 then ⟨GateOp.not, [t.2.1]⟩ + else if t.1 = 3 then ⟨GateOp.and, []⟩ + else ⟨GateOp.or, []⟩ + +def mkCirc (gs : List (Nat × Nat × Nat)) (out : Nat) : Circuit := + ⟨gs.map toGate, out⟩ + +def checkCirc (gs : List (Nat × Nat × Nat)) (out tab : Nat) : Bool := + (evalWiresNat (gs.map toGate) varTabs).getD out 0 == tab + +theorem toGate_fanInTwo (t : Nat × Nat × Nat) : + (toGate t).op ∈ ({GateOp.and, GateOp.or, GateOp.not} : Set GateOp) ∧ (toGate t).inputs.length ≤ 2 := by + unfold toGate + split_ifs <;> simp + +theorem mkCirc_fanInTwo (gs : List (Nat × Nat × Nat)) (out : Nat) : + FanInTwo (mkCirc gs out) := by + intro g hg + simp only [mkCirc, List.mem_map] at hg + rcases hg with ⟨t, _, rfl⟩ + exact toGate_fanInTwo t + +theorem evalGateNat_lt (g : Gate) (hg : g.op ∈ ({GateOp.and, GateOp.or, GateOp.not} : Set GateOp)) + (ws : List Nat) (hws : ∀ w ∈ ws, w < 2 ^ 64) : evalGateNat g ws < 2 ^ 64 := by + have get_lt : ∀ j, ws.getD j 0 < 2 ^ 64 := by + intro j + cases hw : ws[j]? with + | none => simp only [List.getD, hw, Option.getD_none]; exact zero_lt64 + | some w => + have hm := hws w (List.mem_of_getElem? hw) + simp only [List.getD, hw, Option.getD_some]; exact hm + rcases hg with h | h | h + · unfold evalGateNat; rw [h] + suffices ∀ (xs : List Nat) (acc : Nat), acc < 2 ^ 64 → + (xs.foldl (fun a j => a &&& ws.getD j 0) acc) < 2 ^ 64 by + exact this g.inputs full64 (by decide) + intro xs + induction xs with + | nil => intro acc ha; exact ha + | cons j js ih => + intro acc _; exact ih (acc &&& ws.getD j 0) (and_lt64 (get_lt j)) + · unfold evalGateNat; rw [h] + suffices ∀ (xs : List Nat) (acc : Nat), acc < 2 ^ 64 → + (xs.foldl (fun a j => a ||| ws.getD j 0) acc) < 2 ^ 64 by + exact this g.inputs 0 zero_lt64 + intro xs + induction xs with + | nil => intro acc ha; exact ha + | cons j js ih => + intro acc ha; exact ih (acc ||| ws.getD j 0) (or_lt64 ha (get_lt j)) + · simp only [Set.mem_singleton_iff] at h + unfold evalGateNat; rw [h] + cases g.inputs with + | nil => exact (by decide : full64 < 2 ^ 64) + | cons j _ => exact lt64_of_xor (get_lt j) + +theorem foldl_and_eq_all (l : List Bool) (b : Bool) : + l.foldl (· && ·) b = (b && l.all id) := by + induction l generalizing b with + | nil => simp + | cons x xs ih => simp only [List.foldl_cons, List.all_cons, id_eq, ih, Bool.and_assoc] + +theorem foldl_or_eq_any (l : List Bool) (b : Bool) : + l.foldl (· || ·) b = (b || l.any id) := by + induction l generalizing b with + | nil => simp + | cons x xs ih => simp only [List.foldl_cons, List.any_cons, id_eq, ih, Bool.or_assoc] + +theorem evalGateNat_bit (g : Gate) (hg : g.op ∈ ({GateOp.and, GateOp.or, GateOp.not} : Set GateOp)) + (ws : List Nat) (p : Nat) (hp : p < 64) : + (evalGateNat g ws).testBit p = g.op.eval (g.inputs.map fun j => (ws.getD j 0).testBit p) := by + rcases hg with h | h | h + · unfold evalGateNat GateOp.eval; rw [h] + suffices ∀ (xs : List Nat) (acc : Nat) (b : Bool), acc.testBit p = b → + (xs.foldl (fun a j => a &&& ws.getD j 0) acc).testBit p = + (xs.map (fun j => (ws.getD j 0).testBit p)).foldl (· && ·) b by + have hfin := this g.inputs full64 true (full64_tb p hp) + rw [hfin, foldl_and_eq_all, Bool.true_and] + intro xs + induction xs with + | nil => intro acc b hb; exact hb + | cons j js ih => + intro acc b hb + simp only [List.foldl_cons, List.map_cons] + apply ih + rw [Nat.testBit_and, hb] + · unfold evalGateNat GateOp.eval; rw [h] + suffices ∀ (xs : List Nat) (acc : Nat) (b : Bool), acc.testBit p = b → + (xs.foldl (fun a j => a ||| ws.getD j 0) acc).testBit p = + (xs.map (fun j => (ws.getD j 0).testBit p)).foldl (· || ·) b by + have hfin := this g.inputs 0 false (Nat.zero_testBit p) + rw [hfin, foldl_or_eq_any, Bool.false_or] + intro xs + induction xs with + | nil => intro acc b hb; exact hb + | cons j js ih => + intro acc b hb + simp only [List.foldl_cons, List.map_cons] + apply ih + rw [Nat.testBit_or, hb] + · simp only [Set.mem_singleton_iff] at h + unfold evalGateNat GateOp.eval; rw [h] + cases g.inputs with + | nil => simp [full64_tb p hp] + | cons j _ => simp [testBit_xor_full hp] + +theorem evalWiresNat_bit (gs : List Gate) + (hgs : ∀ g ∈ gs, g.op ∈ ({GateOp.and, GateOp.or, GateOp.not} : Set GateOp)) + (init : List Nat) (p : Nat) (hp : p < 64) : + (evalWiresNat gs init).map (fun w => w.testBit p) = + Circuit.wires ⟨gs, 0⟩ (init.map (fun w => w.testBit p)) := by + unfold evalWiresNat Circuit.wires + dsimp only + suffices ∀ (initW : List Nat), + (gs.foldl (fun ws g => ws ++ [evalGateNat g ws]) initW).map (fun w => w.testBit p) = + gs.foldl (fun w g => w ++ [g.op.eval (g.inputs.map fun i => w.getD i false)]) + (initW.map (fun w => w.testBit p)) from this init + induction gs with + | nil => intro initW; rfl + | cons g rest ih => + intro initW + have hg : g.op ∈ ({GateOp.and, GateOp.or, GateOp.not} : Set GateOp) := hgs g (by simp) + have hrest : ∀ g' ∈ rest, g'.op ∈ ({GateOp.and, GateOp.or, GateOp.not} : Set GateOp) := + fun g' hg' => hgs g' (by simp [hg']) + simp only [List.foldl_cons] + have hstep : (initW ++ [evalGateNat g initW]).map (fun w => w.testBit p) = + initW.map (fun w => w.testBit p) ++ + [g.op.eval (g.inputs.map fun i => (initW.map (fun w => w.testBit p)).getD i false)] := by + simp only [List.map_append, List.map_singleton, evalGateNat_bit g hg initW p hp] + congr 1; congr 1; congr 1 + apply List.map_congr_left + intro i _ + cases hnone : initW[i]? with + | none => + have hlen : initW.length ≤ i := List.getElem?_eq_none_iff.mp hnone + have hlen2 : (initW.map (fun w => w.testBit p)).length ≤ i := by simpa using hlen + simp [List.getD, hnone, Nat.zero_testBit, List.getElem?_eq_none_iff.mpr hlen2] + | some w => simp [List.getD, hnone] + rw [ih hrest (initW ++ [evalGateNat g initW]), hstep] + +theorem evalWiresNat_getD_bit (C : Circuit) (hC : FanInTwo C) (init : List Nat) (i p : Nat) (hp : p < 64) : + ((evalWiresNat C.gates init).getD i 0).testBit p = + (C.wires (init.map (fun w => w.testBit p))).getD i false := by + have hmap := evalWiresNat_bit C.gates (fun g hg => (hC g hg).1) init p hp + have heq := congrArg (fun l => l.getD i false) hmap + unfold Circuit.wires at heq ⊢ + rw [← heq] + cases hget : (evalWiresNat C.gates init)[i]? with + | none => + have hlen : (evalWiresNat C.gates init).length ≤ i := List.getElem?_eq_none_iff.mp hget + simp [List.getD, hget, List.getElem?_eq_none_iff.mpr hlen, Nat.zero_testBit] + | some w => simp [List.getD, hget] + +theorem upper_of_check {gs : List (Nat × Nat × Nat)} {out tab : Nat} + (hchk : checkCirc gs out tab = true) : + HasCircuitSize 6 (fOf tab) gs.length := by + refine ⟨mkCirc gs out, mkCirc_fanInTwo gs out, by simp [Circuit.size, mkCirc], ?_⟩ + intro x + have hti := varTabs_ti x + have hbit := evalWiresNat_getD_bit (mkCirc gs out) (mkCirc_fanInTwo gs out) varTabs out + (PEqualNP.MetaComplexity.tableIndex x) hti.1 + have htabs : varTabs.map (fun w => w.testBit (PEqualNP.MetaComplexity.tableIndex x)) = List.ofFn x := by + apply List.ext_getElem + · simp [varTabs] + · intro i hi1 hi2 + have hi : i < 6 := by simp [varTabs] at hi1; exact hi1 + have hi1' : i < varTabs.length := by simp at hi1; exact hi1 + simp only [List.getElem_map, List.getElem_ofFn] + simpa [List.getD, List.getElem?_eq_getElem hi1'] using hti.2 ⟨i, hi⟩ + have heq : (evalWiresNat (gs.map toGate) varTabs).getD out 0 = tab := Nat.eq_of_beq_eq_true hchk + simp only [Circuit.eval, mkCirc] at hbit ⊢ + rw [← htabs, ← hbit, heq] + rfl + +def isNotG (g : Gate) : Bool := + match g.op with + | GateOp.not => true + | _ => false + +theorem mono_ops (gs : List Gate) (hgs : FanInTwo ⟨gs, 0⟩) (hno : ∀ g ∈ gs, isNotG g = false) : + ∀ g ∈ gs, g.op = GateOp.and ∨ g.op = GateOp.or := by + intro g hg + rcases (hgs g hg).1 with h | h | h + · exact Or.inl h + · exact Or.inr h + · simp only [Set.mem_singleton_iff] at h + have hn := hno g hg + simp [isNotG, h] at hn + +theorem gate_mono (g : Gate) (hg : g.op = GateOp.and ∨ g.op = GateOp.or) (a b : List Bool) + (_hlen : a.length = b.length) (hle : ∀ i, a.getD i false = true → b.getD i false = true) : + g.op.eval (g.inputs.map fun i => a.getD i false) = true → + g.op.eval (g.inputs.map fun i => b.getD i false) = true := by + rcases hg with h | h <;> rw [h] + · simp only [GateOp.eval, List.all_eq_true, List.mem_map, id_eq, forall_exists_index, and_imp] + intro ha _ j hj rfl + exact hle j (ha _ j hj rfl) + · simp only [GateOp.eval, List.any_eq_true, List.mem_map, id_eq] + rintro ⟨_, ⟨j, hj, rfl⟩, hb⟩ + exact ⟨b.getD j false, ⟨j, hj, rfl⟩, hle j hb⟩ + +theorem getD_append_last (xs : List Bool) (v : Bool) : (xs ++ [v]).getD xs.length false = v := by + simp [List.getD] + +theorem getD_append_gt (xs : List Bool) (v : Bool) {j : Nat} (hj : xs.length < j) : + (xs ++ [v]).getD j false = false := by + have hj2 : (xs ++ [v]).length ≤ j := by simp; omega + simp [List.getD, List.getElem?_eq_none hj2] + +theorem wires_mono (gs : List Gate) (hgs : ∀ g ∈ gs, g.op = GateOp.and ∨ g.op = GateOp.or) + (a b : List Bool) (hlen : a.length = b.length) + (hle : ∀ i, a.getD i false = true → b.getD i false = true) (k : Nat) : + ((⟨gs, k⟩ : Circuit).wires a).getD k false = true → + ((⟨gs, k⟩ : Circuit).wires b).getD k false = true := by + suffices ∀ (xs ys : List Bool), xs.length = ys.length → + (∀ i, xs.getD i false = true → ys.getD i false = true) → + ∀ i, (gs.foldl (fun w g => w ++ [g.op.eval (g.inputs.map fun j => w.getD j false)]) xs).getD i false = true → + (gs.foldl (fun w g => w ++ [g.op.eval (g.inputs.map fun j => w.getD j false)]) ys).getD i false = true from + this a b hlen hle k + induction gs with + | nil => intro xs ys _ hxy i hi; exact hxy i hi + | cons g rest ih => + intro xs ys hxy hlxy i hi + have hg : g.op = GateOp.and ∨ g.op = GateOp.or := hgs g (by simp) + have hrest : ∀ g' ∈ rest, g'.op = GateOp.and ∨ g'.op = GateOp.or := fun g' hg' => hgs g' (by simp [hg']) + simp only [List.foldl_cons] at hi ⊢ + apply ih hrest (xs ++ [g.op.eval (g.inputs.map fun j => xs.getD j false)]) + (ys ++ [g.op.eval (g.inputs.map fun j => ys.getD j false)]) + · simp [hxy] + · intro j hj + rcases Nat.lt_or_ge j xs.length with hlt | hge + · have hy : j < ys.length := hxy ▸ hlt + simp only [List.getD, List.getElem?_append_left hlt, List.getElem?_append_left hy] at hj ⊢ + exact hlxy j (by simpa [List.getD, List.getElem?_eq_getElem hlt] using hj) + · rcases Nat.eq_or_lt_of_le hge with rfl | hgt + · rw [getD_append_last] at hj + rw [hxy, getD_append_last] + exact gate_mono g hg xs ys hxy hlxy hj + · rw [getD_append_gt _ _ hgt] at hj + exact Bool.noConfusion hj + · exact hi + +theorem le_inputs {p q : Nat} (hq : q < 64) (hle : p &&& q = p) (i : Nat) : + (List.ofFn (row6 p)).getD i false = true → (List.ofFn (row6 q)).getD i false = true := by + intro hi + rcases Nat.lt_or_ge i 6 with h | h + · simp only [List.getD, List.getElem?_ofFn, h, ↓reduceDIte, Option.getD_some, row6] at hi ⊢ + have hp : (varTabs.getD i 0).testBit p = true := hi + have : ∀ j : Fin 6, ∀ a b : Fin 64, a.val &&& b.val = a.val → + (varTabs.getD j.val 0).testBit a.val = true → (varTabs.getD j.val 0).testBit b.val = true := by + decide +kernel + rcases Nat.lt_or_ge p 64 with hp64 | hp64 + · exact this ⟨i, h⟩ ⟨p, hp64⟩ ⟨q, hq⟩ hle hp + · exfalso + have hr : (varTabs.getD i 0).testBit p = false := + Nat.testBit_lt_two_pow (lt_of_lt_of_le (getD_varTabs_lt i) (Nat.pow_le_pow_right (by decide) hp64)) + rw [hr] at hp; exact Bool.noConfusion hp + · simp only [List.getD, List.getElem?_ofFn, not_lt.mpr h, ↓reduceDIte, Option.getD_none] at hi + exact Bool.noConfusion hi + +theorem markov1_of_pair {C : Circuit} {tab p q : Nat} (legal : FanInTwo C) + (computes : ∀ x : Fin 6 → Bool, C.eval (List.ofFn x) = fOf tab x) + (hp : p < 64) (hq : q < 64) (hle : p &&& q = p) + (htp : tab.testBit p = true) (htq : tab.testBit q = false) : + 1 ≤ C.gates.countP isNotG := by + by_contra h0 + have hcnt : C.gates.countP isNotG = 0 := by omega + have hno : ∀ g ∈ C.gates, isNotG g = false := by + intro g hg + by_contra hc + have : isNotG g = true := by cases h : isNotG g <;> simp_all + have hpos : 0 < C.gates.countP isNotG := List.countP_pos_iff.mpr ⟨g, hg, this⟩ + omega + have hp_eval : C.eval (List.ofFn (row6 p)) = true := by + rw [computes (row6 p)]; unfold fOf; rw [row6_ti ⟨p, hp⟩]; exact htp + have hq_eval : C.eval (List.ofFn (row6 q)) = false := by + rw [computes (row6 q)]; unfold fOf; rw [row6_ti ⟨q, hq⟩]; exact htq + have hlen : (List.ofFn (row6 p)).length = (List.ofFn (row6 q)).length := by simp + have hm := wires_mono C.gates (mono_ops C.gates legal hno) (List.ofFn (row6 p)) (List.ofFn (row6 q)) + hlen (le_inputs hq hle) C.output hp_eval + simp only [Circuit.eval] at hq_eval + rw [hm] at hq_eval + exact Bool.noConfusion hq_eval + +theorem foldl_split_not (gs : List Gate) (h : gs.countP isNotG = 1) : + ∃ pre g post, gs = pre ++ g :: post ∧ isNotG g = true ∧ + (∀ q ∈ pre, isNotG q = false) ∧ (∀ q ∈ post, isNotG q = false) := by + induction gs with + | nil => simp at h + | cons x xs ih => + cases hx : isNotG x + · rw [List.countP_cons, hx] at h; simp only [Bool.false_eq_true, ite_false, Nat.add_zero] at h + obtain ⟨pre, g, post, heq, hgn, hp, hq⟩ := ih h + refine ⟨x :: pre, g, post, by rw [heq]; rfl, hgn, ?_, hq⟩ + intro y hy; simp only [List.mem_cons] at hy + rcases hy with rfl | hy + · exact hx + · exact hp y hy + · rw [List.countP_cons, hx] at h; simp only [ite_true] at h + have hz : xs.countP isNotG = 0 := by omega + refine ⟨[], x, xs, rfl, hx, by simp, ?_⟩ + intro y hy + by_contra hc + have hy1 : isNotG y = true := by cases hn : isNotG y <;> simp_all + have hpos : 0 < xs.countP isNotG := List.countP_pos_iff.mpr ⟨y, hy, hy1⟩ + omega + +theorem wires_len (gs : List Gate) (a : List Bool) : + (gs.foldl (fun w g => w ++ [g.op.eval (g.inputs.map fun i => w.getD i false)]) a).length = + a.length + gs.length := by + induction gs generalizing a with + | nil => simp + | cons g rest ih => simp only [List.foldl_cons, ih, List.length_append, List.length_singleton, List.length_cons, List.length_nil]; omega + +theorem le_step_eq {wa wb : List Bool} {va vb : Bool} (hlen : wa.length = wb.length) + (hle : ∀ i, wa.getD i false = true → wb.getD i false = true) (heqv : va = vb) : + ∀ i, (wa ++ [va]).getD i false = true → (wb ++ [vb]).getD i false = true := by + intro i hi + rcases Nat.lt_or_ge i wa.length with hlt | hge + · have hy : i < wb.length := hlen ▸ hlt + simp only [List.getD, List.getElem?_append_left hlt, List.getElem?_append_left hy] at hi ⊢ + exact hle i (by simpa [List.getD, List.getElem?_eq_getElem hlt] using hi) + · rcases Nat.eq_or_lt_of_le hge with rfl | hgt + · rw [getD_append_last] at hi + rw [hlen, getD_append_last] + exact heqv ▸ hi + · rw [getD_append_gt _ _ hgt] at hi + exact Bool.noConfusion hi + +theorem not_eval_anti (g : Gate) (hg : isNotG g = true) {wa wb : List Bool} + (hle : ∀ i, wa.getD i false = true → wb.getD i false = true) + (h1 : g.op.eval (g.inputs.map fun i => wb.getD i false) = true) : + g.op.eval (g.inputs.map fun i => wa.getD i false) = true := by + cases hgop : g.op <;> simp [isNotG, hgop] at hg + cases hgi : g.inputs with + | nil => simp [GateOp.eval, hgop, hgi] + | cons j _ => + simp only [GateOp.eval, hgop, hgi, List.map_cons, List.headD_cons] at h1 ⊢ + cases hwa : wa.getD j false + · rfl + · have := hle j hwa + rw [this] at h1 + exact Bool.noConfusion h1 + +theorem markov2_of_chain {C : Circuit} {tab p0 p1 p2 p3 : Nat} (legal : FanInTwo C) + (computes : ∀ x : Fin 6 → Bool, C.eval (List.ofFn x) = fOf tab x) + (hp0 : p0 < 64) (hp1 : p1 < 64) (hp2 : p2 < 64) (hp3 : p3 < 64) + (h01 : p0 &&& p1 = p0) (h12 : p1 &&& p2 = p1) (h23 : p2 &&& p3 = p2) + (ht0 : tab.testBit p0 = true) (ht1 : tab.testBit p1 = false) + (ht2 : tab.testBit p2 = true) (ht3 : tab.testBit p3 = false) : + 2 ≤ C.gates.countP isNotG := by + by_contra hlt2 + rcases Nat.eq_or_lt_of_le (by omega : C.gates.countP isNotG ≤ 1) with h1 | h0 + · obtain ⟨pre, g, post, heq, hgn, hpre, hpost⟩ := foldl_split_not C.gates h1 + have hp_legal : FanInTwo ⟨pre, 0⟩ := fun q hq => legal q (by rw [heq]; simp [hq]) + have hq_legal : FanInTwo ⟨post, 0⟩ := fun q hq => legal q (by rw [heq]; simp [hq]) + let x := fun p => List.ofFn (row6 p) + let preW := fun p => (⟨pre, 0⟩ : Circuit).wires (x p) + let gval := fun p => g.op.eval (g.inputs.map fun i => (preW p).getD i false) + have wsplit : ∀ p, C.wires (x p) = + (⟨post, C.output⟩ : Circuit).wires (preW p ++ [gval p]) := by + intro p + simp only [Circuit.wires, heq, List.foldl_append, List.foldl_cons] + rfl + have eval_bit : ∀ (p : Nat), p < 64 → (C.wires (x p)).getD C.output false = tab.testBit p := by + intro p hp + have := computes (row6 p) + unfold Circuit.eval fOf at this + rw [row6_ti ⟨p, hp⟩] at this + exact this + have l01 : (preW p0).length = (preW p1).length := by dsimp only [preW, Circuit.wires]; rw [wires_len, wires_len]; simp [x] + have l12 : (preW p1).length = (preW p2).length := by dsimp only [preW, Circuit.wires]; rw [wires_len, wires_len]; simp [x] + have l23 : (preW p2).length = (preW p3).length := by dsimp only [preW, Circuit.wires]; rw [wires_len, wires_len]; simp [x] + have mpre := mono_ops pre hp_legal hpre + have mpost := mono_ops post hq_legal hpost + have hpre01 : ∀ i, (preW p0).getD i false = true → (preW p1).getD i false = true := + fun i hi => wires_mono pre mpre (x p0) (x p1) (by simp [x]) (le_inputs hp1 h01) i hi + have hpre12 : ∀ i, (preW p1).getD i false = true → (preW p2).getD i false = true := + fun i hi => wires_mono pre mpre (x p1) (x p2) (by simp [x]) (le_inputs hp2 h12) i hi + have hpre23 : ∀ i, (preW p2).getD i false = true → (preW p3).getD i false = true := + fun i hi => wires_mono pre mpre (x p2) (x p3) (by simp [x]) (le_inputs hp3 h23) i hi + cases hv1 : gval p1 + · have hv2 : gval p2 = false := by + cases h2 : gval p2 + · rfl + · have := not_eval_anti g hgn hpre12 h2 + change gval p1 = true at this + rw [hv1] at this; exact Bool.noConfusion this + have hv3 : gval p3 = false := by + cases h3 : gval p3 + · rfl + · have := not_eval_anti g hgn hpre23 h3 + change gval p2 = true at this + rw [hv2] at this; exact Bool.noConfusion this + have hstep23 := le_step_eq l23 hpre23 (hv2.trans hv3.symm) + have hw : (C.wires (x p3)).getD C.output false = true := by + rw [wsplit p3] + apply wires_mono post mpost (preW p2 ++ [gval p2]) (preW p3 ++ [gval p3]) + (by simp [l23]) hstep23 C.output + rw [← wsplit p2]; exact (eval_bit p2 hp2).trans ht2 + have hq0 : C.eval (x p3) = false := (eval_bit p3 hp3).trans ht3 + simp only [Circuit.eval] at hq0; rw [hw] at hq0; exact Bool.noConfusion hq0 + · have hv0 : gval p0 = true := not_eval_anti g hgn hpre01 hv1 + have hstep01 := le_step_eq l01 hpre01 (hv0.trans hv1.symm) + have hw : (C.wires (x p1)).getD C.output false = true := by + rw [wsplit p1] + apply wires_mono post mpost (preW p0 ++ [gval p0]) (preW p1 ++ [gval p1]) + (by simp [l01]) hstep01 C.output + rw [← wsplit p0]; exact (eval_bit p0 hp0).trans ht0 + have hq0 : C.eval (x p1) = false := (eval_bit p1 hp1).trans ht1 + simp only [Circuit.eval] at hq0; rw [hw] at hq0; exact Bool.noConfusion hq0 + · have h1 := markov1_of_pair legal computes hp0 hp1 h01 ht0 ht1 + omega + +theorem countP_split (gs : List Gate) : + gs.countP (fun q => !isNotG q) + gs.countP isNotG = gs.length := by + induction gs with + | nil => simp + | cons g rest ih => + cases hn : isNotG g <;> simp [List.countP_cons, hn, List.length_cons] <;> omega + +def ValidProg (u : Bool) (Z : Nat) : List Nat → List (Nat × Nat × Nat) → Nat → Prop + | S, [], T => T ∈ S + | S, (a, b, d) :: rest, T => + a ∈ S ∧ b ∈ S ∧ d < opsCount u ∧ ValidProg u Z (stepVal u Z d a b :: S) rest T + +def varsOf (u : Bool) (Z : Nat) : List Nat := + (varTabs.map (normZ u Z)).filter (fun w => !trivW u Z w) + +def LowU (u : Bool) (Z tab L : Nat) : Prop := + ∀ (P : List (Nat × Nat × Nat)), + ValidProg u Z (varsOf u Z) P (normZ u Z tab) → L ≤ P.length + +theorem minComp_eq_or {m x : Nat} (hx : x &&& m = x) : + minComp m x = x ∨ minComp m x = x ^^^ m := by + unfold minComp + dsimp only + split_ifs + · exact Or.inl rfl + · exact Or.inr rfl + +theorem minComp_xor_self {m x : Nat} (hm : m ≤ full64) (hx : x &&& m = x) : + minComp m (x ^^^ m) = minComp m x := by + have hlt : m < 2 ^ 64 := by rw [full64_eq] at hm; omega + have hxlt : x < 2 ^ 64 := by + have hxm : x ≤ m := hx ▸ Nat.and_le_right + omega + have hxx : (x ^^^ m) ^^^ m = x := by + apply Nat.eq_of_testBit_eq; intro p + simp only [Nat.testBit_xor] + cases x.testBit p <;> cases m.testBit p <;> rfl + unfold minComp + dsimp only + rw [hxx] + split_ifs with h1 h2 h2 + · omega + · rfl + · rfl + · have he : x = x ^^^ m := by omega + rw [← he] + +theorem normZ_xor_full {u : Bool} {Z w : Nat} (hZ : Z ≤ full64) (hu : u = true) : + normZ u Z (w ^^^ full64) = normZ u Z w := by + subst hu + unfold normZ + dsimp only [ite_true] + have heq : (w ^^^ full64) &&& Z = (w &&& Z) ^^^ Z := by + apply Nat.eq_of_testBit_eq; intro p + rcases Nat.lt_or_ge p 64 with hp | hp + · simp only [Nat.testBit_and, Nat.testBit_xor, testBit_xor_full hp] + cases w.testBit p <;> cases Z.testBit p <;> rfl + · have hZ64 : Z < 2 ^ 64 := by rw [full64_eq] at hZ; omega + have hZp : Z.testBit p = false := + Nat.testBit_lt_two_pow (lt_of_lt_of_le hZ64 (Nat.pow_le_pow_right (by decide) hp)) + simp [Nat.testBit_and, Nat.testBit_xor, hZp] + rw [heq] + exact minComp_xor_self hZ (by apply Nat.eq_of_testBit_eq; intro p; simp only [Nat.testBit_and]; cases w.testBit p <;> cases Z.testBit p <;> rfl) + +theorem normZ_and_full {u : Bool} {Z w : Nat} (hZ : Z ≤ full64) : + normZ u Z (w &&& full64) = normZ u Z w := by + have heq : (w &&& full64) &&& Z = w &&& Z := by + apply Nat.eq_of_testBit_eq; intro p + rcases Nat.lt_or_ge p 64 with hp | hp + · simp only [Nat.testBit_and, testBit_and_full hp] + · have hZ64 : Z < 2 ^ 64 := by rw [full64_eq] at hZ; omega + have hZp : Z.testBit p = false := + Nat.testBit_lt_two_pow (lt_of_lt_of_le hZ64 (Nat.pow_le_pow_right (by decide) hp)) + simp [Nat.testBit_and, hZp] + unfold normZ; rw [heq] + +def eqZ (u : Bool) (Z a b : Nat) : Bool := + let az := a &&& Z + let bz := b &&& Z + az == bz || (u && az == (bz ^^^ Z)) + +theorem and_idem_r (a Z : Nat) : (a &&& Z) &&& Z = a &&& Z := by + apply Nat.eq_of_testBit_eq; intro p + simp only [Nat.testBit_and]; cases a.testBit p <;> cases Z.testBit p <;> rfl + +theorem and_sub_mask {Z Z1 a : Nat} (hsub : Z &&& Z1 = Z) : (a &&& Z1) &&& Z = a &&& Z := by + apply Nat.eq_of_testBit_eq; intro p + have h1 := congrArg (fun n => n.testBit p) hsub + simp only [Nat.testBit_and] at h1 ⊢ + cases hz : Z.testBit p <;> cases hz1 : Z1.testBit p <;> cases ha : a.testBit p <;> simp_all + +theorem trivW_iff (u : Bool) (Z w : Nat) : + trivW u Z w = true ↔ (w &&& Z = 0) ∨ (w &&& Z = Z) := by + unfold trivW + simp only [Bool.or_eq_true, beq_iff_eq] + +theorem normZ_triv_true {Z w : Nat} (ht : trivW true Z w = true) : + normZ true Z w = 0 := by + rw [trivW_iff] at ht + unfold normZ; simp only [ite_true] + rcases ht with h0 | h1 + · rw [h0]; simp [minComp] + · rw [h1]; unfold minComp; dsimp only + have hx : Z ^^^ Z = 0 := by + apply Nat.eq_of_testBit_eq; intro p; simp only [Nat.testBit_xor, Nat.zero_testBit]; cases Z.testBit p <;> rfl + rw [hx]; cases Z <;> simp + +theorem normZ_trivW (u : Bool) {Z w : Nat} (hZ : Z ≤ full64) : + trivW u Z (normZ u Z w) = trivW u Z w := by + cases u + · simp [normZ, trivW, and_idem_r] + · unfold normZ; simp only [ite_true] + rcases minComp_eq_or (and_idem_r w Z) with hm | hm <;> rw [hm] + · simp [trivW, and_idem_r] + · apply Bool.eq_iff_iff.mpr + rw [trivW_iff, trivW_iff] + have hx : ((w &&& Z) ^^^ Z) &&& Z = (w &&& Z) ^^^ Z := by + apply Nat.eq_of_testBit_eq; intro p + simp only [Nat.testBit_and, Nat.testBit_xor] + cases w.testBit p <;> cases Z.testBit p <;> rfl + rw [hx] + have hxx : ∀ a b : Nat, (a ^^^ Z = b ↔ a = b ^^^ Z) := by + intro a b + constructor + · intro h; rw [← h]; apply Nat.eq_of_testBit_eq; intro p + simp only [Nat.testBit_xor]; cases a.testBit p <;> cases Z.testBit p <;> rfl + · intro h; rw [h]; apply Nat.eq_of_testBit_eq; intro p + simp only [Nat.testBit_xor]; cases b.testBit p <;> cases Z.testBit p <;> rfl + have hz0 : 0 ^^^ Z = Z := by + apply Nat.eq_of_testBit_eq; intro p; simp only [Nat.testBit_xor, Nat.zero_testBit]; cases Z.testBit p <;> rfl + have hzz : Z ^^^ Z = 0 := by + apply Nat.eq_of_testBit_eq; intro p; simp only [Nat.testBit_xor, Nat.zero_testBit]; cases Z.testBit p <;> rfl + rw [hxx (w &&& Z) 0, hz0, hxx (w &&& Z) Z, hzz, or_comm] + +theorem eqZ_iff (u : Bool) {Z a b : Nat} (hZ : Z ≤ full64) : + eqZ u Z a b = true ↔ normZ u Z a = normZ u Z b := by + unfold eqZ normZ + cases u + · simp only [Bool.false_and, Bool.or_false, beq_iff_eq, Bool.false_eq_true, ite_false] + · simp only [Bool.true_and, Bool.or_eq_true, beq_iff_eq, ite_true] + constructor + · rintro (h | h) + · rw [h] + · rw [h]; exact minComp_xor_self hZ (and_idem_r b Z) + · intro h + have ha := minComp_eq_or (and_idem_r a Z) + have hb := minComp_eq_or (and_idem_r b Z) + rcases ha with ha | ha <;> rcases hb with hb | hb + · left; exact ha.symm.trans (h.trans hb) + · right; exact ha.symm.trans (h.trans hb) + · right + have h1 : (a &&& Z) ^^^ Z = b &&& Z := ha.symm.trans (h.trans hb) + apply Nat.eq_of_testBit_eq; intro p + have hp := congrArg (fun n => n.testBit p) h1 + simp only [Nat.testBit_and, Nat.testBit_xor] at hp ⊢ + cases hpa : a.testBit p <;> cases hpb : b.testBit p <;> cases hpz : Z.testBit p <;> simp_all + · left + have h1 : (a &&& Z) ^^^ Z = (b &&& Z) ^^^ Z := ha.symm.trans (h.trans hb) + apply Nat.eq_of_testBit_eq; intro p + have hp := congrArg (fun n => n.testBit p) h1 + simp only [Nat.testBit_and, Nat.testBit_xor] at hp ⊢ + cases hpa : a.testBit p <;> cases hpb : b.testBit p <;> cases hpz : Z.testBit p <;> simp_all + +theorem normZ_idem (u : Bool) {Z Z1 w : Nat} (hZ : Z ≤ full64) (hsub : Z &&& Z1 = Z) : + normZ u Z (normZ u Z1 w) = normZ u Z w := by + cases u + · unfold normZ; simp only [Bool.false_eq_true, ite_false]; exact and_sub_mask hsub + · unfold normZ; simp only [ite_true] + rcases minComp_eq_or (and_idem_r w Z1) with hm | hm + · rw [hm, and_sub_mask hsub] + · rw [hm] + have heq : ((w &&& Z1) ^^^ Z1) &&& Z = (w &&& Z) ^^^ Z := by + apply Nat.eq_of_testBit_eq; intro p + have h1 := congrArg (fun n => n.testBit p) hsub + simp only [Nat.testBit_and, Nat.testBit_xor] at h1 ⊢ + cases hw : w.testBit p <;> cases hz1 : Z1.testBit p <;> cases hz : Z.testBit p <;> simp_all + rw [heq] + exact minComp_xor_self hZ (and_idem_r w Z) + +theorem normZ_stepVal {u : Bool} {Z d a b : Nat} (hZ : Z ≤ full64) : + normZ u Z (stepVal u Z d a b) = stepVal u Z d a b := + normZ_idem u hZ (Nat.and_self Z) + +theorem opU_lt64 (u : Bool) {d a b : Nat} (ha : a < 2 ^ 64) (hb : b < 2 ^ 64) : + opU u d a b < 2 ^ 64 := by + unfold opU + split_ifs + · exact and_lt64 hb + · exact or_lt64 ha hb + · exact zero_lt64 + · exact and_lt64 (lt64_of_xor hb) + · exact and_lt64 hb + +theorem normZ_lt64 (u : Bool) {Z w : Nat} (hZ : Z ≤ full64) : normZ u Z w < 2 ^ 64 := by + have hZ64 : Z < 2 ^ 64 := by rw [full64_eq] at hZ; omega + have haw : (w &&& Z) < 2 ^ 64 := and_lt64 hZ64 + unfold normZ + cases u + · exact haw + · dsimp only [ite_true] + rcases minComp_eq_or (and_idem_r w Z) with hm | hm <;> rw [hm] + · exact haw + · exact Nat.xor_lt_two_pow haw hZ64 + +theorem stepVal_flip {u : Bool} {Z d a b : Nat} (hZ : Z ≤ full64) (hd : d < opsCount u) : + stepVal u Z d a b = stepVal u Z (flipOp u d) b a := by + unfold stepVal normZ flipOp opU + have heq : ∀ x y : Nat, (x &&& Z) = (y &&& Z) → + (if u then minComp Z (x &&& Z) else x &&& Z) = + (if u then minComp Z (y &&& Z) else y &&& Z) := fun _ _ h => by rw [h] + apply heq + apply Nat.eq_of_testBit_eq; intro p + rcases Nat.lt_or_ge p 64 with hp | hp + · cases hu : u <;> simp [opsCount, hu] at hd ⊢ + · rcases Nat.lt_or_ge d 1 with h0 | h0 + · have : d = 0 := by omega + subst this; simp [Nat.testBit_and, Bool.and_comm] + · have : d = 1 := by omega + subst this; simp [Nat.testBit_and, Nat.testBit_or, Bool.or_comm] + · have : d = 0 ∨ d = 1 ∨ d = 2 ∨ d = 3 := by omega + rcases this with rfl | rfl | rfl | rfl <;> + simp [Nat.testBit_and, Nat.testBit_or, testBit_xor_full hp, Bool.and_comm, Bool.or_comm, Bool.and_left_comm] + · have hZ64 : Z < 2 ^ 64 := by rw [full64_eq] at hZ; omega + have hZp : Z.testBit p = false := + Nat.testBit_lt_two_pow (lt_of_lt_of_le hZ64 (Nat.pow_le_pow_right (by decide) hp)) + simp [Nat.testBit_and, hZp] + +theorem flipOp_lt (u : Bool) {d : Nat} (hd : d < opsCount u) : flipOp u d < opsCount u := by + cases u <;> simp only [flipOp, opsCount] at hd ⊢ <;> split_ifs at * <;> omega + +def opB (u : Bool) (d : Nat) (a b : Bool) : Bool := + if d = 0 then a && b + else if d = 1 then a || b + else if !u then false + else if d = 2 then a && !b + else (!a) && b + +def normD (fa fb : Bool) (d : Nat) : Nat := + if !fa && !fb then d + else if !fa && fb then (if d = 0 then 2 else if d = 1 then 3 else if d = 2 then 0 else 1) + else if fa && !fb then (if d = 0 then 3 else if d = 1 then 2 else if d = 2 then 1 else 0) + else (if d = 0 then 1 else if d = 1 then 0 else if d = 2 then 3 else 2) + +theorem normD_lt (fa fb : Bool) (d : Fin 4) : normD fa fb d.val < 4 := by + revert fa fb d; decide + +theorem normD_spec (d : Fin 4) (fa fb a b : Bool) : + opB true d.val a b = + (let v := opB true (normD fa fb d.val) (if fa then !a else a) (if fb then !b else b) + if (!fa && fb && (d.val = 1 || d.val = 3)) || (fa && !fb && (d.val = 1 || d.val = 2)) || + (fa && fb && (d.val = 0 || d.val = 1)) then !v else v) := by + revert d fa fb a b; decide + +theorem trivL_spec (u : Bool) (d : Fin 4) (ta : Bool) : + d.val < opsCount u → + (∀ b, opB u d.val ta b = false) ∨ + (∀ b, opB u d.val ta b = true) ∨ + (∀ b, opB u d.val ta b = b) ∨ + (u = true ∧ ∀ b, opB u d.val ta b = !b) := by + revert u d ta; decide + +theorem testBit_opU (u : Bool) (d a b p : Nat) (hp : p < 64) : + (opU u d a b).testBit p = opB u d (a.testBit p) (b.testBit p) := by + unfold opU opB + rcases Nat.lt_or_ge d 3 with hd3 | hd3 + · split_ifs with h0 h1 hu h2 <;> simp [Nat.testBit_and, Nat.testBit_or, testBit_xor_full hp, Nat.zero_testBit] + · split_ifs with h0 h1 hu h2 <;> try omega + · simp [Nat.zero_testBit] + · simp [Nat.testBit_and, testBit_xor_full hp] + +theorem stepVal_norm_inputs {u : Bool} {Z Z1 d a b : Nat} (hZ : Z ≤ full64) (hZ1 : Z1 ≤ full64) + (hsub : Z &&& Z1 = Z) (hd : d < opsCount u) : + ∃ d' < opsCount u, stepVal u Z d a b = stepVal u Z d' (normZ u Z1 a) (normZ u Z1 b) := by + have hZ64 : Z < 2 ^ 64 := by rw [full64_eq] at hZ; omega + have mask_bit : ∀ p, 64 ≤ p → Z.testBit p = false := fun p hp => + Nat.testBit_lt_two_pow (lt_of_lt_of_le hZ64 (Nat.pow_le_pow_right (by decide) hp)) + cases u + · refine ⟨d, hd, ?_⟩ + show normZ false Z (opU false d a b) = normZ false Z (opU false d (normZ false Z1 a) (normZ false Z1 b)) + unfold normZ; simp only [Bool.false_eq_true, ↓reduceIte] + have h01 : d = 0 ∨ d = 1 := by unfold opsCount at hd; dsimp at hd; omega + apply Nat.eq_of_testBit_eq; intro p + rcases Nat.lt_or_ge p 64 with hp | hp + · simp only [Nat.testBit_and, testBit_opU false d a b p hp, + testBit_opU false d (a &&& Z1) (b &&& Z1) p hp] + have hz := congrArg (fun n => n.testBit p) hsub + simp only [Nat.testBit_and] at hz + rcases h01 with rfl | rfl <;> + (simp only [opB] at *; cases hpa : a.testBit p <;> cases hpb : b.testBit p <;> + cases hpz : Z.testBit p <;> cases hpz1 : Z1.testBit p <;> simp_all) + · simp [Nat.testBit_and, mask_bit p hp] + · let a1 := normZ true Z1 a + let b1 := normZ true Z1 b + have ha : a1 &&& Z = a &&& Z ∨ a1 &&& Z = (a &&& Z) ^^^ Z := by + rcases minComp_eq_or (and_idem_r a Z1) with hm | hm + · left; simpa [a1, normZ, hm] using and_sub_mask hsub + · right + have ha1 : a1 = (a &&& Z1) ^^^ Z1 := by simp [a1, normZ, hm] + apply Nat.eq_of_testBit_eq; intro p + have hp1 := congrArg (fun n => n.testBit p) ha1 + have hs := congrArg (fun n => n.testBit p) hsub + simp only [Nat.testBit_and, Nat.testBit_xor] at hp1 hs ⊢ + rw [hp1] + cases hpa : a.testBit p <;> cases hpz1 : Z1.testBit p <;> cases hpz : Z.testBit p <;> simp_all + have hb : b1 &&& Z = b &&& Z ∨ b1 &&& Z = (b &&& Z) ^^^ Z := by + rcases minComp_eq_or (and_idem_r b Z1) with hm | hm + · left; simpa [b1, normZ, hm] using and_sub_mask hsub + · right + have hb1 : b1 = (b &&& Z1) ^^^ Z1 := by simp [b1, normZ, hm] + apply Nat.eq_of_testBit_eq; intro p + have hp1 := congrArg (fun n => n.testBit p) hb1 + have hs := congrArg (fun n => n.testBit p) hsub + simp only [Nat.testBit_and, Nat.testBit_xor] at hp1 hs ⊢ + rw [hp1] + cases hpb : b.testBit p <;> cases hpz1 : Z1.testBit p <;> cases hpz : Z.testBit p <;> simp_all + have hd4 : d < 4 := by simpa [opsCount] using hd + let fa := !(a1 &&& Z == a &&& Z) + let fb := !(b1 &&& Z == b &&& Z) + have hfa : ∀ p, Z.testBit p = true → a1.testBit p = if fa then !(a.testBit p) else a.testBit p := by + intro p hZp + rcases ha with h | h + · have he : fa = false := by simp [fa, h] + have hp := congrArg (fun n => n.testBit p) h + simp only [Nat.testBit_and, hZp, Bool.and_true] at hp + simp [he, hp] + · cases hfz : fa + · have heq : a1 &&& Z = a &&& Z := by simpa [fa] using hfz + have hp := congrArg (fun n => n.testBit p) heq + simp only [Nat.testBit_and, hZp, Bool.and_true] at hp; simp [hp] + · have hp := congrArg (fun n => n.testBit p) h + simp only [Nat.testBit_and, Nat.testBit_xor, hZp, Bool.and_true] at hp; simp [hp] + have hfb : ∀ p, Z.testBit p = true → b1.testBit p = if fb then !(b.testBit p) else b.testBit p := by + intro p hZp + rcases hb with h | h + · have he : fb = false := by simp [fb, h] + have hp := congrArg (fun n => n.testBit p) h + simp only [Nat.testBit_and, hZp, Bool.and_true] at hp + simp [he, hp] + · cases hfz : fb + · have heq : b1 &&& Z = b &&& Z := by simpa [fb] using hfz + have hp := congrArg (fun n => n.testBit p) heq + simp only [Nat.testBit_and, hZp, Bool.and_true] at hp; simp [hp] + · have hp := congrArg (fun n => n.testBit p) h + simp only [Nat.testBit_and, Nat.testBit_xor, hZp, Bool.and_true] at hp; simp [hp] + let d' := normD fa fb d + have hd' : d' < opsCount true := by simpa [opsCount] using normD_lt fa fb ⟨d, hd4⟩ + refine ⟨d', hd', ?_⟩ + show normZ true Z (opU true d a b) = normZ true Z (opU true d' a1 b1) + unfold normZ; simp only [ite_true] + let flipOut := (!fa && fb && (d = 1 || d = 3)) || (fa && !fb && (d = 1 || d = 2)) || + (fa && fb && (d = 0 || d = 1)) + have hraw : opU true d a b &&& Z = + if flipOut then (opU true d' a1 b1 &&& Z) ^^^ Z else opU true d' a1 b1 &&& Z := by + split_ifs with hf + · apply Nat.eq_of_testBit_eq; intro p + rcases Nat.lt_or_ge p 64 with hp | hp + · cases hZp : Z.testBit p + · simp [Nat.testBit_and, Nat.testBit_xor, hZp] + · have hsp := normD_spec ⟨d, hd4⟩ fa fb (a.testBit p) (b.testBit p) + have hfl : flipOut = true := hf + dsimp only at hsp + rw [Nat.testBit_and, Nat.testBit_xor, Nat.testBit_and, hZp, Bool.and_true, Bool.and_true, + testBit_opU true d a b p hp, testBit_opU true d' a1 b1 p hp, hfa p hZp, hfb p hZp] + simpa [flipOut, d', hfl] using hsp + · simp [Nat.testBit_and, Nat.testBit_xor, mask_bit p hp] + · apply Nat.eq_of_testBit_eq; intro p + rcases Nat.lt_or_ge p 64 with hp | hp + · cases hZp : Z.testBit p + · simp [Nat.testBit_and, hZp] + · have hsp := normD_spec ⟨d, hd4⟩ fa fb (a.testBit p) (b.testBit p) + have hfl : flipOut = false := by cases hc : flipOut <;> simp_all + dsimp only at hsp + rw [Nat.testBit_and, Nat.testBit_and, hZp, Bool.and_true, Bool.and_true, + testBit_opU true d a b p hp, testBit_opU true d' a1 b1 p hp, hfa p hZp, hfb p hZp] + simpa [flipOut, d', hfl] using hsp + · simp [Nat.testBit_and, mask_bit p hp] + rw [hraw] + split_ifs + · exact minComp_xor_self hZ (and_idem_r (opU true d' a1 b1) Z) + · rfl + +theorem stepVal_triv_left {u : Bool} {Z d a b : Nat} (hZ : Z ≤ full64) (hd : d < opsCount u) + (ht : trivW u Z a = true) : + trivW u Z (stepVal u Z d a b) = true ∨ + eqZ u Z (stepVal u Z d a b) a = true ∨ + eqZ u Z (stepVal u Z d a b) b = true := by + have hZ64 : Z < 2 ^ 64 := by rw [full64_eq] at hZ; omega + have mask_bit : ∀ p, 64 ≤ p → Z.testBit p = false := fun p hp => + Nat.testBit_lt_two_pow (lt_of_lt_of_le hZ64 (Nat.pow_le_pow_right (by decide) hp)) + suffices hraw : trivW u Z (opU u d a b) = true ∨ + eqZ u Z (opU u d a b) a = true ∨ eqZ u Z (opU u d a b) b = true by + rcases hraw with h | h | h + · left; simpa [stepVal, normZ_trivW u hZ] using h + · right; left; rw [eqZ_iff u hZ] at h ⊢; unfold stepVal; rwa [normZ_idem u hZ (Nat.and_self Z)] + · right; right; rw [eqZ_iff u hZ] at h ⊢; unfold stepVal; rwa [normZ_idem u hZ (Nat.and_self Z)] + have hd4 : d < 4 := by cases u <;> simp [opsCount] at hd <;> omega + rw [trivW_iff] at ht + rcases ht with ha0 | haZ + · rcases trivL_spec u ⟨d, hd4⟩ false hd with h | h | h | ⟨hu, h⟩ + · left; rw [trivW_iff]; left; apply Nat.eq_of_testBit_eq; intro p + rcases Nat.lt_or_ge p 64 with hp | hp + · cases hZp : Z.testBit p + · simp [Nat.testBit_and, hZp] + · have h0 := congrArg (fun n => n.testBit p) ha0 + simp only [Nat.testBit_and, Nat.zero_testBit, hZp, Bool.and_true] at h0 + rw [Nat.testBit_and, testBit_opU u d a b p hp, h0, h (b.testBit p), Bool.false_and, Nat.zero_testBit] + · simp [Nat.testBit_and, mask_bit p hp] + · left; rw [trivW_iff]; right; apply Nat.eq_of_testBit_eq; intro p + rcases Nat.lt_or_ge p 64 with hp | hp + · cases hZp : Z.testBit p + · simp [Nat.testBit_and, hZp] + · have h0 := congrArg (fun n => n.testBit p) ha0 + simp only [Nat.testBit_and, Nat.zero_testBit, hZp, Bool.and_true] at h0 + rw [Nat.testBit_and, testBit_opU u d a b p hp, h0, h (b.testBit p), Bool.true_and, hZp] + · simp [Nat.testBit_and, mask_bit p hp] + · right; right; unfold eqZ; simp only [Bool.or_eq_true, beq_iff_eq]; left + apply Nat.eq_of_testBit_eq; intro p + rcases Nat.lt_or_ge p 64 with hp | hp + · cases hZp : Z.testBit p + · simp [Nat.testBit_and, hZp] + · have h0 := congrArg (fun n => n.testBit p) ha0 + simp only [Nat.testBit_and, Nat.zero_testBit, hZp, Bool.and_true] at h0 + rw [Nat.testBit_and, Nat.testBit_and, testBit_opU u d a b p hp, h0, h (b.testBit p), hZp, Bool.and_true] + · simp [Nat.testBit_and, mask_bit p hp] + · subst hu; right; right; unfold eqZ; simp only [Bool.true_and, Bool.or_eq_true, beq_iff_eq]; right + apply Nat.eq_of_testBit_eq; intro p + rcases Nat.lt_or_ge p 64 with hp | hp + · cases hZp : Z.testBit p + · simp [Nat.testBit_and, Nat.testBit_xor, hZp] + · have h0 := congrArg (fun n => n.testBit p) ha0 + simp only [Nat.testBit_and, Nat.zero_testBit, hZp, Bool.and_true] at h0 + rw [Nat.testBit_and, Nat.testBit_xor, Nat.testBit_and, testBit_opU true d a b p hp, h0, h (b.testBit p), hZp, Bool.and_true, Bool.and_true] + cases b.testBit p <;> rfl + · simp [Nat.testBit_and, Nat.testBit_xor, mask_bit p hp] + · rcases trivL_spec u ⟨d, hd4⟩ true hd with h | h | h | ⟨hu, h⟩ + · left; rw [trivW_iff]; left; apply Nat.eq_of_testBit_eq; intro p + rcases Nat.lt_or_ge p 64 with hp | hp + · cases hZp : Z.testBit p + · simp [Nat.testBit_and, hZp] + · have h0 := congrArg (fun n => n.testBit p) haZ + simp only [Nat.testBit_and, hZp, Bool.and_true] at h0 + rw [Nat.testBit_and, testBit_opU u d a b p hp, h0, h (b.testBit p), Bool.false_and, Nat.zero_testBit] + · simp [Nat.testBit_and, mask_bit p hp] + · left; rw [trivW_iff]; right; apply Nat.eq_of_testBit_eq; intro p + rcases Nat.lt_or_ge p 64 with hp | hp + · cases hZp : Z.testBit p + · simp [Nat.testBit_and, hZp] + · have h0 := congrArg (fun n => n.testBit p) haZ + simp only [Nat.testBit_and, hZp, Bool.and_true] at h0 + rw [Nat.testBit_and, testBit_opU u d a b p hp, h0, h (b.testBit p), Bool.true_and, hZp] + · simp [Nat.testBit_and, mask_bit p hp] + · right; right; unfold eqZ; simp only [Bool.or_eq_true, beq_iff_eq]; left + apply Nat.eq_of_testBit_eq; intro p + rcases Nat.lt_or_ge p 64 with hp | hp + · cases hZp : Z.testBit p + · simp [Nat.testBit_and, hZp] + · have h0 := congrArg (fun n => n.testBit p) haZ + simp only [Nat.testBit_and, hZp, Bool.and_true] at h0 + rw [Nat.testBit_and, Nat.testBit_and, testBit_opU u d a b p hp, h0, h (b.testBit p), hZp, Bool.and_true] + · simp [Nat.testBit_and, mask_bit p hp] + · subst hu; right; right; unfold eqZ; simp only [Bool.true_and, Bool.or_eq_true, beq_iff_eq]; right + apply Nat.eq_of_testBit_eq; intro p + rcases Nat.lt_or_ge p 64 with hp | hp + · cases hZp : Z.testBit p + · simp [Nat.testBit_and, Nat.testBit_xor, hZp] + · have h0 := congrArg (fun n => n.testBit p) haZ + simp only [Nat.testBit_and, hZp, Bool.and_true] at h0 + rw [Nat.testBit_and, Nat.testBit_xor, Nat.testBit_and, testBit_opU true d a b p hp, h0, h (b.testBit p), hZp, Bool.and_true, Bool.and_true] + cases b.testBit p <;> rfl + · simp [Nat.testBit_and, Nat.testBit_xor, mask_bit p hp] + +theorem stepVal_triv_in {u : Bool} {Z d a b : Nat} (hZ : Z ≤ full64) (hd : d < opsCount u) + (ht : trivW u Z a = true ∨ trivW u Z b = true) : + trivW u Z (stepVal u Z d a b) = true ∨ + eqZ u Z (stepVal u Z d a b) a = true ∨ + eqZ u Z (stepVal u Z d a b) b = true := by + rcases ht with ha | hb + · exact stepVal_triv_left hZ hd ha + · rw [stepVal_flip hZ hd] + rcases stepVal_triv_left hZ (flipOp_lt u hd) hb with h | h | h + · exact Or.inl h + · exact Or.inr (Or.inr h) + · exact Or.inr (Or.inl h) + +theorem mem_varsOf_of_norm {u : Bool} {Z w : Nat} (hZ : Z ≤ full64) + (hw : w ∈ varTabs) (hnt : !trivW u Z w = true) : + normZ u Z w ∈ varsOf u Z := by + unfold varsOf + rw [List.mem_filter] + refine ⟨List.mem_map.mpr ⟨w, hw, rfl⟩, ?_⟩ + rw [Bool.not_eq_true'] at hnt ⊢ + rw [normZ_trivW u hZ] + exact hnt + +theorem evalGateNat_step (u : Bool) (g : Gate) (hg : g.op ∈ ({GateOp.and, GateOp.or, GateOp.not} : Set GateOp)) + (hlen : g.inputs.length ≤ 2) (hno : u = false → isNotG g = false) + (ws S : List Nat) (hws : ∀ j, trivW u full64 (ws.getD j 0) = true ∨ normZ u full64 (ws.getD j 0) ∈ S) : + (trivW u full64 (evalGateNat g ws) = true) ∨ + (normZ u full64 (evalGateNat g ws) ∈ S) ∨ + (isNotG g = false ∧ ∃ a ∈ S, ∃ b ∈ S, ∃ d < opsCount u, + normZ u full64 (evalGateNat g ws) = stepVal u full64 d a b) := by + have hF : full64 ≤ full64 := Nat.le_refl _ + rcases hg with h | h | h + · have hni : isNotG g = false := by simp [isNotG, h] + cases hi0 : g.inputs with + | nil => + left; simp [evalGateNat, h, hi0, trivW] + | cons i rest => + cases hi1 : rest with + | nil => + have hr : evalGateNat g ws = full64 &&& ws.getD i 0 := by simp [evalGateNat, h, hi0, hi1] + have hn : normZ u full64 (evalGateNat g ws) = normZ u full64 (ws.getD i 0) := by + rw [hr]; unfold normZ + have ha : (full64 &&& ws.getD i 0) &&& full64 = ws.getD i 0 &&& full64 := by + apply Nat.eq_of_testBit_eq; intro p + rcases Nat.lt_or_ge p 64 with hp | hp + · simp [Nat.testBit_and, full64_tb p hp] + · have hf64 : full64 < 2 ^ 64 := by rw [full64_eq]; omega + have hfp : full64.testBit p = false := + Nat.testBit_lt_two_pow (lt_of_lt_of_le hf64 (Nat.pow_le_pow_right (by decide) hp)) + simp [Nat.testBit_and, hfp] + rw [ha] + rcases hws i with ht | hm + · left; rw [← normZ_trivW u hF, hn, normZ_trivW u hF]; exact ht + · right; left; rw [hn]; exact hm + | cons j rest2 => + have hlen2 : rest2 = [] := by rw [hi0, hi1] at hlen; simp only [List.length_cons] at hlen; exact List.length_eq_zero_iff.mp (by omega) + subst hlen2 + have hr : evalGateNat g ws = (full64 &&& ws.getD i 0) &&& ws.getD j 0 := by + simp [evalGateNat, h, hi0, hi1] + have heq_raw : (evalGateNat g ws) &&& full64 = opU u 0 (ws.getD i 0) (ws.getD j 0) &&& full64 := by + rw [hr]; unfold opU; simp only [ite_true] + apply Nat.eq_of_testBit_eq; intro p + rcases Nat.lt_or_ge p 64 with hp | hp + · simp [Nat.testBit_and, full64_tb p hp] + · have hf64 : full64 < 2 ^ 64 := by rw [full64_eq]; omega + have hfp : full64.testBit p = false := + Nat.testBit_lt_two_pow (lt_of_lt_of_le hf64 (Nat.pow_le_pow_right (by decide) hp)) + simp [Nat.testBit_and, hfp] + have hn_raw : normZ u full64 (evalGateNat g ws) = stepVal u full64 0 (ws.getD i 0) (ws.getD j 0) := by + unfold stepVal normZ; rw [heq_raw] + rcases hws i with hti | hmi + · rcases stepVal_triv_in hF (d := 0) (b := ws.getD j 0) (by cases u <;> decide) (Or.inl hti) with ht | he | he + · left; rw [← normZ_trivW u hF, hn_raw]; exact ht + · rw [eqZ_iff u hF, normZ_stepVal hF] at he; left; rw [← normZ_trivW u hF, hn_raw, he, normZ_trivW u hF]; exact hti + · rw [eqZ_iff u hF, normZ_stepVal hF] at he + rcases hws j with htj | hmj + · left; rw [← normZ_trivW u hF, hn_raw, he, normZ_trivW u hF]; exact htj + · right; left; rw [hn_raw, he]; exact hmj + · rcases hws j with htj | hmj + · rcases stepVal_triv_in hF (d := 0) (a := ws.getD i 0) (by cases u <;> decide) (Or.inr htj) with ht | he | he + · left; rw [← normZ_trivW u hF, hn_raw]; exact ht + · rw [eqZ_iff u hF, normZ_stepVal hF] at he; right; left; rw [hn_raw, he]; exact hmi + · rw [eqZ_iff u hF, normZ_stepVal hF] at he; left; rw [← normZ_trivW u hF, hn_raw, he, normZ_trivW u hF]; exact htj + · obtain ⟨d', hd', hstp⟩ := stepVal_norm_inputs (u := u) hF hF (Nat.and_self full64) + (d := 0) (a := ws.getD i 0) (b := ws.getD j 0) (by cases u <;> decide) + right; right; exact ⟨hni, _, hmi, _, hmj, d', hd', hn_raw.trans hstp⟩ + · have hni : isNotG g = false := by simp [isNotG, h] + cases hi0 : g.inputs with + | nil => left; simp [evalGateNat, h, hi0, trivW] + | cons i rest => + cases hi1 : rest with + | nil => + have hr : evalGateNat g ws = 0 ||| ws.getD i 0 := by simp [evalGateNat, h, hi0, hi1] + have hn : normZ u full64 (evalGateNat g ws) = normZ u full64 (ws.getD i 0) := by + rw [hr]; simp [Nat.zero_or] + rcases hws i with ht | hm + · left; rw [← normZ_trivW u hF, hn, normZ_trivW u hF]; exact ht + · right; left; rw [hn]; exact hm + | cons j rest2 => + have hlen2 : rest2 = [] := by rw [hi0, hi1] at hlen; simp only [List.length_cons] at hlen; exact List.length_eq_zero_iff.mp (by omega) + subst hlen2 + have hr : evalGateNat g ws = (0 ||| ws.getD i 0) ||| ws.getD j 0 := by + simp [evalGateNat, h, hi0, hi1] + have heq_raw : (evalGateNat g ws) &&& full64 = opU u 1 (ws.getD i 0) (ws.getD j 0) &&& full64 := by + rw [hr]; unfold opU; simp [Nat.zero_or] + have hn_raw : normZ u full64 (evalGateNat g ws) = stepVal u full64 1 (ws.getD i 0) (ws.getD j 0) := by + unfold stepVal normZ; rw [heq_raw] + rcases hws i with hti | hmi + · rcases stepVal_triv_in hF (d := 1) (b := ws.getD j 0) (by cases u <;> decide) (Or.inl hti) with ht | he | he + · left; rw [← normZ_trivW u hF, hn_raw]; exact ht + · rw [eqZ_iff u hF, normZ_stepVal hF] at he; left; rw [← normZ_trivW u hF, hn_raw, he, normZ_trivW u hF]; exact hti + · rw [eqZ_iff u hF, normZ_stepVal hF] at he + rcases hws j with htj | hmj + · left; rw [← normZ_trivW u hF, hn_raw, he, normZ_trivW u hF]; exact htj + · right; left; rw [hn_raw, he]; exact hmj + · rcases hws j with htj | hmj + · rcases stepVal_triv_in hF (d := 1) (a := ws.getD i 0) (by cases u <;> decide) (Or.inr htj) with ht | he | he + · left; rw [← normZ_trivW u hF, hn_raw]; exact ht + · rw [eqZ_iff u hF, normZ_stepVal hF] at he; right; left; rw [hn_raw, he]; exact hmi + · rw [eqZ_iff u hF, normZ_stepVal hF] at he; left; rw [← normZ_trivW u hF, hn_raw, he, normZ_trivW u hF]; exact htj + · obtain ⟨d', hd', hstp⟩ := stepVal_norm_inputs (u := u) hF hF (Nat.and_self full64) + (d := 1) (a := ws.getD i 0) (b := ws.getD j 0) (by cases u <;> decide) + right; right; exact ⟨hni, _, hmi, _, hmj, d', hd', hn_raw.trans hstp⟩ + · simp only [Set.mem_singleton_iff] at h + have hu : u = true := by + cases hu0 : u + · have hn := hno hu0; simp [isNotG, h] at hn + · rfl + subst hu + cases hi0 : g.inputs with + | nil => left; simp [evalGateNat, h, hi0, trivW] + | cons j _ => + have hr : evalGateNat g ws = (ws.getD j 0) ^^^ full64 := by simp [evalGateNat, h, hi0] + have hn : normZ true full64 (evalGateNat g ws) = normZ true full64 (ws.getD j 0) := by + rw [hr]; exact normZ_xor_full hF rfl + rcases hws j with htj | hmj + · left; rw [← normZ_trivW true hF, hn, normZ_trivW true hF]; exact htj + · right; left; rw [hn]; exact hmj + +theorem foldl_to_ValidProg (u : Bool) (gs : List Gate) + (hgs : ∀ g ∈ gs, (g.op ∈ ({GateOp.and, GateOp.or, GateOp.not} : Set GateOp) ∧ g.inputs.length ≤ 2)) + (hno : u = false → ∀ g ∈ gs, isNotG g = false) : + ∀ (ws S : List Nat), + (∀ w ∈ ws, trivW u full64 w = true ∨ normZ u full64 w ∈ S) → + ∀ (i : Nat), + let outW := (gs.foldl (fun acc g => acc ++ [evalGateNat g acc]) ws).getD i 0 + trivW u full64 outW = true ∨ + ∃ P : List (Nat × Nat × Nat), + P.length ≤ gs.length - gs.countP isNotG ∧ + ValidProg u full64 S P (normZ u full64 outW) := by + induction gs with + | nil => + intro ws S hws i + cases hnone : ws[i]? with + | none => left; simp [List.getD, hnone, trivW] + | some w => + simp only [List.foldl_nil, List.getD, hnone, Option.getD_some] + rcases hws w (List.mem_of_getElem? hnone) with ht | hm + · left; exact ht + · right; exact ⟨[], by simp, hm⟩ + | cons g rest ih => + intro ws S hws i + have hg := hgs g (by simp) + have hrest : ∀ g' ∈ rest, g'.op ∈ ({GateOp.and, GateOp.or, GateOp.not} : Set GateOp) ∧ g'.inputs.length ≤ 2 := + fun g' hg' => hgs g' (by simp [hg']) + have hno_g : u = false → isNotG g = false := fun hu => hno hu g (by simp) + have hno_rest : u = false → ∀ g' ∈ rest, isNotG g' = false := fun hu g' hg' => hno hu g' (by simp [hg']) + have hwsj : ∀ j, trivW u full64 (ws.getD j 0) = true ∨ normZ u full64 (ws.getD j 0) ∈ S := by + intro j + cases hnone : ws[j]? with + | none => left; simp [List.getD, hnone, trivW] + | some w => simp only [List.getD, hnone, Option.getD_some]; exact hws w (List.mem_of_getElem? hnone) + rcases evalGateNat_step u g hg.1 hg.2 hno_g ws S hwsj with htriv | hmem | ⟨hng, a, ha, b, hb, d, hd, heq⟩ + · have hnext := ih hrest hno_rest (ws ++ [evalGateNat g ws]) S (by + intro w hw; simp only [List.mem_append, List.mem_singleton] at hw + rcases hw with hw | rfl + · exact hws w hw + · left; exact htriv) i + rcases hnext with ht | ⟨P, hPlen, hP⟩ + · left; exact ht + · right; refine ⟨P, le_trans hPlen ?_, hP⟩ + have hc := List.countP_le_length (p := isNotG) (l := rest) + cases hn : isNotG g <;> simp only [List.length_cons, List.countP_cons, hn] <;> split_ifs <;> omega + · have hnext := ih hrest hno_rest (ws ++ [evalGateNat g ws]) S (by + intro w hw; simp only [List.mem_append, List.mem_singleton] at hw + rcases hw with hw | rfl + · exact hws w hw + · right; exact hmem) i + rcases hnext with ht | ⟨P, hPlen, hP⟩ + · left; exact ht + · right; refine ⟨P, le_trans hPlen ?_, hP⟩ + have hc := List.countP_le_length (p := isNotG) (l := rest) + cases hn : isNotG g <;> simp only [List.length_cons, List.countP_cons, hn] <;> split_ifs <;> omega + · have hnext := ih hrest hno_rest (ws ++ [evalGateNat g ws]) (stepVal u full64 d a b :: S) (by + intro w hw; simp only [List.mem_append, List.mem_singleton] at hw + rcases hw with hw | rfl + · rcases hws w hw with ht | hm + · left; exact ht + · right; exact List.mem_cons_of_mem _ hm + · right; rw [heq]; exact List.mem_cons_self) i + rcases hnext with ht | ⟨P, hPlen, hP⟩ + · left; exact ht + · right; refine ⟨(a, b, d) :: P, ?_, ⟨ha, hb, hd, hP⟩⟩ + have hc := List.countP_le_length (p := isNotG) (l := rest) + simp only [List.length_cons, List.countP_cons, hng, Bool.false_eq_true, ↓reduceIte, Nat.add_zero] at hPlen ⊢ + omega + +theorem circuit_to_ValidProg {u : Bool} {C : Circuit} {tab : Nat} (legal : FanInTwo C) + (hno : u = false → ∀ g ∈ C.gates, isNotG g = false) + (computes : ∀ x : Fin 6 → Bool, C.eval (List.ofFn x) = fOf tab x) + (hnt : !trivW u full64 tab = true) : + ∃ P : List (Nat × Nat × Nat), + P.length ≤ C.gates.countP (fun g => !isNotG g) ∧ + ValidProg u full64 (varsOf u full64) P (normZ u full64 tab) := by + have hF : full64 ≤ full64 := Nat.le_refl _ + have hws0 : ∀ w ∈ varTabs, trivW u full64 w = true ∨ normZ u full64 w ∈ varsOf u full64 := by + intro w hw + cases ht : trivW u full64 w + · right; exact mem_varsOf_of_norm hF hw (by simp [ht]) + · left; exact rfl + have hbit : ((evalWiresNat C.gates varTabs).getD C.output 0) &&& full64 = tab &&& full64 := by + apply Nat.eq_of_testBit_eq; intro p + rcases Nat.lt_or_ge p 64 with hp | hp + · rw [testBit_and_full hp, testBit_and_full hp] + have hev := evalWiresNat_getD_bit C legal varTabs C.output p hp + have hcmp := computes (row6 p) + unfold Circuit.eval fOf at hcmp + rw [row6_ti ⟨p, hp⟩] at hcmp + have hrm : varTabs.map (fun w => w.testBit p) = List.ofFn (row6 p) := by + have hvl : varTabs.length = 6 := by decide + apply List.ext_getElem + · simp [hvl] + · intro j hj1 hj2 + have hjv : j < varTabs.length := by simp at hj1; exact hj1 + simp only [List.getElem_map, List.getElem_ofFn, row6] + have hge : varTabs[j] = varTabs.getD j 0 := by simp [List.getD, hjv] + rw [hge] + rw [hrm] at hev + exact hev.trans hcmp + · have hf64 : full64 < 2 ^ 64 := by rw [full64_eq]; omega + have hfp : full64.testBit p = false := + Nat.testBit_lt_two_pow (lt_of_lt_of_le hf64 (Nat.pow_le_pow_right (by decide) hp)) + simp [Nat.testBit_and, hfp] + have hn_out : normZ u full64 ((evalWiresNat C.gates varTabs).getD C.output 0) = normZ u full64 tab := by + unfold normZ; rw [hbit] + have ht_out : trivW u full64 ((evalWiresNat C.gates varTabs).getD C.output 0) = false := by + rw [← normZ_trivW u hF, hn_out, normZ_trivW u hF] + cases h : trivW u full64 tab <;> simp_all + have hfold := foldl_to_ValidProg u C.gates legal hno varTabs (varsOf u full64) hws0 C.output + unfold evalWiresNat at hn_out ht_out + rcases hfold with ht | ⟨P, hPlen, hP⟩ + · rw [ht] at ht_out; exact Bool.noConfusion ht_out + · refine ⟨P, ?_, hn_out ▸ hP⟩ + have hs := countP_split C.gates + omega + +def insSort (x : Nat) : List Nat → List Nat + | [] => [x] + | y :: ys => if x < y then x :: y :: ys else if x = y then y :: ys else y :: insSort x ys + +def dedupSort (l : List Nat) : List Nat := + l.foldr insSort [] + +theorem mem_insSort {x z : Nat} {l : List Nat} : z ∈ insSort x l ↔ z = x ∨ z ∈ l := by + induction l with + | nil => simp [insSort] + | cons y ys ih => + unfold insSort + split_ifs with h1 h2 + · simp + · subst h2; simp + · simp only [List.mem_cons, ih]; tauto + +theorem pairwise_insSort {x : Nat} {l : List Nat} (hl : l.Pairwise (· < ·)) : + (insSort x l).Pairwise (· < ·) := by + induction l with + | nil => simp [insSort] + | cons y ys ih => + unfold insSort + split_ifs with h1 h2 + · refine List.Pairwise.cons ?_ hl + intro z hz + rcases List.mem_cons.mp hz with rfl | hzy + · exact h1 + · exact Nat.lt_trans h1 (List.rel_of_pairwise_cons hl hzy) + · exact hl + · have hy := ih (List.pairwise_cons.mp hl).2 + refine List.Pairwise.cons ?_ hy + intro z hz + rw [mem_insSort] at hz + rcases hz with rfl | hzy + · omega + · exact List.rel_of_pairwise_cons hl hzy + +theorem mem_dedupSort {l : List Nat} {x : Nat} : x ∈ dedupSort l ↔ x ∈ l := by + induction l with + | nil => simp [dedupSort] + | cons y ys ih => simp only [dedupSort, List.foldr_cons] at ih ⊢; rw [mem_insSort, ih]; simp + +theorem dedupSort_pairwise (l : List Nat) : (dedupSort l).Pairwise (· < ·) := by + induction l with + | nil => simp [dedupSort] + | cons y ys ih => exact pairwise_insSort ih + +def varsSorted (u : Bool) (Z : Nat) : List Nat := + dedupSort (varsOf u Z) + +def applyStep (S : List Nat) (a b c : Nat) (ka kb : Bool) : List Nat := + insSort c (S.filter (fun w => (w != a || ka) && (w != b || kb))) + +theorem mem_applyStep {S : List Nat} {a b c x : Nat} {ka kb : Bool} : + x ∈ applyStep S a b c ka kb ↔ + x = c ∨ (x ∈ S ∧ (x ≠ a ∨ ka = true) ∧ (x ≠ b ∨ kb = true)) := by + unfold applyStep + rw [mem_insSort, List.mem_filter] + simp only [Bool.and_eq_true, Bool.or_eq_true, bne_iff_ne, ne_eq] + +theorem pairwise_applyStep {S : List Nat} {a b c : Nat} {ka kb : Bool} + (hs : S.Pairwise (· < ·)) : (applyStep S a b c ka kb).Pairwise (· < ·) := + pairwise_insSort (hs.filter _) + +def zCount (S : List Nat) (Z : Nat) : Nat := + (S.filter (fun w => trivW true Z w)).length + +theorem filter_len_split (p : Nat → Bool) (l : List Nat) : + (l.filter p).length + (l.filter (fun x => !p x)).length = l.length := by + induction l with + | nil => simp + | cons x xs ih => + cases hx : p x <;> simp only [List.filter_cons, hx, Bool.not_true, Bool.not_false, + ↓reduceIte, Bool.false_eq_true, List.length_cons] <;> omega + +theorem length_insSort_fresh {x : Nat} {l : List Nat} (hx : x ∉ l) : + (insSort x l).length = l.length + 1 := by + induction l with + | nil => simp [insSort] + | cons y ys ih => + have hxy : x ≠ y := fun h => hx (h ▸ List.mem_cons_self) + have hxys : x ∉ ys := fun h => hx (List.mem_cons_of_mem _ h) + unfold insSort + by_cases h1 : x < y + · simp [h1] + · simp [h1, hxy, ih hxys] + +theorem nodup_of_pairwise {l : List Nat} (hl : l.Pairwise (· < ·)) : l.Nodup := + List.Pairwise.imp (fun h => Nat.ne_of_lt h) hl + +theorem length_filter_erase2 {S : List Nat} (hs : S.Nodup) {a b : Nat} (hab : a ≠ b) + (ha : a ∈ S) (hb : b ∈ S) (ka kb : Bool) : + (S.filter (fun w => (w != a || ka) && (w != b || kb))).length = + S.length - (if !ka then 1 else 0) - (if !kb then 1 else 0) ∧ + (S.filter (fun w => (w != a || ka) && (w != b || kb))).length + 1 ≥ S.length - 1 := by + suffices ∀ (l : List Nat), l.Nodup → + let flt := l.filter (fun w => (w != a || ka) && (w != b || kb)) + flt.length + (if !ka && a ∈ l then 1 else 0) + (if !kb && b ∈ l then 1 else 0) = l.length by + have he := this S hs + simp only [ha, hb, decide_true, Bool.and_true] at he + cases ka <;> cases kb <;> simp at he ⊢ <;> omega + intro l hnd + induction l with + | nil => simp + | cons x xs ih => + have hxnd := List.nodup_cons.mp hnd + have ihl := ih hxnd.2 + simp only [List.filter_cons, List.mem_cons] + by_cases hxa : x = a + · rcases hxa with rfl + have hxb : x ≠ b := hab + have hax : x ∉ xs := hxnd.1 + cases ka <;> simp [hxb, Ne.symm hxb, hax] at ihl ⊢ <;> omega + · by_cases hxb : x = b + · rcases hxb with rfl + have hbx : x ∉ xs := hxnd.1 + cases kb <;> simp [hxa, Ne.symm hxa, hbx] at ihl ⊢ <;> omega + · have hna : (x != a) = true := bne_iff_ne.mpr hxa + have hnb : (x != b) = true := bne_iff_ne.mpr hxb + simp only [hna, hnb, Bool.true_or, Bool.and_true, ↓reduceIte, List.length_cons] + simp only [hxa, Ne.symm hxa, hxb, Ne.symm hxb, false_or] at ihl ⊢ + omega + +theorem zCount_applyStep {S : List Nat} (hs : S.Pairwise (· < ·)) + {a b c Z : Nat} (hab : a < b) (ha : a ∈ S) (hb : b ∈ S) (hc : c ∉ S) (ka kb : Bool) : + zCount (applyStep S a b c ka kb) Z = + zCount S Z - (if !ka && trivW true Z a then 1 else 0) - + (if !kb && trivW true Z b then 1 else 0) + (if trivW true Z c then 1 else 0) := by + have hnd := nodup_of_pairwise hs + unfold zCount applyStep + have hcflt : c ∉ S.filter (fun w => (w != a || ka) && (w != b || kb)) := + fun h => hc (List.mem_filter.mp h).1 + have hlen_ins : ∀ (l : List Nat), c ∉ l → + ((insSort c l).filter (fun w => trivW true Z w)).length = + (l.filter (fun w => trivW true Z w)).length + (if trivW true Z c then 1 else 0) := by + intro l hcl + induction l with + | nil => by_cases hcZ : trivW true Z c = true <;> simp [insSort, hcZ] + | cons y ys ih => + have hcy : c ≠ y := fun h => hcl (h ▸ List.mem_cons_self) + have hcys : c ∉ ys := fun h => hcl (List.mem_cons_of_mem _ h) + by_cases h1 : c < y + · simp only [insSort, h1, ↓reduceIte, List.filter_cons] + cases hcZ : trivW true Z c <;> cases hyZ : trivW true Z y <;> simp [hcZ, hyZ] <;> omega + · simp only [insSort, h1, hcy, ↓reduceIte, List.filter_cons] + have ihl := ih hcys + cases hcZ : trivW true Z c <;> cases hyZ : trivW true Z y <;> simp [hcZ, hyZ] at ihl ⊢ <;> omega + have hbase : ∀ (l : List Nat), l.Nodup → + let p := fun w => trivW true Z w + let l1 := (l.filter (fun w => (w != a || ka) && (w != b || kb))).filter p + l1.length + (if !ka && p a && a ∈ l then 1 else 0) + (if !kb && p b && b ∈ l then 1 else 0) = + (l.filter p).length := by + intro l hl + induction l with + | nil => simp + | cons x xs ih => + have hxnd := List.nodup_cons.mp hl + have ihl := ih hxnd.2 + simp only [List.filter_cons, List.mem_cons] + by_cases hxa : x = a + · rcases hxa with rfl + have hax : x ∉ xs := hxnd.1 + cases ka <;> cases hp : trivW true Z x <;> simp [Nat.ne_of_lt hab, (Nat.ne_of_lt hab).symm, hax, hp] at ihl ⊢ <;> omega + · by_cases hxb : x = b + · rcases hxb with rfl + have hbx : x ∉ xs := hxnd.1 + cases kb <;> cases hp : trivW true Z x <;> simp [hxa, Ne.symm hxa, hbx, hp] at ihl ⊢ <;> omega + · cases hp : trivW true Z x <;> simp [hxa, Ne.symm hxa, hxb, Ne.symm hxb, hp] at ihl ⊢ <;> omega + have hbS := hbase S hnd + simp only [ha, hb, decide_true, Bool.and_true] at hbS + rw [hlen_ins _ hcflt] + cases hc1 : (!ka && trivW true Z a) <;> cases hc2 : (!kb && trivW true Z b) <;> cases hc3 : trivW true Z c <;> + simp [hc1, hc2, hc3] at hbS ⊢ <;> omega + +def isTrivStep (u : Bool) (Z : Nat) (S : List Nat) (a b c : Nat) : Bool := + trivW u Z a || trivW u Z b || trivW u Z c || + eqZ u Z c a || eqZ u Z c b || S.any (fun w => eqZ u Z c w) + +def PORSteps (u : Bool) (Z T : Nat) : + Nat → Nat → Nat → Nat → List Nat → List (Nat × Nat × Nat × Bool × Bool) → Prop + | _, _, _, _, S, [] => S = [T] + | lkOut, lkA, lkB, lkOp, S, (a, b, d, ka, kb) :: rest => + S.Pairwise (· < ·) ∧ + a ∈ S ∧ b ∈ S ∧ a < b ∧ d < opsCount u ∧ + let c := stepVal u Z d a b + !trivW u Z c = true ∧ c ∉ S ∧ + (lkOut = 0 ∨ a = lkOut ∨ b = lkOut ∨ + lkA < a ∨ (lkA = a ∧ (lkB < b ∨ (lkB = b ∧ lkOp < d)))) ∧ + PORSteps u Z T c a b d (applyStep S a b c ka kb) rest + +theorem porSteps_len_bound {u : Bool} {Z T lkOut lkA lkB lkOp : Nat} + {S : List Nat} {steps : List (Nat × Nat × Nat × Bool × Bool)} + (h : PORSteps u Z T lkOut lkA lkB lkOp S steps) : + S.length - 1 ≤ steps.length := by + induction steps generalizing lkOut lkA lkB lkOp S with + | nil => simp [PORSteps] at h; subst h; simp + | cons st rest ih => + rcases st with ⟨a, b, d, ka, kb⟩ + rcases h with ⟨hs, ha, hb, hab, hd, hnt, hc, hpor, hrest⟩ + have ihr := ih hrest + have hlf := (length_filter_erase2 (nodup_of_pairwise hs) (Nat.ne_of_lt hab) ha hb ka kb).2 + have hcf : stepVal u Z d a b ∉ S.filter (fun w => (w != a || ka) && (w != b || kb)) := + fun h => hc (List.mem_filter.mp h).1 + have hlen : (applyStep S a b (stepVal u Z d a b) ka kb).length = + (S.filter (fun w => (w != a || ka) && (w != b || kb))).length + 1 := + length_insSort_fresh hcf + simp only [List.length_cons] at ihr ⊢ + omega + +theorem stepVal_pairFail_step {u : Bool} {Z a b d p q : Nat} (hZ : Z ≤ full64) + (hZp : Z.testBit p = true) (hZq : Z.testBit q = true) (hd : d < opsCount u) + (hpq : if u then (a.testBit p == a.testBit q) && (b.testBit p == b.testBit q) + else (!(a.testBit p) || a.testBit q) && (!(b.testBit p) || b.testBit q)) : + let c := stepVal u Z d a b + if u then c.testBit p == c.testBit q else !(c.testBit p) || c.testBit q := by + have hp64 : p < 64 := by + by_contra hc; have hZ64 : Z < 2 ^ 64 := by rw [full64_eq] at hZ; omega + have := Nat.testBit_lt_two_pow (lt_of_lt_of_le hZ64 (Nat.pow_le_pow_right (by decide) (by omega : 64 ≤ p))) + rw [this] at hZp; exact Bool.noConfusion hZp + have hq64 : q < 64 := by + by_contra hc; have hZ64 : Z < 2 ^ 64 := by rw [full64_eq] at hZ; omega + have := Nat.testBit_lt_two_pow (lt_of_lt_of_le hZ64 (Nat.pow_le_pow_right (by decide) (by omega : 64 ≤ q))) + rw [this] at hZq; exact Bool.noConfusion hZq + unfold stepVal normZ + cases hu : u <;> simp only [hu] at hpq hd ⊢ + · simp only [Bool.false_eq_true, ite_false, Nat.testBit_and, hZp, hZq, Bool.and_true, + testBit_opU false d a b p hp64, testBit_opU false d a b q hq64] + have h01 : d = 0 ∨ d = 1 := by simp [opsCount, hu] at hd; omega + rcases h01 with rfl | rfl <;> revert hpq <;> cases a.testBit p <;> cases a.testBit q <;> + cases b.testBit p <;> cases b.testBit q <;> decide + · simp only [ite_true] + have h4 : d = 0 ∨ d = 1 ∨ d = 2 ∨ d = 3 := by simp [opsCount, hu] at hd; omega + have hap : (opU true d a b).testBit p = (opU true d a b).testBit q := by + rw [testBit_opU true d a b p hp64, testBit_opU true d a b q hq64] + rcases h4 with rfl | rfl | rfl | rfl <;> revert hpq <;> + cases a.testBit p <;> cases a.testBit q <;> cases b.testBit p <;> cases b.testBit q <;> decide + rcases minComp_eq_or (and_idem_r (opU true d a b) Z) with hm | hm <;> rw [hm] + · simp [Nat.testBit_and, hZp, hZq, hap] + · simp [Nat.testBit_xor, Nat.testBit_and, hZp, hZq, hap] + +theorem porSteps_pairFail {u : Bool} {Z T tab p q lkOut lkA lkB lkOp : Nat} + {S : List Nat} {steps : List (Nat × Nat × Nat × Bool × Bool)} + (hZ : Z ≤ full64) (hT : T = normZ u Z tab) + (hZp : Z.testBit p = true) (hZq : Z.testBit q = true) + (htp : tab.testBit p = true) (htq : tab.testBit q = false) + (hpf : pairFail u S p q = true) : + ¬ PORSteps u Z T lkOut lkA lkB lkOp S steps := by + intro hpor + induction steps generalizing lkOut lkA lkB lkOp S with + | nil => + simp [PORSteps] at hpor; subst hpor + unfold pairFail at hpf + cases hu : u + · simp only [hu, List.all_cons, List.all_nil, Bool.and_true] at hpf + have hb : T.testBit p = false ∨ T.testBit q = true := by + cases h1 : T.testBit p <;> cases h2 : T.testBit q <;> simp_all + unfold normZ at hT; simp only [hu, Bool.false_eq_true, ite_false] at hT + rw [hT, Nat.testBit_and, Nat.testBit_and, hZp, hZq, htp, htq] at hb + simp at hb + · simp only [hu, List.all_cons, List.all_nil, Bool.and_true, beq_iff_eq] at hpf + unfold normZ at hT; simp only [hu, ite_true] at hT + rcases minComp_eq_or (and_idem_r tab Z) with hm | hm <;> rw [hm] at hT + · rw [hT, Nat.testBit_and, Nat.testBit_and, hZp, hZq, htp, htq] at hpf; simp at hpf + · rw [hT, Nat.testBit_xor, Nat.testBit_and, Nat.testBit_xor, Nat.testBit_and, hZp, hZq, htp, htq] at hpf + simp at hpf + | cons st rest ih => + rcases st with ⟨a, b, d, ka, kb⟩ + rcases hpor with ⟨_, ha, hb, _, hd, _, _, _, hrest⟩ + apply ih _ hrest + cases hu : u + · simp only [pairFail, hu, Bool.false_eq_true, ↓reduceIte, List.all_eq_true] at hpf ⊢ + intro x hx + rw [mem_applyStep] at hx + rcases hx with rfl | ⟨hxS, _⟩ + · have hstep := stepVal_pairFail_step hZ hZp hZq hd (by + simp only [hu, Bool.false_eq_true, ite_false, Bool.and_eq_true]; exact ⟨hpf a ha, hpf b hb⟩) + simpa [hu] using hstep + · exact hpf x hxS + · simp only [pairFail, hu, ↓reduceIte, List.all_eq_true] at hpf ⊢ + intro x hx + rw [mem_applyStep] at hx + rcases hx with rfl | ⟨hxS, _⟩ + · have hstep := stepVal_pairFail_step hZ hZp hZq hd (by + simp only [hu, ite_true, Bool.and_eq_true]; exact ⟨hpf a ha, hpf b hb⟩) + simpa [hu] using hstep + · exact hpf x hxS + +def countNZSteps (u : Bool) (Z Zr : Nat) : + List Nat → List (Nat × Nat × Nat × Bool × Bool) → Nat + | _, [] => 0 + | S, (a, b, d, ka, kb) :: rest => + let c := stepVal u Z d a b + (if isTrivStep u Zr S a b c then 0 else 1) + + countNZSteps u Z Zr (applyStep S a b c ka kb) rest + +def countTrivSteps (u : Bool) (Z Zr : Nat) : + List Nat → List (Nat × Nat × Nat × Bool × Bool) → Nat + | _, [] => 0 + | S, (a, b, d, ka, kb) :: rest => + let c := stepVal u Z d a b + (if isTrivStep u Zr S a b c then 1 else 0) + + countTrivSteps u Z Zr (applyStep S a b c ka kb) rest + +theorem count_split_steps (u : Bool) (Z Zr : Nat) (S : List Nat) (steps : List (Nat × Nat × Nat × Bool × Bool)) : + countNZSteps u Z Zr S steps + countTrivSteps u Z Zr S steps = steps.length := by + induction steps generalizing S with + | nil => simp [countNZSteps, countTrivSteps] + | cons st rest ih => + rcases st with ⟨a, b, d, ka, kb⟩ + simp only [countNZSteps, countTrivSteps, List.length_cons] + have h := ih (applyStep S a b (stepVal u Z d a b) ka kb) + cases isTrivStep u Zr S a b (stepVal u Z d a b) <;> simp <;> omega + +theorem zCount_le_trivSteps {u : Bool} {Z Zr T lkOut lkA lkB lkOp : Nat} + (hZr : Zr ≤ full64) (hsubZ : Zr &&& Z = Zr) + (hnt : !trivW u Zr T = true) + {S : List Nat} {steps : List (Nat × Nat × Nat × Bool × Bool)} + (hpor : PORSteps u Z T lkOut lkA lkB lkOp S steps) : + zCount S Zr ≤ countTrivSteps u Z Zr S steps := by + induction steps generalizing lkOut lkA lkB lkOp S with + | nil => + simp [PORSteps] at hpor; subst hpor + have htr : trivW true Zr T = false := by + change (!trivW true Zr T) = true at hnt + cases h : trivW true Zr T <;> simp_all + simp [zCount, countTrivSteps, htr] + | cons st rest ih => + rcases st with ⟨a, b, d, ka, kb⟩ + rcases hpor with ⟨hs, ha, hb, hab, hd, _, hc, _, hrest⟩ + have ihr := ih hrest + have hzc := zCount_applyStep hs (Z := Zr) hab ha hb hc ka kb + simp only [countTrivSteps] + cases htr : isTrivStep u Zr S a b (stepVal u Z d a b) + · have hta : trivW u Zr a = false := by + by_contra h + have h1 : trivW u Zr a = true := Bool.eq_true_of_not_eq_false h + have htr1 : isTrivStep u Zr S a b (stepVal u Z d a b) = true := by simp [isTrivStep, h1] + rw [htr1] at htr; exact Bool.noConfusion htr + have htb : trivW u Zr b = false := by + by_contra h + have h1 : trivW u Zr b = true := Bool.eq_true_of_not_eq_false h + have htr1 : isTrivStep u Zr S a b (stepVal u Z d a b) = true := by simp [isTrivStep, h1] + rw [htr1] at htr; exact Bool.noConfusion htr + have htc : trivW u Zr (stepVal u Z d a b) = false := by + by_contra h + have h1 : trivW u Zr (stepVal u Z d a b) = true := Bool.eq_true_of_not_eq_false h + have htr1 : isTrivStep u Zr S a b (stepVal u Z d a b) = true := by simp [isTrivStep, h1] + rw [htr1] at htr; exact Bool.noConfusion htr + have hta1 : trivW true Zr a = false := by simpa [trivW] using hta + have htb1 : trivW true Zr b = false := by simpa [trivW] using htb + have htc1 : trivW true Zr (stepVal u Z d a b) = false := by simpa [trivW] using htc + simp only [hta1, htb1, htc1, Bool.and_false, ↓reduceIte, Nat.sub_zero, Nat.add_zero] at hzc + omega + · simp only [↓reduceIte] + have hboth : trivW true Zr a = true → trivW true Zr b = true → + trivW true Zr (stepVal u Z d a b) = true := by + intro ha_tr hb_tr + have ha_u : trivW u Zr a = true := by simpa [trivW] using ha_tr + have hnorm : normZ u Zr (stepVal u Z d a b) = stepVal u Zr d a b := by + unfold stepVal; exact normZ_idem u hZr hsubZ + have heq_u : trivW true Zr (stepVal u Z d a b) = trivW u Zr (stepVal u Zr d a b) := by + change trivW u Zr (stepVal u Z d a b) = trivW u Zr (stepVal u Zr d a b) + rw [← normZ_trivW u hZr (w := stepVal u Z d a b), hnorm] + rw [heq_u] + rcases stepVal_triv_in hZr hd (Or.inl ha_u) (b := b) with ht | he | he + · exact ht + · rw [eqZ_iff u hZr, normZ_stepVal hZr] at he + rw [he, normZ_trivW u hZr]; exact ha_u + · rw [eqZ_iff u hZr, normZ_stepVal hZr] at he + rw [he, normZ_trivW u hZr]; simpa [trivW] using hb_tr + cases ha_t : trivW true Zr a <;> cases hb_t : trivW true Zr b <;> + cases hc_t : trivW true Zr (stepVal u Z d a b) + all_goals + try (have hcc := hboth ha_t hb_t; rw [hcc] at hc_t; exact Bool.noConfusion hc_t) + all_goals + (cases ka <;> cases kb <;> simp [ha_t, hb_t, hc_t] at hzc <;> omega) + +theorem validProg_proj_from_por {u : Bool} {Z Zr T lkOut lkA lkB lkOp : Nat} + (hZr : Zr ≤ full64) (hZ : Z ≤ full64) (hsubZ : Zr &&& Z = Zr) + {S S_pool : List Nat} (hsub : ∀ w ∈ S, !trivW u Zr w = true → normZ u Zr w ∈ S_pool) + {steps : List (Nat × Nat × Nat × Bool × Bool)} + (hpor : PORSteps u Z T lkOut lkA lkB lkOp S steps) + (hntT : !trivW u Zr T = true) : + ∃ P' : List (Nat × Nat × Nat), + P'.length ≤ countNZSteps u Z Zr S steps ∧ + ValidProg u Zr S_pool P' (normZ u Zr T) := by + induction steps generalizing lkOut lkA lkB lkOp S S_pool with + | nil => + simp [PORSteps] at hpor; subst hpor + exact ⟨[], by simp [countNZSteps], hsub T List.mem_cons_self hntT⟩ + | cons st rest ih => + rcases st with ⟨a, b, d, ka, kb⟩ + rcases hpor with ⟨_, ha, hb, _, hd, _, _, _, hrest⟩ + let c := stepVal u Z d a b + have hc_norm : normZ u Zr c = stepVal u Zr d a b := by + unfold c stepVal; exact normZ_idem u hZr hsubZ + simp only [countNZSteps] + cases htr : isTrivStep u Zr S a b c + · simp only [Bool.false_eq_true, ↓reduceIte] + have hta : !trivW u Zr a = true := by cases h : trivW u Zr a <;> simp_all [isTrivStep] + have htb : !trivW u Zr b = true := by cases h : trivW u Zr b <;> simp_all [isTrivStep] + obtain ⟨d', hd', hstp⟩ := stepVal_norm_inputs hZr hZr (Nat.and_self Zr) hd (a := a) (b := b) + have hnc : normZ u Zr c = stepVal u Zr d' (normZ u Zr a) (normZ u Zr b) := hc_norm.trans hstp + have haP : normZ u Zr a ∈ S_pool := hsub a ha hta + have hbP : normZ u Zr b ∈ S_pool := hsub b hb htb + obtain ⟨P', hPlen, hP⟩ := ih (S_pool := normZ u Zr c :: S_pool) (by + intro w hw hwnt + rw [mem_applyStep] at hw + rcases hw with rfl | ⟨hwS, _⟩ + · exact List.mem_cons_self + · exact List.mem_cons_of_mem _ (hsub w hwS hwnt)) hrest + refine ⟨(normZ u Zr a, normZ u Zr b, d') :: P', by simp; omega, ⟨haP, hbP, hd', ?_⟩⟩ + simpa [← hnc] using hP + · simp only [↓reduceIte, Nat.zero_add] + have hsame : ∀ w ∈ applyStep S a b c ka kb, !trivW u Zr w = true → normZ u Zr w ∈ S_pool := by + intro w hw hwnt + rw [mem_applyStep] at hw + rcases hw with rfl | ⟨hwS, _⟩ + · unfold isTrivStep at htr + simp only [Bool.or_eq_true, List.any_eq_true] at htr + rcases htr with (((((ht | ht) | ht) | heq) | heq) | ⟨u1, hu1, heq⟩) + · rcases stepVal_triv_in hZr hd (Or.inl ht) with hc0 | heq | heq + · rw [← hc_norm, normZ_trivW u hZr] at hc0; rw [hc0] at hwnt; exact absurd hwnt (by decide) + · rw [eqZ_iff u hZr, normZ_stepVal hZr] at heq + rw [← hc_norm] at heq; rw [heq]; apply hsub a ha + rw [← normZ_trivW u hZr (w := a), ← heq, normZ_trivW u hZr]; exact hwnt + · rw [eqZ_iff u hZr, normZ_stepVal hZr] at heq + rw [← hc_norm] at heq; rw [heq]; apply hsub b hb + rw [← normZ_trivW u hZr (w := b), ← heq, normZ_trivW u hZr]; exact hwnt + · rcases stepVal_triv_in hZr hd (Or.inr ht) with hc0 | heq | heq + · rw [← hc_norm, normZ_trivW u hZr] at hc0; rw [hc0] at hwnt; exact absurd hwnt (by decide) + · rw [eqZ_iff u hZr, normZ_stepVal hZr] at heq + rw [← hc_norm] at heq; rw [heq]; apply hsub a ha + rw [← normZ_trivW u hZr (w := a), ← heq, normZ_trivW u hZr]; exact hwnt + · rw [eqZ_iff u hZr, normZ_stepVal hZr] at heq + rw [← hc_norm] at heq; rw [heq]; apply hsub b hb + rw [← normZ_trivW u hZr (w := b), ← heq, normZ_trivW u hZr]; exact hwnt + · rw [ht] at hwnt; exact absurd hwnt (by decide) + · rw [eqZ_iff u hZr] at heq; rw [heq]; apply hsub a ha + rw [← normZ_trivW u hZr, ← heq, normZ_trivW u hZr]; exact hwnt + · rw [eqZ_iff u hZr] at heq; rw [heq]; apply hsub b hb + rw [← normZ_trivW u hZr, ← heq, normZ_trivW u hZr]; exact hwnt + · rw [eqZ_iff u hZr] at heq; rw [heq]; apply hsub u1 hu1 + rw [← normZ_trivW u hZr, ← heq, normZ_trivW u hZr]; exact hwnt + · exact hsub w hwS hwnt + obtain ⟨P', hPlen, hP⟩ := ih hsame hrest + exact ⟨P', by simpa [c] using hPlen, hP⟩ + +theorem zCount_varsSorted_sub {u : Bool} {Zr : Nat} (S₀ : List Nat) (r : Nat) : + zCount S₀ Zr = zCount S₀ Zr := rfl + +def stepLess (g1 g2 : Nat × Nat × Nat) : Bool := + g1.1 < g2.1 || (g1.1 == g2.1 && (g1.2.1 < g2.2.1 || (g1.2.1 == g2.2.1 && g1.2.2 < g2.2.2))) + +def CleanProg (u : Bool) (Z : Nat) : List Nat → List (Nat × Nat × Nat) → Nat → Prop + | S, [], T => T ∈ S + | S, (a, b, d) :: rest, T => + a ∈ S ∧ b ∈ S ∧ a < b ∧ d < opsCount u ∧ + let c := stepVal u Z d a b + !trivW u Z c = true ∧ c ∉ S ∧ CleanProg u Z (c :: S) rest T + +theorem cleanProg_to_validProg {u : Bool} {Z : Nat} {S : List Nat} {P : List (Nat × Nat × Nat)} {T : Nat} + (hc : CleanProg u Z S P T) : ValidProg u Z S P T := by + induction P generalizing S with + | nil => exact hc + | cons st rest ih => + rcases st with ⟨a, b, d⟩ + rcases hc with ⟨ha, hb, _, hd, _, _, hrest⟩ + exact ⟨ha, hb, hd, ih hrest⟩ + +theorem validProg_to_clean {u : Bool} {Z : Nat} (hZ : Z ≤ full64) {S_orig S_clean : List Nat} + (hS : ∀ w ∈ S_orig, trivW u Z w = true ∨ w ∈ S_clean) + (hS_norm : ∀ w ∈ S_clean, normZ u Z w = w ∧ !trivW u Z w = true) + {P : List (Nat × Nat × Nat)} {T : Nat} (hntT : !trivW u Z T = true) (hnT : normZ u Z T = T) + (hv : ValidProg u Z S_orig P T) : + ∃ P' : List (Nat × Nat × Nat), P'.length ≤ P.length ∧ CleanProg u Z S_clean P' T := by + induction P generalizing S_orig S_clean with + | nil => + simp [ValidProg] at hv + rcases hS T hv with ht | hm + · rw [ht] at hntT; exact absurd hntT (by decide) + · exact ⟨[], Nat.le_refl _, hm⟩ + | cons st rest ih => + rcases st with ⟨a, b, d⟩ + rcases hv with ⟨ha, hb, hd, hrest⟩ + let c := stepVal u Z d a b + have hnorm_c : normZ u Z c = c := normZ_stepVal hZ + have skip_step : (trivW u Z c = true ∨ c ∈ S_clean) → + ∃ P', P'.length ≤ rest.length + 1 ∧ CleanProg u Z S_clean P' T := by + intro hc_skip + have hS2 : ∀ w ∈ c :: S_orig, trivW u Z w = true ∨ w ∈ S_clean := by + intro w hw; simp only [List.mem_cons] at hw + rcases hw with rfl | hw + · exact hc_skip + · exact hS w hw + obtain ⟨P', hPlen, hP⟩ := ih hS2 hS_norm hrest + exact ⟨P', Nat.le_succ_of_le hPlen, hP⟩ + rcases hS a ha with hta | haC + · apply skip_step + rcases stepVal_triv_in hZ hd (Or.inl hta) with htc | he | he + · left; exact htc + · rw [eqZ_iff u hZ, hnorm_c] at he + rcases hS a ha with hta2 | haC2 + · left; rw [he, normZ_trivW u hZ]; exact hta2 + · right; rw [he, (hS_norm a haC2).1]; exact haC2 + · rw [eqZ_iff u hZ, hnorm_c] at he + rcases hS b hb with htb2 | hbC2 + · left; rw [he, normZ_trivW u hZ]; exact htb2 + · right; rw [he, (hS_norm b hbC2).1]; exact hbC2 + · rcases hS b hb with htb | hbC + · apply skip_step + rcases stepVal_triv_in hZ hd (Or.inr htb) with htc | he | he + · left; exact htc + · right; rw [eqZ_iff u hZ, hnorm_c] at he; rw [he, (hS_norm a haC).1]; exact haC + · rw [eqZ_iff u hZ, hnorm_c] at he + rcases hS b hb with htb2 | hbC2 + · left; rw [he, normZ_trivW u hZ]; exact htb2 + · right; rw [he, (hS_norm b hbC2).1]; exact hbC2 + · by_cases hab_eq : a = b + · subst hab_eq + apply skip_step + cases u + · have h01 : d = 0 ∨ d = 1 := by unfold opsCount at hd; dsimp at hd; omega + rcases h01 with rfl | rfl + · right; have he : c = normZ false Z a := by + unfold c stepVal normZ opU + have haeq : (a &&& a) &&& Z = a &&& Z := by + apply Nat.eq_of_testBit_eq; intro p; simp [Nat.testBit_and] + simp [haeq] + rw [he, (hS_norm a haC).1]; exact haC + · right; have he : c = normZ false Z a := by + unfold c stepVal normZ opU + have haeq : (a ||| a) &&& Z = a &&& Z := by + apply Nat.eq_of_testBit_eq; intro p; simp [Nat.testBit_and, Nat.testBit_or] + simp [haeq] + rw [he, (hS_norm a haC).1]; exact haC + · have h4 : d = 0 ∨ d = 1 ∨ d = 2 ∨ d = 3 := by unfold opsCount at hd; dsimp at hd; omega + rcases h4 with rfl | rfl | rfl | rfl + · right; have he : c = normZ true Z a := by + unfold c stepVal normZ opU + have haeq : (a &&& a) &&& Z = a &&& Z := by + apply Nat.eq_of_testBit_eq; intro p; simp [Nat.testBit_and] + simp [haeq] + rw [he, (hS_norm a haC).1]; exact haC + · right; have he : c = normZ true Z a := by + unfold c stepVal normZ opU + have haeq : (a ||| a) &&& Z = a &&& Z := by + apply Nat.eq_of_testBit_eq; intro p; simp [Nat.testBit_and, Nat.testBit_or] + simp [haeq] + rw [he, (hS_norm a haC).1]; exact haC + · left + have hz : opU true 2 a a &&& Z = 0 := by + unfold opU; simp only [ite_true] + apply Nat.eq_of_testBit_eq; intro p; rcases Nat.lt_or_ge p 64 with hp | hp + · simp [Nat.testBit_and, testBit_xor_full hp] + · have hZ64 : Z < 2 ^ 64 := by rw [full64_eq] at hZ; omega + have hZp := Nat.testBit_lt_two_pow (lt_of_lt_of_le hZ64 (Nat.pow_le_pow_right (by decide) hp)) + simp [Nat.testBit_and, hZp] + have hm0 : minComp Z 0 = 0 := by unfold minComp; simp + unfold c stepVal normZ trivW; simp [hz, hm0] + · left + have hz : opU true 3 a a &&& Z = 0 := by + unfold opU + apply Nat.eq_of_testBit_eq; intro p; rcases Nat.lt_or_ge p 64 with hp | hp + · simp [Nat.testBit_and, testBit_xor_full hp] + · have hZ64 : Z < 2 ^ 64 := by rw [full64_eq] at hZ; omega + have hZp := Nat.testBit_lt_two_pow (lt_of_lt_of_le hZ64 (Nat.pow_le_pow_right (by decide) hp)) + simp [Nat.testBit_and, hZp] + have hm0 : minComp Z 0 = 0 := by unfold minComp; simp + unfold c stepVal normZ trivW; simp [hz, hm0] + · cases htc : trivW u Z c + · by_cases hcm : c ∈ S_clean + · exact skip_step (Or.inr hcm) + · have hS2 : ∀ w ∈ c :: S_orig, trivW u Z w = true ∨ w ∈ c :: S_clean := by + intro w hw; simp only [List.mem_cons] at hw + rcases hw with rfl | hw + · right; exact List.mem_cons_self + · rcases hS w hw with ht | hm + · left; exact ht + · right; exact List.mem_cons_of_mem _ hm + have hS2_norm : ∀ w ∈ c :: S_clean, normZ u Z w = w ∧ !trivW u Z w = true := by + intro w hw; simp only [List.mem_cons] at hw + rcases hw with rfl | hw + · exact ⟨hnorm_c, by simp [htc]⟩ + · exact hS_norm w hw + obtain ⟨P', hPlen, hP⟩ := ih hS2 hS2_norm hrest + rcases Nat.lt_or_ge a b with hab_lt | hab_gt + · refine ⟨(a, b, d) :: P', Nat.succ_le_succ hPlen, haC, hbC, hab_lt, hd, by show !trivW u Z c = true; simp [htc], hcm, hP⟩ + · have hba_lt : b < a := by omega + have hfl : stepVal u Z (flipOp u d) b a = c := (stepVal_flip hZ hd).symm + refine ⟨(b, a, flipOp u d) :: P', Nat.succ_le_succ hPlen, + hbC, haC, hba_lt, flipOp_lt u hd, by show !trivW u Z (stepVal u Z (flipOp u d) b a) = true; simp [hfl, htc], by simp [hfl, hcm], ?_⟩ + simpa [hfl] using hP + · exact skip_step (Or.inl htc) + +def isUsed (w T : Nat) : List (Nat × Nat × Nat) → Bool + | [] => w == T + | (a, b, _) :: rest => a == w || b == w || isUsed w T rest + +def dropUnused1 (u : Bool) (Z T : Nat) : List (Nat × Nat × Nat) → Option (List (Nat × Nat × Nat)) + | [] => none + | (a, b, d) :: rest => + if rest.isEmpty then none + else if !isUsed (stepVal u Z d a b) T rest then some rest + else (dropUnused1 u Z T rest).map (fun r => (a, b, d) :: r) + +def chopAtT (u : Bool) (Z T : Nat) : List (Nat × Nat × Nat) → List (Nat × Nat × Nat) + | [] => [] + | (a, b, d) :: rest => + if stepVal u Z d a b == T then [(a, b, d)] else (a, b, d) :: chopAtT u Z T rest + +def lastIsT (u : Bool) (Z T : Nat) : List (Nat × Nat × Nat) → Prop + | [] => False + | [(a, b, d)] => stepVal u Z d a b = T + | (a, b, d) :: rest => stepVal u Z d a b ≠ T ∧ lastIsT u Z T rest + +theorem chopAtT_clean {u : Bool} {Z T : Nat} {S : List Nat} {P : List (Nat × Nat × Nat)} + (hTS : T ∉ S) (hc : CleanProg u Z S P T) : + let Pc := chopAtT u Z T P + Pc.length ≤ P.length ∧ CleanProg u Z S Pc T ∧ lastIsT u Z T Pc := by + induction P generalizing S with + | nil => simp [CleanProg] at hc; exact absurd hc hTS + | cons st rest ih => + rcases st with ⟨a, b, d⟩ + rcases hc with ⟨ha, hb, hab, hd, hnt, hcm, hrest⟩ + let c := stepVal u Z d a b + simp only [chopAtT] + by_cases hcT : (stepVal u Z d a b == T) = true + · have hcTeq : c = T := Nat.eq_of_beq_eq_true hcT + simp only [hcT, ↓reduceIte, List.length_singleton, lastIsT] + exact ⟨by simp, ⟨ha, hb, hab, hd, hnt, hcm, hcTeq ▸ List.mem_cons_self⟩, hcTeq⟩ + · simp only [hcT, Bool.false_eq_true, ↓reduceIte] + have hcNe : c ≠ T := fun h => hcT (by simp [c, h]) + have hTS2 : T ∉ c :: S := by + intro h; rcases List.mem_cons.mp h with rfl | h + · exact hcNe rfl + · exact hTS h + obtain ⟨hl, hcl, hlt⟩ := ih hTS2 hrest + refine ⟨Nat.succ_le_succ hl, ⟨ha, hb, hab, hd, hnt, hcm, hcl⟩, ?_⟩ + cases hcp : chopAtT u Z T rest with + | nil => simp [hcp, lastIsT] at hlt + | cons _ _ => simp only [lastIsT]; rw [← hcp]; exact ⟨hcNe, hlt⟩ + +def CleanPoolEq (u : Bool) (Z T : Nat) (S : List Nat) (P : List (Nat × Nat × Nat)) : Prop := + CleanProg u Z S P T ∧ lastIsT u Z T P + +theorem dropUnused1_preserves {u : Bool} {Z T : Nat} {S : List Nat} {P P' : List (Nat × Nat × Nat)} + (hc : CleanProg u Z S P T) (hl : lastIsT u Z T P) (hd1 : dropUnused1 u Z T P = some P') : + P'.length + 1 = P.length ∧ CleanProg u Z S P' T ∧ lastIsT u Z T P' := by + induction P generalizing S P' with + | nil => simp [dropUnused1] at hd1 + | cons st rest ih => + rcases st with ⟨a, b, d⟩ + rcases hc with ⟨ha, hb, hab, hd, hnt, hcm, hrest⟩ + unfold dropUnused1 at hd1 + split_ifs at hd1 with h1 h2 + · injection hd1 with hP' + subst hP' + have hl_rest : lastIsT u Z T rest := by + cases hrc : rest with + | nil => simp [hrc] at h1 + | cons _ _ => rw [hrc] at hl; simp only [lastIsT] at hl; exact hl.2 + suffices ∀ (P0 : List (Nat × Nat × Nat)) (S0 : List Nat) (x : Nat), + x ∉ S0 → !isUsed x T P0 = true → CleanProg u Z (x :: S0) P0 T → + CleanProg u Z S0 P0 T by + exact ⟨rfl, this rest S (stepVal u Z d a b) hcm h2 hrest, hl_rest⟩ + intro P0 + induction P0 with + | nil => + intro S0 x hxs hnu hcl + simp [isUsed] at hnu + simp [CleanProg] at hcl ⊢ + rcases hcl with rfl | h + · exact absurd rfl hnu + · exact h + | cons g rs ihr => + intro S0 x hxs hnu hcl + rcases g with ⟨ga, gb, gd⟩ + simp only [isUsed, Bool.not_eq_true', decide_eq_false_iff_not, Bool.or_eq_true, beq_iff_eq, not_or] at hnu + rcases hcl with ⟨hga, hgb, hgab, hgd, hgnt, hgc, hgr⟩ + simp only [List.mem_cons] at hga hgb + have hx_ne_a : ga ≠ x := hnu.1.1 + have hx_ne_b : gb ≠ x := hnu.1.2 + have hsw : ∀ w, w ∈ stepVal u Z gd ga gb :: x :: S0 ↔ w ∈ x :: stepVal u Z gd ga gb :: S0 := by + intro w; simp [List.mem_cons]; tauto + refine ⟨hga.resolve_left hx_ne_a, hgb.resolve_left hx_ne_b, hgab, hgd, hgnt, + fun h => hgc (List.mem_cons_of_mem _ h), ihr (stepVal u Z gd ga gb :: S0) x + (fun h => by rcases List.mem_cons.mp h with he | he; exact hgc (he ▸ List.mem_cons_self); exact hxs he) + (by simp [Bool.eq_false_iff.mpr hnu.2]) (by + suffices ∀ (Q : List (Nat × Nat × Nat)) (A B : List Nat), (∀ w, w ∈ A ↔ w ∈ B) → + CleanProg u Z A Q T → CleanProg u Z B Q T from + this rs _ _ hsw hgr + intro Q + induction Q with + | nil => intro A B hAB hA; exact (hAB T).mp hA + | cons q qs ihq => + rcases q with ⟨qa, qb, qd⟩ + intro A B hAB ⟨qaA, qbA, qab, qd', qnt, qcm, qr⟩ + exact ⟨(hAB qa).mp qaA, (hAB qb).mp qbA, qab, qd', qnt, + fun h => qcm ((hAB _).mpr h), + ihq _ _ (fun w => by simp [List.mem_cons, hAB w]) qr⟩)⟩ + · rcases hopt : dropUnused1 u Z T rest with _ | r' + · rw [hopt] at hd1; simp at hd1 + · rw [hopt] at hd1; injection hd1 with hP'; subst hP' + have hl_rest : lastIsT u Z T rest := by + cases hrc : rest with + | nil => simp [hrc] at h1 + | cons _ _ => rw [hrc] at hl; simp only [lastIsT] at hl; exact hl.2 + obtain ⟨hln, hrc, hrl⟩ := ih hrest hl_rest hopt + have hr'_ne : r' ≠ [] := by + intro he; subst he; simp [lastIsT] at hrl + refine ⟨by simp; omega, ⟨ha, hb, hab, hd, hnt, hcm, hrc⟩, ?_⟩ + cases hrc2 : r' with + | nil => exact absurd hrc2 hr'_ne + | cons _ _ => + rw [← hrc2]; simp only [lastIsT] + refine ⟨?_, hrl⟩ + cases hrest2 : rest with + | nil => simp [hrest2] at h1 + | cons _ _ => rw [hrest2] at hl; simp only [lastIsT] at hl; exact hl.1 + +def pruneUnusedLoop (u : Bool) (Z T : Nat) : Nat → List (Nat × Nat × Nat) → List (Nat × Nat × Nat) + | 0, P => P + | n + 1, P => match dropUnused1 u Z T P with | none => P | some P' => pruneUnusedLoop u Z T n P' + +theorem pruneUnusedLoop_spec {u : Bool} {Z T : Nat} {S : List Nat} : + ∀ (n : Nat) (P : List (Nat × Nat × Nat)), + P.length ≤ n → CleanProg u Z S P T → lastIsT u Z T P → + let P' := pruneUnusedLoop u Z T n P + P'.length ≤ P.length ∧ CleanProg u Z S P' T ∧ lastIsT u Z T P' ∧ dropUnused1 u Z T P' = none := by + intro n + induction n with + | zero => + intro P hlen hc hl + have : P = [] := List.length_eq_zero_iff.mp (Nat.le_zero.mp hlen) + subst this; simp [lastIsT] at hl + | succ m ih => + intro P hlen hc hl + simp only [pruneUnusedLoop] + cases hopt : dropUnused1 u Z T P with + | none => exact ⟨Nat.le_refl _, hc, hl, hopt⟩ + | some P1 => + dsimp only + obtain ⟨hln, hc1, hl1⟩ := dropUnused1_preserves hc hl hopt + obtain ⟨hl2, hc2, hl2', hd2⟩ := ih P1 (by omega) hc1 hl1 + exact ⟨by omega, hc2, hl2', hd2⟩ + +def AllUsed (u : Bool) (Z T : Nat) : List (Nat × Nat × Nat) → Prop + | [] => True + | (a, b, d) :: rest => (rest = [] ∨ isUsed (stepVal u Z d a b) T rest = true) ∧ AllUsed u Z T rest + +theorem dropUnused1_none_allUsed {u : Bool} {Z T : Nat} (P : List (Nat × Nat × Nat)) + (h : dropUnused1 u Z T P = none) : AllUsed u Z T P := by + induction P with + | nil => simp [AllUsed] + | cons st rest ih => + rcases st with ⟨a, b, d⟩ + unfold dropUnused1 at h + split_ifs at h with h1 h2 + · exact ⟨Or.inl (List.isEmpty_iff.mp h1), ih (by simp [List.isEmpty_iff.mp h1, dropUnused1])⟩ + · have hrest_none : dropUnused1 u Z T rest = none := by + cases hopt : dropUnused1 u Z T rest <;> simp_all + exact ⟨Or.inr (by cases hv : isUsed (stepVal u Z d a b) T rest <;> simp_all), ih hrest_none⟩ + +def porSwap1 (u : Bool) (Z : Nat) : List (Nat × Nat × Nat) → Option (List (Nat × Nat × Nat)) + | [] => none + | [_] => none + | g1 :: g2 :: rest => + let c1 := stepVal u Z g1.2.2 g1.1 g1.2.1 + if g2.1 != c1 && g2.2.1 != c1 && stepLess g2 g1 then + some (g2 :: g1 :: rest) + else + (porSwap1 u Z (g2 :: rest)).map (fun r => g1 :: r) + +def invsAhead (g : Nat × Nat × Nat) : List (Nat × Nat × Nat) → Nat + | [] => 0 + | h :: hs => (if stepLess h g then 1 else 0) + invsAhead g hs + +def invCount : List (Nat × Nat × Nat) → Nat + | [] => 0 + | g :: gs => invsAhead g gs + invCount gs + +theorem invCount_le_sq (P : List (Nat × Nat × Nat)) : invCount P ≤ P.length * P.length := by + have hi : ∀ g l, invsAhead g l ≤ l.length := by + intro g l; induction l with + | nil => simp [invsAhead] + | cons x xs ih => simp [invsAhead]; split_ifs <;> omega + induction P with + | nil => simp [invCount] + | cons x xs ih => + simp only [invCount, List.length_cons] + have h1 := hi x xs + nlinarith + +theorem invsAhead_cons_comm (g x y : Nat × Nat × Nat) (l : List (Nat × Nat × Nat)) : + invsAhead g (x :: y :: l) = invsAhead g (y :: x :: l) := by + simp [invsAhead]; omega + +theorem stepLess_asymm {g1 g2 : Nat × Nat × Nat} (h : stepLess g2 g1 = true) : + stepLess g1 g2 = false := by + unfold stepLess at h ⊢ + simp only [Bool.or_eq_true, Bool.and_eq_true, beq_iff_eq, decide_eq_true_eq] at h + simp only [Bool.or_eq_false_iff, Bool.and_eq_false_iff, beq_eq_false_iff_ne, + decide_eq_false_iff_not, not_lt] + rcases h with h1 | ⟨heq1, h2 | ⟨heq2, h3⟩⟩ + · exact ⟨by omega, Or.inl (by omega)⟩ + · exact ⟨by omega, Or.inr ⟨by omega, Or.inl (by omega)⟩⟩ + · exact ⟨by omega, Or.inr ⟨by omega, Or.inr (by omega)⟩⟩ + +theorem invCount_porSwap1 {u : Bool} {Z : Nat} {P P' : List (Nat × Nat × Nat)} + (h : porSwap1 u Z P = some P') : P'.length = P.length ∧ invCount P' + 1 ≤ invCount P := by + induction P generalizing P' with + | nil => simp [porSwap1] at h + | cons g1 r1 ih => + cases r1 with + | nil => simp [porSwap1] at h + | cons g2 rest => + unfold porSwap1 at h + dsimp only at h + split_ifs at h with hcond + · injection h with hP'; subst hP' + simp only [Bool.and_eq_true] at hcond + have hsl : stepLess g2 g1 = true := hcond.2 + have hn : stepLess g1 g2 = false := stepLess_asymm hsl + simp only [invCount, invsAhead, List.length_cons, hsl, hn, invsAhead_cons_comm g1] + have : ∀ l, invsAhead g1 (g2 :: l) = (if stepLess g2 g1 then 1 else 0) + invsAhead g1 l := fun _ => rfl + simp [invsAhead, hsl, hn] + omega + · cases hopt : porSwap1 u Z (g2 :: rest) with + | none => rw [hopt] at h; simp at h + | some r2 => + rw [hopt] at h; injection h with hP'; subst hP' + obtain ⟨hl, hi⟩ := ih hopt + have hinvs_perm : ∀ (x : Nat × Nat × Nat) (A B : List (Nat × Nat × Nat)), + porSwap1 u Z A = some B → invsAhead x B = invsAhead x A := by + intro x A + induction A with + | nil => intro B hB; simp [porSwap1] at hB + | cons a1 ar ihA => + intro B hB + cases ar with + | nil => simp [porSwap1] at hB + | cons a2 as => + unfold porSwap1 at hB + dsimp only at hB + split_ifs at hB + · injection hB with hB'; subst hB'; exact (invsAhead_cons_comm x a1 a2 as).symm + · cases hopt2 : porSwap1 u Z (a2 :: as) with + | none => rw [hopt2] at hB; simp at hB + | some bs => + rw [hopt2] at hB; injection hB with hB'; subst hB' + simp only [invsAhead, ihA bs hopt2] + refine ⟨by simp [hl], ?_⟩ + change invsAhead g1 r2 + invCount r2 + 1 ≤ invsAhead g1 (g2 :: rest) + invCount (g2 :: rest) + rw [hinvs_perm g1 (g2 :: rest) r2 hopt] + omega + +theorem porSwap1_preserves {u : Bool} {Z T : Nat} {S : List Nat} {P P' : List (Nat × Nat × Nat)} + (hc : CleanProg u Z S P T) (hl : lastIsT u Z T P) (hau : AllUsed u Z T P) + (hs : porSwap1 u Z P = some P') : + CleanProg u Z S P' T ∧ lastIsT u Z T P' ∧ AllUsed u Z T P' := by + induction P generalizing S P' with + | nil => simp [porSwap1] at hs + | cons g1 r1 ih => + cases r1 with + | nil => simp [porSwap1] at hs + | cons g2 rest => + rcases g1 with ⟨a1, b1, d1⟩ + rcases g2 with ⟨a2, b2, d2⟩ + rcases hc with ⟨ha1, hb1, hab1, hd1, hnt1, hc1, ha2, hb2, hab2, hd2, hnt2, hc2, hrest⟩ + unfold porSwap1 at hs + dsimp only at hs + split_ifs at hs with hcond + · injection hs with hP'; subst hP' + simp only [Bool.and_eq_true, bne_iff_ne] at hcond + have ha2_ne : a2 ≠ stepVal u Z d1 a1 b1 := hcond.1.1 + have hb2_ne : b2 ≠ stepVal u Z d1 a1 b1 := hcond.1.2 + simp only [List.mem_cons] at ha2 hb2 hc2 + have ha2S : a2 ∈ S := ha2.resolve_left ha2_ne + have hb2S : b2 ∈ S := hb2.resolve_left hb2_ne + have hc2_ne : stepVal u Z d2 a2 b2 ≠ stepVal u Z d1 a1 b1 := fun h => hc2 (Or.inl h) + have hc2S : stepVal u Z d2 a2 b2 ∉ S := fun h => hc2 (Or.inr h) + have ha1_ne2 : a1 ≠ stepVal u Z d2 a2 b2 := fun h => hc2S (h ▸ ha1) + have hb1_ne2 : b1 ≠ stepVal u Z d2 a2 b2 := fun h => hc2S (h ▸ hb1) + have hc1_not : stepVal u Z d1 a1 b1 ∉ stepVal u Z d2 a2 b2 :: S := by + intro h; rcases List.mem_cons.mp h with h | h + · exact hc2_ne h.symm + · exact hc1 h + have hsw : ∀ w, w ∈ stepVal u Z d1 a1 b1 :: stepVal u Z d2 a2 b2 :: S ↔ + w ∈ stepVal u Z d2 a2 b2 :: stepVal u Z d1 a1 b1 :: S := by + intro w; simp [List.mem_cons]; tauto + have hrest' : CleanProg u Z (stepVal u Z d1 a1 b1 :: stepVal u Z d2 a2 b2 :: S) rest T := by + suffices ∀ (Q : List (Nat × Nat × Nat)) (A B : List Nat), (∀ w, w ∈ A ↔ w ∈ B) → + CleanProg u Z A Q T → CleanProg u Z B Q T from + this rest _ _ (fun w => (hsw w).symm) hrest + intro Q; induction Q with + | nil => intro A B hAB hA; exact (hAB T).mp hA + | cons q qs ihq => + rcases q with ⟨qa, qb, qd⟩ + intro A B hAB ⟨qaA, qbA, qab, qd', qnt, qcm, qr⟩ + exact ⟨(hAB qa).mp qaA, (hAB qb).mp qbA, qab, qd', qnt, + fun h => qcm ((hAB _).mpr h), + ihq _ _ (fun w => by simp [List.mem_cons, hAB w]) qr⟩ + have hrest_ne : rest ≠ [] := by + intro hr_eq; subst hr_eq + rcases hau.1 with he | hu + · simp at he + · simp [isUsed] at hu + rcases hu with (h | h) | h + · exact ha2_ne h + · exact hb2_ne h + · exact hl.1 h + have hl1 : stepVal u Z d1 a1 b1 ≠ T := hl.1 + have hl2 : stepVal u Z d2 a2 b2 ≠ T := by + cases hrc : rest with + | nil => exact absurd hrc hrest_ne + | cons _ _ => rw [hrc] at hl; exact hl.2.1 + have hlr : lastIsT u Z T rest := by + cases hrc : rest with + | nil => exact absurd hrc hrest_ne + | cons _ _ => rw [hrc] at hl; exact hl.2.2 + have hlt_new : lastIsT u Z T ((a2, b2, d2) :: (a1, b1, d1) :: rest) := by + cases hrc : rest with + | nil => exact absurd hrc hrest_ne + | cons _ _ => simp only [lastIsT]; rw [← hrc]; exact ⟨hl2, hl1, hlr⟩ + have hau1 : isUsed (stepVal u Z d1 a1 b1) T rest = true := by + rcases hau.1 with he | hu + · cases he + · simp only [isUsed, Bool.or_eq_true, beq_iff_eq] at hu + rcases hu with (h | h) | h + · exact absurd h ha2_ne + · exact absurd h hb2_ne + · exact h + have hau2 : isUsed (stepVal u Z d2 a2 b2) T ((a1, b1, d1) :: rest) = true := by + rcases hau.2.1 with he | hu + · exact absurd he hrest_ne + · simp only [isUsed, Bool.or_eq_true, beq_iff_eq] + exact Or.inr hu + refine ⟨⟨ha2S, hb2S, hab2, hd2, hnt2, hc2S, + List.mem_cons_of_mem _ ha1, List.mem_cons_of_mem _ hb1, hab1, hd1, hnt1, hc1_not, hrest'⟩, + hlt_new, ⟨Or.inr hau2, ⟨Or.inr hau1, hau.2.2⟩⟩⟩ + · cases hopt : porSwap1 u Z ((a2, b2, d2) :: rest) with + | none => rw [hopt] at hs; simp at hs + | some r2 => + rw [hopt] at hs; injection hs with hP'; subst hP' + have hl2 : lastIsT u Z T ((a2, b2, d2) :: rest) := hl.2 + obtain ⟨hc2', hl2', hau2'⟩ := ih ⟨ha2, hb2, hab2, hd2, hnt2, hc2, hrest⟩ hl2 hau.2 hopt + have hc_eq_len := (invCount_porSwap1 hopt).1 + have hr2_ne : r2 ≠ [] := by intro he; rw [he] at hc_eq_len; simp at hc_eq_len + have hau1_new : isUsed (stepVal u Z d1 a1 b1) T r2 = true := by + suffices ∀ A B, porSwap1 u Z A = some B → ∀ x, isUsed x T A = isUsed x T B by + rcases hau.1 with he | hu + · cases he + · rw [← this _ _ hopt]; exact hu + intro A + induction A with + | nil => intro B hB; simp [porSwap1] at hB + | cons u1 us ihA => + intro B hB x + cases us with + | nil => simp [porSwap1] at hB + | cons u2 vs => + rcases u1 with ⟨u1a, u1b, u1d⟩ + rcases u2 with ⟨u2a, u2b, u2d⟩ + unfold porSwap1 at hB; dsimp only at hB + split_ifs at hB + · injection hB with hB'; subst hB' + apply Bool.eq_iff_iff.mpr; simp only [isUsed, Bool.or_eq_true]; tauto + · cases ho : porSwap1 u Z ((u2a, u2b, u2d) :: vs) with + | none => rw [ho] at hB; simp at hB + | some bs => + rw [ho] at hB; injection hB with hB'; subst hB' + change (u1a == x || u1b == x || isUsed x T ((u2a, u2b, u2d) :: vs)) = + (u1a == x || u1b == x || isUsed x T bs) + rw [ihA bs ho x] + refine ⟨⟨ha1, hb1, hab1, hd1, hnt1, hc1, hc2'⟩, ?_, ⟨Or.inr hau1_new, hau2'⟩⟩ + cases hr2c : r2 with + | nil => exact absurd hr2c hr2_ne + | cons _ _ => rw [← hr2c]; simp only [lastIsT]; exact ⟨hl.1, hl2'⟩ + +def porSortLoop (u : Bool) (Z : Nat) : Nat → List (Nat × Nat × Nat) → List (Nat × Nat × Nat) + | 0, P => P + | n + 1, P => match porSwap1 u Z P with | none => P | some P' => porSortLoop u Z n P' + +theorem porSortLoop_spec {u : Bool} {Z T : Nat} {S : List Nat} : + ∀ (n : Nat) (P : List (Nat × Nat × Nat)), + invCount P ≤ n → CleanProg u Z S P T → lastIsT u Z T P → AllUsed u Z T P → + let P' := porSortLoop u Z n P + P'.length = P.length ∧ CleanProg u Z S P' T ∧ lastIsT u Z T P' ∧ AllUsed u Z T P' ∧ porSwap1 u Z P' = none := by + intro n + induction n with + | zero => + intro P hi hc hl hau + have h0 : invCount P = 0 := Nat.le_zero.mp hi + have hn : porSwap1 u Z P = none := by + cases hopt : porSwap1 u Z P with + | none => rfl + | some P1 => + have := (invCount_porSwap1 hopt).2 + omega + exact ⟨rfl, hc, hl, hau, hn⟩ + | succ m ih => + intro P hi hc hl hau + simp only [porSortLoop] + cases hopt : porSwap1 u Z P with + | none => exact ⟨rfl, hc, hl, hau, hopt⟩ + | some P1 => + obtain ⟨hln, hi1⟩ := invCount_porSwap1 hopt + obtain ⟨hc1, hl1, hau1⟩ := porSwap1_preserves hc hl hau hopt + obtain ⟨hl2, hc2, hl2', hau2, hn2⟩ := ih P1 (by omega) hc1 hl1 hau1 + exact ⟨hl2.trans hln, hc2, hl2', hau2, hn2⟩ + +def PORAdj (u : Bool) (Z : Nat) : Nat → Nat → Nat → Nat → List (Nat × Nat × Nat) → Prop + | _, _, _, _, [] => True + | lkOut, lkA, lkB, lkOp, (a, b, d) :: rest => + (lkOut = 0 ∨ a = lkOut ∨ b = lkOut ∨ + lkA < a ∨ (lkA = a ∧ (lkB < b ∨ (lkB = b ∧ lkOp < d)))) ∧ + PORAdj u Z (stepVal u Z d a b) a b d rest + +theorem pairwise_eq_of_mem_iff {a b : List Nat} (ha : a.Pairwise (· < ·)) (hb : b.Pairwise (· < ·)) + (h : ∀ x, x ∈ a ↔ x ∈ b) : a = b := by + induction a generalizing b with + | nil => + cases b with + | nil => rfl + | cons y ys => exact absurd ((h y).mpr List.mem_cons_self) (by simp) + | cons x xs ih => + cases b with + | nil => exact absurd ((h x).mp List.mem_cons_self) (by simp) + | cons y ys => + have hx_ne : x ∉ xs := fun hm => Nat.lt_irrefl x (List.rel_of_pairwise_cons ha hm) + have hy_ne : y ∉ ys := fun hm => Nat.lt_irrefl y (List.rel_of_pairwise_cons hb hm) + have hxy : x = y := by + rcases List.mem_cons.mp ((h x).mp List.mem_cons_self) with rfl | hxs + · rfl + · have hyx : y ∈ x :: xs := (h y).mpr List.mem_cons_self + rcases List.mem_cons.mp hyx with h1 | h2 + · exact h1.symm + · have hlt1 := List.rel_of_pairwise_cons hb hxs + have hlt2 := List.rel_of_pairwise_cons ha h2 + omega + subst hxy + congr 1 + apply ih (List.pairwise_cons.mp ha).2 (List.pairwise_cons.mp hb).2 + intro z + have hz := h z + simp only [List.mem_cons] at hz + constructor <;> intro hm + · rcases hz.mp (Or.inr hm) with rfl | hy + · exact absurd hm hx_ne + · exact hy + · rcases hz.mpr (Or.inr hm) with rfl | hx + · exact absurd hm hy_ne + · exact hx + +theorem ne_of_stepLess_false {a1 b1 d1 a2 b2 d2 : Nat} + (hne : ¬ (a1 = a2 ∧ b1 = b2 ∧ d1 = d2)) + (hns : stepLess (a2, b2, d2) (a1, b1, d1) = false) : + stepLess (a1, b1, d1) (a2, b2, d2) = true := by + unfold stepLess at hns ⊢ + simp only [Bool.or_eq_false_iff, Bool.and_eq_false_iff, beq_eq_false_iff_ne, + decide_eq_false_iff_not, not_lt] at hns + simp only [Bool.or_eq_true, Bool.and_eq_true, beq_iff_eq, decide_eq_true_eq] + rcases Nat.lt_or_eq_of_le hns.1 with h1 | h1 + · exact Or.inl h1 + · right; refine ⟨h1, ?_⟩ + have hns2 : a2 ≠ a1 ∨ b1 ≤ b2 ∧ (b2 ≠ b1 ∨ d1 ≤ d2) := by + rcases hns.2 with hn | ⟨hbe, hn2⟩ + · exact Or.inl hn + · exact Or.inr ⟨hbe, hn2⟩ + rcases hns2 with hn | ⟨hble, hrest⟩ + · exact absurd h1.symm hn + · rcases Nat.lt_or_eq_of_le hble with hb | hb + · exact Or.inl hb + · right; refine ⟨hb, ?_⟩ + rcases hrest with hn | hdle + · exact absurd hb.symm hn + · omega + +theorem porClean_to_PORSteps {u : Bool} {Z T : Nat} : + ∀ {P : List (Nat × Nat × Nat)} {S_pool : List Nat} (lkOut lkA lkB lkOp : Nat), + S_pool.Pairwise (· < ·) → T ∉ S_pool → + CleanProg u Z S_pool P T → lastIsT u Z T P → AllUsed u Z T P → + porSwap1 u Z P = none → + (P = [] ∨ lkOut = 0 ∨ + match P with + | [] => True + | (a, b, d) :: _ => lkOut = 0 ∨ a = lkOut ∨ b = lkOut ∨ + lkA < a ∨ (lkA = a ∧ (lkB < b ∨ (lkB = b ∧ lkOp < d)))) → + ∃ (S₀ : List Nat) (steps : List (Nat × Nat × Nat × Bool × Bool)), + S₀ = S_pool.filter (fun w => isUsed w T P) ∧ + PORSteps u Z T lkOut lkA lkB lkOp S₀ steps ∧ steps.length = P.length := by + intro P + induction P with + | nil => + intro S_pool _ _ _ _ _ _ hc hl _ _ _ + simp [lastIsT] at hl + | cons st rest ih => + intro S_pool lkOut lkA lkB lkOp hSpw hTS hc hl hau hp hlk + rcases st with ⟨a, b, d⟩ + rcases hc with ⟨ha, hb, hab, hd, hnt, hcm, hrest⟩ + let c := stepVal u Z d a b + let S₀ := S_pool.filter (fun w => isUsed w T ((a, b, d) :: rest)) + have hS0_pw : S₀.Pairwise (· < ·) := hSpw.filter _ + have haS0 : a ∈ S₀ := List.mem_filter.mpr ⟨ha, by simp [isUsed]⟩ + have hbS0 : b ∈ S₀ := List.mem_filter.mpr ⟨hb, by simp [isUsed]⟩ + have hcS0 : c ∉ S₀ := fun h => hcm (List.mem_filter.mp h).1 + have hpor_now : lkOut = 0 ∨ a = lkOut ∨ b = lkOut ∨ + lkA < a ∨ (lkA = a ∧ (lkB < b ∨ (lkB = b ∧ lkOp < d))) := by + rcases hlk with h | h | h + · cases h + · exact Or.inl h + · exact h + let ka := isUsed a T rest + let kb := isUsed b T rest + cases hrc : rest with + | nil => + subst hrc + have hcT : c = T := hl + have hka_f : ka = false := by + simp only [ka, isUsed, beq_eq_false_iff_ne] + intro heq; subst heq; exact hTS ha + have hkb_f : kb = false := by + simp only [kb, isUsed, beq_eq_false_iff_ne] + intro heq; subst heq; exact hTS hb + have hnxt_eq : applyStep S₀ a b c ka kb = [T] := by + apply pairwise_eq_of_mem_iff (pairwise_applyStep hS0_pw) (List.pairwise_singleton _ _) + intro x + rw [mem_applyStep, List.mem_singleton] + constructor + · rintro (rfl | ⟨hx, h1, h2⟩) + · exact hcT + · have hx_used := (List.mem_filter.mp hx).2 + simp only [isUsed, Bool.or_eq_true, beq_iff_eq] at hx_used + rcases hx_used with (rfl | rfl) | rfl + · rcases h1 with hne | hk + · exact absurd rfl hne + · rw [hk] at hka_f; cases hka_f + · rcases h2 with hne | hk + · exact absurd rfl hne + · rw [hk] at hkb_f; cases hkb_f + · exact rfl + · intro rfl; left; exact hcT.symm + refine ⟨S₀, [(a, b, d, ka, kb)], rfl, ?_, by simp⟩ + refine ⟨hS0_pw, haS0, hbS0, hab, hd, hnt, hcS0, hpor_now, ?_⟩ + simpa [PORSteps, c, hnxt_eq] + | cons g2 rest2 => + subst hrc + rcases g2 with ⟨a2, b2, d2⟩ + have hl_rest : lastIsT u Z T ((a2, b2, d2) :: rest2) := hl.2 + have hrest_por : porSwap1 u Z ((a2, b2, d2) :: rest2) = none := by + unfold porSwap1 at hp; dsimp only at hp + split_ifs at hp + cases hopt : porSwap1 u Z ((a2, b2, d2) :: rest2) <;> simp_all + have hcond2 : a2 = c ∨ b2 = c ∨ + (a < a2 ∨ (a = a2 ∧ (b < b2 ∨ (b = b2 ∧ d < d2)))) := by + unfold porSwap1 at hp; dsimp only at hp + split_ifs at hp with hcond + have hnot : ¬ (a2 ≠ c ∧ b2 ≠ c ∧ stepLess (a2, b2, d2) (a, b, d) = true) := by + intro hh + have : (a2 != c && b2 != c && stepLess (a2, b2, d2) (a, b, d)) = true := by + simp [hh.1, hh.2.1, hh.2.2] + exact hcond this + by_cases ha2c : a2 = c + · exact Or.inl ha2c + · by_cases hb2c : b2 = c + · exact Or.inr (Or.inl hb2c) + · right; right + have hns : stepLess (a2, b2, d2) (a, b, d) = false := by + cases hst : stepLess (a2, b2, d2) (a, b, d) + · rfl + · exact absurd ⟨ha2c, hb2c, hst⟩ hnot + have hne : ¬ (a = a2 ∧ b = b2 ∧ d = d2) := by + rintro ⟨rfl, rfl, rfl⟩ + exact hrest.2.2.2.2.2.1 List.mem_cons_self + have hsl := ne_of_stepLess_false hne hns + unfold stepLess at hsl + simp only [Bool.or_eq_true, Bool.and_eq_true, beq_iff_eq, decide_eq_true_eq] at hsl + exact hsl + let S1 := insSort c S_pool + have hS1_pw : S1.Pairwise (· < ·) := pairwise_insSort hSpw + have hTS1 : T ∉ S1 := by + intro hm; rw [mem_insSort] at hm + rcases hm with rfl | h + · exact hl.1 rfl + · exact hTS h + have hsw : ∀ w, w ∈ c :: S_pool ↔ w ∈ S1 := fun w => by rw [mem_insSort, List.mem_cons] + have hrest_clean : CleanProg u Z S1 ((a2, b2, d2) :: rest2) T := by + suffices ∀ (Q : List (Nat × Nat × Nat)) (A B : List Nat), (∀ w, w ∈ A ↔ w ∈ B) → + CleanProg u Z A Q T → CleanProg u Z B Q T from + this _ _ _ hsw hrest + intro Q; induction Q with + | nil => intro A B hAB hA; exact (hAB T).mp hA + | cons q qs ihq => + rcases q with ⟨qa, qb, qd⟩ + intro A B hAB ⟨qaA, qbA, qab, qd', qnt, qcm, qr⟩ + exact ⟨(hAB qa).mp qaA, (hAB qb).mp qbA, qab, qd', qnt, + fun h => qcm ((hAB _).mpr h), + ihq _ _ (fun w => by simp [List.mem_cons, hAB w]) qr⟩ + obtain ⟨S1', steps', hS1'_eq, hpor', hslen'⟩ := ih c a b d hS1_pw hTS1 hrest_clean hl_rest hau.2 hrest_por + (Or.inr (Or.inr (by dsimp only; exact Or.inr hcond2))) + have hnxt_eq : applyStep S₀ a b c ka kb = S1' := by + rw [hS1'_eq] + apply pairwise_eq_of_mem_iff (pairwise_applyStep hS0_pw) (hS1_pw.filter _) + intro x + dsimp only [S1] + rw [mem_applyStep]; rw [List.mem_filter]; rw [List.mem_filter, mem_insSort] + simp only [isUsed, Bool.or_eq_true, beq_iff_eq] + constructor + · rintro (rfl | ⟨hx, h1, h2⟩) + · refine ⟨Or.inl rfl, ?_⟩ + rcases hau.1 with he | hu + · contradiction + · simpa [isUsed] using hu + · rcases hx with ⟨hxP, hxU⟩ + refine ⟨Or.inr hxP, ?_⟩ + rcases hxU with (rfl | rfl) | hrem + · rcases h1 with hne | hk + · exact absurd rfl hne + · simpa [ka, isUsed] using hk + · rcases h2 with hne | hk + · exact absurd rfl hne + · simpa [kb, isUsed] using hk + · exact hrem + · rintro ⟨rfl | hxP, hxU⟩ + · left; rfl + · right + have hxa : x ≠ a ∨ ka = true := by + by_cases he : x = a + · subst he; right; simpa [ka, isUsed] using hxU + · left; exact he + have hxb : x ≠ b ∨ kb = true := by + by_cases he : x = b + · subst he; right; simpa [kb, isUsed] using hxU + · left; exact he + exact ⟨⟨hxP, Or.inr hxU⟩, hxa, hxb⟩ + refine ⟨S₀, (a, b, d, ka, kb) :: steps', rfl, ?_, by simp [hslen']⟩ + refine ⟨hS0_pw, haS0, hbS0, hab, hd, hnt, hcS0, hpor_now, ?_⟩ + simpa [c, hnxt_eq] using hpor' + +theorem validProg_to_PORSteps {u : Bool} {Z tab : Nat} (hZ : Z ≤ full64) + (hnt : !trivW u Z tab = true) (hTnot : normZ u Z tab ∉ varsSorted u Z) + {P : List (Nat × Nat × Nat)} (hv : ValidProg u Z (varsOf u Z) P (normZ u Z tab)) : + ∃ (S₀ : List Nat) (steps : List (Nat × Nat × Nat × Bool × Bool)), + (∀ w ∈ S₀, w ∈ varsSorted u Z) ∧ S₀.Pairwise (· < ·) ∧ + PORSteps u Z (normZ u Z tab) 0 0 0 0 S₀ steps ∧ steps.length ≤ P.length := by + let ZV := varsSorted u Z + let T := normZ u Z tab + have hS : ∀ w ∈ varsOf u Z, trivW u Z w = true ∨ w ∈ ZV := fun w hw => Or.inr (mem_dedupSort.mpr hw) + have hS_norm : ∀ w ∈ ZV, normZ u Z w = w ∧ !trivW u Z w = true := by + intro w hw + have hwv := mem_dedupSort.mp hw + unfold varsOf at hwv + rw [List.mem_filter, List.mem_map] at hwv + rcases hwv with ⟨⟨x, _, rfl⟩, hntw⟩ + exact ⟨normZ_idem u hZ (Nat.and_self Z), hntw⟩ + have hnT : normZ u Z T = T := normZ_idem u hZ (Nat.and_self Z) + have hntT : !trivW u Z T = true := by simpa [T, normZ_trivW u hZ] using hnt + obtain ⟨P1, hPlen1, hc1⟩ := validProg_to_clean hZ hS hS_norm hntT hnT hv + obtain ⟨hPlen2, hc2, hl2⟩ := chopAtT_clean hTnot hc1 + let P2 := chopAtT u Z T P1 + have hPr := pruneUnusedLoop_spec P2.length P2 (Nat.le_refl _) hc2 hl2 + let P3 := pruneUnusedLoop u Z T P2.length P2 + have hau3 := dropUnused1_none_allUsed P3 hPr.2.2.2 + have hPs := porSortLoop_spec (P3.length * P3.length) P3 (invCount_le_sq P3) hPr.2.1 hPr.2.2.1 hau3 + let P4 := porSortLoop u Z (P3.length * P3.length) P3 + obtain ⟨S₀, steps, hS0_eq, hpor, hslen⟩ := porClean_to_PORSteps 0 0 0 0 + (dedupSort_pairwise (varsOf u Z)) hTnot hPs.2.1 hPs.2.2.1 hPs.2.2.2.1 hPs.2.2.2.2 (Or.inr (Or.inl rfl)) + have hS0pw : S₀.Pairwise (· < ·) := by rw [hS0_eq]; exact (dedupSort_pairwise (varsOf u Z)).filter _ + have hS0mem : ∀ w ∈ S₀, w ∈ ZV := fun w hw => by rw [hS0_eq] at hw; exact (List.mem_filter.mp hw).1 + have hl1 : P4.length = P3.length := hPs.1 + have hl2_len : P3.length ≤ P2.length := hPr.1 + have hPlen2' : P2.length ≤ P1.length := hPlen2 + have hslen' : steps.length = P4.length := hslen + exact ⟨S₀, steps, hS0mem, hS0pw, hpor, by omega⟩ + +def sepPairOK (Z p acc : Nat) : List Nat → Bool + | [] => acc == 0 + | w :: ws => + let acc1 := acc &&& (if w.testBit p then w else w ^^^ Z) + if acc1 == 0 then true else sepPairOK Z p acc1 ws + +def sepOK (Z mask : Nat) (S : List Nat) : List Nat → Bool + | [] => true + | p :: ps => sepPairOK Z p mask S && sepOK Z mask S ps + +def covPairOK (p zeros acc : Nat) : List Nat → Bool + | [] => (acc &&& zeros) == 0 + | w :: ws => + let acc1 := if w.testBit p then acc &&& w else acc + if (acc1 &&& zeros) == 0 then true else covPairOK p zeros acc1 ws + +def covOK (Z zeros : Nat) (S : List Nat) : List Nat → Bool + | [] => true + | p :: ps => covPairOK p zeros Z S && covOK Z zeros S ps + +def validOK (u : Bool) (Z mask : Nat) (S pts : List Nat) : Bool := + if u then sepOK Z mask S pts else covOK Z mask S pts + +def ptsMaskOK (u flip : Bool) (Z tab mask : Nat) (pts : List Nat) : Bool := + (mask ≤ full64) && + if u && flip then + (mask == tab &&& Z) && pts.all (fun p => p < 64 && Z.testBit p && !(tab.testBit p)) + else + (mask == (tab ^^^ full64) &&& Z) && pts.all (fun p => p < 64 && Z.testBit p && tab.testBit p) + +theorem exists_bit_lt64 {a : Nat} (ha0 : a ≠ 0) (ha64 : a ≤ full64) : + ∃ q < 64, a.testBit q = true := by + by_contra hnone + push_neg at hnone + have h64 : full64 < 2 ^ 64 := by decide + have ha_lt : a < 2 ^ 64 := by omega + have heq : a = 0 := Nat.eq_of_testBit_eq fun q => by + rcases Nat.lt_or_ge q 64 with hq | hq + · have := hnone q hq + cases hb : a.testBit q <;> simp_all + · have h0 : a < 2 ^ q := Nat.lt_of_lt_of_le ha_lt (Nat.pow_le_pow_right (by decide) hq) + simp [Nat.testBit_lt_two_pow h0] + exact ha0 heq + +theorem sepPairOK_false {Z p : Nat} : + ∀ (ws : List Nat) (acc : Nat), acc ≤ full64 → sepPairOK Z p acc ws = false → + ∃ q < 64, acc.testBit q = true ∧ (Z.testBit q = true → ∀ w ∈ ws, w.testBit q = w.testBit p) := by + intro ws + induction ws with + | nil => + intro acc ha hf + simp only [sepPairOK, beq_eq_false_iff_ne] at hf + obtain ⟨q, hq, hbq⟩ := exists_bit_lt64 hf ha + exact ⟨q, hq, hbq, fun _ _ hw => by simp at hw⟩ + | cons w ws ih => + intro acc ha hf + unfold sepPairOK at hf; dsimp only at hf + let acc1 := acc &&& (if w.testBit p then w else w ^^^ Z) + by_cases h0 : (acc1 == 0) = true + · simp [acc1, h0] at hf + · simp only [acc1, h0, ↓reduceIte] at hf + have hle1 : acc1 ≤ full64 := Nat.le_trans Nat.and_le_left ha + obtain ⟨q, hq, hbq, hrest⟩ := ih acc1 hle1 hf + dsimp only [acc1] at hbq + rw [Nat.testBit_and] at hbq + simp only [Bool.and_eq_true] at hbq + refine ⟨q, hq, hbq.1, fun hZq u hu => ?_⟩ + rcases List.mem_cons.mp hu with rfl | hu_ws + · have h2 := hbq.2 + by_cases hwp : u.testBit p = true + · simp only [hwp, ↓reduceIte] at h2; rw [h2, hwp] + · have hwpf : u.testBit p = false := Bool.eq_false_iff.mpr hwp + simp only [hwpf, Bool.false_eq_true, ↓reduceIte, Nat.testBit_xor, hZq] at h2 + rw [hwpf] + cases hq_b : u.testBit q <;> simp_all + · exact hrest hZq u hu_ws + +theorem sepOK_false {Z mask : Nat} {S : List Nat} (hm : mask ≤ full64) : + ∀ pts : List Nat, sepOK Z mask S pts = false → + ∃ p ∈ pts, ∃ q < 64, mask.testBit q = true ∧ (Z.testBit q = true → ∀ w ∈ S, w.testBit q = w.testBit p) := by + intro pts hf + induction pts with + | nil => simp [sepOK] at hf + | cons p ps ih => + unfold sepOK at hf + rw [Bool.and_eq_false_iff] at hf + rcases hf with h1 | h2 + · obtain ⟨q, hq, hmq, hw⟩ := sepPairOK_false S mask hm h1 + exact ⟨p, List.mem_cons_self, q, hq, hmq, hw⟩ + · obtain ⟨p', hp', q, hq, hmq, hw⟩ := ih h2 + exact ⟨p', List.mem_cons_of_mem _ hp', q, hq, hmq, hw⟩ + +theorem covPairOK_false {p zeros : Nat} (hz : zeros ≤ full64) : + ∀ (ws : List Nat) (acc : Nat), covPairOK p zeros acc ws = false → + ∃ q < 64, acc.testBit q = true ∧ zeros.testBit q = true ∧ + ∀ w ∈ ws, w.testBit p = true → w.testBit q = true := by + intro ws + induction ws with + | nil => + intro acc hf + simp only [covPairOK, beq_eq_false_iff_ne] at hf + have hle : acc &&& zeros ≤ full64 := Nat.le_trans Nat.and_le_right hz + obtain ⟨q, hq, hbq⟩ := exists_bit_lt64 hf hle + rw [Nat.testBit_and] at hbq + simp only [Bool.and_eq_true] at hbq + exact ⟨q, hq, hbq.1, hbq.2, fun _ hw _ => by simp at hw⟩ + | cons w ws ih => + intro acc hf + unfold covPairOK at hf; dsimp only at hf + let acc1 := if w.testBit p then acc &&& w else acc + by_cases h0 : (acc1 &&& zeros == 0) = true + · simp [acc1, h0] at hf + · simp only [acc1, h0, ↓reduceIte] at hf + obtain ⟨q, hq, hacc1, hzq, hrest⟩ := ih acc1 hf + dsimp only [acc1] at hacc1 + refine ⟨q, hq, ?_, hzq, fun u hu hup => ?_⟩ + · by_cases hwp : w.testBit p = true + · simp only [hwp, ↓reduceIte, Nat.testBit_and, Bool.and_eq_true] at hacc1; exact hacc1.1 + · simp only [hwp, Bool.false_eq_true, ↓reduceIte] at hacc1; exact hacc1 + · rcases List.mem_cons.mp hu with rfl | hu_ws + · simp only [hup, ↓reduceIte, Nat.testBit_and, Bool.and_eq_true] at hacc1; exact hacc1.2 + · exact hrest u hu_ws hup + +theorem covOK_false {Z zeros : Nat} {S : List Nat} (hz : zeros ≤ full64) : + ∀ pts : List Nat, covOK Z zeros S pts = false → + ∃ p ∈ pts, ∃ q < 64, Z.testBit q = true ∧ zeros.testBit q = true ∧ + ∀ w ∈ S, w.testBit p = true → w.testBit q = true := by + intro pts hf + induction pts with + | nil => simp [covOK] at hf + | cons p ps ih => + unfold covOK at hf + rw [Bool.and_eq_false_iff] at hf + rcases hf with h1 | h2 + · obtain ⟨q, hq, hZq, hzq, hw⟩ := covPairOK_false hz S Z h1 + exact ⟨p, List.mem_cons_self, q, hq, hZq, hzq, hw⟩ + · obtain ⟨p', hp', q, hq, hZq, hzq, hw⟩ := ih h2 + exact ⟨p', List.mem_cons_of_mem _ hp', q, hq, hZq, hzq, hw⟩ + +theorem validOK_false_no_por {u flip : Bool} {Z tab mask : Nat} {pts S S_sup : List Nat} + {lkOut lkA lkB lkOp : Nat} {steps : List (Nat × Nat × Nat × Bool × Bool)} + (hZ : Z ≤ full64) (hPM : ptsMaskOK u flip Z tab mask pts = true) + (hsub : ∀ w ∈ S, w ∈ S_sup) + (hv : validOK u Z mask S_sup pts = false) : + ¬ PORSteps u Z (normZ u Z tab) lkOut lkA lkB lkOp S steps := by + intro hpor + unfold ptsMaskOK at hPM + simp only [Bool.and_eq_true, decide_eq_true_eq] at hPM + rcases hPM with ⟨hm64, hspec⟩ + unfold validOK at hv + cases u with + | true => + simp only [Bool.true_and, ↓reduceIte] at hspec hv + obtain ⟨p, hp, q, hq, hmq, hws⟩ := sepOK_false hm64 pts hv + cases flip with + | false => + simp only [Bool.false_eq_true, true_and, false_and, ↓reduceIte, Bool.and_eq_true, beq_iff_eq] at hspec + have hpa := (List.all_eq_true.mp hspec.2) p hp + simp only [Bool.and_eq_true, decide_eq_true_eq] at hpa + rw [hspec.1] at hmq + simp only [Nat.testBit_and, Bool.and_eq_true, testBit_xor_full hq, Bool.not_eq_true'] at hmq + have hpf : pairFail true S p q = true := by + unfold pairFail; simp only [↓reduceIte, List.all_eq_true, beq_iff_eq] + intro w hw; exact (hws hmq.2 w (hsub w hw)).symm + exact porSteps_pairFail hZ rfl hpa.1.2 hmq.2 hpa.2 hmq.1 hpf hpor + | true => + simp only [true_and, ↓reduceIte, Bool.and_eq_true, beq_iff_eq, Bool.not_eq_true'] at hspec + have hpa := (List.all_eq_true.mp hspec.2) p hp + simp only [Bool.and_eq_true, decide_eq_true_eq, Bool.not_eq_true'] at hpa + rw [hspec.1] at hmq + simp only [Nat.testBit_and, Bool.and_eq_true] at hmq + have hpf : pairFail true S q p = true := by + unfold pairFail; simp only [↓reduceIte, List.all_eq_true, beq_iff_eq] + intro w hw; exact hws hmq.2 w (hsub w hw) + exact porSteps_pairFail hZ rfl hmq.2 hpa.1.2 hmq.1 hpa.2 hpf hpor + | false => + simp only [Bool.false_eq_true, ↓reduceIte] at hv + simp only [Bool.false_and, Bool.false_eq_true, false_and, ↓reduceIte, Bool.and_eq_true, beq_iff_eq] at hspec + obtain ⟨p, hp, q, hq, hZq, hzq, hws⟩ := covOK_false hm64 pts hv + have hpa := (List.all_eq_true.mp hspec.2) p hp + simp only [Bool.and_eq_true, decide_eq_true_eq] at hpa + rw [hspec.1] at hzq + simp only [Nat.testBit_and, Bool.and_eq_true, testBit_xor_full hq, Bool.not_eq_true'] at hzq + have hpf : pairFail false S p q = true := by + unfold pairFail; simp only [Bool.false_eq_true, ↓reduceIte, List.all_eq_true, Bool.or_eq_true, + Bool.not_eq_true'] + intro w hw + by_cases hwp : w.testBit p = true + · exact Or.inr (hws w (hsub w hw) hwp) + · exact Or.inl (Bool.eq_false_iff.mpr hwp) + exact porSteps_pairFail hZ rfl hpa.1.2 hZq hpa.2 hzq.1 hpf hpor + +def updNz (u : Bool) (S : List Nat) (a b c : Nat) : List Nat → List Nat → List Nat + | [], _ => [] + | Zr :: Zs, nz => + let triv := isTrivStep u Zr S a b c + (nz.headD 0 + if triv then 0 else 1) :: updNz u S a b c Zs nz.tail + +theorem updNz_spec (u : Bool) (S : List Nat) (a b c : Nat) : + ∀ (rfZ nz : List Nat) (r : Nat), r < rfZ.length → + (updNz u S a b c rfZ nz).getD r 0 = + nz.getD r 0 + (if isTrivStep u (rfZ.getD r 0) S a b c then 0 else 1) := by + intro rfZ + induction rfZ with + | nil => intro nz r hr; simp at hr + | cons Zr Zs ih => + intro nz r hr + cases r with + | zero => cases nz <;> simp [updNz, List.getD, List.headD] + | succ r' => + simp only [List.length_cons] at hr + have ih' := ih nz.tail r' (by omega) + cases nz with + | nil => simp only [updNz, List.getD, List.getElem?_cons_succ]; exact ih' + | cons n0 nzs => simp only [updNz, List.getD, List.getElem?_cons_succ]; exact ih' + +def stepZc (remAfter a b c : Nat) (ka kb : Bool) : + List Nat → List Nat → List Nat → List Nat → Option (List Nat) + | [], _, _, _ => some [] + | Zr :: Zs, Lr :: Ls, nzs, zcs => + let ta := trivW true Zr a + let tb := trivW true Zr b + let tc := trivW true Zr c + let zcn := zcs.headD 0 - (if !ka && ta then 1 else 0) - (if !kb && tb then 1 else 0) + + (if tc then 1 else 0) + let reml := Lr - nzs.headD 0 + if remAfter < reml + zcn then none + else + match stepZc remAfter a b c ka kb Zs Ls nzs.tail zcs.tail with + | none => none + | some zcR => some (zcn :: zcR) + | _, _, _, _ => none + +theorem stepZc_spec (remAfter a b c : Nat) (ka kb : Bool) : + ∀ (rfZ rfL nzNext zc zcNext : List Nat), + rfZ.length = rfL.length → + stepZc remAfter a b c ka kb rfZ rfL nzNext zc = some zcNext → + ∀ r < rfZ.length, + let Zr := rfZ.getD r 0 + let Lr := rfL.getD r 0 + let zcn := zc.getD r 0 - (if !ka && trivW true Zr a then 1 else 0) - + (if !kb && trivW true Zr b then 1 else 0) + (if trivW true Zr c then 1 else 0) + zcNext.getD r 0 = zcn ∧ (Lr - nzNext.getD r 0) + zcn ≤ remAfter := by + intro rfZ + induction rfZ with + | nil => intro _ _ _ _ _ _ r hr; simp at hr + | cons Zr Zs ih => + intro rfL nzNext zc zcNext hlen hst r hr + cases rfL with + | nil => simp at hlen + | cons Lr Ls => + unfold stepZc at hst; dsimp only at hst + change (if remAfter < Lr - nzNext.headD 0 + (zc.headD 0 - (if !ka && trivW true Zr a then 1 else 0) - + (if !kb && trivW true Zr b then 1 else 0) + (if trivW true Zr c then 1 else 0)) then none else _) = some zcNext at hst + by_cases hpr : remAfter < Lr - nzNext.headD 0 + (zc.headD 0 - (if !ka && trivW true Zr a then 1 else 0) - + (if !kb && trivW true Zr b then 1 else 0) + (if trivW true Zr c then 1 else 0)) + · rw [if_pos hpr] at hst; exact absurd hst (by simp) + · rw [if_neg hpr] at hst + cases hsub : stepZc remAfter a b c ka kb Zs Ls nzNext.tail zc.tail with + | none => rw [hsub] at hst; simp at hst + | some zcR => + rw [hsub] at hst; injection hst with hzeq; subst hzeq + cases r with + | zero => + refine ⟨by cases zc <;> simp [List.getD, List.headD, Option.getD_none, Option.getD_some], ?_⟩ + cases hnz0 : nzNext <;> cases hzc0 : zc <;> + simp [List.getD, List.headD, hnz0, hzc0] at hpr ⊢ <;> omega + | succ r' => + simp only [List.length_cons] at hr hlen + have ih' := ih Ls nzNext.tail zc.tail zcR (by omega) hsub r' (by omega) + have hztail : zc.tail.getD r' 0 = zc.getD (r' + 1) 0 := by cases zc <;> simp [List.getD] + have hntail : nzNext.tail.getD r' 0 = nzNext.getD (r' + 1) 0 := by cases nzNext <;> simp [List.getD] + rw [hztail, hntail] at ih' + simp only [List.getD, List.getElem?_cons_succ] at ih' ⊢; exact ih' + +def chk2Base (u : Bool) (Z target a b : Nat) : Nat → Bool + | 0 => true + | d + 1 => stepVal u Z d a b != target && chk2Base u Z target a b d + +def chk3Pair (u : Bool) (Z target a b w : Nat) : Nat → Bool + | 0 => true + | d1 + 1 => + let c := stepVal u Z d1 a b + c != target && chk2Base u Z target c w (opsCount u) && chk3Pair u Z target a b w d1 + +def chk3Base (u : Bool) (Z target x y z : Nat) : Bool := + chk3Pair u Z target x y z (opsCount u) && + chk3Pair u Z target x z y (opsCount u) && + chk3Pair u Z target y z x (opsCount u) + +def chkKps (u : Bool) (Z target mask : Nat) (pts rfZ rfL : List Nat) (rk m : Nat) + (S : List Nat) (a b c : Nat) (nzNext zc : List Nat) (op : Nat) + (rec : List Nat → List Nat → List Nat → Nat → Nat → Nat → Nat → Bool) : + List (Bool × Bool) → Bool + | [] => true + | (ka, kb) :: rest => + let cost := (if ka then 1 else 0) + (if kb then 1 else 0) + let ok := + if cost > rk + 1 - (m - 1) then true + else + match stepZc rk a b c ka kb rfZ rfL nzNext zc with + | none => true + | some zcNext => + let nxt := applyStep S a b c ka kb + if !(ka && kb) && !validOK u Z mask nxt pts then true + else rec nxt nzNext zcNext c a b op + ok && chkKps u Z target mask pts rfZ rfL rk m S a b c nzNext zc op rec rest + +def kpList : List (Bool × Bool) := [(false, false), (true, false), (false, true), (true, true)] + +def chkOps (u : Bool) (Z target mask : Nat) (pts rfZ rfL : List Nat) (rk m : Nat) + (S nz zc : List Nat) (lkOut lkA lkB lkOp a b : Nat) + (rec : List Nat → List Nat → List Nat → Nat → Nat → Nat → Nat → Bool) + (op cur : Nat) : Bool := + match cur with + | 0 => true + | k + 1 => + let disj := lkOut != 0 && a != lkOut && b != lkOut + let skipPor := disj && !(lkA < a || (lkA == a && (lkB < b || (lkB == b && lkOp < op)))) + let ok := + if skipPor then true + else + let c := stepVal u Z op a b + if c == target then false + else if trivW u Z c || S.any (· == c) then true + else + let nzNext := updNz u S a b c rfZ nz + chkKps u Z target mask pts rfZ rfL rk m S a b c nzNext zc op rec kpList + ok && chkOps u Z target mask pts rfZ rfL rk m S nz zc lkOut lkA lkB lkOp a b rec (op + 1) k + +def chkPairsWith (u : Bool) (Z target mask : Nat) (pts rfZ rfL : List Nat) (rk m : Nat) + (S nz zc : List Nat) (lkOut lkA lkB lkOp a : Nat) + (rec : List Nat → List Nat → List Nat → Nat → Nat → Nat → Nat → Bool) + (bs : List Nat) : Bool := + match bs with + | [] => true + | b :: rest => + chkOps u Z target mask pts rfZ rfL rk m S nz zc lkOut lkA lkB lkOp a b rec 0 (opsCount u) && + chkPairsWith u Z target mask pts rfZ rfL rk m S nz zc lkOut lkA lkB lkOp a rec rest + +def chkAllPairs (u : Bool) (Z target mask : Nat) (pts rfZ rfL : List Nat) (rk m : Nat) + (S nz zc : List Nat) (lkOut lkA lkB lkOp : Nat) + (rec : List Nat → List Nat → List Nat → Nat → Nat → Nat → Nat → Bool) + (xs : List Nat) : Bool := + match xs with + | [] => true + | a :: bs => + chkPairsWith u Z target mask pts rfZ rfL rk m S nz zc lkOut lkA lkB lkOp a rec bs && + chkAllPairs u Z target mask pts rfZ rfL rk m S nz zc lkOut lkA lkB lkOp rec bs + +def chkTree (u : Bool) (Z target mask : Nat) (pts rfZ rfL : List Nat) : + Nat → List Nat → List Nat → List Nat → Nat → Nat → Nat → Nat → Bool + | rem_k, S, nz, zc, lkOut, lkA, lkB, lkOp => + let m := S.length + if m == 0 then true + else if m == 1 then S.headD 0 != target + else if m - 1 > rem_k then true + else match rem_k with + | 0 => true + | rk + 1 => + if rk == 0 && m == 2 then + chk2Base u Z target (S.getD 0 0) (S.getD 1 0) (opsCount u) + else if rk == 1 && m == 3 then + chk3Base u Z target (S.getD 0 0) (S.getD 1 0) (S.getD 2 0) + else + chkAllPairs u Z target mask pts rfZ rfL rk m S nz zc lkOut lkA lkB lkOp + (chkTree u Z target mask pts rfZ rfL rk) S + +def RInv (u : Bool) (Z : Nat) (rfZ rfL : List Nat) + (S nz zc : List Nat) (steps : List (Nat × Nat × Nat × Bool × Bool)) : Prop := + ∀ r < rfZ.length, + let Zr := rfZ.getD r 0 + let Lr := rfL.getD r 0 + zc.getD r 0 = zCount S Zr ∧ + Lr ≤ nz.getD r 0 + countNZSteps u Z Zr S steps + +def RFSubOK (u : Bool) (Z tab : Nat) : List Nat → List Nat → Prop + | [], [] => True + | Zr :: Zs, Lr :: Ls => + (Zr ≤ full64 ∧ Zr &&& Z = Zr ∧ !trivW u Zr tab = true ∧ LowU u Zr tab Lr) ∧ + RFSubOK u Z tab Zs Ls + | _, _ => False + +theorem rfSubOK_getD {u : Bool} {Z tab : Nat} {rfZ rfL : List Nat} (hRF : RFSubOK u Z tab rfZ rfL) : + rfZ.length = rfL.length ∧ + ∀ r < rfZ.length, + let Zr := rfZ.getD r 0 + let Lr := rfL.getD r 0 + Zr ≤ full64 ∧ Zr &&& Z = Zr ∧ !trivW u Zr tab = true ∧ LowU u Zr tab Lr := by + induction rfZ generalizing rfL with + | nil => cases rfL with | nil => exact ⟨rfl, fun r hr => by simp at hr⟩ | cons => simp [RFSubOK] at hRF + | cons Zr Zs ih => + cases rfL with + | nil => simp [RFSubOK] at hRF + | cons Lr Ls => + rcases hRF with ⟨h0, hr_rest⟩ + obtain ⟨hlen, hget⟩ := ih hr_rest + refine ⟨by simp [hlen], fun r hr => ?_⟩ + cases r with + | zero => exact h0 + | succ r' => exact hget r' (by simp at hr; omega) + +def NoPathFrom (u : Bool) (Z tab : Nat) (rfZ rfL : List Nat) + (S nz zc : List Nat) (lkOut lkA lkB lkOp rem_k : Nat) : Prop := + ∀ steps, steps.length ≤ rem_k → + PORSteps u Z (normZ u Z tab) lkOut lkA lkB lkOp S steps → + RInv u Z rfZ rfL S nz zc steps → False + +theorem rInv_step {u : Bool} {Z : Nat} + {rfZ rfL : List Nat} (hlen : rfZ.length = rfL.length) + {S nz zc zcNext : List Nat} {a b d remAfter : Nat} {ka kb : Bool} + (hs : S.Pairwise (· < ·)) (ha : a ∈ S) (hb : b ∈ S) (hab : a < b) + (hc : stepVal u Z d a b ∉ S) + (rest : List (Nat × Nat × Nat × Bool × Bool)) + (hr : RInv u Z rfZ rfL S nz zc ((a, b, d, ka, kb) :: rest)) + (hzc : stepZc remAfter a b (stepVal u Z d a b) ka kb rfZ rfL (updNz u S a b (stepVal u Z d a b) rfZ nz) zc = some zcNext) : + RInv u Z rfZ rfL (applyStep S a b (stepVal u Z d a b) ka kb) (updNz u S a b (stepVal u Z d a b) rfZ nz) zcNext rest := by + let c := stepVal u Z d a b + let nzNext := updNz u S a b c rfZ nz + intro r hr_lt + have hspec := stepZc_spec remAfter a b c ka kb rfZ rfL nzNext zc zcNext hlen hzc r hr_lt + have hnz := updNz_spec u S a b c rfZ nz r hr_lt + obtain ⟨hzc_eq, hnz_le⟩ := hr r hr_lt + have hzc_nxt := zCount_applyStep hs (Z := rfZ.getD r 0) hab ha hb hc ka kb + simp only [countNZSteps] at hnz_le + refine ⟨?_, ?_⟩ + · rw [hspec.1, hzc_eq, hzc_nxt] + · rw [hnz]; dsimp only [c] at *; omega + +theorem rInv_pruned_impossible {u : Bool} {Z tab : Nat} (hZ : Z ≤ full64) + {rfZ rfL : List Nat} (hRF : RFSubOK u Z tab rfZ rfL) + {S nz zc : List Nat} {a b d remAfter : Nat} {ka kb : Bool} + (hs : S.Pairwise (· < ·)) (ha : a ∈ S) (hb : b ∈ S) (hab : a < b) + (hc : stepVal u Z d a b ∉ S) + (rest : List (Nat × Nat × Nat × Bool × Bool)) + (hrest_len : rest.length ≤ remAfter) + (hpor : PORSteps u Z (normZ u Z tab) (stepVal u Z d a b) a b d (applyStep S a b (stepVal u Z d a b) ka kb) rest) + (hr : RInv u Z rfZ rfL S nz zc ((a, b, d, ka, kb) :: rest)) + (hnone : stepZc remAfter a b (stepVal u Z d a b) ka kb rfZ rfL (updNz u S a b (stepVal u Z d a b) rfZ nz) zc = none) : + False := by + let c := stepVal u Z d a b + let nzNext := updNz u S a b c rfZ nz + have ⟨hlen, hsub⟩ := rfSubOK_getD hRF + suffices ∀ (Zs Ls nzs zcs : List Nat) (off : Nat), + Zs.length = Ls.length → + (∀ r < Zs.length, + let Zr := Zs.getD r 0 + let Lr := Ls.getD r 0 + Zr ≤ full64 ∧ Zr &&& Z = Zr ∧ !trivW u Zr tab = true ∧ LowU u Zr tab Lr ∧ + zcs.getD r 0 = zCount S Zr ∧ + Lr ≤ nzs.getD r 0 + countNZSteps u Z Zr (applyStep S a b c ka kb) rest) → + stepZc remAfter a b c ka kb Zs Ls nzs zcs = none → False by + exact this rfZ rfL nzNext zc 0 hlen (fun r hr_lt => by + obtain ⟨hZr, hZb, hnt, hlow⟩ := hsub r hr_lt + obtain ⟨hzc_eq, hnz_le⟩ := hr r hr_lt + have hnz := updNz_spec u S a b c rfZ nz r hr_lt + simp only [countNZSteps] at hnz_le + refine ⟨hZr, hZb, hnt, hlow, hzc_eq, ?_⟩ + rw [hnz]; dsimp only [c] at *; omega) hnone + intro Zs + induction Zs with + | nil => intro Ls nzs zcs _ _ _ hn; simp [stepZc] at hn + | cons Zr Zs ih => + intro Ls nzs zcs off hle hprops hn + cases Ls with + | nil => simp at hle + | cons Lr Ls => + have h0 := hprops 0 (by simp) + simp only [List.getD, List.getElem?_cons_zero, Option.getD_some] at h0 + obtain ⟨hZr, hZb, hnt, _, hzc0, hnz0⟩ := h0 + have hzc_nxt := zCount_applyStep hs (Z := Zr) hab ha hb hc ka kb + have htriv_le := zCount_le_trivSteps (u := u) (Zr := Zr) hZr hZb (by + rw [← normZ_trivW u hZr, normZ_idem u hZr hZb, normZ_trivW u hZr]; exact hnt) hpor + have hsplit := count_split_steps u Z Zr (applyStep S a b c ka kb) rest + have hbound : (Lr - nzs.headD 0) + (zcs.headD 0 - (if !ka && trivW true Zr a then 1 else 0) - + (if !kb && trivW true Zr b then 1 else 0) + (if trivW true Zr c then 1 else 0)) ≤ remAfter := by + have hz_eq : zcs.headD 0 = zCount S Zr := by cases zcs <;> simpa [List.getD] using hzc0 + have hn_eq : nzs.headD 0 = nzs[0]?.getD 0 := by cases nzs <;> rfl + rw [hz_eq, hn_eq, ← hzc_nxt] + dsimp only [c] at hzc_nxt htriv_le hsplit hnz0 ⊢ + omega + unfold stepZc at hn; dsimp only at hn + by_cases hlt : remAfter < Lr - nzs.headD 0 + (zcs.headD 0 - (if !ka && trivW true Zr a then 1 else 0) - + (if !kb && trivW true Zr b then 1 else 0) + (if trivW true Zr c then 1 else 0)) + · exact absurd hbound (by omega) + · simp only [hlt, ↓reduceIte] at hn + cases hst : stepZc remAfter a b c ka kb Zs Ls nzs.tail zcs.tail with + | some _ => rw [hst] at hn; simp at hn + | none => + exact ih Ls nzs.tail zcs.tail (off + 1) (by simp at hle; omega) (fun r hr_lt => by + have hr' := hprops (r + 1) (by simp at hr_lt ⊢; omega) + have hz1 : zcs.tail.getD r 0 = zcs.getD (r + 1) 0 := by cases zcs <;> simp [List.getD] + have hn1 : nzs.tail.getD r 0 = nzs.getD (r + 1) 0 := by cases nzs <;> simp [List.getD] + rw [hz1, hn1] + simp only [List.getD, List.getElem?_cons_succ] at hr' ⊢; exact hr') hst + +theorem chk2Base_sound {u : Bool} {Z T a b : Nat} (h : chk2Base u Z T a b (opsCount u) = true) + {d : Nat} (hd : d < opsCount u) : stepVal u Z d a b ≠ T := by + suffices ∀ n, chk2Base u Z T a b n = true → ∀ d < n, stepVal u Z d a b ≠ T from this (opsCount u) h d hd + intro n; induction n with + | zero => intro _ _ hd0; omega + | succ m ih => + intro hm d hdm + simp only [chk2Base, Bool.and_eq_true, bne_iff_ne, ne_eq] at hm + rcases Nat.lt_or_eq_of_le (Nat.le_of_lt_succ hdm) with hlt | rfl + · exact ih hm.2 d hlt + · exact hm.1 + +theorem chk3Pair_sound {u : Bool} {Z T a b w : Nat} (hZ : Z ≤ full64) + (h : chk3Pair u Z T a b w (opsCount u) = true) + {d1 d2 : Nat} (hd1 : d1 < opsCount u) (hd2 : d2 < opsCount u) : + stepVal u Z d1 a b ≠ T ∧ stepVal u Z d2 (stepVal u Z d1 a b) w ≠ T ∧ + stepVal u Z d2 w (stepVal u Z d1 a b) ≠ T := by + suffices ∀ (d2' : Nat), d2' < opsCount u → ∀ n, chk3Pair u Z T a b w n = true → ∀ d1 < n, + stepVal u Z d1 a b ≠ T ∧ stepVal u Z d2' (stepVal u Z d1 a b) w ≠ T by + have h1 := this d2 hd2 (opsCount u) h d1 hd1 + have h2 := this (flipOp u d2) (flipOp_lt u hd2) (opsCount u) h d1 hd1 + refine ⟨h1.1, h1.2, ?_⟩ + rw [stepVal_flip hZ hd2] + exact h2.2 + intro d2' hd2' n + induction n with + | zero => intro _ _ h0; omega + | succ m ih => + intro hm d1 hdm + simp only [chk3Pair, Bool.and_eq_true, bne_iff_ne, ne_eq] at hm + rcases Nat.lt_or_eq_of_le (Nat.le_of_lt_succ hdm) with hlt | rfl + · exact ih hm.2 d1 hlt + · exact ⟨hm.1.1, chk2Base_sound hm.1.2 hd2'⟩ + +theorem applyStep_two_eq_one {a b c T : Nat} (_ : a < b) (ka kb : Bool) + (heq : applyStep [a, b] a b c ka kb = [T]) : c = T := by + have hmem : c ∈ applyStep [a, b] a b c ka kb := mem_applyStep.mpr (Or.inl rfl) + rw [heq, List.mem_singleton] at hmem + exact hmem + +theorem applyStep_three_to_two {x y z a b c : Nat} + (hpw : ([x, y, z] : List Nat).Pairwise (· < ·)) + (ha : a ∈ ([x, y, z] : List Nat)) (hb : b ∈ ([x, y, z] : List Nat)) (hab : a < b) + (hc : c ∉ ([x, y, z] : List Nat)) (ka kb : Bool) + (hlen : (applyStep [x, y, z] a b c ka kb).length ≤ 2) : + ∃ w, ((a = x ∧ b = y ∧ w = z) ∨ (a = x ∧ b = z ∧ w = y) ∨ (a = y ∧ b = z ∧ w = x)) ∧ + (∀ u, u ∈ applyStep [x, y, z] a b c ka kb ↔ (u = c ∨ u = w)) := by + have hnd : ([x, y, z] : List Nat).Nodup := nodup_of_pairwise hpw + have hcf : c ∉ [x, y, z].filter (fun w => (w != a || ka) && (w != b || kb)) := + fun h => hc (List.mem_filter.mp h).1 + have hln := length_insSort_fresh hcf + unfold applyStep at hlen + rw [hln] at hlen + have hlf := (length_filter_erase2 hnd (Nat.ne_of_lt hab) ha hb ka kb).1 + simp only [List.length_cons, List.length_nil] at hlf hlen + have ⟨hka, hkb⟩ : ka = false ∧ kb = false := by + cases ka <;> cases kb + · exact ⟨rfl, rfl⟩ + · simp at hlf hlen; omega + · simp at hlf hlen; omega + · simp at hlf hlen + subst hka hkb + have hxy : x < y := List.rel_of_pairwise_cons hpw (by simp) + have hxz : x < z := List.rel_of_pairwise_cons hpw (by simp) + have hyz : y < z := List.rel_of_pairwise_cons (List.pairwise_cons.mp hpw).2 (by simp) + simp only [List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at ha hb + rcases ha with rfl | rfl | rfl <;> rcases hb with rfl | rfl | rfl <;> try omega + · refine ⟨z, Or.inl ⟨rfl, rfl, rfl⟩, fun u => ?_⟩ + rw [mem_applyStep] + simp only [List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false, Bool.false_eq_true] + constructor + · rintro (rfl | ⟨hm, h1, h2⟩) + · left; rfl + · right; rcases hm with rfl | rfl | rfl + · exact absurd rfl h1 + · exact absurd rfl h2 + · rfl + · rintro (rfl | rfl) + · left; rfl + · right; exact ⟨Or.inr (Or.inr rfl), (Nat.ne_of_lt hxz).symm, (Nat.ne_of_lt hyz).symm⟩ + · refine ⟨y, Or.inr (Or.inl ⟨rfl, rfl, rfl⟩), fun u => ?_⟩ + rw [mem_applyStep] + simp only [List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false, Bool.false_eq_true] + constructor + · rintro (rfl | ⟨hm, h1, h2⟩) + · left; rfl + · right; rcases hm with rfl | rfl | rfl + · exact absurd rfl h1 + · rfl + · exact absurd rfl h2 + · rintro (rfl | rfl) + · left; rfl + · right; exact ⟨Or.inr (Or.inl rfl), (Nat.ne_of_lt hxy).symm, Nat.ne_of_lt hyz⟩ + · refine ⟨x, Or.inr (Or.inr ⟨rfl, rfl, rfl⟩), fun u => ?_⟩ + rw [mem_applyStep] + simp only [List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false, Bool.false_eq_true] + constructor + · rintro (rfl | ⟨hm, h1, h2⟩) + · left; rfl + · right; rcases hm with rfl | rfl | rfl + · rfl + · exact absurd rfl h1 + · exact absurd rfl h2 + · rintro (rfl | rfl) + · left; rfl + · right; exact ⟨Or.inl rfl, Nat.ne_of_lt hxy, Nat.ne_of_lt hxz⟩ + +theorem chkTree_sound {u : Bool} {Z tab : Nat} (hZ : Z ≤ full64) + {flip : Bool} {mask : Nat} {pts : List Nat} (hPM : ptsMaskOK u flip Z tab mask pts = true) + {rfZ rfL : List Nat} (hRF : RFSubOK u Z tab rfZ rfL) : + ∀ (rem_k : Nat) (S nz zc : List Nat) (lkOut lkA lkB lkOp : Nat), + chkTree u Z (normZ u Z tab) mask pts rfZ rfL rem_k S nz zc lkOut lkA lkB lkOp = true → + NoPathFrom u Z tab rfZ rfL S nz zc lkOut lkA lkB lkOp rem_k := by + intro rem_k + induction rem_k with + | zero => + intro S nz zc lkOut lkA lkB lkOp hchk steps hlen hpor _ + have hlen0 : steps = [] := List.length_eq_zero_iff.mp (Nat.le_zero.mp hlen) + subst hlen0; simp [PORSteps] at hpor; subst hpor + unfold chkTree at hchk; simp at hchk + | succ rk ih => + intro S nz zc lkOut lkA lkB lkOp hchk steps hlen hpor hr + cases steps with + | nil => + simp [PORSteps] at hpor; subst hpor + unfold chkTree at hchk; simp at hchk + | cons st0 rest => + rcases st0 with ⟨a, b, d, ka, kb⟩ + have hbd := porSteps_len_bound hpor + simp only [List.length_cons] at hlen hbd + have hm2 : 2 ≤ S.length := by + rcases hpor with ⟨_, ha, hb, hab, _⟩ + cases S with | nil => simp at ha | cons _ xs => + cases xs with | nil => simp at ha hb; omega | cons _ _ => simp + rcases hpor with ⟨hs, ha, hb, hab, hd, hntc, hcn, hpor_cond, hrest⟩ + unfold chkTree at hchk + have hm0 : (S.length == 0) = false := by rw [beq_eq_false_iff_ne]; omega + have hm1 : (S.length == 1) = false := by rw [beq_eq_false_iff_ne]; omega + have hmle : ¬ (S.length - 1 > rk + 1) := by omega + simp only [hm0, hm1, hmle, Bool.false_eq_true, ↓reduceIte] at hchk + split_ifs at hchk with hrk2 hrk3 + · simp only [Bool.and_eq_true, beq_iff_eq] at hrk2 + have hlen1 : rest = [] := List.length_eq_zero_iff.mp (by omega) + subst hlen1 + simp only [PORSteps] at hrest + have heq_s : S = [a, b] := by + have : S.length = 2 := hrk2.2 + rcases hS_eq : S with _ | ⟨x, _ | ⟨y, _ | _⟩⟩ + · simp [hS_eq] at this + · simp [hS_eq] at this + · have hxy : x < y := List.rel_of_pairwise_cons (hS_eq ▸ hs) (by simp) + rw [hS_eq] at ha hb + simp only [List.mem_cons, List.mem_singleton, List.not_mem_nil, or_false] at ha hb + rcases ha with rfl | rfl <;> rcases hb with rfl | rfl <;> first | rfl | (exfalso; omega) + · simp [hS_eq] at this + subst heq_s + exact absurd (applyStep_two_eq_one hab ka kb hrest) (chk2Base_sound hchk hd) + · simp only [Bool.and_eq_true, beq_iff_eq] at hrk3 + have hlen1 : rest.length ≤ 1 := by omega + have hn_len : (applyStep S a b (stepVal u Z d a b) ka kb).length ≤ 2 := by + have hb2 := porSteps_len_bound hrest; omega + have heq_s : ∃ x y z, S = [x, y, z] := by + have : S.length = 3 := hrk3.2 + rcases S with _ | ⟨x, _ | ⟨y, _ | ⟨z, _ | _⟩⟩⟩ <;> simp at this + exact ⟨x, y, z, rfl⟩ + rcases heq_s with ⟨x, y, z, rfl⟩ + obtain ⟨w, hw_case, hmem2⟩ := applyStep_three_to_two hs ha hb hab hcn ka kb hn_len + simp only [List.getD, List.getElem?_cons_zero, List.getElem?_cons_succ, Option.getD_some, + chk3Base, Bool.and_eq_true] at hchk + have hlen_nxt : (applyStep [x, y, z] a b (stepVal u Z d a b) ka kb).length = 2 := by + have hb2 := porSteps_len_bound hrest + cases rest with + | nil => + simp [PORSteps] at hrest + have hmw : w ∈ [normZ u Z tab] := hrest ▸ (hmem2 w).mpr (Or.inr rfl) + have hmc : stepVal u Z d a b ∈ [normZ u Z tab] := hrest ▸ (hmem2 _).mpr (Or.inl rfl) + simp at hmw hmc + rcases hw_case with ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ <;> + exact absurd (hmc.trans hmw.symm) (by intro h; rw [h] at hcn; exact hcn (by simp)) + | cons g2 r2 => + simp at hlen1 + subst hlen1 + rcases hrest with ⟨_, ha2, hb2, hab2, _⟩ + cases hs' : applyStep [x, y, z] a b (stepVal u Z d a b) ka kb with + | nil => simp [hs'] at ha2 + | cons _ xs => cases xs with + | nil => simp [hs'] at ha2 hb2; omega + | cons _ _ => rw [hs'] at hn_len; simp at hn_len ⊢; omega + have hr_cons : ∃ a2 b2 d2 ka2 kb2, rest = [(a2, b2, d2, ka2, kb2)] := by + cases rest with + | nil => simp [PORSteps] at hrest; rw [hrest] at hlen_nxt; simp at hlen_nxt + | cons g2 r2 => + cases r2 with + | nil => rcases g2 with ⟨a2, b2, d2, ka2, kb2⟩; exact ⟨a2, b2, d2, ka2, kb2, rfl⟩ + | cons _ _ => simp at hlen1 + rcases hr_cons with ⟨a2, b2, d2, ka2, kb2, rfl⟩ + rcases hrest with ⟨_, ha2, hb2, hab2, hd2, _, hc2, _, hrest2⟩ + simp only [PORSteps] at hrest2 + have hpair2 : (a2 = stepVal u Z d a b ∧ b2 = w) ∨ (a2 = w ∧ b2 = stepVal u Z d a b) := by + rw [hmem2 a2] at ha2; rw [hmem2 b2] at hb2 + rcases ha2 with rfl | rfl <;> rcases hb2 with rfl | rfl <;> omega + have hT2 : stepVal u Z d2 a2 b2 = normZ u Z tab := by + have hmem_t : normZ u Z tab ∈ applyStep (applyStep [x, y, z] a b (stepVal u Z d a b) ka kb) + a2 b2 (stepVal u Z d2 a2 b2) ka2 kb2 := by rw [hrest2]; exact List.mem_cons_self + rw [mem_applyStep] at hmem_t + rcases hmem_t with h | ⟨hmS, h1, h2⟩ + · exact h.symm + · exfalso + have hln2 : (applyStep (applyStep [x, y, z] a b (stepVal u Z d a b) ka kb) a2 b2 + (stepVal u Z d2 a2 b2) ka2 kb2).length = 1 := by rw [hrest2]; simp + have hcf2 : stepVal u Z d2 a2 b2 ∉ (applyStep [x, y, z] a b (stepVal u Z d a b) ka kb).filter + (fun u0 => (u0 != a2 || ka2) && (u0 != b2 || kb2)) := + fun h => hc2 (List.mem_filter.mp h).1 + change (insSort (stepVal u Z d2 a2 b2) ((applyStep [x, y, z] a b (stepVal u Z d a b) ka kb).filter + (fun u0 => (u0 != a2 || ka2) && (u0 != b2 || kb2)))).length = 1 at hln2 + rw [length_insSort_fresh hcf2] at hln2 + have hfl0 : (applyStep [x, y, z] a b (stepVal u Z d a b) ka kb).filter + (fun u0 => (u0 != a2 || ka2) && (u0 != b2 || kb2)) = [] := + List.length_eq_zero_iff.mp (by omega) + have hin_f : normZ u Z tab ∈ (applyStep [x, y, z] a b (stepVal u Z d a b) ka kb).filter + (fun u0 => (u0 != a2 || ka2) && (u0 != b2 || kb2)) := by + rw [List.mem_filter]; refine ⟨hmS, ?_⟩ + simp only [Bool.and_eq_true, Bool.or_eq_true, bne_iff_ne, ne_eq] + exact ⟨h1, h2⟩ + rw [hfl0] at hin_f; simp at hin_f + rcases hw_case with ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ + · have hs3 := chk3Pair_sound hZ hchk.1.1 hd hd2 + rcases hpair2 with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ + · exact absurd hT2 hs3.2.1 + · exact absurd hT2 hs3.2.2 + · have hs3 := chk3Pair_sound hZ hchk.1.2 hd hd2 + rcases hpair2 with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ + · exact absurd hT2 hs3.2.1 + · exact absurd hT2 hs3.2.2 + · have hs3 := chk3Pair_sound hZ hchk.2 hd hd2 + rcases hpair2 with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ + · exact absurd hT2 hs3.2.1 + · exact absurd hT2 hs3.2.2 + · have ⟨hlenRF, _⟩ := rfSubOK_getD hRF + have hstep_ops : ∀ a0 b0, a0 = a → b0 = b → + chkOps u Z (normZ u Z tab) mask pts rfZ rfL rk S.length S nz zc lkOut lkA lkB lkOp a0 b0 + (chkTree u Z (normZ u Z tab) mask pts rfZ rfL rk) 0 (opsCount u) = true → + False := by + intro a0 b0 ha0 hb0 hops; rw [ha0, hb0] at hops + suffices ∀ op cur, d ≥ op → d < op + cur → + chkOps u Z (normZ u Z tab) mask pts rfZ rfL rk S.length S nz zc lkOut lkA lkB lkOp a b + (chkTree u Z (normZ u Z tab) mask pts rfZ rfL rk) op cur = true → False from + this 0 (opsCount u) (Nat.zero_le _) (by omega) hops + intro op cur + induction cur generalizing op with + | zero => intro _ _ _; omega + | succ k ih_op => + intro hge hlt hops_c + unfold chkOps at hops_c + rw [Bool.and_eq_true] at hops_c + rcases Nat.eq_or_lt_of_le hge with hop | hgt + · rw [hop] at hops_c + have hok := hops_c.1 + have hpor_ok : ¬ ((lkOut != 0 && a != lkOut && b != lkOut) && + !(lkA < a || (lkA == a && (lkB < b || (lkB == b && lkOp < d))))) = true := by + simp only [Bool.and_eq_true, bne_iff_ne, ne_eq, Bool.not_eq_true', + Bool.or_eq_false_iff, Bool.and_eq_false_iff, beq_eq_false_iff_ne, + decide_eq_false_iff_not, not_lt] + rintro ⟨⟨⟨hlo, hna⟩, hnb⟩, hbad⟩ + rcases hpor_cond with h | h | h | hlt | ⟨rfl, hrest_cond⟩ <;> simp_all <;> omega + have hsk : ((lkOut != 0 && a != lkOut && b != lkOut) && + !(lkA < a || (lkA == a && (lkB < b || (lkB == b && lkOp < d))))) = false := + Bool.eq_false_iff.mpr hpor_ok + simp only [Bool.and_assoc] at hsk hok + simp only [hsk, Bool.false_eq_true, ↓reduceIte] at hok + let c := stepVal u Z d a b + have hc_nt : trivW u Z c = false := by + change trivW u Z (stepVal u Z d a b) = false + cases ht : trivW u Z (stepVal u Z d a b) + · rfl + · simp [ht] at hntc + have hc_ne : S.any (· == c) = false := by + simp only [List.any_eq_false, beq_iff_eq] + intro w hw heq + dsimp only [c] at heq + exact hcn (heq ▸ hw) + by_cases hcT : (stepVal u Z d a b == normZ u Z tab) = true + · simp only [hcT, ↓reduceIte] at hok; exact Bool.noConfusion hok + · dsimp only [c] at hc_nt hc_ne + simp only [hcT, Bool.false_eq_true, ↓reduceIte, hc_nt, Bool.false_or, hc_ne] at hok + suffices ∀ kps, (ka, kb) ∈ kps → + chkKps u Z (normZ u Z tab) mask pts rfZ rfL rk S.length S a b c + (updNz u S a b c rfZ nz) zc d (chkTree u Z (normZ u Z tab) mask pts rfZ rfL rk) kps = true → + False from + this kpList (by cases ka <;> cases kb <;> simp [kpList]) hok + intro kps + induction kps with + | nil => intro hm _; simp at hm + | cons kp1 kprest ih_kp => + intro hm_kp hkp + unfold chkKps at hkp + rw [Bool.and_eq_true] at hkp + rcases List.mem_cons.mp hm_kp with rfl | hm_rest + · have hok1 := hkp.1 + have hbr := porSteps_len_bound hrest + have hc_ins : c ∉ S.filter (fun w => (w != a || ka) && (w != b || kb)) := + fun h => hcn (List.mem_filter.mp h).1 + have hln_nxt : (applyStep S a b c ka kb).length = + (S.filter (fun w => (w != a || ka) && (w != b || kb))).length + 1 := + length_insSort_fresh hc_ins + have hlf := (length_filter_erase2 (nodup_of_pairwise hs) (Nat.ne_of_lt hab) ha hb ka kb).1 + have hS2 : 2 ≤ S.length := by + have : ∀ L : List Nat, a ∈ L → b ∈ L → a < b → 2 ≤ L.length := by + rintro (_ | ⟨x, _ | ⟨y, ys⟩⟩) h1 h2 hlt <;> simp at h1 h2 ⊢ <;> omega + exact this S ha hb hab + have hcost_ok : ¬ (((if ka then 1 else 0) + (if kb then 1 else 0)) > rk + 1 - (S.length - 1)) := by + dsimp only [c] at hln_nxt + rw [hln_nxt, hlf] at hbr + cases ka <;> cases kb <;> simp at hbr ⊢ <;> omega + simp only [hcost_ok, ↓reduceIte] at hok1 + cases hzc_opt : stepZc rk a b c ka kb rfZ rfL (updNz u S a b c rfZ nz) zc with + | none => exact rInv_pruned_impossible hZ hRF hs ha hb hab hcn rest (by omega) hrest hr hzc_opt + | some zcNext => + rw [hzc_opt] at hok1 + simp only at hok1 + split_ifs at hok1 with hval + · simp only [Bool.and_eq_true, Bool.not_eq_true'] at hval + exact validOK_false_no_por hZ hPM (fun _ h => h) hval.2 hrest + · exact ih (applyStep S a b c ka kb) (updNz u S a b c rfZ nz) zcNext c a b d hok1 + rest (by omega) hrest (rInv_step hlenRF hs ha hb hab hcn rest hr hzc_opt) + · exact ih_kp hm_rest hkp.2 + · exact ih_op (op + 1) (by omega) (by omega) hops_c.2 + have hwalk_all : ∀ xs : List Nat, xs.Pairwise (· < ·) → a ∈ xs → b ∈ xs → + chkAllPairs u Z (normZ u Z tab) mask pts rfZ rfL rk S.length S nz zc lkOut lkA lkB lkOp + (chkTree u Z (normZ u Z tab) mask pts rfZ rfL rk) xs = true → False := by + intro xs + induction xs with + | nil => intro _ hax _ _; simp at hax + | cons x0 bs ih_xs => + intro hpw hax hbx hall + unfold chkAllPairs at hall + rw [Bool.and_eq_true] at hall + have hx0_lt : ∀ y ∈ bs, x0 < y := fun y hy => List.rel_of_pairwise_cons hpw hy + have hpw_bs := (List.pairwise_cons.mp hpw).2 + simp only [List.mem_cons] at hax hbx + rcases hax with rfl | ha_bs + · rcases hbx with rfl | hb_bs + · omega + · suffices ∀ ys : List Nat, b ∈ ys → + chkPairsWith u Z (normZ u Z tab) mask pts rfZ rfL rk S.length S nz zc lkOut lkA lkB lkOp a + (chkTree u Z (normZ u Z tab) mask pts rfZ rfL rk) ys = true → False from + this bs hb_bs hall.1 + intro ys; induction ys with + | nil => intro hby _; simp at hby + | cons y0 ys' ih_ys => + intro hby hp_w + unfold chkPairsWith at hp_w + rw [Bool.and_eq_true] at hp_w + rcases List.mem_cons.mp hby with rfl | hy_ys + · exact hstep_ops a b rfl rfl hp_w.1 + · exact ih_ys hy_ys hp_w.2 + · rcases hbx with rfl | hb_bs + · exact absurd (hx0_lt a ha_bs) (by omega) + · exact ih_xs hpw_bs ha_bs hb_bs hall.2 + exact hwalk_all S hs ha hb hchk + +def rootFail (rfZ rfL cur : List Nat) (k : Nat) : Bool := + (List.zip rfZ rfL).any (fun r => k < r.2 + zCount cur r.1) + +def chkSubsDirect (u : Bool) (Z target mask : Nat) (pts rfZ rfL : List Nat) (k : Nat) : + List Nat → List Nat → Bool + | rem, cur => + if !validOK u Z mask (cur ++ rem) pts then true + else match rem with + | [] => + if cur.isEmpty || cur.length - 1 > k || rootFail rfZ rfL cur k then true + else + chkTree u Z target mask pts rfZ rfL k cur + (List.replicate rfZ.length 0) (rfZ.map (zCount cur)) 0 0 0 0 + | v :: vs => + chkSubsDirect u Z target mask pts rfZ rfL k vs cur && + chkSubsDirect u Z target mask pts rfZ rfL k vs (cur ++ [v]) + +theorem rInv_init {u : Bool} {Z tab : Nat} (hZ : Z ≤ full64) + {rfZ rfL : List Nat} (hRF : RFSubOK u Z tab rfZ rfL) + {S₀ : List Nat} {steps : List (Nat × Nat × Nat × Bool × Bool)} + (hS0_pw : S₀.Pairwise (· < ·)) (hS0_sub : ∀ w ∈ S₀, w ∈ varsSorted u Z) + (hpor : PORSteps u Z (normZ u Z tab) 0 0 0 0 S₀ steps) : + RInv u Z rfZ rfL S₀ (List.replicate rfZ.length 0) (rfZ.map (zCount S₀)) steps := by + have ⟨hlen, hsub⟩ := rfSubOK_getD hRF + intro r hr + obtain ⟨hZr, hZb, hnt, hlow⟩ := hsub r hr + have hrep : (List.replicate rfZ.length 0).getD r 0 = 0 := by + rcases Nat.lt_or_ge r rfZ.length with h | h <;> simp [List.getD, List.getElem?_replicate, h] + have hmap : (rfZ.map (zCount S₀)).getD r 0 = zCount S₀ (rfZ.getD r 0) := by + simp [List.getD, List.getElem?_map, List.getElem?_eq_getElem hr] + refine ⟨hmap, ?_⟩ + rw [hrep, Nat.zero_add] + have hsub_pool : ∀ w ∈ S₀, !trivW u (rfZ.getD r 0) w = true → normZ u (rfZ.getD r 0) w ∈ varsOf u (rfZ.getD r 0) := by + intro w hw hwnt + have hwv := mem_dedupSort.mp (hS0_sub w hw) + unfold varsOf at hwv + rw [List.mem_filter, List.mem_map] at hwv + rcases hwv with ⟨⟨x, hx, rfl⟩, _⟩ + rw [normZ_idem u hZr hZb] + exact mem_varsOf_of_norm hZr hx (by + rw [← normZ_trivW u hZr, normZ_idem u hZr hZb, normZ_trivW u hZr] at hwnt; exact hwnt) + obtain ⟨P', hPlen, hP⟩ := validProg_proj_from_por hZr hZ hZb hsub_pool hpor (by + rw [← normZ_trivW u hZr, normZ_idem u hZr hZb, normZ_trivW u hZr]; exact hnt) + have hlow_P := hlow P' (by rwa [normZ_idem u hZr hZb] at hP) + omega + +theorem rootFail_impossible {u : Bool} {Z tab k : Nat} (hZ : Z ≤ full64) + {rfZ rfL : List Nat} (hRF : RFSubOK u Z tab rfZ rfL) + {S₀ : List Nat} {steps : List (Nat × Nat × Nat × Bool × Bool)} + (hS0_pw : S₀.Pairwise (· < ·)) (hS0_sub : ∀ w ∈ S₀, w ∈ varsSorted u Z) + (hlen : steps.length ≤ k) + (hpor : PORSteps u Z (normZ u Z tab) 0 0 0 0 S₀ steps) + (hrf : rootFail rfZ rfL S₀ k = true) : False := by + have hr_init := rInv_init hZ hRF hS0_pw hS0_sub hpor + have ⟨hzip, _⟩ := rfSubOK_getD hRF + unfold rootFail at hrf + rw [List.any_eq_true] at hrf + rcases hrf with ⟨⟨Zr, Lr⟩, hm_zip, hlt⟩ + simp only [decide_eq_true_eq] at hlt + obtain ⟨r, hr, hget⟩ := List.mem_iff_getElem.mp hm_zip + have hrl : r < rfZ.length := by simp at hr; omega + have hZeq : rfZ.getD r 0 = Zr := by + have := congrArg Prod.fst hget + simp [List.getElem_zip] at this + simpa [List.getD, List.getElem?_eq_getElem hrl] using this + have hLeq : rfL.getD r 0 = Lr := by + have := congrArg Prod.snd hget + simp [List.getElem_zip] at this + simpa [List.getD, List.getElem?_eq_getElem (by omega : r < rfL.length)] using this + obtain ⟨hzc_eq, hnz_le⟩ := hr_init r hrl + have ⟨_, hsub⟩ := rfSubOK_getD hRF + obtain ⟨hZr, hZb, hnt, _⟩ := hsub r hrl + rw [hZeq] at hnt hZb hZr + have htriv := zCount_le_trivSteps (u := u) (Zr := Zr) hZr hZb (by + rw [← normZ_trivW u hZr, normZ_idem u hZr hZb, normZ_trivW u hZr]; exact hnt) hpor + have hsp := count_split_steps u Z Zr S₀ steps + have hnz0 : (List.replicate rfZ.length 0).getD r 0 = 0 := by simp [List.getD, List.getElem?_replicate, hrl] + rw [hnz0, Nat.zero_add, hZeq, hLeq] at hnz_le + omega + +theorem chkSubsDirect_sound {u : Bool} {Z tab : Nat} (hZ : Z ≤ full64) + {flip : Bool} {mask : Nat} {pts : List Nat} (hPM : ptsMaskOK u flip Z tab mask pts = true) + {rfZ rfL : List Nat} (hRF : RFSubOK u Z tab rfZ rfL) + (k : Nat) : + ∀ (rem cur : List Nat), + (cur ++ rem).Pairwise (· < ·) → + chkSubsDirect u Z (normZ u Z tab) mask pts rfZ rfL k rem cur = true → + ∀ (S₀ : List Nat) (steps : List (Nat × Nat × Nat × Bool × Bool)), + S₀.Pairwise (· < ·) → (∀ w ∈ S₀, w ∈ varsSorted u Z) → + (∀ w ∈ cur, w ∈ S₀) → (∀ w ∈ S₀, w ∈ cur ++ rem) → + steps.length ≤ k → + PORSteps u Z (normZ u Z tab) 0 0 0 0 S₀ steps → False := by + intro rem + induction rem with + | nil => + intro cur hcpw hchk S₀ steps hS0pw hS0v hcurS hS0c hlen hpor + have hcur_pw : cur.Pairwise (· < ·) := by simpa using hcpw + have hS0_eq : S₀ = cur := pairwise_eq_of_mem_iff hS0pw hcur_pw (fun w => ⟨fun h => by simpa using hS0c w h, hcurS w⟩) + subst hS0_eq + unfold chkSubsDirect at hchk + split_ifs at hchk with hv hbad + · simp only [Bool.not_eq_true'] at hv + exact validOK_false_no_por hZ hPM (fun _ h => h) (by simpa using hv) hpor + · simp only [Bool.or_eq_true, decide_eq_true_eq] at hbad + rcases hbad with (hce | hmlen) | hrf + · have hce2 : S₀ = [] := List.isEmpty_iff.mp hce + subst hce2 + cases steps with | nil => simp [PORSteps] at hpor | cons g _ => rcases hpor with ⟨_, ha, _⟩; simp at ha + · have hbd := porSteps_len_bound hpor; omega + · exact rootFail_impossible hZ hRF hS0pw hS0v hlen hpor hrf + · exact chkTree_sound hZ hPM hRF k S₀ (List.replicate rfZ.length 0) (rfZ.map (zCount S₀)) + 0 0 0 0 hchk steps hlen hpor (rInv_init hZ hRF hS0pw hS0v hpor) + | cons v vs ih => + intro cur hcpw hchk S₀ steps hS0pw hS0v hcurS hS0c hlen hpor + unfold chkSubsDirect at hchk; dsimp only at hchk + split_ifs at hchk with hv + · simp only [Bool.not_eq_true'] at hv + exact validOK_false_no_por hZ hPM hS0c (by simpa using hv) hpor + · rw [Bool.and_eq_true] at hchk + have hpw_skip : (cur ++ vs).Pairwise (· < ·) := by + refine List.pairwise_append.mpr ⟨(List.pairwise_append.mp hcpw).1, + (List.pairwise_cons.mp (List.pairwise_append.mp hcpw).2.1).2, fun a ha b hb => ?_⟩ + exact (List.pairwise_append.mp hcpw).2.2 a ha b (List.mem_cons_of_mem _ hb) + have hpw_keep : ((cur ++ [v]) ++ vs).Pairwise (· < ·) := by + have heq : (cur ++ [v]) ++ vs = cur ++ v :: vs := by simp [List.append_assoc] + rw [heq]; exact hcpw + by_cases hvS : v ∈ S₀ + · exact ih (cur ++ [v]) hpw_keep hchk.2 S₀ steps hS0pw hS0v + (fun w hw => by simp at hw; rcases hw with hw | rfl; exact hcurS w hw; exact hvS) + (fun w hw => by have hw1 := hS0c w hw; simp at hw1 ⊢; tauto) hlen hpor + · exact ih cur hpw_skip hchk.1 S₀ steps hS0pw hS0v hcurS + (fun w hw => by + have hw1 := hS0c w hw; simp at hw1 ⊢ + rcases hw1 with hw_c | rfl | hw_vs + · left; exact hw_c + · exact absurd hw hvS + · right; exact hw_vs) hlen hpor + +def baseOK (u flip : Bool) (Z tab mask : Nat) (pts rfZ rfL : List Nat) : Bool := + !(full64 < Z) && + !trivW u Z tab && + !(varsSorted u Z).any (· == normZ u Z tab) && + (rfZ.length == rfL.length) && + rfZ.all (fun Zr => !(full64 < Zr) && (Zr &&& Z == Zr) && !trivW u Zr tab) && + ptsMaskOK u flip Z tab mask pts + +def checkLowU (u : Bool) (Z tab mask : Nat) (pts rfZ rfL : List Nat) (k : Nat) : Bool := + chkSubsDirect u Z (normZ u Z tab) mask pts rfZ rfL k (varsSorted u Z) [] + +theorem prove_lowU (u flip : Bool) (Z tab mask : Nat) (pts rfZ rfL : List Nat) (k : Nat) + (hB : baseOK u flip Z tab mask pts rfZ rfL = true) + (hSub : RFSubOK u Z tab rfZ rfL) + (hChk : checkLowU u Z tab mask pts rfZ rfL k = true) : + LowU u Z tab (k + 1) := by + intro P hP + by_contra hlt + have hle : P.length ≤ k := by omega + unfold baseOK at hB + simp only [Bool.and_eq_true, Bool.not_eq_true', decide_eq_false_iff_not] at hB + have hZ : Z ≤ full64 := by omega + have hnt : !trivW u Z tab = true := by + have : trivW u Z tab = false := hB.1.1.1.1.2 + simp [this] + have hTnot : normZ u Z tab ∉ varsSorted u Z := by + intro hm + have ha : (varsSorted u Z).any (· == normZ u Z tab) = true := List.any_eq_true.mpr ⟨_, hm, by simp⟩ + rw [ha] at hB; simp at hB + have hPM : ptsMaskOK u flip Z tab mask pts = true := hB.2 + obtain ⟨S₀, steps, hS0, hS0pw, hpor, hslen⟩ := validProg_to_PORSteps hZ hnt hTnot hP + unfold checkLowU at hChk + exact chkSubsDirect_sound hZ hPM hSub k (varsSorted u Z) [] (dedupSort_pairwise (varsOf u Z)) hChk + S₀ steps hS0pw hS0 (fun _ h => by simp at h) (fun w hw => by simpa using hS0 w hw) (by omega) hpor + +def NoPathJob (u : Bool) (Z tab : Nat) (rfZ rfL : List Nat) + (j : List Nat × List Nat × List Nat × Nat × Nat × Nat × Nat × Nat) : Prop := + NoPathFrom u Z tab rfZ rfL j.1 j.2.1 j.2.2.1 j.2.2.2.2.1 j.2.2.2.2.2.1 j.2.2.2.2.2.2.1 j.2.2.2.2.2.2.2 j.2.2.2.1 + +def chkJobs (u : Bool) (Z tab mask : Nat) (pts rfZ rfL : List Nat) + (jobs : List (List Nat × List Nat × List Nat × Nat × Nat × Nat × Nat × Nat)) : Bool := + jobs.all (fun j => + chkTree u Z (normZ u Z tab) mask pts rfZ rfL j.2.2.2.1 j.1 j.2.1 j.2.2.1 + j.2.2.2.2.1 j.2.2.2.2.2.1 j.2.2.2.2.2.2.1 j.2.2.2.2.2.2.2) + +theorem chkJobs_sound {u : Bool} {Z tab : Nat} (hZ : Z ≤ full64) + {flip : Bool} {mask : Nat} {pts : List Nat} (hPM : ptsMaskOK u flip Z tab mask pts = true) + {rfZ rfL : List Nat} (hRF : RFSubOK u Z tab rfZ rfL) + (jobs : List (List Nat × List Nat × List Nat × Nat × Nat × Nat × Nat × Nat)) + (h : chkJobs u Z tab mask pts rfZ rfL jobs = true) : + ∀ j ∈ jobs, NoPathJob u Z tab rfZ rfL j := by + intro j hj + exact chkTree_sound hZ hPM hRF _ _ _ _ _ _ _ _ ((List.all_eq_true.mp h) j hj) + +def chkSubsCollect (u : Bool) (Z target mask : Nat) (pts rfZ rfL : List Nat) (k : Nat) : + List Nat → List Nat → + List (List Nat × List Nat × List Nat × Nat × Nat × Nat × Nat × Nat) → + Option (List (List Nat × List Nat × List Nat × Nat × Nat × Nat × Nat × Nat)) + | rem, cur, jobs => + if !validOK u Z mask (cur ++ rem) pts then some jobs + else match rem with + | [] => + if cur.isEmpty || cur.length - 1 > k || rootFail rfZ rfL cur k then some jobs + else match jobs with + | [] => none + | j :: js => + if j == (cur, List.replicate rfZ.length 0, rfZ.map (zCount cur), k, 0, 0, 0, 0) then some js + else none + | v :: vs => + match chkSubsCollect u Z target mask pts rfZ rfL k vs cur jobs with + | none => none + | some j1 => chkSubsCollect u Z target mask pts rfZ rfL k vs (cur ++ [v]) j1 + +theorem chkSubsCollect_sound {u : Bool} {Z tab : Nat} (hZ : Z ≤ full64) + {flip : Bool} {mask : Nat} {pts : List Nat} (hPM : ptsMaskOK u flip Z tab mask pts = true) + {rfZ rfL : List Nat} (hRF : RFSubOK u Z tab rfZ rfL) + (k : Nat) : + ∀ (rem cur : List Nat) + (jobs jobs' : List (List Nat × List Nat × List Nat × Nat × Nat × Nat × Nat × Nat)), + (cur ++ rem).Pairwise (· < ·) → + chkSubsCollect u Z (normZ u Z tab) mask pts rfZ rfL k rem cur jobs = some jobs' → + (∀ j ∈ jobs, NoPathJob u Z tab rfZ rfL j) → + ∀ (S₀ : List Nat) (steps : List (Nat × Nat × Nat × Bool × Bool)), + S₀.Pairwise (· < ·) → (∀ w ∈ S₀, w ∈ varsSorted u Z) → + (∀ w ∈ cur, w ∈ S₀) → (∀ w ∈ S₀, w ∈ cur ++ rem) → + steps.length ≤ k → + PORSteps u Z (normZ u Z tab) 0 0 0 0 S₀ steps → False := by + intro rem + induction rem with + | nil => + intro cur jobs jobs' hcpw hchk hJOK S₀ steps hS0pw hS0v hcurS hS0c hlen hpor + have hcur_pw : cur.Pairwise (· < ·) := by simpa using hcpw + have hS0_eq : S₀ = cur := pairwise_eq_of_mem_iff hS0pw hcur_pw (fun w => ⟨fun h => by simpa using hS0c w h, hcurS w⟩) + subst hS0_eq + unfold chkSubsCollect at hchk + split_ifs at hchk with hv hbad + · simp only [Bool.not_eq_true'] at hv + exact validOK_false_no_por hZ hPM (fun _ h => h) (by simpa using hv) hpor + · simp only [Bool.or_eq_true, decide_eq_true_eq] at hbad + rcases hbad with (hce | hmlen) | hrf + · have hce2 : S₀ = [] := List.isEmpty_iff.mp hce + subst hce2 + cases steps with | nil => simp [PORSteps] at hpor | cons g _ => rcases hpor with ⟨_, ha, _⟩; simp at ha + · have hbd := porSteps_len_bound hpor; omega + · exact rootFail_impossible hZ hRF hS0pw hS0v hlen hpor hrf + · cases jobs with + | nil => simp at hchk + | cons j js => + simp only at hchk + split_ifs at hchk with heq + have hjeq := beq_iff_eq.mp heq + have hnj := hJOK j List.mem_cons_self + rw [hjeq] at hnj + exact hnj steps hlen hpor (rInv_init hZ hRF hS0pw hS0v hpor) + | cons v vs ih => + intro cur jobs jobs' hcpw hchk hJOK S₀ steps hS0pw hS0v hcurS hS0c hlen hpor + unfold chkSubsCollect at hchk; dsimp only at hchk + split_ifs at hchk with hv + · simp only [Bool.not_eq_true'] at hv + exact validOK_false_no_por hZ hPM hS0c (by simpa using hv) hpor + · cases hc1 : chkSubsCollect u Z (normZ u Z tab) mask pts rfZ rfL k vs cur jobs with + | none => rw [hc1] at hchk; simp at hchk + | some j1 => + have hJ1 : ∀ j ∈ j1, NoPathJob u Z tab rfZ rfL j := by + suffices ∀ (rm cr : List Nat) jb jb', + chkSubsCollect u Z (normZ u Z tab) mask pts rfZ rfL k rm cr jb = some jb' → + ∃ pref, jb = pref ++ jb' from by + obtain ⟨pref, hpf⟩ := this vs cur jobs j1 hc1 + intro j hj; exact hJOK j (by rw [hpf]; simp [hj]) + intro rm; induction rm with + | nil => + intro cr jb jb' hs + unfold chkSubsCollect at hs + split_ifs at hs + · injection hs with h0; subst_vars; exact ⟨[], rfl⟩ + · injection hs with h0; subst_vars; exact ⟨[], rfl⟩ + · cases jb with | nil => simp at hs | cons jx jxs => + simp only at hs; split_ifs at hs + injection hs with h0; subst h0; exact ⟨[jx], by simp⟩ + | cons x xs ihx => + intro cr jb jb' hs + unfold chkSubsCollect at hs + by_cases hv2 : (!validOK u Z mask (cr ++ x :: xs) pts) = true + · simp only [hv2, ↓reduceIte] at hs; injection hs with h0; subst h0; exact ⟨[], rfl⟩ + · simp only [hv2, Bool.false_eq_true, ↓reduceIte] at hs + cases hs1 : chkSubsCollect u Z (normZ u Z tab) mask pts rfZ rfL k xs cr jb with + | none => rw [hs1] at hs; simp at hs + | some jM => + rw [hs1] at hs + obtain ⟨p1, hp1⟩ := ihx cr jb jM hs1 + obtain ⟨p2, hp2⟩ := ihx (cr ++ [x]) jM jb' hs + exact ⟨p1 ++ p2, by rw [hp1, hp2, List.append_assoc]⟩ + have hpw_skip : (cur ++ vs).Pairwise (· < ·) := by + refine List.pairwise_append.mpr ⟨(List.pairwise_append.mp hcpw).1, + (List.pairwise_cons.mp (List.pairwise_append.mp hcpw).2.1).2, fun a ha b hb => ?_⟩ + exact (List.pairwise_append.mp hcpw).2.2 a ha b (List.mem_cons_of_mem _ hb) + have hpw_keep : ((cur ++ [v]) ++ vs).Pairwise (· < ·) := by + have heq : (cur ++ [v]) ++ vs = cur ++ v :: vs := by simp [List.append_assoc] + rw [heq]; exact hcpw + by_cases hvS : v ∈ S₀ + · rw [hc1] at hchk + exact ih (cur ++ [v]) j1 jobs' hpw_keep hchk hJ1 S₀ steps hS0pw hS0v + (fun w hw => by simp at hw; rcases hw with hw | rfl; exact hcurS w hw; exact hvS) + (fun w hw => by have hw1 := hS0c w hw; simp at hw1 ⊢; tauto) hlen hpor + · exact ih cur jobs j1 hpw_skip hc1 hJOK S₀ steps hS0pw hS0v hcurS + (fun w hw => by + have hw1 := hS0c w hw; simp at hw1 ⊢ + rcases hw1 with hw_c | rfl | hw_vs + · left; exact hw_c + · exact absurd hw hvS + · right; exact hw_vs) hlen hpor + +theorem prove_lowU_jobs (u flip : Bool) (Z tab mask : Nat) (pts rfZ rfL : List Nat) (k : Nat) + (jobs : List (List Nat × List Nat × List Nat × Nat × Nat × Nat × Nat × Nat)) + (hB : baseOK u flip Z tab mask pts rfZ rfL = true) + (hSub : RFSubOK u Z tab rfZ rfL) + (hCol : (chkSubsCollect u Z (normZ u Z tab) mask pts rfZ rfL k (varsSorted u Z) [] jobs).isSome = true) + (hJobs : ∀ j ∈ jobs, NoPathJob u Z tab rfZ rfL j) : + LowU u Z tab (k + 1) := by + intro P hP + by_contra hlt + have hle : P.length ≤ k := by omega + unfold baseOK at hB + simp only [Bool.and_eq_true, Bool.not_eq_true', decide_eq_false_iff_not] at hB + have hZ : Z ≤ full64 := by omega + have hnt : !trivW u Z tab = true := by + have : trivW u Z tab = false := hB.1.1.1.1.2 + simp [this] + have hTnot : normZ u Z tab ∉ varsSorted u Z := by + intro hm + have ha : (varsSorted u Z).any (· == normZ u Z tab) = true := List.any_eq_true.mpr ⟨_, hm, by simp⟩ + rw [ha] at hB; simp at hB + have hPM : ptsMaskOK u flip Z tab mask pts = true := hB.2 + obtain ⟨S₀, steps, hS0, hS0pw, hpor, hslen⟩ := validProg_to_PORSteps hZ hnt hTnot hP + rcases Option.isSome_iff_exists.mp hCol with ⟨j1, hsome⟩ + exact chkSubsCollect_sound hZ hPM hSub k (varsSorted u Z) [] jobs j1 + (dedupSort_pairwise (varsOf u Z)) hsome hJobs S₀ steps hS0pw hS0 (fun _ h => by simp at h) + (fun w hw => by simpa using hS0 w hw) (by omega) hpor + +theorem nbeq_eq (a b : Nat) : Nat.beq a b = (a == b) := rfl + +theorem nbeq_false {a b : Nat} : Nat.beq a b = false ↔ a ≠ b := by + rw [← Bool.not_eq_true, Nat.beq_eq] + +theorem nblt_false {a b : Nat} : Nat.blt a b = false ↔ ¬ (a < b) := by + rw [← Bool.not_eq_true, Nat.blt_eq] + +def anyEq (c : Nat) : List Nat → Bool + | [] => false + | w :: ws => Nat.beq w c || anyEq c ws + +theorem anyEq_false_not_mem {c : Nat} : ∀ {S : List Nat}, anyEq c S = false → c ∉ S + | [], _ => by simp + | w :: ws, h => by + unfold anyEq at h + rw [Bool.or_eq_false_iff, nbeq_false] at h + intro hm + rcases List.mem_cons.mp hm with rfl | hm' + · exact h.1 rfl + · exact anyEq_false_not_mem h.2 hm' + +def anyEqZ (u : Bool) (Zr cz czi : Nat) : List Nat → Bool + | [] => false + | w :: ws => + let wz := w &&& Zr + Nat.beq wz cz || (u && Nat.beq wz czi) || anyEqZ u Zr cz czi ws + +theorem eqZ_eq_fast (u : Bool) (Zr c w : Nat) : + eqZ u Zr c w = (Nat.beq (w &&& Zr) (c &&& Zr) || (u && Nat.beq (w &&& Zr) ((c &&& Zr) ^^^ Zr))) := by + unfold eqZ; dsimp only + apply Bool.eq_iff_iff.mpr + simp only [Bool.or_eq_true, Bool.and_eq_true, beq_iff_eq, Nat.beq_eq] + have hxor : (c &&& Zr) = (w &&& Zr) ^^^ Zr ↔ (w &&& Zr) = (c &&& Zr) ^^^ Zr := by + constructor <;> intro h <;> rw [h, Nat.xor_assoc, Nat.xor_self, Nat.xor_zero] + rw [eq_comm (a := c &&& Zr) (b := w &&& Zr), hxor] + +theorem anyEqZ_eq_any (u : Bool) (Zr c : Nat) : + ∀ S : List Nat, + anyEqZ u Zr (c &&& Zr) (if u then (c &&& Zr) ^^^ Zr else 0) S = S.any (fun w => eqZ u Zr c w) + | [] => rfl + | w :: ws => by + unfold anyEqZ + simp only [List.any_cons, anyEqZ_eq_any u Zr c ws] + congr 1 + cases u with + | false => simp [eqZ_eq_fast] + | true => simp [eqZ_eq_fast] + +def isTrivFast (u : Bool) (Zr : Nat) (S : List Nat) (a b c : Nat) : Bool := + let az := a &&& Zr + if Nat.beq az 0 || Nat.beq az Zr then true + else + let bz := b &&& Zr + if Nat.beq bz 0 || Nat.beq bz Zr then true + else + let cz := c &&& Zr + if Nat.beq cz 0 || Nat.beq cz Zr then true + else anyEqZ u Zr cz (if u then cz ^^^ Zr else 0) S + +theorem isTrivFast_eq {u : Bool} {Zr a b c : Nat} {S : List Nat} (ha : a ∈ S) (hb : b ∈ S) : + isTrivFast u Zr S a b c = isTrivStep u Zr S a b c := by + unfold isTrivFast isTrivStep + dsimp only + have hta : (Nat.beq (a &&& Zr) 0 || Nat.beq (a &&& Zr) Zr) = trivW u Zr a := rfl + have htb : (Nat.beq (b &&& Zr) 0 || Nat.beq (b &&& Zr) Zr) = trivW u Zr b := rfl + have htc : (Nat.beq (c &&& Zr) 0 || Nat.beq (c &&& Zr) Zr) = trivW u Zr c := rfl + rw [hta, htb, htc, anyEqZ_eq_any] + cases trivW u Zr a <;> cases trivW u Zr b <;> cases trivW u Zr c <;> + simp only [↓reduceIte, Bool.true_or, Bool.false_or, Bool.false_eq_true] + apply Bool.eq_iff_iff.mpr + simp only [Bool.or_eq_true, List.any_eq_true] + constructor + · intro h; exact Or.inr h + · rintro ((h | h) | h) + · exact ⟨a, ha, h⟩ + · exact ⟨b, hb, h⟩ + · exact h + +theorem testBit_eq_and_pow2 (w p : Nat) : + w.testBit p = !(Nat.beq (w &&& 2 ^ p) 0) := by + apply Bool.eq_iff_iff.mpr + simp only [Bool.not_eq_true', nbeq_false] + constructor + · intro h h0 + have := congrArg (fun x => x.testBit p) h0 + simp [Nat.testBit_and, Nat.testBit_two_pow_self, h] at this + · intro hne + by_contra hf + have hwf : w.testBit p = false := Bool.eq_false_iff.mpr hf + apply hne + apply Nat.eq_of_testBit_eq + intro i + simp only [Nat.testBit_and, Nat.testBit_two_pow, Nat.zero_testBit] + by_cases hip : p = i + · subst hip; simp [hwf] + · simp [hip] + +def sepPairOKM (Z mp acc : Nat) : List Nat → Bool + | [] => Nat.beq acc 0 + | w :: ws => + let acc1 := acc &&& (if Nat.beq (w &&& mp) 0 then w ^^^ Z else w) + if Nat.beq acc1 0 then true else sepPairOKM Z mp acc1 ws + +theorem sepPairOKM_eq (Z p : Nat) : + ∀ (ws : List Nat) (acc : Nat), sepPairOKM Z (2 ^ p) acc ws = sepPairOK Z p acc ws + | [], acc => rfl + | w :: ws, acc => by + unfold sepPairOKM sepPairOK + dsimp only + have htb := testBit_eq_and_pow2 w p + have hite : (if Nat.beq (w &&& 2 ^ p) 0 = true then w ^^^ Z else w) = (if w.testBit p = true then w else w ^^^ Z) := by + rw [htb] + cases Nat.beq (w &&& 2 ^ p) 0 <;> simp + rw [hite] + change (if ((acc &&& if w.testBit p = true then w else w ^^^ Z) == 0) = true then true + else sepPairOKM Z (2 ^ p) (acc &&& if w.testBit p = true then w else w ^^^ Z) ws) = _ + by_cases h0 : ((acc &&& if w.testBit p = true then w else w ^^^ Z) == 0) = true + · simp only [h0, ↓reduceIte] + · simp only [h0, Bool.false_eq_true, ↓reduceIte] + exact sepPairOKM_eq Z p ws _ + +def covPairOKM (mp zeros acc : Nat) : List Nat → Bool + | [] => Nat.beq (acc &&& zeros) 0 + | w :: ws => + let acc1 := if Nat.beq (w &&& mp) 0 then acc else acc &&& w + if Nat.beq (acc1 &&& zeros) 0 then true else covPairOKM mp zeros acc1 ws + +theorem covPairOKM_eq (p zeros : Nat) : + ∀ (ws : List Nat) (acc : Nat), covPairOKM (2 ^ p) zeros acc ws = covPairOK p zeros acc ws + | [], acc => rfl + | w :: ws, acc => by + unfold covPairOKM covPairOK + dsimp only + have htb := testBit_eq_and_pow2 w p + have hite : (if Nat.beq (w &&& 2 ^ p) 0 = true then acc else acc &&& w) = (if w.testBit p = true then acc &&& w else acc) := by + rw [htb] + cases Nat.beq (w &&& 2 ^ p) 0 <;> simp + rw [hite] + change (if (((if w.testBit p = true then acc &&& w else acc) &&& zeros) == 0) = true then true + else covPairOKM (2 ^ p) zeros (if w.testBit p = true then acc &&& w else acc) ws) = _ + by_cases h0 : (((if w.testBit p = true then acc &&& w else acc) &&& zeros) == 0) = true + · simp only [h0, ↓reduceIte] + · simp only [h0, Bool.false_eq_true, ↓reduceIte] + exact covPairOKM_eq p zeros ws _ + +def validFailAt (u : Bool) (Z mask : Nat) (S mpts : List Nat) (idx : Nat) : Bool := + match mpts[idx]? with + | none => false + | some mp => if u then !(sepPairOKM Z mp mask S) else !(covPairOKM mp mask Z S) + +theorem validFailAt_no_por {u flip : Bool} {Z tab mask idx : Nat} {pts mpts S : List Nat} + {lkOut lkA lkB lkOp : Nat} {steps : List (Nat × Nat × Nat × Bool × Bool)} + (hZ : Z ≤ full64) (hPM : ptsMaskOK u flip Z tab mask pts = true) + (hmpts : mpts = pts.map (2 ^ ·)) + (hf : validFailAt u Z mask S mpts idx = true) : + ¬ PORSteps u Z (normZ u Z tab) lkOut lkA lkB lkOp S steps := by + unfold validFailAt at hf + rw [hmpts, List.getElem?_map] at hf + cases hp_opt : pts[idx]? with + | none => rw [hp_opt] at hf; simp at hf + | some p => + rw [hp_opt] at hf + simp only [Option.map_some] at hf + have hp : p ∈ pts := List.mem_of_getElem? hp_opt + have hvalid : validOK u Z mask S pts = false := by + unfold validOK + cases u with + | true => + simp only [↓reduceIte, Bool.not_eq_true'] at hf ⊢ + rw [sepPairOKM_eq] at hf + suffices ∀ ps : List Nat, p ∈ ps → sepOK Z mask S ps = false from this pts hp + intro ps; induction ps with + | nil => intro h; simp at h + | cons x xs ih => + intro hm; unfold sepOK + rcases List.mem_cons.mp hm with rfl | hm' + · simp [hf] + · simp [ih hm'] + | false => + simp only [Bool.false_eq_true, ↓reduceIte, Bool.not_eq_true'] at hf ⊢ + rw [covPairOKM_eq] at hf + suffices ∀ ps : List Nat, p ∈ ps → covOK Z mask S ps = false from this pts hp + intro ps; induction ps with + | nil => intro h; simp at h + | cons x xs ih => + intro hm; unfold covOK + rcases List.mem_cons.mp hm with rfl | hm' + · simp [hf] + · simp [ih hm'] + exact validOK_false_no_por hZ hPM (fun _ h => h) hvalid + +def chk1R (u : Bool) (rk a b c : Nat) (ka kb : Bool) (S : List Nat) : + List Nat → List Nat → List Nat → List Nat → Nat → Bool + | Zr :: _, Lr :: _, n0 :: _, z0 :: _, 0 => + let ta := trivW true Zr a + let tb := trivW true Zr b + let tc := trivW true Zr c + let zcn := z0 - (if !ka && ta then 1 else 0) - (if !kb && tb then 1 else 0) + (if tc then 1 else 0) + if Nat.blt rk ((Lr - (n0 + 1)) + zcn) then true + else if Nat.blt rk ((Lr - n0) + zcn) then isTrivFast u Zr S a b c + else false + | _ :: Zs, _ :: Ls, _ :: ns, _ :: zs, r + 1 => chk1R u rk a b c ka kb S Zs Ls ns zs r + | _, _, _, _, _ => false + +theorem chk1R_spec (u : Bool) (rk a b c : Nat) (ka kb : Bool) (S : List Nat) : + ∀ (Zs Ls ns zs : List Nat) (r : Nat), + chk1R u rk a b c ka kb S Zs Ls ns zs r = true → + r < Zs.length ∧ + let Zr := Zs.getD r 0 + let Lr := Ls.getD r 0 + let n0 := ns.getD r 0 + let z0 := zs.getD r 0 + let zcn := z0 - (if !ka && trivW true Zr a then 1 else 0) - + (if !kb && trivW true Zr b then 1 else 0) + (if trivW true Zr c then 1 else 0) + rk < (Lr - (n0 + 1)) + zcn ∨ (rk < (Lr - n0) + zcn ∧ isTrivFast u Zr S a b c = true) + | [], _, _, _, _, h => by simp [chk1R] at h + | _ :: _, [], _, _, _, h => by simp [chk1R] at h + | _ :: _, _ :: _, [], _, _, h => by simp [chk1R] at h + | _ :: _, _ :: _, _ :: _, [], _, h => by simp [chk1R] at h + | Zr :: Zs, Lr :: Ls, n0 :: ns, z0 :: zs, 0, h => by + unfold chk1R at h + simp only [List.length_cons, Nat.zero_lt_succ, true_and, List.getD, List.getElem?_cons_zero, Option.getD_some] + dsimp only at h + by_cases h1 : Nat.blt rk ((Lr - (n0 + 1)) + (z0 - (if !ka && trivW true Zr a then 1 else 0) - + (if !kb && trivW true Zr b then 1 else 0) + (if trivW true Zr c then 1 else 0))) = true + · left; exact Nat.blt_eq.mp h1 + · simp only [h1, Bool.false_eq_true, ↓reduceIte] at h + by_cases h2 : Nat.blt rk ((Lr - n0) + (z0 - (if !ka && trivW true Zr a then 1 else 0) - + (if !kb && trivW true Zr b then 1 else 0) + (if trivW true Zr c then 1 else 0))) = true + · simp only [h2, ↓reduceIte] at h + right; exact ⟨Nat.blt_eq.mp h2, h⟩ + · simp only [h2, Bool.false_eq_true, ↓reduceIte] at h + | _ :: Zs, _ :: Ls, _ :: ns, _ :: zs, r + 1, h => by + unfold chk1R at h + obtain ⟨hlt, hrest⟩ := chk1R_spec u rk a b c ka kb S Zs Ls ns zs r h + refine ⟨Nat.succ_lt_succ hlt, ?_⟩ + simp only [List.getD, List.getElem?_cons_succ] + exact hrest + +theorem chk1R_no_por {u : Bool} {Z tab : Nat} + {rfZ rfL : List Nat} (hRF : RFSubOK u Z tab rfZ rfL) + {S nz zc : List Nat} {a b d rk r : Nat} {ka kb ka' kb' : Bool} + (hs : S.Pairwise (· < ·)) (ha : a ∈ S) (hb : b ∈ S) (hab : a < b) + (hc : stepVal u Z d a b ∉ S) + (hka : ka = true → ka' = true) (hkb : kb = true → kb' = true) + (rest : List (Nat × Nat × Nat × Bool × Bool)) + (hrest_len : rest.length ≤ rk) + (hpor : PORSteps u Z (normZ u Z tab) (stepVal u Z d a b) a b d (applyStep S a b (stepVal u Z d a b) ka' kb') rest) + (hr : RInv u Z rfZ rfL S nz zc ((a, b, d, ka', kb') :: rest)) + (h1r : chk1R u rk a b (stepVal u Z d a b) ka kb S rfZ rfL nz zc r = true) : + False := by + let c := stepVal u Z d a b + obtain ⟨hr_lt, hcond⟩ := chk1R_spec u rk a b c ka kb S rfZ rfL nz zc r h1r + obtain ⟨_, hsub⟩ := rfSubOK_getD hRF + obtain ⟨hZr, hZb, hnt, _⟩ := hsub r hr_lt + obtain ⟨hzc_eq, hnz_le⟩ := hr r hr_lt + simp only [countNZSteps] at hnz_le + have hzc_nxt := zCount_applyStep hs (Z := rfZ.getD r 0) hab ha hb hc ka' kb' + have htriv_le := zCount_le_trivSteps (u := u) (Zr := rfZ.getD r 0) hZr hZb (by + rw [← normZ_trivW u hZr, normZ_idem u hZr hZb, normZ_trivW u hZr]; exact hnt) hpor + have hsplit := count_split_steps u Z (rfZ.getD r 0) (applyStep S a b c ka' kb') rest + have hz_mono : + zc.getD r 0 - (if !ka && trivW true (rfZ.getD r 0) a then 1 else 0) - + (if !kb && trivW true (rfZ.getD r 0) b then 1 else 0) + (if trivW true (rfZ.getD r 0) c then 1 else 0) ≤ + zc.getD r 0 - (if !ka' && trivW true (rfZ.getD r 0) a then 1 else 0) - + (if !kb' && trivW true (rfZ.getD r 0) b then 1 else 0) + (if trivW true (rfZ.getD r 0) c then 1 else 0) := by + have h1 : (if !ka' && trivW true (rfZ.getD r 0) a then 1 else 0) ≤ + (if !ka && trivW true (rfZ.getD r 0) a then 1 else 0) := by + cases ka <;> cases ka' <;> simp_all + have h2 : (if !kb' && trivW true (rfZ.getD r 0) b then 1 else 0) ≤ + (if !kb && trivW true (rfZ.getD r 0) b then 1 else 0) := by + cases kb <;> cases kb' <;> simp_all + omega + rcases hcond with hopt | ⟨htriv_lt, htf⟩ + · dsimp only [c] at * + have hinc : (if isTrivStep u (rfZ.getD r 0) S a b (stepVal u Z d a b) = true then 0 else 1) ≤ 1 := by + split_ifs <;> omega + omega + · rw [isTrivFast_eq ha hb] at htf + dsimp only [c] at * + rw [htf] at hnz_le + simp only [↓reduceIte, Nat.add_zero] at hnz_le + omega + +def updNzFast (u : Bool) (S : List Nat) (a b c : Nat) : List Nat → List Nat → List Nat + | [], _ => [] + | Zr :: Zs, nz => + (nz.headD 0 + if isTrivFast u Zr S a b c then 0 else 1) :: updNzFast u S a b c Zs nz.tail + +theorem updNzFast_eq {u : Bool} {S : List Nat} {a b c : Nat} (ha : a ∈ S) (hb : b ∈ S) : + ∀ (Zs nz : List Nat), updNzFast u S a b c Zs nz = updNz u S a b c Zs nz + | [], _ => rfl + | Zr :: Zs, nz => by + unfold updNzFast updNz + rw [isTrivFast_eq ha hb, updNzFast_eq ha hb Zs nz.tail] + +def updZcFast (a b c : Nat) (ka kb : Bool) : List Nat → List Nat → List Nat + | [], _ => [] + | Zr :: Zs, zc => + let ta := trivW true Zr a + let tb := trivW true Zr b + let tc := trivW true Zr c + (zc.headD 0 - (if !ka && ta then 1 else 0) - (if !kb && tb then 1 else 0) + (if tc then 1 else 0)) :: + updZcFast a b c ka kb Zs zc.tail + +theorem updZcFast_getD (a b c : Nat) (ka kb : Bool) : + ∀ (Zs zc : List Nat) (r : Nat), r < Zs.length → + (updZcFast a b c ka kb Zs zc).getD r 0 = + zc.getD r 0 - (if !ka && trivW true (Zs.getD r 0) a then 1 else 0) - + (if !kb && trivW true (Zs.getD r 0) b then 1 else 0) + + (if trivW true (Zs.getD r 0) c then 1 else 0) + | [], _, _, hr => by simp at hr + | Zr :: Zs, zc, 0, _ => by + unfold updZcFast + cases zc <;> simp [List.getD, List.headD] + | Zr :: Zs, zc, r + 1, hr => by + unfold updZcFast + simp only [List.length_cons] at hr + have ih := updZcFast_getD a b c ka kb Zs zc.tail r (by omega) + have hzt : zc.tail.getD r 0 = zc.getD (r + 1) 0 := by cases zc <;> simp [List.getD] + rw [hzt] at ih + simp only [List.getD, List.getElem?_cons_succ] + exact ih + +theorem rInv_step_fast {u : Bool} {Z : Nat} + {rfZ rfL S nz zc : List Nat} {a b d : Nat} {ka kb : Bool} + (hs : S.Pairwise (· < ·)) (ha : a ∈ S) (hb : b ∈ S) (hab : a < b) + (hc : stepVal u Z d a b ∉ S) + (rest : List (Nat × Nat × Nat × Bool × Bool)) + (hr : RInv u Z rfZ rfL S nz zc ((a, b, d, ka, kb) :: rest)) : + RInv u Z rfZ rfL (applyStep S a b (stepVal u Z d a b) ka kb) + (updNzFast u S a b (stepVal u Z d a b) rfZ nz) + (updZcFast a b (stepVal u Z d a b) ka kb rfZ zc) rest := by + let c := stepVal u Z d a b + rw [updNzFast_eq ha hb] + intro r hr_lt + obtain ⟨hzc_eq, hnz_le⟩ := hr r hr_lt + have hnz := updNz_spec u S a b c rfZ nz r hr_lt + have hzc_nxt := zCount_applyStep hs (Z := rfZ.getD r 0) hab ha hb hc ka kb + have hzc_f := updZcFast_getD a b c ka kb rfZ zc r hr_lt + simp only [countNZSteps] at hnz_le + refine ⟨?_, ?_⟩ + · rw [hzc_f, hzc_eq, hzc_nxt] + · rw [hnz]; dsimp only [c] at *; omega + +def unp10 : List Nat → List Nat + | [] => [] + | w :: ws => + (w % 64) :: ((w / 64) % 64) :: ((w / 4096) % 64) :: ((w / 262144) % 64) :: ((w / 16777216) % 64) :: + ((w / 1073741824) % 64) :: ((w / 68719476736) % 64) :: ((w / 4398046511104) % 64) :: + ((w / 281474976710656) % 64) :: ((w / 18014398509481984) % 64) :: unp10 ws + +def chkKpT (u : Bool) (Z mask : Nat) (mpts rfZ rfL : List Nat) (rk slack : Nat) + (S : List Nat) (a b c : Nat) (nz zc : List Nat) (op : Nat) + (rec : List Nat → List Nat → List Nat → Nat → Nat → Nat → Nat → List Nat → Option (List Nat)) + (ka kb : Bool) (cost : Nat) (tr : List Nat) : Option (List Nat) := + if Nat.blt slack cost then some tr + else match tr with + | [] => none + | tag :: tr1 => + if Nat.beq tag 0 then + let nxt := applyStep S a b c ka kb + let nzN := updNzFast u S a b c rfZ nz + let zcN := updZcFast a b c ka kb rfZ zc + rec nxt nzN zcN c a b op tr1 + else if Nat.blt tag 32 then + if chk1R u rk a b c ka kb S rfZ rfL nz zc (tag - 1) then some tr1 else none + else + if !(ka && kb) && validFailAt u Z mask (applyStep S a b c ka kb) mpts (tag - 32) then some tr1 else none + +def chkOpsT (u : Bool) (Z target mask : Nat) (mpts rfZ rfL : List Nat) (rk slack : Nat) + (S nz zc : List Nat) (lkOut lkA lkB lkOp a b : Nat) + (rec : List Nat → List Nat → List Nat → Nat → Nat → Nat → Nat → List Nat → Option (List Nat)) + (op : Nat) : Nat → List Nat → Option (List Nat) + | 0, tr => some tr + | k + 1, tr => + let disj := !(Nat.beq lkOut 0) && !(Nat.beq a lkOut) && !(Nat.beq b lkOut) + let skipPor := disj && !(Nat.blt lkA a || (Nat.beq lkA a && (Nat.blt lkB b || (Nat.beq lkB b && Nat.blt lkOp op)))) + let res := + if skipPor then some tr + else + let c := stepVal u Z op a b + if Nat.beq c target then none + else if trivW u Z c || anyEq c S then some tr + else match tr with + | [] => none + | tag :: tr1 => + if !(Nat.beq tag 0) then + if chk1R u rk a b c false false S rfZ rfL nz zc (tag - 1) then some tr1 else none + else + match chkKpT u Z mask mpts rfZ rfL rk slack S a b c nz zc op rec false false 0 tr1 with + | none => none + | some tr2 => + match chkKpT u Z mask mpts rfZ rfL rk slack S a b c nz zc op rec true false 1 tr2 with + | none => none + | some tr3 => + match chkKpT u Z mask mpts rfZ rfL rk slack S a b c nz zc op rec false true 1 tr3 with + | none => none + | some tr4 => chkKpT u Z mask mpts rfZ rfL rk slack S a b c nz zc op rec true true 2 tr4 + match res with + | none => none + | some tr' => chkOpsT u Z target mask mpts rfZ rfL rk slack S nz zc lkOut lkA lkB lkOp a b rec (op + 1) k tr' + +def chkPairsWithT (u : Bool) (Z target mask : Nat) (mpts rfZ rfL : List Nat) (rk slack : Nat) + (S nz zc : List Nat) (lkOut lkA lkB lkOp a : Nat) + (rec : List Nat → List Nat → List Nat → Nat → Nat → Nat → Nat → List Nat → Option (List Nat)) : + List Nat → List Nat → Option (List Nat) + | [], tr => some tr + | b :: rest, tr => + match chkOpsT u Z target mask mpts rfZ rfL rk slack S nz zc lkOut lkA lkB lkOp a b rec 0 (opsCount u) tr with + | none => none + | some tr1 => chkPairsWithT u Z target mask mpts rfZ rfL rk slack S nz zc lkOut lkA lkB lkOp a rec rest tr1 + +def chkAllPairsT (u : Bool) (Z target mask : Nat) (mpts rfZ rfL : List Nat) (rk slack : Nat) + (S nz zc : List Nat) (lkOut lkA lkB lkOp : Nat) + (rec : List Nat → List Nat → List Nat → Nat → Nat → Nat → Nat → List Nat → Option (List Nat)) : + List Nat → List Nat → Option (List Nat) + | [], tr => some tr + | a :: bs, tr => + match chkPairsWithT u Z target mask mpts rfZ rfL rk slack S nz zc lkOut lkA lkB lkOp a rec bs tr with + | none => none + | some tr1 => chkAllPairsT u Z target mask mpts rfZ rfL rk slack S nz zc lkOut lkA lkB lkOp rec bs tr1 + +def chkTreeT (u : Bool) (Z target mask : Nat) (mpts rfZ rfL : List Nat) (rem_k : Nat) : + List Nat → List Nat → List Nat → Nat → Nat → Nat → Nat → List Nat → Option (List Nat) := + Nat.rec + (fun S _ _ _ _ _ _ tr => + let m := S.length + if Nat.beq m 0 then some tr + else if Nat.beq m 1 then (if !(Nat.beq (S.headD 0) target) then some tr else none) + else some tr) + (fun rk rec S nz zc lkOut lkA lkB lkOp tr => + let m := S.length + if Nat.beq m 0 then some tr + else if Nat.beq m 1 then (if !(Nat.beq (S.headD 0) target) then some tr else none) + else if Nat.blt (rk + 1) (m - 1) then some tr + else if Nat.beq rk 0 && Nat.beq m 2 then + (if chk2Base u Z target (S.getD 0 0) (S.getD 1 0) (opsCount u) then some tr else none) + else if Nat.beq rk 1 && Nat.beq m 3 then + (if chk3Base u Z target (S.getD 0 0) (S.getD 1 0) (S.getD 2 0) then some tr else none) + else + chkAllPairsT u Z target mask mpts rfZ rfL rk (rk + 1 - (m - 1)) S nz zc lkOut lkA lkB lkOp rec S tr) + rem_k + +theorem chkTreeT_sound {u flip : Bool} {Z tab mask : Nat} {pts mpts rfZ rfL : List Nat} + (hZ : Z ≤ full64) (hPM : ptsMaskOK u flip Z tab mask pts = true) + (hmpts : mpts = pts.map (2 ^ ·)) + (hRF : RFSubOK u Z tab rfZ rfL) : + ∀ rem_k S nz zc lkOut lkA lkB lkOp tr, + (chkTreeT u Z (normZ u Z tab) mask mpts rfZ rfL rem_k S nz zc lkOut lkA lkB lkOp tr).isSome = true → + NoPathFrom u Z tab rfZ rfL S nz zc lkOut lkA lkB lkOp rem_k := by + intro rem_k + induction rem_k with + | zero => + intro S nz zc lkOut lkA lkB lkOp tr hchk steps hlen hpor _ + have hnil : steps = [] := List.length_eq_zero_iff.mp (by omega) + subst hnil + simp only [PORSteps] at hpor + subst hpor + simp [chkTreeT, Nat.beq_eq] at hchk + | succ rk ih => + intro S nz zc lkOut lkA lkB lkOp tr hchk steps hlen hpor hr + have hbd := porSteps_len_bound hpor + cases steps with + | nil => + simp only [PORSteps] at hpor + subst hpor + simp [chkTreeT, Nat.beq_eq] at hchk + | cons st rest => + rcases st with ⟨a, b, d, ka, kb⟩ + rcases hpor with ⟨hs, ha, hb, hab, hd, hntc, hcn, hpor_cond, hrest⟩ + have hm2 : 2 ≤ S.length := by + cases S with + | nil => simp at ha + | cons x xs => cases xs with | nil => simp at ha hb; omega | cons _ _ => simp + dsimp only [chkTreeT] at hchk + have hm0 : Nat.beq S.length 0 = false := nbeq_false.mpr (by omega) + have hm1 : Nat.beq S.length 1 = false := nbeq_false.mpr (by omega) + have hmle : Nat.blt (rk + 1) (S.length - 1) = false := nblt_false.mpr (by omega) + simp only [hm0, hm1, hmle, Bool.false_eq_true, ↓reduceIte] at hchk + by_cases hrk2 : (Nat.beq rk 0 && Nat.beq S.length 2) = true + · simp only [hrk2, ↓reduceIte] at hchk + simp only [Bool.and_eq_true, Nat.beq_eq] at hrk2 + obtain ⟨rfl, hlen2⟩ := hrk2 + cases S with + | nil => simp at hlen2 + | cons x xs => cases xs with + | nil => simp at hlen2 + | cons y ys => cases ys with + | cons _ _ => simp at hlen2 + | nil => + have hch2 : chk2Base u Z (normZ u Z tab) x y (opsCount u) = true := by + cases hcb : chk2Base u Z (normZ u Z tab) x y (opsCount u) + · simp [hcb] at hchk + · rfl + have hxy : x < y := List.rel_of_pairwise_cons hs (by simp) + have hax : a = x ∧ b = y := by + simp only [List.mem_cons, List.not_mem_nil, or_false] at ha hb + rcases ha with rfl | rfl <;> rcases hb with rfl | rfl <;> omega + rcases hax with ⟨rfl, rfl⟩ + simp at hlen + subst hlen + simp only [PORSteps] at hrest + have hmem_t : normZ u Z tab ∈ applyStep [a, b] a b (stepVal u Z d a b) ka kb := by + rw [hrest]; exact List.mem_cons_self + rw [mem_applyStep] at hmem_t + rcases hmem_t with hT | ⟨hm_ab, h1, h2⟩ + · exact chk2Base_sound hch2 hd hT.symm + · exfalso + have hln : (applyStep [a, b] a b (stepVal u Z d a b) ka kb).length = 1 := by rw [hrest]; simp + have hcf : stepVal u Z d a b ∉ [a, b].filter (fun w => (w != a || ka) && (w != b || kb)) := + fun h => hcn (List.mem_filter.mp h).1 + change (insSort (stepVal u Z d a b) ([a, b].filter (fun w => (w != a || ka) && (w != b || kb)))).length = 1 at hln + rw [length_insSort_fresh hcf] at hln + have hfl0 : [a, b].filter (fun w => (w != a || ka) && (w != b || kb)) = [] := + List.length_eq_zero_iff.mp (by omega) + have hin_f : normZ u Z tab ∈ [a, b].filter (fun w => (w != a || ka) && (w != b || kb)) := by + rw [List.mem_filter]; refine ⟨hm_ab, ?_⟩ + simp only [Bool.and_eq_true, Bool.or_eq_true, bne_iff_ne, ne_eq] + exact ⟨h1, h2⟩ + rw [hfl0] at hin_f; simp at hin_f + · simp only [hrk2, Bool.false_eq_true, ↓reduceIte] at hchk + by_cases hrk3 : (Nat.beq rk 1 && Nat.beq S.length 3) = true + · simp only [hrk3, ↓reduceIte] at hchk + simp only [Bool.and_eq_true, Nat.beq_eq] at hrk3 + obtain ⟨rfl, hlen3⟩ := hrk3 + cases S with + | nil => simp at hlen3 + | cons x xs => cases xs with + | nil => simp at hlen3 + | cons y ys => cases ys with + | nil => simp at hlen3 + | cons z zs => cases zs with + | cons _ _ => simp at hlen3 + | nil => + have hch3 : chk3Base u Z (normZ u Z tab) x y z = true := by + cases hcb : chk3Base u Z (normZ u Z tab) x y z + · simp [hcb] at hchk + · rfl + have hlen1 : rest.length ≤ 1 := by simp at hlen; omega + have hn_len : (applyStep [x, y, z] a b (stepVal u Z d a b) ka kb).length ≤ 2 := by + have := porSteps_len_bound hrest; omega + obtain ⟨w, hw_case, hmem2⟩ := applyStep_three_to_two hs ha hb hab hcn ka kb hn_len + have hlen_nxt : (applyStep [x, y, z] a b (stepVal u Z d a b) ka kb).length = 2 := by + cases rest with + | nil => + simp [PORSteps] at hrest + have hmw : w ∈ [normZ u Z tab] := hrest ▸ (hmem2 w).mpr (Or.inr rfl) + have hmc : stepVal u Z d a b ∈ [normZ u Z tab] := hrest ▸ (hmem2 _).mpr (Or.inl rfl) + simp at hmw hmc + rcases hw_case with ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ <;> + exact absurd (hmc.trans hmw.symm) (by intro h; rw [h] at hcn; exact hcn (by simp)) + | cons g2 r2 => + simp at hlen1; subst hlen1 + rcases hrest with ⟨_, ha2, hb2, hab2, _⟩ + cases hs' : applyStep [x, y, z] a b (stepVal u Z d a b) ka kb with + | nil => simp [hs'] at ha2 + | cons _ xs => cases xs with + | nil => simp [hs'] at ha2 hb2; omega + | cons _ _ => rw [hs'] at hn_len; simp at hn_len ⊢; omega + have hr_cons : ∃ a2 b2 d2 ka2 kb2, rest = [(a2, b2, d2, ka2, kb2)] := by + cases rest with + | nil => simp [PORSteps] at hrest; rw [hrest] at hlen_nxt; simp at hlen_nxt + | cons g2 r2 => + cases r2 with + | nil => rcases g2 with ⟨a2, b2, d2, ka2, kb2⟩; exact ⟨a2, b2, d2, ka2, kb2, rfl⟩ + | cons _ _ => simp at hlen1 + rcases hr_cons with ⟨a2, b2, d2, ka2, kb2, rfl⟩ + rcases hrest with ⟨_, ha2, hb2, hab2, hd2, _, hc2, _, hrest2⟩ + simp only [PORSteps] at hrest2 + have hpair2 : (a2 = stepVal u Z d a b ∧ b2 = w) ∨ (a2 = w ∧ b2 = stepVal u Z d a b) := by + rw [hmem2 a2] at ha2; rw [hmem2 b2] at hb2 + rcases ha2 with rfl | rfl <;> rcases hb2 with rfl | rfl <;> omega + have hT2 : stepVal u Z d2 a2 b2 = normZ u Z tab := by + have hmem_t : normZ u Z tab ∈ applyStep (applyStep [x, y, z] a b (stepVal u Z d a b) ka kb) + a2 b2 (stepVal u Z d2 a2 b2) ka2 kb2 := by rw [hrest2]; exact List.mem_cons_self + rw [mem_applyStep] at hmem_t + rcases hmem_t with hT | ⟨hm, h1, h2⟩ + · exact hT.symm + · exfalso + have hln2 : (applyStep (applyStep [x, y, z] a b (stepVal u Z d a b) ka kb) a2 b2 + (stepVal u Z d2 a2 b2) ka2 kb2).length = 1 := by rw [hrest2]; simp + have hcf2 : stepVal u Z d2 a2 b2 ∉ (applyStep [x, y, z] a b (stepVal u Z d a b) ka kb).filter + (fun u0 => (u0 != a2 || ka2) && (u0 != b2 || kb2)) := + fun h => hc2 (List.mem_filter.mp h).1 + change (insSort (stepVal u Z d2 a2 b2) ((applyStep [x, y, z] a b (stepVal u Z d a b) ka kb).filter + (fun u0 => (u0 != a2 || ka2) && (u0 != b2 || kb2)))).length = 1 at hln2 + rw [length_insSort_fresh hcf2] at hln2 + have hfl0 : (applyStep [x, y, z] a b (stepVal u Z d a b) ka kb).filter + (fun u0 => (u0 != a2 || ka2) && (u0 != b2 || kb2)) = [] := + List.length_eq_zero_iff.mp (by omega) + have hin_f : normZ u Z tab ∈ (applyStep [x, y, z] a b (stepVal u Z d a b) ka kb).filter + (fun u0 => (u0 != a2 || ka2) && (u0 != b2 || kb2)) := by + rw [List.mem_filter]; refine ⟨hm, ?_⟩ + simp only [Bool.and_eq_true, Bool.or_eq_true, bne_iff_ne, ne_eq] + exact ⟨h1, h2⟩ + rw [hfl0] at hin_f; simp at hin_f + unfold chk3Base at hch3 + simp only [Bool.and_eq_true] at hch3 + rcases hw_case with ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ | ⟨rfl, rfl, rfl⟩ + · have hs3 := chk3Pair_sound hZ hch3.1.1 hd hd2 + rcases hpair2 with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ + · exact absurd hT2 hs3.2.1 + · exact absurd hT2 hs3.2.2 + · have hs3 := chk3Pair_sound hZ hch3.1.2 hd hd2 + rcases hpair2 with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ + · exact absurd hT2 hs3.2.1 + · exact absurd hT2 hs3.2.2 + · have hs3 := chk3Pair_sound hZ hch3.2 hd hd2 + rcases hpair2 with ⟨rfl, rfl⟩ | ⟨rfl, rfl⟩ + · exact absurd hT2 hs3.2.1 + · exact absurd hT2 hs3.2.2 + · simp only [hrk3, Bool.false_eq_true, ↓reduceIte] at hchk + have hkp_sound : ∀ (kpa kpb : Bool) (cost : Nat) (tr0 : List Nat), + kpa = ka → kpb = kb → cost = (if ka then 1 else 0) + (if kb then 1 else 0) → + (chkKpT u Z mask mpts rfZ rfL rk (rk + 1 - (S.length - 1)) S a b (stepVal u Z d a b) nz zc d + (chkTreeT u Z (normZ u Z tab) mask mpts rfZ rfL rk) kpa kpb cost tr0).isSome = true → + False := by + intro kpa kpb cost tr0 hka_eq hkb_eq hcost_eq hkp + subst kpa kpb cost + unfold chkKpT at hkp + have hbr := porSteps_len_bound hrest + have hc_ins : stepVal u Z d a b ∉ S.filter (fun w => (w != a || ka) && (w != b || kb)) := + fun h => hcn (List.mem_filter.mp h).1 + have hln_nxt : (applyStep S a b (stepVal u Z d a b) ka kb).length = + (S.filter (fun w => (w != a || ka) && (w != b || kb))).length + 1 := + length_insSort_fresh hc_ins + have hlf := (length_filter_erase2 (nodup_of_pairwise hs) (Nat.ne_of_lt hab) ha hb ka kb).1 + have hsl : Nat.blt (rk + 1 - (S.length - 1)) ((if ka then 1 else 0) + (if kb then 1 else 0)) = false := by + rw [Bool.eq_false_iff]; intro hlt + have hlt' := Nat.blt_eq.mp hlt + have hrle : rest.length ≤ rk := by simp only [List.length_cons] at hlen; omega + rw [hln_nxt, hlf] at hbr + cases ka <;> cases kb <;> simp at hbr hlt' ⊢ <;> omega + simp only [hsl, Bool.false_eq_true, ↓reduceIte] at hkp + cases tr0 with + | nil => simp at hkp + | cons tag tr1 => + dsimp only at hkp + by_cases ht0 : Nat.beq tag 0 = true + · simp only [ht0, ↓reduceIte] at hkp + exact ih (applyStep S a b (stepVal u Z d a b) ka kb) + (updNzFast u S a b (stepVal u Z d a b) rfZ nz) + (updZcFast a b (stepVal u Z d a b) ka kb rfZ zc) + (stepVal u Z d a b) a b d tr1 hkp rest (by simp at hlen; omega) hrest + (rInv_step_fast hs ha hb hab hcn rest hr) + · simp only [ht0, Bool.false_eq_true, ↓reduceIte] at hkp + by_cases ht32 : Nat.blt tag 32 = true + · simp only [ht32, ↓reduceIte] at hkp + cases h1r : chk1R u rk a b (stepVal u Z d a b) ka kb S rfZ rfL nz zc (tag - 1) + · simp [h1r] at hkp + · exact chk1R_no_por hRF hs ha hb hab hcn (fun h => h) (fun h => h) rest + (by simp at hlen; omega) hrest hr h1r + · simp only [ht32, Bool.false_eq_true, ↓reduceIte] at hkp + by_cases hv : (!(ka && kb) && validFailAt u Z mask (applyStep S a b (stepVal u Z d a b) ka kb) mpts (tag - 32)) = true + · rw [Bool.and_eq_true] at hv + exact validFailAt_no_por hZ hPM hmpts hv.2 hrest + · simp only [hv, Bool.false_eq_true, ↓reduceIte] at hkp + simp at hkp + have hstep_ops : ∀ a0 b0 tr0, a0 = a → b0 = b → + (chkOpsT u Z (normZ u Z tab) mask mpts rfZ rfL rk (rk + 1 - (S.length - 1)) S nz zc lkOut lkA lkB lkOp a0 b0 + (chkTreeT u Z (normZ u Z tab) mask mpts rfZ rfL rk) 0 (opsCount u) tr0).isSome = true → False := by + intro a0 b0 tr0 ha0 hb0 hops; rw [ha0, hb0] at hops + suffices ∀ op cur trc, d ≥ op → d < op + cur → + (chkOpsT u Z (normZ u Z tab) mask mpts rfZ rfL rk (rk + 1 - (S.length - 1)) S nz zc lkOut lkA lkB lkOp a b + (chkTreeT u Z (normZ u Z tab) mask mpts rfZ rfL rk) op cur trc).isSome = true → False from + this 0 (opsCount u) tr0 (Nat.zero_le _) (by omega) hops + intro op cur + induction cur generalizing op with + | zero => intro _ _ _ _; omega + | succ k ih_op => + intro trc hge hlt hops_c + unfold chkOpsT at hops_c + dsimp only at hops_c + split at hops_c + · exact Bool.noConfusion hops_c + · rename_i tr' hres + rcases Nat.eq_or_lt_of_le hge with hop | hgt + · subst hop + have hsk : ((!(Nat.beq lkOut 0) && !(Nat.beq a lkOut) && !(Nat.beq b lkOut)) && + !(Nat.blt lkA a || (Nat.beq lkA a && (Nat.blt lkB b || (Nat.beq lkB b && Nat.blt lkOp op))))) = false := by + rw [Bool.eq_false_iff] + intro hbad0 + simp only [Bool.and_eq_true, Bool.not_eq_true', nbeq_false, + Bool.or_eq_false_iff, Bool.and_eq_false_iff, nblt_false, not_lt] at hbad0 + rcases hbad0 with ⟨⟨⟨hlo, hna⟩, hnb⟩, hlt_a, hrest_a⟩ + rcases hpor_cond with h | h | h | hlt' | ⟨rfl, hrc⟩ + · exact hlo h + · exact hna h + · exact hnb h + · omega + · rcases hrest_a with hne1 | ⟨hlt_b, hrest_b⟩ + · exact hne1 rfl + · rcases hrc with h1 | ⟨rfl, h2⟩ + · omega + · rcases hrest_b with hne2 | hlt_op + · exact hne2 rfl + · omega + simp only [hsk, Bool.false_eq_true, ↓reduceIte] at hres + have hc_nt : trivW u Z (stepVal u Z op a b) = false := by + cases ht : trivW u Z (stepVal u Z op a b) + · rfl + · simp [ht] at hntc + have hc_ne : anyEq (stepVal u Z op a b) S = false := by + cases hae : anyEq (stepVal u Z op a b) S + · rfl + · exfalso + suffices ∀ xs, anyEq (stepVal u Z op a b) xs = true → stepVal u Z op a b ∈ xs from + hcn (this S hae) + intro xs; induction xs with + | nil => intro h; simp [anyEq] at h + | cons x xs ihx => + intro h; unfold anyEq at h; rw [Bool.or_eq_true, Nat.beq_eq] at h + rcases h with rfl | h' + · exact List.mem_cons_self + · exact List.mem_cons_of_mem _ (ihx h') + by_cases hcT : Nat.beq (stepVal u Z op a b) (normZ u Z tab) = true + · simp only [hcT, ↓reduceIte] at hres; simp at hres + · simp only [hcT, Bool.false_eq_true, ↓reduceIte, hc_nt, hc_ne, Bool.or_self] at hres + cases trc with + | nil => simp at hres + | cons tag tr1 => + dsimp only at hres + by_cases ht0 : Nat.beq tag 0 = true + · simp only [ht0, Bool.not_true, Bool.false_eq_true, ↓reduceIte] at hres + cases hk00 : chkKpT u Z mask mpts rfZ rfL rk (rk + 1 - (S.length - 1)) S a b (stepVal u Z op a b) nz zc op + (chkTreeT u Z (normZ u Z tab) mask mpts rfZ rfL rk) false false 0 tr1 with + | none => rw [hk00] at hres; simp at hres + | some tr2 => + rw [hk00] at hres; dsimp only at hres + cases hk10 : chkKpT u Z mask mpts rfZ rfL rk (rk + 1 - (S.length - 1)) S a b (stepVal u Z op a b) nz zc op + (chkTreeT u Z (normZ u Z tab) mask mpts rfZ rfL rk) true false 1 tr2 with + | none => rw [hk10] at hres; simp at hres + | some tr3 => + rw [hk10] at hres; dsimp only at hres + cases hk01 : chkKpT u Z mask mpts rfZ rfL rk (rk + 1 - (S.length - 1)) S a b (stepVal u Z op a b) nz zc op + (chkTreeT u Z (normZ u Z tab) mask mpts rfZ rfL rk) false true 1 tr3 with + | none => rw [hk01] at hres; simp at hres + | some tr4 => + rw [hk01] at hres; dsimp only at hres + cases ka <;> cases kb + · exact hkp_sound false false 0 tr1 rfl rfl rfl (by simp [hk00]) + · exact hkp_sound false true 1 tr3 rfl rfl rfl (by simp [hk01]) + · exact hkp_sound true false 1 tr2 rfl rfl rfl (by simp [hk10]) + · exact hkp_sound true true 2 tr4 rfl rfl rfl (by simp [hres]) + · have htf : Nat.beq tag 0 = false := Bool.eq_false_iff.mpr ht0 + simp only [htf, Bool.not_false, ↓reduceIte] at hres + cases h1r : chk1R u rk a b (stepVal u Z op a b) false false S rfZ rfL nz zc (tag - 1) + · rw [h1r] at hres; simp at hres + · exact chk1R_no_por hRF hs ha hb hab hcn (fun h => by cases h) (fun h => by cases h) rest + (by simp at hlen; omega) hrest hr h1r + · exact ih_op (op + 1) tr' (by omega) (by omega) hops_c + have hwalk_all : ∀ xs : List Nat, xs.Pairwise (· < ·) → a ∈ xs → b ∈ xs → ∀ tr0, + (chkAllPairsT u Z (normZ u Z tab) mask mpts rfZ rfL rk (rk + 1 - (S.length - 1)) S nz zc lkOut lkA lkB lkOp + (chkTreeT u Z (normZ u Z tab) mask mpts rfZ rfL rk) xs tr0).isSome = true → False := by + intro xs + induction xs with + | nil => intro _ hax _ _ _; simp at hax + | cons x0 bs ih_xs => + intro hpw hax hbx tr0 hall + unfold chkAllPairsT at hall + have hx0_lt : ∀ y ∈ bs, x0 < y := fun y hy => List.rel_of_pairwise_cons hpw hy + have hpw_bs := (List.pairwise_cons.mp hpw).2 + cases hpw1 : chkPairsWithT u Z (normZ u Z tab) mask mpts rfZ rfL rk (rk + 1 - (S.length - 1)) S nz zc lkOut lkA lkB lkOp x0 + (chkTreeT u Z (normZ u Z tab) mask mpts rfZ rfL rk) bs tr0 with + | none => rw [hpw1] at hall; simp at hall + | some tr1 => + rw [hpw1] at hall + simp only [List.mem_cons] at hax hbx + rcases hax with rfl | ha_bs + · rcases hbx with rfl | hb_bs + · omega + · suffices ∀ ys : List Nat, b ∈ ys → ∀ try0, + (chkPairsWithT u Z (normZ u Z tab) mask mpts rfZ rfL rk (rk + 1 - (S.length - 1)) S nz zc lkOut lkA lkB lkOp a + (chkTreeT u Z (normZ u Z tab) mask mpts rfZ rfL rk) ys try0).isSome = true → False from + this bs hb_bs tr0 (by simp [hpw1]) + intro ys; induction ys with + | nil => intro hby _ _; simp at hby + | cons y0 ys' ih_ys => + intro hby try0 hp_w + unfold chkPairsWithT at hp_w + cases hop1 : chkOpsT u Z (normZ u Z tab) mask mpts rfZ rfL rk (rk + 1 - (S.length - 1)) S nz zc lkOut lkA lkB lkOp a y0 + (chkTreeT u Z (normZ u Z tab) mask mpts rfZ rfL rk) 0 (opsCount u) try0 with + | none => rw [hop1] at hp_w; simp at hp_w + | some try1 => + rw [hop1] at hp_w + rcases List.mem_cons.mp hby with rfl | hy_ys + · exact hstep_ops a b try0 rfl rfl (by simp [hop1]) + · exact ih_ys hy_ys try1 hp_w + · rcases hbx with rfl | hb_bs + · exact absurd (hx0_lt a ha_bs) (by omega) + · exact ih_xs hpw_bs ha_bs hb_bs tr1 hall + exact hwalk_all S hs ha hb tr hchk + +def chkSubsT (u : Bool) (Z target mask : Nat) (pts mpts rfZ rfL : List Nat) (k : Nat) : + List Nat → List Nat → List Nat → Option (List Nat) + | rem, cur, tr => + if !validOK u Z mask (cur ++ rem) pts then some tr + else match rem with + | [] => + if cur.isEmpty || Nat.blt k (cur.length - 1) || rootFail rfZ rfL cur k then some tr + else chkTreeT u Z target mask mpts rfZ rfL k cur + (List.replicate rfZ.length 0) (rfZ.map (zCount cur)) 0 0 0 0 tr + | v :: vs => + match chkSubsT u Z target mask pts mpts rfZ rfL k vs cur tr with + | none => none + | some tr1 => chkSubsT u Z target mask pts mpts rfZ rfL k vs (cur ++ [v]) tr1 + +theorem chkSubsT_sound {u flip : Bool} {Z tab mask : Nat} {pts mpts rfZ rfL : List Nat} + (hZ : Z ≤ full64) (hPM : ptsMaskOK u flip Z tab mask pts = true) + (hmpts : mpts = pts.map (2 ^ ·)) + (hRF : RFSubOK u Z tab rfZ rfL) (k : Nat) : + ∀ (rem cur tr : List Nat), + (cur ++ rem).Pairwise (· < ·) → + (chkSubsT u Z (normZ u Z tab) mask pts mpts rfZ rfL k rem cur tr).isSome = true → + ∀ (S₀ : List Nat) (steps : List (Nat × Nat × Nat × Bool × Bool)), + S₀.Pairwise (· < ·) → (∀ w ∈ S₀, w ∈ varsSorted u Z) → + (∀ w ∈ cur, w ∈ S₀) → (∀ w ∈ S₀, w ∈ cur ++ rem) → + steps.length ≤ k → + PORSteps u Z (normZ u Z tab) 0 0 0 0 S₀ steps → False := by + intro rem + induction rem with + | nil => + intro cur tr hcpw hchk S₀ steps hS0pw hS0v hcurS hS0c hlen hpor + have hEq : S₀ = cur := pairwise_eq_of_mem_iff hS0pw (by simpa using hcpw) (fun w => ⟨by simpa using hS0c w, hcurS w⟩) + subst hEq + unfold chkSubsT at hchk + simp only [List.append_nil] at hchk + by_cases hv : (!validOK u Z mask S₀ pts) = true + · simp only [Bool.not_eq_true'] at hv + exact validOK_false_no_por hZ hPM (fun _ h => h) hv hpor + · simp only [hv, Bool.false_eq_true, ↓reduceIte] at hchk + by_cases hskip : (S₀.isEmpty || Nat.blt k (S₀.length - 1) || rootFail rfZ rfL S₀ k) = true + · simp only [Bool.or_eq_true, List.isEmpty_iff, Nat.blt_eq] at hskip + rcases hskip with (h_em | h_gt) | h_rf + · subst h_em + cases steps with + | nil => simp [PORSteps] at hpor + | cons st _ => rcases st with ⟨a, _, _, _, _⟩; exact absurd hpor.2.1 (by simp) + · have := porSteps_len_bound hpor; omega + · exact rootFail_impossible hZ hRF hS0pw hS0v hlen hpor h_rf + · simp only [hskip, Bool.false_eq_true, ↓reduceIte] at hchk + exact chkTreeT_sound hZ hPM hmpts hRF k S₀ (List.replicate rfZ.length 0) (rfZ.map (zCount S₀)) 0 0 0 0 tr hchk + steps hlen hpor (rInv_init hZ hRF hS0pw hS0v hpor) + | cons v vs ih => + intro cur tr hcpw hchk S₀ steps hS0pw hS0v hcurS hS0c hlen hpor + unfold chkSubsT at hchk + by_cases hv : (!validOK u Z mask (cur ++ v :: vs) pts) = true + · simp only [Bool.not_eq_true'] at hv + exact validOK_false_no_por hZ hPM hS0c hv hpor + · simp only [hv, Bool.false_eq_true, ↓reduceIte] at hchk + cases hc1 : chkSubsT u Z (normZ u Z tab) mask pts mpts rfZ rfL k vs cur tr with + | none => rw [hc1] at hchk; simp at hchk + | some tr1 => + rw [hc1] at hchk + have hpw_skip : (cur ++ vs).Pairwise (· < ·) := by + refine List.pairwise_append.mpr ⟨(List.pairwise_append.mp hcpw).1, + (List.pairwise_cons.mp (List.pairwise_append.mp hcpw).2.1).2, fun a ha b hb => ?_⟩ + exact (List.pairwise_append.mp hcpw).2.2 a ha b (List.mem_cons_of_mem _ hb) + have hpw_keep : ((cur ++ [v]) ++ vs).Pairwise (· < ·) := by + have heq : (cur ++ [v]) ++ vs = cur ++ v :: vs := by simp [List.append_assoc] + rw [heq]; exact hcpw + by_cases hvS : v ∈ S₀ + · exact ih (cur ++ [v]) tr1 hpw_keep hchk S₀ steps hS0pw hS0v + (fun w hw => by simp at hw; rcases hw with hw | rfl; exact hcurS w hw; exact hvS) + (fun w hw => by have hw1 := hS0c w hw; simp at hw1 ⊢; tauto) hlen hpor + · exact ih cur tr hpw_skip (by simp [hc1]) S₀ steps hS0pw hS0v hcurS + (fun w hw => by + have hw1 := hS0c w hw; simp at hw1 ⊢ + rcases hw1 with hw_c | rfl | hw_vs + · left; exact hw_c + · exact absurd hw hvS + · right; exact hw_vs) hlen hpor + +theorem prove_lowU_T (u flip : Bool) (Z tab mask : Nat) (pts mpts rfZ rfL : List Nat) (k : Nat) (trP : List Nat) + (hB : baseOK u flip Z tab mask pts rfZ rfL = true) + (hmpts : mpts = pts.map (2 ^ ·)) + (hSub : RFSubOK u Z tab rfZ rfL) + (hChk : (chkSubsT u Z (normZ u Z tab) mask pts mpts rfZ rfL k (varsSorted u Z) [] (unp10 trP)).isSome = true) : + LowU u Z tab (k + 1) := by + intro P hP + by_contra hlt + have hle : P.length ≤ k := by omega + unfold baseOK at hB + simp only [Bool.and_eq_true, Bool.not_eq_true', decide_eq_false_iff_not] at hB + have hZ : Z ≤ full64 := by omega + have hnt : !trivW u Z tab = true := by + have : trivW u Z tab = false := hB.1.1.1.1.2 + simp [this] + have hTnot : normZ u Z tab ∉ varsSorted u Z := by + intro hm + have ha : (varsSorted u Z).any (· == normZ u Z tab) = true := List.any_eq_true.mpr ⟨_, hm, by simp⟩ + rw [ha] at hB; simp at hB + have hPM : ptsMaskOK u flip Z tab mask pts = true := hB.2 + obtain ⟨S₀, steps, hS0, hS0pw, hpor, hslen⟩ := validProg_to_PORSteps hZ hnt hTnot hP + exact chkSubsT_sound hZ hPM hmpts hSub k (varsSorted u Z) [] (unp10 trP) + (dedupSort_pairwise (varsOf u Z)) hChk S₀ steps hS0pw hS0 (fun _ h => by simp at h) + (fun w hw => by simpa using hS0 w hw) (by omega) hpor + +def chkJobsT (u : Bool) (Z tab mask : Nat) (mpts rfZ rfL : List Nat) : + List (List Nat × List Nat × List Nat × Nat × Nat × Nat × Nat × Nat) → List Nat → Option (List Nat) + | [], tr => some tr + | (S, nz, zc, rem_k, lkOut, lkA, lkB, lkOp) :: js, tr => + match chkTreeT u Z (normZ u Z tab) mask mpts rfZ rfL rem_k S nz zc lkOut lkA lkB lkOp tr with + | none => none + | some tr1 => chkJobsT u Z tab mask mpts rfZ rfL js tr1 + +theorem chkJobsT_sound {u flip : Bool} {Z tab mask : Nat} {pts mpts rfZ rfL : List Nat} + (hZ : Z ≤ full64) (hPM : ptsMaskOK u flip Z tab mask pts = true) + (hmpts : mpts = pts.map (2 ^ ·)) (hRF : RFSubOK u Z tab rfZ rfL) : + ∀ (jobs : List (List Nat × List Nat × List Nat × Nat × Nat × Nat × Nat × Nat)) (tr : List Nat), + (chkJobsT u Z tab mask mpts rfZ rfL jobs tr).isSome = true → + ∀ j ∈ jobs, NoPathJob u Z tab rfZ rfL j + | [], _, _, _, hj => by simp at hj + | (S, nz, zc, rem_k, lkOut, lkA, lkB, lkOp) :: js, tr, h, j, hj => by + unfold chkJobsT at h + cases ht : chkTreeT u Z (normZ u Z tab) mask mpts rfZ rfL rem_k S nz zc lkOut lkA lkB lkOp tr with + | none => rw [ht] at h; simp at h + | some tr1 => + rw [ht] at h + rcases List.mem_cons.mp hj with rfl | hj' + · exact chkTreeT_sound hZ hPM hmpts hRF rem_k S nz zc lkOut lkA lkB lkOp tr (by simp [ht]) + · exact chkJobsT_sound hZ hPM hmpts hRF js tr1 h j hj' + +theorem lowU_to_circ_lb {u : Bool} {tab V : Nat} (hLow : LowU u full64 tab V) + (hnt : !trivW u full64 tab = true) : + ∀ C : Circuit, FanInTwo C → + (u = false → ∀ g ∈ C.gates, isNotG g = false) → + (∀ x : Fin 6 → Bool, C.eval (List.ofFn x) = fOf tab x) → + V ≤ C.gates.countP (fun g => !isNotG g) := by + intro C hF hno hcmp + obtain ⟨P, hPlen, hP⟩ := circuit_to_ValidProg hF hno hcmp hnt + exact le_trans (hLow P hP) hPlen + +theorem thm_nm2 {tab V p0 p1 p2 p3 : Nat} + (gs : List (Nat × Nat × Nat)) (out : Nat) + (hU : checkCirc gs out tab = true) (hV : gs.length = V) + (hp0 : p0 < 64) (hp1 : p1 < 64) (hp2 : p2 < 64) (hp3 : p3 < 64) + (h01 : p0 &&& p1 = p0) (h12 : p1 &&& p2 = p1) (h23 : p2 &&& p3 = p2) + (ht0 : tab.testBit p0 = true) (ht1 : tab.testBit p1 = false) + (ht2 : tab.testBit p2 = true) (ht3 : tab.testBit p3 = false) + (hnt : !trivW true full64 tab = true) + (hLow : LowU true full64 tab (V - 2)) : + HasCircuitSize 6 (fOf tab) V ∧ ∀ t : Nat, HasCircuitSize 6 (fOf tab) t → V ≤ t := by + refine ⟨hV ▸ upper_of_check hU, ?_⟩ + rintro t ⟨C, hF, ht, hcmp⟩ + have hn2 := markov2_of_chain hF hcmp hp0 hp1 hp2 hp3 h01 h12 h23 ht0 ht1 ht2 ht3 + have hbin := lowU_to_circ_lb hLow hnt C hF (fun h => by cases h) hcmp + have hsp := countP_split C.gates + change C.gates.length ≤ t at ht + omega + +theorem thm_nm1 {tab V p0 p1 : Nat} + (gs : List (Nat × Nat × Nat)) (out : Nat) + (hU : checkCirc gs out tab = true) (hV : gs.length = V) + (hp0 : p0 < 64) (hp1 : p1 < 64) (h01 : p0 &&& p1 = p0) + (ht0 : tab.testBit p0 = true) (ht1 : tab.testBit p1 = false) + (hnt : !trivW true full64 tab = true) + (hLow : LowU true full64 tab (V - 1)) : + HasCircuitSize 6 (fOf tab) V ∧ ∀ t : Nat, HasCircuitSize 6 (fOf tab) t → V ≤ t := by + refine ⟨hV ▸ upper_of_check hU, ?_⟩ + rintro t ⟨C, hF, ht, hcmp⟩ + have hn1 := markov1_of_pair hF hcmp hp0 hp1 h01 ht0 ht1 + have hbin := lowU_to_circ_lb hLow hnt C hF (fun h => by cases h) hcmp + have hsp := countP_split C.gates + change C.gates.length ≤ t at ht + omega + +theorem thm_mono {tab V : Nat} + (gs : List (Nat × Nat × Nat)) (out : Nat) + (hU : checkCirc gs out tab = true) (hV : gs.length = V) + (hnt0 : !trivW false full64 tab = true) + (hnt1 : !trivW true full64 tab = true) + (hLowM : LowU false full64 tab V) + (hLowF : LowU true full64 tab (V - 1)) : + HasCircuitSize 6 (fOf tab) V ∧ ∀ t : Nat, HasCircuitSize 6 (fOf tab) t → V ≤ t := by + refine ⟨hV ▸ upper_of_check hU, ?_⟩ + rintro t ⟨C, hF, ht, hcmp⟩ + change C.gates.length ≤ t at ht + by_cases hmono : ∀ g ∈ C.gates, isNotG g = false + · have hbin := lowU_to_circ_lb hLowM hnt0 C hF (fun _ => hmono) hcmp + have h0 : C.gates.countP isNotG = 0 := by + by_contra hne + obtain ⟨g, hg, hgn⟩ := List.countP_pos_iff.mp (Nat.pos_of_ne_zero hne) + rw [hmono g hg] at hgn; exact Bool.noConfusion hgn + have hsp := countP_split C.gates + omega + · push Not at hmono + obtain ⟨g, hg, hgn⟩ := hmono + have hnt_pos : 1 ≤ C.gates.countP isNotG := + List.countP_pos_iff.mpr ⟨g, hg, by cases hc : isNotG g <;> simp_all⟩ + have hbin := lowU_to_circ_lb hLowF hnt1 C hF (fun h => by cases h) hcmp + have hsp := countP_split C.gates + omega + +def tabT : Nat := 12249967800050819279 +def witG : List (Nat × Nat × Nat) := [(2, 1, 0), (0, 5, 4), (0, 6, 2), (1, 4, 3), (1, 7, 2), (1, 8, 9), (2, 11, 0), (0, 0, 10), (0, 3, 13), (1, 14, 12)] +def rf_f_0_pts : List Nat := [1, 33, 57] +def rf_f_0_Z : List Nat := [] +def rf_f_0_L : List Nat := [] +set_option maxHeartbeats 0 in +set_option maxRecDepth 262144 in +theorem rf_f_0 : LowU true 144680345676153346 tabT 4 := + prove_lowU true false 144680345676153346 tabT 565149010362880 rf_f_0_pts rf_f_0_Z rf_f_0_L 3 + (by decide +kernel) + trivial + (by decide +kernel) +def rf_f_1_pts : List Nat := [33, 45, 57, 61] +def rf_f_1_Z : List Nat := [] +def rf_f_1_L : List Nat := [] +set_option maxHeartbeats 0 in +set_option maxRecDepth 262144 in +theorem rf_f_1 : LowU true 2459565875921944576 tabT 4 := + prove_lowU true false 2459565875921944576 tabT 9572485670371328 rf_f_1_pts rf_f_1_Z rf_f_1_L 3 + (by decide +kernel) + trivial + (by decide +kernel) +def rf_f_2_pts : List Nat := [1, 13, 29] +def rf_f_2_Z : List Nat := [] +def rf_f_2_L : List Nat := [] +set_option maxHeartbeats 0 in +set_option maxRecDepth 262144 in +theorem rf_f_2 : LowU true 572662306 tabT 4 := + prove_lowU true false 572662306 tabT 35783200 rf_f_2_pts rf_f_2_Z rf_f_2_L 3 + (by decide +kernel) + trivial + (by decide +kernel) +def rf_f_3_pts : List Nat := [1, 13, 33, 45] +def rf_f_3_Z : List Nat := [] +def rf_f_3_L : List Nat := [] +set_option maxHeartbeats 0 in +set_option maxRecDepth 262144 in +theorem rf_f_3 : LowU true 37529424241186 tabT 3 := + prove_lowU true false 37529424241186 tabT 2336462209568 rf_f_3_pts rf_f_3_Z rf_f_3_L 2 + (by decide +kernel) + trivial + (by decide +kernel) +def rf_f_4_pts : List Nat := [1, 13, 29, 33, 45, 57, 61] +def rf_f_4_Z : List Nat := [144680345676153346, 2459565875921944576, 572662306, 37529424241186] +def rf_f_4_L : List Nat := [4, 4, 4, 3] +def rf_f_4_mpts : List Nat := [2, 8192, 536870912, 8589934592, 35184372088832, 144115188075855872, 2305843009213693952] +def rf_f_4_tr : List Nat := [18590683296964802, 36882058349641921, 576465254504071171, 1130402107498500, 81084585038844001, 853429337597952, 1271036540616997, 576465254503944290, 18447674887129088, 603763894913073217, 288106406481985] +set_option maxHeartbeats 0 in +set_option maxRecDepth 262144 in +theorem rf_f_4 : LowU true 2459565876494606882 tabT 6 := + prove_lowU_T true false 2459565876494606882 tabT 9572485706154528 rf_f_4_pts rf_f_4_mpts rf_f_4_Z rf_f_4_L 5 rf_f_4_tr + (by decide +kernel) + (by decide +kernel) + ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_0⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_1⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_2⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_3⟩, trivial⟩⟩⟩⟩ + (by decide +kernel) +def rf_f_5_pts : List Nat := [43, 51, 55] +def rf_f_5_Z : List Nat := [] +def rf_f_5_L : List Nat := [] +set_option maxHeartbeats 0 in +set_option maxRecDepth 262144 in +theorem rf_f_5 : LowU true 9838263503687778304 tabT 4 := + prove_lowU true true 9838263503687778304 tabT 9799974110762106880 rf_f_5_pts rf_f_5_Z rf_f_5_L 3 + (by decide +kernel) + trivial + (by decide +kernel) +def rf_f_6_pts : List Nat := [37, 41, 43] +def rf_f_6_Z : List Nat := [] +def rf_f_6_L : List Nat := [] +set_option maxHeartbeats 0 in +set_option maxRecDepth 262144 in +theorem rf_f_6 : LowU true 187647121162240 tabT 4 := + prove_lowU true true 187647121162240 tabT 176514565931008 rf_f_6_pts rf_f_6_Z rf_f_6_L 3 + (by decide +kernel) + trivial + (by decide +kernel) +def rf_f_7_pts : List Nat := [33, 35, 57, 59] +def rf_f_7_Z : List Nat := [] +def rf_f_7_L : List Nat := [] +set_option maxHeartbeats 0 in +set_option maxRecDepth 262144 in +theorem rf_f_7 : LowU true 723401728212336640 tabT 3 := + prove_lowU true false 723401728212336640 tabT 2825744883384320 rf_f_7_pts rf_f_7_Z rf_f_7_L 2 + (by decide +kernel) + trivial + (by decide +kernel) +def rf_f_8_pts : List Nat := [37, 41, 43, 49, 51, 53, 55] +def rf_f_8_Z : List Nat := [9838263503687778304, 2459565875921944576, 187647121162240, 723401728212336640] +def rf_f_8_L : List Nat := [4, 4, 4, 3] +def rf_f_8_mpts : List Nat := [137438953472, 2199023255552, 8796093022208, 562949953421312, 2251799813685248, 9007199254740992, 36028797018963968] +def rf_f_8_tr : List Nat := [631489113530579008, 594488591972827331, 576465289953947907, 73055954370824225, 576478620475201536, 576465289953419393, 576478655354519840, 73047158311356448, 18586248709152897, 36314809645670529, 66] +set_option maxHeartbeats 0 in +set_option maxRecDepth 262144 in +theorem rf_f_8 : LowU true 12297829379609722880 tabT 6 := + prove_lowU_T true true 12297829379609722880 tabT 12249967501013680128 rf_f_8_pts rf_f_8_mpts rf_f_8_Z rf_f_8_L 5 rf_f_8_tr + (by decide +kernel) + (by decide +kernel) + ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_5⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_1⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_6⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_7⟩, trivial⟩⟩⟩⟩ + (by decide +kernel) +def rf_f_9_pts : List Nat := [3, 35, 59] +def rf_f_9_Z : List Nat := [] +def rf_f_9_L : List Nat := [] +set_option maxHeartbeats 0 in +set_option maxRecDepth 262144 in +theorem rf_f_9 : LowU true 578721382704613384 tabT 4 := + prove_lowU true false 578721382704613384 tabT 2260596041451520 rf_f_9_pts rf_f_9_Z rf_f_9_L 3 + (by decide +kernel) + trivial + (by decide +kernel) +def rf_f_10_pts : List Nat := [3, 7, 15, 31] +def rf_f_10_Z : List Nat := [] +def rf_f_10_L : List Nat := [] +set_option maxHeartbeats 0 in +set_option maxRecDepth 262144 in +theorem rf_f_10 : LowU true 2290649224 tabT 3 := + prove_lowU true false 2290649224 tabT 143132672 rf_f_10_pts rf_f_10_Z rf_f_10_L 2 + (by decide +kernel) + trivial + (by decide +kernel) +def rf_f_11_pts : List Nat := [11, 19, 23, 27, 43, 51, 55] +def rf_f_11_Z : List Nat := [578721382704613384, 9838263503687778304, 2290649224] +def rf_f_11_L : List Nat := [4, 4, 3] +def rf_f_11_mpts : List Nat := [2048, 524288, 8388608, 134217728, 8796093022208, 2251799813685248, 36028797018963968] +def rf_f_11_tr : List Nat := [] +set_option maxHeartbeats 0 in +set_option maxRecDepth 262144 in +theorem rf_f_11 : LowU true 9838263505978427528 tabT 5 := + prove_lowU_T true true 9838263505978427528 tabT 9799974112909623432 rf_f_11_pts rf_f_11_mpts rf_f_11_Z rf_f_11_L 4 rf_f_11_tr + (by decide +kernel) + (by decide +kernel) + ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_9⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_5⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_10⟩, trivial⟩⟩⟩ + (by decide +kernel) +def rf_f_12_pts : List Nat := [5, 9, 11] +def rf_f_12_Z : List Nat := [] +def rf_f_12_L : List Nat := [] +set_option maxHeartbeats 0 in +set_option maxRecDepth 262144 in +theorem rf_f_12 : LowU true 43690 tabT 4 := + prove_lowU true true 43690 tabT 41098 rf_f_12_pts rf_f_12_Z rf_f_12_L 3 + (by decide +kernel) + trivial + (by decide +kernel) +def rf_f_13_pts : List Nat := [1, 3, 7, 13, 15, 29, 31] +def rf_f_13_Z : List Nat := [572662306, 43690, 2290649224] +def rf_f_13_L : List Nat := [4, 4, 3] +def rf_f_13_mpts : List Nat := [2, 8, 128, 8192, 32768, 536870912, 2147483648] +def rf_f_13_tr : List Nat := [] +set_option maxHeartbeats 0 in +set_option maxRecDepth 262144 in +theorem rf_f_13 : LowU true 2863311530 tabT 5 := + prove_lowU_T true false 2863311530 tabT 178915872 rf_f_13_pts rf_f_13_mpts rf_f_13_Z rf_f_13_L 4 rf_f_13_tr + (by decide +kernel) + (by decide +kernel) + ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_2⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_12⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_10⟩, trivial⟩⟩⟩ + (by decide +kernel) +def rf_f_14_pts : List Nat := [1, 3, 33, 35, 57, 59] +def rf_f_14_Z : List Nat := [578721382704613384, 144680345676153346, 723401728212336640] +def rf_f_14_L : List Nat := [4, 4, 3] +set_option maxHeartbeats 0 in +set_option maxRecDepth 262144 in +theorem rf_f_14 : LowU true 723401728380766730 tabT 4 := + prove_lowU true false 723401728380766730 tabT 2825745051814400 rf_f_14_pts rf_f_14_Z rf_f_14_L 3 + (by decide +kernel) + ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_9⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_0⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_7⟩, trivial⟩⟩⟩ + (by decide +kernel) +def rf_f_15_pts : List Nat := [5, 9, 11, 37, 41, 43] +def rf_f_15_Z : List Nat := [187647121162240, 43690, 37529424241186] +def rf_f_15_L : List Nat := [4, 4, 3] +set_option maxHeartbeats 0 in +set_option maxRecDepth 262144 in +theorem rf_f_15 : LowU true 187647121205930 tabT 4 := + prove_lowU true true 187647121205930 tabT 176514565972106 rf_f_15_pts rf_f_15_Z rf_f_15_L 3 + (by decide +kernel) + ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_6⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_12⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_3⟩, trivial⟩⟩⟩ + (by decide +kernel) +def rf_f_16_pts : List Nat := [1, 3, 7, 13, 15, 29, 31, 33, 35, 39, 45, 47, 57, 59, 61, 63] +def rf_f_16_Z : List Nat := [2459565876494606882, 12297829379609722880, 9838263505978427528, 2863311530, 723401728380766730, 187647121205930, 43690, 578721382704613384, 144680345676153346, 9838263503687778304] +def rf_f_16_L : List Nat := [6, 6, 5, 5, 4, 4, 4, 4, 4, 4] +def rf_f_16_mpts : List Nat := [2, 8, 128, 8192, 32768, 536870912, 2147483648, 8589934592, 34359738368, 549755813888, 35184372088832, 140737488355328, 144115188075855872, 576460752303423488, 2305843009213693952, 9223372036854775808] +def rf_f_16_tr : List Nat := [] +set_option maxHeartbeats 0 in +set_option maxRecDepth 262144 in +theorem rf_f_16 : LowU true 12297829382473034410 tabT 7 := + prove_lowU_T true false 12297829382473034410 tabT 47861878774958624 rf_f_16_pts rf_f_16_mpts rf_f_16_Z rf_f_16_L 6 rf_f_16_tr + (by decide +kernel) + (by decide +kernel) + ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_4⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_8⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_11⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_13⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_14⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_15⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_12⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_9⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_0⟩, ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_5⟩, trivial⟩⟩⟩⟩⟩⟩⟩⟩⟩⟩ + (by decide +kernel) +def root_f_pts : List Nat := [0, 1, 2, 3, 6, 7, 13, 15, 29, 31, 32, 33, 34, 35, 38, 39, 45, 47, 57, 59, 61, 63] +def root_f_Z : List Nat := [12297829382473034410] +def root_f_L : List Nat := [7] +set_option maxHeartbeats 0 in +set_option maxRecDepth 262144 in +theorem root_f : LowU true 18446744073709551615 tabT 8 := + prove_lowU true false 18446744073709551615 tabT 6196776273658732336 root_f_pts root_f_Z root_f_L 7 + (by decide +kernel) + ⟨⟨by decide +kernel, by decide +kernel, by decide +kernel, rf_f_16⟩, trivial⟩ + (by decide +kernel) +set_option maxHeartbeats 0 in +theorem main_thm : HasCircuitSize 6 (fOf tabT) 10 ∧ ∀ t : Nat, HasCircuitSize 6 (fOf tabT) t → 10 ≤ t := + thm_nm2 (p0 := 0) (p1 := 4) (p2 := 6) (p3 := 14) witG 15 (by decide +kernel) (by decide) + (by decide) (by decide) (by decide) (by decide) + (by decide) (by decide) (by decide) + (by decide +kernel) (by decide +kernel) (by decide +kernel) (by decide +kernel) + (by decide +kernel) root_f + +end PEqualNP.Outside.V_meta_126329__k10 + +theorem PEqualNP.Outside.V_meta_126329__k10 : PEqualNP.MetaComplexity.HasCircuitSize 6 (fun x : Fin 6 → Bool => (12249967800050819279 : ℕ).testBit (PEqualNP.MetaComplexity.tableIndex x)) 10 ∧ ∀ t : ℕ, PEqualNP.MetaComplexity.HasCircuitSize 6 (fun x : Fin 6 → Bool => (12249967800050819279 : ℕ).testBit (PEqualNP.MetaComplexity.tableIndex x)) t → 10 ≤ t := + PEqualNP.Outside.V_meta_126329__k10.main_thm