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