author | wenzelm |
Sun, 15 Nov 2009 19:44:29 +0100 | |
changeset 33699 | f33b036ef318 |
parent 33604 | d4220df6fde2 |
child 33982 | 1ae222745c4a |
permissions | -rw-r--r-- |
33192 | 1 |
(* Title: HOL/Nitpick/Tools/nitpick_isar.ML |
2 |
Author: Jasmin Blanchette, TU Muenchen |
|
3 |
Copyright 2008, 2009 |
|
4 |
||
5 |
Adds the "nitpick" and "nitpick_params" commands to Isabelle/Isar's outer |
|
6 |
syntax. |
|
7 |
*) |
|
8 |
||
9 |
signature NITPICK_ISAR = |
|
10 |
sig |
|
11 |
type params = Nitpick.params |
|
12 |
||
33561
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
13 |
val auto: bool Unsynchronized.ref |
33192 | 14 |
val default_params : theory -> (string * string) list -> params |
33561
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
15 |
val setup : theory -> theory |
33192 | 16 |
end |
17 |
||
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33220
diff
changeset
|
18 |
structure Nitpick_Isar : NITPICK_ISAR = |
33192 | 19 |
struct |
20 |
||
33232
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33220
diff
changeset
|
21 |
open Nitpick_Util |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33220
diff
changeset
|
22 |
open Nitpick_HOL |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33220
diff
changeset
|
23 |
open Nitpick_Rep |
f93390060bbe
internal renaming in Nitpick and fixed Kodkodi invokation on Linux;
blanchet
parents:
33220
diff
changeset
|
24 |
open Nitpick_Nut |
33192 | 25 |
open Nitpick |
26 |
||
33561
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
27 |
val auto = Unsynchronized.ref false; |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
28 |
|
33601
4608243edcfc
plain add_preference, no setmp_CRITICAL required;
wenzelm
parents:
33599
diff
changeset
|
29 |
val _ = |
4608243edcfc
plain add_preference, no setmp_CRITICAL required;
wenzelm
parents:
33599
diff
changeset
|
30 |
ProofGeneralPgip.add_preference Preferences.category_tracing |
4608243edcfc
plain add_preference, no setmp_CRITICAL required;
wenzelm
parents:
33599
diff
changeset
|
31 |
(Preferences.bool_pref auto |
4608243edcfc
plain add_preference, no setmp_CRITICAL required;
wenzelm
parents:
33599
diff
changeset
|
32 |
"auto-nitpick" |
4608243edcfc
plain add_preference, no setmp_CRITICAL required;
wenzelm
parents:
33599
diff
changeset
|
33 |
"Whether to run Nitpick automatically.") |
33561
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
34 |
|
33192 | 35 |
type raw_param = string * string list |
36 |
||
37 |
val default_default_params = |
|
38 |
[("card", ["1\<midarrow>8"]), |
|
39 |
("iter", ["0,1,2,4,8,12,16,24"]), |
|
40 |
("bisim_depth", ["7"]), |
|
41 |
("box", ["smart"]), |
|
42 |
("mono", ["smart"]), |
|
43 |
("wf", ["smart"]), |
|
44 |
("sat_solver", ["smart"]), |
|
45 |
("batch_size", ["smart"]), |
|
46 |
("blocking", ["true"]), |
|
47 |
("falsify", ["true"]), |
|
48 |
("user_axioms", ["smart"]), |
|
49 |
("assms", ["true"]), |
|
33556
cba22e2999d5
renamed Nitpick option "coalesce_type_vars" to "merge_type_vars" (shorter) and cleaned up old hacks that are no longer necessary
blanchet
parents:
33232
diff
changeset
|
50 |
("merge_type_vars", ["false"]), |
33192 | 51 |
("destroy_constrs", ["true"]), |
52 |
("specialize", ["true"]), |
|
53 |
("skolemize", ["true"]), |
|
54 |
("star_linear_preds", ["true"]), |
|
55 |
("uncurry", ["true"]), |
|
56 |
("fast_descrs", ["true"]), |
|
57 |
("peephole_optim", ["true"]), |
|
58 |
("timeout", ["30 s"]), |
|
59 |
("tac_timeout", ["500 ms"]), |
|
60 |
("sym_break", ["20"]), |
|
61 |
("sharing_depth", ["3"]), |
|
62 |
("flatten_props", ["false"]), |
|
63 |
("max_threads", ["0"]), |
|
64 |
("verbose", ["false"]), |
|
65 |
("debug", ["false"]), |
|
33197
de6285ebcc05
continuation of Nitpick's integration into Isabelle;
blanchet
parents:
33192
diff
changeset
|
66 |
("overlord", ["false"]), |
33192 | 67 |
("show_all", ["false"]), |
68 |
("show_skolems", ["true"]), |
|
69 |
("show_datatypes", ["false"]), |
|
70 |
("show_consts", ["false"]), |
|
71 |
("format", ["1"]), |
|
72 |
("max_potential", ["1"]), |
|
73 |
("max_genuine", ["1"]), |
|
74 |
("check_potential", ["false"]), |
|
75 |
("check_genuine", ["false"])] |
|
76 |
||
77 |
val negated_params = |
|
78 |
[("dont_box", "box"), |
|
79 |
("non_mono", "mono"), |
|
80 |
("non_wf", "wf"), |
|
81 |
("non_blocking", "blocking"), |
|
82 |
("satisfy", "falsify"), |
|
83 |
("no_user_axioms", "user_axioms"), |
|
84 |
("no_assms", "assms"), |
|
33556
cba22e2999d5
renamed Nitpick option "coalesce_type_vars" to "merge_type_vars" (shorter) and cleaned up old hacks that are no longer necessary
blanchet
parents:
33232
diff
changeset
|
85 |
("dont_merge_type_vars", "merge_type_vars"), |
33192 | 86 |
("dont_destroy_constrs", "destroy_constrs"), |
87 |
("dont_specialize", "specialize"), |
|
88 |
("dont_skolemize", "skolemize"), |
|
89 |
("dont_star_linear_preds", "star_linear_preds"), |
|
90 |
("dont_uncurry", "uncurry"), |
|
91 |
("full_descrs", "fast_descrs"), |
|
92 |
("no_peephole_optim", "peephole_optim"), |
|
93 |
("dont_flatten_props", "flatten_props"), |
|
94 |
("quiet", "verbose"), |
|
95 |
("no_debug", "debug"), |
|
96 |
("no_overlord", "overlord"), |
|
97 |
("dont_show_all", "show_all"), |
|
98 |
("hide_skolems", "show_skolems"), |
|
99 |
("hide_datatypes", "show_datatypes"), |
|
100 |
("hide_consts", "show_consts"), |
|
101 |
("trust_potential", "check_potential"), |
|
102 |
("trust_genuine", "check_genuine")] |
|
103 |
||
104 |
(* string -> bool *) |
|
105 |
fun is_known_raw_param s = |
|
106 |
AList.defined (op =) default_default_params s |
|
107 |
orelse AList.defined (op =) negated_params s |
|
108 |
orelse s mem ["max", "eval", "expect"] |
|
109 |
orelse exists (fn p => String.isPrefix (p ^ " ") s) |
|
110 |
["card", "max", "iter", "box", "dont_box", "mono", "non_mono", |
|
111 |
"wf", "non_wf", "format"] |
|
112 |
||
113 |
(* string * 'a -> unit *) |
|
114 |
fun check_raw_param (s, _) = |
|
115 |
if is_known_raw_param s then () |
|
116 |
else error ("Unknown parameter " ^ quote s ^ ".") |
|
117 |
||
118 |
(* string -> string option *) |
|
119 |
fun unnegate_param_name name = |
|
120 |
case AList.lookup (op =) negated_params name of |
|
121 |
NONE => if String.isPrefix "dont_" name then SOME (unprefix "dont_" name) |
|
122 |
else if String.isPrefix "non_" name then SOME (unprefix "non_" name) |
|
123 |
else NONE |
|
124 |
| some_name => some_name |
|
125 |
(* raw_param -> raw_param *) |
|
126 |
fun unnegate_raw_param (name, value) = |
|
127 |
case unnegate_param_name name of |
|
128 |
SOME name' => (name', case value of |
|
129 |
["false"] => ["true"] |
|
130 |
| ["true"] => ["false"] |
|
131 |
| [] => ["false"] |
|
132 |
| _ => value) |
|
133 |
| NONE => (name, value) |
|
134 |
||
33583
b5e0909cd5ea
merged, and renamed local "TheoryData" to "Data" (following common Isabelle conventions)
blanchet
diff
changeset
|
135 |
structure Data = Theory_Data( |
33561
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
136 |
type T = {params: raw_param list} |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
137 |
val empty = {params = rev default_default_params} |
33192 | 138 |
val extend = I |
33599
89c439646960
recovered update from 7264824baf66, which got lost in 7264824baf66;
wenzelm
parents:
33584
diff
changeset
|
139 |
fun merge ({params = ps1}, {params = ps2}) : T = |
33699
f33b036ef318
permissive AList.merge -- most likely setup for theory data (beware of spurious AList.DUP);
wenzelm
parents:
33604
diff
changeset
|
140 |
{params = AList.merge (op =) (K true) (ps1, ps2)}) |
33192 | 141 |
|
142 |
(* raw_param -> theory -> theory *) |
|
143 |
fun set_default_raw_param param thy = |
|
33583
b5e0909cd5ea
merged, and renamed local "TheoryData" to "Data" (following common Isabelle conventions)
blanchet
diff
changeset
|
144 |
let val {params} = Data.get thy in |
b5e0909cd5ea
merged, and renamed local "TheoryData" to "Data" (following common Isabelle conventions)
blanchet
diff
changeset
|
145 |
Data.put {params = AList.update (op =) (unnegate_raw_param param) params} |
b5e0909cd5ea
merged, and renamed local "TheoryData" to "Data" (following common Isabelle conventions)
blanchet
diff
changeset
|
146 |
thy |
33192 | 147 |
end |
148 |
(* theory -> raw_param list *) |
|
33583
b5e0909cd5ea
merged, and renamed local "TheoryData" to "Data" (following common Isabelle conventions)
blanchet
diff
changeset
|
149 |
val default_raw_params = #params o Data.get |
33192 | 150 |
|
151 |
(* string -> bool *) |
|
152 |
fun is_punctuation s = (s = "," orelse s = "-" orelse s = "\<midarrow>") |
|
153 |
||
154 |
(* string list -> string *) |
|
155 |
fun stringify_raw_param_value [] = "" |
|
156 |
| stringify_raw_param_value [s] = s |
|
157 |
| stringify_raw_param_value (s1 :: s2 :: ss) = |
|
158 |
s1 ^ (if is_punctuation s1 orelse is_punctuation s2 then "" else " ") ^ |
|
159 |
stringify_raw_param_value (s2 :: ss) |
|
160 |
||
161 |
(* bool -> string -> string -> bool option *) |
|
162 |
fun bool_option_from_string option name s = |
|
163 |
(case s of |
|
164 |
"smart" => if option then NONE else raise Option |
|
165 |
| "false" => SOME false |
|
166 |
| "true" => SOME true |
|
167 |
| "" => SOME true |
|
168 |
| s => raise Option) |
|
169 |
handle Option.Option => |
|
170 |
let val ss = map quote ((option ? cons "smart") ["true", "false"]) in |
|
171 |
error ("Parameter " ^ quote name ^ " must be assigned " ^ |
|
172 |
space_implode " " (serial_commas "or" ss) ^ ".") |
|
173 |
end |
|
174 |
(* bool -> raw_param list -> bool option -> string -> bool option *) |
|
175 |
fun general_lookup_bool option raw_params default_value name = |
|
176 |
case AList.lookup (op =) raw_params name of |
|
177 |
SOME s => s |> stringify_raw_param_value |
|
178 |
|> bool_option_from_string option name |
|
179 |
| NONE => default_value |
|
180 |
||
181 |
(* int -> string -> int *) |
|
182 |
fun maxed_int_from_string min_int s = Int.max (min_int, the (Int.fromString s)) |
|
183 |
||
184 |
(* Proof.context -> bool -> raw_param list -> raw_param list -> params *) |
|
185 |
fun extract_params ctxt auto default_params override_params = |
|
186 |
let |
|
187 |
val override_params = map unnegate_raw_param override_params |
|
188 |
val raw_params = rev override_params @ rev default_params |
|
189 |
val lookup = |
|
190 |
Option.map stringify_raw_param_value o AList.lookup (op =) raw_params |
|
191 |
(* string -> string *) |
|
192 |
fun lookup_string name = the_default "" (lookup name) |
|
193 |
(* string -> bool *) |
|
194 |
val lookup_bool = the o general_lookup_bool false raw_params (SOME false) |
|
195 |
(* string -> bool option *) |
|
196 |
val lookup_bool_option = general_lookup_bool true raw_params NONE |
|
197 |
(* string -> string option -> int *) |
|
198 |
fun do_int name value = |
|
199 |
case value of |
|
200 |
SOME s => (case Int.fromString s of |
|
201 |
SOME i => i |
|
202 |
| NONE => error ("Parameter " ^ quote name ^ |
|
203 |
" must be assigned an integer value.")) |
|
204 |
| NONE => 0 |
|
205 |
(* string -> int *) |
|
206 |
fun lookup_int name = do_int name (lookup name) |
|
207 |
(* string -> int option *) |
|
208 |
fun lookup_int_option name = |
|
209 |
case lookup name of |
|
210 |
SOME "smart" => NONE |
|
211 |
| value => SOME (do_int name value) |
|
212 |
(* string -> int -> string -> int list *) |
|
213 |
fun int_range_from_string name min_int s = |
|
214 |
let |
|
215 |
val (k1, k2) = |
|
216 |
(case space_explode "-" s of |
|
217 |
[s] => the_default (s, s) (first_field "\<midarrow>" s) |
|
218 |
| ["", s2] => ("-" ^ s2, "-" ^ s2) |
|
219 |
| [s1, s2] => (s1, s2) |
|
220 |
| _ => raise Option) |
|
221 |
|> pairself (maxed_int_from_string min_int) |
|
222 |
in if k1 <= k2 then k1 upto k2 else k1 downto k2 end |
|
223 |
handle Option.Option => |
|
224 |
error ("Parameter " ^ quote name ^ |
|
225 |
" must be assigned a sequence of integers.") |
|
226 |
(* string -> int -> string -> int list *) |
|
227 |
fun int_seq_from_string name min_int s = |
|
228 |
maps (int_range_from_string name min_int) (space_explode "," s) |
|
229 |
(* string -> int -> int list *) |
|
230 |
fun lookup_int_seq name min_int = |
|
231 |
case lookup name of |
|
232 |
SOME s => (case int_seq_from_string name min_int s of |
|
233 |
[] => [min_int] |
|
234 |
| value => value) |
|
235 |
| NONE => [min_int] |
|
236 |
(* (string -> 'a) -> int -> string -> ('a option * int list) list *) |
|
237 |
fun lookup_ints_assigns read prefix min_int = |
|
238 |
(NONE, lookup_int_seq prefix min_int) |
|
239 |
:: map (fn (name, value) => |
|
240 |
(SOME (read (String.extract (name, size prefix + 1, NONE))), |
|
241 |
value |> stringify_raw_param_value |
|
242 |
|> int_seq_from_string name min_int)) |
|
243 |
(filter (String.isPrefix (prefix ^ " ") o fst) raw_params) |
|
244 |
(* (string -> 'a) -> string -> ('a option * bool option) list *) |
|
245 |
fun lookup_bool_option_assigns read prefix = |
|
246 |
(NONE, lookup_bool_option prefix) |
|
247 |
:: map (fn (name, value) => |
|
248 |
(SOME (read (String.extract (name, size prefix + 1, NONE))), |
|
249 |
value |> stringify_raw_param_value |
|
250 |
|> bool_option_from_string true name)) |
|
251 |
(filter (String.isPrefix (prefix ^ " ") o fst) raw_params) |
|
252 |
(* string -> Time.time option *) |
|
253 |
fun lookup_time name = |
|
254 |
case lookup name of |
|
255 |
NONE => NONE |
|
256 |
| SOME "none" => NONE |
|
257 |
| SOME s => |
|
258 |
let |
|
259 |
val msecs = |
|
260 |
case space_explode " " s of |
|
261 |
[s1, "min"] => 60000 * the (Int.fromString s1) |
|
262 |
| [s1, "s"] => 1000 * the (Int.fromString s1) |
|
263 |
| [s1, "ms"] => the (Int.fromString s1) |
|
264 |
| _ => 0 |
|
265 |
in |
|
266 |
if msecs <= 0 then |
|
267 |
error ("Parameter " ^ quote name ^ " must be assigned a positive \ |
|
268 |
\time value (e.g., \"60 s\", \"200 ms\") or \"none\".") |
|
269 |
else |
|
270 |
SOME (Time.fromMilliseconds msecs) |
|
271 |
end |
|
272 |
(* string -> term list *) |
|
273 |
val lookup_term_list = |
|
274 |
AList.lookup (op =) raw_params #> these #> Syntax.read_terms ctxt |
|
275 |
val read_type_polymorphic = |
|
276 |
Syntax.read_typ ctxt #> Logic.mk_type |
|
277 |
#> singleton (Variable.polymorphic ctxt) #> Logic.dest_type |
|
278 |
(* string -> term *) |
|
279 |
val read_term_polymorphic = |
|
280 |
Syntax.read_term ctxt #> singleton (Variable.polymorphic ctxt) |
|
281 |
(* string -> styp *) |
|
282 |
val read_const_polymorphic = read_term_polymorphic #> dest_Const |
|
283 |
val cards_assigns = lookup_ints_assigns read_type_polymorphic "card" 1 |
|
284 |
val maxes_assigns = lookup_ints_assigns read_const_polymorphic "max" ~1 |
|
285 |
val iters_assigns = lookup_ints_assigns read_const_polymorphic "iter" 0 |
|
286 |
val bisim_depths = lookup_int_seq "bisim_depth" ~1 |
|
287 |
val boxes = |
|
288 |
lookup_bool_option_assigns read_type_polymorphic "box" @ |
|
289 |
map_filter (fn (SOME T, _) => |
|
290 |
if is_fun_type T orelse is_pair_type T then |
|
291 |
SOME (SOME T, SOME true) |
|
292 |
else |
|
293 |
NONE |
|
294 |
| (NONE, _) => NONE) cards_assigns |
|
295 |
val monos = lookup_bool_option_assigns read_type_polymorphic "mono" |
|
296 |
val wfs = lookup_bool_option_assigns read_const_polymorphic "wf" |
|
297 |
val sat_solver = lookup_string "sat_solver" |
|
298 |
val blocking = not auto andalso lookup_bool "blocking" |
|
299 |
val falsify = lookup_bool "falsify" |
|
300 |
val debug = not auto andalso lookup_bool "debug" |
|
301 |
val verbose = debug orelse (not auto andalso lookup_bool "verbose") |
|
302 |
val overlord = lookup_bool "overlord" |
|
303 |
val user_axioms = lookup_bool_option "user_axioms" |
|
304 |
val assms = lookup_bool "assms" |
|
33556
cba22e2999d5
renamed Nitpick option "coalesce_type_vars" to "merge_type_vars" (shorter) and cleaned up old hacks that are no longer necessary
blanchet
parents:
33232
diff
changeset
|
305 |
val merge_type_vars = lookup_bool "merge_type_vars" |
33192 | 306 |
val destroy_constrs = lookup_bool "destroy_constrs" |
307 |
val specialize = lookup_bool "specialize" |
|
308 |
val skolemize = lookup_bool "skolemize" |
|
309 |
val star_linear_preds = lookup_bool "star_linear_preds" |
|
310 |
val uncurry = lookup_bool "uncurry" |
|
311 |
val fast_descrs = lookup_bool "fast_descrs" |
|
312 |
val peephole_optim = lookup_bool "peephole_optim" |
|
33561
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
313 |
val timeout = if auto then NONE else lookup_time "timeout" |
33192 | 314 |
val tac_timeout = lookup_time "tac_timeout" |
315 |
val sym_break = Int.max (0, lookup_int "sym_break") |
|
316 |
val sharing_depth = Int.max (1, lookup_int "sharing_depth") |
|
317 |
val flatten_props = lookup_bool "flatten_props" |
|
318 |
val max_threads = Int.max (0, lookup_int "max_threads") |
|
319 |
val show_all = debug orelse lookup_bool "show_all" |
|
320 |
val show_skolems = show_all orelse lookup_bool "show_skolems" |
|
321 |
val show_datatypes = show_all orelse lookup_bool "show_datatypes" |
|
322 |
val show_consts = show_all orelse lookup_bool "show_consts" |
|
323 |
val formats = lookup_ints_assigns read_term_polymorphic "format" 0 |
|
324 |
val evals = lookup_term_list "eval" |
|
33561
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
325 |
val max_potential = |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
326 |
if auto then 0 else Int.max (0, lookup_int "max_potential") |
33192 | 327 |
val max_genuine = Int.max (0, lookup_int "max_genuine") |
328 |
val check_potential = lookup_bool "check_potential" |
|
329 |
val check_genuine = lookup_bool "check_genuine" |
|
330 |
val batch_size = case lookup_int_option "batch_size" of |
|
331 |
SOME n => Int.max (1, n) |
|
332 |
| NONE => if debug then 1 else 64 |
|
333 |
val expect = lookup_string "expect" |
|
334 |
in |
|
335 |
{cards_assigns = cards_assigns, maxes_assigns = maxes_assigns, |
|
336 |
iters_assigns = iters_assigns, bisim_depths = bisim_depths, boxes = boxes, |
|
337 |
monos = monos, wfs = wfs, sat_solver = sat_solver, blocking = blocking, |
|
338 |
falsify = falsify, debug = debug, verbose = verbose, overlord = overlord, |
|
339 |
user_axioms = user_axioms, assms = assms, |
|
33556
cba22e2999d5
renamed Nitpick option "coalesce_type_vars" to "merge_type_vars" (shorter) and cleaned up old hacks that are no longer necessary
blanchet
parents:
33232
diff
changeset
|
340 |
merge_type_vars = merge_type_vars, destroy_constrs = destroy_constrs, |
33192 | 341 |
specialize = specialize, skolemize = skolemize, |
342 |
star_linear_preds = star_linear_preds, uncurry = uncurry, |
|
343 |
fast_descrs = fast_descrs, peephole_optim = peephole_optim, |
|
344 |
timeout = timeout, tac_timeout = tac_timeout, sym_break = sym_break, |
|
345 |
sharing_depth = sharing_depth, flatten_props = flatten_props, |
|
346 |
max_threads = max_threads, show_skolems = show_skolems, |
|
347 |
show_datatypes = show_datatypes, show_consts = show_consts, |
|
348 |
formats = formats, evals = evals, max_potential = max_potential, |
|
349 |
max_genuine = max_genuine, check_potential = check_potential, |
|
350 |
check_genuine = check_genuine, batch_size = batch_size, expect = expect} |
|
351 |
end |
|
352 |
||
353 |
(* theory -> (string * string) list -> params *) |
|
354 |
fun default_params thy = |
|
355 |
extract_params (ProofContext.init thy) false (default_raw_params thy) |
|
356 |
o map (apsnd single) |
|
357 |
||
358 |
(* OuterParse.token list -> string * OuterParse.token list *) |
|
359 |
val scan_key = Scan.repeat1 OuterParse.typ_group >> space_implode " " |
|
360 |
||
361 |
(* OuterParse.token list -> string list * OuterParse.token list *) |
|
362 |
val scan_value = |
|
363 |
Scan.repeat1 (OuterParse.minus >> single |
|
364 |
|| Scan.repeat1 (Scan.unless OuterParse.minus OuterParse.name) |
|
365 |
|| OuterParse.$$$ "," |-- OuterParse.number >> prefix "," |
|
366 |
>> single) >> flat |
|
367 |
||
368 |
(* OuterParse.token list -> raw_param * OuterParse.token list *) |
|
369 |
val scan_param = |
|
370 |
scan_key -- (Scan.option (OuterParse.$$$ "=" |-- scan_value) >> these) |
|
371 |
(* OuterParse.token list -> raw_param list option * OuterParse.token list *) |
|
372 |
val scan_params = Scan.option (OuterParse.$$$ "[" |-- OuterParse.list scan_param |
|
373 |
--| OuterParse.$$$ "]") |
|
374 |
||
375 |
(* Proof.context -> ('a -> 'a) -> 'a -> 'a *) |
|
376 |
fun handle_exceptions ctxt f x = |
|
377 |
f x |
|
378 |
handle ARG (loc, details) => |
|
379 |
error ("Bad argument(s) to " ^ quote loc ^ ": " ^ details ^ ".") |
|
380 |
| BAD (loc, details) => |
|
381 |
error ("Internal error (" ^ quote loc ^ "): " ^ details ^ ".") |
|
382 |
| LIMIT (_, details) => |
|
383 |
(warning ("Limit reached: " ^ details ^ "."); x) |
|
384 |
| NOT_SUPPORTED details => |
|
385 |
(warning ("Unsupported case: " ^ details ^ "."); x) |
|
386 |
| NUT (loc, us) => |
|
387 |
error ("Invalid intermediate term" ^ plural_s_for_list us ^ |
|
388 |
" (" ^ quote loc ^ "): " ^ |
|
389 |
commas (map (string_for_nut ctxt) us) ^ ".") |
|
390 |
| REP (loc, Rs) => |
|
391 |
error ("Invalid representation" ^ plural_s_for_list Rs ^ |
|
392 |
" (" ^ quote loc ^ "): " ^ commas (map string_for_rep Rs) ^ ".") |
|
393 |
| TERM (loc, ts) => |
|
394 |
error ("Invalid term" ^ plural_s_for_list ts ^ |
|
395 |
" (" ^ quote loc ^ "): " ^ |
|
396 |
commas (map (Syntax.string_of_term ctxt) ts) ^ ".") |
|
397 |
| TYPE (loc, Ts, ts) => |
|
398 |
error ("Invalid type" ^ plural_s_for_list Ts ^ |
|
399 |
(if null ts then |
|
400 |
"" |
|
401 |
else |
|
402 |
" for term" ^ plural_s_for_list ts ^ " " ^ |
|
403 |
commas (map (quote o Syntax.string_of_term ctxt) ts)) ^ |
|
404 |
" (" ^ quote loc ^ "): " ^ |
|
405 |
commas (map (Syntax.string_of_typ ctxt) Ts) ^ ".") |
|
406 |
| Kodkod.SYNTAX (_, details) => |
|
407 |
(warning ("Ill-formed Kodkodi output: " ^ details ^ "."); x) |
|
408 |
| Refute.REFUTE (loc, details) => |
|
409 |
error ("Unhandled Refute error (" ^ quote loc ^ "): " ^ details ^ ".") |
|
410 |
||
33561
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
411 |
(* raw_param list -> bool -> int -> Proof.state -> bool * Proof.state *) |
33192 | 412 |
fun pick_nits override_params auto subgoal state = |
413 |
let |
|
414 |
val thy = Proof.theory_of state |
|
415 |
val ctxt = Proof.context_of state |
|
33292 | 416 |
val thm = #goal (Proof.raw_goal state) |
33192 | 417 |
val _ = List.app check_raw_param override_params |
418 |
val params as {blocking, debug, ...} = |
|
419 |
extract_params ctxt auto (default_raw_params thy) override_params |
|
33561
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
420 |
(* unit -> bool * Proof.state *) |
33192 | 421 |
fun go () = |
33561
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
422 |
(false, state) |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
423 |
|> (if auto then perhaps o try |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
424 |
else if debug then fn f => fn x => f x |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
425 |
else handle_exceptions ctxt) |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
426 |
(fn (_, state) => pick_nits_in_subgoal state params auto subgoal |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
427 |
|>> equal "genuine") |
33192 | 428 |
in |
33561
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
429 |
if auto orelse blocking then go () |
33604
d4220df6fde2
Toplevel.thread provides Isar-style exception output;
wenzelm
parents:
33601
diff
changeset
|
430 |
else (Toplevel.thread true (fn () => (go (); ())); (false, state)) |
33192 | 431 |
end |
432 |
||
433 |
(* (TableFun().key * string list) list option * int option |
|
434 |
-> Toplevel.transition -> Toplevel.transition *) |
|
435 |
fun nitpick_trans (opt_params, opt_subgoal) = |
|
436 |
Toplevel.keep (K () |
|
33561
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
437 |
o snd o pick_nits (these opt_params) false (the_default 1 opt_subgoal) |
33192 | 438 |
o Toplevel.proof_of) |
439 |
||
440 |
(* raw_param -> string *) |
|
441 |
fun string_for_raw_param (name, value) = |
|
442 |
name ^ " = " ^ stringify_raw_param_value value |
|
443 |
||
444 |
(* (TableFun().key * string) list option -> Toplevel.transition |
|
445 |
-> Toplevel.transition *) |
|
446 |
fun nitpick_params_trans opt_params = |
|
447 |
Toplevel.theory |
|
33561
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
448 |
(fold set_default_raw_param (these opt_params) |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
449 |
#> tap (fn thy => |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
450 |
writeln ("Default parameters for Nitpick:\n" ^ |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
451 |
(case rev (default_raw_params thy) of |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
452 |
[] => "none" |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
453 |
| params => |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
454 |
(map check_raw_param params; |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
455 |
params |> map string_for_raw_param |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
456 |
|> sort_strings |> cat_lines))))) |
33192 | 457 |
|
458 |
(* OuterParse.token list |
|
459 |
-> (Toplevel.transition -> Toplevel.transition) * OuterParse.token list *) |
|
33561
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
460 |
val scan_nitpick_command = |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
461 |
(scan_params -- Scan.option OuterParse.nat) #>> nitpick_trans |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
462 |
val scan_nitpick_params_command = scan_params #>> nitpick_params_trans |
33192 | 463 |
|
464 |
val _ = OuterSyntax.improper_command "nitpick" |
|
465 |
"try to find a counterexample for a given subgoal using Kodkod" |
|
466 |
OuterKeyword.diag scan_nitpick_command |
|
467 |
val _ = OuterSyntax.command "nitpick_params" |
|
468 |
"set and display the default parameters for Nitpick" |
|
469 |
OuterKeyword.thy_decl scan_nitpick_params_command |
|
470 |
||
33561
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
471 |
(* Proof.state -> bool * Proof.state *) |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
472 |
fun auto_nitpick state = |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
473 |
if not (!auto) then (false, state) else pick_nits [] true 1 state |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
474 |
|
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
475 |
val setup = Auto_Counterexample.register_tool ("nitpick", auto_nitpick) |
ab01b72715ef
introduced Auto Nitpick in addition to Auto Quickcheck;
blanchet
parents:
33556
diff
changeset
|
476 |
|
33192 | 477 |
end; |