src/HOL/Tools/Sledgehammer/sledgehammer_provers.ML
author blanchet
Sat Dec 15 19:57:12 2012 +0100 (2012-12-15 ago)
changeset 50557 31313171deb5
parent 50494 06b199a042ff
child 50558 a719106124d8
permissions -rw-r--r--
thread no timeout properly
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@50557
    40
     timeout : Time.time option,
blanchet@50557
    41
     preplay_timeout : Time.time option,
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@50557
   333
   timeout : Time.time option,
blanchet@50557
   334
   preplay_timeout : Time.time option,
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@50557
   482
  in time_limit 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@45520
   503
    fun get_preferred reconstrs =
blanchet@45520
   504
      if member (op =) reconstrs preferred then preferred
blanchet@45520
   505
      else List.last reconstrs
blanchet@43034
   506
  in
blanchet@50557
   507
    if timeout = SOME Time.zeroTime then
blanchet@45520
   508
      Trust_Playable (get_preferred reconstrs, NONE)
blanchet@45379
   509
    else
blanchet@50557
   510
      let
blanchet@50557
   511
        val _ =
blanchet@50557
   512
          if mode = Minimize then Output.urgent_message "Preplaying proof..."
blanchet@50557
   513
          else ()
blanchet@50557
   514
        val ths = pairs |> sort_wrt (fst o fst) |> map snd
blanchet@50557
   515
        fun play [] [] = Failed_to_Play (get_preferred reconstrs)
blanchet@50557
   516
          | play timed_outs [] =
blanchet@50557
   517
            Trust_Playable (get_preferred timed_outs, timeout)
blanchet@50557
   518
          | play timed_out (reconstr :: reconstrs) =
blanchet@50557
   519
            let
blanchet@50557
   520
              val _ =
blanchet@50557
   521
                if verbose then
blanchet@50557
   522
                  "Trying \"" ^ string_for_reconstructor reconstr ^ "\"" ^
blanchet@50557
   523
                  (case timeout of
blanchet@50557
   524
                     SOME timeout => " for " ^ string_from_time timeout
blanchet@50557
   525
                   | NONE => "") ^ "..."
blanchet@50557
   526
                  |> Output.urgent_message
blanchet@50557
   527
                else
blanchet@50557
   528
                  ()
blanchet@50557
   529
              val timer = Timer.startRealTimer ()
blanchet@50557
   530
            in
blanchet@50557
   531
              case timed_reconstructor reconstr debug timeout ths state i of
blanchet@50557
   532
                SOME (SOME _) => Played (reconstr, Timer.checkRealTimer timer)
blanchet@50557
   533
              | _ => play timed_out reconstrs
blanchet@50557
   534
            end
blanchet@50557
   535
            handle TimeLimit.TimeOut => play (reconstr :: timed_out) reconstrs
blanchet@50557
   536
      in play [] reconstrs end
blanchet@43033
   537
  end
blanchet@43033
   538
blanchet@41313
   539
blanchet@41256
   540
(* generic TPTP-based ATPs *)
blanchet@40061
   541
blanchet@42730
   542
(* Too general means, positive equality literal with a variable X as one
blanchet@42730
   543
   operand, when X does not occur properly in the other operand. This rules out
blanchet@42730
   544
   clearly inconsistent facts such as X = a | X = b, though it by no means
blanchet@42730
   545
   guarantees soundness. *)
blanchet@42730
   546
blanchet@42730
   547
(* Unwanted equalities are those between a (bound or schematic) variable that
blanchet@42730
   548
   does not properly occur in the second operand. *)
blanchet@42730
   549
val is_exhaustive_finite =
blanchet@42730
   550
  let
blanchet@42730
   551
    fun is_bad_equal (Var z) t =
blanchet@42730
   552
        not (exists_subterm (fn Var z' => z = z' | _ => false) t)
blanchet@42730
   553
      | is_bad_equal (Bound j) t = not (loose_bvar1 (t, j))
blanchet@42730
   554
      | is_bad_equal _ _ = false
blanchet@42730
   555
    fun do_equals t1 t2 = is_bad_equal t1 t2 orelse is_bad_equal t2 t1
blanchet@42730
   556
    fun do_formula pos t =
blanchet@42730
   557
      case (pos, t) of
blanchet@42730
   558
        (_, @{const Trueprop} $ t1) => do_formula pos t1
blanchet@42730
   559
      | (true, Const (@{const_name all}, _) $ Abs (_, _, t')) =>
blanchet@42730
   560
        do_formula pos t'
blanchet@42730
   561
      | (true, Const (@{const_name All}, _) $ Abs (_, _, t')) =>
blanchet@42730
   562
        do_formula pos t'
blanchet@42730
   563
      | (false, Const (@{const_name Ex}, _) $ Abs (_, _, t')) =>
blanchet@42730
   564
        do_formula pos t'
blanchet@42730
   565
      | (_, @{const "==>"} $ t1 $ t2) =>
blanchet@42730
   566
        do_formula (not pos) t1 andalso
blanchet@42730
   567
        (t2 = @{prop False} orelse do_formula pos t2)
blanchet@42730
   568
      | (_, @{const HOL.implies} $ t1 $ t2) =>
blanchet@42730
   569
        do_formula (not pos) t1 andalso
blanchet@42730
   570
        (t2 = @{const False} orelse do_formula pos t2)
blanchet@42730
   571
      | (_, @{const Not} $ t1) => do_formula (not pos) t1
blanchet@42730
   572
      | (true, @{const HOL.disj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
blanchet@42730
   573
      | (false, @{const HOL.conj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
blanchet@42730
   574
      | (true, Const (@{const_name HOL.eq}, _) $ t1 $ t2) => do_equals t1 t2
blanchet@42730
   575
      | (true, Const (@{const_name "=="}, _) $ t1 $ t2) => do_equals t1 t2
blanchet@42730
   576
      | _ => false
blanchet@42730
   577
  in do_formula true end
blanchet@42730
   578
blanchet@42730
   579
fun has_bound_or_var_of_type pred =
blanchet@42730
   580
  exists_subterm (fn Var (_, T as Type _) => pred T
blanchet@42730
   581
                   | Abs (_, T as Type _, _) => pred T
blanchet@42730
   582
                   | _ => false)
blanchet@42730
   583
blanchet@42730
   584
(* Facts are forbidden to contain variables of these types. The typical reason
blanchet@42730
   585
   is that they lead to unsoundness. Note that "unit" satisfies numerous
blanchet@42730
   586
   equations like "?x = ()". The resulting clauses will have no type constraint,
blanchet@42730
   587
   yielding false proofs. Even "bool" leads to many unsound proofs, though only
blanchet@42730
   588
   for higher-order problems. *)
blanchet@42730
   589
blanchet@42730
   590
(* Facts containing variables of type "unit" or "bool" or of the form
blanchet@42730
   591
   "ALL x. x = A | x = B | x = C" are likely to lead to unsound proofs if types
blanchet@42730
   592
   are omitted. *)
blanchet@42944
   593
fun is_dangerous_prop ctxt =
blanchet@42944
   594
  transform_elim_prop
blanchet@44393
   595
  #> (has_bound_or_var_of_type (is_type_surely_finite ctxt) orf
blanchet@42730
   596
      is_exhaustive_finite)
blanchet@42730
   597
blanchet@39492
   598
(* Important messages are important but not so important that users want to see
blanchet@39492
   599
   them each time. *)
blanchet@44649
   600
val atp_important_message_keep_quotient = 25
blanchet@39492
   601
blanchet@44416
   602
fun choose_type_enc soundness best_type_enc format =
blanchet@44397
   603
  the_default best_type_enc
blanchet@44397
   604
  #> type_enc_from_string soundness
blanchet@44416
   605
  #> adjust_type_enc format
blanchet@42548
   606
blanchet@43051
   607
val metis_minimize_max_time = seconds 2.0
blanchet@43051
   608
blanchet@45561
   609
fun choose_minimize_command params minimize_command name preplay =
blanchet@45520
   610
  let
blanchet@45520
   611
    val (name, override_params) =
blanchet@45520
   612
      case preplay of
blanchet@45520
   613
        Played (reconstr, time) =>
blanchet@45520
   614
        if Time.<= (time, metis_minimize_max_time) then
blanchet@45561
   615
          extract_reconstructor params reconstr
blanchet@45520
   616
        else
blanchet@45520
   617
          (name, [])
blanchet@45520
   618
      | _ => (name, [])
blanchet@45520
   619
  in minimize_command override_params name end
blanchet@43051
   620
blanchet@47962
   621
fun repair_monomorph_context max_iters best_max_iters max_new_instances
blanchet@47962
   622
                             best_max_new_instances =
blanchet@47962
   623
  Config.put Monomorph.max_rounds (max_iters |> the_default best_max_iters)
blanchet@47962
   624
  #> Config.put Monomorph.max_new_instances
blanchet@47962
   625
         (max_new_instances |> the_default best_max_new_instances)
boehmes@43230
   626
  #> Config.put Monomorph.keep_partial_instances false
blanchet@43226
   627
blanchet@50494
   628
fun suffix_for_mode Auto_Try = "_try"
blanchet@44509
   629
  | suffix_for_mode Try = "_try"
blanchet@44509
   630
  | suffix_for_mode Normal = ""
blanchet@50494
   631
  | suffix_for_mode MaSh = ""
blanchet@50494
   632
  | suffix_for_mode Auto_Minimize = "_min"
blanchet@44509
   633
  | suffix_for_mode Minimize = "_min"
blanchet@44509
   634
blanchet@44423
   635
(* Give the ATPs some slack before interrupting them the hard way. "z3_tptp" on
blanchet@43631
   636
   Linux appears to be the only ATP that does not honor its time limit. *)
blanchet@43690
   637
val atp_timeout_slack = seconds 1.0
blanchet@43631
   638
blanchet@48331
   639
val mono_max_privileged_facts = 10
blanchet@48331
   640
blanchet@43021
   641
fun run_atp mode name
blanchet@48376
   642
        ({exec, arguments, proof_delims, known_failures, prem_role, best_slices,
blanchet@48376
   643
          best_max_mono_iters, best_max_new_mono_instances, ...} : atp_config)
blanchet@46301
   644
        (params as {debug, verbose, overlord, type_enc, strict, lam_trans,
blanchet@48293
   645
                    uncurried_aliases, max_facts, max_mono_iters,
blanchet@50020
   646
                    max_new_mono_instances, isar_proofs, isar_shrink,
blanchet@46409
   647
                    slice, timeout, preplay_timeout, ...})
blanchet@43037
   648
        minimize_command
blanchet@43037
   649
        ({state, goal, subgoal, subgoal_count, facts, ...} : prover_problem) =
blanchet@38023
   650
  let
blanchet@42182
   651
    val thy = Proof.theory_of state
blanchet@39318
   652
    val ctxt = Proof.context_of state
blanchet@47946
   653
    val atp_mode =
blanchet@48143
   654
      if Config.get ctxt completish then Sledgehammer_Completish
blanchet@47946
   655
      else Sledgehammer
blanchet@43004
   656
    val (_, hyp_ts, concl_t) = strip_subgoal ctxt goal subgoal
blanchet@41159
   657
    val (dest_dir, problem_prefix) =
blanchet@41159
   658
      if overlord then overlord_file_location_for_prover name
blanchet@41159
   659
      else (Config.get ctxt dest_dir, Config.get ctxt problem_prefix)
blanchet@40061
   660
    val problem_file_name =
blanchet@41159
   661
      Path.basic (problem_prefix ^ (if overlord then "" else serial_string ()) ^
blanchet@44509
   662
                  suffix_for_mode mode ^ "_" ^ string_of_int subgoal)
blanchet@48656
   663
    val prob_path =
blanchet@40059
   664
      if dest_dir = "" then
blanchet@40061
   665
        File.tmp_path problem_file_name
blanchet@40059
   666
      else if File.exists (Path.explode dest_dir) then
blanchet@40061
   667
        Path.append (Path.explode dest_dir) problem_file_name
blanchet@39003
   668
      else
blanchet@40059
   669
        error ("No such directory: " ^ quote dest_dir ^ ".")
blanchet@48376
   670
    val command0 =
blanchet@47055
   671
      case find_first (fn var => getenv var <> "") (fst exec) of
blanchet@48376
   672
        SOME var =>
blanchet@48376
   673
        let
blanchet@48376
   674
          val pref = getenv var ^ "/"
blanchet@48376
   675
          val paths = map (Path.explode o prefix pref) (snd exec)
blanchet@48376
   676
        in
blanchet@48376
   677
          case find_first File.exists paths of
blanchet@48376
   678
            SOME path => path
blanchet@48376
   679
          | NONE => error ("Bad executable: " ^ Path.print (hd paths) ^ ".")
blanchet@48376
   680
        end
blanchet@47055
   681
      | NONE => error ("The environment variable " ^ quote (hd (fst exec)) ^
blanchet@47055
   682
                       " is not set.")
blanchet@38023
   683
    fun split_time s =
blanchet@38023
   684
      let
blanchet@42448
   685
        val split = String.tokens (fn c => str c = "\n")
blanchet@47737
   686
        val (output, t) =
blanchet@47737
   687
          s |> split |> (try split_last #> the_default ([], "0"))
blanchet@47737
   688
            |>> cat_lines
blanchet@42448
   689
        fun as_num f = f >> (fst o read_int)
blanchet@42448
   690
        val num = as_num (Scan.many1 Symbol.is_ascii_digit)
blanchet@42448
   691
        val digit = Scan.one Symbol.is_ascii_digit
blanchet@42448
   692
        val num3 = as_num (digit ::: digit ::: (digit >> single))
blanchet@42448
   693
        val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b)
blanchet@45381
   694
        val as_time =
blanchet@45381
   695
          raw_explode #> Scan.read Symbol.stopper time #> the_default 0
blanchet@47737
   696
      in (output, as_time t |> Time.fromMilliseconds) end
blanchet@48656
   697
    fun run () =
blanchet@48376
   698
      let
blanchet@48376
   699
        (* If slicing is disabled, we expand the last slice to fill the entire
blanchet@48376
   700
           time available. *)
blanchet@48376
   701
        val actual_slices = get_slices slice (best_slices ctxt)
blanchet@48376
   702
        val num_actual_slices = length actual_slices
blanchet@48376
   703
        fun monomorphize_facts facts =
blanchet@48376
   704
          let
blanchet@48376
   705
            val ctxt =
blanchet@48376
   706
              ctxt
blanchet@48376
   707
              |> repair_monomorph_context max_mono_iters best_max_mono_iters
blanchet@48376
   708
                      max_new_mono_instances best_max_new_mono_instances
blanchet@48376
   709
            (* pseudo-theorem involving the same constants as the subgoal *)
blanchet@48376
   710
            val subgoal_th =
blanchet@48376
   711
              Logic.list_implies (hyp_ts, concl_t) |> Skip_Proof.make_thm thy
blanchet@48376
   712
            val rths =
blanchet@48376
   713
              facts |> chop mono_max_privileged_facts
blanchet@48376
   714
                    |>> map (pair 1 o snd)
blanchet@48376
   715
                    ||> map (pair 2 o snd)
blanchet@48376
   716
                    |> op @
blanchet@48376
   717
                    |> cons (0, subgoal_th)
blanchet@48376
   718
          in
blanchet@48376
   719
            Monomorph.monomorph atp_schematic_consts_of rths ctxt |> fst |> tl
blanchet@48376
   720
            |> curry ListPair.zip (map fst facts)
blanchet@48376
   721
            |> maps (fn (name, rths) =>
blanchet@48376
   722
                        map (pair name o zero_var_indexes o snd) rths)
blanchet@48376
   723
          end
blanchet@48376
   724
        fun run_slice time_left (cache_key, cache_value)
blanchet@48716
   725
                (slice, (time_frac,
blanchet@48376
   726
                     (key as (best_max_facts, format, best_type_enc,
blanchet@48376
   727
                              best_lam_trans, best_uncurried_aliases),
blanchet@48716
   728
                      extra))) =
blanchet@38032
   729
          let
blanchet@48376
   730
            val num_facts =
blanchet@48376
   731
              length facts |> is_none max_facts ? Integer.min best_max_facts
blanchet@48376
   732
            val soundness = if strict then Strict else Non_Strict
blanchet@48376
   733
            val type_enc =
blanchet@48376
   734
              type_enc |> choose_type_enc soundness best_type_enc format
blanchet@48376
   735
            val sound = is_type_enc_sound type_enc
blanchet@48376
   736
            val real_ms = Real.fromInt o Time.toMilliseconds
blanchet@48376
   737
            val slice_timeout =
blanchet@50557
   738
              case time_left of
blanchet@50557
   739
                SOME time_left =>
blanchet@50557
   740
                ((real_ms time_left
blanchet@50557
   741
                  |> (if slice < num_actual_slices - 1 then
blanchet@50557
   742
                        curry Real.min (time_frac * real_ms (the timeout))
blanchet@50557
   743
                      else
blanchet@50557
   744
                        I))
blanchet@50557
   745
                 * 0.001)
blanchet@50557
   746
                |> seconds |> SOME
blanchet@50557
   747
              | NONE => NONE
blanchet@48376
   748
            val generous_slice_timeout =
blanchet@50557
   749
              Option.map (curry Time.+ atp_timeout_slack) slice_timeout
blanchet@48376
   750
            val _ =
blanchet@48376
   751
              if debug then
blanchet@48376
   752
                quote name ^ " slice #" ^ string_of_int (slice + 1) ^
blanchet@48376
   753
                " with " ^ string_of_int num_facts ^ " fact" ^
blanchet@50557
   754
                plural_s num_facts ^
blanchet@50557
   755
                (case slice_timeout of
blanchet@50557
   756
                   SOME timeout => " for " ^ string_from_time timeout
blanchet@50557
   757
                 | NONE => "") ^ "..."
blanchet@48376
   758
                |> Output.urgent_message
blanchet@48376
   759
              else
blanchet@48376
   760
                ()
blanchet@48376
   761
            val readable_names = not (Config.get ctxt atp_full_names)
blanchet@48376
   762
            val lam_trans =
blanchet@48376
   763
              case lam_trans of
blanchet@48376
   764
                SOME s => s
blanchet@48376
   765
              | NONE => best_lam_trans
blanchet@48376
   766
            val uncurried_aliases =
blanchet@48376
   767
              case uncurried_aliases of
blanchet@48376
   768
                SOME b => b
blanchet@48376
   769
              | NONE => best_uncurried_aliases
blanchet@48376
   770
            val value as (atp_problem, _, fact_names, _, _) =
blanchet@48376
   771
              if cache_key = SOME key then
blanchet@48376
   772
                cache_value
blanchet@48376
   773
              else
blanchet@48376
   774
                facts
blanchet@48376
   775
                |> map untranslated_fact
blanchet@48376
   776
                |> not sound
blanchet@48376
   777
                   ? filter_out (is_dangerous_prop ctxt o prop_of o snd)
blanchet@48376
   778
                |> take num_facts
blanchet@48376
   779
                |> not (is_type_enc_polymorphic type_enc) ? monomorphize_facts
blanchet@48376
   780
                |> map (apsnd prop_of)
blanchet@48376
   781
                |> prepare_atp_problem ctxt format prem_role type_enc atp_mode
blanchet@48376
   782
                                       lam_trans uncurried_aliases
blanchet@48376
   783
                                       readable_names true hyp_ts concl_t
blanchet@48376
   784
            fun sel_weights () = atp_problem_selection_weights atp_problem
blanchet@48376
   785
            fun ord_info () = atp_problem_term_order_info atp_problem
blanchet@48376
   786
            val ord = effective_term_order ctxt name
blanchet@49918
   787
            val full_proof = debug orelse isar_proofs
blanchet@50557
   788
            val args = arguments ctxt full_proof extra
blanchet@50557
   789
                                 (slice_timeout |> the_default one_day)
blanchet@48376
   790
                                 (ord, ord_info, sel_weights)
blanchet@48376
   791
            val command =
blanchet@48532
   792
              "(exec 2>&1; " ^ File.shell_path command0 ^ " " ^ args ^ " " ^
blanchet@48656
   793
              File.shell_path prob_path ^ ")"
blanchet@48532
   794
              |> enclose "TIMEFORMAT='%3R'; { time " " ; }"
blanchet@48376
   795
            val _ =
blanchet@48376
   796
              atp_problem
blanchet@48376
   797
              |> lines_for_atp_problem format ord ord_info
blanchet@48376
   798
              |> cons ("% " ^ command ^ "\n")
blanchet@48656
   799
              |> File.write_list prob_path
blanchet@48376
   800
            val ((output, run_time), (atp_proof, outcome)) =
blanchet@50557
   801
              time_limit generous_slice_timeout Isabelle_System.bash_output
blanchet@50557
   802
                         command
blanchet@48376
   803
              |>> (if overlord then
blanchet@48376
   804
                     prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n")
blanchet@48376
   805
                   else
blanchet@48376
   806
                     I)
blanchet@48376
   807
              |> fst |> split_time
blanchet@48376
   808
              |> (fn accum as (output, _) =>
blanchet@48376
   809
                     (accum,
blanchet@48716
   810
                      extract_tstplike_proof_and_outcome verbose proof_delims
blanchet@48716
   811
                                                         known_failures output
blanchet@48376
   812
                      |>> atp_proof_from_tstplike_proof atp_problem
blanchet@48376
   813
                      handle UNRECOGNIZED_ATP_PROOF () =>
blanchet@48376
   814
                             ([], SOME ProofIncomplete)))
blanchet@48376
   815
              handle TimeLimit.TimeOut =>
blanchet@50557
   816
                     (("", the slice_timeout), ([], SOME TimedOut))
blanchet@48376
   817
            val outcome =
blanchet@48376
   818
              case outcome of
blanchet@48376
   819
                NONE =>
blanchet@48376
   820
                (case used_facts_in_unsound_atp_proof ctxt fact_names atp_proof
blanchet@48376
   821
                      |> Option.map (sort string_ord) of
blanchet@48376
   822
                   SOME facts =>
blanchet@48376
   823
                   let
blanchet@48376
   824
                     val failure =
blanchet@48376
   825
                       UnsoundProof (is_type_enc_sound type_enc, facts)
blanchet@48376
   826
                   in
blanchet@48376
   827
                     if debug then (warning (string_for_failure failure); NONE)
blanchet@48376
   828
                     else SOME failure
blanchet@48376
   829
                   end
blanchet@48376
   830
                 | NONE => NONE)
blanchet@48376
   831
              | _ => outcome
blanchet@48376
   832
          in ((SOME key, value), (output, run_time, atp_proof, outcome)) end
blanchet@48376
   833
        val timer = Timer.startRealTimer ()
blanchet@48376
   834
        fun maybe_run_slice slice
blanchet@48376
   835
                (result as (cache, (_, run_time0, _, SOME _))) =
blanchet@48376
   836
            let
blanchet@50557
   837
              val time_left =
blanchet@50557
   838
                Option.map
blanchet@50557
   839
                    (fn timeout => Time.- (timeout, Timer.checkRealTimer timer))
blanchet@50557
   840
                    timeout
blanchet@48376
   841
            in
blanchet@50557
   842
              if time_left <> NONE andalso
blanchet@50557
   843
                 Time.<= (the time_left, Time.zeroTime) then
blanchet@48376
   844
                result
blanchet@48376
   845
              else
blanchet@48376
   846
                run_slice time_left cache slice
blanchet@48376
   847
                |> (fn (cache, (output, run_time, atp_proof, outcome)) =>
blanchet@48376
   848
                       (cache, (output, Time.+ (run_time0, run_time),
blanchet@48376
   849
                                atp_proof, outcome)))
blanchet@48376
   850
            end
blanchet@48376
   851
          | maybe_run_slice _ result = result
blanchet@48376
   852
      in
blanchet@48376
   853
        ((NONE, ([], Symtab.empty, Vector.fromList [], [], Symtab.empty)),
blanchet@48376
   854
         ("", Time.zeroTime, [], SOME InternalError))
blanchet@48376
   855
        |> fold maybe_run_slice actual_slices
blanchet@48376
   856
      end
blanchet@38023
   857
    (* If the problem file has not been exported, remove it; otherwise, export
blanchet@38023
   858
       the proof file too. *)
blanchet@48656
   859
    fun clean_up () =
blanchet@48656
   860
      if dest_dir = "" then (try File.rm prob_path; ()) else ()
blanchet@48656
   861
    fun export (_, (output, _, _, _)) =
blanchet@48376
   862
      if dest_dir = "" then ()
blanchet@48656
   863
      else File.write (Path.explode (Path.implode prob_path ^ "_proof")) output
blanchet@46407
   864
    val ((_, (_, pool, fact_names, _, sym_tab)),
blanchet@46407
   865
         (output, run_time, atp_proof, outcome)) =
blanchet@48656
   866
      with_cleanup clean_up run () |> tap export
blanchet@39492
   867
    val important_message =
blanchet@43021
   868
      if mode = Normal andalso
blanchet@42609
   869
         random_range 0 (atp_important_message_keep_quotient - 1) = 0 then
blanchet@39492
   870
        extract_important_message output
blanchet@39492
   871
      else
blanchet@39492
   872
        ""
blanchet@43261
   873
    val (used_facts, preplay, message, message_tail) =
blanchet@38023
   874
      case outcome of
blanchet@38023
   875
        NONE =>
blanchet@43033
   876
        let
blanchet@45551
   877
          val used_facts = used_facts_in_atp_proof ctxt fact_names atp_proof
blanchet@45590
   878
          val needs_full_types = is_typed_helper_used_in_atp_proof atp_proof
blanchet@45521
   879
          val reconstrs =
blanchet@45554
   880
            bunch_of_reconstructors needs_full_types
blanchet@45560
   881
                (lam_trans_from_atp_proof atp_proof
blanchet@46405
   882
                 o (fn desperate => if desperate then hide_lamsN
blanchet@46405
   883
                                    else metis_default_lam_trans))
blanchet@43033
   884
        in
blanchet@43052
   885
          (used_facts,
blanchet@43052
   886
           fn () =>
blanchet@43052
   887
              let
blanchet@45781
   888
                val used_pairs =
blanchet@48798
   889
                  facts |> map untranslated_fact
blanchet@48798
   890
                        |> filter_used_facts false used_facts
blanchet@43052
   891
              in
blanchet@45781
   892
                play_one_line_proof mode debug verbose preplay_timeout
blanchet@45781
   893
                    used_pairs state subgoal (hd reconstrs) reconstrs
blanchet@43052
   894
              end,
blanchet@43052
   895
           fn preplay =>
blanchet@43052
   896
              let
blanchet@49921
   897
                val _ =
blanchet@49921
   898
                  if verbose then
blanchet@49921
   899
                    Output.urgent_message "Generating proof text..."
blanchet@49921
   900
                  else
blanchet@49921
   901
                    ()
blanchet@43052
   902
                val isar_params =
blanchet@50020
   903
                  (debug, verbose, preplay_timeout, isar_shrink,
blanchet@50004
   904
                   pool, fact_names, sym_tab, atp_proof, goal)
blanchet@43052
   905
                val one_line_params =
blanchet@43052
   906
                  (preplay, proof_banner mode name, used_facts,
blanchet@45561
   907
                   choose_minimize_command params minimize_command name preplay,
blanchet@43052
   908
                   subgoal, subgoal_count)
blanchet@48799
   909
                val num_chained = length (#facts (Proof.goal state))
blanchet@48799
   910
              in
blanchet@49918
   911
                proof_text ctxt isar_proofs isar_params num_chained
blanchet@48799
   912
                           one_line_params
blanchet@48799
   913
              end,
blanchet@43261
   914
           (if verbose then
blanchet@45381
   915
              "\nATP real CPU time: " ^ string_from_time run_time ^ "."
blanchet@43261
   916
            else
blanchet@43261
   917
              "") ^
blanchet@43261
   918
           (if important_message <> "" then
blanchet@43261
   919
              "\n\nImportant message from Dr. Geoff Sutcliffe:\n" ^
blanchet@43261
   920
              important_message
blanchet@43261
   921
            else
blanchet@43261
   922
              ""))
blanchet@43033
   923
        end
blanchet@43052
   924
      | SOME failure =>
blanchet@45519
   925
        ([], K (Failed_to_Play plain_metis),
blanchet@45519
   926
         fn _ => string_for_failure failure, "")
blanchet@38023
   927
  in
blanchet@45381
   928
    {outcome = outcome, used_facts = used_facts, run_time = run_time,
blanchet@43261
   929
     preplay = preplay, message = message, message_tail = message_tail}
blanchet@38023
   930
  end
blanchet@38023
   931
blanchet@40669
   932
(* "SMT_Failure.Abnormal_Termination" carries the solver's return code. Until
blanchet@40669
   933
   these are sorted out properly in the SMT module, we have to interpret these
blanchet@40669
   934
   ourselves. *)
blanchet@40684
   935
val remote_smt_failures =
blanchet@43631
   936
  [(2, NoLibwwwPerl),
blanchet@43631
   937
   (22, CantConnect)]
blanchet@40684
   938
val z3_failures =
blanchet@41236
   939
  [(101, OutOfResources),
blanchet@41236
   940
   (103, MalformedInput),
blanchet@41222
   941
   (110, MalformedInput)]
blanchet@40684
   942
val unix_failures =
blanchet@48797
   943
  [(138, Crashed),
blanchet@48797
   944
   (139, Crashed)]
blanchet@43631
   945
val smt_failures = remote_smt_failures @ z3_failures @ unix_failures
blanchet@40555
   946
blanchet@42100
   947
fun failure_from_smt_failure (SMT_Failure.Counterexample {is_real_cex, ...}) =
blanchet@43050
   948
    if is_real_cex then Unprovable else GaveUp
blanchet@41222
   949
  | failure_from_smt_failure SMT_Failure.Time_Out = TimedOut
blanchet@41222
   950
  | failure_from_smt_failure (SMT_Failure.Abnormal_Termination code) =
blanchet@41222
   951
    (case AList.lookup (op =) smt_failures code of
blanchet@40684
   952
       SOME failure => failure
blanchet@41259
   953
     | NONE => UnknownError ("Abnormal termination with exit code " ^
blanchet@41259
   954
                             string_of_int code ^ "."))
blanchet@41222
   955
  | failure_from_smt_failure SMT_Failure.Out_Of_Memory = OutOfResources
blanchet@41222
   956
  | failure_from_smt_failure (SMT_Failure.Other_Failure msg) =
blanchet@42061
   957
    UnknownError msg
blanchet@40063
   958
blanchet@40698
   959
(* FUDGE *)
blanchet@42646
   960
val smt_max_slices =
blanchet@42646
   961
  Attrib.setup_config_int @{binding sledgehammer_smt_max_slices} (K 8)
blanchet@42646
   962
val smt_slice_fact_frac =
blanchet@42646
   963
  Attrib.setup_config_real @{binding sledgehammer_smt_slice_fact_frac} (K 0.5)
blanchet@42646
   964
val smt_slice_time_frac =
blanchet@42646
   965
  Attrib.setup_config_real @{binding sledgehammer_smt_slice_time_frac} (K 0.5)
blanchet@42646
   966
val smt_slice_min_secs =
blanchet@42646
   967
  Attrib.setup_config_int @{binding sledgehammer_smt_slice_min_secs} (K 5)
blanchet@40409
   968
blanchet@42646
   969
fun smt_filter_loop ctxt name
blanchet@42724
   970
                    ({debug, verbose, overlord, max_mono_iters,
blanchet@45706
   971
                      max_new_mono_instances, timeout, slice, ...} : params)
blanchet@50486
   972
                    state goal i =
blanchet@40409
   973
  let
blanchet@45706
   974
    val max_slices = if slice then Config.get ctxt smt_max_slices else 1
blanchet@41242
   975
    val repair_context =
blanchet@43233
   976
      select_smt_solver name
blanchet@45557
   977
      #> Config.put SMT_Config.verbose debug
blanchet@43233
   978
      #> (if overlord then
blanchet@43233
   979
            Config.put SMT_Config.debug_files
blanchet@43233
   980
                       (overlord_file_location_for_prover name
blanchet@43233
   981
                        |> (fn (path, name) => path ^ "/" ^ name))
blanchet@43233
   982
          else
blanchet@43233
   983
            I)
blanchet@43233
   984
      #> Config.put SMT_Config.infer_triggers (Config.get ctxt smt_triggers)
blanchet@41242
   985
    val state = state |> Proof.map_context repair_context
blanchet@42443
   986
    fun do_slice timeout slice outcome0 time_so_far 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@40553
  1005
        val num_facts = length 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@50486
  1021
          SMT_Solver.smt_filter_preprocess ctxt state_facts goal facts i
blanchet@50557
  1022
          |> SMT_Solver.smt_filter_apply (slice_timeout |> the_default one_day)
blanchet@41239
  1023
          |> (fn {outcome, used_facts} => (outcome, used_facts))
blanchet@41209
  1024
          handle exn => if Exn.is_interrupt exn then
blanchet@41209
  1025
                          reraise exn
blanchet@41209
  1026
                        else
blanchet@42061
  1027
                          (ML_Compiler.exn_message exn
blanchet@41209
  1028
                           |> SMT_Failure.Other_Failure |> SOME, [])
blanchet@41168
  1029
        val death = Timer.checkRealTimer timer
blanchet@40553
  1030
        val outcome0 = if is_none outcome0 then SOME outcome else outcome0
blanchet@41168
  1031
        val time_so_far = Time.+ (time_so_far, Time.- (death, birth))
blanchet@40553
  1032
        val too_many_facts_perhaps =
blanchet@40553
  1033
          case outcome of
blanchet@40553
  1034
            NONE => false
blanchet@40553
  1035
          | SOME (SMT_Failure.Counterexample _) => false
blanchet@42443
  1036
          | SOME SMT_Failure.Time_Out => slice_timeout <> timeout
blanchet@42614
  1037
          | SOME (SMT_Failure.Abnormal_Termination _) => true (* kind of *)
blanchet@40553
  1038
          | SOME SMT_Failure.Out_Of_Memory => true
blanchet@41211
  1039
          | SOME (SMT_Failure.Other_Failure _) => true
blanchet@50557
  1040
        val timeout =
blanchet@50557
  1041
          Option.map
blanchet@50557
  1042
              (fn timeout => Time.- (timeout, Timer.checkRealTimer timer))
blanchet@50557
  1043
              timeout
blanchet@40553
  1044
      in
blanchet@42443
  1045
        if too_many_facts_perhaps andalso slice < max_slices andalso
blanchet@50557
  1046
           num_facts > 0 andalso
blanchet@50557
  1047
           (timeout = NONE orelse Time.> (the timeout, Time.zeroTime)) then
blanchet@41169
  1048
          let
blanchet@42614
  1049
            val new_num_facts =
blanchet@42646
  1050
              Real.ceil (Config.get ctxt smt_slice_fact_frac
blanchet@42646
  1051
                         * Real.fromInt num_facts)
blanchet@42614
  1052
            val _ =
blanchet@42614
  1053
              if verbose andalso is_some outcome then
blanchet@42614
  1054
                quote name ^ " invoked with " ^ string_of_int num_facts ^
blanchet@42614
  1055
                " fact" ^ plural_s num_facts ^ ": " ^
blanchet@42614
  1056
                string_for_failure (failure_from_smt_failure (the outcome)) ^
blanchet@42614
  1057
                " Retrying with " ^ string_of_int new_num_facts ^ " fact" ^
blanchet@42638
  1058
                plural_s new_num_facts ^ "..."
blanchet@42614
  1059
                |> Output.urgent_message
blanchet@42614
  1060
              else
blanchet@42614
  1061
                ()
blanchet@42443
  1062
          in
blanchet@42614
  1063
            facts |> take new_num_facts
blanchet@42614
  1064
                  |> do_slice timeout (slice + 1) outcome0 time_so_far
blanchet@42443
  1065
          end
blanchet@40553
  1066
        else
blanchet@40553
  1067
          {outcome = if is_none outcome then NONE else the outcome0,
blanchet@45370
  1068
           used_facts = used_facts, run_time = time_so_far}
blanchet@40409
  1069
      end
blanchet@42443
  1070
  in do_slice timeout 1 NONE Time.zeroTime end
blanchet@40409
  1071
blanchet@43052
  1072
fun run_smt_solver mode name (params as {debug, verbose, preplay_timeout, ...})
blanchet@43011
  1073
        minimize_command
blanchet@50486
  1074
        ({state, goal, subgoal, subgoal_count, facts, ...} : prover_problem) =
blanchet@36379
  1075
  let
blanchet@41242
  1076
    val ctxt = Proof.context_of state
blanchet@41256
  1077
    val num_facts = length facts
blanchet@41256
  1078
    val facts = facts ~~ (0 upto num_facts - 1)
blanchet@42646
  1079
                |> map (smt_weighted_fact ctxt num_facts)
blanchet@45781
  1080
    val {outcome, used_facts = used_pairs, run_time} =
blanchet@50486
  1081
      smt_filter_loop ctxt name params state goal subgoal facts
blanchet@45781
  1082
    val used_facts = used_pairs |> map fst
blanchet@41222
  1083
    val outcome = outcome |> Option.map failure_from_smt_failure
blanchet@43261
  1084
    val (preplay, message, message_tail) =
blanchet@40184
  1085
      case outcome of
blanchet@40184
  1086
        NONE =>
blanchet@43052
  1087
        (fn () =>
blanchet@45781
  1088
            play_one_line_proof mode debug verbose preplay_timeout used_pairs
blanchet@45554
  1089
                state subgoal SMT
blanchet@45560
  1090
                (bunch_of_reconstructors false
blanchet@48800
  1091
                     (fn desperate =>
blanchet@48800
  1092
                         if desperate then liftingN
blanchet@48800
  1093
                         else metis_default_lam_trans)),
blanchet@43052
  1094
         fn preplay =>
blanchet@43052
  1095
            let
blanchet@43052
  1096
              val one_line_params =
blanchet@43052
  1097
                (preplay, proof_banner mode name, used_facts,
blanchet@45561
  1098
                 choose_minimize_command params minimize_command name preplay,
blanchet@43052
  1099
                 subgoal, subgoal_count)
blanchet@48799
  1100
              val num_chained = length (#facts (Proof.goal state))
blanchet@48799
  1101
            in one_line_proof_text num_chained one_line_params end,
blanchet@43261
  1102
         if verbose then
blanchet@45370
  1103
           "\nSMT solver real CPU time: " ^ string_from_time run_time ^ "."
blanchet@43261
  1104
         else
blanchet@43261
  1105
           "")
blanchet@43166
  1106
      | SOME failure =>
blanchet@45519
  1107
        (K (Failed_to_Play plain_metis), fn _ => string_for_failure failure, "")
blanchet@40063
  1108
  in
blanchet@45370
  1109
    {outcome = outcome, used_facts = used_facts, run_time = run_time,
blanchet@45370
  1110
     preplay = preplay, message = message, message_tail = message_tail}
blanchet@40063
  1111
  end
blanchet@40063
  1112
blanchet@45520
  1113
fun run_reconstructor mode name
blanchet@45561
  1114
        (params as {debug, verbose, timeout, type_enc, lam_trans, ...})
blanchet@45379
  1115
        minimize_command
blanchet@45379
  1116
        ({state, subgoal, subgoal_count, facts, ...} : prover_problem) =
blanchet@43050
  1117
  let
blanchet@45520
  1118
    val reconstr =
blanchet@45520
  1119
      if name = metisN then
blanchet@45520
  1120
        Metis (type_enc |> the_default (hd partial_type_encs),
blanchet@45520
  1121
               lam_trans |> the_default metis_default_lam_trans)
blanchet@45520
  1122
      else if name = smtN then
blanchet@45520
  1123
        SMT
blanchet@45520
  1124
      else
blanchet@45520
  1125
        raise Fail ("unknown reconstructor: " ^ quote name)
blanchet@45781
  1126
    val used_pairs = facts |> map untranslated_fact
blanchet@45781
  1127
    val used_facts = used_pairs |> map fst
blanchet@43050
  1128
  in
blanchet@45379
  1129
    case play_one_line_proof (if mode = Minimize then Normal else mode) debug
blanchet@45781
  1130
                             verbose timeout used_pairs state subgoal reconstr
blanchet@45520
  1131
                             [reconstr] of
blanchet@43050
  1132
      play as Played (_, time) =>
blanchet@45370
  1133
      {outcome = NONE, used_facts = used_facts, run_time = time,
blanchet@43052
  1134
       preplay = K play,
blanchet@45561
  1135
       message =
blanchet@45561
  1136
         fn play =>
blanchet@45561
  1137
            let
blanchet@45561
  1138
              val (_, override_params) = extract_reconstructor params reconstr
blanchet@45561
  1139
              val one_line_params =
blanchet@45561
  1140
                (play, proof_banner mode name, used_facts,
blanchet@45561
  1141
                 minimize_command override_params name, subgoal,
blanchet@45561
  1142
                 subgoal_count)
blanchet@48799
  1143
              val num_chained = length (#facts (Proof.goal state))
blanchet@48799
  1144
            in one_line_proof_text num_chained one_line_params end,
blanchet@43261
  1145
       message_tail = ""}
blanchet@43052
  1146
    | play =>
blanchet@43166
  1147
      let
blanchet@43166
  1148
        val failure = case play of Failed_to_Play _ => GaveUp | _ => TimedOut
blanchet@43166
  1149
      in
blanchet@45370
  1150
        {outcome = SOME failure, used_facts = [], run_time = Time.zeroTime,
blanchet@43261
  1151
         preplay = K play, message = fn _ => string_for_failure failure,
blanchet@43261
  1152
         message_tail = ""}
blanchet@43050
  1153
      end
blanchet@43050
  1154
  end
blanchet@43050
  1155
blanchet@43021
  1156
fun get_prover ctxt mode name =
wenzelm@42361
  1157
  let val thy = Proof_Context.theory_of ctxt in
blanchet@45379
  1158
    if is_reconstructor name then run_reconstructor mode name
blanchet@47606
  1159
    else if is_atp thy name then run_atp mode name (get_atp thy name ())
blanchet@43052
  1160
    else if is_smt_prover ctxt name then run_smt_solver mode name
blanchet@43052
  1161
    else error ("No such prover: " ^ name ^ ".")
blanchet@40941
  1162
  end
blanchet@40063
  1163
wenzelm@28582
  1164
end;