Documentation

CslibFrontier.Algorithms.Lean.CDCL.Assign

Assignments indexed by variable #

The value of each variable on the trail, in an array indexed by variable, as MiniSat's assigns: whether a literal is true, or a variable assigned, is read in constant time instead of by a scan of the trail. Assign.Agrees A M says that the array A holds exactly the literals of the trail M. It survives assigning a new variable (Assign.Agrees.cons), and undoing the newest literals of a trail, over variables not on the rest (Assign.Agrees.drop), as Backjump does; any trail without repeated variables has an array agreeing with it (Assign.agrees_ofTrail).

Main definitions #

Main results #

An assignment indexed by variable: the value of each variable, if it has one, stored as a small number (Assign.code): 0 for none, 1 for false, 2 for true. Small numbers are unboxed at run time, so reading a value touches no reference count.

  • vals : Array ℕ

    The codes of the values, indexed by variable.

The empty assignment, which the solver's state holds while its assignment is taken out of it.

@[inline]

The code of a value: 0 for none, 1 for false, 2 for true.

@[irreducible]

The value of a code: 1 is false, 2 is true, and any other code (0 in particular) is none. Irreducible, so that unification does not try to compute the code of an assignment it cannot evaluate; the kernel still runs it.

The value of the variable v, none past the end of the array.

@[inline]

Whether the literal l is true, read on the code of its variable.

@[inline]

Whether the literal l is false: holds of its negation, without building the negation.

@[inline]

Whether the variable v is assigned.

A variable is assigned when it has a value.

@[inline]

Setting the value of v to x, growing the array if needed.

@[inline]

Assigning the literal l.

@[inline]

Unassigning the variable v: an update within the array, which is left as it is if v is past its end, since v then has no value.

Unassigning the variables vs. It never grows the array.

Assigning the literals of a trail on top of A, oldest first so that the newest occurrence of a variable wins, with a left fold, which uses constant stack.

The assignment of the literals of a trail.

Unassigning never grows the array.

The array agrees with the trail M: the variable v has the value b exactly when the literal (v, b) is on M.

A literal holds exactly when it is on the trail.

A literal is false exactly when its negation is on the trail.

A variable is assigned exactly when it is a variable of the trail.

theorem Cslib.Algorithms.Lean.CDCL.Assign.Agrees.cons {A : Assign} {M : Trail} (h : A.Agrees M) {l : Logic.PL.Literal ℕ} (hl : l.1 ∉ M.vars) (d : Bool) :
(A.assign l).Agrees ({ lit := l, decision := d } :: M)

Assigning a literal of an unassigned variable keeps the agreement.

theorem Cslib.Algorithms.Lean.CDCL.Assign.Agrees.drop {A : Assign} {N M₀ : Trail} (h : A.Agrees (N ++ M₀)) (hdisj : ∀ v ∈ N.vars, v ∉ M₀.vars) :
(A.unassign N.vars).Agrees M₀

Undoing the newest literals N of a trail, over variables not on the rest M₀, by unassigning their variables, keeps the agreement with the rest.

Whether the clause c is false: every literal is false.

Assigning a trail without repeated variables on top of an empty array, one with no value anywhere, gives an array agreeing with the trail.

theorem Cslib.Algorithms.Lean.CDCL.Assign.value_unassign_eq_none {A : Assign} {vs : List ℕ} (h : ∀ v ∉ vs, A.value v = none) (v : ℕ) :
(A.unassign vs).value v = none

Unassigning every variable that has a value leaves no value.

Marking the variables vs in A: each is given the value true. Marking on top of an array with no value, and clearing the same variables afterwards, reuses the array instead of allocating one by the greatest variable.

Marking the variables of the literals C: mark of their variables (markLits_eq), without listing them.

Unassigning the variables of the literals C: unassign of their variables (unassignLits_eq), without listing them.

An array in which exactly the variables vs are assigned. It is sized once for the greatest of them, so that marking never grows it.

The assignment of a trail without repeated variables agrees with it.

Removing repetitions in linear time #

@[specialize #[]]
def Cslib.Algorithms.Lean.CDCL.dedupKeyGo {α : Type} (key : α → ℕ) :
List α → Array Bool → List α → List α × Array Bool

The elements of xs not yet marked in m by their keys, in order, after acc reversed, and the marks with those elements marked too. A caller that keeps its marks between lists reads both (dedupWith); dedupKey reads the first.

@[specialize #[]]
def Cslib.Algorithms.Lean.CDCL.dedupKey {α : Type} (key : α → ℕ) (xs : List α) :
List α

Removing repetitions from xs, keeping first occurrences: an element is kept unless its key is already marked, in an array sized to the greatest key. For keys computed in constant time this takes time and space O(xs.length + greatest key + 1): linear in the list when the keys are dense, as they are for literals once solveCompact has renamed the variables to 0, …, k - 1, but not for sparse keys. With an injective key, the elements are those of xs (mem_dedupKey), each once (nodup_dedupKey).

theorem Cslib.Algorithms.Lean.CDCL.lt_foldl_max {α : Type} (key : α → ℕ) (xs : List α) (a : ℕ) :
(∀ x ∈ xs, key x < List.foldl (fun (a : ℕ) (x : α) => max a (key x)) a xs + 1) ∧ a ≤ List.foldl (fun (a : ℕ) (x : α) => max a (key x)) a xs
theorem Cslib.Algorithms.Lean.CDCL.dedupKeyGo_spec {α : Type} {key : α → ℕ} (xs : List α) (m : Array Bool) (acc : List α) :
(∀ x ∈ acc ++ xs, ∀ y ∈ acc ++ xs, key x = key y → x = y) → (∀ x ∈ xs, key x < m.size) → (∀ (y : α), m.getD (key y) false = true ↔ ∃ z ∈ acc, key z = key y) → acc.Nodup → (∀ (y : α), y ∈ (dedupKeyGo key xs m acc).1 ↔ y ∈ acc ∨ y ∈ xs) ∧ (dedupKeyGo key xs m acc).1.Nodup ∧ ∀ (y : α), (dedupKeyGo key xs m acc).2.getD (key y) false = true ↔ ∃ z ∈ (dedupKeyGo key xs m acc).1, key z = key y

What removing repetitions keeps, with a key injective on the elements of acc and xs: the elements of both, each once; the marks hold the keys of the elements kept.

theorem Cslib.Algorithms.Lean.CDCL.dedupKey_spec {α : Type} {key : α → ℕ} (xs : List α) (hkey : ∀ x ∈ xs, ∀ y ∈ xs, key x = key y → x = y) :
(∀ (y : α), y ∈ dedupKey key xs ↔ y ∈ xs) ∧ (dedupKey key xs).Nodup

Removing repetitions with a key injective on the elements of xs keeps the elements of xs, each once.

theorem Cslib.Algorithms.Lean.CDCL.mem_dedupKey {α : Type} {key : α → ℕ} (hkey : Function.Injective key) {xs : List α} {y : α} :
y ∈ dedupKey key xs ↔ y ∈ xs

With an injective key, removing repetitions keeps the elements.

theorem Cslib.Algorithms.Lean.CDCL.nodup_dedupKey {α : Type} {key : α → ℕ} (hkey : Function.Injective key) (xs : List α) :
(dedupKey key xs).Nodup

With an injective key, removing repetitions leaves each element once.

theorem Cslib.Algorithms.Lean.CDCL.dedupKeyGo_of_nodup {α : Type} {key : α → ℕ} (hkey : Function.Injective key) (xs : List α) (m : Array Bool) (acc : List α) :
xs.Nodup → (∀ x ∈ xs, m.getD (key x) false = false) → (dedupKeyGo key xs m acc).1 = acc.reverse ++ xs
theorem Cslib.Algorithms.Lean.CDCL.dedupKey_of_nodup {α : Type} {key : α → ℕ} (hkey : Function.Injective key) {xs : List α} (h : xs.Nodup) :
dedupKey key xs = xs

A list without repetitions is its own removal of repetitions, with an injective key.

The key of a literal is its word code Cslib.Algorithms.WordRAM.ClauseCode.litCode: 2 * v + 1 for v and 2 * v for ¬v. It keys the removal of repeated literals (dedupLits), indexes the watch lists, and is how the watch store holds literals.

The key of the negation of l, computed without building the negation.

@[inline]

The literal of a key: the variable is the key divided by two, and the literal is positive when the key is odd. It inverts litCode (litOfKey_litCode, litCode_litOfKey), so every number is the key of exactly one literal.

The key of the negation of a literal is its key with the low bit flipped.

@[inline]

The key of the negation of the literal of key c, without building the literal.

@[inline]

Whether the literal of the key c is true, read on the key, without building the literal.

@[inline]

Whether the literal of the key c is false, read on the key.

@[inline]

Assigning the literal of key c, without building the literal.

@[reducible, inline]

Removing repeated literals, in linear time.

@[reducible, inline]

Removing repeated variables, in linear time.