Skip to content

Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.

A verified LRAT checker, so an unsat is a proof and not an opinion (#222)

Fabio Rovai85cf8f217e9a
diff --git a/lean/Lrat.lean b/lean/Lrat.lean
new file mode 100644
index 00000000..6e01f060
--- /dev/null
+++ b/lean/Lrat.lean
@@ -0,0 +1,4 @@
+import Lrat.Basic
+import Lrat.Propagate
+import Lrat.Check
+import Lrat.Demo
diff --git a/lean/Lrat/Basic.lean b/lean/Lrat/Basic.lean
new file mode 100644
index 00000000..197cd111
--- /dev/null
+++ b/lean/Lrat/Basic.lean
@@ -0,0 +1,121 @@
+/-
+ A verified checker for LRAT proofs of propositional unsatisfiability.
+
+ ## Why this file exists
+
+ `src/fol_solve.rs` says it plainly: "two solvers agreeing on `unsat` is two
+ opinions and not a proof". That is the honest position while nothing reads
+ what the solver emits, and it is what makes Vampire, E, Z3 and Mace4 read
+ `opinion` rather than `certificate` in the front-page figure.
+
+ A solver's `unsat` is an opinion because the search behind it is large, fast
+ and untrusted. The proof it can emit is neither large to check nor trusted to
+ produce: an LRAT line is one clause plus the identifiers of the clauses that
+ make it follow, and checking a line is unit propagation over those few. Same
+ propose-and-dispose split as every other checker here, applied to the one
+ place in this repository that had not got it.
+
+ ## What is proved
+
+ `Lrat.unsat_of_check`: if `check` returns `true` then the formula it was
+ given has no model.
+
+ Nothing here is proved about the solver, about DIMACS parsing, or about the
+ translation that produced the formula. A checker that accepts says the PROOF
+ is a proof of the FORMULA IT WAS HANDED, and not one word more.
+-/
+
+namespace Lrat
+
+/-- A literal: a variable and a polarity.
+
+Deliberately NOT a signed integer. DIMACS writes literals as signed integers
+and that is a serialisation detail; carrying it into the core would put an
+argument about the sign of zero in the middle of every soundness proof. The
+parser converts at the boundary, in `Lrat.Parse`, and a zero there is a parse
+error rather than a literal with no polarity. -/
+structure Lit where
+ var : Nat
+ pos : Bool
+ deriving Repr, DecidableEq
+
+/-- The negation of a literal: same variable, opposite polarity. -/
+def Lit.neg (l : Lit) : Lit := ⟨l.var, !l.pos⟩
+
+abbrev Clause := List Lit
+
+/-- A formula is an indexed list of clauses, because LRAT hints name clauses by
+identifier and identifiers are not positions: a proof deletes clauses as it
+goes, and later identifiers keep counting up regardless. -/
+abbrev Formula := List (Nat × Clause)
+
+/-- An assignment of every variable to a truth value. Total on purpose: a
+partial assignment is a device of the checker, and the theorem is about models,
+which are total. -/
+abbrev Assign := Nat → Bool
+
+def litVal (σ : Assign) (l : Lit) : Bool :=
+ if l.pos then σ l.var else !(σ l.var)
+
+def clauseHolds (σ : Assign) (c : Clause) : Prop :=
+ ∃ l, l ∈ c ∧ litVal σ l = true
+
+def Models (σ : Assign) (F : Formula) : Prop :=
+ ∀ p, p ∈ F → clauseHolds σ p.2
+
+def Unsat (F : Formula) : Prop := ∀ σ, ¬ Models σ F
+
+/-- A literal and its negation never agree. The whole polarity argument, once. -/
+theorem litVal_neg (σ : Assign) (l : Lit) : litVal σ l.neg = !(litVal σ l) := by
+ cases h : l.pos <;> simp [litVal, Lit.neg, h]
+
+/-! ## The trail
+
+The checker carries a partial assignment as a list of literals forced TRUE.
+`l ∈ trail` means `l` is true and `l.neg ∈ trail` means `l` is false. Nothing
+forbids both at once, and nothing needs to: a contradictory trail makes the
+invariant below vacuous rather than unsound. -/
+
+abbrev Trail := List Lit
+
+/-- Every literal on the trail is true under `σ`. This is the whole invariant. -/
+def Respects (σ : Assign) (t : Trail) : Prop :=
+ ∀ l, l ∈ t → litVal σ l = true
+
+/-- `l` is false on the trail exactly when its negation was forced true. -/
+def isFalse (t : Trail) (l : Lit) : Bool := decide (l.neg ∈ t)
+
+/-- What one hinted clause contributes under the current trail. -/
+inductive Step where
+ /-- Every literal is false: no model can respect this trail. -/
+ | conflict
+ /-- All but `l` are false, so `l` must be true. -/
+ | unit (l : Lit)
+ /-- Neither, so this hint proves nothing and the check fails. -/
+ | stuck
+ deriving Repr, DecidableEq
+
+/-- The literals of `c` the trail has not already falsified.
+
+A fold rather than `List.filter`, so the two facts the soundness proof needs
+are one induction each with no library lemma about `filter` in between. There
+is no Mathlib here and the trust surface is meant to stay readable. -/
+def unfalsified (t : Trail) : Clause → Clause
+ | [] => []
+ | l :: rest => if isFalse t l then unfalsified t rest else l :: unfalsified t rest
+
+/-- Nothing left is a conflict; one DISTINCT literal left is a unit; anything
+else proves nothing and the check fails.
+
+Distinct, not one element. A clause may repeat a literal -- DIMACS permits it
+and real files contain it -- and `[-3, 2, -3]` with `2` false leaves `[-3, -3]`,
+which is a unit clause written twice. Matching on `[l]` read that as stuck and
+refused proofs that were correct. Found by fuzzing against picosat with clauses
+drawn WITH replacement; the first fuzzer sampled distinct variables per clause
+and never reached it. -/
+def classify (t : Trail) (c : Clause) : Step :=
+ match unfalsified t c with
+ | [] => .conflict
+ | l :: rest => if rest.all (· == l) then .unit l else .stuck
+
+end Lrat
diff --git a/lean/Lrat/Check.lean b/lean/Lrat/Check.lean
new file mode 100644
index 00000000..5e15f4c7
--- /dev/null
+++ b/lean/Lrat/Check.lean
@@ -0,0 +1,206 @@
+import Lrat.Propagate
+
+/-!
+ The checker, and the theorem that makes it worth running.
+
+ An LRAT line is a clause together with the identifiers of the clauses that
+ make it follow by unit propagation. Checking one line is: assume the clause
+ is FALSE, propagate through the named clauses in the order given, and require
+ a conflict. If that works the clause is implied, so adding it changes no
+ model. Derive the empty clause and there were no models to begin with.
+-/
+
+namespace Lrat
+
+/-- One line of an LRAT proof. -/
+structure Line where
+ id : Nat
+ clause : Clause
+ hints : List Nat
+ deriving Repr
+
+/-- Clauses are named by identifier, not by position. -/
+def lookup (F : Formula) (i : Nat) : Option Clause :=
+ match F with
+ | [] => none
+ | p :: rest => if p.1 = i then some p.2 else lookup rest i
+
+theorem lookup_mem {F : Formula} {i : Nat} {c : Clause}
+ (h : lookup F i = some c) : (i, c) ∈ F := by
+ induction F with
+ | nil => simp [lookup] at h
+ | cons p rest ih =>
+ unfold lookup at h
+ by_cases hp : p.1 = i
+ · rw [if_pos hp] at h
+ have : p.2 = c := by simpa using h
+ subst this
+ subst hp
+ simp
+ · rw [if_neg hp] at h
+ exact List.mem_cons_of_mem _ (ih h)
+
+/-- Scan the hints for one that is a conflict or a unit under the current
+trail, and return it with the hints that remain.
+
+A SCAN and not a queue, and the difference is what makes real solver output
+checkable. LRAT's hints are nominally a propagation order, but a trace from a
+solver is a resolution chain, and a chain is not a propagation order: measured
+on 53 unsatisfiable instances from picosat's extended trace, 40 failed when the
+hints were consumed in the order given and all 53 check when they are scanned.
+
+Scanning is sound for the same reason consuming was: every step still goes
+through `conflict_sound` or `unit_sound`, which care about the clause and the
+trail and not about where in the list the clause was found. It accepts strictly
+more proofs, and every one it accepts is still a proof. -/
+def pick (F : Formula) (t : Trail) : List Nat → Option (Nat × Step × List Nat)
+ | [] => none
+ | h :: hs =>
+ match lookup F h with
+ | none => none
+ | some c =>
+ match classify t c with
+ | .conflict => some (h, .conflict, hs)
+ | .unit l => some (h, .unit l, hs)
+ | .stuck =>
+ match pick F t hs with
+ | some (i, st, rest) => some (i, st, h :: rest)
+ | none => none
+
+/-- Whatever `pick` returns, it really did come from a clause of `F`. -/
+theorem pick_spec {F : Formula} {t : Trail} : ∀ {hs : List Nat} {i : Nat}
+ {st : Step} {rest : List Nat}, pick F t hs = some (i, st, rest) →
+ ∃ c, lookup F i = some c ∧ classify t c = st := by
+ intro hs
+ induction hs with
+ | nil => intro i st rest h; simp [pick] at h
+ | cons hd tl ih =>
+ intro i st rest h
+ unfold pick at h
+ split at h
+ · exact absurd h (by simp)
+ · next c hl =>
+ split at h
+ · next hcl =>
+ simp only [Option.some.injEq, Prod.mk.injEq] at h
+ obtain ⟨hi, hst, _⟩ := h
+ subst hi; subst hst
+ exact ⟨c, hl, hcl⟩
+ · next l hcl =>
+ simp only [Option.some.injEq, Prod.mk.injEq] at h
+ obtain ⟨hi, hst, _⟩ := h
+ subst hi; subst hst
+ exact ⟨c, hl, hcl⟩
+ · split at h
+ · next res hr =>
+ obtain ⟨c', hc1, hc2⟩ := ih hr
+ simp only [Option.some.injEq, Prod.mk.injEq] at h
+ obtain ⟨hi, hst, _⟩ := h
+ subst hi; subst hst
+ exact ⟨c', hc1, hc2⟩
+ · exact absurd h (by simp)
+
+/-- Propagate until a conflict, taking hints in whatever order they work.
+
+`fuel` is the hint count: each successful step consumes one hint, so a proof
+that needs more steps than it gave hints is a proof that does not check. -/
+def runHints (F : Formula) : Nat → Trail → List Nat → Bool
+ | 0, _, _ => false
+ | fuel + 1, t, hs =>
+ match pick F t hs with
+ | some (_, .conflict, _) => true
+ | some (_, .unit l, rest) => runHints F fuel (l :: t) rest
+ | _ => false
+
+/-- No model of `F` respects a trail the hints drive to a conflict. -/
+theorem runHints_sound {σ : Assign} {F : Formula} (hm : Models σ F) :
+ ∀ (fuel : Nat) (t : Trail) (hs : List Nat),
+ Respects σ t → runHints F fuel t hs = true → False := by
+ intro fuel
+ induction fuel with
+ | zero => intro t hs _ h; simp [runHints] at h
+ | succ f ih =>
+ intro t hs hr h
+ unfold runHints at h
+ split at h
+ · next i rest hp =>
+ obtain ⟨c, hl, hcl⟩ := pick_spec hp
+ exact conflict_sound hr hcl (hm (i, c) (lookup_mem hl))
+ · next i l rest hp =>
+ obtain ⟨c, hl, hcl⟩ := pick_spec hp
+ exact ih (l :: t) rest
+ (respects_cons hr (unit_sound hr hcl (hm (i, c) (lookup_mem hl)))) h
+ · exact absurd h (by simp)
+
+/-- The trail a RUP check starts from: the clause, negated. -/
+def negAll (c : Clause) : Trail := c.map Lit.neg
+
+/-- Is `c` implied by `F`, by the propagation the hints name? -/
+def rupCheck (F : Formula) (c : Clause) (hints : List Nat) : Bool :=
+ runHints F hints.length (negAll c) hints
+
+/-- A model that falsifies every literal of `c` respects `negAll c`. -/
+theorem respects_negAll {σ : Assign} {c : Clause} (h : ¬ clauseHolds σ c) :
+ Respects σ (negAll c) := by
+ intro x hx
+ have : ∃ l, l ∈ c ∧ l.neg = x := by
+ unfold negAll at hx
+ exact List.mem_map.mp hx
+ obtain ⟨l, hl, heq⟩ := this
+ subst heq
+ rw [litVal_neg]
+ cases hv : litVal σ l with
+ | false => simp
+ | true => exact absurd ⟨l, hl, hv⟩ h
+
+/-- **A checked line is implied.** -/
+theorem rup_sound {σ : Assign} {F : Formula} {c : Clause} {hints : List Nat}
+ (hm : Models σ F) (h : rupCheck F c hints = true) : clauseHolds σ c := by
+ apply Classical.byContradiction
+ intro hno
+ exact runHints_sound hm hints.length (negAll c) hints (respects_negAll hno) h
+
+/-- Check a proof against a formula. Accepts exactly when some checked line is
+the empty clause. -/
+def check (F : Formula) : List Line → Bool
+ | [] => false
+ | ln :: rest =>
+ if rupCheck F ln.clause ln.hints then
+ if ln.clause.isEmpty then true
+ else check ((ln.id, ln.clause) :: F) rest
+ else false
+
+theorem check_sound : ∀ (p : List Line) (F : Formula) (σ : Assign),
+ Models σ F → check F p = true → False := by
+ intro p
+ induction p with
+ | nil => intro F σ _ h; simp [check] at h
+ | cons ln rest ih =>
+ intro F σ hm h
+ unfold check at h
+ split at h
+ · next hr =>
+ have himp : clauseHolds σ ln.clause := rup_sound hm hr
+ split at h
+ · next he =>
+ -- The empty clause holds under no assignment at all.
+ have hnil : ln.clause = [] := by
+ cases hc : ln.clause with
+ | nil => rfl
+ | cons a as => rw [hc] at he; simp at he
+ rw [hnil] at himp
+ obtain ⟨l, hl, _⟩ := himp
+ exact absurd hl (by simp)
+ · refine ih ((ln.id, ln.clause) :: F) σ ?_ h
+ intro q hq
+ rcases List.mem_cons.mp hq with heq | hin
+ · subst heq; exact himp
+ · exact hm q hin
+ · exact absurd h (by simp)
+
+/-- **The theorem.** A proof this checker accepts is a proof that the formula
+it was handed has no model. -/
+theorem unsat_of_check {F : Formula} {p : List Line} (h : check F p = true) :
+ Unsat F := fun σ hm => check_sound p F σ hm h
+
+end Lrat
diff --git a/lean/Lrat/Demo.lean b/lean/Lrat/Demo.lean
new file mode 100644
index 00000000..5ab17109
--- /dev/null
+++ b/lean/Lrat/Demo.lean
@@ -0,0 +1,86 @@
+import Lrat.Check
+
+/-!
+ The checker, run. Accepting cases and forgeries, side by side.
+
+ A gate nobody has watched fail is not a gate. Every `false` below is this one
+ refusing, and each refuses for a different reason.
+-/
+
+namespace Lrat
+
+def p : Lit := ⟨1, true⟩
+def q : Lit := ⟨2, true⟩
+
+-- `x` and `not x`: unsatisfiable, and the smallest thing that is.
+def F₁ : Formula := [(1, [p]), (2, [p.neg])]
+
+-- Assume the empty clause false, which assumes nothing, propagate `p` from
+-- clause 1, and then clause 2 is all false.
+def proof₁ : List Line := [⟨3, [], [1, 2]⟩]
+
+/-- info: true -/
+#guard_msgs in #eval check F₁ proof₁
+
+theorem F₁_unsat : Unsat F₁ := unsat_of_check (p := proof₁) (by native_decide)
+
+-- Four clauses over two variables: `p ∨ q`, `p ∨ ¬q`, `¬p ∨ q`, `¬p ∨ ¬q`.
+def F₂ : Formula :=
+ [(1, [p, q]), (2, [p, q.neg]), (3, [p.neg, q]), (4, [p.neg, q.neg])]
+
+-- Derive `p` from 1 and 2, `¬p` from 3 and 4, then the empty clause from both.
+def proof₂ : List Line :=
+ [⟨5, [p], [2, 1]⟩, ⟨6, [p.neg], [4, 3]⟩, ⟨7, [], [5, 6]⟩]
+
+/-- info: true -/
+#guard_msgs in #eval check F₂ proof₂
+
+theorem F₂_unsat : Unsat F₂ := unsat_of_check (p := proof₂) (by native_decide)
+
+/-! ## Forgeries
+
+Five ways to be refused, one per way a proof can be wrong. -/
+
+-- Every line checks, and none of them is the empty clause, so nothing is
+-- proved. A prefix of a refutation is not a refutation.
+/-- info: false -/
+#guard_msgs in #eval check F₂ [⟨5, [p], [2, 1]⟩]
+
+-- Hints that do not propagate. Clauses 3 and 4 are `¬p ∨ q` and `¬p ∨ ¬q`,
+-- and under the trail `¬p` neither is unit, so the line is stuck. The hints
+-- have to be the clauses that actually do the work.
+/-- info: false -/
+#guard_msgs in #eval check F₂ [⟨5, [p], [3, 4]⟩, ⟨7, [], [5]⟩]
+
+-- Naming a clause that is not there.
+/-- info: false -/
+#guard_msgs in #eval check F₂ [⟨7, [], [99]⟩]
+
+-- Claiming the empty clause with no hints at all: the commonest forgery there
+-- is, refused because running out of hints WITHOUT a conflict is a failure
+-- and not a success.
+/-- info: false -/
+#guard_msgs in #eval check F₂ [⟨7, [], []⟩]
+
+-- A satisfiable formula has no refutation and the checker will not invent one.
+/-- info: false -/
+#guard_msgs in #eval check [(1, [p, q])] [⟨2, [], [1]⟩]
+
+/-- And the claim behind that last one, for EVERY proof rather than the one
+tried: a formula with a model cannot be refuted. Exhibit the model, and the
+soundness theorem does the rest. -/
+theorem no_refutation_of_a_satisfiable_formula (prf : List Line) :
+ check [(1, [p, q])] prf = false := by
+ cases h : check [(1, [p, q])] prf with
+ | false => rfl
+ | true =>
+ exact absurd
+ (unsat_of_check h (fun _ => true)
+ (by
+ intro c hc
+ have : c = (1, [p, q]) := by simpa using hc
+ subst this
+ exact ⟨p, by simp, rfl⟩))
+ (by simp)
+
+end Lrat
diff --git a/lean/Lrat/Propagate.lean b/lean/Lrat/Propagate.lean
new file mode 100644
index 00000000..1a0b0469
--- /dev/null
+++ b/lean/Lrat/Propagate.lean
@@ -0,0 +1,119 @@
+import Lrat.Basic
+
+/-!
+ The two facts every LRAT check rests on, and nothing else.
+
+ `conflict_sound`: a clause of `F` whose literals the trail has all falsified
+ rules out every model that respects the trail.
+
+ `unit_sound`: a clause of `F` with exactly one literal left forces that
+ literal true in every model that respects the trail.
+
+ Everything above these is induction.
+-/
+
+namespace Lrat
+
+theorem isFalse_iff {t : Trail} {l : Lit} : isFalse t l = true ↔ l.neg ∈ t := by
+ simp [isFalse]
+
+/-- A literal the trail calls false is false under any model respecting it.
+One line, because `Lit` carries its polarity instead of encoding it in a sign. -/
+theorem false_of_isFalse {σ : Assign} {t : Trail} (hr : Respects σ t)
+ {l : Lit} (hf : isFalse t l = true) : litVal σ l = false := by
+ have hneg : litVal σ l.neg = true := hr _ (isFalse_iff.mp hf)
+ rw [litVal_neg] at hneg
+ cases h : litVal σ l with
+ | false => rfl
+ | true => rw [h] at hneg; exact absurd hneg (by simp)
+
+/-- Nothing survives `unfalsified` that the trail has falsified, and nothing
+the trail has left alone is dropped. Both directions, one induction. -/
+theorem mem_unfalsified {t : Trail} {c : Clause} {l : Lit} :
+ l ∈ unfalsified t c ↔ (l ∈ c ∧ isFalse t l = false) := by
+ induction c with
+ | nil => simp [unfalsified]
+ | cons a rest ih =>
+ unfold unfalsified
+ by_cases ha : isFalse t a = true
+ · rw [if_pos ha, ih]
+ constructor
+ · intro h
+ exact ⟨List.mem_cons_of_mem _ h.1, h.2⟩
+ · intro h
+ rcases List.mem_cons.mp h.1 with heq | hin
+ · subst heq
+ rw [ha] at h
+ exact absurd h.2 (by simp)
+ · exact ⟨hin, h.2⟩
+ · rw [if_neg ha]
+ simp only [List.mem_cons]
+ constructor
+ · intro h
+ rcases h with heq | hin
+ · subst heq
+ exact ⟨Or.inl rfl, by simpa using ha⟩
+ · exact ⟨Or.inr (ih.mp hin).1, (ih.mp hin).2⟩
+ · intro h
+ rcases h.1 with heq | hin
+ · exact Or.inl heq
+ · exact Or.inr (ih.mpr ⟨hin, h.2⟩)
+
+/-- **Conflict is sound.** -/
+theorem conflict_sound {σ : Assign} {t : Trail} {c : Clause}
+ (hr : Respects σ t) (hc : classify t c = .conflict)
+ (hsat : clauseHolds σ c) : False := by
+ have hnil : unfalsified t c = [] := by
+ unfold classify at hc
+ split at hc
+ · assumption
+ · split at hc <;> exact absurd hc (by simp)
+ obtain ⟨l, hl, hval⟩ := hsat
+ by_cases hf : isFalse t l
+ · rw [false_of_isFalse hr hf] at hval; exact Bool.noConfusion hval
+ · have : l ∈ unfalsified t c := mem_unfalsified.mpr ⟨hl, by simpa using hf⟩
+ rw [hnil] at this
+ exact absurd this (by simp)
+
+/-- **Unit is sound.** Every literal of `c` is either false on the trail or is
+`l` itself, so a model respecting the trail has to make `l` true. -/
+theorem unit_sound {σ : Assign} {t : Trail} {c : Clause} {l : Lit}
+ (hr : Respects σ t) (hc : classify t c = .unit l)
+ (hsat : clauseHolds σ c) : litVal σ l = true := by
+ -- The shape the classifier saw: a non-empty remainder, every element of it
+ -- equal to the head.
+ have hshape : ∃ rest, unfalsified t c = l :: rest ∧ rest.all (· == l) = true := by
+ unfold classify at hc
+ split at hc
+ · exact absurd hc (by simp)
+ · next hd rest heq =>
+ split at hc
+ · next hall =>
+ have : hd = l := by simpa using hc
+ subst this
+ exact ⟨rest, heq, hall⟩
+ · exact absurd hc (by simp)
+ obtain ⟨rest, heq, hall⟩ := hshape
+ obtain ⟨m, hm, hval⟩ := hsat
+ by_cases hf : isFalse t m
+ · rw [false_of_isFalse hr hf] at hval; exact Bool.noConfusion hval
+ · have hmem : m ∈ unfalsified t c := mem_unfalsified.mpr ⟨hm, by simpa using hf⟩
+ rw [heq] at hmem
+ rcases List.mem_cons.mp hmem with h | h
+ · subst h; exact hval
+ · have : m = l := by
+ have := List.all_eq_true.mp hall m h
+ simpa using this
+ subst this
+ exact hval
+
+/-- Extending a respected trail with a literal the model makes true keeps it
+respected. -/
+theorem respects_cons {σ : Assign} {t : Trail} {l : Lit}
+ (hr : Respects σ t) (hl : litVal σ l = true) : Respects σ (l :: t) := by
+ intro x hx
+ rcases List.mem_cons.mp hx with h | h
+ · subst h; exact hl
+ · exact hr x h
+
+end Lrat
diff --git a/lean/LratMain.lean b/lean/LratMain.lean
new file mode 100644
index 00000000..c803ca46
--- /dev/null
+++ b/lean/LratMain.lean
@@ -0,0 +1,165 @@
+import Lrat
+
+/-!
+`oo-lrat PROBLEM.cnf PROOF.lrat`
+
+Reads a DIMACS CNF and an LRAT proof, checks the proof against that formula,
+and prints one JSON object.
+
+Exit codes, matching the convention the other checkers here use:
+
+* `0` the proof checks, so **the formula in `PROBLEM.cnf` has no model**. The
+ statement covering that is `Lrat.unsat_of_check`.
+* `1` the proof does not check. That is a statement about the CERTIFICATE and
+ not about the formula: a solver can fail to emit a proof that exists, and
+ this exit code never means the formula is satisfiable.
+* `2` a file could not be read or parsed. No verdict either way.
+
+## What exit 0 does not say
+
+* Nothing about the SOLVER. A checked proof is checked; the search that found
+ it is still untrusted, which is the point of checking it.
+* Nothing about whether `PROBLEM.cnf` is your problem. The translation that
+ produced the CNF is outside this checker, and a proof of the wrong formula
+ is a proof of the wrong formula however well it checks.
+* Nothing about first-order satisfiability. This is propositional. A CNF that
+ came from grounding a first-order problem at some finite size says what it
+ says about that grounding and no more.
+
+## Deletion lines
+
+`d` lines are parsed and IGNORED. Deleting a clause is a performance device
+for the checker's own propagation, not part of what is proved: every clause
+this checker holds is either an original one or one it has already verified is
+implied, so keeping a clause that the proof deleted can only make propagation
+easier. It cannot make an accepted proof unsound, because `check_sound` proves
+the invariant over whatever formula it is actually carrying.
+-/
+
+open Lrat
+
+/-- Split on runs of whitespace, dropping empties. -/
+def tokens (s : String) : List String :=
+ (s.splitOn " ").flatMap (fun a => (a.splitOn "\t").flatMap (fun b => b.splitOn "\r"))
+ |>.filter (· ≠ "")
+
+def parseInt? (s : String) : Option Int :=
+ if s.startsWith "-" then
+ match (s.drop 1).toNat? with
+ | some n => if n == 0 then none else some (-(Int.ofNat n))
+ | none => none
+ else
+ match s.toNat? with
+ | some n => some (Int.ofNat n)
+ | none => none
+
+/-- A DIMACS literal to a `Lit`. Zero is the clause terminator and never a
+literal, so it is rejected here rather than given a polarity. -/
+def litOfInt? (i : Int) : Option Lit :=
+ if i > 0 then some ⟨i.toNat, true⟩
+ else if i < 0 then some ⟨(-i).toNat, false⟩
+ else none
+
+/-- DIMACS CNF. `c` comments and the `p cnf` header are skipped; every other
+line is a clause terminated by `0`. Clauses are numbered from 1 in the order
+they appear, which is what LRAT hints refer to. -/
+def parseCnf (text : String) : Option Formula := do
+ let mut out : Formula := []
+ let mut id := 1
+ let mut cur : Clause := []
+ let mut open_ := false
+ for line in text.splitOn "\n" do
+ let ts := tokens line
+ match ts with
+ | [] => pure ()
+ | h :: _ =>
+ if h == "c" || h == "p" || h == "%" then pure ()
+ else
+ for t in ts do
+ let i ← parseInt? t
+ if i == 0 then
+ out := out ++ [(id, cur)]
+ id := id + 1
+ cur := []
+ open_ := false
+ else
+ let l ← litOfInt? i
+ cur := cur ++ [l]
+ open_ := true
+ -- A clause left unterminated is a truncated file, not an empty clause.
+ if open_ then none else some out
+
+/-- One LRAT line: `id lit* 0 hint* 0`, or `id d id* 0`, which is ignored. -/
+def parseLratLine (ts : List String) : Option (Option Line) :=
+ match ts with
+ | [] => some none
+ | idTok :: rest =>
+ match idTok.toNat? with
+ | none => none
+ | some id =>
+ match rest with
+ | "d" :: _ => some none -- a deletion; see the header
+ | _ => do
+ let mut lits : Clause := []
+ let mut hints : List Nat := []
+ let mut seenZero := false
+ let mut closed := false
+ for t in rest do
+ let i ← parseInt? t
+ if i == 0 then
+ if seenZero then closed := true else seenZero := true
+ else if !seenZero then
+ let l ← litOfInt? i
+ lits := lits ++ [l]
+ else
+ -- Hints may be negative in full LRAT (RAT hints). This checker
+ -- implements the RUP fragment only, so a negative hint is a
+ -- proof it cannot check rather than one it may skip.
+ if i < 0 then failure
+ else hints := hints ++ [i.toNat]
+ if closed then some (some ⟨id, lits, hints⟩) else none
+
+def parseLrat (text : String) : Option (List Line) := do
+ let mut out : List Line := []
+ for line in text.splitOn "\n" do
+ match ← parseLratLine (tokens line) with
+ | none => pure ()
+ | some ln => out := out ++ [ln]
+ some out
+
+def jsonStr (s : String) : String :=
+ "\"" ++ (s.foldl (fun acc c =>
+ acc ++ (if c == '"' then "\\\"" else if c == '\\' then "\\\\" else String.singleton c)) "") ++ "\""
+
+def main (args : List String) : IO UInt32 := do
+ match args with
+ | [cnfPath, prfPath] =>
+ let cnfText ← try IO.FS.readFile cnfPath catch _ =>
+ IO.println s!"\{\"ok\":false,\"error\":\"could not read {cnfPath}\"}"
+ return 2
+ let prfText ← try IO.FS.readFile prfPath catch _ =>
+ IO.println s!"\{\"ok\":false,\"error\":\"could not read {prfPath}\"}"
+ return 2
+ match parseCnf cnfText, parseLrat prfText with
+ | some F, some prf =>
+ if check F prf then
+ IO.println s!"\{\"ok\":true,\"verdict\":\"unsatisfiable\",\
+ \"clauses\":{F.length},\"lines\":{prf.length},\
+ \"theorem\":\"Lrat.unsat_of_check\",\
+ \"means\":\"the formula in this CNF has no model, of any size\"}"
+ return 0
+ else
+ IO.println s!"\{\"ok\":false,\"verdict\":\"not_checked\",\
+ \"clauses\":{F.length},\"lines\":{prf.length},\
+ \"means\":\"the proof did not check. This says nothing about whether \
+the formula is satisfiable: a solver can fail to emit a proof that exists\"}"
+ return 1
+ | none, _ =>
+ IO.println s!"\{\"ok\":false,\"error\":\"could not parse {cnfPath} as DIMACS CNF\"}"
+ return 2
+ | _, none =>
+ IO.println s!"\{\"ok\":false,\"error\":\"could not parse {prfPath} as LRAT\"}"
+ return 2
+ | _ =>
+ IO.println "usage: oo-lrat PROBLEM.cnf PROOF.lrat"
+ return 2
diff --git a/lean/lakefile.toml b/lean/lakefile.toml
index f90037e3..2f93a080 100644
--- a/lean/lakefile.toml
+++ b/lean/lakefile.toml
@@ -1,6 +1,6 @@
name = "oocert"
version = "0.1.0"
-defaultTargets = ["OOCert", "oo-cert", "oo-horn", "Shacl", "oo-shacl", "oo-refute", "Dl", "oo-dlmodel", "oo-dlrefute", "Fol", "oo-folmodel", "Institution", "Fo", "oo-fores"]
+defaultTargets = ["OOCert", "oo-cert", "oo-horn", "Shacl", "oo-shacl", "oo-refute", "Dl", "oo-dlmodel", "oo-dlrefute", "Fol", "oo-folmodel", "Institution", "Lrat", "oo-lrat", "Fo", "oo-fores"]
# Every library and executable here is in defaultTargets on purpose. A target
# left out of the default set is not built by a bare `lake build`, which is what
@@ -85,6 +85,23 @@ root = "FolMain"
[[lean_lib]]
name = "Institution"
+# Propositional proof checking. A solver's `unsat` is an opinion while nothing
+# reads the proof behind it, which is why the first-order family is drawn as
+# `opinion` and not `certificate`. An LRAT line is one clause plus the
+# identifiers of the clauses that make it follow, and checking it is unit
+# propagation over those few: expensive to find, cheap to check, which is the
+# same split every other checker here rests on.
+#
+# Separate from `Fol` on purpose. `Fol` certifies that a problem HAS a model by
+# exhibiting one; this certifies that a propositional formula has NONE by
+# exhibiting a refutation. Different sentences, different executables.
+[[lean_lib]]
+name = "Lrat"
+
+[[lean_exe]]
+name = "oo-lrat"
+root = "LratMain"
+
# First-order resolution, checked.
#
# Decision 0005 says a superposition proof cannot be certified here because it
diff --git a/src/lib.rs b/src/lib.rs
index 820c9798..5e392938 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -118,6 +118,9 @@ pub mod toolfilter;
pub mod fol_model;
pub mod fol_solve;
pub mod tptp;
+/// Propositional refutation with the solver's proof CHECKED, which is what
+/// separates a certificate from two solvers agreeing.
+pub mod sat;
/// Reading a prover's TSTP derivation and re-checking what can be re-checked.
/// Decision 0005's addendum says exactly what this earns and what it does not.
pub mod tstp;
diff --git a/src/sat.rs b/src/sat.rs
new file mode 100644
index 00000000..dfd4b93d
--- /dev/null
+++ b/src/sat.rs
@@ -0,0 +1,182 @@
+//! Propositional refutation, with the proof checked rather than believed.
+//!
+//! `fol_solve.rs` states the problem this module answers: "two solvers agreeing
+//! on `unsat` is two opinions and not a proof". A solver's `unsat` is an opinion
+//! because the search behind it is large, fast and untrusted. The proof it emits
+//! is neither large to check nor trusted to produce, so this runs the solver,
+//! converts what it emits, and hands it to `oo-lrat`, whose acceptance
+//! discharges `Lrat.unsat_of_check`.
+//!
+//! The word `refuted` is minted by [`crate::verdict::CheckerRun::accepted_naming`]
+//! and nowhere else, so it cannot be printed without a checker run that named
+//! that theorem.
+
+use crate::verdict::{CheckerBinary, CheckerRun, Certified};
+use std::path::{Path, PathBuf};
+use std::process::Command;
+
+/// What a refutation attempt produced.
+#[derive(Debug)]
+pub enum SatOutcome {
+ /// The proof checked. The formula has no model, and the token says which
+ /// statement says so.
+ Refuted(Certified),
+ /// The solver reported a model. Not a certificate: this module checks
+ /// refutations, and a reported model is the solver's word.
+ Satisfiable,
+ /// No answer, and the reason. Never folded into either of the above.
+ Undetermined(String),
+}
+
+/// A clause as DIMACS writes it: non-zero signed integers.
+pub type Clause = Vec<i32>;
+
+/// Render clauses as DIMACS CNF.
+pub fn to_dimacs(clauses: &[Clause]) -> String {
+ let vars = clauses
+ .iter()
+ .flat_map(|c| c.iter())
+ .map(|l| l.unsigned_abs())
+ .max()
+ .unwrap_or(0);
+ let mut s = format!("p cnf {} {}\n", vars, clauses.len());
+ for c in clauses {
+ for l in c {
+ s.push_str(&l.to_string());
+ s.push(' ');
+ }
+ s.push_str("0\n");
+ }
+ s
+}
+
+/// picosat's extended trace (`-T`) to LRAT.
+///
+/// The formats agree on derived lines: `id lit* 0 antecedent* 0`. A trace also
+/// lists the ORIGINAL clauses, with an EMPTY antecedent list, and those come
+/// from the CNF rather than from the proof, so they are dropped. A trace also
+/// lists only the clauses in the unsat core and keeps their input identifiers,
+/// which is why identifiers are not positions anywhere in this pipeline.
+pub fn trace_to_lrat(trace: &str) -> String {
+ let mut out = String::new();
+ for line in trace.lines() {
+ let toks: Vec<&str> = line.split_whitespace().collect();
+ let Some(zero) = toks.iter().position(|t| *t == "0") else {
+ continue;
+ };
+ // An original clause: nothing between the two terminators.
+ if toks.get(zero + 1).is_none_or(|t| *t == "0") {
+ continue;
+ }
+ out.push_str(&toks.join(" "));
+ out.push('\n');
+ }
+ out
+}
+
+fn find(env: &str, name: &str) -> Option<PathBuf> {
+ if let Ok(p) = std::env::var(env) {
+ let p = PathBuf::from(p);
+ return p.exists().then_some(p);
+ }
+ let built = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
+ .join("lean/.lake/build/bin")
+ .join(name);
+ if built.exists() {
+ return Some(built);
+ }
+ which_on_path(name)
+}
+
+fn which_on_path(name: &str) -> Option<PathBuf> {
+ let path = std::env::var_os("PATH")?;
+ std::env::split_paths(&path)
+ .map(|d| d.join(name))
+ .find(|p| p.is_file())
+}
+
+/// Refute `clauses`, and check the refutation.
+///
+/// `dir` is where the intermediate files go; the caller owns its lifetime so a
+/// failed run can be inspected.
+pub fn refute(clauses: &[Clause], dir: &Path) -> anyhow::Result<SatOutcome> {
+ let Some(solver) = find("OO_SAT_SOLVER", "picosat") else {
+ return Ok(SatOutcome::Undetermined(
+ "no SAT solver found. Set OO_SAT_SOLVER, or install picosat".into(),
+ ));
+ };
+ let Some(checker) = find("OO_LRAT", "oo-lrat") else {
+ return Ok(SatOutcome::Undetermined(
+ "oo-lrat was not found, so a proof could not be checked. Build it \
+ with `cd lean && lake build`, or set OO_LRAT"
+ .into(),
+ ));
+ };
+
+ std::fs::create_dir_all(dir)?;
+ let cnf = dir.join("problem.cnf");
+ let trace = dir.join("problem.trace");
+ let lrat = dir.join("problem.lrat");
+ std::fs::write(&cnf, to_dimacs(clauses))?;
+
+ let run = Command::new(&solver).arg("-T").arg(&trace).arg(&cnf).output()?;
+ match run.status.code() {
+ // picosat: 20 unsatisfiable, 10 satisfiable.
+ Some(10) => return Ok(SatOutcome::Satisfiable),
+ Some(20) => {}
+ other => {
+ return Ok(SatOutcome::Undetermined(format!(
+ "the solver exited {other:?}, which its CLI does not define as an answer"
+ )))
+ }
+ }
+
+ let trace_text = std::fs::read_to_string(&trace).unwrap_or_default();
+ if trace_text.is_empty() {
+ return Ok(SatOutcome::Undetermined(
+ "the solver said unsatisfiable and emitted no trace, so there is \
+ nothing to check. That is an opinion, and this function does not \
+ report opinions"
+ .into(),
+ ));
+ }
+ std::fs::write(&lrat, trace_to_lrat(&trace_text))?;
+
+ let mut cmd = Command::new(checker.clone());
+ cmd.arg(&cnf).arg(&lrat);
+ let checked = CheckerRun::spawn(&CheckerBinary::found_at(checker), cmd)?;
+ match checked.accepted_naming(&["Lrat.unsat_of_check"]) {
+ Some(cert) => Ok(SatOutcome::Refuted(cert)),
+ None => Ok(SatOutcome::Undetermined(format!(
+ "the solver said unsatisfiable and the proof did NOT check, which is \
+ a disagreement between a solver and a checker and not a verdict. \
+ exit {}: {}",
+ checked.exit(),
+ checked.output().trim()
+ ))),
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn dimacs_counts_the_variables_it_saw() {
+ let s = to_dimacs(&[vec![1, -3], vec![2]]);
+ assert!(s.starts_with("p cnf 3 2\n"), "{s}");
+ assert!(s.contains("1 -3 0\n"), "{s}");
+ }
+
+ #[test]
+ fn the_original_clauses_of_a_trace_are_not_proof_lines() {
+ // Two originals (empty antecedent lists) and one derived line.
+ let t = "1 1 2 0 0\n2 -1 0 0\n5 2 0 1 2 0\n";
+ assert_eq!(trace_to_lrat(t), "5 2 0 1 2 0\n");
+ }
+
+ #[test]
+ fn a_blank_or_ragged_line_is_skipped_and_not_guessed_at() {
+ assert_eq!(trace_to_lrat("\n \nnot a line\n"), "");
+ }
+}
diff --git a/tests/lean_lrat_checker_test.rs b/tests/lean_lrat_checker_test.rs
new file mode 100644
index 00000000..af653562
--- /dev/null
+++ b/tests/lean_lrat_checker_test.rs
@@ -0,0 +1,198 @@
+//! `oo-lrat` end to end, through the same discipline every other checker uses.
+//!
+//! The point of this checker is the one thing `src/fol_solve.rs` says it does
+//! not have: "two solvers agreeing on `unsat` is two opinions and not a proof".
+//! A checked proof is not an opinion. These tests run the binary on real files
+//! and require the three exit codes to mean three different things.
+
+use std::path::PathBuf;
+use std::process::Command;
+use std::sync::atomic::{AtomicU64, Ordering};
+
+/// One scratch directory per CALL, not per process.
+///
+/// Keyed on the pid alone, the five tests below share a directory, and cargo
+/// runs them in parallel: each overwrote the others' `p.cnf` and `p.lrat`
+/// mid-run, so four of them checked a proof against a formula another test had
+/// just written. Every one of them failed, which was lucky; the shape that is
+/// not lucky is two tests agreeing for the wrong reason.
+static NEXT: AtomicU64 = AtomicU64::new(0);
+
+fn checker() -> Option<PathBuf> {
+ if let Ok(p) = std::env::var("OO_LRAT") {
+ let p = PathBuf::from(p);
+ return p.exists().then_some(p);
+ }
+ let built = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("lean/.lake/build/bin/oo-lrat");
+ built.exists().then_some(built)
+}
+
+struct Run {
+ code: i32,
+ out: String,
+}
+
+fn run(cnf: &str, lrat: &str) -> Option<Run> {
+ let bin = checker()?;
+ let dir = std::env::temp_dir().join(format!(
+ "oo-lrat-{}-{}",
+ std::process::id(),
+ NEXT.fetch_add(1, Ordering::Relaxed)
+ ));
+ std::fs::create_dir_all(&dir).unwrap();
+ let c = dir.join("p.cnf");
+ let l = dir.join("p.lrat");
+ std::fs::write(&c, cnf).unwrap();
+ std::fs::write(&l, lrat).unwrap();
+ let o = Command::new(bin).arg(&c).arg(&l).output().expect("oo-lrat runs");
+ Some(Run {
+ code: o.status.code().unwrap_or(-1),
+ out: String::from_utf8_lossy(&o.stdout).into_owned(),
+ })
+}
+
+const UNSAT: &str = "p cnf 2 4\n1 2 0\n1 -2 0\n-1 2 0\n-1 -2 0\n";
+const PROOF: &str = "5 1 0 2 1 0\n6 -1 0 4 3 0\n7 0 5 6 0\n";
+
+#[test]
+fn a_checked_proof_exits_zero_and_names_its_theorem() {
+ let Some(r) = run(UNSAT, PROOF) else { return };
+ assert_eq!(r.code, 0, "a checked proof must be exit 0: {}", r.out);
+ assert!(
+ r.out.contains(r#""theorem":"Lrat.unsat_of_check""#),
+ "exit 0 must name the statement it discharged: {}",
+ r.out
+ );
+ assert!(r.out.contains(r#""verdict":"unsatisfiable""#), "{}", r.out);
+}
+
+#[test]
+fn a_proof_that_does_not_check_is_exit_one_and_names_no_theorem() {
+ // Same formula, hints that do not propagate.
+ let Some(r) = run(UNSAT, "5 1 0 3 4 0\n7 0 5 0\n") else { return };
+ assert_eq!(r.code, 1, "a proof that fails to check must be exit 1: {}", r.out);
+ assert!(
+ !r.out.contains("theorem"),
+ "a refusal must not name a theorem, or the word is free: {}",
+ r.out
+ );
+}
+
+#[test]
+fn a_satisfiable_formula_cannot_be_refuted() {
+ // `1 ∨ 2` has a model, so no proof of the empty clause can check.
+ let Some(r) = run("p cnf 2 1\n1 2 0\n", "2 0 1 0\n") else { return };
+ assert_eq!(r.code, 1, "{}", r.out);
+ assert!(!r.out.contains("theorem"), "{}", r.out);
+}
+
+#[test]
+fn an_unreadable_file_is_exit_two_and_not_a_verdict() {
+ // A clause that never terminates is a truncated file, not an empty clause.
+ let Some(r) = run("p cnf 1 1\n1\n", PROOF) else { return };
+ assert_eq!(r.code, 2, "a parse failure must be exit 2, not a verdict: {}", r.out);
+ assert!(!r.out.contains("verdict"), "{}", r.out);
+}
+
+#[test]
+fn the_empty_clause_must_be_reached_and_a_prefix_is_not_a_proof() {
+ let Some(r) = run(UNSAT, "5 1 0 2 1 0\n") else { return };
+ assert_eq!(r.code, 1, "every line checking is not the same as proving: {}", r.out);
+}
+
+// ── The solver, wired ───────────────────────────────────────────────────────
+//
+// The tests above check proofs this file wrote. These check proofs a SOLVER
+// wrote, which is the only kind that matters: a checker that only accepts its
+// author's proofs has not been tested against anything.
+
+use open_ontologies::sat::{refute, to_dimacs, SatOutcome};
+
+fn solver_present() -> bool {
+ std::env::var("OO_SAT_SOLVER").map(|p| std::path::Path::new(&p).exists()).unwrap_or(false)
+ || std::env::split_paths(&std::env::var_os("PATH").unwrap_or_default())
+ .any(|d| d.join("picosat").is_file())
+}
+
+fn scratch(tag: &str) -> PathBuf {
+ std::env::temp_dir().join(format!(
+ "oo-sat-{}-{}-{}",
+ std::process::id(),
+ tag,
+ NEXT.fetch_add(1, Ordering::Relaxed)
+ ))
+}
+
+/// A deterministic pseudo-random 3-CNF. No `rand` dependency, and the same
+/// seeds give the same formulas on every machine, which is what makes a
+/// failure reproducible from the seed alone.
+fn random_cnf(seed: u64) -> Vec<Vec<i32>> {
+ let mut x = seed.wrapping_mul(6364136223846793005).wrapping_add(1);
+ let mut next = move || {
+ x ^= x << 13;
+ x ^= x >> 7;
+ x ^= x << 17;
+ x
+ };
+ let vars = 3 + (next() % 7) as i32;
+ let n = (vars as f64 * 5.5) as usize;
+ (0..n)
+ .map(|_| {
+ (0..3)
+ .map(|_| {
+ let v = 1 + (next() % vars as u64) as i32;
+ if next() % 2 == 0 { v } else { -v }
+ })
+ .collect()
+ })
+ .collect()
+}
+
+#[test]
+fn a_solvers_own_refutations_check() {
+ if checker().is_none() || !solver_present() {
+ return;
+ }
+ // No `undetermined` counter: an undetermined outcome panics below, so a
+ // count of them could never be read, and `-D warnings` says so.
+ let (mut refuted, mut sat) = (0, 0);
+ for seed in 1..60u64 {
+ let clauses = random_cnf(seed);
+ let dir = scratch("fuzz");
+ match refute(&clauses, &dir).expect("the pipeline runs") {
+ SatOutcome::Refuted(cert) => {
+ assert_eq!(
+ cert.theorem(),
+ "Lrat.unsat_of_check",
+ "the token must carry the statement oo-lrat named"
+ );
+ refuted += 1;
+ }
+ SatOutcome::Satisfiable => sat += 1,
+ SatOutcome::Undetermined(why) => {
+ // A solver that says unsat and whose proof does not check is a
+ // DISAGREEMENT, and the one thing that must never be silent.
+ panic!("seed {seed}: {why}\n{}", to_dimacs(&clauses));
+ }
+ }
+ let _ = std::fs::remove_dir_all(&dir);
+ }
+ assert!(
+ refuted > 0 && sat > 0,
+ "the corpus must contain both answers or it is measuring nothing: \
+ refuted {refuted}, satisfiable {sat}"
+ );
+}
+
+#[test]
+fn a_satisfiable_formula_is_not_reported_as_refuted() {
+ if checker().is_none() || !solver_present() {
+ return;
+ }
+ let dir = scratch("sat");
+ match refute(&[vec![1, 2], vec![-1, 2]], &dir).expect("runs") {
+ SatOutcome::Satisfiable => {}
+ other => panic!("a satisfiable formula came back as {other:?}"),
+ }
+ let _ = std::fs::remove_dir_all(&dir);
+}