src/HOL/Tools/Sledgehammer/sledgehammer_minimize.ML
author blanchet
Sun, 06 Nov 2011 13:37:49 +0100
changeset 46229 ff2edf24e83a
parent 46228 cb54f1b34cf9
child 46230 fbf2e1bdbf16
permissions -rw-r--r--
cascading timeouts in minimizer
blanchet@39232
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_minimize.ML
immler@31037
     2
    Author:     Philipp Meyer, TU Muenchen
blanchet@36370
     3
    Author:     Jasmin Blanchette, TU Muenchen
immler@31037
     4
blanchet@41223
     5
Minimization of fact list for Metis using external provers.
immler@31037
     6
*)
immler@31037
     7
blanchet@39232
     8
signature SLEDGEHAMMER_MINIMIZE =
boehmes@32525
     9
sig
blanchet@43926
    10
  type locality = ATP_Translate.locality
blanchet@43926
    11
  type play = ATP_Reconstruct.play
blanchet@41335
    12
  type params = Sledgehammer_Provers.params
blanchet@35867
    13
blanchet@43517
    14
  val binary_min_facts : int Config.T
blanchet@40242
    15
  val minimize_facts :
blanchet@43905
    16
    string -> params -> bool -> int -> int -> Proof.state
blanchet@41339
    17
    -> ((string * locality) * thm list) list
blanchet@43893
    18
    -> ((string * locality) * thm list) list option
blanchet@44102
    19
       * ((unit -> play) * (play -> string) * string)
blanchet@39240
    20
  val run_minimize :
blanchet@39240
    21
    params -> int -> (Facts.ref * Attrib.src list) list -> Proof.state -> unit
blanchet@35866
    22
end;
boehmes@32525
    23
blanchet@39232
    24
structure Sledgehammer_Minimize : SLEDGEHAMMER_MINIMIZE =
immler@31037
    25
struct
immler@31037
    26
blanchet@43926
    27
open ATP_Util
blanchet@39736
    28
open ATP_Proof
blanchet@43926
    29
open ATP_Translate
blanchet@43926
    30
open ATP_Reconstruct
blanchet@36140
    31
open Sledgehammer_Util
blanchet@39232
    32
open Sledgehammer_Filter
blanchet@41335
    33
open Sledgehammer_Provers
nipkow@33492
    34
blanchet@36370
    35
(* wrapper for calling external prover *)
wenzelm@31236
    36
blanchet@40242
    37
fun n_facts names =
blanchet@38937
    38
  let val n = length names in
blanchet@40242
    39
    string_of_int n ^ " fact" ^ plural_s n ^
blanchet@38338
    40
    (if n > 0 then
blanchet@46227
    41
       ": " ^ (names |> map fst |> sort_distinct string_ord
blanchet@46227
    42
                     |> space_implode " ")
blanchet@38338
    43
     else
blanchet@38338
    44
       "")
blanchet@38338
    45
  end
blanchet@38338
    46
blanchet@41339
    47
fun print silent f = if silent then () else Output.urgent_message (f ())
blanchet@41339
    48
blanchet@43589
    49
fun test_facts ({debug, verbose, overlord, provers, max_mono_iters,
blanchet@44493
    50
                 max_new_mono_instances, type_enc, isar_proof,
blanchet@43856
    51
                 isar_shrink_factor, preplay_timeout, ...} : params)
blanchet@43905
    52
               silent (prover : prover) timeout i n state facts =
wenzelm@31236
    53
  let
blanchet@41525
    54
    val _ =
blanchet@41525
    55
      print silent (fn () =>
blanchet@41525
    56
          "Testing " ^ n_facts (map fst facts) ^
blanchet@41525
    57
          (if verbose then " (timeout: " ^ string_from_time timeout ^ ")"
blanchet@43926
    58
           else "") ^ "...")
blanchet@42613
    59
    val {goal, ...} = Proof.goal state
blanchet@45320
    60
    val facts =
blanchet@45320
    61
      facts |> maps (fn (n, ths) => ths |> map (Untranslated_Fact o pair n))
blanchet@38346
    62
    val params =
blanchet@42915
    63
      {debug = debug, verbose = verbose, overlord = overlord, blocking = true,
blanchet@44493
    64
       provers = provers, type_enc = type_enc, sound = true,
blanchet@45320
    65
       relevance_thresholds = (1.01, 1.01), max_relevant = SOME (length facts),
blanchet@43605
    66
       max_mono_iters = max_mono_iters,
blanchet@43605
    67
       max_new_mono_instances = max_new_mono_instances, isar_proof = isar_proof,
blanchet@43605
    68
       isar_shrink_factor = isar_shrink_factor, slicing = false,
blanchet@43856
    69
       timeout = timeout, preplay_timeout = preplay_timeout, expect = ""}
blanchet@40246
    70
    val problem =
blanchet@40246
    71
      {state = state, goal = goal, subgoal = i, subgoal_count = n,
blanchet@42612
    72
       facts = facts, smt_filter = NONE}
blanchet@43891
    73
    val result as {outcome, used_facts, run_time_in_msecs, ...} =
blanchet@43892
    74
      prover params (K (K "")) problem
blanchet@36223
    75
  in
blanchet@44100
    76
    print silent
blanchet@44100
    77
          (fn () =>
blanchet@44100
    78
              case outcome of
blanchet@44100
    79
                SOME failure => string_for_failure failure
blanchet@44100
    80
              | NONE =>
blanchet@44100
    81
                "Found proof" ^
blanchet@44100
    82
                 (if length used_facts = length facts then ""
blanchet@44100
    83
                  else " with " ^ n_facts used_facts) ^
blanchet@44100
    84
                 (case run_time_in_msecs of
blanchet@44100
    85
                    SOME ms =>
blanchet@44100
    86
                    " (" ^ string_from_time (Time.fromMilliseconds ms) ^ ")"
blanchet@44100
    87
                  | NONE => "") ^ ".");
blanchet@38338
    88
    result
blanchet@36223
    89
  end
wenzelm@31236
    90
blanchet@40445
    91
(* minimalization of facts *)
wenzelm@31236
    92
blanchet@46228
    93
(* The linear algorithm usually outperforms the binary algorithm when over 60%
blanchet@46228
    94
   of the facts are actually needed. The binary algorithm is much more
blanchet@46228
    95
   appropriate for provers that cannot return the list of used facts and hence
blanchet@46228
    96
   returns all facts as used. Since we cannot know in advance how many facts are
blanchet@46228
    97
   actually needed, we heuristically set the threshold to 10 facts. *)
blanchet@43517
    98
val binary_min_facts =
blanchet@43517
    99
  Attrib.setup_config_int @{binding sledgehammer_minimize_binary_min_facts}
blanchet@46229
   100
                          (K 20)
blanchet@41223
   101
blanchet@46229
   102
fun linear_minimize test timeout result xs =
blanchet@46229
   103
  let
blanchet@46229
   104
    fun min _ [] p = p
blanchet@46229
   105
      | min timeout (x :: xs) (seen, result) =
blanchet@46229
   106
        case test timeout (xs @ seen) of
blanchet@46229
   107
          result as {outcome = NONE, used_facts, ...} : prover_result =>
blanchet@46229
   108
          min timeout (filter_used_facts used_facts xs)
blanchet@46228
   109
                      (filter_used_facts used_facts seen, result)
blanchet@46229
   110
        | _ => min timeout xs (x :: seen, result)
blanchet@46229
   111
  in min timeout xs ([], result) end
blanchet@38249
   112
blanchet@46229
   113
fun binary_minimize test timeout result xs =
blanchet@41223
   114
  let
blanchet@46228
   115
    fun min depth result sup (xs as _ :: _ :: _) =
blanchet@42614
   116
        let
blanchet@46228
   117
          val (l0, r0) = chop (length xs div 2) xs
blanchet@42614
   118
(*
blanchet@46227
   119
          val _ = warning (replicate_string depth " " ^ "{ " ^
blanchet@46227
   120
                           "sup: " ^ n_facts (map fst sup))
blanchet@46227
   121
          val _ = warning (replicate_string depth " " ^ "  " ^
blanchet@46227
   122
                           "xs: " ^ n_facts (map fst xs))
blanchet@46227
   123
          val _ = warning (replicate_string depth " " ^ "  " ^
blanchet@46227
   124
                           "l0: " ^ n_facts (map fst l0))
blanchet@46227
   125
          val _ = warning (replicate_string depth " " ^ "  " ^
blanchet@46227
   126
                           "r0: " ^ n_facts (map fst r0))
blanchet@42614
   127
*)
blanchet@46228
   128
          val depth = depth + 1
blanchet@42614
   129
        in
blanchet@46229
   130
          case test timeout (sup @ l0) of
blanchet@46228
   131
            result as {outcome = NONE, used_facts, ...} : prover_result =>
blanchet@46228
   132
            min depth result (filter_used_facts used_facts sup)
blanchet@46228
   133
                      (filter_used_facts used_facts l0)
blanchet@46228
   134
          | _ =>
blanchet@46229
   135
            case test timeout (sup @ r0) of
blanchet@46228
   136
              result as {outcome = NONE, used_facts, ...} =>
blanchet@46228
   137
              min depth result (filter_used_facts used_facts sup)
blanchet@46228
   138
                        (filter_used_facts used_facts r0)
blanchet@46228
   139
            | _ =>
blanchet@46228
   140
              let
blanchet@46228
   141
                val (sup_r0, (l, result)) = min depth result (sup @ r0) l0
blanchet@46228
   142
                val (sup, r0) =
blanchet@46228
   143
                  (sup, r0) |> pairself (filter_used_facts (map fst sup_r0))
blanchet@46228
   144
                val (sup_l, (r, result)) = min depth result (sup @ l) r0
blanchet@46228
   145
                val sup = sup |> filter_used_facts (map fst sup_l)
blanchet@46228
   146
              in (sup, (l @ r, result)) end
blanchet@41223
   147
        end
blanchet@42614
   148
(*
blanchet@42614
   149
        |> tap (fn _ => warning (replicate_string depth " " ^ "}"))
blanchet@42614
   150
*)
blanchet@46228
   151
      | min _ result sup xs = (sup, (xs, result))
blanchet@46228
   152
  in
blanchet@46228
   153
    case snd (min 0 result [] xs) of
blanchet@46228
   154
      ([x], result) =>
blanchet@46229
   155
      (case test timeout [] of
blanchet@46228
   156
         result as {outcome = NONE, ...} => ([], result)
blanchet@46228
   157
       | _ => ([x], result))
blanchet@46228
   158
    | p => p
blanchet@46228
   159
  end
blanchet@41223
   160
blanchet@41223
   161
(* Give the external prover some slack. The ATP gets further slack because the
blanchet@41223
   162
   Sledgehammer preprocessing time is included in the estimate below but isn't
blanchet@41223
   163
   part of the timeout. *)
blanchet@41525
   164
val slack_msecs = 200
blanchet@38338
   165
blanchet@43905
   166
fun minimize_facts prover_name (params as {timeout, ...}) silent i n state
blanchet@43905
   167
                   facts =
wenzelm@31236
   168
  let
blanchet@41189
   169
    val ctxt = Proof.context_of state
blanchet@43862
   170
    val prover = get_prover ctxt Minimize prover_name
blanchet@38813
   171
    val msecs = Time.toMilliseconds timeout
blanchet@43899
   172
    val _ = print silent (fn () => "Sledgehammer minimizer: " ^
blanchet@41223
   173
                                   quote prover_name ^ ".")
blanchet@46229
   174
    fun test timeout = test_facts params silent prover timeout i n state
blanchet@38338
   175
    val timer = Timer.startRealTimer ()
wenzelm@31236
   176
  in
blanchet@46229
   177
    (case test timeout facts of
blanchet@40445
   178
       result as {outcome = NONE, used_facts, ...} =>
blanchet@38249
   179
       let
blanchet@38338
   180
         val time = Timer.checkRealTimer timer
blanchet@46229
   181
         val timeout =
blanchet@41525
   182
           Int.min (msecs, Time.toMilliseconds time + slack_msecs)
blanchet@38338
   183
           |> Time.fromMilliseconds
blanchet@41223
   184
         val facts = filter_used_facts used_facts facts
blanchet@46229
   185
         val min = 
blanchet@46229
   186
           if length facts >= Config.get ctxt binary_min_facts then
blanchet@46229
   187
             binary_minimize
blanchet@46229
   188
           else
blanchet@46229
   189
             linear_minimize
blanchet@44439
   190
         val (min_facts, {preplay, message, message_tail, ...}) =
blanchet@46229
   191
           min test timeout result facts
blanchet@41339
   192
         val _ = print silent (fn () => cat_lines
blanchet@44497
   193
           ["Minimized to " ^ n_facts (map fst min_facts)] ^
blanchet@44439
   194
            (case length (filter (curry (op =) Chained o snd o fst) min_facts) of
blanchet@38937
   195
               0 => ""
blanchet@44439
   196
             | n => "\n(including " ^ string_of_int n ^ " chained)") ^ ".")
blanchet@44439
   197
       in (SOME min_facts, (preplay, message, message_tail)) end
blanchet@43893
   198
     | {outcome = SOME TimedOut, preplay, ...} =>
blanchet@43893
   199
       (NONE,
blanchet@43893
   200
        (preplay,
blanchet@43893
   201
         fn _ => "Timeout: You can increase the time limit using the \
blanchet@43893
   202
                 \\"timeout\" option (e.g., \"timeout = " ^
blanchet@44102
   203
                 string_of_int (10 + msecs div 1000) ^ "\").",
blanchet@44102
   204
         ""))
blanchet@43893
   205
     | {preplay, message, ...} =>
blanchet@44102
   206
       (NONE, (preplay, prefix "Prover error: " o message, "")))
blanchet@44007
   207
    handle ERROR msg =>
blanchet@44102
   208
           (NONE, (K (Failed_to_Play Metis), fn _ => "Error: " ^ msg, ""))
immler@31037
   209
  end
immler@31037
   210
blanchet@41513
   211
fun run_minimize (params as {provers, ...}) i refs state =
blanchet@38291
   212
  let
blanchet@38291
   213
    val ctxt = Proof.context_of state
blanchet@38935
   214
    val reserved = reserved_isar_keyword_table ()
blanchet@43884
   215
    val chained_ths = normalize_chained_theorems (#facts (Proof.goal state))
blanchet@40445
   216
    val facts =
blanchet@41339
   217
      refs
blanchet@41339
   218
      |> maps (map (apsnd single) o fact_from_ref ctxt reserved chained_ths)
blanchet@38291
   219
  in
blanchet@38291
   220
    case subgoal_count state of
wenzelm@40392
   221
      0 => Output.urgent_message "No subgoal!"
blanchet@41513
   222
    | n => case provers of
blanchet@41513
   223
             [] => error "No prover is set."
blanchet@41513
   224
           | prover :: _ =>
blanchet@41513
   225
             (kill_provers ();
blanchet@43905
   226
              minimize_facts prover params false i n state facts
blanchet@44102
   227
              |> (fn (_, (preplay, message, message_tail)) =>
blanchet@44102
   228
                     message (preplay ()) ^ message_tail
blanchet@44102
   229
                     |> Output.urgent_message))
blanchet@38291
   230
  end
blanchet@38291
   231
blanchet@35866
   232
end;