wenzelm@30826
|
1 |
(* Title: Tools/quickcheck.ML
|
bulwahn@40481
|
2 |
Author: Stefan Berghofer, Florian Haftmann, Lukas Bulwahn, TU Muenchen
|
haftmann@28256
|
3 |
|
haftmann@28256
|
4 |
Generic counterexample search engine.
|
haftmann@28256
|
5 |
*)
|
haftmann@28256
|
6 |
|
haftmann@28256
|
7 |
signature QUICKCHECK =
|
haftmann@28256
|
8 |
sig
|
bulwahn@38150
|
9 |
val setup: theory -> theory
|
bulwahn@38150
|
10 |
(* configuration *)
|
wenzelm@32740
|
11 |
val auto: bool Unsynchronized.ref
|
bulwahn@34935
|
12 |
val timing : bool Unsynchronized.ref
|
wenzelm@41765
|
13 |
val tester : string Config.T
|
bulwahn@40892
|
14 |
val size : int Config.T
|
bulwahn@40892
|
15 |
val iterations : int Config.T
|
bulwahn@40892
|
16 |
val no_assms : bool Config.T
|
bulwahn@40892
|
17 |
val report : bool Config.T
|
bulwahn@40892
|
18 |
val quiet : bool Config.T
|
bulwahn@40892
|
19 |
val timeout : real Config.T
|
bulwahn@40896
|
20 |
val finite_types : bool Config.T
|
bulwahn@40896
|
21 |
val finite_type_size : int Config.T
|
bulwahn@35378
|
22 |
datatype report = Report of
|
bulwahn@35378
|
23 |
{ iterations : int, raised_match_errors : int,
|
bulwahn@35378
|
24 |
satisfied_assms : int list, positive_concl_tests : int }
|
wenzelm@41765
|
25 |
datatype expectation = No_Expectation | No_Counterexample | Counterexample;
|
bulwahn@40892
|
26 |
datatype test_params = Test_Params of {default_type: typ list, expect : expectation};
|
bulwahn@40481
|
27 |
val test_params_of : Proof.context -> test_params
|
bulwahn@40892
|
28 |
val map_test_params : (typ list * expectation -> typ list * expectation)
|
bulwahn@40481
|
29 |
-> Context.generic -> Context.generic
|
bulwahn@38150
|
30 |
val add_generator:
|
bulwahn@41155
|
31 |
string * (Proof.context -> term -> int -> term list option * report option)
|
bulwahn@39479
|
32 |
-> Context.generic -> Context.generic
|
bulwahn@42733
|
33 |
val add_batch_generator:
|
bulwahn@42733
|
34 |
string * (Proof.context -> term list -> (int -> term list option) list)
|
bulwahn@42733
|
35 |
-> Context.generic -> Context.generic
|
bulwahn@38150
|
36 |
(* testing terms and proof states *)
|
bulwahn@42625
|
37 |
val test_term: Proof.context -> bool * bool -> term ->
|
bulwahn@41153
|
38 |
(string * term) list option * ((string * int) list * ((int * report) list) option)
|
bulwahn@40896
|
39 |
val test_goal_terms:
|
bulwahn@42625
|
40 |
Proof.context -> bool * bool -> (string * typ) list -> term list
|
bulwahn@41153
|
41 |
-> (string * term) list option * ((string * int) list * ((int * report) list) option) list
|
bulwahn@38149
|
42 |
val quickcheck: (string * string list) list -> int -> Proof.state -> (string * term) list option
|
bulwahn@42733
|
43 |
(* testing a batch of terms *)
|
bulwahn@42733
|
44 |
val test_terms: Proof.context -> term list -> (string * term) list option list option
|
haftmann@28256
|
45 |
end;
|
haftmann@28256
|
46 |
|
haftmann@28256
|
47 |
structure Quickcheck : QUICKCHECK =
|
haftmann@28256
|
48 |
struct
|
haftmann@28256
|
49 |
|
wenzelm@30981
|
50 |
(* preferences *)
|
wenzelm@30981
|
51 |
|
wenzelm@32740
|
52 |
val auto = Unsynchronized.ref false;
|
wenzelm@30981
|
53 |
|
bulwahn@34935
|
54 |
val timing = Unsynchronized.ref false;
|
bulwahn@34935
|
55 |
|
wenzelm@30981
|
56 |
val _ =
|
wenzelm@30981
|
57 |
ProofGeneralPgip.add_preference Preferences.category_tracing
|
wenzelm@39862
|
58 |
(Unsynchronized.setmp auto true (fn () =>
|
wenzelm@30981
|
59 |
Preferences.bool_pref auto
|
wenzelm@30981
|
60 |
"auto-quickcheck"
|
blanchet@39575
|
61 |
"Run Quickcheck automatically.") ());
|
wenzelm@30981
|
62 |
|
bulwahn@35378
|
63 |
(* quickcheck report *)
|
bulwahn@35378
|
64 |
|
bulwahn@35378
|
65 |
datatype report = Report of
|
bulwahn@35378
|
66 |
{ iterations : int, raised_match_errors : int,
|
bulwahn@35378
|
67 |
satisfied_assms : int list, positive_concl_tests : int }
|
bulwahn@35378
|
68 |
|
bulwahn@38169
|
69 |
(* expectation *)
|
bulwahn@38169
|
70 |
|
wenzelm@41765
|
71 |
datatype expectation = No_Expectation | No_Counterexample | Counterexample;
|
bulwahn@38169
|
72 |
|
bulwahn@38169
|
73 |
fun merge_expectation (expect1, expect2) =
|
bulwahn@38169
|
74 |
if expect1 = expect2 then expect1 else No_Expectation
|
bulwahn@38169
|
75 |
|
haftmann@28315
|
76 |
(* quickcheck configuration -- default parameters, test generators *)
|
bulwahn@41152
|
77 |
val (tester, setup_tester) = Attrib.config_string "quickcheck_tester" (K "")
|
bulwahn@40892
|
78 |
val (size, setup_size) = Attrib.config_int "quickcheck_size" (K 10)
|
bulwahn@40892
|
79 |
val (iterations, setup_iterations) = Attrib.config_int "quickcheck_iterations" (K 100)
|
bulwahn@40892
|
80 |
val (no_assms, setup_no_assms) = Attrib.config_bool "quickcheck_no_assms" (K false)
|
bulwahn@40892
|
81 |
val (report, setup_report) = Attrib.config_bool "quickcheck_report" (K true)
|
bulwahn@40892
|
82 |
val (quiet, setup_quiet) = Attrib.config_bool "quickcheck_quiet" (K false)
|
bulwahn@40892
|
83 |
val (timeout, setup_timeout) = Attrib.config_real "quickcheck_timeout" (K 30.0)
|
bulwahn@40894
|
84 |
val (finite_types, setup_finite_types) = Attrib.config_bool "quickcheck_finite_types" (K true)
|
wenzelm@41765
|
85 |
val (finite_type_size, setup_finite_type_size) =
|
wenzelm@41765
|
86 |
Attrib.config_int "quickcheck_finite_type_size" (K 3)
|
haftmann@28315
|
87 |
|
bulwahn@40892
|
88 |
val setup_config =
|
bulwahn@41152
|
89 |
setup_tester #> setup_size #> setup_iterations #> setup_no_assms #> setup_report #> setup_quiet
|
bulwahn@41152
|
90 |
#> setup_timeout #> setup_finite_types #> setup_finite_type_size
|
bulwahn@40894
|
91 |
|
haftmann@28309
|
92 |
datatype test_params = Test_Params of
|
bulwahn@40892
|
93 |
{default_type: typ list, expect : expectation};
|
haftmann@28309
|
94 |
|
bulwahn@40892
|
95 |
fun dest_test_params (Test_Params {default_type, expect}) = (default_type, expect);
|
wenzelm@39034
|
96 |
|
wenzelm@41765
|
97 |
fun make_test_params (default_type, expect) =
|
wenzelm@41765
|
98 |
Test_Params {default_type = default_type, expect = expect};
|
wenzelm@39034
|
99 |
|
wenzelm@41765
|
100 |
fun map_test_params' f (Test_Params {default_type, expect}) =
|
wenzelm@41765
|
101 |
make_test_params (f (default_type, expect));
|
wenzelm@39034
|
102 |
|
wenzelm@39034
|
103 |
fun merge_test_params
|
wenzelm@41720
|
104 |
(Test_Params {default_type = default_type1, expect = expect1},
|
wenzelm@41720
|
105 |
Test_Params {default_type = default_type2, expect = expect2}) =
|
wenzelm@41720
|
106 |
make_test_params
|
wenzelm@41720
|
107 |
(merge (op =) (default_type1, default_type2), merge_expectation (expect1, expect2));
|
haftmann@28309
|
108 |
|
bulwahn@39479
|
109 |
structure Data = Generic_Data
|
wenzelm@33522
|
110 |
(
|
wenzelm@39034
|
111 |
type T =
|
bulwahn@42733
|
112 |
((string * (Proof.context -> term -> int -> term list option * report option)) list
|
bulwahn@42733
|
113 |
* (string * (Proof.context -> term list -> (int -> term list option) list)) list)
|
wenzelm@39034
|
114 |
* test_params;
|
bulwahn@42733
|
115 |
val empty = (([], []), Test_Params {default_type = [], expect = No_Expectation});
|
haftmann@28256
|
116 |
val extend = I;
|
bulwahn@42733
|
117 |
fun merge (((generators1, batch_generators1), params1), ((generators2, batch_generators2), params2)) : T =
|
bulwahn@42733
|
118 |
((AList.merge (op =) (K true) (generators1, generators2),
|
bulwahn@42733
|
119 |
AList.merge (op =) (K true) (batch_generators1, batch_generators2)),
|
haftmann@28309
|
120 |
merge_test_params (params1, params2));
|
wenzelm@33522
|
121 |
);
|
haftmann@28256
|
122 |
|
bulwahn@39479
|
123 |
val test_params_of = snd o Data.get o Context.Proof;
|
bulwahn@38150
|
124 |
|
bulwahn@40892
|
125 |
val default_type = fst o dest_test_params o test_params_of
|
bulwahn@40481
|
126 |
|
bulwahn@40892
|
127 |
val expect = snd o dest_test_params o test_params_of
|
bulwahn@40481
|
128 |
|
bulwahn@40481
|
129 |
val map_test_params = Data.map o apsnd o map_test_params'
|
bulwahn@39480
|
130 |
|
bulwahn@42733
|
131 |
val add_generator = Data.map o apfst o apfst o AList.update (op =);
|
bulwahn@42733
|
132 |
|
bulwahn@42733
|
133 |
val add_batch_generator = Data.map o apfst o apsnd o AList.update (op =);
|
haftmann@28256
|
134 |
|
haftmann@28315
|
135 |
(* generating tests *)
|
haftmann@28315
|
136 |
|
bulwahn@42733
|
137 |
fun gen_mk_tester lookup ctxt v =
|
haftmann@28309
|
138 |
let
|
bulwahn@41153
|
139 |
val name = Config.get ctxt tester
|
bulwahn@42733
|
140 |
val tester = case lookup ctxt name
|
bulwahn@41153
|
141 |
of NONE => error ("No such quickcheck tester: " ^ name)
|
bulwahn@41153
|
142 |
| SOME tester => tester ctxt;
|
wenzelm@40491
|
143 |
in
|
bulwahn@41153
|
144 |
if Config.get ctxt quiet then
|
bulwahn@42733
|
145 |
try tester v
|
bulwahn@41153
|
146 |
else
|
bulwahn@41153
|
147 |
let
|
bulwahn@42733
|
148 |
val tester = Exn.interruptible_capture tester v
|
bulwahn@41153
|
149 |
in case Exn.get_result tester of
|
bulwahn@41153
|
150 |
NONE => SOME (Exn.release tester)
|
bulwahn@41153
|
151 |
| SOME tester => SOME tester
|
bulwahn@41153
|
152 |
end
|
bulwahn@41153
|
153 |
end
|
haftmann@28315
|
154 |
|
bulwahn@42733
|
155 |
val mk_tester = gen_mk_tester (fn ctxt =>
|
bulwahn@42733
|
156 |
AList.lookup (op =) ((fst o fst o Data.get o Context.Proof) ctxt))
|
bulwahn@42733
|
157 |
val mk_batch_tester = gen_mk_tester (fn ctxt => AList.lookup (op =) ((snd o fst o Data.get o Context.Proof) ctxt))
|
bulwahn@42733
|
158 |
|
haftmann@28315
|
159 |
(* testing propositions *)
|
haftmann@28315
|
160 |
|
haftmann@28309
|
161 |
fun prep_test_term t =
|
haftmann@28309
|
162 |
let
|
wenzelm@29266
|
163 |
val _ = (null (Term.add_tvars t []) andalso null (Term.add_tfrees t [])) orelse
|
haftmann@28309
|
164 |
error "Term to be tested contains type variables";
|
wenzelm@29266
|
165 |
val _ = null (Term.add_vars t []) orelse
|
haftmann@28309
|
166 |
error "Term to be tested contains schematic variables";
|
haftmann@31138
|
167 |
val frees = Term.add_frees t [];
|
bulwahn@41334
|
168 |
in (frees, list_abs_free (frees, t)) end
|
haftmann@28309
|
169 |
|
bulwahn@35324
|
170 |
fun cpu_time description f =
|
bulwahn@35324
|
171 |
let
|
bulwahn@35324
|
172 |
val start = start_timing ()
|
bulwahn@35324
|
173 |
val result = Exn.capture f ()
|
bulwahn@35324
|
174 |
val time = Time.toMilliseconds (#cpu (end_timing start))
|
bulwahn@35324
|
175 |
in (Exn.release result, (description, time)) end
|
wenzelm@41765
|
176 |
|
bulwahn@42625
|
177 |
fun limit ctxt (limit_time, is_interactive) f exc () =
|
bulwahn@42625
|
178 |
if limit_time then
|
bulwahn@42625
|
179 |
TimeLimit.timeLimit (seconds (Config.get ctxt timeout)) f ()
|
bulwahn@42625
|
180 |
handle TimeLimit.TimeOut =>
|
bulwahn@42625
|
181 |
if is_interactive then exc () else raise TimeLimit.TimeOut
|
bulwahn@42625
|
182 |
else
|
bulwahn@42625
|
183 |
f ()
|
bulwahn@42624
|
184 |
|
bulwahn@42625
|
185 |
fun test_term ctxt (limit_time, is_interactive) t =
|
haftmann@28309
|
186 |
let
|
bulwahn@41334
|
187 |
val (names, t') = apfst (map fst) (prep_test_term t);
|
bulwahn@40612
|
188 |
val current_size = Unsynchronized.ref 0
|
bulwahn@40612
|
189 |
fun excipit s =
|
bulwahn@40612
|
190 |
"Quickcheck " ^ s ^ " while testing at size " ^ string_of_int (!current_size)
|
bulwahn@41153
|
191 |
val (test_fun, comp_time) = cpu_time "quickcheck compilation" (fn () => mk_tester ctxt t');
|
bulwahn@41155
|
192 |
fun with_size test_fun k reports =
|
bulwahn@41155
|
193 |
if k > Config.get ctxt size then
|
bulwahn@41155
|
194 |
(NONE, reports)
|
bulwahn@41155
|
195 |
else
|
bulwahn@35378
|
196 |
let
|
bulwahn@41155
|
197 |
val _ = if Config.get ctxt quiet then () else Output.urgent_message
|
bulwahn@41155
|
198 |
("Test data size: " ^ string_of_int k)
|
bulwahn@40896
|
199 |
val _ = current_size := k
|
bulwahn@41155
|
200 |
val ((result, new_report), timing) =
|
bulwahn@41155
|
201 |
cpu_time ("size " ^ string_of_int k) (fn () => test_fun (k - 1))
|
bulwahn@41155
|
202 |
val reports = case new_report of NONE => reports | SOME rep => (k, rep) :: reports
|
wenzelm@41765
|
203 |
in
|
wenzelm@41765
|
204 |
case result of NONE => with_size test_fun (k + 1) reports | SOME q => (SOME q, reports)
|
wenzelm@41765
|
205 |
end;
|
bulwahn@34935
|
206 |
in
|
wenzelm@41765
|
207 |
case test_fun of NONE => (NONE, ([comp_time], NONE))
|
bulwahn@41153
|
208 |
| SOME test_fun =>
|
bulwahn@42625
|
209 |
limit ctxt (limit_time, is_interactive) (fn () =>
|
bulwahn@41153
|
210 |
let
|
bulwahn@41153
|
211 |
val ((result, reports), exec_time) =
|
bulwahn@41153
|
212 |
cpu_time "quickcheck execution" (fn () => with_size test_fun 1 [])
|
bulwahn@41153
|
213 |
in
|
bulwahn@41153
|
214 |
(case result of NONE => NONE | SOME ts => SOME (names ~~ ts),
|
bulwahn@41160
|
215 |
([exec_time, comp_time],
|
bulwahn@41160
|
216 |
if Config.get ctxt report andalso not (null reports) then SOME reports else NONE))
|
bulwahn@42624
|
217 |
end) (fn () => error (excipit "ran out of time")) ()
|
haftmann@28309
|
218 |
end;
|
haftmann@28309
|
219 |
|
bulwahn@42733
|
220 |
fun test_terms ctxt ts =
|
bulwahn@42733
|
221 |
let
|
bulwahn@42733
|
222 |
val (namess, ts') = split_list (map (fn t => apfst (map fst) (prep_test_term t)) ts)
|
bulwahn@42733
|
223 |
val test_funs = mk_batch_tester ctxt ts'
|
bulwahn@42733
|
224 |
fun with_size tester k =
|
bulwahn@42733
|
225 |
if k > Config.get ctxt size then NONE
|
bulwahn@42733
|
226 |
else case tester k of SOME ts => SOME ts | NONE => with_size tester (k + 1)
|
bulwahn@42733
|
227 |
val results =
|
bulwahn@42733
|
228 |
Option.map (map (fn test_fun => TimeLimit.timeLimit (seconds (Config.get ctxt timeout))
|
bulwahn@42733
|
229 |
(fn () => with_size test_fun 1) ()
|
bulwahn@42733
|
230 |
handle TimeLimit.TimeOut => NONE)) test_funs
|
bulwahn@42733
|
231 |
in
|
bulwahn@42733
|
232 |
Option.map (map2 (fn names => Option.map (fn ts => names ~~ ts)) namess) results
|
bulwahn@42733
|
233 |
end
|
bulwahn@42733
|
234 |
|
bulwahn@41334
|
235 |
(* FIXME: this function shows that many assumptions are made upon the generation *)
|
bulwahn@41334
|
236 |
(* In the end there is probably no generic quickcheck interface left... *)
|
bulwahn@41334
|
237 |
|
bulwahn@42625
|
238 |
fun test_term_with_increasing_cardinality ctxt (limit_time, is_interactive) ts =
|
bulwahn@41291
|
239 |
let
|
bulwahn@41334
|
240 |
val thy = ProofContext.theory_of ctxt
|
bulwahn@41334
|
241 |
val (freess, ts') = split_list (map prep_test_term ts)
|
bulwahn@41334
|
242 |
val Ts = map snd (hd freess)
|
bulwahn@41291
|
243 |
val (test_funs, comp_time) = cpu_time "quickcheck compilation"
|
bulwahn@41291
|
244 |
(fn () => map (mk_tester ctxt) ts')
|
bulwahn@41291
|
245 |
fun test_card_size (card, size) =
|
bulwahn@41291
|
246 |
(* FIXME: why decrement size by one? *)
|
bulwahn@41291
|
247 |
case fst (the (nth test_funs (card - 1)) (size - 1)) of
|
bulwahn@41334
|
248 |
SOME ts => SOME (map fst (nth freess (card - 1)) ~~ ts)
|
bulwahn@41291
|
249 |
| NONE => NONE
|
bulwahn@41291
|
250 |
val enumeration_card_size =
|
bulwahn@41334
|
251 |
if forall (fn T => Sign.of_sort thy (T, ["Enum.enum"])) Ts then
|
bulwahn@41334
|
252 |
(* size does not matter *)
|
bulwahn@41334
|
253 |
map (rpair 0) (1 upto (length ts))
|
bulwahn@41334
|
254 |
else
|
bulwahn@41334
|
255 |
(* size does matter *)
|
bulwahn@41334
|
256 |
map_product pair (1 upto (length ts)) (1 upto (Config.get ctxt size))
|
bulwahn@41334
|
257 |
|> sort (fn ((c1, s1), (c2, s2)) => int_ord ((c1 + s1), (c2 + s2)))
|
bulwahn@41291
|
258 |
in
|
bulwahn@41291
|
259 |
if (forall is_none test_funs) then
|
bulwahn@41291
|
260 |
(NONE, ([comp_time], NONE))
|
bulwahn@41291
|
261 |
else if (forall is_some test_funs) then
|
bulwahn@42625
|
262 |
limit ctxt (limit_time, is_interactive) (fn () =>
|
bulwahn@42624
|
263 |
(get_first test_card_size enumeration_card_size, ([comp_time], NONE)))
|
bulwahn@42624
|
264 |
(fn () => error "Quickcheck ran out of time") ()
|
bulwahn@41291
|
265 |
else
|
wenzelm@41765
|
266 |
error "Unexpectedly, testers of some cardinalities are executable but others are not"
|
bulwahn@41291
|
267 |
end
|
bulwahn@41291
|
268 |
|
bulwahn@40895
|
269 |
fun get_finite_types ctxt =
|
bulwahn@40895
|
270 |
fst (chop (Config.get ctxt finite_type_size)
|
bulwahn@40895
|
271 |
(map (Type o rpair []) ["Enum.finite_1", "Enum.finite_2", "Enum.finite_3",
|
wenzelm@41765
|
272 |
"Enum.finite_4", "Enum.finite_5"]))
|
bulwahn@40895
|
273 |
|
bulwahn@38153
|
274 |
exception WELLSORTED of string
|
bulwahn@38153
|
275 |
|
wenzelm@41765
|
276 |
fun monomorphic_term thy insts default_T =
|
haftmann@28309
|
277 |
let
|
haftmann@28309
|
278 |
fun subst (T as TFree (v, S)) =
|
bulwahn@41147
|
279 |
let
|
bulwahn@41147
|
280 |
val T' = AList.lookup (op =) insts v
|
bulwahn@41147
|
281 |
|> the_default default_T
|
bulwahn@41147
|
282 |
in if Sign.of_sort thy (T', S) then T'
|
wenzelm@41765
|
283 |
else raise (WELLSORTED ("For instantiation with default_type " ^
|
wenzelm@41765
|
284 |
Syntax.string_of_typ_global thy default_T ^
|
bulwahn@41147
|
285 |
":\n" ^ Syntax.string_of_typ_global thy T' ^
|
bulwahn@41147
|
286 |
" to be substituted for variable " ^
|
bulwahn@41147
|
287 |
Syntax.string_of_typ_global thy T ^ " does not have sort " ^
|
bulwahn@41147
|
288 |
Syntax.string_of_sort_global thy S))
|
bulwahn@41147
|
289 |
end
|
haftmann@28309
|
290 |
| subst T = T;
|
haftmann@28309
|
291 |
in (map_types o map_atyps) subst end;
|
haftmann@28309
|
292 |
|
bulwahn@38153
|
293 |
datatype wellsorted_error = Wellsorted_Error of string | Term of term
|
bulwahn@38153
|
294 |
|
bulwahn@42625
|
295 |
fun test_goal_terms lthy (limit_time, is_interactive) insts check_goals =
|
haftmann@28309
|
296 |
let
|
wenzelm@41765
|
297 |
val thy = ProofContext.theory_of lthy
|
bulwahn@41170
|
298 |
val default_insts =
|
bulwahn@41170
|
299 |
if Config.get lthy finite_types then (get_finite_types lthy) else (default_type lthy)
|
bulwahn@40895
|
300 |
val inst_goals =
|
bulwahn@41291
|
301 |
map (fn check_goal =>
|
bulwahn@41170
|
302 |
if not (null (Term.add_tfree_names check_goal [])) then
|
bulwahn@41170
|
303 |
map (fn T =>
|
wenzelm@41765
|
304 |
(pair (SOME T) o Term o Object_Logic.atomize_term thy o monomorphic_term thy insts T)
|
wenzelm@41765
|
305 |
check_goal
|
bulwahn@41291
|
306 |
handle WELLSORTED s => (SOME T, Wellsorted_Error s)) default_insts
|
bulwahn@41170
|
307 |
else
|
bulwahn@41291
|
308 |
[(NONE, Term (Object_Logic.atomize_term thy check_goal))]) check_goals
|
wenzelm@41765
|
309 |
val error_msg =
|
wenzelm@41765
|
310 |
cat_lines
|
wenzelm@41765
|
311 |
(maps (map_filter (fn (_, Term t) => NONE | (_, Wellsorted_Error s) => SOME s)) inst_goals)
|
bulwahn@41291
|
312 |
fun is_wellsorted_term (T, Term t) = SOME (T, t)
|
bulwahn@41291
|
313 |
| is_wellsorted_term (_, Wellsorted_Error s) = NONE
|
bulwahn@38153
|
314 |
val correct_inst_goals =
|
bulwahn@41291
|
315 |
case map (map_filter is_wellsorted_term) inst_goals of
|
bulwahn@41291
|
316 |
[[]] => error error_msg
|
bulwahn@38153
|
317 |
| xs => xs
|
bulwahn@40892
|
318 |
val _ = if Config.get lthy quiet then () else warning error_msg
|
bulwahn@38152
|
319 |
fun collect_results f reports [] = (NONE, rev reports)
|
bulwahn@38152
|
320 |
| collect_results f reports (t :: ts) =
|
bulwahn@38152
|
321 |
case f t of
|
bulwahn@38152
|
322 |
(SOME res, report) => (SOME res, rev (report :: reports))
|
bulwahn@38152
|
323 |
| (NONE, report) => collect_results f (report :: reports) ts
|
bulwahn@41291
|
324 |
fun test_term' goal =
|
bulwahn@41291
|
325 |
case goal of
|
bulwahn@42625
|
326 |
[(NONE, t)] => test_term lthy (limit_time, is_interactive) t
|
bulwahn@42625
|
327 |
| ts => test_term_with_increasing_cardinality lthy (limit_time, is_interactive) (map snd ts)
|
bulwahn@41291
|
328 |
in
|
bulwahn@41291
|
329 |
if Config.get lthy finite_types then
|
bulwahn@41291
|
330 |
collect_results test_term' [] correct_inst_goals
|
bulwahn@41291
|
331 |
else
|
bulwahn@42625
|
332 |
collect_results (test_term lthy (limit_time, is_interactive)) [] (maps (map snd) correct_inst_goals)
|
bulwahn@41291
|
333 |
end;
|
bulwahn@38152
|
334 |
|
bulwahn@42625
|
335 |
fun test_goal (time_limit, is_interactive) insts i state =
|
bulwahn@40896
|
336 |
let
|
bulwahn@40896
|
337 |
val lthy = Proof.context_of state;
|
bulwahn@40896
|
338 |
val thy = Proof.theory_of state;
|
bulwahn@40896
|
339 |
fun strip (Const ("all", _) $ Abs (_, _, t)) = strip t
|
bulwahn@40896
|
340 |
| strip t = t;
|
bulwahn@40896
|
341 |
val {goal = st, ...} = Proof.raw_goal state;
|
bulwahn@40896
|
342 |
val (gi, frees) = Logic.goal_params (prop_of st) i;
|
bulwahn@40896
|
343 |
val some_locale = case (Option.map #target o Named_Target.peek) lthy
|
bulwahn@40896
|
344 |
of NONE => NONE
|
bulwahn@40896
|
345 |
| SOME "" => NONE
|
bulwahn@40896
|
346 |
| SOME locale => SOME locale;
|
bulwahn@40896
|
347 |
val assms = if Config.get lthy no_assms then [] else case some_locale
|
bulwahn@40896
|
348 |
of NONE => Assumption.all_assms_of lthy
|
bulwahn@40896
|
349 |
| SOME locale => Assumption.local_assms_of lthy (Locale.init locale thy);
|
bulwahn@40896
|
350 |
val proto_goal = Logic.list_implies (map Thm.term_of assms, subst_bounds (frees, strip gi));
|
bulwahn@40896
|
351 |
val check_goals = case some_locale
|
bulwahn@40896
|
352 |
of NONE => [proto_goal]
|
bulwahn@40896
|
353 |
| SOME locale => map (fn (_, phi) => Morphism.term phi proto_goal)
|
bulwahn@40896
|
354 |
(Locale.registrations_of (Context.Theory thy) (*FIXME*) locale);
|
bulwahn@40896
|
355 |
in
|
bulwahn@42625
|
356 |
test_goal_terms lthy (time_limit, is_interactive) insts check_goals
|
bulwahn@40896
|
357 |
end
|
bulwahn@40896
|
358 |
|
bulwahn@38152
|
359 |
(* pretty printing *)
|
haftmann@28315
|
360 |
|
blanchet@40466
|
361 |
fun tool_name auto = (if auto then "Auto " else "") ^ "Quickcheck"
|
blanchet@40466
|
362 |
|
blanchet@40466
|
363 |
fun pretty_counterex ctxt auto NONE = Pretty.str (tool_name auto ^ " found no counterexample.")
|
blanchet@40466
|
364 |
| pretty_counterex ctxt auto (SOME cex) =
|
blanchet@40466
|
365 |
Pretty.chunks (Pretty.str (tool_name auto ^ " found a counterexample:\n") ::
|
haftmann@28315
|
366 |
map (fn (s, t) =>
|
bulwahn@41160
|
367 |
Pretty.block [Pretty.str (s ^ " ="), Pretty.brk 1, Syntax.pretty_term ctxt t]) (rev cex));
|
haftmann@28315
|
368 |
|
bulwahn@35378
|
369 |
fun pretty_report (Report {iterations = iterations, raised_match_errors = raised_match_errors,
|
bulwahn@35378
|
370 |
satisfied_assms = satisfied_assms, positive_concl_tests = positive_concl_tests}) =
|
bulwahn@35378
|
371 |
let
|
bulwahn@35378
|
372 |
fun pretty_stat s i = Pretty.block ([Pretty.str (s ^ ": " ^ string_of_int i)])
|
bulwahn@35378
|
373 |
in
|
bulwahn@35378
|
374 |
([pretty_stat "iterations" iterations,
|
bulwahn@35378
|
375 |
pretty_stat "match exceptions" raised_match_errors]
|
wenzelm@41765
|
376 |
@ map_index
|
wenzelm@41765
|
377 |
(fn (i, n) => pretty_stat ("satisfied " ^ string_of_int (i + 1) ^ ". assumption") n)
|
bulwahn@35378
|
378 |
satisfied_assms
|
bulwahn@35378
|
379 |
@ [pretty_stat "positive conclusion tests" positive_concl_tests])
|
bulwahn@35378
|
380 |
end
|
bulwahn@35378
|
381 |
|
bulwahn@35380
|
382 |
fun pretty_reports ctxt (SOME reports) =
|
bulwahn@41160
|
383 |
Pretty.chunks (Pretty.fbrk :: Pretty.str "Quickcheck report:" ::
|
bulwahn@41153
|
384 |
maps (fn (size, report) =>
|
bulwahn@41153
|
385 |
Pretty.str ("size " ^ string_of_int size ^ ":") :: pretty_report report @ [Pretty.brk 1])
|
bulwahn@35378
|
386 |
(rev reports))
|
bulwahn@35380
|
387 |
| pretty_reports ctxt NONE = Pretty.str ""
|
bulwahn@35378
|
388 |
|
blanchet@40466
|
389 |
fun pretty_counterex_and_reports ctxt auto (cex, timing_and_reports) =
|
blanchet@40466
|
390 |
Pretty.chunks (pretty_counterex ctxt auto cex ::
|
blanchet@40466
|
391 |
map (pretty_reports ctxt) (map snd timing_and_reports))
|
haftmann@28315
|
392 |
|
haftmann@28315
|
393 |
(* automatic testing *)
|
haftmann@28309
|
394 |
|
blanchet@33552
|
395 |
fun auto_quickcheck state =
|
blanchet@41175
|
396 |
let
|
blanchet@41175
|
397 |
val ctxt = Proof.context_of state;
|
blanchet@41175
|
398 |
val res =
|
blanchet@41175
|
399 |
state
|
blanchet@41175
|
400 |
|> Proof.map_context (Config.put report false #> Config.put quiet true)
|
bulwahn@42625
|
401 |
|> try (test_goal (false, false) [] 1);
|
blanchet@41175
|
402 |
in
|
blanchet@41175
|
403 |
case res of
|
blanchet@41175
|
404 |
NONE => (false, state)
|
blanchet@41175
|
405 |
| SOME (NONE, report) => (false, state)
|
blanchet@41175
|
406 |
| SOME (cex, report) => (true, Proof.goal_message (K (Pretty.chunks [Pretty.str "",
|
blanchet@41175
|
407 |
Pretty.mark Markup.hilite (pretty_counterex ctxt true cex)])) state)
|
blanchet@41175
|
408 |
end
|
blanchet@33552
|
409 |
|
blanchet@41175
|
410 |
val setup = Auto_Tools.register_tool (auto, auto_quickcheck)
|
bulwahn@40892
|
411 |
#> setup_config
|
haftmann@28309
|
412 |
|
wenzelm@30981
|
413 |
(* Isar commands *)
|
haftmann@28315
|
414 |
|
haftmann@28336
|
415 |
fun read_nat s = case (Library.read_int o Symbol.explode) s
|
haftmann@28336
|
416 |
of (k, []) => if k >= 0 then k
|
haftmann@28336
|
417 |
else error ("Not a natural number: " ^ s)
|
haftmann@28336
|
418 |
| (_, _ :: _) => error ("Not a natural number: " ^ s);
|
bulwahn@38149
|
419 |
|
blanchet@34125
|
420 |
fun read_bool "false" = false
|
blanchet@34125
|
421 |
| read_bool "true" = true
|
blanchet@34125
|
422 |
| read_bool s = error ("Not a Boolean value: " ^ s)
|
haftmann@28315
|
423 |
|
bulwahn@40612
|
424 |
fun read_real s =
|
bulwahn@40612
|
425 |
case (Real.fromString s) of
|
bulwahn@40612
|
426 |
SOME s => s
|
bulwahn@40612
|
427 |
| NONE => error ("Not a real number: " ^ s)
|
bulwahn@40612
|
428 |
|
bulwahn@38169
|
429 |
fun read_expectation "no_expectation" = No_Expectation
|
wenzelm@41765
|
430 |
| read_expectation "no_counterexample" = No_Counterexample
|
bulwahn@38169
|
431 |
| read_expectation "counterexample" = Counterexample
|
wenzelm@41765
|
432 |
| read_expectation s = error ("Not an expectation value: " ^ s)
|
bulwahn@38169
|
433 |
|
bulwahn@42733
|
434 |
fun valid_tester_name genctxt = AList.defined (op =) (fst (fst (Data.get genctxt)))
|
bulwahn@41156
|
435 |
|
bulwahn@41156
|
436 |
fun parse_tester name genctxt =
|
bulwahn@41156
|
437 |
if valid_tester_name genctxt name then
|
bulwahn@41156
|
438 |
Config.put_generic tester name genctxt
|
bulwahn@41156
|
439 |
else
|
bulwahn@41156
|
440 |
error ("Unknown tester: " ^ name)
|
bulwahn@41156
|
441 |
|
bulwahn@41156
|
442 |
fun parse_test_param ("tester", [arg]) = parse_tester arg
|
bulwahn@41153
|
443 |
| parse_test_param ("size", [arg]) = Config.put_generic size (read_nat arg)
|
bulwahn@40892
|
444 |
| parse_test_param ("iterations", [arg]) = Config.put_generic iterations (read_nat arg)
|
bulwahn@40892
|
445 |
| parse_test_param ("default_type", arg) = (fn gen_ctxt =>
|
wenzelm@41765
|
446 |
map_test_params
|
wenzelm@41765
|
447 |
((apfst o K) (map (ProofContext.read_typ (Context.proof_of gen_ctxt)) arg)) gen_ctxt)
|
bulwahn@40892
|
448 |
| parse_test_param ("no_assms", [arg]) = Config.put_generic no_assms (read_bool arg)
|
bulwahn@40892
|
449 |
| parse_test_param ("expect", [arg]) = map_test_params ((apsnd o K) (read_expectation arg))
|
bulwahn@40892
|
450 |
| parse_test_param ("report", [arg]) = Config.put_generic report (read_bool arg)
|
bulwahn@40892
|
451 |
| parse_test_param ("quiet", [arg]) = Config.put_generic quiet (read_bool arg)
|
bulwahn@40892
|
452 |
| parse_test_param ("timeout", [arg]) = Config.put_generic timeout (read_real arg)
|
bulwahn@40894
|
453 |
| parse_test_param ("finite_types", [arg]) = Config.put_generic finite_types (read_bool arg)
|
wenzelm@41765
|
454 |
| parse_test_param ("finite_type_size", [arg]) =
|
wenzelm@41765
|
455 |
Config.put_generic finite_type_size (read_nat arg)
|
bulwahn@41156
|
456 |
| parse_test_param (name, _) = fn genctxt =>
|
bulwahn@41156
|
457 |
if valid_tester_name genctxt name then
|
bulwahn@41156
|
458 |
Config.put_generic tester name genctxt
|
bulwahn@41156
|
459 |
else error ("Unknown tester or test parameter: " ^ name);
|
haftmann@28315
|
460 |
|
bulwahn@41153
|
461 |
fun parse_test_param_inst (name, arg) (insts, ctxt) =
|
haftmann@28336
|
462 |
case try (ProofContext.read_typ ctxt) name
|
bulwahn@41153
|
463 |
of SOME (TFree (v, _)) => (apfst o AList.update (op =))
|
bulwahn@41153
|
464 |
(v, ProofContext.read_typ ctxt (the_single arg)) (insts, ctxt)
|
bulwahn@41153
|
465 |
| _ => (apsnd o Context.proof_map o parse_test_param) (name, arg) (insts, ctxt);
|
haftmann@28315
|
466 |
|
bulwahn@40892
|
467 |
fun quickcheck_params_cmd args = Context.theory_map (fold parse_test_param args);
|
wenzelm@41765
|
468 |
|
bulwahn@35378
|
469 |
fun gen_quickcheck args i state =
|
bulwahn@40892
|
470 |
state
|
bulwahn@41153
|
471 |
|> Proof.map_context_result (fn ctxt => fold parse_test_param_inst args ([], ctxt))
|
bulwahn@42625
|
472 |
|> (fn (insts, state') => test_goal (true, true) insts i state'
|
bulwahn@40892
|
473 |
|> tap (fn (SOME x, _) => if expect (Proof.context_of state') = No_Counterexample then
|
bulwahn@40892
|
474 |
error ("quickcheck expected to find no counterexample but found one") else ()
|
bulwahn@40892
|
475 |
| (NONE, _) => if expect (Proof.context_of state') = Counterexample then
|
bulwahn@40892
|
476 |
error ("quickcheck expected to find a counterexample but did not find one") else ()))
|
boehmes@32295
|
477 |
|
wenzelm@36970
|
478 |
fun quickcheck args i state = fst (gen_quickcheck args i state);
|
bulwahn@35378
|
479 |
|
boehmes@32295
|
480 |
fun quickcheck_cmd args i state =
|
bulwahn@35378
|
481 |
gen_quickcheck args i (Toplevel.proof_of state)
|
blanchet@40466
|
482 |
|> Pretty.writeln o pretty_counterex_and_reports (Toplevel.context_of state) false;
|
haftmann@28309
|
483 |
|
wenzelm@41765
|
484 |
val parse_arg =
|
wenzelm@41765
|
485 |
Parse.name --
|
wenzelm@41765
|
486 |
(Scan.optional (Parse.$$$ "=" |--
|
wenzelm@41765
|
487 |
(((Parse.name || Parse.float_number) >> single) ||
|
wenzelm@41765
|
488 |
(Parse.$$$ "[" |-- Parse.list1 Parse.name --| Parse.$$$ "]"))) ["true"]);
|
haftmann@28309
|
489 |
|
wenzelm@41765
|
490 |
val parse_args =
|
wenzelm@41765
|
491 |
Parse.$$$ "[" |-- Parse.list1 parse_arg --| Parse.$$$ "]" || Scan.succeed [];
|
haftmann@28336
|
492 |
|
wenzelm@36970
|
493 |
val _ =
|
wenzelm@36970
|
494 |
Outer_Syntax.command "quickcheck_params" "set parameters for random testing" Keyword.thy_decl
|
wenzelm@36970
|
495 |
(parse_args >> (fn args => Toplevel.theory (quickcheck_params_cmd args)));
|
haftmann@28309
|
496 |
|
wenzelm@36970
|
497 |
val _ =
|
wenzelm@36970
|
498 |
Outer_Syntax.improper_command "quickcheck" "try to find counterexample for subgoal" Keyword.diag
|
wenzelm@36970
|
499 |
(parse_args -- Scan.optional Parse.nat 1
|
wenzelm@36970
|
500 |
>> (fn (args, i) => Toplevel.no_timing o Toplevel.keep (quickcheck_cmd args i)));
|
haftmann@28309
|
501 |
|
haftmann@28309
|
502 |
end;
|
haftmann@28256
|
503 |
|
haftmann@28315
|
504 |
|
haftmann@28315
|
505 |
val auto_quickcheck = Quickcheck.auto;
|