The state and moves of the CDCL solver #
The state of the executable CDCL solver (Cslib.Algorithms.Lean.CDCL.solve), an instance of
abstract DPLL with learning (Cslib.Algorithms.Lean.CDCL.Step), with its moves and their
invariant. Finding a conflict or a unit clause is left to the solver, which uses watched literals.
State. The solver keeps a trail of decisions and of implied literals carrying the number of
their reason, a clause of the log (Cslib.Algorithms.Lean.CDCL.XEntry), the numbered clauses of
its database, and an LRAT certificate under construction (Cslib.Logic.PL.CNF.LRATLog). Its
invariant (Cslib.Algorithms.Lean.CDCL.ExecL.Inv) is the invariant of the abstract system together
with:
- every reason contains its literal, and its other literals are false further down the trail
(
Cslib.Algorithms.Lean.CDCL.Reasons); - every clause of the database is in the log under its number;
- no clause of the database is false on the part of the trail below a decision;
- the numbered clauses of the input stay in the database.
Representation. The solver's state Cslib.Algorithms.Lean.CDCL.Exec keeps the trail in two
arrays of small numbers, oldest first: the code of each literal and the code of its reason
(Cslib.Algorithms.Lean.CDCL.ATrail). Numbers in the scalar range of Lean's runtime are unboxed,
so an entry makes no object; the arrays are written in place when they are not shared, growing or
copying otherwise. Backjump and Restart pop the entries undone, and conflict analysis and the
backjump read the arrays position by position. The
trail read back as a list of entries, newest first (Cslib.Algorithms.Lean.CDCL.ATrail.toX), gives
the state Cslib.Algorithms.Lean.CDCL.ExecL (Cslib.Algorithms.Lean.CDCL.Exec.toL), on which the
moves are specified and the invariant is stated (Cslib.Algorithms.Lean.CDCL.Exec.Inv). Each move
on arrays has a representation lemma (Cslib.Algorithms.Lean.CDCL.Exec.toL_propagate,
Cslib.Algorithms.Lean.CDCL.Exec.toL_afterConflict, …), through which the invariant carries over.
Moves.
- If a clause is false and the trail has no decision, resolving it with the reasons of the trail,
newest first, derives the empty clause (
Cslib.Algorithms.Lean.CDCL.refute). The resolvents are not built: the variables met are marked, and the certificate is the log closed by one empty-clause step hinting the reasons resolved, oldest first, and the conflict clause (Cslib.Algorithms.Lean.CDCL.Refutation.cert). - If a clause is false and the trail has a decision, first-UIP analysis
(
Cslib.Algorithms.Lean.CDCL.analyze) resolves it with the reasons of the newest implied literals until exactly one of its literals is false at the current level. As in MiniSat, the resolvents are not built: the variables met are marked in an array, the literals below the current level are collected, and the marked variables of the current level not yet passed are counted (Cslib.Algorithms.Lean.CDCL.Marks). The invariant of the analysis guarantees an asserting clause: the count is never zero, and at the decision only the decision's variable can be counted. It also guarantees the hints of the learned clause, the reasons resolved, oldest first, and then the conflict clause: they refute the clause that the marks describe (Cslib.Logic.PL.CNF.RupHints), which is the resolvent so far. The solver backjumps, as MiniSat does, to the second-highest level of the learned clause: below the newest decision and every level below it with no variable of the clause (Cslib.Algorithms.Lean.CDCL.ExecL.jump, without decision levels). It adds the asserting literal with the learned clause as its reason, and learns the clause, adding it to the log as one RUP step (Backjump and Learn,Cslib.Algorithms.Lean.CDCL.Exec.afterConflict). - UnitPropagate (
Cslib.Algorithms.Lean.CDCL.Exec.propagate) and Decide (Cslib.Algorithms.Lean.CDCL.Exec.decide). - Restart (
Cslib.Algorithms.Lean.CDCL.Exec.restart), back to the literals implied below every decision. - Forget (
Cslib.Algorithms.Lean.CDCL.Exec.forget): deleting clauses other than the reasons of the trail from the database and the log, input clauses only if a literal of the root of the trail, implied without any decision, satisfies them (Cslib.Algorithms.Lean.CDCL.Exec.forget_inv). The reasons kept are in the database and entail the root (Cslib.Algorithms.Lean.CDCL.Reasons.eval_of_no_decision), so under total assignments the remaining clauses entail the input clauses, and with them the clauses deleted. For partial assignments the guarantee concerns the input only: a partial model of the remaining clauses satisfies the input clauses through the root. It is not shown to satisfy the deleted clauses.
Every move other than Restart and Forget is a Basic DPLL step, or Backjump followed by Learn, of
the abstract system, and so lowers its rank (Cslib.Algorithms.Lean.CDCL.rank); Forget keeps it.
Every learned clause enters the certificate as one RUP step whose hints are proved to propagate to
a conflict, so the LRUP checker accepts the certificate by construction, with no check at run time
(Cslib.Logic.PL.CNF.LRATLog.checkLRUP_finish).
Scope. Exec does not choose decisions: the solver with watched literals chooses them
(Cslib.Algorithms.Lean.CDCL.Activity).
Main definitions #
Cslib.Algorithms.Lean.CDCL.Exec,Cslib.Algorithms.Lean.CDCL.Exec.Inv: the state and its invariant, with the trail as arrays (Cslib.Algorithms.Lean.CDCL.ATrail), read back asCslib.Algorithms.Lean.CDCL.ExecL.Cslib.Algorithms.Lean.CDCL.analyze: first-UIP conflict analysis, with its marksCslib.Algorithms.Lean.CDCL.Marks, and the learned clauseCslib.Algorithms.Lean.CDCL.Learned.Cslib.Algorithms.Lean.CDCL.ExecL.jump: the backjump to the second-highest level of the learned clause, found on the arrays byCslib.Algorithms.Lean.CDCL.ATrail.cutPos(Cslib.Algorithms.Lean.CDCL.Exec.jump_eq).Cslib.Algorithms.Lean.CDCL.refute: the certificate when there is no decision.
Main results #
Cslib.Algorithms.Lean.CDCL.Exec.afterConflict_spec,Cslib.Algorithms.Lean.CDCL.Exec.propagate_spec,Cslib.Algorithms.Lean.CDCL.Exec.decide_spec: the moves keep the invariant and lower the rank.Cslib.Algorithms.Lean.CDCL.Exec.restart_inv,Cslib.Algorithms.Lean.CDCL.Exec.forget_inv: Restart and Forget keep the invariant.
References #
An entry of the solver's trail: a decision, or an implied literal with the number of its reason clause in the log.
- decision (l : Logic.PL.Literal ℕ) : XEntry
A decision.
- implied (l : Logic.PL.Literal ℕ) (reason : ℕ) : XEntry
An implied literal and the number of its reason.
The solver's trail, newest first.
The abstract trail of the solver's trail.
The literal of an entry of the solver's trail.
Whether an entry is a decision, read on the entry.
The code of the reason of an entry: 0 for a decision, and r + 1 for an implied literal
whose reason is numbered r.
The trail as arrays #
The variable of a literal is its key halved.
The solver's trail as two arrays, oldest first: the code of each literal (litCode), and
the code of the reason of each entry (Cslib.Algorithms.Lean.CDCL.XEntry.rsnCode). Codes in the
scalar range of Lean's runtime are unboxed, so an entry makes no object; push and shrink write
the arrays in place when they are not shared (and push has room), and grow or copy them
otherwise. Backjumping pops the entries undone. The trail read back as a list of entries, newest
first, is Cslib.Algorithms.Lean.CDCL.ATrail.toX, on which the invariants are stated.
The codes of the literals, oldest first.
The codes of the reasons, oldest first.
The literal at position i.
The entry at position i.
The trail read back as a list of entries, newest first.
Adding an entry, given by the code of its literal and of its reason.
Popping k elements, in place if the array is not shared. Unlike Array.shrink, whose body is
not exposed, the kernel can unfold it, so that decide +kernel runs the solver.
Keeping the first q entries, by popping the others, in place if the arrays are not
shared.
A loop down the positions lo + k - 1 to lo, newest first, reading the codes of each
entry: the fold of the entries T.seg lo k (ATrail.foldDown_eq).
A loop up the positions lo to lo + k - 1, oldest first, reading the codes of each entry:
the right fold of the entries T.seg lo k (ATrail.foldUp_eq).
The reason code (0 or r + 1) of v among the entries below position p, looked up in
constant time through the hint array vpos (vpos[v] giving a candidate position). Whatever
vpos holds, a non-zero answer r + 1 certifies an implied entry on v with reason r in
T.seg 0 p (ATrail.mem_seg_of_reasonAt).
Every implied literal of the trail has a reason clause in the log, containing it, whose other literals are false further down the trail; decisions have no reason.
- nil {φ : Logic.PL.CNF ℕ} {log : φ.LRATLog} : Reasons log []
- decision {φ : Logic.PL.CNF ℕ} {log : φ.LRATLog} {M : XTrail} (l : Logic.PL.Literal ℕ) : Reasons log M → Reasons log (XEntry.decision l :: M)
- implied {φ : Logic.PL.CNF ℕ} {log : φ.LRATLog} {M : XTrail} (l : Logic.PL.Literal ℕ) (r : ℕ) {R : Logic.PL.Clause ℕ} : Reasons log M → log.clause? r = some R → l ∈ R → (∀ l' ∈ R, l' ≠ l → Std.Sat.Literal.negate l' ∈ M.trail.lits) → Reasons log (XEntry.implied l r :: M)
The reason of an implied literal at the head of a trail.
An implied literal at the head of a trail has a reason.
The number of the reason of an entry, if it is implied.
The numbers of the reasons of a trail, MiniSat's locked clauses.
XTrail.reasonIds read directly from the rsns array of the trail without allocating an
XTrail list (ATrail.reasonIds_eq).
The negation is injective.
Conflict analysis with marks #
The state of conflict analysis, as MiniSat keeps it: the array of the variables seen, the
literals out of the analysed clause false below the current level, the number path of seen
variables of the current level not yet passed, and the variables seen, newest first.
- seen : Assign
The variables seen, as an array.
- out : Logic.PL.Clause ℕ
The literals of the analysed clause false below the current level.
- path : ℕ
The seen variables of the current level not yet passed.
The variables seen, newest first.
Nothing seen, in a cleared assignment array seen.
The analysed clause of the state m, with the entries Cur of the current level still to
pass: the literals of out, and the negations of the literals of Cur whose variables are seen.
It is never built; the hints of the learned clause refute it (Cslib.Logic.PL.CNF.RupHints).
The loop of Marks.absorb, with the four parts of the state as arguments, so that compiled
code builds no state per literal.
Adding the literals of a clause to the analysis: each literal on a variable not yet seen is
seen; it counts on the path if it is false on the current level, whose literals cur holds, and
goes to out otherwise.
The invariant of the analysis, with the entries Cur of the current level still to pass, the
trail M₀ below it, and the variables V of the formula: the literals of out are false below the
current level, and are those of the seen variables there; path counts the seen variables of
Cur; and vars lists the seen variables, of V.
- out_nodup : List.Nodup m.out
Adding a clause keeps the invariant, if the literals of the clause on variables not yet
seen are false on the current level or below; every variable of the clause is seen afterwards,
and nothing seen or in out is lost.
A learned clause: a clause all of whose literals but lit are false in the trail M₀, where
lit is undefined, and whose negation is among the literals A above M₀, with hints along which
unit propagation in the log refutes it. Adding it to the log
(Cslib.Algorithms.Lean.CDCL.Learned.log) is one RUP step.
- clause : Logic.PL.Clause ℕ
The learned clause.
- lit : Logic.PL.Literal ℕ
The asserting literal.
The hints of the RUP step: the reasons resolved, oldest first, and the conflict clause.
The variables met by the analysis: those of the conflict clause and of the reasons resolved. Branching heuristics bump them.
The numbers of the clauses met by the analysis: the conflict clause and the reasons resolved. Clause activities bump them.
- rup : Logic.PL.CNF.propagateHints log.db (List.map Std.Sat.Literal.negate self.clause) self.hints = true
- false_below (y : Logic.PL.Literal ℕ) : y ∈ self.clause → y ≠ self.lit → Std.Sat.Literal.negate y ∈ M₀.lits
- marks : Assign
The assignment array the analysis was reading when it stopped, returned so that the solver can clear it and use it again: it has values only for variables of
A. The
seenarray of the analysis, cleared of its marked variables so that minimization and later conflicts reuse it without allocating.- nodup : List.Nodup self.clause
The learned clause has no repeated literal, so that the solver need not remove repetitions.
The asserting literal comes first, the literal the watch store watches first.
The variables met are listed once each, so that bumping them need not remove repetitions.
The log with the learned clause added, as one RUP step.
The number of the learned clause in L.log.
The learned clause at the first unique implication point l, the entry of the current
level ⟨l, b⟩ :: Cur whose variable is the only one seen there: the negation of l and the
literals out.
The current level read back: the entries above position p, then the decision at p.
First-UIP conflict analysis, as MiniSat's, on the trail T with its newest decision at
position p: with the k implied literals over it still to pass, at positions p + 1 to p + k,
and the trail below it, the state m marks the analysed clause, which the hints refute. At an entry
whose variable is seen, the analysis stops if it is the only seen variable left on the level, its
first unique implication point, and otherwise adds its reason, whose number it prepends to the
hints; entries whose variables are not seen are passed. The entries are read from the arrays of
the trail, position by position, so no list is built; the entries read back (ATrail.seg) appear
only in the invariant. The literals of the current level are read from the assignment cur, which
agrees with them; passing an implied literal unassigns its variable.
First-UIP conflict analysis on the trail T, whose newest decision is at position p:
with N, the implied literals over it, and M₀, the trail below it, read back
(ATrail.seg), the clause numbered i is false and has a literal false at the current level. Its
literals are marked (Cslib.Algorithms.Lean.CDCL.Marks.absorb), and the analysis walks down the
level, adding the reasons of the entries whose variables are seen, until one seen variable is left
on the level, its first unique implication point, which is reached at the latest at the decision.
The learned clause is never false at the current level elsewhere, and the hints, the reasons
added, oldest first, and then the clause i, refute it by unit propagation. The variables seen are
marked in the cleared scratch array seen, which is cleared again and returned in
Learned.seenMarks.
A refutation in the log: hints along which unit propagation in the log reaches a conflict from no assumption.
- log : φ.LRATLog
The log.
The hints of the empty-clause step.
The certificate of a refutation: its log closed with an empty-clause step.
The hints refuting a conflict with no decision, the trail T still to pass: at an implied
literal whose variable is seen, its reason is hinted, first, and its variables are seen.
Refuting a conflict with no decision: the clause numbered i, falsified by a trail of
implied literals, is hinted last, after the reasons of the entries of the trail whose variables
it or a reason hinted contains, oldest first.
The root of a trail, in one tail-recursive pass from the newest entry: acc is the part below
the oldest decision seen so far, or the whole trail if there is none.
The root of a trail: the part below its oldest decision, the literals implied without any decision. It is found in one tail-recursive pass over the trail.
Below a decision, the root is that of the trail below it.
The position of the oldest decision, or the size of the trail if there is none: the
entries below it are the root of the trail (ATrail.root_seg).
The root of the trail read back is the part below the oldest decision.
The reasons make the root true in partial models: on a trail without decisions, every partial model of numbered clauses containing the reasons makes every literal of the trail true.
The reasons entail the root: on a trail without decisions, in every model of numbered
clauses containing the reasons, every literal holds. The total assignment σ is the partial
assignment fun v => some (σ v), so this is Reasons.psat_of_no_decision.
The solver state #
A trail split at its newest decision: the implied literals above it, the decision, and the trail below it.
- above : XTrail
The implied literals above the decision.
- decision : Logic.PL.Literal ℕ
The decision literal.
- below : XTrail
The trail below the decision.
The split of a trail at its newest decision, if it has one.
The split of the trail at the position p of its newest decision, read back. It appears only
in statements: the solver reads the arrays.
The state of the solver read back, with the trail as a list of entries: its trail, the
numbered clauses of the database, and the log. The moves are specified, and the invariant stated,
on it; the solver's state Cslib.Algorithms.Lean.CDCL.Exec keeps the trail as arrays.
The clauses of the database.
The abstract state of a solver state.
The invariant of the solver: the abstract invariant, the reasons of the trail, the numbers
of the database in the log, no clause of the database false below a decision, the numbered clauses
of φ in the database unless the root satisfies them, and the reasons in the database.
The numbered clauses of
φstay in the database, unless a literal of the root of the trail satisfies them.The reasons of the trail are clauses of the database.
The initial state: the empty trail, the clauses of φ with their numbers, and the empty log.
In the numbering of checkLRUP, each number names its own clause.
The clauses of the initial state are those of φ.
Steps of the solver #
The variables of the current level, from the newest decision up, are distinct.
A clause false on the trail but not below its newest decision has a literal false at the current level.
A clause learned at a conflict, above and below the newest decision of the split sp.
Walking down the trail for XTrail.dropLevels: acc holds the entries of the current level
seen so far, newest last, and hit whether one of them is marked. At a decision, the level is
dropped if neither it nor its entries are marked, and otherwise kept with everything below; at the
bottom, the entries left are the root level, which is kept.
The entries dropped and the trail kept make up the trail; the entries dropped end with a decision, if any, and none of their variables is marked; and the trail kept is the root, or its newest level has a marked variable, in an implied entry or in the decision.
The backjump of a conflict at the split sp, as MiniSat's: the entries undone, the newest
decision level and the levels below it with no marked variable, and the trail kept. With the
variables of the learned clause marked, the trail kept is the trail at the second highest level of
the learned clause (Cslib.Algorithms.Lean.CDCL.ExecL.jump_kept). The cost is that of reading the
levels undone and the first level kept.
The entries undone end with a decision, and the learned clause is asserting on the trail kept.
The target of the backjump: the trail kept is the root, or its newest level has a marked
variable, in an implied entry or in the decision. With exactly the variables of the learned clause
marked, and none in the levels undone (Cslib.Algorithms.Lean.CDCL.XTrail.dropLevels_spec), this
is MiniSat's target: the highest level of the learned clause's literals other than the asserting
one, or the root for a unit clause.
The state after Backjump and Learn, back to the trail K with the asserting literal implied by
the learned clause, which is added to the log.
The learned clause of a conflict, and the state after Backjump and Learn: back to the trail
kept by ExecL.jump, with the asserting literal implied by the learned clause, which is added to
the log as one RUP step.
Backjump and Learn keep the invariant and lower the rank.
A unit clause of the database under the assignment A: a numbered clause with every literal but
one false, that literal being unassigned.
UnitPropagate: add the literal of a unit clause, with that clause as its reason.
UnitPropagate with a clause of the database keeps the invariant and lowers the rank.
Decide: add the literal of the variable x with polarity b as a decision.
Restarts #
Restart: going back to the root of the trail, keeping the database and the log.
Restart keeps the invariant.
Forgetting clauses #
The numbers of the clauses of φ are at most its length.
Every clause of φ has a number.
Forget: deleting the clauses numbered ids from the database and the log.
Forgetting clauses keeps the invariant, if they are not reasons of the trail, and the
clauses of φ among them are satisfied by the root of the trail. The reasons kept entail the root
(Cslib.Algorithms.Lean.CDCL.Reasons.eval_of_no_decision), so the remaining clauses still have the
models of φ, and their partial models, through the root, satisfy φ. This does not show that
each removed clause is partially entailed by the remaining ones, the side condition of the abstract
Forget rule; the invariant does not need it.
The solver state on arrays #
Scanning down for the backjump from position i (exclusive): cut is the lowest position of
the levels dropped so far, and hit whether the level being read has a marked variable. At a
decision, the scan stops if the level has a marked variable, its decision's included, and otherwise
the level is dropped.
The backjump point below the decision at position p: the entries from it up are undone.
It is what XTrail.dropLevels finds on the trail below p read back
(ATrail.dropLevels_seg).
The backjump point read back: XTrail.dropLevels on the trail below p drops the entries
from the backjump point to p, and keeps those below it.
The state of the solver: its trail as arrays (Cslib.Algorithms.Lean.CDCL.ATrail), the
numbered clauses of the database, and the log. Its moves and its invariant are those of the state
ExecL it reads back as (Cslib.Algorithms.Lean.CDCL.Exec.toL): each move has a representation
lemma, through which the invariants carry over.
The state read back, with the trail as a list of entries.
The trail read back as a list of entries, newest first.
The clauses of the database.
The abstract state.
The invariant, ExecL.Inv of the state read back.
A clause learned at a conflict, above and below the newest decision of the split sp: the
type ExecL.LearnedAt of the state read back, stated on the log itself, so that compiled code
reading the log of a learned clause does not read the trail back.
The initial state: the empty trail, the clauses of φ with their numbers, and the empty
log.
UnitPropagate: the literal of a unit clause pushed, with that clause as its reason.
Decide: the literal of the variable x with polarity b pushed as a decision.
Restart: popping back to the oldest decision, keeping the root of the trail.
Forget: deleting the clauses numbered ids from the database and the log.
The state after Backjump and Learn: the trail popped down to q entries, the asserting
literal pushed with the learned clause as its reason, and the clause added to the log.
Backjump and Learn, at the newest decision p: back to the trail kept by ExecL.jump,
popping the entries undone, with the asserting literal implied by the learned clause.
The backjump read back: the entries undone are those from the backjump point up, and the trail kept is the one below it.
Backjump and Learn keep the invariant and lower the rank.