src/HOL/Tools/Sledgehammer/sledgehammer_provers.ML
author blanchet
Tue Nov 06 15:15:33 2012 +0100 (2012-11-06)
changeset 50020 6b9611abcd4c
parent 50004 c96e8e40d789
child 50486 d5dc28fafd9d
permissions -rw-r--r--
renamed Sledgehammer option
blanchet@41087
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_provers.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@41087
     6
Generic prover abstraction for Sledgehammer.
wenzelm@28477
     7
*)
wenzelm@28477
     8
blanchet@41087
     9
signature SLEDGEHAMMER_PROVERS =
wenzelm@28477
    10
sig
blanchet@40181
    11
  type failure = ATP_Proof.failure
blanchet@46340
    12
  type stature = ATP_Problem_Generate.stature
blanchet@46320
    13
  type type_enc = ATP_Problem_Generate.type_enc
blanchet@49914
    14
  type reconstructor = Sledgehammer_Reconstruct.reconstructor
blanchet@49914
    15
  type play = Sledgehammer_Reconstruct.play
blanchet@49914
    16
  type minimize_command = Sledgehammer_Reconstruct.minimize_command
blanchet@39493
    17
blanchet@48392
    18
  datatype mode = Auto_Try | Try | Normal | MaSh | Auto_Minimize | Minimize
blanchet@43021
    19
blanchet@35969
    20
  type params =
blanchet@48321
    21
    {debug : bool,
blanchet@48321
    22
     verbose : bool,
blanchet@48321
    23
     overlord : bool,
blanchet@48321
    24
     blocking : bool,
blanchet@48321
    25
     provers : string list,
blanchet@48321
    26
     type_enc : string option,
blanchet@48321
    27
     strict : bool,
blanchet@48321
    28
     lam_trans : string option,
blanchet@48321
    29
     uncurried_aliases : bool option,
blanchet@48321
    30
     learn : bool,
blanchet@48321
    31
     fact_filter : string option,
blanchet@48321
    32
     max_facts : int option,
blanchet@48321
    33
     fact_thresholds : real * real,
blanchet@48321
    34
     max_mono_iters : int option,
blanchet@48321
    35
     max_new_mono_instances : int option,
blanchet@49918
    36
     isar_proofs : bool,
blanchet@50020
    37
     isar_shrink : real,
blanchet@48321
    38
     slice : bool,
blanchet@48321
    39
     minimize : bool option,
blanchet@48321
    40
     timeout : Time.time,
blanchet@48321
    41
     preplay_timeout : Time.time,
blanchet@48321
    42
     expect : string}
blanchet@39493
    43
blanchet@48288
    44
  type relevance_fudge =
blanchet@48288
    45
    {local_const_multiplier : real,
blanchet@48288
    46
     worse_irrel_freq : real,
blanchet@48288
    47
     higher_order_irrel_weight : real,
blanchet@48288
    48
     abs_rel_weight : real,
blanchet@48288
    49
     abs_irrel_weight : real,
blanchet@48288
    50
     skolem_irrel_weight : real,
blanchet@48288
    51
     theory_const_rel_weight : real,
blanchet@48288
    52
     theory_const_irrel_weight : real,
blanchet@48288
    53
     chained_const_irrel_weight : real,
blanchet@48288
    54
     intro_bonus : real,
blanchet@48288
    55
     elim_bonus : real,
blanchet@48288
    56
     simp_bonus : real,
blanchet@48288
    57
     local_bonus : real,
blanchet@48288
    58
     assum_bonus : real,
blanchet@48288
    59
     chained_bonus : real,
blanchet@48288
    60
     max_imperfect : real,
blanchet@48288
    61
     max_imperfect_exp : real,
blanchet@48288
    62
     threshold_divisor : real,
blanchet@48288
    63
     ridiculous_threshold : real}
blanchet@48288
    64
blanchet@41090
    65
  datatype prover_fact =
blanchet@46340
    66
    Untranslated_Fact of (string * stature) * thm |
blanchet@46340
    67
    SMT_Weighted_Fact of (string * stature) * (int option * thm)
blanchet@40061
    68
blanchet@40061
    69
  type prover_problem =
blanchet@48321
    70
    {state : Proof.state,
blanchet@48321
    71
     goal : thm,
blanchet@48321
    72
     subgoal : int,
blanchet@48321
    73
     subgoal_count : int,
blanchet@48321
    74
     facts : prover_fact list}
blanchet@39493
    75
blanchet@40061
    76
  type prover_result =
blanchet@48321
    77
    {outcome : failure option,
blanchet@48321
    78
     used_facts : (string * stature) list,
blanchet@48321
    79
     run_time : Time.time,
blanchet@48321
    80
     preplay : unit -> play,
blanchet@48321
    81
     message : play -> string,
blanchet@48321
    82
     message_tail : string}
blanchet@39493
    83
blanchet@43051
    84
  type prover =
blanchet@45520
    85
    params -> ((string * string list) list -> string -> minimize_command)
blanchet@45520
    86
    -> prover_problem -> prover_result
blanchet@35867
    87
blanchet@43092
    88
  val dest_dir : string Config.T
blanchet@43092
    89
  val problem_prefix : string Config.T
blanchet@48143
    90
  val completish : bool Config.T
blanchet@44592
    91
  val atp_full_names : bool Config.T
blanchet@42646
    92
  val smt_triggers : bool Config.T
blanchet@42646
    93
  val smt_weights : bool Config.T
blanchet@42646
    94
  val smt_weight_min_facts : int Config.T
blanchet@42646
    95
  val smt_min_weight : int Config.T
blanchet@42646
    96
  val smt_max_weight : int Config.T
blanchet@42646
    97
  val smt_max_weight_index : int Config.T
blanchet@41256
    98
  val smt_weight_curve : (int -> int) Unsynchronized.ref
blanchet@42646
    99
  val smt_max_slices : int Config.T
blanchet@42646
   100
  val smt_slice_fact_frac : real Config.T
blanchet@42646
   101
  val smt_slice_time_frac : real Config.T
blanchet@42646
   102
  val smt_slice_min_secs : int Config.T
blanchet@48319
   103
  val SledgehammerN : string
blanchet@45519
   104
  val plain_metis : reconstructor
blanchet@41242
   105
  val select_smt_solver : string -> Proof.context -> Proof.context
blanchet@45520
   106
  val extract_reconstructor :
blanchet@45561
   107
    params -> reconstructor -> string * (string * string list) list
blanchet@45379
   108
  val is_reconstructor : string -> bool
blanchet@43050
   109
  val is_atp : theory -> string -> bool
blanchet@41087
   110
  val is_smt_prover : Proof.context -> string -> bool
blanchet@47962
   111
  val is_ho_atp: Proof.context -> string -> bool
blanchet@42944
   112
  val is_unit_equational_atp : Proof.context -> string -> bool
blanchet@41727
   113
  val is_prover_supported : Proof.context -> string -> bool
blanchet@40072
   114
  val is_prover_installed : Proof.context -> string -> bool
blanchet@48293
   115
  val default_max_facts_for_prover : Proof.context -> bool -> string -> int
blanchet@42944
   116
  val is_unit_equality : term -> bool
blanchet@42952
   117
  val is_appropriate_prop_for_prover : Proof.context -> string -> term -> bool
blanchet@40369
   118
  val is_built_in_const_for_prover :
blanchet@41336
   119
    Proof.context -> string -> string * typ -> term list -> bool * term list
blanchet@41087
   120
  val atp_relevance_fudge : relevance_fudge
blanchet@41087
   121
  val smt_relevance_fudge : relevance_fudge
blanchet@40941
   122
  val relevance_fudge_for_prover : Proof.context -> string -> relevance_fudge
blanchet@41256
   123
  val weight_smt_fact :
blanchet@46340
   124
    Proof.context -> int -> ((string * stature) * thm) * int
blanchet@46340
   125
    -> (string * stature) * (int option * thm)
blanchet@46340
   126
  val untranslated_fact : prover_fact -> (string * stature) * thm
blanchet@41242
   127
  val smt_weighted_fact :
blanchet@42646
   128
    Proof.context -> int -> prover_fact * int
blanchet@46340
   129
    -> (string * stature) * (int option * thm)
blanchet@41727
   130
  val supported_provers : Proof.context -> unit
blanchet@40059
   131
  val kill_provers : unit -> unit
blanchet@40059
   132
  val running_provers : unit -> unit
blanchet@40059
   133
  val messages : int option -> unit
blanchet@48798
   134
  val is_fact_chained : (('a * stature) * 'b) -> bool
blanchet@48798
   135
  val filter_used_facts :
blanchet@48798
   136
    bool -> (''a * stature) list -> ((''a * stature) * 'b) list ->
blanchet@48798
   137
    ((''a * stature) * 'b) list
blanchet@43021
   138
  val get_prover : Proof.context -> mode -> string -> prover
wenzelm@28477
   139
end;
wenzelm@28477
   140
blanchet@41087
   141
structure Sledgehammer_Provers : SLEDGEHAMMER_PROVERS =
wenzelm@28477
   142
struct
wenzelm@28477
   143
blanchet@43085
   144
open ATP_Util
blanchet@38028
   145
open ATP_Problem
blanchet@39491
   146
open ATP_Proof
blanchet@38028
   147
open ATP_Systems
blanchet@46320
   148
open ATP_Problem_Generate
blanchet@46320
   149
open ATP_Proof_Reconstruct
blanchet@45521
   150
open Metis_Tactic
blanchet@38023
   151
open Sledgehammer_Util
blanchet@49881
   152
open Sledgehammer_Reconstruct
blanchet@48288
   153
blanchet@37583
   154
blanchet@37583
   155
(** The Sledgehammer **)
blanchet@37583
   156
blanchet@48392
   157
datatype mode = Auto_Try | Try | Normal | MaSh | Auto_Minimize | Minimize
blanchet@43021
   158
blanchet@45376
   159
(* Identifier that distinguishes Sledgehammer from other tools that could use
blanchet@38102
   160
   "Async_Manager". *)
blanchet@48319
   161
val SledgehammerN = "Sledgehammer"
blanchet@37585
   162
blanchet@45520
   163
val reconstructor_names = [metisN, smtN]
blanchet@46365
   164
val plain_metis = Metis (hd partial_type_encs, combsN)
blanchet@45561
   165
val is_reconstructor = member (op =) reconstructor_names
blanchet@43228
   166
blanchet@43050
   167
val is_atp = member (op =) o supported_atps
blanchet@43050
   168
blanchet@43233
   169
val select_smt_solver = Context.proof_map o SMT_Config.select_solver
blanchet@41242
   170
blanchet@45376
   171
fun is_smt_prover ctxt = member (op =) (SMT_Solver.available_solvers_of ctxt)
blanchet@40062
   172
blanchet@44597
   173
fun is_atp_for_format is_format ctxt name =
blanchet@42944
   174
  let val thy = Proof_Context.theory_of ctxt in
blanchet@42944
   175
    case try (get_atp thy) name of
blanchet@47606
   176
      SOME config =>
blanchet@48716
   177
      exists (fn (_, ((_, format, _, _, _), _)) => is_format format)
blanchet@47606
   178
             (#best_slices (config ()) ctxt)
blanchet@42944
   179
    | NONE => false
blanchet@42944
   180
  end
blanchet@42944
   181
blanchet@44597
   182
val is_unit_equational_atp = is_atp_for_format (curry (op =) CNF_UEQ)
blanchet@45303
   183
val is_ho_atp = is_atp_for_format is_format_higher_order
blanchet@44597
   184
blanchet@45376
   185
fun is_prover_supported ctxt =
wenzelm@42361
   186
  let val thy = Proof_Context.theory_of ctxt in
blanchet@45379
   187
    is_reconstructor orf is_atp thy orf is_smt_prover ctxt
blanchet@40941
   188
  end
blanchet@40072
   189
boehmes@41432
   190
fun is_prover_installed ctxt =
blanchet@45379
   191
  is_reconstructor orf is_smt_prover ctxt orf
blanchet@43050
   192
  is_atp_installed (Proof_Context.theory_of ctxt)
blanchet@40941
   193
blanchet@45706
   194
fun get_slices slice slices =
blanchet@45706
   195
  (0 upto length slices - 1) ~~ slices |> not slice ? (List.last #> single)
blanchet@42443
   196
blanchet@48293
   197
val reconstructor_default_max_facts = 20
blanchet@43050
   198
blanchet@48293
   199
fun default_max_facts_for_prover ctxt slice name =
wenzelm@42361
   200
  let val thy = Proof_Context.theory_of ctxt in
blanchet@45379
   201
    if is_reconstructor name then
blanchet@48293
   202
      reconstructor_default_max_facts
blanchet@43050
   203
    else if is_atp thy name then
blanchet@48716
   204
      fold (Integer.max o #1 o fst o snd o snd)
blanchet@47606
   205
           (get_slices slice (#best_slices (get_atp thy name ()) ctxt)) 0
blanchet@43050
   206
    else (* is_smt_prover ctxt name *)
blanchet@43050
   207
      SMT_Solver.default_max_relevant ctxt name
blanchet@40941
   208
  end
blanchet@40063
   209
blanchet@42956
   210
fun is_if (@{const_name If}, _) = true
blanchet@42956
   211
  | is_if _ = false
blanchet@42956
   212
blanchet@42956
   213
(* Beware of "if and only if" (which is translated as such) and "If" (which is
blanchet@42956
   214
   translated to conditional equations). *)
blanchet@42956
   215
fun is_good_unit_equality T t u =
blanchet@42956
   216
  T <> @{typ bool} andalso not (exists (exists_Const is_if) [t, u])
blanchet@42956
   217
blanchet@42944
   218
fun is_unit_equality (@{const Trueprop} $ t) = is_unit_equality t
blanchet@42944
   219
  | is_unit_equality (Const (@{const_name all}, _) $ Abs (_, _, t)) =
blanchet@42944
   220
    is_unit_equality t
blanchet@42944
   221
  | is_unit_equality (Const (@{const_name All}, _) $ Abs (_, _, t)) =
blanchet@42944
   222
    is_unit_equality t
blanchet@42956
   223
  | is_unit_equality (Const (@{const_name "=="}, Type (_, [T, _])) $ t $ u) =
blanchet@42956
   224
    is_good_unit_equality T t u
blanchet@42956
   225
  | is_unit_equality (Const (@{const_name HOL.eq}, Type (_ , [T, _])) $ t $ u) =
blanchet@42956
   226
    is_good_unit_equality T t u
blanchet@42944
   227
  | is_unit_equality _ = false
blanchet@42944
   228
blanchet@42952
   229
fun is_appropriate_prop_for_prover ctxt name =
blanchet@42944
   230
  if is_unit_equational_atp ctxt name then is_unit_equality else K true
blanchet@42944
   231
blanchet@41242
   232
fun is_built_in_const_for_prover ctxt name =
blanchet@41242
   233
  if is_smt_prover ctxt name then
blanchet@41336
   234
    let val ctxt = ctxt |> select_smt_solver name in
blanchet@41336
   235
      fn x => fn ts =>
blanchet@41336
   236
         if SMT_Builtin.is_builtin_num_ext ctxt (list_comb (Const x, ts)) then
blanchet@41336
   237
           (true, [])
blanchet@41336
   238
         else if SMT_Builtin.is_builtin_fun_ext ctxt x ts then
blanchet@41336
   239
           (true, ts)
blanchet@41336
   240
         else
blanchet@41336
   241
           (false, ts)
blanchet@41336
   242
    end
blanchet@41242
   243
  else
blanchet@41336
   244
    fn (s, _) => fn ts => (member (op =) atp_irrelevant_consts s, ts)
blanchet@40071
   245
blanchet@40070
   246
(* FUDGE *)
blanchet@40070
   247
val atp_relevance_fudge =
blanchet@42738
   248
  {local_const_multiplier = 1.5,
blanchet@41159
   249
   worse_irrel_freq = 100.0,
blanchet@40070
   250
   higher_order_irrel_weight = 1.05,
blanchet@40070
   251
   abs_rel_weight = 0.5,
blanchet@40070
   252
   abs_irrel_weight = 2.0,
blanchet@47934
   253
   skolem_irrel_weight = 0.05,
blanchet@40070
   254
   theory_const_rel_weight = 0.5,
blanchet@40070
   255
   theory_const_irrel_weight = 0.25,
blanchet@42735
   256
   chained_const_irrel_weight = 0.25,
blanchet@40070
   257
   intro_bonus = 0.15,
blanchet@40070
   258
   elim_bonus = 0.15,
blanchet@40070
   259
   simp_bonus = 0.15,
blanchet@40070
   260
   local_bonus = 0.55,
blanchet@40070
   261
   assum_bonus = 1.05,
blanchet@40070
   262
   chained_bonus = 1.5,
blanchet@40070
   263
   max_imperfect = 11.5,
blanchet@40070
   264
   max_imperfect_exp = 1.0,
blanchet@40070
   265
   threshold_divisor = 2.0,
blanchet@41093
   266
   ridiculous_threshold = 0.01}
blanchet@40070
   267
blanchet@40071
   268
(* FUDGE (FIXME) *)
blanchet@40070
   269
val smt_relevance_fudge =
blanchet@42738
   270
  {local_const_multiplier = #local_const_multiplier atp_relevance_fudge,
blanchet@41159
   271
   worse_irrel_freq = #worse_irrel_freq atp_relevance_fudge,
blanchet@40071
   272
   higher_order_irrel_weight = #higher_order_irrel_weight atp_relevance_fudge,
blanchet@40071
   273
   abs_rel_weight = #abs_rel_weight atp_relevance_fudge,
blanchet@40071
   274
   abs_irrel_weight = #abs_irrel_weight atp_relevance_fudge,
blanchet@40071
   275
   skolem_irrel_weight = #skolem_irrel_weight atp_relevance_fudge,
blanchet@40071
   276
   theory_const_rel_weight = #theory_const_rel_weight atp_relevance_fudge,
blanchet@40071
   277
   theory_const_irrel_weight = #theory_const_irrel_weight atp_relevance_fudge,
blanchet@42735
   278
   chained_const_irrel_weight = #chained_const_irrel_weight atp_relevance_fudge,
blanchet@40071
   279
   intro_bonus = #intro_bonus atp_relevance_fudge,
blanchet@40071
   280
   elim_bonus = #elim_bonus atp_relevance_fudge,
blanchet@40071
   281
   simp_bonus = #simp_bonus atp_relevance_fudge,
blanchet@40071
   282
   local_bonus = #local_bonus atp_relevance_fudge,
blanchet@40071
   283
   assum_bonus = #assum_bonus atp_relevance_fudge,
blanchet@40071
   284
   chained_bonus = #chained_bonus atp_relevance_fudge,
blanchet@40071
   285
   max_imperfect = #max_imperfect atp_relevance_fudge,
blanchet@40071
   286
   max_imperfect_exp = #max_imperfect_exp atp_relevance_fudge,
blanchet@40071
   287
   threshold_divisor = #threshold_divisor atp_relevance_fudge,
blanchet@40071
   288
   ridiculous_threshold = #ridiculous_threshold atp_relevance_fudge}
blanchet@40070
   289
blanchet@40941
   290
fun relevance_fudge_for_prover ctxt name =
blanchet@40941
   291
  if is_smt_prover ctxt name then smt_relevance_fudge else atp_relevance_fudge
blanchet@40070
   292
blanchet@41727
   293
fun supported_provers ctxt =
blanchet@40060
   294
  let
wenzelm@42361
   295
    val thy = Proof_Context.theory_of ctxt
blanchet@40060
   296
    val (remote_provers, local_provers) =
blanchet@45520
   297
      reconstructor_names @
blanchet@41727
   298
      sort_strings (supported_atps thy) @
blanchet@41727
   299
      sort_strings (SMT_Solver.available_solvers_of ctxt)
blanchet@40060
   300
      |> List.partition (String.isPrefix remote_prefix)
blanchet@40060
   301
  in
blanchet@41727
   302
    Output.urgent_message ("Supported provers: " ^
blanchet@40205
   303
                           commas (local_provers @ remote_provers) ^ ".")
blanchet@40060
   304
  end
blanchet@35969
   305
blanchet@48319
   306
fun kill_provers () = Async_Manager.kill_threads SledgehammerN "prover"
blanchet@48319
   307
fun running_provers () = Async_Manager.running_threads SledgehammerN "prover"
blanchet@48319
   308
val messages = Async_Manager.thread_messages SledgehammerN "prover"
blanchet@40059
   309
blanchet@48288
   310
blanchet@40059
   311
(** problems, results, ATPs, etc. **)
blanchet@35969
   312
blanchet@35969
   313
type params =
blanchet@48321
   314
  {debug : bool,
blanchet@48321
   315
   verbose : bool,
blanchet@48321
   316
   overlord : bool,
blanchet@48321
   317
   blocking : bool,
blanchet@48321
   318
   provers : string list,
blanchet@48321
   319
   type_enc : string option,
blanchet@48321
   320
   strict : bool,
blanchet@48321
   321
   lam_trans : string option,
blanchet@48321
   322
   uncurried_aliases : bool option,
blanchet@48321
   323
   learn : bool,
blanchet@48321
   324
   fact_filter : string option,
blanchet@48321
   325
   max_facts : int option,
blanchet@48321
   326
   fact_thresholds : real * real,
blanchet@48321
   327
   max_mono_iters : int option,
blanchet@48321
   328
   max_new_mono_instances : int option,
blanchet@49918
   329
   isar_proofs : bool,
blanchet@50020
   330
   isar_shrink : real,
blanchet@48321
   331
   slice : bool,
blanchet@48321
   332
   minimize : bool option,
blanchet@48321
   333
   timeout : Time.time,
blanchet@48321
   334
   preplay_timeout : Time.time,
blanchet@48321
   335
   expect : string}
blanchet@35867
   336
blanchet@48288
   337
type relevance_fudge =
blanchet@48288
   338
  {local_const_multiplier : real,
blanchet@48288
   339
   worse_irrel_freq : real,
blanchet@48288
   340
   higher_order_irrel_weight : real,
blanchet@48288
   341
   abs_rel_weight : real,
blanchet@48288
   342
   abs_irrel_weight : real,
blanchet@48288
   343
   skolem_irrel_weight : real,
blanchet@48288
   344
   theory_const_rel_weight : real,
blanchet@48288
   345
   theory_const_irrel_weight : real,
blanchet@48288
   346
   chained_const_irrel_weight : real,
blanchet@48288
   347
   intro_bonus : real,
blanchet@48288
   348
   elim_bonus : real,
blanchet@48288
   349
   simp_bonus : real,
blanchet@48288
   350
   local_bonus : real,
blanchet@48288
   351
   assum_bonus : real,
blanchet@48288
   352
   chained_bonus : real,
blanchet@48288
   353
   max_imperfect : real,
blanchet@48288
   354
   max_imperfect_exp : real,
blanchet@48288
   355
   threshold_divisor : real,
blanchet@48288
   356
   ridiculous_threshold : real}
blanchet@48288
   357
blanchet@41090
   358
datatype prover_fact =
blanchet@46340
   359
  Untranslated_Fact of (string * stature) * thm |
blanchet@46340
   360
  SMT_Weighted_Fact of (string * stature) * (int option * thm)
blanchet@40061
   361
blanchet@40061
   362
type prover_problem =
blanchet@48321
   363
  {state : Proof.state,
blanchet@48321
   364
   goal : thm,
blanchet@48321
   365
   subgoal : int,
blanchet@48321
   366
   subgoal_count : int,
blanchet@48321
   367
   facts : prover_fact list}
blanchet@35867
   368
blanchet@40061
   369
type prover_result =
blanchet@48321
   370
  {outcome : failure option,
blanchet@48321
   371
   used_facts : (string * stature) list,
blanchet@48321
   372
   run_time : Time.time,
blanchet@48321
   373
   preplay : unit -> play,
blanchet@48321
   374
   message : play -> string,
blanchet@48321
   375
   message_tail : string}
blanchet@35867
   376
blanchet@43051
   377
type prover =
blanchet@45520
   378
  params -> ((string * string list) list -> string -> minimize_command)
blanchet@45520
   379
  -> prover_problem -> prover_result
blanchet@35867
   380
blanchet@38023
   381
(* configuration attributes *)
blanchet@38023
   382
blanchet@43092
   383
(* Empty string means create files in Isabelle's temporary files directory. *)
wenzelm@42616
   384
val dest_dir =
wenzelm@42616
   385
  Attrib.setup_config_string @{binding sledgehammer_dest_dir} (K "")
wenzelm@42616
   386
val problem_prefix =
wenzelm@42616
   387
  Attrib.setup_config_string @{binding sledgehammer_problem_prefix} (K "prob")
blanchet@48143
   388
val completish =
blanchet@48143
   389
  Attrib.setup_config_bool @{binding sledgehammer_completish} (K false)
wenzelm@28484
   390
blanchet@43092
   391
(* In addition to being easier to read, readable names are often much shorter,
blanchet@44394
   392
   especially if types are mangled in names. This makes a difference for some
blanchet@44394
   393
   provers (e.g., E). For these reason, short names are enabled by default. *)
blanchet@44592
   394
val atp_full_names =
blanchet@44592
   395
  Attrib.setup_config_bool @{binding sledgehammer_atp_full_names} (K false)
blanchet@43092
   396
blanchet@42646
   397
val smt_triggers =
blanchet@42646
   398
  Attrib.setup_config_bool @{binding sledgehammer_smt_triggers} (K true)
blanchet@42646
   399
val smt_weights =
blanchet@42646
   400
  Attrib.setup_config_bool @{binding sledgehammer_smt_weights} (K true)
blanchet@42646
   401
val smt_weight_min_facts =
blanchet@42646
   402
  Attrib.setup_config_int @{binding sledgehammer_smt_weight_min_facts} (K 20)
blanchet@41256
   403
blanchet@41256
   404
(* FUDGE *)
blanchet@42646
   405
val smt_min_weight =
blanchet@42646
   406
  Attrib.setup_config_int @{binding sledgehammer_smt_min_weight} (K 0)
blanchet@42646
   407
val smt_max_weight =
blanchet@42646
   408
  Attrib.setup_config_int @{binding sledgehammer_smt_max_weight} (K 10)
blanchet@42646
   409
val smt_max_weight_index =
blanchet@42646
   410
  Attrib.setup_config_int @{binding sledgehammer_smt_max_weight_index} (K 200)
blanchet@41256
   411
val smt_weight_curve = Unsynchronized.ref (fn x : int => x * x)
blanchet@41256
   412
blanchet@42646
   413
fun smt_fact_weight ctxt j num_facts =
blanchet@42646
   414
  if Config.get ctxt smt_weights andalso
blanchet@42646
   415
     num_facts >= Config.get ctxt smt_weight_min_facts then
blanchet@42646
   416
    let
blanchet@42646
   417
      val min = Config.get ctxt smt_min_weight
blanchet@42646
   418
      val max = Config.get ctxt smt_max_weight
blanchet@42646
   419
      val max_index = Config.get ctxt smt_max_weight_index
blanchet@42646
   420
      val curve = !smt_weight_curve
blanchet@42646
   421
    in
blanchet@42646
   422
      SOME (max - (max - min + 1) * curve (Int.max (0, max_index - j - 1))
blanchet@42646
   423
            div curve max_index)
blanchet@42646
   424
    end
blanchet@41256
   425
  else
blanchet@41256
   426
    NONE
blanchet@41256
   427
blanchet@42646
   428
fun weight_smt_fact ctxt num_facts ((info, th), j) =
blanchet@42646
   429
  let val thy = Proof_Context.theory_of ctxt in
blanchet@42646
   430
    (info, (smt_fact_weight ctxt j num_facts, th |> Thm.transfer thy))
blanchet@42646
   431
  end
blanchet@38023
   432
blanchet@41091
   433
fun untranslated_fact (Untranslated_Fact p) = p
blanchet@41242
   434
  | untranslated_fact (SMT_Weighted_Fact (info, (_, th))) = (info, th)
blanchet@41256
   435
fun smt_weighted_fact _ _ (SMT_Weighted_Fact p, _) = p
blanchet@42646
   436
  | smt_weighted_fact ctxt num_facts (fact, j) =
blanchet@42646
   437
    (untranslated_fact fact, j) |> weight_smt_fact ctxt num_facts
blanchet@41256
   438
blanchet@41313
   439
fun overlord_file_location_for_prover prover =
blanchet@41313
   440
  (getenv "ISABELLE_HOME_USER", "prob_" ^ prover)
blanchet@41313
   441
blanchet@43052
   442
fun proof_banner mode name =
blanchet@43033
   443
  case mode of
blanchet@43033
   444
    Auto_Try => "Auto Sledgehammer (" ^ quote name ^ ") found a proof"
blanchet@43033
   445
  | Try => "Sledgehammer (" ^ quote name ^ ") found a proof"
blanchet@43052
   446
  | _ => "Try this"
blanchet@43033
   447
blanchet@45561
   448
fun bunch_of_reconstructors needs_full_types lam_trans =
blanchet@48800
   449
  if needs_full_types then
blanchet@48802
   450
    [Metis (full_type_enc, lam_trans false),
blanchet@48802
   451
     Metis (really_full_type_enc, lam_trans false),
blanchet@48802
   452
     Metis (full_type_enc, lam_trans true),
blanchet@48802
   453
     Metis (really_full_type_enc, lam_trans true),
blanchet@48802
   454
     SMT]
blanchet@48802
   455
  else
blanchet@48800
   456
    [Metis (partial_type_enc, lam_trans false),
blanchet@48800
   457
     Metis (full_type_enc, lam_trans false),
blanchet@48800
   458
     Metis (no_typesN, lam_trans true),
blanchet@48800
   459
     Metis (really_full_type_enc, lam_trans true),
blanchet@48800
   460
     SMT]
blanchet@45561
   461
blanchet@45561
   462
fun extract_reconstructor ({type_enc, lam_trans, ...} : params)
blanchet@45561
   463
                          (Metis (type_enc', lam_trans')) =
blanchet@45561
   464
    let
blanchet@45561
   465
      val override_params =
blanchet@45561
   466
        (if is_none type_enc andalso type_enc' = hd partial_type_encs then
blanchet@45561
   467
           []
blanchet@45561
   468
         else
blanchet@45566
   469
           [("type_enc", [hd (unalias_type_enc type_enc')])]) @
blanchet@45561
   470
        (if is_none lam_trans andalso lam_trans' = metis_default_lam_trans then
blanchet@45561
   471
           []
blanchet@45561
   472
         else
blanchet@45561
   473
           [("lam_trans", [lam_trans'])])
blanchet@45561
   474
    in (metisN, override_params) end
blanchet@45561
   475
  | extract_reconstructor _ SMT = (smtN, [])
blanchet@45561
   476
blanchet@43033
   477
(* based on "Mirabelle.can_apply" and generalized *)
blanchet@43034
   478
fun timed_apply timeout tac state i =
blanchet@43033
   479
  let
blanchet@43033
   480
    val {context = ctxt, facts, goal} = Proof.goal state
blanchet@43033
   481
    val full_tac = Method.insert_tac facts i THEN tac ctxt i
blanchet@43034
   482
  in TimeLimit.timeLimit timeout (try (Seq.pull o full_tac)) goal end
blanchet@43033
   483
blanchet@45519
   484
fun tac_for_reconstructor (Metis (type_enc, lam_trans)) =
blanchet@45521
   485
    metis_tac [type_enc] lam_trans
blanchet@45379
   486
  | tac_for_reconstructor SMT = SMT_Solver.smt_tac
blanchet@43034
   487
blanchet@45520
   488
fun timed_reconstructor reconstr debug timeout ths =
blanchet@44651
   489
  (Config.put Metis_Tactic.verbose debug
blanchet@45557
   490
   #> Config.put SMT_Config.verbose debug
blanchet@45520
   491
   #> (fn ctxt => tac_for_reconstructor reconstr ctxt ths))
blanchet@43034
   492
  |> timed_apply timeout
blanchet@43033
   493
blanchet@48798
   494
fun is_fact_chained ((_, (sc, _)), _) = sc = Chained
blanchet@48798
   495
blanchet@48798
   496
fun filter_used_facts keep_chained used =
blanchet@48798
   497
  filter ((member (op =) used o fst) orf
blanchet@48798
   498
          (if keep_chained then is_fact_chained else K false))
blanchet@43033
   499
blanchet@45781
   500
fun play_one_line_proof mode debug verbose timeout pairs state i preferred
blanchet@45520
   501
                        reconstrs =
blanchet@43034
   502
  let
blanchet@45379
   503
    val _ =
blanchet@45379
   504
      if mode = Minimize andalso Time.> (timeout, Time.zeroTime) then
blanchet@45379
   505
        Output.urgent_message "Preplaying proof..."
blanchet@45379
   506
      else
blanchet@45379
   507
        ()
blanchet@45781
   508
    val ths = pairs |> sort_wrt (fst o fst) |> map snd
blanchet@45520
   509
    fun get_preferred reconstrs =
blanchet@45520
   510
      if member (op =) reconstrs preferred then preferred
blanchet@45520
   511
      else List.last reconstrs
blanchet@45520
   512
    fun play [] [] = Failed_to_Play (get_preferred reconstrs)
blanchet@45379
   513
      | play timed_outs [] =
blanchet@45379
   514
        Trust_Playable (get_preferred timed_outs, SOME timeout)
blanchet@45520
   515
      | play timed_out (reconstr :: reconstrs) =
blanchet@45378
   516
        let
blanchet@45378
   517
          val _ =
blanchet@45378
   518
            if verbose then
blanchet@45554
   519
              "Trying \"" ^ string_for_reconstructor reconstr ^ "\" for " ^
blanchet@45378
   520
              string_from_time timeout ^ "..."
blanchet@45378
   521
              |> Output.urgent_message
blanchet@45378
   522
            else
blanchet@45378
   523
              ()
blanchet@45378
   524
          val timer = Timer.startRealTimer ()
blanchet@45378
   525
        in
blanchet@45520
   526
          case timed_reconstructor reconstr debug timeout ths state i of
blanchet@45520
   527
            SOME (SOME _) => Played (reconstr, Timer.checkRealTimer timer)
blanchet@45520
   528
          | _ => play timed_out reconstrs
blanchet@43044
   529
        end
blanchet@45520
   530
        handle TimeLimit.TimeOut => play (reconstr :: timed_out) reconstrs
blanchet@43034
   531
  in
blanchet@45379
   532
    if timeout = Time.zeroTime then
blanchet@45520
   533
      Trust_Playable (get_preferred reconstrs, NONE)
blanchet@45379
   534
    else
blanchet@45520
   535
      play [] reconstrs
blanchet@43033
   536
  end
blanchet@43033
   537
blanchet@41313
   538
blanchet@41256
   539
(* generic TPTP-based ATPs *)
blanchet@40061
   540
blanchet@42730
   541
(* Too general means, positive equality literal with a variable X as one
blanchet@42730
   542
   operand, when X does not occur properly in the other operand. This rules out
blanchet@42730
   543
   clearly inconsistent facts such as X = a | X = b, though it by no means
blanchet@42730
   544
   guarantees soundness. *)
blanchet@42730
   545
blanchet@42730
   546
(* Unwanted equalities are those between a (bound or schematic) variable that
blanchet@42730
   547
   does not properly occur in the second operand. *)
blanchet@42730
   548
val is_exhaustive_finite =
blanchet@42730
   549
  let
blanchet@42730
   550
    fun is_bad_equal (Var z) t =
blanchet@42730
   551
        not (exists_subterm (fn Var z' => z = z' | _ => false) t)
blanchet@42730
   552
      | is_bad_equal (Bound j) t = not (loose_bvar1 (t, j))
blanchet@42730
   553
      | is_bad_equal _ _ = false
blanchet@42730
   554
    fun do_equals t1 t2 = is_bad_equal t1 t2 orelse is_bad_equal t2 t1
blanchet@42730
   555
    fun do_formula pos t =
blanchet@42730
   556
      case (pos, t) of
blanchet@42730
   557
        (_, @{const Trueprop} $ t1) => do_formula pos t1
blanchet@42730
   558
      | (true, Const (@{const_name all}, _) $ Abs (_, _, t')) =>
blanchet@42730
   559
        do_formula pos t'
blanchet@42730
   560
      | (true, Const (@{const_name All}, _) $ Abs (_, _, t')) =>
blanchet@42730
   561
        do_formula pos t'
blanchet@42730
   562
      | (false, Const (@{const_name Ex}, _) $ Abs (_, _, t')) =>
blanchet@42730
   563
        do_formula pos t'
blanchet@42730
   564
      | (_, @{const "==>"} $ t1 $ t2) =>
blanchet@42730
   565
        do_formula (not pos) t1 andalso
blanchet@42730
   566
        (t2 = @{prop False} orelse do_formula pos t2)
blanchet@42730
   567
      | (_, @{const HOL.implies} $ t1 $ t2) =>
blanchet@42730
   568
        do_formula (not pos) t1 andalso
blanchet@42730
   569
        (t2 = @{const False} orelse do_formula pos t2)
blanchet@42730
   570
      | (_, @{const Not} $ t1) => do_formula (not pos) t1
blanchet@42730
   571
      | (true, @{const HOL.disj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
blanchet@42730
   572
      | (false, @{const HOL.conj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
blanchet@42730
   573
      | (true, Const (@{const_name HOL.eq}, _) $ t1 $ t2) => do_equals t1 t2
blanchet@42730
   574
      | (true, Const (@{const_name "=="}, _) $ t1 $ t2) => do_equals t1 t2
blanchet@42730
   575
      | _ => false
blanchet@42730
   576
  in do_formula true end
blanchet@42730
   577
blanchet@42730
   578
fun has_bound_or_var_of_type pred =
blanchet@42730
   579
  exists_subterm (fn Var (_, T as Type _) => pred T
blanchet@42730
   580
                   | Abs (_, T as Type _, _) => pred T
blanchet@42730
   581
                   | _ => false)
blanchet@42730
   582
blanchet@42730
   583
(* Facts are forbidden to contain variables of these types. The typical reason
blanchet@42730
   584
   is that they lead to unsoundness. Note that "unit" satisfies numerous
blanchet@42730
   585
   equations like "?x = ()". The resulting clauses will have no type constraint,
blanchet@42730
   586
   yielding false proofs. Even "bool" leads to many unsound proofs, though only
blanchet@42730
   587
   for higher-order problems. *)
blanchet@42730
   588
blanchet@42730
   589
(* Facts containing variables of type "unit" or "bool" or of the form
blanchet@42730
   590
   "ALL x. x = A | x = B | x = C" are likely to lead to unsound proofs if types
blanchet@42730
   591
   are omitted. *)
blanchet@42944
   592
fun is_dangerous_prop ctxt =
blanchet@42944
   593
  transform_elim_prop
blanchet@44393
   594
  #> (has_bound_or_var_of_type (is_type_surely_finite ctxt) orf
blanchet@42730
   595
      is_exhaustive_finite)
blanchet@42730
   596
blanchet@39492
   597
(* Important messages are important but not so important that users want to see
blanchet@39492
   598
   them each time. *)
blanchet@44649
   599
val atp_important_message_keep_quotient = 25
blanchet@39492
   600
blanchet@44416
   601
fun choose_type_enc soundness best_type_enc format =
blanchet@44397
   602
  the_default best_type_enc
blanchet@44397
   603
  #> type_enc_from_string soundness
blanchet@44416
   604
  #> adjust_type_enc format
blanchet@42548
   605
blanchet@43051
   606
val metis_minimize_max_time = seconds 2.0
blanchet@43051
   607
blanchet@45561
   608
fun choose_minimize_command params minimize_command name preplay =
blanchet@45520
   609
  let
blanchet@45520
   610
    val (name, override_params) =
blanchet@45520
   611
      case preplay of
blanchet@45520
   612
        Played (reconstr, time) =>
blanchet@45520
   613
        if Time.<= (time, metis_minimize_max_time) then
blanchet@45561
   614
          extract_reconstructor params reconstr
blanchet@45520
   615
        else
blanchet@45520
   616
          (name, [])
blanchet@45520
   617
      | _ => (name, [])
blanchet@45520
   618
  in minimize_command override_params name end
blanchet@43051
   619
blanchet@47962
   620
fun repair_monomorph_context max_iters best_max_iters max_new_instances
blanchet@47962
   621
                             best_max_new_instances =
blanchet@47962
   622
  Config.put Monomorph.max_rounds (max_iters |> the_default best_max_iters)
blanchet@47962
   623
  #> Config.put Monomorph.max_new_instances
blanchet@47962
   624
         (max_new_instances |> the_default best_max_new_instances)
boehmes@43230
   625
  #> Config.put Monomorph.keep_partial_instances false
blanchet@43226
   626
blanchet@44509
   627
fun suffix_for_mode Auto_Try = "_auto_try"
blanchet@44509
   628
  | suffix_for_mode Try = "_try"
blanchet@44509
   629
  | suffix_for_mode Normal = ""
blanchet@48392
   630
  | suffix_for_mode MaSh = "_mash"
blanchet@45574
   631
  | suffix_for_mode Auto_Minimize = "_auto_min"
blanchet@44509
   632
  | suffix_for_mode Minimize = "_min"
blanchet@44509
   633
blanchet@44423
   634
(* Give the ATPs some slack before interrupting them the hard way. "z3_tptp" on
blanchet@43631
   635
   Linux appears to be the only ATP that does not honor its time limit. *)
blanchet@43690
   636
val atp_timeout_slack = seconds 1.0
blanchet@43631
   637
blanchet@48331
   638
val mono_max_privileged_facts = 10
blanchet@48331
   639
blanchet@43021
   640
fun run_atp mode name
blanchet@48376
   641
        ({exec, arguments, proof_delims, known_failures, prem_role, best_slices,
blanchet@48376
   642
          best_max_mono_iters, best_max_new_mono_instances, ...} : atp_config)
blanchet@46301
   643
        (params as {debug, verbose, overlord, type_enc, strict, lam_trans,
blanchet@48293
   644
                    uncurried_aliases, max_facts, max_mono_iters,
blanchet@50020
   645
                    max_new_mono_instances, isar_proofs, isar_shrink,
blanchet@46409
   646
                    slice, timeout, preplay_timeout, ...})
blanchet@43037
   647
        minimize_command
blanchet@43037
   648
        ({state, goal, subgoal, subgoal_count, facts, ...} : prover_problem) =
blanchet@38023
   649
  let
blanchet@42182
   650
    val thy = Proof.theory_of state
blanchet@39318
   651
    val ctxt = Proof.context_of state
blanchet@47946
   652
    val atp_mode =
blanchet@48143
   653
      if Config.get ctxt completish then Sledgehammer_Completish
blanchet@47946
   654
      else Sledgehammer
blanchet@43004
   655
    val (_, hyp_ts, concl_t) = strip_subgoal ctxt goal subgoal
blanchet@41159
   656
    val (dest_dir, problem_prefix) =
blanchet@41159
   657
      if overlord then overlord_file_location_for_prover name
blanchet@41159
   658
      else (Config.get ctxt dest_dir, Config.get ctxt problem_prefix)
blanchet@40061
   659
    val problem_file_name =
blanchet@41159
   660
      Path.basic (problem_prefix ^ (if overlord then "" else serial_string ()) ^
blanchet@44509
   661
                  suffix_for_mode mode ^ "_" ^ string_of_int subgoal)
blanchet@48656
   662
    val prob_path =
blanchet@40059
   663
      if dest_dir = "" then
blanchet@40061
   664
        File.tmp_path problem_file_name
blanchet@40059
   665
      else if File.exists (Path.explode dest_dir) then
blanchet@40061
   666
        Path.append (Path.explode dest_dir) problem_file_name
blanchet@39003
   667
      else
blanchet@40059
   668
        error ("No such directory: " ^ quote dest_dir ^ ".")
blanchet@48376
   669
    val command0 =
blanchet@47055
   670
      case find_first (fn var => getenv var <> "") (fst exec) of
blanchet@48376
   671
        SOME var =>
blanchet@48376
   672
        let
blanchet@48376
   673
          val pref = getenv var ^ "/"
blanchet@48376
   674
          val paths = map (Path.explode o prefix pref) (snd exec)
blanchet@48376
   675
        in
blanchet@48376
   676
          case find_first File.exists paths of
blanchet@48376
   677
            SOME path => path
blanchet@48376
   678
          | NONE => error ("Bad executable: " ^ Path.print (hd paths) ^ ".")
blanchet@48376
   679
        end
blanchet@47055
   680
      | NONE => error ("The environment variable " ^ quote (hd (fst exec)) ^
blanchet@47055
   681
                       " is not set.")
blanchet@38023
   682
    fun split_time s =
blanchet@38023
   683
      let
blanchet@42448
   684
        val split = String.tokens (fn c => str c = "\n")
blanchet@47737
   685
        val (output, t) =
blanchet@47737
   686
          s |> split |> (try split_last #> the_default ([], "0"))
blanchet@47737
   687
            |>> cat_lines
blanchet@42448
   688
        fun as_num f = f >> (fst o read_int)
blanchet@42448
   689
        val num = as_num (Scan.many1 Symbol.is_ascii_digit)
blanchet@42448
   690
        val digit = Scan.one Symbol.is_ascii_digit
blanchet@42448
   691
        val num3 = as_num (digit ::: digit ::: (digit >> single))
blanchet@42448
   692
        val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b)
blanchet@45381
   693
        val as_time =
blanchet@45381
   694
          raw_explode #> Scan.read Symbol.stopper time #> the_default 0
blanchet@47737
   695
      in (output, as_time t |> Time.fromMilliseconds) end
blanchet@48656
   696
    fun run () =
blanchet@48376
   697
      let
blanchet@48376
   698
        (* If slicing is disabled, we expand the last slice to fill the entire
blanchet@48376
   699
           time available. *)
blanchet@48376
   700
        val actual_slices = get_slices slice (best_slices ctxt)
blanchet@48376
   701
        val num_actual_slices = length actual_slices
blanchet@48376
   702
        fun monomorphize_facts facts =
blanchet@48376
   703
          let
blanchet@48376
   704
            val ctxt =
blanchet@48376
   705
              ctxt
blanchet@48376
   706
              |> repair_monomorph_context max_mono_iters best_max_mono_iters
blanchet@48376
   707
                      max_new_mono_instances best_max_new_mono_instances
blanchet@48376
   708
            (* pseudo-theorem involving the same constants as the subgoal *)
blanchet@48376
   709
            val subgoal_th =
blanchet@48376
   710
              Logic.list_implies (hyp_ts, concl_t) |> Skip_Proof.make_thm thy
blanchet@48376
   711
            val rths =
blanchet@48376
   712
              facts |> chop mono_max_privileged_facts
blanchet@48376
   713
                    |>> map (pair 1 o snd)
blanchet@48376
   714
                    ||> map (pair 2 o snd)
blanchet@48376
   715
                    |> op @
blanchet@48376
   716
                    |> cons (0, subgoal_th)
blanchet@48376
   717
          in
blanchet@48376
   718
            Monomorph.monomorph atp_schematic_consts_of rths ctxt |> fst |> tl
blanchet@48376
   719
            |> curry ListPair.zip (map fst facts)
blanchet@48376
   720
            |> maps (fn (name, rths) =>
blanchet@48376
   721
                        map (pair name o zero_var_indexes o snd) rths)
blanchet@48376
   722
          end
blanchet@48376
   723
        fun run_slice time_left (cache_key, cache_value)
blanchet@48716
   724
                (slice, (time_frac,
blanchet@48376
   725
                     (key as (best_max_facts, format, best_type_enc,
blanchet@48376
   726
                              best_lam_trans, best_uncurried_aliases),
blanchet@48716
   727
                      extra))) =
blanchet@38032
   728
          let
blanchet@48376
   729
            val num_facts =
blanchet@48376
   730
              length facts |> is_none max_facts ? Integer.min best_max_facts
blanchet@48376
   731
            val soundness = if strict then Strict else Non_Strict
blanchet@48376
   732
            val type_enc =
blanchet@48376
   733
              type_enc |> choose_type_enc soundness best_type_enc format
blanchet@48376
   734
            val sound = is_type_enc_sound type_enc
blanchet@48376
   735
            val real_ms = Real.fromInt o Time.toMilliseconds
blanchet@48376
   736
            val slice_timeout =
blanchet@48376
   737
              ((real_ms time_left
blanchet@48376
   738
                |> (if slice < num_actual_slices - 1 then
blanchet@48376
   739
                      curry Real.min (time_frac * real_ms timeout)
blanchet@48376
   740
                    else
blanchet@48376
   741
                      I))
blanchet@48376
   742
               * 0.001) |> seconds
blanchet@48376
   743
            val generous_slice_timeout =
blanchet@48376
   744
              Time.+ (slice_timeout, atp_timeout_slack)
blanchet@48376
   745
            val _ =
blanchet@48376
   746
              if debug then
blanchet@48376
   747
                quote name ^ " slice #" ^ string_of_int (slice + 1) ^
blanchet@48376
   748
                " with " ^ string_of_int num_facts ^ " fact" ^
blanchet@48376
   749
                plural_s num_facts ^ " for " ^
blanchet@48376
   750
                string_from_time slice_timeout ^ "..."
blanchet@48376
   751
                |> Output.urgent_message
blanchet@48376
   752
              else
blanchet@48376
   753
                ()
blanchet@48376
   754
            val readable_names = not (Config.get ctxt atp_full_names)
blanchet@48376
   755
            val lam_trans =
blanchet@48376
   756
              case lam_trans of
blanchet@48376
   757
                SOME s => s
blanchet@48376
   758
              | NONE => best_lam_trans
blanchet@48376
   759
            val uncurried_aliases =
blanchet@48376
   760
              case uncurried_aliases of
blanchet@48376
   761
                SOME b => b
blanchet@48376
   762
              | NONE => best_uncurried_aliases
blanchet@48376
   763
            val value as (atp_problem, _, fact_names, _, _) =
blanchet@48376
   764
              if cache_key = SOME key then
blanchet@48376
   765
                cache_value
blanchet@48376
   766
              else
blanchet@48376
   767
                facts
blanchet@48376
   768
                |> map untranslated_fact
blanchet@48376
   769
                |> not sound
blanchet@48376
   770
                   ? filter_out (is_dangerous_prop ctxt o prop_of o snd)
blanchet@48376
   771
                |> take num_facts
blanchet@48376
   772
                |> not (is_type_enc_polymorphic type_enc) ? monomorphize_facts
blanchet@48376
   773
                |> map (apsnd prop_of)
blanchet@48376
   774
                |> prepare_atp_problem ctxt format prem_role type_enc atp_mode
blanchet@48376
   775
                                       lam_trans uncurried_aliases
blanchet@48376
   776
                                       readable_names true hyp_ts concl_t
blanchet@48376
   777
            fun sel_weights () = atp_problem_selection_weights atp_problem
blanchet@48376
   778
            fun ord_info () = atp_problem_term_order_info atp_problem
blanchet@48376
   779
            val ord = effective_term_order ctxt name
blanchet@49918
   780
            val full_proof = debug orelse isar_proofs
blanchet@48376
   781
            val args = arguments ctxt full_proof extra slice_timeout
blanchet@48376
   782
                                 (ord, ord_info, sel_weights)
blanchet@48376
   783
            val command =
blanchet@48532
   784
              "(exec 2>&1; " ^ File.shell_path command0 ^ " " ^ args ^ " " ^
blanchet@48656
   785
              File.shell_path prob_path ^ ")"
blanchet@48532
   786
              |> enclose "TIMEFORMAT='%3R'; { time " " ; }"
blanchet@48376
   787
            val _ =
blanchet@48376
   788
              atp_problem
blanchet@48376
   789
              |> lines_for_atp_problem format ord ord_info
blanchet@48376
   790
              |> cons ("% " ^ command ^ "\n")
blanchet@48656
   791
              |> File.write_list prob_path
blanchet@48376
   792
            val ((output, run_time), (atp_proof, outcome)) =
blanchet@48376
   793
              TimeLimit.timeLimit generous_slice_timeout
blanchet@48376
   794
                                  Isabelle_System.bash_output command
blanchet@48376
   795
              |>> (if overlord then
blanchet@48376
   796
                     prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n")
blanchet@48376
   797
                   else
blanchet@48376
   798
                     I)
blanchet@48376
   799
              |> fst |> split_time
blanchet@48376
   800
              |> (fn accum as (output, _) =>
blanchet@48376
   801
                     (accum,
blanchet@48716
   802
                      extract_tstplike_proof_and_outcome verbose proof_delims
blanchet@48716
   803
                                                         known_failures output
blanchet@48376
   804
                      |>> atp_proof_from_tstplike_proof atp_problem
blanchet@48376
   805
                      handle UNRECOGNIZED_ATP_PROOF () =>
blanchet@48376
   806
                             ([], SOME ProofIncomplete)))
blanchet@48376
   807
              handle TimeLimit.TimeOut =>
blanchet@48376
   808
                     (("", slice_timeout), ([], SOME TimedOut))
blanchet@48376
   809
            val outcome =
blanchet@48376
   810
              case outcome of
blanchet@48376
   811
                NONE =>
blanchet@48376
   812
                (case used_facts_in_unsound_atp_proof ctxt fact_names atp_proof
blanchet@48376
   813
                      |> Option.map (sort string_ord) of
blanchet@48376
   814
                   SOME facts =>
blanchet@48376
   815
                   let
blanchet@48376
   816
                     val failure =
blanchet@48376
   817
                       UnsoundProof (is_type_enc_sound type_enc, facts)
blanchet@48376
   818
                   in
blanchet@48376
   819
                     if debug then (warning (string_for_failure failure); NONE)
blanchet@48376
   820
                     else SOME failure
blanchet@48376
   821
                   end
blanchet@48376
   822
                 | NONE => NONE)
blanchet@48376
   823
              | _ => outcome
blanchet@48376
   824
          in ((SOME key, value), (output, run_time, atp_proof, outcome)) end
blanchet@48376
   825
        val timer = Timer.startRealTimer ()
blanchet@48376
   826
        fun maybe_run_slice slice
blanchet@48376
   827
                (result as (cache, (_, run_time0, _, SOME _))) =
blanchet@48376
   828
            let
blanchet@48376
   829
              val time_left = Time.- (timeout, Timer.checkRealTimer timer)
blanchet@48376
   830
            in
blanchet@48376
   831
              if Time.<= (time_left, Time.zeroTime) then
blanchet@48376
   832
                result
blanchet@48376
   833
              else
blanchet@48376
   834
                run_slice time_left cache slice
blanchet@48376
   835
                |> (fn (cache, (output, run_time, atp_proof, outcome)) =>
blanchet@48376
   836
                       (cache, (output, Time.+ (run_time0, run_time),
blanchet@48376
   837
                                atp_proof, outcome)))
blanchet@48376
   838
            end
blanchet@48376
   839
          | maybe_run_slice _ result = result
blanchet@48376
   840
      in
blanchet@48376
   841
        ((NONE, ([], Symtab.empty, Vector.fromList [], [], Symtab.empty)),
blanchet@48376
   842
         ("", Time.zeroTime, [], SOME InternalError))
blanchet@48376
   843
        |> fold maybe_run_slice actual_slices
blanchet@48376
   844
      end
blanchet@38023
   845
    (* If the problem file has not been exported, remove it; otherwise, export
blanchet@38023
   846
       the proof file too. *)
blanchet@48656
   847
    fun clean_up () =
blanchet@48656
   848
      if dest_dir = "" then (try File.rm prob_path; ()) else ()
blanchet@48656
   849
    fun export (_, (output, _, _, _)) =
blanchet@48376
   850
      if dest_dir = "" then ()
blanchet@48656
   851
      else File.write (Path.explode (Path.implode prob_path ^ "_proof")) output
blanchet@46407
   852
    val ((_, (_, pool, fact_names, _, sym_tab)),
blanchet@46407
   853
         (output, run_time, atp_proof, outcome)) =
blanchet@48656
   854
      with_cleanup clean_up run () |> tap export
blanchet@39492
   855
    val important_message =
blanchet@43021
   856
      if mode = Normal andalso
blanchet@42609
   857
         random_range 0 (atp_important_message_keep_quotient - 1) = 0 then
blanchet@39492
   858
        extract_important_message output
blanchet@39492
   859
      else
blanchet@39492
   860
        ""
blanchet@43261
   861
    val (used_facts, preplay, message, message_tail) =
blanchet@38023
   862
      case outcome of
blanchet@38023
   863
        NONE =>
blanchet@43033
   864
        let
blanchet@45551
   865
          val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof
blanchet@45590
   866
          val needs_full_types = is_typed_helper_used_in_atp_proof atp_proof
blanchet@45521
   867
          val reconstrs =
blanchet@45554
   868
            bunch_of_reconstructors needs_full_types
blanchet@45560
   869
                (lam_trans_from_atp_proof atp_proof
blanchet@46405
   870
                 o (fn desperate => if desperate then hide_lamsN
blanchet@46405
   871
                                    else metis_default_lam_trans))
blanchet@43033
   872
        in
blanchet@43052
   873
          (used_facts,
blanchet@43052
   874
           fn () =>
blanchet@43052
   875
              let
blanchet@45781
   876
                val used_pairs =
blanchet@48798
   877
                  facts |> map untranslated_fact
blanchet@48798
   878
                        |> filter_used_facts false used_facts
blanchet@43052
   879
              in
blanchet@45781
   880
                play_one_line_proof mode debug verbose preplay_timeout
blanchet@45781
   881
                    used_pairs state subgoal (hd reconstrs) reconstrs
blanchet@43052
   882
              end,
blanchet@43052
   883
           fn preplay =>
blanchet@43052
   884
              let
blanchet@49921
   885
                val _ =
blanchet@49921
   886
                  if verbose then
blanchet@49921
   887
                    Output.urgent_message "Generating proof text..."
blanchet@49921
   888
                  else
blanchet@49921
   889
                    ()
blanchet@43052
   890
                val isar_params =
blanchet@50020
   891
                  (debug, verbose, preplay_timeout, isar_shrink,
blanchet@50004
   892
                   pool, fact_names, sym_tab, atp_proof, goal)
blanchet@43052
   893
                val one_line_params =
blanchet@43052
   894
                  (preplay, proof_banner mode name, used_facts,
blanchet@45561
   895
                   choose_minimize_command params minimize_command name preplay,
blanchet@43052
   896
                   subgoal, subgoal_count)
blanchet@48799
   897
                val num_chained = length (#facts (Proof.goal state))
blanchet@48799
   898
              in
blanchet@49918
   899
                proof_text ctxt isar_proofs isar_params num_chained
blanchet@48799
   900
                           one_line_params
blanchet@48799
   901
              end,
blanchet@43261
   902
           (if verbose then
blanchet@45381
   903
              "\nATP real CPU time: " ^ string_from_time run_time ^ "."
blanchet@43261
   904
            else
blanchet@43261
   905
              "") ^
blanchet@43261
   906
           (if important_message <> "" then
blanchet@43261
   907
              "\n\nImportant message from Dr. Geoff Sutcliffe:\n" ^
blanchet@43261
   908
              important_message
blanchet@43261
   909
            else
blanchet@43261
   910
              ""))
blanchet@43033
   911
        end
blanchet@43052
   912
      | SOME failure =>
blanchet@45519
   913
        ([], K (Failed_to_Play plain_metis),
blanchet@45519
   914
         fn _ => string_for_failure failure, "")
blanchet@38023
   915
  in
blanchet@45381
   916
    {outcome = outcome, used_facts = used_facts, run_time = run_time,
blanchet@43261
   917
     preplay = preplay, message = message, message_tail = message_tail}
blanchet@38023
   918
  end
blanchet@38023
   919
blanchet@40669
   920
(* "SMT_Failure.Abnormal_Termination" carries the solver's return code. Until
blanchet@40669
   921
   these are sorted out properly in the SMT module, we have to interpret these
blanchet@40669
   922
   ourselves. *)
blanchet@40684
   923
val remote_smt_failures =
blanchet@43631
   924
  [(2, NoLibwwwPerl),
blanchet@43631
   925
   (22, CantConnect)]
blanchet@40684
   926
val z3_failures =
blanchet@41236
   927
  [(101, OutOfResources),
blanchet@41236
   928
   (103, MalformedInput),
blanchet@41222
   929
   (110, MalformedInput)]
blanchet@40684
   930
val unix_failures =
blanchet@48797
   931
  [(138, Crashed),
blanchet@48797
   932
   (139, Crashed)]
blanchet@43631
   933
val smt_failures = remote_smt_failures @ z3_failures @ unix_failures
blanchet@40555
   934
blanchet@42100
   935
fun failure_from_smt_failure (SMT_Failure.Counterexample {is_real_cex, ...}) =
blanchet@43050
   936
    if is_real_cex then Unprovable else GaveUp
blanchet@41222
   937
  | failure_from_smt_failure SMT_Failure.Time_Out = TimedOut
blanchet@41222
   938
  | failure_from_smt_failure (SMT_Failure.Abnormal_Termination code) =
blanchet@41222
   939
    (case AList.lookup (op =) smt_failures code of
blanchet@40684
   940
       SOME failure => failure
blanchet@41259
   941
     | NONE => UnknownError ("Abnormal termination with exit code " ^
blanchet@41259
   942
                             string_of_int code ^ "."))
blanchet@41222
   943
  | failure_from_smt_failure SMT_Failure.Out_Of_Memory = OutOfResources
blanchet@41222
   944
  | failure_from_smt_failure (SMT_Failure.Other_Failure msg) =
blanchet@42061
   945
    UnknownError msg
blanchet@40063
   946
blanchet@40698
   947
(* FUDGE *)
blanchet@42646
   948
val smt_max_slices =
blanchet@42646
   949
  Attrib.setup_config_int @{binding sledgehammer_smt_max_slices} (K 8)
blanchet@42646
   950
val smt_slice_fact_frac =
blanchet@42646
   951
  Attrib.setup_config_real @{binding sledgehammer_smt_slice_fact_frac} (K 0.5)
blanchet@42646
   952
val smt_slice_time_frac =
blanchet@42646
   953
  Attrib.setup_config_real @{binding sledgehammer_smt_slice_time_frac} (K 0.5)
blanchet@42646
   954
val smt_slice_min_secs =
blanchet@42646
   955
  Attrib.setup_config_int @{binding sledgehammer_smt_slice_min_secs} (K 5)
blanchet@40409
   956
blanchet@42646
   957
fun smt_filter_loop ctxt name
blanchet@42724
   958
                    ({debug, verbose, overlord, max_mono_iters,
blanchet@45706
   959
                      max_new_mono_instances, timeout, slice, ...} : params)
blanchet@47531
   960
                    state i =
blanchet@40409
   961
  let
blanchet@45706
   962
    val max_slices = if slice then Config.get ctxt smt_max_slices else 1
blanchet@41242
   963
    val repair_context =
blanchet@43233
   964
      select_smt_solver name
blanchet@45557
   965
      #> Config.put SMT_Config.verbose debug
blanchet@43233
   966
      #> (if overlord then
blanchet@43233
   967
            Config.put SMT_Config.debug_files
blanchet@43233
   968
                       (overlord_file_location_for_prover name
blanchet@43233
   969
                        |> (fn (path, name) => path ^ "/" ^ name))
blanchet@43233
   970
          else
blanchet@43233
   971
            I)
blanchet@43233
   972
      #> Config.put SMT_Config.infer_triggers (Config.get ctxt smt_triggers)
blanchet@41242
   973
    val state = state |> Proof.map_context repair_context
blanchet@42443
   974
    fun do_slice timeout slice outcome0 time_so_far facts =
blanchet@40553
   975
      let
blanchet@40553
   976
        val timer = Timer.startRealTimer ()
blanchet@42724
   977
        val state =
blanchet@42724
   978
          state |> Proof.map_context
blanchet@43267
   979
                       (repair_monomorph_context max_mono_iters
blanchet@47962
   980
                            default_max_mono_iters max_new_mono_instances
blanchet@47962
   981
                            default_max_new_mono_instances)
blanchet@40553
   982
        val ms = timeout |> Time.toMilliseconds
blanchet@42443
   983
        val slice_timeout =
blanchet@42443
   984
          if slice < max_slices then
blanchet@41169
   985
            Int.min (ms,
blanchet@42646
   986
                Int.max (1000 * Config.get ctxt smt_slice_min_secs,
blanchet@42646
   987
                    Real.ceil (Config.get ctxt smt_slice_time_frac
blanchet@42646
   988
                               * Real.fromInt ms)))
blanchet@40553
   989
            |> Time.fromMilliseconds
blanchet@40553
   990
          else
blanchet@40553
   991
            timeout
blanchet@40553
   992
        val num_facts = length facts
blanchet@40553
   993
        val _ =
blanchet@42614
   994
          if debug then
blanchet@42614
   995
            quote name ^ " slice " ^ string_of_int slice ^ " with " ^
blanchet@42614
   996
            string_of_int num_facts ^ " fact" ^ plural_s num_facts ^ " for " ^
blanchet@42614
   997
            string_from_time slice_timeout ^ "..."
blanchet@40553
   998
            |> Output.urgent_message
blanchet@40553
   999
          else
blanchet@40553
  1000
            ()
blanchet@41168
  1001
        val birth = Timer.checkRealTimer timer
blanchet@41171
  1002
        val _ =
blanchet@41211
  1003
          if debug then Output.urgent_message "Invoking SMT solver..." else ()
blanchet@41209
  1004
        val (outcome, used_facts) =
blanchet@47531
  1005
          SMT_Solver.smt_filter_preprocess state facts i
blanchet@42443
  1006
          |> SMT_Solver.smt_filter_apply slice_timeout
blanchet@41239
  1007
          |> (fn {outcome, used_facts} => (outcome, used_facts))
blanchet@41209
  1008
          handle exn => if Exn.is_interrupt exn then
blanchet@41209
  1009
                          reraise exn
blanchet@41209
  1010
                        else
blanchet@42061
  1011
                          (ML_Compiler.exn_message exn
blanchet@41209
  1012
                           |> SMT_Failure.Other_Failure |> SOME, [])
blanchet@41168
  1013
        val death = Timer.checkRealTimer timer
blanchet@40553
  1014
        val outcome0 = if is_none outcome0 then SOME outcome else outcome0
blanchet@41168
  1015
        val time_so_far = Time.+ (time_so_far, Time.- (death, birth))
blanchet@40553
  1016
        val too_many_facts_perhaps =
blanchet@40553
  1017
          case outcome of
blanchet@40553
  1018
            NONE => false
blanchet@40553
  1019
          | SOME (SMT_Failure.Counterexample _) => false
blanchet@42443
  1020
          | SOME SMT_Failure.Time_Out => slice_timeout <> timeout
blanchet@42614
  1021
          | SOME (SMT_Failure.Abnormal_Termination _) => true (* kind of *)
blanchet@40553
  1022
          | SOME SMT_Failure.Out_Of_Memory => true
blanchet@41211
  1023
          | SOME (SMT_Failure.Other_Failure _) => true
blanchet@40553
  1024
        val timeout = Time.- (timeout, Timer.checkRealTimer timer)
blanchet@40553
  1025
      in
blanchet@42443
  1026
        if too_many_facts_perhaps andalso slice < max_slices andalso
blanchet@40553
  1027
           num_facts > 0 andalso Time.> (timeout, Time.zeroTime) then
blanchet@41169
  1028
          let
blanchet@42614
  1029
            val new_num_facts =
blanchet@42646
  1030
              Real.ceil (Config.get ctxt smt_slice_fact_frac
blanchet@42646
  1031
                         * Real.fromInt num_facts)
blanchet@42614
  1032
            val _ =
blanchet@42614
  1033
              if verbose andalso is_some outcome then
blanchet@42614
  1034
                quote name ^ " invoked with " ^ string_of_int num_facts ^
blanchet@42614
  1035
                " fact" ^ plural_s num_facts ^ ": " ^
blanchet@42614
  1036
                string_for_failure (failure_from_smt_failure (the outcome)) ^
blanchet@42614
  1037
                " Retrying with " ^ string_of_int new_num_facts ^ " fact" ^
blanchet@42638
  1038
                plural_s new_num_facts ^ "..."
blanchet@42614
  1039
                |> Output.urgent_message
blanchet@42614
  1040
              else
blanchet@42614
  1041
                ()
blanchet@42443
  1042
          in
blanchet@42614
  1043
            facts |> take new_num_facts
blanchet@42614
  1044
                  |> do_slice timeout (slice + 1) outcome0 time_so_far
blanchet@42443
  1045
          end
blanchet@40553
  1046
        else
blanchet@40553
  1047
          {outcome = if is_none outcome then NONE else the outcome0,
blanchet@45370
  1048
           used_facts = used_facts, run_time = time_so_far}
blanchet@40409
  1049
      end
blanchet@42443
  1050
  in do_slice timeout 1 NONE Time.zeroTime end
blanchet@40409
  1051
blanchet@43052
  1052
fun run_smt_solver mode name (params as {debug, verbose, preplay_timeout, ...})
blanchet@43011
  1053
        minimize_command
blanchet@47531
  1054
        ({state, subgoal, subgoal_count, facts, ...} : prover_problem) =
blanchet@36379
  1055
  let
blanchet@41242
  1056
    val ctxt = Proof.context_of state
blanchet@41256
  1057
    val num_facts = length facts
blanchet@41256
  1058
    val facts = facts ~~ (0 upto num_facts - 1)
blanchet@42646
  1059
                |> map (smt_weighted_fact ctxt num_facts)
blanchet@45781
  1060
    val {outcome, used_facts = used_pairs, run_time} =
blanchet@47531
  1061
      smt_filter_loop ctxt name params state subgoal facts
blanchet@45781
  1062
    val used_facts = used_pairs |> map fst
blanchet@41222
  1063
    val outcome = outcome |> Option.map failure_from_smt_failure
blanchet@43261
  1064
    val (preplay, message, message_tail) =
blanchet@40184
  1065
      case outcome of
blanchet@40184
  1066
        NONE =>
blanchet@43052
  1067
        (fn () =>
blanchet@45781
  1068
            play_one_line_proof mode debug verbose preplay_timeout used_pairs
blanchet@45554
  1069
                state subgoal SMT
blanchet@45560
  1070
                (bunch_of_reconstructors false
blanchet@48800
  1071
                     (fn desperate =>
blanchet@48800
  1072
                         if desperate then liftingN
blanchet@48800
  1073
                         else metis_default_lam_trans)),
blanchet@43052
  1074
         fn preplay =>
blanchet@43052
  1075
            let
blanchet@43052
  1076
              val one_line_params =
blanchet@43052
  1077
                (preplay, proof_banner mode name, used_facts,
blanchet@45561
  1078
                 choose_minimize_command params minimize_command name preplay,
blanchet@43052
  1079
                 subgoal, subgoal_count)
blanchet@48799
  1080
              val num_chained = length (#facts (Proof.goal state))
blanchet@48799
  1081
            in one_line_proof_text num_chained one_line_params end,
blanchet@43261
  1082
         if verbose then
blanchet@45370
  1083
           "\nSMT solver real CPU time: " ^ string_from_time run_time ^ "."
blanchet@43261
  1084
         else
blanchet@43261
  1085
           "")
blanchet@43166
  1086
      | SOME failure =>
blanchet@45519
  1087
        (K (Failed_to_Play plain_metis), fn _ => string_for_failure failure, "")
blanchet@40063
  1088
  in
blanchet@45370
  1089
    {outcome = outcome, used_facts = used_facts, run_time = run_time,
blanchet@45370
  1090
     preplay = preplay, message = message, message_tail = message_tail}
blanchet@40063
  1091
  end
blanchet@40063
  1092
blanchet@45520
  1093
fun run_reconstructor mode name
blanchet@45561
  1094
        (params as {debug, verbose, timeout, type_enc, lam_trans, ...})
blanchet@45379
  1095
        minimize_command
blanchet@45379
  1096
        ({state, subgoal, subgoal_count, facts, ...} : prover_problem) =
blanchet@43050
  1097
  let
blanchet@45520
  1098
    val reconstr =
blanchet@45520
  1099
      if name = metisN then
blanchet@45520
  1100
        Metis (type_enc |> the_default (hd partial_type_encs),
blanchet@45520
  1101
               lam_trans |> the_default metis_default_lam_trans)
blanchet@45520
  1102
      else if name = smtN then
blanchet@45520
  1103
        SMT
blanchet@45520
  1104
      else
blanchet@45520
  1105
        raise Fail ("unknown reconstructor: " ^ quote name)
blanchet@45781
  1106
    val used_pairs = facts |> map untranslated_fact
blanchet@45781
  1107
    val used_facts = used_pairs |> map fst
blanchet@43050
  1108
  in
blanchet@45379
  1109
    case play_one_line_proof (if mode = Minimize then Normal else mode) debug
blanchet@45781
  1110
                             verbose timeout used_pairs state subgoal reconstr
blanchet@45520
  1111
                             [reconstr] of
blanchet@43050
  1112
      play as Played (_, time) =>
blanchet@45370
  1113
      {outcome = NONE, used_facts = used_facts, run_time = time,
blanchet@43052
  1114
       preplay = K play,
blanchet@45561
  1115
       message =
blanchet@45561
  1116
         fn play =>
blanchet@45561
  1117
            let
blanchet@45561
  1118
              val (_, override_params) = extract_reconstructor params reconstr
blanchet@45561
  1119
              val one_line_params =
blanchet@45561
  1120
                (play, proof_banner mode name, used_facts,
blanchet@45561
  1121
                 minimize_command override_params name, subgoal,
blanchet@45561
  1122
                 subgoal_count)
blanchet@48799
  1123
              val num_chained = length (#facts (Proof.goal state))
blanchet@48799
  1124
            in one_line_proof_text num_chained one_line_params end,
blanchet@43261
  1125
       message_tail = ""}
blanchet@43052
  1126
    | play =>
blanchet@43166
  1127
      let
blanchet@43166
  1128
        val failure = case play of Failed_to_Play _ => GaveUp | _ => TimedOut
blanchet@43166
  1129
      in
blanchet@45370
  1130
        {outcome = SOME failure, used_facts = [], run_time = Time.zeroTime,
blanchet@43261
  1131
         preplay = K play, message = fn _ => string_for_failure failure,
blanchet@43261
  1132
         message_tail = ""}
blanchet@43050
  1133
      end
blanchet@43050
  1134
  end
blanchet@43050
  1135
blanchet@43021
  1136
fun get_prover ctxt mode name =
wenzelm@42361
  1137
  let val thy = Proof_Context.theory_of ctxt in
blanchet@45379
  1138
    if is_reconstructor name then run_reconstructor mode name
blanchet@47606
  1139
    else if is_atp thy name then run_atp mode name (get_atp thy name ())
blanchet@43052
  1140
    else if is_smt_prover ctxt name then run_smt_solver mode name
blanchet@43052
  1141
    else error ("No such prover: " ^ name ^ ".")
blanchet@40941
  1142
  end
blanchet@40063
  1143
wenzelm@28582
  1144
end;