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