src/HOL/Tools/Sledgehammer/sledgehammer_fact_minimize.ML
author blanchet
Wed, 25 Aug 2010 17:49:52 +0200
changeset 38983 2b6333f78a9e
parent 38980 7635bf8918a1
child 38984 ad577fd62ee4
permissions -rw-r--r--
make relevance filter work in term of a "max_relevant" option + use Vampire SOS;
"max_relevant" is more reliable than "max_relevant_per_iter";
also made sure that the option is monotone -- larger values should lead to more axioms -- which wasn't always the case before;
SOS for Vampire makes a difference of about 3% (i.e. 3% more proofs are found)
     1 (*  Title:      HOL/Tools/Sledgehammer/sledgehammer_fact_minimize.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_MINIMIZE =
     9 sig
    10   type params = Sledgehammer.params
    11 
    12   val minimize_theorems :
    13     params -> int -> int -> Proof.state -> ((string * bool) * thm list) list
    14     -> ((string * bool) * thm list) list option * string
    15   val run_minimize : params -> int -> Facts.ref list -> Proof.state -> unit
    16 end;
    17 
    18 structure Sledgehammer_Fact_Minimize : SLEDGEHAMMER_FACT_MINIMIZE =
    19 struct
    20 
    21 open ATP_Systems
    22 open Sledgehammer_Util
    23 open Sledgehammer_Fact_Filter
    24 open Sledgehammer_Proof_Reconstruct
    25 open Sledgehammer
    26 
    27 (* wrapper for calling external prover *)
    28 
    29 fun string_for_failure Unprovable = "Unprovable."
    30   | string_for_failure TimedOut = "Timed out."
    31   | string_for_failure _ = "Unknown error."
    32 
    33 fun n_theorems names =
    34   let val n = length names in
    35     string_of_int n ^ " theorem" ^ plural_s n ^
    36     (if n > 0 then
    37        ": " ^ (names |> map fst
    38                      |> sort_distinct string_ord |> space_implode " ")
    39      else
    40        "")
    41   end
    42 
    43 fun test_theorems ({debug, verbose, overlord, atps, full_types, isar_proof,
    44                     isar_shrink_factor, ...} : params)
    45                   (prover : prover) explicit_apply timeout subgoal state
    46                   axioms =
    47   let
    48     val _ =
    49       priority ("Testing " ^ n_theorems (map fst axioms) ^ "...")
    50     val params =
    51       {debug = debug, verbose = verbose, overlord = overlord, atps = atps,
    52        full_types = full_types, explicit_apply = explicit_apply,
    53        relevance_threshold = 1.1, relevance_decay = NONE, max_relevant = NONE,
    54        theory_relevant = NONE, isar_proof = isar_proof,
    55        isar_shrink_factor = isar_shrink_factor, timeout = timeout}
    56     val axioms = maps (fn (n, ths) => map (pair n) ths) axioms
    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       axioms = SOME axioms}
    62     val result as {outcome, used_thm_names, ...} = prover params (K "") problem
    63   in
    64     priority (case outcome of
    65                 NONE =>
    66                 if length used_thm_names = length axioms then
    67                   "Found proof."
    68                 else
    69                   "Found proof with " ^ n_theorems used_thm_names ^ "."
    70               | SOME failure => string_for_failure failure);
    71     result
    72   end
    73 
    74 (* minimalization of thms *)
    75 
    76 fun filter_used_facts used = filter (member (op =) used o fst)
    77 
    78 fun sublinear_minimize _ [] p = p
    79   | sublinear_minimize test (x :: xs) (seen, result) =
    80     case test (xs @ seen) of
    81       result as {outcome = NONE, proof, used_thm_names, ...} : prover_result =>
    82       sublinear_minimize test (filter_used_facts used_thm_names xs)
    83                          (filter_used_facts used_thm_names seen, result)
    84     | _ => sublinear_minimize test xs (x :: seen, result)
    85 
    86 (* Give the ATP some slack. The ATP gets further slack because the Sledgehammer
    87    preprocessing time is included in the estimate below but isn't part of the
    88    timeout. *)
    89 val fudge_msecs = 1000
    90 
    91 fun minimize_theorems {atps = [], ...} _ _ _ _ = error "No ATP is set."
    92   | minimize_theorems (params as {debug, atps = atp :: _, full_types,
    93                                   isar_proof, isar_shrink_factor, timeout, ...})
    94                       i n state axioms =
    95   let
    96     val thy = Proof.theory_of state
    97     val prover = get_prover_fun thy atp
    98     val msecs = Time.toMilliseconds timeout
    99     val _ = priority ("Sledgehammer minimize: ATP " ^ quote atp ^ ".")
   100     val {context = ctxt, goal, ...} = Proof.goal state
   101     val (_, hyp_ts, concl_t) = strip_subgoal goal i
   102     val explicit_apply =
   103       not (forall (Meson.is_fol_term thy)
   104                   (concl_t :: hyp_ts @ maps (map prop_of o snd) axioms))
   105     fun do_test timeout =
   106       test_theorems params prover explicit_apply timeout i state
   107     val timer = Timer.startRealTimer ()
   108   in
   109     (case do_test timeout axioms of
   110        result as {outcome = NONE, pool, used_thm_names,
   111                   conjecture_shape, ...} =>
   112        let
   113          val time = Timer.checkRealTimer timer
   114          val new_timeout =
   115            Int.min (msecs, Time.toMilliseconds time + fudge_msecs)
   116            |> Time.fromMilliseconds
   117          val (min_thms, {proof, axiom_names, ...}) =
   118            sublinear_minimize (do_test new_timeout)
   119                (filter_used_facts used_thm_names axioms) ([], result)
   120          val n = length min_thms
   121          val _ = priority (cat_lines
   122            ["Minimized: " ^ string_of_int n ^ " theorem" ^ plural_s n] ^
   123             (case length (filter (snd o fst) min_thms) of
   124                0 => ""
   125              | n => " (including " ^ Int.toString n ^ " chained)") ^ ".")
   126        in
   127          (SOME min_thms,
   128           proof_text isar_proof
   129               (pool, debug, isar_shrink_factor, ctxt, conjecture_shape)
   130               (full_types, K "", proof, axiom_names, goal, i) |> fst)
   131        end
   132      | {outcome = SOME TimedOut, ...} =>
   133        (NONE, "Timeout: You can increase the time limit using the \"timeout\" \
   134               \option (e.g., \"timeout = " ^
   135               string_of_int (10 + msecs div 1000) ^ " s\").")
   136      | {outcome = SOME UnknownError, ...} =>
   137        (* Failure sometimes mean timeout, unfortunately. *)
   138        (NONE, "Failure: No proof was found with the current time limit. You \
   139               \can increase the time limit using the \"timeout\" \
   140               \option (e.g., \"timeout = " ^
   141               string_of_int (10 + msecs div 1000) ^ " s\").")
   142      | {message, ...} => (NONE, "ATP error: " ^ message))
   143     handle ERROR msg => (NONE, "Error: " ^ msg)
   144   end
   145 
   146 fun run_minimize params i refs state =
   147   let
   148     val ctxt = Proof.context_of state
   149     val reserved = reserved_isar_keyword_table ()
   150     val chained_ths = #facts (Proof.goal state)
   151     val axioms =
   152       maps (map (fn (name, (_, th)) => (name (), [th]))
   153             o name_thm_pairs_from_ref ctxt reserved chained_ths) refs
   154   in
   155     case subgoal_count state of
   156       0 => priority "No subgoal!"
   157     | n =>
   158       (kill_atps (); priority (#2 (minimize_theorems params i n state axioms)))
   159   end
   160 
   161 end;