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