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