src/HOL/Tools/try0.ML
changeset 55181 9434810f7ab5
parent 55179 71cc2db86eec
child 55182 dd1e95e67b30
equal deleted inserted replaced
55180:03ac74b01e49 55181:9434810f7ab5
    39       SOME (x, _) => nprems_of (post x) < nprems_of goal
    39       SOME (x, _) => nprems_of (post x) < nprems_of goal
    40     | NONE => false
    40     | NONE => false
    41   end
    41   end
    42   handle TimeLimit.TimeOut => false;
    42   handle TimeLimit.TimeOut => false;
    43 
    43 
    44 fun do_generic timeout_opt name command pre post apply st =
    44 fun apply_generic timeout_opt name command pre post apply st =
    45   let val timer = Timer.startRealTimer () in
    45   let val timer = Timer.startRealTimer () in
    46     if can_apply timeout_opt pre post apply st then
    46     if can_apply timeout_opt pre post apply st then
    47       SOME (name, command, Time.toMilliseconds (Timer.checkRealTimer timer))
    47       SOME (name, command, Time.toMilliseconds (Timer.checkRealTimer timer))
    48     else
    48     else
    49       NONE
    49       NONE
    71     s ^ " " ^ (if x = "" then "" else x ^ ": ") ^ space_implode " " fs;
    71     s ^ " " ^ (if x = "" then "" else x ^ ": ") ^ space_implode " " fs;
    72 
    72 
    73 fun attrs_text (sx, ix, ex, dx) (ss, is, es, ds) =
    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)];
    74   "" |> fold add_attr_text [(sx, ss), (ix, is), (ex, es), (dx, ds)];
    75 
    75 
    76 fun do_named_method (name, ((all_goals, run_if_auto_try), attrs)) mode timeout_opt quad st =
    76 fun apply_named_method (name, ((all_goals, run_if_auto_try), attrs)) mode timeout_opt quad st =
    77   if mode <> Auto_Try orelse run_if_auto_try then
    77   if mode <> Auto_Try orelse run_if_auto_try then
    78     let val attrs = attrs_text attrs quad in
    78     let val attrs = attrs_text attrs quad in
    79       do_generic timeout_opt name
    79       apply_generic timeout_opt name
    80         ((name ^ attrs |> attrs <> "" ? enclose "(" ")") ^
    80         ((name ^ attrs |> attrs <> "" ? enclose "(" ")") ^
    81          (if all_goals andalso nprems_of (#goal (Proof.goal st)) > 1 then "[1]" else ""))
    81          (if all_goals andalso nprems_of (#goal (Proof.goal st)) > 1 then "[1]" else ""))
    82         I (#goal o Proof.goal)
    82         I (#goal o Proof.goal)
    83         (apply_named_method_on_first_goal (Proof.theory_of st) (name ^ attrs)) st
    83         (apply_named_method_on_first_goal (Proof.theory_of st) (name ^ attrs)) st
    84     end
    84     end
   103    ("fast", ((false, false), clas_attrs)),
   103    ("fast", ((false, false), clas_attrs)),
   104    ("fastforce", ((false, false), full_attrs)),
   104    ("fastforce", ((false, false), full_attrs)),
   105    ("force", ((false, false), full_attrs)),
   105    ("force", ((false, false), full_attrs)),
   106    ("meson", ((false, false), metis_attrs))]
   106    ("meson", ((false, false), metis_attrs))]
   107 
   107 
   108 val do_methods = map do_named_method named_methods;
   108 val apply_methods = map apply_named_method named_methods;
   109 
   109 
   110 fun time_string ms = string_of_int ms ^ " ms";
   110 fun time_string ms = string_of_int ms ^ " ms";
   111 fun tool_time_string (s, ms) = s ^ ": " ^ time_string ms;
   111 fun tool_time_string (s, ms) = s ^ ": " ^ time_string ms;
   112 
   112 
   113 (* Makes reconstructor tools as silent as possible. The "set_visible" calls suppresses "Unification
   113 (* Makes reconstructor tools as silent as possible. The "set_visible" calls suppresses "Unification
   120      #> Proof_Context.background_theory (fn thy =>
   120      #> Proof_Context.background_theory (fn thy =>
   121        thy
   121        thy
   122        |> Context_Position.set_visible_global false
   122        |> Context_Position.set_visible_global false
   123        |> Config.put_global Unify.trace_bound (Config.get_global thy Unify.search_bound))));
   123        |> Config.put_global Unify.trace_bound (Config.get_global thy Unify.search_bound))));
   124 
   124 
   125 fun do_try0 mode timeout_opt quad st =
   125 fun generic_try0 mode timeout_opt quad st =
   126   let
   126   let
   127     val st = st |> Proof.map_context (silence_methods false);
   127     val st = st |> Proof.map_context (silence_methods false);
   128     fun trd (_, _, t) = t;
   128     fun trd (_, _, t) = t;
   129     fun par_map f =
   129     fun par_map f =
   130       if mode = Normal then Par_List.map f #> map_filter I #> sort (int_ord o pairself trd)
   130       if mode = Normal then Par_List.map f #> map_filter I #> sort (int_ord o pairself trd)
   134       "Trying " ^ space_implode " " (Try.serial_commas "and" (map (quote o fst) named_methods)) ^
   134       "Trying " ^ space_implode " " (Try.serial_commas "and" (map (quote o fst) named_methods)) ^
   135       "..."
   135       "..."
   136       |> Output.urgent_message
   136       |> Output.urgent_message
   137     else
   137     else
   138       ();
   138       ();
   139     (case par_map (fn f => f mode timeout_opt quad st) do_methods of
   139     (case par_map (fn f => f mode timeout_opt quad st) apply_methods of
   140       [] =>
   140       [] =>
   141       (if mode = Normal then Output.urgent_message "No proof found." else (); (false, (noneN, st)))
   141       (if mode = Normal then Output.urgent_message "No proof found." else (); (false, (noneN, st)))
   142     | xs as (name, command, _) :: _ =>
   142     | xs as (name, command, _) :: _ =>
   143       let
   143       let
   144         val xs = xs |> map (fn (name, _, n) => (n, name))
   144         val xs = xs |> map (fn (name, _, n) => (n, name))
   163                else
   163                else
   164                  tap (fn _ => Output.urgent_message message))))
   164                  tap (fn _ => Output.urgent_message message))))
   165       end)
   165       end)
   166   end;
   166   end;
   167 
   167 
   168 fun try0 timeout_opt = fst oo do_try0 Normal timeout_opt;
   168 fun try0 timeout_opt = fst oo generic_try0 Normal timeout_opt;
   169 
   169 
   170 fun try0_trans quad =
   170 fun try0_trans quad =
   171   Toplevel.unknown_proof o
   171   Toplevel.unknown_proof o
   172   Toplevel.keep (K () o do_try0 Normal (SOME default_timeout) quad o Toplevel.proof_of);
   172   Toplevel.keep (K () o generic_try0 Normal (SOME default_timeout) quad o Toplevel.proof_of);
   173 
   173 
   174 fun merge_attrs (s1, i1, e1, d1) (s2, i2, e2, d2) = (s1 @ s2, i1 @ i2, e1 @ e2, d1 @ d2);
   174 fun merge_attrs (s1, i1, e1, d1) (s2, i2, e2, d2) = (s1 @ s2, i1 @ i2, e1 @ e2, d1 @ d2);
   175 
   175 
   176 fun string_of_xthm (xref, args) =
   176 fun string_of_xthm (xref, args) =
   177   Facts.string_of_ref xref ^
   177   Facts.string_of_ref xref ^
   192 
   192 
   193 val _ =
   193 val _ =
   194   Outer_Syntax.improper_command @{command_spec "try0"} "try a combination of proof methods"
   194   Outer_Syntax.improper_command @{command_spec "try0"} "try a combination of proof methods"
   195     (Scan.optional parse_attrs ([], [], [], []) #>> try0_trans);
   195     (Scan.optional parse_attrs ([], [], [], []) #>> try0_trans);
   196 
   196 
   197 fun try_try0 auto = do_try0 (if auto then Auto_Try else Try) NONE ([], [], [], []);
   197 fun try_try0 auto = generic_try0 (if auto then Auto_Try else Try) NONE ([], [], [], []);
   198 
   198 
   199 val _ = Try.tool_setup (try0N, (30, @{option auto_methods}, try_try0));
   199 val _ = Try.tool_setup (try0N, (30, @{option auto_methods}, try_try0));
   200 
   200 
   201 end;
   201 end;