src/HOL/Tools/ATP_Manager/atp_minimal.ML
author wenzelm
Thu, 15 Oct 2009 00:55:29 +0200
changeset 32937 34f66c9dd8a2
parent 32936 9491bec20595
child 32941 72d48e333b77
permissions -rw-r--r--
structure ATP_Manager: eliminated slightly odd get/set operations in favour of Unsynchronized.ref; uniform interpretation of ATP_Manager.atps via ATP_Manager.get_atps;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
32327
0971cc0b6a57 src/HOL/Tools/ATP_Manager as separate component, with (almost) everything in one place;
wenzelm
parents: 32091
diff changeset
     1
(*  Title:      HOL/Tools/ATP_Manager/atp_minimal.ML
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
     2
    Author:     Philipp Meyer, TU Muenchen
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
     3
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
     4
Minimalization of theorem list for metis by using an external automated theorem prover
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
     5
*)
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
     6
32525
ea322e847633 added signature ATP_MINIMAL,
boehmes
parents: 32510
diff changeset
     7
signature ATP_MINIMAL =
ea322e847633 added signature ATP_MINIMAL,
boehmes
parents: 32510
diff changeset
     8
sig
32936
9491bec20595 modernized structure names;
wenzelm
parents: 32864
diff changeset
     9
  val minimalize: ATP_Wrapper.prover -> string -> int -> Proof.state ->
32571
d4bb776874b8 count number of iterations required for minimization (and fixed bug: minimization was always called)
nipkow
parents: 32538
diff changeset
    10
    (string * thm list) list -> ((string * thm list) list * int) option * string
32525
ea322e847633 added signature ATP_MINIMAL,
boehmes
parents: 32510
diff changeset
    11
end
ea322e847633 added signature ATP_MINIMAL,
boehmes
parents: 32510
diff changeset
    12
32936
9491bec20595 modernized structure names;
wenzelm
parents: 32864
diff changeset
    13
structure ATP_Minimal: ATP_MINIMAL =
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
    14
struct
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
    15
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    16
(* output control *)
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
    17
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    18
fun debug str = Output.debug (fn () => str)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    19
fun debug_fn f = if ! Output.debugging then f () else ()
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    20
fun answer str = Output.writeln str
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    21
fun println str = Output.priority str
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
    22
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    23
fun order_unique name_list = OrdList.make (String.collate Char.compare) name_list
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    24
fun length_string namelist = Int.toString (length namelist)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    25
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    26
fun print_names name_thms_pairs =
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    27
  let
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    28
    val names = map fst name_thms_pairs
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    29
    val ordered = order_unique names
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
    30
  in
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    31
    app (fn name => (debug ("  " ^ name))) ordered
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
    32
  end
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
    33
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
    34
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    35
(* minimalization algorithm *)
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
    36
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    37
local
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    38
  fun isplit (l,r) [] = (l,r)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    39
    | isplit (l,r) (h::[]) = (h::l, r)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    40
    | isplit (l,r) (h1::h2::t) = isplit (h1::l, h2::r) t
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    41
in
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    42
  fun split lst = isplit ([],[]) lst
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    43
end
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
    44
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    45
local
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    46
  fun min p sup [] = raise Empty
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    47
    | min p sup [s0] = [s0]
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    48
    | min p sup s =
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
    49
      let
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    50
        val (l0, r0) = split s
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
    51
      in
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    52
        if p (sup @ l0)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    53
        then min p sup l0
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
    54
        else
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    55
          if p (sup @ r0)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    56
          then min p sup r0
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    57
          else
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    58
            let
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    59
              val l = min p (sup @ r0) l0
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    60
              val r = min p (sup @ l) r0
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    61
            in
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    62
              l @ r
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    63
            end
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
    64
      end
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    65
in
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    66
  (* return a minimal subset v of s that satisfies p
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    67
   @pre p(s) & ~p([]) & monotone(p)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    68
   @post v subset s & p(v) &
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    69
         forall e in v. ~p(v \ e)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    70
   *)
32538
86035c5f61b5 Fixed "minimal" to cover the case that "p []" holds (excluded in the article by Bradley & Manna)
nipkow
parents: 32525
diff changeset
    71
  fun minimal p s =
32740
9dd0a2f83429 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32571
diff changeset
    72
    let val c = Unsynchronized.ref 0
32571
d4bb776874b8 count number of iterations required for minimization (and fixed bug: minimization was always called)
nipkow
parents: 32538
diff changeset
    73
        fun pc xs = (c := !c + 1; p xs)
d4bb776874b8 count number of iterations required for minimization (and fixed bug: minimization was always called)
nipkow
parents: 32538
diff changeset
    74
    in
d4bb776874b8 count number of iterations required for minimization (and fixed bug: minimization was always called)
nipkow
parents: 32538
diff changeset
    75
    (case min pc [] s of
d4bb776874b8 count number of iterations required for minimization (and fixed bug: minimization was always called)
nipkow
parents: 32538
diff changeset
    76
       [x] => if pc [] then [] else [x]
d4bb776874b8 count number of iterations required for minimization (and fixed bug: minimization was always called)
nipkow
parents: 32538
diff changeset
    77
     | m => m,
d4bb776874b8 count number of iterations required for minimization (and fixed bug: minimization was always called)
nipkow
parents: 32538
diff changeset
    78
     !c)
d4bb776874b8 count number of iterations required for minimization (and fixed bug: minimization was always called)
nipkow
parents: 32538
diff changeset
    79
    end
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    80
end
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    81
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
    82
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    83
(* failure check and producing answer *)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    84
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    85
datatype 'a prove_result = Success of 'a | Failure | Timeout | Error
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    86
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    87
val string_of_result =
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    88
  fn Success _ => "Success"
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    89
   | Failure => "Failure"
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    90
   | Timeout => "Timeout"
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    91
   | Error => "Error"
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    92
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    93
val failure_strings =
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    94
  [("SPASS beiseite: Ran out of time.", Timeout),
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    95
   ("Timeout", Timeout),
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    96
   ("time limit exceeded", Timeout),
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    97
   ("# Cannot determine problem status within resource limit", Timeout),
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    98
   ("Error", Error)]
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
    99
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   100
fun produce_answer result =
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   101
  let
32936
9491bec20595 modernized structure names;
wenzelm
parents: 32864
diff changeset
   102
    val ATP_Wrapper.Prover_Result {success, message, proof=result_string,
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   103
      internal_thm_names=thm_name_vec, filtered_clauses=filtered, ...} = result
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   104
  in
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   105
  if success then
31752
19a5f1c8a844 use results of relevance-filter to determine additional clauses;
immler@in.tum.de
parents: 31409
diff changeset
   106
    (Success (Vector.foldr op:: [] thm_name_vec, filtered), result_string)
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   107
  else
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
   108
    let
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   109
      val failure = failure_strings |> get_first (fn (s, t) =>
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   110
          if String.isSubstring s result_string then SOME (t, result_string) else NONE)
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
   111
    in
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   112
      if is_some failure then
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   113
        the failure
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   114
      else
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   115
        (Failure, result_string)
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
   116
    end
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   117
  end
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   118
32936
9491bec20595 modernized structure names;
wenzelm
parents: 32864
diff changeset
   119
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   120
(* wrapper for calling external prover *)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   121
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   122
fun sh_test_thms prover time_limit subgoalno state filtered name_thms_pairs =
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   123
  let
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   124
    val _ = println ("Testing " ^ (length_string name_thms_pairs) ^ " theorems... ")
32525
ea322e847633 added signature ATP_MINIMAL,
boehmes
parents: 32510
diff changeset
   125
    val name_thm_pairs = maps (fn (n, ths) => map (pair n) ths) name_thms_pairs
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   126
    val _ = debug_fn (fn () => print_names name_thm_pairs)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   127
    val axclauses = ResAxioms.cnf_rules_pairs (Proof.theory_of state) name_thm_pairs
32936
9491bec20595 modernized structure names;
wenzelm
parents: 32864
diff changeset
   128
    val problem = ATP_Wrapper.ATP_Problem {
32937
34f66c9dd8a2 structure ATP_Manager: eliminated slightly odd get/set operations in favour of Unsynchronized.ref;
wenzelm
parents: 32936
diff changeset
   129
      with_full_types = ! ATP_Manager.full_types,
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   130
      subgoal = subgoalno,
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   131
      goal = Proof.get_goal state,
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   132
      axiom_clauses = SOME axclauses,
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   133
      filtered_clauses = filtered }
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   134
    val (result, proof) = produce_answer (prover problem time_limit)
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   135
    val _ = println (string_of_result result)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   136
    val _ = debug proof
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   137
  in
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   138
    (result, proof)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   139
  end
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   140
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   141
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   142
(* minimalization of thms *)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   143
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   144
fun minimalize prover prover_name time_limit state name_thms_pairs =
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   145
  let
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   146
    val _ =
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   147
      println ("Minimize called with " ^ length_string name_thms_pairs ^ " theorems, prover: "
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   148
        ^ prover_name ^ ", time limit: " ^ Int.toString time_limit ^ " seconds")
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   149
    val _ = debug_fn (fn () => app (fn (n, tl) =>
32091
30e2ffbba718 proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents: 31752
diff changeset
   150
        (debug n; app (fn t =>
30e2ffbba718 proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents: 31752
diff changeset
   151
          debug ("  " ^ Display.string_of_thm (Proof.context_of state) t)) tl)) name_thms_pairs)
32864
a226f29d4bdc re-organized signature of AtpWrapper structure: records instead of unnamed parameters and return values,
boehmes
parents: 32740
diff changeset
   152
    val test_thms_fun = sh_test_thms prover time_limit 1 state
31752
19a5f1c8a844 use results of relevance-filter to determine additional clauses;
immler@in.tum.de
parents: 31409
diff changeset
   153
    fun test_thms filtered thms =
19a5f1c8a844 use results of relevance-filter to determine additional clauses;
immler@in.tum.de
parents: 31409
diff changeset
   154
      case test_thms_fun filtered thms of (Success _, _) => true | _ => false
32525
ea322e847633 added signature ATP_MINIMAL,
boehmes
parents: 32510
diff changeset
   155
    val answer' = pair and answer'' = pair NONE
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   156
  in
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   157
    (* try prove first to check result and get used theorems *)
31409
d8537ba165b5 split preparing clauses and writing problemfile;
immler@in.tum.de
parents: 31236
diff changeset
   158
    (case test_thms_fun NONE name_thms_pairs of
31752
19a5f1c8a844 use results of relevance-filter to determine additional clauses;
immler@in.tum.de
parents: 31409
diff changeset
   159
      (Success (used, filtered), _) =>
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   160
        let
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   161
          val ordered_used = order_unique used
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   162
          val to_use =
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   163
            if length ordered_used < length name_thms_pairs then
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   164
              filter (fn (name1, _) => List.exists (equal name1) ordered_used) name_thms_pairs
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   165
            else
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   166
              name_thms_pairs
32571
d4bb776874b8 count number of iterations required for minimization (and fixed bug: minimization was always called)
nipkow
parents: 32538
diff changeset
   167
          val (min_thms, n) = if null to_use then ([], 0)
32525
ea322e847633 added signature ATP_MINIMAL,
boehmes
parents: 32510
diff changeset
   168
            else minimal (test_thms (SOME filtered)) to_use
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   169
          val min_names = order_unique (map fst min_thms)
32571
d4bb776874b8 count number of iterations required for minimization (and fixed bug: minimization was always called)
nipkow
parents: 32538
diff changeset
   170
          val _ = println ("Interations: " ^ string_of_int n)
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   171
          val _ = println ("Minimal " ^ (length_string min_thms) ^ " theorems")
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   172
          val _ = debug_fn (fn () => print_names min_thms)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   173
        in
32571
d4bb776874b8 count number of iterations required for minimization (and fixed bug: minimization was always called)
nipkow
parents: 32538
diff changeset
   174
          answer' (SOME(min_thms,n)) ("Try this command: " ^
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   175
            Markup.markup Markup.sendback ("apply (metis " ^ space_implode " " min_names ^ ")"))
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   176
        end
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   177
    | (Timeout, _) =>
32525
ea322e847633 added signature ATP_MINIMAL,
boehmes
parents: 32510
diff changeset
   178
        answer'' ("Timeout: You may need to increase the time limit of " ^
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   179
          Int.toString time_limit ^ " seconds. Call atp_minimize [time=...] ")
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   180
    | (Error, msg) =>
32525
ea322e847633 added signature ATP_MINIMAL,
boehmes
parents: 32510
diff changeset
   181
        answer'' ("Error in prover: " ^ msg)
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   182
    | (Failure, _) =>
32525
ea322e847633 added signature ATP_MINIMAL,
boehmes
parents: 32510
diff changeset
   183
        answer'' "Failure: No proof with the theorems supplied")
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   184
    handle ResHolClause.TOO_TRIVIAL =>
32571
d4bb776874b8 count number of iterations required for minimization (and fixed bug: minimization was always called)
nipkow
parents: 32538
diff changeset
   185
        answer' (SOME ([],0)) ("Trivial: Try this command: " ^ Markup.markup Markup.sendback "apply metis")
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   186
    | ERROR msg =>
32525
ea322e847633 added signature ATP_MINIMAL,
boehmes
parents: 32510
diff changeset
   187
        answer'' ("Error: " ^ msg)
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   188
  end
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   189
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   190
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   191
(* Isar command and parsing input *)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   192
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   193
local structure K = OuterKeyword and P = OuterParse and T = OuterLex in
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   194
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   195
fun get_thms context =
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   196
  map (fn (name, interval) =>
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
   197
    let
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   198
      val thmref = Facts.Named ((name, Position.none), interval)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   199
      val ths = ProofContext.get_fact context thmref
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   200
      val name' = Facts.string_of_ref thmref
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
   201
    in
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   202
      (name', ths)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   203
    end)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   204
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   205
val default_prover = "remote_vampire"
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   206
val default_time_limit = 5
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
   207
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   208
fun get_time_limit_arg time_string =
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   209
  (case Int.fromString time_string of
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   210
    SOME t => t
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   211
  | NONE => error ("Invalid time limit: " ^ quote time_string))
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
   212
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   213
val get_options =
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   214
  let
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   215
    val def = (default_prover, default_time_limit)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   216
  in
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   217
    foldl (fn ((name, a), (p, t)) =>
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   218
      (case name of
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
   219
        "time" => (p, (get_time_limit_arg a))
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
   220
      | "atp" => (a, t)
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
   221
      | n => error ("Invalid argument: " ^ n))) def
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   222
  end
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
   223
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   224
fun sh_min_command args thm_names state =
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   225
  let
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   226
    val (prover_name, time_limit) = get_options args
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   227
    val prover =
32936
9491bec20595 modernized structure names;
wenzelm
parents: 32864
diff changeset
   228
      case ATP_Manager.get_prover prover_name (Proof.theory_of state) of
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   229
        SOME prover => prover
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   230
      | NONE => error ("Unknown prover: " ^ quote prover_name)
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   231
    val name_thms_pairs = get_thms (Proof.context_of state) thm_names
32525
ea322e847633 added signature ATP_MINIMAL,
boehmes
parents: 32510
diff changeset
   232
    fun print_answer (_, msg) = answer msg
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   233
  in
32525
ea322e847633 added signature ATP_MINIMAL,
boehmes
parents: 32510
diff changeset
   234
    print_answer (minimalize prover prover_name time_limit state name_thms_pairs)
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   235
  end
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
   236
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   237
val parse_args = Scan.optional (Args.bracks (P.list (P.xname --| P.$$$ "=" -- P.xname))) []
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   238
val parse_thm_names = Scan.repeat (P.xname -- Scan.option Attrib.thm_sel)
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
   239
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   240
val _ =
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   241
  OuterSyntax.command "atp_minimize" "minimize theorem list with external prover" K.diag
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   242
    (parse_args -- parse_thm_names >> (fn (args, thm_names) =>
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   243
      Toplevel.no_timing o Toplevel.unknown_proof o
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   244
        Toplevel.keep (sh_min_command args thm_names o Toplevel.proof_of)))
31037
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
   245
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
   246
end
ac8669134e7a added Philipp Meyer's implementation of AtpMinimal
immler@in.tum.de
parents:
diff changeset
   247
31236
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   248
end
2a1f5c87ac28 proper signature constraint;
wenzelm
parents: 31037
diff changeset
   249