src/HOL/Quickcheck_Exhaustive.thy
author bulwahn
Sun Feb 05 08:24:38 2012 +0100 (2012-02-05)
changeset 46417 1a68fcb80b62
parent 46311 56fae81902ce
child 46950 d0181abdbdac
permissions -rw-r--r--
beautifying definitions of check_all and adding instance for finite_4
     1 (* Author: Lukas Bulwahn, TU Muenchen *)
     2 
     3 header {* A simple counterexample generator performing exhaustive testing *}
     4 
     5 theory Quickcheck_Exhaustive
     6 imports Quickcheck
     7 uses
     8   ("Tools/Quickcheck/exhaustive_generators.ML")
     9   ("Tools/Quickcheck/abstract_generators.ML")
    10 begin
    11 
    12 subsection {* basic operations for exhaustive generators *}
    13 
    14 definition orelse :: "'a option => 'a option => 'a option" (infixr "orelse" 55)
    15 where
    16   [code_unfold]: "x orelse y = (case x of Some x' => Some x' | None => y)"
    17 
    18 subsection {* exhaustive generator type classes *}
    19 
    20 class exhaustive = term_of +
    21   fixes exhaustive :: "('a \<Rightarrow> (bool * term list) option) \<Rightarrow> code_numeral \<Rightarrow> (bool * term list) option"
    22   
    23 class full_exhaustive = term_of +
    24   fixes full_exhaustive :: "('a * (unit => term) \<Rightarrow> (bool * term list) option) \<Rightarrow> code_numeral \<Rightarrow> (bool * term list) option"
    25 
    26 instantiation code_numeral :: full_exhaustive
    27 begin
    28 
    29 function full_exhaustive_code_numeral' :: "(code_numeral * (unit => term) => (bool * term list) option) => code_numeral => code_numeral => (bool * term list) option"
    30   where "full_exhaustive_code_numeral' f d i =
    31     (if d < i then None
    32     else (f (i, %_. Code_Evaluation.term_of i)) orelse (full_exhaustive_code_numeral' f d (i + 1)))"
    33 by pat_completeness auto
    34 
    35 termination
    36   by (relation "measure (%(_, d, i). Code_Numeral.nat_of (d + 1 - i))") auto
    37 
    38 definition "full_exhaustive f d = full_exhaustive_code_numeral' f d 0"
    39 
    40 instance ..
    41 
    42 end
    43 
    44 instantiation code_numeral :: exhaustive
    45 begin
    46 
    47 function exhaustive_code_numeral' :: "(code_numeral => (bool * term list) option) => code_numeral => code_numeral => (bool * term list) option"
    48   where "exhaustive_code_numeral' f d i =
    49     (if d < i then None
    50     else (f i orelse exhaustive_code_numeral' f d (i + 1)))"
    51 by pat_completeness auto
    52 
    53 termination
    54   by (relation "measure (%(_, d, i). Code_Numeral.nat_of (d + 1 - i))") auto
    55 
    56 definition "exhaustive f d = exhaustive_code_numeral' f d 0"
    57 
    58 instance ..
    59 
    60 end
    61 
    62 instantiation nat :: exhaustive
    63 begin
    64 
    65 definition "exhaustive f d = exhaustive (%x. f (Code_Numeral.nat_of x)) d"
    66 
    67 instance ..
    68 
    69 end
    70 
    71 instantiation nat :: full_exhaustive
    72 begin
    73 
    74 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"
    75 
    76 instance ..
    77 
    78 end
    79 
    80 instantiation int :: exhaustive
    81 begin
    82 
    83 function exhaustive' :: "(int => (bool * term list) option) => int => int => (bool * term list) option"
    84   where "exhaustive' f d i = (if d < i then None else (f i orelse exhaustive' f d (i + 1)))"
    85 by pat_completeness auto
    86 
    87 termination 
    88   by (relation "measure (%(_, d, i). nat (d + 1 - i))") auto
    89 
    90 definition "exhaustive f d = exhaustive' f (Code_Numeral.int_of d) (- (Code_Numeral.int_of d))"
    91 
    92 instance ..
    93 
    94 end
    95 
    96 instantiation int :: full_exhaustive
    97 begin
    98 
    99 function full_exhaustive' :: "(int * (unit => term) => (bool * term list) option) => int => int => (bool * term list) option"
   100   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)))"
   101 by pat_completeness auto
   102 
   103 termination 
   104   by (relation "measure (%(_, d, i). nat (d + 1 - i))") auto
   105 
   106 definition "full_exhaustive f d = full_exhaustive' f (Code_Numeral.int_of d) (- (Code_Numeral.int_of d))"
   107 
   108 instance ..
   109 
   110 end
   111 
   112 instantiation prod :: (exhaustive, exhaustive) exhaustive
   113 begin
   114 
   115 definition
   116   "exhaustive f d = exhaustive (%x. exhaustive (%y. f ((x, y))) d) d"
   117 
   118 instance ..
   119 
   120 end
   121 
   122 definition (in term_syntax) [code_unfold]: "valtermify_pair x y = Code_Evaluation.valtermify (Pair :: 'a :: typerep => 'b :: typerep => 'a * 'b) {\<cdot>} x {\<cdot>} y"
   123 
   124 instantiation prod :: (full_exhaustive, full_exhaustive) full_exhaustive
   125 begin
   126 
   127 definition
   128   "full_exhaustive f d = full_exhaustive (%x. full_exhaustive (%y. f (valtermify_pair x y)) d) d"
   129 
   130 instance ..
   131 
   132 end
   133 
   134 instantiation set :: (exhaustive) exhaustive
   135 begin
   136 
   137 fun exhaustive_set
   138 where
   139   "exhaustive_set f i = (if i = 0 then None else (f {} orelse exhaustive_set (%A. f A orelse exhaustive (%x. if x \<in> A then None else f (insert x A)) (i - 1)) (i - 1)))"
   140 
   141 instance ..
   142 
   143 end
   144 
   145 instantiation set :: (full_exhaustive) full_exhaustive
   146 begin
   147 
   148 fun full_exhaustive_set 
   149 where
   150   "full_exhaustive_set f i = (if i = 0 then None else (f valterm_emptyset orelse full_exhaustive_set (%A. f A orelse Quickcheck_Exhaustive.full_exhaustive (%x. if fst x \<in> fst A then None else f (valtermify_insert x A)) (i - 1)) (i - 1)))"
   151 
   152 instance ..
   153 
   154 end
   155 
   156 instantiation "fun" :: ("{equal, exhaustive}", exhaustive) exhaustive
   157 begin
   158 
   159 fun exhaustive_fun' :: "(('a => 'b) => (bool * term list) option) => code_numeral => code_numeral => (bool * term list) option"
   160 where
   161   "exhaustive_fun' f i d = (exhaustive (%b. f (%_. b)) d)
   162    orelse (if i > 1 then
   163      exhaustive_fun' (%g. exhaustive (%a. exhaustive (%b.
   164        f (g(a := b))) d) d) (i - 1) d else None)"
   165 
   166 definition exhaustive_fun :: "(('a => 'b) => (bool * term list) option) => code_numeral => (bool * term list) option"
   167 where
   168   "exhaustive_fun f d = exhaustive_fun' f d d" 
   169 
   170 instance ..
   171 
   172 end
   173 
   174 definition [code_unfold]: "valtermify_absdummy = (%(v, t). (%_::'a. v, %u::unit. Code_Evaluation.Abs (STR ''x'') (Typerep.typerep TYPE('a::typerep)) (t ())))"
   175 
   176 definition (in term_syntax) [code_unfold]: "valtermify_fun_upd g a b = Code_Evaluation.valtermify (fun_upd :: ('a :: typerep => 'b :: typerep) => 'a => 'b => 'a => 'b) {\<cdot>} g {\<cdot>} a {\<cdot>} b"
   177 
   178 instantiation "fun" :: ("{equal, full_exhaustive}", full_exhaustive) full_exhaustive
   179 begin
   180 
   181 fun full_exhaustive_fun' :: "(('a => 'b) * (unit => term) => (bool * term list) option) => code_numeral => code_numeral => (bool * term list) option"
   182 where
   183   "full_exhaustive_fun' f i d = (full_exhaustive (%v. f (valtermify_absdummy v)) d)
   184    orelse (if i > 1 then
   185      full_exhaustive_fun' (%g. full_exhaustive (%a. full_exhaustive (%b.
   186        f (valtermify_fun_upd g a b)) d) d) (i - 1) d else None)"
   187 
   188 definition full_exhaustive_fun :: "(('a => 'b) * (unit => term) => (bool * term list) option) => code_numeral => (bool * term list) option"
   189 where
   190   "full_exhaustive_fun f d = full_exhaustive_fun' f d d" 
   191 
   192 instance ..
   193 
   194 end
   195 
   196 subsubsection {* A smarter enumeration scheme for functions over finite datatypes *}
   197 
   198 class check_all = enum + term_of +
   199   fixes check_all :: "('a * (unit \<Rightarrow> term) \<Rightarrow> (bool * term list) option) \<Rightarrow> (bool * term list) option"
   200   fixes enum_term_of :: "'a itself \<Rightarrow> unit \<Rightarrow> term list"
   201   
   202 fun check_all_n_lists :: "(('a :: check_all) list * (unit \<Rightarrow> term list) \<Rightarrow> (bool * term list) option) \<Rightarrow> code_numeral \<Rightarrow> (bool * term list) option"
   203 where
   204   "check_all_n_lists f n =
   205      (if n = 0 then f ([], (%_. [])) else check_all (%(x, xt). check_all_n_lists (%(xs, xst). f ((x # xs), (%_. (xt () # xst ())))) (n - 1)))"
   206 
   207 definition (in term_syntax) [code_unfold]: "termify_fun_upd g a b = (Code_Evaluation.termify (fun_upd :: ('a :: typerep => 'b :: typerep) => 'a => 'b => 'a => 'b) <\<cdot>> g <\<cdot>> a <\<cdot>> b)"
   208 
   209 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"
   210 where
   211   "mk_map_term T1 T2 domm rng =
   212      (%_. let T1 = T1 ();
   213               T2 = T2 ();
   214               update_term = (%g (a, b).
   215                 Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.App
   216                  (Code_Evaluation.Const (STR ''Fun.fun_upd'')
   217                    (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''fun'') [T1, T2],
   218                       Typerep.Typerep (STR ''fun'') [T1,
   219                         Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''fun'') [T1, T2]]]]))
   220                         g) a) b)
   221           in
   222              List.foldl update_term (Code_Evaluation.Abs (STR ''x'') T1 (Code_Evaluation.Const (STR ''HOL.undefined'') T2)) (zip (domm ()) (rng ())))"
   223 
   224 instantiation "fun" :: ("{equal, check_all}", check_all) check_all
   225 begin
   226 
   227 definition
   228   "check_all f =
   229     (let
   230       mk_term = mk_map_term (%_. Typerep.typerep (TYPE('a))) (%_. Typerep.typerep (TYPE('b))) (enum_term_of (TYPE('a)));
   231       enum = (Enum.enum :: 'a list)
   232     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)))"
   233 
   234 definition enum_term_of_fun :: "('a => 'b) itself => unit => term list"
   235 where
   236   "enum_term_of_fun = (%_ _. let
   237     enum_term_of_a = enum_term_of (TYPE('a));
   238     mk_term = mk_map_term (%_. Typerep.typerep (TYPE('a))) (%_. Typerep.typerep (TYPE('b))) enum_term_of_a
   239   in map (%ys. mk_term (%_. ys) ()) (Enum.n_lists (length (enum_term_of_a ())) (enum_term_of (TYPE('b)) ())))"
   240  
   241 instance ..
   242 
   243 end
   244 
   245 fun (in term_syntax) check_all_subsets :: "(('a :: typerep) set * (unit => term) => (bool * term list) option) => ('a * (unit => term)) list => (bool * term list) option"
   246 where
   247   "check_all_subsets f [] = f valterm_emptyset"
   248 | "check_all_subsets f (x # xs) = check_all_subsets (%s. case f s of Some ts => Some ts | None => f (valtermify_insert x s)) xs"
   249 
   250 
   251 definition (in term_syntax) [code_unfold]: "term_emptyset = Code_Evaluation.termify ({} :: ('a :: typerep) set)"
   252 definition (in term_syntax) [code_unfold]: "termify_insert x s = Code_Evaluation.termify (insert :: ('a::typerep) => 'a set => 'a set)  <\<cdot>> x <\<cdot>> s"
   253 
   254 definition (in term_syntax) setify :: "('a::typerep) itself => term list => term"
   255 where
   256   "setify T ts = foldr (termify_insert T) ts (term_emptyset T)" 
   257 
   258 instantiation set :: (check_all) check_all
   259 begin
   260 
   261 definition
   262   "check_all_set f =
   263      check_all_subsets f (zip (Enum.enum :: 'a list) (map (%a. %u :: unit. a) (Quickcheck_Exhaustive.enum_term_of (TYPE ('a)) ())))"
   264 
   265 definition enum_term_of_set :: "'a set itself => unit => term list"
   266 where
   267   "enum_term_of_set _ _ = map (setify (TYPE('a))) (sublists (Quickcheck_Exhaustive.enum_term_of (TYPE('a)) ()))"
   268 
   269 instance ..
   270 
   271 end
   272 
   273 instantiation unit :: check_all
   274 begin
   275 
   276 definition
   277   "check_all f = f (Code_Evaluation.valtermify ())"
   278 
   279 definition enum_term_of_unit :: "unit itself => unit => term list"
   280 where
   281   "enum_term_of_unit = (%_ _. [Code_Evaluation.term_of ()])"
   282 
   283 instance ..
   284 
   285 end
   286 
   287 
   288 instantiation bool :: check_all
   289 begin
   290 
   291 definition
   292   "check_all f = (case f (Code_Evaluation.valtermify False) of Some x' \<Rightarrow> Some x' | None \<Rightarrow> f (Code_Evaluation.valtermify True))"
   293 
   294 definition enum_term_of_bool :: "bool itself => unit => term list"
   295 where
   296   "enum_term_of_bool = (%_ _. map Code_Evaluation.term_of (Enum.enum :: bool list))"
   297 
   298 instance ..
   299 
   300 end
   301 
   302 definition (in term_syntax) [code_unfold]: "termify_pair x y = Code_Evaluation.termify (Pair :: 'a :: typerep => 'b :: typerep => 'a * 'b) <\<cdot>> x <\<cdot>> y"
   303 
   304 instantiation prod :: (check_all, check_all) check_all
   305 begin
   306 
   307 definition
   308   "check_all f = check_all (%x. check_all (%y. f (valtermify_pair x y)))"
   309 
   310 definition enum_term_of_prod :: "('a * 'b) itself => unit => term list"
   311 where
   312   "enum_term_of_prod = (%_ _. map (%(x, y). termify_pair TYPE('a) TYPE('b) x y)
   313      (Enum.product (enum_term_of (TYPE('a)) ()) (enum_term_of (TYPE('b)) ())))"
   314 
   315 instance ..
   316 
   317 end
   318 
   319 definition (in term_syntax) [code_unfold]: "valtermify_Inl x = Code_Evaluation.valtermify (Inl :: 'a :: typerep => 'a + 'b :: typerep) {\<cdot>} x"
   320 definition (in term_syntax) [code_unfold]: "valtermify_Inr x = Code_Evaluation.valtermify (Inr :: 'b :: typerep => 'a ::typerep + 'b) {\<cdot>} x"
   321 
   322 instantiation sum :: (check_all, check_all) check_all
   323 begin
   324 
   325 definition
   326   "check_all f = check_all (%a. f (valtermify_Inl a)) orelse check_all (%b. f (valtermify_Inr b))"
   327 
   328 definition enum_term_of_sum :: "('a + 'b) itself => unit => term list"
   329 where
   330   "enum_term_of_sum = (%_ _.
   331      let
   332        T1 = (Typerep.typerep (TYPE('a)));
   333        T2 = (Typerep.typerep (TYPE('b)))
   334      in
   335        map (Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inl'') 
   336              (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])))
   337              (enum_term_of (TYPE('a)) ()) @
   338        map (Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inr'') 
   339              (Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])))
   340              (enum_term_of (TYPE('b)) ()))"
   341 
   342 instance ..
   343 
   344 end
   345 
   346 instantiation nibble :: check_all
   347 begin
   348 
   349 definition
   350   "check_all f =
   351     f (Code_Evaluation.valtermify Nibble0) orelse
   352     f (Code_Evaluation.valtermify Nibble1) orelse
   353     f (Code_Evaluation.valtermify Nibble2) orelse
   354     f (Code_Evaluation.valtermify Nibble3) orelse
   355     f (Code_Evaluation.valtermify Nibble4) orelse
   356     f (Code_Evaluation.valtermify Nibble5) orelse
   357     f (Code_Evaluation.valtermify Nibble6) orelse
   358     f (Code_Evaluation.valtermify Nibble7) orelse
   359     f (Code_Evaluation.valtermify Nibble8) orelse
   360     f (Code_Evaluation.valtermify Nibble9) orelse
   361     f (Code_Evaluation.valtermify NibbleA) orelse
   362     f (Code_Evaluation.valtermify NibbleB) orelse
   363     f (Code_Evaluation.valtermify NibbleC) orelse
   364     f (Code_Evaluation.valtermify NibbleD) orelse
   365     f (Code_Evaluation.valtermify NibbleE) orelse
   366     f (Code_Evaluation.valtermify NibbleF)"
   367 
   368 definition enum_term_of_nibble :: "nibble itself => unit => term list"
   369 where
   370   "enum_term_of_nibble = (%_ _. map Code_Evaluation.term_of (Enum.enum :: nibble list))"
   371 
   372 instance ..
   373 
   374 end
   375 
   376 
   377 instantiation char :: check_all
   378 begin
   379 
   380 definition
   381   "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 ()))))"
   382 
   383 definition enum_term_of_char :: "char itself => unit => term list"
   384 where
   385   "enum_term_of_char = (%_ _. map Code_Evaluation.term_of (Enum.enum :: char list))"
   386 
   387 instance ..
   388 
   389 end
   390 
   391 
   392 instantiation option :: (check_all) check_all
   393 begin
   394 
   395 definition
   396   "check_all f = f (Code_Evaluation.valtermify (None :: 'a option)) orelse check_all (%(x, t). f (Some x, %_. Code_Evaluation.App
   397     (Code_Evaluation.Const (STR ''Option.option.Some'')
   398       (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a),  Typerep.Typerep (STR ''Option.option'') [Typerep.typerep TYPE('a)]])) (t ())))"
   399 
   400 definition enum_term_of_option :: "'a option itself => unit => term list"
   401 where
   402   "enum_term_of_option = (% _ _. (Code_Evaluation.term_of (None :: 'a option)) # (map (Code_Evaluation.App (Code_Evaluation.Const (STR ''Option.option.Some'')
   403       (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a),  Typerep.Typerep (STR ''Option.option'') [Typerep.typerep TYPE('a)]]))) (enum_term_of (TYPE('a)) ())))"
   404 
   405 instance ..
   406 
   407 end
   408 
   409 
   410 instantiation Enum.finite_1 :: check_all
   411 begin
   412 
   413 definition
   414   "check_all f = f (Code_Evaluation.valtermify Enum.finite_1.a\<^isub>1)"
   415 
   416 definition enum_term_of_finite_1 :: "Enum.finite_1 itself => unit => term list"
   417 where
   418   "enum_term_of_finite_1 = (%_ _. [Code_Evaluation.term_of Enum.finite_1.a\<^isub>1])"
   419 
   420 instance ..
   421 
   422 end
   423 
   424 instantiation Enum.finite_2 :: check_all
   425 begin
   426 
   427 definition
   428   "check_all f = (f (Code_Evaluation.valtermify Enum.finite_2.a\<^isub>1)
   429     orelse f (Code_Evaluation.valtermify Enum.finite_2.a\<^isub>2))"
   430 
   431 definition enum_term_of_finite_2 :: "Enum.finite_2 itself => unit => term list"
   432 where
   433   "enum_term_of_finite_2 = (%_ _. map Code_Evaluation.term_of (Enum.enum :: Enum.finite_2 list))"
   434 
   435 instance ..
   436 
   437 end
   438 
   439 instantiation Enum.finite_3 :: check_all
   440 begin
   441 
   442 definition
   443   "check_all f = (f (Code_Evaluation.valtermify Enum.finite_3.a\<^isub>1)
   444     orelse f (Code_Evaluation.valtermify Enum.finite_3.a\<^isub>2)
   445     orelse f (Code_Evaluation.valtermify Enum.finite_3.a\<^isub>3))"
   446 
   447 definition enum_term_of_finite_3 :: "Enum.finite_3 itself => unit => term list"
   448 where
   449   "enum_term_of_finite_3 = (%_ _. map Code_Evaluation.term_of (Enum.enum :: Enum.finite_3 list))"
   450 
   451 instance ..
   452 
   453 end
   454 
   455 instantiation Enum.finite_4 :: check_all
   456 begin
   457 
   458 definition
   459   "check_all f = (f (Code_Evaluation.valtermify Enum.finite_4.a\<^isub>1)
   460     orelse f (Code_Evaluation.valtermify Enum.finite_4.a\<^isub>2)
   461     orelse f (Code_Evaluation.valtermify Enum.finite_4.a\<^isub>3)
   462     orelse f (Code_Evaluation.valtermify Enum.finite_4.a\<^isub>4))"
   463 
   464 definition enum_term_of_finite_4 :: "Enum.finite_4 itself => unit => term list"
   465 where
   466   "enum_term_of_finite_4 = (%_ _. map Code_Evaluation.term_of (Enum.enum :: Enum.finite_4 list))"
   467 
   468 instance ..
   469 
   470 end
   471 
   472 subsection {* Bounded universal quantifiers *}
   473 
   474 class bounded_forall =
   475   fixes bounded_forall :: "('a \<Rightarrow> bool) \<Rightarrow> code_numeral \<Rightarrow> bool"
   476 
   477 subsection {* Fast exhaustive combinators *}
   478 
   479 class fast_exhaustive = term_of +
   480   fixes fast_exhaustive :: "('a \<Rightarrow> unit) \<Rightarrow> code_numeral \<Rightarrow> unit"
   481 
   482 axiomatization throw_Counterexample :: "term list => unit"
   483 axiomatization catch_Counterexample :: "unit => term list option"
   484 
   485 code_const throw_Counterexample
   486   (Quickcheck "raise (Exhaustive'_Generators.Counterexample _)")
   487 code_const catch_Counterexample
   488   (Quickcheck "(((_); NONE) handle Exhaustive'_Generators.Counterexample ts => SOME ts)")
   489 
   490 subsection {* Continuation passing style functions as plus monad *}
   491   
   492 type_synonym 'a cps = "('a => term list option) => term list option"
   493 
   494 definition cps_empty :: "'a cps"
   495 where
   496   "cps_empty = (%cont. None)"
   497 
   498 definition cps_single :: "'a => 'a cps"
   499 where
   500   "cps_single v = (%cont. cont v)"
   501 
   502 definition cps_bind :: "'a cps => ('a => 'b cps) => 'b cps" 
   503 where
   504   "cps_bind m f = (%cont. m (%a. (f a) cont))"
   505 
   506 definition cps_plus :: "'a cps => 'a cps => 'a cps"
   507 where
   508   "cps_plus a b = (%c. case a c of None => b c | Some x => Some x)"
   509 
   510 definition cps_if :: "bool => unit cps"
   511 where
   512   "cps_if b = (if b then cps_single () else cps_empty)"
   513 
   514 definition cps_not :: "unit cps => unit cps"
   515 where
   516   "cps_not n = (%c. case n (%u. Some []) of None => c () | Some _ => None)"
   517 
   518 type_synonym 'a pos_bound_cps = "('a => (bool * term list) option) => code_numeral => (bool * term list) option"
   519 
   520 definition pos_bound_cps_empty :: "'a pos_bound_cps"
   521 where
   522   "pos_bound_cps_empty = (%cont i. None)"
   523 
   524 definition pos_bound_cps_single :: "'a => 'a pos_bound_cps"
   525 where
   526   "pos_bound_cps_single v = (%cont i. cont v)"
   527 
   528 definition pos_bound_cps_bind :: "'a pos_bound_cps => ('a => 'b pos_bound_cps) => 'b pos_bound_cps" 
   529 where
   530   "pos_bound_cps_bind m f = (%cont i. if i = 0 then None else (m (%a. (f a) cont i) (i - 1)))"
   531 
   532 definition pos_bound_cps_plus :: "'a pos_bound_cps => 'a pos_bound_cps => 'a pos_bound_cps"
   533 where
   534   "pos_bound_cps_plus a b = (%c i. case a c i of None => b c i | Some x => Some x)"
   535 
   536 definition pos_bound_cps_if :: "bool => unit pos_bound_cps"
   537 where
   538   "pos_bound_cps_if b = (if b then pos_bound_cps_single () else pos_bound_cps_empty)"
   539 
   540 datatype 'a unknown = Unknown | Known 'a
   541 datatype 'a three_valued = Unknown_value | Value 'a | No_value
   542 
   543 type_synonym 'a neg_bound_cps = "('a unknown => term list three_valued) => code_numeral => term list three_valued"
   544 
   545 definition neg_bound_cps_empty :: "'a neg_bound_cps"
   546 where
   547   "neg_bound_cps_empty = (%cont i. No_value)"
   548 
   549 definition neg_bound_cps_single :: "'a => 'a neg_bound_cps"
   550 where
   551   "neg_bound_cps_single v = (%cont i. cont (Known v))"
   552 
   553 definition neg_bound_cps_bind :: "'a neg_bound_cps => ('a => 'b neg_bound_cps) => 'b neg_bound_cps" 
   554 where
   555   "neg_bound_cps_bind m f = (%cont i. if i = 0 then cont Unknown else m (%a. case a of Unknown => cont Unknown | Known a' => f a' cont i) (i - 1))"
   556 
   557 definition neg_bound_cps_plus :: "'a neg_bound_cps => 'a neg_bound_cps => 'a neg_bound_cps"
   558 where
   559   "neg_bound_cps_plus a b = (%c i. case a c i of No_value => b c i | Value x => Value x | Unknown_value => (case b c i of No_value => Unknown_value | Value x => Value x | Unknown_value => Unknown_value))"
   560 
   561 definition neg_bound_cps_if :: "bool => unit neg_bound_cps"
   562 where
   563   "neg_bound_cps_if b = (if b then neg_bound_cps_single () else neg_bound_cps_empty)"
   564 
   565 definition neg_bound_cps_not :: "unit pos_bound_cps => unit neg_bound_cps"
   566 where
   567   "neg_bound_cps_not n = (%c i. case n (%u. Some (True, [])) i of None => c (Known ()) | Some _ => No_value)"
   568 
   569 definition pos_bound_cps_not :: "unit neg_bound_cps => unit pos_bound_cps"
   570 where
   571   "pos_bound_cps_not n = (%c i. case n (%u. Value []) i of No_value => c () | Value _ => None | Unknown_value => None)"
   572 
   573 subsection {* Defining generators for any first-order data type *}
   574 
   575 axiomatization unknown :: 'a
   576 
   577 notation (output) unknown  ("?")
   578  
   579 use "Tools/Quickcheck/exhaustive_generators.ML"
   580 
   581 setup {* Exhaustive_Generators.setup *}
   582 
   583 declare [[quickcheck_batch_tester = exhaustive]]
   584 
   585 subsection {* Defining generators for abstract types *}
   586 
   587 use "Tools/Quickcheck/abstract_generators.ML"
   588 
   589 hide_fact orelse_def
   590 no_notation orelse (infixr "orelse" 55)
   591 
   592 hide_fact
   593   exhaustive'_def
   594   exhaustive_code_numeral'_def
   595 
   596 hide_const valtermify_absdummy valtermify_fun_upd valterm_emptyset valtermify_insert valtermify_pair
   597   valtermify_Inl valtermify_Inr
   598   termify_fun_upd term_emptyset termify_insert termify_pair setify
   599 
   600 hide_const (open)
   601   exhaustive full_exhaustive exhaustive' exhaustive_code_numeral' full_exhaustive_code_numeral'
   602   throw_Counterexample catch_Counterexample
   603   check_all enum_term_of
   604   orelse unknown mk_map_term check_all_n_lists check_all_subsets
   605 
   606 hide_type (open) cps pos_bound_cps neg_bound_cps unknown three_valued
   607 hide_const (open) cps_empty cps_single cps_bind cps_plus cps_if cps_not
   608   pos_bound_cps_empty pos_bound_cps_single pos_bound_cps_bind pos_bound_cps_plus pos_bound_cps_if pos_bound_cps_not
   609   neg_bound_cps_empty neg_bound_cps_single neg_bound_cps_bind neg_bound_cps_plus neg_bound_cps_if neg_bound_cps_not
   610   Unknown Known Unknown_value Value No_value
   611 
   612 end