src/HOL/Tools/Sledgehammer/sledgehammer_provers.ML
author blanchet
Tue May 31 16:38:36 2011 +0200 (2011-05-31 ago)
changeset 43085 0a2f5b86bdd7
parent 43063 8f1f80a40498
child 43092 93ec303e1917
permissions -rw-r--r--
first step in sharing more code between ATP and Metis translation
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@43085
    12
  type locality = ATP_Translate.locality
blanchet@40070
    13
  type relevance_fudge = Sledgehammer_Filter.relevance_fudge
blanchet@43085
    14
  type translated_formula = ATP_Translate.translated_formula
blanchet@43085
    15
  type type_system = ATP_Translate.type_system
blanchet@43085
    16
  type play = ATP_Reconstruct.play
blanchet@43085
    17
  type minimize_command = ATP_Reconstruct.minimize_command
blanchet@39493
    18
blanchet@43021
    19
  datatype mode = Auto_Try | Try | Normal | Minimize
blanchet@43021
    20
blanchet@42579
    21
  datatype rich_type_system =
blanchet@42579
    22
    Dumb_Type_Sys of type_system |
blanchet@42579
    23
    Smart_Type_Sys of bool
blanchet@42579
    24
blanchet@35969
    25
  type params =
blanchet@41208
    26
    {debug: bool,
blanchet@35969
    27
     verbose: bool,
blanchet@36143
    28
     overlord: bool,
blanchet@41208
    29
     blocking: bool,
blanchet@40059
    30
     provers: string list,
blanchet@42579
    31
     type_sys: rich_type_system,
blanchet@42180
    32
     relevance_thresholds: real * real,
blanchet@42180
    33
     max_relevant: int option,
blanchet@42724
    34
     max_mono_iters: int,
blanchet@42740
    35
     max_new_mono_instances: int,
blanchet@43063
    36
     explicit_apply: bool option,
blanchet@35969
    37
     isar_proof: bool,
blanchet@36924
    38
     isar_shrink_factor: int,
blanchet@42443
    39
     slicing: bool,
blanchet@38985
    40
     timeout: Time.time,
blanchet@43015
    41
     preplay_timeout: Time.time,
blanchet@38985
    42
     expect: string}
blanchet@39493
    43
blanchet@41090
    44
  datatype prover_fact =
blanchet@41090
    45
    Untranslated_Fact of (string * locality) * thm |
blanchet@41242
    46
    SMT_Weighted_Fact of (string * locality) * (int option * thm)
blanchet@40061
    47
blanchet@40061
    48
  type prover_problem =
blanchet@39318
    49
    {state: Proof.state,
blanchet@38998
    50
     goal: thm,
blanchet@38998
    51
     subgoal: int,
blanchet@40065
    52
     subgoal_count: int,
blanchet@41242
    53
     facts: prover_fact list,
blanchet@41741
    54
     smt_filter: (string * locality) SMT_Solver.smt_filter_data option}
blanchet@39493
    55
blanchet@40061
    56
  type prover_result =
blanchet@36370
    57
    {outcome: failure option,
blanchet@40204
    58
     used_facts: (string * locality) list,
blanchet@40062
    59
     run_time_in_msecs: int option,
blanchet@43052
    60
     preplay: unit -> play,
blanchet@43052
    61
     message: play -> string}
blanchet@39493
    62
blanchet@43051
    63
  type prover =
blanchet@43051
    64
    params -> (string -> minimize_command) -> prover_problem -> prover_result
blanchet@35867
    65
blanchet@42646
    66
  val smt_triggers : bool Config.T
blanchet@42646
    67
  val smt_weights : bool Config.T
blanchet@42646
    68
  val smt_weight_min_facts : int Config.T
blanchet@42646
    69
  val smt_min_weight : int Config.T
blanchet@42646
    70
  val smt_max_weight : int Config.T
blanchet@42646
    71
  val smt_max_weight_index : int Config.T
blanchet@41256
    72
  val smt_weight_curve : (int -> int) Unsynchronized.ref
blanchet@42646
    73
  val smt_max_slices : int Config.T
blanchet@42646
    74
  val smt_slice_fact_frac : real Config.T
blanchet@42646
    75
  val smt_slice_time_frac : real Config.T
blanchet@42646
    76
  val smt_slice_min_secs : int Config.T
blanchet@43005
    77
  val das_tool : string
blanchet@41242
    78
  val select_smt_solver : string -> Proof.context -> Proof.context
blanchet@43050
    79
  val is_metis_prover : string -> bool
blanchet@43050
    80
  val is_atp : theory -> string -> bool
blanchet@41087
    81
  val is_smt_prover : Proof.context -> string -> bool
blanchet@42944
    82
  val is_unit_equational_atp : Proof.context -> string -> bool
blanchet@41727
    83
  val is_prover_supported : Proof.context -> string -> bool
blanchet@40072
    84
  val is_prover_installed : Proof.context -> string -> bool
blanchet@42443
    85
  val default_max_relevant_for_prover : Proof.context -> bool -> string -> int
blanchet@42944
    86
  val is_unit_equality : term -> bool
blanchet@42952
    87
  val is_appropriate_prop_for_prover : Proof.context -> string -> term -> bool
blanchet@40369
    88
  val is_built_in_const_for_prover :
blanchet@41336
    89
    Proof.context -> string -> string * typ -> term list -> bool * term list
blanchet@41087
    90
  val atp_relevance_fudge : relevance_fudge
blanchet@41087
    91
  val smt_relevance_fudge : relevance_fudge
blanchet@40941
    92
  val relevance_fudge_for_prover : Proof.context -> string -> relevance_fudge
blanchet@38023
    93
  val dest_dir : string Config.T
blanchet@38023
    94
  val problem_prefix : string Config.T
blanchet@39003
    95
  val measure_run_time : bool Config.T
blanchet@41256
    96
  val weight_smt_fact :
blanchet@42646
    97
    Proof.context -> int -> ((string * locality) * thm) * int
blanchet@41256
    98
    -> (string * locality) * (int option * thm)
blanchet@41091
    99
  val untranslated_fact : prover_fact -> (string * locality) * thm
blanchet@41242
   100
  val smt_weighted_fact :
blanchet@42646
   101
    Proof.context -> int -> prover_fact * int
blanchet@41256
   102
    -> (string * locality) * (int option * thm)
blanchet@41727
   103
  val supported_provers : Proof.context -> unit
blanchet@40059
   104
  val kill_provers : unit -> unit
blanchet@40059
   105
  val running_provers : unit -> unit
blanchet@40059
   106
  val messages : int option -> unit
blanchet@43033
   107
  val filter_used_facts : ''a list -> (''a * 'b) list -> (''a * 'b) list
blanchet@43021
   108
  val get_prover : Proof.context -> mode -> string -> prover
wenzelm@28477
   109
end;
wenzelm@28477
   110
blanchet@41087
   111
structure Sledgehammer_Provers : SLEDGEHAMMER_PROVERS =
wenzelm@28477
   112
struct
wenzelm@28477
   113
blanchet@43085
   114
open ATP_Util
blanchet@38028
   115
open ATP_Problem
blanchet@39491
   116
open ATP_Proof
blanchet@38028
   117
open ATP_Systems
blanchet@43085
   118
open ATP_Translate
blanchet@43085
   119
open ATP_Reconstruct
blanchet@38023
   120
open Sledgehammer_Util
blanchet@38988
   121
open Sledgehammer_Filter
blanchet@37583
   122
blanchet@37583
   123
(** The Sledgehammer **)
blanchet@37583
   124
blanchet@43021
   125
datatype mode = Auto_Try | Try | Normal | Minimize
blanchet@43021
   126
blanchet@38102
   127
(* Identifier to distinguish Sledgehammer from other tools using
blanchet@38102
   128
   "Async_Manager". *)
blanchet@43005
   129
val das_tool = "Sledgehammer"
blanchet@37585
   130
blanchet@43050
   131
val metis_prover_names = [Metis_Tactics.metisN, Metis_Tactics.metisFT_N]
blanchet@43050
   132
blanchet@43050
   133
val is_metis_prover = member (op =) metis_prover_names
blanchet@43050
   134
val is_atp = member (op =) o supported_atps
blanchet@43050
   135
blanchet@41242
   136
val select_smt_solver =
boehmes@41432
   137
  Context.proof_map o SMT_Config.select_solver
blanchet@41242
   138
blanchet@40941
   139
fun is_smt_prover ctxt name =
boehmes@41432
   140
  member (op =) (SMT_Solver.available_solvers_of ctxt) name
blanchet@40062
   141
blanchet@42944
   142
fun is_unit_equational_atp ctxt name =
blanchet@42944
   143
  let val thy = Proof_Context.theory_of ctxt in
blanchet@42944
   144
    case try (get_atp thy) name of
blanchet@42944
   145
      SOME {formats, ...} => member (op =) formats CNF_UEQ
blanchet@42944
   146
    | NONE => false
blanchet@42944
   147
  end
blanchet@42944
   148
blanchet@41727
   149
fun is_prover_supported ctxt name =
wenzelm@42361
   150
  let val thy = Proof_Context.theory_of ctxt in
blanchet@43050
   151
    is_metis_prover name orelse is_atp thy name orelse is_smt_prover ctxt name
blanchet@40941
   152
  end
blanchet@40072
   153
boehmes@41432
   154
fun is_prover_installed ctxt =
blanchet@43050
   155
  is_metis_prover orf is_smt_prover ctxt orf
blanchet@43050
   156
  is_atp_installed (Proof_Context.theory_of ctxt)
blanchet@40941
   157
blanchet@43063
   158
fun get_slices num_facts slicing slices =
blanchet@43063
   159
  (0 upto length slices - 1) ~~ slices
blanchet@43063
   160
  |> (if slicing andalso
blanchet@43063
   161
         exists (fn (_, (_, (max_facts, _))) => max_facts < num_facts)
blanchet@43063
   162
                slices then
blanchet@43063
   163
        I
blanchet@43063
   164
      else
blanchet@43063
   165
        List.last #> single)
blanchet@42443
   166
blanchet@43050
   167
val metis_default_max_relevant = 20
blanchet@43050
   168
blanchet@42443
   169
fun default_max_relevant_for_prover ctxt slicing name =
wenzelm@42361
   170
  let val thy = Proof_Context.theory_of ctxt in
blanchet@43050
   171
    if is_metis_prover name then
blanchet@43050
   172
      metis_default_max_relevant
blanchet@43050
   173
    else if is_atp thy name then
blanchet@42723
   174
      fold (Integer.max o fst o snd o snd o snd)
blanchet@43063
   175
           (get_slices 16384 (* large number *) slicing
blanchet@43063
   176
                       (#best_slices (get_atp thy name) ctxt)) 0
blanchet@43050
   177
    else (* is_smt_prover ctxt name *)
blanchet@43050
   178
      SMT_Solver.default_max_relevant ctxt name
blanchet@40941
   179
  end
blanchet@40063
   180
blanchet@41140
   181
(* These are either simplified away by "Meson.presimplify" (most of the time) or
blanchet@41140
   182
   handled specially via "fFalse", "fTrue", ..., "fequal". *)
blanchet@40071
   183
val atp_irrelevant_consts =
blanchet@41140
   184
  [@{const_name False}, @{const_name True}, @{const_name Not},
blanchet@41140
   185
   @{const_name conj}, @{const_name disj}, @{const_name implies},
blanchet@41140
   186
   @{const_name HOL.eq}, @{const_name If}, @{const_name Let}]
blanchet@40206
   187
blanchet@42956
   188
fun is_if (@{const_name If}, _) = true
blanchet@42956
   189
  | is_if _ = false
blanchet@42956
   190
blanchet@42956
   191
(* Beware of "if and only if" (which is translated as such) and "If" (which is
blanchet@42956
   192
   translated to conditional equations). *)
blanchet@42956
   193
fun is_good_unit_equality T t u =
blanchet@42956
   194
  T <> @{typ bool} andalso not (exists (exists_Const is_if) [t, u])
blanchet@42956
   195
blanchet@42944
   196
fun is_unit_equality (@{const Trueprop} $ t) = is_unit_equality t
blanchet@42944
   197
  | is_unit_equality (Const (@{const_name all}, _) $ Abs (_, _, t)) =
blanchet@42944
   198
    is_unit_equality t
blanchet@42944
   199
  | is_unit_equality (Const (@{const_name All}, _) $ Abs (_, _, t)) =
blanchet@42944
   200
    is_unit_equality t
blanchet@42956
   201
  | is_unit_equality (Const (@{const_name "=="}, Type (_, [T, _])) $ t $ u) =
blanchet@42956
   202
    is_good_unit_equality T t u
blanchet@42956
   203
  | is_unit_equality (Const (@{const_name HOL.eq}, Type (_ , [T, _])) $ t $ u) =
blanchet@42956
   204
    is_good_unit_equality T t u
blanchet@42944
   205
  | is_unit_equality _ = false
blanchet@42944
   206
blanchet@42952
   207
fun is_appropriate_prop_for_prover ctxt name =
blanchet@42944
   208
  if is_unit_equational_atp ctxt name then is_unit_equality else K true
blanchet@42944
   209
blanchet@41242
   210
fun is_built_in_const_for_prover ctxt name =
blanchet@41242
   211
  if is_smt_prover ctxt name then
blanchet@41336
   212
    let val ctxt = ctxt |> select_smt_solver name in
blanchet@41336
   213
      fn x => fn ts =>
blanchet@41336
   214
         if SMT_Builtin.is_builtin_num_ext ctxt (list_comb (Const x, ts)) then
blanchet@41336
   215
           (true, [])
blanchet@41336
   216
         else if SMT_Builtin.is_builtin_fun_ext ctxt x ts then
blanchet@41336
   217
           (true, ts)
blanchet@41336
   218
         else
blanchet@41336
   219
           (false, ts)
blanchet@41336
   220
    end
blanchet@41242
   221
  else
blanchet@41336
   222
    fn (s, _) => fn ts => (member (op =) atp_irrelevant_consts s, ts)
blanchet@40071
   223
blanchet@40070
   224
(* FUDGE *)
blanchet@40070
   225
val atp_relevance_fudge =
blanchet@42738
   226
  {local_const_multiplier = 1.5,
blanchet@41159
   227
   worse_irrel_freq = 100.0,
blanchet@40070
   228
   higher_order_irrel_weight = 1.05,
blanchet@40070
   229
   abs_rel_weight = 0.5,
blanchet@40070
   230
   abs_irrel_weight = 2.0,
blanchet@42746
   231
   skolem_irrel_weight = 0.25,
blanchet@40070
   232
   theory_const_rel_weight = 0.5,
blanchet@40070
   233
   theory_const_irrel_weight = 0.25,
blanchet@42735
   234
   chained_const_irrel_weight = 0.25,
blanchet@40070
   235
   intro_bonus = 0.15,
blanchet@40070
   236
   elim_bonus = 0.15,
blanchet@40070
   237
   simp_bonus = 0.15,
blanchet@40070
   238
   local_bonus = 0.55,
blanchet@40070
   239
   assum_bonus = 1.05,
blanchet@40070
   240
   chained_bonus = 1.5,
blanchet@40070
   241
   max_imperfect = 11.5,
blanchet@40070
   242
   max_imperfect_exp = 1.0,
blanchet@40070
   243
   threshold_divisor = 2.0,
blanchet@41093
   244
   ridiculous_threshold = 0.01}
blanchet@40070
   245
blanchet@40071
   246
(* FUDGE (FIXME) *)
blanchet@40070
   247
val smt_relevance_fudge =
blanchet@42738
   248
  {local_const_multiplier = #local_const_multiplier atp_relevance_fudge,
blanchet@41159
   249
   worse_irrel_freq = #worse_irrel_freq atp_relevance_fudge,
blanchet@40071
   250
   higher_order_irrel_weight = #higher_order_irrel_weight atp_relevance_fudge,
blanchet@40071
   251
   abs_rel_weight = #abs_rel_weight atp_relevance_fudge,
blanchet@40071
   252
   abs_irrel_weight = #abs_irrel_weight atp_relevance_fudge,
blanchet@40071
   253
   skolem_irrel_weight = #skolem_irrel_weight atp_relevance_fudge,
blanchet@40071
   254
   theory_const_rel_weight = #theory_const_rel_weight atp_relevance_fudge,
blanchet@40071
   255
   theory_const_irrel_weight = #theory_const_irrel_weight atp_relevance_fudge,
blanchet@42735
   256
   chained_const_irrel_weight = #chained_const_irrel_weight atp_relevance_fudge,
blanchet@40071
   257
   intro_bonus = #intro_bonus atp_relevance_fudge,
blanchet@40071
   258
   elim_bonus = #elim_bonus atp_relevance_fudge,
blanchet@40071
   259
   simp_bonus = #simp_bonus atp_relevance_fudge,
blanchet@40071
   260
   local_bonus = #local_bonus atp_relevance_fudge,
blanchet@40071
   261
   assum_bonus = #assum_bonus atp_relevance_fudge,
blanchet@40071
   262
   chained_bonus = #chained_bonus atp_relevance_fudge,
blanchet@40071
   263
   max_imperfect = #max_imperfect atp_relevance_fudge,
blanchet@40071
   264
   max_imperfect_exp = #max_imperfect_exp atp_relevance_fudge,
blanchet@40071
   265
   threshold_divisor = #threshold_divisor atp_relevance_fudge,
blanchet@40071
   266
   ridiculous_threshold = #ridiculous_threshold atp_relevance_fudge}
blanchet@40070
   267
blanchet@40941
   268
fun relevance_fudge_for_prover ctxt name =
blanchet@40941
   269
  if is_smt_prover ctxt name then smt_relevance_fudge else atp_relevance_fudge
blanchet@40070
   270
blanchet@41727
   271
fun supported_provers ctxt =
blanchet@40060
   272
  let
wenzelm@42361
   273
    val thy = Proof_Context.theory_of ctxt
blanchet@40060
   274
    val (remote_provers, local_provers) =
blanchet@43050
   275
      metis_prover_names @
blanchet@41727
   276
      sort_strings (supported_atps thy) @
blanchet@41727
   277
      sort_strings (SMT_Solver.available_solvers_of ctxt)
blanchet@40060
   278
      |> List.partition (String.isPrefix remote_prefix)
blanchet@40060
   279
  in
blanchet@41727
   280
    Output.urgent_message ("Supported provers: " ^
blanchet@40205
   281
                           commas (local_provers @ remote_provers) ^ ".")
blanchet@40060
   282
  end
blanchet@35969
   283
blanchet@43005
   284
fun kill_provers () = Async_Manager.kill_threads das_tool "prover"
blanchet@43005
   285
fun running_provers () = Async_Manager.running_threads das_tool "prover"
blanchet@43005
   286
val messages = Async_Manager.thread_messages das_tool "prover"
blanchet@40059
   287
blanchet@40059
   288
(** problems, results, ATPs, etc. **)
blanchet@35969
   289
blanchet@42579
   290
datatype rich_type_system =
blanchet@42579
   291
  Dumb_Type_Sys of type_system |
blanchet@42579
   292
  Smart_Type_Sys of bool
blanchet@42579
   293
blanchet@35969
   294
type params =
blanchet@41208
   295
  {debug: bool,
blanchet@35969
   296
   verbose: bool,
blanchet@36143
   297
   overlord: bool,
blanchet@41208
   298
   blocking: bool,
blanchet@40059
   299
   provers: string list,
blanchet@42579
   300
   type_sys: rich_type_system,
blanchet@42180
   301
   relevance_thresholds: real * real,
blanchet@42180
   302
   max_relevant: int option,
blanchet@42724
   303
   max_mono_iters: int,
blanchet@42740
   304
   max_new_mono_instances: int,
blanchet@43063
   305
   explicit_apply: bool option,
blanchet@35969
   306
   isar_proof: bool,
blanchet@36924
   307
   isar_shrink_factor: int,
blanchet@42443
   308
   slicing: bool,
blanchet@38985
   309
   timeout: Time.time,
blanchet@43015
   310
   preplay_timeout: Time.time,
blanchet@38985
   311
   expect: string}
blanchet@35867
   312
blanchet@41090
   313
datatype prover_fact =
blanchet@41090
   314
  Untranslated_Fact of (string * locality) * thm |
blanchet@41242
   315
  SMT_Weighted_Fact of (string * locality) * (int option * thm)
blanchet@40061
   316
blanchet@40061
   317
type prover_problem =
blanchet@39318
   318
  {state: Proof.state,
blanchet@38998
   319
   goal: thm,
blanchet@38998
   320
   subgoal: int,
blanchet@40065
   321
   subgoal_count: int,
blanchet@41242
   322
   facts: prover_fact list,
blanchet@41741
   323
   smt_filter: (string * locality) SMT_Solver.smt_filter_data option}
blanchet@35867
   324
blanchet@40061
   325
type prover_result =
blanchet@36370
   326
  {outcome: failure option,
blanchet@40204
   327
   used_facts: (string * locality) list,
blanchet@43052
   328
   run_time_in_msecs: int option,
blanchet@43052
   329
   preplay: unit -> play,
blanchet@43052
   330
   message: play -> string}
blanchet@35867
   331
blanchet@43051
   332
type prover =
blanchet@43051
   333
  params -> (string -> minimize_command) -> prover_problem -> prover_result
blanchet@35867
   334
blanchet@43033
   335
blanchet@38023
   336
(* configuration attributes *)
blanchet@38023
   337
wenzelm@42616
   338
val dest_dir =
wenzelm@42616
   339
  Attrib.setup_config_string @{binding sledgehammer_dest_dir} (K "")
blanchet@38991
   340
  (* Empty string means create files in Isabelle's temporary files directory. *)
blanchet@38023
   341
wenzelm@42616
   342
val problem_prefix =
wenzelm@42616
   343
  Attrib.setup_config_string @{binding sledgehammer_problem_prefix} (K "prob")
blanchet@38023
   344
wenzelm@42616
   345
val measure_run_time =
wenzelm@42616
   346
  Attrib.setup_config_bool @{binding sledgehammer_measure_run_time} (K false)
wenzelm@28484
   347
blanchet@42646
   348
val smt_triggers =
blanchet@42646
   349
  Attrib.setup_config_bool @{binding sledgehammer_smt_triggers} (K true)
blanchet@42646
   350
val smt_weights =
blanchet@42646
   351
  Attrib.setup_config_bool @{binding sledgehammer_smt_weights} (K true)
blanchet@42646
   352
val smt_weight_min_facts =
blanchet@42646
   353
  Attrib.setup_config_int @{binding sledgehammer_smt_weight_min_facts} (K 20)
blanchet@41256
   354
blanchet@41256
   355
(* FUDGE *)
blanchet@42646
   356
val smt_min_weight =
blanchet@42646
   357
  Attrib.setup_config_int @{binding sledgehammer_smt_min_weight} (K 0)
blanchet@42646
   358
val smt_max_weight =
blanchet@42646
   359
  Attrib.setup_config_int @{binding sledgehammer_smt_max_weight} (K 10)
blanchet@42646
   360
val smt_max_weight_index =
blanchet@42646
   361
  Attrib.setup_config_int @{binding sledgehammer_smt_max_weight_index} (K 200)
blanchet@41256
   362
val smt_weight_curve = Unsynchronized.ref (fn x : int => x * x)
blanchet@41256
   363
blanchet@42646
   364
fun smt_fact_weight ctxt j num_facts =
blanchet@42646
   365
  if Config.get ctxt smt_weights andalso
blanchet@42646
   366
     num_facts >= Config.get ctxt smt_weight_min_facts then
blanchet@42646
   367
    let
blanchet@42646
   368
      val min = Config.get ctxt smt_min_weight
blanchet@42646
   369
      val max = Config.get ctxt smt_max_weight
blanchet@42646
   370
      val max_index = Config.get ctxt smt_max_weight_index
blanchet@42646
   371
      val curve = !smt_weight_curve
blanchet@42646
   372
    in
blanchet@42646
   373
      SOME (max - (max - min + 1) * curve (Int.max (0, max_index - j - 1))
blanchet@42646
   374
            div curve max_index)
blanchet@42646
   375
    end
blanchet@41256
   376
  else
blanchet@41256
   377
    NONE
blanchet@41256
   378
blanchet@42646
   379
fun weight_smt_fact ctxt num_facts ((info, th), j) =
blanchet@42646
   380
  let val thy = Proof_Context.theory_of ctxt in
blanchet@42646
   381
    (info, (smt_fact_weight ctxt j num_facts, th |> Thm.transfer thy))
blanchet@42646
   382
  end
blanchet@38023
   383
blanchet@41091
   384
fun untranslated_fact (Untranslated_Fact p) = p
blanchet@41242
   385
  | untranslated_fact (SMT_Weighted_Fact (info, (_, th))) = (info, th)
blanchet@42994
   386
fun atp_translated_fact ctxt format type_sys fact =
blanchet@42994
   387
  translate_atp_fact ctxt format type_sys false (untranslated_fact fact)
blanchet@41256
   388
fun smt_weighted_fact _ _ (SMT_Weighted_Fact p, _) = p
blanchet@42646
   389
  | smt_weighted_fact ctxt num_facts (fact, j) =
blanchet@42646
   390
    (untranslated_fact fact, j) |> weight_smt_fact ctxt num_facts
blanchet@41256
   391
blanchet@41313
   392
fun overlord_file_location_for_prover prover =
blanchet@41313
   393
  (getenv "ISABELLE_HOME_USER", "prob_" ^ prover)
blanchet@41313
   394
blanchet@43033
   395
fun with_path cleanup after f path =
blanchet@43033
   396
  Exn.capture f path
blanchet@43033
   397
  |> tap (fn _ => cleanup path)
blanchet@43033
   398
  |> Exn.release
blanchet@43033
   399
  |> tap (after path)
blanchet@43033
   400
blanchet@43052
   401
fun proof_banner mode name =
blanchet@43033
   402
  case mode of
blanchet@43033
   403
    Auto_Try => "Auto Sledgehammer (" ^ quote name ^ ") found a proof"
blanchet@43033
   404
  | Try => "Sledgehammer (" ^ quote name ^ ") found a proof"
blanchet@43052
   405
  | _ => "Try this"
blanchet@43033
   406
blanchet@43033
   407
(* based on "Mirabelle.can_apply" and generalized *)
blanchet@43034
   408
fun timed_apply timeout tac state i =
blanchet@43033
   409
  let
blanchet@43033
   410
    val {context = ctxt, facts, goal} = Proof.goal state
blanchet@43033
   411
    val full_tac = Method.insert_tac facts i THEN tac ctxt i
blanchet@43034
   412
  in TimeLimit.timeLimit timeout (try (Seq.pull o full_tac)) goal end
blanchet@43033
   413
blanchet@43034
   414
fun tac_for_reconstructor Metis = Metis_Tactics.metisHO_tac
blanchet@43034
   415
  | tac_for_reconstructor MetisFT = Metis_Tactics.metisFT_tac
blanchet@43034
   416
  | tac_for_reconstructor _ = raise Fail "unexpected reconstructor"
blanchet@43034
   417
blanchet@43034
   418
fun timed_reconstructor reconstructor debug timeout ths =
blanchet@43034
   419
  (Config.put Metis_Tactics.verbose debug
blanchet@43034
   420
   #> (fn ctxt => tac_for_reconstructor reconstructor ctxt ths))
blanchet@43034
   421
  |> timed_apply timeout
blanchet@43033
   422
blanchet@43033
   423
fun filter_used_facts used = filter (member (op =) used o fst)
blanchet@43033
   424
blanchet@43050
   425
fun play_one_line_proof debug timeout ths state i reconstructors =
blanchet@43034
   426
  let
blanchet@43050
   427
    fun play [] [] = Failed_to_Play
blanchet@43050
   428
      | play (timed_out :: _) [] = Trust_Playable (timed_out, SOME timeout)
blanchet@43050
   429
      | play timed_out (reconstructor :: reconstructors) =
blanchet@43044
   430
        let val timer = Timer.startRealTimer () in
blanchet@43044
   431
          case timed_reconstructor reconstructor debug timeout ths state i of
blanchet@43050
   432
            SOME (SOME _) => Played (reconstructor, Timer.checkRealTimer timer)
blanchet@43050
   433
          | _ => play timed_out reconstructors
blanchet@43044
   434
        end
blanchet@43044
   435
        handle TimeLimit.TimeOut =>
blanchet@43050
   436
               play (reconstructor :: timed_out) reconstructors
blanchet@43034
   437
  in
blanchet@43044
   438
    if timeout = Time.zeroTime then Trust_Playable (hd reconstructors, NONE)
blanchet@43050
   439
    else play [] reconstructors
blanchet@43033
   440
  end
blanchet@43033
   441
blanchet@41313
   442
blanchet@41256
   443
(* generic TPTP-based ATPs *)
blanchet@40061
   444
blanchet@42730
   445
(* Too general means, positive equality literal with a variable X as one
blanchet@42730
   446
   operand, when X does not occur properly in the other operand. This rules out
blanchet@42730
   447
   clearly inconsistent facts such as X = a | X = b, though it by no means
blanchet@42730
   448
   guarantees soundness. *)
blanchet@42730
   449
blanchet@42730
   450
(* Unwanted equalities are those between a (bound or schematic) variable that
blanchet@42730
   451
   does not properly occur in the second operand. *)
blanchet@42730
   452
val is_exhaustive_finite =
blanchet@42730
   453
  let
blanchet@42730
   454
    fun is_bad_equal (Var z) t =
blanchet@42730
   455
        not (exists_subterm (fn Var z' => z = z' | _ => false) t)
blanchet@42730
   456
      | is_bad_equal (Bound j) t = not (loose_bvar1 (t, j))
blanchet@42730
   457
      | is_bad_equal _ _ = false
blanchet@42730
   458
    fun do_equals t1 t2 = is_bad_equal t1 t2 orelse is_bad_equal t2 t1
blanchet@42730
   459
    fun do_formula pos t =
blanchet@42730
   460
      case (pos, t) of
blanchet@42730
   461
        (_, @{const Trueprop} $ t1) => do_formula pos t1
blanchet@42730
   462
      | (true, Const (@{const_name all}, _) $ Abs (_, _, t')) =>
blanchet@42730
   463
        do_formula pos t'
blanchet@42730
   464
      | (true, Const (@{const_name All}, _) $ Abs (_, _, t')) =>
blanchet@42730
   465
        do_formula pos t'
blanchet@42730
   466
      | (false, Const (@{const_name Ex}, _) $ Abs (_, _, t')) =>
blanchet@42730
   467
        do_formula pos t'
blanchet@42730
   468
      | (_, @{const "==>"} $ t1 $ t2) =>
blanchet@42730
   469
        do_formula (not pos) t1 andalso
blanchet@42730
   470
        (t2 = @{prop False} orelse do_formula pos t2)
blanchet@42730
   471
      | (_, @{const HOL.implies} $ t1 $ t2) =>
blanchet@42730
   472
        do_formula (not pos) t1 andalso
blanchet@42730
   473
        (t2 = @{const False} orelse do_formula pos t2)
blanchet@42730
   474
      | (_, @{const Not} $ t1) => do_formula (not pos) t1
blanchet@42730
   475
      | (true, @{const HOL.disj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
blanchet@42730
   476
      | (false, @{const HOL.conj} $ t1 $ t2) => forall (do_formula pos) [t1, t2]
blanchet@42730
   477
      | (true, Const (@{const_name HOL.eq}, _) $ t1 $ t2) => do_equals t1 t2
blanchet@42730
   478
      | (true, Const (@{const_name "=="}, _) $ t1 $ t2) => do_equals t1 t2
blanchet@42730
   479
      | _ => false
blanchet@42730
   480
  in do_formula true end
blanchet@42730
   481
blanchet@42730
   482
fun has_bound_or_var_of_type pred =
blanchet@42730
   483
  exists_subterm (fn Var (_, T as Type _) => pred T
blanchet@42730
   484
                   | Abs (_, T as Type _, _) => pred T
blanchet@42730
   485
                   | _ => false)
blanchet@42730
   486
blanchet@42730
   487
(* Facts are forbidden to contain variables of these types. The typical reason
blanchet@42730
   488
   is that they lead to unsoundness. Note that "unit" satisfies numerous
blanchet@42730
   489
   equations like "?x = ()". The resulting clauses will have no type constraint,
blanchet@42730
   490
   yielding false proofs. Even "bool" leads to many unsound proofs, though only
blanchet@42730
   491
   for higher-order problems. *)
blanchet@42730
   492
blanchet@42730
   493
(* Facts containing variables of type "unit" or "bool" or of the form
blanchet@42730
   494
   "ALL x. x = A | x = B | x = C" are likely to lead to unsound proofs if types
blanchet@42730
   495
   are omitted. *)
blanchet@42944
   496
fun is_dangerous_prop ctxt =
blanchet@42944
   497
  transform_elim_prop
blanchet@42730
   498
  #> (has_bound_or_var_of_type (is_type_surely_finite ctxt) orf
blanchet@42730
   499
      is_exhaustive_finite)
blanchet@42730
   500
blanchet@40409
   501
fun int_opt_add (SOME m) (SOME n) = SOME (m + n)
blanchet@40409
   502
  | int_opt_add _ _ = NONE
blanchet@40062
   503
blanchet@42452
   504
val atp_blacklist_max_iters = 10
blanchet@39492
   505
(* Important messages are important but not so important that users want to see
blanchet@39492
   506
   them each time. *)
blanchet@42609
   507
val atp_important_message_keep_quotient = 10
blanchet@39492
   508
blanchet@42589
   509
val fallback_best_type_systems =
blanchet@42853
   510
  [Preds (Mangled_Monomorphic, Nonmonotonic_Types, Light)]
blanchet@42579
   511
blanchet@42722
   512
fun adjust_dumb_type_sys formats (Simple_Types level) =
blanchet@42963
   513
    if member (op =) formats THF then
blanchet@42963
   514
      (THF, Simple_Types level)
blanchet@42963
   515
    else if member (op =) formats TFF then
blanchet@42963
   516
      (TFF, Simple_Types level)
blanchet@42963
   517
    else
blanchet@42963
   518
      adjust_dumb_type_sys formats (Preds (Mangled_Monomorphic, level, Heavy))
blanchet@42849
   519
  | adjust_dumb_type_sys formats type_sys =
blanchet@42998
   520
    (case hd formats of
blanchet@42998
   521
       CNF_UEQ =>
blanchet@42998
   522
       (CNF_UEQ, case type_sys of
blanchet@42998
   523
                   Preds stuff =>
blanchet@42998
   524
                   (if is_type_sys_fairly_sound type_sys then Preds else Tags)
blanchet@42998
   525
                       stuff
blanchet@42998
   526
                 | _ => type_sys)
blanchet@42998
   527
     | format => (format, type_sys))
blanchet@42994
   528
blanchet@43051
   529
fun choose_format_and_type_sys _ formats (Dumb_Type_Sys type_sys) =
blanchet@42579
   530
    adjust_dumb_type_sys formats type_sys
blanchet@43051
   531
  | choose_format_and_type_sys best_type_systems formats
blanchet@43051
   532
                               (Smart_Type_Sys full_types) =
blanchet@42613
   533
    map type_sys_from_string best_type_systems @ fallback_best_type_systems
blanchet@42589
   534
    |> find_first (if full_types then is_type_sys_virtually_sound else K true)
blanchet@42589
   535
    |> the |> adjust_dumb_type_sys formats
blanchet@42548
   536
blanchet@43051
   537
val metis_minimize_max_time = seconds 2.0
blanchet@43051
   538
blanchet@43051
   539
fun choose_minimize_command minimize_command name preplay =
blanchet@43051
   540
  (case preplay of
blanchet@43051
   541
     Played (reconstructor, time) =>
blanchet@43051
   542
     if Time.<= (time, metis_minimize_max_time) then
blanchet@43051
   543
       reconstructor_name reconstructor
blanchet@43051
   544
     else
blanchet@43051
   545
       name
blanchet@43051
   546
   | _ => name)
blanchet@43051
   547
  |> minimize_command
blanchet@43051
   548
blanchet@42740
   549
fun repair_smt_monomorph_context debug max_mono_iters max_mono_instances =
blanchet@42724
   550
  Config.put SMT_Config.verbose debug
blanchet@42724
   551
  #> Config.put SMT_Config.monomorph_full false
blanchet@42724
   552
  #> Config.put SMT_Config.monomorph_limit max_mono_iters
blanchet@42740
   553
  #> Config.put SMT_Config.monomorph_instances max_mono_instances
blanchet@42724
   554
blanchet@43021
   555
fun run_atp mode name
blanchet@42578
   556
        ({exec, required_execs, arguments, proof_delims, known_failures,
blanchet@42723
   557
          conj_sym_kind, prem_kind, formats, best_slices, ...} : atp_config)
blanchet@43052
   558
        ({debug, verbose, overlord, type_sys, max_relevant, max_mono_iters,
blanchet@43052
   559
          max_new_mono_instances, explicit_apply, isar_proof,
blanchet@43033
   560
          isar_shrink_factor, slicing, timeout, preplay_timeout, ...} : params)
blanchet@43037
   561
        minimize_command
blanchet@43037
   562
        ({state, goal, subgoal, subgoal_count, facts, ...} : prover_problem) =
blanchet@38023
   563
  let
blanchet@42182
   564
    val thy = Proof.theory_of state
blanchet@39318
   565
    val ctxt = Proof.context_of state
blanchet@43004
   566
    val (_, hyp_ts, concl_t) = strip_subgoal ctxt goal subgoal
blanchet@41159
   567
    val (dest_dir, problem_prefix) =
blanchet@41159
   568
      if overlord then overlord_file_location_for_prover name
blanchet@41159
   569
      else (Config.get ctxt dest_dir, Config.get ctxt problem_prefix)
blanchet@40061
   570
    val problem_file_name =
blanchet@41159
   571
      Path.basic (problem_prefix ^ (if overlord then "" else serial_string ()) ^
blanchet@41159
   572
                  "_" ^ string_of_int subgoal)
blanchet@40061
   573
    val problem_path_name =
blanchet@40059
   574
      if dest_dir = "" then
blanchet@40061
   575
        File.tmp_path problem_file_name
blanchet@40059
   576
      else if File.exists (Path.explode dest_dir) then
blanchet@40061
   577
        Path.append (Path.explode dest_dir) problem_file_name
blanchet@39003
   578
      else
blanchet@40059
   579
        error ("No such directory: " ^ quote dest_dir ^ ".")
blanchet@39003
   580
    val measure_run_time = verbose orelse Config.get ctxt measure_run_time
blanchet@38092
   581
    val command = Path.explode (getenv (fst exec) ^ "/" ^ snd exec)
blanchet@38023
   582
    fun split_time s =
blanchet@38023
   583
      let
blanchet@42448
   584
        val split = String.tokens (fn c => str c = "\n")
blanchet@42448
   585
        val (output, t) = s |> split |> split_last |> apfst cat_lines
blanchet@42448
   586
        fun as_num f = f >> (fst o read_int)
blanchet@42448
   587
        val num = as_num (Scan.many1 Symbol.is_ascii_digit)
blanchet@42448
   588
        val digit = Scan.one Symbol.is_ascii_digit
blanchet@42448
   589
        val num3 = as_num (digit ::: digit ::: (digit >> single))
blanchet@42448
   590
        val time = num --| Scan.$$ "." -- num3 >> (fn (a, b) => a * 1000 + b)
wenzelm@40627
   591
        val as_time = Scan.read Symbol.stopper time o raw_explode
blanchet@42448
   592
      in (output, as_time t) end
blanchet@41313
   593
    fun run_on prob_file =
blanchet@38092
   594
      case filter (curry (op =) "" o getenv o fst) (exec :: required_execs) of
blanchet@38032
   595
        (home_var, _) :: _ =>
blanchet@38023
   596
        error ("The environment variable " ^ quote home_var ^ " is not set.")
blanchet@38032
   597
      | [] =>
blanchet@38032
   598
        if File.exists command then
blanchet@38032
   599
          let
blanchet@42443
   600
            (* If slicing is disabled, we expand the last slice to fill the
blanchet@42443
   601
               entire time available. *)
blanchet@43063
   602
            val actual_slices =
blanchet@43063
   603
              get_slices (length facts) slicing (best_slices ctxt)
blanchet@42443
   604
            val num_actual_slices = length actual_slices
blanchet@42445
   605
            fun monomorphize_facts facts =
blanchet@42445
   606
              let
blanchet@42445
   607
                val facts = facts |> map untranslated_fact
blanchet@42445
   608
                (* pseudo-theorem involving the same constants as the subgoal *)
blanchet@42445
   609
                val subgoal_th =
blanchet@42445
   610
                  Logic.list_implies (hyp_ts, concl_t)
blanchet@42445
   611
                  |> Skip_Proof.make_thm thy
blanchet@42445
   612
                val indexed_facts =
blanchet@42445
   613
                  (~1, subgoal_th) :: (0 upto length facts - 1 ~~ map snd facts)
blanchet@42740
   614
                val max_mono_instances = max_new_mono_instances + length facts
blanchet@42445
   615
              in
blanchet@42724
   616
                ctxt |> repair_smt_monomorph_context debug max_mono_iters
blanchet@42740
   617
                                                     max_mono_instances
blanchet@42724
   618
                     |> SMT_Monomorph.monomorph indexed_facts
blanchet@42724
   619
                     |> fst |> sort (int_ord o pairself fst)
blanchet@42724
   620
                     |> filter_out (curry (op =) ~1 o fst)
blanchet@42724
   621
                     |> map (Untranslated_Fact o apfst (fst o nth facts))
blanchet@42445
   622
              end
blanchet@42723
   623
            fun run_slice blacklist (slice, (time_frac, (complete,
blanchet@42723
   624
                                       (best_max_relevant, best_type_systems))))
blanchet@42443
   625
                          time_left =
blanchet@38032
   626
              let
blanchet@42443
   627
                val num_facts =
blanchet@42443
   628
                  length facts |> is_none max_relevant
blanchet@42723
   629
                                  ? Integer.min best_max_relevant
blanchet@42723
   630
                val (format, type_sys) =
blanchet@43051
   631
                  choose_format_and_type_sys best_type_systems formats type_sys
blanchet@42638
   632
                val fairly_sound = is_type_sys_fairly_sound type_sys
blanchet@42451
   633
                val facts =
blanchet@42671
   634
                  facts |> not fairly_sound
blanchet@42944
   635
                           ? filter_out (is_dangerous_prop ctxt o prop_of o snd
blanchet@42638
   636
                                         o untranslated_fact)
blanchet@42638
   637
                        |> take num_facts
blanchet@42452
   638
                        |> not (null blacklist)
blanchet@42452
   639
                           ? filter_out (member (op =) blacklist o fst
blanchet@42452
   640
                                         o untranslated_fact)
blanchet@42589
   641
                        |> polymorphism_of_type_sys type_sys <> Polymorphic
blanchet@42589
   642
                           ? monomorphize_facts
blanchet@42994
   643
                        |> map (atp_translated_fact ctxt format type_sys)
blanchet@42443
   644
                val real_ms = Real.fromInt o Time.toMilliseconds
blanchet@42443
   645
                val slice_timeout =
blanchet@42443
   646
                  ((real_ms time_left
blanchet@42443
   647
                    |> (if slice < num_actual_slices - 1 then
blanchet@42443
   648
                          curry Real.min (time_frac * real_ms timeout)
blanchet@42443
   649
                        else
blanchet@42443
   650
                          I))
blanchet@42443
   651
                   * 0.001) |> seconds
blanchet@42443
   652
                val _ =
blanchet@42614
   653
                  if debug then
blanchet@42699
   654
                    quote name ^ " slice #" ^ string_of_int (slice + 1) ^
blanchet@42699
   655
                    " with " ^ string_of_int num_facts ^ " fact" ^
blanchet@42699
   656
                    plural_s num_facts ^ " for " ^
blanchet@42699
   657
                    string_from_time slice_timeout ^ "..."
blanchet@42443
   658
                    |> Output.urgent_message
blanchet@42443
   659
                  else
blanchet@42443
   660
                    ()
blanchet@42541
   661
                val (atp_problem, pool, conjecture_offset, facts_offset,
blanchet@42881
   662
                     fact_names, typed_helpers, sym_tab) =
blanchet@42939
   663
                  prepare_atp_problem ctxt format conj_sym_kind prem_kind
blanchet@42939
   664
                      type_sys explicit_apply hyp_ts concl_t facts
blanchet@41313
   665
                fun weights () = atp_problem_weights atp_problem
blanchet@41313
   666
                val core =
blanchet@41313
   667
                  File.shell_path command ^ " " ^
blanchet@42646
   668
                  arguments ctxt slice slice_timeout weights ^ " " ^
blanchet@41313
   669
                  File.shell_path prob_file
blanchet@41313
   670
                val command =
blanchet@41313
   671
                  (if measure_run_time then
blanchet@41313
   672
                     "TIMEFORMAT='%3R'; { time " ^ core ^ " ; }"
blanchet@41313
   673
                   else
blanchet@41313
   674
                     "exec " ^ core) ^ " 2>&1"
blanchet@42443
   675
                val _ =
blanchet@42443
   676
                  atp_problem
blanchet@42709
   677
                  |> tptp_strings_for_atp_problem format
blanchet@42443
   678
                  |> cons ("% " ^ command ^ "\n")
blanchet@42443
   679
                  |> File.write_list prob_file
blanchet@42443
   680
                val conjecture_shape =
blanchet@42443
   681
                  conjecture_offset + 1
blanchet@42443
   682
                    upto conjecture_offset + length hyp_ts + 1
blanchet@42443
   683
                  |> map single
blanchet@38032
   684
                val ((output, msecs), res_code) =
blanchet@38032
   685
                  bash_output command
blanchet@38032
   686
                  |>> (if overlord then
blanchet@38032
   687
                         prefix ("% " ^ command ^ "\n% " ^ timestamp () ^ "\n")
blanchet@38032
   688
                       else
blanchet@38032
   689
                         I)
blanchet@40062
   690
                  |>> (if measure_run_time then split_time else rpair NONE)
blanchet@42449
   691
                val (atp_proof, outcome) =
blanchet@42849
   692
                  extract_tstplike_proof_and_outcome verbose complete res_code
blanchet@42849
   693
                      proof_delims known_failures output
blanchet@42943
   694
                  |>> atp_proof_from_tstplike_proof atp_problem
blanchet@42965
   695
                  handle UNRECOGNIZED_ATP_PROOF () => ([], SOME ProofIncomplete)
blanchet@42881
   696
                val (conjecture_shape, facts_offset, fact_names,
blanchet@42881
   697
                     typed_helpers) =
blanchet@42449
   698
                  if is_none outcome then
blanchet@42647
   699
                    repair_conjecture_shape_and_fact_names type_sys output
blanchet@42881
   700
                        conjecture_shape facts_offset fact_names typed_helpers
blanchet@42449
   701
                  else
blanchet@42587
   702
                    (* don't bother repairing *)
blanchet@42881
   703
                    (conjecture_shape, facts_offset, fact_names, typed_helpers)
blanchet@42449
   704
                val outcome =
blanchet@42451
   705
                  case outcome of
blanchet@42587
   706
                    NONE =>
blanchet@42968
   707
                    used_facts_in_unsound_atp_proof ctxt type_sys
blanchet@42876
   708
                        conjecture_shape facts_offset fact_names atp_proof
blanchet@42876
   709
                    |> Option.map (fn facts =>
blanchet@42876
   710
                           UnsoundProof (is_type_sys_virtually_sound type_sys,
blanchet@42943
   711
                                         facts |> sort string_ord))
blanchet@42451
   712
                  | SOME Unprovable =>
blanchet@43050
   713
                    if null blacklist then outcome else SOME GaveUp
blanchet@42451
   714
                  | _ => outcome
blanchet@42443
   715
              in
blanchet@42881
   716
                ((pool, conjecture_shape, facts_offset, fact_names,
blanchet@42881
   717
                  typed_helpers, sym_tab),
blanchet@42723
   718
                 (output, msecs, type_sys, atp_proof, outcome))
blanchet@42443
   719
              end
blanchet@38645
   720
            val timer = Timer.startRealTimer ()
blanchet@42452
   721
            fun maybe_run_slice blacklist slice
blanchet@42723
   722
                                (result as (_, (_, msecs0, _, _, SOME _))) =
blanchet@42452
   723
                let
blanchet@42452
   724
                  val time_left = Time.- (timeout, Timer.checkRealTimer timer)
blanchet@42452
   725
                in
blanchet@42452
   726
                  if Time.<= (time_left, Time.zeroTime) then
blanchet@42452
   727
                    result
blanchet@42452
   728
                  else
blanchet@42452
   729
                    (run_slice blacklist slice time_left
blanchet@42723
   730
                     |> (fn (stuff, (output, msecs, type_sys, atp_proof,
blanchet@42723
   731
                                     outcome)) =>
blanchet@42723
   732
                            (stuff, (output, int_opt_add msecs0 msecs, type_sys,
blanchet@42723
   733
                                     atp_proof, outcome))))
blanchet@42452
   734
                end
blanchet@42450
   735
              | maybe_run_slice _ _ result = result
blanchet@42452
   736
            fun maybe_blacklist_facts_and_retry iter blacklist
blanchet@42881
   737
                    (result as ((_, _, facts_offset, fact_names, _, _),
blanchet@42723
   738
                                (_, _, type_sys, atp_proof,
blanchet@42876
   739
                                 SOME (UnsoundProof (false, _))))) =
blanchet@42968
   740
                (case used_facts_in_atp_proof ctxt type_sys facts_offset
blanchet@42968
   741
                                              fact_names atp_proof of
blanchet@42452
   742
                   [] => result
blanchet@42452
   743
                 | new_baddies =>
blanchet@42835
   744
                   if slicing andalso iter < atp_blacklist_max_iters - 1 then
blanchet@42777
   745
                     let val blacklist = new_baddies @ blacklist in
blanchet@42777
   746
                       result
blanchet@42777
   747
                       |> maybe_run_slice blacklist (List.last actual_slices)
blanchet@42777
   748
                       |> maybe_blacklist_facts_and_retry (iter + 1) blacklist
blanchet@42777
   749
                     end
blanchet@42777
   750
                   else
blanchet@42777
   751
                     result)
blanchet@42452
   752
              | maybe_blacklist_facts_and_retry _ _ result = result
blanchet@42443
   753
          in
blanchet@42881
   754
            ((Symtab.empty, [], 0, Vector.fromList [], [], Symtab.empty),
blanchet@42723
   755
             ("", SOME 0, hd fallback_best_type_systems, [],
blanchet@42723
   756
              SOME InternalError))
blanchet@42451
   757
            |> fold (maybe_run_slice []) actual_slices
blanchet@42451
   758
               (* The ATP found an unsound proof? Automatically try again
blanchet@42451
   759
                  without the offending facts! *)
blanchet@42452
   760
            |> maybe_blacklist_facts_and_retry 0 []
blanchet@42443
   761
          end
blanchet@38032
   762
        else
wenzelm@41944
   763
          error ("Bad executable: " ^ Path.print command ^ ".")
blanchet@38023
   764
blanchet@38023
   765
    (* If the problem file has not been exported, remove it; otherwise, export
blanchet@38023
   766
       the proof file too. *)
blanchet@41313
   767
    fun cleanup prob_file =
blanchet@41313
   768
      if dest_dir = "" then try File.rm prob_file else NONE
blanchet@42723
   769
    fun export prob_file (_, (output, _, _, _, _)) =
blanchet@40059
   770
      if dest_dir = "" then
blanchet@38023
   771
        ()
blanchet@38023
   772
      else
blanchet@41313
   773
        File.write (Path.explode (Path.implode prob_file ^ "_proof")) output
blanchet@42881
   774
    val ((pool, conjecture_shape, facts_offset, fact_names, typed_helpers,
blanchet@42881
   775
          sym_tab),
blanchet@42723
   776
         (output, msecs, type_sys, atp_proof, outcome)) =
blanchet@40061
   777
      with_path cleanup export run_on problem_path_name
blanchet@39492
   778
    val important_message =
blanchet@43021
   779
      if mode = Normal andalso
blanchet@42609
   780
         random_range 0 (atp_important_message_keep_quotient - 1) = 0 then
blanchet@39492
   781
        extract_important_message output
blanchet@39492
   782
      else
blanchet@39492
   783
        ""
blanchet@43052
   784
    val (used_facts, preplay, message) =
blanchet@38023
   785
      case outcome of
blanchet@38023
   786
        NONE =>
blanchet@43033
   787
        let
blanchet@43033
   788
          val used_facts =
blanchet@43033
   789
            used_facts_in_atp_proof ctxt type_sys facts_offset fact_names
blanchet@43033
   790
                                    atp_proof
blanchet@43033
   791
        in
blanchet@43052
   792
          (used_facts,
blanchet@43052
   793
           fn () =>
blanchet@43052
   794
              let
blanchet@43052
   795
                val used_ths =
blanchet@43052
   796
                  facts |> map untranslated_fact |> filter_used_facts used_facts
blanchet@43052
   797
                        |> map snd
blanchet@43052
   798
              in
blanchet@43052
   799
                play_one_line_proof debug preplay_timeout used_ths state subgoal
blanchet@43063
   800
                                    [Metis, MetisFT]
blanchet@43052
   801
              end,
blanchet@43052
   802
           fn preplay =>
blanchet@43052
   803
              let
blanchet@43052
   804
                val full_types = uses_typed_helpers typed_helpers atp_proof
blanchet@43052
   805
                val isar_params =
blanchet@43052
   806
                  (debug, full_types, isar_shrink_factor, type_sys, pool,
blanchet@43052
   807
                   conjecture_shape, facts_offset, fact_names, sym_tab, atp_proof,
blanchet@43052
   808
                   goal)
blanchet@43052
   809
                val one_line_params =
blanchet@43052
   810
                  (preplay, proof_banner mode name, used_facts,
blanchet@43052
   811
                   choose_minimize_command minimize_command name preplay,
blanchet@43052
   812
                   subgoal, subgoal_count)
blanchet@43052
   813
              in
blanchet@43052
   814
                proof_text ctxt isar_proof isar_params one_line_params ^
blanchet@43052
   815
                (if verbose then
blanchet@43052
   816
                   "\nATP real CPU time: " ^
blanchet@43052
   817
                   string_from_time (Time.fromMilliseconds (the msecs)) ^ "."
blanchet@43052
   818
                 else
blanchet@43052
   819
                   "") ^
blanchet@43052
   820
                (if important_message <> "" then
blanchet@43052
   821
                   "\n\nImportant message from Dr. Geoff Sutcliffe:\n" ^
blanchet@43052
   822
                   important_message
blanchet@43052
   823
                 else
blanchet@43052
   824
                   "")
blanchet@43052
   825
              end)
blanchet@43033
   826
        end
blanchet@43052
   827
      | SOME failure =>
blanchet@43052
   828
        ([], K Failed_to_Play, fn _ => string_for_failure failure)
blanchet@38023
   829
  in
blanchet@43052
   830
    {outcome = outcome, used_facts = used_facts, run_time_in_msecs = msecs,
blanchet@43052
   831
     preplay = preplay, message = message}
blanchet@38023
   832
  end
blanchet@38023
   833
blanchet@40669
   834
(* "SMT_Failure.Abnormal_Termination" carries the solver's return code. Until
blanchet@40669
   835
   these are sorted out properly in the SMT module, we have to interpret these
blanchet@40669
   836
   ourselves. *)
blanchet@40684
   837
val remote_smt_failures =
blanchet@40684
   838
  [(22, CantConnect),
blanchet@40684
   839
   (2, NoLibwwwPerl)]
blanchet@41222
   840
val z3_wrapper_failures =
blanchet@43085
   841
  [(11, InternalError),
blanchet@41222
   842
   (12, InternalError),
blanchet@41222
   843
   (13, InternalError)]
blanchet@40684
   844
val z3_failures =
blanchet@41236
   845
  [(101, OutOfResources),
blanchet@41236
   846
   (103, MalformedInput),
blanchet@41222
   847
   (110, MalformedInput)]
blanchet@40684
   848
val unix_failures =
blanchet@40684
   849
  [(139, Crashed)]
blanchet@41222
   850
val smt_failures =
blanchet@41799
   851
  remote_smt_failures @ z3_wrapper_failures @ z3_failures @ unix_failures
blanchet@40555
   852
blanchet@42100
   853
fun failure_from_smt_failure (SMT_Failure.Counterexample {is_real_cex, ...}) =
blanchet@43050
   854
    if is_real_cex then Unprovable else GaveUp
blanchet@41222
   855
  | failure_from_smt_failure SMT_Failure.Time_Out = TimedOut
blanchet@41222
   856
  | failure_from_smt_failure (SMT_Failure.Abnormal_Termination code) =
blanchet@41222
   857
    (case AList.lookup (op =) smt_failures code of
blanchet@40684
   858
       SOME failure => failure
blanchet@41259
   859
     | NONE => UnknownError ("Abnormal termination with exit code " ^
blanchet@41259
   860
                             string_of_int code ^ "."))
blanchet@41222
   861
  | failure_from_smt_failure SMT_Failure.Out_Of_Memory = OutOfResources
blanchet@41222
   862
  | failure_from_smt_failure (SMT_Failure.Other_Failure msg) =
blanchet@42061
   863
    UnknownError msg
blanchet@40063
   864
blanchet@40698
   865
(* FUDGE *)
blanchet@42646
   866
val smt_max_slices =
blanchet@42646
   867
  Attrib.setup_config_int @{binding sledgehammer_smt_max_slices} (K 8)
blanchet@42646
   868
val smt_slice_fact_frac =
blanchet@42646
   869
  Attrib.setup_config_real @{binding sledgehammer_smt_slice_fact_frac} (K 0.5)
blanchet@42646
   870
val smt_slice_time_frac =
blanchet@42646
   871
  Attrib.setup_config_real @{binding sledgehammer_smt_slice_time_frac} (K 0.5)
blanchet@42646
   872
val smt_slice_min_secs =
blanchet@42646
   873
  Attrib.setup_config_int @{binding sledgehammer_smt_slice_min_secs} (K 5)
blanchet@40409
   874
blanchet@42646
   875
fun smt_filter_loop ctxt name
blanchet@42724
   876
                    ({debug, verbose, overlord, max_mono_iters,
blanchet@42740
   877
                      max_new_mono_instances, timeout, slicing, ...} : params)
blanchet@41741
   878
                    state i smt_filter =
blanchet@40409
   879
  let
blanchet@42646
   880
    val max_slices = if slicing then Config.get ctxt smt_max_slices else 1
blanchet@41242
   881
    val repair_context =
blanchet@42724
   882
          select_smt_solver name
blanchet@42724
   883
          #> (if overlord then
blanchet@42724
   884
                Config.put SMT_Config.debug_files
blanchet@42724
   885
                           (overlord_file_location_for_prover name
blanchet@42724
   886
                            |> (fn (path, name) => path ^ "/" ^ name))
blanchet@42724
   887
              else
blanchet@42724
   888
                I)
blanchet@42724
   889
          #> Config.put SMT_Config.infer_triggers (Config.get ctxt smt_triggers)
blanchet@41242
   890
    val state = state |> Proof.map_context repair_context
blanchet@42443
   891
    fun do_slice timeout slice outcome0 time_so_far facts =
blanchet@40553
   892
      let
blanchet@40553
   893
        val timer = Timer.startRealTimer ()
blanchet@42724
   894
        val state =
blanchet@42724
   895
          state |> Proof.map_context
blanchet@42724
   896
                       (repair_smt_monomorph_context debug max_mono_iters
blanchet@42740
   897
                            (max_new_mono_instances + length facts))
blanchet@40553
   898
        val ms = timeout |> Time.toMilliseconds
blanchet@42443
   899
        val slice_timeout =
blanchet@42443
   900
          if slice < max_slices then
blanchet@41169
   901
            Int.min (ms,
blanchet@42646
   902
                Int.max (1000 * Config.get ctxt smt_slice_min_secs,
blanchet@42646
   903
                    Real.ceil (Config.get ctxt smt_slice_time_frac
blanchet@42646
   904
                               * Real.fromInt ms)))
blanchet@40553
   905
            |> Time.fromMilliseconds
blanchet@40553
   906
          else
blanchet@40553
   907
            timeout
blanchet@40553
   908
        val num_facts = length facts
blanchet@40553
   909
        val _ =
blanchet@42614
   910
          if debug then
blanchet@42614
   911
            quote name ^ " slice " ^ string_of_int slice ^ " with " ^
blanchet@42614
   912
            string_of_int num_facts ^ " fact" ^ plural_s num_facts ^ " for " ^
blanchet@42614
   913
            string_from_time slice_timeout ^ "..."
blanchet@40553
   914
            |> Output.urgent_message
blanchet@40553
   915
          else
blanchet@40553
   916
            ()
blanchet@41168
   917
        val birth = Timer.checkRealTimer timer
blanchet@41171
   918
        val _ =
blanchet@41211
   919
          if debug then Output.urgent_message "Invoking SMT solver..." else ()
blanchet@41209
   920
        val (outcome, used_facts) =
blanchet@42443
   921
          (case (slice, smt_filter) of
boehmes@41432
   922
             (1, SOME head) => head |> apsnd (apsnd repair_context)
boehmes@41432
   923
           | _ => SMT_Solver.smt_filter_preprocess state facts i)
blanchet@42443
   924
          |> SMT_Solver.smt_filter_apply slice_timeout
blanchet@41239
   925
          |> (fn {outcome, used_facts} => (outcome, used_facts))
blanchet@41209
   926
          handle exn => if Exn.is_interrupt exn then
blanchet@41209
   927
                          reraise exn
blanchet@41209
   928
                        else
blanchet@42061
   929
                          (ML_Compiler.exn_message exn
blanchet@41209
   930
                           |> SMT_Failure.Other_Failure |> SOME, [])
blanchet@41168
   931
        val death = Timer.checkRealTimer timer
blanchet@40553
   932
        val outcome0 = if is_none outcome0 then SOME outcome else outcome0
blanchet@41168
   933
        val time_so_far = Time.+ (time_so_far, Time.- (death, birth))
blanchet@40553
   934
        val too_many_facts_perhaps =
blanchet@40553
   935
          case outcome of
blanchet@40553
   936
            NONE => false
blanchet@40553
   937
          | SOME (SMT_Failure.Counterexample _) => false
blanchet@42443
   938
          | SOME SMT_Failure.Time_Out => slice_timeout <> timeout
blanchet@42614
   939
          | SOME (SMT_Failure.Abnormal_Termination _) => true (* kind of *)
blanchet@40553
   940
          | SOME SMT_Failure.Out_Of_Memory => true
blanchet@41211
   941
          | SOME (SMT_Failure.Other_Failure _) => true
blanchet@40553
   942
        val timeout = Time.- (timeout, Timer.checkRealTimer timer)
blanchet@40553
   943
      in
blanchet@42443
   944
        if too_many_facts_perhaps andalso slice < max_slices andalso
blanchet@40553
   945
           num_facts > 0 andalso Time.> (timeout, Time.zeroTime) then
blanchet@41169
   946
          let
blanchet@42614
   947
            val new_num_facts =
blanchet@42646
   948
              Real.ceil (Config.get ctxt smt_slice_fact_frac
blanchet@42646
   949
                         * Real.fromInt num_facts)
blanchet@42614
   950
            val _ =
blanchet@42614
   951
              if verbose andalso is_some outcome then
blanchet@42614
   952
                quote name ^ " invoked with " ^ string_of_int num_facts ^
blanchet@42614
   953
                " fact" ^ plural_s num_facts ^ ": " ^
blanchet@42614
   954
                string_for_failure (failure_from_smt_failure (the outcome)) ^
blanchet@42614
   955
                " Retrying with " ^ string_of_int new_num_facts ^ " fact" ^
blanchet@42638
   956
                plural_s new_num_facts ^ "..."
blanchet@42614
   957
                |> Output.urgent_message
blanchet@42614
   958
              else
blanchet@42614
   959
                ()
blanchet@42443
   960
          in
blanchet@42614
   961
            facts |> take new_num_facts
blanchet@42614
   962
                  |> do_slice timeout (slice + 1) outcome0 time_so_far
blanchet@42443
   963
          end
blanchet@40553
   964
        else
blanchet@40553
   965
          {outcome = if is_none outcome then NONE else the outcome0,
blanchet@41168
   966
           used_facts = used_facts,
blanchet@41168
   967
           run_time_in_msecs = SOME (Time.toMilliseconds time_so_far)}
blanchet@40409
   968
      end
blanchet@42443
   969
  in do_slice timeout 1 NONE Time.zeroTime end
blanchet@40409
   970
blanchet@43052
   971
fun run_smt_solver mode name (params as {debug, verbose, preplay_timeout, ...})
blanchet@43011
   972
        minimize_command
blanchet@43011
   973
        ({state, subgoal, subgoal_count, facts, smt_filter, ...}
blanchet@43011
   974
         : prover_problem) =
blanchet@36379
   975
  let
blanchet@41242
   976
    val ctxt = Proof.context_of state
blanchet@41256
   977
    val num_facts = length facts
blanchet@41256
   978
    val facts = facts ~~ (0 upto num_facts - 1)
blanchet@42646
   979
                |> map (smt_weighted_fact ctxt num_facts)
blanchet@40181
   980
    val {outcome, used_facts, run_time_in_msecs} =
blanchet@42646
   981
      smt_filter_loop ctxt name params state subgoal smt_filter facts
blanchet@43037
   982
    val (used_facts, used_ths) = used_facts |> ListPair.unzip
blanchet@41222
   983
    val outcome = outcome |> Option.map failure_from_smt_failure
blanchet@43052
   984
    val (preplay, message) =
blanchet@40184
   985
      case outcome of
blanchet@40184
   986
        NONE =>
blanchet@43052
   987
        (fn () =>
blanchet@43052
   988
            let
blanchet@43052
   989
              fun smt_settings () =
blanchet@43052
   990
                if name = SMT_Solver.solver_name_of ctxt then ""
blanchet@43052
   991
                else "smt_solver = " ^ maybe_quote name
blanchet@43052
   992
            in
blanchet@43052
   993
              case play_one_line_proof debug preplay_timeout used_ths state
blanchet@43052
   994
                                       subgoal [Metis, MetisFT] of
blanchet@43052
   995
                     p as Played _ => p
blanchet@43052
   996
                   | _ => Trust_Playable (SMT (smt_settings ()), NONE)
blanchet@43052
   997
            end,
blanchet@43052
   998
         fn preplay =>
blanchet@43052
   999
            let
blanchet@43052
  1000
              val one_line_params =
blanchet@43052
  1001
                (preplay, proof_banner mode name, used_facts,
blanchet@43052
  1002
                 choose_minimize_command minimize_command name preplay,
blanchet@43052
  1003
                 subgoal, subgoal_count)
blanchet@43052
  1004
            in
blanchet@43052
  1005
              one_line_proof_text one_line_params ^
blanchet@43052
  1006
              (if verbose then
blanchet@43052
  1007
                 "\nSMT solver real CPU time: " ^
blanchet@43052
  1008
                 string_from_time (Time.fromMilliseconds
blanchet@43052
  1009
                                       (the run_time_in_msecs)) ^ "."
blanchet@43052
  1010
               else
blanchet@43052
  1011
                 "")
blanchet@43052
  1012
            end)
blanchet@43052
  1013
      | SOME failure => (K Failed_to_Play, fn _ => string_for_failure failure)
blanchet@40063
  1014
  in
blanchet@43037
  1015
    {outcome = outcome, used_facts = used_facts,
blanchet@43052
  1016
     run_time_in_msecs = run_time_in_msecs, preplay = preplay,
blanchet@43052
  1017
     message = message}
blanchet@40063
  1018
  end
blanchet@40063
  1019
blanchet@43052
  1020
fun run_metis mode name ({debug, timeout, ...} : params) minimize_command
blanchet@43050
  1021
              ({state, subgoal, subgoal_count, facts, ...} : prover_problem) =
blanchet@43050
  1022
  let
blanchet@43050
  1023
    val reconstructor = if name = Metis_Tactics.metisN then Metis
blanchet@43050
  1024
                        else if name = Metis_Tactics.metisFT_N then MetisFT
blanchet@43050
  1025
                        else raise Fail ("unknown Metis version: " ^ quote name)
blanchet@43050
  1026
    val (used_facts, used_ths) =
blanchet@43050
  1027
      facts |> map untranslated_fact |> ListPair.unzip
blanchet@43050
  1028
  in
blanchet@43050
  1029
    case play_one_line_proof debug timeout used_ths state subgoal
blanchet@43050
  1030
                             [reconstructor] of
blanchet@43050
  1031
      play as Played (_, time) =>
blanchet@43052
  1032
      {outcome = NONE, used_facts = used_facts,
blanchet@43052
  1033
       run_time_in_msecs = SOME (Time.toMilliseconds time),
blanchet@43052
  1034
       preplay = K play,
blanchet@43052
  1035
       message = fn play =>
blanchet@43052
  1036
                    let
blanchet@43052
  1037
                      val one_line_params =
blanchet@43052
  1038
                         (play, proof_banner mode name, used_facts,
blanchet@43052
  1039
                          minimize_command name, subgoal, subgoal_count)
blanchet@43052
  1040
                    in one_line_proof_text one_line_params end}
blanchet@43052
  1041
    | play =>
blanchet@43052
  1042
      let val failure = if play = Failed_to_Play then GaveUp else TimedOut in
blanchet@43052
  1043
        {outcome = SOME failure, used_facts = [], run_time_in_msecs = NONE,
blanchet@43052
  1044
         preplay = K play, message = fn _ => string_for_failure failure}
blanchet@43050
  1045
      end
blanchet@43050
  1046
  end
blanchet@43050
  1047
blanchet@43021
  1048
fun get_prover ctxt mode name =
wenzelm@42361
  1049
  let val thy = Proof_Context.theory_of ctxt in
blanchet@43052
  1050
    if is_metis_prover name then run_metis mode name
blanchet@43052
  1051
    else if is_atp thy name then run_atp mode name (get_atp thy name)
blanchet@43052
  1052
    else if is_smt_prover ctxt name then run_smt_solver mode name
blanchet@43052
  1053
    else error ("No such prover: " ^ name ^ ".")
blanchet@40941
  1054
  end
blanchet@40063
  1055
wenzelm@28582
  1056
end;