Documentation

CslibFrontier.Algorithms.Lean.DPLL.Trail

The DPLL search with an explicit trail #

The solver Cslib.Algorithms.Lean.DPLL.solve searches recursively and builds a resolution derivation as it returns. A machine without recursion runs the same search with an explicit trail: the literals assumed so far, each marked as a first or a second branch (Cslib.Algorithms.Lean.DPLL.Trail). One step (Cslib.Algorithms.Lean.DPLL.Trail.step) looks at the partial assignment of the trail (Cslib.Algorithms.Lean.DPLL.status):

The run of the steps (Cslib.Algorithms.Lean.DPLL.Trail.run) explores the same tree as the recursive search, except that it always tries the second branch of a variable, where the solver skips it when the derivation of the first branch does not mention the variable. In that case the second branch has no model either, since the derived clause is falsified by the partial assignment (Cslib.Algorithms.Lean.DPLL.dfs_eq_none_of_derivation); so both find the same first model (Cslib.Algorithms.Lean.DPLL.solve_eq_sat_iff_dfs). The run takes at most 2 ^ (N + 1) steps, where N is the number of distinct variables (Cslib.Algorithms.Lean.DPLL.Trail.run_answer): one per node of the search tree.

Main definitions #

Main results #

The status of a partial assignment #

Whether a partial assignment assigns the variable x.

The literal the solver branches on at π: the literal of a unit clause, or else the variable of the first literal occurrence that π leaves open, true first.

What the search does at a partial assignment.

Instances For

    The status of the partial assignment π, tested in the order of the solver: a falsified clause, then satisfaction, then branching.

    No variable is assumed both true and false.

    Assuming a literal on an unassigned variable keeps a partial assignment consistent.

    Every variable of the formula is assigned when no variable of φ.vars is open.

    The search without skipping #

    The search without skipping from π with fuel levels left: the first partial assignment satisfying every clause, trying both branches of every variable in order.

    A partial assignment falsifying a clause has the status conflict.

    A partial assignment of status conflict falsifies a clause.

    A partial assignment falsifying no clause, with a true literal in every clause, has the status sat.

    Otherwise the status branches on nextLiteral.

    theorem Cslib.Algorithms.Lean.DPLL.dfs_eq_some {φ : Logic.PL.CNF ℕ} (fuel : ℕ) {π π' : List (Logic.PL.Literal ℕ)} :
    Consistent π → dfs φ fuel π = some π' → (∀ l ∈ π, l ∈ π') ∧ Consistent π' ∧ (List.all φ fun (c : Logic.PL.Clause ℕ) => List.any c (Holds π')) = true

    A model found by the search extends the partial assignment, stays consistent, and satisfies every clause.

    theorem Cslib.Algorithms.Lean.DPLL.dfs_eq_none_of_derivation {φ : Logic.PL.CNF ℕ} {π : List (Logic.PL.Literal ℕ)} (hπ : Consistent π) {c : Logic.PL.Clause ℕ} (d : φ.Derivation c) (hc : ∀ l ∈ c, Std.Sat.Literal.negate l ∈ π) (fuel : ℕ) :
    dfs φ fuel π = none

    No model below a falsified derived clause: if π falsifies a clause derived from φ, the search from π finds no model.

    The search without skipping finds the solver's answer #

    The open variables of π in the order of their first occurrences in φ.vars.

    theorem Cslib.Algorithms.Lean.DPLL.length_openVars_cons_le {φ : Logic.PL.CNF ℕ} {π : List (Logic.PL.Literal ℕ)} {l : Logic.PL.Literal ℕ} {fuel : ℕ} (hs : status φ π = Status.branch l) (hf : (openVars φ π).length ≤ fuel) :
    ∃ (f : ℕ), fuel = f + 1 ∧ ∀ (l' : Logic.PL.Literal ℕ), l'.1 = l.1 → (openVars φ (l' :: π)).length ≤ f

    At a branching on l, a bound fuel on the open variables is positive, and the partial assignments with a literal on the variable of l added have fewer.

    theorem Cslib.Algorithms.Lean.DPLL.branchLiteral_eq {φ : Logic.PL.CNF ℕ} {π : List (Logic.PL.Literal ℕ)} {x : ℕ} {xs : List ℕ} (hvs : x :: xs = openVars φ π) :
    ↑(branchLiteral φ π x xs) = nextLiteral φ π

    The literal of the solver's branch is nextLiteral when the open variables are listed in the order of their first occurrences.

    The model of an outcome, if it is one.

    theorem Cslib.Algorithms.Lean.DPLL.model?_resolveBranches {φ : Logic.PL.CNF ℕ} {π : List (Logic.PL.Literal ℕ)} (v : ℕ) (b : Bool) {c₁ c₂ : Logic.PL.Clause ℕ} (d₁ : φ.Derivation c₁) (hc₁ : (v, !b) ∈ c₁) (h₁ : ∀ l ∈ c₁, Std.Sat.Literal.negate l ∈ (v, b) :: π) (d₂ : φ.Derivation c₂) (hc₂ : (v, b) ∈ c₂) (h₂ : ∀ l ∈ c₂, Std.Sat.Literal.negate l ∈ (v, !b) :: π) :
    (resolveBranches v b d₁ hc₁ h₁ d₂ hc₂ h₂).model? = none

    The number of distinct variables of φ: the depth of the search tree.

    The search without skipping finds the solver's model.

    The search without skipping decides satisfiability as the solver does.

    The search with an explicit trail #

    An entry of a trail: a literal assumed, and whether it is the second branch of its variable.

    • The literal assumed.

    • isSecond : Bool

      Whether this is the second branch of its variable, after the first was refuted.

    Instances For
      @[reducible, inline]

      A trail: its entries, last first.

      The partial assignment of a trail.

      @[simp]
      theorem Cslib.Algorithms.Lean.DPLL.Trail.lits_cons (l : Logic.PL.Literal ℕ) (b : Bool) (t : Trail) :
      lits ({ lit := l, isSecond := b } :: t) = l :: t.lits

      Backtracking: drop the second branches on top, and turn the first branch below them into its second branch; none when every branch is a second branch.

      The end of a run: a partial assignment satisfying every clause, or none.

      Instances For

        One step of the search: end at a satisfying partial assignment, backtrack at a falsified clause, and otherwise push the branch literal.

        The run of at most k steps from the trail t: its answer, or none if the steps run out.

        The number of nodes of the search tree from π with fuel levels.

        theorem Cslib.Algorithms.Lean.DPLL.Trail.nodes_lt (φ : Logic.PL.CNF ℕ) (fuel : ℕ) (π : List (Logic.PL.Literal ℕ)) :
        nodes φ fuel π < 2 ^ (fuel + 1)

        What a run does after the subtree at the top of the trail: the model found in it, or else the backtracked trail continuing with k steps.

        theorem Cslib.Algorithms.Lean.DPLL.Trail.run_add_nodes (φ : Logic.PL.CNF ℕ) (fuel : ℕ) (t : Trail) (k : ℕ) :
        (openVars φ t.lits).length ≤ fuel → run φ (k + nodes φ fuel t.lits) t = after φ k t (dfs φ fuel t.lits)

        The run explores the subtree at the top of the trail in nodes steps: it ends with the model of the subtree if there is one, and otherwise continues by backtracking.

        The answer of the search without skipping.

        theorem Cslib.Algorithms.Lean.DPLL.Trail.run_answer (φ : Logic.PL.CNF ℕ) :
        ∃ k ≤ 2 ^ (depth φ + 1), run φ k [] = some (answer φ)

        The trail run finds the answer within 2 ^ (N + 1) steps, where N is the number of distinct variables of φ.