Signed literals, clauses, and distance to satisfiability
DefinitionYoshidaTwoSAT_FormulaA formula is a list of clauses on Boolean variables. The distance is the minimum number of clauses violated by any one assignment.
import Mathlib.Order.Lattice.Nat
namespace TwoSAT
/-- The sign gives the variable value that makes the literal true. -/
structure Literal (V : Type) where
var : V
sign : Bool
deriving DecidableEq
variable {V : Type}
def Literal.flip (a : Literal V) : Literal V := ⟨a.var, !a.sign⟩
def Literal.eval (a : Literal V) (t : V → Bool) : Bool := t a.var == a.sign
@[simp] theorem Literal.flip_flip (a : Literal V) : a.flip.flip = a := by
cases a with
| mk x s => cases s <;> rfl
@[simp] theorem Literal.eval_flip (a : Literal V) (t : V → Bool) :
a.flip.eval t = !(a.eval t) := by
rcases a with ⟨x, s⟩
cases h : t x <;> cases s <;> simp [Literal.eval, Literal.flip, h]
/-- Input normalization represents (a ∨ a) by a unit and (x ∨ ¬x) by taut x. -/
inductive Clause (V : Type) where
| unit (a : Literal V)
| taut (x : V)
| pair (a b : Literal V)
deriving DecidableEq
def Clause.eval (t : V → Bool) : Clause V → Bool
| .unit a => a.eval t
| .taut _ => true
| .pair a b => a.eval t || b.eval t
def Clause.incident (x : V) : Clause V → Prop
| .unit a => x = a.var
| .taut y => x = y
| .pair a b => x = a.var ∨ x = b.var
def loss (t : V → Bool) : List (Clause V) → ℕ
| [] => 0
| c :: cs => (if c.eval t then 0 else 1) + loss t cs
def Satisfiable (F : List (Clause V)) : Prop := ∃ t, loss t F = 0
theorem loss_eq_zero_iff (t : V → Bool) (F : List (Clause V)) :
loss t F = 0 ↔ ∀ c ∈ F, c.eval t = true := by
induction F with
| nil => simp [loss]
| cons c cs ih => cases h : c.eval t <;> simp [loss, h, ih]
noncomputable def distance (F : List (Clause V)) : ℕ := sInf (Set.range (fun t => loss t F))
end TwoSAT
Read-back
What the Lean code literally says, in plain math · OpenAI GPT-6 (Codex)
For any type of variables, a literal is a pair with and Boolean. Flipping it gives . Under an assignment from to the Booleans, evaluates to true exactly when . Flipping twice restores the literal, and flipping once negates its evaluation under every assignment. A clause is one of three distinct forms. A unit contains one literal and has its value. A tautology carries a variable and always evaluates to true. A pair contains two literals in order and evaluates to their Boolean disjunction. A variable is incident to a unit exactly when it is the literal's variable, to a tautology exactly when it is the carried variable, and to a pair exactly when it is either literal's variable. The pair constructor permits equal variables, equal literals, and either order. For every finite list of clauses and assignment , the loss is the number of false clause occurrences, counting repetitions. It is zero if and only if every clause in evaluates to true under . Satisfiability means that some assignment has zero loss. The distance is the infimum in the natural numbers of the losses over all assignments, hence the minimum since a constant assignment exists for every . The empty list has distance zero and is satisfiable, including when is empty.