NIM paper · interactive edition Interactive edition · paper, explanations and Lean proofs.

Theorem 5.12 (iii)From a known zero-nim-sum predecessor a two-frame AC0 selector restores zero and maintains it

Checked theorem Lean checks a formal statement corresponding to this claim.

1 Paper text

Theorem 5.12 (Restricted bound and bypasses) The following statements hold under Definition 5.7.

  1. (i) For each fixed B≥2B\ge2, let PP be uniform on {0,…,2B−1}N\{0,\ldots,2^B-1\}^N conditional on σ(P)≠0\sigma(P)\ne0. For one fixed structured head πj\pi_j and an AC0\AC^0 readout FN(P,ROπj(P))F_N(P,\mathrm{RO}_{\pi_j}(P)), winning-move success is at most 22B−1+negl⁡(N).\frac{2}{2^B-1}+\operatorname{negl}(N). Here N≥1N\ge1, and a negligible error is a nonnegative sequence smaller than every inverse polynomial for sufficiently large NN, with circuit depth, polynomial size exponent and BB fixed. More precisely, a uniform parity-correlation bound ηN\eta_N for the fixed-bit readout indicators gives the finite upper bound 2/(2B−1)+(2B−1)ηN2/(2^B-1)+(2^B-1)\eta_N; the indicators and correlation premise are specified in Appendix H. The leading constant is asymptotically attainable by guessing one nonzero nim-sum in each channel fibre.

  2. (ii) For any BB, the BB structured heads and an AC0\AC^0 readout select an optimal move using O(BN2B)O(BN2^B) rollout steps, as in Proposition 5.9.

  3. (iii) Given a legal transition from a known zero-nim-sum predecessor, a two-frame AC0\AC^0 selector restores zero. Once a player has established that invariant, it maintains it against every legal opponent response. This statement does not supply the initial winning move from an arbitrary position.

Generated from paper_jmlr.tex, lines 802–814.

2 Explain this

Given a legal opponent move from a known zero-nim-sum board, a shallow two-frame selector restores nim-sum zero. A player who keeps doing this after each opponent move maintains the invariant and wins. This part does not supply the first winning move from an arbitrary position.

Two-frame restoration, step by step

Exact construction trace Every value is computed from the construction’s definitions for the example shown.

This is the checked two-frame selector run on one example, gate group by gate group. It starts from the two frames of Figure 2 and ends with the binary output fields shown in Figure 7. Every value below is computed from the selector's definition for the boards shown. Choose another example to see several candidates at once, the fallback, or a terminal board. The two examples marked "outside the promise" do not start from a zero-nim-sum board after a legal move, so the guarantee does not apply to them.

Inside the promise. The earlier board has nim-sum 0 and the opponent decreased one heap, so the restoration theorem guarantees that the output restores nim-sum 0.

Earlier board Q (3, 5, 6)nim-sum 0
012
Current board Q′ (3, 4, 6)nim-sum 1; changed: heap 1
012
After the reply (2, 4, 6)nim-sum 0; reply in heap 0
012
  1. Compare the frames bit by bit

    Each heap is a 3-bit block. For every heap and every bit position, one two-input XOR compares Q with Q′, all in parallel.

    HeapQ Q′ XORHeap
    03 0113 011000same
    15 1014 100001changed
    26 1106 110000same
  2. Extract the difference δ

    Bit k of δ is the OR of column k of the XOR table: 001 = δ = 1.

    At most one heap changed, so at most one row is nonzero and the OR equals the XOR of the rows, which is nim(Q) ⊕ nim(Q′).

  3. Propose replacements and test them in parallel

    For every heap at once: replace Q′i by Q′i ⊕ δ and test whether that is a decrease. The decisive bit is the highest set bit of δ (weight 1): a heap passes exactly when it has that bit set.

    HeapQ′ Q′ ⊕ δTest
    03 0112 010passes: 2 < 3
    14 1005 101fails: 5 > 4
    26 1107 111fails: 7 > 6
    Show the bit comparisons

    The test x ⊕ δ < x is a fixed-depth circuit: an OR over the possible highest differing bit, requiring equality above it, a 1 in x and a 0 in the replacement at that bit.

    • Heap 0: 011 vs 010 — the highest differing bit has weight 1; the heap has 1 there, so the replacement is smaller.
    • Heap 1: 100 vs 101 — the highest differing bit has weight 1; the heap has 0 there, so the replacement is larger.
    • Heap 2: 110 vs 111 — the highest differing bit has weight 1; the heap has 0 there, so the replacement is larger.

    Candidates passing: 1.

  4. Choose between cancelling and the fallback

    cancelling move At least one replacement passes, so the flags are the passing tests and the fallback is masked off.

    Heappassesnonzerofallback valueflag si
    0113 − 1 = 21
    1014 − 1 = 30
    2016 − 1 = 50
  5. Encode the first flagged heap as a binary index

    For N = 3 heaps the construction uses r = ⌊log2 N⌋ + 1 = 2 index bits (the encoder itself needs only N ≤ 2r). The first flag is at heap 0, so the index bits are 00.

    Show the priority encoder

    fi = si ∧ ¬sj for all j < i keeps only the first flag; index bit k is the OR of fi over the heaps i whose binary index has a 1 at bit k. This is a fixed-depth circuit, not a search.

    Heap ii in binarysifirst fi
    00011
    10100
    21000

    Index bits (OR of the fi in each 1-column): 00

  6. Output the move

    validity1index (2 bits)00value (3 bits)010

    Decoded move: set heap 0 to 2. New board (2, 4, 6), nim-sum 0.

How this relates to the Lean proof. The selector is Family.restS (B, N): the local difference circuits LocalRestore.dC, the candidate tests of SelectCircuit.varSel and the priority encoder PriorityEncoder.encode, read on the two-frame input LocalRestore.tf. Family.rest_semantics proves that, on the promised domain, these formulas and the compiled network output a legal move that restores nim-sum 0. Outside the promise the circuit still computes the values shown, and no theorem says that the result restores 0.

What this does not show. The selector does not find a first winning move from an arbitrary board. Given a zero-nim-sum predecessor it maintains the invariant (Theorem 5.12(iii)). The boxes of Figure 7 group operations; the stages here group gates, not network layers.

Depicted in Lean: Family.rest_semantics, LocalRestore.restore_equiv, LocalRestore.localDelta_eq, LocalRestore.restC_eval, SelectCircuit.varSel_correct, AttainCircuit.cancel_iff_bit, PriorityEncoder.encode_correct, Family.rest_ac0, Family.rest_net. Explains the paper’s Figure 2, Figure 7.

Computed in your browser by a short re-implementation of the definitions above (tools/visuals/nimvis-core.js). It is not itself checked by Lean. At build time it agreed with Lean's own evaluation of those definitions on all 12,299 reference cases. Lean's evaluation fixes the output wires, the difference δ and the decoded move; the intermediate tables (flags, first-flag bits) are re-derived from the same definitions.

3 How this is formalised

Game.restore_correct (the restoration selector returns a legal move to nim-sum zero under the promise), LocalRestore.restore_equiv (the same move from the local circuit) and WholeGame.twoFrame_wins (the stateful whole-game guarantee). See Section 5.3.1, paragraph 2.

Coverage record
Paper anchorthm:search-gap (manuscript label)
Displayed asTheorem 5.12 (iii)
ClaimFrom a known zero-nim-sum predecessor a two-frame AC0 selector restores zero and maintains it
Coverage categoryChecked theorem
Statement sourcepaper_jmlr.tex lines 802–814; SHA-256 of the normalised block source 6a8c9a4107356a14…
Lean declarations
Depends onProposition 5.5, Section 5.3.1, paragraph 2
Audited correspondence rowsthm:search-gap
Explanationsexplain/thm:search-gap.iii, formal/thm:search-gap.iii
Build evidencefresh isolated rebuild, audit report

4 Lean statement and source

Principal declarations

theorem Game.restore_correct

Module NimGame (lean/phase2/NimGame.lean, lines 412–425) · file SHA-256 23df6bb87ecfd344… Axioms reported by the fresh build: propext, Quot.sound

Statement as elaborated and printed by Lean 4.26.0

∀ {Q Q' : List ℕ},
  NimAudit.nimSum Q = 0 →
    NimAudit.Game.Move Q Q' →
      ∃ R, NimAudit.Game.restore Q Q' = some R ∧ NimAudit.Game.Move Q' R ∧ NimAudit.nimSum R = 0
Exact source text, lines 412–425
412theorem restore_correct {Q Q' : List Nat} (h0 : nimSum Q = 0) (hm : Move Q Q') :413    ∃ R, restore Q Q' = some R ∧ Move Q' R ∧ nimSum R = 0 := by414  have hne := move_from_zero hm h0415  have hd : nimSum Q ^^^ nimSum Q' = nimSum Q' := by rw [h0, Nat.zero_xor]416  obtain ⟨R, hsel, hmR, hR⟩ := selectMove_zero hne417  refine ⟨R, ?_, hmR, hR⟩418  unfold restore419  rw [hd]420  simp only [selectMove, hne, if_false] at hsel421  split at hsel422  · next q' hq' => cases hsel; exact hq'423  · next _ hnone =>424    obtain ⟨pre, x, post, hp, hlt, _⟩ := exists_decreasing_heap Q' hne425    exact absurd hlt (cancelFirst_none hnone x (by rw [hp]; simp))

Project declarations in the statement: Game.Move, Game.restore, nimSum

Project declarations used by the proof or definition body

Game.Move, Game.cancelFirst, Game.cancelFirst_none, Game.move_from_zero, Game.restore, Game.selectMove, Game.selectMove_zero, Rollout.exists_decreasing_heap, Rollout.subFirstPos, nimSum

theorem LocalRestore.restore_equiv

Module LocalRestore (lean/phase4/LocalRestore.lean, lines 208–229) · file SHA-256 fc4c7f908b0c9c58… Axioms reported by the fresh build: propext, Quot.sound

LOCAL RESTORATION EQUIVALENCE: under the zero-predecessor / legal-transition promise (nim(Q) = 0 and Q → Q' a legal move), the purely local circuit outputs a move (i, v) whose application is exactly `NimGame.restore Q Q'`; the move is legal and restores nim-sum 0.

Statement as elaborated and printed by Lean 4.26.0

∀ {B N : ℕ} {Q Q' : List ℕ},
  Q.length = N →
    Q'.length = N →
      NimAudit.Game.Width B Q →
        NimAudit.Game.Width B Q' →
          NimAudit.nimSum Q = 0 →
            NimAudit.Game.Move Q Q' →
              ∃ i v,
                NimAudit.SelectCircuit.selReadout (NimAudit.LocalRestore.restC B N)
                      (NimAudit.LocalRestore.tf B N Q Q') =
                    some (i, v) ∧
                  NimAudit.Game.restore Q Q' = some (Q'.set i v) ∧
                    NimAudit.Game.Move Q' (Q'.set i v) ∧ NimAudit.nimSum (Q'.set i v) = 0
Exact source text, lines 208–229
208/-- LOCAL RESTORATION EQUIVALENCE: under the zero-predecessor / legal-transition209    promise (nim(Q) = 0 and Q → Q' a legal move), the purely local circuit outputs a210    move (i, v) whose application is exactly `NimGame.restore Q Q'`; the move is legal211    and restores nim-sum 0. -/212theorem restore_equiv {B N : Nat} {Q Q' : List Nat} (hQ : Q.length = N) (hQ' : Q'.length = N)213    (hw : Width B Q) (hw' : Width B Q') (h0 : nimSum Q = 0) (hm : Move Q Q') :214    ∃ i v, selReadout (restC B N) (tf B N Q Q') = some (i, v) ∧215      restore Q Q' = some (Q'.set i v) ∧ Move Q' (Q'.set i v) ∧ nimSum (Q'.set i v) = 0 := by216  rw [restC_eval hQ hQ' hw', localDelta_eq hQ hQ' hw hw' (oneChange_of_move hm)]217  obtain ⟨R, hR, hmR, hzR⟩ := restore_correct h0 hm218  have hR' := hR219  unfold restore at hR'220  rw [cancelFirst_eq] at hR'221  cases hc : cancelIdx (nimSum Q ^^^ nimSum Q') Q' with222  | none => rw [hc] at hR'; cases hR'223  | some m =>224    obtain ⟨i, v⟩ := m225    rw [hc] at hR'226    simp only [applyM, Option.some.injEq] at hR'227    subst hR'228    refine ⟨i, v, ?_, hR, hmR, hzR⟩229    unfold attainT; rw [hc]

Project declarations in the statement: Game.Move, Game.Width, Game.restore, LocalRestore.restC, LocalRestore.tf, SelectCircuit.selReadout, nimSum

Project declarations used by the proof or definition body

Attain.cancelIdx, Attain.decIdx, AttainCircuit.attainT, Game.Move, Game.Width, Game.cancelFirst, Game.restore, Game.restore_correct, LocalRestore.cancelFirst_eq, LocalRestore.localDelta, LocalRestore.localDelta_eq, LocalRestore.oneChange_of_move, LocalRestore.restC, LocalRestore.restC_eval, LocalRestore.tf, SelectCircuit.applyM, SelectCircuit.selReadout, nimSum

theorem WholeGame.twoFrame_wins

Module WholeGame (lean/phase5/WholeGame.lean, lines 75–96) · file SHA-256 809a74ad589db609… Axioms reported by the fresh build: propext, Classical.choice, Quot.sound

TWO-FRAME WHOLE-GAME THEOREM: from an established zero-nim-sum stored board (opponent to move), the network-driven two-frame player answers every legal opponent move legally, keeps the zero invariant, the board length and the bit width, and every play terminates with the player making the last move. Includes terminal and empty boards (no opponent move exists).

Statement as elaborated and printed by Lean 4.26.0

∀ {B : ℕ} (Q : List ℕ),
  NimAudit.Game.Width B Q →
    NimAudit.nimSum Q = 0 → NimAudit.WholeGame.TwoFrameWins (NimAudit.WholeGame.restReply B) Q
Exact source text, lines 75–96
75/-- TWO-FRAME WHOLE-GAME THEOREM: from an established zero-nim-sum stored board (opponent76    to move), the network-driven two-frame player answers every legal opponent move77    legally, keeps the zero invariant, the board length and the bit width, and every play78    terminates with the player making the last move.  Includes terminal and empty79    boards (no opponent move exists). -/80theorem twoFrame_wins {B : Nat} : ∀ (Q : List Nat), Width B Q → nimSum Q = 0 →81    TwoFrameWins (restReply B) Q := by82  intro Q83  induction Q using (measure List.sum).wf.induction with84  | h Q ih =>85    intro hw h086    refine TwoFrameWins.intro Q (fun Q' hm => ?_) (fun Q' R hm hR => ?_) (fun Q' R hm hR => ?_)87    · obtain ⟨R, h1, _⟩ := restReply_step hw h0 hm88      rw [h1]; rfl89    · obtain ⟨R', h1, _, h3, _⟩ := restReply_step hw h0 hm90      rw [h1] at hR; cases hR; exact h391    · obtain ⟨R', h1, _, h3, h4, _, h6⟩ := restReply_step hw h0 hm92      have hRR : R' = R := by rw [h1] at hR; exact Option.some.inj hR93      subst hRR94      refine ih R' ?_ h6 h495      have := move_sum_lt hm; have := move_sum_lt h396      show R'.sum < Q.sum; omega

Project declarations in the statement: Game.Width, WholeGame.TwoFrameWins, WholeGame.restReply, nimSum

Project declarations used by the proof or definition body

Game.Move, Game.Width, Game.move_sum_lt, Game.restore, WholeGame.TwoFrameWins, WholeGame.TwoFrameWins.intro, WholeGame.restReply, WholeGame.restReply_step, nimSum

Definitions these statements use

def Game.Move

Module NimGame (lean/phase2/NimGame.lean, lines 24–26) · file SHA-256 23df6bb87ecfd344…

An ordinary legal Nim move: exactly one heap strictly decreases.

Statement as elaborated and printed by Lean 4.26.0

List ℕ → List ℕ → Prop
Exact source text, lines 24–26
24/-- An ordinary legal Nim move: exactly one heap strictly decreases. -/25def Move (p q : List Nat) : Prop :=26  ∃ pre x post y, p = pre ++ x :: post ∧ q = pre ++ y :: post ∧ y < x

Project declarations in the statement: none

Project declarations used by the proof or definition body

none

def Game.restore

Module NimGame (lean/phase2/NimGame.lean, lines 407–410) · file SHA-256 23df6bb87ecfd344…

Two-frame restoration selector (paper Prop. restore / Thm search-gap (iii)): from the predecessor Q and current board Q', cancel delta = nim(Q) XOR nim(Q').

Statement as elaborated and printed by Lean 4.26.0

List ℕ → List ℕ → Option (List ℕ)
Exact source text, lines 407–410
407/-- Two-frame restoration selector (paper Prop. restore / Thm search-gap (iii)):408    from the predecessor Q and current board Q', cancel delta = nim(Q) XOR nim(Q'). -/409def restore (Q Q' : List Nat) : Option (List Nat) :=410  cancelFirst (nimSum Q ^^^ nimSum Q') Q'

Project declarations in the statement: none

Project declarations used by the proof or definition body

Game.cancelFirst, nimSum

def nimSum

Module NimCore (lean/NimCore.lean, lines 7–9) · file SHA-256 b07db6c505210d5a…

Statement as elaborated and printed by Lean 4.26.0

List ℕ → ℕ
Exact source text, lines 7–9
7def nimSum : List Nat → Nat8  | [] => 09  | h :: t => h ^^^ nimSum t

Project declarations in the statement: none

Project declarations used by the proof or definition body

none

def Game.Width

Module NimGame (lean/phase2/NimGame.lean, lines 31–32) · file SHA-256 23df6bb87ecfd344…

B-bit board: every heap is below 2^B.

Statement as elaborated and printed by Lean 4.26.0

ℕ → List ℕ → Prop
Exact source text, lines 31–32
31/-- B-bit board: every heap is below 2^B. -/32def Width (B : Nat) (p : List Nat) : Prop := ∀ x ∈ p, x < 2 ^ B

Project declarations in the statement: none

Project declarations used by the proof or definition body

none

def LocalRestore.restC

Module LocalRestore (lean/phase4/LocalRestore.lean, lines 181–181) · file SHA-256 fc4c7f908b0c9c58…

Statement as elaborated and printed by Lean 4.26.0

ℕ → ℕ → NimAudit.CorrBridge.RCirc
Exact source text, lines 181–181
181def restC (B N : Nat) : RCirc := substR (restSubst B N) (varSel B N)

Project declarations in the statement: CorrBridge.RCirc

Project declarations used by the proof or definition body

LocalRestore.restSubst, SelectCircuit.substR, SelectCircuit.varSel

def LocalRestore.tf

Module LocalRestore (lean/phase4/LocalRestore.lean, lines 29–31) · file SHA-256 fc4c7f908b0c9c58…

Two-frame input vector.

Statement as elaborated and printed by Lean 4.26.0

ℕ → ℕ → List ℕ → List ℕ → ℕ → Bool
Exact source text, lines 29–31
29/-- Two-frame input vector. -/30def tf (B N : Nat) (Q Q' : List Nat) : Nat → Bool := fun j =>31  if j < B * N then boardBits B Q j else boardBits B Q' (j - B * N)

Project declarations in the statement: none

Project declarations used by the proof or definition body

CorrBridge.boardBits

def SelectCircuit.selReadout

Module SelectCircuit (lean/phase4/SelectCircuit.lean, lines 26–32) · file SHA-256 922ee44ce0ababa9…

Evaluation of a move readout on an arbitrary input vector.

Statement as elaborated and printed by Lean 4.26.0

NimAudit.CorrBridge.RCirc → (ℕ → Bool) → Option (ℕ × ℕ)
Exact source text, lines 26–32
26/-- Evaluation of a move readout on an arbitrary input vector. -/27def selReadout (R : RCirc) (z : Nat → Bool) : Option (Nat × Nat) :=28  if eval z R.valid then29    match firstTrue (R.sel.map (eval z)) with30    | some i => some (i, toNatBE (R.val.map (eval z)))31    | none => none32  else none

Project declarations in the statement: CorrBridge.RCirc

Project declarations used by the proof or definition body

AC0.Circ, AC0.Circ.eval, Bridge.toNatBE, CorrBridge.RCirc, CorrBridge.RCirc.sel, CorrBridge.RCirc.val, CorrBridge.RCirc.valid, CorrBridge.firstTrue

inductive WholeGame.TwoFrameWins

Module WholeGame (lean/phase5/WholeGame.lean, lines 41–50) · file SHA-256 809a74ad589db609…

Stateful two-frame game predicate (opponent to move at the stored board Q): every legal opponent move is answered by a legal reply, the reply's board becomes the new stored board, and play continues. Being inductive, every such play is finite; at a terminal stored board the opponent cannot move and loses.

Statement as elaborated and printed by Lean 4.26.0

(List ℕ → List ℕ → Option (List ℕ)) → List ℕ → Prop
Exact source text, lines 41–50
41/-- Stateful two-frame game predicate (opponent to move at the stored board Q): every42    legal opponent move is answered by a legal reply, the reply's board becomes the new43    stored board, and play continues.  Being inductive, every such play is finite; at a44    terminal stored board the opponent cannot move and loses. -/45inductive TwoFrameWins (reply : List Nat → List Nat → Option (List Nat)) : List Nat → Prop46  | intro (Q : List Nat) :47      (∀ Q', Move Q Q' → (reply Q Q').isSome = true) →48      (∀ Q' R, Move Q Q' → reply Q Q' = some R → Move Q' R) →49      (∀ Q' R, Move Q Q' → reply Q Q' = some R → TwoFrameWins reply R) →50      TwoFrameWins reply Q

Project declarations in the statement: none

Project declarations used by the proof or definition body

Game.Move

def WholeGame.restReply

Module WholeGame (lean/phase5/WholeGame.lean, lines 35–39) · file SHA-256 809a74ad589db609…

The player's reply: evaluate the compiled restoration network on (Q, Q'), decode its output wires and apply the decoded move to Q'. Index width r = log₂N + 1.

Statement as elaborated and printed by Lean 4.26.0

ℕ → List ℕ → List ℕ → Option (List ℕ)
Exact source text, lines 35–39
35/-- The player's reply: evaluate the compiled restoration network on (Q, Q'), decode its36    output wires and apply the decoded move to Q'.  Index width r = log₂N + 1. -/37def restReply (B : Nat) (Q Q' : List Nat) : Option (List Nat) :=38  applyM Q' (netBinSem (restNet B Q'.length (Q'.length.log2 + 1)) Q'.length (Q'.length.log2 + 1) B39    (LocalRestore.tf B Q'.length Q Q'))

Project declarations in the statement: none

Project declarations used by the proof or definition body

LocalRestore.tf, SelectCircuit.applyM, ThresholdInst.netBinSem, ThresholdInst.restNet

Full modules

ModuleFileLinesImports
LocalRestorelean/phase4/LocalRestore.lean587SelectCircuit
NimCorelean/NimCore.lean139Std
NimGamelean/phase2/NimGame.lean634NimRollout
SelectCircuitlean/phase4/SelectCircuit.lean1064ControllerBits
WholeGamelean/phase5/WholeGame.lean347ThresholdInst

Build evidence: fresh isolated rebuild of all 38 bundled modules with Lean (version 4.26.0, started 2026-10-10T15:21:56Z, finished 2026-10-10T15:27:00Z: 38/38 modules compiled, 1588 axiom reports, no errors. See Build for the commands and the independent audit of 2026-10-10 (dated evidence).