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