True/false tests without three equal answers in a row: keys, surplus, teachers, transfer matrices
DefinitionMiyadera2026_DefsAn answer key is , stored as a Boolean vector; it is admissible if no three consecutive answers are equal, and is the set of admissible keys. The surplus is . For a weight on the positive integers, ; is the case and . The score of a strategy is the total weighted number of correct guesses over the keys with both habits, and a strategy is uniquely optimal if every other strategy scores strictly less. The pattern answers false exactly at questions and . Also defined: Fibonacci and Lucas numbers on , the function of equation (5), the matrices , , the generating function and its coefficients, the predicate that is the coefficient function of , the transfer data , , of Lemma 7.2, and , , of Section 7.5. Questions are counted from in Lean: question below is the paper's question .
import Mathlib
/-!
# A habit of a teacher
Definitions for R. Miyadera, "A habit of a teacher: an exact formula for a weighted version and a
computer-assisted proof of the original puzzle", arXiv:2612.00079v1 (2026), Sections 2 to 7.
Conventions. An answer key is `ε : Fin n → Bool` (`true` = the statement is true); its value
`εⱼ ∈ {1, -1}` is `sgn (ε j)`. Positions are counted from `0`: question `i : Fin n` is the paper's
question `i + 1`, so the paper's `L = i - 1` and `M = n - i` are `i.val` and `n - 1 - i.val`.
-/
namespace Miyadera2026
open Finset
/-- The value of an answer: `true ↦ 1`, `false ↦ -1`. -/
def sgn (b : Bool) : ℤ := if b then 1 else -1
/-- Definition 2.1: no three equal answers in a row. -/
def IsAdmissible {n : ℕ} (ε : Fin n → Bool) : Prop :=
∀ j : Fin n, ∀ h : j.val + 2 < n,
¬ (ε j = ε ⟨j.val + 1, by omega⟩ ∧ ε ⟨j.val + 1, by omega⟩ = ε ⟨j.val + 2, h⟩)
instance {n : ℕ} : DecidablePred (@IsAdmissible n) := fun ε => by
unfold IsAdmissible; infer_instance
/-- The set `Aₙ` of admissible answer keys. -/
def adm (n : ℕ) : Finset (Fin n → Bool) := univ.filter IsAdmissible
/-- The surplus `d(ε) = ε₁ + ⋯ + εₙ` of true answers. -/
def surplus {n : ℕ} (ε : Fin n → Bool) : ℤ := ∑ j, sgn (ε j)
/-- The answer keys allowed by both habits: admissible with `d(ε) > 0`. -/
def keys (n : ℕ) : Finset (Fin n → Bool) := (adm n).filter (fun ε => 0 < surplus ε)
/-- Equation (2): `D^w_i(n) = ∑_{ε ∈ Aₙ, d(ε) > 0} w(d(ε)) εᵢ` for a weight `w` on the positive
integers. -/
noncomputable def Dw (w : ℕ → ℝ) (n : ℕ) (i : Fin n) : ℝ :=
∑ ε ∈ keys n, w (surplus ε).toNat * (sgn (ε i) : ℝ)
/-- `Dᵢ(n)`, the original puzzle (`w ≡ 1`). -/
def D (n : ℕ) (i : Fin n) : ℤ := ∑ ε ∈ keys n, sgn (ε i)
/-- Equation (4): `Wᵢ(n) = ∑_{ε ∈ Aₙ} d(ε) εᵢ`. -/
def W (n : ℕ) (i : Fin n) : ℤ := ∑ ε ∈ adm n, surplus ε * sgn (ε i)
/-- Total weighted number of correct guesses of the strategy `s` against the `w`-teacher; the
expected number of correct guesses is this divided by the positive constant `∑ w(d(ε))`. -/
noncomputable def score (w : ℕ → ℝ) (n : ℕ) (s : Fin n → Bool) : ℝ :=
∑ ε ∈ keys n, w (surplus ε).toNat * ((univ.filter fun i => s i = ε i).card : ℝ)
/-- `s` is the unique optimal strategy against the `w`-teacher. -/
def IsUniqueOptimal (w : ℕ → ℝ) (n : ℕ) (s : Fin n → Bool) : Prop :=
∀ t : Fin n → Bool, t ≠ s → score w n t < score w n s
/-- Strategy (1): "false" exactly at the third and at the `(n-2)`-nd question. -/
def pattern (n : ℕ) : Fin n → Bool := fun i => !(i.val == 2 || i.val + 3 == n)
/-- Fibonacci numbers on `ℤ`, extended to negative indices by the same recurrence:
`F₋ₖ = (-1)^(k+1) Fₖ`. -/
def fibZ (k : ℤ) : ℤ :=
if 0 ≤ k then (Nat.fib k.toNat : ℤ) else (-1) ^ (k.natAbs + 1) * (Nat.fib k.natAbs : ℤ)
/-- Lucas numbers `Lₖ = Fₖ₋₁ + Fₖ₊₁` (so `L₀ = 2`, `L₁ = 1`). -/
def lucas (k : ℤ) : ℤ := fibZ (k - 1) + fibZ (k + 1)
/-- Equation (5). -/
def g (m x : ℕ) : ℤ :=
if m % 3 = 0 then lucas (x + 1) else if m % 3 = 1 then fibZ ((x : ℤ) - 1) else -fibZ (x + 3)
/-- The matrix `B` (unweighted step). -/
def matB : Matrix (Fin 2) (Fin 2) ℤ := !![1, 1; 1, 0]
/-- The matrix `A` (weighted step). -/
def matA : Matrix (Fin 2) (Fin 2) ℤ := !![-1, -1; 1, 0]
/-- `e₁ = (1, 0)ᵀ`. -/
def e1 : Fin 2 → ℤ := ![1, 0]
/-- `1₂ = (1, 1)`. -/
def one2 : Fin 2 → ℤ := ![1, 1]
/-- The generating function `Gᵢ(z) = ∑_{ε ∈ Aₙ} εᵢ z^{d(ε)}`, evaluated at `z ∈ ℂ`. -/
noncomputable def G (n : ℕ) (i : Fin n) (z : ℂ) : ℂ :=
∑ ε ∈ adm n, (sgn (ε i) : ℂ) * z ^ surplus ε
/-- The coefficient of `z^k` in `Gᵢ(z)`. -/
def gcoef (n : ℕ) (i : Fin n) (k : ℤ) : ℤ :=
∑ ε ∈ (adm n).filter (fun ε => surplus ε = k), sgn (ε i)
/-- `h` is the coefficient function of the Laurent polynomial `Hᵢ` of Lemma 6.1: finitely
supported, symmetric, and `Gᵢ(z) = (z - z⁻¹) Hᵢ(z)`, i.e. `[z^k] Gᵢ = h(k-1) - h(k+1)`. -/
def IsH (n : ℕ) (i : Fin n) (h : ℤ → ℤ) : Prop :=
(∃ N : ℕ, ∀ k : ℤ, (N : ℤ) < |k| → h k = 0) ∧ (∀ k, h (-k) = h k) ∧
∀ k, gcoef n i k = h (k - 1) - h (k + 1)
/-- States of the `4 × 4` transfer matrix, ordered `(+,1), (+,2), (-,1), (-,2)`. -/
def stSign : Fin 4 → Bool := ![true, true, false, false]
/-- Run length of a state. -/
def stRun : Fin 4 → ℕ := ![1, 2, 1, 2]
/-- Admissible transitions: `(σ,1) → (σ,2)` and `(σ,r) → (-σ,1)`. -/
def stTrans (a b : Fin 4) : Prop :=
(stRun a = 1 ∧ stSign b = stSign a ∧ stRun b = 2) ∨ (stSign b ≠ stSign a ∧ stRun b = 1)
instance : DecidableRel stTrans := fun a b => by unfold stTrans; infer_instance
/-- `T(z)`: entry `z^{σ'}` on admissible transitions into a state with answer `σ'`. -/
noncomputable def matT (z : ℂ) : Matrix (Fin 4) (Fin 4) ℂ :=
fun a b => if stTrans a b then (if stSign b then z else z⁻¹) else 0
/-- `a(z) = (z, 0, z⁻¹, 0)`. -/
noncomputable def vecA (z : ℂ) : Fin 4 → ℂ := ![z, 0, z⁻¹, 0]
/-- `E = diag(1, 1, -1, -1)`. -/
noncomputable def matE : Matrix (Fin 4) (Fin 4) ℂ := Matrix.diagonal ![1, 1, -1, -1]
/-- The golden ratio `φ = (1 + √5)/2`. -/
noncomputable def phi : ℝ := (1 + Real.sqrt 5) / 2
/-- `γ₀ = φ`, `γ₁ = 1/(√5 φ)`, `γ₂ = -φ³/√5` (Section 7.5). -/
noncomputable def gam (r : ℕ) : ℝ :=
if r % 3 = 0 then phi else if r % 3 = 1 then 1 / (Real.sqrt 5 * phi) else -phi ^ 3 / Real.sqrt 5
/-- `A(L) = (1 - (-1)^L φ^{-2L})/5 + γ_{L mod 3}/(2 φ^L)`. -/
noncomputable def Acoef (L : ℕ) : ℝ :=
(1 - (-1) ^ L * phi⁻¹ ^ (2 * L)) / 5 + gam L / (2 * phi ^ L)
end Miyadera2026
Read-back
What the Lean code literally says, in plain math · claude-opus-5-5
Answer keys are maps eps : Fin n -> Bool, valued +1 (true) / -1 (false) via sgn. IsAdmissible: no 0-based position j with j+2<n has eps j = eps (j+1) = eps (j+2) (no three equal consecutive answers). adm n = all admissible keys (Finset). surplus eps = sum_j sgn(eps j) in Z. keys n = admissible keys with surplus > 0. Dw w n i = sum over keys of w(toNat surplus) * sgn(eps i) in R. D n i = sum over keys of sgn(eps i) in Z. W n i = sum over ALL admissible keys of surplus * sgn(eps i). score w n s = sum over keys of w(toNat surplus) * #{i : s i = eps i}. IsUniqueOptimal w n s: every strategy t different from s has strictly smaller score. pattern n i = false exactly when i = 2 or i + 3 = n (0-based positions 2 and n-3, i.e. paper questions 3 and n-2). fibZ = Fibonacci extended by F_{-k} = (-1)^(k+1) F_k; lucas k = F_{k-1}+F_{k+1}; g m x = L_{x+1}, F_{x-1}, or -F_{x+3} according as m mod 3 = 0, 1, 2. matB = [[1,1],[1,0]], matA = [[-1,-1],[1,0]], e1 = (1,0), one2 = (1,1). G n i z = sum over adm of sgn(eps i) * z^surplus (integer power in C). gcoef n i k = sum of sgn(eps i) over admissible eps with surplus k. IsH n i h: h : Z -> Z has finite support, is even (h(-k) = h k), and gcoef n i k = h(k-1) - h(k+1) for all k. Transfer matrix matT z on 4 states (+,1),(+,2),(-,1),(-,2): entry (a,b) is z (b positive) or z^-1 (b negative) when b is reachable from a ((s,1)->(s,2) or any (s,r)->(-s,1)), else 0. vecA z = (z,0,z^-1,0), matE = diag(1,1,-1,-1). phi = (1+sqrt 5)/2; gam r = phi, 1/(sqrt5 phi), -phi^3/sqrt5 by r mod 3; Acoef L = (1 - (-1)^L phi^-(2L))/5 + gam L/(2 phi^L).
Auditor concerns: none of substance. Checked by #eval: W 11, D 11, pattern 11, card adm 9 = 110, fibZ(-4..3) = [-3,2,-1,1,0,1,1,2], lucas 0..5 = [2,1,3,4,7,11], the stTrans table, all as intended. Minor: Dw/score apply w to Int.toNat of the surplus, which is harmless because keys only contain surplus > 0, so w 0 is never used. pattern n puts both falses on the same position when n = 5 and has only one false for n < 3 (irrelevant: every pattern theorem has n >= 7). For n < 3 IsAdmissible is vacuous (all 2^n keys admissible), consistent with 2 F_{n+1}. G at z = 0 is degenerate (0^0 = 1, 0^-1 = 0 in Lean); the only theorem using G assumes z != 0. IsH is satisfiable (the Laurent quotient exists, see H_exists).
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.