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