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).
- clause : ℕ
The position of the clause in the store.
- blocker : Logic.PL.Literal ℕ
A literal of the clause.
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 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.
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.
Two arrays of watch lists of the same size with the same lists are equal.
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.
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.
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.
Support survives extending the trail.
Support survives going back below a decision, to a trail where both watched literals are false.
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.
- covered (e : ℕ × Array (Logic.PL.Literal ℕ)) : e ∈ S.clauses → ∀ (w₁ w₂ : Logic.PL.Literal ℕ) (rest : List (Logic.PL.Literal ℕ)), e.2.toList = w₁ :: w₂ :: rest → Processed M Q (Std.Sat.Literal.negate w₁) → Processed M Q (Std.Sat.Literal.negate w₂) → ∃ b ∈ e.2.toList, Supported M b w₁ w₂
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
bis true: the clause stays on the watch list ofu, withbas blocker. - move (j : ℕ) (x b : Logic.PL.Literal ℕ) : Visit
The literal
xat positionj, not false, replacesuas a watched literal, watching with the blockerb, the other watched literal (Cslib.Algorithms.Lean.CDCL.WatchStore.moveTo). - unit (l : Logic.PL.Literal ℕ) (id : ℕ) : Visit
Every literal but the other watched literal
lis false, andlis undefined: the clause numberedidis unit. - conflict (id : ℕ) : Visit
Every literal is false: the clause numbered
idis 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
xat positionjreplaces the watch, with blocker of keyb. - unit (l id : ℕ) : KeyVisit
The clause numbered
idis unit, on the other watched literal, of keyl. - conflict (id : ℕ) : KeyVisit
The clause numbered
idis in conflict. - drop : KeyVisit
The clause no longer watches the literal.
The outcome with its literals decoded from their keys (litOfKey).
WatchStore.visit directly on the array codes of clause arrays, so a loop holding codes
apart from watches avoids packing a WatchStore.
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.
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.
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.
Comparing a key with the key of a literal is comparing the literals.
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 #
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.
The clauses after adding one, read back: the clause added, with its literals.
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.
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.
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).
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.)