src/HOL/Tools/try_methods.ML
author nipkow
Mon, 12 Sep 2011 07:55:43 +0200
changeset 45761 22f665a2e91c
parent 45515 5d6a11e166cf
child 46537 d83797ef0d2d
permissions -rw-r--r--
new fastforce replacing fastsimp - less confusing name
blanchet@43857
     1
(*  Title:      HOL/Tools/try_methods.ML
blanchet@39168
     2
    Author:     Jasmin Blanchette, TU Muenchen
blanchet@39168
     3
blanchet@39168
     4
Try a combination of proof methods.
blanchet@39168
     5
*)
blanchet@39168
     6
blanchet@43857
     7
signature TRY_METHODS =
blanchet@39168
     8
sig
blanchet@43861
     9
  val try_methodsN : string
blanchet@43861
    10
  val noneN : string
blanchet@39577
    11
  val auto : bool Unsynchronized.ref
blanchet@43857
    12
  val try_methods :
blanchet@43050
    13
    Time.time option -> string list * string list * string list * string list
blanchet@43050
    14
    -> Proof.state -> bool
blanchet@39577
    15
  val setup : theory -> theory
blanchet@39168
    16
end;
blanchet@39168
    17
blanchet@43857
    18
structure Try_Methods : TRY_METHODS =
blanchet@39168
    19
struct
blanchet@39168
    20
blanchet@43867
    21
datatype mode = Auto_Try | Try | Normal
blanchet@43867
    22
blanchet@43861
    23
val try_methodsN = "try_methods"
blanchet@43861
    24
blanchet@43861
    25
val noneN = "none"
blanchet@43861
    26
blanchet@39577
    27
val auto = Unsynchronized.ref false
blanchet@39577
    28
blanchet@39579
    29
val _ =
blanchet@39579
    30
  ProofGeneralPgip.add_preference Preferences.category_tracing
blanchet@43857
    31
      (Preferences.bool_pref auto "auto-try-methods"
blanchet@43857
    32
                             "Try standard proof methods.")
blanchet@39579
    33
wenzelm@40553
    34
val default_timeout = seconds 5.0
blanchet@39170
    35
blanchet@39582
    36
fun can_apply timeout_opt pre post tac st =
blanchet@39168
    37
  let val {goal, ...} = Proof.goal st in
blanchet@39582
    38
    case (case timeout_opt of
blanchet@39582
    39
            SOME timeout => TimeLimit.timeLimit timeout
blanchet@39582
    40
          | NONE => fn f => fn x => f x) (Seq.pull o tac) (pre st) of
blanchet@39168
    41
      SOME (x, _) => nprems_of (post x) < nprems_of goal
blanchet@39168
    42
    | NONE => false
blanchet@39168
    43
  end
blanchet@40357
    44
  handle TimeLimit.TimeOut => false
blanchet@39168
    45
blanchet@39582
    46
fun do_generic timeout_opt command pre post apply st =
blanchet@39168
    47
  let val timer = Timer.startRealTimer () in
blanchet@39582
    48
    if can_apply timeout_opt pre post apply st then
blanchet@39168
    49
      SOME (command, Time.toMilliseconds (Timer.checkRealTimer timer))
blanchet@39168
    50
    else
blanchet@39168
    51
      NONE
blanchet@39168
    52
  end
blanchet@39168
    53
blanchet@42870
    54
val parse_method =
blanchet@42870
    55
  enclose "(" ")"
blanchet@42870
    56
  #> Outer_Syntax.scan Position.start
blanchet@42870
    57
  #> filter Token.is_proper
blanchet@42870
    58
  #> Scan.read Token.stopper Method.parse
blanchet@42870
    59
  #> (fn SOME (Method.Source src) => src | _ => raise Fail "expected Source")
blanchet@39168
    60
blanchet@42870
    61
fun apply_named_method_on_first_goal method thy =
blanchet@42870
    62
  method |> parse_method
blanchet@42870
    63
         |> Method.method thy
blanchet@42870
    64
         |> Method.Basic
blanchet@42870
    65
         |> curry Method.SelectGoals 1
blanchet@42870
    66
         |> Proof.refine
blanchet@40463
    67
  handle ERROR _ => K Seq.empty (* e.g., the method isn't available yet *)
blanchet@39168
    68
blanchet@42870
    69
fun add_attr_text (NONE, _) s = s
blanchet@42870
    70
  | add_attr_text (_, []) s = s
blanchet@42870
    71
  | add_attr_text (SOME x, fs) s =
blanchet@42870
    72
    s ^ " " ^ (if x = "" then "" else x ^ ": ") ^ space_implode " " fs
blanchet@43050
    73
fun attrs_text (sx, ix, ex, dx) (ss, is, es, ds) =
blanchet@43050
    74
  "" |> fold add_attr_text [(sx, ss), (ix, is), (ex, es), (dx, ds)]
blanchet@42870
    75
blanchet@43867
    76
fun do_named_method (name, ((all_goals, run_if_auto_try), attrs)) mode
blanchet@43867
    77
                    timeout_opt quad st =
blanchet@43867
    78
  if mode <> Auto_Try orelse run_if_auto_try then
blanchet@43050
    79
    let val attrs = attrs_text attrs quad in
blanchet@42870
    80
      do_generic timeout_opt
blanchet@42870
    81
                 (name ^ (if all_goals andalso
blanchet@42870
    82
                             nprems_of (#goal (Proof.goal st)) > 1 then
blanchet@42870
    83
                            "[1]"
blanchet@42870
    84
                          else
blanchet@42870
    85
                            "") ^
blanchet@42870
    86
                  attrs) I (#goal o Proof.goal)
blanchet@42870
    87
                 (apply_named_method_on_first_goal (name ^ attrs)
blanchet@42870
    88
                                                   (Proof.theory_of st)) st
blanchet@42870
    89
    end
blanchet@40463
    90
  else
blanchet@40463
    91
    NONE
blanchet@39168
    92
blanchet@43050
    93
val full_attrs = (SOME "simp", SOME "intro", SOME "elim", SOME "dest")
blanchet@43050
    94
val clas_attrs = (NONE, SOME "intro", SOME "elim", SOME "dest")
blanchet@43050
    95
val simp_attrs = (SOME "add", NONE, NONE, NONE)
blanchet@43050
    96
val metis_attrs = (SOME "", SOME "", SOME "", SOME "")
blanchet@43050
    97
val no_attrs = (NONE, NONE, NONE, NONE)
blanchet@42870
    98
blanchet@43867
    99
(* name * ((all_goals, run_if_auto_try), (simp, intro, elim, dest) *)
blanchet@39769
   100
val named_methods =
blanchet@42870
   101
  [("simp", ((false, true), simp_attrs)),
blanchet@42870
   102
   ("auto", ((true, true), full_attrs)),
blanchet@42870
   103
   ("fast", ((false, false), clas_attrs)),
nipkow@45761
   104
   ("fastforce", ((false, false), full_attrs)),
blanchet@42870
   105
   ("force", ((false, false), full_attrs)),
blanchet@42870
   106
   ("blast", ((false, true), clas_attrs)),
blanchet@42870
   107
   ("metis", ((false, true), metis_attrs)),
blanchet@42870
   108
   ("linarith", ((false, true), no_attrs)),
blanchet@42870
   109
   ("presburger", ((false, true), no_attrs))]
blanchet@39769
   110
val do_methods = map do_named_method named_methods
blanchet@39168
   111
blanchet@39168
   112
fun time_string (s, ms) = s ^ ": " ^ string_of_int ms ^ " ms"
blanchet@39168
   113
blanchet@43867
   114
fun do_try_methods mode timeout_opt quad st =
blanchet@41286
   115
  let
blanchet@45515
   116
    val st = st |> Proof.map_context (Config.put Metis_Tactic.verbose false #>
boehmes@44463
   117
      Config.put Lin_Arith.verbose false)
blanchet@41286
   118
  in
blanchet@43867
   119
    if mode = Normal then
blanchet@43867
   120
      "Trying " ^ space_implode " " (Try.serial_commas "and"
blanchet@43867
   121
                                      (map (quote o fst) named_methods)) ^ "..."
blanchet@43867
   122
      |> Output.urgent_message
blanchet@43867
   123
    else
blanchet@43867
   124
      ();
blanchet@43867
   125
    case do_methods |> Par_List.map (fn f => f mode timeout_opt quad st)
blanchet@41286
   126
                    |> map_filter I |> sort (int_ord o pairself snd) of
blanchet@43867
   127
      [] =>
blanchet@43867
   128
      (if mode = Normal then Output.urgent_message "No proof found." else ();
blanchet@43867
   129
       (false, (noneN, st)))
blanchet@41286
   130
    | xs as (s, _) :: _ =>
blanchet@41286
   131
      let
blanchet@42870
   132
        val xs = xs |> map (fn (s, n) => (n, hd (space_explode " " s)))
blanchet@42870
   133
                    |> AList.coalesce (op =)
blanchet@41286
   134
                    |> map (swap o apsnd commas)
blanchet@42870
   135
        val need_parens = exists_string (curry (op =) " ") s
blanchet@41286
   136
        val message =
blanchet@43867
   137
          (case mode of
blanchet@43867
   138
             Auto_Try => "Auto Try Methods found a proof"
blanchet@43867
   139
           | Try => "Try Methods found a proof"
blanchet@43872
   140
           | Normal => "Try this") ^ ": " ^
blanchet@41286
   141
          Markup.markup Markup.sendback
blanchet@41286
   142
              ((if nprems_of (#goal (Proof.goal st)) = 1 then "by"
blanchet@42870
   143
                else "apply") ^ " " ^ (s |> need_parens ? enclose "(" ")")) ^
blanchet@41286
   144
          "\n(" ^ space_implode "; " (map time_string xs) ^ ").\n"
blanchet@41286
   145
      in
blanchet@43867
   146
        (true, (s, st |> (if mode = Auto_Try then
blanchet@43861
   147
                            Proof.goal_message
blanchet@43861
   148
                                (fn () => Pretty.chunks [Pretty.str "",
blanchet@43861
   149
                                          Pretty.markup Markup.hilite
blanchet@43861
   150
                                                        [Pretty.str message]])
blanchet@43861
   151
                          else
blanchet@43861
   152
                            tap (fn _ => Output.urgent_message message))))
blanchet@41286
   153
      end
blanchet@41286
   154
  end
blanchet@39577
   155
blanchet@43867
   156
fun try_methods timeout_opt = fst oo do_try_methods Normal timeout_opt
blanchet@39168
   157
blanchet@43857
   158
fun try_methods_trans quad =
blanchet@43867
   159
  Toplevel.keep (K () o do_try_methods Normal (SOME default_timeout) quad
blanchet@42870
   160
                 o Toplevel.proof_of)
blanchet@42870
   161
blanchet@43050
   162
fun merge_attrs (s1, i1, e1, d1) (s2, i2, e2, d2) =
blanchet@43050
   163
  (s1 @ s2, i1 @ i2, e1 @ e2, d1 @ d2)
blanchet@42870
   164
blanchet@42870
   165
fun string_of_xthm (xref, args) =
blanchet@42870
   166
  Facts.string_of_ref xref ^
blanchet@42870
   167
  implode (map (enclose "[" "]" o Pretty.str_of
blanchet@42870
   168
                o Args.pretty_src @{context}) args)
blanchet@42870
   169
blanchet@42870
   170
val parse_fact_refs =
blanchet@42870
   171
  Scan.repeat1 (Scan.unless (Parse.name -- Args.colon)
blanchet@42870
   172
                            (Parse_Spec.xthm >> string_of_xthm))
blanchet@42870
   173
val parse_attr =
blanchet@42870
   174
     Args.$$$ "simp" |-- Args.colon |-- parse_fact_refs
blanchet@43050
   175
     >> (fn ss => (ss, [], [], []))
blanchet@42870
   176
  || Args.$$$ "intro" |-- Args.colon |-- parse_fact_refs
blanchet@43050
   177
     >> (fn is => ([], is, [], []))
blanchet@42870
   178
  || Args.$$$ "elim" |-- Args.colon |-- parse_fact_refs
blanchet@43050
   179
     >> (fn es => ([], [], es, []))
blanchet@43050
   180
  || Args.$$$ "dest" |-- Args.colon |-- parse_fact_refs
blanchet@43050
   181
     >> (fn ds => ([], [], [], ds))
blanchet@42870
   182
fun parse_attrs x =
blanchet@42870
   183
    (Args.parens parse_attrs
blanchet@42870
   184
  || Scan.repeat parse_attr
blanchet@43050
   185
     >> (fn quad => fold merge_attrs quad ([], [], [], []))) x
blanchet@42870
   186
blanchet@43857
   187
val parse_try_methods_command =
blanchet@43857
   188
  Scan.optional parse_attrs ([], [], [], []) #>> try_methods_trans
blanchet@42870
   189
blanchet@39168
   190
val _ =
blanchet@43857
   191
  Outer_Syntax.improper_command try_methodsN
blanchet@43857
   192
      "try a combination of proof methods" Keyword.diag
blanchet@43857
   193
      parse_try_methods_command
blanchet@39168
   194
blanchet@43867
   195
fun try_try_methods auto =
blanchet@43867
   196
  do_try_methods (if auto then Auto_Try else Try) NONE ([], [], [], [])
blanchet@39577
   197
blanchet@43867
   198
val setup = Try.register_tool (try_methodsN, (30, auto, try_try_methods))
blanchet@39577
   199
blanchet@39168
   200
end;