src/HOL/Tools/Sledgehammer/sledgehammer_run.ML
author blanchet
Thu, 01 Dec 2011 13:34:13 +0100
changeset 46577 418846ea4f99
parent 46537 d83797ef0d2d
child 46578 6bf7eec9b153
permissions -rw-r--r--
renamed "slicing" to "slice"
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@43926
    11
  type minimize_command = ATP_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@43926
    34
open ATP_Translate
blanchet@43926
    35
open ATP_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@46391
    76
        ({debug, verbose, overlord, blocking, provers, type_enc, sound,
blanchet@46391
    77
         lam_trans, relevance_thresholds, max_relevant, max_mono_iters,
blanchet@46577
    78
         max_new_mono_instances, isar_proof, isar_shrink_factor, slice, timeout,
blanchet@46577
    79
         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@46391
    91
     provers = provers, type_enc = type_enc, sound = sound,
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@46577
    96
     isar_shrink_factor = isar_shrink_factor, slice = slice, timeout = timeout,
blanchet@46577
    97
     preplay_timeout = preplay_timeout, expect = expect}
blanchet@46391
    98
  end
blanchet@46391
    99
blanchet@44006
   100
fun minimize ctxt mode name (params as {debug, verbose, isar_proof, ...})
blanchet@44005
   101
             ({state, subgoal, subgoal_count, facts, ...} : prover_problem)
blanchet@46231
   102
             (result as {outcome, used_facts, run_time, preplay, message,
blanchet@46231
   103
                         message_tail} : prover_result) =
blanchet@44150
   104
  if is_some outcome orelse null used_facts then
blanchet@44005
   105
    result
blanchet@44005
   106
  else
blanchet@44005
   107
    let
blanchet@44005
   108
      val num_facts = length used_facts
blanchet@46391
   109
      val ((minimize, (minimize_name, params)), preplay) =
blanchet@44005
   110
        if mode = Normal then
blanchet@45763
   111
          if num_facts >= Config.get ctxt auto_minimize_min_facts then
blanchet@46391
   112
            ((true, (name, params)), preplay)
blanchet@44005
   113
          else
blanchet@44006
   114
            let
blanchet@46231
   115
              fun can_min_fast_enough time =
blanchet@46231
   116
                0.001
blanchet@46231
   117
                * Real.fromInt ((num_facts + 1) * Time.toMilliseconds time)
blanchet@45763
   118
                <= Config.get ctxt auto_minimize_max_time
blanchet@46231
   119
              val prover_fast_enough = can_min_fast_enough run_time
blanchet@44006
   120
            in
blanchet@44006
   121
              if isar_proof then
blanchet@46391
   122
                ((prover_fast_enough, (name, params)), preplay)
blanchet@44006
   123
              else
blanchet@44006
   124
                let val preplay = preplay () in
blanchet@44006
   125
                  (case preplay of
blanchet@46391
   126
                     Played (reconstr, timeout) =>
blanchet@46231
   127
                     if can_min_fast_enough timeout then
blanchet@46432
   128
                       (true, extract_reconstructor params reconstr
blanchet@46391
   129
                              ||> (fn override_params =>
blanchet@46391
   130
                                      adjust_reconstructor_params
blanchet@46391
   131
                                          override_params params))
blanchet@44006
   132
                     else
blanchet@46391
   133
                       (prover_fast_enough, (name, params))
blanchet@46391
   134
                   | _ => (prover_fast_enough, (name, params)),
blanchet@44006
   135
                   K preplay)
blanchet@44006
   136
                end
blanchet@44005
   137
            end
blanchet@44005
   138
        else
blanchet@46391
   139
          ((false, (name, params)), preplay)
blanchet@44102
   140
      val (used_facts, (preplay, message, _)) =
blanchet@44005
   141
        if minimize then
blanchet@44005
   142
          minimize_facts minimize_name params (not verbose) subgoal
blanchet@44005
   143
                         subgoal_count state
blanchet@44005
   144
                         (filter_used_facts used_facts
blanchet@44005
   145
                              (map (apsnd single o untranslated_fact) facts))
blanchet@44005
   146
          |>> Option.map (map fst)
blanchet@44005
   147
        else
blanchet@44102
   148
          (SOME used_facts, (preplay, message, ""))
blanchet@44005
   149
    in
blanchet@44005
   150
      case used_facts of
blanchet@44005
   151
        SOME used_facts =>
blanchet@44005
   152
        (if debug andalso not (null used_facts) then
blanchet@44005
   153
           facts ~~ (0 upto length facts - 1)
blanchet@44005
   154
           |> map (fn (fact, j) => fact |> untranslated_fact |> apsnd (K j))
blanchet@44005
   155
           |> filter_used_facts used_facts
blanchet@44005
   156
           |> map (fn ((name, _), j) => name ^ "@" ^ string_of_int j)
blanchet@44005
   157
           |> commas
blanchet@44005
   158
           |> enclose ("Fact" ^ plural_s (length facts) ^ " in " ^ quote name ^
blanchet@44005
   159
                       " proof (of " ^ string_of_int (length facts) ^ "): ") "."
blanchet@44005
   160
           |> Output.urgent_message
blanchet@44005
   161
         else
blanchet@44005
   162
           ();
blanchet@46231
   163
         {outcome = NONE, used_facts = used_facts, run_time = run_time,
blanchet@46231
   164
          preplay = preplay, message = message, message_tail = message_tail})
blanchet@44005
   165
      | NONE => result
blanchet@44005
   166
    end
blanchet@44005
   167
blanchet@44005
   168
fun get_minimizing_prover ctxt mode name params minimize_command problem =
blanchet@43862
   169
  get_prover ctxt mode name params minimize_command problem
blanchet@44005
   170
  |> minimize ctxt mode name params problem
blanchet@41510
   171
nik@45450
   172
fun is_induction_fact (Untranslated_Fact ((_, Induction), _)) = true
nik@45450
   173
  | is_induction_fact _ = false
nik@45450
   174
blanchet@46577
   175
fun launch_prover (params as {debug, verbose, blocking, max_relevant, slice,
blanchet@43900
   176
                              timeout, expect, ...})
blanchet@43862
   177
        mode minimize_command only
blanchet@42612
   178
        {state, goal, subgoal, subgoal_count, facts, smt_filter} name =
blanchet@41337
   179
  let
blanchet@41337
   180
    val ctxt = Proof.context_of state
blanchet@43719
   181
    val hard_timeout = Time.+ (timeout, timeout)
blanchet@41337
   182
    val birth_time = Time.now ()
blanchet@43719
   183
    val death_time = Time.+ (birth_time, hard_timeout)
blanchet@41337
   184
    val max_relevant =
blanchet@43314
   185
      max_relevant
blanchet@46577
   186
      |> the_default (default_max_relevant_for_prover ctxt slice name)
blanchet@41337
   187
    val num_facts = length facts |> not only ? Integer.min max_relevant
blanchet@43847
   188
    fun desc () =
blanchet@41337
   189
      prover_description ctxt params name num_facts subgoal subgoal_count goal
blanchet@41337
   190
    val problem =
nik@45449
   191
      let
nik@45450
   192
        val filter_induction = filter_out is_induction_fact
nik@45449
   193
      in {state = state, goal = goal, subgoal = subgoal,
nik@45449
   194
         subgoal_count = subgoal_count, facts =
nik@45450
   195
          ((Sledgehammer_Provers.is_ho_atp ctxt name |> not) ? filter_induction)
nik@45450
   196
            facts
nik@45449
   197
          |> take num_facts,
nik@45449
   198
         smt_filter = smt_filter}
nik@45449
   199
      end
blanchet@41501
   200
    fun really_go () =
blanchet@41511
   201
      problem
blanchet@43892
   202
      |> get_minimizing_prover ctxt mode name params minimize_command
blanchet@44102
   203
      |> (fn {outcome, preplay, message, message_tail, ...} =>
blanchet@43846
   204
             (if outcome = SOME ATP_Proof.TimedOut then timeoutN
blanchet@43846
   205
              else if is_some outcome then noneN
blanchet@44102
   206
              else someN, fn () => message (preplay ()) ^ message_tail))
blanchet@41337
   207
    fun go () =
blanchet@41337
   208
      let
blanchet@41337
   209
        val (outcome_code, message) =
blanchet@41337
   210
          if debug then
blanchet@41337
   211
            really_go ()
blanchet@41337
   212
          else
blanchet@41337
   213
            (really_go ()
blanchet@43893
   214
             handle ERROR msg => (unknownN, fn () => "Error: " ^ msg ^ "\n")
blanchet@41337
   215
                  | exn =>
blanchet@41337
   216
                    if Exn.is_interrupt exn then
blanchet@41337
   217
                      reraise exn
blanchet@41337
   218
                    else
blanchet@43893
   219
                      (unknownN, fn () => "Internal error:\n" ^
blanchet@43893
   220
                                          ML_Compiler.exn_message exn ^ "\n"))
blanchet@41337
   221
        val _ =
blanchet@41390
   222
          (* The "expect" argument is deliberately ignored if the prover is
blanchet@41390
   223
             missing so that the "Metis_Examples" can be processed on any
blanchet@41390
   224
             machine. *)
blanchet@41390
   225
          if expect = "" orelse outcome_code = expect orelse
blanchet@41390
   226
             not (is_prover_installed ctxt name) then
blanchet@41337
   227
            ()
blanchet@41337
   228
          else if blocking then
blanchet@41337
   229
            error ("Unexpected outcome: " ^ quote outcome_code ^ ".")
blanchet@41337
   230
          else
blanchet@41337
   231
            warning ("Unexpected outcome: " ^ quote outcome_code ^ ".");
blanchet@43846
   232
      in (outcome_code, message) end
blanchet@41337
   233
  in
blanchet@43862
   234
    if mode = Auto_Try then
blanchet@43847
   235
      let val (outcome_code, message) = TimeLimit.timeLimit timeout go () in
blanchet@43847
   236
        (outcome_code,
blanchet@43847
   237
         state
blanchet@43847
   238
         |> outcome_code = someN
blanchet@43847
   239
            ? Proof.goal_message (fn () =>
blanchet@43847
   240
                  [Pretty.str "",
wenzelm@46537
   241
                   Pretty.mark Isabelle_Markup.hilite (Pretty.str (message ()))]
blanchet@43847
   242
                  |> Pretty.chunks))
blanchet@41337
   243
      end
blanchet@41337
   244
    else if blocking then
blanchet@43847
   245
      let
blanchet@43847
   246
        val (outcome_code, message) = TimeLimit.timeLimit hard_timeout go ()
blanchet@43847
   247
      in
blanchet@43899
   248
        (if outcome_code = someN orelse mode = Normal then
blanchet@43899
   249
           quote name ^ ": " ^ message ()
blanchet@43899
   250
         else
blanchet@43899
   251
           "")
blanchet@43846
   252
        |> Async_Manager.break_into_chunks
blanchet@43846
   253
        |> List.app Output.urgent_message;
blanchet@43847
   254
        (outcome_code, state)
blanchet@41337
   255
      end
blanchet@41337
   256
    else
blanchet@43847
   257
      (Async_Manager.launch das_tool birth_time death_time (desc ())
blanchet@43893
   258
                            ((fn (outcome_code, message) =>
blanchet@43900
   259
                                 (verbose orelse outcome_code = someN,
blanchet@43900
   260
                                  message ())) o go);
blanchet@43847
   261
       (unknownN, state))
blanchet@41337
   262
  end
blanchet@41337
   263
blanchet@41483
   264
fun class_of_smt_solver ctxt name =
blanchet@41483
   265
  ctxt |> select_smt_solver name
blanchet@41483
   266
       |> SMT_Config.solver_class_of |> SMT_Utils.string_of_class
blanchet@41483
   267
blanchet@43884
   268
(* Makes backtraces more transparent and might well be more efficient as
blanchet@43884
   269
   well. *)
blanchet@41483
   270
fun smart_par_list_map _ [] = []
blanchet@41483
   271
  | smart_par_list_map f [x] = [f x]
blanchet@41483
   272
  | smart_par_list_map f xs = Par_List.map f xs
blanchet@41483
   273
blanchet@41502
   274
fun dest_SMT_Weighted_Fact (SMT_Weighted_Fact p) = p
blanchet@41502
   275
  | dest_SMT_Weighted_Fact _ = raise Fail "dest_SMT_Weighted_Fact"
blanchet@41502
   276
blanchet@43862
   277
val auto_try_max_relevant_divisor = 2 (* FUDGE *)
blanchet@40241
   278
blanchet@43787
   279
fun run_sledgehammer (params as {debug, verbose, blocking, provers,
blanchet@46577
   280
                                 relevance_thresholds, max_relevant, slice,
blanchet@43787
   281
                                 timeout, ...})
blanchet@43862
   282
        mode i (relevance_override as {only, ...}) minimize_command state =
blanchet@40240
   283
  if null provers then
blanchet@40240
   284
    error "No prover is set."
blanchet@39564
   285
  else case subgoal_count state of
blanchet@43861
   286
    0 => (Output.urgent_message "No subgoal!"; (false, (noneN, state)))
blanchet@39564
   287
  | n =>
blanchet@39564
   288
    let
blanchet@39610
   289
      val _ = Proof.assert_backward state
blanchet@43862
   290
      val print = if mode = Normal then Output.urgent_message else K ()
blanchet@41483
   291
      val state =
blanchet@41483
   292
        state |> Proof.map_context (Config.put SMT_Config.verbose debug)
blanchet@40441
   293
      val ctxt = Proof.context_of state
blanchet@40441
   294
      val {facts = chained_ths, goal, ...} = Proof.goal state
blanchet@43884
   295
      val chained_ths = chained_ths |> normalize_chained_theorems
blanchet@43845
   296
      val (_, hyp_ts, concl_t) = strip_subgoal ctxt goal i
blanchet@45483
   297
      val ho_atp = exists (Sledgehammer_Provers.is_ho_atp ctxt) provers
blanchet@45483
   298
      val facts =
blanchet@45483
   299
        nearly_all_facts ctxt ho_atp relevance_override chained_ths hyp_ts
blanchet@45483
   300
                         concl_t
nik@45450
   301
      val _ = () |> not blocking ? kill_provers
blanchet@42591
   302
      val _ = case find_first (not o is_prover_supported ctxt) provers of
blanchet@41189
   303
                SOME name => error ("No such prover: " ^ name ^ ".")
blanchet@41189
   304
              | NONE => ()
blanchet@42644
   305
      val _ = print "Sledgehammering..."
blanchet@43785
   306
      val (smts, (ueq_atps, full_atps)) =
blanchet@43785
   307
        provers |> List.partition (is_smt_prover ctxt)
blanchet@43785
   308
                ||> List.partition (is_unit_equational_atp ctxt)
blanchet@42612
   309
      fun launch_provers state get_facts translate maybe_smt_filter provers =
blanchet@41502
   310
        let
blanchet@41502
   311
          val facts = get_facts ()
blanchet@41502
   312
          val num_facts = length facts
blanchet@41502
   313
          val facts = facts ~~ (0 upto num_facts - 1)
blanchet@41502
   314
                      |> map (translate num_facts)
blanchet@41502
   315
          val problem =
blanchet@41502
   316
            {state = state, goal = goal, subgoal = i, subgoal_count = n,
blanchet@41502
   317
             facts = facts,
blanchet@42612
   318
             smt_filter = maybe_smt_filter
blanchet@41502
   319
                  (fn () => map_filter (try dest_SMT_Weighted_Fact) facts) i}
blanchet@43862
   320
          val launch = launch_prover params mode minimize_command only
blanchet@41502
   321
        in
blanchet@43862
   322
          if mode = Auto_Try orelse mode = Try then
blanchet@43861
   323
            (unknownN, state)
blanchet@43862
   324
            |> fold (fn prover => fn accum as (outcome_code, _) =>
blanchet@43861
   325
                        if outcome_code = someN then accum
blanchet@43861
   326
                        else launch problem prover)
blanchet@43861
   327
                    provers
blanchet@41502
   328
          else
blanchet@41502
   329
            provers
blanchet@43861
   330
            |> (if blocking then smart_par_list_map else map)
blanchet@43861
   331
                   (launch problem #> fst)
blanchet@43861
   332
            |> max_outcome_code |> rpair state
blanchet@41502
   333
        end
blanchet@43793
   334
      fun get_facts label is_appropriate_prop relevance_fudge provers =
blanchet@41483
   335
        let
blanchet@41483
   336
          val max_max_relevant =
blanchet@41483
   337
            case max_relevant of
blanchet@41483
   338
              SOME n => n
blanchet@41483
   339
            | NONE =>
blanchet@43314
   340
              0 |> fold (Integer.max
blanchet@46577
   341
                         o default_max_relevant_for_prover ctxt slice)
blanchet@41483
   342
                        provers
blanchet@43862
   343
                |> mode = Auto_Try
blanchet@43862
   344
                   ? (fn n => n div auto_try_max_relevant_divisor)
blanchet@41483
   345
          val is_built_in_const =
blanchet@41483
   346
            is_built_in_const_for_prover ctxt (hd provers)
blanchet@41483
   347
        in
blanchet@44217
   348
          facts
blanchet@44217
   349
          |> (case is_appropriate_prop of
blanchet@44217
   350
                SOME is_app => filter (is_app o prop_of o snd)
blanchet@44217
   351
              | NONE => I)
blanchet@45483
   352
          |> relevant_facts ctxt relevance_thresholds max_max_relevant
blanchet@45483
   353
                            is_built_in_const relevance_fudge relevance_override
blanchet@45483
   354
                            chained_ths hyp_ts concl_t
blanchet@41483
   355
          |> tap (fn facts =>
blanchet@41483
   356
                     if debug then
blanchet@41483
   357
                       label ^ plural_s (length provers) ^ ": " ^
blanchet@41483
   358
                       (if null facts then
blanchet@41483
   359
                          "Found no relevant facts."
blanchet@41483
   360
                        else
blanchet@41483
   361
                          "Including (up to) " ^ string_of_int (length facts) ^
blanchet@41483
   362
                          " relevant fact" ^ plural_s (length facts) ^ ":\n" ^
blanchet@41483
   363
                          (facts |> map (fst o fst) |> space_implode " ") ^ ".")
blanchet@42644
   364
                       |> print
blanchet@41483
   365
                     else
blanchet@41483
   366
                       ())
blanchet@41483
   367
        end
blanchet@43793
   368
      fun launch_atps label is_appropriate_prop atps accum =
blanchet@43787
   369
        if null atps then
blanchet@41502
   370
          accum
blanchet@44217
   371
        else if is_some is_appropriate_prop andalso
blanchet@44217
   372
                not (the is_appropriate_prop concl_t) then
blanchet@43787
   373
          (if verbose orelse length atps = length provers then
blanchet@43787
   374
             "Goal outside the scope of " ^
blanchet@43787
   375
             space_implode " " (serial_commas "and" (map quote atps)) ^ "."
blanchet@43787
   376
             |> Output.urgent_message
blanchet@43787
   377
           else
blanchet@43787
   378
             ();
blanchet@43787
   379
           accum)
blanchet@41502
   380
        else
blanchet@43785
   381
          launch_provers state
blanchet@43793
   382
              (get_facts label is_appropriate_prop atp_relevance_fudge o K atps)
blanchet@43785
   383
              (K (Untranslated_Fact o fst)) (K (K NONE)) atps
blanchet@42617
   384
      fun launch_smts accum =
blanchet@42617
   385
        if null smts then
blanchet@41483
   386
          accum
blanchet@41483
   387
        else
blanchet@41483
   388
          let
blanchet@44217
   389
            val facts = get_facts "SMT solver" NONE smt_relevance_fudge smts
blanchet@43517
   390
            val weight = SMT_Weighted_Fact oo weight_smt_fact ctxt
blanchet@42612
   391
            fun smt_filter facts =
blanchet@42659
   392
              (if debug then curry (op o) SOME
blanchet@42659
   393
               else TimeLimit.timeLimit timeout o try)
boehmes@41680
   394
                  (SMT_Solver.smt_filter_preprocess state (facts ()))
blanchet@41483
   395
          in
blanchet@41483
   396
            smts |> map (`(class_of_smt_solver ctxt))
blanchet@41483
   397
                 |> AList.group (op =)
blanchet@43861
   398
                 |> map (snd #> launch_provers state (K facts) weight smt_filter
blanchet@43861
   399
                             #> fst)
blanchet@43861
   400
                 |> max_outcome_code |> rpair state
blanchet@41483
   401
          end
blanchet@44217
   402
      val launch_full_atps = launch_atps "ATP" NONE full_atps
blanchet@43785
   403
      val launch_ueq_atps =
blanchet@44217
   404
        launch_atps "Unit equational provers" (SOME is_unit_equality) ueq_atps
blanchet@41510
   405
      fun launch_atps_and_smt_solvers () =
blanchet@43884
   406
        [launch_full_atps, launch_smts, launch_ueq_atps]
blanchet@43862
   407
        |> smart_par_list_map (fn f => ignore (f (unknownN, state)))
blanchet@42644
   408
        handle ERROR msg => (print ("Error: " ^ msg); error msg)
blanchet@43862
   409
      fun maybe f (accum as (outcome_code, _)) =
blanchet@43862
   410
        accum |> (mode = Normal orelse outcome_code <> someN) ? f
blanchet@40241
   411
    in
blanchet@43861
   412
      (unknownN, state)
blanchet@43785
   413
      |> (if blocking then
blanchet@43862
   414
            launch_full_atps
blanchet@43862
   415
            #> mode <> Auto_Try ? (maybe launch_ueq_atps #> maybe launch_smts)
blanchet@43785
   416
          else
blanchet@43785
   417
            (fn p => Future.fork (tap launch_atps_and_smt_solvers) |> K p))
blanchet@42644
   418
      handle TimeLimit.TimeOut =>
blanchet@43861
   419
             (print "Sledgehammer ran out of time."; (unknownN, state))
blanchet@40241
   420
    end
blanchet@43861
   421
    |> `(fn (outcome_code, _) => outcome_code = someN)
blanchet@38290
   422
wenzelm@28582
   423
end;