1 (* Author: Lukas Bulwahn, TU Muenchen *)
3 header {* A simple counterexample generator performing exhaustive testing *}
5 theory Quickcheck_Exhaustive
7 uses ("Tools/Quickcheck/exhaustive_generators.ML")
10 subsection {* basic operations for exhaustive generators *}
12 definition orelse :: "'a option => 'a option => 'a option" (infixr "orelse" 55)
14 [code_unfold]: "x orelse y = (case x of Some x' => Some x' | None => y)"
16 subsection {* exhaustive generator type classes *}
18 class exhaustive = term_of +
19 fixes exhaustive :: "('a \<Rightarrow> term list option) \<Rightarrow> code_numeral \<Rightarrow> term list option"
21 class full_exhaustive = term_of +
22 fixes full_exhaustive :: "('a * (unit => term) \<Rightarrow> term list option) \<Rightarrow> code_numeral \<Rightarrow> term list option"
24 instantiation code_numeral :: full_exhaustive
27 function full_exhaustive_code_numeral' :: "(code_numeral * (unit => term) => term list option) => code_numeral => code_numeral => term list option"
28 where "full_exhaustive_code_numeral' f d i =
30 else (f (i, %_. Code_Evaluation.term_of i)) orelse (full_exhaustive_code_numeral' f d (i + 1)))"
31 by pat_completeness auto
34 by (relation "measure (%(_, d, i). Code_Numeral.nat_of (d + 1 - i))") auto
36 definition "full_exhaustive f d = full_exhaustive_code_numeral' f d 0"
42 instantiation code_numeral :: exhaustive
45 function exhaustive_code_numeral' :: "(code_numeral => term list option) => code_numeral => code_numeral => term list option"
46 where "exhaustive_code_numeral' f d i =
48 else (f i orelse exhaustive_code_numeral' f d (i + 1)))"
49 by pat_completeness auto
52 by (relation "measure (%(_, d, i). Code_Numeral.nat_of (d + 1 - i))") auto
54 definition "exhaustive f d = exhaustive_code_numeral' f d 0"
60 instantiation nat :: exhaustive
63 definition "exhaustive f d = exhaustive (%x. f (Code_Numeral.nat_of x)) d"
69 instantiation nat :: full_exhaustive
72 definition "full_exhaustive f d = full_exhaustive (%(x, xt). f (Code_Numeral.nat_of x, %_. Code_Evaluation.term_of (Code_Numeral.nat_of x))) d"
78 instantiation int :: exhaustive
81 function exhaustive' :: "(int => term list option) => int => int => term list option"
82 where "exhaustive' f d i = (if d < i then None else (f i orelse exhaustive' f d (i + 1)))"
83 by pat_completeness auto
86 by (relation "measure (%(_, d, i). nat (d + 1 - i))") auto
88 definition "exhaustive f d = exhaustive' f (Code_Numeral.int_of d) (- (Code_Numeral.int_of d))"
94 instantiation int :: full_exhaustive
97 function full_exhaustive' :: "(int * (unit => term) => term list option) => int => int => term list option"
98 where "full_exhaustive' f d i = (if d < i then None else (case f (i, %_. Code_Evaluation.term_of i) of Some t => Some t | None => full_exhaustive' f d (i + 1)))"
99 by pat_completeness auto
102 by (relation "measure (%(_, d, i). nat (d + 1 - i))") auto
104 definition "full_exhaustive f d = full_exhaustive' f (Code_Numeral.int_of d) (- (Code_Numeral.int_of d))"
110 instantiation prod :: (exhaustive, exhaustive) exhaustive
114 "exhaustive f d = exhaustive (%x. exhaustive (%y. f ((x, y))) d) d"
120 instantiation prod :: (full_exhaustive, full_exhaustive) full_exhaustive
124 "full_exhaustive f d = full_exhaustive (%(x, t1). full_exhaustive (%(y, t2). f ((x, y),
125 %u. let T1 = (Typerep.typerep (TYPE('a)));
126 T2 = (Typerep.typerep (TYPE('b)))
127 in Code_Evaluation.App (Code_Evaluation.App (
128 Code_Evaluation.Const (STR ''Product_Type.Pair'')
129 (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Product_Type.prod'') [T1, T2]]]))
130 (t1 ())) (t2 ()))) d) d"
136 instantiation "fun" :: ("{equal, exhaustive}", exhaustive) exhaustive
139 fun exhaustive_fun' :: "(('a => 'b) => term list option) => code_numeral => code_numeral => term list option"
141 "exhaustive_fun' f i d = (exhaustive (%b. f (%_. b)) d)
142 orelse (if i > 1 then
143 exhaustive_fun' (%g. exhaustive (%a. exhaustive (%b.
144 f (g(a := b))) d) d) (i - 1) d else None)"
146 definition exhaustive_fun :: "(('a => 'b) => term list option) => code_numeral => term list option"
148 "exhaustive_fun f d = exhaustive_fun' f d d"
154 instantiation "fun" :: ("{equal, full_exhaustive}", full_exhaustive) full_exhaustive
157 fun full_exhaustive_fun' :: "(('a => 'b) * (unit => term) => term list option) => code_numeral => code_numeral => term list option"
159 "full_exhaustive_fun' f i d = (full_exhaustive (%(b, t). f (%_. b, %_. Code_Evaluation.Abs (STR ''x'') (Typerep.typerep TYPE('a)) (t ()))) d)
160 orelse (if i > 1 then
161 full_exhaustive_fun' (%(g, gt). full_exhaustive (%(a, at). full_exhaustive (%(b, bt).
163 (%_. let A = (Typerep.typerep (TYPE('a)));
164 B = (Typerep.typerep (TYPE('b)));
165 fun = (%T U. Typerep.Typerep (STR ''fun'') [T, U])
167 Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.App
168 (Code_Evaluation.Const (STR ''Fun.fun_upd'') (fun (fun A B) (fun A (fun B (fun A B)))))
169 (gt ())) (at ())) (bt ())))) d) d) (i - 1) d else None)"
171 definition full_exhaustive_fun :: "(('a => 'b) * (unit => term) => term list option) => code_numeral => term list option"
173 "full_exhaustive_fun f d = full_exhaustive_fun' f d d"
179 subsubsection {* A smarter enumeration scheme for functions over finite datatypes *}
181 class check_all = enum + term_of +
182 fixes check_all :: "('a * (unit \<Rightarrow> term) \<Rightarrow> term list option) \<Rightarrow> term list option"
183 fixes enum_term_of :: "'a itself \<Rightarrow> unit \<Rightarrow> term list"
185 fun check_all_n_lists :: "(('a :: check_all) list * (unit \<Rightarrow> term list) \<Rightarrow> term list option) \<Rightarrow> code_numeral \<Rightarrow> term list option"
187 "check_all_n_lists f n =
188 (if n = 0 then f ([], (%_. [])) else check_all (%(x, xt). check_all_n_lists (%(xs, xst). f ((x # xs), (%_. (xt () # xst ())))) (n - 1)))"
190 definition mk_map_term :: " (unit \<Rightarrow> typerep) \<Rightarrow> (unit \<Rightarrow> typerep) \<Rightarrow> (unit \<Rightarrow> term list) \<Rightarrow> (unit \<Rightarrow> term list) \<Rightarrow> unit \<Rightarrow> term"
192 "mk_map_term T1 T2 domm rng =
195 update_term = (%g (a, b).
196 Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.App
197 (Code_Evaluation.Const (STR ''Fun.fun_upd'')
198 (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''fun'') [T1, T2],
199 Typerep.Typerep (STR ''fun'') [T1,
200 Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''fun'') [T1, T2]]]]))
203 List.foldl update_term (Code_Evaluation.Abs (STR ''x'') T1 (Code_Evaluation.Const (STR ''HOL.undefined'') T2)) (zip (domm ()) (rng ())))"
205 instantiation "fun" :: ("{equal, check_all}", check_all) check_all
211 mk_term = mk_map_term (%_. Typerep.typerep (TYPE('a))) (%_. Typerep.typerep (TYPE('b))) (enum_term_of (TYPE('a)));
212 enum = (Enum.enum :: 'a list)
213 in check_all_n_lists (\<lambda>(ys, yst). f (the o map_of (zip enum ys), mk_term yst)) (Code_Numeral.of_nat (length enum)))"
215 definition enum_term_of_fun :: "('a => 'b) itself => unit => term list"
217 "enum_term_of_fun = (%_ _. let
218 enum_term_of_a = enum_term_of (TYPE('a));
219 mk_term = mk_map_term (%_. Typerep.typerep (TYPE('a))) (%_. Typerep.typerep (TYPE('b))) enum_term_of_a
220 in map (%ys. mk_term (%_. ys) ()) (Enum.n_lists (length (enum_term_of_a ())) (enum_term_of (TYPE('b)) ())))"
227 instantiation unit :: check_all
231 "check_all f = f (Code_Evaluation.valtermify ())"
233 definition enum_term_of_unit :: "unit itself => unit => term list"
235 "enum_term_of_unit = (%_ _. [Code_Evaluation.term_of ()])"
242 instantiation bool :: check_all
246 "check_all f = (case f (Code_Evaluation.valtermify False) of Some x' \<Rightarrow> Some x' | None \<Rightarrow> f (Code_Evaluation.valtermify True))"
248 definition enum_term_of_bool :: "bool itself => unit => term list"
250 "enum_term_of_bool = (%_ _. map Code_Evaluation.term_of (Enum.enum :: bool list))"
257 instantiation prod :: (check_all, check_all) check_all
261 "check_all f = check_all (%(x, t1). check_all (%(y, t2). f ((x, y),
262 %u. let T1 = (Typerep.typerep (TYPE('a)));
263 T2 = (Typerep.typerep (TYPE('b)))
264 in Code_Evaluation.App (Code_Evaluation.App (
265 Code_Evaluation.Const (STR ''Product_Type.Pair'')
266 (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Product_Type.prod'') [T1, T2]]]))
269 definition enum_term_of_prod :: "('a * 'b) itself => unit => term list"
271 "enum_term_of_prod = (%_ _. map (%(x, y).
272 let T1 = (Typerep.typerep (TYPE('a)));
273 T2 = (Typerep.typerep (TYPE('b)))
274 in Code_Evaluation.App (Code_Evaluation.App (
275 Code_Evaluation.Const (STR ''Product_Type.Pair'')
276 (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Product_Type.prod'') [T1, T2]]])) x) y)
277 (Enum.product (enum_term_of (TYPE('a)) ()) (enum_term_of (TYPE('b)) ()))) "
284 instantiation sum :: (check_all, check_all) check_all
288 "check_all f = (case check_all (%(a, t). f (Inl a, %_.
289 let T1 = (Typerep.typerep (TYPE('a)));
290 T2 = (Typerep.typerep (TYPE('b)))
291 in Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inl'')
292 (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])) (t ()))) of Some x' => Some x'
293 | None => check_all (%(b, t). f (Inr b, %_. let
294 T1 = (Typerep.typerep (TYPE('a)));
295 T2 = (Typerep.typerep (TYPE('b)))
296 in Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inr'')
297 (Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])) (t ()))))"
299 definition enum_term_of_sum :: "('a + 'b) itself => unit => term list"
301 "enum_term_of_sum = (%_ _.
303 T1 = (Typerep.typerep (TYPE('a)));
304 T2 = (Typerep.typerep (TYPE('b)))
306 map (Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inl'')
307 (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])))
308 (enum_term_of (TYPE('a)) ()) @
309 map (Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inr'')
310 (Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])))
311 (enum_term_of (TYPE('b)) ()))"
317 instantiation nibble :: check_all
322 f (Code_Evaluation.valtermify Nibble0) orelse
323 f (Code_Evaluation.valtermify Nibble1) orelse
324 f (Code_Evaluation.valtermify Nibble2) orelse
325 f (Code_Evaluation.valtermify Nibble3) orelse
326 f (Code_Evaluation.valtermify Nibble4) orelse
327 f (Code_Evaluation.valtermify Nibble5) orelse
328 f (Code_Evaluation.valtermify Nibble6) orelse
329 f (Code_Evaluation.valtermify Nibble7) orelse
330 f (Code_Evaluation.valtermify Nibble8) orelse
331 f (Code_Evaluation.valtermify Nibble9) orelse
332 f (Code_Evaluation.valtermify NibbleA) orelse
333 f (Code_Evaluation.valtermify NibbleB) orelse
334 f (Code_Evaluation.valtermify NibbleC) orelse
335 f (Code_Evaluation.valtermify NibbleD) orelse
336 f (Code_Evaluation.valtermify NibbleE) orelse
337 f (Code_Evaluation.valtermify NibbleF)"
339 definition enum_term_of_nibble :: "nibble itself => unit => term list"
341 "enum_term_of_nibble = (%_ _. map Code_Evaluation.term_of (Enum.enum :: nibble list))"
348 instantiation char :: check_all
352 "check_all f = check_all (%(x, t1). check_all (%(y, t2). f (Char x y, %_. Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.term_of Char) (t1 ())) (t2 ()))))"
354 definition enum_term_of_char :: "char itself => unit => term list"
356 "enum_term_of_char = (%_ _. map Code_Evaluation.term_of (Enum.enum :: char list))"
363 instantiation option :: (check_all) check_all
367 "check_all f = f (Code_Evaluation.valtermify (None :: 'a option)) orelse check_all (%(x, t). f (Some x, %_. Code_Evaluation.App
368 (Code_Evaluation.Const (STR ''Option.option.Some'')
369 (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a), Typerep.Typerep (STR ''Option.option'') [Typerep.typerep TYPE('a)]])) (t ())))"
371 definition enum_term_of_option :: "'a option itself => unit => term list"
373 "enum_term_of_option = (% _ _. (Code_Evaluation.term_of (None :: 'a option)) # (map (Code_Evaluation.App (Code_Evaluation.Const (STR ''Option.option.Some'')
374 (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a), Typerep.Typerep (STR ''Option.option'') [Typerep.typerep TYPE('a)]]))) (enum_term_of (TYPE('a)) ())))"
381 instantiation Enum.finite_1 :: check_all
385 "check_all f = f (Code_Evaluation.valtermify Enum.finite_1.a\<^isub>1)"
387 definition enum_term_of_finite_1 :: "Enum.finite_1 itself => unit => term list"
389 "enum_term_of_finite_1 = (%_ _. [Code_Evaluation.term_of Enum.finite_1.a\<^isub>1])"
395 instantiation Enum.finite_2 :: check_all
399 "check_all f = (case f (Code_Evaluation.valtermify Enum.finite_2.a\<^isub>1) of Some x' \<Rightarrow> Some x' | None \<Rightarrow> f (Code_Evaluation.valtermify Enum.finite_2.a\<^isub>2))"
401 definition enum_term_of_finite_2 :: "Enum.finite_2 itself => unit => term list"
403 "enum_term_of_finite_2 = (%_ _. map Code_Evaluation.term_of (Enum.enum :: Enum.finite_2 list))"
409 instantiation Enum.finite_3 :: check_all
413 "check_all f = (case f (Code_Evaluation.valtermify Enum.finite_3.a\<^isub>1) of Some x' \<Rightarrow> Some x' | None \<Rightarrow> (case f (Code_Evaluation.valtermify Enum.finite_3.a\<^isub>2) of Some x' \<Rightarrow> Some x' | None \<Rightarrow> f (Code_Evaluation.valtermify Enum.finite_3.a\<^isub>3)))"
415 definition enum_term_of_finite_3 :: "Enum.finite_3 itself => unit => term list"
417 "enum_term_of_finite_3 = (%_ _. map Code_Evaluation.term_of (Enum.enum :: Enum.finite_3 list))"
423 subsection {* Bounded universal quantifiers *}
425 class bounded_forall =
426 fixes bounded_forall :: "('a \<Rightarrow> bool) \<Rightarrow> code_numeral \<Rightarrow> bool"
428 subsection {* Fast exhaustive combinators *}
431 class fast_exhaustive = term_of +
432 fixes fast_exhaustive :: "('a \<Rightarrow> unit) \<Rightarrow> code_numeral \<Rightarrow> unit"
434 consts throw_Counterexample :: "term list => unit"
435 consts catch_Counterexample :: "unit => term list option"
437 code_const throw_Counterexample
438 (Quickcheck "raise (Exhaustive'_Generators.Counterexample _)")
439 code_const catch_Counterexample
440 (Quickcheck "(((_); NONE) handle Exhaustive'_Generators.Counterexample ts => SOME ts)")
442 subsection {* Defining combinators for any first-order data type *}
444 definition catch_match :: "term list option => term list option => term list option"
446 [code del]: "catch_match t1 t2 = (SOME t. t = t1 \<or> t = t2)"
448 code_const catch_match
449 (Quickcheck "(_) handle Match => _")
451 use "Tools/Quickcheck/exhaustive_generators.ML"
453 setup {* Exhaustive_Generators.setup *}
455 declare [[quickcheck_tester = exhaustive]]
457 hide_fact orelse_def catch_match_def
458 no_notation orelse (infixr "orelse" 55)
459 hide_const (open) orelse catch_match mk_map_term check_all_n_lists