src/HOL/Tools/Sledgehammer/sledgehammer_run.ML
author blanchet
Mon, 23 Jan 2012 17:40:32 +0100
changeset 47148 0b8b73b49848
parent 47129 e2e52c7d25c9
child 47168 cac402c486b0
permissions -rw-r--r--
renamed two files to make room for a new file
blanchet@41335
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_run.ML
wenzelm@28477
     2
    Author:     Fabian Immler, TU Muenchen
wenzelm@32996
     3
    Author:     Makarius
blanchet@35967
     4
    Author:     Jasmin Blanchette, TU Muenchen
wenzelm@28477
     5
blanchet@38255
     6
Sledgehammer's heart.
wenzelm@28477
     7
*)
wenzelm@28477
     8
blanchet@41335
     9
signature SLEDGEHAMMER_RUN =
wenzelm@28477
    10
sig
blanchet@47148
    11
  type minimize_command = ATP_Proof_Reconstruct.minimize_command
blanchet@39232
    12
  type relevance_override = Sledgehammer_Filter.relevance_override
blanchet@43862
    13
  type mode = Sledgehammer_Provers.mode
blanchet@41335
    14
  type params = Sledgehammer_Provers.params
blanchet@41511
    15
  type prover = Sledgehammer_Provers.prover
blanchet@39733
    16
blanchet@43861
    17
  val someN : string
blanchet@43861
    18
  val noneN : string
blanchet@43861
    19
  val timeoutN : string
blanchet@43861
    20
  val unknownN : string
blanchet@45763
    21
  val auto_minimize_min_facts : int Config.T
blanchet@45763
    22
  val auto_minimize_max_time : real Config.T
blanchet@43862
    23
  val get_minimizing_prover : Proof.context -> mode -> string -> prover
blanchet@38290
    24
  val run_sledgehammer :
blanchet@46391
    25
    params -> mode -> int -> relevance_override
blanchet@46391
    26
    -> ((string * string list) list -> string -> minimize_command)
blanchet@43861
    27
    -> Proof.state -> bool * (string * Proof.state)
wenzelm@28477
    28
end;
wenzelm@28477
    29
blanchet@41335
    30
structure Sledgehammer_Run : SLEDGEHAMMER_RUN =
wenzelm@28477
    31
struct
wenzelm@28477
    32
blanchet@43926
    33
open ATP_Util
blanchet@47148
    34
open ATP_Problem_Generate
blanchet@47148
    35
open ATP_Proof_Reconstruct
blanchet@38257
    36
open Sledgehammer_Util
blanchet@39232
    37
open Sledgehammer_Filter
blanchet@41335
    38
open Sledgehammer_Provers
blanchet@41339
    39
open Sledgehammer_Minimize
blanchet@40253
    40
blanchet@43861
    41
val someN = "some"
blanchet@43861
    42
val noneN = "none"
blanchet@43861
    43
val timeoutN = "timeout"
blanchet@43861
    44
val unknownN = "unknown"
blanchet@43861
    45
blanchet@43861
    46
val ordered_outcome_codes = [someN, unknownN, timeoutN, noneN]
blanchet@43861
    47
blanchet@43861
    48
fun max_outcome_code codes =
blanchet@43861
    49
  NONE
blanchet@43861
    50
  |> fold (fn candidate =>
blanchet@43861
    51
              fn accum as SOME _ => accum
blanchet@43861
    52
               | NONE => if member (op =) codes candidate then SOME candidate
blanchet@43861
    53
                         else NONE)
blanchet@43861
    54
          ordered_outcome_codes
blanchet@43861
    55
  |> the_default unknownN
blanchet@43861
    56
blanchet@41456
    57
fun prover_description ctxt ({verbose, blocking, ...} : params) name num_facts i
blanchet@41337
    58
                       n goal =
blanchet@43846
    59
  (name,
blanchet@43846
    60
   (if verbose then
blanchet@43846
    61
      " with " ^ string_of_int num_facts ^ " fact" ^ plural_s num_facts
blanchet@43846
    62
    else
blanchet@43846
    63
      "") ^
blanchet@43846
    64
   " on " ^ (if n = 1 then "goal" else "subgoal " ^ string_of_int i) ^
blanchet@46250
    65
   (if blocking then "."
blanchet@46250
    66
    else "\n" ^ Syntax.string_of_term ctxt (Thm.term_of (Thm.cprem_of goal i))))
blanchet@41337
    67
blanchet@45763
    68
val auto_minimize_min_facts =
blanchet@45763
    69
  Attrib.setup_config_int @{binding sledgehammer_auto_minimize_min_facts}
blanchet@43809
    70
      (fn generic => Config.get_generic generic binary_min_facts)
blanchet@45763
    71
val auto_minimize_max_time =
blanchet@45763
    72
  Attrib.setup_config_real @{binding sledgehammer_auto_minimize_max_time}
blanchet@45763
    73
                           (K 5.0)
blanchet@43893
    74
blanchet@46391
    75
fun adjust_reconstructor_params override_params
blanchet@47129
    76
        ({debug, verbose, overlord, blocking, provers, type_enc, strict,
blanchet@46391
    77
         lam_trans, relevance_thresholds, max_relevant, max_mono_iters,
blanchet@46578
    78
         max_new_mono_instances, isar_proof, isar_shrink_factor, slice,
blanchet@46578
    79
         minimize, timeout, preplay_timeout, expect} : params) =
blanchet@46391
    80
  let
blanchet@46391
    81
    fun lookup_override name default_value =
blanchet@46391
    82
      case AList.lookup (op =) override_params name of
blanchet@46391
    83
        SOME [s] => SOME s
blanchet@46391
    84
      | _ => default_value
blanchet@46391
    85
    (* Only those options that reconstructors are interested in are considered
blanchet@46391
    86
       here. *)
blanchet@46391
    87
    val type_enc = lookup_override "type_enc" type_enc
blanchet@46391
    88
    val lam_trans = lookup_override "lam_trans" lam_trans
blanchet@46391
    89
  in
blanchet@46391
    90
    {debug = debug, verbose = verbose, overlord = overlord, blocking = blocking,
blanchet@47129
    91
     provers = provers, type_enc = type_enc, strict = strict,
blanchet@46391
    92
     lam_trans = lam_trans, max_relevant = max_relevant,
blanchet@46391
    93
     relevance_thresholds = relevance_thresholds,
blanchet@46391
    94
     max_mono_iters = max_mono_iters,
blanchet@46391
    95
     max_new_mono_instances = max_new_mono_instances, isar_proof = isar_proof,
blanchet@46578
    96
     isar_shrink_factor = isar_shrink_factor, slice = slice,
blanchet@46578
    97
     minimize = minimize, timeout = timeout, preplay_timeout = preplay_timeout,
blanchet@46578
    98
     expect = expect}
blanchet@46391
    99
  end
blanchet@46391
   100
blanchet@46578
   101
fun minimize ctxt mode name
blanchet@46578
   102
             (params as {debug, verbose, isar_proof, minimize, ...})
blanchet@44005
   103
             ({state, subgoal, subgoal_count, facts, ...} : prover_problem)
blanchet@46231
   104
             (result as {outcome, used_facts, run_time, preplay, message,
blanchet@46231
   105
                         message_tail} : prover_result) =
blanchet@44150
   106
  if is_some outcome orelse null used_facts then
blanchet@44005
   107
    result
blanchet@44005
   108
  else
blanchet@44005
   109
    let
blanchet@44005
   110
      val num_facts = length used_facts
blanchet@46578
   111
      val ((perhaps_minimize, (minimize_name, params)), preplay) =
blanchet@44005
   112
        if mode = Normal then
blanchet@45763
   113
          if num_facts >= Config.get ctxt auto_minimize_min_facts then
blanchet@46391
   114
            ((true, (name, params)), preplay)
blanchet@44005
   115
          else
blanchet@44006
   116
            let
blanchet@46231
   117
              fun can_min_fast_enough time =
blanchet@46231
   118
                0.001
blanchet@46231
   119
                * Real.fromInt ((num_facts + 1) * Time.toMilliseconds time)
blanchet@45763
   120
                <= Config.get ctxt auto_minimize_max_time
blanchet@46578
   121
              fun prover_fast_enough () = can_min_fast_enough run_time
blanchet@44006
   122
            in
blanchet@44006
   123
              if isar_proof then
blanchet@46578
   124
                ((prover_fast_enough (), (name, params)), preplay)
blanchet@44006
   125
              else
blanchet@44006
   126
                let val preplay = preplay () in
blanchet@44006
   127
                  (case preplay of
blanchet@46391
   128
                     Played (reconstr, timeout) =>
blanchet@46231
   129
                     if can_min_fast_enough timeout then
blanchet@46432
   130
                       (true, extract_reconstructor params reconstr
blanchet@46391
   131
                              ||> (fn override_params =>
blanchet@46391
   132
                                      adjust_reconstructor_params
blanchet@46391
   133
                                          override_params params))
blanchet@44006
   134
                     else
blanchet@46578
   135
                       (prover_fast_enough (), (name, params))
blanchet@46578
   136
                   | _ => (prover_fast_enough (), (name, params)),
blanchet@44006
   137
                   K preplay)
blanchet@44006
   138
                end
blanchet@44005
   139
            end
blanchet@44005
   140
        else
blanchet@46391
   141
          ((false, (name, params)), preplay)
blanchet@46578
   142
      val minimize = minimize |> the_default perhaps_minimize
blanchet@44102
   143
      val (used_facts, (preplay, message, _)) =
blanchet@44005
   144
        if minimize then
blanchet@44005
   145
          minimize_facts minimize_name params (not verbose) subgoal
blanchet@44005
   146
                         subgoal_count state
blanchet@44005
   147
                         (filter_used_facts used_facts
blanchet@44005
   148
                              (map (apsnd single o untranslated_fact) facts))
blanchet@44005
   149
          |>> Option.map (map fst)
blanchet@44005
   150
        else
blanchet@44102
   151
          (SOME used_facts, (preplay, message, ""))
blanchet@44005
   152
    in
blanchet@44005
   153
      case used_facts of
blanchet@44005
   154
        SOME used_facts =>
blanchet@44005
   155
        (if debug andalso not (null used_facts) then
blanchet@44005
   156
           facts ~~ (0 upto length facts - 1)
blanchet@44005
   157
           |> map (fn (fact, j) => fact |> untranslated_fact |> apsnd (K j))
blanchet@44005
   158
           |> filter_used_facts used_facts
blanchet@44005
   159
           |> map (fn ((name, _), j) => name ^ "@" ^ string_of_int j)
blanchet@44005
   160
           |> commas
blanchet@44005
   161
           |> enclose ("Fact" ^ plural_s (length facts) ^ " in " ^ quote name ^
blanchet@44005
   162
                       " proof (of " ^ string_of_int (length facts) ^ "): ") "."
blanchet@44005
   163
           |> Output.urgent_message
blanchet@44005
   164
         else
blanchet@44005
   165
           ();
blanchet@46231
   166
         {outcome = NONE, used_facts = used_facts, run_time = run_time,
blanchet@46231
   167
          preplay = preplay, message = message, message_tail = message_tail})
blanchet@44005
   168
      | NONE => result
blanchet@44005
   169
    end
blanchet@44005
   170
blanchet@44005
   171
fun get_minimizing_prover ctxt mode name params minimize_command problem =
blanchet@43862
   172
  get_prover ctxt mode name params minimize_command problem
blanchet@44005
   173
  |> minimize ctxt mode name params problem
blanchet@41510
   174
nik@45450
   175
fun is_induction_fact (Untranslated_Fact ((_, Induction), _)) = true
nik@45450
   176
  | is_induction_fact _ = false
nik@45450
   177
blanchet@46577
   178
fun launch_prover (params as {debug, verbose, blocking, max_relevant, slice,
blanchet@43900
   179
                              timeout, expect, ...})
blanchet@43862
   180
        mode minimize_command only
blanchet@42612
   181
        {state, goal, subgoal, subgoal_count, facts, smt_filter} name =
blanchet@41337
   182
  let
blanchet@41337
   183
    val ctxt = Proof.context_of state
blanchet@43719
   184
    val hard_timeout = Time.+ (timeout, timeout)
blanchet@41337
   185
    val birth_time = Time.now ()
blanchet@43719
   186
    val death_time = Time.+ (birth_time, hard_timeout)
blanchet@41337
   187
    val max_relevant =
blanchet@43314
   188
      max_relevant
blanchet@46577
   189
      |> the_default (default_max_relevant_for_prover ctxt slice name)
blanchet@41337
   190
    val num_facts = length facts |> not only ? Integer.min max_relevant
blanchet@43847
   191
    fun desc () =
blanchet@41337
   192
      prover_description ctxt params name num_facts subgoal subgoal_count goal
blanchet@41337
   193
    val problem =
nik@45449
   194
      let
nik@45450
   195
        val filter_induction = filter_out is_induction_fact
nik@45449
   196
      in {state = state, goal = goal, subgoal = subgoal,
nik@45449
   197
         subgoal_count = subgoal_count, facts =
nik@45450
   198
          ((Sledgehammer_Provers.is_ho_atp ctxt name |> not) ? filter_induction)
nik@45450
   199
            facts
nik@45449
   200
          |> take num_facts,
nik@45449
   201
         smt_filter = smt_filter}
nik@45449
   202
      end
blanchet@41501
   203
    fun really_go () =
blanchet@41511
   204
      problem
blanchet@43892
   205
      |> get_minimizing_prover ctxt mode name params minimize_command
blanchet@44102
   206
      |> (fn {outcome, preplay, message, message_tail, ...} =>
blanchet@43846
   207
             (if outcome = SOME ATP_Proof.TimedOut then timeoutN
blanchet@43846
   208
              else if is_some outcome then noneN
blanchet@44102
   209
              else someN, fn () => message (preplay ()) ^ message_tail))
blanchet@41337
   210
    fun go () =
blanchet@41337
   211
      let
blanchet@41337
   212
        val (outcome_code, message) =
blanchet@41337
   213
          if debug then
blanchet@41337
   214
            really_go ()
blanchet@41337
   215
          else
blanchet@41337
   216
            (really_go ()
blanchet@43893
   217
             handle ERROR msg => (unknownN, fn () => "Error: " ^ msg ^ "\n")
blanchet@41337
   218
                  | exn =>
blanchet@41337
   219
                    if Exn.is_interrupt exn then
blanchet@41337
   220
                      reraise exn
blanchet@41337
   221
                    else
blanchet@43893
   222
                      (unknownN, fn () => "Internal error:\n" ^
blanchet@43893
   223
                                          ML_Compiler.exn_message exn ^ "\n"))
blanchet@41337
   224
        val _ =
blanchet@41390
   225
          (* The "expect" argument is deliberately ignored if the prover is
blanchet@41390
   226
             missing so that the "Metis_Examples" can be processed on any
blanchet@41390
   227
             machine. *)
blanchet@41390
   228
          if expect = "" orelse outcome_code = expect orelse
blanchet@41390
   229
             not (is_prover_installed ctxt name) then
blanchet@41337
   230
            ()
blanchet@41337
   231
          else if blocking then
blanchet@41337
   232
            error ("Unexpected outcome: " ^ quote outcome_code ^ ".")
blanchet@41337
   233
          else
blanchet@41337
   234
            warning ("Unexpected outcome: " ^ quote outcome_code ^ ".");
blanchet@43846
   235
      in (outcome_code, message) end
blanchet@41337
   236
  in
blanchet@43862
   237
    if mode = Auto_Try then
blanchet@43847
   238
      let val (outcome_code, message) = TimeLimit.timeLimit timeout go () in
blanchet@43847
   239
        (outcome_code,
blanchet@43847
   240
         state
blanchet@43847
   241
         |> outcome_code = someN
blanchet@43847
   242
            ? Proof.goal_message (fn () =>
blanchet@43847
   243
                  [Pretty.str "",
wenzelm@46537
   244
                   Pretty.mark Isabelle_Markup.hilite (Pretty.str (message ()))]
blanchet@43847
   245
                  |> Pretty.chunks))
blanchet@41337
   246
      end
blanchet@41337
   247
    else if blocking then
blanchet@43847
   248
      let
blanchet@43847
   249
        val (outcome_code, message) = TimeLimit.timeLimit hard_timeout go ()
blanchet@43847
   250
      in
blanchet@43899
   251
        (if outcome_code = someN orelse mode = Normal then
blanchet@43899
   252
           quote name ^ ": " ^ message ()
blanchet@43899
   253
         else
blanchet@43899
   254
           "")
blanchet@43846
   255
        |> Async_Manager.break_into_chunks
blanchet@43846
   256
        |> List.app Output.urgent_message;
blanchet@43847
   257
        (outcome_code, state)
blanchet@41337
   258
      end
blanchet@41337
   259
    else
blanchet@43847
   260
      (Async_Manager.launch das_tool birth_time death_time (desc ())
blanchet@43893
   261
                            ((fn (outcome_code, message) =>
blanchet@43900
   262
                                 (verbose orelse outcome_code = someN,
blanchet@43900
   263
                                  message ())) o go);
blanchet@43847
   264
       (unknownN, state))
blanchet@41337
   265
  end
blanchet@41337
   266
blanchet@41483
   267
fun class_of_smt_solver ctxt name =
blanchet@41483
   268
  ctxt |> select_smt_solver name
blanchet@41483
   269
       |> SMT_Config.solver_class_of |> SMT_Utils.string_of_class
blanchet@41483
   270
blanchet@43884
   271
(* Makes backtraces more transparent and might well be more efficient as
blanchet@43884
   272
   well. *)
blanchet@41483
   273
fun smart_par_list_map _ [] = []
blanchet@41483
   274
  | smart_par_list_map f [x] = [f x]
blanchet@41483
   275
  | smart_par_list_map f xs = Par_List.map f xs
blanchet@41483
   276
blanchet@41502
   277
fun dest_SMT_Weighted_Fact (SMT_Weighted_Fact p) = p
blanchet@41502
   278
  | dest_SMT_Weighted_Fact _ = raise Fail "dest_SMT_Weighted_Fact"
blanchet@41502
   279
blanchet@43862
   280
val auto_try_max_relevant_divisor = 2 (* FUDGE *)
blanchet@40241
   281
blanchet@43787
   282
fun run_sledgehammer (params as {debug, verbose, blocking, provers,
blanchet@46577
   283
                                 relevance_thresholds, max_relevant, slice,
blanchet@43787
   284
                                 timeout, ...})
blanchet@43862
   285
        mode i (relevance_override as {only, ...}) minimize_command state =
blanchet@40240
   286
  if null provers then
blanchet@40240
   287
    error "No prover is set."
blanchet@39564
   288
  else case subgoal_count state of
blanchet@43861
   289
    0 => (Output.urgent_message "No subgoal!"; (false, (noneN, state)))
blanchet@39564
   290
  | n =>
blanchet@39564
   291
    let
blanchet@39610
   292
      val _ = Proof.assert_backward state
blanchet@43862
   293
      val print = if mode = Normal then Output.urgent_message else K ()
blanchet@41483
   294
      val state =
blanchet@41483
   295
        state |> Proof.map_context (Config.put SMT_Config.verbose debug)
blanchet@40441
   296
      val ctxt = Proof.context_of state
blanchet@40441
   297
      val {facts = chained_ths, goal, ...} = Proof.goal state
blanchet@43884
   298
      val chained_ths = chained_ths |> normalize_chained_theorems
blanchet@43845
   299
      val (_, hyp_ts, concl_t) = strip_subgoal ctxt goal i
blanchet@45483
   300
      val ho_atp = exists (Sledgehammer_Provers.is_ho_atp ctxt) provers
blanchet@45483
   301
      val facts =
blanchet@45483
   302
        nearly_all_facts ctxt ho_atp relevance_override chained_ths hyp_ts
blanchet@45483
   303
                         concl_t
nik@45450
   304
      val _ = () |> not blocking ? kill_provers
blanchet@42591
   305
      val _ = case find_first (not o is_prover_supported ctxt) provers of
blanchet@41189
   306
                SOME name => error ("No such prover: " ^ name ^ ".")
blanchet@41189
   307
              | NONE => ()
blanchet@42644
   308
      val _ = print "Sledgehammering..."
blanchet@43785
   309
      val (smts, (ueq_atps, full_atps)) =
blanchet@43785
   310
        provers |> List.partition (is_smt_prover ctxt)
blanchet@43785
   311
                ||> List.partition (is_unit_equational_atp ctxt)
blanchet@42612
   312
      fun launch_provers state get_facts translate maybe_smt_filter provers =
blanchet@41502
   313
        let
blanchet@41502
   314
          val facts = get_facts ()
blanchet@41502
   315
          val num_facts = length facts
blanchet@41502
   316
          val facts = facts ~~ (0 upto num_facts - 1)
blanchet@41502
   317
                      |> map (translate num_facts)
blanchet@41502
   318
          val problem =
blanchet@41502
   319
            {state = state, goal = goal, subgoal = i, subgoal_count = n,
blanchet@41502
   320
             facts = facts,
blanchet@42612
   321
             smt_filter = maybe_smt_filter
blanchet@41502
   322
                  (fn () => map_filter (try dest_SMT_Weighted_Fact) facts) i}
blanchet@43862
   323
          val launch = launch_prover params mode minimize_command only
blanchet@41502
   324
        in
blanchet@43862
   325
          if mode = Auto_Try orelse mode = Try then
blanchet@43861
   326
            (unknownN, state)
blanchet@43862
   327
            |> fold (fn prover => fn accum as (outcome_code, _) =>
blanchet@43861
   328
                        if outcome_code = someN then accum
blanchet@43861
   329
                        else launch problem prover)
blanchet@43861
   330
                    provers
blanchet@41502
   331
          else
blanchet@41502
   332
            provers
blanchet@43861
   333
            |> (if blocking then smart_par_list_map else map)
blanchet@43861
   334
                   (launch problem #> fst)
blanchet@43861
   335
            |> max_outcome_code |> rpair state
blanchet@41502
   336
        end
blanchet@43793
   337
      fun get_facts label is_appropriate_prop relevance_fudge provers =
blanchet@41483
   338
        let
blanchet@41483
   339
          val max_max_relevant =
blanchet@41483
   340
            case max_relevant of
blanchet@41483
   341
              SOME n => n
blanchet@41483
   342
            | NONE =>
blanchet@43314
   343
              0 |> fold (Integer.max
blanchet@46577
   344
                         o default_max_relevant_for_prover ctxt slice)
blanchet@41483
   345
                        provers
blanchet@43862
   346
                |> mode = Auto_Try
blanchet@43862
   347
                   ? (fn n => n div auto_try_max_relevant_divisor)
blanchet@41483
   348
          val is_built_in_const =
blanchet@41483
   349
            is_built_in_const_for_prover ctxt (hd provers)
blanchet@41483
   350
        in
blanchet@44217
   351
          facts
blanchet@44217
   352
          |> (case is_appropriate_prop of
blanchet@44217
   353
                SOME is_app => filter (is_app o prop_of o snd)
blanchet@44217
   354
              | NONE => I)
blanchet@45483
   355
          |> relevant_facts ctxt relevance_thresholds max_max_relevant
blanchet@45483
   356
                            is_built_in_const relevance_fudge relevance_override
blanchet@45483
   357
                            chained_ths hyp_ts concl_t
blanchet@41483
   358
          |> tap (fn facts =>
blanchet@41483
   359
                     if debug then
blanchet@41483
   360
                       label ^ plural_s (length provers) ^ ": " ^
blanchet@41483
   361
                       (if null facts then
blanchet@41483
   362
                          "Found no relevant facts."
blanchet@41483
   363
                        else
blanchet@41483
   364
                          "Including (up to) " ^ string_of_int (length facts) ^
blanchet@41483
   365
                          " relevant fact" ^ plural_s (length facts) ^ ":\n" ^
blanchet@41483
   366
                          (facts |> map (fst o fst) |> space_implode " ") ^ ".")
blanchet@42644
   367
                       |> print
blanchet@41483
   368
                     else
blanchet@41483
   369
                       ())
blanchet@41483
   370
        end
blanchet@43793
   371
      fun launch_atps label is_appropriate_prop atps accum =
blanchet@43787
   372
        if null atps then
blanchet@41502
   373
          accum
blanchet@44217
   374
        else if is_some is_appropriate_prop andalso
blanchet@44217
   375
                not (the is_appropriate_prop concl_t) then
blanchet@43787
   376
          (if verbose orelse length atps = length provers then
blanchet@43787
   377
             "Goal outside the scope of " ^
blanchet@43787
   378
             space_implode " " (serial_commas "and" (map quote atps)) ^ "."
blanchet@43787
   379
             |> Output.urgent_message
blanchet@43787
   380
           else
blanchet@43787
   381
             ();
blanchet@43787
   382
           accum)
blanchet@41502
   383
        else
blanchet@43785
   384
          launch_provers state
blanchet@43793
   385
              (get_facts label is_appropriate_prop atp_relevance_fudge o K atps)
blanchet@43785
   386
              (K (Untranslated_Fact o fst)) (K (K NONE)) atps
blanchet@42617
   387
      fun launch_smts accum =
blanchet@42617
   388
        if null smts then
blanchet@41483
   389
          accum
blanchet@41483
   390
        else
blanchet@41483
   391
          let
blanchet@44217
   392
            val facts = get_facts "SMT solver" NONE smt_relevance_fudge smts
blanchet@43517
   393
            val weight = SMT_Weighted_Fact oo weight_smt_fact ctxt
blanchet@42612
   394
            fun smt_filter facts =
blanchet@42659
   395
              (if debug then curry (op o) SOME
blanchet@42659
   396
               else TimeLimit.timeLimit timeout o try)
boehmes@41680
   397
                  (SMT_Solver.smt_filter_preprocess state (facts ()))
blanchet@41483
   398
          in
blanchet@41483
   399
            smts |> map (`(class_of_smt_solver ctxt))
blanchet@41483
   400
                 |> AList.group (op =)
blanchet@43861
   401
                 |> map (snd #> launch_provers state (K facts) weight smt_filter
blanchet@43861
   402
                             #> fst)
blanchet@43861
   403
                 |> max_outcome_code |> rpair state
blanchet@41483
   404
          end
blanchet@44217
   405
      val launch_full_atps = launch_atps "ATP" NONE full_atps
blanchet@43785
   406
      val launch_ueq_atps =
blanchet@44217
   407
        launch_atps "Unit equational provers" (SOME is_unit_equality) ueq_atps
blanchet@41510
   408
      fun launch_atps_and_smt_solvers () =
blanchet@43884
   409
        [launch_full_atps, launch_smts, launch_ueq_atps]
blanchet@43862
   410
        |> smart_par_list_map (fn f => ignore (f (unknownN, state)))
blanchet@42644
   411
        handle ERROR msg => (print ("Error: " ^ msg); error msg)
blanchet@43862
   412
      fun maybe f (accum as (outcome_code, _)) =
blanchet@43862
   413
        accum |> (mode = Normal orelse outcome_code <> someN) ? f
blanchet@40241
   414
    in
blanchet@43861
   415
      (unknownN, state)
blanchet@43785
   416
      |> (if blocking then
blanchet@43862
   417
            launch_full_atps
blanchet@43862
   418
            #> mode <> Auto_Try ? (maybe launch_ueq_atps #> maybe launch_smts)
blanchet@43785
   419
          else
blanchet@43785
   420
            (fn p => Future.fork (tap launch_atps_and_smt_solvers) |> K p))
blanchet@42644
   421
      handle TimeLimit.TimeOut =>
blanchet@43861
   422
             (print "Sledgehammer ran out of time."; (unknownN, state))
blanchet@40241
   423
    end
blanchet@43861
   424
    |> `(fn (outcome_code, _) => outcome_code = someN)
blanchet@38290
   425
wenzelm@28582
   426
end;