"""Turn Magma certificate blocks (magma/out/*.out) into the Lean file formal/Quintic85/Cert.lean. Magma FINDS the identities (scripts magma/02_lines_certificates.m, magma/03_good_pair_small.m); Lean CHECKS every one of them with `ring` / `linear_combination` when Cert.lean is compiled. Magma's polynomial syntax (`*`, `^`, `/`, `+`, `-`, integer/rational literals, variables named as in the Lean statements) is valid Lean syntax over a field, so the only transformation is wrapping each polynomial in parentheses. Usage: python3 -I tools/cert2lean.py magma/out/02_lines_certificates.out \ magma/out/03_good_pair_small.out > formal/Quintic85/Cert.lean """ import re import sys blocks: dict[str, dict[str, str]] = {} for path in sys.argv[1:]: txt = open(path).read() for m in re.finditer(r"^CERT (\S+)\n(.*?)\nEND", txt, re.S | re.M): d = {} for line in m.group(2).split("\n"): if " := " in line: k, v = line.split(" := ", 1) d[k.strip()] = v.strip() blocks[m.group(1)] = d def P(name: str, key: str, rename: dict[str, str] | None = None) -> str: s = blocks[name][key] if rename: for a, b in rename.items(): s = re.sub(rf"\b{a}\b", b, s) return f"({s})" E = [P("skew_coeffs", f"E{k}") for k in range(6)] F_pl = [P("plane_coeffs", f"f{k}") for k in range(6)] F_in = [P("inf_coeffs", f"f{k}") for k in range(6)] out = [] w = out.append w("""module public import Mathlib.Tactic.LinearCombination public import Mathlib.Tactic.Ring @[expose] public section set_option linter.unusedVariables false set_option linter.unusedSimpArgs false set_option linter.unusedSectionVars false /-! # Algebraic certificates for Theorem 1.1 (generated) **Do not edit by hand.** Generated by `tools/cert2lean.py` from the Magma transcripts `magma/out/02_lines_certificates.out` and `magma/out/03_good_pair_small.out`. Magma *found* every identity below (ideal coordinates, eliminations, extended gcds); Lean *checks* every one of them here with `ring` or `linear_combination`. Nothing in this file is trusted from Magma: a wrong certificate would simply fail to compile. Notation: `Fv t x₁ x₂ x₃ x₄` is the value of the quintic form `F_t` of Yu–Zhu (3.1). * Skew lines: `x = (a11 X + a12 Y, a21 X + a22 Y, X, Y)`; coefficients `skewE0 … skewE5`. In normalised coordinates `a11 = l u r, a12 = l v, a21 = l u, a22 = l` they become the paper's equations (3.2)–(3.7), written with `polR, polN1, polP4, polP5, polW4, polV`. * `good_pair`: from (3.4), (3.5) and `R(r) = V(v) = 0` follows `r² + 2rv − 3 = 0`. * Lines meeting `L₁`: `x = α(c1, c2, 0, 0) + β(d1, d2, 1, μ)` (plane `x₄ = μx₃`) and `x = α(c1, c2, 0, 0) + β(d1, d2, 0, 1)` (plane `x₃ = 0`). -/ namespace Quintic85.Cert variable {K : Type*} [Field K] [CharZero K] /-- The value of the quintic form `F_t` of Yu–Zhu (3.1) at `(x₁, x₂, x₃, x₄)`. -/ def Fv (t x1 x2 x3 x4 : K) : K := (x3 - x4) * (x1 ^ 4 - 3 * x2 ^ 4) - 6 * (x3 + x4) * x1 ^ 2 * x2 ^ 2 + x3 * x4 * (x4 ^ 3 - t * x3 ^ 3) /-! ## Binary quintics: coefficients from six values -/ /-- A binary quintic vanishing identically has all six coefficients zero. The coefficients are recovered from the values at `(1,0), (0,1), (1,1), (1,-1), (1,2), (2,1)` (inverse Vandermonde weights computed by Magma, checked here). -/ theorem quintic_coeffs {e0 e1 e2 e3 e4 e5 : K} (h : ∀ a b : K, e0 * a ^ 5 + e1 * a ^ 4 * b + e2 * a ^ 3 * b ^ 2 + e3 * a ^ 2 * b ^ 3 + e4 * a * b ^ 4 + e5 * b ^ 5 = 0) : e0 = 0 ∧ e1 = 0 ∧ e2 = 0 ∧ e3 = 0 ∧ e4 = 0 ∧ e5 = 0 := by have h1 := h 1 0 have h2 := h 0 1 have h3 := h 1 1 have h4 := h 1 (-1) have h5 := h 1 2 have h6 := h 2 1 refine ⟨?_, ?_, ?_, ?_, ?_, ?_⟩""") # Vandermonde weights from the Magma transcript vtxt = open(sys.argv[1]).read() vw = {} for m in re.finditer(r"VANDERMONDE e(\d) := \[ (.*?) \]", vtxt): vw[int(m.group(1))] = [x.strip() for x in m.group(2).split(",")] for k in range(6): terms = " + ".join(f"({c} : K) * h{i+1}" for i, c in enumerate(vw[k]) if c != "0") w(f" · linear_combination {terms}") w(""" /-! ## Skew lines: the binary quintic `F_t(a11 X + a12 Y, a21 X + a22 Y, X, Y)` -/ """) for k in range(6): w(f"/-- Coefficient of `X^{5-k} Y^{k}` in `F_t(a11 X + a12 Y, a21 X + a22 Y, X, Y)`. -/") w(f"def skewE{k} (t a11 a12 a21 a22 : K) : K :=\n {E[k]}\n") w("""theorem skew_expand (t a11 a12 a21 a22 X Y : K) : Fv t (a11 * X + a12 * Y) (a21 * X + a22 * Y) X Y = skewE0 t a11 a12 a21 a22 * X ^ 5 + skewE1 t a11 a12 a21 a22 * X ^ 4 * Y + skewE2 t a11 a12 a21 a22 * X ^ 3 * Y ^ 2 + skewE3 t a11 a12 a21 a22 * X ^ 2 * Y ^ 3 + skewE4 t a11 a12 a21 a22 * X * Y ^ 4 + skewE5 t a11 a12 a21 a22 * Y ^ 5 := by simp only [Fv, skewE0, skewE1, skewE2, skewE3, skewE4, skewE5] ring """) w("""/-- `R(r) = r⁴ - 6r² - 3`. -/ def polR (r : K) : K := r ^ 4 - 6 * r ^ 2 - 3 /-- `V(v) = v⁴ + 6v² - 3`. -/ def polV (v : K) : K := v ^ 4 + 6 * v ^ 2 - 3 /-- The long factor of (3.3). -/ def polN1 (r v u : K) : K := 12+12*r^2-3*u+6*r^2*u+r^4*u+12*r*v-4*r^3*v /-- The long factor of (3.4). -/ def polP4 (r v u : K) : K := 9+3*r^2-6*u+6*r^2*u+12*r*v+6*r*u*v+2*r^3*u*v+3*v^2-3*r^2*v^2 /-- The long factor of (3.5). -/ def polP5 (r v u : K) : K := 6-9*u+3*r^2*u+6*r*v+12*r*u*v+6*v^2+3*u*v^2+3*r^2*u*v^2-2*r*v^3 /-- The long factor of (3.6). -/ def polW4 (r v u : K) : K := -3+12*u-12*r*u*v-6*v^2-12*u*v^2-4*r*u*v^3+v^4 /-- In normalised coordinates `a11 = l u r`, `a12 = l v`, `a21 = l u`, `a22 = l` the six coefficients are exactly the left-hand sides of Yu–Zhu (3.2)–(3.7). -/ theorem skew_normalised (t l u r v : K) : skewE0 t (l*u*r) (l*v) (l*u) l = l^4 * u^4 * polR r ∧ skewE1 t (l*u*r) (l*v) (l*u) l = -t - l^4 * u^3 * polN1 r v u ∧ skewE2 t (l*u*r) (l*v) (l*u) l = -2 * l^4 * u^2 * polP4 r v u ∧ skewE3 t (l*u*r) (l*v) (l*u) l = -2 * l^4 * u * polP5 r v u ∧ skewE4 t (l*u*r) (l*v) (l*u) l = 1 + l^4 * polW4 r v u ∧ skewE5 t (l*u*r) (l*v) (l*u) l = -l^4 * polV v := by refine ⟨?_, ?_, ?_, ?_, ?_, ?_⟩ <;> · simp only [skewE0, skewE1, skewE2, skewE3, skewE4, skewE5, polR, polN1, polP4, polP5, polW4, polV] ring """) be4, be5, res = P("res", "be4"), P("res", "be5"), P("res", "Res") w(f"""/-- The `u`-free combination `β₅ P₄ - β₄ P₅` (both long factors are linear in `u`). -/ def polRes (r v : K) : K := {res} /-- **The four good pairs.** If (3.4), (3.5) hold and `R(r) = V(v) = 0`, then `r² + 2rv - 3 = 0`. (Magma: the resultant `Res` and then `r² + 2rv - 3 ∈ (Res, R, V)`.) -/ theorem good_pair {{r v u : K}} (h4 : polP4 r v u = 0) (h5 : polP5 r v u = 0) (hR : polR r = 0) (hV : polV v = 0) : r ^ 2 + 2 * r * v - 3 = 0 := by have hRes : polRes r v = 0 := by simp only [polRes, polP4, polP5] at h4 h5 ⊢ linear_combination {be5} * h4 - {be4} * h5 simp only [polRes, polR, polV] at hRes hR hV linear_combination {P("good_pair_small", "a")} * hRes + {P("good_pair_small", "cR")} * hR + {P("good_pair_small", "cV")} * hV """) for nm, lhs, rhs, doc in [ ("N1_mod", "polN1 r v u", "12 * (3 + r ^ 2 + r ^ 2 * u)", "(3.3): `N₁ ≡ 12 (3 + r² + r² u)`"), ("W4_mod", "polW4 r v u", "12 * (6 - r ^ 2 + (9 - r ^ 2) * u)", "(3.6): `W₄ ≡ 12 (6 - r² + (9 - r²) u)`"), ("P4_mod", "polP4 r v u", "0", "(3.4) vanishes identically on good pairs"), ("P5_mod", "polP5 r v u", "0", "(3.5) vanishes identically on good pairs"), ("V_mod", "polV v", "0", "(3.7) vanishes identically on good pairs"), ]: w(f"""/-- Modulo `R(r) = 0` and `r² + 2rv - 3 = 0`: {doc}. -/ theorem {nm} {{r v u : K}} (hR : polR r = 0) (hG : r ^ 2 + 2 * r * v - 3 = 0) : {lhs} = {rhs} := by simp only [polR, polN1, polP4, polP5, polW4, polV] at hR ⊢ linear_combination {P(nm, "cR")} * hR + {P(nm, "cG")} * hG """) # separability certificates w(f"""/-! ## Simple roots -/ /-- `R(r) = r⁴ - 6r² - 3` has only simple roots. -/ theorem R_simple {{r : K}} (h : r ^ 4 - 6 * r ^ 2 - 3 = 0) (h' : 4 * r ^ 3 - 12 * r = 0) : False := by have : (1 : K) = 0 := by linear_combination {P("sep_R", "a")} * h + {P("sep_R", "b")} * h' exact one_ne_zero this /-- `c⁴ + 6c² - 3` has only simple roots. -/ theorem Pinf_simple {{c : K}} (h : c ^ 4 + 6 * c ^ 2 - 3 = 0) (h' : 4 * c ^ 3 + 12 * c = 0) : False := by have : (1 : K) = 0 := by linear_combination {P("sep_Pinf", "a", {"r": "c"})} * h + {P("sep_Pinf", "b", {"r": "c"})} * h' exact one_ne_zero this /-- For a root `r` of `R`, the quartic `Q_r(u) = r²u⁴ + (3+r²)u³ - t(9-r²)u - t(6-r²)` has only simple roots unless `t ∈ {{0, 1}}` (Magma: the elimination ideal of `(Q, Q', R)` is `(t² - t)`). -/ theorem Q_simple {{r t u : K}} (hR : r ^ 4 - 6 * r ^ 2 - 3 = 0) (h : r ^ 2 * u ^ 4 + (3 + r ^ 2) * u ^ 3 - t * (9 - r ^ 2) * u - t * (6 - r ^ 2) = 0) (h' : 4 * r ^ 2 * u ^ 3 + 3 * (3 + r ^ 2) * u ^ 2 - t * (9 - r ^ 2) = 0) : t ^ 2 - t = 0 := by linear_combination {P("sep_Q", "a")} * h + {P("sep_Q", "b")} * h' + {P("sep_Q", "e")} * hR /-- `B(μ) = μ⁴ - tμ` has only simple roots unless `t = 0`. -/ theorem B_simple {{mu t : K}} (h : mu ^ 4 - t * mu = 0) (h' : 4 * mu ^ 3 - t = 0) : {P("sep_B", "M")} = 0 := by linear_combination {P("sep_B", "a")} * h + {P("sep_B", "b")} * h' /-- For a root `μ` of `B`, the quartic `q_μ(c) = (1-μ)c⁴ - 6(1+μ)c² - 3(1-μ)` has only simple roots unless `t = 1`. -/ theorem q_simple {{mu cc t : K}} (hB : mu ^ 4 - t * mu = 0) (h : (1 - mu) * cc ^ 4 - 6 * (1 + mu) * cc ^ 2 - 3 * (1 - mu) = 0) (h' : 4 * (1 - mu) * cc ^ 3 - 12 * (1 + mu) * cc = 0) : {P("sep_q", "M")} = 0 := by linear_combination {P("sep_q", "a")} * h + {P("sep_q", "b")} * h' + {P("sep_q", "e")} * hB /-- For a root `μ` of `B`, `(1 - μ) · h = 1 - t`: the leading coefficient of `q_μ` is nonzero when `t ≠ 1`. -/ theorem lead_q {{mu t : K}} (hB : mu ^ 4 - t * mu = 0) : (1 - mu) * {P("lead_q", "h")} = 1 - t := by linear_combination -{P("lead_q", "e")} * hB """) # lines meeting L1 w("""/-! ## Lines meeting `L₁` -/ """) for k in range(6): w(f"/-- Coefficient of `α^{5-k} β^{k}` in `F_t(α(c1,c2,0,0) + β(d1,d2,1,μ))`. -/") w(f"def planeF{k} (t c1 c2 d1 d2 mu : K) : K :=\n {F_pl[k]}\n") w("""theorem plane_expand (t c1 c2 d1 d2 mu a b : K) : Fv t (a * c1 + b * d1) (a * c2 + b * d2) b (mu * b) = planeF0 t c1 c2 d1 d2 mu * a ^ 5 + planeF1 t c1 c2 d1 d2 mu * a ^ 4 * b + planeF2 t c1 c2 d1 d2 mu * a ^ 3 * b ^ 2 + planeF3 t c1 c2 d1 d2 mu * a ^ 2 * b ^ 3 + planeF4 t c1 c2 d1 d2 mu * a * b ^ 4 + planeF5 t c1 c2 d1 d2 mu * b ^ 5 := by simp only [Fv, planeF0, planeF1, planeF2, planeF3, planeF4, planeF5] ring """) w("""/-- Change of variables `y = αc + βd` (`δ = c1 d2 - c2 d1`): the five coefficients of `δ⁴ Q_μ(y) + κ (c1 y₂ - c2 y₁)⁴`, `κ = μ(μ³ - t)`, are combinations of the coefficients of `F_t(αc + β(d, 1, μ))`. If the latter vanish, so do the former. -/ theorem plane_change {t c1 c2 d1 d2 mu : K} (h1 : planeF1 t c1 c2 d1 d2 mu = 0) (h2 : planeF2 t c1 c2 d1 d2 mu = 0) (h3 : planeF3 t c1 c2 d1 d2 mu = 0) (h4 : planeF4 t c1 c2 d1 d2 mu = 0) (h5 : planeF5 t c1 c2 d1 d2 mu = 0) :""") lhs_list = [P(f"plane_y{j}", "lhs") for j in range(5)] w(" " + " ∧\n ".join(f"{l} = 0" for l in lhs_list) + " := by") w(" simp only [planeF1, planeF2, planeF3, planeF4, planeF5] at h1 h2 h3 h4 h5") w(" refine ⟨?_, ?_, ?_, ?_, ?_⟩") for j in range(5): terms = " + ".join(f"{P(f'plane_y{j}', f'm{k}')} * h{k+1}" for k in range(5)) w(f" · linear_combination {terms}") w("") for k in range(6): w(f"/-- Coefficient of `α^{5-k} β^{k}` in `F_t(α(c1,c2,0,0) + β(d1,d2,0,1))`. -/") w(f"def infF{k} (t c1 c2 d1 d2 : K) : K :=\n {F_in[k]}\n") w("""theorem inf_expand (t c1 c2 d1 d2 a b : K) : Fv t (a * c1 + b * d1) (a * c2 + b * d2) 0 b = infF0 t c1 c2 d1 d2 * a ^ 5 + infF1 t c1 c2 d1 d2 * a ^ 4 * b + infF2 t c1 c2 d1 d2 * a ^ 3 * b ^ 2 + infF3 t c1 c2 d1 d2 * a ^ 2 * b ^ 3 + infF4 t c1 c2 d1 d2 * a * b ^ 4 + infF5 t c1 c2 d1 d2 * b ^ 5 := by simp only [Fv, infF0, infF1, infF2, infF3, infF4, infF5] ring /-- In the plane `x₃ = 0`: if all coefficients vanish then `δ⁴ = (c1 d2 - c2 d1)⁴ = 0`. -/ theorem inf_change {t c1 c2 d1 d2 : K} (h1 : infF1 t c1 c2 d1 d2 = 0) (h2 : infF2 t c1 c2 d1 d2 = 0) (h3 : infF3 t c1 c2 d1 d2 = 0) (h4 : infF4 t c1 c2 d1 d2 = 0) (h5 : infF5 t c1 c2 d1 d2 = 0) :""") w(f" {P('inf_y0', 'lhs')} = 0 := by") w(" simp only [infF1, infF2, infF3, infF4, infF5] at h1 h2 h3 h4 h5") # inf: g_k = -f_{k+1} terms = " + ".join(f"(-{P('inf_y0', f'm{k}')}) * h{k+1}" for k in range(5)) w(f" linear_combination {terms}") w("\nend Quintic85.Cert") print("\n".join(out))