A total bit-string implementation of Definition 20
DefinitionNQueensComp_NPC_CodeReduction23n-queensparsimonious-reduction
Read the family length from the unary prefix and read that many five-field gadget codes, retaining the order of every list. Subtract the board-dependent offset to recover each signed difference diagonal. Accept the parsed family if its re-encoding is the entire input string, its lists are duplicate-free, and each gadget represents a valid Problem 4 instance with an empty set of excluded sum-diagonals.
On acceptance, apply the mission’s Definition 20 to the family, sort each output set, and encode the resulting Problem 4 instance. On failure, return the empty bit string. This defines the total function reduce23Bits used to state the machine bound separately from its count-preservation property.
Definition code
import Definitions.Def_NQueensComp_NPC_CodeReduction36
namespace NQueensComp.NPC
/-- Interpret the four lists of a gadget code as finite sets. -/
def Code4.toInstance (L : Code4) : Inst4 :=
⟨L.n, L.C.toFinset, L.R.toFinset, L.Dm.toFinset, L.Dp.toFinset⟩
namespace CodeReduction23
def readNat (x : List Bool) := CookLevin.decodeNatFuel x.length x
def readNatList (x : List Bool) := CookLevin.decodeListNatFuel x.length x
/-- Read one gadget and leave the suffix for the next gadget. -/
def readGadget (x : List Bool) : Code4 × List Bool :=
let (n, x) := readNat x
let (cols, x) := readNatList x
let (rows, x) := readNatList x
let (minus, x) := readNatList x
let (plus, rest) := readNatList x
(⟨n, cols, rows, minus.map (fun k : ℕ => (k : ℤ) - ((n : ℤ) - 1)), plus⟩, rest)
def readGadgets : ℕ → List Bool → Code5
| 0, _ => []
| k + 1, x =>
let (L, rest) := readGadget x
L :: readGadgets k rest
def readFamily (x : List Bool) : Code5 :=
let (k, rest) := readNat x
readGadgets k rest
/-- A Problem 5 entry lists its four sets without duplicates and represents a valid
gadget with no excluded sum-diagonals. -/
def EntryOK (L : Code4) : Prop :=
L.C.Nodup ∧ L.R.Nodup ∧ L.Dm.Nodup ∧ L.Dp.Nodup ∧
L.toInstance.Valid ∧ L.toInstance.Dp = ∅
instance (L : Code4) : Decidable (EntryOK L) := by
unfold EntryOK Inst4.Valid Inst3.Valid Inst2.Valid DistinctRowsCols FitsOn
infer_instance
end CodeReduction23
/-- Validate a Problem 5 bit string, apply Definition 20, and write its sorted Problem 4
code. Re-encoding the parsed input checks the entire string, including its final bit. -/
def reduce23Bits (x : List Bool) : List Bool :=
let Ls := CodeReduction23.readFamily x
if enc5 Ls = x ∧ ∀ L ∈ Ls, CodeReduction23.EntryOK L then
(def20 (Ls.map Code4.toInstance)).toCode4.enc
else []
end NQueensComp.NPC
Source
Gent, Jefferson and Nightingale, Complexity of n-Queens Completion, JAIR 59 (2017), 815–848. Unary encoding convention, p. 820. Definition 20, pp. 828–829. Theorem 23, p. 831. https://doi.org/10.1613/jair.5512