src/HOL/Tools/Sledgehammer/sledgehammer_prover.ML
author blanchet
Fri Jan 31 13:42:47 2014 +0100 (2014-01-31 ago)
changeset 55208 11dd3d1da83b
parent 55207 42ad887a1c7c
child 55211 5d027af93a08
permissions -rw-r--r--
compile
blanchet@55201
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_prover.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@55201
     9
signature SLEDGEHAMMER_PROVER =
wenzelm@28477
    10
sig
blanchet@53586
    11
  type atp_failure = ATP_Proof.atp_failure
blanchet@46340
    12
  type stature = ATP_Problem_Generate.stature
blanchet@46320
    13
  type type_enc = ATP_Problem_Generate.type_enc
blanchet@51005
    14
  type fact = Sledgehammer_Fact.fact
smolkas@52555
    15
  type reconstructor = Sledgehammer_Reconstructor.reconstructor
blanchet@54824
    16
  type play_outcome = Sledgehammer_Reconstructor.play_outcome
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@53800
    25
     spy : bool,
blanchet@48321
    26
     blocking : bool,
blanchet@48321
    27
     provers : string list,
blanchet@48321
    28
     type_enc : string option,
blanchet@48321
    29
     strict : bool,
blanchet@48321
    30
     lam_trans : string option,
blanchet@48321
    31
     uncurried_aliases : bool option,
blanchet@48321
    32
     learn : bool,
blanchet@48321
    33
     fact_filter : string option,
blanchet@48321
    34
     max_facts : int option,
blanchet@48321
    35
     fact_thresholds : real * real,
blanchet@48321
    36
     max_mono_iters : int option,
blanchet@48321
    37
     max_new_mono_instances : int option,
blanchet@51190
    38
     isar_proofs : bool option,
blanchet@55183
    39
     compress_isar : real,
blanchet@55183
    40
     try0_isar : bool,
blanchet@48321
    41
     slice : bool,
blanchet@48321
    42
     minimize : bool option,
blanchet@54816
    43
     timeout : Time.time,
blanchet@54816
    44
     preplay_timeout : Time.time,
blanchet@48321
    45
     expect : string}
blanchet@39493
    46
blanchet@40061
    47
  type prover_problem =
blanchet@54141
    48
    {comment : string,
blanchet@54141
    49
     state : Proof.state,
blanchet@48321
    50
     goal : thm,
blanchet@48321
    51
     subgoal : int,
blanchet@48321
    52
     subgoal_count : int,
blanchet@51010
    53
     factss : (string * fact list) list}
blanchet@39493
    54
blanchet@40061
    55
  type prover_result =
blanchet@53586
    56
    {outcome : atp_failure option,
blanchet@51009
    57
     used_facts : (string * stature) list,
blanchet@51009
    58
     used_from : fact list,
blanchet@48321
    59
     run_time : Time.time,
blanchet@54824
    60
     preplay : (reconstructor * play_outcome) Lazy.lazy,
blanchet@54824
    61
     message : reconstructor * play_outcome -> string,
blanchet@48321
    62
     message_tail : string}
blanchet@39493
    63
blanchet@43051
    64
  type prover =
blanchet@45520
    65
    params -> ((string * string list) list -> string -> minimize_command)
blanchet@45520
    66
    -> prover_problem -> prover_result
blanchet@35867
    67
blanchet@43092
    68
  val dest_dir : string Config.T
blanchet@43092
    69
  val problem_prefix : string Config.T
blanchet@48143
    70
  val completish : bool Config.T
blanchet@44592
    71
  val atp_full_names : bool Config.T
blanchet@48319
    72
  val SledgehammerN : string
blanchet@45519
    73
  val plain_metis : reconstructor
blanchet@55205
    74
  val overlord_file_location_of_prover : string -> string * string
blanchet@55205
    75
  val proof_banner : mode -> string -> string
blanchet@54811
    76
  val extract_reconstructor : params -> reconstructor -> string * (string * string list) list
blanchet@45379
    77
  val is_reconstructor : string -> bool
blanchet@43050
    78
  val is_atp : theory -> string -> bool
blanchet@47962
    79
  val is_ho_atp: Proof.context -> string -> bool
blanchet@41727
    80
  val supported_provers : Proof.context -> unit
blanchet@40059
    81
  val kill_provers : unit -> unit
blanchet@40059
    82
  val running_provers : unit -> unit
blanchet@40059
    83
  val messages : int option -> unit
blanchet@48798
    84
  val is_fact_chained : (('a * stature) * 'b) -> bool
blanchet@55205
    85
  val bunch_of_reconstructors : bool -> (bool -> string) -> reconstructor list
blanchet@48798
    86
  val filter_used_facts :
blanchet@48798
    87
    bool -> (''a * stature) list -> ((''a * stature) * 'b) list ->
blanchet@48798
    88
    ((''a * stature) * 'b) list
blanchet@55205
    89
  val play_one_line_proof : mode -> bool -> bool -> Time.time -> ((string * 'a) * thm) list ->
blanchet@55205
    90
    Proof.state -> int -> reconstructor -> reconstructor list -> reconstructor * play_outcome
blanchet@55205
    91
  val remotify_atp_if_not_installed : theory -> string -> string option
blanchet@55205
    92
  val isar_supported_prover_of : theory -> string -> string
blanchet@55205
    93
  val choose_minimize_command : theory -> params -> ((string * string list) list -> string -> 'a) ->
blanchet@55205
    94
    string -> reconstructor * play_outcome -> 'a
blanchet@55205
    95
  val repair_monomorph_context : int option -> int -> int option -> int -> Proof.context ->
blanchet@55205
    96
    Proof.context
blanchet@55205
    97
  val run_reconstructor : mode -> string -> prover
wenzelm@28477
    98
end;
wenzelm@28477
    99
blanchet@55201
   100
structure Sledgehammer_Prover : SLEDGEHAMMER_PROVER =
wenzelm@28477
   101
struct
wenzelm@28477
   102
blanchet@43085
   103
open ATP_Util
blanchet@38028
   104
open ATP_Problem
blanchet@39491
   105
open ATP_Proof
blanchet@38028
   106
open ATP_Systems
blanchet@46320
   107
open ATP_Problem_Generate
blanchet@46320
   108
open ATP_Proof_Reconstruct
blanchet@45521
   109
open Metis_Tactic
blanchet@38023
   110
open Sledgehammer_Util
blanchet@51005
   111
open Sledgehammer_Fact
smolkas@52555
   112
open Sledgehammer_Reconstructor
blanchet@55202
   113
open Sledgehammer_Isar_Print
blanchet@55202
   114
open Sledgehammer_Isar
blanchet@48288
   115
blanchet@54000
   116
(* Empty string means create files in Isabelle's temporary files directory. *)
blanchet@54094
   117
val dest_dir = Attrib.setup_config_string @{binding sledgehammer_dest_dir} (K "")
blanchet@54094
   118
val problem_prefix = Attrib.setup_config_string @{binding sledgehammer_problem_prefix} (K "prob")
blanchet@54094
   119
val completish = Attrib.setup_config_bool @{binding sledgehammer_completish} (K false)
blanchet@54000
   120
blanchet@54000
   121
(* In addition to being easier to read, readable names are often much shorter,
blanchet@54000
   122
   especially if types are mangled in names. This makes a difference for some
blanchet@54000
   123
   provers (e.g., E). For these reason, short names are enabled by default. *)
blanchet@54094
   124
val atp_full_names = Attrib.setup_config_bool @{binding sledgehammer_atp_full_names} (K false)
blanchet@54000
   125
wenzelm@53052
   126
datatype mode = Auto_Try | Try | Normal | MaSh | Auto_Minimize | Minimize
blanchet@43021
   127
blanchet@45376
   128
(* Identifier that distinguishes Sledgehammer from other tools that could use
blanchet@38102
   129
   "Async_Manager". *)
blanchet@48319
   130
val SledgehammerN = "Sledgehammer"
blanchet@37585
   131
blanchet@45520
   132
val reconstructor_names = [metisN, smtN]
blanchet@46365
   133
val plain_metis = Metis (hd partial_type_encs, combsN)
blanchet@45561
   134
val is_reconstructor = member (op =) reconstructor_names
blanchet@43228
   135
blanchet@43050
   136
val is_atp = member (op =) o supported_atps
blanchet@43050
   137
blanchet@51998
   138
fun is_atp_of_format is_format ctxt name =
blanchet@42944
   139
  let val thy = Proof_Context.theory_of ctxt in
blanchet@55205
   140
    (case try (get_atp thy) name of
blanchet@47606
   141
      SOME config =>
blanchet@55205
   142
      exists (fn (_, ((_, format, _, _, _), _)) => is_format format) (#best_slices (config ()) ctxt)
blanchet@55205
   143
    | NONE => false)
blanchet@42944
   144
  end
blanchet@42944
   145
blanchet@51998
   146
val is_ho_atp = is_atp_of_format is_format_higher_order
blanchet@44597
   147
blanchet@55205
   148
fun remotify_atp_if_supported_and_not_already_remote thy name =
blanchet@51200
   149
  if String.isPrefix remote_prefix name then
blanchet@51200
   150
    SOME name
blanchet@51200
   151
  else
blanchet@51200
   152
    let val remote_name = remote_prefix ^ name in
blanchet@55205
   153
      if is_atp thy remote_name then SOME remote_name else NONE
blanchet@51200
   154
    end
blanchet@51200
   155
blanchet@55205
   156
fun remotify_atp_if_not_installed thy name =
blanchet@55207
   157
  if is_atp thy name andalso is_atp_installed thy name then SOME name
blanchet@55205
   158
  else remotify_atp_if_supported_and_not_already_remote thy name
blanchet@40063
   159
blanchet@41727
   160
fun supported_provers ctxt =
blanchet@40060
   161
  let
wenzelm@42361
   162
    val thy = Proof_Context.theory_of ctxt
blanchet@40060
   163
    val (remote_provers, local_provers) =
blanchet@45520
   164
      reconstructor_names @
blanchet@41727
   165
      sort_strings (supported_atps thy) @
blanchet@41727
   166
      sort_strings (SMT_Solver.available_solvers_of ctxt)
blanchet@40060
   167
      |> List.partition (String.isPrefix remote_prefix)
blanchet@40060
   168
  in
blanchet@41727
   169
    Output.urgent_message ("Supported provers: " ^
blanchet@40205
   170
                           commas (local_provers @ remote_provers) ^ ".")
blanchet@40060
   171
  end
blanchet@35969
   172
blanchet@48319
   173
fun kill_provers () = Async_Manager.kill_threads SledgehammerN "prover"
blanchet@48319
   174
fun running_provers () = Async_Manager.running_threads SledgehammerN "prover"
blanchet@48319
   175
val messages = Async_Manager.thread_messages SledgehammerN "prover"
blanchet@40059
   176
blanchet@35969
   177
type params =
blanchet@48321
   178
  {debug : bool,
blanchet@48321
   179
   verbose : bool,
blanchet@48321
   180
   overlord : bool,
blanchet@53800
   181
   spy : bool,
blanchet@48321
   182
   blocking : bool,
blanchet@48321
   183
   provers : string list,
blanchet@48321
   184
   type_enc : string option,
blanchet@48321
   185
   strict : bool,
blanchet@48321
   186
   lam_trans : string option,
blanchet@48321
   187
   uncurried_aliases : bool option,
blanchet@48321
   188
   learn : bool,
blanchet@48321
   189
   fact_filter : string option,
blanchet@48321
   190
   max_facts : int option,
blanchet@48321
   191
   fact_thresholds : real * real,
blanchet@48321
   192
   max_mono_iters : int option,
blanchet@48321
   193
   max_new_mono_instances : int option,
blanchet@51190
   194
   isar_proofs : bool option,
blanchet@55183
   195
   compress_isar : real,
blanchet@55183
   196
   try0_isar : bool,
blanchet@48321
   197
   slice : bool,
blanchet@48321
   198
   minimize : bool option,
blanchet@54816
   199
   timeout : Time.time,
blanchet@54816
   200
   preplay_timeout : Time.time,
blanchet@48321
   201
   expect : string}
blanchet@35867
   202
blanchet@40061
   203
type prover_problem =
blanchet@54141
   204
  {comment : string,
blanchet@54141
   205
   state : Proof.state,
blanchet@48321
   206
   goal : thm,
blanchet@48321
   207
   subgoal : int,
blanchet@48321
   208
   subgoal_count : int,
blanchet@51010
   209
   factss : (string * fact list) list}
blanchet@35867
   210
blanchet@40061
   211
type prover_result =
blanchet@53586
   212
  {outcome : atp_failure option,
blanchet@48321
   213
   used_facts : (string * stature) list,
blanchet@51009
   214
   used_from : fact list,
blanchet@48321
   215
   run_time : Time.time,
blanchet@54824
   216
   preplay : (reconstructor * play_outcome) Lazy.lazy,
blanchet@54824
   217
   message : reconstructor * play_outcome -> string,
blanchet@48321
   218
   message_tail : string}
blanchet@35867
   219
blanchet@43051
   220
type prover =
blanchet@45520
   221
  params -> ((string * string list) list -> string -> minimize_command)
blanchet@45520
   222
  -> prover_problem -> prover_result
blanchet@35867
   223
blanchet@55205
   224
fun overlord_file_location_of_prover prover = (getenv "ISABELLE_HOME_USER", "prob_" ^ prover)
blanchet@41313
   225
blanchet@43052
   226
fun proof_banner mode name =
blanchet@55205
   227
  (case mode of
blanchet@43033
   228
    Auto_Try => "Auto Sledgehammer (" ^ quote name ^ ") found a proof"
blanchet@43033
   229
  | Try => "Sledgehammer (" ^ quote name ^ ") found a proof"
blanchet@55205
   230
  | _ => "Try this")
blanchet@43033
   231
blanchet@45561
   232
fun bunch_of_reconstructors needs_full_types lam_trans =
blanchet@48800
   233
  if needs_full_types then
blanchet@48802
   234
    [Metis (full_type_enc, lam_trans false),
blanchet@48802
   235
     Metis (really_full_type_enc, lam_trans false),
blanchet@48802
   236
     Metis (full_type_enc, lam_trans true),
blanchet@48802
   237
     Metis (really_full_type_enc, lam_trans true),
blanchet@48802
   238
     SMT]
blanchet@48802
   239
  else
blanchet@48800
   240
    [Metis (partial_type_enc, lam_trans false),
blanchet@48800
   241
     Metis (full_type_enc, lam_trans false),
blanchet@48800
   242
     Metis (no_typesN, lam_trans true),
blanchet@48800
   243
     Metis (really_full_type_enc, lam_trans true),
blanchet@48800
   244
     SMT]
blanchet@45561
   245
blanchet@54824
   246
fun extract_reconstructor ({type_enc, lam_trans, ...} : params) (Metis (type_enc', lam_trans')) =
blanchet@45561
   247
    let
blanchet@45561
   248
      val override_params =
blanchet@54824
   249
        (if is_none type_enc andalso type_enc' = hd partial_type_encs then []
blanchet@54824
   250
         else [("type_enc", [hd (unalias_type_enc type_enc')])]) @
blanchet@54824
   251
        (if is_none lam_trans andalso lam_trans' = default_metis_lam_trans then []
blanchet@54824
   252
         else [("lam_trans", [lam_trans'])])
blanchet@45561
   253
    in (metisN, override_params) end
blanchet@45561
   254
  | extract_reconstructor _ SMT = (smtN, [])
blanchet@45561
   255
blanchet@43033
   256
(* based on "Mirabelle.can_apply" and generalized *)
blanchet@43034
   257
fun timed_apply timeout tac state i =
blanchet@43033
   258
  let
blanchet@43033
   259
    val {context = ctxt, facts, goal} = Proof.goal state
blanchet@43033
   260
    val full_tac = Method.insert_tac facts i THEN tac ctxt i
blanchet@54816
   261
  in
blanchet@54816
   262
    TimeLimit.timeLimit timeout (try (Seq.pull o full_tac)) goal
blanchet@54816
   263
  end
blanchet@43033
   264
blanchet@54824
   265
fun tac_of_reconstructor (Metis (type_enc, lam_trans)) = metis_tac [type_enc] lam_trans
blanchet@51998
   266
  | tac_of_reconstructor SMT = SMT_Solver.smt_tac
blanchet@43034
   267
blanchet@45520
   268
fun timed_reconstructor reconstr debug timeout ths =
blanchet@55170
   269
  timed_apply timeout (silence_reconstructors debug
blanchet@55170
   270
    #> (fn ctxt => tac_of_reconstructor reconstr ctxt ths))
blanchet@43033
   271
blanchet@48798
   272
fun is_fact_chained ((_, (sc, _)), _) = sc = Chained
blanchet@48798
   273
blanchet@48798
   274
fun filter_used_facts keep_chained used =
blanchet@54773
   275
  filter ((member (op =) used o fst) orf (if keep_chained then is_fact_chained else K false))
blanchet@43033
   276
blanchet@54815
   277
fun play_one_line_proof mode debug verbose timeout pairs state i preferred reconstrs =
blanchet@43034
   278
  let
blanchet@45520
   279
    fun get_preferred reconstrs =
blanchet@45520
   280
      if member (op =) reconstrs preferred then preferred
blanchet@45520
   281
      else List.last reconstrs
blanchet@43034
   282
  in
blanchet@54816
   283
    if timeout = Time.zeroTime then
blanchet@54824
   284
      (get_preferred reconstrs, Not_Played)
blanchet@45379
   285
    else
blanchet@50557
   286
      let
blanchet@54815
   287
        val _ = if mode = Minimize then Output.urgent_message "Preplaying proof..." else ()
blanchet@50557
   288
        val ths = pairs |> sort_wrt (fst o fst) |> map snd
blanchet@54824
   289
        fun play [] [] = (get_preferred reconstrs, Play_Failed)
blanchet@54824
   290
          | play timed_outs [] = (get_preferred timed_outs, Play_Timed_Out timeout)
blanchet@50557
   291
          | play timed_out (reconstr :: reconstrs) =
blanchet@50557
   292
            let
blanchet@50557
   293
              val _ =
blanchet@50557
   294
                if verbose then
blanchet@54816
   295
                  Output.urgent_message ("Trying \"" ^ string_of_reconstructor reconstr ^
blanchet@54816
   296
                    "\" for " ^ string_of_time timeout ^ "...")
blanchet@50557
   297
                else
blanchet@50557
   298
                  ()
blanchet@50557
   299
              val timer = Timer.startRealTimer ()
blanchet@50557
   300
            in
blanchet@50557
   301
              case timed_reconstructor reconstr debug timeout ths state i of
blanchet@54824
   302
                SOME (SOME _) => (reconstr, Played (Timer.checkRealTimer timer))
blanchet@50557
   303
              | _ => play timed_out reconstrs
blanchet@50557
   304
            end
blanchet@50557
   305
            handle TimeLimit.TimeOut => play (reconstr :: timed_out) reconstrs
blanchet@54824
   306
      in
blanchet@54824
   307
        play [] reconstrs
blanchet@54824
   308
      end
blanchet@43033
   309
  end
blanchet@43033
   310
blanchet@55205
   311
val canonical_isar_supported_prover = eN
blanchet@51013
   312
blanchet@55205
   313
fun isar_supported_prover_of thy name =
blanchet@55205
   314
  if is_atp thy name then name
blanchet@55205
   315
  else the_default name (remotify_atp_if_not_installed thy canonical_isar_supported_prover)
blanchet@43051
   316
blanchet@55202
   317
(* FIXME: See the analogous logic in the function "maybe_minimize" in
blanchet@55202
   318
   "sledgehammer_prover_minimize.ML". *)
blanchet@55205
   319
fun choose_minimize_command thy (params as {isar_proofs, ...}) minimize_command name preplay =
blanchet@45520
   320
  let
blanchet@55205
   321
    val maybe_isar_name = name |> isar_proofs = SOME true ? isar_supported_prover_of thy
blanchet@51200
   322
    val (min_name, override_params) =
blanchet@54824
   323
      (case preplay of
blanchet@54824
   324
        (reconstr, Played _) =>
blanchet@51200
   325
        if isar_proofs = SOME true then (maybe_isar_name, [])
blanchet@51200
   326
        else extract_reconstructor params reconstr
blanchet@54824
   327
      | _ => (maybe_isar_name, []))
blanchet@51200
   328
  in minimize_command override_params min_name end
blanchet@43051
   329
blanchet@53480
   330
val max_fact_instances = 10 (* FUDGE *)
blanchet@53480
   331
blanchet@55205
   332
fun repair_monomorph_context max_iters best_max_iters max_new_instances best_max_new_instances =
blanchet@52034
   333
  Config.put Monomorph.max_rounds (max_iters |> the_default best_max_iters)
blanchet@52034
   334
  #> Config.put Monomorph.max_new_instances
blanchet@55205
   335
       (max_new_instances |> the_default best_max_new_instances)
blanchet@53480
   336
  #> Config.put Monomorph.max_thm_instances max_fact_instances
blanchet@52034
   337
blanchet@55205
   338
fun run_reconstructor mode name (params as {debug, verbose, timeout, type_enc, lam_trans, ...})
blanchet@54824
   339
    minimize_command
blanchet@55205
   340
    ({state, subgoal, subgoal_count, factss = (_, facts) :: _, ...} : prover_problem) =
blanchet@43050
   341
  let
blanchet@45520
   342
    val reconstr =
blanchet@45520
   343
      if name = metisN then
blanchet@45520
   344
        Metis (type_enc |> the_default (hd partial_type_encs),
blanchet@54500
   345
               lam_trans |> the_default default_metis_lam_trans)
blanchet@45520
   346
      else if name = smtN then
blanchet@45520
   347
        SMT
blanchet@45520
   348
      else
blanchet@45520
   349
        raise Fail ("unknown reconstructor: " ^ quote name)
blanchet@51005
   350
    val used_facts = facts |> map fst
blanchet@43050
   351
  in
blanchet@54828
   352
    (case play_one_line_proof (if mode = Minimize then Normal else mode) debug verbose timeout facts
blanchet@54828
   353
       state subgoal reconstr [reconstr] of
blanchet@54824
   354
      play as (_, Played time) =>
blanchet@54824
   355
      {outcome = NONE, used_facts = used_facts, used_from = facts, run_time = time,
blanchet@54824
   356
       preplay = Lazy.value play,
blanchet@45561
   357
       message =
blanchet@45561
   358
         fn play =>
blanchet@45561
   359
            let
blanchet@45561
   360
              val (_, override_params) = extract_reconstructor params reconstr
blanchet@45561
   361
              val one_line_params =
blanchet@54824
   362
                (play, proof_banner mode name, used_facts, minimize_command override_params name,
blanchet@54824
   363
                 subgoal, subgoal_count)
blanchet@48799
   364
              val num_chained = length (#facts (Proof.goal state))
blanchet@52754
   365
            in
wenzelm@53052
   366
              one_line_proof_text num_chained one_line_params
blanchet@52754
   367
            end,
blanchet@43261
   368
       message_tail = ""}
blanchet@43052
   369
    | play =>
blanchet@43166
   370
      let
blanchet@54824
   371
        val failure = (case play of (_, Play_Failed) => GaveUp | _ => TimedOut)
blanchet@43166
   372
      in
blanchet@51009
   373
        {outcome = SOME failure, used_facts = [], used_from = [],
blanchet@51009
   374
         run_time = Time.zeroTime, preplay = Lazy.value play,
blanchet@53586
   375
         message = fn _ => string_of_atp_failure failure, message_tail = ""}
blanchet@54824
   376
      end)
blanchet@43050
   377
  end
blanchet@43050
   378
wenzelm@28582
   379
end;