src/HOL/Tools/Sledgehammer/sledgehammer_fact_minimizer.ML
author blanchet
Fri Apr 23 17:38:25 2010 +0200 (2010-04-23)
changeset 36375 2482446a604c
parent 36370 src/HOL/Tools/ATP_Manager/atp_minimal.ML@a4f601daa175
child 36378 f32c567dbcaa
permissions -rw-r--r--
move the minimizer to the Sledgehammer directory
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 = ATP_Manager.prover
blanchet@35867
    12
  type prover_result = ATP_Manager.prover_result
blanchet@35867
    13
blanchet@35866
    14
  val minimize_theorems :
blanchet@36223
    15
    params -> prover -> string -> int -> Proof.state -> (string * thm list) list
blanchet@35866
    16
    -> (string * thm list) list option * string
blanchet@35866
    17
end;
boehmes@32525
    18
blanchet@36375
    19
structure Sledgehammer_Fact_Minimizer : SLEDGEHAMMER_FACT_MINIMIZER =
immler@31037
    20
struct
immler@31037
    21
blanchet@36142
    22
open Sledgehammer_Util
blanchet@35866
    23
open Sledgehammer_Fact_Preprocessor
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@36370
    42
  | string_for_failure TimedOut = "Timed out."
blanchet@36370
    43
  | string_for_failure OutOfResources = "Failed."
blanchet@36370
    44
  | string_for_failure OldSpass = "Error."
blanchet@36370
    45
  | string_for_failure MalformedOutput = "Error."
blanchet@36370
    46
  | string_for_failure UnknownError = "Failed."
blanchet@36370
    47
fun string_for_outcome NONE = "Success."
blanchet@36370
    48
  | string_for_outcome (SOME failure) = string_for_failure failure
wenzelm@31236
    49
blanchet@35969
    50
fun sledgehammer_test_theorems (params as {full_types, ...} : params) prover
blanchet@36223
    51
        timeout subgoal state filtered_clauses name_thms_pairs =
wenzelm@31236
    52
  let
blanchet@36142
    53
    val num_theorems = length name_thms_pairs
blanchet@36142
    54
    val _ = priority ("Testing " ^ string_of_int num_theorems ^
blanchet@36142
    55
                      " theorem" ^ plural_s num_theorems ^ "...")
boehmes@32525
    56
    val name_thm_pairs = maps (fn (n, ths) => map (pair n) ths) name_thms_pairs
blanchet@35866
    57
    val axclauses = cnf_rules_pairs (Proof.theory_of state) name_thm_pairs
blanchet@36263
    58
    val {context = ctxt, facts, goal} = Proof.goal state
wenzelm@32941
    59
    val problem =
blanchet@36063
    60
     {subgoal = subgoal, goal = (ctxt, (facts, goal)),
blanchet@35969
    61
      relevance_override = {add = [], del = [], only = false},
blanchet@36232
    62
      axiom_clauses = SOME axclauses,
blanchet@36232
    63
      filtered_clauses = SOME (the_default axclauses filtered_clauses)}
blanchet@36223
    64
  in
blanchet@36370
    65
    prover params (K "") timeout problem
blanchet@36370
    66
    |> tap (priority o string_for_outcome o #outcome)
blanchet@36223
    67
  end
wenzelm@31236
    68
wenzelm@31236
    69
(* minimalization of thms *)
wenzelm@31236
    70
blanchet@36283
    71
fun minimize_theorems (params as {debug, minimize_timeout, isar_proof, modulus,
blanchet@36223
    72
                                  sorts, ...})
blanchet@36223
    73
                      prover atp_name i state name_thms_pairs =
wenzelm@31236
    74
  let
blanchet@35969
    75
    val msecs = Time.toMilliseconds minimize_timeout
blanchet@36224
    76
    val n = length name_thms_pairs
wenzelm@31236
    77
    val _ =
blanchet@36224
    78
      priority ("Sledgehammer minimizer: ATP " ^ quote atp_name ^
blanchet@36224
    79
                " with a time limit of " ^ string_of_int msecs ^ " ms.")
blanchet@35969
    80
    val test_thms_fun =
blanchet@36063
    81
      sledgehammer_test_theorems params prover minimize_timeout i state
immler@31752
    82
    fun test_thms filtered thms =
blanchet@36223
    83
      case test_thms_fun filtered thms of
blanchet@36370
    84
        (result as {outcome = NONE, ...}) => SOME result
blanchet@36223
    85
      | _ => NONE
blanchet@36223
    86
blanchet@36223
    87
    val {context = ctxt, facts, goal} = Proof.goal state;
blanchet@36223
    88
    val n = Logic.count_prems (prop_of goal)
wenzelm@31236
    89
  in
wenzelm@31236
    90
    (* try prove first to check result and get used theorems *)
immler@31409
    91
    (case test_thms_fun NONE name_thms_pairs of
blanchet@36370
    92
      result as {outcome = NONE, internal_thm_names, filtered_clauses, ...} =>
wenzelm@31236
    93
        let
blanchet@36223
    94
          val used = internal_thm_names |> Vector.foldr (op ::) []
blanchet@36223
    95
                                        |> sort_distinct string_ord
wenzelm@31236
    96
          val to_use =
blanchet@36223
    97
            if length used < length name_thms_pairs then
blanchet@36223
    98
              filter (fn (name1, _) => List.exists (curry (op =) name1) used)
blanchet@36223
    99
                     name_thms_pairs
wenzelm@33305
   100
            else name_thms_pairs
blanchet@36231
   101
          val (min_thms, {proof, internal_thm_names, ...}) =
blanchet@36223
   102
            linear_minimize (test_thms (SOME filtered_clauses)) to_use
blanchet@36223
   103
                            ([], result)
blanchet@36224
   104
          val n = length min_thms
wenzelm@32947
   105
          val _ = priority (cat_lines
blanchet@36224
   106
            ["Minimized: " ^ string_of_int n ^ " theorem" ^ plural_s n] ^ ".")
blanchet@36223
   107
        in
blanchet@36223
   108
          (SOME min_thms,
blanchet@36289
   109
           proof_text isar_proof debug modulus sorts ctxt
blanchet@36287
   110
                      (K "", proof, internal_thm_names, goal, i) |> fst)
blanchet@36223
   111
        end
blanchet@36370
   112
    | {outcome = SOME TimedOut, ...} =>
blanchet@36142
   113
        (NONE, "Timeout: You can increase the time limit using the \"timeout\" \
blanchet@36142
   114
               \option (e.g., \"timeout = " ^
blanchet@36142
   115
               string_of_int (10 + msecs div 1000) ^ " s\").")
blanchet@36370
   116
    | {outcome = SOME UnknownError, ...} =>
blanchet@36142
   117
        (* Failure sometimes mean timeout, unfortunately. *)
blanchet@36142
   118
        (NONE, "Failure: No proof was found with the current time limit. You \
blanchet@36142
   119
               \can increase the time limit using the \"timeout\" \
blanchet@36142
   120
               \option (e.g., \"timeout = " ^
blanchet@36370
   121
               string_of_int (10 + msecs div 1000) ^ " s\").")
blanchet@36370
   122
    | {message, ...} => (NONE, "ATP error: " ^ message))
blanchet@35865
   123
    handle Sledgehammer_HOL_Clause.TRIVIAL =>
blanchet@36063
   124
        (SOME [], metis_line i n [])
wenzelm@32947
   125
      | ERROR msg => (NONE, "Error: " ^ msg)
wenzelm@31236
   126
  end
wenzelm@31236
   127
blanchet@35866
   128
end;