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