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
|
blanchet@43861
|
9 |
val quickcheckN: string
|
blanchet@43861
|
10 |
val genuineN: string
|
blanchet@43861
|
11 |
val noneN: string
|
blanchet@43861
|
12 |
val unknownN: string
|
bulwahn@38150
|
13 |
val setup: theory -> theory
|
bulwahn@38150
|
14 |
(* configuration *)
|
wenzelm@32740
|
15 |
val auto: bool Unsynchronized.ref
|
wenzelm@41765
|
16 |
val tester : string Config.T
|
bulwahn@40892
|
17 |
val size : int Config.T
|
bulwahn@40892
|
18 |
val iterations : int Config.T
|
bulwahn@40892
|
19 |
val no_assms : bool Config.T
|
bulwahn@40892
|
20 |
val report : bool Config.T
|
bulwahn@42952
|
21 |
val timing : bool Config.T
|
bulwahn@40892
|
22 |
val quiet : bool Config.T
|
bulwahn@40892
|
23 |
val timeout : real Config.T
|
bulwahn@40896
|
24 |
val finite_types : bool Config.T
|
bulwahn@40896
|
25 |
val finite_type_size : int Config.T
|
wenzelm@41765
|
26 |
datatype expectation = No_Expectation | No_Counterexample | Counterexample;
|
bulwahn@40892
|
27 |
datatype test_params = Test_Params of {default_type: typ list, expect : expectation};
|
bulwahn@40481
|
28 |
val test_params_of : Proof.context -> test_params
|
bulwahn@40892
|
29 |
val map_test_params : (typ list * expectation -> typ list * expectation)
|
bulwahn@40481
|
30 |
-> Context.generic -> Context.generic
|
bulwahn@42953
|
31 |
datatype report = Report of
|
bulwahn@42953
|
32 |
{ iterations : int, raised_match_errors : int,
|
bulwahn@42953
|
33 |
satisfied_assms : int list, positive_concl_tests : int }
|
bulwahn@42953
|
34 |
(* quickcheck's result *)
|
bulwahn@42952
|
35 |
datatype result =
|
bulwahn@42952
|
36 |
Result of
|
bulwahn@42952
|
37 |
{counterexample : (string * term) list option,
|
bulwahn@42952
|
38 |
evaluation_terms : (term * term) list option,
|
bulwahn@42952
|
39 |
timings : (string * int) list,
|
bulwahn@42952
|
40 |
reports : (int * report) list}
|
bulwahn@44185
|
41 |
val empty_result : result
|
bulwahn@44456
|
42 |
val add_timing : (string * int) -> result Unsynchronized.ref -> unit
|
bulwahn@42953
|
43 |
val counterexample_of : result -> (string * term) list option
|
bulwahn@42953
|
44 |
val timings_of : result -> (string * int) list
|
bulwahn@43953
|
45 |
(* registering generators *)
|
bulwahn@43953
|
46 |
val add_generator:
|
bulwahn@43953
|
47 |
string * (Proof.context -> (term * term list) list -> int list -> term list option * report option)
|
bulwahn@43953
|
48 |
-> Context.generic -> Context.generic
|
bulwahn@43953
|
49 |
val add_tester:
|
bulwahn@43953
|
50 |
string * (Proof.context -> bool * bool -> (string * typ) list -> (term * term list) list -> result list)
|
bulwahn@43953
|
51 |
-> Context.generic -> Context.generic
|
bulwahn@43953
|
52 |
val add_batch_generator:
|
bulwahn@43953
|
53 |
string * (Proof.context -> term list -> (int -> term list option) list)
|
bulwahn@43953
|
54 |
-> Context.generic -> Context.generic
|
bulwahn@43953
|
55 |
val add_batch_validator:
|
bulwahn@43953
|
56 |
string * (Proof.context -> term list -> (int -> bool) list)
|
bulwahn@43953
|
57 |
-> Context.generic -> Context.generic
|
bulwahn@43954
|
58 |
(* basic operations *)
|
bulwahn@44456
|
59 |
val limit : Time.time -> (bool * bool) -> (unit -> 'a) -> (unit -> 'a) -> unit -> 'a
|
bulwahn@43954
|
60 |
val instantiate_goals: Proof.context -> (string * typ) list -> (term * term list) list
|
bulwahn@43954
|
61 |
-> (typ option * (term * term list)) list list
|
bulwahn@43954
|
62 |
val collect_results: ('a -> result) -> 'a list -> result list -> result list
|
bulwahn@44746
|
63 |
val generator_test_goal_terms: Proof.context -> bool * bool -> (string * typ) list ->
|
bulwahn@44746
|
64 |
(term * term list) list -> result list
|
bulwahn@38150
|
65 |
(* testing terms and proof states *)
|
bulwahn@42952
|
66 |
val test_term: Proof.context -> bool * bool -> term * term list -> result
|
bulwahn@40896
|
67 |
val test_goal_terms:
|
bulwahn@42897
|
68 |
Proof.context -> bool * bool -> (string * typ) list -> (term * term list) list
|
bulwahn@42952
|
69 |
-> result list
|
bulwahn@38149
|
70 |
val quickcheck: (string * string list) list -> int -> Proof.state -> (string * term) list option
|
bulwahn@42733
|
71 |
(* testing a batch of terms *)
|
bulwahn@43059
|
72 |
val test_terms:
|
bulwahn@43059
|
73 |
Proof.context -> term list -> (string * term) list option list option * (string * int) list
|
bulwahn@43065
|
74 |
val validate_terms: Proof.context -> term list -> bool list option * (string * int) list
|
haftmann@28256
|
75 |
end;
|
haftmann@28256
|
76 |
|
haftmann@28256
|
77 |
structure Quickcheck : QUICKCHECK =
|
haftmann@28256
|
78 |
struct
|
haftmann@28256
|
79 |
|
blanchet@43861
|
80 |
val quickcheckN = "quickcheck"
|
blanchet@43861
|
81 |
val quickcheck_paramsN = "quickcheck_params"
|
blanchet@43861
|
82 |
|
blanchet@43861
|
83 |
val genuineN = "genuine"
|
blanchet@43861
|
84 |
val noneN = "none"
|
blanchet@43861
|
85 |
val unknownN = "unknown"
|
blanchet@43861
|
86 |
|
wenzelm@30981
|
87 |
(* preferences *)
|
wenzelm@30981
|
88 |
|
wenzelm@32740
|
89 |
val auto = Unsynchronized.ref false;
|
wenzelm@30981
|
90 |
|
wenzelm@30981
|
91 |
val _ =
|
wenzelm@30981
|
92 |
ProofGeneralPgip.add_preference Preferences.category_tracing
|
wenzelm@39862
|
93 |
(Unsynchronized.setmp auto true (fn () =>
|
wenzelm@30981
|
94 |
Preferences.bool_pref auto
|
wenzelm@30981
|
95 |
"auto-quickcheck"
|
blanchet@39575
|
96 |
"Run Quickcheck automatically.") ());
|
wenzelm@30981
|
97 |
|
bulwahn@35378
|
98 |
(* quickcheck report *)
|
bulwahn@35378
|
99 |
|
bulwahn@35378
|
100 |
datatype report = Report of
|
bulwahn@35378
|
101 |
{ iterations : int, raised_match_errors : int,
|
bulwahn@35378
|
102 |
satisfied_assms : int list, positive_concl_tests : int }
|
bulwahn@35378
|
103 |
|
bulwahn@42952
|
104 |
(* Quickcheck Result *)
|
bulwahn@42952
|
105 |
|
bulwahn@42952
|
106 |
datatype result = Result of
|
bulwahn@42952
|
107 |
{ counterexample : (string * term) list option, evaluation_terms : (term * term) list option,
|
bulwahn@42952
|
108 |
timings : (string * int) list, reports : (int * report) list}
|
bulwahn@42952
|
109 |
|
bulwahn@42952
|
110 |
val empty_result =
|
bulwahn@42952
|
111 |
Result {counterexample = NONE, evaluation_terms = NONE, timings = [], reports = []}
|
bulwahn@42952
|
112 |
|
bulwahn@42952
|
113 |
fun counterexample_of (Result r) = #counterexample r
|
bulwahn@42952
|
114 |
|
bulwahn@42952
|
115 |
fun found_counterexample (Result r) = is_some (#counterexample r)
|
bulwahn@42952
|
116 |
|
bulwahn@42952
|
117 |
fun response_of (Result r) = case (#counterexample r, #evaluation_terms r) of
|
bulwahn@42952
|
118 |
(SOME ts, SOME evals) => SOME (ts, evals)
|
bulwahn@42952
|
119 |
| (NONE, NONE) => NONE
|
bulwahn@42952
|
120 |
|
bulwahn@42952
|
121 |
fun timings_of (Result r) = #timings r
|
bulwahn@42952
|
122 |
|
bulwahn@42952
|
123 |
fun set_reponse names eval_terms (SOME ts) (Result r) =
|
bulwahn@42952
|
124 |
let
|
bulwahn@42952
|
125 |
val (ts1, ts2) = chop (length names) ts
|
bulwahn@42952
|
126 |
in
|
bulwahn@42952
|
127 |
Result {counterexample = SOME (names ~~ ts1), evaluation_terms = SOME (eval_terms ~~ ts2),
|
bulwahn@42952
|
128 |
timings = #timings r, reports = #reports r}
|
bulwahn@42952
|
129 |
end
|
bulwahn@42952
|
130 |
| set_reponse _ _ NONE result = result
|
bulwahn@42952
|
131 |
|
bulwahn@44456
|
132 |
|
bulwahn@44456
|
133 |
fun set_counterexample counterexample (Result r) =
|
bulwahn@44456
|
134 |
Result {counterexample = counterexample, evaluation_terms = #evaluation_terms r,
|
bulwahn@44456
|
135 |
timings = #timings r, reports = #reports r}
|
bulwahn@44456
|
136 |
|
bulwahn@44456
|
137 |
fun set_evaluation_terms evaluation_terms (Result r) =
|
bulwahn@44456
|
138 |
Result {counterexample = #counterexample r, evaluation_terms = evaluation_terms,
|
bulwahn@44456
|
139 |
timings = #timings r, reports = #reports r}
|
bulwahn@44456
|
140 |
|
bulwahn@42952
|
141 |
fun cons_timing timing (Result r) =
|
bulwahn@42952
|
142 |
Result {counterexample = #counterexample r, evaluation_terms = #evaluation_terms r,
|
bulwahn@42952
|
143 |
timings = cons timing (#timings r), reports = #reports r}
|
bulwahn@42952
|
144 |
|
bulwahn@42952
|
145 |
fun cons_report size (SOME report) (Result r) =
|
bulwahn@42952
|
146 |
Result {counterexample = #counterexample r, evaluation_terms = #evaluation_terms r,
|
bulwahn@42952
|
147 |
timings = #timings r, reports = cons (size, report) (#reports r)}
|
bulwahn@42952
|
148 |
| cons_report _ NONE result = result
|
bulwahn@42952
|
149 |
|
wenzelm@43071
|
150 |
fun add_timing timing result_ref =
|
wenzelm@43071
|
151 |
Unsynchronized.change result_ref (cons_timing timing)
|
bulwahn@42952
|
152 |
|
wenzelm@43071
|
153 |
fun add_report size report result_ref =
|
wenzelm@43071
|
154 |
Unsynchronized.change result_ref (cons_report size report)
|
bulwahn@42952
|
155 |
|
bulwahn@42952
|
156 |
fun add_response names eval_terms response result_ref =
|
wenzelm@43071
|
157 |
Unsynchronized.change result_ref (set_reponse names eval_terms response)
|
bulwahn@42952
|
158 |
|
bulwahn@38169
|
159 |
(* expectation *)
|
bulwahn@38169
|
160 |
|
wenzelm@41765
|
161 |
datatype expectation = No_Expectation | No_Counterexample | Counterexample;
|
bulwahn@38169
|
162 |
|
bulwahn@38169
|
163 |
fun merge_expectation (expect1, expect2) =
|
bulwahn@38169
|
164 |
if expect1 = expect2 then expect1 else No_Expectation
|
bulwahn@38169
|
165 |
|
haftmann@28315
|
166 |
(* quickcheck configuration -- default parameters, test generators *)
|
wenzelm@43487
|
167 |
val tester = Attrib.setup_config_string @{binding quickcheck_tester} (K "")
|
wenzelm@43487
|
168 |
val size = Attrib.setup_config_int @{binding quickcheck_size} (K 10)
|
wenzelm@43487
|
169 |
val iterations = Attrib.setup_config_int @{binding quickcheck_iterations} (K 100)
|
wenzelm@43487
|
170 |
val no_assms = Attrib.setup_config_bool @{binding quickcheck_no_assms} (K false)
|
wenzelm@43487
|
171 |
val report = Attrib.setup_config_bool @{binding quickcheck_report} (K true)
|
wenzelm@43487
|
172 |
val timing = Attrib.setup_config_bool @{binding quickcheck_timing} (K false)
|
wenzelm@43487
|
173 |
val quiet = Attrib.setup_config_bool @{binding quickcheck_quiet} (K false)
|
wenzelm@43487
|
174 |
val timeout = Attrib.setup_config_real @{binding quickcheck_timeout} (K 30.0)
|
wenzelm@43487
|
175 |
val finite_types = Attrib.setup_config_bool @{binding quickcheck_finite_types} (K true)
|
wenzelm@43487
|
176 |
val finite_type_size = Attrib.setup_config_int @{binding quickcheck_finite_type_size} (K 3)
|
bulwahn@40894
|
177 |
|
haftmann@28309
|
178 |
datatype test_params = Test_Params of
|
bulwahn@40892
|
179 |
{default_type: typ list, expect : expectation};
|
haftmann@28309
|
180 |
|
bulwahn@40892
|
181 |
fun dest_test_params (Test_Params {default_type, expect}) = (default_type, expect);
|
wenzelm@39034
|
182 |
|
wenzelm@41765
|
183 |
fun make_test_params (default_type, expect) =
|
wenzelm@41765
|
184 |
Test_Params {default_type = default_type, expect = expect};
|
wenzelm@39034
|
185 |
|
wenzelm@41765
|
186 |
fun map_test_params' f (Test_Params {default_type, expect}) =
|
wenzelm@41765
|
187 |
make_test_params (f (default_type, expect));
|
wenzelm@39034
|
188 |
|
wenzelm@39034
|
189 |
fun merge_test_params
|
wenzelm@41720
|
190 |
(Test_Params {default_type = default_type1, expect = expect1},
|
wenzelm@41720
|
191 |
Test_Params {default_type = default_type2, expect = expect2}) =
|
wenzelm@41720
|
192 |
make_test_params
|
wenzelm@41720
|
193 |
(merge (op =) (default_type1, default_type2), merge_expectation (expect1, expect2));
|
haftmann@28309
|
194 |
|
bulwahn@39479
|
195 |
structure Data = Generic_Data
|
wenzelm@33522
|
196 |
(
|
wenzelm@39034
|
197 |
type T =
|
bulwahn@43953
|
198 |
(((string * (Proof.context -> (term * term list) list -> int list -> term list option * report option)) list
|
bulwahn@43953
|
199 |
* (string * (Proof.context -> bool * bool -> (string * typ) list -> (term * term list) list -> result list)) list)
|
bulwahn@43065
|
200 |
* ((string * (Proof.context -> term list -> (int -> term list option) list)) list
|
bulwahn@43065
|
201 |
* ((string * (Proof.context -> term list -> (int -> bool) list)) list)))
|
wenzelm@39034
|
202 |
* test_params;
|
bulwahn@43953
|
203 |
val empty = ((([], []), ([], [])), Test_Params {default_type = [], expect = No_Expectation});
|
haftmann@28256
|
204 |
val extend = I;
|
bulwahn@43953
|
205 |
fun merge ((((generators1, testers1), (batch_generators1, batch_validators1)), params1),
|
bulwahn@43953
|
206 |
(((generators2, testers2), (batch_generators2, batch_validators2)), params2)) : T =
|
bulwahn@43953
|
207 |
(((AList.merge (op =) (K true) (generators1, generators2),
|
bulwahn@43953
|
208 |
AList.merge (op =) (K true) (testers1, testers2)),
|
bulwahn@43953
|
209 |
(AList.merge (op =) (K true) (batch_generators1, batch_generators2),
|
bulwahn@43953
|
210 |
AList.merge (op =) (K true) (batch_validators1, batch_validators2))),
|
haftmann@28309
|
211 |
merge_test_params (params1, params2));
|
wenzelm@33522
|
212 |
);
|
haftmann@28256
|
213 |
|
bulwahn@39479
|
214 |
val test_params_of = snd o Data.get o Context.Proof;
|
bulwahn@38150
|
215 |
|
bulwahn@40892
|
216 |
val default_type = fst o dest_test_params o test_params_of
|
bulwahn@40481
|
217 |
|
bulwahn@40892
|
218 |
val expect = snd o dest_test_params o test_params_of
|
bulwahn@40481
|
219 |
|
bulwahn@40481
|
220 |
val map_test_params = Data.map o apsnd o map_test_params'
|
bulwahn@39480
|
221 |
|
bulwahn@43953
|
222 |
val add_generator = Data.map o apfst o apfst o apfst o AList.update (op =);
|
bulwahn@43953
|
223 |
|
bulwahn@43953
|
224 |
val add_tester = Data.map o apfst o apfst o apsnd o AList.update (op =);
|
bulwahn@42733
|
225 |
|
bulwahn@43065
|
226 |
val add_batch_generator = Data.map o apfst o apsnd o apfst o AList.update (op =);
|
bulwahn@43065
|
227 |
|
bulwahn@43065
|
228 |
val add_batch_validator = Data.map o apfst o apsnd o apsnd o AList.update (op =);
|
haftmann@28256
|
229 |
|
haftmann@28315
|
230 |
(* generating tests *)
|
haftmann@28315
|
231 |
|
bulwahn@42733
|
232 |
fun gen_mk_tester lookup ctxt v =
|
haftmann@28309
|
233 |
let
|
bulwahn@41153
|
234 |
val name = Config.get ctxt tester
|
bulwahn@42733
|
235 |
val tester = case lookup ctxt name
|
bulwahn@41153
|
236 |
of NONE => error ("No such quickcheck tester: " ^ name)
|
bulwahn@41153
|
237 |
| SOME tester => tester ctxt;
|
wenzelm@40491
|
238 |
in
|
bulwahn@41153
|
239 |
if Config.get ctxt quiet then
|
bulwahn@42733
|
240 |
try tester v
|
bulwahn@41153
|
241 |
else
|
wenzelm@44633
|
242 |
let (* FIXME !?!? *)
|
bulwahn@42733
|
243 |
val tester = Exn.interruptible_capture tester v
|
wenzelm@44633
|
244 |
in case Exn.get_res tester of
|
bulwahn@41153
|
245 |
NONE => SOME (Exn.release tester)
|
bulwahn@41153
|
246 |
| SOME tester => SOME tester
|
bulwahn@41153
|
247 |
end
|
bulwahn@41153
|
248 |
end
|
haftmann@28315
|
249 |
|
bulwahn@43065
|
250 |
val mk_tester =
|
bulwahn@43953
|
251 |
gen_mk_tester (fn ctxt => AList.lookup (op =) ((fst o fst o fst o Data.get o Context.Proof) ctxt))
|
bulwahn@43065
|
252 |
val mk_batch_tester =
|
bulwahn@43065
|
253 |
gen_mk_tester (fn ctxt => AList.lookup (op =) ((fst o snd o fst o Data.get o Context.Proof) ctxt))
|
bulwahn@43065
|
254 |
val mk_batch_validator =
|
bulwahn@43065
|
255 |
gen_mk_tester (fn ctxt => AList.lookup (op =) ((snd o snd o fst o Data.get o Context.Proof) ctxt))
|
bulwahn@43065
|
256 |
|
bulwahn@43953
|
257 |
|
bulwahn@43953
|
258 |
fun lookup_tester ctxt = AList.lookup (op =) ((snd o fst o fst o Data.get o Context.Proof) ctxt)
|
bulwahn@43953
|
259 |
|
haftmann@28315
|
260 |
(* testing propositions *)
|
haftmann@28315
|
261 |
|
bulwahn@42899
|
262 |
fun check_test_term t =
|
haftmann@28309
|
263 |
let
|
wenzelm@29266
|
264 |
val _ = (null (Term.add_tvars t []) andalso null (Term.add_tfrees t [])) orelse
|
haftmann@28309
|
265 |
error "Term to be tested contains type variables";
|
wenzelm@29266
|
266 |
val _ = null (Term.add_vars t []) orelse
|
haftmann@28309
|
267 |
error "Term to be tested contains schematic variables";
|
bulwahn@42899
|
268 |
in () end
|
haftmann@28309
|
269 |
|
wenzelm@42885
|
270 |
fun cpu_time description e =
|
wenzelm@42885
|
271 |
let val ({cpu, ...}, result) = Timing.timing e ()
|
wenzelm@42885
|
272 |
in (result, (description, Time.toMilliseconds cpu)) end
|
wenzelm@41765
|
273 |
|
bulwahn@44456
|
274 |
fun limit timeout (limit_time, is_interactive) f exc () =
|
bulwahn@42625
|
275 |
if limit_time then
|
bulwahn@44456
|
276 |
TimeLimit.timeLimit timeout f ()
|
bulwahn@42625
|
277 |
handle TimeLimit.TimeOut =>
|
bulwahn@42625
|
278 |
if is_interactive then exc () else raise TimeLimit.TimeOut
|
bulwahn@42625
|
279 |
else
|
bulwahn@42625
|
280 |
f ()
|
bulwahn@42624
|
281 |
|
bulwahn@42897
|
282 |
fun test_term ctxt (limit_time, is_interactive) (t, eval_terms) =
|
bulwahn@42733
|
283 |
let
|
bulwahn@42952
|
284 |
fun message s = if Config.get ctxt quiet then () else Output.urgent_message s
|
bulwahn@42899
|
285 |
val _ = check_test_term t
|
bulwahn@42899
|
286 |
val names = Term.add_free_names t []
|
bulwahn@40612
|
287 |
val current_size = Unsynchronized.ref 0
|
bulwahn@42952
|
288 |
val current_result = Unsynchronized.ref empty_result
|
bulwahn@42951
|
289 |
fun excipit () =
|
bulwahn@42951
|
290 |
"Quickcheck ran out of time while testing at size " ^ string_of_int (!current_size)
|
bulwahn@42952
|
291 |
fun with_size test_fun k =
|
bulwahn@41155
|
292 |
if k > Config.get ctxt size then
|
bulwahn@42952
|
293 |
NONE
|
bulwahn@41155
|
294 |
else
|
bulwahn@35378
|
295 |
let
|
bulwahn@42952
|
296 |
val _ = message ("Test data size: " ^ string_of_int k)
|
bulwahn@40896
|
297 |
val _ = current_size := k
|
bulwahn@42952
|
298 |
val ((result, report), timing) =
|
bulwahn@43030
|
299 |
cpu_time ("size " ^ string_of_int k) (fn () => test_fun [1, k - 1])
|
bulwahn@42952
|
300 |
val _ = add_timing timing current_result
|
bulwahn@42952
|
301 |
val _ = add_report k report current_result
|
wenzelm@41765
|
302 |
in
|
bulwahn@42952
|
303 |
case result of NONE => with_size test_fun (k + 1) | SOME q => SOME q
|
wenzelm@41765
|
304 |
end;
|
bulwahn@34935
|
305 |
in
|
bulwahn@44456
|
306 |
limit (seconds (Config.get ctxt timeout)) (limit_time, is_interactive) (fn () =>
|
bulwahn@43033
|
307 |
let
|
bulwahn@43033
|
308 |
val (test_fun, comp_time) = cpu_time "quickcheck compilation"
|
bulwahn@43033
|
309 |
(fn () => mk_tester ctxt [(t, eval_terms)]);
|
bulwahn@43033
|
310 |
val _ = add_timing comp_time current_result
|
bulwahn@43033
|
311 |
in
|
bulwahn@43033
|
312 |
case test_fun of NONE => !current_result
|
bulwahn@43033
|
313 |
| SOME test_fun =>
|
bulwahn@43033
|
314 |
let
|
bulwahn@43033
|
315 |
val (response, exec_time) =
|
bulwahn@43033
|
316 |
cpu_time "quickcheck execution" (fn () => with_size test_fun 1)
|
bulwahn@43033
|
317 |
val _ = add_response names eval_terms response current_result
|
bulwahn@43033
|
318 |
val _ = add_timing exec_time current_result
|
bulwahn@43033
|
319 |
in
|
bulwahn@43033
|
320 |
!current_result
|
bulwahn@43033
|
321 |
end
|
bulwahn@43033
|
322 |
end)
|
bulwahn@43033
|
323 |
(fn () => (message (excipit ()); !current_result)) ()
|
haftmann@28309
|
324 |
end;
|
haftmann@28309
|
325 |
|
bulwahn@43065
|
326 |
fun validate_terms ctxt ts =
|
bulwahn@43065
|
327 |
let
|
bulwahn@43065
|
328 |
val _ = map check_test_term ts
|
bulwahn@43065
|
329 |
val size = Config.get ctxt size
|
bulwahn@43065
|
330 |
val (test_funs, comp_time) = cpu_time "quickcheck batch compilation"
|
bulwahn@43065
|
331 |
(fn () => mk_batch_validator ctxt ts)
|
bulwahn@43065
|
332 |
fun with_size tester k =
|
bulwahn@43065
|
333 |
if k > size then true
|
bulwahn@43065
|
334 |
else if tester k then with_size tester (k + 1) else false
|
bulwahn@43065
|
335 |
val (results, exec_time) = cpu_time "quickcheck batch execution" (fn () =>
|
bulwahn@43065
|
336 |
Option.map (map (fn test_fun => TimeLimit.timeLimit (seconds (Config.get ctxt timeout))
|
bulwahn@43065
|
337 |
(fn () => with_size test_fun 1) ()
|
bulwahn@43065
|
338 |
handle TimeLimit.TimeOut => true)) test_funs)
|
bulwahn@43065
|
339 |
in
|
bulwahn@43065
|
340 |
(results, [comp_time, exec_time])
|
bulwahn@43065
|
341 |
end
|
bulwahn@43065
|
342 |
|
bulwahn@42733
|
343 |
fun test_terms ctxt ts =
|
bulwahn@42733
|
344 |
let
|
bulwahn@42899
|
345 |
val _ = map check_test_term ts
|
bulwahn@43065
|
346 |
val size = Config.get ctxt size
|
bulwahn@42899
|
347 |
val namess = map (fn t => Term.add_free_names t []) ts
|
bulwahn@43059
|
348 |
val (test_funs, comp_time) = cpu_time "quickcheck batch compilation" (fn () => mk_batch_tester ctxt ts)
|
bulwahn@42733
|
349 |
fun with_size tester k =
|
bulwahn@43065
|
350 |
if k > size then NONE
|
bulwahn@42733
|
351 |
else case tester k of SOME ts => SOME ts | NONE => with_size tester (k + 1)
|
bulwahn@43059
|
352 |
val (results, exec_time) = cpu_time "quickcheck batch execution" (fn () =>
|
bulwahn@43059
|
353 |
Option.map (map (fn test_fun => TimeLimit.timeLimit (seconds (Config.get ctxt timeout))
|
bulwahn@43065
|
354 |
(fn () => with_size test_fun 1) ()
|
bulwahn@43059
|
355 |
handle TimeLimit.TimeOut => NONE)) test_funs)
|
bulwahn@42733
|
356 |
in
|
bulwahn@43059
|
357 |
(Option.map (map2 (fn names => Option.map (fn ts => names ~~ ts)) namess) results,
|
bulwahn@43059
|
358 |
[comp_time, exec_time])
|
bulwahn@42733
|
359 |
end
|
bulwahn@42733
|
360 |
|
bulwahn@41334
|
361 |
(* FIXME: this function shows that many assumptions are made upon the generation *)
|
bulwahn@41334
|
362 |
(* In the end there is probably no generic quickcheck interface left... *)
|
bulwahn@41334
|
363 |
|
bulwahn@42625
|
364 |
fun test_term_with_increasing_cardinality ctxt (limit_time, is_interactive) ts =
|
bulwahn@41291
|
365 |
let
|
wenzelm@43232
|
366 |
val thy = Proof_Context.theory_of ctxt
|
bulwahn@42952
|
367 |
fun message s = if Config.get ctxt quiet then () else Output.urgent_message s
|
bulwahn@43030
|
368 |
val (ts', eval_terms) = split_list ts
|
bulwahn@43030
|
369 |
val _ = map check_test_term ts'
|
bulwahn@43030
|
370 |
val names = Term.add_free_names (hd ts') []
|
bulwahn@43030
|
371 |
val Ts = map snd (Term.add_frees (hd ts') [])
|
bulwahn@43033
|
372 |
val current_result = Unsynchronized.ref empty_result
|
bulwahn@43033
|
373 |
fun test_card_size test_fun (card, size) =
|
bulwahn@41291
|
374 |
(* FIXME: why decrement size by one? *)
|
bulwahn@42952
|
375 |
let
|
bulwahn@42952
|
376 |
val (ts, timing) = cpu_time ("size " ^ string_of_int size ^ " and card " ^ string_of_int card)
|
bulwahn@43139
|
377 |
(fn () => fst (test_fun [card, size - 1]))
|
bulwahn@42952
|
378 |
val _ = add_timing timing current_result
|
bulwahn@42952
|
379 |
in
|
bulwahn@42952
|
380 |
Option.map (pair card) ts
|
bulwahn@42952
|
381 |
end
|
bulwahn@41291
|
382 |
val enumeration_card_size =
|
bulwahn@41334
|
383 |
if forall (fn T => Sign.of_sort thy (T, ["Enum.enum"])) Ts then
|
bulwahn@41334
|
384 |
(* size does not matter *)
|
bulwahn@41334
|
385 |
map (rpair 0) (1 upto (length ts))
|
bulwahn@41334
|
386 |
else
|
bulwahn@41334
|
387 |
(* size does matter *)
|
bulwahn@41334
|
388 |
map_product pair (1 upto (length ts)) (1 upto (Config.get ctxt size))
|
bulwahn@41334
|
389 |
|> sort (fn ((c1, s1), (c2, s2)) => int_ord ((c1 + s1), (c2 + s2)))
|
bulwahn@43033
|
390 |
in
|
bulwahn@44456
|
391 |
limit (seconds (Config.get ctxt timeout)) (limit_time, is_interactive) (fn () =>
|
bulwahn@43033
|
392 |
let
|
bulwahn@43033
|
393 |
val (test_fun, comp_time) = cpu_time "quickcheck compilation" (fn () => mk_tester ctxt ts)
|
bulwahn@43033
|
394 |
val _ = add_timing comp_time current_result
|
bulwahn@43033
|
395 |
in
|
bulwahn@43033
|
396 |
case test_fun of
|
bulwahn@43030
|
397 |
NONE => !current_result
|
bulwahn@43030
|
398 |
| SOME test_fun =>
|
bulwahn@42952
|
399 |
let
|
bulwahn@43033
|
400 |
val _ = case get_first (test_card_size test_fun) enumeration_card_size of
|
bulwahn@42952
|
401 |
SOME (card, ts) => add_response names (nth eval_terms (card - 1)) (SOME ts) current_result
|
bulwahn@42952
|
402 |
| NONE => ()
|
bulwahn@43033
|
403 |
in !current_result end
|
bulwahn@43033
|
404 |
end)
|
bulwahn@43033
|
405 |
(fn () => (message "Quickcheck ran out of time"; !current_result)) ()
|
bulwahn@43033
|
406 |
end
|
bulwahn@41291
|
407 |
|
bulwahn@40895
|
408 |
fun get_finite_types ctxt =
|
bulwahn@40895
|
409 |
fst (chop (Config.get ctxt finite_type_size)
|
bulwahn@40895
|
410 |
(map (Type o rpair []) ["Enum.finite_1", "Enum.finite_2", "Enum.finite_3",
|
wenzelm@41765
|
411 |
"Enum.finite_4", "Enum.finite_5"]))
|
bulwahn@40895
|
412 |
|
bulwahn@38153
|
413 |
exception WELLSORTED of string
|
bulwahn@38153
|
414 |
|
wenzelm@41765
|
415 |
fun monomorphic_term thy insts default_T =
|
haftmann@28309
|
416 |
let
|
haftmann@28309
|
417 |
fun subst (T as TFree (v, S)) =
|
bulwahn@41147
|
418 |
let
|
bulwahn@41147
|
419 |
val T' = AList.lookup (op =) insts v
|
bulwahn@41147
|
420 |
|> the_default default_T
|
bulwahn@41147
|
421 |
in if Sign.of_sort thy (T', S) then T'
|
wenzelm@41765
|
422 |
else raise (WELLSORTED ("For instantiation with default_type " ^
|
wenzelm@41765
|
423 |
Syntax.string_of_typ_global thy default_T ^
|
bulwahn@41147
|
424 |
":\n" ^ Syntax.string_of_typ_global thy T' ^
|
bulwahn@41147
|
425 |
" to be substituted for variable " ^
|
bulwahn@41147
|
426 |
Syntax.string_of_typ_global thy T ^ " does not have sort " ^
|
bulwahn@41147
|
427 |
Syntax.string_of_sort_global thy S))
|
bulwahn@41147
|
428 |
end
|
haftmann@28309
|
429 |
| subst T = T;
|
haftmann@28309
|
430 |
in (map_types o map_atyps) subst end;
|
haftmann@28309
|
431 |
|
bulwahn@42897
|
432 |
datatype wellsorted_error = Wellsorted_Error of string | Term of term * term list
|
bulwahn@38153
|
433 |
|
bulwahn@43954
|
434 |
fun instantiate_goals lthy insts goals =
|
haftmann@28309
|
435 |
let
|
bulwahn@42897
|
436 |
fun map_goal_and_eval_terms f (check_goal, eval_terms) = (f check_goal, map f eval_terms)
|
wenzelm@43232
|
437 |
val thy = Proof_Context.theory_of lthy
|
bulwahn@41170
|
438 |
val default_insts =
|
bulwahn@41170
|
439 |
if Config.get lthy finite_types then (get_finite_types lthy) else (default_type lthy)
|
bulwahn@40895
|
440 |
val inst_goals =
|
bulwahn@42897
|
441 |
map (fn (check_goal, eval_terms) =>
|
bulwahn@41170
|
442 |
if not (null (Term.add_tfree_names check_goal [])) then
|
bulwahn@41170
|
443 |
map (fn T =>
|
bulwahn@42897
|
444 |
(pair (SOME T) o Term o apfst (Object_Logic.atomize_term thy))
|
bulwahn@42897
|
445 |
(map_goal_and_eval_terms (monomorphic_term thy insts T) (check_goal, eval_terms))
|
bulwahn@41291
|
446 |
handle WELLSORTED s => (SOME T, Wellsorted_Error s)) default_insts
|
bulwahn@41170
|
447 |
else
|
bulwahn@43954
|
448 |
[(NONE, Term (Object_Logic.atomize_term thy check_goal, eval_terms))]) goals
|
wenzelm@41765
|
449 |
val error_msg =
|
wenzelm@41765
|
450 |
cat_lines
|
wenzelm@41765
|
451 |
(maps (map_filter (fn (_, Term t) => NONE | (_, Wellsorted_Error s) => SOME s)) inst_goals)
|
bulwahn@41291
|
452 |
fun is_wellsorted_term (T, Term t) = SOME (T, t)
|
bulwahn@41291
|
453 |
| is_wellsorted_term (_, Wellsorted_Error s) = NONE
|
bulwahn@38153
|
454 |
val correct_inst_goals =
|
bulwahn@41291
|
455 |
case map (map_filter is_wellsorted_term) inst_goals of
|
bulwahn@41291
|
456 |
[[]] => error error_msg
|
bulwahn@38153
|
457 |
| xs => xs
|
bulwahn@40892
|
458 |
val _ = if Config.get lthy quiet then () else warning error_msg
|
bulwahn@43954
|
459 |
in
|
bulwahn@43954
|
460 |
correct_inst_goals
|
bulwahn@43954
|
461 |
end
|
bulwahn@43954
|
462 |
|
bulwahn@43954
|
463 |
fun collect_results f [] results = results
|
bulwahn@43954
|
464 |
| collect_results f (t :: ts) results =
|
bulwahn@43954
|
465 |
let
|
bulwahn@43954
|
466 |
val result = f t
|
bulwahn@43954
|
467 |
in
|
bulwahn@43954
|
468 |
if found_counterexample result then
|
bulwahn@43954
|
469 |
(result :: results)
|
bulwahn@43954
|
470 |
else
|
bulwahn@43954
|
471 |
collect_results f ts (result :: results)
|
bulwahn@43954
|
472 |
end
|
bulwahn@43954
|
473 |
|
bulwahn@44746
|
474 |
fun generator_test_goal_terms ctxt (limit_time, is_interactive) insts goals =
|
bulwahn@43954
|
475 |
let
|
bulwahn@41291
|
476 |
fun test_term' goal =
|
bulwahn@41291
|
477 |
case goal of
|
bulwahn@43988
|
478 |
[(NONE, t)] => test_term ctxt (limit_time, is_interactive) t
|
bulwahn@43988
|
479 |
| ts => test_term_with_increasing_cardinality ctxt (limit_time, is_interactive) (map snd ts)
|
bulwahn@43988
|
480 |
val correct_inst_goals = instantiate_goals ctxt insts goals
|
bulwahn@41291
|
481 |
in
|
bulwahn@44746
|
482 |
if Config.get ctxt finite_types then
|
bulwahn@44746
|
483 |
collect_results test_term' correct_inst_goals []
|
bulwahn@44746
|
484 |
else
|
bulwahn@44746
|
485 |
collect_results (test_term ctxt (limit_time, is_interactive))
|
bulwahn@44746
|
486 |
(maps (map snd) correct_inst_goals) []
|
bulwahn@41291
|
487 |
end;
|
bulwahn@38152
|
488 |
|
bulwahn@44746
|
489 |
fun test_goal_terms ctxt (limit_time, is_interactive) insts goals =
|
bulwahn@44746
|
490 |
case lookup_tester ctxt (Config.get ctxt tester) of
|
bulwahn@44746
|
491 |
SOME test_goal_terms => test_goal_terms ctxt (limit_time, is_interactive) insts goals
|
bulwahn@44746
|
492 |
| NONE => error ("Unknown tester: " ^ Config.get ctxt tester)
|
bulwahn@44746
|
493 |
|
bulwahn@42897
|
494 |
fun test_goal (time_limit, is_interactive) (insts, eval_terms) i state =
|
bulwahn@40896
|
495 |
let
|
bulwahn@40896
|
496 |
val lthy = Proof.context_of state;
|
bulwahn@40896
|
497 |
val thy = Proof.theory_of state;
|
bulwahn@40896
|
498 |
fun strip (Const ("all", _) $ Abs (_, _, t)) = strip t
|
bulwahn@40896
|
499 |
| strip t = t;
|
bulwahn@40896
|
500 |
val {goal = st, ...} = Proof.raw_goal state;
|
bulwahn@40896
|
501 |
val (gi, frees) = Logic.goal_params (prop_of st) i;
|
bulwahn@40896
|
502 |
val some_locale = case (Option.map #target o Named_Target.peek) lthy
|
bulwahn@40896
|
503 |
of NONE => NONE
|
bulwahn@40896
|
504 |
| SOME "" => NONE
|
bulwahn@40896
|
505 |
| SOME locale => SOME locale;
|
bulwahn@40896
|
506 |
val assms = if Config.get lthy no_assms then [] else case some_locale
|
bulwahn@40896
|
507 |
of NONE => Assumption.all_assms_of lthy
|
bulwahn@40896
|
508 |
| SOME locale => Assumption.local_assms_of lthy (Locale.init locale thy);
|
bulwahn@40896
|
509 |
val proto_goal = Logic.list_implies (map Thm.term_of assms, subst_bounds (frees, strip gi));
|
bulwahn@43954
|
510 |
val goals = case some_locale
|
bulwahn@42897
|
511 |
of NONE => [(proto_goal, eval_terms)]
|
bulwahn@42897
|
512 |
| SOME locale =>
|
bulwahn@42897
|
513 |
map (fn (_, phi) => (Morphism.term phi proto_goal, map (Morphism.term phi) eval_terms))
|
bulwahn@42897
|
514 |
(Locale.registrations_of (Context.Theory thy) (*FIXME*) locale);
|
bulwahn@40896
|
515 |
in
|
bulwahn@43988
|
516 |
test_goal_terms lthy (time_limit, is_interactive) insts goals
|
bulwahn@40896
|
517 |
end
|
bulwahn@40896
|
518 |
|
bulwahn@38152
|
519 |
(* pretty printing *)
|
haftmann@28315
|
520 |
|
blanchet@40466
|
521 |
fun tool_name auto = (if auto then "Auto " else "") ^ "Quickcheck"
|
blanchet@40466
|
522 |
|
blanchet@40466
|
523 |
fun pretty_counterex ctxt auto NONE = Pretty.str (tool_name auto ^ " found no counterexample.")
|
bulwahn@42899
|
524 |
| pretty_counterex ctxt auto (SOME (cex, eval_terms)) =
|
bulwahn@42899
|
525 |
Pretty.chunks ((Pretty.str (tool_name auto ^ " found a counterexample:\n") ::
|
haftmann@28315
|
526 |
map (fn (s, t) =>
|
bulwahn@42899
|
527 |
Pretty.block [Pretty.str (s ^ " ="), Pretty.brk 1, Syntax.pretty_term ctxt t]) (rev cex))
|
blanchet@43861
|
528 |
@ (if null eval_terms then []
|
blanchet@43861
|
529 |
else (Pretty.str ("Evaluated terms:\n") ::
|
blanchet@43861
|
530 |
map (fn (t, u) =>
|
blanchet@43861
|
531 |
Pretty.block [Syntax.pretty_term ctxt t, Pretty.str " =", Pretty.brk 1, Syntax.pretty_term ctxt u])
|
blanchet@43861
|
532 |
(rev eval_terms))));
|
haftmann@28315
|
533 |
|
bulwahn@35378
|
534 |
fun pretty_report (Report {iterations = iterations, raised_match_errors = raised_match_errors,
|
bulwahn@35378
|
535 |
satisfied_assms = satisfied_assms, positive_concl_tests = positive_concl_tests}) =
|
bulwahn@35378
|
536 |
let
|
bulwahn@35378
|
537 |
fun pretty_stat s i = Pretty.block ([Pretty.str (s ^ ": " ^ string_of_int i)])
|
bulwahn@35378
|
538 |
in
|
bulwahn@35378
|
539 |
([pretty_stat "iterations" iterations,
|
bulwahn@35378
|
540 |
pretty_stat "match exceptions" raised_match_errors]
|
wenzelm@41765
|
541 |
@ map_index
|
wenzelm@41765
|
542 |
(fn (i, n) => pretty_stat ("satisfied " ^ string_of_int (i + 1) ^ ". assumption") n)
|
bulwahn@35378
|
543 |
satisfied_assms
|
bulwahn@35378
|
544 |
@ [pretty_stat "positive conclusion tests" positive_concl_tests])
|
bulwahn@35378
|
545 |
end
|
bulwahn@35378
|
546 |
|
bulwahn@35380
|
547 |
fun pretty_reports ctxt (SOME reports) =
|
bulwahn@41160
|
548 |
Pretty.chunks (Pretty.fbrk :: Pretty.str "Quickcheck report:" ::
|
bulwahn@41153
|
549 |
maps (fn (size, report) =>
|
bulwahn@41153
|
550 |
Pretty.str ("size " ^ string_of_int size ^ ":") :: pretty_report report @ [Pretty.brk 1])
|
bulwahn@35378
|
551 |
(rev reports))
|
bulwahn@35380
|
552 |
| pretty_reports ctxt NONE = Pretty.str ""
|
bulwahn@35378
|
553 |
|
bulwahn@42952
|
554 |
fun pretty_timings timings =
|
bulwahn@42952
|
555 |
Pretty.chunks (Pretty.fbrk :: Pretty.str "timings:" ::
|
bulwahn@42952
|
556 |
maps (fn (label, time) =>
|
bulwahn@42952
|
557 |
Pretty.str (label ^ ": " ^ string_of_int time ^ " ms") :: Pretty.brk 1 :: []) (rev timings))
|
bulwahn@42952
|
558 |
|
bulwahn@43299
|
559 |
fun pretty_counterex_and_reports ctxt auto [] =
|
bulwahn@43299
|
560 |
Pretty.chunks [Pretty.strs (tool_name auto ::
|
bulwahn@43299
|
561 |
space_explode " " "is used in a locale where no interpretation is provided."),
|
bulwahn@43299
|
562 |
Pretty.strs (space_explode " " "Please provide an executable interpretation for the locale.")]
|
bulwahn@43299
|
563 |
| pretty_counterex_and_reports ctxt auto (result :: _) =
|
bulwahn@43299
|
564 |
Pretty.chunks (pretty_counterex ctxt auto (response_of result) ::
|
bulwahn@43299
|
565 |
(* map (pretty_reports ctxt) (reports_of result) :: *)
|
bulwahn@43299
|
566 |
(if Config.get ctxt timing then cons (pretty_timings (timings_of result)) else I) [])
|
haftmann@28315
|
567 |
|
wenzelm@30981
|
568 |
(* Isar commands *)
|
haftmann@28315
|
569 |
|
haftmann@28336
|
570 |
fun read_nat s = case (Library.read_int o Symbol.explode) s
|
haftmann@28336
|
571 |
of (k, []) => if k >= 0 then k
|
haftmann@28336
|
572 |
else error ("Not a natural number: " ^ s)
|
haftmann@28336
|
573 |
| (_, _ :: _) => error ("Not a natural number: " ^ s);
|
bulwahn@38149
|
574 |
|
blanchet@34125
|
575 |
fun read_bool "false" = false
|
blanchet@34125
|
576 |
| read_bool "true" = true
|
blanchet@34125
|
577 |
| read_bool s = error ("Not a Boolean value: " ^ s)
|
haftmann@28315
|
578 |
|
bulwahn@40612
|
579 |
fun read_real s =
|
bulwahn@40612
|
580 |
case (Real.fromString s) of
|
bulwahn@40612
|
581 |
SOME s => s
|
bulwahn@40612
|
582 |
| NONE => error ("Not a real number: " ^ s)
|
bulwahn@40612
|
583 |
|
bulwahn@38169
|
584 |
fun read_expectation "no_expectation" = No_Expectation
|
wenzelm@41765
|
585 |
| read_expectation "no_counterexample" = No_Counterexample
|
bulwahn@38169
|
586 |
| read_expectation "counterexample" = Counterexample
|
wenzelm@41765
|
587 |
| read_expectation s = error ("Not an expectation value: " ^ s)
|
bulwahn@38169
|
588 |
|
bulwahn@43955
|
589 |
fun valid_tester_name genctxt name =
|
bulwahn@43955
|
590 |
AList.defined (op =) (fst (fst (fst (Data.get genctxt)))) name orelse
|
bulwahn@43955
|
591 |
AList.defined (op =) (snd (fst (fst (Data.get genctxt)))) name
|
bulwahn@41156
|
592 |
|
bulwahn@41156
|
593 |
fun parse_tester name genctxt =
|
bulwahn@41156
|
594 |
if valid_tester_name genctxt name then
|
bulwahn@41156
|
595 |
Config.put_generic tester name genctxt
|
bulwahn@41156
|
596 |
else
|
bulwahn@41156
|
597 |
error ("Unknown tester: " ^ name)
|
bulwahn@41156
|
598 |
|
bulwahn@41156
|
599 |
fun parse_test_param ("tester", [arg]) = parse_tester arg
|
bulwahn@41153
|
600 |
| parse_test_param ("size", [arg]) = Config.put_generic size (read_nat arg)
|
bulwahn@40892
|
601 |
| parse_test_param ("iterations", [arg]) = Config.put_generic iterations (read_nat arg)
|
bulwahn@40892
|
602 |
| parse_test_param ("default_type", arg) = (fn gen_ctxt =>
|
wenzelm@41765
|
603 |
map_test_params
|
wenzelm@43232
|
604 |
((apfst o K) (map (Proof_Context.read_typ (Context.proof_of gen_ctxt)) arg)) gen_ctxt)
|
bulwahn@40892
|
605 |
| parse_test_param ("no_assms", [arg]) = Config.put_generic no_assms (read_bool arg)
|
bulwahn@40892
|
606 |
| parse_test_param ("expect", [arg]) = map_test_params ((apsnd o K) (read_expectation arg))
|
bulwahn@40892
|
607 |
| parse_test_param ("report", [arg]) = Config.put_generic report (read_bool arg)
|
bulwahn@40892
|
608 |
| parse_test_param ("quiet", [arg]) = Config.put_generic quiet (read_bool arg)
|
bulwahn@40892
|
609 |
| parse_test_param ("timeout", [arg]) = Config.put_generic timeout (read_real arg)
|
bulwahn@40894
|
610 |
| parse_test_param ("finite_types", [arg]) = Config.put_generic finite_types (read_bool arg)
|
wenzelm@41765
|
611 |
| parse_test_param ("finite_type_size", [arg]) =
|
wenzelm@41765
|
612 |
Config.put_generic finite_type_size (read_nat arg)
|
bulwahn@41156
|
613 |
| parse_test_param (name, _) = fn genctxt =>
|
bulwahn@41156
|
614 |
if valid_tester_name genctxt name then
|
bulwahn@41156
|
615 |
Config.put_generic tester name genctxt
|
bulwahn@41156
|
616 |
else error ("Unknown tester or test parameter: " ^ name);
|
haftmann@28315
|
617 |
|
bulwahn@42896
|
618 |
fun parse_test_param_inst (name, arg) ((insts, eval_terms), ctxt) =
|
wenzelm@43232
|
619 |
case try (Proof_Context.read_typ ctxt) name
|
bulwahn@42896
|
620 |
of SOME (TFree (v, _)) =>
|
wenzelm@43232
|
621 |
((AList.update (op =) (v, Proof_Context.read_typ ctxt (the_single arg)) insts, eval_terms), ctxt)
|
bulwahn@42896
|
622 |
| NONE => (case name of
|
bulwahn@42896
|
623 |
"eval" => ((insts, eval_terms @ map (Syntax.read_term ctxt) arg), ctxt)
|
bulwahn@42896
|
624 |
| _ => ((insts, eval_terms), Context.proof_map (parse_test_param (name, arg)) ctxt));
|
haftmann@28315
|
625 |
|
bulwahn@40892
|
626 |
fun quickcheck_params_cmd args = Context.theory_map (fold parse_test_param args);
|
wenzelm@41765
|
627 |
|
bulwahn@42952
|
628 |
fun check_expectation state results =
|
bulwahn@43299
|
629 |
(if exists found_counterexample results andalso expect (Proof.context_of state) = No_Counterexample
|
bulwahn@42952
|
630 |
then
|
bulwahn@42952
|
631 |
error ("quickcheck expected to find no counterexample but found one")
|
bulwahn@42952
|
632 |
else
|
bulwahn@43299
|
633 |
(if not (exists found_counterexample results) andalso expect (Proof.context_of state) = Counterexample
|
bulwahn@42952
|
634 |
then
|
bulwahn@42952
|
635 |
error ("quickcheck expected to find a counterexample but did not find one")
|
bulwahn@42952
|
636 |
else ()))
|
bulwahn@42952
|
637 |
|
bulwahn@35378
|
638 |
fun gen_quickcheck args i state =
|
bulwahn@40892
|
639 |
state
|
bulwahn@42896
|
640 |
|> Proof.map_context_result (fn ctxt => fold parse_test_param_inst args (([], []), ctxt))
|
bulwahn@42897
|
641 |
|> (fn ((insts, eval_terms), state') => test_goal (true, true) (insts, eval_terms) i state'
|
bulwahn@42952
|
642 |
|> tap (check_expectation state'))
|
boehmes@32295
|
643 |
|
bulwahn@42952
|
644 |
fun quickcheck args i state = counterexample_of (hd (gen_quickcheck args i state))
|
bulwahn@35378
|
645 |
|
boehmes@32295
|
646 |
fun quickcheck_cmd args i state =
|
bulwahn@35378
|
647 |
gen_quickcheck args i (Toplevel.proof_of state)
|
blanchet@43861
|
648 |
|> Output.urgent_message o Pretty.string_of
|
blanchet@43861
|
649 |
o pretty_counterex_and_reports (Toplevel.context_of state) false;
|
haftmann@28309
|
650 |
|
wenzelm@41765
|
651 |
val parse_arg =
|
wenzelm@41765
|
652 |
Parse.name --
|
wenzelm@41765
|
653 |
(Scan.optional (Parse.$$$ "=" |--
|
wenzelm@41765
|
654 |
(((Parse.name || Parse.float_number) >> single) ||
|
wenzelm@41765
|
655 |
(Parse.$$$ "[" |-- Parse.list1 Parse.name --| Parse.$$$ "]"))) ["true"]);
|
haftmann@28309
|
656 |
|
wenzelm@41765
|
657 |
val parse_args =
|
wenzelm@41765
|
658 |
Parse.$$$ "[" |-- Parse.list1 parse_arg --| Parse.$$$ "]" || Scan.succeed [];
|
haftmann@28336
|
659 |
|
wenzelm@36970
|
660 |
val _ =
|
blanchet@43861
|
661 |
Outer_Syntax.command quickcheck_paramsN "set parameters for random testing" Keyword.thy_decl
|
wenzelm@36970
|
662 |
(parse_args >> (fn args => Toplevel.theory (quickcheck_params_cmd args)));
|
haftmann@28309
|
663 |
|
wenzelm@36970
|
664 |
val _ =
|
blanchet@43861
|
665 |
Outer_Syntax.improper_command quickcheckN "try to find counterexample for subgoal" Keyword.diag
|
wenzelm@36970
|
666 |
(parse_args -- Scan.optional Parse.nat 1
|
wenzelm@36970
|
667 |
>> (fn (args, i) => Toplevel.no_timing o Toplevel.keep (quickcheck_cmd args i)));
|
haftmann@28309
|
668 |
|
blanchet@43861
|
669 |
(* automatic testing *)
|
blanchet@43861
|
670 |
|
blanchet@43861
|
671 |
fun try_quickcheck auto state =
|
blanchet@43861
|
672 |
let
|
blanchet@43861
|
673 |
val ctxt = Proof.context_of state;
|
blanchet@43861
|
674 |
val i = 1;
|
blanchet@43861
|
675 |
val res =
|
blanchet@43861
|
676 |
state
|
blanchet@43861
|
677 |
|> Proof.map_context (Config.put report false #> Config.put quiet true)
|
blanchet@43861
|
678 |
|> try (test_goal (false, false) ([], []) i);
|
blanchet@43861
|
679 |
in
|
blanchet@43861
|
680 |
case res of
|
blanchet@43861
|
681 |
NONE => (unknownN, state)
|
blanchet@43861
|
682 |
| SOME results =>
|
blanchet@43861
|
683 |
let
|
blanchet@43861
|
684 |
val msg = pretty_counterex_and_reports ctxt auto results
|
blanchet@43861
|
685 |
in
|
blanchet@43861
|
686 |
if exists found_counterexample results then
|
blanchet@43861
|
687 |
(genuineN,
|
blanchet@43870
|
688 |
state
|
blanchet@43870
|
689 |
|> (if auto then
|
blanchet@43870
|
690 |
Proof.goal_message (K (Pretty.chunks [Pretty.str "",
|
blanchet@43870
|
691 |
Pretty.mark Markup.hilite msg]))
|
blanchet@43870
|
692 |
else
|
blanchet@43870
|
693 |
tap (fn _ => Output.urgent_message (Pretty.string_of msg))))
|
blanchet@43861
|
694 |
else
|
blanchet@43861
|
695 |
(noneN, state)
|
blanchet@43861
|
696 |
end
|
blanchet@43861
|
697 |
end
|
blanchet@43861
|
698 |
|> `(fn (outcome_code, _) => outcome_code = genuineN)
|
blanchet@43861
|
699 |
|
blanchet@43870
|
700 |
val setup = Try.register_tool (quickcheckN, (20, auto, try_quickcheck))
|
blanchet@43861
|
701 |
|
haftmann@28309
|
702 |
end;
|
haftmann@28256
|
703 |
|
haftmann@28315
|
704 |
val auto_quickcheck = Quickcheck.auto;
|