Writing a SAT solver as a hobby project

Masahiro Sakai
Masahiro SakaiSoftware Engineer at Preferred Networks, Inc.
Thursday Seminar 2017-10-12
Masahiro Sakai (酒井 政裕)
Writing a SAT solver
as 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 satisfy
a 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 φ
Counter
example
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, and
got 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
– Gröbner 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.
SAT
Solver
Arithmetic
Solver:
+, ×, ≤
BitVector
Solver
Uninterpreted
Function Solver:
f, g, =
Array Solver
read, 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 0
45 -218 38 0
191 -129 -88 0
117 -164 -29 0
107 53 115 0
167 111 -57 0
-115 94 98 0
25 -51 -165 0
247 31 -64 0
156 228 11 0
64 199 -162 0
1 173 -54 0
$ toysat UF250.1065.100/uf250-01.cnf
……
c #vars 250
c #constraints 1065
c Solving starts ...
c ============================[ Search Statistics ]================
c Time | Restart | Decision | Conflict | LEARNT | Fixed | Removed
c | | | | Limit GC | Var | Constra
c ==========================================================
c 0.0s | 0 | 0 | 0 | 438 0 | 0 | 0
……
c 4.0s | 11 | 23520 | 19665 | 1259 37 | 0 | 0
c #cpu_time = 3.872s
c #wall_clock_time = 3.957s
c #decision = 23520
c #random_decision = 105
c #conflict = 19665
c #restart = 11
s SATISFIABLE
v -1 -2 3 -4 5 -6 7 -8 9 -10
v -11 -12 -13 14 15 16 17 -18 19 20
v 21 22 -23 -24 25 26 -27 28 29 -30
v 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.smt2
success
…
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 2016
QF_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?
1 of 31

Recommended

Template Meta Programming入門から応用まで by
Template Meta Programming入門から応用までTemplate Meta Programming入門から応用まで
Template Meta Programming入門から応用までyoshihikoozaki5
12.2K views417 slides
AtCoder Beginner Contest 028 解説 by
AtCoder Beginner Contest 028 解説AtCoder Beginner Contest 028 解説
AtCoder Beginner Contest 028 解説AtCoder Inc.
9.5K views32 slides
Coqチュートリアル by
CoqチュートリアルCoqチュートリアル
CoqチュートリアルYoshihiro Mizoguchi
3.1K views36 slides
AtCoder Beginner Contest 007 解説 by
AtCoder Beginner Contest 007 解説AtCoder Beginner Contest 007 解説
AtCoder Beginner Contest 007 解説AtCoder Inc.
16.1K views31 slides
関数プログラミング入門 by
関数プログラミング入門関数プログラミング入門
関数プログラミング入門Hideyuki Tanaka
21.8K views101 slides
AtCoder Beginner Contest 014 解説 by
AtCoder Beginner Contest 014 解説AtCoder Beginner Contest 014 解説
AtCoder Beginner Contest 014 解説AtCoder Inc.
14.3K views11 slides

More Related Content

What's hot

AtCoder Regular Contest 030 解説 by
AtCoder Regular Contest 030 解説AtCoder Regular Contest 030 解説
AtCoder Regular Contest 030 解説AtCoder Inc.
9.9K views25 slides
AtCoder Regular Contest 017 by
AtCoder Regular Contest 017AtCoder Regular Contest 017
AtCoder Regular Contest 017AtCoder Inc.
6.5K views32 slides
AtCoder Beginner Contest 020 解説 by
AtCoder Beginner Contest 020 解説AtCoder Beginner Contest 020 解説
AtCoder Beginner Contest 020 解説AtCoder Inc.
11.9K views32 slides
圏論のモナドとHaskellのモナド by
圏論のモナドとHaskellのモナド圏論のモナドとHaskellのモナド
圏論のモナドとHaskellのモナドYoshihiro Mizoguchi
10.6K views24 slides
AtCoder Regular Contest 040 解説 by
AtCoder Regular Contest 040 解説AtCoder Regular Contest 040 解説
AtCoder Regular Contest 040 解説AtCoder Inc.
6.2K views25 slides
定理証明支援系Coqについて by
定理証明支援系Coqについて定理証明支援系Coqについて
定理証明支援系CoqについてYoshihiro Mizoguchi
8K views45 slides

Viewers also liked

自然言語をラムダ式で解釈する体系PTQのHaskell実装 by
自然言語をラムダ式で解釈する体系PTQのHaskell実装自然言語をラムダ式で解釈する体系PTQのHaskell実装
自然言語をラムダ式で解釈する体系PTQのHaskell実装Masahiro Sakai
2.4K views17 slides
ゼロピッチ: MOOC by
ゼロピッチ: MOOCゼロピッチ: MOOC
ゼロピッチ: MOOCMasahiro Sakai
1.1K views8 slides
SAT/SMT solving in Haskell by
SAT/SMT solving in HaskellSAT/SMT solving in Haskell
SAT/SMT solving in HaskellMasahiro Sakai
3.4K views32 slides
Run-time Code Generation and Modal-ML の紹介@PLDIr#2 by
Run-time Code Generation and Modal-ML の紹介@PLDIr#2Run-time Code Generation and Modal-ML の紹介@PLDIr#2
Run-time Code Generation and Modal-ML の紹介@PLDIr#2Masahiro Sakai
997 views27 slides
RClassify: Classifying Race Conditions in Web Applications via Deterministic ... by
RClassify: Classifying Race Conditions in Web Applications via Deterministic ...RClassify: Classifying Race Conditions in Web Applications via Deterministic ...
RClassify: Classifying Race Conditions in Web Applications via Deterministic ...Masahiro Sakai
794 views5 slides
自動定理証明の紹介 by
自動定理証明の紹介自動定理証明の紹介
自動定理証明の紹介Masahiro Sakai
8.3K views23 slides

Viewers also liked(8)

自然言語をラムダ式で解釈する体系PTQのHaskell実装 by Masahiro Sakai
自然言語をラムダ式で解釈する体系PTQのHaskell実装自然言語をラムダ式で解釈する体系PTQのHaskell実装
自然言語をラムダ式で解釈する体系PTQのHaskell実装
Masahiro Sakai2.4K views
SAT/SMT solving in Haskell by Masahiro Sakai
SAT/SMT solving in HaskellSAT/SMT solving in Haskell
SAT/SMT solving in Haskell
Masahiro Sakai3.4K views
Run-time Code Generation and Modal-ML の紹介@PLDIr#2 by Masahiro Sakai
Run-time Code Generation and Modal-ML の紹介@PLDIr#2Run-time Code Generation and Modal-ML の紹介@PLDIr#2
Run-time Code Generation and Modal-ML の紹介@PLDIr#2
Masahiro Sakai997 views
RClassify: Classifying Race Conditions in Web Applications via Deterministic ... by Masahiro Sakai
RClassify: Classifying Race Conditions in Web Applications via Deterministic ...RClassify: Classifying Race Conditions in Web Applications via Deterministic ...
RClassify: Classifying Race Conditions in Web Applications via Deterministic ...
Masahiro Sakai794 views
自動定理証明の紹介 by Masahiro Sakai
自動定理証明の紹介自動定理証明の紹介
自動定理証明の紹介
Masahiro Sakai8.3K views
ディープラーニングの産業応用とそれを支える技術 by Shohei Hido
ディープラーニングの産業応用とそれを支える技術ディープラーニングの産業応用とそれを支える技術
ディープラーニングの産業応用とそれを支える技術
Shohei Hido8.2K views
機械学習モデルフォーマットの話:さようならPMML、こんにちはPFA by Shohei Hido
機械学習モデルフォーマットの話:さようならPMML、こんにちはPFA機械学習モデルフォーマットの話:さようならPMML、こんにちはPFA
機械学習モデルフォーマットの話:さようならPMML、こんにちはPFA
Shohei Hido17.2K views

Similar to Writing a SAT solver as a hobby project

Introduction to Max-SAT and Max-SAT Evaluation by
Introduction to Max-SAT and Max-SAT EvaluationIntroduction to Max-SAT and Max-SAT Evaluation
Introduction to Max-SAT and Max-SAT EvaluationMasahiro Sakai
5.4K views42 slides
Using R in remote computer clusters by
Using R in remote computer clustersUsing R in remote computer clusters
Using R in remote computer clustersBurak Himmetoglu
689 views19 slides
Computability and Complexity by
Computability and ComplexityComputability and Complexity
Computability and ComplexityEdward Blurock
2.2K views104 slides
Lecture 1 (bce-7) by
Lecture   1 (bce-7)Lecture   1 (bce-7)
Lecture 1 (bce-7)farazahmad005
456 views55 slides
Teaching Constraint Programming, Patrick Prosser by
Teaching Constraint Programming,  Patrick ProsserTeaching Constraint Programming,  Patrick Prosser
Teaching Constraint Programming, Patrick ProsserPierre Schaus
1.2K views97 slides
Algo_Lecture01.pptx by
Algo_Lecture01.pptxAlgo_Lecture01.pptx
Algo_Lecture01.pptxShaistaRiaz4
1 view73 slides

Similar to Writing a SAT solver as a hobby project(20)

Introduction to Max-SAT and Max-SAT Evaluation by Masahiro Sakai
Introduction to Max-SAT and Max-SAT EvaluationIntroduction to Max-SAT and Max-SAT Evaluation
Introduction to Max-SAT and Max-SAT Evaluation
Masahiro Sakai5.4K views
Computability and Complexity by Edward Blurock
Computability and ComplexityComputability and Complexity
Computability and Complexity
Edward Blurock2.2K views
Teaching Constraint Programming, Patrick Prosser by Pierre Schaus
Teaching Constraint Programming,  Patrick ProsserTeaching Constraint Programming,  Patrick Prosser
Teaching Constraint Programming, Patrick Prosser
Pierre Schaus1.2K views
DAA ppt.pptx by RAJESH S
DAA ppt.pptxDAA ppt.pptx
DAA ppt.pptx
RAJESH S18 views
DAA 1 ppt.pptx by RAJESH S
DAA 1 ppt.pptxDAA 1 ppt.pptx
DAA 1 ppt.pptx
RAJESH S14 views
Class[1][23ed may] [algorithms] by Saajid Akram
Class[1][23ed may] [algorithms]Class[1][23ed may] [algorithms]
Class[1][23ed may] [algorithms]
Saajid Akram55 views
lec_4_data_structures_and_algorithm_analysis.ppt by SourabhPal46
lec_4_data_structures_and_algorithm_analysis.pptlec_4_data_structures_and_algorithm_analysis.ppt
lec_4_data_structures_and_algorithm_analysis.ppt
SourabhPal462 views
lec_4_data_structures_and_algorithm_analysis.ppt by Mard Geer
lec_4_data_structures_and_algorithm_analysis.pptlec_4_data_structures_and_algorithm_analysis.ppt
lec_4_data_structures_and_algorithm_analysis.ppt
Mard Geer9 views
MLlecture1.ppt by butest
MLlecture1.pptMLlecture1.ppt
MLlecture1.ppt
butest424 views
MLlecture1.ppt by butest
MLlecture1.pptMLlecture1.ppt
MLlecture1.ppt
butest310 views
Algorithms & Complexity Calculation by Akhil Kaushik
Algorithms & Complexity CalculationAlgorithms & Complexity Calculation
Algorithms & Complexity Calculation
Akhil Kaushik103 views

More from Masahiro Sakai

DeepXplore: Automated Whitebox Testing of Deep Learning by
DeepXplore: Automated Whitebox Testing of Deep LearningDeepXplore: Automated Whitebox Testing of Deep Learning
DeepXplore: Automated Whitebox Testing of Deep LearningMasahiro Sakai
1.6K views25 slides
Towards formal verification of neural networks by
Towards formal verification of neural networksTowards formal verification of neural networks
Towards formal verification of neural networksMasahiro Sakai
660 views32 slides
関数プログラマから見たPythonと機械学習 by
関数プログラマから見たPythonと機械学習関数プログラマから見たPythonと機械学習
関数プログラマから見たPythonと機械学習Masahiro Sakai
31.4K views29 slides
SAT/SMTソルバの仕組み by
SAT/SMTソルバの仕組みSAT/SMTソルバの仕組み
SAT/SMTソルバの仕組みMasahiro Sakai
36.7K views51 slides
Aluminum: Principled Scenario Exploration through Minimality by
Aluminum: Principled Scenario Exploration through MinimalityAluminum: Principled Scenario Exploration through Minimality
Aluminum: Principled Scenario Exploration through MinimalityMasahiro Sakai
1.2K views7 slides
代数的実数とCADの実装紹介 by
代数的実数とCADの実装紹介代数的実数とCADの実装紹介
代数的実数とCADの実装紹介Masahiro Sakai
3.8K views71 slides

More from Masahiro Sakai(20)

DeepXplore: Automated Whitebox Testing of Deep Learning by Masahiro Sakai
DeepXplore: Automated Whitebox Testing of Deep LearningDeepXplore: Automated Whitebox Testing of Deep Learning
DeepXplore: Automated Whitebox Testing of Deep Learning
Masahiro Sakai1.6K views
Towards formal verification of neural networks by Masahiro Sakai
Towards formal verification of neural networksTowards formal verification of neural networks
Towards formal verification of neural networks
Masahiro Sakai660 views
関数プログラマから見たPythonと機械学習 by Masahiro Sakai
関数プログラマから見たPythonと機械学習関数プログラマから見たPythonと機械学習
関数プログラマから見たPythonと機械学習
Masahiro Sakai31.4K views
SAT/SMTソルバの仕組み by Masahiro Sakai
SAT/SMTソルバの仕組みSAT/SMTソルバの仕組み
SAT/SMTソルバの仕組み
Masahiro Sakai36.7K views
Aluminum: Principled Scenario Exploration through Minimality by Masahiro Sakai
Aluminum: Principled Scenario Exploration through MinimalityAluminum: Principled Scenario Exploration through Minimality
Aluminum: Principled Scenario Exploration through Minimality
Masahiro Sakai1.2K views
代数的実数とCADの実装紹介 by Masahiro Sakai
代数的実数とCADの実装紹介代数的実数とCADの実装紹介
代数的実数とCADの実装紹介
Masahiro Sakai3.8K views
How a CDCL SAT solver works by Masahiro Sakai
How a CDCL SAT solver worksHow a CDCL SAT solver works
How a CDCL SAT solver works
Masahiro Sakai25.2K views
萩野服部研究室 スキー合宿 2012 自己紹介(酒井) by Masahiro Sakai
萩野服部研究室 スキー合宿 2012 自己紹介(酒井)萩野服部研究室 スキー合宿 2012 自己紹介(酒井)
萩野服部研究室 スキー合宿 2012 自己紹介(酒井)
Masahiro Sakai1K views
“Adoption and Focus: Practical Linear Types for Imperative Programming”他の紹介@P... by Masahiro Sakai
“Adoption and Focus: Practical Linear Types for Imperative Programming”他の紹介@P...“Adoption and Focus: Practical Linear Types for Imperative Programming”他の紹介@P...
“Adoption and Focus: Practical Linear Types for Imperative Programming”他の紹介@P...
Masahiro Sakai839 views
“Design and Implementation of Generics for the .NET Common Language Runtime”他... by Masahiro Sakai
“Design and Implementation of Generics for the .NET Common Language Runtime”他...“Design and Implementation of Generics for the .NET Common Language Runtime”他...
“Design and Implementation of Generics for the .NET Common Language Runtime”他...
Masahiro Sakai907 views
“Symbolic bounds analysis of pointers, array indices, and accessed memory reg... by Masahiro Sakai
“Symbolic bounds analysis of pointers, array indices, and accessed memory reg...“Symbolic bounds analysis of pointers, array indices, and accessed memory reg...
“Symbolic bounds analysis of pointers, array indices, and accessed memory reg...
Masahiro Sakai1.3K views
Whole Program Paths 等の紹介@PLDIr#3 by Masahiro Sakai
Whole Program Paths 等の紹介@PLDIr#3Whole Program Paths 等の紹介@PLDIr#3
Whole Program Paths 等の紹介@PLDIr#3
Masahiro Sakai1.4K views
Introduction to Categorical Programming (Revised) by Masahiro Sakai
Introduction to Categorical Programming (Revised)Introduction to Categorical Programming (Revised)
Introduction to Categorical Programming (Revised)
Masahiro Sakai6.4K views
Introduction to Categorical Programming by Masahiro Sakai
Introduction to Categorical ProgrammingIntroduction to Categorical Programming
Introduction to Categorical Programming
Masahiro Sakai1.7K views
融合変換による最適化の理論的基盤と正当性 (2006-06-27) by Masahiro Sakai
融合変換による最適化の理論的基盤と正当性 (2006-06-27)融合変換による最適化の理論的基盤と正当性 (2006-06-27)
融合変換による最適化の理論的基盤と正当性 (2006-06-27)
Masahiro Sakai2K views
融合変換による最適化の理論的基盤と正当性 (2006-06-20) by Masahiro Sakai
融合変換による最適化の理論的基盤と正当性 (2006-06-20)融合変換による最適化の理論的基盤と正当性 (2006-06-20)
融合変換による最適化の理論的基盤と正当性 (2006-06-20)
Masahiro Sakai919 views
Ruby-GNOME2におけるGC問題 by Masahiro Sakai
Ruby-GNOME2におけるGC問題Ruby-GNOME2におけるGC問題
Ruby-GNOME2におけるGC問題
Masahiro Sakai817 views
LLW2004 その場でどう書く - Haskell by Masahiro Sakai
LLW2004 その場でどう書く - HaskellLLW2004 その場でどう書く - Haskell
LLW2004 その場でどう書く - Haskell
Masahiro Sakai454 views

Recently uploaded

CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T by
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&TCloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&TShapeBlue
38 views34 slides
Five Things You SHOULD Know About Postman by
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About PostmanPostman
38 views43 slides
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue by
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlueElevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlueShapeBlue
70 views7 slides
Network Source of Truth and Infrastructure as Code revisited by
Network Source of Truth and Infrastructure as Code revisitedNetwork Source of Truth and Infrastructure as Code revisited
Network Source of Truth and Infrastructure as Code revisitedNetwork Automation Forum
32 views45 slides
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R... by
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...ShapeBlue
37 views15 slides
Ransomware is Knocking your Door_Final.pdf by
Ransomware is Knocking your Door_Final.pdfRansomware is Knocking your Door_Final.pdf
Ransomware is Knocking your Door_Final.pdfSecurity Bootcamp
66 views46 slides

Recently uploaded(20)

CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T by ShapeBlue
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&TCloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T
CloudStack and GitOps at Enterprise Scale - Alex Dometrius, Rene Glover - AT&T
ShapeBlue38 views
Five Things You SHOULD Know About Postman by Postman
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About Postman
Postman38 views
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue by ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlueElevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
Elevating Privacy and Security in CloudStack - Boris Stoyanov - ShapeBlue
ShapeBlue70 views
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R... by ShapeBlue
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...
Setting Up Your First CloudStack Environment with Beginners Challenges - MD R...
ShapeBlue37 views
DRBD Deep Dive - Philipp Reisner - LINBIT by ShapeBlue
DRBD Deep Dive - Philipp Reisner - LINBITDRBD Deep Dive - Philipp Reisner - LINBIT
DRBD Deep Dive - Philipp Reisner - LINBIT
ShapeBlue44 views
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue by ShapeBlue
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlueWhat’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue
What’s New in CloudStack 4.19 - Abhishek Kumar - ShapeBlue
ShapeBlue89 views
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti... by ShapeBlue
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...
DRaaS using Snapshot copy and destination selection (DRaaS) - Alexandre Matti...
ShapeBlue26 views
KVM Security Groups Under the Hood - Wido den Hollander - Your.Online by ShapeBlue
KVM Security Groups Under the Hood - Wido den Hollander - Your.OnlineKVM Security Groups Under the Hood - Wido den Hollander - Your.Online
KVM Security Groups Under the Hood - Wido den Hollander - Your.Online
ShapeBlue75 views
Keynote Talk: Open Source is Not Dead - Charles Schulz - Vates by ShapeBlue
Keynote Talk: Open Source is Not Dead - Charles Schulz - VatesKeynote Talk: Open Source is Not Dead - Charles Schulz - Vates
Keynote Talk: Open Source is Not Dead - Charles Schulz - Vates
ShapeBlue84 views
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT by ShapeBlue
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBITUpdates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
Updates on the LINSTOR Driver for CloudStack - Rene Peinthor - LINBIT
ShapeBlue66 views
"Surviving highload with Node.js", Andrii Shumada by Fwdays
"Surviving highload with Node.js", Andrii Shumada "Surviving highload with Node.js", Andrii Shumada
"Surviving highload with Node.js", Andrii Shumada
Fwdays33 views
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue by ShapeBlue
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlueCloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue
CloudStack Object Storage - An Introduction - Vladimir Petrov - ShapeBlue
ShapeBlue26 views
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit... by ShapeBlue
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...
Transitioning from VMware vCloud to Apache CloudStack: A Path to Profitabilit...
ShapeBlue40 views
HTTP headers that make your website go faster - devs.gent November 2023 by Thijs Feryn
HTTP headers that make your website go faster - devs.gent November 2023HTTP headers that make your website go faster - devs.gent November 2023
HTTP headers that make your website go faster - devs.gent November 2023
Thijs Feryn26 views
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De... by Moses Kemibaro
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...
Don’t Make A Human Do A Robot’s Job! : 6 Reasons Why AI Will Save Us & Not De...
Moses Kemibaro27 views
NTGapps NTG LowCode Platform by Mustafa Kuğu
NTGapps NTG LowCode Platform NTGapps NTG LowCode Platform
NTGapps NTG LowCode Platform
Mustafa Kuğu28 views

Writing a SAT solver as a hobby project

  • 1. Thursday Seminar 2017-10-12 Masahiro Sakai (酒井 政裕) Writing a SAT solver as a hobby project
  • 2. Outline • Decision procedures and Me • My hobby project • About SAT and SMT • Some results • Conclusion
  • 3. 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!)
  • 4. 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 satisfy a 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 φ Counter example
  • 5. 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, and got interested in their algorithms and implementations • Therefore I started writing toy level implementations …
  • 7. 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
  • 8. Some of problems and algorithms • Presburger Arithmetic – Omega Test – Cooper’s Algorithm • Real Arithmetic – Fourier-Motzkin variable elimination – Simplex method – Gröbner 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.
  • 11. 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?”
  • 12. Why I’m writing it? • Developing a state-of-the-art implementation • Learning algorithms • Familiarizing other libraries and tools • Handy implementation for my self
  • 13. 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
  • 14. 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.
  • 15. 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
  • 17. 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
  • 18. 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
  • 19. 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)”?
  • 20. SMT = SAT solver + Theory solvers • SAT solver is responsible for Boolean reasoning • Theory solvers are responsible for handling specific functions/relations etc. SAT Solver Arithmetic Solver: +, ×, ≤ BitVector Solver Uninterpreted Function Solver: f, g, = Array Solver read, write …
  • 21. 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
  • 22. My SAT solver: toysat p cnf 250 1065 -159 -234 197 0 -71 13 194 0 45 -218 38 0 191 -129 -88 0 117 -164 -29 0 107 53 115 0 167 111 -57 0 -115 94 98 0 25 -51 -165 0 247 31 -64 0 156 228 11 0 64 199 -162 0 1 173 -54 0 $ toysat UF250.1065.100/uf250-01.cnf …… c #vars 250 c #constraints 1065 c Solving starts ... c ============================[ Search Statistics ]================ c Time | Restart | Decision | Conflict | LEARNT | Fixed | Removed c | | | | Limit GC | Var | Constra c ========================================================== c 0.0s | 0 | 0 | 0 | 438 0 | 0 | 0 …… c 4.0s | 11 | 23520 | 19665 | 1259 37 | 0 | 0 c #cpu_time = 3.872s c #wall_clock_time = 3.957s c #decision = 23520 c #random_decision = 105 c #conflict = 19665 c #restart = 11 s SATISFIABLE v -1 -2 3 -4 5 -6 7 -8 9 -10 v -11 -12 -13 14 15 16 17 -18 19 20 v 21 22 -23 -24 25 26 -27 28 29 -30 v 31 32 -33 34 -35 36 37 -38 39 -40 … ¬x159∨¬x234∨x197
  • 23. 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.smt2 success … 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)))
  • 25. Some results? • Several talks • Solver competitions – Pseudo Boolean Competition 2012 – Max-SAT Evaluation 2013 – SMT-COMP 2016 – … • How was writing SAT solver in Haskell?
  • 26. I have had talks based on what I learned
  • 27. 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
  • 28. SMT-COMP 2016 QF_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!)
  • 29. 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.
  • 30. 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
  • 31. Thank you! And any questions or comments?