Documentation

CslibFrontier.Algorithms.WordRAM.DPLL.Steps

The DPLL solver on the word RAM: the steps #

This file proves that one iteration of the main loop of the word-RAM DPLL solver performs one step of the search with an explicit trail (Cslib.Algorithms.WordRAM.DPLL.stepBody_spec): the memory holds the trail as a literal array and as a stack of entries (Cslib.Algorithms.WordRAM.DPLL.StateAt), and the step either moves to the memory of the next trail or writes the output.

The memory of a trail #

The words of the trail: two per entry, bottom first, the code of its literal and 1 for a second branch.

@[simp]
theorem Cslib.Algorithms.WordRAM.DPLL.entryVar_mk (l : Logic.PL.Literal ℕ) (b : Bool) :
entryVar { lit := l, isSecond := b } = l.1

The trail invariant: the variables of the trail are distinct variables of the formula.

The memory of a trail: the clauses from address 2 to T, the literal array at T, and the trail entries at T + 2 N.

theorem Cslib.Algorithms.WordRAM.DPLL.mem_vars_lt {φ : Logic.PL.CNF ℕ} {N T : ℕ} {mem : ℕ → ℕ} {t : Lean.DPLL.Trail} (h : MemAt φ N T mem t) {v : ℕ} (hv : v ∈ φ.vars) :
v < N
theorem Cslib.Algorithms.WordRAM.DPLL.lits_not_mem {φ : Logic.PL.CNF ℕ} {l : Logic.PL.Literal ℕ} {b : Bool} {t : Lean.DPLL.Trail} (h : TrailOK φ ({ lit := l, isSecond := b } :: t)) :
theorem Cslib.Algorithms.WordRAM.DPLL.LitArr.update {mem : ℕ → ℕ} {T N : ℕ} {π π' : List (Logic.PL.Literal ℕ)} (h : LitArr mem T N π) (l : Logic.PL.Literal ℕ) (v : ℕ) (hv : v = (π'.contains l).toNat) (hπ : ∀ (l' : Logic.PL.Literal ℕ), l' ≠ l → π'.contains l' = π.contains l') :

Updating a cell of the literal array.

theorem Cslib.Algorithms.WordRAM.DPLL.LitArr.update_ge {mem : ℕ → ℕ} {T N : ℕ} {π : List (Logic.PL.Literal ℕ)} (h : LitArr mem T N π) {a : ℕ} (ha : T + 2 * N ≤ a) (v : ℕ) :
LitArr (Function.update mem a v) T N π

Cells at or above T + 2 N are outside the literal array.

theorem Cslib.Algorithms.WordRAM.DPLL.MemAt.cons {φ : Logic.PL.CNF ℕ} {N T : ℕ} {mem mem' : ℕ → ℕ} {t : Lean.DPLL.Trail} (h : MemAt φ N T mem t) {l : Logic.PL.Literal ℕ} {b : Bool} (hl : l.1 ∈ φ.vars) (hnot : l.1 ∉ List.map entryVar t) (hmem : ∀ (a : ℕ), mem' a = if a = T + ClauseCode.litCode l then 1 else if a = T + 2 * N + 2 * List.length t then ClauseCode.litCode l else if a = T + 2 * N + 2 * List.length t + 1 then b.toNat else mem a) :
MemAt φ N T mem' ({ lit := l, isSecond := b } :: t)

Pushing an entry ⟨l, b⟩ on a variable of the formula not on the trail: the new memory sets the literal's cell to 1 and writes the entry's two words above the trail, and agrees with the old memory elsewhere.

theorem Cslib.Algorithms.WordRAM.DPLL.MemAt.push {φ : Logic.PL.CNF ℕ} {N T : ℕ} {mem : ℕ → ℕ} {t : Lean.DPLL.Trail} (h : MemAt φ N T mem t) {l : Logic.PL.Literal ℕ} (hl : l.1 ∈ φ.vars) (ha : Lean.DPLL.Assigned t.lits l.1 = false) :
MemAt φ N T (Function.update (Function.update (Function.update mem (T + 2 * N + 2 * List.length t) (ClauseCode.litCode l)) (T + 2 * N + 2 * List.length t + 1) 0) (T + ClauseCode.litCode l) 1) ({ lit := l, isSecond := false } :: t)

Pushing a first branch on an open variable of the formula.

theorem Cslib.Algorithms.WordRAM.DPLL.MemAt.pop {φ : Logic.PL.CNF ℕ} {N T : ℕ} {mem : ℕ → ℕ} {l : Logic.PL.Literal ℕ} {b : Bool} {t : Lean.DPLL.Trail} (h : MemAt φ N T mem ({ lit := l, isSecond := b } :: t)) :
MemAt φ N T (Function.update mem (T + ClauseCode.litCode l) 0) t

Popping an entry: clearing its literal.

theorem Cslib.Algorithms.WordRAM.DPLL.MemAt.flip {φ : Logic.PL.CNF ℕ} {N T : ℕ} {mem : ℕ → ℕ} {l : Logic.PL.Literal ℕ} {t : Lean.DPLL.Trail} (h : MemAt φ N T mem ({ lit := l, isSecond := false } :: t)) :

Flipping a first branch into the second branch: popping it, then pushing its negation as a second branch.

theorem Cslib.Algorithms.WordRAM.DPLL.TrailOK.length_le {φ : Logic.PL.CNF ℕ} {t : Lean.DPLL.Trail} (h : TrailOK φ t) {N : ℕ} (hN : ∀ v ∈ φ.vars, v < N) :

The state at the head of the main loop for the trail t: the constants, the bases of the arrays, the top of the trail, and the memory of the trail.

The trail without the second branches on top.

theorem Cslib.Algorithms.WordRAM.DPLL.backtrack_eq (t : Lean.DPLL.Trail) :
t.backtrack = match dropSecond t with | [] => none | { lit := l, isSecond := isSecond } :: t' => some ({ lit := Std.Sat.Literal.negate l, isSecond := true } :: t')
theorem Cslib.Algorithms.WordRAM.DPLL.dropSecond_cons_false (l : Logic.PL.Literal ℕ) (t : Lean.DPLL.Trail) :
dropSecond ({ lit := l, isSecond := false } :: t) = { lit := l, isSecond := false } :: t
theorem Cslib.Algorithms.WordRAM.DPLL.dropSecond_eq_cons {t t' : Lean.DPLL.Trail} {l : Logic.PL.Literal ℕ} {b : Bool} (h : dropSecond t = { lit := l, isSecond := b } :: t') :
theorem Cslib.Algorithms.WordRAM.DPLL.MemAt.top {φ : Logic.PL.CNF ℕ} {N T : ℕ} {mem : ℕ → ℕ} {l : Logic.PL.Literal ℕ} {b : Bool} {t : Lean.DPLL.Trail} (h : MemAt φ N T mem ({ lit := l, isSecond := b } :: t)) :
mem (T + 2 * N + 2 * List.length t) = ClauseCode.litCode l ∧ mem (T + 2 * N + 2 * List.length t + 1) = b.toNat

The two words of the top entry of a trail.

The output of a model: 1, then the bits of the true literals of π below N.

The output for an answer of the search.

The code of the literal pushed by a branching, from the scan registers: that of the unit clause, or else the first open variable, true.

The instructions of one step: a pass over the clauses, then backtracking, branching, or writing the model.

theorem Cslib.Algorithms.WordRAM.DPLL.push_spec {w : ℕ} (φ : Logic.PL.CNF ℕ) (N T : ℕ) (x : WordRAM.Data 32) (t : Lean.DPLL.Trail) (l : Logic.PL.Literal ℕ) (hs : StateAt φ N T x t) (hcode : (if x.regs Reg.uf = 0 then x.regs Reg.ov + x.regs Reg.ov + 1 else x.regs Reg.unit) = ClauseCode.litCode l) (hl : l.1 ∈ φ.vars) (ha : Lean.DPLL.Assigned t.lits l.1 = false) (hfit : WordRAM.Fits w (T + 5 * N + 2)) :
∃ (y : WordRAM.Data 32), WordRAM.ExecWithin w push x y 12 ∧ StateAt φ N T y ({ lit := l, isSecond := false } :: t) ∧ ∀ (a : ℕ), T + 4 * N + 2 ≤ a → y.mem a = x.mem a

Branching pushes the literal l given by the scan registers, and changes no cell from T + 4 N + 2 on.

theorem Cslib.Algorithms.WordRAM.DPLL.popLoop_spec {w : ℕ} (φ : Logic.PL.CNF ℕ) (N T : ℕ) (t : Lean.DPLL.Trail) (x : WordRAM.Data 32) (hs : StateAt φ N T x t) (haux : x.regs Reg.aux = 1) (hfit : WordRAM.Fits w (T + 5 * N + 2)) :
∃ (y : WordRAM.Data 32), WordRAM.ExecWithin w popLoop x y (14 * List.length t + 14) ∧ StateAt φ N T y (dropSecond t) ∧ y.regs Reg.aux = 0 ∧ ∀ (a : ℕ), T + 4 * N + 2 ≤ a → y.mem a = x.mem a

The pop loop drops the second branches on top of the trail, and changes no cell from T + 4 N + 2 on.

theorem Cslib.Algorithms.WordRAM.DPLL.backtrack_spec {w : ℕ} (φ : Logic.PL.CNF ℕ) (N T : ℕ) (t : Lean.DPLL.Trail) (x : WordRAM.Data 32) (hs : StateAt φ N T x t) (hfit : WordRAM.Fits w (T + 5 * N + 2)) :
∃ (y : WordRAM.Data 32), WordRAM.ExecWithin w backtrack x y (14 * List.length t + 40) ∧ (match t.backtrack with | some t' => StateAt φ N T y t' | none => y.regs Reg.go = 0 ∧ y.regs Reg.out = T + 4 * N ∧ WordRAM.outputList y.mem (T + 4 * N) = [0]) ∧ ∀ (a : ℕ), T + 4 * N + 2 ≤ a → y.mem a = x.mem a

Backtracking: the state of the backtracked trail, or the output [0] at T + 4 N; no cell from T + 4 N + 2 on changes.

theorem Cslib.Algorithms.WordRAM.DPLL.outLoop_spec {w : ℕ} (π : List (Logic.PL.Literal ℕ)) (N T : ℕ) (x : WordRAM.Data 32) (hone : x.regs Reg.one = 1) (htwo : x.regs Reg.two = 2) (htb : x.regs Reg.tb = T) (hob : x.regs Reg.ob = T + 4 * N) (hnv : x.regs Reg.nv = N) (hvar : x.regs Reg.var = 0) (hπ : LitArr x.mem T N π) (hfit : WordRAM.Fits w (T + 5 * N + 2)) :
∃ (y : WordRAM.Data 32), WordRAM.ExecWithin w (WordRAM.Code.loop (WordRAM.Cond.lt Reg.var Reg.nv) outBody) x y (10 * N + 2) ∧ (∀ i < N, y.mem (T + 4 * N + 2 + i) = (π.contains (i, true)).toNat) ∧ ∀ a < T + 4 * N + 2, y.mem a = x.mem a

The model loop writes the bits of the true literals of π for the variables below N at T + 4 N + 2, and changes no cell below.

theorem Cslib.Algorithms.WordRAM.DPLL.outSat_spec {w : ℕ} (φ : Logic.PL.CNF ℕ) (N T : ℕ) (t : Lean.DPLL.Trail) (x : WordRAM.Data 32) (hs : StateAt φ N T x t) (hfit : WordRAM.Fits w (T + 5 * N + 2)) :
∃ (y : WordRAM.Data 32), WordRAM.ExecWithin w outSat x y (10 * N + 10) ∧ y.regs Reg.go = 0 ∧ y.regs Reg.out = T + 4 * N ∧ WordRAM.outputList y.mem (T + 4 * N) = modelWords N t.lits

Writing the model of the trail at T + 4 N.

theorem Cslib.Algorithms.WordRAM.DPLL.stepBody_spec {w : ℕ} (φ : Logic.PL.CNF ℕ) (N T : ℕ) (t : Lean.DPLL.Trail) (x : WordRAM.Data 32) (hs : StateAt φ N T x t) (hfit : WordRAM.Fits w (T + 5 * N + 2)) :
∃ (y : WordRAM.Data 32), WordRAM.ExecWithin w stepBody x y (stepTime φ N t) ∧ match Lean.DPLL.Trail.step φ t with | Sum.inr t' => StateAt φ N T y t' | Sum.inl a => y.regs Reg.go = 0 ∧ y.regs Reg.out = T + 4 * N ∧ WordRAM.outputList y.mem (T + 4 * N) = answerList N a

One step of the search on the word RAM: the state of the next trail, or the output of the answer at T + 4 N.