Lean / 0xOsprey/shub-smale-tau9 / Canonical.lean
Lean reader

Research/PvsNP/Tau/Canonical.lean

1,845 lines118 declarationsno sorry
  1. module
  2. public import Research.PvsNP.Tau.TauConjecture
  3. @[expose] public section
  4. namespace Research.PvsNP.Tau
  5. /-!
  6. # Canonicalization and Pruning Soundness for Constant-Free SLPs
  7. This module formalizes the list-based straight-line program (`GateSpec`) representation,
  8. its equivalence with `SLP τ`, the canonicalization predicate `Canonical` used by the
  9. exhaustive enumerator, and the soundness theorems:
  10. 1. `exists_canonical`: every nonzero polynomial `f` computed by a length-`t` `SLP`
  11. either belongs to `{1, -1, X, -X}` or is computed up to sign (`±f`) by a
  12. `Canonical` gate list of length `1 ≤ k ≤ t`.
  13. 2. `deadgate_count_bound`: in any `Canonical` (or `UsedGates`) gate list of length `n`,
  14. after `d < n` gates, the number of unused gate wires `unusedWires gs d` satisfies
  15. `≤ n - d + 1`, and at the last gate (`d = n - 1`) at most `2` gate wires remain unused
  16. and every unused gate wire must be an operand of the last gate.
  17. 3. `zmax_le_of_checklist`, `zmax9_le_six_of_checklist`, `zmax10_le_seven_of_checklist`:
  18. conditional upper bounds transferring root-count bounds on all `Canonical` gate lists
  19. of length `1 ≤ n ≤ τ` to all nonzero `SLP t` with `t ≤ τ`.
  20. -/
  21. /-! ## 1. List-Based Gate Representation & Equivalence with `SLP` -/
  22. /-- A single gate in a list-based constant-free straight-line program.
  23. Gate `k` (0-based) reads wires `i` and `j` and outputs wire `k + 2`. -/
  24. structure GateSpec where
  25. op : SLPOp
  26. i : ℕ
  27. j : ℕ
  28. deriving DecidableEq, Repr
  29. instance : Inhabited GateSpec := ⟨⟨.add, 0, 0⟩⟩
  30. /-- Auxiliary accumulator evaluator for a list of gate specifications over `ℤ[X]`. -/
  31. noncomputable def wireValsAux : List (Polynomial ℤ) → List GateSpec → List (Polynomial ℤ)
  32. | acc, [] => acc
  33. | acc, g :: rest =>
  34. wireValsAux (acc ++ [g.op.applyPoly (acc.getD g.i 0) (acc.getD g.j 0)]) rest
  35. /-- Polynomial values of all wires `0, 1, ..., gs.length + 1` computed by `gs`.
  36. Wire `0` holds `1`, wire `1` holds `X`, and gate `k` computes wire `k + 2`. -/
  37. noncomputable def wireVals (gs : List GateSpec) : List (Polynomial ℤ) :=
  38. wireValsAux [1, Polynomial.X] gs
  39. /-- Output polynomial of `gs` (wire `gs.length + 1`, or `X` when `gs = []`). -/
  40. noncomputable def output (gs : List GateSpec) : Polynomial ℤ :=
  41. (wireVals gs).getD (gs.length + 1) 0
  42. /-- A gate list `gs` is well-formed when every gate `k` reads only wires `< k + 2`. -/
  43. def WellFormed (gs : List GateSpec) : Prop :=
  44. ∀ (k : ℕ) (hk : k < gs.length), gs[k].i < k + 2 ∧ gs[k].j < k + 2
  45. private lemma getD_eq_getElem_of_lt {α : Type*} (l : List α) (d : α) {i : ℕ}
  46. (hi : i < l.length) : l.getD i d = l[i] := by
  47. simp [List.getD, hi]
  48. private lemma getD_append_left {α : Type*} (l₁ l₂ : List α) (d : α) {i : ℕ}
  49. (hi : i < l₁.length) : (l₁ ++ l₂).getD i d = l₁.getD i d := by
  50. have hi' : i < (l₁ ++ l₂).length := by simp; omega
  51. rw [getD_eq_getElem_of_lt _ d hi', getD_eq_getElem_of_lt _ d hi, List.getElem_append_left hi]
  52. private lemma getD_append_right {α : Type*} (l₁ l₂ : List α) (d : α) {i : ℕ}
  53. (hi : l₁.length ≤ i) : (l₁ ++ l₂).getD i d = l₂.getD (i - l₁.length) d := by
  54. by_cases hlt : i < (l₁ ++ l₂).length
  55. · have hlt2 : i - l₁.length < l₂.length := by simp at hlt; omega
  56. rw [getD_eq_getElem_of_lt _ d hlt, getD_eq_getElem_of_lt _ d hlt2, List.getElem_append_right hi]
  57. · have hge1 : (l₁ ++ l₂).length ≤ i := Nat.le_of_not_lt hlt
  58. have hge2 : l₂.length ≤ i - l₁.length := by simp at hge1; omega
  59. simp [List.getD, List.getElem?_eq_none hge1, List.getElem?_eq_none hge2]
  60. theorem wireValsAux_length (acc : List (Polynomial ℤ)) (gs : List GateSpec) :
  61. (wireValsAux acc gs).length = acc.length + gs.length := by
  62. induction gs generalizing acc with
  63. | nil => simp [wireValsAux]
  64. | cons g rest ih =>
  65. simp only [wireValsAux]
  66. rw [ih]
  67. simp
  68. omega
  69. theorem wireVals_length (gs : List GateSpec) :
  70. (wireVals gs).length = gs.length + 2 := by
  71. rw [wireVals, wireValsAux_length]
  72. simp
  73. omega
  74. theorem wireValsAux_append (acc : List (Polynomial ℤ)) (gs₁ gs₂ : List GateSpec) :
  75. wireValsAux acc (gs₁ ++ gs₂) = wireValsAux (wireValsAux acc gs₁) gs₂ := by
  76. induction gs₁ generalizing acc with
  77. | nil => simp [wireValsAux]
  78. | cons g rest ih => simp [wireValsAux, ih]
  79. theorem wireVals_snoc (gs : List GateSpec) (g : GateSpec) :
  80. wireVals (gs ++ [g]) =
  81. wireVals gs ++ [g.op.applyPoly ((wireVals gs).getD g.i 0) ((wireVals gs).getD g.j 0)] := by
  82. simp [wireVals, wireValsAux_append, wireValsAux]
  83. theorem wireValsAux_getD_lt (acc : List (Polynomial ℤ)) (gs : List GateSpec)
  84. {w : ℕ} (hw : w < acc.length) :
  85. (wireValsAux acc gs).getD w 0 = acc.getD w 0 := by
  86. induction gs generalizing acc with
  87. | nil => rfl
  88. | cons g rest ih =>
  89. have hw' : w < (acc ++ [g.op.applyPoly (acc.getD g.i 0) (acc.getD g.j 0)]).length := by
  90. simp; omega
  91. rw [wireValsAux, ih _ hw', getD_append_left _ _ _ hw]
  92. theorem wireVals_zero (gs : List GateSpec) : (wireVals gs).getD 0 0 = 1 := by
  93. have h := wireValsAux_getD_lt [1, Polynomial.X] gs (w := 0) (by decide)
  94. simpa [wireVals] using h
  95. theorem wireVals_one (gs : List GateSpec) : (wireVals gs).getD 1 0 = Polynomial.X := by
  96. have h := wireValsAux_getD_lt [1, Polynomial.X] gs (w := 1) (by decide)
  97. simpa [wireVals] using h
  98. theorem wireVals_take_getD (gs : List GateSpec) {m w : ℕ}
  99. (hm : m ≤ gs.length) (hw : w < m + 2) :
  100. (wireVals (gs.take m)).getD w 0 = (wireVals gs).getD w 0 := by
  101. have hsplit : gs = gs.take m ++ gs.drop m := (List.take_append_drop m gs).symm
  102. have hlen : (wireVals (gs.take m)).length = m + 2 := by
  103. rw [wireVals_length, List.length_take, Nat.min_eq_left hm]
  104. have haux : wireVals gs = wireValsAux (wireVals (gs.take m)) (gs.drop m) := by
  105. rw [wireVals, wireVals, ← wireValsAux_append, ← hsplit]
  106. rw [haux]
  107. exact (wireValsAux_getD_lt (wireVals (gs.take m)) (gs.drop m) (hw.trans_eq hlen.symm)).symm
  108. theorem wireVals_gate_take (gs : List GateSpec) {k : ℕ} (hk : k < gs.length) :
  109. (wireVals gs).getD (k + 2) 0 =
  110. (gs[k]).op.applyPoly
  111. ((wireVals (gs.take k)).getD (gs[k]).i 0)
  112. ((wireVals (gs.take k)).getD (gs[k]).j 0) := by
  113. have htake : gs.take (k + 1) = gs.take k ++ [gs[k]] := List.take_add_one.trans (by simp [hk])
  114. have hlen : (wireVals (gs.take k)).length = k + 2 := by
  115. rw [wireVals_length, List.length_take, Nat.min_eq_left hk.le]
  116. have hw := wireVals_take_getD gs (m := k + 1) (w := k + 2) hk (by omega)
  117. rw [← hw, htake, wireVals_snoc, getD_append_right _ _ _ hlen.le]
  118. simp [hlen]
  119. theorem wireVals_gate (gs : List GateSpec) {k : ℕ} (hk : k < gs.length)
  120. (hi : gs[k].i < k + 2) (hj : gs[k].j < k + 2) :
  121. (wireVals gs).getD (k + 2) 0 =
  122. (gs[k]).op.applyPoly ((wireVals gs).getD gs[k].i 0) ((wireVals gs).getD gs[k].j 0) := by
  123. rw [wireVals_gate_take gs hk,
  124. wireVals_take_getD gs hk.le hi,
  125. wireVals_take_getD gs hk.le hj]
  126. theorem wireVals_gate_wf {gs : List GateSpec} (hwf : WellFormed gs) {k : ℕ} (hk : k < gs.length) :
  127. (wireVals gs).getD (k + 2) 0 =
  128. (gs[k]).op.applyPoly ((wireVals gs).getD gs[k].i 0) ((wireVals gs).getD gs[k].j 0) :=
  129. wireVals_gate gs hk (hwf k hk).1 (hwf k hk).2
  130. /-- Convert an `SLP τ` into a well-formed list of `τ` gate specifications. -/
  131. def SLP.toGateList : {τ : ℕ} → SLP τ → List GateSpec
  132. | 0, .nil => []
  133. | _ + 1, .snoc P op i j => P.toGateList ++ [⟨op, i.val, j.val⟩]
  134. theorem SLP.toGateList_length : ∀ {τ : ℕ} (P : SLP τ), P.toGateList.length = τ
  135. | 0, .nil => rfl
  136. | _ + 1, .snoc P _ _ _ => by simp [SLP.toGateList, P.toGateList_length]
  137. theorem SLP.toGateList_wellFormed : ∀ {τ : ℕ} (P : SLP τ), WellFormed P.toGateList
  138. | 0, .nil => fun k hk => by simp [SLP.toGateList] at hk
  139. | τ + 1, .snoc P op i j => by
  140. intro k hk
  141. simp only [SLP.toGateList, List.length_append, List.length_singleton, P.toGateList_length] at hk
  142. by_cases hlt : k < τ
  143. · have hlen : k < P.toGateList.length := by simpa [P.toGateList_length] using hlt
  144. simpa [SLP.toGateList, List.getElem_append_left hlen] using P.toGateList_wellFormed k hlen
  145. · have heq : k = τ := by omega
  146. subst k
  147. have hlen : P.toGateList.length = τ := P.toGateList_length
  148. simp only [SLP.toGateList, List.getElem_append_right hlen.le, hlen, Nat.sub_self,
  149. List.getElem_singleton]
  150. exact ⟨i.isLt, j.isLt⟩
  151. theorem SLP.wireVals_toGateList :
  152. ∀ {τ : ℕ} (P : SLP τ) (k : Fin (τ + 2)),
  153. (wireVals P.toGateList).getD k.val 0 = P.evalWire k
  154. | 0, .nil, k => by
  155. simp only [SLP.toGateList, wireVals, wireValsAux, SLP.evalWire]
  156. rcases k with ⟨kv, hkv⟩
  157. rcases kv with _ | _ | kv
  158. · simp [List.getD]
  159. · simp [List.getD]
  160. · omega
  161. | τ + 1, .snoc P op i j, k => by
  162. simp only [SLP.toGateList, wireVals_snoc, SLP.evalWire]
  163. have hlen : (wireVals P.toGateList).length = τ + 2 := by
  164. rw [wireVals_length, P.toGateList_length]
  165. split_ifs with hk
  166. · rw [getD_append_left _ _ _ (hk.trans_eq hlen.symm)]
  167. exact P.wireVals_toGateList ⟨k.val, hk⟩
  168. · have hk_eq : k.val = τ + 2 := by omega
  169. rw [getD_append_right _ _ _ (by omega)]
  170. simp only [hlen, hk_eq, Nat.sub_self]
  171. rw [getD_eq_getElem_of_lt _ _ (by simp), List.getElem_singleton]
  172. rw [P.wireVals_toGateList i, P.wireVals_toGateList j]
  173. theorem SLP.output_toGateList {τ : ℕ} (P : SLP τ) :
  174. output P.toGateList = P.evalPoly := by
  175. have h := P.wireVals_toGateList ⟨τ + 1, Nat.lt_succ_self _⟩
  176. simpa [output, SLP.evalPoly, P.toGateList_length] using h
  177. /-- Auxiliary builder turning the prefix of length `n ≤ gs.length` of a well-formed gate list
  178. into an `SLP n`. -/
  179. def SLP.ofGateListAux (gs : List GateSpec) (hwf : WellFormed gs) :
  180. (n : ℕ) → n ≤ gs.length → SLP n
  181. | 0, _ => .nil
  182. | n + 1, hn =>
  183. let P := SLP.ofGateListAux gs hwf n (Nat.le_of_lt hn)
  184. let g := gs[n]'hn
  185. let hg := hwf n hn
  186. P.snoc g.op ⟨g.i, hg.1⟩ ⟨g.j, hg.2⟩
  187. /-- Convert a well-formed gate list `gs` back into an `SLP gs.length`. -/
  188. def SLP.ofGateList (gs : List GateSpec) (hwf : WellFormed gs) : SLP gs.length :=
  189. SLP.ofGateListAux gs hwf gs.length le_rfl
  190. theorem SLP.evalWire_ofGateListAux (gs : List GateSpec) (hwf : WellFormed gs) :
  191. ∀ (n : ℕ) (hn : n ≤ gs.length) (k : Fin (n + 2)),
  192. (SLP.ofGateListAux gs hwf n hn).evalWire k = (wireVals (gs.take n)).getD k.val 0
  193. | 0, _, k => by
  194. simp only [SLP.ofGateListAux, SLP.evalWire, List.take_zero, wireVals, wireValsAux]
  195. rcases k with ⟨kv, hkv⟩
  196. rcases kv with _ | _ | kv
  197. · simp [List.getD]
  198. · simp [List.getD]
  199. · omega
  200. | n + 1, hn, k => by
  201. simp only [SLP.ofGateListAux, SLP.evalWire]
  202. have htake : gs.take (n + 1) = gs.take n ++ [gs[n]'hn] := by
  203. rw [List.take_add_one, List.getElem?_eq_getElem (Nat.lt_of_succ_le hn)]
  204. rfl
  205. have hlen : (wireVals (gs.take n)).length = n + 2 := by
  206. rw [wireVals_length, List.length_take, Nat.min_eq_left (Nat.le_of_lt hn)]
  207. split_ifs with hk
  208. · rw [SLP.evalWire_ofGateListAux gs hwf n (Nat.le_of_lt hn) ⟨k.val, hk⟩,
  209. htake, wireVals_snoc, getD_append_left _ _ _ (hk.trans_eq hlen.symm)]
  210. · have hk_eq : k.val = n + 2 := by omega
  211. rw [htake, wireVals_snoc, getD_append_right _ _ _ (by omega)]
  212. simp only [hlen, hk_eq, Nat.sub_self]
  213. rw [getD_eq_getElem_of_lt _ _ (by simp), List.getElem_singleton]
  214. rw [SLP.evalWire_ofGateListAux gs hwf n (Nat.le_of_lt hn),
  215. SLP.evalWire_ofGateListAux gs hwf n (Nat.le_of_lt hn)]
  216. theorem SLP.evalPoly_ofGateList (gs : List GateSpec) (hwf : WellFormed gs) :
  217. (SLP.ofGateList gs hwf).evalPoly = output gs := by
  218. have h := SLP.evalWire_ofGateListAux gs hwf gs.length le_rfl ⟨gs.length + 1, Nat.lt_succ_self _⟩
  219. simpa [SLP.ofGateList, SLP.evalPoly, output] using h
  220. /-! ## 2. Canonical Predicate -/
  221. /-- Canonical numeric index of an `SLPOp` (`add = 0`, `sub = 1`, `mul = 2`). -/
  222. def SLPOp.idx : SLPOp → ℕ
  223. | .add => 0
  224. | .sub => 1
  225. | .mul => 2
  226. /-- Operand-order condition `(ord)` for a single gate:
  227. `i < j` for subtraction, `i ≤ j` for addition and multiplication. -/
  228. def GateSpec.Ord (g : GateSpec) : Prop :=
  229. match g.op with
  230. | .sub => g.i < g.j
  231. | .add | .mul => g.i ≤ g.j
  232. /-- Lexicographic `≤` comparison on `(op.idx, i, j)` triples. -/
  233. def GateSpec.keyLe (g₁ g₂ : GateSpec) : Prop :=
  234. g₁.op.idx < g₂.op.idx ∨
  235. (g₁.op.idx = g₂.op.idx ∧ (g₁.i < g₂.i ∨ (g₁.i = g₂.i ∧ g₁.j ≤ g₂.j)))
  236. /-- Strict lexicographic `<` comparison on `(op.idx, i, j)` triples. -/
  237. def GateSpec.keyLt (g₁ g₂ : GateSpec) : Prop :=
  238. g₁.op.idx < g₂.op.idx ∨
  239. (g₁.op.idx = g₂.op.idx ∧ (g₁.i < g₂.i ∨ (g₁.i = g₂.i ∧ g₁.j < g₂.j)))
  240. /-- Base-32 packed integer key `op * 1024 + i * 32 + j` used by the C enumerator
  241. when wire indices are `< 32` (which holds whenever `gs.length ≤ 30` and `WellFormed gs`). -/
  242. def GateSpec.key32 (g : GateSpec) : ℕ :=
  243. g.op.idx * 1024 + g.i * 32 + g.j
  244. theorem GateSpec.not_keyLe_iff {g₁ g₂ : GateSpec} : ¬ g₁.keyLe g₂ ↔ g₂.keyLt g₁ := by
  245. unfold GateSpec.keyLe GateSpec.keyLt
  246. omega
  247. theorem GateSpec.keyLe_iff_key32 {g₁ g₂ : GateSpec}
  248. (hi₁ : g₁.i < 32) (hj₁ : g₁.j < 32) (hi₂ : g₂.i < 32) (hj₂ : g₂.j < 32) :
  249. g₁.keyLe g₂ ↔ g₁.key32 ≤ g₂.key32 := by
  250. unfold GateSpec.keyLe GateSpec.key32
  251. omega
  252. /-- Every gate in `gs` satisfies the operand-order condition `(ord)`. -/
  253. def OrdCanonical (gs : List GateSpec) : Prop :=
  254. ∀ (k : ℕ) (hk : k < gs.length), gs[k].Ord
  255. /-- Every gate wire `k + 2` in `gs` evaluates to a nonzero polynomial `(nonzero)`. -/
  256. def NonzeroWires (gs : List GateSpec) : Prop :=
  257. ∀ (k : ℕ) (hk : k < gs.length), (wireVals gs).getD (k + 2) 0 ≠ 0
  258. /-- No gate wire `k + 2` duplicates an earlier wire `m < k + 2` up to sign `(nodup)`. -/
  259. def NodupWires (gs : List GateSpec) : Prop :=
  260. ∀ (k : ℕ) (hk : k < gs.length) (m : ℕ), m < k + 2 →
  261. (wireVals gs).getD (k + 2) 0 ≠ (wireVals gs).getD m 0 ∧
  262. (wireVals gs).getD (k + 2) 0 ≠ -((wireVals gs).getD m 0)
  263. /-- Every gate except the last is an operand of at least one later gate `(used)`. -/
  264. def UsedGates (gs : List GateSpec) : Prop :=
  265. ∀ (k : ℕ), k + 1 < gs.length →
  266. ∃ (k' : ℕ) (hk' : k' < gs.length), k < k' ∧ (gs[k'].i = k + 2 ∨ gs[k'].j = k + 2)
  267. /-- For every non-final gate `k` (`1 ≤ k ≤ n - 2`) that does not use gate `k - 1`
  268. (wire `k + 1`), the key triple of gate `k - 1` precedes or equals that of gate `k` `(lex)`. -/
  269. def LexOrdered (gs : List GateSpec) : Prop :=
  270. ∀ (k : ℕ), 1 ≤ k → k + 1 < gs.length →
  271. (h1 : k - 1 < gs.length) → (h2 : k < gs.length) →
  272. gs[k].i ≠ k + 1 → gs[k].j ≠ k + 1 → (gs[k - 1]'h1).keyLe (gs[k]'h2)
  273. /-- Complete canonicality predicate matching the exhaustive enumerator. -/
  274. structure Canonical (gs : List GateSpec) : Prop where
  275. wf : WellFormed gs
  276. ord : OrdCanonical gs
  277. nonzero : NonzeroWires gs
  278. nodup : NodupWires gs
  279. used : UsedGates gs
  280. lex : LexOrdered gs
  281. /-! ## 3. Reduction Lemmas Toward `exists_canonical` -/
  282. /-- Target predicate: `gs` computes `f` or `-f`. -/
  283. def ComputesSigned (f : Polynomial ℤ) (gs : List GateSpec) : Prop :=
  284. output gs = f ∨ output gs = -f
  285. private noncomputable def sgnVal (b : Bool) : Polynomial ℤ :=
  286. if b then -1 else 1
  287. private lemma sgnVal_mul_self (b : Bool) : sgnVal b * sgnVal b = 1 := by
  288. cases b <;> simp [sgnVal]
  289. private lemma sgnVal_xor (b₁ b₂ : Bool) : sgnVal (xor b₁ b₂) = sgnVal b₁ * sgnVal b₂ := by
  290. cases b₁ <;> cases b₂ <;> simp [sgnVal]
  291. private lemma sgnVal_not (b : Bool) : sgnVal (!b) = -sgnVal b := by
  292. cases b <;> simp [sgnVal]
  293. private lemma sgnVal_ne_zero (b : Bool) : sgnVal b ≠ 0 := by
  294. cases b <;> simp [sgnVal]
  295. private def flipAddSub : SLPOp → SLPOp
  296. | .add => .sub
  297. | .sub => .add
  298. | .mul => .mul
  299. private lemma applyPoly_signed_combine (op : SLPOp) (s₁ s₂ : Bool) (a b : Polynomial ℤ) :
  300. (match op with
  301. | .mul => sgnVal (xor s₁ s₂) * (a * b)
  302. | .add => sgnVal s₁ * ((if s₁ = s₂ then op else flipAddSub op).applyPoly a b)
  303. | .sub => sgnVal s₁ * ((if s₁ = s₂ then op else flipAddSub op).applyPoly a b)) =
  304. op.applyPoly (sgnVal s₁ * a) (sgnVal s₂ * b) := by
  305. cases op <;> cases s₁ <;> cases s₂ <;>
  306. simp [sgnVal, flipAddSub, SLPOp.applyPoly] <;> ring
  307. private def shiftDown (k w : ℕ) : ℕ :=
  308. if k + 2 < w then w - 1 else w
  309. private lemma shiftDown_lt {k m w : ℕ} (hm : k ≤ m) (hw : w < m + 3) (hne : w ≠ k + 2) :
  310. shiftDown k w < m + 2 := by
  311. unfold shiftDown
  312. split_ifs <;> omega
  313. private lemma shiftDown_inv {k w : ℕ} (hne : w ≠ k + 2) :
  314. (if shiftDown k w < k + 2 then shiftDown k w else shiftDown k w + 1) = w := by
  315. unfold shiftDown
  316. split_ifs <;> omega
  317. /-- Delete unused gate `k` (`k + 1 < gs.length`) and shift wire indices `> k + 2` down by `1`. -/
  318. private def deleteUnused (gs : List GateSpec) (k : ℕ) : List GateSpec :=
  319. List.ofFn (fun m : Fin (gs.length - 1) =>
  320. if m.val < k then
  321. gs[m.val]'(by omega)
  322. else
  323. let g := gs[m.val + 1]'(by omega)
  324. ⟨g.op, shiftDown k g.i, shiftDown k g.j⟩)
  325. private lemma deleteUnused_length (gs : List GateSpec) (k : ℕ) :
  326. (deleteUnused gs k).length = gs.length - 1 := by
  327. simp [deleteUnused]
  328. private lemma deleteUnused_wellFormed {gs : List GateSpec} (hwf : WellFormed gs)
  329. {k : ℕ} (hdead : ∀ (k' : ℕ) (hk' : k' < gs.length), k < k' → gs[k'].i ≠ k + 2 ∧ gs[k'].j ≠ k + 2) :
  330. WellFormed (deleteUnused gs k) := by
  331. intro m hm
  332. rw [deleteUnused_length] at hm
  333. simp only [deleteUnused, List.getElem_ofFn]
  334. split_ifs with hmk
  335. · exact hwf m (by omega)
  336. · have hm1 : m + 1 < gs.length := by omega
  337. have hgm := hwf (m + 1) hm1
  338. have hd := hdead (m + 1) hm1 (by omega)
  339. exact ⟨shiftDown_lt (by omega) hgm.1 hd.1, shiftDown_lt (by omega) hgm.2 hd.2⟩
  340. private lemma deleteUnused_wireVals {gs : List GateSpec} (hwf : WellFormed gs)
  341. {k : ℕ} (hk : k + 1 < gs.length)
  342. (hdead : ∀ (k' : ℕ) (hk' : k' < gs.length), k < k' → gs[k'].i ≠ k + 2 ∧ gs[k'].j ≠ k + 2) :
  343. ∀ (w : ℕ), w < (deleteUnused gs k).length + 2 →
  344. (wireVals (deleteUnused gs k)).getD w 0 =
  345. (wireVals gs).getD (if w < k + 2 then w else w + 1) 0 := by
  346. have hwf' := deleteUnused_wellFormed hwf hdead
  347. intro w
  348. induction w using Nat.strongRecOn with
  349. | ind w ih =>
  350. intro hw
  351. rcases w with _ | _ | m
  352. · rw [if_pos (by omega), wireVals_zero, wireVals_zero]
  353. · change (wireVals (deleteUnused gs k)).getD 1 0 =
  354. (wireVals gs).getD (if 1 < k + 2 then 1 else 1 + 1) 0
  355. rw [if_pos (by omega), wireVals_one, wireVals_one]
  356. · have hm : m < (deleteUnused gs k).length := by omega
  357. have hm' : m < gs.length - 1 := by simpa [deleteUnused_length] using hm
  358. rw [wireVals_gate_wf hwf' hm]
  359. have hi_lt := (hwf' m hm).1
  360. have hj_lt := (hwf' m hm).2
  361. rw [ih _ hi_lt (by omega), ih _ hj_lt (by omega)]
  362. by_cases hmk : m < k
  363. · have hg_eq : (deleteUnused gs k)[m] = gs[m]'(by omega) := by
  364. simp [deleteUnused, hmk]
  365. have hm_lt : m < gs.length := by omega
  366. have hmi : gs[m].i < k + 2 := (hwf m hm_lt).1.trans_le (by omega)
  367. have hmj : gs[m].j < k + 2 := (hwf m hm_lt).2.trans_le (by omega)
  368. rw [hg_eq, if_pos hmi, if_pos hmj, if_pos (by omega), wireVals_gate_wf hwf hm_lt]
  369. · have hm1 : m + 1 < gs.length := by omega
  370. have hd := hdead (m + 1) hm1 (by omega)
  371. have hg_eq : (deleteUnused gs k)[m] =
  372. ⟨(gs[m + 1]'hm1).op, shiftDown k (gs[m + 1]'hm1).i, shiftDown k (gs[m + 1]'hm1).j⟩ := by
  373. simp [deleteUnused, hmk]
  374. rw [hg_eq, shiftDown_inv hd.1, shiftDown_inv hd.2, if_neg (by omega),
  375. wireVals_gate_wf hwf hm1]
  376. private lemma deleteUnused_output {gs : List GateSpec} (hwf : WellFormed gs)
  377. {k : ℕ} (hk : k + 1 < gs.length)
  378. (hdead : ∀ (k' : ℕ) (hk' : k' < gs.length), k < k' → gs[k'].i ≠ k + 2 ∧ gs[k'].j ≠ k + 2) :
  379. output (deleteUnused gs k) = output gs := by
  380. have hlen : (deleteUnused gs k).length = gs.length - 1 := deleteUnused_length gs k
  381. have h := deleteUnused_wireVals hwf hk hdead ((deleteUnused gs k).length + 1) (by omega)
  382. have hge : ¬ ((deleteUnused gs k).length + 1 < k + 2) := by omega
  383. have heq : (deleteUnused gs k).length + 1 + 1 = gs.length + 1 := by omega
  384. simpa [output, hge, heq] using h
  385. /-- Sign tracking function for redirecting wire `k + 2` to `(m, flip)` (`m < k + 2`)
  386. and deleting gate `k` (`k + 1 < gs.length`). Here `w` is the new wire index. -/
  387. private def redirSign (gs : List GateSpec) (k m : ℕ) (flip : Bool) : ℕ → Bool
  388. | 0 => false
  389. | 1 => false
  390. | idx + 2 =>
  391. if hidx : idx < k then
  392. false
  393. else if hlen : idx + 1 < gs.length then
  394. let g := gs[idx + 1]'hlen
  395. let si : Bool :=
  396. if g.i < k + 2 then false
  397. else if g.i = k + 2 then flip
  398. else if g.i - 1 < idx + 2 then redirSign gs k m flip (g.i - 1)
  399. else false
  400. match g.op with
  401. | .mul =>
  402. let sj : Bool :=
  403. if g.j < k + 2 then false
  404. else if g.j = k + 2 then flip
  405. else if g.j - 1 < idx + 2 then redirSign gs k m flip (g.j - 1)
  406. else false
  407. xor si sj
  408. | .add | .sub => si
  409. else false
  410. termination_by w => w
  411. decreasing_by all_goals omega
  412. private lemma redirSign_lt {gs : List GateSpec} {k m : ℕ} {flip : Bool}
  413. {w : ℕ} (hw : w < k + 2) : redirSign gs k m flip w = false := by
  414. rcases w with _ | _ | idx
  415. · simp [redirSign]
  416. · simp [redirSign]
  417. · unfold redirSign
  418. simp [show idx < k by omega]
  419. private def redirOperand (gs : List GateSpec) (k m : ℕ) (flip : Bool) (u : ℕ) : ℕ × Bool :=
  420. if u < k + 2 then (u, false)
  421. else if u = k + 2 then (m, flip)
  422. else (u - 1, redirSign gs k m flip (u - 1))
  423. private lemma redirOperand_idx_lt {k m idx u : ℕ} {gs : List GateSpec} {flip : Bool}
  424. (hk : k ≤ idx) (hm : m < k + 2) (hu : u < idx + 3) :
  425. (redirOperand gs k m flip u).1 < idx + 2 := by
  426. unfold redirOperand
  427. split_ifs <;> omega
  428. private def redirGate (gs : List GateSpec) (k m : ℕ) (flip : Bool) : List GateSpec :=
  429. List.ofFn (fun idx : Fin (gs.length - 1) =>
  430. if idx.val < k then
  431. gs[idx.val]'(by omega)
  432. else
  433. let g := gs[idx.val + 1]'(by omega)
  434. let p₁ := redirOperand gs k m flip g.i
  435. let p₂ := redirOperand gs k m flip g.j
  436. let op' :=
  437. match g.op with
  438. | .mul => .mul
  439. | .add | .sub => if p₁.2 = p₂.2 then g.op else flipAddSub g.op
  440. ⟨op', p₁.1, p₂.1⟩)
  441. private lemma redirGate_length (gs : List GateSpec) (k m : ℕ) (flip : Bool) :
  442. (redirGate gs k m flip).length = gs.length - 1 := by
  443. simp [redirGate]
  444. private lemma redirGate_wellFormed {gs : List GateSpec} (hwf : WellFormed gs)
  445. {k m : ℕ} (hm : m < k + 2) (flip : Bool) :
  446. WellFormed (redirGate gs k m flip) := by
  447. intro idx hidx
  448. rw [redirGate_length] at hidx
  449. by_cases hk : idx < k
  450. · simp only [redirGate, List.getElem_ofFn, hk, ↓reduceIte]
  451. exact hwf idx (by omega)
  452. · simp only [redirGate, List.getElem_ofFn, hk, ↓reduceIte]
  453. have hidx1 : idx + 1 < gs.length := by omega
  454. have hg := hwf (idx + 1) hidx1
  455. exact ⟨redirOperand_idx_lt (by omega) hm hg.1, redirOperand_idx_lt (by omega) hm hg.2⟩
  456. private lemma redirSign_succ_ge {gs : List GateSpec} (hwf : WellFormed gs)
  457. {k m idx : ℕ} (hk : k ≤ idx) (hidx1 : idx + 1 < gs.length) (flip : Bool) :
  458. let g := gs[idx + 1]'hidx1
  459. let p₁ := redirOperand gs k m flip g.i
  460. let p₂ := redirOperand gs k m flip g.j
  461. redirSign gs k m flip (idx + 2) =
  462. match g.op with
  463. | .mul => xor p₁.2 p₂.2
  464. | .add | .sub => p₁.2 := by
  465. intro g p₁ p₂
  466. have hgi : g.i - 1 < idx + 2 := by
  467. have := (hwf (idx + 1) hidx1).1
  468. dsimp only [g]
  469. omega
  470. have hgj : g.j - 1 < idx + 2 := by
  471. have := (hwf (idx + 1) hidx1).2
  472. dsimp only [g]
  473. omega
  474. conv_lhs => unfold redirSign
  475. rw [dif_neg (by omega), dif_pos hidx1]
  476. dsimp only [p₁, p₂, redirOperand, g] at hgi hgj ⊢
  477. cases (gs[idx + 1]'hidx1).op <;> simp only [hgi, hgj, ↓reduceIte] <;> split_ifs <;> rfl
  478. private lemma redirGate_wireVals {gs : List GateSpec} (hwf : WellFormed gs)
  479. {k m : ℕ} (hk : k + 1 < gs.length) (hm : m < k + 2) (flip : Bool)
  480. (hdup : (wireVals gs).getD (k + 2) 0 = sgnVal flip * (wireVals gs).getD m 0) :
  481. ∀ (w : ℕ), w < (redirGate gs k m flip).length + 2 →
  482. (wireVals (redirGate gs k m flip)).getD w 0 =
  483. sgnVal (redirSign gs k m flip w) *
  484. (wireVals gs).getD (if w < k + 2 then w else w + 1) 0 := by
  485. have hwf' := redirGate_wellFormed hwf hm flip
  486. intro w
  487. induction w using Nat.strongRecOn with
  488. | ind w ih =>
  489. intro hw
  490. rcases w with _ | _ | idx
  491. · rw [if_pos (by omega), wireVals_zero, wireVals_zero]
  492. simp [redirSign, sgnVal]
  493. · change (wireVals (redirGate gs k m flip)).getD 1 0 =
  494. sgnVal (redirSign gs k m flip 1) * (wireVals gs).getD (if 1 < k + 2 then 1 else 1 + 1) 0
  495. rw [if_pos (by omega), wireVals_one, wireVals_one]
  496. simp [redirSign, sgnVal]
  497. · have hidx_len : idx < (redirGate gs k m flip).length := by omega
  498. have hidx1 : idx + 1 < gs.length := by
  499. rw [redirGate_length] at hidx_len
  500. omega
  501. rw [wireVals_gate_wf hwf' hidx_len]
  502. by_cases hlt : idx < k
  503. · have hidx_gs : idx < gs.length := by omega
  504. have hi : gs[idx].i < idx + 2 := (hwf idx hidx_gs).1
  505. have hj : gs[idx].j < idx + 2 := (hwf idx hidx_gs).2
  506. have hgi_k : gs[idx].i < k + 2 := by omega
  507. have hgj_k : gs[idx].j < k + 2 := by omega
  508. have hg_eq : (redirGate gs k m flip)[idx] = gs[idx] := by
  509. simp [redirGate, hlt]
  510. rw [hg_eq]
  511. have ihi := ih gs[idx].i hi (by omega)
  512. have ihj := ih gs[idx].j hj (by omega)
  513. rw [if_pos hgi_k, redirSign_lt hgi_k] at ihi
  514. rw [if_pos hgj_k, redirSign_lt hgj_k] at ihj
  515. rw [ihi, ihj, if_pos (show idx + 2 < k + 2 by omega),
  516. redirSign_lt (show idx + 2 < k + 2 by omega), wireVals_gate_wf hwf hidx_gs]
  517. simp [sgnVal]
  518. · have hk_le : k ≤ idx := by omega
  519. have hgi : gs[idx + 1].i < idx + 3 := (hwf (idx + 1) hidx1).1
  520. have hgj : gs[idx + 1].j < idx + 3 := (hwf (idx + 1) hidx1).2
  521. have hg_eq : (redirGate gs k m flip)[idx] =
  522. let g := gs[idx + 1]
  523. let p₁ := redirOperand gs k m flip g.i
  524. let p₂ := redirOperand gs k m flip g.j
  525. let op' :=
  526. match g.op with
  527. | .mul => .mul
  528. | .add | .sub => if p₁.2 = p₂.2 then g.op else flipAddSub g.op
  529. ⟨op', p₁.1, p₂.1⟩ := by
  530. simp [redirGate, hlt]
  531. have h_op : ∀ (u : ℕ), u < idx + 3 →
  532. (wireVals gs).getD u 0 =
  533. sgnVal (redirOperand gs k m flip u).2 *
  534. (wireVals (redirGate gs k m flip)).getD (redirOperand gs k m flip u).1 0 := by
  535. intro u hu
  536. unfold redirOperand
  537. split_ifs with hu1 hu2
  538. · have ihu := ih u (by omega) (by omega)
  539. rw [if_pos hu1, redirSign_lt hu1] at ihu
  540. rw [ihu]
  541. simp [sgnVal]
  542. · subst hu2
  543. have ihm := ih m (by omega) (by omega)
  544. rw [if_pos hm, redirSign_lt hm] at ihm
  545. simp only [sgnVal, Bool.false_eq_true, ↓reduceIte, one_mul] at ihm
  546. rw [hdup, ihm]
  547. · have ihu := ih (u - 1) (by omega) (by omega)
  548. have hge : ¬ (u - 1 < k + 2) := by omega
  549. have hu_eq : u - 1 + 1 = u := by omega
  550. rw [if_neg hge, hu_eq] at ihu
  551. rw [ihu, ← mul_assoc, sgnVal_mul_self, one_mul]
  552. have h1 := h_op gs[idx + 1].i hgi
  553. have h2 := h_op gs[idx + 1].j hgj
  554. -- Since sgnVal s ∈ {1, -1}, multiply both sides by sgnVal s
  555. have h1' : (wireVals (redirGate gs k m flip)).getD (redirOperand gs k m flip gs[idx + 1].i).1 0 =
  556. sgnVal (redirOperand gs k m flip gs[idx + 1].i).2 * (wireVals gs).getD gs[idx + 1].i 0 := by
  557. rw [h1, ← mul_assoc, sgnVal_mul_self, one_mul]
  558. have h2' : (wireVals (redirGate gs k m flip)).getD (redirOperand gs k m flip gs[idx + 1].j).1 0 =
  559. sgnVal (redirOperand gs k m flip gs[idx + 1].j).2 * (wireVals gs).getD gs[idx + 1].j 0 := by
  560. rw [h2, ← mul_assoc, sgnVal_mul_self, one_mul]
  561. --Wait: express old = sgn * new, then compute new = sgn * old
  562. rw [hg_eq, if_neg (by omega)]
  563. have hs_eq := redirSign_succ_ge (m := m) hwf hk_le hidx1 flip
  564. dsimp only at hs_eq ⊢
  565. rw [hs_eq, wireVals_gate_wf hwf hidx1, h1, h2]
  566. -- Now simplify the goal: both sides are op' (new1) (new2) vs sgnVal (new_s) * op (sgn1 * new1) (sgn2 * new2)
  567. set a := (wireVals (redirGate gs k m flip)).getD (redirOperand gs k m flip gs[idx + 1].i).1 0
  568. set b := (wireVals (redirGate gs k m flip)).getD (redirOperand gs k m flip gs[idx + 1].j).1 0
  569. set s₁ := (redirOperand gs k m flip gs[idx + 1].i).2
  570. set s₂ := (redirOperand gs k m flip gs[idx + 1].j).2
  571. cases hop : gs[idx + 1].op <;> cases s₁ <;> cases s₂ <;>
  572. simp [sgnVal, flipAddSub, SLPOp.applyPoly] <;> ring
  573. private lemma redirGate_computesSigned {f : Polynomial ℤ} {gs : List GateSpec}
  574. (hwf : WellFormed gs) (hcomp : ComputesSigned f gs)
  575. {k m : ℕ} (hk : k + 1 < gs.length) (hm : m < k + 2) (flip : Bool)
  576. (hdup : (wireVals gs).getD (k + 2) 0 = sgnVal flip * (wireVals gs).getD m 0) :
  577. ComputesSigned f (redirGate gs k m flip) := by
  578. have hlen := redirGate_length gs k m flip
  579. have h := redirGate_wireVals hwf hk hm flip hdup ((redirGate gs k m flip).length + 1) (by omega)
  580. have hge : ¬ ((redirGate gs k m flip).length + 1 < k + 2) := by omega
  581. have heq : (redirGate gs k m flip).length + 1 + 1 = gs.length + 1 := by omega
  582. rw [if_neg hge, heq] at h
  583. change output (redirGate gs k m flip) =
  584. sgnVal (redirSign gs k m flip ((redirGate gs k m flip).length + 1)) * output gs at h
  585. rcases hcomp with hcomp | hcomp <;>
  586. cases hs : redirSign gs k m flip ((redirGate gs k m flip).length + 1) <;>
  587. simp [ComputesSigned, h, hs, sgnVal, hcomp]
  588. /-- Sign tracking function for `ordNormalize`. `normSign gs w` is `true` iff
  589. normalized wire `w` holds `- (wireVals gs)[w]`. -/
  590. private def normSign (gs : List GateSpec) : ℕ → Bool
  591. | 0 => false
  592. | 1 => false
  593. | k + 2 =>
  594. if hk : k < gs.length then
  595. let g := gs[k]'hk
  596. let s₁ := if g.i < k + 2 then normSign gs g.i else false
  597. let s₂ := if g.j < k + 2 then normSign gs g.j else false
  598. match g.op with
  599. | .mul => xor s₁ s₂
  600. | .add | .sub =>
  601. let op₁ := if s₁ = s₂ then g.op else flipAddSub g.op
  602. if op₁ = .sub ∧ g.j < g.i then !s₁ else s₁
  603. else false
  604. termination_by w => w
  605. decreasing_by all_goals omega
  606. private def normGate (g : GateSpec) (s₁ s₂ : Bool) : GateSpec :=
  607. match g.op with
  608. | .mul => ⟨.mul, min g.i g.j, max g.i g.j⟩
  609. | .add | .sub =>
  610. let op₁ := if s₁ = s₂ then g.op else flipAddSub g.op
  611. match op₁ with
  612. | .add => ⟨.add, min g.i g.j, max g.i g.j⟩
  613. | .sub => if g.i ≤ g.j then ⟨.sub, g.i, g.j⟩ else ⟨.sub, g.j, g.i⟩
  614. | .mul => ⟨.mul, min g.i g.j, max g.i g.j⟩
  615. private def ordNormalize (gs : List GateSpec) : List GateSpec :=
  616. List.ofFn (fun k : Fin gs.length =>
  617. let g := gs[k.val]
  618. normGate g (normSign gs g.i) (normSign gs g.j))
  619. private lemma ordNormalize_length (gs : List GateSpec) :
  620. (ordNormalize gs).length = gs.length := by
  621. simp [ordNormalize]
  622. private lemma normGate_operands (g : GateSpec) (s₁ s₂ : Bool) :
  623. ((normGate g s₁ s₂).i = g.i ∧ (normGate g s₁ s₂).j = g.j) ∨
  624. ((normGate g s₁ s₂).i = g.j ∧ (normGate g s₁ s₂).j = g.i) := by
  625. have hminmax : (min g.i g.j = g.i ∧ max g.i g.j = g.j) ∨
  626. (min g.i g.j = g.j ∧ max g.i g.j = g.i) := by
  627. by_cases h : g.i ≤ g.j
  628. · exact Or.inl ⟨min_eq_left h, max_eq_right h⟩
  629. · exact Or.inr ⟨min_eq_right (Nat.le_of_not_ge h), max_eq_left (Nat.le_of_not_ge h)⟩
  630. cases hop : g.op <;> cases s₁ <;> cases s₂ <;>
  631. simp only [normGate, hop, flipAddSub, Bool.false_eq_true, Bool.true_eq_false, ↓reduceIte]
  632. all_goals first
  633. | exact hminmax
  634. | (by_cases hij : g.i ≤ g.j
  635. · simp [hij]
  636. · simp [hij])
  637. private lemma ordNormalize_wellFormed {gs : List GateSpec} (hwf : WellFormed gs) :
  638. WellFormed (ordNormalize gs) := by
  639. intro k hk
  640. rw [ordNormalize_length] at hk
  641. have hg := hwf k hk
  642. simp only [ordNormalize, List.getElem_ofFn]
  643. rcases normGate_operands gs[k] (normSign gs gs[k].i) (normSign gs gs[k].j) with ⟨hi, hj⟩ | ⟨hi, hj⟩
  644. · rw [hi, hj]; exact hg
  645. · rw [hi, hj]; exact ⟨hg.2, hg.1⟩
  646. private lemma normGate_ord {g : GateSpec} {s₁ s₂ : Bool}
  647. (hne : ¬ ((if s₁ = s₂ then g.op else flipAddSub g.op) = .sub ∧ g.i = g.j)) :
  648. (normGate g s₁ s₂).Ord := by
  649. have hmm : min g.i g.j ≤ max g.i g.j := le_trans (min_le_left _ _) (le_max_left _ _)
  650. have hne' : (if s₁ = s₂ then g.op else flipAddSub g.op) = .sub → g.i ≠ g.j :=
  651. fun h1 h2 => hne ⟨h1, h2⟩
  652. cases hop : g.op <;> cases s₁ <;> cases s₂ <;>
  653. simp only [normGate, hop, flipAddSub, Bool.false_eq_true, Bool.true_eq_false, ↓reduceIte] at hne' ⊢
  654. all_goals first
  655. | exact hmm
  656. | (by_cases hij : g.i ≤ g.j
  657. · simp only [hij, ↓reduceIte, GateSpec.Ord]
  658. have : g.i ≠ g.j := hne' trivial
  659. omega
  660. · simp only [hij, ↓reduceIte, GateSpec.Ord]
  661. omega)
  662. private lemma normGate_wire_eq {g : GateSpec} {s₁ s₂ : Bool}
  663. (hneq : ¬ ((if s₁ = s₂ then g.op else flipAddSub g.op) = .sub ∧ g.i = g.j))
  664. (new_i new_j : Polynomial ℤ) (heq : g.i = g.j → new_i = new_j) :
  665. let out_s :=
  666. match g.op with
  667. | .mul => xor s₁ s₂
  668. | .add | .sub =>
  669. let op₁ := if s₁ = s₂ then g.op else flipAddSub g.op
  670. if op₁ = .sub ∧ g.j < g.i then !s₁ else s₁
  671. let g' := normGate g s₁ s₂
  672. g'.op.applyPoly (if g'.i = g.i then new_i else new_j) (if g'.j = g.i then new_i else new_j) =
  673. sgnVal out_s * g.op.applyPoly (sgnVal s₁ * new_i) (sgnVal s₂ * new_j) := by
  674. by_cases hij_eq : g.i = g.j
  675. · have hnew : new_i = new_j := heq hij_eq
  676. subst hnew
  677. cases hop : g.op <;> cases s₁ <;> cases s₂ <;>
  678. simp [normGate, flipAddSub, SLPOp.applyPoly, sgnVal, hop, hij_eq] at hneq ⊢ <;> ring
  679. · by_cases hij : g.i ≤ g.j
  680. · have hlt : g.i < g.j := by omega
  681. have hnot_ji : ¬ (g.j < g.i) := by omega
  682. have hne_ji : g.j ≠ g.i := by omega
  683. cases hop : g.op <;> cases s₁ <;> cases s₂ <;>
  684. simp [normGate, flipAddSub, SLPOp.applyPoly, sgnVal, hop, hij, hnot_ji, hne_ji,
  685. min_eq_left hij, max_eq_right hij] <;> ring
  686. · have hlt : g.j < g.i := by omega
  687. have hji : g.j ≤ g.i := by omega
  688. have hne_ji : g.j ≠ g.i := by omega
  689. cases hop : g.op <;> cases s₁ <;> cases s₂ <;>
  690. simp [normGate, flipAddSub, SLPOp.applyPoly, sgnVal, hop, hij, hlt, hne_ji,
  691. min_eq_right hji, max_eq_left hji] <;> ring
  692. private lemma ordNormalize_wireVals_and_ord {gs : List GateSpec}
  693. (hwf : WellFormed gs) (hnz : NonzeroWires gs) :
  694. (∀ (w : ℕ), w < gs.length + 2 →
  695. (wireVals (ordNormalize gs)).getD w 0 = sgnVal (normSign gs w) * (wireVals gs).getD w 0) ∧
  696. OrdCanonical (ordNormalize gs) := by
  697. -- Prove simultaneously by induction on w (and k for OrdCanonical)
  698. have hmain : ∀ (w : ℕ), w < gs.length + 2 →
  699. (wireVals (ordNormalize gs)).getD w 0 = sgnVal (normSign gs w) * (wireVals gs).getD w 0 := by
  700. intro w
  701. induction w using Nat.strongRecOn with
  702. | ind w ih =>
  703. intro hw
  704. rcases w with _ | _ | k
  705. · rw [wireVals_zero, wireVals_zero]; simp [normSign, sgnVal]
  706. · change (wireVals (ordNormalize gs)).getD 1 0 =
  707. sgnVal (normSign gs 1) * (wireVals gs).getD 1 0
  708. rw [wireVals_one, wireVals_one]; simp [normSign, sgnVal]
  709. · have hk : k < gs.length := by omega
  710. have hk' : k < (ordNormalize gs).length := by rwa [ordNormalize_length]
  711. have hwf' := ordNormalize_wellFormed hwf
  712. have hgi : gs[k].i < k + 2 := (hwf k hk).1
  713. have hgj : gs[k].j < k + 2 := (hwf k hk).2
  714. have h_old := wireVals_gate_wf hwf hk
  715. have h_new := wireVals_gate_wf hwf' hk'
  716. have h_neq : ¬ ((if normSign gs gs[k].i = normSign gs gs[k].j then gs[k].op
  717. else flipAddSub gs[k].op) = .sub ∧ gs[k].i = gs[k].j) := by
  718. intro ⟨hop_sub, hij⟩
  719. rw [hij, if_pos rfl] at hop_sub
  720. have h0 : (wireVals gs).getD (k + 2) 0 = 0 := by
  721. rw [h_old, hij, hop_sub]
  722. simp [SLPOp.applyPoly]
  723. exact hnz k hk h0
  724. have ihi_old := ih gs[k].i hgi (by omega)
  725. have ihj_old := ih gs[k].j hgj (by omega)
  726. have h_ng : (ordNormalize gs)[k] =
  727. normGate gs[k] (normSign gs gs[k].i) (normSign gs gs[k].j) := by
  728. simp [ordNormalize]
  729. have h_normSign_k : normSign gs (k + 2) =
  730. match gs[k].op with
  731. | .mul => xor (normSign gs gs[k].i) (normSign gs gs[k].j)
  732. | .add | .sub =>
  733. let op₁ := if normSign gs gs[k].i = normSign gs gs[k].j then gs[k].op
  734. else flipAddSub gs[k].op
  735. if op₁ = .sub ∧ gs[k].j < gs[k].i then !(normSign gs gs[k].i)
  736. else normSign gs gs[k].i := by
  737. conv_lhs => unfold normSign
  738. simp [hk, hgi, hgj]
  739. set g := gs[k]
  740. set s₁ := normSign gs g.i
  741. set s₂ := normSign gs g.j
  742. set g' := normGate g s₁ s₂
  743. have h_mem_i : g'.i = g.i ∨ g'.i = g.j := by
  744. rcases normGate_operands g s₁ s₂ with ⟨h1, _⟩ | ⟨h1, _⟩
  745. · exact Or.inl h1
  746. · exact Or.inr h1
  747. have h_mem_j : g'.j = g.i ∨ g'.j = g.j := by
  748. rcases normGate_operands g s₁ s₂ with ⟨_, h2⟩ | ⟨_, h2⟩
  749. · exact Or.inr h2
  750. · exact Or.inl h2
  751. have h_new_i_old : (wireVals (ordNormalize gs)).getD g'.i 0 =
  752. if g'.i = g.i then (wireVals (ordNormalize gs)).getD g.i 0
  753. else (wireVals (ordNormalize gs)).getD g.j 0 := by
  754. rcases h_mem_i with h | h <;> split_ifs with h1 <;> simp_all
  755. have h_new_j_old : (wireVals (ordNormalize gs)).getD g'.j 0 =
  756. if g'.j = g.i then (wireVals (ordNormalize gs)).getD g.i 0
  757. else (wireVals (ordNormalize gs)).getD g.j 0 := by
  758. rcases h_mem_j with h | h <;> split_ifs with h1 <;> simp_all
  759. rw [h_new, h_ng]
  760. change g'.op.applyPoly ((wireVals (ordNormalize gs)).getD g'.i 0)
  761. ((wireVals (ordNormalize gs)).getD g'.j 0) =
  762. sgnVal (normSign gs (k + 2)) * (wireVals gs).getD (k + 2) 0
  763. rw [h_new_i_old, h_new_j_old]
  764. have hlem := normGate_wire_eq h_neq
  765. ((wireVals (ordNormalize gs)).getD g.i 0)
  766. ((wireVals (ordNormalize gs)).getD g.j 0)
  767. (fun hij => by rw [hij])
  768. dsimp only at hlem
  769. rw [hlem, ← h_normSign_k, ihi_old, ihj_old, h_old]
  770. have h_old_i : sgnVal s₁ * (sgnVal s₁ * (wireVals gs).getD g.i 0) =
  771. (wireVals gs).getD g.i 0 := by
  772. rw [← mul_assoc, sgnVal_mul_self, one_mul]
  773. have h_old_j : sgnVal s₂ * (sgnVal s₂ * (wireVals gs).getD g.j 0) =
  774. (wireVals gs).getD g.j 0 := by
  775. rw [← mul_assoc, sgnVal_mul_self, one_mul]
  776. change sgnVal (normSign gs (k + 2)) *
  777. g.op.applyPoly (sgnVal s₁ * (sgnVal s₁ * (wireVals gs).getD g.i 0))
  778. (sgnVal s₂ * (sgnVal s₂ * (wireVals gs).getD g.j 0)) =
  779. sgnVal (normSign gs (k + 2)) * g.op.applyPoly ((wireVals gs).getD g.i 0) ((wireVals gs).getD g.j 0)
  780. rw [h_old_i, h_old_j]
  781. refine ⟨hmain, ?_⟩
  782. intro k hk
  783. rw [ordNormalize_length] at hk
  784. simp only [ordNormalize, List.getElem_ofFn]
  785. apply normGate_ord
  786. intro ⟨hop_sub, hij⟩
  787. rw [hij, if_pos rfl] at hop_sub
  788. have h0 : (wireVals gs).getD (k + 2) 0 = 0 := by
  789. rw [wireVals_gate_wf hwf hk, hij, hop_sub]
  790. simp [SLPOp.applyPoly]
  791. exact hnz k hk h0
  792. private lemma ordNormalize_preserves_nonzero {gs : List GateSpec}
  793. (hwf : WellFormed gs) (hnz : NonzeroWires gs) :
  794. NonzeroWires (ordNormalize gs) := by
  795. intro k hk
  796. rw [ordNormalize_length] at hk
  797. rw [(ordNormalize_wireVals_and_ord hwf hnz).1 (k + 2) (by omega)]
  798. exact mul_ne_zero (sgnVal_ne_zero _) (hnz k hk)
  799. private lemma ordNormalize_preserves_nodup {gs : List GateSpec}
  800. (hwf : WellFormed gs) (hnz : NonzeroWires gs) (hnodup : NodupWires gs) :
  801. NodupWires (ordNormalize gs) := by
  802. intro k hk m hm
  803. rw [ordNormalize_length] at hk
  804. have hvals := (ordNormalize_wireVals_and_ord hwf hnz).1
  805. rw [hvals (k + 2) (by omega), hvals m (by omega)]
  806. have hnd := hnodup k hk m hm
  807. cases hs1 : normSign gs (k + 2) <;> cases hs2 : normSign gs m <;> simp [sgnVal]
  808. · exact hnd
  809. · exact ⟨hnd.2, hnd.1⟩
  810. · exact ⟨fun h => hnd.2 (neg_eq_iff_eq_neg.mp h), fun h => hnd.1 h⟩
  811. · exact ⟨fun h => hnd.1 h, fun h => hnd.2 (neg_eq_iff_eq_neg.mp h)⟩
  812. private lemma ordNormalize_preserves_used {gs : List GateSpec}
  813. (hused : UsedGates gs) : UsedGates (ordNormalize gs) := by
  814. intro k hk
  815. rw [ordNormalize_length] at hk
  816. rcases hused k hk with ⟨k', hk', hkk', hor⟩
  817. refine ⟨k', by rwa [ordNormalize_length], hkk', ?_⟩
  818. simp only [ordNormalize, List.getElem_ofFn]
  819. rcases normGate_operands gs[k'] (normSign gs gs[k'].i) (normSign gs gs[k'].j) with
  820. ⟨hi, hj⟩ | ⟨hi, hj⟩
  821. · rw [hi, hj]; exact hor
  822. · rw [hi, hj]; exact hor.symm
  823. private lemma ordNormalize_computesSigned {f : Polynomial ℤ} {gs : List GateSpec}
  824. (hwf : WellFormed gs) (hnz : NonzeroWires gs) (hcomp : ComputesSigned f gs) :
  825. ComputesSigned f (ordNormalize gs) := by
  826. have h := (ordNormalize_wireVals_and_ord hwf hnz).1 (gs.length + 1) (by omega)
  827. have hlen := ordNormalize_length gs
  828. have hout : output (ordNormalize gs) = sgnVal (normSign gs (gs.length + 1)) * output gs := by
  829. simpa [output, hlen] using h
  830. rcases hcomp with hcomp | hcomp <;>
  831. cases hs : normSign gs (gs.length + 1) <;>
  832. simp [ComputesSigned, hout, hs, sgnVal, hcomp]
  833. private lemma normSign_false_of_ord_prefix {gs : List GateSpec} (hwf : WellFormed gs)
  834. {p : ℕ} (hp : p ≤ gs.length)
  835. (hord : ∀ (m : ℕ) (hm : m < p), (gs[m]'(by omega)).Ord) :
  836. ∀ (w : ℕ), w < p + 2 → normSign gs w = false := by
  837. intro w
  838. induction w using Nat.strongRecOn with
  839. | ind w ih =>
  840. intro hw
  841. rcases w with _ | _ | m
  842. · simp [normSign]
  843. · simp [normSign]
  844. · have hm : m < p := by omega
  845. have hm_gs : m < gs.length := by omega
  846. have hgi : gs[m].i < m + 2 := (hwf m hm_gs).1
  847. have hgj : gs[m].j < m + 2 := (hwf m hm_gs).2
  848. have si_f : normSign gs gs[m].i = false := ih gs[m].i hgi (by omega)
  849. have sj_f : normSign gs gs[m].j = false := ih gs[m].j hgj (by omega)
  850. have hm_ord := hord m hm
  851. conv_lhs => unfold normSign
  852. simp only [hm_gs, ↓reduceDIte, hgi, hgj, si_f, sj_f]
  853. cases hop : gs[m].op <;> simp [GateSpec.Ord, hop] at hm_ord ⊢
  854. omega
  855. private lemma ordNormalize_prefix_eq {gs : List GateSpec} (hwf : WellFormed gs)
  856. {p : ℕ} (hp : p ≤ gs.length)
  857. (hord : ∀ (m : ℕ) (hm : m < p), (gs[m]'(by omega)).Ord) :
  858. ∀ (m : ℕ) (hm : m < p), (ordNormalize gs)[m]'(by rw [ordNormalize_length]; omega) = gs[m]'(by omega) := by
  859. intro m hm
  860. have hm_gs : m < gs.length := by omega
  861. have hgi : gs[m].i < m + 2 := (hwf m hm_gs).1
  862. have hgj : gs[m].j < m + 2 := (hwf m hm_gs).2
  863. have si_f : normSign gs gs[m].i = false := normSign_false_of_ord_prefix hwf hp hord gs[m].i (by omega)
  864. have sj_f : normSign gs gs[m].j = false := normSign_false_of_ord_prefix hwf hp hord gs[m].j (by omega)
  865. have hm_ord := hord m hm
  866. simp only [ordNormalize, List.getElem_ofFn, si_f, sj_f]
  867. rcases hg : gs[m] with ⟨op, i, j⟩
  868. rw [hg] at hm_ord
  869. cases op <;> simp only [normGate, ↓reduceIte, GateSpec.Ord] at hm_ord ⊢
  870. · simp [min_eq_left hm_ord, max_eq_right hm_ord]
  871. · simp [hm_ord.le]
  872. · simp [min_eq_left hm_ord, max_eq_right hm_ord]
  873. /-- Swap wires `a` and `b`, fixing all other indices. -/
  874. private def swapIdx (a b x : ℕ) : ℕ :=
  875. if x = a then b else if x = b then a else x
  876. private lemma swapIdx_invol (a b x : ℕ) : swapIdx a b (swapIdx a b x) = x := by
  877. unfold swapIdx
  878. split_ifs <;> omega
  879. private lemma swapIdx_lt {a b m x : ℕ} (ha : a < m) (hb : b < m) (hx : x < m) :
  880. swapIdx a b x < m := by
  881. unfold swapIdx
  882. split_ifs <;> omega
  883. private lemma swapIdx_lt_of_lt {a b x : ℕ} (hx1 : x < a) (hx2 : x < b) :
  884. swapIdx a b x = x := by
  885. unfold swapIdx
  886. split_ifs <;> omega
  887. /-- Swap independent adjacent gates `k - 1` and `k` (`1 ≤ k`, `k + 1 < gs.length`)
  888. and swap wire references `k + 1 ↔ k + 2` in all gates `> k`. -/
  889. private def swapRaw (gs : List GateSpec) (k : ℕ) : List GateSpec :=
  890. List.ofFn (fun m : Fin gs.length =>
  891. if m.val = k - 1 then
  892. gs[k]!
  893. else if m.val = k then
  894. gs[k - 1]!
  895. else if m.val < k - 1 then
  896. gs[m.val]
  897. else
  898. let g := gs[m.val]
  899. ⟨g.op, swapIdx (k + 1) (k + 2) g.i, swapIdx (k + 1) (k + 2) g.j⟩)
  900. private lemma swapRaw_length (gs : List GateSpec) (k : ℕ) :
  901. (swapRaw gs k).length = gs.length := by
  902. simp [swapRaw]
  903. private lemma swapRaw_wellFormed {gs : List GateSpec} (hwf : WellFormed gs)
  904. {k : ℕ} (hk1 : 1 ≤ k) (hk2 : k + 1 < gs.length)
  905. (hind_i : gs[k].i ≠ k + 1) (hind_j : gs[k].j ≠ k + 1) :
  906. WellFormed (swapRaw gs k) := by
  907. intro m hm
  908. rw [swapRaw_length] at hm
  909. simp only [swapRaw, List.getElem_ofFn]
  910. split_ifs with h1 h2 h3
  911. · have hgk := hwf k (by omega)
  912. have heq : gs[k]! = gs[k] := by simp [show k < gs.length by omega]
  913. rw [heq]
  914. omega
  915. · have hgk1 := hwf (k - 1) (by omega)
  916. have heq : gs[k - 1]! = gs[k - 1] := by simp [show k - 1 < gs.length by omega]
  917. rw [heq]
  918. omega
  919. · exact hwf m hm
  920. · have hgm := hwf m hm
  921. have hk_lt : k + 1 < m + 2 := by omega
  922. have hk2_lt : k + 2 < m + 2 := by omega
  923. exact ⟨swapIdx_lt hk_lt hk2_lt hgm.1, swapIdx_lt hk_lt hk2_lt hgm.2⟩
  924. private lemma swapRaw_wireVals {gs : List GateSpec} (hwf : WellFormed gs)
  925. {k : ℕ} (hk1 : 1 ≤ k) (hk2 : k + 1 < gs.length)
  926. (hind_i : gs[k].i ≠ k + 1) (hind_j : gs[k].j ≠ k + 1) :
  927. ∀ (w : ℕ), w < gs.length + 2 →
  928. (wireVals (swapRaw gs k)).getD w 0 = (wireVals gs).getD (swapIdx (k + 1) (k + 2) w) 0 := by
  929. have hwf' := swapRaw_wellFormed hwf hk1 hk2 hind_i hind_j
  930. intro w
  931. induction w using Nat.strongRecOn with
  932. | ind w ih =>
  933. intro hw
  934. rcases w with _ | _ | m
  935. · have h0 : swapIdx (k + 1) (k + 2) 0 = 0 := swapIdx_lt_of_lt (by omega) (by omega)
  936. rw [h0, wireVals_zero, wireVals_zero]
  937. · have h1 : swapIdx (k + 1) (k + 2) 1 = 1 := swapIdx_lt_of_lt (by omega) (by omega)
  938. change (wireVals (swapRaw gs k)).getD 1 0 = (wireVals gs).getD (swapIdx (k + 1) (k + 2) 1) 0
  939. rw [h1, wireVals_one, wireVals_one]
  940. · have hm : m < gs.length := by omega
  941. have hm' : m < (swapRaw gs k).length := by rwa [swapRaw_length]
  942. rw [wireVals_gate_wf hwf' hm']
  943. have hi' := (hwf' m hm').1
  944. have hj' := (hwf' m hm').2
  945. rw [ih _ hi' (by omega), ih _ hj' (by omega)]
  946. simp only [swapRaw, List.getElem_ofFn]
  947. split_ifs with h1 h2 h3
  948. · have heq : gs[k]! = gs[k] := by simp [show k < gs.length by omega]
  949. rw [heq]
  950. have hgk := hwf k (by omega)
  951. have hsi : swapIdx (k + 1) (k + 2) gs[k].i = gs[k].i :=
  952. swapIdx_lt_of_lt (by omega) (by omega)
  953. have hsj : swapIdx (k + 1) (k + 2) gs[k].j = gs[k].j :=
  954. swapIdx_lt_of_lt (by omega) (by omega)
  955. have hsw : swapIdx (k + 1) (k + 2) (m + 2) = k + 2 := by
  956. unfold swapIdx; split_ifs <;> omega
  957. rw [hsi, hsj, hsw, ← wireVals_gate_wf hwf (show k < gs.length by omega)]
  958. · have heq : gs[k - 1]! = gs[k - 1] := by simp [show k - 1 < gs.length by omega]
  959. rw [heq]
  960. have hgk1 := hwf (k - 1) (by omega)
  961. have hsi : swapIdx (k + 1) (k + 2) gs[k - 1].i = gs[k - 1].i :=
  962. swapIdx_lt_of_lt (by omega) (by omega)
  963. have hsj : swapIdx (k + 1) (k + 2) gs[k - 1].j = gs[k - 1].j :=
  964. swapIdx_lt_of_lt (by omega) (by omega)
  965. have hsw : swapIdx (k + 1) (k + 2) (m + 2) = k + 1 := by
  966. unfold swapIdx; split_ifs <;> omega
  967. have hk1_eq : (k - 1) + 2 = k + 1 := by omega
  968. rw [hsi, hsj, hsw, ← hk1_eq, ← wireVals_gate_wf hwf (show k - 1 < gs.length by omega)]
  969. · have hgm := hwf m hm
  970. have hsi : swapIdx (k + 1) (k + 2) gs[m].i = gs[m].i :=
  971. swapIdx_lt_of_lt (by omega) (by omega)
  972. have hsj : swapIdx (k + 1) (k + 2) gs[m].j = gs[m].j :=
  973. swapIdx_lt_of_lt (by omega) (by omega)
  974. have hsw : swapIdx (k + 1) (k + 2) (m + 2) = m + 2 :=
  975. swapIdx_lt_of_lt (by omega) (by omega)
  976. rw [hsi, hsj, hsw, ← wireVals_gate_wf hwf hm]
  977. · dsimp only
  978. rw [swapIdx_invol, swapIdx_invol]
  979. have hsw : swapIdx (k + 1) (k + 2) (m + 2) = m + 2 := by
  980. unfold swapIdx; split_ifs <;> omega
  981. rw [hsw, ← wireVals_gate_wf hwf hm]
  982. private lemma swapRaw_preserves_nonzero {gs : List GateSpec} (hwf : WellFormed gs)
  983. (hnz : NonzeroWires gs) {k : ℕ} (hk1 : 1 ≤ k) (hk2 : k + 1 < gs.length)
  984. (hind_i : gs[k].i ≠ k + 1) (hind_j : gs[k].j ≠ k + 1) :
  985. NonzeroWires (swapRaw gs k) := by
  986. intro m hm
  987. rw [swapRaw_length] at hm
  988. rw [swapRaw_wireVals hwf hk1 hk2 hind_i hind_j (m + 2) (by omega)]
  989. have hlt : swapIdx (k + 1) (k + 2) (m + 2) < gs.length + 2 :=
  990. swapIdx_lt (by omega) (by omega) (by omega)
  991. have hge2 : 2 ≤ swapIdx (k + 1) (k + 2) (m + 2) := by
  992. unfold swapIdx; split_ifs <;> omega
  993. have heq : swapIdx (k + 1) (k + 2) (m + 2) = (swapIdx (k + 1) (k + 2) (m + 2) - 2) + 2 := by omega
  994. rw [heq]
  995. exact hnz _ (by omega)
  996. private lemma swapRaw_preserves_nodup {gs : List GateSpec} (hwf : WellFormed gs)
  997. (hnodup : NodupWires gs) {k : ℕ} (hk1 : 1 ≤ k) (hk2 : k + 1 < gs.length)
  998. (hind_i : gs[k].i ≠ k + 1) (hind_j : gs[k].j ≠ k + 1) :
  999. NodupWires (swapRaw gs k) := by
  1000. intro m hm u hu
  1001. rw [swapRaw_length] at hm
  1002. have hw := swapRaw_wireVals hwf hk1 hk2 hind_i hind_j
  1003. rw [hw (m + 2) (by omega), hw u (by omega)]
  1004. by_cases hmk : m = k
  1005. · -- new wire k+2 holds old wire k+1; u < k+2 => either u < k+1 or u = k+1 (holds old k+2)
  1006. subst m
  1007. have hsw1 : swapIdx (k + 1) (k + 2) (k + 2) = k + 1 := by unfold swapIdx; split_ifs <;> omega
  1008. rw [hsw1]
  1009. by_cases huk : u = k + 1
  1010. · subst u
  1011. have hsw2 : swapIdx (k + 1) (k + 2) (k + 1) = k + 2 := by unfold swapIdx; split_ifs <;> omega
  1012. rw [hsw2]
  1013. have hnd := hnodup k (by omega) (k + 1) (by omega)
  1014. constructor <;> intro h
  1015. · exact hnd.1 h.symm
  1016. · exact hnd.2 (neg_eq_iff_eq_neg.mp h.symm)
  1017. · have hu_lt : u < k + 1 := by omega
  1018. have hsw2 : swapIdx (k + 1) (k + 2) u = u := swapIdx_lt_of_lt hu_lt (by omega)
  1019. rw [hsw2]
  1020. have hk1_eq : (k - 1) + 2 = k + 1 := by omega
  1021. rw [← hk1_eq]
  1022. exact hnodup (k - 1) (by omega) u (by omega)
  1023. · by_cases hmk1 : m = k - 1
  1024. · -- new wire k+1 holds old wire k+2; u < k+1 => swapIdx u = u < k+2
  1025. have hsw1 : swapIdx (k + 1) (k + 2) (m + 2) = k + 2 := by
  1026. unfold swapIdx; split_ifs <;> omega
  1027. have hsw2 : swapIdx (k + 1) (k + 2) u = u := swapIdx_lt_of_lt (by omega) (by omega)
  1028. rw [hsw1, hsw2]
  1029. exact hnodup k (by omega) u (by omega)
  1030. · by_cases hlt : m < k - 1
  1031. · have hsw1 : swapIdx (k + 1) (k + 2) (m + 2) = m + 2 :=
  1032. swapIdx_lt_of_lt (by omega) (by omega)
  1033. have hsw2 : swapIdx (k + 1) (k + 2) u = u :=
  1034. swapIdx_lt_of_lt (by omega) (by omega)
  1035. rw [hsw1, hsw2]
  1036. exact hnodup m hm u hu
  1037. · -- m > k, so m + 2 >= k + 3
  1038. have hm_gt : k < m := by omega
  1039. have hsw1 : swapIdx (k + 1) (k + 2) (m + 2) = m + 2 := by
  1040. unfold swapIdx; split_ifs <;> omega
  1041. have hu_lt : swapIdx (k + 1) (k + 2) u < m + 2 :=
  1042. swapIdx_lt (by omega) (by omega) hu
  1043. rw [hsw1]
  1044. exact hnodup m hm _ hu_lt
  1045. private lemma swapRaw_preserves_used {gs : List GateSpec} (hused : UsedGates gs)
  1046. {k : ℕ} (hk1 : 1 ≤ k) (hk2 : k + 1 < gs.length)
  1047. (hind_i : gs[k].i ≠ k + 1) (hind_j : gs[k].j ≠ k + 1) :
  1048. UsedGates (swapRaw gs k) := by
  1049. intro u hu
  1050. rw [swapRaw_length] at hu
  1051. have hlen : (swapRaw gs k).length = gs.length := swapRaw_length gs k
  1052. by_cases huk : u = k
  1053. · -- new wire k + 2 corresponds to old gate k - 1 (wire k + 1).
  1054. -- Its user in gs cannot be gate k (by hind_i, hind_j), so it must be some m > k.
  1055. subst u
  1056. rcases hused (k - 1) (by omega) with ⟨m, hm, hkm, hor⟩
  1057. have hm_gt : k < m := by
  1058. by_contra hle
  1059. have hmk : m = k := by omega
  1060. subst m
  1061. rcases hor with hi | hj
  1062. · exact hind_i (by omega)
  1063. · exact hind_j (by omega)
  1064. refine ⟨m, by omega, hm_gt, ?_⟩
  1065. simp only [swapRaw, List.getElem_ofFn]
  1066. rw [if_neg (by omega), if_neg (by omega), if_neg (by omega)]
  1067. have hsw : swapIdx (k + 1) (k + 2) (k + 1) = k + 2 := by unfold swapIdx; split_ifs <;> omega
  1068. rcases hor with hi | hj
  1069. · left; dsimp; rw [show (k - 1) + 2 = k + 1 by omega] at hi; rw [hi, hsw]
  1070. · right; dsimp; rw [show (k - 1) + 2 = k + 1 by omega] at hj; rw [hj, hsw]
  1071. · by_cases huk1 : u = k - 1
  1072. · -- new wire k + 1 corresponds to old gate k (wire k + 2); since k + 1 < gs.length, used by m > k
  1073. rcases hused k hk2 with ⟨m, hm, hkm, hor⟩
  1074. refine ⟨m, by omega, by omega, ?_⟩
  1075. simp only [swapRaw, List.getElem_ofFn]
  1076. rw [if_neg (by omega), if_neg (by omega), if_neg (by omega)]
  1077. have hsw : swapIdx (k + 1) (k + 2) (k + 2) = k + 1 := by unfold swapIdx; split_ifs <;> omega
  1078. have hu2 : u + 2 = k + 1 := by omega
  1079. rw [hu2]
  1080. rcases hor with hi | hj
  1081. · left; dsimp; rw [hi, hsw]
  1082. · right; dsimp; rw [hj, hsw]
  1083. · by_cases hlt : u < k - 1
  1084. · rcases hused u (by omega) with ⟨m, hm, hum, hor⟩
  1085. by_cases hmk : m = k
  1086. · subst m
  1087. refine ⟨k - 1, by omega, by omega, ?_⟩
  1088. have hget : (swapRaw gs k)[k - 1]'(by omega) = gs[k]'(by omega) := by
  1089. simp [swapRaw, show k < gs.length by omega]
  1090. rw [hget]; exact hor
  1091. · by_cases hmk1 : m = k - 1
  1092. · refine ⟨k, by omega, by omega, ?_⟩
  1093. have hne : k ≠ k - 1 := by omega
  1094. have hget : (swapRaw gs k)[k]'(by omega) = gs[k - 1]'(by omega) := by
  1095. simp [swapRaw, hne, show k - 1 < gs.length by omega]
  1096. rw [hget]
  1097. subst m
  1098. exact hor
  1099. · by_cases hm_lt : m < k - 1
  1100. · refine ⟨m, by omega, hum, ?_⟩
  1101. have hget : (swapRaw gs k)[m]'(by omega) = gs[m]'(by omega) := by
  1102. simp [swapRaw, show m ≠ k - 1 by omega, show m ≠ k by omega, hm_lt]
  1103. rw [hget]
  1104. exact hor
  1105. · have hm_gt : k < m := by omega
  1106. refine ⟨m, by omega, hum, ?_⟩
  1107. have hget : (swapRaw gs k)[m]'(by omega) =
  1108. ⟨gs[m].op, swapIdx (k + 1) (k + 2) gs[m].i, swapIdx (k + 1) (k + 2) gs[m].j⟩ := by
  1109. simp [swapRaw, show m ≠ k - 1 by omega, show m ≠ k by omega, hm_lt]
  1110. rw [hget]
  1111. have hsw : swapIdx (k + 1) (k + 2) (u + 2) = u + 2 := swapIdx_lt_of_lt (by omega) (by omega)
  1112. rcases hor with hi | hj
  1113. · left; rw [hi, hsw]
  1114. · right; rw [hj, hsw]
  1115. · -- u > k
  1116. have hu_gt : k < u := by omega
  1117. rcases hused u hu with ⟨m, hm, hum, hor⟩
  1118. refine ⟨m, by omega, hum, ?_⟩
  1119. have hget : (swapRaw gs k)[m]'(by omega) =
  1120. ⟨gs[m].op, swapIdx (k + 1) (k + 2) gs[m].i, swapIdx (k + 1) (k + 2) gs[m].j⟩ := by
  1121. simp [swapRaw, show m ≠ k - 1 by omega, show m ≠ k by omega, show ¬ m < k - 1 by omega]
  1122. rw [hget]
  1123. have hsw : swapIdx (k + 1) (k + 2) (u + 2) = u + 2 := by
  1124. unfold swapIdx; split_ifs <;> omega
  1125. rcases hor with hi | hj
  1126. · left; rw [hi, hsw]
  1127. · right; rw [hj, hsw]
  1128. private lemma swapRaw_computesSigned {f : Polynomial ℤ} {gs : List GateSpec}
  1129. (hwf : WellFormed gs) (hcomp : ComputesSigned f gs)
  1130. {k : ℕ} (hk1 : 1 ≤ k) (hk2 : k + 1 < gs.length)
  1131. (hind_i : gs[k].i ≠ k + 1) (hind_j : gs[k].j ≠ k + 1) :
  1132. ComputesSigned f (swapRaw gs k) := by
  1133. have h := swapRaw_wireVals hwf hk1 hk2 hind_i hind_j (gs.length + 1) (by omega)
  1134. have hsw : swapIdx (k + 1) (k + 2) (gs.length + 1) = gs.length + 1 := by
  1135. unfold swapIdx; split_ifs <;> omega
  1136. rw [hsw] at h
  1137. have hout : output (swapRaw gs k) = output gs := by
  1138. simpa [output, swapRaw_length] using h
  1139. simpa [ComputesSigned, hout] using hcomp
  1140. private lemma swapRaw_ord_prefix {gs : List GateSpec} (hord : OrdCanonical gs)
  1141. {k : ℕ} (hk1 : 1 ≤ k) (hk2 : k + 1 < gs.length) :
  1142. ∀ (m : ℕ) (hm : m < k + 1),
  1143. ((swapRaw gs k)[m]'(by rw [swapRaw_length]; omega)).Ord := by
  1144. intro m hm
  1145. simp only [swapRaw, List.getElem_ofFn]
  1146. split_ifs with h1 h2 h3
  1147. · have heq : gs[k]! = gs[k] := by simp [show k < gs.length by omega]
  1148. rw [heq]; exact hord k (by omega)
  1149. · have heq : gs[k - 1]! = gs[k - 1] := by simp [show k - 1 < gs.length by omega]
  1150. rw [heq]; exact hord (k - 1) (by omega)
  1151. · exact hord m (by omega)
  1152. · omega
  1153. /-! ### Weight Function for Lexicographic Minimization at Fixed Length -/
  1154. private def keyCode (M : ℕ) (g : GateSpec) : ℕ :=
  1155. g.op.idx * M * M + g.i * M + g.j
  1156. private lemma keyCode_lt {M : ℕ} {g : GateSpec} (hi : g.i < M) (hj : g.j < M) :
  1157. keyCode M g < 3 * M * M := by
  1158. unfold keyCode
  1159. have hop : g.op.idx ≤ 2 := by cases g.op <;> simp [SLPOp.idx]
  1160. have h1 : g.i * M + g.j < M * M := by
  1161. calc
  1162. g.i * M + g.j < g.i * M + M := Nat.add_lt_add_left hj _
  1163. _ = (g.i + 1) * M := by ring
  1164. _ ≤ M * M := Nat.mul_le_mul_right M hi
  1165. have h2 : g.op.idx * M * M ≤ 2 * M * M :=
  1166. Nat.mul_le_mul_right M (Nat.mul_le_mul_right M hop)
  1167. calc
  1168. g.op.idx * M * M + g.i * M + g.j = g.op.idx * M * M + (g.i * M + g.j) := by ring
  1169. _ < 2 * M * M + M * M := Nat.add_lt_add_of_le_of_lt h2 h1
  1170. _ = 3 * M * M := by ring
  1171. private lemma keyCode_lt_iff {M : ℕ} {g₁ g₂ : GateSpec}
  1172. (hi₁ : g₁.i < M) (hj₁ : g₁.j < M) (hi₂ : g₂.i < M) (hj₂ : g₂.j < M) :
  1173. keyCode M g₁ < keyCode M g₂ ↔ g₁.keyLt g₂ := by
  1174. unfold keyCode GateSpec.keyLt
  1175. have htail : ∀ a b : ℕ, a < M → b < M → a * M + b < M * M := fun a b ha hb => by
  1176. calc
  1177. a * M + b < a * M + M := Nat.add_lt_add_left hb _
  1178. _ = (a + 1) * M := by ring
  1179. _ ≤ M * M := Nat.mul_le_mul_right M ha
  1180. constructor
  1181. · intro h
  1182. by_cases hop : g₁.op.idx < g₂.op.idx
  1183. · exact Or.inl hop
  1184. · by_cases hop2 : g₂.op.idx < g₁.op.idx
  1185. · exfalso
  1186. have h1 : g₂.op.idx * M * M + g₂.i * M + g₂.j < (g₂.op.idx + 1) * M * M := by
  1187. calc
  1188. g₂.op.idx * M * M + g₂.i * M + g₂.j = g₂.op.idx * M * M + (g₂.i * M + g₂.j) := by ring
  1189. _ < g₂.op.idx * M * M + M * M := Nat.add_lt_add_left (htail _ _ hi₂ hj₂) _
  1190. _ = (g₂.op.idx + 1) * M * M := by ring
  1191. have h2 : (g₂.op.idx + 1) * M * M ≤ g₁.op.idx * M * M :=
  1192. Nat.mul_le_mul_right M (Nat.mul_le_mul_right M hop2)
  1193. omega
  1194. · have hop_eq : g₁.op.idx = g₂.op.idx := by omega
  1195. right; refine ⟨hop_eq, ?_⟩
  1196. rw [hop_eq] at h
  1197. have h' : g₁.i * M + g₁.j < g₂.i * M + g₂.j := by omega
  1198. by_cases hi : g₁.i < g₂.i
  1199. · exact Or.inl hi
  1200. · by_cases hi2 : g₂.i < g₁.i
  1201. · exfalso
  1202. have h1 : g₂.i * M + g₂.j < (g₂.i + 1) * M := by
  1203. calc
  1204. g₂.i * M + g₂.j < g₂.i * M + M := Nat.add_lt_add_left hj₂ _
  1205. _ = (g₂.i + 1) * M := by ring
  1206. have h2 : (g₂.i + 1) * M ≤ g₁.i * M := Nat.mul_le_mul_right M hi2
  1207. omega
  1208. · have hi_eq : g₁.i = g₂.i := by omega
  1209. rw [hi_eq] at h'
  1210. right; exact ⟨hi_eq, by omega⟩
  1211. · intro h
  1212. rcases h with hop | ⟨hop, hi | ⟨hi, hj⟩⟩
  1213. · have h1 : g₁.op.idx * M * M + g₁.i * M + g₁.j < (g₁.op.idx + 1) * M * M := by
  1214. calc
  1215. g₁.op.idx * M * M + g₁.i * M + g₁.j = g₁.op.idx * M * M + (g₁.i * M + g₁.j) := by ring
  1216. _ < g₁.op.idx * M * M + M * M := Nat.add_lt_add_left (htail _ _ hi₁ hj₁) _
  1217. _ = (g₁.op.idx + 1) * M * M := by ring
  1218. have h2 : (g₁.op.idx + 1) * M * M ≤ g₂.op.idx * M * M :=
  1219. Nat.mul_le_mul_right M (Nat.mul_le_mul_right M hop)
  1220. omega
  1221. · rw [hop]
  1222. have h1 : g₁.i * M + g₁.j < (g₁.i + 1) * M := by
  1223. calc
  1224. g₁.i * M + g₁.j < g₁.i * M + M := Nat.add_lt_add_left hj₁ _
  1225. _ = (g₁.i + 1) * M := by ring
  1226. have h2 : (g₁.i + 1) * M ≤ g₂.i * M := Nat.mul_le_mul_right M hi
  1227. omega
  1228. · rw [hop, hi]; omega
  1229. private def lexWeightAux (B M : ℕ) : List GateSpec → ℕ
  1230. | [] => 0
  1231. | g :: rest => keyCode M g * B ^ rest.length + lexWeightAux B M rest
  1232. private lemma lexWeightAux_lt {B M : ℕ} (hB : 0 < B) :
  1233. ∀ (l : List GateSpec), (∀ g ∈ l, keyCode M g < B) → lexWeightAux B M l < B ^ l.length
  1234. | [], _ => by simp [lexWeightAux]
  1235. | g :: rest, h => by
  1236. simp only [lexWeightAux, List.length_cons, pow_succ']
  1237. have hg : keyCode M g < B := h g (by simp)
  1238. have hrest := lexWeightAux_lt hB rest (fun x hx => h x (by simp [hx]))
  1239. have hpow : 0 < B ^ rest.length := pow_pos hB _
  1240. calc
  1241. keyCode M g * B ^ rest.length + lexWeightAux B M rest
  1242. < keyCode M g * B ^ rest.length + B ^ rest.length := Nat.add_lt_add_left hrest _
  1243. _ = (keyCode M g + 1) * B ^ rest.length := by ring
  1244. _ ≤ B * B ^ rest.length := Nat.mul_le_mul_right _ hg
  1245. private lemma lexWeightAux_lt_of_prefix {B M : ℕ} (hB : 0 < B) :
  1246. ∀ (l₁ l₂ : List GateSpec) (p : ℕ)
  1247. (hlen : l₁.length = l₂.length) (hp : p < l₁.length),
  1248. (∀ g ∈ l₂, keyCode M g < B) →
  1249. (∀ idx (h1 : idx < p), l₂[idx]'(by omega) = l₁[idx]'(by omega)) →
  1250. keyCode M (l₂[p]'(by omega)) < keyCode M (l₁[p]'(by omega)) →
  1251. lexWeightAux B M l₂ < lexWeightAux B M l₁
  1252. | [], _, _, _, hp, _, _, _ => by simp at hp
  1253. | _ :: _, [], _, hlen, _, _, _, _ => by simp at hlen
  1254. | g₁ :: r₁, g₂ :: r₂, 0, hlen, _, hbound, _, hlt => by
  1255. simp only [List.length_cons, Nat.add_right_cancel_iff] at hlen
  1256. simp only [List.getElem_cons_zero] at hlt
  1257. simp only [lexWeightAux, ← hlen]
  1258. have hrest := lexWeightAux_lt hB r₂ (fun x hx => hbound x (by simp [hx]))
  1259. rw [← hlen] at hrest
  1260. calc
  1261. keyCode M g₂ * B ^ r₁.length + lexWeightAux B M r₂
  1262. < keyCode M g₂ * B ^ r₁.length + B ^ r₁.length := Nat.add_lt_add_left hrest _
  1263. _ = (keyCode M g₂ + 1) * B ^ r₁.length := by ring
  1264. _ ≤ keyCode M g₁ * B ^ r₁.length := Nat.mul_le_mul_right _ hlt
  1265. _ ≤ keyCode M g₁ * B ^ r₁.length + lexWeightAux B M r₁ := Nat.le_add_right _ _
  1266. | g₁ :: r₁, g₂ :: r₂, p + 1, hlen, hp, hbound, hpref, hlt => by
  1267. have hg : g₂ = g₁ := hpref 0 (by omega)
  1268. subst hg
  1269. simp only [lexWeightAux]
  1270. have hlen' : r₁.length = r₂.length := by simp at hlen; omega
  1271. rw [hlen']
  1272. refine Nat.add_lt_add_left (lexWeightAux_lt_of_prefix hB r₁ r₂ p hlen' (by simp at hp; omega)
  1273. (fun x hx => hbound x (by simp [hx])) ?_ ?_) _
  1274. · intro idx hidx
  1275. exact hpref (idx + 1) (by omega)
  1276. · exact hlt
  1277. private def lexWeight (n : ℕ) (gs : List GateSpec) : ℕ :=
  1278. lexWeightAux (3 * (n + 2) * (n + 2)) (n + 2) gs
  1279. /-! ## 4. Main Theorem: `exists_canonical` -/
  1280. /-- Minimal well-formed lists computing `±f` satisfy `UsedGates`. -/
  1281. private lemma minimal_used {f : Polynomial ℤ} {n : ℕ}
  1282. (hmin : ∀ gs' : List GateSpec, WellFormed gs' → gs'.length < n → ¬ ComputesSigned f gs')
  1283. {gs : List GateSpec} (hwf : WellFormed gs) (hlen : gs.length = n) (hcomp : ComputesSigned f gs) :
  1284. UsedGates gs := by
  1285. intro k hk
  1286. by_contra hdead
  1287. push Not at hdead
  1288. have hdead' : ∀ (k' : ℕ) (hk' : k' < gs.length), k < k' → gs[k'].i ≠ k + 2 ∧ gs[k'].j ≠ k + 2 :=
  1289. fun k' hk' hkk' => hdead k' hk' hkk'
  1290. have hwf' := deleteUnused_wellFormed hwf hdead'
  1291. have hlen' : (deleteUnused gs k).length < n := by rw [deleteUnused_length]; omega
  1292. have hout := deleteUnused_output hwf hk hdead'
  1293. have hcomp' : ComputesSigned f (deleteUnused gs k) := by simpa [ComputesSigned, hout] using hcomp
  1294. exact hmin (deleteUnused gs k) hwf' hlen' hcomp'
  1295. /-- Minimal well-formed lists computing `±f` (with `f ∉ {1, -1, X, -X}`) satisfy `NodupWires`. -/
  1296. private lemma minimal_nodup {f : Polynomial ℤ}
  1297. (hf1 : f ≠ 1) (hf1' : f ≠ -1) (hfX : f ≠ Polynomial.X) (hfX' : f ≠ -Polynomial.X)
  1298. {n : ℕ}
  1299. (hmin : ∀ gs' : List GateSpec, WellFormed gs' → gs'.length < n → ¬ ComputesSigned f gs')
  1300. {gs : List GateSpec} (hwf : WellFormed gs) (hlen : gs.length = n) (hcomp : ComputesSigned f gs) :
  1301. NodupWires gs := by
  1302. intro k hk m hm
  1303. suffices h : ∀ flip : Bool, (wireVals gs).getD (k + 2) 0 ≠ sgnVal flip * (wireVals gs).getD m 0 by
  1304. exact ⟨by simpa [sgnVal] using h false, by simpa [sgnVal] using h true⟩
  1305. intro flip hdup
  1306. by_cases hlast : k + 1 = gs.length
  1307. · have hout : output gs = (wireVals gs).getD (k + 2) 0 := by
  1308. have h1 : gs.length + 1 = k + 2 := by omega
  1309. simp [output, h1]
  1310. rw [← hout] at hdup
  1311. rcases m with _ | _ | m'
  1312. · rw [wireVals_zero] at hdup
  1313. rcases hcomp with hpos | hneg <;> cases flip <;> simp [sgnVal] at hdup
  1314. · exact hf1 (hpos.symm.trans hdup)
  1315. · exact hf1' (hpos.symm.trans hdup)
  1316. · exact hf1' (neg_eq_iff_eq_neg.mp (hneg.symm.trans hdup))
  1317. · exact hf1 (neg_inj.mp (hneg.symm.trans hdup))
  1318. · rw [wireVals_one] at hdup
  1319. rcases hcomp with hpos | hneg <;> cases flip <;> simp [sgnVal] at hdup
  1320. · exact hfX (hpos.symm.trans hdup)
  1321. · exact hfX' (hpos.symm.trans hdup)
  1322. · exact hfX' (neg_eq_iff_eq_neg.mp (hneg.symm.trans hdup))
  1323. · exact hfX (neg_inj.mp (hneg.symm.trans hdup))
  1324. · have hm_lt : m' + 1 < gs.length := by omega
  1325. let gs' := gs.take (m' + 1)
  1326. have hlen' : gs'.length = m' + 1 := by simp [gs', hm_lt.le]
  1327. have hwf' : WellFormed gs' := by
  1328. intro r hr
  1329. have hr1 : r < m' + 1 := by simpa [hlen'] using hr
  1330. have hr_gs : r < gs.length := by omega
  1331. have hget : gs'[r] = gs[r] := by simp [gs']
  1332. rw [hget]
  1333. exact hwf r hr_gs
  1334. have hout' : output gs' = (wireVals gs).getD (m' + 2) 0 := by
  1335. have htk := wireVals_take_getD gs hm_lt.le (w := m' + 2) (by omega)
  1336. simpa [output, gs', hlen'] using htk
  1337. have hcomp' : ComputesSigned f gs' := by
  1338. rw [ComputesSigned, hout']
  1339. rcases hcomp with hpos | hneg <;> cases flip <;> simp [sgnVal] at hdup
  1340. · left; exact hdup.symm.trans hpos
  1341. · right; exact neg_eq_iff_eq_neg.mp (hdup.symm.trans hpos)
  1342. · right; exact hdup.symm.trans hneg
  1343. · left; exact neg_inj.mp (hdup.symm.trans hneg)
  1344. exact hmin gs' hwf' (by omega) hcomp'
  1345. · have hk1 : k + 1 < gs.length := by omega
  1346. have hwf' := redirGate_wellFormed hwf hm flip
  1347. have hcomp' := redirGate_computesSigned hwf hcomp hk1 hm flip hdup
  1348. have hlen' : (redirGate gs k m flip).length < n := by rw [redirGate_length]; omega
  1349. exact hmin _ hwf' hlen' hcomp'
  1350. /-- Any `UsedGates` + `NodupWires` list with nonzero output has `NonzeroWires`. -/
  1351. private lemma nonzeroWires_of_used_nodup {gs : List GateSpec}
  1352. (hwf : WellFormed gs) (hused : UsedGates gs) (hnodup : NodupWires gs)
  1353. (hout : output gs ≠ 0) :
  1354. NonzeroWires gs := by
  1355. classical
  1356. by_contra hnz
  1357. simp only [NonzeroWires, not_forall, not_not] at hnz
  1358. let S : Finset ℕ := (Finset.range gs.length).filter (fun k => (wireVals gs).getD (k + 2) 0 = 0)
  1359. have hS_ne : S.Nonempty := by
  1360. rcases hnz with ⟨k, hk, hk0⟩
  1361. exact ⟨k, Finset.mem_filter.mpr ⟨Finset.mem_range.mpr hk, hk0⟩⟩
  1362. let k := S.max' hS_ne
  1363. have hk_mem : k ∈ S := Finset.max'_mem S hS_ne
  1364. have hk_lt : k < gs.length := Finset.mem_range.mp (Finset.mem_filter.mp hk_mem).1
  1365. have hk_zero : (wireVals gs).getD (k + 2) 0 = 0 := (Finset.mem_filter.mp hk_mem).2
  1366. have hk_not_last : k + 1 < gs.length := by
  1367. by_contra hge
  1368. have hlast : k + 1 = gs.length := by omega
  1369. have hout_eq : output gs = (wireVals gs).getD (k + 2) 0 := by
  1370. have h1 : gs.length + 1 = k + 2 := by omega
  1371. simp [output, h1]
  1372. exact hout (hout_eq.trans hk_zero)
  1373. rcases hused k hk_not_last with ⟨k', hk', hkk', hor⟩
  1374. have hk'_ne : (wireVals gs).getD (k' + 2) 0 ≠ 0 := by
  1375. intro h0
  1376. have hmem : k' ∈ S := Finset.mem_filter.mpr ⟨Finset.mem_range.mpr hk', h0⟩
  1377. have hle := Finset.le_max' S k' hmem
  1378. omega
  1379. have hgate := wireVals_gate_wf hwf hk'
  1380. have hgi := (hwf k' hk').1
  1381. have hgj := (hwf k' hk').2
  1382. cases hop : gs[k'].op <;> simp only [hop, SLPOp.applyPoly] at hgate
  1383. · rcases hor with hi | hj
  1384. · rw [hi, hk_zero, zero_add] at hgate
  1385. exact (hnodup k' hk' gs[k'].j hgj).1 hgate
  1386. · rw [hj, hk_zero, add_zero] at hgate
  1387. exact (hnodup k' hk' gs[k'].i hgi).1 hgate
  1388. · rcases hor with hi | hj
  1389. · rw [hi, hk_zero, zero_sub] at hgate
  1390. exact (hnodup k' hk' gs[k'].j hgj).2 hgate
  1391. · rw [hj, hk_zero, sub_zero] at hgate
  1392. exact (hnodup k' hk' gs[k'].i hgi).1 hgate
  1393. · rcases hor with hi | hj
  1394. · rw [hi, hk_zero, zero_mul] at hgate
  1395. exact hk'_ne hgate
  1396. · rw [hj, hk_zero, mul_zero] at hgate
  1397. exact hk'_ne hgate
  1398. /-- **Main Canonicalization Soundness Theorem (`exists_canonical`):**
  1399. For every `t` and `P : SLP t` with nonzero output `f := P.evalPoly ≠ 0`,
  1400. either `f ∈ {1, -1, X, -X}` or there exists a `Canonical` gate list `gs` with
  1401. `1 ≤ gs.length ≤ t` computing `f` or `-f`. -/
  1402. theorem exists_canonical {t : ℕ} (P : SLP t) (hf : P.evalPoly ≠ 0) :
  1403. (P.evalPoly = 1 ∨ P.evalPoly = -1 ∨ P.evalPoly = Polynomial.X ∨ P.evalPoly = -Polynomial.X) ∨
  1404. ∃ gs : List GateSpec, Canonical gs ∧ 1 ≤ gs.length ∧ gs.length ≤ t ∧
  1405. (output gs = P.evalPoly ∨ output gs = -P.evalPoly) := by
  1406. classical
  1407. set f := P.evalPoly
  1408. by_cases hbase : f = 1 ∨ f = -1 ∨ f = Polynomial.X ∨ f = -Polynomial.X
  1409. · exact Or.inl hbase
  1410. right
  1411. push Not at hbase
  1412. rcases hbase with ⟨hf1, hf1', hfX, hfX'⟩
  1413. have hex_len : ∃ n : ℕ, ∃ gs : List GateSpec, WellFormed gs ∧ gs.length = n ∧ ComputesSigned f gs :=
  1414. ⟨t, P.toGateList, P.toGateList_wellFormed, P.toGateList_length, Or.inl P.output_toGateList⟩
  1415. let n := Nat.find hex_len
  1416. have hn_spec : ∃ gs : List GateSpec, WellFormed gs ∧ gs.length = n ∧ ComputesSigned f gs :=
  1417. Nat.find_spec hex_len
  1418. have hn_le : n ≤ t := Nat.find_min' hex_len ⟨P.toGateList, P.toGateList_wellFormed,
  1419. P.toGateList_length, Or.inl P.output_toGateList⟩
  1420. have hn_min : ∀ gs' : List GateSpec, WellFormed gs' → gs'.length < n → ¬ ComputesSigned f gs' := by
  1421. intro gs' hwf' hlt' hcomp'
  1422. exact Nat.find_min hex_len hlt' ⟨gs', hwf', rfl, hcomp'⟩
  1423. have hn_pos : 1 ≤ n := by
  1424. by_contra h0
  1425. have hn0 : n = 0 := by omega
  1426. rcases hn_spec with ⟨gs, _, hlen, hcomp⟩
  1427. have hnil : gs = [] := List.eq_nil_of_length_eq_zero (hlen.trans hn0)
  1428. subst hnil
  1429. have hout : output [] = Polynomial.X := by simp [output, wireVals, wireValsAux]
  1430. rcases hcomp with hpos | hneg
  1431. · exact hfX (hpos.symm.trans hout)
  1432. · exact hfX' (neg_eq_iff_eq_neg.mp (hneg.symm.trans hout))
  1433. -- Take one minimal-length witness and normalize it to get a GoodNormalized witness
  1434. rcases hn_spec with ⟨gs₁, hwf₁, hlen₁, hcomp₁⟩
  1435. have hused₁ := minimal_used hn_min hwf₁ hlen₁ hcomp₁
  1436. have hnodup₁ := minimal_nodup hf1 hf1' hfX hfX' hn_min hwf₁ hlen₁ hcomp₁
  1437. have hout₁ : output gs₁ ≠ 0 := by
  1438. rcases hcomp₁ with h | h <;> rw [h]
  1439. · exact hf
  1440. · exact neg_ne_zero.mpr hf
  1441. have hnz₁ := nonzeroWires_of_used_nodup hwf₁ hused₁ hnodup₁ hout₁
  1442. let gs₂ := ordNormalize gs₁
  1443. have hwf₂ := ordNormalize_wellFormed hwf₁
  1444. have hlen₂ : gs₂.length = n := by simp [gs₂, ordNormalize_length, hlen₁]
  1445. have hord₂ := (ordNormalize_wireVals_and_ord hwf₁ hnz₁).2
  1446. have hnz₂ := ordNormalize_preserves_nonzero hwf₁ hnz₁
  1447. have hnodup₂ := ordNormalize_preserves_nodup hwf₁ hnz₁ hnodup₁
  1448. have hused₂ := ordNormalize_preserves_used hused₁
  1449. have hcomp₂ := ordNormalize_computesSigned hwf₁ hnz₁ hcomp₁
  1450. -- Minimize lexWeight among all GoodNormalized lists of length n
  1451. let GoodNorm (gs : List GateSpec) : Prop :=
  1452. WellFormed gs ∧ gs.length = n ∧ OrdCanonical gs ∧ NonzeroWires gs ∧
  1453. NodupWires gs ∧ UsedGates gs ∧ ComputesSigned f gs
  1454. have hex_w : ∃ w : ℕ, ∃ gs : List GateSpec, GoodNorm gs ∧ lexWeight n gs = w :=
  1455. ⟨lexWeight n gs₂, gs₂, ⟨hwf₂, hlen₂, hord₂, hnz₂, hnodup₂, hused₂, hcomp₂⟩, rfl⟩
  1456. let w_min := Nat.find hex_w
  1457. rcases Nat.find_spec hex_w with ⟨gs, ⟨hwf, hlen, hord, hnz, hnodup, hused, hcomp⟩, hw_eq⟩
  1458. have hlex : LexOrdered gs := by
  1459. intro k hk1 hk2 h1 h2 hind_i hind_j
  1460. by_contra hnot
  1461. rw [GateSpec.not_keyLe_iff] at hnot
  1462. -- Swap gates k-1 and k, then re-normalize
  1463. let gs_sw := swapRaw gs k
  1464. have hwf_sw := swapRaw_wellFormed hwf hk1 hk2 hind_i hind_j
  1465. have hnz_sw := swapRaw_preserves_nonzero hwf hnz hk1 hk2 hind_i hind_j
  1466. have hnodup_sw := swapRaw_preserves_nodup hwf hnodup hk1 hk2 hind_i hind_j
  1467. have hused_sw := swapRaw_preserves_used hused hk1 hk2 hind_i hind_j
  1468. have hcomp_sw := swapRaw_computesSigned hwf hcomp hk1 hk2 hind_i hind_j
  1469. have hord_pref := swapRaw_ord_prefix hord hk1 hk2
  1470. let gs' := ordNormalize gs_sw
  1471. have hwf' := ordNormalize_wellFormed hwf_sw
  1472. have hlen' : gs'.length = n := by
  1473. dsimp only [gs', gs_sw]
  1474. rw [ordNormalize_length, swapRaw_length, hlen]
  1475. have hord' := (ordNormalize_wireVals_and_ord hwf_sw hnz_sw).2
  1476. have hnz' := ordNormalize_preserves_nonzero hwf_sw hnz_sw
  1477. have hnodup' := ordNormalize_preserves_nodup hwf_sw hnz_sw hnodup_sw
  1478. have hused' := ordNormalize_preserves_used hused_sw
  1479. have hcomp' := ordNormalize_computesSigned hwf_sw hnz_sw hcomp_sw
  1480. -- Compare lexWeight n gs' < lexWeight n gs
  1481. have hB : 0 < 3 * (n + 2) * (n + 2) := by positivity
  1482. have hbound : ∀ g ∈ gs', keyCode (n + 2) g < 3 * (n + 2) * (n + 2) := by
  1483. intro g hg
  1484. rcases List.mem_iff_getElem.mp hg with ⟨m, hm, rfl⟩
  1485. have hgm : gs'[m].i < m + 2 ∧ gs'[m].j < m + 2 := hwf' m hm
  1486. rw [hlen'] at hm
  1487. exact keyCode_lt (by omega) (by omega)
  1488. have hpref_eq : ∀ idx (hidx : idx < k - 1),
  1489. gs'[idx]'(by omega) = gs[idx]'(by omega) := by
  1490. intro idx hidx
  1491. have hnorm_eq := ordNormalize_prefix_eq hwf_sw (p := k + 1)
  1492. (by rw [swapRaw_length]; omega) hord_pref idx (by omega)
  1493. rw [hnorm_eq]
  1494. simp [swapRaw, show idx ≠ k - 1 by omega, show idx ≠ k by omega, hidx]
  1495. have hpk_eq : gs'[k - 1]'(by omega) = gs[k]'(by omega) := by
  1496. have hnorm_eq := ordNormalize_prefix_eq hwf_sw (p := k + 1)
  1497. (by rw [swapRaw_length]; omega) hord_pref (k - 1) (by omega)
  1498. rw [hnorm_eq]
  1499. simp [swapRaw, show k < gs.length by omega]
  1500. have hlt_code : keyCode (n + 2) (gs'[k - 1]'(by omega)) <
  1501. keyCode (n + 2) (gs[k - 1]'(by omega)) := by
  1502. rw [hpk_eq]
  1503. have hgk := hwf k (by omega)
  1504. have hgk1 := hwf (k - 1) (by omega)
  1505. rw [keyCode_lt_iff (by omega) (by omega) (by omega) (by omega)]
  1506. exact hnot
  1507. have hw_lt : lexWeight n gs' < lexWeight n gs :=
  1508. lexWeightAux_lt_of_prefix hB gs gs' (k - 1) (by omega) (by omega) hbound hpref_eq hlt_code
  1509. exact Nat.find_min hex_w (hw_eq ▸ hw_lt)
  1510. ⟨gs', ⟨hwf', hlen', hord', hnz', hnodup', hused', hcomp'⟩, rfl⟩
  1511. exact ⟨gs, ⟨hwf, hord, hnz, hnodup, hused, hlex⟩, by omega, by omega, hcomp⟩
  1512. /-! ## 5. Dead-Gate Pruning Bound (`deadgate_count_bound`) -/
  1513. /-- Set of gate wires among `{2, ..., d + 1}` that are not an operand of any of the
  1514. first `d` gates `0, ..., d - 1`. This matches `unused_mask` in the enumerator after `d` gates. -/
  1515. def unusedWires (gs : List GateSpec) (d : ℕ) : Finset ℕ :=
  1516. (Finset.Ico 2 (d + 2)).filter (fun w =>
  1517. ∀ k < d, (gs.getD k default).i ≠ w ∧ (gs.getD k default).j ≠ w)
  1518. theorem unusedWires_zero (gs : List GateSpec) : unusedWires gs 0 = ∅ := by
  1519. simp [unusedWires]
  1520. theorem unusedWires_succ {gs : List GateSpec} (hwf : WellFormed gs) {d : ℕ} (hd : d < gs.length) :
  1521. unusedWires gs (d + 1) =
  1522. insert (d + 2) (unusedWires gs d \ {gs[d].i, gs[d].j}) := by
  1523. ext w
  1524. simp only [unusedWires, Finset.mem_insert, Finset.mem_sdiff, Finset.mem_filter,
  1525. Finset.mem_Ico, Finset.mem_singleton]
  1526. constructor
  1527. · rintro ⟨⟨hw2, hwd3⟩, hnot⟩
  1528. by_cases hweq : w = d + 2
  1529. · exact Or.inl hweq
  1530. · right
  1531. refine ⟨⟨⟨hw2, by omega⟩, fun k hk => hnot k (by omega)⟩, ?_⟩
  1532. have hd_get := hnot d (by omega)
  1533. rw [getD_eq_getElem_of_lt _ _ hd] at hd_get
  1534. tauto
  1535. · rintro (rfl | ⟨⟨⟨hw2, hwd2⟩, hnot⟩, hne⟩)
  1536. · refine ⟨⟨by omega, by omega⟩, fun k hk => ?_⟩
  1537. have hk_gs : k < gs.length := by omega
  1538. have hgk := hwf k hk_gs
  1539. rw [getD_eq_getElem_of_lt _ _ hk_gs]
  1540. omega
  1541. · refine ⟨⟨hw2, by omega⟩, fun k hk => ?_⟩
  1542. by_cases hkd : k < d
  1543. · exact hnot k hkd
  1544. · have heq : k = d := by omega
  1545. subst heq
  1546. rw [getD_eq_getElem_of_lt _ _ hd]
  1547. tauto
  1548. /-- **Dead-Gate Pruning Soundness (`deadgate_count_bound`):**
  1549. For any gate list of length `n` satisfying `UsedGates` (in particular, any `Canonical` list)
  1550. and any prefix length `d < n`, the number of unused gate wires after `d` gates is at most
  1551. `n - d + 1`. Equivalently, with `depth = d - 1` and `rem_after = n - 1 - depth`,
  1552. `unused_cnt ≤ rem_after + 1`. -/
  1553. theorem deadgate_count_bound {gs : List GateSpec} (hused : UsedGates gs)
  1554. {d : ℕ} (hd : d < gs.length) :
  1555. (unusedWires gs d).card ≤ gs.length - d + 1 := by
  1556. classical
  1557. let n := gs.length
  1558. let A : Finset ℕ := unusedWires gs d
  1559. let B : Finset ℕ := Finset.Ico (d + 2) (n + 1)
  1560. let Slots : Finset ℕ := (Finset.Ico d n).biUnion (fun k' =>
  1561. ({(gs.getD k' default).i, (gs.getD k' default).j} : Finset ℕ))
  1562. have hdisj : Disjoint A B := by
  1563. rw [Finset.disjoint_left]
  1564. intro w hwA hwB
  1565. have h1 := (Finset.mem_Ico.mp (Finset.mem_filter.mp hwA).1).2
  1566. have h2 := (Finset.mem_Ico.mp hwB).1
  1567. omega
  1568. have hA_sub : A ⊆ Slots := by
  1569. intro w hwA
  1570. have hw_ico := (Finset.mem_filter.mp hwA).1
  1571. have hnot := (Finset.mem_filter.mp hwA).2
  1572. have hw2 : 2 ≤ w := (Finset.mem_Ico.mp hw_ico).1
  1573. have hwd : w < d + 2 := (Finset.mem_Ico.mp hw_ico).2
  1574. have hu : w - 2 + 1 < n := by omega
  1575. rcases hused (w - 2) hu with ⟨k', hk', huk', hor⟩
  1576. have hdk' : d ≤ k' := by
  1577. by_contra hlt
  1578. have hk'_d : k' < d := by omega
  1579. have hnd := hnot k' hk'_d
  1580. rw [getD_eq_getElem_of_lt _ _ hk'] at hnd
  1581. omega
  1582. rw [Finset.mem_biUnion]
  1583. refine ⟨k', Finset.mem_Ico.mpr ⟨hdk', hk'⟩, ?_⟩
  1584. rw [getD_eq_getElem_of_lt _ _ hk', Finset.mem_insert, Finset.mem_singleton]
  1585. omega
  1586. have hB_sub : B ⊆ Slots := by
  1587. intro w hwB
  1588. have hw1 : d + 2 ≤ w := (Finset.mem_Ico.mp hwB).1
  1589. have hw2 : w < n + 1 := (Finset.mem_Ico.mp hwB).2
  1590. have hu : w - 2 + 1 < n := by omega
  1591. rcases hused (w - 2) hu with ⟨k', hk', huk', hor⟩
  1592. have hdk' : d ≤ k' := by omega
  1593. rw [Finset.mem_biUnion]
  1594. refine ⟨k', Finset.mem_Ico.mpr ⟨hdk', hk'⟩, ?_⟩
  1595. rw [getD_eq_getElem_of_lt _ _ hk', Finset.mem_insert, Finset.mem_singleton]
  1596. omega
  1597. have hAB_sub : A ∪ B ⊆ Slots := Finset.union_subset hA_sub hB_sub
  1598. have hSlots_card : Slots.card ≤ 2 * (n - d) := by
  1599. calc
  1600. Slots.card ≤ ∑ k' ∈ Finset.Ico d n,
  1601. ({(gs.getD k' default).i, (gs.getD k' default).j} : Finset ℕ).card :=
  1602. Finset.card_biUnion_le
  1603. _ ≤ ∑ _k' ∈ Finset.Ico d n, 2 :=
  1604. Finset.sum_le_sum (fun _ _ => Finset.card_le_two)
  1605. _ = (Finset.Ico d n).card * 2 := Finset.sum_const_nat (fun _ _ => rfl)
  1606. _ = 2 * (n - d) := by rw [Nat.card_Ico]; ring
  1607. have hB_card : B.card = n - d - 1 := by
  1608. rw [Nat.card_Ico]
  1609. omega
  1610. have hAB_card : A.card + B.card ≤ 2 * (n - d) := by
  1611. calc
  1612. A.card + B.card = (A ∪ B).card := (Finset.card_union_of_disjoint hdisj).symm
  1613. _ ≤ Slots.card := Finset.card_le_card hAB_sub
  1614. _ ≤ 2 * (n - d) := hSlots_card
  1615. dsimp only [A, n] at hAB_card hB_card
  1616. omega
  1617. /-- Corollary for `Canonical` lists. -/
  1618. theorem deadgate_count_bound_of_canonical {gs : List GateSpec} (hcan : Canonical gs)
  1619. {d : ℕ} (hd : d < gs.length) :
  1620. (unusedWires gs d).card ≤ gs.length - d + 1 :=
  1621. deadgate_count_bound hcan.used hd
  1622. /-- Leaf-level consequence of `UsedGates`: before the last gate (`1 ≤ gs.length`),
  1623. at most `2` gate wires remain unused, and every unused gate wire must be an operand
  1624. of the last gate `gs[gs.length - 1]`. -/
  1625. theorem unusedWires_last_gate {gs : List GateSpec} (hused : UsedGates gs)
  1626. (hne : 1 ≤ gs.length) :
  1627. (unusedWires gs (gs.length - 1)).card ≤ 2 ∧
  1628. ∀ w ∈ unusedWires gs (gs.length - 1),
  1629. w = (gs[gs.length - 1]'(by omega)).i ∨ w = (gs[gs.length - 1]'(by omega)).j := by
  1630. have hcard : (unusedWires gs (gs.length - 1)).card ≤ 2 := by
  1631. have h := deadgate_count_bound hused (d := gs.length - 1) (by omega)
  1632. omega
  1633. refine ⟨hcard, fun w hw => ?_⟩
  1634. have hw_ico := (Finset.mem_filter.mp hw).1
  1635. have hnot := (Finset.mem_filter.mp hw).2
  1636. have hw2 : 2 ≤ w := (Finset.mem_Ico.mp hw_ico).1
  1637. have hwd : w < gs.length - 1 + 2 := (Finset.mem_Ico.mp hw_ico).2
  1638. rcases hused (w - 2) (by omega) with ⟨k', hk', huk', hor⟩
  1639. have hk'_ge : gs.length - 1 ≤ k' := by
  1640. by_contra hlt
  1641. have hnd := hnot k' (by omega)
  1642. rw [getD_eq_getElem_of_lt _ _ hk'] at hnd
  1643. omega
  1644. have hk'_eq : k' = gs.length - 1 := by omega
  1645. subst hk'_eq
  1646. rw [show w - 2 + 2 = w by omega] at hor
  1647. exact hor.imp Eq.symm Eq.symm
  1648. /-! ## 6. Checklist Reduction Theorem (`zmax_le_of_checklist`) -/
  1649. /-- Negating a polynomial in `ℤ[X]` preserves its distinct integer root count. -/
  1650. theorem intRootCount_neg (f : Polynomial ℤ) : intRootCount (-f) = intRootCount f := by
  1651. simp [intRootCount, intRootSet]
  1652. /-- The basic seed polynomials `{1, -1, X, -X}` each have at most `1` distinct integer root. -/
  1653. theorem intRootCount_le_one_of_basic {f : Polynomial ℤ}
  1654. (hf : f = 1 ∨ f = -1 ∨ f = Polynomial.X ∨ f = -Polynomial.X) :
  1655. intRootCount f ≤ 1 := by
  1656. rcases hf with rfl | rfl | rfl | rfl
  1657. · exact (intRootCount_le_natDegree one_ne_zero).trans (by simp)
  1658. · rw [intRootCount_neg]
  1659. exact (intRootCount_le_natDegree one_ne_zero).trans (by simp)
  1660. · exact (intRootCount_le_natDegree Polynomial.X_ne_zero).trans (by simp)
  1661. · rw [intRootCount_neg]
  1662. exact (intRootCount_le_natDegree Polynomial.X_ne_zero).trans (by simp)
  1663. /-- **Checklist Soundness Theorem (`zmax_le_of_checklist`):**
  1664. If every `Canonical` gate list `gs` with `1 ≤ gs.length ≤ τ` and `output gs ≠ 0`
  1665. satisfies `intRootCount (output gs) ≤ K` (where `1 ≤ K`), then every nonzero `SLP t`
  1666. with `t ≤ τ` computes a polynomial with at most `K` distinct integer roots. -/
  1667. theorem zmax_le_of_checklist (τ K : ℕ) (hK : 1 ≤ K)
  1668. (hcheck : ∀ gs : List GateSpec, Canonical gs → 1 ≤ gs.length → gs.length ≤ τ →
  1669. output gs ≠ 0 → intRootCount (output gs) ≤ K) :
  1670. ∀ t ≤ τ, ∀ P : SLP t, P.evalPoly ≠ 0 → intRootCount P.evalPoly ≤ K := by
  1671. intro t ht P hne
  1672. rcases exists_canonical P hne with hbasic | ⟨gs, hcan, h1, hlen_t, hout⟩
  1673. · exact (intRootCount_le_one_of_basic hbasic).trans hK
  1674. · have hlen_tau : gs.length ≤ τ := hlen_t.trans ht
  1675. have hout_ne : output gs ≠ 0 := by
  1676. rcases hout with h | h <;> rw [h]
  1677. · exact hne
  1678. · exact neg_ne_zero.mpr hne
  1679. have hbound := hcheck gs hcan h1 hlen_tau hout_ne
  1680. rcases hout with h | h
  1681. · rwa [← h]
  1682. · rwa [← intRootCount_neg P.evalPoly, ← h]
  1683. /-- **Conditional Upper Bound `z_max(9) ≤ 6`:**
  1684. If the exhaustive checklist over all `Canonical` gate lists of length `1 ≤ n ≤ 9`
  1685. with nonzero output establishes `intRootCount (output gs) ≤ 6`, then every nonzero
  1686. `SLP t` with `t ≤ 9` has at most `6` distinct integer roots.
  1687. *Verification boundary:* This theorem machine-checks the soundness of canonicalization
  1688. (`WellFormed`, `OrdCanonical`, `NonzeroWires`, `NodupWires`, `UsedGates`, `LexOrdered`)
  1689. and sign/length reduction. It does **not** internally verify the C/Python enumeration loop,
  1690. hash-table collision freeness, or the execution trace of external runs; those must be
  1691. supplied via the checklist hypothesis `hcheck` (or per-leaf certificates). -/
  1692. theorem zmax9_le_six_of_checklist
  1693. (hcheck : ∀ gs : List GateSpec, Canonical gs → 1 ≤ gs.length → gs.length ≤ 9 →
  1694. output gs ≠ 0 → intRootCount (output gs) ≤ 6) :
  1695. ∀ t ≤ 9, ∀ P : SLP t, P.evalPoly ≠ 0 → intRootCount P.evalPoly ≤ 6 :=
  1696. zmax_le_of_checklist 9 6 (by omega) hcheck
  1697. /-- **Conditional Upper Bound `z_max(10) ≤ 7`:**
  1698. If the exhaustive checklist over all `Canonical` gate lists of length `1 ≤ n ≤ 10`
  1699. with nonzero output establishes `intRootCount (output gs) ≤ 7`, then every nonzero
  1700. `SLP t` with `t ≤ 10` has at most `7` distinct integer roots.
  1701. *Verification boundary:* Same as `zmax9_le_six_of_checklist`; the external enumerator loop
  1702. itself is not verified inside Lean and appears as the hypothesis `hcheck`. -/
  1703. theorem zmax10_le_seven_of_checklist
  1704. (hcheck : ∀ gs : List GateSpec, Canonical gs → 1 ≤ gs.length → gs.length ≤ 10 →
  1705. output gs ≠ 0 → intRootCount (output gs) ≤ 7) :
  1706. ∀ t ≤ 10, ∀ P : SLP t, P.evalPoly ≠ 0 → intRootCount P.evalPoly ≤ 7 :=
  1707. zmax_le_of_checklist 10 7 (by omega) hcheck
  1708. end Research.PvsNP.Tau
  1709. end