src/HOL/Tools/Sledgehammer/sledgehammer_run.ML
author blanchet
Tue, 21 Dec 2010 03:56:51 +0100
changeset 41583 66edbd0f7a2e
parent 41564 558afd8b94d6
child 41680 3214c39777ab
permissions -rw-r--r--
added "smt_triggers" option to experiment with triggers in Sledgehammer;
renamings (for consistency)
     1 (*  Title:      HOL/Tools/Sledgehammer/sledgehammer_run.ML
     2     Author:     Fabian Immler, TU Muenchen
     3     Author:     Makarius
     4     Author:     Jasmin Blanchette, TU Muenchen
     5 
     6 Sledgehammer's heart.
     7 *)
     8 
     9 signature SLEDGEHAMMER_RUN =
    10 sig
    11   type relevance_override = Sledgehammer_Filter.relevance_override
    12   type minimize_command = Sledgehammer_ATP_Reconstruct.minimize_command
    13   type params = Sledgehammer_Provers.params
    14   type prover = Sledgehammer_Provers.prover
    15 
    16   val auto_minimize_min_facts : int Unsynchronized.ref
    17   val get_minimizing_prover : Proof.context -> bool -> string -> prover
    18   val run_sledgehammer :
    19     params -> bool -> int -> relevance_override -> (string -> minimize_command)
    20     -> Proof.state -> bool * Proof.state
    21 end;
    22 
    23 structure Sledgehammer_Run : SLEDGEHAMMER_RUN =
    24 struct
    25 
    26 open Sledgehammer_Util
    27 open Sledgehammer_Filter
    28 open Sledgehammer_ATP_Translate
    29 open Sledgehammer_Provers
    30 open Sledgehammer_Minimize
    31 
    32 fun prover_description ctxt ({verbose, blocking, ...} : params) name num_facts i
    33                        n goal =
    34   quote name ^
    35   (if verbose then
    36      " with " ^ string_of_int num_facts ^ " fact" ^ plural_s num_facts
    37    else
    38      "") ^
    39   " on " ^ (if n = 1 then "goal" else "subgoal " ^ string_of_int i) ^ ":" ^
    40   (if blocking then
    41      ""
    42    else
    43      "\n" ^ Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i)))
    44 
    45 val auto_minimize_min_facts = Unsynchronized.ref (!binary_min_facts)
    46 
    47 fun get_minimizing_prover ctxt auto name (params as {debug, verbose, ...})
    48         minimize_command
    49         (problem as {state, subgoal, subgoal_count, facts, ...}) =
    50   get_prover ctxt auto name params minimize_command problem
    51   |> (fn result as {outcome, used_facts, run_time_in_msecs, message} =>
    52          if is_some outcome then
    53            result
    54          else
    55            let
    56              val (used_facts, message) =
    57                if length used_facts >= !auto_minimize_min_facts then
    58                  minimize_facts name params (not verbose) subgoal subgoal_count
    59                      state
    60                      (filter_used_facts used_facts
    61                           (map (apsnd single o untranslated_fact) facts))
    62                  |>> Option.map (map fst)
    63                else
    64                  (SOME used_facts, message)
    65            in
    66              case used_facts of
    67                SOME used_facts =>
    68                (if debug andalso not (null used_facts) then
    69                   facts ~~ (0 upto length facts - 1)
    70                   |> map (fn (fact, j) =>
    71                              fact |> untranslated_fact |> apsnd (K j))
    72                   |> filter_used_facts used_facts
    73                   |> map (fn ((name, _), j) => name ^ "@" ^ string_of_int j)
    74                   |> commas
    75                   |> enclose ("Fact" ^ plural_s (length facts) ^ " in " ^
    76                               quote name ^ " proof (of " ^
    77                               string_of_int (length facts) ^ "): ") "."
    78                   |> Output.urgent_message
    79                 else
    80                   ();
    81                 {outcome = NONE, used_facts = used_facts,
    82                  run_time_in_msecs = run_time_in_msecs, message = message})
    83              | NONE => result
    84            end)
    85 
    86 fun launch_prover
    87         (params as {debug, blocking, max_relevant, timeout, expect, ...})
    88         auto minimize_command only
    89         {state, goal, subgoal, subgoal_count, facts, smt_head} name =
    90   let
    91     val ctxt = Proof.context_of state
    92     val birth_time = Time.now ()
    93     val death_time = Time.+ (birth_time, timeout)
    94     val max_relevant =
    95       the_default (default_max_relevant_for_prover ctxt name) max_relevant
    96     val num_facts = length facts |> not only ? Integer.min max_relevant
    97     val desc =
    98       prover_description ctxt params name num_facts subgoal subgoal_count goal
    99     val problem =
   100       {state = state, goal = goal, subgoal = subgoal,
   101        subgoal_count = subgoal_count, facts = take num_facts facts,
   102        smt_head = smt_head}
   103     fun really_go () =
   104       problem
   105       |> get_minimizing_prover ctxt auto name params (minimize_command name)
   106       |> (fn {outcome, message, ...} =>
   107              (if is_some outcome then "none" else "some" (* sic *), message))
   108     fun go () =
   109       let
   110         val (outcome_code, message) =
   111           if debug then
   112             really_go ()
   113           else
   114             (really_go ()
   115              handle ERROR message => ("unknown", "Error: " ^ message ^ "\n")
   116                   | exn =>
   117                     if Exn.is_interrupt exn then
   118                       reraise exn
   119                     else
   120                       ("unknown", "Internal error:\n" ^
   121                                   ML_Compiler.exn_message exn ^ "\n"))
   122         val _ =
   123           (* The "expect" argument is deliberately ignored if the prover is
   124              missing so that the "Metis_Examples" can be processed on any
   125              machine. *)
   126           if expect = "" orelse outcome_code = expect orelse
   127              not (is_prover_installed ctxt name) then
   128             ()
   129           else if blocking then
   130             error ("Unexpected outcome: " ^ quote outcome_code ^ ".")
   131           else
   132             warning ("Unexpected outcome: " ^ quote outcome_code ^ ".");
   133       in (outcome_code = "some", message) end
   134   in
   135     if auto then
   136       let val (success, message) = TimeLimit.timeLimit timeout go () in
   137         (success, state |> success ? Proof.goal_message (fn () =>
   138              Pretty.chunks [Pretty.str "",
   139                             Pretty.mark Markup.hilite (Pretty.str message)]))
   140       end
   141     else if blocking then
   142       let val (success, message) = TimeLimit.timeLimit timeout go () in
   143         List.app Output.urgent_message
   144                  (Async_Manager.break_into_chunks [desc ^ "\n" ^ message]);
   145         (success, state)
   146       end
   147     else
   148       (Async_Manager.launch das_Tool birth_time death_time desc (snd o go);
   149        (false, state))
   150   end
   151 
   152 fun class_of_smt_solver ctxt name =
   153   ctxt |> select_smt_solver name
   154        |> SMT_Config.solver_class_of |> SMT_Utils.string_of_class
   155 
   156 (* Makes backtraces more transparent and might be more efficient as well. *)
   157 fun smart_par_list_map _ [] = []
   158   | smart_par_list_map f [x] = [f x]
   159   | smart_par_list_map f xs = Par_List.map f xs
   160 
   161 fun dest_SMT_Weighted_Fact (SMT_Weighted_Fact p) = p
   162   | dest_SMT_Weighted_Fact _ = raise Fail "dest_SMT_Weighted_Fact"
   163 
   164 (* FUDGE *)
   165 val auto_max_relevant_divisor = 2
   166 
   167 fun run_sledgehammer (params as {debug, blocking, provers, type_sys,
   168                                  relevance_thresholds, max_relevant, ...})
   169                      auto i (relevance_override as {only, ...}) minimize_command
   170                      state =
   171   if null provers then
   172     error "No prover is set."
   173   else case subgoal_count state of
   174     0 => (Output.urgent_message "No subgoal!"; (false, state))
   175   | n =>
   176     let
   177       val _ = Proof.assert_backward state
   178       val state =
   179         state |> Proof.map_context (Config.put SMT_Config.verbose debug)
   180       val ctxt = Proof.context_of state
   181       val thy = ProofContext.theory_of ctxt
   182       val {facts = chained_ths, goal, ...} = Proof.goal state
   183       val (_, hyp_ts, concl_t) = strip_subgoal goal i
   184       val no_dangerous_types = types_dangerous_types type_sys
   185       val _ = () |> not blocking ? kill_provers
   186       val _ = case find_first (not o is_prover_available ctxt) provers of
   187                 SOME name => error ("No such prover: " ^ name ^ ".")
   188               | NONE => ()
   189       val _ = if auto then () else Output.urgent_message "Sledgehammering..."
   190       val (smts, atps) = provers |> List.partition (is_smt_prover ctxt)
   191       fun launch_provers state get_facts translate maybe_smt_head provers =
   192         let
   193           val facts = get_facts ()
   194           val num_facts = length facts
   195           val facts = facts ~~ (0 upto num_facts - 1)
   196                       |> map (translate num_facts)
   197           val problem =
   198             {state = state, goal = goal, subgoal = i, subgoal_count = n,
   199              facts = facts,
   200              smt_head = maybe_smt_head
   201                   (fn () => map_filter (try dest_SMT_Weighted_Fact) facts) i}
   202           val launch = launch_prover params auto minimize_command only
   203         in
   204           if auto then
   205             fold (fn prover => fn (true, state) => (true, state)
   206                                 | (false, _) => launch problem prover)
   207                  provers (false, state)
   208           else
   209             provers
   210             |> (if blocking then smart_par_list_map else map) (launch problem)
   211             |> exists fst |> rpair state
   212         end
   213       fun get_facts label no_dangerous_types relevance_fudge provers =
   214         let
   215           val max_max_relevant =
   216             case max_relevant of
   217               SOME n => n
   218             | NONE =>
   219               0 |> fold (Integer.max o default_max_relevant_for_prover ctxt)
   220                         provers
   221                 |> auto ? (fn n => n div auto_max_relevant_divisor)
   222           val is_built_in_const =
   223             is_built_in_const_for_prover ctxt (hd provers)
   224         in
   225           relevant_facts ctxt no_dangerous_types relevance_thresholds
   226                          max_max_relevant is_built_in_const relevance_fudge
   227                          relevance_override chained_ths hyp_ts concl_t
   228           |> tap (fn facts =>
   229                      if debug then
   230                        label ^ plural_s (length provers) ^ ": " ^
   231                        (if null facts then
   232                           "Found no relevant facts."
   233                         else
   234                           "Including (up to) " ^ string_of_int (length facts) ^
   235                           " relevant fact" ^ plural_s (length facts) ^ ":\n" ^
   236                           (facts |> map (fst o fst) |> space_implode " ") ^ ".")
   237                        |> Output.urgent_message
   238                      else
   239                        ())
   240         end
   241       fun launch_atps (accum as (success, _)) =
   242         if success orelse null atps then
   243           accum
   244         else
   245           launch_provers state
   246               (get_facts "ATP" no_dangerous_types atp_relevance_fudge o K atps)
   247               (ATP_Translated_Fact oo K (translate_atp_fact ctxt o fst))
   248               (K (K NONE)) atps
   249       fun launch_smts (accum as (success, _)) =
   250         if success orelse null smts then
   251           accum
   252         else
   253           let
   254             val facts = get_facts "SMT solver" true smt_relevance_fudge smts
   255             val weight = SMT_Weighted_Fact oo weight_smt_fact thy
   256             fun smt_head facts =
   257               (if debug then curry (op o) SOME else try)
   258                   (SMT_Solver.smt_filter_head state (facts ()))
   259           in
   260             smts |> map (`(class_of_smt_solver ctxt))
   261                  |> AList.group (op =)
   262                  |> map (launch_provers state (K facts) weight smt_head o snd)
   263                  |> exists fst |> rpair state
   264           end
   265       fun launch_atps_and_smt_solvers () =
   266         [launch_atps, launch_smts]
   267         |> smart_par_list_map (fn f => f (false, state) |> K ())
   268         handle ERROR msg => (Output.urgent_message ("Error: " ^ msg); error msg)
   269     in
   270       (false, state)
   271       |> (if blocking then launch_atps #> not auto ? launch_smts
   272           else (fn p => Future.fork (tap launch_atps_and_smt_solvers) |> K p))
   273     end
   274 
   275 end;