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