SlideShare a Scribd company logo
1 of 24
Download to read offline
A MuDDy Experience
         ML Bindings to a BDD Library
(Including discussant slides by Oleg Kiselyov)


               Ken Friis Larsen
             kflarsen@diku.dk

          Department of Computer Science
             University of Copenhagen


                 July 15, 2009




                                                 1 / 21
The Message




ML and BDDs hits a sweet spot




                            2 / 21
Background



   BuDDy
      C library for Binary Decision Diagrams by Jørn Lind-Nielsen
      State-of-art performance . . . 10 years ago
   MuDDy
      An ML interface to BuDDy
      Comes in Moscow ML, MLton, and O’Caml flavours
      Been used in many different projects over the years
   Domain Specific Embedded Language (DSEL)
      You embed a DSL in a general-purpose language




                                                                    3 / 21
Binary Decision Diagrams



   A BDD is:
       A canonical explicit directed acyclic graph representation of a
       boolean function
       A boolean expression on if-then-else normal form
   BDDs are mainly used for formal verification and hardware
   synthesis
   Excellent for representing large relations, for instance.
   Did I mention it is a canonical representation?




                                                                         4 / 21
An Example BDD

A BDD representing (x ⇔ y) ∧ ¬z


                                   x


                          y            y



                                   z



                         0             1


With variable ordering x < y < z


                                           5 / 21
MuDDy




MuDDy supports most features from BuDDy
   Structure bdd contains functions for manipulating BDDs
   Structure fdd contains functions for manipulating finite domain
   values, represented by a set of BDDs
   Structure bvec contains functions for representing and
   manipulating machine words represented by a set of BDDs.




                                                                6 / 21
Building BDDs from SML



Building a BDD representing the expression (x ⇒ y ∧ x) ⇒ y

 val (x, y) = (bdd.ithvar 0, bdd.ithvar 1)
 val b = (x ==> y / x) ==> y

Given the syntactic sugar:

 infix ==> /
 val(op /, op ==>) = (bdd.AND, bdd.IMP)




                                                             7 / 21
Using BDDs to Analyse a DS(E)L




 1. Design your language
 2. Declare types for representing abstract syntax trees
 3. Design a concrete syntax
 4. Use BDDs to model the semantics of your language
        Usually that means defining a (huge) transition predicate
 5. Use BDDs to find the set of reachable states and analyse all
    reachable states in one go.




                                                                   8 / 21
Simple Guarded Command Language




   e            ::=   true | false | x | e1 op e2 | ¬e
   assignment   ::=   x1 , . . . , xn := e1 , . . . , en
   command      ::=   e ? assignment
   program      ::=   assignment
                      command1 || . . . || commandn




                                                           9 / 21
Milner’s Scheduler


                                             h1
                                        c1          c2

                     start         h0                    h2

                                        c0          c3
                                             h3




    cycler i   =    ci ∧ ¬ti   ?   ti , ci , hi := true, ¬ci , true
               ||         hi   ?   ci+1 mod N , hi := true, false




                                                                      10 / 21
SGCL Embedded in SML, Abstract syntax




type var = string
datatype boolop = AND | OR | IMP | BIIMP
datatype bexp = BVar of var
              | BBin of bexp * boolop * bexp
              | NOT of bexp
              | TRUE | FALSE
datatype command = CMD of bexp * (var * bexp) list
datatype program = PRG of (var * bexp) list * command list




                                                       11 / 21
SGCL Embedded in SML, Syntactic Sugar

fun mkBBin opr (x, y) = BBin(x, opr, y)
infix / / ==> <==>
val (op /, op /, op ==>, op <==>) =
    (mkBBin AND, mkBBin OR, mkBBin IMP, mkBBin BIIMP)

infix ::=
val op ::= = ListPair.zip

infix ?
fun g ? ass = [CMD(g, ass)]

infix ||
val op|| = op@

val $ = BVar

                                                        12 / 21
Milner’s Scheduler in SML/SGCL
val (c0, t0,...,t3, h3) = ("c0", "t0",...,"t3", "h3")

fun cycler c t h c’ =
      ($c <==> TRUE / $t <==> FALSE) ?
                ([t, c, h] ::= [TRUE, NOT($c), TRUE])
   || (($h <==> TRUE) ? ([c’, h] ::= [TRUE, FALSE]))

fun task t = $t ? ([t] ::= [FALSE])

val milner4 =
    PRG( [(c0, TRUE),   (t0, FALSE), (h0, FALSE), ... ]
         cycler c0 t0   h0 c1
      || cycler c1 t1   h1 c2
      || cycler c2 t2   h2 c3
      || cycler c3 t3   h3 c0
      || task t0 ||     task t1 || task t2 || task t3)
                                                          13 / 21
Semantics of SGCL


The semantics of a command is a predicate describing a state change
by using a ordinary variables to describe the current state and primed
variables to describe the next state.
The semantics of a program is a predicate describing the initial state,
and conjunction of the semantics of the commands.

type bdd_vars = { var: int, primed: int}
type var_map = string * bdd_vars

val commandToBDD: var_map -> command -> bdd.bdd
val programToBDD: var_map -> program -> bdd.bdd * bdd.bdd




                                                                   14 / 21
Semantics of SGCL

fun commandToBDD allVars (CMD(guard, assignments)) =
    let val changed = List.map #1 assignments
        val unchanged =
            List.foldl (fn ((v, {var, primed}), res) =>
              if mem v changed then res
              else bdd.AND(bdd.BIIMP(bdd.ithvar primed,
                                     bdd.ithvar var),
                           res))
              bdd.TRUE allVars
        val assigns =
            conj (map (fn (v,be) =>
                            bdd.BIIMP(primed v, bexp be))
                  assignments)
    in bdd.IMP(bexp guard, assigns) end


                                                       15 / 21
Finding All Reachable States

fun reachable allVars I T =
    let val renames =
            List.map (fn(_,{var,primed}) => (var, primed))
                     allVars
        val pairset = bdd.makepairSet renames
        val unprimed = bdd.makeset(List.map #var allVars)

         open bdd infix OR
         fun loop R =
             let val post = appex T R And unprimed
                 val next = R OR replace next pairset
             in if equal R next then R else loop next end
    in   loop I end



                                                       16 / 21
Putting It All Together

To find all the reachable states of a SGCL program first call
programToBDD and then reachable:

  val   milner4 = ...
  val   allVars = ...
  val   (I, T) = programToBDD allVars milner4
  val   states = reachable allVars I T

We can now easily check some invariants. For example, that if
cycler 2 holds a token, no other cycler has a token:

  val c2inv = $c2 ==> NOT($c0) / NOT($c1) / NOT($c3)
  val check_c2inv = bdd.IMP (states, bexp allVars c2inv)




                                                                17 / 21
Keeping It Honest


The construction of the mapping between DSL variables and BDDs
variables is usually where things go sour. That is, it is hard to choose
a good BDD variable ordering.

No general algorithm, but the following heuristics gives good results:
    a variable and its primed version should be next to each other in
    the ordering
    if two variables are “close” to each other in the syntax tree, they
    should be close to each other in the ordering
    if a variable occurs with high frequency in the syntax tree, it
    should be in the beginning of the ordering




                                                                      18 / 21
What About Performance?
          No. of Schedulers:    50      100     150     200

          C                     1.63    4.69    13.66   31.20
          C++                   1.66    4.82    13.89   31.51
          O’Caml (native)       1.71    5.04    14.47   32.05
          O’Caml (bytecode)     1.74    5.15    14.58   32.91
          Moscow ML             1.76    5.15    15.12   33.42

Fresh runs on current laptop:

           No. of Schedulers:    50      100     150    200

           C                     0.38    1.07    3.18   7.25
           O’Caml (native)       0.35    1.21    3.42   7.64
           O’Caml (bytecode)     0.38    1.24    3.52   7.82
           Moscow ML             0.37    1.21    3.45   7.73
           MLton                 0.38    1.29    3.68   8.14

                                                                19 / 21
Summary




  ML and BDDs hits a sweet spot
      reasonably easy to embed DSLs in ML languages
      MLs gives few surprises with respect to space usage and execution
      BDDs can be used to represent many nice abstractions
      symbolically
  SGCL is a nice way to specify finite state machines




                                                                   20 / 21
The following three slides are the discussant slides prepared by
Oleg Kiselyov.




                                                                   21 / 21
Terminology
  DSL
  -(a+1)

  Initial embedding in ML
  type ast = Var of String | Lit of int
           | Neg of ast | Add of ast * ast
  let term1 = Neg (Add (Var "a") (Lit 1))
  val eval : ast -> t

  Final embedding in ML
  type repr (* abstract; concrete; parameter *)
  val var : string -> repr
  val lit : int -> repr
  val neg : repr -> repr
  val add : repr -> repr -> repr
  let term1 = neg (add (var "a") (lit 1))
Summary
  Two DSL embedded in OCaml

   1. DSL of BDD, embedded finally
   2. DSL of SGCL, embedded initially
      one non-standard eval: a model checker
Questions, comments
   1. Both initial or both final embeddings?
   2. Details (esp. regarding GC) for BDD EDSL?


     Stale performance comparisons
     Performance with MLton?
     BDD in a high-level language?
     Related work: BDD in Datalog (Lam et al.)

More Related Content

Similar to A MuDDy Experience - ML Bindings to a BDD Library

20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilers20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilers
Computer Science Club
 
Logic Design - Chapter 5: Part1 Combinattional Logic
Logic Design - Chapter 5: Part1 Combinattional LogicLogic Design - Chapter 5: Part1 Combinattional Logic
Logic Design - Chapter 5: Part1 Combinattional Logic
Gouda Mando
 
Parallel Evaluation of Multi-Semi-Joins
Parallel Evaluation of Multi-Semi-JoinsParallel Evaluation of Multi-Semi-Joins
Parallel Evaluation of Multi-Semi-Joins
Jonny Daenen
 

Similar to A MuDDy Experience - ML Bindings to a BDD Library (20)

A MuDDy Experience - ML Bindings to a BDD Library
A MuDDy Experience - ML Bindings to a BDD LibraryA MuDDy Experience - ML Bindings to a BDD Library
A MuDDy Experience - ML Bindings to a BDD Library
 
micro_lecture-ch-456.pptx
micro_lecture-ch-456.pptxmicro_lecture-ch-456.pptx
micro_lecture-ch-456.pptx
 
20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilers20101017 program analysis_for_security_livshits_lecture02_compilers
20101017 program analysis_for_security_livshits_lecture02_compilers
 
Incremental and parallel computation of structural graph summaries for evolvi...
Incremental and parallel computation of structural graph summaries for evolvi...Incremental and parallel computation of structural graph summaries for evolvi...
Incremental and parallel computation of structural graph summaries for evolvi...
 
ON AN OPTIMIZATION TECHNIQUE USING BINARY DECISION DIAGRAM
ON AN OPTIMIZATION TECHNIQUE USING BINARY DECISION DIAGRAMON AN OPTIMIZATION TECHNIQUE USING BINARY DECISION DIAGRAM
ON AN OPTIMIZATION TECHNIQUE USING BINARY DECISION DIAGRAM
 
Logic Design - Chapter 5: Part1 Combinattional Logic
Logic Design - Chapter 5: Part1 Combinattional LogicLogic Design - Chapter 5: Part1 Combinattional Logic
Logic Design - Chapter 5: Part1 Combinattional Logic
 
CS 354 Transformation, Clipping, and Culling
CS 354 Transformation, Clipping, and CullingCS 354 Transformation, Clipping, and Culling
CS 354 Transformation, Clipping, and Culling
 
Java Keeps Throttling Up!
Java Keeps Throttling Up!Java Keeps Throttling Up!
Java Keeps Throttling Up!
 
Parallel Evaluation of Multi-Semi-Joins
Parallel Evaluation of Multi-Semi-JoinsParallel Evaluation of Multi-Semi-Joins
Parallel Evaluation of Multi-Semi-Joins
 
Lecture 3: Data-Intensive Computing for Text Analysis (Fall 2011)
Lecture 3: Data-Intensive Computing for Text Analysis (Fall 2011)Lecture 3: Data-Intensive Computing for Text Analysis (Fall 2011)
Lecture 3: Data-Intensive Computing for Text Analysis (Fall 2011)
 
FPGA based BCH Decoder
FPGA based BCH DecoderFPGA based BCH Decoder
FPGA based BCH Decoder
 
Scalding
ScaldingScalding
Scalding
 
Tuning and Debugging in Apache Spark
Tuning and Debugging in Apache SparkTuning and Debugging in Apache Spark
Tuning and Debugging in Apache Spark
 
ERTS UNIT 3.ppt
ERTS UNIT 3.pptERTS UNIT 3.ppt
ERTS UNIT 3.ppt
 
Exact Inference in Bayesian Networks using MapReduce (Hadoop Summit 2010)
Exact Inference in Bayesian Networks using MapReduce (Hadoop Summit 2010)Exact Inference in Bayesian Networks using MapReduce (Hadoop Summit 2010)
Exact Inference in Bayesian Networks using MapReduce (Hadoop Summit 2010)
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Trident International Graphics Workshop 2014 4/5
Trident International Graphics Workshop 2014 4/5Trident International Graphics Workshop 2014 4/5
Trident International Graphics Workshop 2014 4/5
 
Lecture 2: Data-Intensive Computing for Text Analysis (Fall 2011)
Lecture 2: Data-Intensive Computing for Text Analysis (Fall 2011)Lecture 2: Data-Intensive Computing for Text Analysis (Fall 2011)
Lecture 2: Data-Intensive Computing for Text Analysis (Fall 2011)
 
05-Debug.pdf
05-Debug.pdf05-Debug.pdf
05-Debug.pdf
 
LCD_Example.pptx
LCD_Example.pptxLCD_Example.pptx
LCD_Example.pptx
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 

A MuDDy Experience - ML Bindings to a BDD Library

  • 1. A MuDDy Experience ML Bindings to a BDD Library (Including discussant slides by Oleg Kiselyov) Ken Friis Larsen kflarsen@diku.dk Department of Computer Science University of Copenhagen July 15, 2009 1 / 21
  • 2. The Message ML and BDDs hits a sweet spot 2 / 21
  • 3. Background BuDDy C library for Binary Decision Diagrams by Jørn Lind-Nielsen State-of-art performance . . . 10 years ago MuDDy An ML interface to BuDDy Comes in Moscow ML, MLton, and O’Caml flavours Been used in many different projects over the years Domain Specific Embedded Language (DSEL) You embed a DSL in a general-purpose language 3 / 21
  • 4. Binary Decision Diagrams A BDD is: A canonical explicit directed acyclic graph representation of a boolean function A boolean expression on if-then-else normal form BDDs are mainly used for formal verification and hardware synthesis Excellent for representing large relations, for instance. Did I mention it is a canonical representation? 4 / 21
  • 5. An Example BDD A BDD representing (x ⇔ y) ∧ ¬z x y y z 0 1 With variable ordering x < y < z 5 / 21
  • 6. MuDDy MuDDy supports most features from BuDDy Structure bdd contains functions for manipulating BDDs Structure fdd contains functions for manipulating finite domain values, represented by a set of BDDs Structure bvec contains functions for representing and manipulating machine words represented by a set of BDDs. 6 / 21
  • 7. Building BDDs from SML Building a BDD representing the expression (x ⇒ y ∧ x) ⇒ y val (x, y) = (bdd.ithvar 0, bdd.ithvar 1) val b = (x ==> y / x) ==> y Given the syntactic sugar: infix ==> / val(op /, op ==>) = (bdd.AND, bdd.IMP) 7 / 21
  • 8. Using BDDs to Analyse a DS(E)L 1. Design your language 2. Declare types for representing abstract syntax trees 3. Design a concrete syntax 4. Use BDDs to model the semantics of your language Usually that means defining a (huge) transition predicate 5. Use BDDs to find the set of reachable states and analyse all reachable states in one go. 8 / 21
  • 9. Simple Guarded Command Language e ::= true | false | x | e1 op e2 | ¬e assignment ::= x1 , . . . , xn := e1 , . . . , en command ::= e ? assignment program ::= assignment command1 || . . . || commandn 9 / 21
  • 10. Milner’s Scheduler h1 c1 c2 start h0 h2 c0 c3 h3 cycler i = ci ∧ ¬ti ? ti , ci , hi := true, ¬ci , true || hi ? ci+1 mod N , hi := true, false 10 / 21
  • 11. SGCL Embedded in SML, Abstract syntax type var = string datatype boolop = AND | OR | IMP | BIIMP datatype bexp = BVar of var | BBin of bexp * boolop * bexp | NOT of bexp | TRUE | FALSE datatype command = CMD of bexp * (var * bexp) list datatype program = PRG of (var * bexp) list * command list 11 / 21
  • 12. SGCL Embedded in SML, Syntactic Sugar fun mkBBin opr (x, y) = BBin(x, opr, y) infix / / ==> <==> val (op /, op /, op ==>, op <==>) = (mkBBin AND, mkBBin OR, mkBBin IMP, mkBBin BIIMP) infix ::= val op ::= = ListPair.zip infix ? fun g ? ass = [CMD(g, ass)] infix || val op|| = op@ val $ = BVar 12 / 21
  • 13. Milner’s Scheduler in SML/SGCL val (c0, t0,...,t3, h3) = ("c0", "t0",...,"t3", "h3") fun cycler c t h c’ = ($c <==> TRUE / $t <==> FALSE) ? ([t, c, h] ::= [TRUE, NOT($c), TRUE]) || (($h <==> TRUE) ? ([c’, h] ::= [TRUE, FALSE])) fun task t = $t ? ([t] ::= [FALSE]) val milner4 = PRG( [(c0, TRUE), (t0, FALSE), (h0, FALSE), ... ] cycler c0 t0 h0 c1 || cycler c1 t1 h1 c2 || cycler c2 t2 h2 c3 || cycler c3 t3 h3 c0 || task t0 || task t1 || task t2 || task t3) 13 / 21
  • 14. Semantics of SGCL The semantics of a command is a predicate describing a state change by using a ordinary variables to describe the current state and primed variables to describe the next state. The semantics of a program is a predicate describing the initial state, and conjunction of the semantics of the commands. type bdd_vars = { var: int, primed: int} type var_map = string * bdd_vars val commandToBDD: var_map -> command -> bdd.bdd val programToBDD: var_map -> program -> bdd.bdd * bdd.bdd 14 / 21
  • 15. Semantics of SGCL fun commandToBDD allVars (CMD(guard, assignments)) = let val changed = List.map #1 assignments val unchanged = List.foldl (fn ((v, {var, primed}), res) => if mem v changed then res else bdd.AND(bdd.BIIMP(bdd.ithvar primed, bdd.ithvar var), res)) bdd.TRUE allVars val assigns = conj (map (fn (v,be) => bdd.BIIMP(primed v, bexp be)) assignments) in bdd.IMP(bexp guard, assigns) end 15 / 21
  • 16. Finding All Reachable States fun reachable allVars I T = let val renames = List.map (fn(_,{var,primed}) => (var, primed)) allVars val pairset = bdd.makepairSet renames val unprimed = bdd.makeset(List.map #var allVars) open bdd infix OR fun loop R = let val post = appex T R And unprimed val next = R OR replace next pairset in if equal R next then R else loop next end in loop I end 16 / 21
  • 17. Putting It All Together To find all the reachable states of a SGCL program first call programToBDD and then reachable: val milner4 = ... val allVars = ... val (I, T) = programToBDD allVars milner4 val states = reachable allVars I T We can now easily check some invariants. For example, that if cycler 2 holds a token, no other cycler has a token: val c2inv = $c2 ==> NOT($c0) / NOT($c1) / NOT($c3) val check_c2inv = bdd.IMP (states, bexp allVars c2inv) 17 / 21
  • 18. Keeping It Honest The construction of the mapping between DSL variables and BDDs variables is usually where things go sour. That is, it is hard to choose a good BDD variable ordering. No general algorithm, but the following heuristics gives good results: a variable and its primed version should be next to each other in the ordering if two variables are “close” to each other in the syntax tree, they should be close to each other in the ordering if a variable occurs with high frequency in the syntax tree, it should be in the beginning of the ordering 18 / 21
  • 19. What About Performance? No. of Schedulers: 50 100 150 200 C 1.63 4.69 13.66 31.20 C++ 1.66 4.82 13.89 31.51 O’Caml (native) 1.71 5.04 14.47 32.05 O’Caml (bytecode) 1.74 5.15 14.58 32.91 Moscow ML 1.76 5.15 15.12 33.42 Fresh runs on current laptop: No. of Schedulers: 50 100 150 200 C 0.38 1.07 3.18 7.25 O’Caml (native) 0.35 1.21 3.42 7.64 O’Caml (bytecode) 0.38 1.24 3.52 7.82 Moscow ML 0.37 1.21 3.45 7.73 MLton 0.38 1.29 3.68 8.14 19 / 21
  • 20. Summary ML and BDDs hits a sweet spot reasonably easy to embed DSLs in ML languages MLs gives few surprises with respect to space usage and execution BDDs can be used to represent many nice abstractions symbolically SGCL is a nice way to specify finite state machines 20 / 21
  • 21. The following three slides are the discussant slides prepared by Oleg Kiselyov. 21 / 21
  • 22. Terminology DSL -(a+1) Initial embedding in ML type ast = Var of String | Lit of int | Neg of ast | Add of ast * ast let term1 = Neg (Add (Var "a") (Lit 1)) val eval : ast -> t Final embedding in ML type repr (* abstract; concrete; parameter *) val var : string -> repr val lit : int -> repr val neg : repr -> repr val add : repr -> repr -> repr let term1 = neg (add (var "a") (lit 1))
  • 23. Summary Two DSL embedded in OCaml 1. DSL of BDD, embedded finally 2. DSL of SGCL, embedded initially one non-standard eval: a model checker
  • 24. Questions, comments 1. Both initial or both final embeddings? 2. Details (esp. regarding GC) for BDD EDSL? Stale performance comparisons Performance with MLton? BDD in a high-level language? Related work: BDD in Datalog (Lam et al.)