Documentation

CslibFrontier.Algorithms.Lean.CDCL.Watch

Two watched literals #

The watch store of the CDCL solver: an array of numbered clauses without repetitions, each of at least two literals, whose first two literals are its watches, and an array of watch lists indexed by literal (its key litCode). A watch list holds watchers (Watcher): the position of a clause and a blocker, a literal of the clause. As in MiniSat, a watcher whose blocker is true is skipped without reading its clause.

The store holds numbers only, so that the solver follows no pointer to read a literal, and moving a watch or keeping a watcher makes no object for the watcher or the literal. This holds for numbers in the scalar range of Lean's runtime: a larger number is boxed. The arrays are written in place when they are not shared and have room; otherwise Array.push grows or copies them, and adding a watch may also grow the array of watch lists (addWatch). A literal is its key 2 * v + b (the word code Cslib.Algorithms.WordRAM.ClauseCode.litCode), decoded by litOfKey, a bijection between literals and numbers. A clause is an array of numbers: the keys of its literals, then its number (WatchStore.codes). A watch list is an array of numbers, two per watcher: the position of the clause and the key of the blocker.

The invariants are stated on the store read back (the representation): its clauses, numbered arrays of literals (WatchStore.clauses, by decodeClause), and the watchers of a watch list, read backwards (watchList, by unpackWatchers), so that a watcher pushed at the end of the array is the head of the list. The operations of the solver have representation lemmas (WatchStore.clauses_moveTo, WatchStore.clauses_addClause, watchList_addWatch, watchList_setWatch), and WatchStore.visit, which reads the numbers, is specified on the literals read back (WatchStore.visit_spec, through KeyVisit.lits).

Moving a watch, as in MiniSat, writes into the clause's array in place: its first two literals become the other watched literal and the new one, which takes the place of the literal no longer watched (WatchStore.moveTo).

WatchStore.Inv F M Q relates the store to the database F, the trail M and the queue Q: its clauses are clauses of F up to repetition and order, every clause of F with two different literals is stored, every clause is on the watch lists of its watches, every blocker is a literal of its clause, and a clause with both watches falsified by processed literals (Processed) has a supported literal (Supported): a true literal that stays true after any backjump keeping both watches false. Support needs no decision levels: it quantifies over the splits of the trail at a decision, and survives extending the trail (Supported.cons) and backjumping (Supported.backjump). With an empty queue, no clause with two different literals is false (WatchStore.Inv.not_falsifies).

WatchStore.visit (visitCodes on the unpacked clause array) is the step of visiting one watcher on the watch list of a falsified literal, unrolling binary and ternary clauses inline (findFree2) before scanning longer clauses by position (findFree), with specification WatchStore.visit_spec; it reads variable values from an assignment indexed by variable (Assign) agreeing with the trail, while moveClause updates the unpacked clause array when moving a watch. WatchStore.build builds a store on a trail, watching literals that are not false, so that it satisfies the invariant with every literal of the trail processed as long as no clause is false (WatchStore.build_inv).

A watcher: a clause of the store, by position, and its blocker, a literal of the clause. When the blocker is true, the clause is satisfied and is skipped without being read. The store holds a watcher as two numbers of its watch list (Watches), and the invariants read them back as watchers (unpackWatchers).

@[reducible, inline]

The positions of the clauses of a list of watchers.

@[reducible, inline]

The watch lists: for each index (litCode), an array of numbers, two for each watcher: the position of its clause, then the key of its blocker (litCode). Numbers in the scalar range are unboxed at run time, so a watch list of them holds no pointers, and keeping or adding a watcher makes no object of its own. Pushing writes in place only when the array is not shared and has room, and otherwise grows or copies it.

The watchers of a watch list read backwards: two numbers each, the key of the blocker and then the position of the clause (litOfKey). A number left over at the end is not read.

@[simp]
theorem Cslib.Algorithms.Lean.CDCL.unpackWatchers_cons_cons (b k : ℕ) (rest : List ℕ) :
unpackWatchers (b :: k :: rest) = { clause := k, blocker := litOfKey b } :: unpackWatchers rest

The watchers of an array of numbers read backwards: pushing a watcher, its clause then its blocker, adds it in front.

A number alone is not a watcher.

The watchers of two lists of numbers, the first of even length.

The watch list at index i, as a list of watchers, empty past the end of the array. The list is the array read backwards, so that adding a watcher at the end of the array (Array.push, in place) adds it at the head of the list, and the solver visits the array from its end. The invariants are stated on these lists.

@[inline]

Applying f to the watch list at index i, growing the array if needed. The list is taken out of the array first (Array.modify), so that f updates it in place.

Adding the watcher of the clause at position k with the blocker of key b to the watch list at index i, at the end of its array, growing the array of lists if needed.

Replacing the watch list at index i, growing the array if needed.

theorem Cslib.Algorithms.Lean.CDCL.watches_ext {W W' : Watches} (hs : Array.size W = Array.size W') (h : ∀ (j : ℕ), Array.getD W j #[] = Array.getD W' j #[]) :
W = W'

Two arrays of watch lists of the same size with the same lists are equal.

Modifying the same watch list twice is modifying it once.

theorem Cslib.Algorithms.Lean.CDCL.modifyWatch_comm (W : Watches) {i j : ℕ} (hij : i ≠ j) (f g : Array ℕ → Array ℕ) :

Modifying two different watch lists, in either order.

theorem Cslib.Algorithms.Lean.CDCL.addWatch_setWatch (W : Watches) (i : ℕ) (L : Array ℕ) (k b : ℕ) :
addWatch (setWatch W i L) i k b = setWatch W i ((L.push k).push b)

Adding a watcher to a list just replaced is replacing it with the longer array.

theorem Cslib.Algorithms.Lean.CDCL.addWatch_setWatch_comm (W : Watches) {i j : ℕ} (hij : i ≠ j) (L : Array ℕ) (k b : ℕ) :
addWatch (setWatch W i L) j k b = setWatch (addWatch W j k b) i L

Replacing a watch list and adding a watcher to another, in either order.

theorem Cslib.Algorithms.Lean.CDCL.setWatch_setWatch (W : Watches) (i : ℕ) (L L' : Array ℕ) :
setWatch (setWatch W i L) i L' = setWatch W i L'

Replacing a watch list twice.

@[simp]
theorem Cslib.Algorithms.Lean.CDCL.watchList_addWatch (W : Watches) (i k b j : ℕ) :
watchList (addWatch W i k b) j = if j = i then { clause := k, blocker := litOfKey b } :: watchList W i else watchList W j
theorem Cslib.Algorithms.Lean.CDCL.mem_watchList_addWatch {W : Watches} {i j k b : ℕ} {x : Watcher} (h : x ∈ watchList (addWatch W i k b) j) :
x = { clause := k, blocker := litOfKey b } ∨ x ∈ watchList W j

A watcher of a list is still on it after one is added.

The clause of the watcher added is on the list it is added to.

The watch store: the clauses with at least two literals, each with its watched literals first, and the watch lists, giving for each literal the clauses that may watch it.

  • codes : Array (Array ℕ)

    The numbered clauses, each an array of numbers: the keys of its literals (litCode), watched literals first, then its number. Moving a watch updates the array in place, and reading a literal follows no pointer.

  • watches : Watches

    The watch lists, by litCode.

The empty watch store, which the solver's state holds while its store is taken out of it. A constant, so that taking the store out allocates nothing.

The number of a clause of the store: the last number of its array.

A clause of the store read back: its number, and its literals decoded from their keys, all the numbers of its array but the last.

The clauses of the store, read back as numbered arrays of literals (decodeClause). The invariants are stated on them; the solver reads WatchStore.codes.

Encoding the literals of a clause, then its number, and decoding them gives them back.

Writing into an array and dropping its last element, in either order.

Writing before the last element keeps the number of a clause.

A literal is processed if it is on the trail and no longer in the propagation queue.

A literal b of a clause with watched literals w₁ and w₂ is supported on the trail M when it is true and stays true after any backjump that keeps both watched literals false: for every split M = N ++ dᵈ :: M₀ at a decision d, not only the newest, with w₁ and w₂ false in M₀, b is true in M₀. Quantifying over every decision lets support survive repeated backjumps.

theorem Cslib.Algorithms.Lean.CDCL.Supported.cons {M : Trail} {b w₁ w₂ : Logic.PL.Literal ℕ} (h : Supported M b w₁ w₂) (x : Entry) :
Supported (x :: M) b w₁ w₂

Support survives extending the trail.

theorem Cslib.Algorithms.Lean.CDCL.Supported.truncate {M M₀ N : Trail} {d b w₁ w₂ : Logic.PL.Literal ℕ} (h : Supported M b w₁ w₂) (hM : M = N ++ { lit := d, decision := true } :: M₀) (h₁ : Std.Sat.Literal.negate w₁ ∈ M₀.lits) (h₂ : Std.Sat.Literal.negate w₂ ∈ M₀.lits) :
Supported M₀ b w₁ w₂

Support survives going back below a decision, to a trail where both watched literals are false.

theorem Cslib.Algorithms.Lean.CDCL.Supported.backjump {M M₀ N : Trail} {d b w₁ w₂ : Logic.PL.Literal ℕ} (h : Supported M b w₁ w₂) (hM : M = N ++ { lit := d, decision := true } :: M₀) (h₁ : Std.Sat.Literal.negate w₁ ∈ M₀.lits) (h₂ : Std.Sat.Literal.negate w₂ ∈ M₀.lits) (x : Entry) :
Supported (x :: M₀) b w₁ w₂

Support survives a backjump below a decision that keeps both watched literals false.

The invariant of the watch store for the numbered clauses F, the trail M and the queue Q: its clauses are the clauses of F with at least two different literals, without repetitions and up to order; each is on the watch lists of its two watched literals; every blocker is a literal of its clause; and a clause with both watched literals falsified by processed literals has a supported literal.

With the queue empty, no clause of at least two literals is false.

Visiting a watching clause #

The outcome of visiting a watcher of the store when its watched literal u has become false.

  • skip : Visit

    The blocker is true: the watcher stays on the watch list of u, and the clause is not read.

  • keep (b : Logic.PL.Literal ℕ) : Visit

    The other watched literal b is true: the clause stays on the watch list of u, with b as blocker.

  • move (j : ℕ) (x b : Logic.PL.Literal ℕ) : Visit

    The literal x at position j, not false, replaces u as a watched literal, watching with the blocker b, the other watched literal (Cslib.Algorithms.Lean.CDCL.WatchStore.moveTo).

  • unit (l : Logic.PL.Literal ℕ) (id : ℕ) : Visit

    Every literal but the other watched literal l is false, and l is undefined: the clause numbered id is unit.

  • conflict (id : ℕ) : Visit

    Every literal is false: the clause numbered id is in conflict.

  • drop : Visit

    The clause no longer watches u.

What a visit finds, on the keys: Visit with the literals as their keys (litCode), so that the solver decodes none of them but the one it propagates.

  • skip : KeyVisit

    The blocker is true.

  • keep (b : ℕ) : KeyVisit

    The other watched literal, of key b, is true.

  • move (j x b : ℕ) : KeyVisit

    The literal of key x at position j replaces the watch, with blocker of key b.

  • unit (l id : ℕ) : KeyVisit

    The clause numbered id is unit, on the other watched literal, of key l.

  • conflict (id : ℕ) : KeyVisit

    The clause numbered id is in conflict.

  • drop : KeyVisit

    The clause no longer watches the literal.

The outcome with its literals decoded from their keys (litOfKey).

The first position from i on, among n, of a literal of c that is not false under A, read on the keys, or the size of c if there is none.

theorem Cslib.Algorithms.Lean.CDCL.findFree_lt {A : Assign} {c : Array ℕ} {n i : ℕ} (h : findFree A c n i < c.size) :
i ≤ findFree A c n i ∧ findFree A c n i < i + n ∧ A.isFalseKey c[findFree A c n i] = false
theorem Cslib.Algorithms.Lean.CDCL.findFree_none {A : Assign} {c : Array ℕ} {n i : ℕ} :
¬findFree A c n i < c.size → ∀ (j : ℕ) (h : j < c.size), i ≤ j → j < i + n → A.isFalseKey c[j] = true
@[inline]

findFree A c (c.size - 3) 2 with the binary (c.size = 3) and ternary (c.size = 4) cases unrolled inline, avoiding out-of-line recursive calls on 2-literal and 3-literal clauses.

@[inline]

WatchStore.visit directly on the array codes of clause arrays, so a loop holding codes apart from watches avoids packing a WatchStore.

@[inline]

Visiting a watcher, of the clause at position k with the blocker of key b, when the watched literal of key u has become false under the assignment A: it is skipped if its blocker is true (Assign.holdsKey); otherwise its clause is read by position (visitCodes), loading the other watched variable's value once and scanning with findFree2. Inlined, so that the outcome is matched on without being built.

@[inline]

Updating the clause at position k in codes when moving a watch from u to x at position j, with o as the other watched literal.

Moving a watch of the clause at position k, from the literal of key u to the one of key x at position j, with the other watched literal, of key o, as blocker: as MiniSat does, the clause becomes o, x, then the others with u in place of x, written into its array in place, and the watch list of x gets the watcher. The arrays are taken out of the store first, so that they are updated in place when the store is not shared.

The position j must be one of the clause's literals but the watched ones: 1 < j and j + 1 < a.size for the array a of the clause at k, as WatchStore.visit finds it and WatchStore.clauses_moveTo assumes. The last number of a is the clause's number, which j = a.size - 1 would overwrite; nothing here checks the bound.

theorem Cslib.Algorithms.Lean.CDCL.WatchStore.clauses_moveTo (S : WatchStore) (k j o x u : ℕ) (hj : ∀ (hk : k < S.codes.size), 1 < j ∧ j + 1 < S.codes[k].size) :

The clauses after moving a watch, read back: the literals of the keys written, at positions before the clause's number.

The other watched literal of a clause watching u with watched literals w₁ and w₂.

What a visit of the watcher w finds, by outcome, on the list of the clause's literals.

An array of at least two elements, as a list: its first two, then the others.

Comparing a key with the key of a literal is comparing the literals.

theorem Cslib.Algorithms.Lean.CDCL.WatchStore.visit_spec (S : WatchStore) {A : Assign} {M : Trail} (hA : A.Agrees M) (u : Logic.PL.Literal ℕ) (k b : ℕ) :
VisitSpec S M u { clause := k, blocker := litOfKey b } (S.visit A (WordRAM.ClauseCode.litCode u) k b).lits

The visit meets its specification, under an assignment agreeing with the trail, for the watcher read back from the two numbers and the outcome read back from the keys.

Changing the trail and the clauses #

theorem Cslib.Algorithms.Lean.CDCL.WatchStore.Inv.weaken {F : List (ℕ × Logic.PL.Clause ℕ)} {M M' : Trail} {Q Q' : List (Logic.PL.Literal ℕ)} {S : WatchStore} (h : Inv F M Q S) (hproc : ∀ (x : Logic.PL.Literal ℕ), Processed M' Q' x → Processed M Q x) (hsupp : ∀ (b w₁ w₂ : Logic.PL.Literal ℕ), Processed M' Q' (Std.Sat.Literal.negate w₁) → Processed M' Q' (Std.Sat.Literal.negate w₂) → Supported M b w₁ w₂ → Supported M' b w₁ w₂) :
Inv F M' Q' S

The invariant survives a change of trail and queue that processes no new literal and keeps the support of the clauses with both watched literals falsified by processed literals.

A short clause added to the database leaves the store unchanged.

Adding a clause without repetitions to the store, watching its first two literals, each with the other as blocker.

theorem Cslib.Algorithms.Lean.CDCL.WatchStore.clauses_addClause (S : WatchStore) (id : ℕ) (c : Logic.PL.Clause ℕ) :
(S.addClause id c).clauses = match c with | head :: head_1 :: tail => S.clauses.push (id, List.toArray c) | x => S.clauses

The clauses after adding one, read back: the clause added, with its literals.

theorem Cslib.Algorithms.Lean.CDCL.WatchStore.Inv.addClause {F : List (ℕ × Logic.PL.Clause ℕ)} {M : Trail} {Q : List (Logic.PL.Literal ℕ)} {S : WatchStore} (h : Inv F M Q S) {id : ℕ} {C : Logic.PL.Clause ℕ} {w₁ w₂ : Logic.PL.Literal ℕ} {rest : Logic.PL.Clause ℕ} (hnd : (w₁ :: w₂ :: rest).Nodup) (hmem : ∀ (l : Logic.PL.Literal ℕ), l ∈ w₁ :: w₂ :: rest ↔ l ∈ C) (hw₁ : ¬Processed M Q (Std.Sat.Literal.negate w₁)) :
Inv ((id, C) :: F) M Q (S.addClause id (w₁ :: w₂ :: rest))

Adding a clause of the database, without repetitions, whose first watched literal is not falsified by a processed literal.

A store with no clauses and nb empty watch lists.

A clause with its literals that are not false under the assignment A first, so that they are the ones watched.

If the first literal of watchOrder A c is false, every literal of c is.

The literal assigned last, first: the literal of c with the greatest position P of its variable (the first among ties) is moved to the front.

theorem Cslib.Algorithms.Lean.CDCL.latestFirst_max {P : Array ℕ} {c rest : Logic.PL.Clause ℕ} {x : Logic.PL.Literal ℕ} (h : latestFirst P c = x :: rest) (z : Logic.PL.Literal ℕ) :
z ∈ c → P.getD z.1 0 ≤ P.getD x.1 0

The first literal of latestFirst has the greatest position among the literals of c.

The order of the watches at a rebuild: watchOrder, the literals that are not false first, then, if the second literal is false, the false literal assigned last second (latestFirst), as MiniSat's watches would be after the propagations, so that a clause with a single literal that is not false watches the literal false at the highest level. The positions P of the variables on the trail affect only the order.

theorem Cslib.Algorithms.Lean.CDCL.watchOrderAt_head {P : Array ℕ} {A : Assign} {c rest : Logic.PL.Clause ℕ} {w : Logic.PL.Literal ℕ} (h : watchOrderAt P A c = w :: rest) :
∃ (rest' : List (Logic.PL.Literal ℕ)), watchOrder A c = w :: rest'
theorem Cslib.Algorithms.Lean.CDCL.watchOrderAt_second {P : Array ℕ} {A : Assign} {c rest : Logic.PL.Clause ℕ} {w x : Logic.PL.Literal ℕ} (h : watchOrderAt P A c = w :: x :: rest) (hx : A.isFalse x = true) (z : Logic.PL.Literal ℕ) :
z ∈ x :: rest → P.getD z.1 0 ≤ P.getD x.1 0

A false second watch is the one assigned last: if the second literal of watchOrderAt is false, its variable has the greatest position among the literals after the first.

The literals of a clause without repetitions, removed in linear time (dedupLits), are those of List.dedup, up to order.

Removing repetitions with a shared array #

Clearing the marks of the literals L.

Clearing the marks of L, which include every literal marked, leaves no mark.

Removing the repetitions of c with the marks m, which mark nothing and are cleared again: the array is not allocated per clause, as dedupLits allocates it. If a literal of c lies beyond the array, dedupLits is used instead.

Tail-recursively adding the reversed clause list G onto the watch store S, removing repetitions with the reusable marks m.

One past the greatest literal code of F: the size of the marks for its repetitions and initial watch-list table.

The watch store of the numbered clauses F under the assignment A: every clause with at least two different literals, without repetitions, watching its first two literals once ordered by watchOrderAt P A: those that are not false under A first, then, if the second is false, the false literal whose variable has the greatest position P on the trail. It adds F.reverse tail-recursively with Cslib.Algorithms.Lean.CDCL.WatchStore.addClause onto pre-allocated codes and watches, removing repetitions with one array of marks for all the clauses (Cslib.Algorithms.Lean.CDCL.dedupWith).

theorem Cslib.Algorithms.Lean.CDCL.WatchStore.buildWith_inv (P : Array ℕ) {A : Assign} {M : Trail} (hA : A.Agrees M) (G H : List (ℕ × Logic.PL.Clause ℕ)) (S : WatchStore) (m : Array Bool) :
Inv H M [] S → (∀ (y : Logic.PL.Literal ℕ), m.getD (WordRAM.ClauseCode.litCode y) false = false) → (∀ e ∈ G, 2 ≤ (List.dedup e.2).length → ¬M.Falsifies e.2) → Inv (G.reverse ++ H) M [] (buildWith P A G S m)
theorem Cslib.Algorithms.Lean.CDCL.WatchStore.build_inv (P : Array ℕ) {A : Assign} {M : Trail} (hA : A.Agrees M) (F : List (ℕ × Logic.PL.Clause ℕ)) (hF : ∀ e ∈ F, 2 ≤ (List.dedup e.2).length → ¬M.Falsifies e.2) :
Inv F M [] (build P A F)

The watch store built from F under an assignment agreeing with the trail M, none of whose clauses M falsifies, satisfies the invariant with every literal of M processed: a clause whose first watched literal is false has every literal false. (Its second watched literal may be false while the clause is still open, as in a binary clause with one false and one unassigned literal.)