src/HOL/Tools/SMT2/smt2_solver.ML
author blanchet
Fri May 16 19:13:50 2014 +0200 (2014-05-16)
changeset 56981 3ef45ce002b5
parent 56132 64eeda68e693
child 57156 3546a67226ea
permissions -rw-r--r--
honor original format of conjecture or hypotheses in Z3-to-Isar proofs
     1 (*  Title:      HOL/Tools/SMT2/smt2_solver.ML
     2     Author:     Sascha Boehme, TU Muenchen
     3 
     4 SMT solvers registry and SMT tactic.
     5 *)
     6 
     7 signature SMT2_SOLVER =
     8 sig
     9   (*configuration*)
    10   datatype outcome = Unsat | Sat | Unknown
    11   type solver_config = {
    12     name: string,
    13     class: Proof.context -> SMT2_Util.class,
    14     avail: unit -> bool,
    15     command: unit -> string list,
    16     options: Proof.context -> string list,
    17     default_max_relevant: int,
    18     can_filter: bool,
    19     outcome: string -> string list -> outcome * string list,
    20     cex_parser: (Proof.context -> SMT2_Translate.replay_data -> string list ->
    21       term list * term list) option,
    22     replay: (Proof.context -> SMT2_Translate.replay_data -> string list ->
    23       ((int * (int * thm)) list * Z3_New_Proof.z3_step list) * thm) option}
    24 
    25   (*registry*)
    26   val add_solver: solver_config -> theory -> theory
    27   val default_max_relevant: Proof.context -> string -> int
    28 
    29   (*filter*)
    30   val smt2_filter: Proof.context -> thm -> ('a * (int option * thm)) list -> int -> Time.time ->
    31     {outcome: SMT2_Failure.failure option, rewrite_rules: thm list, conjecture_id: int,
    32      prem_ids: int list, helper_ids: (int * thm) list, fact_ids: (int * ('a * thm)) list,
    33      z3_proof: Z3_New_Proof.z3_step list}
    34 
    35   (*tactic*)
    36   val smt2_tac: Proof.context -> thm list -> int -> tactic
    37   val smt2_tac': Proof.context -> thm list -> int -> tactic
    38 end
    39 
    40 structure SMT2_Solver: SMT2_SOLVER =
    41 struct
    42 
    43 
    44 (* interface to external solvers *)
    45 
    46 local
    47 
    48 fun make_command command options problem_path proof_path =
    49   "(exec 2>&1;" :: map File.shell_quote (command () @ options) @
    50   [File.shell_path problem_path, ")", ">", File.shell_path proof_path]
    51   |> space_implode " "
    52 
    53 fun with_trace ctxt msg f x =
    54   let val _ = SMT2_Config.trace_msg ctxt (fn () => msg) ()
    55   in f x end
    56 
    57 fun run ctxt name mk_cmd input =
    58   (case SMT2_Config.certificates_of ctxt of
    59     NONE =>
    60       if not (SMT2_Config.is_available ctxt name) then
    61         error ("The SMT solver " ^ quote name ^ " is not installed")
    62       else if Config.get ctxt SMT2_Config.debug_files = "" then
    63         with_trace ctxt ("Invoking SMT solver " ^ quote name ^ " ...") (Cache_IO.run mk_cmd) input
    64       else
    65         let
    66           val base_path = Path.explode (Config.get ctxt SMT2_Config.debug_files)
    67           val in_path = Path.ext "smt2_in" base_path
    68           val out_path = Path.ext "smt2_out" base_path
    69         in Cache_IO.raw_run mk_cmd input in_path out_path end
    70   | SOME certs =>
    71       (case Cache_IO.lookup certs input of
    72         (NONE, key) =>
    73           if Config.get ctxt SMT2_Config.read_only_certificates then
    74             error ("Bad certificate cache: missing certificate")
    75           else
    76             Cache_IO.run_and_cache certs key mk_cmd input
    77       | (SOME output, _) =>
    78           with_trace ctxt ("Using cached certificate from " ^
    79             File.shell_path (Cache_IO.cache_path_of certs) ^ " ...") I output))
    80 
    81 (* Z3 returns 1 if "get-model" or "get-model" fails *)
    82 val normal_return_codes = [0, 1]
    83 
    84 fun run_solver ctxt name mk_cmd input =
    85   let
    86     fun pretty tag ls = Pretty.string_of (Pretty.big_list tag (map Pretty.str ls))
    87 
    88     val _ = SMT2_Config.trace_msg ctxt (pretty "Problem:" o split_lines) input
    89 
    90     val {redirected_output = res, output = err, return_code} =
    91       SMT2_Config.with_timeout ctxt (run ctxt name mk_cmd) input
    92     val _ = SMT2_Config.trace_msg ctxt (pretty "Solver:") err
    93 
    94     val output = fst (take_suffix (equal "") res)
    95     val _ = SMT2_Config.trace_msg ctxt (pretty "Result:") output
    96 
    97     val _ = member (op =) normal_return_codes return_code orelse
    98       raise SMT2_Failure.SMT (SMT2_Failure.Abnormal_Termination return_code)
    99   in output end
   100 
   101 fun trace_assms ctxt =
   102   SMT2_Config.trace_msg ctxt (Pretty.string_of o
   103     Pretty.big_list "Assertions:" o map (Display.pretty_thm ctxt o snd))
   104 
   105 fun trace_replay_data ({context = ctxt, typs, terms, ...} : SMT2_Translate.replay_data) =
   106   let
   107     fun pretty_eq n p = Pretty.block [Pretty.str n, Pretty.str " = ", p]
   108     fun p_typ (n, T) = pretty_eq n (Syntax.pretty_typ ctxt T)
   109     fun p_term (n, t) = pretty_eq n (Syntax.pretty_term ctxt t)
   110   in
   111     SMT2_Config.trace_msg ctxt (fn () =>
   112       Pretty.string_of (Pretty.big_list "Names:" [
   113         Pretty.big_list "sorts:" (map p_typ (Symtab.dest typs)),
   114         Pretty.big_list "functions:" (map p_term (Symtab.dest terms))])) ()
   115   end
   116 
   117 in
   118 
   119 fun invoke name command ithms ctxt =
   120   let
   121     val options = SMT2_Config.solver_options_of ctxt
   122     val comments = [space_implode " " options]
   123 
   124     val (str, replay_data as {context = ctxt', ...}) =
   125       ithms
   126       |> tap (trace_assms ctxt)
   127       |> SMT2_Translate.translate ctxt comments
   128       ||> tap trace_replay_data
   129   in (run_solver ctxt' name (make_command command options) str, replay_data) end
   130 
   131 end
   132 
   133 
   134 (* configuration *)
   135 
   136 datatype outcome = Unsat | Sat | Unknown
   137 
   138 type solver_config = {
   139   name: string,
   140   class: Proof.context -> SMT2_Util.class,
   141   avail: unit -> bool,
   142   command: unit -> string list,
   143   options: Proof.context -> string list,
   144   default_max_relevant: int,
   145   can_filter: bool,
   146   outcome: string -> string list -> outcome * string list,
   147   cex_parser: (Proof.context -> SMT2_Translate.replay_data -> string list ->
   148     term list * term list) option,
   149   replay: (Proof.context -> SMT2_Translate.replay_data -> string list ->
   150     ((int * (int * thm)) list * Z3_New_Proof.z3_step list) * thm) option}
   151 
   152 
   153 (* check well-sortedness *)
   154 
   155 val has_topsort = Term.exists_type (Term.exists_subtype (fn
   156     TFree (_, []) => true
   157   | TVar (_, []) => true
   158   | _ => false))
   159 
   160 (* top sorts cause problems with atomization *)
   161 fun check_topsort ctxt thm =
   162   if has_topsort (Thm.prop_of thm) then (SMT2_Normalize.drop_fact_warning ctxt thm; TrueI) else thm
   163 
   164 
   165 (* registry *)
   166 
   167 type solver_info = {
   168   command: unit -> string list,
   169   default_max_relevant: int,
   170   can_filter: bool,
   171   finish: Proof.context -> SMT2_Translate.replay_data -> string list ->
   172     ((int * (int * thm)) list * Z3_New_Proof.z3_step list) * thm}
   173 
   174 structure Solvers = Generic_Data
   175 (
   176   type T = solver_info Symtab.table
   177   val empty = Symtab.empty
   178   val extend = I
   179   fun merge data = Symtab.merge (K true) data
   180 )
   181 
   182 local
   183   fun finish outcome cex_parser replay ocl outer_ctxt
   184       (replay_data as {context = ctxt, ...} : SMT2_Translate.replay_data) output =
   185     (case outcome output of
   186       (Unsat, ls) =>
   187         if not (Config.get ctxt SMT2_Config.oracle) andalso is_some replay
   188         then the replay outer_ctxt replay_data ls
   189         else (([], []), ocl ())
   190     | (result, ls) =>
   191         let val (ts, us) = (case cex_parser of SOME f => f ctxt replay_data ls | _ => ([], []))
   192         in
   193           raise SMT2_Failure.SMT (SMT2_Failure.Counterexample {
   194             is_real_cex = (result = Sat),
   195             free_constraints = ts,
   196             const_defs = us})
   197         end)
   198 
   199   val cfalse = Thm.cterm_of @{theory} (@{const Trueprop} $ @{const False})
   200 in
   201 
   202 fun add_solver ({name, class, avail, command, options, default_max_relevant, can_filter,
   203     outcome, cex_parser, replay} : solver_config) =
   204   let
   205     fun solver ocl = {
   206       command = command,
   207       default_max_relevant = default_max_relevant,
   208       can_filter = can_filter,
   209       finish = finish (outcome name) cex_parser replay ocl}
   210 
   211     val info = {name = name, class = class, avail = avail, options = options}
   212   in
   213     Thm.add_oracle (Binding.name name, K cfalse) #-> (fn (_, ocl) =>
   214     Context.theory_map (Solvers.map (Symtab.update_new (name, solver ocl)))) #>
   215     Context.theory_map (SMT2_Config.add_solver info)
   216   end
   217 
   218 end
   219 
   220 fun get_info ctxt name = the (Symtab.lookup (Solvers.get (Context.Proof ctxt)) name)
   221 
   222 fun name_and_info_of ctxt =
   223   let val name = SMT2_Config.solver_of ctxt
   224   in (name, get_info ctxt name) end
   225 
   226 fun apply_solver ctxt wthms0 =
   227   let
   228     val wthms = map (apsnd (check_topsort ctxt)) wthms0
   229     val (name, {command, finish, ...}) = name_and_info_of ctxt
   230     val (output, replay_data) = invoke name command (SMT2_Normalize.normalize ctxt wthms) ctxt
   231   in (finish ctxt replay_data output, replay_data) end
   232 
   233 val default_max_relevant = #default_max_relevant oo get_info
   234 val can_filter = #can_filter o snd o name_and_info_of 
   235 
   236 
   237 (* filter *)
   238 
   239 val no_id = ~1
   240 
   241 fun smt2_filter ctxt goal xwfacts i time_limit =
   242   let
   243     val ctxt =
   244       ctxt
   245       |> Config.put SMT2_Config.oracle false
   246       |> Config.put SMT2_Config.filter_only_facts true
   247       |> Config.put SMT2_Config.timeout (Time.toReal time_limit)
   248 
   249     val ({context = ctxt, prems, concl, ...}, _) = Subgoal.focus ctxt i goal
   250     fun negate ct = Thm.dest_comb ct ||> Thm.apply @{cterm Not} |-> Thm.apply
   251     val cprop =
   252       (case try negate (Thm.rhs_of (SMT2_Normalize.atomize_conv ctxt concl)) of
   253         SOME ct => ct
   254       | NONE => raise SMT2_Failure.SMT (SMT2_Failure.Other_Failure "goal is not a HOL term"))
   255 
   256     val wconjecture = (NONE, Thm.assume cprop)
   257     val wprems = map (pair NONE) prems
   258     val wfacts = map snd xwfacts
   259     val wthms = wconjecture :: wprems @ wfacts
   260     val iwthms = map_index I wthms
   261 
   262     val conjecture_i = 0
   263     val prems_i = 1
   264     val facts_i = prems_i + length wprems
   265   in
   266     wthms
   267     |> apply_solver ctxt
   268     |> (fn (((iidths0, z3_proof), _), {rewrite_rules, ...}) =>
   269       let
   270         val iidths = if can_filter ctxt then iidths0 else map (apsnd (apfst (K no_id))) iwthms
   271         fun id_of_index i = the_default no_id (Option.map fst (AList.lookup (op =) iidths i))
   272       in
   273         {outcome = NONE,
   274          rewrite_rules = rewrite_rules,
   275          conjecture_id = id_of_index conjecture_i,
   276          prem_ids = map id_of_index (prems_i upto facts_i - 1),
   277          helper_ids = map_filter (try (fn (~1, idth) => idth)) iidths,
   278          fact_ids = map_filter (fn (i, (id, _)) =>
   279            try (apsnd (apsnd snd o nth xwfacts)) (id, i - facts_i)) iidths,
   280          z3_proof = z3_proof}
   281       end)
   282   end
   283   handle SMT2_Failure.SMT fail => {outcome = SOME fail, rewrite_rules = [], conjecture_id = no_id,
   284     prem_ids = [], helper_ids = [], fact_ids = [], z3_proof = []}
   285 
   286 
   287 (* SMT tactic *)
   288 
   289 local
   290   fun trace_assumptions ctxt wfacts iidths =
   291     let val used = map_filter (try (snd o nth wfacts) o fst) iidths in
   292       if Config.get ctxt SMT2_Config.trace_used_facts andalso length wfacts > 0 then
   293         tracing (Pretty.string_of (Pretty.big_list "SMT used facts:"
   294           (map (Display.pretty_thm ctxt) used)))
   295       else ()
   296     end
   297 
   298   fun solve ctxt wfacts =
   299     wfacts
   300     |> apply_solver ctxt
   301     |> fst
   302     |>> apfst (trace_assumptions ctxt wfacts)
   303     |> snd
   304 
   305   fun str_of ctxt fail =
   306     "Solver " ^ SMT2_Config.solver_of ctxt ^ ": " ^ SMT2_Failure.string_of_failure ctxt fail
   307 
   308   fun safe_solve ctxt wfacts = SOME (solve ctxt wfacts)
   309     handle
   310       SMT2_Failure.SMT (fail as SMT2_Failure.Counterexample _) =>
   311         (SMT2_Config.verbose_msg ctxt (str_of ctxt) fail; NONE)
   312     | SMT2_Failure.SMT (fail as SMT2_Failure.Time_Out) =>
   313         error ("SMT: Solver " ^ quote (SMT2_Config.solver_of ctxt) ^ ": " ^
   314           SMT2_Failure.string_of_failure ctxt fail ^ " (setting the " ^
   315           "configuration option " ^ quote (Config.name_of SMT2_Config.timeout) ^ " might help)")
   316     | SMT2_Failure.SMT fail => error (str_of ctxt fail)
   317 
   318   fun resolve (SOME thm) = rtac thm 1
   319     | resolve NONE = no_tac
   320 
   321   fun tac prove ctxt rules =
   322     CONVERSION (SMT2_Normalize.atomize_conv ctxt)
   323     THEN' rtac @{thm ccontr}
   324     THEN' SUBPROOF (fn {context = ctxt, prems, ...} =>
   325       resolve (prove ctxt (map (pair NONE) (rules @ prems)))) ctxt
   326 in
   327 
   328 val smt2_tac = tac safe_solve
   329 val smt2_tac' = tac (SOME oo solve)
   330 
   331 end
   332 
   333 end