| author | haftmann | 
| Thu, 02 Jul 2020 12:10:58 +0000 | |
| changeset 71989 | bad75618fb82 | 
| parent 69593 | 3dda49e08b9d | 
| child 74507 | a241eadc0e3f | 
| permissions | -rw-r--r-- | 
| 43018 | 1 | (* Title: Tools/try.ML | 
| 33561 
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
 blanchet parents: diff
changeset | 2 | Author: Jasmin Blanchette, TU Muenchen | 
| 
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
 blanchet parents: diff
changeset | 3 | |
| 43018 | 4 | Manager for tools that should be tried on conjectures. | 
| 33561 
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
 blanchet parents: diff
changeset | 5 | *) | 
| 
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
 blanchet parents: diff
changeset | 6 | |
| 43018 | 7 | signature TRY = | 
| 33561 
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
 blanchet parents: diff
changeset | 8 | sig | 
| 63690 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 9 | type tool = string * (int * string * (bool -> Proof.state -> bool * (string * string list))) | 
| 43020 
abb5d1f907e4
added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
 blanchet parents: 
43018diff
changeset | 10 | |
| 63690 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 11 | val tryN: string | 
| 33561 
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
 blanchet parents: diff
changeset | 12 | |
| 63690 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 13 | val serial_commas: string -> string list -> string list | 
| 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 14 | val subgoal_count: Proof.state -> int | 
| 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 15 | val get_tools: theory -> tool list | 
| 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 16 | val try_tools: Proof.state -> (string * string) option | 
| 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 17 | val tool_setup: tool -> unit | 
| 33561 
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
 blanchet parents: diff
changeset | 18 | end; | 
| 
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
 blanchet parents: diff
changeset | 19 | |
| 43018 | 20 | structure Try : TRY = | 
| 33561 
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
 blanchet parents: diff
changeset | 21 | struct | 
| 
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
 blanchet parents: diff
changeset | 22 | |
| 63690 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 23 | type tool = string * (int * string * (bool -> Proof.state -> bool * (string * string list))); | 
| 43020 
abb5d1f907e4
added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
 blanchet parents: 
43018diff
changeset | 24 | |
| 63690 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 25 | val tryN = "try"; | 
| 43020 
abb5d1f907e4
added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
 blanchet parents: 
43018diff
changeset | 26 | |
| 
abb5d1f907e4
added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
 blanchet parents: 
43018diff
changeset | 27 | |
| 43028 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 28 | (* helpers *) | 
| 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 29 | |
| 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 30 | fun serial_commas _ [] = ["??"] | 
| 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 31 | | serial_commas _ [s] = [s] | 
| 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 32 | | serial_commas conj [s1, s2] = [s1, conj, s2] | 
| 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 33 | | serial_commas conj [s1, s2, s3] = [s1 ^ ",", s2 ^ ",", conj, s3] | 
| 63690 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 34 | | serial_commas conj (s :: ss) = s ^ "," :: serial_commas conj ss; | 
| 43028 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 35 | |
| 63690 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 36 | val subgoal_count = Logic.count_prems o Thm.prop_of o #goal o Proof.goal; | 
| 43028 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 37 | |
| 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 38 | |
| 33561 
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
 blanchet parents: diff
changeset | 39 | (* configuration *) | 
| 
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
 blanchet parents: diff
changeset | 40 | |
| 43061 | 41 | fun tool_ord ((name1, (weight1, _, _)), (name2, (weight2, _, _))) = | 
| 63690 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 42 | prod_ord int_ord string_ord ((weight1, name1), (weight2, name2)); | 
| 43028 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 43 | |
| 33600 | 44 | structure Data = Theory_Data | 
| 45 | ( | |
| 63690 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 46 | type T = tool list; | 
| 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 47 | val empty = []; | 
| 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 48 | val extend = I; | 
| 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 49 | fun merge data : T = Ord_List.merge tool_ord data; | 
| 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 50 | ); | 
| 33561 
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
 blanchet parents: diff
changeset | 51 | |
| 63690 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 52 | val get_tools = Data.get; | 
| 43020 
abb5d1f907e4
added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
 blanchet parents: 
43018diff
changeset | 53 | |
| 63690 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 54 | val register_tool = Data.map o Ord_List.insert tool_ord; | 
| 33561 
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
 blanchet parents: diff
changeset | 55 | |
| 52641 
c56b6fa636e8
hybrid "auto" tool setup, for TTY (within theory) and PIDE (global print function);
 wenzelm parents: 
52639diff
changeset | 56 | |
| 43020 
abb5d1f907e4
added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
 blanchet parents: 
43018diff
changeset | 57 | (* try command *) | 
| 
abb5d1f907e4
added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
 blanchet parents: 
43018diff
changeset | 58 | |
| 
abb5d1f907e4
added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
 blanchet parents: 
43018diff
changeset | 59 | fun try_tools state = | 
| 43028 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 60 | if subgoal_count state = 0 then | 
| 58843 | 61 | (writeln "No subgoal!"; NONE) | 
| 43028 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 62 | else | 
| 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 63 | get_tools (Proof.theory_of state) | 
| 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 64 | |> tap (fn tools => | 
| 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 65 | "Trying " ^ space_implode " " | 
| 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 66 | (serial_commas "and" (map (quote o fst) tools)) ^ "..." | 
| 58843 | 67 | |> writeln) | 
| 43028 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 68 | |> Par_List.get_some | 
| 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 69 | (fn (name, (_, _, tool)) => | 
| 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 70 | case try (tool false) state of | 
| 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 71 | SOME (true, (outcome_code, _)) => SOME (name, outcome_code) | 
| 
1c451bbb3ad7
repaired theory merging and defined/used helpers
 blanchet parents: 
43024diff
changeset | 72 | | _ => NONE) | 
| 63690 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 73 | |> tap (fn NONE => writeln "Tried in vain" | _ => ()); | 
| 43020 
abb5d1f907e4
added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
 blanchet parents: 
43018diff
changeset | 74 | |
| 
abb5d1f907e4
added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
 blanchet parents: 
43018diff
changeset | 75 | val _ = | 
| 67149 | 76 | Outer_Syntax.command \<^command_keyword>\<open>try\<close> | 
| 46961 
5c6955f487e5
outer syntax command definitions based on formal command_spec derived from theory header declarations;
 wenzelm parents: 
43061diff
changeset | 77 | "try a combination of automatic proving and disproving tools" | 
| 63690 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 78 | (Scan.succeed (Toplevel.keep_proof (ignore o try_tools o Toplevel.proof_of))); | 
| 43020 
abb5d1f907e4
added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
 blanchet parents: 
43018diff
changeset | 79 | |
| 33561 
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
 blanchet parents: diff
changeset | 80 | |
| 52641 
c56b6fa636e8
hybrid "auto" tool setup, for TTY (within theory) and PIDE (global print function);
 wenzelm parents: 
52639diff
changeset | 81 | (* automatic try (TTY) *) | 
| 33561 
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
 blanchet parents: diff
changeset | 82 | |
| 43020 
abb5d1f907e4
added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
 blanchet parents: 
43018diff
changeset | 83 | fun auto_try state = | 
| 
abb5d1f907e4
added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
 blanchet parents: 
43018diff
changeset | 84 | get_tools (Proof.theory_of state) | 
| 52639 | 85 | |> map_filter (fn (_, (_, auto, tool)) => if Options.default_bool auto then SOME tool else NONE) | 
| 43020 
abb5d1f907e4
added "try" command, to launch Solve Direct, Quickcheck, Nitpick, Sledgehammer, and Try Methods
 blanchet parents: 
43018diff
changeset | 86 | |> Par_List.get_some (fn tool => | 
| 63690 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 87 | (case try (tool true) state of | 
| 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 88 | SOME (true, (_, outcome)) => SOME outcome | 
| 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 89 | | _ => NONE)) | 
| 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 90 | |> the_default []; | 
| 40931 | 91 | |
| 52641 
c56b6fa636e8
hybrid "auto" tool setup, for TTY (within theory) and PIDE (global print function);
 wenzelm parents: 
52639diff
changeset | 92 | |
| 
c56b6fa636e8
hybrid "auto" tool setup, for TTY (within theory) and PIDE (global print function);
 wenzelm parents: 
52639diff
changeset | 93 | (* asynchronous print function (PIDE) *) | 
| 
c56b6fa636e8
hybrid "auto" tool setup, for TTY (within theory) and PIDE (global print function);
 wenzelm parents: 
52639diff
changeset | 94 | |
| 
c56b6fa636e8
hybrid "auto" tool setup, for TTY (within theory) and PIDE (global print function);
 wenzelm parents: 
52639diff
changeset | 95 | fun print_function ((name, (weight, auto, tool)): tool) = | 
| 53839 
274a892b1230
avoid clash of auto print functions with query operations, notably sledgehammer (cf. 3461985dcbc3);
 wenzelm parents: 
53171diff
changeset | 96 |   Command.print_function ("auto_" ^ name)
 | 
| 58923 | 97 |     (fn {keywords, command_name, ...} =>
 | 
| 98 | if Keyword.is_theory_goal keywords command_name andalso Options.default_bool auto then | |
| 52651 | 99 | SOME | 
| 69593 | 100 |          {delay = SOME (seconds (Options.default_real \<^system_option>\<open>auto_time_start\<close>)),
 | 
| 52651 | 101 | pri = ~ weight, | 
| 102 | persistent = true, | |
| 52953 
2c927b7501c5
explicit "strict" flag for print functions (flipped internal meaning);
 wenzelm parents: 
52850diff
changeset | 103 | strict = true, | 
| 52651 | 104 | print_fn = fn _ => fn st => | 
| 105 | let | |
| 106 | val state = Toplevel.proof_of st | |
| 52652 
ebdbd5c79a13
avoid spurious warnings, notably of 'try0' about already declared simps etc.;
 wenzelm parents: 
52651diff
changeset | 107 | |> Proof.map_context (Context_Position.set_visible false) | 
| 69593 | 108 | val auto_time_limit = Options.default_real \<^system_option>\<open>auto_time_limit\<close> | 
| 52651 | 109 | in | 
| 110 | if auto_time_limit > 0.0 then | |
| 62519 | 111 | (case Timeout.apply (seconds auto_time_limit) (fn () => tool true state) () of | 
| 59184 
830bb7ddb3ab
explicit message channels for "state", "information";
 wenzelm parents: 
58923diff
changeset | 112 | (true, (_, outcome)) => List.app Output.information outcome | 
| 52651 | 113 | | _ => ()) | 
| 114 | else () | |
| 62505 | 115 | end handle exn => if Exn.is_interrupt exn then Exn.reraise exn else ()} | 
| 63690 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 116 | else NONE); | 
| 52641 
c56b6fa636e8
hybrid "auto" tool setup, for TTY (within theory) and PIDE (global print function);
 wenzelm parents: 
52639diff
changeset | 117 | |
| 
c56b6fa636e8
hybrid "auto" tool setup, for TTY (within theory) and PIDE (global print function);
 wenzelm parents: 
52639diff
changeset | 118 | |
| 
c56b6fa636e8
hybrid "auto" tool setup, for TTY (within theory) and PIDE (global print function);
 wenzelm parents: 
52639diff
changeset | 119 | (* hybrid tool setup *) | 
| 
c56b6fa636e8
hybrid "auto" tool setup, for TTY (within theory) and PIDE (global print function);
 wenzelm parents: 
52639diff
changeset | 120 | |
| 63690 
48a2c88091d7
tuning punctuation in messages output by Isabelle
 blanchet parents: 
62519diff
changeset | 121 | fun tool_setup tool = (Theory.setup (register_tool tool); print_function tool); | 
| 52641 
c56b6fa636e8
hybrid "auto" tool setup, for TTY (within theory) and PIDE (global print function);
 wenzelm parents: 
52639diff
changeset | 122 | |
| 33561 
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
 blanchet parents: diff
changeset | 123 | end; |