src/HOL/Tools/Sledgehammer/sledgehammer_fact_minimizer.ML
author blanchet
Tue Jun 22 14:28:22 2010 +0200 (2010-06-22 ago)
changeset 37498 b426cbdb5a23
parent 37479 f6b1ee5b420b
child 37506 32a1ee39c49b
permissions -rw-r--r--
removed Sledgehammer's support for the DFG syntax;
this removes 350 buggy lines from Sledgehammer. SPASS 3.5 and above support the TPTP syntax.
     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 IncompleteUnprovable = "Failed."
    42   | string_for_failure TimedOut = "Timed out."
    43   | string_for_failure OutOfResources = "Failed."
    44   | string_for_failure OldSpass = "Error."
    45   | string_for_failure MalformedOutput = "Error."
    46   | string_for_failure UnknownError = "Failed."
    47 fun string_for_outcome NONE = "Success."
    48   | string_for_outcome (SOME failure) = string_for_failure failure
    49 
    50 fun sledgehammer_test_theorems (params : params) prover timeout subgoal state
    51                                filtered_clauses name_thms_pairs =
    52   let
    53     val num_theorems = length name_thms_pairs
    54     val _ = priority ("Testing " ^ string_of_int num_theorems ^
    55                       " theorem" ^ plural_s num_theorems ^ "...")
    56     val name_thm_pairs = maps (fn (n, ths) => map (pair n) ths) name_thms_pairs
    57     val axclauses = cnf_rules_pairs (Proof.theory_of state) name_thm_pairs
    58     val {context = ctxt, facts, goal} = Proof.goal state
    59     val problem =
    60      {subgoal = subgoal, goal = (ctxt, (facts, goal)),
    61       relevance_override = {add = [], del = [], only = false},
    62       axiom_clauses = SOME axclauses,
    63       filtered_clauses = SOME (the_default axclauses filtered_clauses)}
    64   in
    65     prover params (K "") timeout problem
    66     |> tap (fn result : prover_result =>
    67          priority (string_for_outcome (#outcome result)))
    68   end
    69 
    70 (* minimalization of thms *)
    71 
    72 fun minimize_theorems (params as {debug, atps, full_types, minimize_timeout,
    73                                   isar_proof, isar_shrink_factor, ...})
    74                       i n state name_thms_pairs =
    75   let
    76     val thy = Proof.theory_of state
    77     val prover = case atps of
    78                    [atp_name] => get_prover thy atp_name
    79                  | _ => error "Expected a single ATP."
    80     val msecs = Time.toMilliseconds minimize_timeout
    81     val _ =
    82       priority ("Sledgehammer minimizer: ATP " ^ quote (the_single atps) ^
    83                 " with a time limit of " ^ string_of_int msecs ^ " ms.")
    84     val test_thms_fun =
    85       sledgehammer_test_theorems params prover minimize_timeout i state
    86     fun test_thms filtered thms =
    87       case test_thms_fun filtered thms of
    88         (result as {outcome = NONE, ...}) => SOME result
    89       | _ => NONE
    90 
    91     val {context = ctxt, goal, ...} = Proof.goal state;
    92   in
    93     (* try prove first to check result and get used theorems *)
    94     (case test_thms_fun NONE name_thms_pairs of
    95       result as {outcome = NONE, pool, internal_thm_names, conjecture_shape,
    96                  filtered_clauses, ...} =>
    97         let
    98           val used = internal_thm_names |> Vector.foldr (op ::) []
    99                                         |> sort_distinct string_ord
   100           val to_use =
   101             if length used < length name_thms_pairs then
   102               filter (fn (name1, _) => exists (curry (op =) name1) used)
   103                      name_thms_pairs
   104             else name_thms_pairs
   105           val (min_thms, {proof, internal_thm_names, ...}) =
   106             linear_minimize (test_thms (SOME filtered_clauses)) to_use
   107                             ([], result)
   108           val m = length min_thms
   109           val _ = priority (cat_lines
   110             ["Minimized: " ^ string_of_int m ^ " theorem" ^ plural_s m] ^ ".")
   111         in
   112           (SOME min_thms,
   113            proof_text isar_proof
   114                (pool, debug, isar_shrink_factor, ctxt, conjecture_shape)
   115                (full_types, K "", proof, internal_thm_names, goal, i) |> fst)
   116         end
   117     | {outcome = SOME TimedOut, ...} =>
   118         (NONE, "Timeout: You can increase the time limit using the \"timeout\" \
   119                \option (e.g., \"timeout = " ^
   120                string_of_int (10 + msecs div 1000) ^ " s\").")
   121     | {outcome = SOME UnknownError, ...} =>
   122         (* Failure sometimes mean timeout, unfortunately. *)
   123         (NONE, "Failure: No proof was found with the current time limit. You \
   124                \can increase the time limit using the \"timeout\" \
   125                \option (e.g., \"timeout = " ^
   126                string_of_int (10 + msecs div 1000) ^ " s\").")
   127     | {message, ...} => (NONE, "ATP error: " ^ message))
   128     handle Sledgehammer_HOL_Clause.TRIVIAL =>
   129            (SOME [], metis_line full_types i n [])
   130          | ERROR msg => (NONE, "Error: " ^ msg)
   131   end
   132 
   133 end;