Binary encoding of CNF formulas
DefinitionReading a unary length prefix#✓fully proved
On a list of Booleans that begins with \(k\) copies of \(\mathrm{true}\) followed by \(\mathrm{false}\), the reader returns \(k\) together with the rest of the list after that \(\mathrm{false}\). On a list containing no \(\mathrm{false}\), including the empty list, it fails.
def readUnaryLength : List Bool → Option (Nat × List Bool)
| [] => none
| false :: rest => some (0, rest)
| true :: rest => (readUnaryLength rest).map fun result => (result.1 + 1, result.2)
Used by 68
k) is NP-hard · Subset-sum emitter: unterminated unary prefix at rdK · Subset-sum emitter: unterminated unary prefix at rdN · Run of the VERTEX-COVER to SUBSET-SUM machine · SUBSET-SUM is NP-hard · Witness decoder · Decoding inverts the witness encoding · Decoding the empty witness · Witness decoding fails on a cut-off first number · Decoding peels off the first number · The witness decoder succeeds only on encodings · The unary reader of the Hamiltonian path verifier rejects an unterminated number · The unary mover of the Hamiltonian path verifier rejects an unterminated number · The edge-list check of the Hamiltonian-path verifier · The witness check of the Hamiltonian-path verifier · Correctness and running time of the Hamiltonian path verifier · Correctness of the SAT-to-3SAT reduction on bit strings · Decoding a PATH instance · Decoding inverts the PATH instance encoding · The PATH instance parser succeeds exactly on encodings · Reading a cut-off unary number · Running time and output of the marking machine · Validation phase of the PATH marking machineDefinitionPrefix-free code of a natural number#✓fully proved
The code of \(n \in \mathbb{N}\) is the self-delimiting block of the binary representation of \(n\) (least significant bit first, with \(0\) represented by the empty list): each bit \(c\) is written as the two bits \(\mathrm{true}, c\), and the block ends with a terminating \(\mathrm{false}\).
def encodeNatPrefix (n : Nat) : List Bool :=
MetaMathlibExt.BitstringEncoding.delimit (Computability.encodeNat n)
def delimit : List Bool → List Bool
| [] => [false]
| b :: l => true :: b :: delimit l
MathlibExt/Computability/Complexity.lean:68
Uses 1 · Used by 20
DefinitionDecoding a prefix-coded natural number#✓fully proved
Given a bit string \(w\), writing \(1\) for true and \(0\) for false, the decoder reads one self-delimiting block \(1\,b_1\,1\,b_2 \cdots 1\,b_k\,0\) from the front of \(w\), with payload \(b_1 \cdots b_k\) and remaining bits \(r\). If the payload is the binary expansion of some natural number \(n\), least significant bit first and without trailing zeros (empty for \(n = 0\)), the result is \((n, r)\). Decoding fails if \(w\) does not begin with such a block or the payload is not of this form.
def decodeNatPrefix (input : List Bool) : Option (Nat × List Bool) := do
let (bits, rest) ← MetaMathlibExt.BitstringEncoding.undelimit input
let n := Computability.decodeNat bits
if Computability.encodeNat n = bits then some (n, rest) else none
def undelimit : List Bool → Option (List Bool × List Bool)
| false :: rest => some ([], rest)
| true :: b :: input => (undelimit input).map fun p => (b :: p.1, p.2)
| _ => none
MathlibExt/Computability/Complexity.lean:74
Uses 1 · Used by 17
DefinitionBinary code of a literal#✓fully proved
A literal \((n, b)\), with variable index \(n \in \mathbb{N}\) and polarity \(b \in \{\mathrm{false}, \mathrm{true}\}\), is encoded as the prefix-free code of \(n\) followed by the single bit \(b\).
def encodeLit (l : Nat × Bool) : List Bool :=
encodeNatPrefix l.1 ++ [l.2]
def encodeNatPrefix (n : Nat) : List Bool :=
MetaMathlibExt.BitstringEncoding.delimit (Computability.encodeNat n)
CSLibExt/Computability/CookLevin.lean:53
Uses 1 · Used by 28
DefinitionDecoding a literal#✓fully proved
Given a bit string \(w\), the decoder reads a prefix-coded natural number \(n\) from the front of \(w\) and then one more bit \(b\). The result is the literal \((n, b)\), with variable index \(n\) and polarity \(b\), together with the bits left after \(b\). Decoding fails if no prefix-coded natural number can be read or no bit follows it.
def decodeLit (input : List Bool) : Option ((Nat × Bool) × List Bool) := do
let (n, rest) ← decodeNatPrefix input
match rest with
| [] => none
| polarity :: tail => some ((n, polarity), tail)
def decodeNatPrefix (input : List Bool) : Option (Nat × List Bool) := do
let (bits, rest) ← MetaMathlibExt.BitstringEncoding.undelimit input
let n := Computability.decodeNat bits
if Computability.encodeNat n = bits then some (n, rest) else none
CSLibExt/Computability/CookLevin.lean:57
Uses 1 · Used by 11
DefinitionDecoding a fixed number of literals#✓fully proved
For a natural number \(n\) and a bit string \(w\), the decoder reads \(n\) literals one after another from the front of \(w\) with the literal decoder. The result is the clause made of these literals, in order, together with the bits left after them; for \(n = 0\) it is the empty clause together with \(w\). Decoding fails if one of the \(n\) literals cannot be read.
def decodeLits : Nat → List Bool → Option (CNFClause × List Bool)
| 0, input => some ([], input)
| n + 1, input => do
let (literal, rest) ← decodeLit input
let (literals, tail) ← decodeLits n rest
some (literal :: literals, tail)
abbrev CNFClause := List (Nat × Bool)
CSLibExt/Logics/Propositional/CNF/Basic.lean:30
def decodeLit (input : List Bool) : Option ((Nat × Bool) × List Bool) := do
let (n, rest) ← decodeNatPrefix input
match rest with
| [] => none
| polarity :: tail => some ((n, polarity), tail)
CSLibExt/Computability/CookLevin.lean:67
Uses 2 · Used by 10
DefinitionLength-prefixed binary code of a clause#✓fully proved
Writing \(1\) for true and \(0\) for false, the code of a clause \(C = (\ell_1, \dots, \ell_k)\) is \(1^k 0\) (\(k\) ones followed by a zero) followed by the binary codes of \(\ell_1, \dots, \ell_k\) in order, where a literal \((x, b)\) with variable index \(x\) and polarity \(b\) is coded by the prefix-free code of \(x\) followed by the bit \(b\).
def encodeClause (C : CNFClause) : List Bool :=
List.replicate C.length true ++ false :: C.flatMap encodeLit
abbrev CNFClause := List (Nat × Bool)
CSLibExt/Logics/Propositional/CNF/Basic.lean:30
def encodeLit (l : Nat × Bool) : List Bool :=
encodeNatPrefix l.1 ++ [l.2]
CSLibExt/Computability/CookLevin.lean:63
Uses 2 · Used by 24
DefinitionDecoding a length-prefixed clause#✓fully proved
Given a bit string \(w\), writing \(1\) for true and \(0\) for false, the decoder reads a unary length prefix \(1^n 0\) (\(n\) ones followed by a zero) from the front of \(w\), then reads exactly \(n\) literals from the remaining bits with the literal decoder. The result is the clause made of these literals, in order, together with the bits left after them. Decoding fails if \(w\) does not begin with such a prefix or one of the literals cannot be read.
def decodeClause (input : List Bool) : Option (CNFClause × List Bool) := do
let (n, rest) ← readUnaryLength input
decodeLits n rest
abbrev CNFClause := List (Nat × Bool)
CSLibExt/Logics/Propositional/CNF/Basic.lean:30
def readUnaryLength : List Bool → Option (Nat × List Bool)
| [] => none
| false :: rest => some (0, rest)
| true :: rest => (readUnaryLength rest).map fun result => (result.1 + 1, result.2)
CSLibExt/Computability/CookLevin.lean:46
def decodeLits : Nat → List Bool → Option (CNFClause × List Bool)
| 0, input => some ([], input)
| n + 1, input => do
let (literal, rest) ← decodeLit input
let (literals, tail) ← decodeLits n rest
some (literal :: literals, tail)
CSLibExt/Computability/CookLevin.lean:74
Uses 3 · Used by 9
DefinitionDecoding a fixed number of clauses#✓fully proved
For a natural number \(n\) and a bit string \(w\), the decoder reads \(n\) clauses one after another from the front of \(w\) with the length-prefixed clause decoder. The result is the CNF formula made of these clauses, in order, together with the bits left after them; for \(n = 0\) it is the empty formula together with \(w\). Decoding fails if one of the \(n\) clauses cannot be read.
def decodeClauses : Nat → List Bool → Option (CNF × List Bool)
| 0, input => some ([], input)
| n + 1, input => do
let (clause, rest) ← decodeClause input
let (clauses, tail) ← decodeClauses n rest
some (clause :: clauses, tail)
abbrev CNF := List CNFClause
CSLibExt/Logics/Propositional/CNF/Basic.lean:33
def decodeClause (input : List Bool) : Option (CNFClause × List Bool) := do
let (n, rest) ← readUnaryLength input
decodeLits n rest
CSLibExt/Computability/CookLevin.lean:86
Uses 3 · Used by 9
DefinitionLength-prefixed binary code of a CNF formula#✓fully proved
Writing \(1\) for true and \(0\) for false, the code of a CNF formula \(\varphi = (C_1, \dots, C_m)\) is \(1^m 0\) (\(m\) ones followed by a zero) followed by the length-prefixed binary codes of the clauses \(C_1, \dots, C_m\) in order.
def encodeCNF (φ : CNF) : List Bool :=
List.replicate φ.length true ++ false :: φ.flatMap encodeClause
abbrev CNF := List CNFClause
CSLibExt/Logics/Propositional/CNF/Basic.lean:33
def encodeClause (C : CNFClause) : List Bool :=
List.replicate C.length true ++ false :: C.flatMap encodeLit
CSLibExt/Computability/CookLevin.lean:82
Uses 3 · Used by 27
TM2 SAT verifier · 3SAT is in NP · Token length bound for the SAT verifier (moved alias) · Correctness of the SAT to 3-COLOR map on bit strings · CLIQUE is NP-hard · SAT ≤ HAMPATH preserves membership · The SAT-to-3SAT emitter · CNF encodings are nonempty · The SAT-to-3SAT reduction is polynomial-time · The SAT-to-3SAT reduction on bit strings · The SAT-to-3SAT emitter writes the encoded translation in quadratic time · Tokenize, then emit · Correctness of the SAT-to-3SAT reduction on bit strings · The 3SAT decision problemDefinitionDecoding a length-prefixed CNF formula#✓fully proved
Given a bit string \(w\), writing \(1\) for true and \(0\) for false, the decoder reads a unary length prefix \(1^n 0\) (\(n\) ones followed by a zero) from the front of \(w\), then reads exactly \(n\) clauses from the remaining bits with the length-prefixed clause decoder. The result is the CNF formula made of these clauses, in order, together with the bits left after them. Decoding fails if \(w\) does not begin with such a prefix or one of the clauses cannot be read.
def decodeCNFAux (input : List Bool) : Option (CNF × List Bool) := do
let (n, rest) ← readUnaryLength input
decodeClauses n rest
abbrev CNF := List CNFClause
CSLibExt/Logics/Propositional/CNF/Basic.lean:33
def readUnaryLength : List Bool → Option (Nat × List Bool)
| [] => none
| false :: rest => some (0, rest)
| true :: rest => (readUnaryLength rest).map fun result => (result.1 + 1, result.2)
CSLibExt/Computability/CookLevin.lean:46
def decodeClauses : Nat → List Bool → Option (CNF × List Bool)
| 0, input => some ([], input)
| n + 1, input => do
let (clause, rest) ← decodeClause input
let (clauses, tail) ← decodeClauses n rest
some (clause :: clauses, tail)
CSLibExt/Computability/CookLevin.lean:91
Uses 3 · Used by 8
DefinitionDecoding a complete length-prefixed CNF formula#✓fully proved
Given a bit string \(w\), the decoder reads one CNF formula \(\varphi\) from the front of \(w\) with the length-prefixed CNF decoder, which also returns the bits left after \(\varphi\). The result is \(\varphi\) if no bits are left; decoding fails if no formula can be read or if bits remain after it.
def decodeCNF (input : List Bool) : Option CNF := do
let (φ, rest) ← decodeCNFAux input
if rest.isEmpty then some φ else none
abbrev CNF := List CNFClause
CSLibExt/Logics/Propositional/CNF/Basic.lean:33
def decodeCNFAux (input : List Bool) : Option (CNF × List Bool) := do
let (n, rest) ← readUnaryLength input
decodeClauses n rest
CSLibExt/Computability/CookLevin.lean:103
Uses 2 · Used by 33
TM2 SAT verifier · Answer of the SAT host with the three-literal filter · 3SAT is in NP · The SAT to 3-COLOR reduction map on bit strings · Correctness of the SAT to 3-COLOR map on bit strings · The SAT-to-3-COLOR reduction is polynomial-time · Karp's SAT-to-CLIQUE map on bit strings · SAT decision on undecodable input (moved alias) · SAT ≤ CLIQUE is polynomial-time · CLIQUE is NP-hard · The SAT-to-HAMPATH reduction map · SAT ≤ HAMPATH preserves membership · SAT ≤ HAMPATH is polynomial-time · The SAT-to-3SAT reduction is polynomial-time · Output of an emitter on a bit string · The SAT-to-3SAT reduction on bit strings · Tokenize, then emit · Correctness of the SAT-to-3SAT reduction on bit stringsTheoremParsing a self-delimiting block#✓fully proved
For all lists of Booleans \(u\), \(p\) and \(r\), parsing one self-delimiting block from the front of \(u\) succeeds with payload \(p\) and remainder \(r\) if and only if \(u\) is the self-delimiting block of \(p\) followed by \(r\).
theorem undelimit_eq_some_iff (input bits rest : List Bool) :
MetaMathlibExt.BitstringEncoding.undelimit input = some (bits, rest) ↔
input = MetaMathlibExt.BitstringEncoding.delimit bits ++ rest
def undelimit : List Bool → Option (List Bool × List Bool)
| false :: rest => some ([], rest)
| true :: b :: input => (undelimit input).map fun p => (b :: p.1, p.2)
| _ => none
MathlibExt/Computability/Complexity.lean:74
def delimit : List Bool → List Bool
| [] => [false]
| b :: l => true :: b :: delimit l
MathlibExt/Computability/Complexity.lean:68
Uses 2 · Used by 19
TheoremThe CNF decoder inverts the length-prefixed CNF code#✓fully proved
For every bit string \(w\) and every CNF formula \(\varphi\), the complete CNF decoder returns \(\varphi\) on input \(w\) if and only if \(w\) is the length-prefixed binary code of \(\varphi\).
@[simp] theorem decodeCNF_eq_some_iff (input : List Bool) (φ : CNF) :
decodeCNF input = some φ ↔ input = encodeCNF φ
abbrev CNF := List CNFClause
CSLibExt/Logics/Propositional/CNF/Basic.lean:33
def decodeCNF (input : List Bool) : Option CNF := do
let (φ, rest) ← decodeCNFAux input
if rest.isEmpty then some φ else none
CSLibExt/Computability/CookLevin.lean:108
def encodeCNF (φ : CNF) : List Bool :=
List.replicate φ.length true ++ false :: φ.flatMap encodeClause
CSLibExt/Computability/CookLevin.lean:99
Uses 17 · Used by 11
TM2 SAT verifier · 3SAT is in NP · Correctness of the SAT to 3-COLOR map on bit strings · CLIQUE is NP-hard · SAT ≤ HAMPATH preserves membership · Tokenize, then emit · Correctness of the SAT-to-3SAT reduction on bit stringsDefinitionEncodings of satisfiable CNF formulas#✓fully proved
A list \(x\) of Booleans encodes a satisfiable CNF formula when \(x\) is the binary code of some CNF formula \(\varphi\) and \(\varphi\) is satisfiable.
def IsSATEncoded (x : List Bool) : Prop :=
∃ φ : CNF, encodeCNF φ = x ∧ CNFSat φ
abbrev CNF := List CNFClause
CSLibExt/Logics/Propositional/CNF/Basic.lean:33
def encodeCNF (φ : CNF) : List Bool :=
List.replicate φ.length true ++ false :: φ.flatMap encodeClause
CSLibExt/Computability/CookLevin.lean:99
def CNFSat (φ : CNF) : Prop :=
∃ v : Nat → Bool, ∀ C ∈ φ, ∃ (x : Nat) (b : Bool), (x, b) ∈ C ∧ v x = b
CSLibExt/Logics/Propositional/CNF/Basic.lean:36
Uses 3 · Used by 1
DefinitionThe SAT language#✓fully proved
SAT is the set of lists of Booleans that encode a satisfiable CNF formula.
def SATLang : Set (List Bool) :=
{x | IsSATEncoded x}
def IsSATEncoded (x : List Bool) : Prop :=
∃ φ : CNF, encodeCNF φ = x ∧ CNFSat φ
CSLibExt/Computability/CookLevin.lean:381
DefinitionSAT as a decision problem#✓fully proved
The SAT decision problem returns true on a list \(x\) of Booleans when \(x\) lies in the SAT language, that is, when \(x\) encodes a satisfiable CNF formula, and false otherwise.
noncomputable def satDec : MetaMathlibExt.ComplexityTheory.DecisionProblem :=
fun x => if x ∈ SATLang then true else false
abbrev DecisionProblem := List Bool → Bool
MathlibExt/Computability/Complexity.lean:170
def SATLang : Set (List Bool) :=
{x | IsSATEncoded x}
CSLibExt/Computability/CookLevin.lean:385
Uses 2 · Used by 21
DefinitionPolynomial-time many-one reduction#✓fully proved
A decision problem \(L_1\) reduces to a decision problem \(L_2\) in polynomial time when there is a polynomial-time computable function \(f\) from lists of Booleans to lists of Booleans such that \(L_1(x) = L_2(f(x))\) for every list \(x\) of Booleans.
def PolyManyOneRed (L₁ L₂ : MetaMathlibExt.ComplexityTheory.DecisionProblem) : Prop :=
∃ reduce : List Bool → List Bool,
MetaMathlibExt.ComplexityTheory.IsPolyTime reduce ∧ ∀ x, L₁ x = L₂ (reduce x)
abbrev DecisionProblem := List Bool → Bool
MathlibExt/Computability/Complexity.lean:170
def IsPolyTime {α β : Type} [BitstringEncoding α] [BitstringEncoding β]
(f : α → β) : Prop :=
IsPolyTimeWithEncoding (BitstringEncoding.toEncoding (α := α))
(BitstringEncoding.toEncoding (α := β)) f
MathlibExt/Computability/Complexity.lean:186
Uses 10 · Used by 141
k) is NP-complete · VERTEX-COVER (exact size k) is NP-hard · CONNECTED DOMINATING SET is NP-complete · DENSEST k-SUBGRAPH is NP-complete · DOMINATING SET is NP-complete · EDGE DOMINATING SET is NP-complete · EFFICIENT DOMINATION (PERFECT CODE) is NP-complete · FEEDBACK ARC SET is NP-complete · FEEDBACK VERTEX SET is NP-complete · INDEPENDENT DOMINATING SET is NP-complete · MAXIMUM INDUCED MATCHING is NP-complete · MINIMUM FILL-IN (CHORDAL COMPLETION) is NP-complete · ODD CYCLE TRANSVERSAL is NP-complete · ROMAN DOMINATION is NP-complete · TOTAL DOMINATING SET is NP-complete · 0-1 INTEGER PROGRAMMING FEASIBILITY is NP-complete · 3-PARTITION is NP-complete · BIN PACKING is NP-complete · EXACT-CARDINALITY SUBSET SUM is NP-complete · KNAPSACK is NP-complete · MULTIPLE KNAPSACK is NP-complete · MULTIWAY NUMBER PARTITION is NP-complete · NUMERICAL 3-DIMENSIONAL MATCHING is NP-complete · PARTITION is NP-complete · Polynomial-time reduction from SUBSET-SUM to PARTITION · STRIP PACKING is NP-complete · SUBSET PRODUCT is NP-complete · SUBSET-SUM is NP-complete · SUBSET-SUM is NP-hard · UNBOUNDED KNAPSACK is NP-complete · TRAVELING SALESPERSON (ASYMMETRIC) is NP-complete · CAPACITATED VEHICLE ROUTING is NP-complete · DEGREE-CONSTRAINED SPANNING TREE is NP-complete · HAMILTONIAN CYCLE (DIRECTED) is NP-complete · HAMPATH is NP-complete · HAMPATH is NP-hard · INDUCED SUBGRAPH ISOMORPHISM is NP-complete · LONGEST CYCLE is NP-complete · LONGEST PATH is NP-complete · MAXIMUM COMMON INDUCED SUBGRAPH is NP-complete · MAXIMUM LEAF SPANNING TREE is NP-complete · MINIMUM EQUIVALENT DIGRAPH is NP-complete · DIRECTED PATH COVER is NP-complete · STEINER TREE is NP-complete · SUBGRAPH ISOMORPHISM is NP-complete · TRAVELING SALESPERSON (SYMMETRIC) is NP-complete · HAMILTONIAN CYCLE (UNDIRECTED) is NP-complete · HAMILTONIAN PATH (UNDIRECTED) is NP-complete · DIRECTED DISJOINT PATHS is NP-complete · 1-IN-3-SAT is NP-complete · 3-SAT WITH BOUNDED OCCURRENCES is NP-complete · NP-hardness transfers along reductions · Transitivity of polynomial-time many-one reductions · SAT reduces to 3SAT · 3SAT is NP-complete · 3SAT is NP-hard · BETWEENNESS is NP-complete · BINARY CONSTRAINT SATISFACTION is NP-complete · CIRCUIT SAT is NP-complete · MAXIMUM 2-SATISFIABILITY is NP-complete · MAXIMUM SATISFIABILITY is NP-complete · MINIMUM 2-SATISFIABILITY is NP-complete · MINIMUM ONES 3-SAT is NP-complete · MONOTONE 3-SAT is NP-complete · NOT-ALL-EQUAL 3-SAT is NP-complete · JOB SEQUENCING WITH DEADLINES, PROCESSING TIMES, AND PROFITS is NP-complete · PARALLEL MACHINES WITH DEADLINES is NP-complete · PRECEDENCE-CONSTRAINED MULTIPROCESSOR SCHEDULING is NP-complete · RESOURCE-CONSTRAINED PROJECT SCHEDULING is NP-complete · SINGLE-MACHINE SCHEDULING WITH RELEASE TIMES AND DEADLINES is NP-complete · 3-MACHINE FLOW SHOP SCHEDULING is NP-complete · TOTAL TARDINESS is NP-complete · SINGLE-MACHINE WEIGHTED COMPLETION WITH PRECEDENCE is NP-complete · WEIGHTED NUMBER OF TARDY JOBS is NP-complete · 3-DIMENSIONAL MATCHING is NP-complete · EXACT COVER BY 3-SETS is NP-complete · EXACT COVER is NP-complete · FACILITY LOCATION is NP-complete · GENERALIZED ASSIGNMENT is NP-complete · MAXIMUM COVERAGE is NP-complete · MAXIMUM n-TIMES COVERAGE is NP-complete · MINIMUM TEST SET is NP-complete · k-CENTER (GENERALIZED, NONMETRIC) is NP-complete · k-MEDIAN (GENERALIZED, NONMETRIC) is NP-complete · QUADRATIC ASSIGNMENT is NP-complete · SET COVER is NP-complete · SET PACKING is NP-complete · SET SPLITTING is NP-complete · AKARI (LIGHT UP) is NP-complete · CLOSEST STRING is NP-complete · LATIN SQUARE COMPLETION is NP-complete · LONGEST COMMON SUBSEQUENCE is NP-complete · MINESWEEPER CONSISTENCY is NP-complete · NONOGRAM is NP-complete · SHORTEST COMMON SUPERSEQUENCE is NP-complete · SUDOKU is NP-completeLoading dependency explorer…
The dependency explorer needs JavaScript and its graph data.