src/HOL/Tools/ATP_Manager/atp_minimal.ML
author nipkow
Fri, 06 Nov 2009 19:22:32 +0100
changeset 33492 4168294a9f96
parent 33324 6a72af4e84b8
child 35596 768d17f54125
permissions -rw-r--r--
Command atp_minimize uses the naive linear algorithm now
because the binary chop one had turned out to be a little bit suboptimal.
Internally the binary chop one is still available.
wenzelm@32327
     1
(*  Title:      HOL/Tools/ATP_Manager/atp_minimal.ML
immler@31037
     2
    Author:     Philipp Meyer, TU Muenchen
immler@31037
     3
nipkow@33492
     4
Minimization of theorem list for metis by using an external automated theorem prover
immler@31037
     5
*)
immler@31037
     6
boehmes@32525
     7
signature ATP_MINIMAL =
boehmes@32525
     8
sig
nipkow@33492
     9
  val minimize: ATP_Wrapper.prover -> string -> int -> Proof.state ->
nipkow@33492
    10
    (string * thm list) list -> ((string * thm list) list * int) option * string
nipkow@33492
    11
  (* To be removed once TN has finished his measurements;
nipkow@33492
    12
     the int component of the result should then be removed: *)
wenzelm@32936
    13
  val minimalize: ATP_Wrapper.prover -> string -> int -> Proof.state ->
nipkow@32571
    14
    (string * thm list) list -> ((string * thm list) list * int) option * string
boehmes@32525
    15
end
boehmes@32525
    16
wenzelm@32936
    17
structure ATP_Minimal: ATP_MINIMAL =
immler@31037
    18
struct
immler@31037
    19
nipkow@33492
    20
(* Linear minimization *)
nipkow@33492
    21
nipkow@33492
    22
fun lin_gen_minimize p s =
nipkow@33492
    23
let
nipkow@33492
    24
  fun min [] needed = needed
nipkow@33492
    25
    | min (x::xs) needed =
nipkow@33492
    26
        if p(xs @ needed) then min xs needed else min xs (x::needed)
nipkow@33492
    27
in (min s [], length s) end;
nipkow@33492
    28
nipkow@33492
    29
(* Clever minimalization algorithm *)
wenzelm@31236
    30
wenzelm@31236
    31
local
wenzelm@32947
    32
  fun isplit (l, r) [] = (l, r)
wenzelm@32947
    33
    | isplit (l, r) [h] = (h :: l, r)
wenzelm@32947
    34
    | isplit (l, r) (h1 :: h2 :: t) = isplit (h1 :: l, h2 :: r) t
wenzelm@31236
    35
in
wenzelm@32947
    36
  fun split lst = isplit ([], []) lst
wenzelm@31236
    37
end
wenzelm@31236
    38
wenzelm@31236
    39
local
wenzelm@31236
    40
  fun min p sup [] = raise Empty
wenzelm@31236
    41
    | min p sup [s0] = [s0]
wenzelm@31236
    42
    | min p sup s =
wenzelm@31236
    43
      let
wenzelm@31236
    44
        val (l0, r0) = split s
wenzelm@31236
    45
      in
wenzelm@31236
    46
        if p (sup @ l0)
wenzelm@31236
    47
        then min p sup l0
wenzelm@31236
    48
        else
wenzelm@31236
    49
          if p (sup @ r0)
wenzelm@31236
    50
          then min p sup r0
wenzelm@31236
    51
          else
wenzelm@31236
    52
            let
wenzelm@31236
    53
              val l = min p (sup @ r0) l0
wenzelm@31236
    54
              val r = min p (sup @ l) r0
wenzelm@31236
    55
            in
wenzelm@31236
    56
              l @ r
wenzelm@31236
    57
            end
wenzelm@31236
    58
      end
wenzelm@31236
    59
in
wenzelm@31236
    60
  (* return a minimal subset v of s that satisfies p
wenzelm@31236
    61
   @pre p(s) & ~p([]) & monotone(p)
wenzelm@31236
    62
   @post v subset s & p(v) &
wenzelm@31236
    63
         forall e in v. ~p(v \ e)
wenzelm@31236
    64
   *)
nipkow@32538
    65
  fun minimal p s =
wenzelm@32947
    66
    let
wenzelm@32947
    67
      val count = Unsynchronized.ref 0
wenzelm@32947
    68
      fun p_count xs = (Unsynchronized.inc count; p xs)
wenzelm@32947
    69
      val v =
wenzelm@32947
    70
        (case min p_count [] s of
wenzelm@32947
    71
          [x] => if p_count [] then [] else [x]
wenzelm@32947
    72
        | m => m);
wenzelm@32947
    73
    in (v, ! count) end
wenzelm@31236
    74
end
wenzelm@31236
    75
wenzelm@31236
    76
wenzelm@31236
    77
(* failure check and producing answer *)
wenzelm@31236
    78
wenzelm@31236
    79
datatype 'a prove_result = Success of 'a | Failure | Timeout | Error
wenzelm@31236
    80
wenzelm@31236
    81
val string_of_result =
wenzelm@31236
    82
  fn Success _ => "Success"
wenzelm@31236
    83
   | Failure => "Failure"
wenzelm@31236
    84
   | Timeout => "Timeout"
wenzelm@31236
    85
   | Error => "Error"
wenzelm@31236
    86
wenzelm@31236
    87
val failure_strings =
wenzelm@31236
    88
  [("SPASS beiseite: Ran out of time.", Timeout),
wenzelm@31236
    89
   ("Timeout", Timeout),
wenzelm@31236
    90
   ("time limit exceeded", Timeout),
wenzelm@31236
    91
   ("# Cannot determine problem status within resource limit", Timeout),
wenzelm@31236
    92
   ("Error", Error)]
wenzelm@31236
    93
wenzelm@32941
    94
fun produce_answer (result: ATP_Wrapper.prover_result) =
boehmes@32864
    95
  let
wenzelm@32942
    96
    val {success, proof = result_string, internal_thm_names = thm_name_vec,
wenzelm@32942
    97
      filtered_clauses = filtered, ...} = result
boehmes@32864
    98
  in
wenzelm@32947
    99
    if success then
wenzelm@32947
   100
      (Success (Vector.foldr (op ::) [] thm_name_vec, filtered), result_string)
wenzelm@32947
   101
    else
wenzelm@32947
   102
      let
wenzelm@32947
   103
        val failure = failure_strings |> get_first (fn (s, t) =>
wenzelm@32947
   104
            if String.isSubstring s result_string then SOME (t, result_string) else NONE)
wenzelm@32947
   105
      in
wenzelm@32947
   106
        (case failure of
wenzelm@32947
   107
          SOME res => res
wenzelm@32947
   108
        | NONE => (Failure, result_string))
wenzelm@32947
   109
      end
boehmes@32864
   110
  end
wenzelm@31236
   111
wenzelm@32936
   112
wenzelm@31236
   113
(* wrapper for calling external prover *)
wenzelm@31236
   114
boehmes@32864
   115
fun sh_test_thms prover time_limit subgoalno state filtered name_thms_pairs =
wenzelm@31236
   116
  let
wenzelm@32947
   117
    val _ = priority ("Testing " ^ string_of_int (length name_thms_pairs) ^ " theorems... ")
boehmes@32525
   118
    val name_thm_pairs = maps (fn (n, ths) => map (pair n) ths) name_thms_pairs
wenzelm@33324
   119
    val axclauses = Res_Axioms.cnf_rules_pairs (Proof.theory_of state) name_thm_pairs
wenzelm@33292
   120
    val {context = ctxt, facts, goal} = Proof.raw_goal state   (* FIXME ?? *)
wenzelm@32941
   121
    val problem =
wenzelm@32941
   122
     {with_full_types = ! ATP_Manager.full_types,
boehmes@32864
   123
      subgoal = subgoalno,
wenzelm@33292
   124
      goal = (ctxt, (facts, goal)),
boehmes@32864
   125
      axiom_clauses = SOME axclauses,
wenzelm@32941
   126
      filtered_clauses = filtered}
wenzelm@32948
   127
    val (result, proof) = produce_answer (prover time_limit problem)
wenzelm@32947
   128
    val _ = priority (string_of_result result)
immler@31037
   129
  in
wenzelm@31236
   130
    (result, proof)
immler@31037
   131
  end
immler@31037
   132
wenzelm@31236
   133
wenzelm@31236
   134
(* minimalization of thms *)
wenzelm@31236
   135
nipkow@33492
   136
fun gen_minimalize gen_min prover prover_name time_limit state name_thms_pairs =
wenzelm@31236
   137
  let
wenzelm@31236
   138
    val _ =
wenzelm@32947
   139
      priority ("Minimize called with " ^ string_of_int (length name_thms_pairs) ^
wenzelm@32947
   140
        " theorems, prover: " ^ prover_name ^
wenzelm@32947
   141
        ", time limit: " ^ string_of_int time_limit ^ " seconds")
boehmes@32864
   142
    val test_thms_fun = sh_test_thms prover time_limit 1 state
immler@31752
   143
    fun test_thms filtered thms =
immler@31752
   144
      case test_thms_fun filtered thms of (Success _, _) => true | _ => false
wenzelm@31236
   145
  in
wenzelm@31236
   146
    (* try prove first to check result and get used theorems *)
immler@31409
   147
    (case test_thms_fun NONE name_thms_pairs of
immler@31752
   148
      (Success (used, filtered), _) =>
immler@31037
   149
        let
wenzelm@32947
   150
          val ordered_used = sort_distinct string_ord used
wenzelm@31236
   151
          val to_use =
wenzelm@31236
   152
            if length ordered_used < length name_thms_pairs then
wenzelm@31236
   153
              filter (fn (name1, _) => List.exists (equal name1) ordered_used) name_thms_pairs
wenzelm@33313
   154
            else name_thms_pairs
wenzelm@33313
   155
          val (min_thms, n) =
wenzelm@33313
   156
            if null to_use then ([], 0)
nipkow@33492
   157
            else gen_min (test_thms (SOME filtered)) to_use
wenzelm@32947
   158
          val min_names = sort_distinct string_ord (map fst min_thms)
wenzelm@32947
   159
          val _ = priority (cat_lines
nipkow@33492
   160
            ["Iterations: " ^ string_of_int n (* FIXME TN remove later *),
nipkow@33492
   161
             "Minimal " ^ string_of_int (length min_thms) ^ " theorems"])
immler@31037
   162
        in
wenzelm@32947
   163
          (SOME (min_thms, n), "Try this command: " ^
wenzelm@31236
   164
            Markup.markup Markup.sendback ("apply (metis " ^ space_implode " " min_names ^ ")"))
immler@31037
   165
        end
wenzelm@31236
   166
    | (Timeout, _) =>
wenzelm@32947
   167
        (NONE, "Timeout: You may need to increase the time limit of " ^
wenzelm@32947
   168
          string_of_int time_limit ^ " seconds. Call atp_minimize [time=...] ")
wenzelm@31236
   169
    | (Error, msg) =>
wenzelm@32947
   170
        (NONE, "Error in prover: " ^ msg)
wenzelm@31236
   171
    | (Failure, _) =>
wenzelm@32947
   172
        (NONE, "Failure: No proof with the theorems supplied"))
wenzelm@33324
   173
    handle Res_HOL_Clause.TOO_TRIVIAL =>
wenzelm@32947
   174
        (SOME ([], 0), "Trivial: Try this command: " ^ Markup.markup Markup.sendback "apply metis")
wenzelm@32947
   175
      | ERROR msg => (NONE, "Error: " ^ msg)
immler@31037
   176
  end
immler@31037
   177
immler@31037
   178
wenzelm@31236
   179
(* Isar command and parsing input *)
immler@31037
   180
wenzelm@31236
   181
local structure K = OuterKeyword and P = OuterParse and T = OuterLex in
immler@31037
   182
wenzelm@31236
   183
fun get_thms context =
wenzelm@31236
   184
  map (fn (name, interval) =>
wenzelm@31236
   185
    let
wenzelm@31236
   186
      val thmref = Facts.Named ((name, Position.none), interval)
wenzelm@31236
   187
      val ths = ProofContext.get_fact context thmref
wenzelm@31236
   188
      val name' = Facts.string_of_ref thmref
wenzelm@31236
   189
    in
wenzelm@31236
   190
      (name', ths)
wenzelm@31236
   191
    end)
immler@31037
   192
wenzelm@31236
   193
val default_prover = "remote_vampire"
wenzelm@31236
   194
val default_time_limit = 5
immler@31037
   195
wenzelm@31236
   196
fun get_time_limit_arg time_string =
wenzelm@31236
   197
  (case Int.fromString time_string of
wenzelm@31236
   198
    SOME t => t
wenzelm@31236
   199
  | NONE => error ("Invalid time limit: " ^ quote time_string))
immler@31037
   200
wenzelm@32947
   201
fun get_opt (name, a) (p, t) =
wenzelm@32947
   202
  (case name of
wenzelm@32947
   203
    "time" => (p, get_time_limit_arg a)
wenzelm@32947
   204
  | "atp" => (a, t)
wenzelm@32947
   205
  | n => error ("Invalid argument: " ^ n))
wenzelm@32947
   206
wenzelm@32947
   207
fun get_options args = fold get_opt args (default_prover, default_time_limit)
immler@31037
   208
nipkow@33492
   209
val minimize = gen_minimalize lin_gen_minimize
nipkow@33492
   210
nipkow@33492
   211
val minimalize = gen_minimalize minimal
nipkow@33492
   212
wenzelm@31236
   213
fun sh_min_command args thm_names state =
wenzelm@31236
   214
  let
wenzelm@31236
   215
    val (prover_name, time_limit) = get_options args
wenzelm@31236
   216
    val prover =
wenzelm@32947
   217
      (case ATP_Manager.get_prover (Proof.theory_of state) prover_name of
wenzelm@31236
   218
        SOME prover => prover
wenzelm@32947
   219
      | NONE => error ("Unknown prover: " ^ quote prover_name))
wenzelm@31236
   220
    val name_thms_pairs = get_thms (Proof.context_of state) thm_names
wenzelm@31236
   221
  in
nipkow@33492
   222
    writeln (#2 (minimize prover prover_name time_limit state name_thms_pairs))
wenzelm@31236
   223
  end
immler@31037
   224
nipkow@33492
   225
val parse_args =
nipkow@33492
   226
  Scan.optional (Args.bracks (P.list (P.xname --| P.$$$ "=" -- P.xname))) []
wenzelm@31236
   227
val parse_thm_names = Scan.repeat (P.xname -- Scan.option Attrib.thm_sel)
immler@31037
   228
wenzelm@31236
   229
val _ =
wenzelm@31236
   230
  OuterSyntax.command "atp_minimize" "minimize theorem list with external prover" K.diag
wenzelm@31236
   231
    (parse_args -- parse_thm_names >> (fn (args, thm_names) =>
wenzelm@31236
   232
      Toplevel.no_timing o Toplevel.unknown_proof o
wenzelm@31236
   233
        Toplevel.keep (sh_min_command args thm_names o Toplevel.proof_of)))
immler@31037
   234
immler@31037
   235
end
immler@31037
   236
wenzelm@31236
   237
end