Documentation

CslibFrontier.Logics.Propositional.CNF.Dimacs

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 #

Main statements #

Words and numbers #

The word in progress, cur reversed, if it is not empty.

The words of a line: its maximal runs of characters other than whitespace, cur the word in progress, reversed.

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?.

An integer: a natural number with an optional -.

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.

The header p cnf V C, as words.

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.

A clause as a DIMACS line: its literals, 0 and a newline.

The header line p cnf V C.

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.

The characters of a literal are not whitespace.

theorem Cslib.Logic.PL.Dimacs.words_append {w rest cur : List Char} (hw : ∀ c ∈ w, c.isWhitespace = false) :
words (w ++ rest) cur = words rest (w.reverse ++ cur)

A run of characters other than whitespace joins the word in progress.

theorem Cslib.Logic.PL.Dimacs.words_flatMap_space {ws : List (List Char)} (rest : List Char) (hw : ∀ w ∈ ws, w ≠ [] ∧ ∀ c ∈ w, c.isWhitespace = false) :
words (List.flatMap (fun (x : List Char) => x ++ [' ']) ws ++ rest) [] = ws ++ words rest []

The words of words each followed by a space.

theorem Cslib.Logic.PL.Dimacs.words_of_forall {w : List Char} (hne : w ≠ []) (hw : ∀ c ∈ w, c.isWhitespace = false) :

A nonempty run of characters other than whitespace is one word.

theorem Cslib.Logic.PL.Dimacs.ne_of_isDigit {d c : Char} (hd : d.isDigit = true) (hc : c.isDigit = false) :
d ≠ c

A decimal digit is not the character c of cs, for the characters that the reader treats specially.

Decimal digits without separators pass the separator check.

@[simp]

Reading the digits of a number.

theorem Cslib.Logic.PL.Dimacs.intOfChars?_of_ne {c : Char} {cs : List Char} (h : c ≠ '-') :
intOfChars? (c :: cs) = Option.map (fun (n : ℕ) => ↑n) (natOfChars? (c :: cs))

A word not starting with - is read as a natural number.

@[simp]

Reading the digits of an integer.

Reading a literal: the integer of its word names it.

theorem Cslib.Logic.PL.Dimacs.readLits_litChars {nvars : ℕ} {l : Literal ℕ} (hl : l.1 < nvars) (toks : List (List Char)) (cls : Array (Clause ℕ)) (cur : Array (Literal ℕ)) :
readLits nvars (litChars l :: toks) cls cur = readLits nvars toks cls (cur.push l)

Reading the word of a literal adds it to the clause in progress.

theorem Cslib.Logic.PL.Dimacs.readLits_clause {nvars : ℕ} {cl : Clause ℕ} (hcl : ∀ l ∈ cl, l.1 < nvars) (cls : Array (Clause ℕ)) (cur : Array (Literal ℕ)) :
readLits nvars (List.map litChars cl ++ [['0']]) cls cur = Except.ok (cls.push (cur.toList ++ cl), #[])

Reading a clause line.

The word of a literal is not empty.

The words of a clause line.

theorem Cslib.Logic.PL.Dimacs.exists_words_clause_eq_cons (cl : Clause ℕ) :
∃ (tok : List Char) (toks : List (List Char)), List.map litChars cl ++ [['0']] = tok :: toks ∧ ∀ c ∈ tok.head?, c = '-' ∨ c.isDigit = true

The first word of a clause line starts with a digit or -.

theorem Cslib.Logic.PL.Dimacs.splitOn_newline {a : List Char} (b : List Char) (h : ∀ c ∈ a, c ≠ '\n') :
List.splitOn '\n' (a ++ '\n' :: b) = a :: List.splitOn '\n' b

Splitting at the first newline.

theorem Cslib.Logic.PL.Dimacs.ne_newline_of_mem_clause {cl : Clause ℕ} {c : Char} (h : c ∈ List.flatMap (fun (x : Literal ℕ) => litChars x ++ [' ']) cl ++ ['0']) :
c ≠ '\n'

A clause line has no newline before its end.

Reading the clause lines.

theorem Cslib.Logic.PL.CNF.lt_dimacsVars {φ : CNF ℕ} {v : ℕ} (h : v ∈ φ.vars) :

The variables of a formula are below its number of variables.

Reading what is written gives the formula back, for every formula.