✅ SAT Solver

Boolean satisfiability via the DPLL algorithm

Overview

A SAT solver determines whether a Boolean formula in conjunctive normal form (CNF) has a satisfying assignment. This is the canonical NP-complete problem — the first problem proven NP-complete (Cook–Levin, 1971). Despite worst-case exponential time, modern SAT solvers handle millions of variables using clever heuristics.

This implementation uses the DPLL (Davis–Putnam–Logemann–Loveland) algorithm with unit propagation, pure literal elimination, and backtracking. Includes support for DIMACS CNF parsing and solution verification.

Concepts Demonstrated

The DPLL Algorithm

(* CNF formula: (a ∨ ¬b) ∧ (b ∨ c) ∧ (¬a ∨ ¬c) *)
let formula = [
  [Pos 1; Neg 2];       (* a ∨ ¬b *)
  [Pos 2; Pos 3];       (* b ∨ c  *)
  [Neg 1; Neg 3];       (* ¬a ∨ ¬c *)
]

(* DPLL proceeds by:
   1. Unit propagation — if a clause has one unset literal, force it
   2. Pure literal elim — if a var appears only positive (or only
      negative), assign it to satisfy all those clauses
   3. Choose — pick an unassigned variable, try true then false
   4. Backtrack — if a conflict is found, undo and try the other branch
*)

match solve formula with
| Some assignment -> (* satisfying assignment found *)
| None            -> (* formula is unsatisfiable *)

Key Functions

FunctionDescription
solve formulaFind satisfying assignment or return None
unit_propagateFind and propagate unit clauses (single-literal)
pure_literal_elimAssign variables appearing in only one polarity
simplifyRemove satisfied clauses and falsified literals
variablesCollect all variables in a formula
eval_clauseEvaluate clause under partial assignment
eval_formulaEvaluate entire formula under assignment
parse_dimacsParse standard DIMACS CNF format
verifyVerify a solution against the original formula

DPLL Optimizations

TechniqueEffect
Unit propagationForced deductions — dramatically prunes the search tree
Pure literal eliminationVariables that only appear positive (or negative) can be set freely
Early terminationStop as soon as a clause is falsified (no need to check the rest)
Immutable assignmentsBacktracking is free — just discard the current map

Applications of SAT Solving

CNF Format

(* DIMACS CNF — standard interchange format *)
(* p cnf <num_vars> <num_clauses> *)
(* Each line: space-separated literals, 0-terminated *)
(* Positive int = positive literal, negative = negation *)

p cnf 3 3
1 -2 0       (* a ∨ ¬b *)
2 3 0        (* b ∨ c  *)
-1 -3 0      (* ¬a ∨ ¬c *)

(* Parse with: parse_dimacs input_string *)