CookBook/Recipes/Sat.thy
author Christian Urban <urbanc@in.tum.de>
Tue, 17 Mar 2009 01:56:29 +0100
changeset 180 9c25418db6f0
parent 127 74846cb0fff9
child 181 5baaabe1ab92
permissions -rw-r--r--
added a recipy about SAT solvers
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
theory Sat
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
     3
imports Main "../Base"
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
begin
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
     6
section {* SAT Solver\label{rec:sat} *}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
     7
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
     8
text {*
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
     9
  {\bf Problem:}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    10
  You like to use a SAT solver to find out whether
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    11
  an Isabelle formula is satisfiable or not.\smallskip
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    12
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    13
  {\bf Solution:} Isabelle contains a general interface for 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    14
  a number of external SAT solvers (including ZChaff and Minisat)
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    15
  and also contains a simple internal SAT solver that
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    16
  is based on the DPLL algorithm.\smallskip
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    17
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    18
  The SAT solvers expect a propositional formula as input and produce
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    19
  a result indicating that the formula is satisfiable, unsatisfiable or
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    20
  unknown. The type of the propositional formula is  
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    21
  @{ML_type "PropLogic.prop_formula"} with the usual constructors such 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    22
  as @{ML And in PropLogic}, @{ML Or in PropLogic} and so on.
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    23
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    24
  There is the function  @{ML  PropLogic.prop_formula_of_term}, which
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    25
  translates an Isabelle term into a propositional formula. Let
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    26
  us illustrate this function with translating the term @{term "A \<and> \<not>A \<or> B"}. 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    27
  Suppose the ML-value
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    28
*}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    29
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    30
ML{*val (form, tab) = 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    31
       PropLogic.prop_formula_of_term @{term "A \<and> \<not>A \<or> B"}  Termtab.empty*}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    32
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    33
text {*
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    34
  then the resulting propositional formula @{ML form} is 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    35
  @{ML "Or (And (BoolVar 1, Not (BoolVar 1)), BoolVar 2)" in PropLogic} 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    36
  where indices are assigned  for the propositional variables 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    37
  @{text "A"} and @{text "B"} respectively. This assignment is recorded 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    38
  in the table that is given to the translation function and also returned 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    39
  (appropriately updated) in the result. In the case above the
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    40
  input table is empty and the output table is
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    41
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    42
  @{ML_response_fake [display,gray]
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    43
  "Termtab.dest tab"
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    44
  "[(Free (\"A\", \"bool\"), 1), (Free (\"B\", \"bool\"), 2)]"}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    45
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    46
  A propositional variable is also introduced whenever the translation
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    47
  function cannot find an appropriate propositional formula for a term. 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    48
  Given the ML-value
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    49
*}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    50
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    51
ML{*val (form', tab') = 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    52
       PropLogic.prop_formula_of_term @{term "\<forall>x::nat. P x"}  Termtab.empty*}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    53
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    54
text {*
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    55
  @{ML form'} is now the propositional variable @{ML "BoolVar 1" in PropLogic} 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    56
  and the table @{ML tab'} is
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    57
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    58
  @{ML_response_fake [display,gray]
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    59
  "map (apfst (Syntax.string_of_term @{context})) (Termtab.dest tab')"
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    60
  "(\<forall>x. P x, 1)"}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    61
  
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    62
  Having produced a propositional formula, you can call the SAT solvers 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    63
  with the function @{ML "SatSolver.invoke_solver"}. 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    64
  For example
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    65
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    66
  @{ML_response_fake [display,gray]
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    67
  "SatSolver.invoke_solver \"dpll\" form"
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    68
  "SatSolver.SATISFIABLE ass"}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    69
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    70
  determines that the formula @{ML form} is satisfiable. If we inspect
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    71
  the returned function @{text ass}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    72
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    73
  @{ML_response [display,gray]
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    74
"let
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    75
  val SatSolver.SATISFIABLE ass = SatSolver.invoke_solver \"dpll\" form
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    76
in 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    77
  (ass 1, ass 2, ass 3)
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    78
end"
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    79
  "(SOME true, SOME true, NONE)"}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    80
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    81
  we obtain a possible assignment for the variables @{text "A"} and @{text "B"}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    82
  that makes the formula satisfiable. 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    83
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    84
  If we instead invoke the SAT solver with the string @{text [quotes] "auto"}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    85
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    86
  @{ML [display,gray] "SatSolver.invoke_solver \"auto\" form"}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    87
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    88
  several external SAT solvers will be tried (if they are installed) and
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    89
  the default is the internal SAT solver @{text [quotes] "dpll"}.
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    90
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    91
  There are also two tactics that make use of the SAT solvers. One 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    92
  is the tactic @{ML sat_tac in sat}. For example 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    93
*}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    94
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    95
lemma "True"
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    96
apply(tactic {* sat.sat_tac 1 *})
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    97
done
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    98
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    99
text {*
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   100
  \begin{readmore} 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   101
  The interface for the external SAT solvers is implemented
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   102
  in @{ML_file "HOL/Tools/sat_solver.ML"}. This file contains also a simple
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   103
  SAT solver based on the DPLL algorithm. The tactics for SAT solvers are
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   104
  implemented in @{ML_file "HOL/Tools/sat_funcs.ML"} Functions concerning
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   105
  propositional formulas are implemented in @{ML_file
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   106
  "HOL/Tools/prop_logic.ML"}. Tables used in the translation function are
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   107
  implemented in @{ML_file "Pure/General/table.ML"}.  
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   108
  \end{readmore}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   109
*}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   110
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   111
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   112
end