Documentation

CslibFrontier.Algorithms.Lean.CDCL.Exec

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:

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.

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 #

Main results #

References #

An entry of the solver's trail: a decision, or an implied literal with the number of its reason clause in the log.

The entry of the abstract trail.

@[simp]
@[reducible, inline]

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.

@[inline]

The literal at position i.

The entry at position i.

The entries at positions lo to lo + k - 1, newest first.

The trail read back as a list of entries, newest first.

@[inline]

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.

@[inline]

Keeping the first q entries, by popping the others, in place if the arrays are not shared.

@[specialize #[]]
def Cslib.Algorithms.Lean.CDCL.ATrail.foldDown {β : Type} (T : ATrail) (f : β → ℕ → ℕ → β) (lo : ℕ) :
ℕ → β → β

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

@[specialize #[]]
def Cslib.Algorithms.Lean.CDCL.ATrail.foldUp {β : Type} (T : ATrail) (f : β → ℕ → ℕ → β) :
ℕ → ℕ → β → β

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 entry at a position holding the codes of e is e.

theorem Cslib.Algorithms.Lean.CDCL.ATrail.entry_congr {T T' : ATrail} {i : ℕ} (hl : T'.lits.getD i 0 = T.lits.getD i 0) (hr : T'.rsns.getD i 0 = T.rsns.getD i 0) :
T'.entry i = T.entry i

Two trails with the same codes at a position have the same entry there.

theorem Cslib.Algorithms.Lean.CDCL.ATrail.seg_succ (T : ATrail) (lo k : ℕ) :
T.seg lo (k + 1) = T.entry (lo + k) :: T.seg lo k
theorem Cslib.Algorithms.Lean.CDCL.ATrail.seg_add (T : ATrail) (lo a b : ℕ) :
T.seg lo (a + b) = T.seg (lo + a) b ++ T.seg lo a

Cutting a segment: the newer b entries, then the older a.

theorem Cslib.Algorithms.Lean.CDCL.ATrail.seg_succ_bottom (T : ATrail) (lo k : ℕ) :
T.seg lo (k + 1) = T.seg (lo + 1) k ++ [T.entry lo]
theorem Cslib.Algorithms.Lean.CDCL.ATrail.mem_seg {T : ATrail} {lo k : ℕ} {x : XEntry} :
x ∈ T.seg lo k ↔ ∃ (i : ℕ), lo ≤ i ∧ i < lo + k ∧ T.entry i = x
theorem Cslib.Algorithms.Lean.CDCL.ATrail.seg_congr {T T' : ATrail} {lo k : ℕ} (h : ∀ (i : ℕ), lo ≤ i → i < lo + k → T'.entry i = T.entry i) :
T'.seg lo k = T.seg lo k

Segments of two trails agreeing on their positions are equal.

theorem Cslib.Algorithms.Lean.CDCL.ATrail.foldDown_eq {β : Type} (T : ATrail) (f : β → ℕ → ℕ → β) (lo k : ℕ) (b : β) :
T.foldDown f lo k b = List.foldl (fun (b : β) (e : XEntry) => f b (WordRAM.ClauseCode.litCode e.lit) e.rsnCode) b (T.seg lo k)
theorem Cslib.Algorithms.Lean.CDCL.ATrail.foldUp_eq {β : Type} (T : ATrail) (f : β → ℕ → ℕ → β) (lo k : ℕ) (b : β) :
T.foldUp f lo k b = List.foldr (fun (e : XEntry) (b : β) => f b (WordRAM.ClauseCode.litCode e.lit) e.rsnCode) b (T.seg lo k)
@[simp]
theorem Cslib.Algorithms.Lean.CDCL.ATrail.entry_push_lt (T : ATrail) {c r i : ℕ} (h : i < T.size) :
(T.push c r).entry i = T.entry i

Pushing an entry puts it at the head of the trail read back.

theorem Cslib.Algorithms.Lean.CDCL.ATrail.toX_shrink (T : ATrail) {q : ℕ} (h : q ≤ T.size) :
(T.shrink q).toX = T.seg 0 q

Popping down to q entries keeps the oldest q.

@[inline]

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

theorem Cslib.Algorithms.Lean.CDCL.ATrail.mem_seg_of_reasonAt (T : ATrail) (vpos : Array ℕ) {p v r : ℕ} (h : T.reasonAt vpos p v = r + 1) :
∃ (b : Bool), XEntry.implied (v, b) r ∈ T.seg 0 p

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.

theorem Cslib.Algorithms.Lean.CDCL.Reasons.mono {φ : Logic.PL.CNF ℕ} {log log' : φ.LRATLog} (hext : log.Extends log') {M : XTrail} (h : Reasons log M) :
Reasons log' M
theorem Cslib.Algorithms.Lean.CDCL.Reasons.tail {φ : Logic.PL.CNF ℕ} {log : φ.LRATLog} {x : XEntry} {M : XTrail} (h : Reasons log (x :: M)) :
Reasons log M
theorem Cslib.Algorithms.Lean.CDCL.Reasons.reason {φ : Logic.PL.CNF ℕ} {log : φ.LRATLog} {l : Logic.PL.Literal ℕ} {r : ℕ} {M : XTrail} {R : Logic.PL.Clause ℕ} (h : Reasons log (XEntry.implied l r :: M)) (hR : log.clause? r = some R) :
l ∈ R ∧ ∀ l' ∈ R, l' ≠ l → Std.Sat.Literal.negate l' ∈ M.trail.lits

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

theorem Cslib.Algorithms.Lean.CDCL.Reasons.lt_next {φ : Logic.PL.CNF ℕ} {log : φ.LRATLog} {M : XTrail} (h : Reasons log M) (r : ℕ) :
r ∈ M.reasonIds → r < log.next

The reasons of a trail are numbers of the log, so below its next number.

theorem Cslib.Algorithms.Lean.CDCL.Reasons.delete {φ : Logic.PL.CNF ℕ} {log : φ.LRATLog} {ids : List ℕ} {M : XTrail} (h : Reasons log M) (hids : ∀ r ∈ M.reasonIds, r ∉ ids) :
Reasons (log.delete ids) M

Deleting clauses other than the reasons keeps the reasons.

theorem Cslib.Algorithms.Lean.CDCL.eq_of_mem_lits {M : Trail} (hM : M.vars.Nodup) {a b : Logic.PL.Literal ℕ} (ha : a ∈ M.lits) (hb : b ∈ M.lits) (h : a.1 = b.1) :
a = b

On a trail assigning each variable once, a literal is determined by its variable.

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.

  • The literals of the analysed clause false below the current level.

  • path : ℕ

    The seen variables of the current level not yet passed.

  • vars : List ℕ

    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.

theorem Cslib.Algorithms.Lean.CDCL.Marks.absorb_cons (cur : Assign) (m : Marks) (y : Logic.PL.Literal ℕ) (R : Logic.PL.Clause ℕ) :
absorb cur m (y :: R) = if m.seen.assigned y.1 = true then absorb cur m R else if cur.isFalse y = true then absorb cur { seen := m.seen.set y.1 (some true), out := m.out, path := m.path + 1, vars := y.1 :: m.vars } R else absorb cur { seen := m.seen.set y.1 (some true), out := y :: m.out, path := m.path, vars := y.1 :: m.vars } R
structure Cslib.Algorithms.Lean.CDCL.Marks.Inv (m : Marks) (Cur M₀ : Trail) (V : List ℕ) :

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.

theorem Cslib.Algorithms.Lean.CDCL.Marks.inv_ofClean {seen : Assign} (hseen : ∀ (v : ℕ), seen.value v = none) (Cur M₀ : Trail) (V : List ℕ) :
(ofClean seen).Inv Cur M₀ V
theorem Cslib.Algorithms.Lean.CDCL.Marks.Inv.clause_of_seen {m : Marks} {Cur M₀ : Trail} {V : List ℕ} (hm : m.Inv Cur M₀ V) {y : Logic.PL.Literal ℕ} (hs : m.seen.assigned y.1 = true) (hy : Std.Sat.Literal.negate y ∈ Cur.lits ∨ Std.Sat.Literal.negate y ∈ M₀.lits) :
m.clause Cur y
theorem Cslib.Algorithms.Lean.CDCL.Marks.absorb_spec {cur : Assign} {Cur M₀ : Trail} {V : List ℕ} (hcur : cur.Agrees Cur) (hnd : (Cur ++ M₀).vars.Nodup) (m : Marks) (R : Logic.PL.Clause ℕ) :
m.Inv Cur M₀ V → (∀ y ∈ R, y.1 ∈ V) → (∀ y ∈ R, m.seen.assigned y.1 = false → Std.Sat.Literal.negate y ∈ Cur.lits ∨ Std.Sat.Literal.negate y ∈ M₀.lits) → (absorb cur m R).Inv Cur M₀ V ∧ (∀ y ∈ R, (absorb cur m R).seen.assigned y.1 = true) ∧ (∀ (v : ℕ), m.seen.assigned v = true → (absorb cur m R).seen.assigned v = true) ∧ ∀ y ∈ m.out, y ∈ (absorb cur m R).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.

theorem Cslib.Algorithms.Lean.CDCL.Marks.Inv.pass {m : Marks} {e : Entry} {Cur M₀ : Trail} {V : List ℕ} (hm : m.Inv (e :: Cur) M₀ V) (he : m.seen.assigned e.lit.1 = false) :
m.Inv Cur M₀ V

Passing an entry of the current level: if its variable is not seen, nothing changes.

theorem Cslib.Algorithms.Lean.CDCL.Marks.Inv.resolve {m : Marks} {e : Entry} {Cur M₀ : Trail} {V : List ℕ} (hm : m.Inv (e :: Cur) M₀ V) (he : m.seen.assigned e.lit.1 = true) :
{ seen := m.seen, out := m.out, path := m.path - 1, vars := m.vars }.Inv Cur M₀ V

Passing an entry of the current level whose variable is seen takes one off the path.

structure Cslib.Algorithms.Lean.CDCL.Learned {φ : Logic.PL.CNF ℕ} (log : φ.LRATLog) (A M₀ : Trail) :

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.

def Cslib.Algorithms.Lean.CDCL.Learned.log {φ : Logic.PL.CNF ℕ} {log₀ : φ.LRATLog} {A M₀ : Trail} (L : Learned log₀ A M₀) :

The log with the learned clause added, as one RUP step.

def Cslib.Algorithms.Lean.CDCL.Learned.id {φ : Logic.PL.CNF ℕ} {log₀ : φ.LRATLog} {A M₀ : Trail} (_L : Learned log₀ A M₀) :

The number of the learned clause in L.log.

theorem Cslib.Algorithms.Lean.CDCL.Learned.clause?_eq {φ : Logic.PL.CNF ℕ} {log₀ : φ.LRATLog} {A M₀ : Trail} (L : Learned log₀ A M₀) :
theorem Cslib.Algorithms.Lean.CDCL.Learned.ext {φ : Logic.PL.CNF ℕ} {log₀ : φ.LRATLog} {A M₀ : Trail} (L : Learned log₀ A M₀) :
log₀.Extends L.log
def Cslib.Algorithms.Lean.CDCL.Marks.learned {φ : Logic.PL.CNF ℕ} (log : φ.LRATLog) {A M₀ Cur : Trail} (m : Marks) (l : Logic.PL.Literal ℕ) (b : Bool) (hm : m.Inv ({ lit := l, decision := b } :: Cur) M₀ φ.vars) (hl : m.seen.assigned l.1 = true) (hp : m.path = 1) (hints : List ℕ) (hh : Logic.PL.CNF.RupHints log.db (m.clause ({ lit := l, decision := b } :: Cur)) hints) (hlA : l ∈ A.lits) (hlM : l.1 ∉ M₀.vars) (cur : Assign) (hcur : ∀ v ∉ A.vars, cur.value v = none) :
Learned log A M₀

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.

theorem Cslib.Algorithms.Lean.CDCL.trail_level (N M₀ : XTrail) (d : Logic.PL.Literal ℕ) :
(N ++ XEntry.decision d :: M₀).trail = N.trail ++ [{ lit := d, decision := true }] ++ M₀.trail

The trail split at a decision, the current level first.

The current level read back: the entries above position p, then the decision at p.

def Cslib.Algorithms.Lean.CDCL.analyzeGo {φ : Logic.PL.CNF ℕ} (log : φ.LRATLog) (T : ATrail) (p k : ℕ) :
((T.seg (p + 1) k).trail ++ [{ lit := T.lit p, decision := true }]).lits ⊆ (T.level p).lits → (cur : Assign) → cur.Agrees ((T.seg (p + 1) k).trail ++ [{ lit := T.lit p, decision := true }]) → (m : Marks) → m.Inv ((T.seg (p + 1) k).trail ++ [{ lit := T.lit p, decision := true }]) (T.seg 0 p).trail φ.vars → 1 ≤ m.path → (hints : List ℕ) → Logic.PL.CNF.RupHints log.db (m.clause ((T.seg (p + 1) k).trail ++ [{ lit := T.lit p, decision := true }])) hints → Reasons log (T.seg (p + 1) k ++ XEntry.decision (T.lit p) :: T.seg 0 p) → (∀ x ∈ T.seg (p + 1) k, x.entry.decision = false) → (T.seg (p + 1) k ++ XEntry.decision (T.lit p) :: T.seg 0 p).trail.vars.Nodup → Learned log (T.level p) (T.seg 0 p).trail

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.

def Cslib.Algorithms.Lean.CDCL.analyze {φ : Logic.PL.CNF ℕ} (log : φ.LRATLog) (T : ATrail) (p : ℕ) (cur : Assign) (hcurA : cur.Agrees (T.level p)) (seen : { A : Assign // ∀ (v : ℕ), A.value v = none }) (i : ℕ) (C : Logic.PL.Clause ℕ) (hC : log.clause? i = some C) (hreasons : Reasons log (T.seg (p + 1) (T.size - (p + 1)) ++ XEntry.decision (T.lit p) :: T.seg 0 p)) (hdec : ∀ x ∈ T.seg (p + 1) (T.size - (p + 1)), x.entry.decision = false) (hfalse : ∀ y ∈ C, Std.Sat.Literal.negate y ∈ (T.seg (p + 1) (T.size - (p + 1)) ++ XEntry.decision (T.lit p) :: T.seg 0 p).trail.lits) (hcur : ∃ y ∈ C, Std.Sat.Literal.negate y ∈ (T.level p).lits) (hnd : (T.seg (p + 1) (T.size - (p + 1)) ++ XEntry.decision (T.lit p) :: T.seg 0 p).trail.vars.Nodup) :
Learned log (T.level p) (T.seg 0 p).trail

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.

The certificate of a refutation: its log closed with an empty-clause step.

def Cslib.Algorithms.Lean.CDCL.refuteGo {φ : Logic.PL.CNF ℕ} (log : φ.LRATLog) (T : XTrail) (seen : Assign) (hints : List ℕ) :
Logic.PL.CNF.RupHints log.db (fun (y : Logic.PL.Literal ℕ) => seen.assigned y.1 = true ∧ Std.Sat.Literal.negate y ∈ T.trail.lits) hints → Reasons log T → (∀ x ∈ T, x.entry.decision = false) → Refutation φ

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.

def Cslib.Algorithms.Lean.CDCL.refute {φ : Logic.PL.CNF ℕ} (T : XTrail) (log : φ.LRATLog) (i : ℕ) (C : Logic.PL.Clause ℕ) (hC : log.clause? i = some C) (hreasons : Reasons log T) (hdec : ∀ x ∈ T, x.entry.decision = false) (hfalse : ∀ y ∈ C, Std.Sat.Literal.negate y ∈ T.trail.lits) :

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.

theorem Cslib.Algorithms.Lean.CDCL.XTrail.root_split (M : XTrail) :
M.root = M ∨ ∃ (N : Trail) (d : Logic.PL.Literal ℕ), M.trail = N ++ { lit := d, decision := true } :: M.root.trail

The root is the whole trail, or the part below a decision.

Below a decision, the root is that of the trail below it.

The root only grows when an entry is added.

Scanning up from position i, through k positions: the first decision, or i + k if there is none.

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

theorem Cslib.Algorithms.Lean.CDCL.ATrail.firstDecGo_spec (T : ATrail) (i k : ℕ) :
i ≤ T.firstDecGo i k ∧ T.firstDecGo i k ≤ i + k ∧ (∀ (j : ℕ), i ≤ j → j < T.firstDecGo i k → T.rsns.getD j 0 ≠ 0) ∧ (T.firstDecGo i k < i + k → T.rsns.getD (T.firstDecGo i k) 0 = 0)
theorem Cslib.Algorithms.Lean.CDCL.ATrail.root_seg (T : ATrail) (k : ℕ) :
(T.seg 0 k).root = T.seg 0 (T.firstDecGo 0 k)

The root of the trail read back is the part below the oldest decision.

theorem Cslib.Algorithms.Lean.CDCL.Reasons.psat_of_no_decision {φ : Logic.PL.CNF ℕ} {log : φ.LRATLog} {db : List (ℕ × Logic.PL.Clause ℕ)} (hdb : ∀ e ∈ db, log.clause? e.1 = some e.2) {ρ : ℕ → Option Bool} (hρ : ∀ e ∈ db, PSat ρ e.2) {M : XTrail} (h : Reasons log M) (hnodec : ∀ x ∈ M, x.entry.decision = false) (hR : ∀ r ∈ M.reasonIds, ∃ (R : Logic.PL.Clause ℕ), (r, R) ∈ db) (l : Logic.PL.Literal ℕ) :
l ∈ M.trail.lits → ρ l.1 = some l.2

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.

theorem Cslib.Algorithms.Lean.CDCL.Reasons.eval_of_no_decision {φ : Logic.PL.CNF ℕ} {log : φ.LRATLog} {db : List (ℕ × Logic.PL.Clause ℕ)} (hdb : ∀ e ∈ db, log.clause? e.1 = some e.2) {σ : ℕ → Bool} (hσ : ∀ e ∈ db, Logic.PL.Clause.eval σ e.2 = true) {M : XTrail} (h : Reasons log M) (hnodec : ∀ x ∈ M, x.entry.decision = false) (hR : ∀ r ∈ M.reasonIds, ∃ (R : Logic.PL.Clause ℕ), (r, R) ∈ db) (l : Logic.PL.Literal ℕ) :

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.

The split of a trail at its newest decision, if it has one.

The newest decision among the first k positions, found by scanning down from k - 1.

Position p holds the newest decision of the trail.

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 initial state: the empty trail, the clauses of φ with their numbers, and the empty log.

theorem Cslib.Algorithms.Lean.CDCL.find?_zipIdx_map {e : ℕ × Logic.PL.Clause ℕ} (φ : Logic.PL.CNF ℕ) (n : ℕ) :
e ∈ List.map (fun (p : Logic.PL.Clause ℕ × ℕ) => (p.2 + 1, p.1)) (List.zipIdx φ n) → List.find? (fun (x : ℕ × Logic.PL.Clause ℕ) => x.1 == e.1) (List.map (fun (p : Logic.PL.Clause ℕ × ℕ) => (p.2 + 1, p.1)) (List.zipIdx φ n)) = some e

In the numbering of checkLRUP, each number names its own clause.

The clauses of the initial state are those of φ.

Steps of the solver #

theorem Cslib.Algorithms.Lean.CDCL.XTrail.trail_of_split {M : XTrail} {sp : Split} (hsp : M.split = some sp) :
M.trail = sp.above.trail ++ { lit := sp.decision, decision := true } :: sp.below.trail
theorem Cslib.Algorithms.Lean.CDCL.ExecL.Inv.split_level_nodup {φ : Logic.PL.CNF ℕ} {S : ExecL φ} (hS : S.Inv) {sp : Split} (hsp : S.trail.split = some sp) :
(sp.above.trail ++ [{ lit := sp.decision, decision := true }]).vars.Nodup

The variables of the current level, from the newest decision up, are distinct.

theorem Cslib.Algorithms.Lean.CDCL.exists_cur {A B : Trail} {d : Logic.PL.Literal ℕ} {c : Logic.PL.Clause ℕ} (hconf : (A ++ { lit := d, decision := true } :: B).Falsifies c) (hnb : ¬B.Falsifies c) :
∃ y ∈ c, Std.Sat.Literal.negate y ∈ (A ++ [{ lit := d, decision := true }]).lits

A clause false on the trail but not below its newest decision has a literal false at the current level.

theorem Cslib.Algorithms.Lean.CDCL.ExecL.Inv.split_cur {φ : Logic.PL.CNF ℕ} {S : ExecL φ} (hS : S.Inv) {sp : Split} (hsp : S.trail.split = some sp) {e : ℕ × Logic.PL.Clause ℕ} (he : e ∈ S.db) (hconf : S.trail.trail.Falsifies e.2) :
∃ y ∈ e.2, Std.Sat.Literal.negate y ∈ (sp.above.trail ++ [{ lit := sp.decision, decision := true }]).lits
@[reducible, inline]

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.

Dropping the newest levels of the trail M none of whose variables is marked: the entries dropped, and the trail kept. It reads the levels dropped and the first level kept, entry by entry.

theorem Cslib.Algorithms.Lean.CDCL.XTrail.dropLevelsGo_spec (marked : ℕ → Bool) (acc : XTrail) (hit : Bool) (M : XTrail) :
(∀ x ∈ acc, x.entry.decision = false) → (hit = List.any acc fun (x : XEntry) => marked x.entry.lit.1) → List.reverse acc ++ M = (dropLevelsGo marked acc hit M).1 ++ (dropLevelsGo marked acc hit M).2 ∧ ((dropLevelsGo marked acc hit M).1 = [] ∨ ∃ (J : List XEntry) (d : Logic.PL.Literal ℕ), (dropLevelsGo marked acc hit M).1 = J ++ [XEntry.decision d]) ∧ (∀ x ∈ (dropLevelsGo marked acc hit M).1, marked x.entry.lit.1 = false) ∧ ((∀ x ∈ (dropLevelsGo marked acc hit M).2, x.entry.decision = false) ∨ ∃ (N : List XEntry) (d : Logic.PL.Literal ℕ) (M₀ : List XEntry), (dropLevelsGo marked acc hit M).2 = N ++ XEntry.decision d :: M₀ ∧ (∀ x ∈ N, x.entry.decision = false) ∧ (marked d.1 = true ∨ ∃ x ∈ N, marked x.entry.lit.1 = true))
theorem Cslib.Algorithms.Lean.CDCL.XTrail.dropLevels_spec (marked : ℕ → Bool) (M : XTrail) :
M = (dropLevels marked M).1 ++ (dropLevels marked M).2 ∧ ((dropLevels marked M).1 = [] ∨ ∃ (J : List XEntry) (d : Logic.PL.Literal ℕ), (dropLevels marked M).1 = J ++ [XEntry.decision d]) ∧ (∀ x ∈ (dropLevels marked M).1, marked x.entry.lit.1 = false) ∧ ((∀ x ∈ (dropLevels marked M).2, x.entry.decision = false) ∨ ∃ (N : List XEntry) (d : Logic.PL.Literal ℕ) (M₀ : List XEntry), (dropLevels marked M).2 = N ++ XEntry.decision d :: M₀ ∧ (∀ x ∈ N, x.entry.decision = false) ∧ (marked d.1 = true ∨ ∃ x ∈ N, marked x.entry.lit.1 = true))

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.

theorem Cslib.Algorithms.Lean.CDCL.ExecL.jump_spec {φ : Logic.PL.CNF ℕ} {S : ExecL φ} {sp : Split} (hsp : S.trail.split = some sp) (L : S.LearnedAt sp) {marked : ℕ → Bool} (hmarked : ∀ v ∈ List.map Prod.fst L.clause, marked v = true) :
S.trail = (S.jump sp marked).1 ++ (S.jump sp marked).2 ∧ (∃ (J : List XEntry) (d : Logic.PL.Literal ℕ), (S.jump sp marked).1 = J ++ [XEntry.decision d]) ∧ (∀ y ∈ L.clause, y ≠ L.lit → Std.Sat.Literal.negate y ∈ (S.jump sp marked).2.trail.lits) ∧ L.lit.1 ∉ (S.jump sp marked).2.trail.vars

The entries undone end with a decision, and the learned clause is asserting on the trail kept.

theorem Cslib.Algorithms.Lean.CDCL.ExecL.jump_kept {φ : Logic.PL.CNF ℕ} {S : ExecL φ} (sp : Split) {marked : ℕ → Bool} :
(∀ x ∈ (S.jump sp marked).2, x.entry.decision = false) ∨ ∃ (N : List XEntry) (d : Logic.PL.Literal ℕ) (M₀ : List XEntry), (S.jump sp marked).2 = N ++ XEntry.decision d :: M₀ ∧ (∀ x ∈ N, x.entry.decision = false) ∧ (marked d.1 = true ∨ ∃ x ∈ N, marked x.entry.lit.1 = true)

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.

def Cslib.Algorithms.Lean.CDCL.ExecL.afterConflict {φ : Logic.PL.CNF ℕ} (S : ExecL φ) (sp : Split) (L : S.LearnedAt sp) (marked : ℕ → Bool) :

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.

theorem Cslib.Algorithms.Lean.CDCL.ExecL.afterConflict_spec {φ : Logic.PL.CNF ℕ} {S : ExecL φ} (hS : S.Inv) {e : ℕ × Logic.PL.Clause ℕ} (he : e ∈ S.db) (hconf : S.trail.trail.Falsifies e.2) {sp : Split} (hsp : S.trail.split = some sp) (L : S.LearnedAt sp) {marked : ℕ → Bool} (hmarked : ∀ v ∈ List.map Prod.fst L.clause, marked v = true) :
(S.afterConflict sp L marked).Inv ∧ rank φ.vars (S.afterConflict sp L marked).state < rank φ.vars S.state

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.

theorem Cslib.Algorithms.Lean.CDCL.findUnit_spec {A : Assign} {M : Trail} (hA : A.Agrees M) {db : List (ℕ × Logic.PL.Clause ℕ)} {e : ℕ × Logic.PL.Clause ℕ} {l : Logic.PL.Literal ℕ} (h : findUnit A db = some (e, l)) :
e ∈ db ∧ l ∈ e.2 ∧ l.1 ∉ M.vars ∧ ∀ l' ∈ e.2, l' ≠ l → Std.Sat.Literal.negate l' ∈ M.lits

UnitPropagate: add the literal of a unit clause, with that clause as its reason.

theorem Cslib.Algorithms.Lean.CDCL.ExecL.propagate_spec {φ : Logic.PL.CNF ℕ} {S : ExecL φ} (hS : S.Inv) {e : ℕ × Logic.PL.Clause ℕ} {l : Logic.PL.Literal ℕ} (he : e ∈ S.db) (hl : l ∈ e.2) (hundef : l.1 ∉ S.trail.trail.vars) (hfalse : ∀ l' ∈ e.2, l' ≠ l → Std.Sat.Literal.negate l' ∈ S.trail.trail.lits) :
(S.propagate e l).Inv ∧ rank φ.vars (S.propagate e l).state < rank φ.vars S.state

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.

theorem Cslib.Algorithms.Lean.CDCL.ExecL.decide_spec {φ : Logic.PL.CNF ℕ} {S : ExecL φ} (hS : S.Inv) (hnoconf : ∀ c ∈ S.clauses, ¬S.trail.trail.Falsifies c) {x : ℕ} (hx : x ∈ S.clauses.vars) (hundef : x ∉ S.trail.trail.vars) (b : Bool) :
(S.decide x b).Inv ∧ rank φ.vars (S.decide x b).state < rank φ.vars S.state

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.

theorem Cslib.Algorithms.Lean.CDCL.exists_mem_lratDB {φ : Logic.PL.CNF ℕ} {c : Logic.PL.Clause ℕ} (hc : c ∈ φ) :
∃ e ∈ φ.lratDB, e.2 = c

Every clause of φ has a number.

Forget: deleting the clauses numbered ids from the database and the log.

theorem Cslib.Algorithms.Lean.CDCL.ExecL.mem_forget_db {φ : Logic.PL.CNF ℕ} {S : ExecL φ} {ids : List ℕ} {e : ℕ × Logic.PL.Clause ℕ} :
e ∈ (S.forget ids).db ↔ e ∈ S.db ∧ e.1 ∉ ids

The database after Forget: the clauses whose number is not deleted.

theorem Cslib.Algorithms.Lean.CDCL.ExecL.forget_inv {φ : Logic.PL.CNF ℕ} {S : ExecL φ} (hS : S.Inv) {ids : List ℕ} (horig : ∀ e ∈ S.db, e ∈ φ.lratDB → e.1 ∈ ids → ∃ l ∈ e.2, l ∈ S.trail.root.trail.lits) (hreasons : ∀ r ∈ S.trail.reasonIds, r ∉ ids) :
(S.forget ids).Inv

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.

Forgetting leaves the trail, hence the rank, unchanged.

The solver state on arrays #

def Cslib.Algorithms.Lean.CDCL.ATrail.cutGo (T : ATrail) (marked : ℕ → Bool) :
ℕ → ℕ → Bool → ℕ

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

theorem Cslib.Algorithms.Lean.CDCL.ATrail.dropLevels_seg (T : ATrail) (marked : ℕ → Bool) (p : ℕ) :
T.cutPos marked p ≤ p ∧ XTrail.dropLevels marked (T.seg 0 p) = (T.seg (T.cutPos marked p) (p - T.cutPos marked p), T.seg 0 (T.cutPos marked p))

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 invariant, ExecL.Inv of the state read back.

@[reducible, inline]

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.

theorem Cslib.Algorithms.Lean.CDCL.Exec.propagate_spec {φ : Logic.PL.CNF ℕ} {S : Exec φ} (hS : S.Inv) {e : ℕ × Logic.PL.Clause ℕ} {l : Logic.PL.Literal ℕ} (he : e ∈ S.db) (hl : l ∈ e.2) (hundef : l.1 ∉ S.trail.trail.vars) (hfalse : ∀ l' ∈ e.2, l' ≠ l → Std.Sat.Literal.negate l' ∈ S.trail.trail.lits) :
(S.propagate e l).Inv ∧ rank φ.vars (S.propagate e l).state < rank φ.vars S.state

Decide: the literal of the variable x with polarity b pushed as a decision.

theorem Cslib.Algorithms.Lean.CDCL.Exec.decide_spec {φ : Logic.PL.CNF ℕ} {S : Exec φ} (hS : S.Inv) (hnoconf : ∀ c ∈ S.clauses, ¬S.trail.trail.Falsifies c) {x : ℕ} (hx : x ∈ S.clauses.vars) (hundef : x ∉ S.trail.trail.vars) (b : Bool) :
(S.decide x b).Inv ∧ rank φ.vars (S.decide x b).state < rank φ.vars S.state

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.

theorem Cslib.Algorithms.Lean.CDCL.Exec.forget_inv {φ : Logic.PL.CNF ℕ} {S : Exec φ} (hS : S.Inv) {ids : List ℕ} (horig : ∀ e ∈ S.db, e ∈ φ.lratDB → e.1 ∈ ids → ∃ l ∈ e.2, l ∈ S.trail.root.trail.lits) (hreasons : ∀ r ∈ S.trail.reasonIds, r ∉ ids) :
(S.forget ids).Inv

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.

def Cslib.Algorithms.Lean.CDCL.Exec.afterConflict {φ : Logic.PL.CNF ℕ} (S : Exec φ) (p : ℕ) (L : S.LearnedAt (S.tr.splitAt p)) (marked : ℕ → Bool) :
Exec φ

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.

theorem Cslib.Algorithms.Lean.CDCL.Exec.jump_eq {φ : Logic.PL.CNF ℕ} (S : Exec φ) {p : ℕ} (hp : S.tr.IsNewest p) (marked : ℕ → Bool) :
S.toL.jump (S.tr.splitAt p) marked = (S.tr.seg (S.tr.cutPos marked p) (S.tr.size - S.tr.cutPos marked p), S.tr.seg 0 (S.tr.cutPos marked p))

The backjump read back: the entries undone are those from the backjump point up, and the trail kept is the one below it.

theorem Cslib.Algorithms.Lean.CDCL.Exec.cutPos_le {φ : Logic.PL.CNF ℕ} {S : Exec φ} {p : ℕ} (hp : S.tr.IsNewest p) (marked : ℕ → Bool) :
S.tr.cutPos marked p ≤ S.tr.size
theorem Cslib.Algorithms.Lean.CDCL.Exec.toL_afterConflict {φ : Logic.PL.CNF ℕ} (S : Exec φ) {p : ℕ} (hp : S.tr.IsNewest p) (L : S.LearnedAt (S.tr.splitAt p)) (marked : ℕ → Bool) :
(S.afterConflict p L marked).toL = S.toL.afterConflict (S.tr.splitAt p) L marked
theorem Cslib.Algorithms.Lean.CDCL.Exec.afterConflict_spec {φ : Logic.PL.CNF ℕ} {S : Exec φ} (hS : S.Inv) {e : ℕ × Logic.PL.Clause ℕ} (he : e ∈ S.db) (hconf : S.trail.trail.Falsifies e.2) {p : ℕ} (hp : S.tr.IsNewest p) (L : S.LearnedAt (S.tr.splitAt p)) {marked : ℕ → Bool} (hmarked : ∀ v ∈ List.map Prod.fst L.clause, marked v = true) :
(S.afterConflict p L marked).Inv ∧ rank φ.vars (S.afterConflict p L marked).state < rank φ.vars S.state

Backjump and Learn keep the invariant and lower the rank.