SAT engine in Rust based on CDCL (Conflict-Driven Clause Learning) with the Two-Watched Literals (2WL) scheme for efficient propagation.
The crate exposes a lightweight, reusable solver core designed to be embedded in planning, verification, or reasoning systems.
- CDCL with 1-UIP-style conflict analysis
- Boolean propagation with two watched literals per clause
- Simple API for variables, literals, and clauses
- Variable-assignment callbacks via listeners
- Safe Rust implementation with unit tests
In Cargo.toml:
[dependencies]
watchsat = "0.1"Or from a local source checkout:
[dependencies]
watchsat = { path = "../WatchSAT" }use watchsat::{Engine, LBool, neg, pos};
fn main() {
let mut engine = Engine::new();
let a = engine.add_var();
let b = engine.add_var();
// (not a or b)
engine.add_clause(vec![neg(a), pos(b)]).unwrap();
// Decide a = true, then b is propagated to true.
engine.assert(pos(a)).unwrap();
assert_eq!(engine.value(b), LBool::True);
}VarId: variable identifier created byEngine::add_varLit: literal (variable + sign)LBool: tri-state value (True,False,Undef)Engine: solver state (assignments, watch lists, clauses)
Important: variable index 0 and literals TRUE_LIT/FALSE_LIT are reserved internally by the solver.
Engine::new(): create an empty engineEngine::add_var(): add a variableEngine::add_clause(...): insert a clauseEngine::assert(lit): make a decision and propagateEngine::value(var): read the current value of a variableEngine::lit_value(lit): read the current value of a literalEngine::add_listener(var, callback): register assignment callback
Insertion or propagation operations can return:
PropagationError::Conflict { clause }
The conflict clause is a learned clause produced by 1-UIP analysis.
cargo test
cargo clippy --all-targets --all-features