src/HOL/Tools/Sledgehammer/sledgehammer_provers.ML
author blanchet
Mon Sep 09 15:22:04 2013 +0200 (2013-09-09 ago)
changeset 53480 247817dbb990
parent 53478 8c3ccb314469
child 53492 c3d7d9911aae
permissions -rw-r--r--
limit the number of instances of a single theorem
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
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
     skolem_irrel_weight : real,
blanchet@48288
    57
     theory_const_rel_weight : real,
blanchet@48288
    58
     theory_const_irrel_weight : real,
blanchet@48288
    59
     chained_const_irrel_weight : real,
blanchet@48288
    60
     intro_bonus : real,
blanchet@48288
    61
     elim_bonus : real,
blanchet@48288
    62
     simp_bonus : real,
blanchet@48288
    63
     local_bonus : real,
blanchet@48288
    64
     assum_bonus : real,
blanchet@48288
    65
     chained_bonus : real,
blanchet@48288
    66
     max_imperfect : real,
blanchet@48288
    67
     max_imperfect_exp : real,
blanchet@48288
    68
     threshold_divisor : real,
blanchet@48288
    69
     ridiculous_threshold : real}
blanchet@48288
    70
blanchet@40061
    71
  type prover_problem =
blanchet@48321
    72
    {state : Proof.state,
blanchet@48321
    73
     goal : thm,
blanchet@48321
    74
     subgoal : int,
blanchet@48321
    75
     subgoal_count : int,
blanchet@51010
    76
     factss : (string * fact list) list}
blanchet@39493
    77
blanchet@40061
    78
  type prover_result =
blanchet@48321
    79
    {outcome : failure option,
blanchet@51009
    80
     used_facts : (string * stature) list,
blanchet@51009
    81
     used_from : fact list,
blanchet@48321
    82
     run_time : Time.time,
blanchet@50669
    83
     preplay : play Lazy.lazy,
blanchet@48321
    84
     message : play -> string,
blanchet@48321
    85
     message_tail : string}
blanchet@39493
    86
blanchet@43051
    87
  type prover =
blanchet@45520
    88
    params -> ((string * string list) list -> string -> minimize_command)
blanchet@45520
    89
    -> prover_problem -> prover_result
blanchet@35867
    90
blanchet@43092
    91
  val dest_dir : string Config.T
blanchet@43092
    92
  val problem_prefix : string Config.T
blanchet@48143
    93
  val completish : bool Config.T
blanchet@44592
    94
  val atp_full_names : bool Config.T
blanchet@42646
    95
  val smt_triggers : bool Config.T
blanchet@42646
    96
  val smt_weights : bool Config.T
blanchet@42646
    97
  val smt_weight_min_facts : int Config.T
blanchet@42646
    98
  val smt_min_weight : int Config.T
blanchet@42646
    99
  val smt_max_weight : int Config.T
blanchet@42646
   100
  val smt_max_weight_index : int Config.T
blanchet@41256
   101
  val smt_weight_curve : (int -> int) Unsynchronized.ref
blanchet@42646
   102
  val smt_max_slices : int Config.T
blanchet@42646
   103
  val smt_slice_fact_frac : real Config.T
blanchet@42646
   104
  val smt_slice_time_frac : real Config.T
blanchet@42646
   105
  val smt_slice_min_secs : int Config.T
blanchet@48319
   106
  val SledgehammerN : string
blanchet@45519
   107
  val plain_metis : reconstructor
blanchet@41242
   108
  val select_smt_solver : string -> Proof.context -> Proof.context
blanchet@45520
   109
  val extract_reconstructor :
blanchet@45561
   110
    params -> reconstructor -> string * (string * string list) list
blanchet@45379
   111
  val is_reconstructor : string -> bool
blanchet@43050
   112
  val is_atp : theory -> string -> bool
blanchet@41087
   113
  val is_smt_prover : Proof.context -> string -> bool
blanchet@47962
   114
  val is_ho_atp: Proof.context -> string -> bool
blanchet@42944
   115
  val is_unit_equational_atp : Proof.context -> string -> bool
blanchet@41727
   116
  val is_prover_supported : Proof.context -> string -> bool
blanchet@40072
   117
  val is_prover_installed : Proof.context -> string -> bool
blanchet@51200
   118
  val remotify_prover_if_supported_and_not_already_remote :
blanchet@51200
   119
    Proof.context -> string -> string option
blanchet@51200
   120
  val remotify_prover_if_not_installed :
blanchet@51200
   121
    Proof.context -> string -> string option
blanchet@51998
   122
  val default_max_facts_of_prover : Proof.context -> bool -> string -> int
blanchet@42944
   123
  val is_unit_equality : term -> bool
blanchet@51998
   124
  val is_appropriate_prop_of_prover : Proof.context -> string -> term -> bool
blanchet@51998
   125
  val is_built_in_const_of_prover :
blanchet@41336
   126
    Proof.context -> string -> string * typ -> term list -> bool * term list
blanchet@41087
   127
  val atp_relevance_fudge : relevance_fudge
blanchet@41087
   128
  val smt_relevance_fudge : relevance_fudge
blanchet@51998
   129
  val relevance_fudge_of_prover : Proof.context -> string -> relevance_fudge
blanchet@41256
   130
  val weight_smt_fact :
blanchet@46340
   131
    Proof.context -> int -> ((string * stature) * thm) * int
blanchet@46340
   132
    -> (string * stature) * (int option * thm)
blanchet@41727
   133
  val supported_provers : Proof.context -> unit
blanchet@40059
   134
  val kill_provers : unit -> unit
blanchet@40059
   135
  val running_provers : unit -> unit
blanchet@40059
   136
  val messages : int option -> unit
blanchet@48798
   137
  val is_fact_chained : (('a * stature) * 'b) -> bool
blanchet@48798
   138
  val filter_used_facts :
blanchet@48798
   139
    bool -> (''a * stature) list -> ((''a * stature) * 'b) list ->
blanchet@48798
   140
    ((''a * stature) * 'b) list
blanchet@51200
   141
  val isar_proof_reconstructor : Proof.context -> string -> string
blanchet@43021
   142
  val get_prover : Proof.context -> mode -> string -> prover
wenzelm@28477
   143
end;
wenzelm@28477
   144
blanchet@41087
   145
structure Sledgehammer_Provers : SLEDGEHAMMER_PROVERS =
wenzelm@28477
   146
struct
wenzelm@28477
   147
blanchet@43085
   148
open ATP_Util
blanchet@38028
   149
open ATP_Problem
blanchet@39491
   150
open ATP_Proof
blanchet@38028
   151
open ATP_Systems
blanchet@46320
   152
open ATP_Problem_Generate
blanchet@46320
   153
open ATP_Proof_Reconstruct
blanchet@45521
   154
open Metis_Tactic
blanchet@38023
   155
open Sledgehammer_Util
blanchet@51005
   156
open Sledgehammer_Fact
smolkas@52555
   157
open Sledgehammer_Reconstructor
smolkas@52555
   158
open Sledgehammer_Print
blanchet@49881
   159
open Sledgehammer_Reconstruct
blanchet@48288
   160
blanchet@37583
   161
blanchet@37583
   162
(** The Sledgehammer **)
blanchet@37583
   163
wenzelm@53052
   164
datatype mode = Auto_Try | Try | Normal | MaSh | Auto_Minimize | Minimize
blanchet@43021
   165
blanchet@45376
   166
(* Identifier that distinguishes Sledgehammer from other tools that could use
blanchet@38102
   167
   "Async_Manager". *)
blanchet@48319
   168
val SledgehammerN = "Sledgehammer"
blanchet@37585
   169
blanchet@45520
   170
val reconstructor_names = [metisN, smtN]
blanchet@46365
   171
val plain_metis = Metis (hd partial_type_encs, combsN)
blanchet@45561
   172
val is_reconstructor = member (op =) reconstructor_names
blanchet@43228
   173
blanchet@43050
   174
val is_atp = member (op =) o supported_atps
blanchet@43050
   175
blanchet@43233
   176
val select_smt_solver = Context.proof_map o SMT_Config.select_solver
blanchet@41242
   177
blanchet@45376
   178
fun is_smt_prover ctxt = member (op =) (SMT_Solver.available_solvers_of ctxt)
blanchet@40062
   179
blanchet@51998
   180
fun is_atp_of_format is_format ctxt name =
blanchet@42944
   181
  let val thy = Proof_Context.theory_of ctxt in
blanchet@42944
   182
    case try (get_atp thy) name of
blanchet@47606
   183
      SOME config =>
blanchet@48716
   184
      exists (fn (_, ((_, format, _, _, _), _)) => is_format format)
blanchet@47606
   185
             (#best_slices (config ()) ctxt)
blanchet@42944
   186
    | NONE => false
blanchet@42944
   187
  end
blanchet@42944
   188
blanchet@51998
   189
val is_unit_equational_atp = is_atp_of_format (curry (op =) CNF_UEQ)
blanchet@51998
   190
val is_ho_atp = is_atp_of_format is_format_higher_order
blanchet@44597
   191
blanchet@45376
   192
fun is_prover_supported ctxt =
wenzelm@42361
   193
  let val thy = Proof_Context.theory_of ctxt in
blanchet@45379
   194
    is_reconstructor orf is_atp thy orf is_smt_prover ctxt
blanchet@40941
   195
  end
blanchet@40072
   196
boehmes@41432
   197
fun is_prover_installed ctxt =
blanchet@45379
   198
  is_reconstructor orf is_smt_prover ctxt orf
blanchet@43050
   199
  is_atp_installed (Proof_Context.theory_of ctxt)
blanchet@40941
   200
blanchet@51200
   201
fun remotify_prover_if_supported_and_not_already_remote ctxt name =
blanchet@51200
   202
  if String.isPrefix remote_prefix name then
blanchet@51200
   203
    SOME name
blanchet@51200
   204
  else
blanchet@51200
   205
    let val remote_name = remote_prefix ^ name in
blanchet@51200
   206
      if is_prover_supported ctxt remote_name then SOME remote_name else NONE
blanchet@51200
   207
    end
blanchet@51200
   208
blanchet@51200
   209
fun remotify_prover_if_not_installed ctxt name =
blanchet@51200
   210
  if is_prover_supported ctxt name andalso is_prover_installed ctxt name then
blanchet@51200
   211
    SOME name
blanchet@51200
   212
  else
blanchet@51200
   213
    remotify_prover_if_supported_and_not_already_remote ctxt name
blanchet@51200
   214
blanchet@45706
   215
fun get_slices slice slices =
blanchet@45706
   216
  (0 upto length slices - 1) ~~ slices |> not slice ? (List.last #> single)
blanchet@42443
   217
blanchet@48293
   218
val reconstructor_default_max_facts = 20
blanchet@43050
   219
blanchet@51186
   220
fun slice_max_facts (_, (_, ( ((max_facts, _), _, _, _, _), _))) = max_facts
blanchet@51186
   221
blanchet@51998
   222
fun default_max_facts_of_prover ctxt slice name =
wenzelm@42361
   223
  let val thy = Proof_Context.theory_of ctxt in
blanchet@45379
   224
    if is_reconstructor name then
blanchet@48293
   225
      reconstructor_default_max_facts
blanchet@43050
   226
    else if is_atp thy name then
blanchet@51186
   227
      fold (Integer.max o slice_max_facts)
blanchet@47606
   228
           (get_slices slice (#best_slices (get_atp thy name ()) ctxt)) 0
blanchet@43050
   229
    else (* is_smt_prover ctxt name *)
blanchet@43050
   230
      SMT_Solver.default_max_relevant ctxt name
blanchet@40941
   231
  end
blanchet@40063
   232
blanchet@42956
   233
fun is_if (@{const_name If}, _) = true
blanchet@42956
   234
  | is_if _ = false
blanchet@42956
   235
blanchet@42956
   236
(* Beware of "if and only if" (which is translated as such) and "If" (which is
blanchet@42956
   237
   translated to conditional equations). *)
blanchet@42956
   238
fun is_good_unit_equality T t u =
blanchet@42956
   239
  T <> @{typ bool} andalso not (exists (exists_Const is_if) [t, u])
blanchet@42956
   240
blanchet@42944
   241
fun is_unit_equality (@{const Trueprop} $ t) = is_unit_equality t
blanchet@42944
   242
  | is_unit_equality (Const (@{const_name all}, _) $ Abs (_, _, t)) =
blanchet@42944
   243
    is_unit_equality t
blanchet@42944
   244
  | is_unit_equality (Const (@{const_name All}, _) $ Abs (_, _, t)) =
blanchet@42944
   245
    is_unit_equality t
blanchet@42956
   246
  | is_unit_equality (Const (@{const_name "=="}, Type (_, [T, _])) $ t $ u) =
blanchet@42956
   247
    is_good_unit_equality T t u
blanchet@42956
   248
  | is_unit_equality (Const (@{const_name HOL.eq}, Type (_ , [T, _])) $ t $ u) =
blanchet@42956
   249
    is_good_unit_equality T t u
blanchet@42944
   250
  | is_unit_equality _ = false
blanchet@42944
   251
blanchet@51998
   252
fun is_appropriate_prop_of_prover ctxt name =
blanchet@42944
   253
  if is_unit_equational_atp ctxt name then is_unit_equality else K true
blanchet@42944
   254
blanchet@51998
   255
fun is_built_in_const_of_prover ctxt name =
blanchet@41242
   256
  if is_smt_prover ctxt name then
blanchet@41336
   257
    let val ctxt = ctxt |> select_smt_solver name in
blanchet@41336
   258
      fn x => fn ts =>
blanchet@41336
   259
         if SMT_Builtin.is_builtin_num_ext ctxt (list_comb (Const x, ts)) then
blanchet@41336
   260
           (true, [])
blanchet@41336
   261
         else if SMT_Builtin.is_builtin_fun_ext ctxt x ts then
blanchet@41336
   262
           (true, ts)
blanchet@41336
   263
         else
blanchet@41336
   264
           (false, ts)
blanchet@41336
   265
    end
blanchet@41242
   266
  else
blanchet@41336
   267
    fn (s, _) => fn ts => (member (op =) atp_irrelevant_consts s, ts)
blanchet@40071
   268
blanchet@40070
   269
(* FUDGE *)
blanchet@40070
   270
val atp_relevance_fudge =
blanchet@42738
   271
  {local_const_multiplier = 1.5,
blanchet@41159
   272
   worse_irrel_freq = 100.0,
blanchet@40070
   273
   higher_order_irrel_weight = 1.05,
blanchet@40070
   274
   abs_rel_weight = 0.5,
blanchet@40070
   275
   abs_irrel_weight = 2.0,
blanchet@47934
   276
   skolem_irrel_weight = 0.05,
blanchet@40070
   277
   theory_const_rel_weight = 0.5,
blanchet@40070
   278
   theory_const_irrel_weight = 0.25,
blanchet@42735
   279
   chained_const_irrel_weight = 0.25,
blanchet@40070
   280
   intro_bonus = 0.15,
blanchet@40070
   281
   elim_bonus = 0.15,
blanchet@40070
   282
   simp_bonus = 0.15,
blanchet@40070
   283
   local_bonus = 0.55,
blanchet@40070
   284
   assum_bonus = 1.05,
blanchet@40070
   285
   chained_bonus = 1.5,
blanchet@40070
   286
   max_imperfect = 11.5,
blanchet@40070
   287
   max_imperfect_exp = 1.0,
blanchet@40070
   288
   threshold_divisor = 2.0,
blanchet@41093
   289
   ridiculous_threshold = 0.01}
blanchet@40070
   290
blanchet@40071
   291
(* FUDGE (FIXME) *)
blanchet@40070
   292
val smt_relevance_fudge =
blanchet@42738
   293
  {local_const_multiplier = #local_const_multiplier atp_relevance_fudge,
blanchet@41159
   294
   worse_irrel_freq = #worse_irrel_freq atp_relevance_fudge,
blanchet@40071
   295
   higher_order_irrel_weight = #higher_order_irrel_weight atp_relevance_fudge,
blanchet@40071
   296
   abs_rel_weight = #abs_rel_weight atp_relevance_fudge,
blanchet@40071
   297
   abs_irrel_weight = #abs_irrel_weight atp_relevance_fudge,
blanchet@40071
   298
   skolem_irrel_weight = #skolem_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
   skolem_irrel_weight : real,
blanchet@48288
   372
   theory_const_rel_weight : real,
blanchet@48288
   373
   theory_const_irrel_weight : real,
blanchet@48288
   374
   chained_const_irrel_weight : real,
blanchet@48288
   375
   intro_bonus : real,
blanchet@48288
   376
   elim_bonus : real,
blanchet@48288
   377
   simp_bonus : real,
blanchet@48288
   378
   local_bonus : real,
blanchet@48288
   379
   assum_bonus : real,
blanchet@48288
   380
   chained_bonus : real,
blanchet@48288
   381
   max_imperfect : real,
blanchet@48288
   382
   max_imperfect_exp : real,
blanchet@48288
   383
   threshold_divisor : real,
blanchet@48288
   384
   ridiculous_threshold : real}
blanchet@48288
   385
blanchet@40061
   386
type prover_problem =
blanchet@48321
   387
  {state : Proof.state,
blanchet@48321
   388
   goal : thm,
blanchet@48321
   389
   subgoal : int,
blanchet@48321
   390
   subgoal_count : int,
blanchet@51010
   391
   factss : (string * fact list) list}
blanchet@35867
   392
blanchet@40061
   393
type prover_result =
blanchet@48321
   394
  {outcome : failure option,
blanchet@48321
   395
   used_facts : (string * stature) list,
blanchet@51009
   396
   used_from : fact list,
blanchet@48321
   397
   run_time : Time.time,
blanchet@50669
   398
   preplay : play Lazy.lazy,
blanchet@48321
   399
   message : play -> string,
blanchet@48321
   400
   message_tail : string}
blanchet@35867
   401
blanchet@43051
   402
type prover =
blanchet@45520
   403
  params -> ((string * string list) list -> string -> minimize_command)
blanchet@45520
   404
  -> prover_problem -> prover_result
blanchet@35867
   405
blanchet@38023
   406
(* configuration attributes *)
blanchet@38023
   407
blanchet@43092
   408
(* Empty string means create files in Isabelle's temporary files directory. *)
wenzelm@42616
   409
val dest_dir =
wenzelm@42616
   410
  Attrib.setup_config_string @{binding sledgehammer_dest_dir} (K "")
wenzelm@42616
   411
val problem_prefix =
wenzelm@42616
   412
  Attrib.setup_config_string @{binding sledgehammer_problem_prefix} (K "prob")
blanchet@48143
   413
val completish =
blanchet@48143
   414
  Attrib.setup_config_bool @{binding sledgehammer_completish} (K false)
wenzelm@28484
   415
blanchet@43092
   416
(* In addition to being easier to read, readable names are often much shorter,
blanchet@44394
   417
   especially if types are mangled in names. This makes a difference for some
blanchet@44394
   418
   provers (e.g., E). For these reason, short names are enabled by default. *)
blanchet@44592
   419
val atp_full_names =
blanchet@44592
   420
  Attrib.setup_config_bool @{binding sledgehammer_atp_full_names} (K false)
blanchet@43092
   421
blanchet@42646
   422
val smt_triggers =
blanchet@42646
   423
  Attrib.setup_config_bool @{binding sledgehammer_smt_triggers} (K true)
blanchet@42646
   424
val smt_weights =
blanchet@42646
   425
  Attrib.setup_config_bool @{binding sledgehammer_smt_weights} (K true)
blanchet@42646
   426
val smt_weight_min_facts =
blanchet@42646
   427
  Attrib.setup_config_int @{binding sledgehammer_smt_weight_min_facts} (K 20)
blanchet@41256
   428
blanchet@41256
   429
(* FUDGE *)
blanchet@42646
   430
val smt_min_weight =
blanchet@42646
   431
  Attrib.setup_config_int @{binding sledgehammer_smt_min_weight} (K 0)
blanchet@42646
   432
val smt_max_weight =
blanchet@42646
   433
  Attrib.setup_config_int @{binding sledgehammer_smt_max_weight} (K 10)
blanchet@42646
   434
val smt_max_weight_index =
blanchet@42646
   435
  Attrib.setup_config_int @{binding sledgehammer_smt_max_weight_index} (K 200)
blanchet@41256
   436
val smt_weight_curve = Unsynchronized.ref (fn x : int => x * x)
blanchet@41256
   437
blanchet@42646
   438
fun smt_fact_weight ctxt j num_facts =
blanchet@42646
   439
  if Config.get ctxt smt_weights andalso
blanchet@42646
   440
     num_facts >= Config.get ctxt smt_weight_min_facts then
blanchet@42646
   441
    let
blanchet@42646
   442
      val min = Config.get ctxt smt_min_weight
blanchet@42646
   443
      val max = Config.get ctxt smt_max_weight
blanchet@42646
   444
      val max_index = Config.get ctxt smt_max_weight_index
blanchet@42646
   445
      val curve = !smt_weight_curve
blanchet@42646
   446
    in
blanchet@42646
   447
      SOME (max - (max - min + 1) * curve (Int.max (0, max_index - j - 1))
blanchet@42646
   448
            div curve max_index)
blanchet@42646
   449
    end
blanchet@41256
   450
  else
blanchet@41256
   451
    NONE
blanchet@41256
   452
blanchet@42646
   453
fun weight_smt_fact ctxt num_facts ((info, th), j) =
blanchet@42646
   454
  let val thy = Proof_Context.theory_of ctxt in
blanchet@42646
   455
    (info, (smt_fact_weight ctxt j num_facts, th |> Thm.transfer thy))
blanchet@42646
   456
  end
blanchet@38023
   457
blanchet@51998
   458
fun overlord_file_location_of_prover prover =
blanchet@41313
   459
  (getenv "ISABELLE_HOME_USER", "prob_" ^ prover)
blanchet@41313
   460
blanchet@43052
   461
fun proof_banner mode name =
blanchet@43033
   462
  case mode of
blanchet@43033
   463
    Auto_Try => "Auto Sledgehammer (" ^ quote name ^ ") found a proof"
blanchet@43033
   464
  | Try => "Sledgehammer (" ^ quote name ^ ") found a proof"
blanchet@43052
   465
  | _ => "Try this"
blanchet@43033
   466
blanchet@45561
   467
fun bunch_of_reconstructors needs_full_types lam_trans =
blanchet@48800
   468
  if needs_full_types then
blanchet@48802
   469
    [Metis (full_type_enc, lam_trans false),
blanchet@48802
   470
     Metis (really_full_type_enc, lam_trans false),
blanchet@48802
   471
     Metis (full_type_enc, lam_trans true),
blanchet@48802
   472
     Metis (really_full_type_enc, lam_trans true),
blanchet@48802
   473
     SMT]
blanchet@48802
   474
  else
blanchet@48800
   475
    [Metis (partial_type_enc, lam_trans false),
blanchet@48800
   476
     Metis (full_type_enc, lam_trans false),
blanchet@48800
   477
     Metis (no_typesN, lam_trans true),
blanchet@48800
   478
     Metis (really_full_type_enc, lam_trans true),
blanchet@48800
   479
     SMT]
blanchet@45561
   480
blanchet@45561
   481
fun extract_reconstructor ({type_enc, lam_trans, ...} : params)
blanchet@45561
   482
                          (Metis (type_enc', lam_trans')) =
blanchet@45561
   483
    let
blanchet@45561
   484
      val override_params =
blanchet@45561
   485
        (if is_none type_enc andalso type_enc' = hd partial_type_encs then
blanchet@45561
   486
           []
blanchet@45561
   487
         else
blanchet@45566
   488
           [("type_enc", [hd (unalias_type_enc type_enc')])]) @
blanchet@45561
   489
        (if is_none lam_trans andalso lam_trans' = metis_default_lam_trans then
blanchet@45561
   490
           []
blanchet@45561
   491
         else
blanchet@45561
   492
           [("lam_trans", [lam_trans'])])
blanchet@45561
   493
    in (metisN, override_params) end
blanchet@45561
   494
  | extract_reconstructor _ SMT = (smtN, [])
blanchet@45561
   495
blanchet@43033
   496
(* based on "Mirabelle.can_apply" and generalized *)
blanchet@43034
   497
fun timed_apply timeout tac state i =
blanchet@43033
   498
  let
blanchet@43033
   499
    val {context = ctxt, facts, goal} = Proof.goal state
blanchet@43033
   500
    val full_tac = Method.insert_tac facts i THEN tac ctxt i
blanchet@50557
   501
  in time_limit timeout (try (Seq.pull o full_tac)) goal end
blanchet@43033
   502
blanchet@51998
   503
fun tac_of_reconstructor (Metis (type_enc, lam_trans)) =
blanchet@45521
   504
    metis_tac [type_enc] lam_trans
blanchet@51998
   505
  | tac_of_reconstructor SMT = SMT_Solver.smt_tac
blanchet@43034
   506
blanchet@45520
   507
fun timed_reconstructor reconstr debug timeout ths =
blanchet@44651
   508
  (Config.put Metis_Tactic.verbose debug
blanchet@45557
   509
   #> Config.put SMT_Config.verbose debug
blanchet@51998
   510
   #> (fn ctxt => tac_of_reconstructor reconstr ctxt ths))
blanchet@43034
   511
  |> timed_apply timeout
blanchet@43033
   512
blanchet@48798
   513
fun is_fact_chained ((_, (sc, _)), _) = sc = Chained
blanchet@48798
   514
blanchet@48798
   515
fun filter_used_facts keep_chained used =
blanchet@48798
   516
  filter ((member (op =) used o fst) orf
blanchet@48798
   517
          (if keep_chained then is_fact_chained else K false))
blanchet@43033
   518
blanchet@45781
   519
fun play_one_line_proof mode debug verbose timeout pairs state i preferred
blanchet@45520
   520
                        reconstrs =
blanchet@43034
   521
  let
blanchet@45520
   522
    fun get_preferred reconstrs =
blanchet@45520
   523
      if member (op =) reconstrs preferred then preferred
blanchet@45520
   524
      else List.last reconstrs
blanchet@43034
   525
  in
blanchet@50557
   526
    if timeout = SOME Time.zeroTime then
blanchet@45520
   527
      Trust_Playable (get_preferred reconstrs, NONE)
blanchet@45379
   528
    else
blanchet@50557
   529
      let
blanchet@50557
   530
        val _ =
blanchet@50557
   531
          if mode = Minimize then Output.urgent_message "Preplaying proof..."
blanchet@50557
   532
          else ()
blanchet@50557
   533
        val ths = pairs |> sort_wrt (fst o fst) |> map snd
blanchet@50557
   534
        fun play [] [] = Failed_to_Play (get_preferred reconstrs)
blanchet@50557
   535
          | play timed_outs [] =
blanchet@50557
   536
            Trust_Playable (get_preferred timed_outs, timeout)
blanchet@50557
   537
          | play timed_out (reconstr :: reconstrs) =
blanchet@50557
   538
            let
blanchet@50557
   539
              val _ =
blanchet@50557
   540
                if verbose then
blanchet@51998
   541
                  "Trying \"" ^ string_of_reconstructor reconstr ^ "\"" ^
blanchet@50557
   542
                  (case timeout of
blanchet@52031
   543
                     SOME timeout => " for " ^ string_of_time timeout
blanchet@50557
   544
                   | NONE => "") ^ "..."
blanchet@50557
   545
                  |> Output.urgent_message
blanchet@50557
   546
                else
blanchet@50557
   547
                  ()
blanchet@50557
   548
              val timer = Timer.startRealTimer ()
blanchet@50557
   549
            in
blanchet@50557
   550
              case timed_reconstructor reconstr debug timeout ths state i of
blanchet@50557
   551
                SOME (SOME _) => Played (reconstr, Timer.checkRealTimer timer)
blanchet@50557
   552
              | _ => play timed_out reconstrs
blanchet@50557
   553
            end
blanchet@50557
   554
            handle TimeLimit.TimeOut => play (reconstr :: timed_out) reconstrs
blanchet@50557
   555
      in play [] reconstrs end
blanchet@43033
   556
  end
blanchet@43033
   557
blanchet@41313
   558
blanchet@41256
   559
(* generic TPTP-based ATPs *)
blanchet@40061
   560
blanchet@42730
   561
(* Too general means, positive equality literal with a variable X as one
blanchet@42730
   562
   operand, when X does not occur properly in the other operand. This rules out
blanchet@42730
   563
   clearly inconsistent facts such as X = a | X = b, though it by no means
blanchet@42730
   564
   guarantees soundness. *)
blanchet@42730
   565
blanchet@51998
   566
fun get_facts_of_filter _ [(_, facts)] = facts
blanchet@51998
   567
  | get_facts_of_filter fact_filter factss =
blanchet@51013
   568
    case AList.lookup (op =) factss fact_filter of
blanchet@51013
   569
      SOME facts => facts
blanchet@51013
   570
    | NONE => snd (hd factss)
blanchet@51013
   571
blanchet@42730
   572
(* Unwanted equalities are those between a (bound or schematic) variable that
blanchet@42730
   573
   does not properly occur in the second operand. *)
blanchet@42730
   574
val is_exhaustive_finite =
blanchet@42730
   575
  let
blanchet@42730
   576
    fun is_bad_equal (Var z) t =
blanchet@42730
   577
        not (exists_subterm (fn Var z' => z = z' | _ => false) t)
blanchet@42730
   578
      | is_bad_equal (Bound j) t = not (loose_bvar1 (t, j))
blanchet@42730
   579
      | is_bad_equal _ _ = false
blanchet@42730
   580
    fun do_equals t1 t2 = is_bad_equal t1 t2 orelse is_bad_equal t2 t1
blanchet@42730
   581
    fun do_formula pos t =
blanchet@42730
   582
      case (pos, t) of
blanchet@42730
   583
        (_, @{const Trueprop} $ t1) => do_formula pos t1
blanchet@42730
   584
      | (true, Const (@{const_name all}, _) $ Abs (_, _, t')) =>
blanchet@42730
   585
        do_formula pos t'
blanchet@42730
   586
      | (true, Const (@{const_name All}, _) $ Abs (_, _, t')) =>
blanchet@42730
   587
        do_formula pos t'
blanchet@42730
   588
      | (false, Const (@{const_name Ex}, _) $ Abs (_, _, t')) =>
blanchet@42730
   589
        do_formula pos t'
blanchet@42730
   590
      | (_, @{const "==>"} $ t1 $ t2) =>
blanchet@42730
   591
        do_formula (not pos) t1 andalso
blanchet@42730
   592
        (t2 = @{prop False} orelse do_formula pos t2)
blanchet@42730
   593
      | (_, @{const HOL.implies} $ t1 $ t2) =>
blanchet@42730
   594
        do_formula (not pos) t1 andalso
blanchet@42730
   595
        (t2 = @{const False} orelse do_formula pos t2)
blanchet@42730
   596
      | (_, @{const Not} $ t1) => do_formula (not pos) t1
blanchet@42730
   597
      | (true, @{const HOL.disj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
blanchet@42730
   598
      | (false, @{const HOL.conj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
blanchet@42730
   599
      | (true, Const (@{const_name HOL.eq}, _) $ t1 $ t2) => do_equals t1 t2
blanchet@42730
   600
      | (true, Const (@{const_name "=="}, _) $ t1 $ t2) => do_equals t1 t2
blanchet@42730
   601
      | _ => false
blanchet@42730
   602
  in do_formula true end
blanchet@42730
   603
blanchet@42730
   604
fun has_bound_or_var_of_type pred =
blanchet@42730
   605
  exists_subterm (fn Var (_, T as Type _) => pred T
blanchet@42730
   606
                   | Abs (_, T as Type _, _) => pred T
blanchet@42730
   607
                   | _ => false)
blanchet@42730
   608
blanchet@42730
   609
(* Facts are forbidden to contain variables of these types. The typical reason
blanchet@42730
   610
   is that they lead to unsoundness. Note that "unit" satisfies numerous
blanchet@42730
   611
   equations like "?x = ()". The resulting clauses will have no type constraint,
blanchet@42730
   612
   yielding false proofs. Even "bool" leads to many unsound proofs, though only
blanchet@42730
   613
   for higher-order problems. *)
blanchet@42730
   614
blanchet@42730
   615
(* Facts containing variables of type "unit" or "bool" or of the form
blanchet@42730
   616
   "ALL x. x = A | x = B | x = C" are likely to lead to unsound proofs if types
blanchet@42730
   617
   are omitted. *)
blanchet@42944
   618
fun is_dangerous_prop ctxt =
blanchet@42944
   619
  transform_elim_prop
blanchet@44393
   620
  #> (has_bound_or_var_of_type (is_type_surely_finite ctxt) orf
blanchet@42730
   621
      is_exhaustive_finite)
blanchet@42730
   622
blanchet@39492
   623
(* Important messages are important but not so important that users want to see
blanchet@39492
   624
   them each time. *)
blanchet@44649
   625
val atp_important_message_keep_quotient = 25
blanchet@39492
   626
blanchet@44416
   627
fun choose_type_enc soundness best_type_enc format =
blanchet@44397
   628
  the_default best_type_enc
blanchet@52031
   629
  #> type_enc_of_string soundness
blanchet@44416
   630
  #> adjust_type_enc format
blanchet@42548
   631
blanchet@51200
   632
fun isar_proof_reconstructor ctxt name =
blanchet@51200
   633
  let val thy = Proof_Context.theory_of ctxt in
blanchet@51200
   634
    if is_atp thy name then name
blanchet@51200
   635
    else remotify_prover_if_not_installed ctxt eN |> the_default name
blanchet@51200
   636
  end
blanchet@43051
   637
blanchet@51200
   638
(* See the analogous logic in the function "maybe_minimize" in
blanchet@51200
   639
  "sledgehammer_minimize.ML". *)
blanchet@51200
   640
fun choose_minimize_command ctxt (params as {isar_proofs, ...}) minimize_command
blanchet@51200
   641
                            name preplay =
blanchet@45520
   642
  let
blanchet@51200
   643
    val maybe_isar_name =
blanchet@51200
   644
      name |> isar_proofs = SOME true ? isar_proof_reconstructor ctxt
blanchet@51200
   645
    val (min_name, override_params) =
blanchet@45520
   646
      case preplay of
blanchet@51200
   647
        Played (reconstr, _) =>
blanchet@51200
   648
        if isar_proofs = SOME true then (maybe_isar_name, [])
blanchet@51200
   649
        else extract_reconstructor params reconstr
blanchet@51200
   650
      | _ => (maybe_isar_name, [])
blanchet@51200
   651
  in minimize_command override_params min_name end
blanchet@43051
   652
blanchet@53480
   653
val max_fact_instances = 10 (* FUDGE *)
blanchet@53480
   654
blanchet@52034
   655
fun repair_monomorph_context max_iters best_max_iters max_new_instances
blanchet@52034
   656
                             best_max_new_instances =
blanchet@52034
   657
  Config.put Monomorph.max_rounds (max_iters |> the_default best_max_iters)
blanchet@52034
   658
  #> Config.put Monomorph.max_new_instances
blanchet@52034
   659
         (max_new_instances |> the_default best_max_new_instances)
blanchet@53480
   660
  #> Config.put Monomorph.max_thm_instances max_fact_instances
blanchet@52034
   661
blanchet@51998
   662
fun suffix_of_mode Auto_Try = "_try"
blanchet@51998
   663
  | suffix_of_mode Try = "_try"
blanchet@51998
   664
  | suffix_of_mode Normal = ""
blanchet@51998
   665
  | suffix_of_mode MaSh = ""
blanchet@51998
   666
  | suffix_of_mode Auto_Minimize = "_min"
blanchet@51998
   667
  | suffix_of_mode Minimize = "_min"
blanchet@44509
   668
blanchet@44423
   669
(* Give the ATPs some slack before interrupting them the hard way. "z3_tptp" on
blanchet@43631
   670
   Linux appears to be the only ATP that does not honor its time limit. *)
blanchet@43690
   671
val atp_timeout_slack = seconds 1.0
blanchet@43631
   672
blanchet@48331
   673
val mono_max_privileged_facts = 10
blanchet@48331
   674
blanchet@51186
   675
(* For low values of "max_facts", this fudge value ensures that most slices are
blanchet@51186
   676
   invoked with a nontrivial amount of facts. *)
blanchet@51186
   677
val max_fact_factor_fudge = 5
blanchet@51186
   678
blanchet@43021
   679
fun run_atp mode name
blanchet@48376
   680
        ({exec, arguments, proof_delims, known_failures, prem_role, best_slices,
blanchet@48376
   681
          best_max_mono_iters, best_max_new_mono_instances, ...} : atp_config)
blanchet@46301
   682
        (params as {debug, verbose, overlord, type_enc, strict, lam_trans,
blanchet@51024
   683
                    uncurried_aliases, fact_filter, max_facts, max_mono_iters,
smolkas@51130
   684
                    max_new_mono_instances, isar_proofs, isar_compress,
blanchet@53137
   685
                    isar_compress_degree, merge_timeout_slack, isar_try0,
blanchet@53137
   686
                    isar_minimize, slice, timeout, preplay_timeout,
blanchet@53137
   687
                    preplay_trace, ...})
blanchet@43037
   688
        minimize_command
blanchet@51013
   689
        ({state, goal, subgoal, subgoal_count, factss, ...} : prover_problem) =
blanchet@38023
   690
  let
blanchet@42182
   691
    val thy = Proof.theory_of state
blanchet@39318
   692
    val ctxt = Proof.context_of state
blanchet@47946
   693
    val atp_mode =
blanchet@48143
   694
      if Config.get ctxt completish then Sledgehammer_Completish
blanchet@47946
   695
      else Sledgehammer
blanchet@52196
   696
    val (_, hyp_ts, concl_t) = strip_subgoal goal subgoal ctxt
blanchet@41159
   697
    val (dest_dir, problem_prefix) =
blanchet@51998
   698
      if overlord then overlord_file_location_of_prover name
blanchet@41159
   699
      else (Config.get ctxt dest_dir, Config.get ctxt problem_prefix)
blanchet@40061
   700
    val problem_file_name =
blanchet@41159
   701
      Path.basic (problem_prefix ^ (if overlord then "" else serial_string ()) ^
blanchet@51998
   702
                  suffix_of_mode mode ^ "_" ^ string_of_int subgoal)
blanchet@48656
   703
    val prob_path =
blanchet@40059
   704
      if dest_dir = "" then
blanchet@40061
   705
        File.tmp_path problem_file_name
blanchet@40059
   706
      else if File.exists (Path.explode dest_dir) then
blanchet@40061
   707
        Path.append (Path.explode dest_dir) problem_file_name
blanchet@39003
   708
      else
blanchet@40059
   709
        error ("No such directory: " ^ quote dest_dir ^ ".")
blanchet@52754
   710
    val exec = exec ()
blanchet@48376
   711
    val command0 =
blanchet@47055
   712
      case find_first (fn var => getenv var <> "") (fst exec) of
blanchet@48376
   713
        SOME var =>
blanchet@48376
   714
        let
blanchet@48376
   715
          val pref = getenv var ^ "/"
blanchet@48376
   716
          val paths = map (Path.explode o prefix pref) (snd exec)
blanchet@48376
   717
        in
blanchet@48376
   718
          case find_first File.exists paths of
blanchet@48376
   719
            SOME path => path
blanchet@48376
   720
          | NONE => error ("Bad executable: " ^ Path.print (hd paths) ^ ".")
blanchet@48376
   721
        end
blanchet@47055
   722
      | NONE => error ("The environment variable " ^ quote (hd (fst exec)) ^
blanchet@47055
   723
                       " is not set.")
blanchet@38023
   724
    fun split_time s =
blanchet@38023
   725
      let
blanchet@42448
   726
        val split = String.tokens (fn c => str c = "\n")
blanchet@47737
   727
        val (output, t) =
blanchet@47737
   728
          s |> split |> (try split_last #> the_default ([], "0"))
blanchet@47737
   729
            |>> cat_lines
blanchet@42448
   730
        fun as_num f = f >> (fst o read_int)
blanchet@42448
   731
        val num = as_num (Scan.many1 Symbol.is_ascii_digit)
blanchet@42448
   732
        val digit = Scan.one Symbol.is_ascii_digit
blanchet@42448
   733
        val num3 = as_num (digit ::: digit ::: (digit >> single))
blanchet@42448
   734
        val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b)
blanchet@45381
   735
        val as_time =
blanchet@45381
   736
          raw_explode #> Scan.read Symbol.stopper time #> the_default 0
blanchet@47737
   737
      in (output, as_time t |> Time.fromMilliseconds) end
blanchet@48656
   738
    fun run () =
blanchet@48376
   739
      let
blanchet@48376
   740
        (* If slicing is disabled, we expand the last slice to fill the entire
blanchet@48376
   741
           time available. *)
blanchet@48376
   742
        val actual_slices = get_slices slice (best_slices ctxt)
blanchet@48376
   743
        val num_actual_slices = length actual_slices
blanchet@51186
   744
        val max_fact_factor =
blanchet@51186
   745
          case max_facts of
blanchet@51186
   746
            NONE => 1.0
blanchet@51186
   747
          | SOME max =>
blanchet@51186
   748
            Real.fromInt max
blanchet@51186
   749
            / Real.fromInt (fold (Integer.max o slice_max_facts)
blanchet@51186
   750
                                 actual_slices 0)
blanchet@48376
   751
        fun monomorphize_facts facts =
blanchet@48376
   752
          let
blanchet@48376
   753
            val ctxt =
blanchet@48376
   754
              ctxt
blanchet@53478
   755
              |> repair_monomorph_context max_mono_iters
blanchet@52034
   756
                     best_max_mono_iters max_new_mono_instances
blanchet@52034
   757
                     best_max_new_mono_instances
blanchet@48376
   758
            (* pseudo-theorem involving the same constants as the subgoal *)
blanchet@48376
   759
            val subgoal_th =
blanchet@48376
   760
              Logic.list_implies (hyp_ts, concl_t) |> Skip_Proof.make_thm thy
blanchet@48376
   761
            val rths =
blanchet@48376
   762
              facts |> chop mono_max_privileged_facts
blanchet@48376
   763
                    |>> map (pair 1 o snd)
blanchet@48376
   764
                    ||> map (pair 2 o snd)
blanchet@48376
   765
                    |> op @
blanchet@48376
   766
                    |> cons (0, subgoal_th)
blanchet@48376
   767
          in
blanchet@53478
   768
            Monomorph.monomorph atp_schematic_consts_of ctxt rths
blanchet@53478
   769
            |> tl |> curry ListPair.zip (map fst facts)
blanchet@48376
   770
            |> maps (fn (name, rths) =>
blanchet@48376
   771
                        map (pair name o zero_var_indexes o snd) rths)
blanchet@48376
   772
          end
blanchet@48376
   773
        fun run_slice time_left (cache_key, cache_value)
blanchet@48716
   774
                (slice, (time_frac,
blanchet@51011
   775
                     (key as ((best_max_facts, best_fact_filter), format,
blanchet@51011
   776
                              best_type_enc, best_lam_trans,
blanchet@51011
   777
                              best_uncurried_aliases),
blanchet@48716
   778
                      extra))) =
blanchet@38032
   779
          let
blanchet@51024
   780
            val effective_fact_filter =
blanchet@51024
   781
              fact_filter |> the_default best_fact_filter
blanchet@51998
   782
            val facts = get_facts_of_filter effective_fact_filter factss
blanchet@48376
   783
            val num_facts =
blanchet@51186
   784
              Real.ceil (max_fact_factor * Real.fromInt best_max_facts) +
blanchet@51186
   785
              max_fact_factor_fudge
blanchet@51186
   786
              |> Integer.min (length facts)
blanchet@48376
   787
            val soundness = if strict then Strict else Non_Strict
blanchet@48376
   788
            val type_enc =
blanchet@48376
   789
              type_enc |> choose_type_enc soundness best_type_enc format
blanchet@48376
   790
            val sound = is_type_enc_sound type_enc
blanchet@48376
   791
            val real_ms = Real.fromInt o Time.toMilliseconds
blanchet@48376
   792
            val slice_timeout =
blanchet@50557
   793
              case time_left of
blanchet@50557
   794
                SOME time_left =>
blanchet@50557
   795
                ((real_ms time_left
blanchet@50557
   796
                  |> (if slice < num_actual_slices - 1 then
blanchet@50557
   797
                        curry Real.min (time_frac * real_ms (the timeout))
blanchet@50557
   798
                      else
blanchet@50557
   799
                        I))
blanchet@50557
   800
                 * 0.001)
blanchet@50557
   801
                |> seconds |> SOME
blanchet@50557
   802
              | NONE => NONE
blanchet@48376
   803
            val generous_slice_timeout =
blanchet@50558
   804
              if mode = MaSh then NONE
blanchet@50558
   805
              else Option.map (curry Time.+ atp_timeout_slack) slice_timeout
blanchet@48376
   806
            val _ =
blanchet@48376
   807
              if debug then
blanchet@48376
   808
                quote name ^ " slice #" ^ string_of_int (slice + 1) ^
blanchet@48376
   809
                " with " ^ string_of_int num_facts ^ " fact" ^
blanchet@50557
   810
                plural_s num_facts ^
blanchet@50557
   811
                (case slice_timeout of
blanchet@52031
   812
                   SOME timeout => " for " ^ string_of_time timeout
blanchet@50557
   813
                 | NONE => "") ^ "..."
blanchet@48376
   814
                |> Output.urgent_message
blanchet@48376
   815
              else
blanchet@48376
   816
                ()
blanchet@48376
   817
            val readable_names = not (Config.get ctxt atp_full_names)
blanchet@48376
   818
            val lam_trans =
blanchet@48376
   819
              case lam_trans of
blanchet@48376
   820
                SOME s => s
blanchet@48376
   821
              | NONE => best_lam_trans
blanchet@48376
   822
            val uncurried_aliases =
blanchet@48376
   823
              case uncurried_aliases of
blanchet@48376
   824
                SOME b => b
blanchet@48376
   825
              | NONE => best_uncurried_aliases
blanchet@48376
   826
            val value as (atp_problem, _, fact_names, _, _) =
blanchet@48376
   827
              if cache_key = SOME key then
blanchet@48376
   828
                cache_value
blanchet@48376
   829
              else
blanchet@48376
   830
                facts
blanchet@48376
   831
                |> not sound
blanchet@48376
   832
                   ? filter_out (is_dangerous_prop ctxt o prop_of o snd)
blanchet@48376
   833
                |> take num_facts
blanchet@48376
   834
                |> not (is_type_enc_polymorphic type_enc) ? monomorphize_facts
blanchet@48376
   835
                |> map (apsnd prop_of)
blanchet@48376
   836
                |> prepare_atp_problem ctxt format prem_role type_enc atp_mode
blanchet@48376
   837
                                       lam_trans uncurried_aliases
blanchet@48376
   838
                                       readable_names true hyp_ts concl_t
blanchet@48376
   839
            fun sel_weights () = atp_problem_selection_weights atp_problem
blanchet@48376
   840
            fun ord_info () = atp_problem_term_order_info atp_problem
blanchet@48376
   841
            val ord = effective_term_order ctxt name
blanchet@51200
   842
            val full_proof =
blanchet@51200
   843
              debug orelse (isar_proofs |> the_default (mode = Minimize))
blanchet@50927
   844
            val args =
blanchet@50927
   845
              arguments ctxt full_proof extra
blanchet@50927
   846
                        (slice_timeout |> the_default one_day)
blanchet@50927
   847
                        (File.shell_path prob_path) (ord, ord_info, sel_weights)
blanchet@48376
   848
            val command =
blanchet@50927
   849
              "(exec 2>&1; " ^ File.shell_path command0 ^ " " ^ args ^ " " ^ ")"
blanchet@48532
   850
              |> enclose "TIMEFORMAT='%3R'; { time " " ; }"
blanchet@48376
   851
            val _ =
blanchet@48376
   852
              atp_problem
blanchet@51998
   853
              |> lines_of_atp_problem format ord ord_info
blanchet@48376
   854
              |> cons ("% " ^ command ^ "\n")
blanchet@48656
   855
              |> File.write_list prob_path
blanchet@52034
   856
            val ((output, run_time), (atp_proof, outcome)) =
blanchet@50557
   857
              time_limit generous_slice_timeout Isabelle_System.bash_output
blanchet@50557
   858
                         command
blanchet@48376
   859
              |>> (if overlord then
blanchet@48376
   860
                     prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n")
blanchet@48376
   861
                   else
blanchet@48376
   862
                     I)
blanchet@48376
   863
              |> fst |> split_time
blanchet@48376
   864
              |> (fn accum as (output, _) =>
blanchet@52034
   865
                     (accum,
blanchet@48716
   866
                      extract_tstplike_proof_and_outcome verbose proof_delims
blanchet@48716
   867
                                                         known_failures output
blanchet@52031
   868
                      |>> atp_proof_of_tstplike_proof atp_problem
blanchet@48376
   869
                      handle UNRECOGNIZED_ATP_PROOF () =>
blanchet@48376
   870
                             ([], SOME ProofIncomplete)))
blanchet@48376
   871
              handle TimeLimit.TimeOut =>
blanchet@52034
   872
                     (("", the slice_timeout), ([], SOME TimedOut))
blanchet@48376
   873
            val outcome =
blanchet@48376
   874
              case outcome of
blanchet@48376
   875
                NONE =>
blanchet@48376
   876
                (case used_facts_in_unsound_atp_proof ctxt fact_names atp_proof
blanchet@48376
   877
                      |> Option.map (sort string_ord) of
blanchet@48376
   878
                   SOME facts =>
blanchet@48376
   879
                   let
blanchet@48376
   880
                     val failure =
blanchet@48376
   881
                       UnsoundProof (is_type_enc_sound type_enc, facts)
blanchet@48376
   882
                   in
blanchet@51998
   883
                     if debug then (warning (string_of_failure failure); NONE)
blanchet@48376
   884
                     else 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@51998
   987
         fn _ => string_of_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@42614
  1126
              if verbose andalso is_some outcome then
blanchet@51024
  1127
                quote name ^ " invoked with " ^
blanchet@51024
  1128
                num_of_facts fact_filter num_facts ^ ": " ^
blanchet@52031
  1129
                string_of_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@51998
  1185
         fn _ => string_of_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@51998
  1233
         message = fn _ => string_of_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;