CNF formulas and gadget instances as bit strings
DefinitionPNPMission_InstancesVariables are natural numbers and a literal is a pair whose value under an assignment is . A CNF formula is a list of clauses, each a list of literals; it is satisfiable when some makes at least one literal true in every clause. Formulas are written as bit strings by a prefix-free encoding: a natural number is written as the pairs for its binary digits (least significant first) followed by ; a literal is , the number, and its sign; a clause is its literals followed by ; a formula is the concatenation of its clauses. SatLang is the set of bit strings that encode satisfiable CNF formulas.
For a MAX NAE-3SAT instance with clauses and signs sg for the fresh literals, xiList is the list of the clauses of (the gadget construction of the paper), with the variable of named and the fresh variable named , so that is a list of three-literal clauses over . instanceBits writes such a list as a bit string, and bitsToAssignment reads the assignment of the variables from a bit string, with every variable beyond its length false.
import Mathlib
import Definitions.Def_PNP_ThmMain_Defs
namespace PNPMission
open PNP.Gadget PNP.Semantics
/-- Self-delimiting binary encoding of a natural number `n`: each binary digit `d` of `n`
(least significant first) is written as `true, d`, followed by the terminator `false`. -/
def natBits (n : ℕ) : List Bool :=
(Nat.digits 2 n).flatMap (fun d => [true, decide (d = 1)]) ++ [false]
/-- A literal `(v, s)` is written as `true`, the encoding of `v`, and the sign `s`. -/
def litBits (l : ℕ × Bool) : List Bool := [true] ++ natBits l.1 ++ [l.2]
/-- A clause (a list of literals) is written as its literals followed by `false`. -/
def clauseBits (c : List (ℕ × Bool)) : List Bool := c.flatMap litBits ++ [false]
/-- A formula (a list of clauses) is written as the concatenation of its clauses. -/
def formulaBits (φ : List (List (ℕ × Bool))) : List Bool := φ.flatMap clauseBits
/-- A list of three-literal clauses, written as a formula. -/
def instanceBits (xs : List (Clause ℕ)) : List Bool :=
formulaBits (xs.map fun c => [c.1, c.2.1, c.2.2])
/-- A CNF formula, a list of clauses each a list of literals `(v, s)` whose value under `σ` is
`σ v ^^ s`, is satisfiable when some assignment makes a literal true in every clause. -/
def SatCNF (φ : List (List (ℕ × Bool))) : Prop :=
∃ σ : ℕ → Bool, ∀ c ∈ φ, ∃ l ∈ c, litVal σ l = true
/-- The language of bit strings that encode satisfiable CNF formulas. -/
def SatLang : Set (List Bool) := {x | ∃ φ, x = formulaBits φ ∧ SatCNF φ}
/-- Natural-number names for the variables of `Ξ(Φ)`: a variable `v` of `Φ` is named `2v`, the
fresh variable `(i, j)` is named `2(3i + j) + 1`. -/
def tgtVar {m : ℕ} : Target ℕ m → ℕ
| Sum.inl v => 2 * v
| Sum.inr p => 2 * (3 * p.1.val + p.2.val) + 1
/-- A clause of `Ξ(Φ)` with its variables renamed by `tgtVar`. -/
def renameClause {m : ℕ} (c : Clause (Target ℕ m)) : Clause ℕ :=
((tgtVar c.1.1, c.1.2), (tgtVar c.2.1.1, c.2.1.2), (tgtVar c.2.2.1, c.2.2.2))
/-- The list of the `4m` clauses of the instance `Ξ(Φ)` with fresh-literal signs `sg`, over
natural-number variables. -/
def xiList {m : ℕ} (Φ : Fin m → Clause ℕ) (sg : Fin m → Fin 3 → Bool) : List (Clause ℕ) :=
((List.finRange m).flatMap (xiBlock Φ sg)).map renameClause
/-- The assignment of the variables `0, 1, 2, …` read from a bit string; variables beyond its
length are `false`. -/
def bitsToAssignment (bs : List Bool) : ℕ → Bool := fun v => bs.getD v false
end PNPMission
Read-back
What the Lean code literally says, in plain math · claude-sonnet-5-5 (independent blind auditor)
natBits. For , the binary digits of least significant first, each digit written as the two entries , followed by a final ; .
litBits. For a literal : , then , then .
clauseBits. For a clause (list of literals): the concatenation of its literals' codes followed by one ; the empty clause is .
formulaBits. For a formula (list of clauses): the concatenation of the clause codes; the empty formula is the empty list.
instanceBits. For a list of triple-clauses over : formulaBits of the formula obtained by turning each triple into the three-literal list, in order. No distinctness is required.
SatCNF. . The empty formula is satisfiable; a formula with an empty clause is not.
SatLang. The set of bit strings for which some formula has and ; strings that are no formula encoding are excluded; the empty string is included.
tgtVar. On : an original variable , a fresh variable .
renameClause. Applies tgtVar to the three variables of a clause over the target type and keeps the signs.
xiList. For and signs : for in order, the block of four clauses with the literals of lifted to the left summand and the fresh literals , being with flipped sign; the concatenation of blocks, each clause renamed by renameClause; clauses in total.
bitsToAssignment. For a bit list : if , else .
Confirmed by the mission captain (proposal self-audit).
Confirmed by the moderator at approval.