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
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@36142
    21
open Sledgehammer_Util
blanchet@35866
    22
open Sledgehammer_Fact_Preprocessor
blanchet@36063
    23
open Sledgehammer_Proof_Reconstruct
blanchet@35867
    24
open ATP_Manager
blanchet@35866
    25
blanchet@35866
    26
(* Linear minimization algorithm *)
nipkow@33492
    27
blanchet@36223
    28
fun linear_minimize test s =
blanchet@35866
    29
  let
blanchet@36223
    30
    fun aux [] p = p
blanchet@36223
    31
      | aux (x :: xs) (needed, result) =
blanchet@36223
    32
        case test (xs @ needed) of
blanchet@36223
    33
          SOME result => aux xs (needed, result)
blanchet@36223
    34
        | NONE => aux xs (x :: needed, result)
blanchet@36223
    35
  in aux s end
wenzelm@31236
    36
immler@31037
    37
blanchet@36370
    38
(* wrapper for calling external prover *)
wenzelm@31236
    39
blanchet@36370
    40
fun string_for_failure Unprovable = "Unprovable."
blanchet@36370
    41
  | string_for_failure TimedOut = "Timed out."
blanchet@36370
    42
  | string_for_failure OutOfResources = "Failed."
blanchet@36370
    43
  | string_for_failure OldSpass = "Error."
blanchet@36370
    44
  | string_for_failure MalformedOutput = "Error."
blanchet@36370
    45
  | string_for_failure UnknownError = "Failed."
blanchet@36370
    46
fun string_for_outcome NONE = "Success."
blanchet@36370
    47
  | string_for_outcome (SOME failure) = string_for_failure failure
wenzelm@31236
    48
blanchet@35969
    49
fun sledgehammer_test_theorems (params as {full_types, ...} : params) prover
blanchet@36223
    50
        timeout subgoal state filtered_clauses name_thms_pairs =
wenzelm@31236
    51
  let
blanchet@36142
    52
    val num_theorems = length name_thms_pairs
blanchet@36142
    53
    val _ = priority ("Testing " ^ string_of_int num_theorems ^
blanchet@36142
    54
                      " theorem" ^ plural_s num_theorems ^ "...")
boehmes@32525
    55
    val name_thm_pairs = maps (fn (n, ths) => map (pair n) ths) name_thms_pairs
blanchet@35866
    56
    val axclauses = cnf_rules_pairs (Proof.theory_of state) name_thm_pairs
blanchet@36263
    57
    val {context = ctxt, facts, goal} = Proof.goal state
wenzelm@32941
    58
    val problem =
blanchet@36063
    59
     {subgoal = subgoal, goal = (ctxt, (facts, goal)),
blanchet@35969
    60
      relevance_override = {add = [], del = [], only = false},
blanchet@36232
    61
      axiom_clauses = SOME axclauses,
blanchet@36232
    62
      filtered_clauses = SOME (the_default axclauses filtered_clauses)}
blanchet@36223
    63
  in
blanchet@36370
    64
    prover params (K "") timeout problem
blanchet@36370
    65
    |> tap (priority o string_for_outcome o #outcome)
blanchet@36223
    66
  end
wenzelm@31236
    67
wenzelm@31236
    68
(* minimalization of thms *)
wenzelm@31236
    69
blanchet@36378
    70
fun minimize_theorems (params as {debug, atps, minimize_timeout, isar_proof,
blanchet@36488
    71
                                  shrink_factor, ...})
blanchet@36481
    72
                      i n state name_thms_pairs =
wenzelm@31236
    73
  let
blanchet@36378
    74
    val thy = Proof.theory_of state
blanchet@36378
    75
    val prover = case atps of
blanchet@36378
    76
                   [atp_name] => get_prover thy atp_name
blanchet@36378
    77
                 | _ => error "Expected a single ATP."
blanchet@35969
    78
    val msecs = Time.toMilliseconds minimize_timeout
wenzelm@31236
    79
    val _ =
blanchet@36378
    80
      priority ("Sledgehammer minimizer: ATP " ^ quote (the_single atps) ^
blanchet@36224
    81
                " with a time limit of " ^ string_of_int msecs ^ " ms.")
blanchet@35969
    82
    val test_thms_fun =
blanchet@36063
    83
      sledgehammer_test_theorems params prover minimize_timeout i state
immler@31752
    84
    fun test_thms filtered thms =
blanchet@36223
    85
      case test_thms_fun filtered thms of
blanchet@36370
    86
        (result as {outcome = NONE, ...}) => SOME result
blanchet@36223
    87
      | _ => NONE
blanchet@36223
    88
blanchet@36223
    89
    val {context = ctxt, facts, goal} = Proof.goal state;
wenzelm@31236
    90
  in
wenzelm@31236
    91
    (* try prove first to check result and get used theorems *)
immler@31409
    92
    (case test_thms_fun NONE name_thms_pairs of
blanchet@36402
    93
      result as {outcome = NONE, pool, internal_thm_names, conjecture_shape,
blanchet@36402
    94
                 filtered_clauses, ...} =>
wenzelm@31236
    95
        let
blanchet@36223
    96
          val used = internal_thm_names |> Vector.foldr (op ::) []
blanchet@36223
    97
                                        |> sort_distinct string_ord
wenzelm@31236
    98
          val to_use =
blanchet@36223
    99
            if length used < length name_thms_pairs then
blanchet@36393
   100
              filter (fn (name1, _) => exists (curry (op =) name1) used)
blanchet@36223
   101
                     name_thms_pairs
wenzelm@33305
   102
            else name_thms_pairs
blanchet@36231
   103
          val (min_thms, {proof, internal_thm_names, ...}) =
blanchet@36223
   104
            linear_minimize (test_thms (SOME filtered_clauses)) to_use
blanchet@36223
   105
                            ([], result)
blanchet@36481
   106
          val m = length min_thms
wenzelm@32947
   107
          val _ = priority (cat_lines
blanchet@36481
   108
            ["Minimized: " ^ string_of_int m ^ " theorem" ^ plural_s m] ^ ".")
blanchet@36223
   109
        in
blanchet@36223
   110
          (SOME min_thms,
blanchet@36402
   111
           proof_text isar_proof
blanchet@36488
   112
                      (pool, debug, shrink_factor, ctxt, conjecture_shape)
blanchet@36287
   113
                      (K "", proof, internal_thm_names, goal, i) |> fst)
blanchet@36223
   114
        end
blanchet@36370
   115
    | {outcome = SOME TimedOut, ...} =>
blanchet@36142
   116
        (NONE, "Timeout: You can increase the time limit using the \"timeout\" \
blanchet@36142
   117
               \option (e.g., \"timeout = " ^
blanchet@36142
   118
               string_of_int (10 + msecs div 1000) ^ " s\").")
blanchet@36370
   119
    | {outcome = SOME UnknownError, ...} =>
blanchet@36142
   120
        (* Failure sometimes mean timeout, unfortunately. *)
blanchet@36142
   121
        (NONE, "Failure: No proof was found with the current time limit. You \
blanchet@36142
   122
               \can increase the time limit using the \"timeout\" \
blanchet@36142
   123
               \option (e.g., \"timeout = " ^
blanchet@36370
   124
               string_of_int (10 + msecs div 1000) ^ " s\").")
blanchet@36370
   125
    | {message, ...} => (NONE, "ATP error: " ^ message))
blanchet@36382
   126
    handle Sledgehammer_HOL_Clause.TRIVIAL => (SOME [], metis_line i n [])
blanchet@36382
   127
         | ERROR msg => (NONE, "Error: " ^ msg)
wenzelm@31236
   128
  end
wenzelm@31236
   129
blanchet@35866
   130
end;