CookBook/Recipes/ExternalSolver.thy
author Christian Urban <urbanc@in.tum.de>
Wed, 14 Jan 2009 23:44:14 +0000
changeset 72 7b8c4fe235aa
parent 61 64c9540f2f84
child 79 a53c7810e38b
permissions -rw-r--r--
added an antiquotation option [gray] for gray boxes around displays
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
61
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     1
theory ExternalSolver
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     2
imports "../Base"
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     3
begin
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     4
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     5
section {* Using an External Solver *} 
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     6
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     7
text {*
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     8
  {\bf Problem:}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
     9
  You want to use an external solver, say, because it is more efficient in
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    10
  deciding particular formulas than any Isabelle tactic.
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    11
  \smallskip
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    12
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    13
  {\bf Solution:} The easiest way to do this is writing an oracle.
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    14
  To yield results checked by Isabelle's kernel, one can reconstruct the
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    15
  proofs.
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    16
  \smallskip
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    17
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    18
  \begin{readmore}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    19
  A short introduction to oracles can be found in [isar-ref: no suitable label
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    20
  for section 3.11]. A simple example is given in 
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    21
  @{ML_file "FOL/ex/IffOracle"}.
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    22
  (TODO: add more references to the code)
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    23
  \end{readmore}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    24
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    25
  The general layout will be as follows. Given a goal G, we transform it into
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    26
  the syntactical respresentation of the external solver, and invoke the
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    27
  solver. The solver's result is then used inside the oracle to either return
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    28
  the proved goal or raise an exception meaning that the solver was unable to
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    29
  prove the goal.
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    30
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    31
  For communication with external programs, there are the primitives
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    32
  @{ML_text system} and @{ML_text system_out}, the latter of which captures
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    33
  the invoked program's output. For simplicity, here, we will use metis, an
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    34
  external solver included in the Isabelle destribution. Since it is written
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    35
  in ML, we can call it directly without the detour of invoking an external
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    36
  program.
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    37
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    38
  We will restrict ourselves to proving formulas of propositional logic, a
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    39
  task metis is very good at.
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    40
  *}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    41
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    42
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    43
ML {*
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    44
fun trans t =
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    45
  (case t of
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    46
    @{term Trueprop} $ t => trans t
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    47
  | @{term True} => Metis.Formula.True
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    48
  | @{term False} => Metis.Formula.False
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    49
  | @{term Not} $ t => Metis.Formula.Not (trans t)
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    50
  | @{term "op &"} $ t1 $ t2 => Metis.Formula.And (trans t1, trans t2)
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    51
  | @{term "op |"} $ t1 $ t2 => Metis.Formula.Or (trans t1, trans t2)
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    52
  | @{term "op -->"} $ t1 $ t2 => Metis.Formula.Imp (trans t1, trans t2)
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    53
  | @{term "op = :: bool => bool => bool"} $ t1 $ t2 => 
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    54
      Metis.Formula.Iff (trans t1, trans t2)
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    55
  | Free (n, @{typ bool}) => Metis.Formula.Atom (n, [])
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    56
  | _ => error "inacceptable term")
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    57
*}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    58
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    59
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    60
ML {*
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    61
fun solve f =
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    62
  let
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    63
    open Metis
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    64
    fun fromLiterals fms = LiteralSet.fromList (map Literal.fromFormula fms)
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    65
    fun fromClause fm = fromLiterals (Formula.stripDisj fm)
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    66
    fun fromCnf fm = map fromClause (Formula.stripConj fm)
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    67
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    68
    val mk_cnfs = map fromCnf o Normalize.cnf o Formula.Not
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    69
    fun refute cls =
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    70
      let val res = Resolution.new Resolution.default (map Thm.axiom cls)
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    71
      in
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    72
        (case Resolution.loop res of
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    73
          Resolution.Contradiction _ => true
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    74
        | Resolution.Satisfiable _ => false)
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    75
      end
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    76
  in List.all refute (mk_cnfs f) end
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    77
*}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    78
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    79
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    80
ML {*
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    81
fun prop_dp (thy, t) = if solve (trans t) then Thm.cterm_of thy t 
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    82
  else error "Proof failed."
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    83
*}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    84
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    85
oracle prop_oracle = prop_dp
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    86
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    87
ML {*
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    88
fun prop_oracle_tac ctxt = 
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    89
  SUBGOAL (fn (goal, i) => 
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    90
    (case try prop_oracle (ProofContext.theory_of ctxt, goal) of
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    91
      SOME thm => rtac thm i
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    92
    | NONE => no_tac))
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    93
*}
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    94
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    95
method_setup prop_oracle = {*
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    96
  Method.ctxt_args (fn ctxt => Method.SIMPLE_METHOD' (prop_oracle_tac ctxt))
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    97
*} "Oracle-based decision procedure for propositional logic"
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    98
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
    99
lemma "p \<or> \<not>p"
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
   100
  by prop_oracle
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
   101
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
   102
lemma "((p \<longrightarrow> q) \<longrightarrow> p) \<longrightarrow> p"
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
   103
  by prop_oracle
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
   104
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
   105
lemma "\<forall>x::nat. x \<ge> 0"
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
   106
  sorry
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
   107
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
   108
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
   109
(* TODO: proof reconstruction *)
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
   110
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
   111
64c9540f2f84 Added four recipes.
boehmes
parents:
diff changeset
   112
end