writing a sat solver as a hobby project
TRANSCRIPT
Thursday Seminar 2017-10-12Masahiro Sakai (酒井政裕)
Writing a SAT solveras a hobby project
Outline
• Decision procedures and Me• My hobby project• About SAT and SMT• Some results• Conclusion
About me
• Masahiro Sakai (酒井政裕)• joined PFN this May
as an Engineer• My technical interest includes:
– Functional Programming (esp. Haskell)– Program Verification and Decision Procedures for it– and Machine Learning (of course!)
Decision procedures• Decision problem: yes/no answer problems
– e.g. Is intersection of given two regular language empty?• Model Checking
– Does a finite state system M satisfya temporal logic specification φ ?
– it is just a language emptiness of M ∩ ¬φ,if we represent both M and φ as automaton
– and can be decided using automata algorithm• Likewise, many program verification task can be reduced to
simple decision problems,• Fast decision procedures have accelerated program verification.
All possible behavior of M
Behaviors that violate φ
Counterexample
Decision procedures and Me
• When I was a grad student, ~10 years ago, I listened to talks about model checking tools.
• Later, I was working on program verification,using decision procedures as off-the-shelf tools
• I was impressed by their strength and usefulness, andgot interested in their algorithms and implementations
• Therefore I started writing toy level implementations …
My hobby project
My hobby project : toysolver
• Several years ago– I started writing some code snippet just to
understand those algorithms• Over the years,
– I have added more and more algorithms,and the codebase grew
• Now– it contains not a few algorithms and it’s ~30 kloc
Some of problems and algorithms
• Presburger Arithmetic– Omega Test– Cooper’s Algorithm
• Real Arithmetic– Fourier-Motzkin variable
elimination – Simplex method – Grob̈ner basis
(Buchberger) – Quantifier Elimination
• (Mixed) Integer Programming– Branch-and-bound– Cutting plane (Gomory’s Cut) – Conti-Traverso
• SAT / MaxSAT / Pseudo Boolean– DPLL / CDCL
• SMT– Uninterpreted function
(Congruence Closure)– Bitvectors
Most of them are toy-level, but the SAT solver is modestly fast.
https://github.com/msakai/toysolver http://hackage.haskell.org/package/toysolver
It’s available online
Wait !?
http://hackage.haskell.org/package/toysolver
……
Why Haskell?
1. Because it’s the language I used most, and it’s handy for me if usable from Haskell easily
2. It’s suitable for symbolic computation like formula manipulation.3. I have two questions:
– Haskell has a good compiler and runtime suitable for usual application domain, But…
– “How about computation intensive application like SAT?”– “Is good functional interface possible?”
Why I’m writing it?
• Developing a state-of-the-art implementation• Learning algorithms• Familiarizing other libraries and tools• Handy implementation for my self
Reason: Learning algorithms
• Learning cleaver algorithm is itself fun• Implementation is the best way to understand it
• In addition to that,• the knowledge of algorithms was useful when I use
other (state-of-the-art) implementation as a off-the-shelf tool
Reason: Familiarizing other libraries and tools
• Writing and maintaining such project is a good opportunity to try new libraries, frameworks and services
• For example,– I have learned several Haskell libraries,– and also integration of GitHub, Traivs CI, AppVeyor,
Coveralls.io, etc.
Reason: Handy implementation for myself
• Having handy implementation for myself is useful for– experimenting new algorithms, – solving puzzles (e.g. Sudoku, Number Link, etc.)– using in a programming competition
• In particular, there are less such library available for Haskell
About SAT and SMT
What is SAT?
• SAT = Boolean SATisfiability problem– “Is there an assignment that makes given formula true?”
• Examples:– (P∨Q)∧(P∨¬Q)∧(¬P∨¬Q) is satisfiable with
{P ↦ True, Q ↦ False}– (P∨Q)∧(P∨¬Q)∧(¬P∨¬Q)∧(¬P∨Q) is unsatisfiable
• SAT is NP complete, but state-of-the-art SAT-solver can often solve problems with millions of variables / constraints.
• Has many applications
Some Applications of SAT (and SMT)
• Software/Hardware verification– Model checking, Test-case generation, …
• Theorem proving• Puzzles: Sudoku, Number link, Nonogram, etc.• Program Synthesis• and more
What is SMT?
• Weakness of SAT: Really low-level representation– Encoding problems into SAT sometimes blows-up– SAT solver cannot leverage high-level knowledge
• SMT = Satisfiability Modulo Theories– An approach to overcome the weakness of SAT– Problem Example:
Is there array a, function f, integers i, j such that“0 ≤ i∧ i < 10 ∧ (2i+1=j ∨ read(a,i)=0) ∧f(read(write(a,i,3), j-2)) ≠ f(j-i+1)”?
SMT = SAT solver + Theory solvers
• SAT solver is responsible for Boolean reasoning• Theory solvers are responsible for handling specific
functions/relations etc.
SATSolver
Arithmetic Solver:+, ×, ≤
BitVector Solver
Uninterpreted Function Solver:
f, g, =Array Solverread, write
…
What is interesting about SAT and SMT? (for me)
• Theory of mathematical logic is connected to practical application
• For example,– In SAT, its CDCL algorithm can be seen as doing
concurrently • search satisfying assignment (model theoretic)• constraint propagation and conflict learning (proof
theoretic deduction)– In SMT, theory combination closely related to Craig
interpolation theorem
My SAT solver: toysat
p cnf 250 1065-159 -234 197 0-71 13 194 045 -218 38 0191 -129 -88 0117 -164 -29 0107 53 115 0167 111 -57 0-115 94 98 025 -51 -165 0247 31 -64 0156 228 11 064 199 -162 01 173 -54 0136 -98 -215 0
$ toysat UF250.1065.100/uf250-01.cnf……c #vars 250c #constraints 1065c Solving starts ...c ============================[ Search Statistics ]============================c Time | Restart | Decision | Conflict | LEARNT | Fixed | Removedc | | | | Limit GC | Var | Constrac =============================================================================c 0.0s | 0 | 0 | 0 | 438 0 | 0 | 0……c 4.0s | 11 | 23520 | 19665 | 1259 37 | 0 | 0c #cpu_time = 3.872sc #wall_clock_time = 3.957sc #decision = 23520c #random_decision = 105c #conflict = 19665c #restart = 11s SATISFIABLEv -1 -2 3 -4 5 -6 7 -8 9 -10v -11 -12 -13 14 15 16 17 -18 19 20v 21 22 -23 -24 25 26 -27 28 29 -30v 31 32 -33 34 -35 36 37 -38 39 -40…
¬x159∨¬x234∨x197
My SMT solver: toysmt
(set-option :produce-models true)(set-logic QF_UFLRA)(declare-sort U 0)(declare-fun x () Real)(declare-fun f (U) Real)(declare-fun P (U) Bool)(declare-fun g (U) U)(declare-fun c () U)(declare-fun d () U)(assert (= (P c) (= (g c) c)))(assert (ite (P c) (> x (f d)) (< x (f d))))(check-sat)(get-model)(exit)
$ toysmt QF_UFLRA.smt2success…sat((define-fun P ((x!1 U)) Bool
(ite (= x!1 (as @3 U)) true false))(define-fun c () U (as @3 U))(define-fun d () U (as @4 U))(define-fun f ((x!1 U)) Real
(ite (= x!1 (as @4 U)) 0 (/ 555555 1)))(define-fun g ((x!1 U)) U
(ite (= x!1 (as @3 U)) (as @3 U) (as @-1 U)))(define-fun x () Real (/ 1 10)))
Some results?
Some results?
• Several talks• Solver competitions
– Pseudo Boolean Competition 2012– Max-SAT Evaluation 2013– SMT-COMP 2016– …
• How was writing SAT solver in Haskell?
I have had talks based on what I learned
Pseudo Boolean Competition 2012 (PB'12) competition result:
• Ranked high in some minor divisions:– placed 2nd in PARTIAL-BIGINT-LIN and SOFT-
BIGINT-LIN categories– placed 4th in PARTIAL-SMALLINT-LIN and SOFT-
SMALLINT-LIN categories– placed 8th in OPT-BIGINT-LIN category
SMT-COMP 2016QF_LRA (Main Track) http://smtcomp.sourceforge.net/2016/results-QF_LRA.shtml?v=1467876482
‘toysmt’ ended up dead last.But without wrong results! (Thanks to QuickCheck!)
How was writing SAT solver in Haskell?
• How about writing computation intensive application like SAT?– Not so bad, but with some frustration:– e.g. Haskell allocate memory like breathing, but I want to
avoid memory allocation in tight loop. It’s possible if we write carefully, but …
• Is good functional interface possible?– Not tried yet. My SAT solver is based on imperative API
and imperative implementation.
Conclusion
• As a hobby project, I have been writing toy-level implementation of various algorithms
• In particular, algorithms for SAT and related problems• It’s mainly for fun, but I have gained good experience
from the activity
Thank you!And any questions or comments?