CookBook/Recipes/Sat.thy
author Christian Urban <urbanc@in.tum.de>
Wed, 18 Mar 2009 03:27:15 +0100
changeset 185 043ef82000b4
parent 184 c7f04a008c9c
permissions -rw-r--r--
some polishing
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
181
5baaabe1ab92 updated to new method_setup
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
     6
section {* SAT Solvers\label{rec:sat} *}
180
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
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    19
  a result indicating that the formula is either satisfiable, unsatisfiable or
180
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
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    24
  The function  @{ML  PropLogic.prop_formula_of_term} translates an Isabelle 
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    25
  term into a propositional formula. Let
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    26
  us illustrate this function by translating @{term "A \<and> \<not>A \<or> B"}. 
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    27
  The function will return a propositional formula and a table. Suppose 
180
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
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    30
ML{*val (pform, table) = 
180
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 {*
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    34
  then the resulting propositional formula @{ML pform} is 
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    35
  
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    36
  @{ML [display] "Or (And (BoolVar 1, Not (BoolVar 1)), BoolVar 2)" in PropLogic} 
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    37
  
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    38
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    39
  where indices are assigned for the variables 
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    40
  @{text "A"} and @{text "B"}, respectively. This assignment is recorded 
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    41
  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
    42
  (appropriately updated) in the result. In the case above the
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    43
  input table is empty (i.e.~@{ML Termtab.empty}) and the output table is
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    44
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    45
  @{ML_response_fake [display,gray]
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    46
  "Termtab.dest table"
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    47
  "[(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
    48
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    49
  An index is also produced whenever the translation
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    50
  function cannot find an appropriate propositional formula for a term. 
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    51
  Attempting to translate @{term "\<forall>x::nat. P x"}
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    52
*}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    53
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    54
ML{*val (pform', table') = 
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    55
       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
    56
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    57
text {*
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    58
  returns @{ML "BoolVar 1" in PropLogic} for  @{ML pform'} and the table 
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    59
  @{ML table'} is:
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    60
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    61
  @{ML_response_fake [display,gray]
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    62
  "map (apfst (Syntax.string_of_term @{context})) (Termtab.dest table')"
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    63
  "(\<forall>x. P x, 1)"}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    64
  
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    65
  We used some pretty printing scaffolding to see better what the output is.
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    66
 
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    67
  Having produced a propositional formula, you can now call the SAT solvers 
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    68
  with the function @{ML "SatSolver.invoke_solver"}. For example
180
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
  @{ML_response_fake [display,gray]
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    71
  "SatSolver.invoke_solver \"dpll\" pform"
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    72
  "SatSolver.SATISFIABLE assg"}
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    73
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    74
  determines that the formula @{ML pform} is satisfiable. If we inspect
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    75
  the returned function @{text assg}
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    76
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    77
  @{ML_response [display,gray]
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    78
"let
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    79
  val SatSolver.SATISFIABLE assg = SatSolver.invoke_solver \"dpll\" pform
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    80
in 
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    81
  (assg 1, assg 2, assg 3)
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    82
end"
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    83
  "(SOME true, SOME true, NONE)"}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    84
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    85
  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
    86
  that makes the formula satisfiable. 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    87
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    88
  Note that we invoked the SAT solver with the string @{text [quotes] dpll}. 
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    89
  This string specifies which SAT solver is invoked (in this case the internal
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    90
  one). If instead you invoke the SAT solver with the string @{text [quotes] "auto"}
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    91
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    92
  @{ML [display,gray] "SatSolver.invoke_solver \"auto\" pform"}
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    93
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    94
  several external SAT solvers will be tried (assuming they are installed). 
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    95
  If no external SAT solver is installed, then the default is 
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    96
  @{text [quotes] "dpll"}.
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    97
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
    98
  There are also two tactics that make use of SAT solvers. One 
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
    99
  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
   100
*}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   101
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   102
lemma "True"
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   103
apply(tactic {* sat.sat_tac 1 *})
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   104
done
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   105
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   106
text {*
185
043ef82000b4 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 184
diff changeset
   107
  However, for proving anything more exciting you have to use a SAT solver
184
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   108
  that can produce a proof. The internal one is not usuable for this. 
c7f04a008c9c some polishing
Christian Urban <urbanc@in.tum.de>
parents: 181
diff changeset
   109
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   110
  \begin{readmore} 
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   111
  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
   112
  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
   113
  SAT solver based on the DPLL algorithm. The tactics for SAT solvers are
181
5baaabe1ab92 updated to new method_setup
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   114
  implemented in @{ML_file "HOL/Tools/sat_funcs.ML"}. Functions concerning
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   115
  propositional formulas are implemented in @{ML_file
181
5baaabe1ab92 updated to new method_setup
Christian Urban <urbanc@in.tum.de>
parents: 180
diff changeset
   116
  "HOL/Tools/prop_logic.ML"}. The tables used in the translation function are
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   117
  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
   118
  \end{readmore}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   119
*}
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   120
127
74846cb0fff9 updated and added two tentative recipes
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   121
180
9c25418db6f0 added a recipy about SAT solvers
Christian Urban <urbanc@in.tum.de>
parents: 127
diff changeset
   122
end