src/HOL/Tools/try.ML
changeset 40222 cd6d2b0a4096
parent 40132 7ee65dbffa31
child 40301 bf39a257b3d3
equal deleted inserted replaced
40221:d10b68c6e6d4 40222:cd6d2b0a4096
     1 (*  Title:      HOL/Tools/try.ML
     1 (*  Title:      HOL/Tools/try.ML
     2     Author:     Jasmin Blanchette, TU Muenchen
     2     Author:     Jasmin Blanchette, TU Muenchen
     3 
     3 
     4 Try a combination of proof methods.
     4 Try a combination of proof methods.
     5 
       
     6 FIXME: reintroduce or remove commented code (see also HOL.thy)
       
     7 *)
     5 *)
     8 
     6 
     9 signature TRY =
     7 signature TRY =
    10 sig
     8 sig
    11 (*
       
    12   val auto : bool Unsynchronized.ref
     9   val auto : bool Unsynchronized.ref
    13 *)
       
    14   val invoke_try : Time.time option -> Proof.state -> bool
    10   val invoke_try : Time.time option -> Proof.state -> bool
    15 (*
       
    16   val setup : theory -> theory
    11   val setup : theory -> theory
    17 *)
       
    18 end;
    12 end;
    19 
    13 
    20 structure Try : TRY =
    14 structure Try : TRY =
    21 struct
    15 struct
    22 
    16 
    23 (*
       
    24 val auto = Unsynchronized.ref false
    17 val auto = Unsynchronized.ref false
    25 
    18 
    26 val _ =
    19 val _ =
    27   ProofGeneralPgip.add_preference Preferences.category_tracing
    20   ProofGeneralPgip.add_preference Preferences.category_tracing
    28   (Unsynchronized.setmp auto true (fn () =>
    21       (Preferences.bool_pref auto "auto-try" "Try standard proof methods.")
    29     Preferences.bool_pref auto
       
    30       "auto-try" "Try standard proof methods.") ());
       
    31 *)
       
    32 
    22 
    33 val default_timeout = Time.fromSeconds 5
    23 val default_timeout = Time.fromSeconds 5
    34 
    24 
    35 fun can_apply timeout_opt pre post tac st =
    25 fun can_apply timeout_opt pre post tac st =
    36   let val {goal, ...} = Proof.goal st in
    26   let val {goal, ...} = Proof.goal st in
    55 
    45 
    56 fun apply_named_method_on_first_goal name ctxt =
    46 fun apply_named_method_on_first_goal name ctxt =
    57   let val thy = ProofContext.theory_of ctxt in
    47   let val thy = ProofContext.theory_of ctxt in
    58     Proof.refine (Method.SelectGoals (1, Method.Basic (named_method thy name)))
    48     Proof.refine (Method.SelectGoals (1, Method.Basic (named_method thy name)))
    59   end
    49   end
       
    50   handle ERROR _ => K Seq.empty (* e.g., the method isn't available yet *)
    60 
    51 
    61 fun do_named_method (name, all_goals) timeout_opt st =
    52 fun do_named_method (name, (all_goals, run_if_auto)) auto timeout_opt st =
    62   do_generic timeout_opt
    53   if not auto orelse run_if_auto then
    63              (name ^ (if all_goals andalso
    54     do_generic timeout_opt
    64                          nprems_of (#goal (Proof.goal st)) > 1 then
    55                (name ^ (if all_goals andalso
    65                         "[1]"
    56                            nprems_of (#goal (Proof.goal st)) > 1 then
    66                       else
    57                           "[1]"
    67                         "")) I (#goal o Proof.goal)
    58                         else
    68              (apply_named_method_on_first_goal name (Proof.context_of st)) st
    59                           "")) I (#goal o Proof.goal)
       
    60                (apply_named_method_on_first_goal name (Proof.context_of st)) st
       
    61   else
       
    62     NONE
    69 
    63 
       
    64 (* name * (all_goals, run_if_auto) *)
    70 val named_methods =
    65 val named_methods =
    71   [("simp", false), ("auto", true), ("fast", false), ("fastsimp", false),
    66   [("simp", (false, true)),
    72    ("force", false), ("blast", false), ("metis", false), ("linarith", false),
    67    ("auto", (true, true)),
    73    ("presburger", false)]
    68    ("fast", (false, false)),
       
    69    ("fastsimp", (false, false)),
       
    70    ("force", (false, false)),
       
    71    ("blast", (false, true)),
       
    72    ("metis", (false, true)),
       
    73    ("linarith", (false, true)),
       
    74    ("presburger", (false, true))]
    74 val do_methods = map do_named_method named_methods
    75 val do_methods = map do_named_method named_methods
    75 
    76 
    76 fun time_string (s, ms) = s ^ ": " ^ string_of_int ms ^ " ms"
    77 fun time_string (s, ms) = s ^ ": " ^ string_of_int ms ^ " ms"
    77 
    78 
    78 fun do_try auto timeout_opt st =
    79 fun do_try auto timeout_opt st =
    79   case do_methods |> Par_List.map (fn f => f timeout_opt st)
    80   case do_methods |> Par_List.map (fn f => f auto timeout_opt st)
    80                   |> map_filter I |> sort (int_ord o pairself snd) of
    81                   |> map_filter I |> sort (int_ord o pairself snd) of
    81     [] => (if auto then () else writeln "No proof found."; (false, st))
    82     [] => (if auto then () else writeln "No proof found."; (false, st))
    82   | xs as (s, _) :: _ =>
    83   | xs as (s, _) :: _ =>
    83     let
    84     let
    84       val xs = xs |> map swap |> AList.coalesce (op =)
    85       val xs = xs |> map swap |> AList.coalesce (op =)
    86       val message =
    87       val message =
    87         (if auto then "Auto Try found a proof" else "Try this command") ^ ": " ^
    88         (if auto then "Auto Try found a proof" else "Try this command") ^ ": " ^
    88         Markup.markup Markup.sendback
    89         Markup.markup Markup.sendback
    89             ((if nprems_of (#goal (Proof.goal st)) = 1 then "by" else "apply") ^
    90             ((if nprems_of (#goal (Proof.goal st)) = 1 then "by" else "apply") ^
    90              " " ^ s) ^
    91              " " ^ s) ^
    91         ".\n(" ^ space_implode "; " (map time_string xs) ^ ")\n"
    92         "\n(" ^ space_implode "; " (map time_string xs) ^ ").\n"
    92     in
    93     in
    93       (true, st |> (if auto then
    94       (true, st |> (if auto then
    94                       Proof.goal_message
    95                       Proof.goal_message
    95                           (fn () => Pretty.chunks [Pretty.str "",
    96                           (fn () => Pretty.chunks [Pretty.str "",
    96                                     Pretty.markup Markup.hilite
    97                                     Pretty.markup Markup.hilite
   107   Outer_Syntax.improper_command tryN
   108   Outer_Syntax.improper_command tryN
   108       "try a combination of proof methods" Keyword.diag
   109       "try a combination of proof methods" Keyword.diag
   109       (Scan.succeed (Toplevel.keep (K () o do_try false (SOME default_timeout)
   110       (Scan.succeed (Toplevel.keep (K () o do_try false (SOME default_timeout)
   110                                     o Toplevel.proof_of)))
   111                                     o Toplevel.proof_of)))
   111 
   112 
   112 (*
       
   113 fun auto_try st = if not (!auto) then (false, st) else do_try true NONE st
   113 fun auto_try st = if not (!auto) then (false, st) else do_try true NONE st
   114 
   114 
   115 val setup = Auto_Tools.register_tool (tryN, auto_try)
   115 val setup = Auto_Tools.register_tool (tryN, auto_try)
   116 *)
       
   117 
   116 
   118 end;
   117 end;