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
|
bulwahn@44753
|
16 |
val batch_tester : string Config.T
|
bulwahn@40892
|
17 |
val size : int Config.T
|
bulwahn@40892
|
18 |
val iterations : int Config.T
|
bulwahn@46084
|
19 |
val depth : int Config.T
|
bulwahn@40892
|
20 |
val no_assms : bool Config.T
|
bulwahn@40892
|
21 |
val report : bool Config.T
|
bulwahn@47436
|
22 |
val timeout : real Config.T
|
bulwahn@42952
|
23 |
val timing : bool Config.T
|
bulwahn@46628
|
24 |
val genuine_only : bool Config.T
|
bulwahn@47329
|
25 |
val abort_potential : bool Config.T
|
bulwahn@40892
|
26 |
val quiet : bool Config.T
|
bulwahn@46635
|
27 |
val verbose : bool Config.T
|
bulwahn@47436
|
28 |
val use_subtype : bool Config.T
|
bulwahn@47436
|
29 |
val allow_function_inversion : bool Config.T
|
bulwahn@40896
|
30 |
val finite_types : bool Config.T
|
bulwahn@40896
|
31 |
val finite_type_size : int Config.T
|
bulwahn@47726
|
32 |
val tag : string Config.T
|
bulwahn@48206
|
33 |
val locale : string Config.T
|
bulwahn@44783
|
34 |
val set_active_testers: string list -> Context.generic -> Context.generic
|
wenzelm@41765
|
35 |
datatype expectation = No_Expectation | No_Counterexample | Counterexample;
|
bulwahn@40892
|
36 |
datatype test_params = Test_Params of {default_type: typ list, expect : expectation};
|
bulwahn@40481
|
37 |
val test_params_of : Proof.context -> test_params
|
bulwahn@40892
|
38 |
val map_test_params : (typ list * expectation -> typ list * expectation)
|
bulwahn@40481
|
39 |
-> Context.generic -> Context.generic
|
bulwahn@46029
|
40 |
val default_type : Proof.context -> typ list
|
bulwahn@42953
|
41 |
datatype report = Report of
|
bulwahn@42953
|
42 |
{ iterations : int, raised_match_errors : int,
|
bulwahn@42953
|
43 |
satisfied_assms : int list, positive_concl_tests : int }
|
bulwahn@42953
|
44 |
(* quickcheck's result *)
|
bulwahn@42952
|
45 |
datatype result =
|
bulwahn@42952
|
46 |
Result of
|
bulwahn@46598
|
47 |
{counterexample : (bool * (string * term) list) option,
|
bulwahn@42952
|
48 |
evaluation_terms : (term * term) list option,
|
bulwahn@42952
|
49 |
timings : (string * int) list,
|
bulwahn@42952
|
50 |
reports : (int * report) list}
|
bulwahn@44185
|
51 |
val empty_result : result
|
bulwahn@46029
|
52 |
val found_counterexample : result -> bool
|
bulwahn@44456
|
53 |
val add_timing : (string * int) -> result Unsynchronized.ref -> unit
|
bulwahn@46598
|
54 |
val add_response : string list -> term list -> (bool * term list) option -> result Unsynchronized.ref -> unit
|
bulwahn@46029
|
55 |
val add_report : int -> report option -> result Unsynchronized.ref -> unit
|
bulwahn@46598
|
56 |
val counterexample_of : result -> (bool * (string * term) list) option
|
bulwahn@42953
|
57 |
val timings_of : result -> (string * int) list
|
bulwahn@44749
|
58 |
(* registering testers & generators *)
|
bulwahn@44749
|
59 |
type tester =
|
bulwahn@46290
|
60 |
Proof.context -> bool -> (string * typ) list -> (term * term list) list -> result list
|
bulwahn@44749
|
61 |
val add_tester : string * (bool Config.T * tester) -> Context.generic -> Context.generic
|
bulwahn@44749
|
62 |
val add_batch_generator :
|
bulwahn@43953
|
63 |
string * (Proof.context -> term list -> (int -> term list option) list)
|
bulwahn@43953
|
64 |
-> Context.generic -> Context.generic
|
bulwahn@44749
|
65 |
val add_batch_validator :
|
bulwahn@43953
|
66 |
string * (Proof.context -> term list -> (int -> bool) list)
|
bulwahn@43953
|
67 |
-> Context.generic -> Context.generic
|
bulwahn@43954
|
68 |
(* basic operations *)
|
bulwahn@46029
|
69 |
val message : Proof.context -> string -> unit
|
bulwahn@46636
|
70 |
val verbose_message : Proof.context -> string -> unit
|
bulwahn@46029
|
71 |
val limit : Time.time -> (bool * bool) -> (unit -> 'a) -> (unit -> 'a) -> unit -> 'a
|
bulwahn@46626
|
72 |
val pretty_counterex : Proof.context -> bool ->
|
bulwahn@46626
|
73 |
((bool * (string * term) list) * (term * term) list) option -> Pretty.T
|
bulwahn@38150
|
74 |
(* testing terms and proof states *)
|
bulwahn@46029
|
75 |
val mk_batch_validator : Proof.context -> term list -> (int -> bool) list option
|
bulwahn@46029
|
76 |
val mk_batch_tester : Proof.context -> term list -> (int -> term list option) list option
|
bulwahn@46029
|
77 |
val active_testers : Proof.context -> tester list
|
bulwahn@46029
|
78 |
val test_terms :
|
bulwahn@44754
|
79 |
Proof.context -> bool * bool -> (string * typ) list -> (term * term list) list -> result list option
|
bulwahn@46598
|
80 |
val quickcheck: (string * string list) list -> int -> Proof.state -> (bool * (string * term) list) option
|
haftmann@28256
|
81 |
end;
|
haftmann@28256
|
82 |
|
haftmann@28256
|
83 |
structure Quickcheck : QUICKCHECK =
|
haftmann@28256
|
84 |
struct
|
haftmann@28256
|
85 |
|
blanchet@43861
|
86 |
val quickcheckN = "quickcheck"
|
blanchet@43861
|
87 |
|
blanchet@43861
|
88 |
val genuineN = "genuine"
|
blanchet@43861
|
89 |
val noneN = "none"
|
blanchet@43861
|
90 |
val unknownN = "unknown"
|
blanchet@43861
|
91 |
|
wenzelm@30981
|
92 |
(* preferences *)
|
wenzelm@30981
|
93 |
|
wenzelm@32740
|
94 |
val auto = Unsynchronized.ref false;
|
wenzelm@30981
|
95 |
|
wenzelm@30981
|
96 |
val _ =
|
wenzelm@30981
|
97 |
ProofGeneralPgip.add_preference Preferences.category_tracing
|
wenzelm@39862
|
98 |
(Unsynchronized.setmp auto true (fn () =>
|
wenzelm@30981
|
99 |
Preferences.bool_pref auto
|
wenzelm@30981
|
100 |
"auto-quickcheck"
|
blanchet@39575
|
101 |
"Run Quickcheck automatically.") ());
|
wenzelm@30981
|
102 |
|
bulwahn@35378
|
103 |
(* quickcheck report *)
|
bulwahn@35378
|
104 |
|
bulwahn@35378
|
105 |
datatype report = Report of
|
bulwahn@35378
|
106 |
{ iterations : int, raised_match_errors : int,
|
bulwahn@35378
|
107 |
satisfied_assms : int list, positive_concl_tests : int }
|
bulwahn@35378
|
108 |
|
bulwahn@42952
|
109 |
(* Quickcheck Result *)
|
bulwahn@42952
|
110 |
|
bulwahn@42952
|
111 |
datatype result = Result of
|
bulwahn@46598
|
112 |
{ counterexample : (bool * (string * term) list) option, evaluation_terms : (term * term) list option,
|
bulwahn@42952
|
113 |
timings : (string * int) list, reports : (int * report) list}
|
bulwahn@42952
|
114 |
|
bulwahn@42952
|
115 |
val empty_result =
|
bulwahn@42952
|
116 |
Result {counterexample = NONE, evaluation_terms = NONE, timings = [], reports = []}
|
bulwahn@42952
|
117 |
|
bulwahn@42952
|
118 |
fun counterexample_of (Result r) = #counterexample r
|
bulwahn@42952
|
119 |
|
bulwahn@42952
|
120 |
fun found_counterexample (Result r) = is_some (#counterexample r)
|
bulwahn@42952
|
121 |
|
bulwahn@42952
|
122 |
fun response_of (Result r) = case (#counterexample r, #evaluation_terms r) of
|
bulwahn@46601
|
123 |
(SOME ts, SOME evals) => SOME (ts, evals)
|
bulwahn@42952
|
124 |
| (NONE, NONE) => NONE
|
bulwahn@42952
|
125 |
|
bulwahn@42952
|
126 |
fun timings_of (Result r) = #timings r
|
bulwahn@42952
|
127 |
|
bulwahn@46598
|
128 |
fun set_response names eval_terms (SOME (genuine, ts)) (Result r) =
|
bulwahn@42952
|
129 |
let
|
bulwahn@42952
|
130 |
val (ts1, ts2) = chop (length names) ts
|
bulwahn@46029
|
131 |
val (eval_terms', _) = chop (length ts2) eval_terms
|
bulwahn@42952
|
132 |
in
|
bulwahn@46598
|
133 |
Result {counterexample = SOME (genuine, (names ~~ ts1)),
|
bulwahn@46598
|
134 |
evaluation_terms = SOME (eval_terms' ~~ ts2),
|
bulwahn@42952
|
135 |
timings = #timings r, reports = #reports r}
|
bulwahn@42952
|
136 |
end
|
bulwahn@46029
|
137 |
| set_response _ _ NONE result = result
|
bulwahn@42952
|
138 |
|
bulwahn@44456
|
139 |
|
bulwahn@42952
|
140 |
fun cons_timing timing (Result r) =
|
bulwahn@42952
|
141 |
Result {counterexample = #counterexample r, evaluation_terms = #evaluation_terms r,
|
bulwahn@42952
|
142 |
timings = cons timing (#timings r), reports = #reports r}
|
bulwahn@42952
|
143 |
|
bulwahn@42952
|
144 |
fun cons_report size (SOME report) (Result r) =
|
bulwahn@42952
|
145 |
Result {counterexample = #counterexample r, evaluation_terms = #evaluation_terms r,
|
bulwahn@42952
|
146 |
timings = #timings r, reports = cons (size, report) (#reports r)}
|
bulwahn@42952
|
147 |
| cons_report _ NONE result = result
|
bulwahn@42952
|
148 |
|
wenzelm@43071
|
149 |
fun add_timing timing result_ref =
|
wenzelm@43071
|
150 |
Unsynchronized.change result_ref (cons_timing timing)
|
bulwahn@42952
|
151 |
|
wenzelm@43071
|
152 |
fun add_report size report result_ref =
|
wenzelm@43071
|
153 |
Unsynchronized.change result_ref (cons_report size report)
|
bulwahn@42952
|
154 |
|
bulwahn@42952
|
155 |
fun add_response names eval_terms response result_ref =
|
bulwahn@46029
|
156 |
Unsynchronized.change result_ref (set_response names eval_terms response)
|
bulwahn@42952
|
157 |
|
bulwahn@38169
|
158 |
(* expectation *)
|
bulwahn@38169
|
159 |
|
wenzelm@41765
|
160 |
datatype expectation = No_Expectation | No_Counterexample | Counterexample;
|
bulwahn@38169
|
161 |
|
bulwahn@38169
|
162 |
fun merge_expectation (expect1, expect2) =
|
bulwahn@38169
|
163 |
if expect1 = expect2 then expect1 else No_Expectation
|
bulwahn@38169
|
164 |
|
haftmann@28315
|
165 |
(* quickcheck configuration -- default parameters, test generators *)
|
bulwahn@44753
|
166 |
val batch_tester = Attrib.setup_config_string @{binding quickcheck_batch_tester} (K "")
|
wenzelm@43487
|
167 |
val size = Attrib.setup_config_int @{binding quickcheck_size} (K 10)
|
wenzelm@43487
|
168 |
val iterations = Attrib.setup_config_int @{binding quickcheck_iterations} (K 100)
|
bulwahn@46084
|
169 |
val depth = Attrib.setup_config_int @{binding quickcheck_depth} (K 10)
|
bulwahn@46084
|
170 |
|
wenzelm@43487
|
171 |
val no_assms = Attrib.setup_config_bool @{binding quickcheck_no_assms} (K false)
|
bulwahn@48206
|
172 |
val locale = Attrib.setup_config_string @{binding quickcheck_locale} (K "interpret expand")
|
wenzelm@43487
|
173 |
val report = Attrib.setup_config_bool @{binding quickcheck_report} (K true)
|
wenzelm@43487
|
174 |
val timing = Attrib.setup_config_bool @{binding quickcheck_timing} (K false)
|
bulwahn@47436
|
175 |
val timeout = Attrib.setup_config_real @{binding quickcheck_timeout} (K 30.0)
|
bulwahn@47436
|
176 |
|
bulwahn@46628
|
177 |
val genuine_only = Attrib.setup_config_bool @{binding quickcheck_genuine_only} (K false)
|
bulwahn@47329
|
178 |
val abort_potential = Attrib.setup_config_bool @{binding quickcheck_abort_potential} (K false)
|
bulwahn@47436
|
179 |
|
wenzelm@43487
|
180 |
val quiet = Attrib.setup_config_bool @{binding quickcheck_quiet} (K false)
|
bulwahn@46635
|
181 |
val verbose = Attrib.setup_config_bool @{binding quickcheck_verbose} (K false)
|
bulwahn@47726
|
182 |
val tag = Attrib.setup_config_string @{binding quickcheck_tag} (K "")
|
bulwahn@47436
|
183 |
|
bulwahn@47436
|
184 |
val use_subtype = Attrib.setup_config_bool @{binding quickcheck_use_subtype} (K false)
|
bulwahn@47436
|
185 |
|
bulwahn@46320
|
186 |
val allow_function_inversion =
|
bulwahn@46320
|
187 |
Attrib.setup_config_bool @{binding quickcheck_allow_function_inversion} (K false)
|
wenzelm@43487
|
188 |
val finite_types = Attrib.setup_config_bool @{binding quickcheck_finite_types} (K true)
|
wenzelm@43487
|
189 |
val finite_type_size = Attrib.setup_config_int @{binding quickcheck_finite_type_size} (K 3)
|
bulwahn@40894
|
190 |
|
haftmann@28309
|
191 |
datatype test_params = Test_Params of
|
bulwahn@40892
|
192 |
{default_type: typ list, expect : expectation};
|
haftmann@28309
|
193 |
|
bulwahn@40892
|
194 |
fun dest_test_params (Test_Params {default_type, expect}) = (default_type, expect);
|
wenzelm@39034
|
195 |
|
wenzelm@41765
|
196 |
fun make_test_params (default_type, expect) =
|
wenzelm@41765
|
197 |
Test_Params {default_type = default_type, expect = expect};
|
wenzelm@39034
|
198 |
|
wenzelm@41765
|
199 |
fun map_test_params' f (Test_Params {default_type, expect}) =
|
wenzelm@41765
|
200 |
make_test_params (f (default_type, expect));
|
wenzelm@39034
|
201 |
|
wenzelm@39034
|
202 |
fun merge_test_params
|
wenzelm@41720
|
203 |
(Test_Params {default_type = default_type1, expect = expect1},
|
wenzelm@41720
|
204 |
Test_Params {default_type = default_type2, expect = expect2}) =
|
wenzelm@41720
|
205 |
make_test_params
|
wenzelm@41720
|
206 |
(merge (op =) (default_type1, default_type2), merge_expectation (expect1, expect2));
|
haftmann@28309
|
207 |
|
bulwahn@44749
|
208 |
type tester =
|
bulwahn@46290
|
209 |
Proof.context -> bool -> (string * typ) list -> (term * term list) list -> result list
|
bulwahn@44749
|
210 |
|
bulwahn@39479
|
211 |
structure Data = Generic_Data
|
wenzelm@33522
|
212 |
(
|
wenzelm@39034
|
213 |
type T =
|
bulwahn@44749
|
214 |
((string * (bool Config.T * tester)) list
|
bulwahn@43065
|
215 |
* ((string * (Proof.context -> term list -> (int -> term list option) list)) list
|
bulwahn@43065
|
216 |
* ((string * (Proof.context -> term list -> (int -> bool) list)) list)))
|
wenzelm@39034
|
217 |
* test_params;
|
bulwahn@44748
|
218 |
val empty = (([], ([], [])), Test_Params {default_type = [], expect = No_Expectation});
|
haftmann@28256
|
219 |
val extend = I;
|
bulwahn@44748
|
220 |
fun merge (((testers1, (batch_generators1, batch_validators1)), params1),
|
bulwahn@44748
|
221 |
((testers2, (batch_generators2, batch_validators2)), params2)) : T =
|
bulwahn@44748
|
222 |
((AList.merge (op =) (K true) (testers1, testers2),
|
bulwahn@43953
|
223 |
(AList.merge (op =) (K true) (batch_generators1, batch_generators2),
|
bulwahn@43953
|
224 |
AList.merge (op =) (K true) (batch_validators1, batch_validators2))),
|
haftmann@28309
|
225 |
merge_test_params (params1, params2));
|
wenzelm@33522
|
226 |
);
|
haftmann@28256
|
227 |
|
bulwahn@39479
|
228 |
val test_params_of = snd o Data.get o Context.Proof;
|
bulwahn@38150
|
229 |
|
bulwahn@40892
|
230 |
val default_type = fst o dest_test_params o test_params_of
|
bulwahn@40481
|
231 |
|
bulwahn@40892
|
232 |
val expect = snd o dest_test_params o test_params_of
|
bulwahn@40481
|
233 |
|
bulwahn@40481
|
234 |
val map_test_params = Data.map o apsnd o map_test_params'
|
bulwahn@39480
|
235 |
|
bulwahn@44748
|
236 |
val add_tester = Data.map o apfst o apfst o AList.update (op =);
|
bulwahn@42733
|
237 |
|
bulwahn@43065
|
238 |
val add_batch_generator = Data.map o apfst o apsnd o apfst o AList.update (op =);
|
bulwahn@43065
|
239 |
|
bulwahn@43065
|
240 |
val add_batch_validator = Data.map o apfst o apsnd o apsnd o AList.update (op =);
|
haftmann@28256
|
241 |
|
bulwahn@44752
|
242 |
fun active_testers ctxt =
|
bulwahn@44752
|
243 |
let
|
bulwahn@44752
|
244 |
val testers = (map snd o fst o fst o Data.get o Context.Proof) ctxt
|
bulwahn@44752
|
245 |
in
|
bulwahn@44752
|
246 |
map snd (filter (fn (active, _) => Config.get ctxt active) testers)
|
bulwahn@44752
|
247 |
end
|
bulwahn@44752
|
248 |
|
bulwahn@44755
|
249 |
fun set_active_testers [] gen_ctxt = gen_ctxt
|
bulwahn@44755
|
250 |
| set_active_testers testers gen_ctxt =
|
bulwahn@44752
|
251 |
let
|
bulwahn@44752
|
252 |
val registered_testers = (fst o fst o Data.get) gen_ctxt
|
bulwahn@44752
|
253 |
in
|
bulwahn@44752
|
254 |
fold (fn (name, (config, _)) => Config.put_generic config (member (op =) testers name))
|
bulwahn@44752
|
255 |
registered_testers gen_ctxt
|
bulwahn@44752
|
256 |
end
|
bulwahn@44752
|
257 |
|
haftmann@28315
|
258 |
(* generating tests *)
|
haftmann@28315
|
259 |
|
bulwahn@42733
|
260 |
fun gen_mk_tester lookup ctxt v =
|
haftmann@28309
|
261 |
let
|
bulwahn@44753
|
262 |
val name = Config.get ctxt batch_tester
|
bulwahn@42733
|
263 |
val tester = case lookup ctxt name
|
bulwahn@44753
|
264 |
of NONE => error ("No such quickcheck batch-tester: " ^ name)
|
bulwahn@41153
|
265 |
| SOME tester => tester ctxt;
|
wenzelm@40491
|
266 |
in
|
bulwahn@41153
|
267 |
if Config.get ctxt quiet then
|
bulwahn@42733
|
268 |
try tester v
|
bulwahn@41153
|
269 |
else
|
wenzelm@44633
|
270 |
let (* FIXME !?!? *)
|
bulwahn@42733
|
271 |
val tester = Exn.interruptible_capture tester v
|
wenzelm@44633
|
272 |
in case Exn.get_res tester of
|
bulwahn@41153
|
273 |
NONE => SOME (Exn.release tester)
|
bulwahn@41153
|
274 |
| SOME tester => SOME tester
|
bulwahn@41153
|
275 |
end
|
bulwahn@41153
|
276 |
end
|
bulwahn@44753
|
277 |
|
bulwahn@43065
|
278 |
val mk_batch_tester =
|
bulwahn@43065
|
279 |
gen_mk_tester (fn ctxt => AList.lookup (op =) ((fst o snd o fst o Data.get o Context.Proof) ctxt))
|
bulwahn@43065
|
280 |
val mk_batch_validator =
|
bulwahn@43065
|
281 |
gen_mk_tester (fn ctxt => AList.lookup (op =) ((snd o snd o fst o Data.get o Context.Proof) ctxt))
|
bulwahn@43953
|
282 |
|
haftmann@28315
|
283 |
(* testing propositions *)
|
haftmann@28315
|
284 |
|
bulwahn@44747
|
285 |
type compile_generator =
|
bulwahn@44747
|
286 |
Proof.context -> (term * term list) list -> int list -> term list option * report option
|
bulwahn@44747
|
287 |
|
bulwahn@44456
|
288 |
fun limit timeout (limit_time, is_interactive) f exc () =
|
bulwahn@42625
|
289 |
if limit_time then
|
bulwahn@44456
|
290 |
TimeLimit.timeLimit timeout f ()
|
bulwahn@42625
|
291 |
handle TimeLimit.TimeOut =>
|
bulwahn@42625
|
292 |
if is_interactive then exc () else raise TimeLimit.TimeOut
|
bulwahn@42625
|
293 |
else
|
bulwahn@42625
|
294 |
f ()
|
bulwahn@42624
|
295 |
|
bulwahn@44786
|
296 |
fun message ctxt s = if Config.get ctxt quiet then () else Output.urgent_message s
|
bulwahn@46636
|
297 |
|
bulwahn@46636
|
298 |
fun verbose_message ctxt s = if not (Config.get ctxt quiet) andalso Config.get ctxt verbose
|
bulwahn@46636
|
299 |
then Output.urgent_message s else ()
|
bulwahn@46636
|
300 |
|
bulwahn@46029
|
301 |
fun test_terms ctxt (limit_time, is_interactive) insts goals =
|
bulwahn@44749
|
302 |
case active_testers ctxt of
|
bulwahn@44752
|
303 |
[] => error "No active testers for quickcheck"
|
bulwahn@46636
|
304 |
| testers =>
|
bulwahn@46636
|
305 |
limit (seconds (Config.get ctxt timeout)) (limit_time, is_interactive)
|
bulwahn@44786
|
306 |
(fn () => Par_List.get_some (fn tester =>
|
bulwahn@46290
|
307 |
tester ctxt (length testers > 1) insts goals |>
|
bulwahn@44786
|
308 |
(fn result => if exists found_counterexample result then SOME result else NONE)) testers)
|
bulwahn@44786
|
309 |
(fn () => (message ctxt "Quickcheck ran out of time"; NONE)) ();
|
bulwahn@47629
|
310 |
|
bulwahn@47629
|
311 |
fun all_axioms_of ctxt t =
|
bulwahn@47629
|
312 |
let
|
bulwahn@47629
|
313 |
val intros = Locale.get_intros ctxt
|
bulwahn@47629
|
314 |
val unfolds = Locale.get_unfolds ctxt
|
bulwahn@47629
|
315 |
fun retrieve_prems thms t =
|
bulwahn@47629
|
316 |
case filter (fn th => Term.could_unify (Thm.concl_of th, t)) thms of
|
bulwahn@47629
|
317 |
[] => NONE
|
bulwahn@47629
|
318 |
| [th] =>
|
bulwahn@47629
|
319 |
let
|
bulwahn@47629
|
320 |
val (tyenv, tenv) =
|
bulwahn@47629
|
321 |
Pattern.match (Proof_Context.theory_of ctxt) (Thm.concl_of th, t) (Vartab.empty, Vartab.empty)
|
bulwahn@47629
|
322 |
in SOME (map (Envir.subst_term (tyenv, tenv)) (Thm.prems_of th)) end
|
bulwahn@47629
|
323 |
fun all t =
|
bulwahn@47629
|
324 |
case retrieve_prems intros t of
|
bulwahn@47629
|
325 |
NONE => retrieve_prems unfolds t
|
bulwahn@47629
|
326 |
| SOME ts => SOME (maps (fn t => the_default [t] (all t)) ts)
|
bulwahn@47629
|
327 |
in
|
bulwahn@47629
|
328 |
all t
|
bulwahn@47629
|
329 |
end
|
bulwahn@47629
|
330 |
|
bulwahn@48206
|
331 |
fun locale_config_of s =
|
bulwahn@48206
|
332 |
let
|
bulwahn@48206
|
333 |
val cs = space_explode " " s
|
bulwahn@48206
|
334 |
in
|
bulwahn@48206
|
335 |
if forall (fn c => c = "expand" orelse c = "interpret") cs then cs
|
wenzelm@48254
|
336 |
else (warning ("Invalid quickcheck_locale setting: falling back to the default setting.");
|
bulwahn@48206
|
337 |
["interpret", "expand"])
|
bulwahn@48206
|
338 |
end
|
bulwahn@48206
|
339 |
|
bulwahn@42897
|
340 |
fun test_goal (time_limit, is_interactive) (insts, eval_terms) i state =
|
bulwahn@40896
|
341 |
let
|
bulwahn@40896
|
342 |
val lthy = Proof.context_of state;
|
bulwahn@40896
|
343 |
val thy = Proof.theory_of state;
|
bulwahn@46636
|
344 |
val _ = message lthy "Quickchecking..."
|
bulwahn@40896
|
345 |
fun strip (Const ("all", _) $ Abs (_, _, t)) = strip t
|
bulwahn@40896
|
346 |
| strip t = t;
|
bulwahn@40896
|
347 |
val {goal = st, ...} = Proof.raw_goal state;
|
bulwahn@40896
|
348 |
val (gi, frees) = Logic.goal_params (prop_of st) i;
|
bulwahn@40896
|
349 |
val some_locale = case (Option.map #target o Named_Target.peek) lthy
|
bulwahn@40896
|
350 |
of NONE => NONE
|
bulwahn@40896
|
351 |
| SOME "" => NONE
|
bulwahn@40896
|
352 |
| SOME locale => SOME locale;
|
bulwahn@40896
|
353 |
val assms = if Config.get lthy no_assms then [] else case some_locale
|
bulwahn@40896
|
354 |
of NONE => Assumption.all_assms_of lthy
|
bulwahn@40896
|
355 |
| SOME locale => Assumption.local_assms_of lthy (Locale.init locale thy);
|
bulwahn@40896
|
356 |
val proto_goal = Logic.list_implies (map Thm.term_of assms, subst_bounds (frees, strip gi));
|
bulwahn@47629
|
357 |
fun axioms_of locale = case fst (Locale.specification_of thy locale) of
|
bulwahn@47629
|
358 |
NONE => []
|
bulwahn@47629
|
359 |
| SOME t => the_default [] (all_axioms_of lthy t)
|
bulwahn@48206
|
360 |
val config = locale_config_of (Config.get lthy locale)
|
bulwahn@48206
|
361 |
val goals = case some_locale
|
bulwahn@42897
|
362 |
of NONE => [(proto_goal, eval_terms)]
|
bulwahn@48206
|
363 |
| SOME locale => fold (fn c =>
|
bulwahn@48206
|
364 |
if c = "expand" then cons (Logic.list_implies (axioms_of locale, proto_goal), eval_terms) else
|
bulwahn@48206
|
365 |
if c = "interpret" then
|
bulwahn@48206
|
366 |
append (map (fn (_, phi) => (Morphism.term phi proto_goal, map (Morphism.term phi) eval_terms))
|
bulwahn@48206
|
367 |
(Locale.registrations_of (Context.Theory thy) (*FIXME*) locale)) else I) config [];
|
bulwahn@47629
|
368 |
val _ = verbose_message lthy (Pretty.string_of
|
bulwahn@47629
|
369 |
(Pretty.big_list ("Checking goals: ") (map (Syntax.pretty_term lthy o fst) goals)))
|
bulwahn@40896
|
370 |
in
|
bulwahn@46029
|
371 |
test_terms lthy (time_limit, is_interactive) insts goals
|
bulwahn@40896
|
372 |
end
|
bulwahn@40896
|
373 |
|
bulwahn@38152
|
374 |
(* pretty printing *)
|
haftmann@28315
|
375 |
|
blanchet@40466
|
376 |
fun tool_name auto = (if auto then "Auto " else "") ^ "Quickcheck"
|
blanchet@40466
|
377 |
|
bulwahn@47726
|
378 |
fun pretty_counterex ctxt auto NONE =
|
bulwahn@47726
|
379 |
Pretty.str (tool_name auto ^ " found no counterexample." ^ Config.get ctxt tag)
|
bulwahn@46601
|
380 |
| pretty_counterex ctxt auto (SOME ((genuine, cex), eval_terms)) =
|
bulwahn@46601
|
381 |
Pretty.chunks ((Pretty.str (tool_name auto ^ " found a " ^
|
bulwahn@47726
|
382 |
(if genuine then "counterexample:"
|
bulwahn@47726
|
383 |
else "potentially spurious counterexample due to underspecified functions:")
|
bulwahn@47726
|
384 |
^ Config.get ctxt tag ^ "\n") ::
|
haftmann@28315
|
385 |
map (fn (s, t) =>
|
bulwahn@42899
|
386 |
Pretty.block [Pretty.str (s ^ " ="), Pretty.brk 1, Syntax.pretty_term ctxt t]) (rev cex))
|
blanchet@43861
|
387 |
@ (if null eval_terms then []
|
bulwahn@46029
|
388 |
else (Pretty.fbrk :: Pretty.str ("Evaluated terms:") ::
|
blanchet@43861
|
389 |
map (fn (t, u) =>
|
bulwahn@44752
|
390 |
Pretty.block [Syntax.pretty_term ctxt t, Pretty.str " =", Pretty.brk 1,
|
bulwahn@44752
|
391 |
Syntax.pretty_term ctxt u]) (rev eval_terms))));
|
haftmann@28315
|
392 |
|
bulwahn@35378
|
393 |
fun pretty_report (Report {iterations = iterations, raised_match_errors = raised_match_errors,
|
bulwahn@35378
|
394 |
satisfied_assms = satisfied_assms, positive_concl_tests = positive_concl_tests}) =
|
bulwahn@35378
|
395 |
let
|
bulwahn@35378
|
396 |
fun pretty_stat s i = Pretty.block ([Pretty.str (s ^ ": " ^ string_of_int i)])
|
bulwahn@35378
|
397 |
in
|
bulwahn@35378
|
398 |
([pretty_stat "iterations" iterations,
|
bulwahn@35378
|
399 |
pretty_stat "match exceptions" raised_match_errors]
|
wenzelm@41765
|
400 |
@ map_index
|
wenzelm@41765
|
401 |
(fn (i, n) => pretty_stat ("satisfied " ^ string_of_int (i + 1) ^ ". assumption") n)
|
bulwahn@35378
|
402 |
satisfied_assms
|
bulwahn@35378
|
403 |
@ [pretty_stat "positive conclusion tests" positive_concl_tests])
|
bulwahn@35378
|
404 |
end
|
bulwahn@35378
|
405 |
|
bulwahn@42952
|
406 |
fun pretty_timings timings =
|
bulwahn@42952
|
407 |
Pretty.chunks (Pretty.fbrk :: Pretty.str "timings:" ::
|
bulwahn@42952
|
408 |
maps (fn (label, time) =>
|
bulwahn@42952
|
409 |
Pretty.str (label ^ ": " ^ string_of_int time ^ " ms") :: Pretty.brk 1 :: []) (rev timings))
|
bulwahn@42952
|
410 |
|
wenzelm@30981
|
411 |
(* Isar commands *)
|
haftmann@28315
|
412 |
|
haftmann@28336
|
413 |
fun read_nat s = case (Library.read_int o Symbol.explode) s
|
haftmann@28336
|
414 |
of (k, []) => if k >= 0 then k
|
haftmann@28336
|
415 |
else error ("Not a natural number: " ^ s)
|
haftmann@28336
|
416 |
| (_, _ :: _) => error ("Not a natural number: " ^ s);
|
bulwahn@38149
|
417 |
|
blanchet@34125
|
418 |
fun read_bool "false" = false
|
blanchet@34125
|
419 |
| read_bool "true" = true
|
blanchet@34125
|
420 |
| read_bool s = error ("Not a Boolean value: " ^ s)
|
haftmann@28315
|
421 |
|
bulwahn@40612
|
422 |
fun read_real s =
|
bulwahn@40612
|
423 |
case (Real.fromString s) of
|
bulwahn@40612
|
424 |
SOME s => s
|
bulwahn@40612
|
425 |
| NONE => error ("Not a real number: " ^ s)
|
bulwahn@40612
|
426 |
|
bulwahn@38169
|
427 |
fun read_expectation "no_expectation" = No_Expectation
|
wenzelm@41765
|
428 |
| read_expectation "no_counterexample" = No_Counterexample
|
bulwahn@38169
|
429 |
| read_expectation "counterexample" = Counterexample
|
wenzelm@41765
|
430 |
| read_expectation s = error ("Not an expectation value: " ^ s)
|
bulwahn@38169
|
431 |
|
bulwahn@44748
|
432 |
fun valid_tester_name genctxt name = AList.defined (op =) (fst (fst (Data.get genctxt))) name
|
bulwahn@44748
|
433 |
|
bulwahn@44752
|
434 |
fun parse_tester name (testers, genctxt) =
|
bulwahn@41156
|
435 |
if valid_tester_name genctxt name then
|
bulwahn@44752
|
436 |
(insert (op =) name testers, genctxt)
|
bulwahn@41156
|
437 |
else
|
bulwahn@41156
|
438 |
error ("Unknown tester: " ^ name)
|
bulwahn@41156
|
439 |
|
bulwahn@44752
|
440 |
fun parse_test_param ("tester", args) = fold parse_tester args
|
bulwahn@44752
|
441 |
| parse_test_param ("size", [arg]) = apsnd (Config.put_generic size (read_nat arg))
|
bulwahn@44752
|
442 |
| parse_test_param ("iterations", [arg]) = apsnd (Config.put_generic iterations (read_nat arg))
|
bulwahn@46084
|
443 |
| parse_test_param ("depth", [arg]) = apsnd (Config.put_generic depth (read_nat arg))
|
bulwahn@44752
|
444 |
| parse_test_param ("default_type", arg) = (fn (testers, gen_ctxt) =>
|
bulwahn@44752
|
445 |
(testers, map_test_params
|
bulwahn@44752
|
446 |
((apfst o K) (map (Proof_Context.read_typ (Context.proof_of gen_ctxt)) arg)) gen_ctxt))
|
bulwahn@44752
|
447 |
| parse_test_param ("no_assms", [arg]) = apsnd (Config.put_generic no_assms (read_bool arg))
|
bulwahn@44752
|
448 |
| parse_test_param ("expect", [arg]) = apsnd (map_test_params ((apsnd o K) (read_expectation arg)))
|
bulwahn@44752
|
449 |
| parse_test_param ("report", [arg]) = apsnd (Config.put_generic report (read_bool arg))
|
bulwahn@46628
|
450 |
| parse_test_param ("genuine_only", [arg]) = apsnd (Config.put_generic genuine_only (read_bool arg))
|
bulwahn@47329
|
451 |
| parse_test_param ("abort_potential", [arg]) = apsnd (Config.put_generic abort_potential (read_bool arg))
|
bulwahn@44752
|
452 |
| parse_test_param ("quiet", [arg]) = apsnd (Config.put_generic quiet (read_bool arg))
|
bulwahn@46635
|
453 |
| parse_test_param ("verbose", [arg]) = apsnd (Config.put_generic verbose (read_bool arg))
|
bulwahn@47726
|
454 |
| parse_test_param ("tag", [arg]) = apsnd (Config.put_generic tag arg)
|
bulwahn@47436
|
455 |
| parse_test_param ("use_subtype", [arg]) = apsnd (Config.put_generic use_subtype (read_bool arg))
|
bulwahn@44752
|
456 |
| parse_test_param ("timeout", [arg]) = apsnd (Config.put_generic timeout (read_real arg))
|
bulwahn@44752
|
457 |
| parse_test_param ("finite_types", [arg]) = apsnd (Config.put_generic finite_types (read_bool arg))
|
bulwahn@46320
|
458 |
| parse_test_param ("allow_function_inversion", [arg]) =
|
bulwahn@46320
|
459 |
apsnd (Config.put_generic allow_function_inversion (read_bool arg))
|
wenzelm@41765
|
460 |
| parse_test_param ("finite_type_size", [arg]) =
|
bulwahn@44752
|
461 |
apsnd (Config.put_generic finite_type_size (read_nat arg))
|
bulwahn@44752
|
462 |
| parse_test_param (name, _) = (fn (testers, genctxt) =>
|
bulwahn@41156
|
463 |
if valid_tester_name genctxt name then
|
bulwahn@44752
|
464 |
(insert (op =) name testers, genctxt)
|
bulwahn@44752
|
465 |
else error ("Unknown tester or test parameter: " ^ name));
|
haftmann@28315
|
466 |
|
bulwahn@44752
|
467 |
fun proof_map_result f = apsnd Context.the_proof o f o Context.Proof;
|
bulwahn@44752
|
468 |
|
bulwahn@44752
|
469 |
fun parse_test_param_inst (name, arg) ((insts, eval_terms), (testers, ctxt)) =
|
wenzelm@43232
|
470 |
case try (Proof_Context.read_typ ctxt) name
|
bulwahn@42896
|
471 |
of SOME (TFree (v, _)) =>
|
bulwahn@44752
|
472 |
((AList.update (op =) (v, Proof_Context.read_typ ctxt (the_single arg)) insts, eval_terms),
|
bulwahn@44752
|
473 |
(testers, ctxt))
|
bulwahn@42896
|
474 |
| NONE => (case name of
|
bulwahn@44752
|
475 |
"eval" => ((insts, eval_terms @ map (Syntax.read_term ctxt) arg), (testers, ctxt))
|
bulwahn@44752
|
476 |
| _ => ((insts, eval_terms),
|
bulwahn@44752
|
477 |
proof_map_result (fn gen_ctxt => parse_test_param (name, arg) (testers, gen_ctxt)) ctxt));
|
haftmann@28315
|
478 |
|
bulwahn@44752
|
479 |
fun quickcheck_params_cmd args = Context.theory_map
|
bulwahn@44752
|
480 |
(fn gen_ctxt => uncurry set_active_testers (fold parse_test_param args ([], gen_ctxt)));
|
wenzelm@41765
|
481 |
|
bulwahn@42952
|
482 |
fun check_expectation state results =
|
bulwahn@44750
|
483 |
(if is_some results andalso expect (Proof.context_of state) = No_Counterexample
|
bulwahn@42952
|
484 |
then
|
bulwahn@42952
|
485 |
error ("quickcheck expected to find no counterexample but found one")
|
bulwahn@42952
|
486 |
else
|
bulwahn@44750
|
487 |
(if is_none results andalso expect (Proof.context_of state) = Counterexample
|
bulwahn@42952
|
488 |
then
|
bulwahn@42952
|
489 |
error ("quickcheck expected to find a counterexample but did not find one")
|
bulwahn@42952
|
490 |
else ()))
|
bulwahn@42952
|
491 |
|
bulwahn@35378
|
492 |
fun gen_quickcheck args i state =
|
bulwahn@40892
|
493 |
state
|
bulwahn@44752
|
494 |
|> Proof.map_context_result (fn ctxt =>
|
bulwahn@44752
|
495 |
apsnd (fn (testers, ctxt) => Context.proof_map (set_active_testers testers) ctxt)
|
bulwahn@44752
|
496 |
(fold parse_test_param_inst args (([], []), ([], ctxt))))
|
bulwahn@42897
|
497 |
|> (fn ((insts, eval_terms), state') => test_goal (true, true) (insts, eval_terms) i state'
|
bulwahn@47726
|
498 |
|> tap (check_expectation state') |> rpair state')
|
boehmes@32295
|
499 |
|
bulwahn@44750
|
500 |
fun quickcheck args i state =
|
bulwahn@47726
|
501 |
Option.map (the o get_first counterexample_of) (fst (gen_quickcheck args i state))
|
bulwahn@35378
|
502 |
|
boehmes@32295
|
503 |
fun quickcheck_cmd args i state =
|
bulwahn@35378
|
504 |
gen_quickcheck args i (Toplevel.proof_of state)
|
bulwahn@47726
|
505 |
|> apfst (Option.map (the o get_first response_of))
|
bulwahn@47726
|
506 |
|> (fn (r, state) => Output.urgent_message (Pretty.string_of
|
bulwahn@47726
|
507 |
(pretty_counterex (Proof.context_of state) false r)));
|
haftmann@28309
|
508 |
|
wenzelm@41765
|
509 |
val parse_arg =
|
wenzelm@41765
|
510 |
Parse.name --
|
wenzelm@47823
|
511 |
(Scan.optional (@{keyword "="} |--
|
wenzelm@41765
|
512 |
(((Parse.name || Parse.float_number) >> single) ||
|
wenzelm@47823
|
513 |
(@{keyword "["} |-- Parse.list1 Parse.name --| @{keyword "]"}))) ["true"]);
|
haftmann@28309
|
514 |
|
wenzelm@41765
|
515 |
val parse_args =
|
wenzelm@47823
|
516 |
@{keyword "["} |-- Parse.list1 parse_arg --| @{keyword "]"} || Scan.succeed [];
|
haftmann@28336
|
517 |
|
wenzelm@36970
|
518 |
val _ =
|
wenzelm@47836
|
519 |
Outer_Syntax.command @{command_spec "quickcheck_params"} "set parameters for random testing"
|
wenzelm@36970
|
520 |
(parse_args >> (fn args => Toplevel.theory (quickcheck_params_cmd args)));
|
haftmann@28309
|
521 |
|
wenzelm@36970
|
522 |
val _ =
|
wenzelm@47836
|
523 |
Outer_Syntax.improper_command @{command_spec "quickcheck"}
|
wenzelm@47836
|
524 |
"try to find counterexample for subgoal"
|
wenzelm@36970
|
525 |
(parse_args -- Scan.optional Parse.nat 1
|
wenzelm@36970
|
526 |
>> (fn (args, i) => Toplevel.no_timing o Toplevel.keep (quickcheck_cmd args i)));
|
haftmann@28309
|
527 |
|
blanchet@43861
|
528 |
(* automatic testing *)
|
blanchet@43861
|
529 |
|
blanchet@43861
|
530 |
fun try_quickcheck auto state =
|
blanchet@43861
|
531 |
let
|
blanchet@43861
|
532 |
val ctxt = Proof.context_of state;
|
blanchet@43861
|
533 |
val i = 1;
|
blanchet@43861
|
534 |
val res =
|
blanchet@43861
|
535 |
state
|
blanchet@43861
|
536 |
|> Proof.map_context (Config.put report false #> Config.put quiet true)
|
blanchet@43861
|
537 |
|> try (test_goal (false, false) ([], []) i);
|
blanchet@43861
|
538 |
in
|
blanchet@43861
|
539 |
case res of
|
blanchet@43861
|
540 |
NONE => (unknownN, state)
|
blanchet@43861
|
541 |
| SOME results =>
|
blanchet@43861
|
542 |
let
|
bulwahn@44750
|
543 |
val msg = pretty_counterex ctxt auto (Option.map (the o get_first response_of) results)
|
blanchet@43861
|
544 |
in
|
bulwahn@44750
|
545 |
if is_some results then
|
blanchet@43861
|
546 |
(genuineN,
|
blanchet@43870
|
547 |
state
|
blanchet@43870
|
548 |
|> (if auto then
|
blanchet@43870
|
549 |
Proof.goal_message (K (Pretty.chunks [Pretty.str "",
|
wenzelm@46537
|
550 |
Pretty.mark Isabelle_Markup.hilite msg]))
|
blanchet@43870
|
551 |
else
|
blanchet@43870
|
552 |
tap (fn _ => Output.urgent_message (Pretty.string_of msg))))
|
blanchet@43861
|
553 |
else
|
blanchet@43861
|
554 |
(noneN, state)
|
blanchet@43861
|
555 |
end
|
blanchet@43861
|
556 |
end
|
blanchet@43861
|
557 |
|> `(fn (outcome_code, _) => outcome_code = genuineN)
|
blanchet@43861
|
558 |
|
blanchet@43870
|
559 |
val setup = Try.register_tool (quickcheckN, (20, auto, try_quickcheck))
|
blanchet@43861
|
560 |
|
haftmann@28309
|
561 |
end;
|
haftmann@28256
|
562 |
|
haftmann@28315
|
563 |
val auto_quickcheck = Quickcheck.auto;
|