Reading and writing DIMACS CNF #
The DIMACS CNF format of SAT solvers and benchmarks: a header p cnf V C giving the number of
variables and clauses, then the clauses, each a list of nonzero integers ended by 0, the integer
i standing for variable |i|, negated if i < 0. Lines starting with c are comments.
Cslib.Logic.PL.CNF.ofDIMACS reads a formula, numbering the variables as the LRAT checker does
(Cslib.Logic.PL.Literal.ofDIMACS): the DIMACS variable v is the variable v - 1, so that a
certificate for the formula read refers to the same variables as the file. It checks the header:
the literals are within its number of variables and the clauses are as many as it says. As in
the SATLIB benchmarks, a line % ends the formula. A line is split into words at whitespace
(Char.isWhitespace). A number is read as String.toNat? and String.toInt? read it: a nonempty
string of decimal digits with _ digit separators, each between two digits (1_000), an
integer one with an optional -. The digits themselves are read by the decimal reader
Nat.ofDecimal? shared with the OPB front end.
Cslib.Logic.PL.CNF.toDIMACS writes a formula, the header giving one more variable than the
largest, and one clause per line. It is defined on lists of characters, so the kernel evaluates
it.
Round trip. Reading what Cslib.Logic.PL.CNF.toDIMACS writes gives the formula back
(Cslib.Logic.PL.CNF.ofDIMACS_toDIMACS), for every formula: repeated literals, empty clauses and
the empty formula included. So a theorem about the formula read from φ.toDIMACS is a theorem
about φ, and for formulas built in Lean the parser is not part of what is trusted. For a file
written by other tools, the parser is still trusted to read it as intended.
Main definitions #
Cslib.Logic.PL.CNF.ofDIMACS: reading a formula.Cslib.Logic.PL.CNF.toDIMACS: writing a formula.
Main statements #
Cslib.Logic.PL.CNF.ofDIMACS_toDIMACS: reading what is written gives the formula back.
Words and numbers #
Whether cs is a nonempty string of decimal digits with _ digit separators, each between
two digits, as String.isNat checks; last tells whether a digit comes before cs.
A natural number, as String.toNat? reads it: decimal digits with _ digit separators, each
between two digits. The separators are dropped and the digits read by Nat.ofDecimal?.
Reading #
Read the literal tokens toks of a body over nvars variables, with cls the clauses read
and cur the clause in progress: 0 ends a clause. Fails on a token that is not an integer and
a literal beyond the variables.
Read the lines ls, with hd the header read, cls the clauses read and cur the clause in
progress. Empty lines and lines starting with c are skipped, and a line starting with % ends
the formula.
Reading a DIMACS CNF formula, with the DIMACS variable v as the variable v - 1. Fails
on a missing or malformed header, a token that is not an integer, a literal beyond the number of
variables of the header, an unterminated last clause, or a number of clauses other than the
header's. A line starting with % ends the formula, as in the SATLIB benchmarks.
Writing #
A literal as a DIMACS integer (Cslib.Logic.PL.Literal.toDIMACS): the variable v as v + 1,
negated if the literal is.
The number of variables of a formula in DIMACS: one more than the largest, 0 for none.
Writing a DIMACS CNF formula: the header p cnf V C, with V one more than the largest
variable, then one clause per line, the variable v as the DIMACS variable v + 1.
Reading what is written #
A decimal digit is not whitespace.
Reading the digits of a number.
A word not starting with - is read as a natural number.
Reading the digits of an integer.
Reading a literal: the integer of its word names it.
The variables of a formula are below its number of variables.