SlideShare a Scribd company logo
SAT/SMT solving in

Haskell
Masahiro Sakai (酒井 政裕)

Haskell Day 2016
2016-09-17
Self Introduction

Masahiro Sakai
Twitter: @masahiro_sakai
github: https://github.com/msakai/
G+: https://plus.google.com/+MasahiroSakai
Translated “Software Abstractions”
and TaPL into Japanese with colleagues
Interests: Categorical Programming,
Theorem Proving / Decision Procedures,

…
Agenda
What are SAT and SMT?
Haskell libraries for SMT solving
sbv
toysat/toysmt
Conclusion
What are

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.
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 Solver Impl.

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
Uninterpre
ted Function

Solver

f, g, =
Array

Solver

read, write


・・・
Some Applications

of SAT/SMT
Software/Hardware verification
Model checking, Test-case generation, …
Theorem proving
Puzzles: Sudoku, Numberlink, Nonogram, etc.
Type checking in Liquid Haskell
eg: doubles :: [{x : Int | x >= 0}]→[{x : Int | x `mod` 2 = 0}]
Program Synthesis
and more
Haskell libraries for
SMT solving
Some Haskell packages
for SMT
Binding
sbv, smtlib2, simple-smt
z3, bindings-yices, yices-easy, yices-painless
SMT solvers written in Haskell:
toysolver, Smooth
SMT-LIB2 file parser/printer
smt-lib, SmtLib
SMT-LIB2 is a standard

input/output format
for SMT solvers
SBV: SMT Based
Verification in Haskell
SMT library developed by Levent Erkok
It provides:
High-Level DSL for specifying problems in
Haskell, and
Interfaces to multiple SMT solver
backends including Z3, CVC4, Yices,
Boolector.
You can install simply using stack/cabal
“stack install sbv” or “cabal install sbv"
SBV Example: “send more money”

Data.SBV.Examples.Puzzles.SendMoreMoney module
sendMoreMoney :: IO SatResult
sendMoreMoney = sat $ do
ds@[s,e,n,d,m,o,r,y] <- mapM sInteger ["s", "e", "n", "d", "m", "o", "r", "y"]
let isDigit x = x .>= 0 &&& x .<= 9
val xs = sum $ zipWith (*) (reverse xs) (iterate (*10) 1)
send = val [s,e,n,d]
more = val [m,o,r,e]
money = val [m,o,n,e,y]
constrain $ bAll isDigit ds
constrain $ allDifferent ds
constrain $ s ./= 0 &&& m ./= 0
solve [send + more .== money]
SEND

+MORE
————

MONEY
SBV Example: “send more money”

Data.SBV.Examples.Puzzles.SendMoreMoney module
sendMoreMoney :: IO SatResult
sendMoreMoney = sat $ do
ds@[s,e,n,d,m,o,r,y] <- mapM sInteger ["s", "e", "n", "d", "m", "o", "r", "y"]
let isDigit x = x .>= 0 &&& x .<= 9
val xs = sum $ zipWith (*) (reverse xs) (iterate (*10) 1)
send = val [s,e,n,d]
more = val [m,o,r,e]
money = val [m,o,n,e,y]
constrain $ bAll isDigit ds
constrain $ allDifferent ds
constrain $ s ./= 0 &&& m ./= 0
solve [send + more .== money]
SMT problem is defined using Symbolic monad,
and SMT solving is performed by

sat :: Symbolic SBool → IO SatResult
SBV Example: “send more money”

Data.SBV.Examples.Puzzles.SendMoreMoney module
sendMoreMoney :: IO SatResult
sendMoreMoney = sat $ do
ds@[s,e,n,d,m,o,r,y] <- mapM sInteger ["s", "e", "n", "d", "m", "o", "r", "y"]
let isDigit x = x .>= 0 &&& x .<= 9
val xs = sum $ zipWith (*) (reverse xs) (iterate (*10) 1)
send = val [s,e,n,d]
more = val [m,o,r,e]
money = val [m,o,n,e,y]
constrain $ bAll isDigit ds
constrain $ allDifferent ds
constrain $ s ./= 0 &&& m ./= 0
solve [send + more .== money]
sInteger :: String → Symbolic SInteger

creates integer variable
SBV Example: “send more money”

Data.SBV.Examples.Puzzles.SendMoreMoney module
sendMoreMoney :: IO SatResult
sendMoreMoney = sat $ do
ds@[s,e,n,d,m,o,r,y] <- mapM sInteger ["s", "e", "n", "d", "m", "o", "r", "y"]
let isDigit x = x .>= 0 &&& x .<= 9
val xs = sum $ zipWith (*) (reverse xs) (iterate (*10) 1)
send = val [s,e,n,d]
more = val [m,o,r,e]
money = val [m,o,n,e,y]
constrain $ bAll isDigit ds
constrain $ allDifferent ds
constrain $ s ./= 0 &&& m ./= 0
solve [send + more .== money]
Comparison over symbolic values:
we have to use slightly difference operators like (.>=), (&&&).
Because Haskell’s (>=), (&&) returns Bool, but we want SBool.
SBV Example: “send more money”

Data.SBV.Examples.Puzzles.SendMoreMoney module
sendMoreMoney :: IO SatResult
sendMoreMoney = sat $ do
ds@[s,e,n,d,m,o,r,y] <- mapM sInteger ["s", "e", "n", "d", "m", "o", "r", "y"]
let isDigit x = x .>= 0 &&& x .<= 9
val xs = sum $ zipWith (*) (reverse xs) (iterate (*10) 1)
send = val [s,e,n,d]
more = val [m,o,r,e]
money = val [m,o,n,e,y]
constrain $ bAll isDigit ds
constrain $ allDifferent ds
constrain $ s ./= 0 &&& m ./= 0
solve [send + more .== money]
val :: [SInteger] → SInteger is defined as in normal Haskell.
Thanks to the Num type class.
SBV Example: “send more money”

Data.SBV.Examples.Puzzles.SendMoreMoney module
sendMoreMoney :: IO SatResult
sendMoreMoney = sat $ do
ds@[s,e,n,d,m,o,r,y] <- mapM sInteger ["s", "e", "n", "d", "m", "o", "r", "y"]
let isDigit x = x .>= 0 &&& x .<= 9
val xs = sum $ zipWith (*) (reverse xs) (iterate (*10) 1)
send = val [s,e,n,d]
more = val [m,o,r,e]
money = val [m,o,n,e,y]
constrain $ bAll isDigit ds
constrain $ allDifferent ds
constrain $ s ./= 0 &&& m ./= 0
solve [send + more .== money]
Actual constraints specification
SBV Example: “send more money”

Data.SBV.Examples.Puzzles.SendMoreMoney module
sendMoreMoney :: IO SatResult
sendMoreMoney = sat $ do
ds@[s,e,n,d,m,o,r,y] <- mapM sInteger ["s", "e", "n", "d", "m", "o", "r", "y"]
let isDigit x = x .>= 0 &&& x .<= 9
val xs = sum $ zipWith (*) (reverse xs) (iterate (*10) 1)
send = val [s,e,n,d]
more = val [m,o,r,e]
money = val [m,o,n,e,y]
constrain $ bAll isDigit ds
constrain $ allDifferent ds
constrain $ s ./= 0 &&& m ./= 0
solve [send + more .== money]
Satisfiable. Model:
s = 9 :: Integer
e = 5 :: Integer
n = 6 :: Integer
d = 7 :: Integer
m = 1 :: Integer
o = 0 :: Integer
r = 8 :: Integer
y = 2 :: Integer
You need SMT solver Z3

to run the code.
SBV Example: “send more money”

Data.SBV.Examples.Puzzles.SendMoreMoney module
sendMoreMoney :: IO AllSatResult
sendMoreMoney = allSat $ do
ds@[s,e,n,d,m,o,r,y] <- mapM sInteger ["s", "e", "n", "d", "m", "o", "r", "y"]
let isDigit x = x .>= 0 &&& x .<= 9
val xs = sum $ zipWith (*) (reverse xs) (iterate (*10) 1)
send = val [s,e,n,d]
more = val [m,o,r,e]
money = val [m,o,n,e,y]
constrain $ bAll isDigit ds
constrain $ allDifferent ds
constrain $ s ./= 0 &&& m ./= 0
solve [send + more .== money]
By changing sat :: Symbolic SBool → IO SatResult with

allSat :: Symbolic SBool → IO AllSatResult
SBV Summary
This is only one example and sbv includes
variety of examples. You should try!
toysolver package
I’m implementing some decision procedure in Haskell
to leaning the algorithms
https://github.com/msakai/toysolver
http://hackage.haskell.org/package/toysolver
It contains some algorithms/solvers.
In particular, it contains a SAT solver ‘toysat’ and
SMT solver ‘toysmt’
Recalling Last Year …
At Proof Summit 2015,

I talked about how SAT/SMT

solver works.
At that time, I already had implemented SAT
solver ‘toysat’, but not implemented SMT solver
yet.
It triggered my motivation to implement a SMT
solver, I worked hard, and finally I did it!
http://www.slideshare.net/sakai/satsmt
toysat / toysmt
Written in pure Haskell
but implemented in very imperative way
toysat is modestly fast.
It was once the fastest among SAT solvers
written in Haskell. But now mios by Shoji
Narazaki is faster.
toysmt is slow, and has very limited features.
toysmt
toysat based SMT solver
implementation is really native and not-
efficient at all
Theories
Equality and Uninterpreted functions ✓
Linear Real Arithmetic ✓
Bit-vector (currently implementing)
Linear Integer Arithmetic, Array, etc. (not yet)
toysmt: demonstration
(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)
QF_UFLRA.smt2

toysmt: demonstration
$ 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)))
For those who do not
read SEXP
U = {@-1, @1, …, @4, …}
x = 1/10 : Real
c = @3 : U
d = @4 : U
P(x) = if x = @3 then true else false
f(x) = if x = @4 then 0 else 55555
g(x) = if x = @3 then @3 else @-1
toysmt in 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!)
toysmt: Future work
Fill the gap with state-of-the-art solvers (even a little)
There’re lots of rooms for performance improvement.
More theories: Bit-vectors, Integer arithmetic,
Array, …
More features: e.g. Proof-generation
Using ‘toysmt’ as a backend of ‘sbv'.
Re-challenge in next year's SMT-COMP competition.
Conclusion
SAT solvers are amazingly fast for solving many
combinatorial problems
SMT is an extension of SAT to handle high-level
constraints using specialized solvers.
sbv is a neat Haskell library for using SMT
solvers
toysmt is a SMT solver written in Haskell
Further readings
http://www.slideshare.net/sakai/satsmt
http://www.slideshare.net/sakai/
how-a-cdcl-sat-solver-works
Further readings
Handbook of Satisfiability
A. Biere, M. Heule, H. Van
Maaren, and T. Walsh, Eds.
IOS Press, Feb. 2009.
It is a very good book covering
variety of topics related to SAT/
SMT.

More Related Content

What's hot (20)

PDF
暗号技術入門
MITSUNARI Shigeo
 
PDF
インメモリーデータグリッドの選択肢
Masaki Yamakawa
 
PDF
【DL輪読会】RLCD: Reinforcement Learning from Contrast Distillation for Language M...
Deep Learning JP
 
PPTX
【DL輪読会】Reward Design with Language Models
Deep Learning JP
 
PDF
【論文紹介】PGQ: Combining Policy Gradient And Q-learning
Sotetsu KOYAMADA(小山田創哲)
 
PPTX
Come-Closer-Diffuse-Faster Accelerating Conditional Diffusion Models for Inve...
Chung Hyung Jin
 
PDF
Introduction to A3C model
WEBFARMER. ltd.
 
PDF
【DL輪読会】Diffusion Policy: Visuomotor Policy Learning via Action Diffusion
Deep Learning JP
 
PPTX
goroutineはどうやって動いているのか
ota42y
 
PPTX
Paxos
nobu_k
 
PDF
[DL輪読会]In Search of Lost Domain Generalization
Deep Learning JP
 
PPTX
9/14にリリースされたばかりの新LTS版Java 17、ここ3年間のJavaの変化を知ろう!(Open Source Conference 2021 O...
NTT DATA Technology & Innovation
 
PDF
Icra2020 v1
robotpaperchallenge
 
PDF
Pythonではじめる OpenAI Gymトレーニング
Takahiro Kubo
 
PDF
【DL輪読会】Mastering Diverse Domains through World Models
Deep Learning JP
 
PDF
Chapter1 4.6 mod
Takuya Minagawa
 
PDF
最急降下法
Akira Miyazawa
 
PDF
[DL輪読会]Non-Autoregressive Machine Translation with Latent Alignments
Deep Learning JP
 
PDF
Contrastive learning 20200607
ぱんいち すみもと
 
PPTX
Metaspace
Yasumasa Suenaga
 
暗号技術入門
MITSUNARI Shigeo
 
インメモリーデータグリッドの選択肢
Masaki Yamakawa
 
【DL輪読会】RLCD: Reinforcement Learning from Contrast Distillation for Language M...
Deep Learning JP
 
【DL輪読会】Reward Design with Language Models
Deep Learning JP
 
【論文紹介】PGQ: Combining Policy Gradient And Q-learning
Sotetsu KOYAMADA(小山田創哲)
 
Come-Closer-Diffuse-Faster Accelerating Conditional Diffusion Models for Inve...
Chung Hyung Jin
 
Introduction to A3C model
WEBFARMER. ltd.
 
【DL輪読会】Diffusion Policy: Visuomotor Policy Learning via Action Diffusion
Deep Learning JP
 
goroutineはどうやって動いているのか
ota42y
 
Paxos
nobu_k
 
[DL輪読会]In Search of Lost Domain Generalization
Deep Learning JP
 
9/14にリリースされたばかりの新LTS版Java 17、ここ3年間のJavaの変化を知ろう!(Open Source Conference 2021 O...
NTT DATA Technology & Innovation
 
Icra2020 v1
robotpaperchallenge
 
Pythonではじめる OpenAI Gymトレーニング
Takahiro Kubo
 
【DL輪読会】Mastering Diverse Domains through World Models
Deep Learning JP
 
Chapter1 4.6 mod
Takuya Minagawa
 
最急降下法
Akira Miyazawa
 
[DL輪読会]Non-Autoregressive Machine Translation with Latent Alignments
Deep Learning JP
 
Contrastive learning 20200607
ぱんいち すみもと
 
Metaspace
Yasumasa Suenaga
 

Viewers also liked (8)

PDF
自然言語をラムダ式で解釈する体系PTQのHaskell実装
Masahiro Sakai
 
PDF
RClassify: Classifying Race Conditions in Web Applications via Deterministic ...
Masahiro Sakai
 
PDF
Run-time Code Generation and Modal-ML の紹介@PLDIr#2
Masahiro Sakai
 
PDF
ゼロピッチ: MOOC
Masahiro Sakai
 
PDF
Writing a SAT solver as a hobby project
Masahiro Sakai
 
PDF
自動定理証明の紹介
Masahiro Sakai
 
PDF
ディープラーニングの産業応用とそれを支える技術
Shohei Hido
 
PDF
機械学習モデルフォーマットの話:さようならPMML、こんにちはPFA
Shohei Hido
 
自然言語をラムダ式で解釈する体系PTQのHaskell実装
Masahiro Sakai
 
RClassify: Classifying Race Conditions in Web Applications via Deterministic ...
Masahiro Sakai
 
Run-time Code Generation and Modal-ML の紹介@PLDIr#2
Masahiro Sakai
 
ゼロピッチ: MOOC
Masahiro Sakai
 
Writing a SAT solver as a hobby project
Masahiro Sakai
 
自動定理証明の紹介
Masahiro Sakai
 
ディープラーニングの産業応用とそれを支える技術
Shohei Hido
 
機械学習モデルフォーマットの話:さようならPMML、こんにちはPFA
Shohei Hido
 
Ad

Similar to SAT/SMT solving in Haskell (20)

PPTX
Dependent Types with Idris
Abdulsattar Mohammed
 
PPTX
Introduction to Dependently Types: Idris
Abdulsattar Mohammed
 
PDF
QuickCheck - Software Testing
Javran
 
PPTX
Integer security analysis using smt solver
Dharmalingam Ganesan
 
PDF
High-Performance Haskell
Johan Tibell
 
PDF
Liquid Haskell: Theorem Proving for All
Facultad de Informática UCM
 
PDF
Constraint Programming in Haskell
David Overton
 
PDF
Reasoning about laziness
Johan Tibell
 
PDF
Refinement Types for Haskell
Martin Ockajak
 
PDF
Towards an SMT-based approach for Quantitative Information Flow
Quoc-Sang Phan
 
PDF
Introduction to idris
Conor Farrell
 
PDF
Introduction to Functional Languages
suthi
 
PDF
Modularity for Accurate Static Analysis of Smart Contracts
Facultad de Informática UCM
 
KEY
Pontificating quantification
Aaron Bedra
 
PDF
02. haskell motivation
Sebastian Rettig
 
PPTX
Week 4
a_akhavan
 
PDF
Functional Programming in C++
sankeld
 
PDF
Intro To Agda
Larry Diehl
 
PPTX
Prolog 7-Languages
Pierre de Lacaze
 
PPT
Life & Work of Robin Milner | Turing100@Persistent
Persistent Systems Ltd.
 
Dependent Types with Idris
Abdulsattar Mohammed
 
Introduction to Dependently Types: Idris
Abdulsattar Mohammed
 
QuickCheck - Software Testing
Javran
 
Integer security analysis using smt solver
Dharmalingam Ganesan
 
High-Performance Haskell
Johan Tibell
 
Liquid Haskell: Theorem Proving for All
Facultad de Informática UCM
 
Constraint Programming in Haskell
David Overton
 
Reasoning about laziness
Johan Tibell
 
Refinement Types for Haskell
Martin Ockajak
 
Towards an SMT-based approach for Quantitative Information Flow
Quoc-Sang Phan
 
Introduction to idris
Conor Farrell
 
Introduction to Functional Languages
suthi
 
Modularity for Accurate Static Analysis of Smart Contracts
Facultad de Informática UCM
 
Pontificating quantification
Aaron Bedra
 
02. haskell motivation
Sebastian Rettig
 
Week 4
a_akhavan
 
Functional Programming in C++
sankeld
 
Intro To Agda
Larry Diehl
 
Prolog 7-Languages
Pierre de Lacaze
 
Life & Work of Robin Milner | Turing100@Persistent
Persistent Systems Ltd.
 
Ad

More from Masahiro Sakai (20)

PDF
DeepXplore: Automated Whitebox Testing of Deep Learning
Masahiro Sakai
 
PDF
Towards formal verification of neural networks
Masahiro Sakai
 
PDF
関数プログラマから見たPythonと機械学習
Masahiro Sakai
 
PDF
SAT/SMTソルバの仕組み
Masahiro Sakai
 
PDF
Introduction to Max-SAT and Max-SAT Evaluation
Masahiro Sakai
 
PDF
Aluminum: Principled Scenario Exploration through Minimality
Masahiro Sakai
 
PDF
代数的実数とCADの実装紹介
Masahiro Sakai
 
KEY
How a CDCL SAT solver works
Masahiro Sakai
 
PDF
Omega test and beyond
Masahiro Sakai
 
PDF
萩野服部研究室 スキー合宿 2012 自己紹介(酒井)
Masahiro Sakai
 
PDF
“Adoption and Focus: Practical Linear Types for Imperative Programming”他の紹介@P...
Masahiro Sakai
 
PDF
“Design and Implementation of Generics for the .NET Common Language Runtime”他...
Masahiro Sakai
 
PDF
Relaxed Dependency Analysis
Masahiro Sakai
 
PDF
“Symbolic bounds analysis of pointers, array indices, and accessed memory reg...
Masahiro Sakai
 
PDF
Whole Program Paths 等の紹介@PLDIr#3
Masahiro Sakai
 
PDF
Introduction to Categorical Programming (Revised)
Masahiro Sakai
 
PDF
Introduction to Categorical Programming
Masahiro Sakai
 
PPT
融合変換による最適化の理論的基盤と正当性 (2006-06-27)
Masahiro Sakai
 
PPT
融合変換による最適化の理論的基盤と正当性 (2006-06-20)
Masahiro Sakai
 
PPT
Ruby-GNOME2におけるGC問題
Masahiro Sakai
 
DeepXplore: Automated Whitebox Testing of Deep Learning
Masahiro Sakai
 
Towards formal verification of neural networks
Masahiro Sakai
 
関数プログラマから見たPythonと機械学習
Masahiro Sakai
 
SAT/SMTソルバの仕組み
Masahiro Sakai
 
Introduction to Max-SAT and Max-SAT Evaluation
Masahiro Sakai
 
Aluminum: Principled Scenario Exploration through Minimality
Masahiro Sakai
 
代数的実数とCADの実装紹介
Masahiro Sakai
 
How a CDCL SAT solver works
Masahiro Sakai
 
Omega test and beyond
Masahiro Sakai
 
萩野服部研究室 スキー合宿 2012 自己紹介(酒井)
Masahiro Sakai
 
“Adoption and Focus: Practical Linear Types for Imperative Programming”他の紹介@P...
Masahiro Sakai
 
“Design and Implementation of Generics for the .NET Common Language Runtime”他...
Masahiro Sakai
 
Relaxed Dependency Analysis
Masahiro Sakai
 
“Symbolic bounds analysis of pointers, array indices, and accessed memory reg...
Masahiro Sakai
 
Whole Program Paths 等の紹介@PLDIr#3
Masahiro Sakai
 
Introduction to Categorical Programming (Revised)
Masahiro Sakai
 
Introduction to Categorical Programming
Masahiro Sakai
 
融合変換による最適化の理論的基盤と正当性 (2006-06-27)
Masahiro Sakai
 
融合変換による最適化の理論的基盤と正当性 (2006-06-20)
Masahiro Sakai
 
Ruby-GNOME2におけるGC問題
Masahiro Sakai
 

Recently uploaded (20)

PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PPTX
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPT
Electrical Safety Presentation for Basics Learning
AliJaved79382
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PDF
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
DOCX
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
PPTX
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PPTX
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PDF
smart lot access control system with eye
rasabzahra
 
PPTX
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Design Thinking basics for Engineers.pdf
CMR University
 
Lecture 1 Shell and Tube Heat exchanger-1.pptx
mailforillegalwork
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
Heart Bleed Bug - A case study (Course: Cryptography and Network Security)
Adri Jovin
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Electrical Safety Presentation for Basics Learning
AliJaved79382
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
Reasons for the succes of MENARD PRESSUREMETER.pdf
majdiamz
 
CS-802 (A) BDH Lab manual IPS Academy Indore
thegodhimself05
 
Solar Thermal Energy System Seminar.pptx
Gpc Purapuza
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
VITEEE 2026 Exam Details , Important Dates
SonaliSingh127098
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
smart lot access control system with eye
rasabzahra
 
Damage of stability of a ship and how its change .pptx
ehamadulhaque
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 

SAT/SMT solving in Haskell

  • 1. SAT/SMT solving in
 Haskell Masahiro Sakai (酒井 政裕)
 Haskell Day 2016 2016-09-17
  • 2. Self Introduction
 Masahiro Sakai Twitter: @masahiro_sakai github: https://github.com/msakai/ G+: https://plus.google.com/+MasahiroSakai Translated “Software Abstractions” and TaPL into Japanese with colleagues Interests: Categorical Programming, Theorem Proving / Decision Procedures,
 …
  • 3. Agenda What are SAT and SMT? Haskell libraries for SMT solving sbv toysat/toysmt Conclusion
  • 5. 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.
  • 6. 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)”?
  • 7. SMT Solver Impl.
 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 Uninterpre ted Function
 Solver
 f, g, = Array
 Solver
 read, write 
 ・・・
  • 8. Some Applications
 of SAT/SMT Software/Hardware verification Model checking, Test-case generation, … Theorem proving Puzzles: Sudoku, Numberlink, Nonogram, etc. Type checking in Liquid Haskell eg: doubles :: [{x : Int | x >= 0}]→[{x : Int | x `mod` 2 = 0}] Program Synthesis and more
  • 10. Some Haskell packages for SMT Binding sbv, smtlib2, simple-smt z3, bindings-yices, yices-easy, yices-painless SMT solvers written in Haskell: toysolver, Smooth SMT-LIB2 file parser/printer smt-lib, SmtLib SMT-LIB2 is a standard
 input/output format for SMT solvers
  • 11. SBV: SMT Based Verification in Haskell SMT library developed by Levent Erkok It provides: High-Level DSL for specifying problems in Haskell, and Interfaces to multiple SMT solver backends including Z3, CVC4, Yices, Boolector. You can install simply using stack/cabal “stack install sbv” or “cabal install sbv"
  • 12. SBV Example: “send more money”
 Data.SBV.Examples.Puzzles.SendMoreMoney module sendMoreMoney :: IO SatResult sendMoreMoney = sat $ do ds@[s,e,n,d,m,o,r,y] <- mapM sInteger ["s", "e", "n", "d", "m", "o", "r", "y"] let isDigit x = x .>= 0 &&& x .<= 9 val xs = sum $ zipWith (*) (reverse xs) (iterate (*10) 1) send = val [s,e,n,d] more = val [m,o,r,e] money = val [m,o,n,e,y] constrain $ bAll isDigit ds constrain $ allDifferent ds constrain $ s ./= 0 &&& m ./= 0 solve [send + more .== money] SEND
 +MORE ————
 MONEY
  • 13. SBV Example: “send more money”
 Data.SBV.Examples.Puzzles.SendMoreMoney module sendMoreMoney :: IO SatResult sendMoreMoney = sat $ do ds@[s,e,n,d,m,o,r,y] <- mapM sInteger ["s", "e", "n", "d", "m", "o", "r", "y"] let isDigit x = x .>= 0 &&& x .<= 9 val xs = sum $ zipWith (*) (reverse xs) (iterate (*10) 1) send = val [s,e,n,d] more = val [m,o,r,e] money = val [m,o,n,e,y] constrain $ bAll isDigit ds constrain $ allDifferent ds constrain $ s ./= 0 &&& m ./= 0 solve [send + more .== money] SMT problem is defined using Symbolic monad, and SMT solving is performed by
 sat :: Symbolic SBool → IO SatResult
  • 14. SBV Example: “send more money”
 Data.SBV.Examples.Puzzles.SendMoreMoney module sendMoreMoney :: IO SatResult sendMoreMoney = sat $ do ds@[s,e,n,d,m,o,r,y] <- mapM sInteger ["s", "e", "n", "d", "m", "o", "r", "y"] let isDigit x = x .>= 0 &&& x .<= 9 val xs = sum $ zipWith (*) (reverse xs) (iterate (*10) 1) send = val [s,e,n,d] more = val [m,o,r,e] money = val [m,o,n,e,y] constrain $ bAll isDigit ds constrain $ allDifferent ds constrain $ s ./= 0 &&& m ./= 0 solve [send + more .== money] sInteger :: String → Symbolic SInteger
 creates integer variable
  • 15. SBV Example: “send more money”
 Data.SBV.Examples.Puzzles.SendMoreMoney module sendMoreMoney :: IO SatResult sendMoreMoney = sat $ do ds@[s,e,n,d,m,o,r,y] <- mapM sInteger ["s", "e", "n", "d", "m", "o", "r", "y"] let isDigit x = x .>= 0 &&& x .<= 9 val xs = sum $ zipWith (*) (reverse xs) (iterate (*10) 1) send = val [s,e,n,d] more = val [m,o,r,e] money = val [m,o,n,e,y] constrain $ bAll isDigit ds constrain $ allDifferent ds constrain $ s ./= 0 &&& m ./= 0 solve [send + more .== money] Comparison over symbolic values: we have to use slightly difference operators like (.>=), (&&&). Because Haskell’s (>=), (&&) returns Bool, but we want SBool.
  • 16. SBV Example: “send more money”
 Data.SBV.Examples.Puzzles.SendMoreMoney module sendMoreMoney :: IO SatResult sendMoreMoney = sat $ do ds@[s,e,n,d,m,o,r,y] <- mapM sInteger ["s", "e", "n", "d", "m", "o", "r", "y"] let isDigit x = x .>= 0 &&& x .<= 9 val xs = sum $ zipWith (*) (reverse xs) (iterate (*10) 1) send = val [s,e,n,d] more = val [m,o,r,e] money = val [m,o,n,e,y] constrain $ bAll isDigit ds constrain $ allDifferent ds constrain $ s ./= 0 &&& m ./= 0 solve [send + more .== money] val :: [SInteger] → SInteger is defined as in normal Haskell. Thanks to the Num type class.
  • 17. SBV Example: “send more money”
 Data.SBV.Examples.Puzzles.SendMoreMoney module sendMoreMoney :: IO SatResult sendMoreMoney = sat $ do ds@[s,e,n,d,m,o,r,y] <- mapM sInteger ["s", "e", "n", "d", "m", "o", "r", "y"] let isDigit x = x .>= 0 &&& x .<= 9 val xs = sum $ zipWith (*) (reverse xs) (iterate (*10) 1) send = val [s,e,n,d] more = val [m,o,r,e] money = val [m,o,n,e,y] constrain $ bAll isDigit ds constrain $ allDifferent ds constrain $ s ./= 0 &&& m ./= 0 solve [send + more .== money] Actual constraints specification
  • 18. SBV Example: “send more money”
 Data.SBV.Examples.Puzzles.SendMoreMoney module sendMoreMoney :: IO SatResult sendMoreMoney = sat $ do ds@[s,e,n,d,m,o,r,y] <- mapM sInteger ["s", "e", "n", "d", "m", "o", "r", "y"] let isDigit x = x .>= 0 &&& x .<= 9 val xs = sum $ zipWith (*) (reverse xs) (iterate (*10) 1) send = val [s,e,n,d] more = val [m,o,r,e] money = val [m,o,n,e,y] constrain $ bAll isDigit ds constrain $ allDifferent ds constrain $ s ./= 0 &&& m ./= 0 solve [send + more .== money] Satisfiable. Model: s = 9 :: Integer e = 5 :: Integer n = 6 :: Integer d = 7 :: Integer m = 1 :: Integer o = 0 :: Integer r = 8 :: Integer y = 2 :: Integer You need SMT solver Z3
 to run the code.
  • 19. SBV Example: “send more money”
 Data.SBV.Examples.Puzzles.SendMoreMoney module sendMoreMoney :: IO AllSatResult sendMoreMoney = allSat $ do ds@[s,e,n,d,m,o,r,y] <- mapM sInteger ["s", "e", "n", "d", "m", "o", "r", "y"] let isDigit x = x .>= 0 &&& x .<= 9 val xs = sum $ zipWith (*) (reverse xs) (iterate (*10) 1) send = val [s,e,n,d] more = val [m,o,r,e] money = val [m,o,n,e,y] constrain $ bAll isDigit ds constrain $ allDifferent ds constrain $ s ./= 0 &&& m ./= 0 solve [send + more .== money] By changing sat :: Symbolic SBool → IO SatResult with
 allSat :: Symbolic SBool → IO AllSatResult
  • 20. SBV Summary This is only one example and sbv includes variety of examples. You should try!
  • 21. toysolver package I’m implementing some decision procedure in Haskell to leaning the algorithms https://github.com/msakai/toysolver http://hackage.haskell.org/package/toysolver It contains some algorithms/solvers. In particular, it contains a SAT solver ‘toysat’ and SMT solver ‘toysmt’
  • 22. Recalling Last Year … At Proof Summit 2015,
 I talked about how SAT/SMT
 solver works. At that time, I already had implemented SAT solver ‘toysat’, but not implemented SMT solver yet. It triggered my motivation to implement a SMT solver, I worked hard, and finally I did it! http://www.slideshare.net/sakai/satsmt
  • 23. toysat / toysmt Written in pure Haskell but implemented in very imperative way toysat is modestly fast. It was once the fastest among SAT solvers written in Haskell. But now mios by Shoji Narazaki is faster. toysmt is slow, and has very limited features.
  • 24. toysmt toysat based SMT solver implementation is really native and not- efficient at all Theories Equality and Uninterpreted functions ✓ Linear Real Arithmetic ✓ Bit-vector (currently implementing) Linear Integer Arithmetic, Array, etc. (not yet)
  • 25. toysmt: demonstration (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) QF_UFLRA.smt2

  • 26. toysmt: demonstration $ 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)))
  • 27. For those who do not read SEXP U = {@-1, @1, …, @4, …} x = 1/10 : Real c = @3 : U d = @4 : U P(x) = if x = @3 then true else false f(x) = if x = @4 then 0 else 55555 g(x) = if x = @3 then @3 else @-1
  • 28. toysmt in 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. toysmt: Future work Fill the gap with state-of-the-art solvers (even a little) There’re lots of rooms for performance improvement. More theories: Bit-vectors, Integer arithmetic, Array, … More features: e.g. Proof-generation Using ‘toysmt’ as a backend of ‘sbv'. Re-challenge in next year's SMT-COMP competition.
  • 30. Conclusion SAT solvers are amazingly fast for solving many combinatorial problems SMT is an extension of SAT to handle high-level constraints using specialized solvers. sbv is a neat Haskell library for using SMT solvers toysmt is a SMT solver written in Haskell
  • 32. Further readings Handbook of Satisfiability A. Biere, M. Heule, H. Van Maaren, and T. Walsh, Eds. IOS Press, Feb. 2009. It is a very good book covering variety of topics related to SAT/ SMT.