src/HOL/Tools/Sledgehammer/sledgehammer_fact_minimizer.ML
author blanchet
Tue Jun 29 10:25:53 2010 +0200 (2010-06-29)
changeset 37626 1146291fe718
parent 37578 9367cb36b1c4
child 37628 78334f400ae6
permissions -rw-r--r--
move blacklisting completely out of the clausifier;
the only reason it was in the clausifier as well was the Skolem cache
blanchet@36375
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_fact_minimizer.ML
immler@31037
     2
    Author:     Philipp Meyer, TU Muenchen
blanchet@36370
     3
    Author:     Jasmin Blanchette, TU Muenchen
immler@31037
     4
blanchet@35867
     5
Minimization of theorem list for Metis using automatic theorem provers.
immler@31037
     6
*)
immler@31037
     7
blanchet@36375
     8
signature SLEDGEHAMMER_FACT_MINIMIZER =
boehmes@32525
     9
sig
blanchet@35969
    10
  type params = ATP_Manager.params
blanchet@35867
    11
  type prover_result = ATP_Manager.prover_result
blanchet@35867
    12
blanchet@35866
    13
  val minimize_theorems :
blanchet@36481
    14
    params -> int -> int -> Proof.state -> (string * thm list) list
blanchet@35866
    15
    -> (string * thm list) list option * string
blanchet@35866
    16
end;
boehmes@32525
    17
blanchet@36375
    18
structure Sledgehammer_Fact_Minimizer : SLEDGEHAMMER_FACT_MINIMIZER =
immler@31037
    19
struct
immler@31037
    20
blanchet@37574
    21
open Clausifier
blanchet@37578
    22
open Metis_Clauses
blanchet@36142
    23
open Sledgehammer_Util
blanchet@36063
    24
open Sledgehammer_Proof_Reconstruct
blanchet@35867
    25
open ATP_Manager
blanchet@35866
    26
blanchet@35866
    27
(* Linear minimization algorithm *)
nipkow@33492
    28
blanchet@36223
    29
fun linear_minimize test s =
blanchet@35866
    30
  let
blanchet@36223
    31
    fun aux [] p = p
blanchet@36223
    32
      | aux (x :: xs) (needed, result) =
blanchet@36223
    33
        case test (xs @ needed) of
blanchet@36223
    34
          SOME result => aux xs (needed, result)
blanchet@36223
    35
        | NONE => aux xs (x :: needed, result)
blanchet@36223
    36
  in aux s end
wenzelm@31236
    37
immler@31037
    38
blanchet@36370
    39
(* wrapper for calling external prover *)
wenzelm@31236
    40
blanchet@36370
    41
fun string_for_failure Unprovable = "Unprovable."
blanchet@37418
    42
  | string_for_failure IncompleteUnprovable = "Failed."
blanchet@36370
    43
  | string_for_failure TimedOut = "Timed out."
blanchet@36370
    44
  | string_for_failure OutOfResources = "Failed."
blanchet@36370
    45
  | string_for_failure OldSpass = "Error."
blanchet@36370
    46
  | string_for_failure MalformedOutput = "Error."
blanchet@36370
    47
  | string_for_failure UnknownError = "Failed."
blanchet@36370
    48
fun string_for_outcome NONE = "Success."
blanchet@36370
    49
  | string_for_outcome (SOME failure) = string_for_failure failure
wenzelm@31236
    50
blanchet@37498
    51
fun sledgehammer_test_theorems (params : params) prover timeout subgoal state
blanchet@37498
    52
                               filtered_clauses name_thms_pairs =
wenzelm@31236
    53
  let
blanchet@36142
    54
    val num_theorems = length name_thms_pairs
blanchet@36142
    55
    val _ = priority ("Testing " ^ string_of_int num_theorems ^
blanchet@36142
    56
                      " theorem" ^ plural_s num_theorems ^ "...")
boehmes@32525
    57
    val name_thm_pairs = maps (fn (n, ths) => map (pair n) ths) name_thms_pairs
blanchet@35866
    58
    val axclauses = cnf_rules_pairs (Proof.theory_of state) name_thm_pairs
blanchet@36263
    59
    val {context = ctxt, facts, goal} = Proof.goal state
wenzelm@32941
    60
    val problem =
blanchet@36063
    61
     {subgoal = subgoal, goal = (ctxt, (facts, goal)),
blanchet@35969
    62
      relevance_override = {add = [], del = [], only = false},
blanchet@36232
    63
      axiom_clauses = SOME axclauses,
blanchet@36232
    64
      filtered_clauses = SOME (the_default axclauses filtered_clauses)}
blanchet@36223
    65
  in
blanchet@36370
    66
    prover params (K "") timeout problem
krauss@36607
    67
    |> tap (fn result : prover_result =>
krauss@36607
    68
         priority (string_for_outcome (#outcome result)))
blanchet@36223
    69
  end
wenzelm@31236
    70
wenzelm@31236
    71
(* minimalization of thms *)
wenzelm@31236
    72
blanchet@36909
    73
fun minimize_theorems (params as {debug, atps, full_types, minimize_timeout,
blanchet@36924
    74
                                  isar_proof, isar_shrink_factor, ...})
blanchet@36481
    75
                      i n state name_thms_pairs =
wenzelm@31236
    76
  let
blanchet@36378
    77
    val thy = Proof.theory_of state
blanchet@36378
    78
    val prover = case atps of
blanchet@36378
    79
                   [atp_name] => get_prover thy atp_name
blanchet@36378
    80
                 | _ => error "Expected a single ATP."
blanchet@35969
    81
    val msecs = Time.toMilliseconds minimize_timeout
wenzelm@31236
    82
    val _ =
blanchet@36378
    83
      priority ("Sledgehammer minimizer: ATP " ^ quote (the_single atps) ^
blanchet@36224
    84
                " with a time limit of " ^ string_of_int msecs ^ " ms.")
blanchet@35969
    85
    val test_thms_fun =
blanchet@36063
    86
      sledgehammer_test_theorems params prover minimize_timeout i state
immler@31752
    87
    fun test_thms filtered thms =
blanchet@36223
    88
      case test_thms_fun filtered thms of
blanchet@36370
    89
        (result as {outcome = NONE, ...}) => SOME result
blanchet@36223
    90
      | _ => NONE
blanchet@36223
    91
blanchet@37498
    92
    val {context = ctxt, goal, ...} = Proof.goal state;
wenzelm@31236
    93
  in
wenzelm@31236
    94
    (* try prove first to check result and get used theorems *)
immler@31409
    95
    (case test_thms_fun NONE name_thms_pairs of
blanchet@36402
    96
      result as {outcome = NONE, pool, internal_thm_names, conjecture_shape,
blanchet@36402
    97
                 filtered_clauses, ...} =>
wenzelm@31236
    98
        let
blanchet@36223
    99
          val used = internal_thm_names |> Vector.foldr (op ::) []
blanchet@36223
   100
                                        |> sort_distinct string_ord
wenzelm@31236
   101
          val to_use =
blanchet@36223
   102
            if length used < length name_thms_pairs then
blanchet@36393
   103
              filter (fn (name1, _) => exists (curry (op =) name1) used)
blanchet@36223
   104
                     name_thms_pairs
wenzelm@33305
   105
            else name_thms_pairs
blanchet@36231
   106
          val (min_thms, {proof, internal_thm_names, ...}) =
blanchet@36223
   107
            linear_minimize (test_thms (SOME filtered_clauses)) to_use
blanchet@36223
   108
                            ([], result)
blanchet@36481
   109
          val m = length min_thms
wenzelm@32947
   110
          val _ = priority (cat_lines
blanchet@36481
   111
            ["Minimized: " ^ string_of_int m ^ " theorem" ^ plural_s m] ^ ".")
blanchet@36223
   112
        in
blanchet@36223
   113
          (SOME min_thms,
blanchet@36402
   114
           proof_text isar_proof
blanchet@37479
   115
               (pool, debug, isar_shrink_factor, ctxt, conjecture_shape)
blanchet@37479
   116
               (full_types, K "", proof, internal_thm_names, goal, i) |> fst)
blanchet@36223
   117
        end
blanchet@36370
   118
    | {outcome = SOME TimedOut, ...} =>
blanchet@36142
   119
        (NONE, "Timeout: You can increase the time limit using the \"timeout\" \
blanchet@36142
   120
               \option (e.g., \"timeout = " ^
blanchet@36142
   121
               string_of_int (10 + msecs div 1000) ^ " s\").")
blanchet@36370
   122
    | {outcome = SOME UnknownError, ...} =>
blanchet@36142
   123
        (* Failure sometimes mean timeout, unfortunately. *)
blanchet@36142
   124
        (NONE, "Failure: No proof was found with the current time limit. You \
blanchet@36142
   125
               \can increase the time limit using the \"timeout\" \
blanchet@36142
   126
               \option (e.g., \"timeout = " ^
blanchet@36370
   127
               string_of_int (10 + msecs div 1000) ^ " s\").")
blanchet@36370
   128
    | {message, ...} => (NONE, "ATP error: " ^ message))
blanchet@37506
   129
    handle TRIVIAL () => (SOME [], metis_line full_types i n [])
blanchet@36382
   130
         | ERROR msg => (NONE, "Error: " ^ msg)
wenzelm@31236
   131
  end
wenzelm@31236
   132
blanchet@35866
   133
end;