src/HOL/Quickcheck_Exhaustive.thy
author bulwahn
Fri Mar 11 15:21:12 2011 +0100 (2011-03-11)
changeset 41915 fba21941bdc5
parent 41722 src/HOL/Smallcheck.thy@9575694d2da5
child 41916 80060d5f864a
permissions -rw-r--r--
renaming Smallcheck to Quickcheck_Exhaustive
     1 (* Author: Lukas Bulwahn, TU Muenchen *)
     2 
     3 header {* Another simple counterexample generator *}
     4 
     5 theory Smallcheck
     6 imports Quickcheck
     7 uses ("Tools/smallvalue_generators.ML")
     8 begin
     9 
    10 subsection {* basic operations for generators *}
    11 
    12 definition orelse :: "'a option => 'a option => 'a option" (infixr "orelse" 55)
    13 where
    14   [code_unfold]: "x orelse y = (case x of Some x' => Some x' | None => y)"
    15 
    16 subsection {* small value generator type classes *}
    17 
    18 class small = term_of +
    19 fixes small :: "('a \<Rightarrow> term list option) \<Rightarrow> code_numeral \<Rightarrow> term list option"
    20 
    21 instantiation unit :: small
    22 begin
    23 
    24 definition "small f d = f ()"
    25 
    26 instance ..
    27 
    28 end
    29 
    30 instantiation int :: small
    31 begin
    32 
    33 function small' :: "(int => term list option) => int => int => term list option"
    34 where "small' f d i = (if d < i then None else (case f i of Some t => Some t | None => small' f d (i + 1)))"
    35 by pat_completeness auto
    36 
    37 termination 
    38   by (relation "measure (%(_, d, i). nat (d + 1 - i))") auto
    39 
    40 definition "small f d = small' f (Code_Numeral.int_of d) (- (Code_Numeral.int_of d))"
    41 
    42 instance ..
    43 
    44 end
    45 
    46 instantiation prod :: (small, small) small
    47 begin
    48 
    49 definition
    50   "small f d = small (%x. small (%y. f (x, y)) d) d"
    51 
    52 instance ..
    53 
    54 end
    55 
    56 subsection {* full small value generator type classes *}
    57 
    58 class full_small = term_of +
    59 fixes full_small :: "('a * (unit => term) \<Rightarrow> term list option) \<Rightarrow> code_numeral \<Rightarrow> term list option"
    60 
    61 instantiation unit :: full_small
    62 begin
    63 
    64 definition "full_small f d = f (Code_Evaluation.valtermify ())"
    65 
    66 instance ..
    67 
    68 end
    69 
    70 instantiation code_numeral :: full_small
    71 begin
    72 
    73 function full_small_code_numeral' :: "(code_numeral * (unit => term) => term list option) => code_numeral => code_numeral => term list option"
    74   where "full_small_code_numeral' f d i = (if d < i then None else (case f (i, %_. Code_Evaluation.term_of i) of Some t => Some t | None => full_small_code_numeral' f d (i + 1)))"
    75 by pat_completeness auto
    76 
    77 termination 
    78   by (relation "measure (%(_, d, i). Code_Numeral.nat_of (d + 1 - i))") auto
    79 
    80 definition "full_small f d = full_small_code_numeral' f d 0"
    81 
    82 instance ..
    83 
    84 end
    85 
    86 instantiation nat :: full_small
    87 begin
    88 
    89 definition "full_small f d = full_small (%(x, xt). f (Code_Numeral.nat_of x, %_. Code_Evaluation.term_of (Code_Numeral.nat_of x))) d"
    90 
    91 instance ..
    92 
    93 end
    94 
    95 instantiation int :: full_small
    96 begin
    97 
    98 function full_small' :: "(int * (unit => term) => term list option) => int => int => term list option"
    99   where "full_small' f d i = (if d < i then None else (case f (i, %_. Code_Evaluation.term_of i) of Some t => Some t | None => full_small' f d (i + 1)))"
   100 by pat_completeness auto
   101 
   102 termination 
   103   by (relation "measure (%(_, d, i). nat (d + 1 - i))") auto
   104 
   105 definition "full_small f d = full_small' f (Code_Numeral.int_of d) (- (Code_Numeral.int_of d))"
   106 
   107 instance ..
   108 
   109 end
   110 
   111 instantiation prod :: (full_small, full_small) full_small
   112 begin
   113 
   114 definition
   115   "full_small f d = full_small (%(x, t1). full_small (%(y, t2). f ((x, y),
   116     %u. let T1 = (Typerep.typerep (TYPE('a)));
   117             T2 = (Typerep.typerep (TYPE('b)))
   118     in Code_Evaluation.App (Code_Evaluation.App (
   119       Code_Evaluation.Const (STR ''Product_Type.Pair'') 
   120       (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Product_Type.prod'') [T1, T2]]]))
   121       (t1 ())) (t2 ()))) d) d"
   122 
   123 instance ..
   124 
   125 end
   126 
   127 instantiation "fun" :: ("{equal, full_small}", full_small) full_small
   128 begin
   129 
   130 fun full_small_fun' :: "(('a => 'b) * (unit => term) => term list option) => code_numeral => code_numeral => term list option"
   131 where
   132   "full_small_fun' f i d = (if i > 1 then
   133     full_small (%(a, at). full_small (%(b, bt).
   134       full_small_fun' (%(g, gt). f (g(a := b),
   135         (%_. let T1 = (Typerep.typerep (TYPE('a)));
   136                  T2 = (Typerep.typerep (TYPE('b)))
   137              in
   138                Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.App
   139                  (Code_Evaluation.Const (STR ''Fun.fun_upd'')
   140                     (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''fun'') [T1, T2],
   141                        Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''fun'') [T1, T2]]]]))
   142                (gt ())) (at ())) (bt ())))) (i - 1) d) d) d
   143   else (if i > 0 then
   144     full_small (%(b, t). f (%_. b, %_. Code_Evaluation.Abs (STR ''x'') (Typerep.typerep TYPE('a)) (t ()))) d else None))"
   145 
   146 definition full_small_fun :: "(('a => 'b) * (unit => term) => term list option) => code_numeral => term list option"
   147 where
   148   "full_small_fun f d = full_small_fun' f d d" 
   149 
   150 instance ..
   151 
   152 end
   153 
   154 subsubsection {* A smarter enumeration scheme for functions over finite datatypes *}
   155 
   156 
   157 class check_all = enum + term_of +
   158   fixes check_all :: "('a * (unit \<Rightarrow> term) \<Rightarrow> term list option) \<Rightarrow> term list option"
   159   fixes enum_term_of :: "'a itself \<Rightarrow> unit \<Rightarrow> term list"
   160   
   161 fun check_all_n_lists :: "(('a :: check_all) list * (unit \<Rightarrow> term list) \<Rightarrow> term list option) \<Rightarrow> code_numeral \<Rightarrow> term list option"
   162 where
   163   "check_all_n_lists f n =
   164      (if n = 0 then f ([], (%_. [])) else check_all (%(x, xt). check_all_n_lists (%(xs, xst). f ((x # xs), (%_. (xt () # xst ())))) (n - 1)))"
   165 
   166 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"
   167 where
   168   "mk_map_term T1 T2 domm rng =
   169      (%_. let T1 = T1 ();
   170               T2 = T2 ();
   171               update_term = (%g (a, b).
   172                 Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.App
   173                  (Code_Evaluation.Const (STR ''Fun.fun_upd'')
   174                    (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''fun'') [T1, T2],
   175                       Typerep.Typerep (STR ''fun'') [T1,
   176                         Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''fun'') [T1, T2]]]]))
   177                         g) a) b)
   178           in
   179              List.foldl update_term (Code_Evaluation.Abs (STR ''x'') T1 (Code_Evaluation.Const (STR ''HOL.undefined'') T2)) (zip (domm ()) (rng ())))"
   180 
   181 instantiation "fun" :: ("{equal, check_all}", check_all) check_all
   182 begin
   183 
   184 definition
   185   "check_all f =
   186     (let
   187       mk_term = mk_map_term (%_. Typerep.typerep (TYPE('a))) (%_. Typerep.typerep (TYPE('b))) (enum_term_of (TYPE('a)));
   188       enum = (Enum.enum :: 'a list)
   189     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)))"
   190 
   191 definition enum_term_of_fun :: "('a => 'b) itself => unit => term list"
   192 where
   193   "enum_term_of_fun = (%_ _. let
   194     enum_term_of_a = enum_term_of (TYPE('a));
   195     mk_term = mk_map_term (%_. Typerep.typerep (TYPE('a))) (%_. Typerep.typerep (TYPE('b))) enum_term_of_a
   196   in map (%ys. mk_term (%_. ys) ()) (Enum.n_lists (length (enum_term_of_a ())) (enum_term_of (TYPE('b)) ())))"
   197  
   198 instance ..
   199 
   200 end
   201 
   202 
   203 instantiation unit :: check_all
   204 begin
   205 
   206 definition
   207   "check_all f = f (Code_Evaluation.valtermify ())"
   208 
   209 definition enum_term_of_unit :: "unit itself => unit => term list"
   210 where
   211   "enum_term_of_unit = (%_ _. [Code_Evaluation.term_of ()])"
   212 
   213 instance ..
   214 
   215 end
   216 
   217 
   218 instantiation bool :: check_all
   219 begin
   220 
   221 definition
   222   "check_all f = (case f (Code_Evaluation.valtermify False) of Some x' \<Rightarrow> Some x' | None \<Rightarrow> f (Code_Evaluation.valtermify True))"
   223 
   224 definition enum_term_of_bool :: "bool itself => unit => term list"
   225 where
   226   "enum_term_of_bool = (%_ _. map Code_Evaluation.term_of (Enum.enum :: bool list))"
   227 
   228 instance ..
   229 
   230 end
   231 
   232 
   233 instantiation prod :: (check_all, check_all) check_all
   234 begin
   235 
   236 definition
   237   "check_all f = check_all (%(x, t1). check_all (%(y, t2). f ((x, y),
   238     %u. let T1 = (Typerep.typerep (TYPE('a)));
   239             T2 = (Typerep.typerep (TYPE('b)))
   240     in Code_Evaluation.App (Code_Evaluation.App (
   241       Code_Evaluation.Const (STR ''Product_Type.Pair'') 
   242       (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Product_Type.prod'') [T1, T2]]]))
   243       (t1 ())) (t2 ()))))"
   244 
   245 definition enum_term_of_prod :: "('a * 'b) itself => unit => term list"
   246 where
   247   "enum_term_of_prod = (%_ _. map (%(x, y).
   248        let T1 = (Typerep.typerep (TYPE('a)));
   249            T2 = (Typerep.typerep (TYPE('b)))
   250        in Code_Evaluation.App (Code_Evaluation.App (
   251          Code_Evaluation.Const (STR ''Product_Type.Pair'') 
   252            (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Product_Type.prod'') [T1, T2]]])) x) y)
   253      (Enum.product (enum_term_of (TYPE('a)) ()) (enum_term_of (TYPE('b)) ())))  "
   254 
   255 instance ..
   256 
   257 end
   258 
   259 
   260 instantiation sum :: (check_all, check_all) check_all
   261 begin
   262 
   263 definition
   264   "check_all f = (case check_all (%(a, t). f (Inl a, %_. 
   265      let T1 = (Typerep.typerep (TYPE('a)));
   266          T2 = (Typerep.typerep (TYPE('b)))
   267        in Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inl'') 
   268            (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])) (t ()))) of Some x' => Some x'
   269              | None => check_all (%(b, t). f (Inr b, %_. let
   270                  T1 = (Typerep.typerep (TYPE('a)));
   271                  T2 = (Typerep.typerep (TYPE('b)))
   272                in Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inr'') 
   273                  (Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])) (t ()))))"
   274 
   275 definition enum_term_of_sum :: "('a + 'b) itself => unit => term list"
   276 where
   277   "enum_term_of_sum = (%_ _.
   278      let
   279        T1 = (Typerep.typerep (TYPE('a)));
   280        T2 = (Typerep.typerep (TYPE('b)))
   281      in
   282        map (Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inl'') 
   283              (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])))
   284              (enum_term_of (TYPE('a)) ()) @
   285        map (Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inr'') 
   286              (Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])))
   287              (enum_term_of (TYPE('b)) ()))"
   288 
   289 instance ..
   290 
   291 end
   292 
   293 instantiation nibble :: check_all
   294 begin
   295 
   296 definition
   297   "check_all f =
   298     f (Code_Evaluation.valtermify Nibble0) orelse
   299     f (Code_Evaluation.valtermify Nibble1) orelse
   300     f (Code_Evaluation.valtermify Nibble2) orelse
   301     f (Code_Evaluation.valtermify Nibble3) orelse
   302     f (Code_Evaluation.valtermify Nibble4) orelse
   303     f (Code_Evaluation.valtermify Nibble5) orelse
   304     f (Code_Evaluation.valtermify Nibble6) orelse
   305     f (Code_Evaluation.valtermify Nibble7) orelse
   306     f (Code_Evaluation.valtermify Nibble8) orelse
   307     f (Code_Evaluation.valtermify Nibble9) orelse
   308     f (Code_Evaluation.valtermify NibbleA) orelse
   309     f (Code_Evaluation.valtermify NibbleB) orelse
   310     f (Code_Evaluation.valtermify NibbleC) orelse
   311     f (Code_Evaluation.valtermify NibbleD) orelse
   312     f (Code_Evaluation.valtermify NibbleE) orelse
   313     f (Code_Evaluation.valtermify NibbleF)"
   314 
   315 definition enum_term_of_nibble :: "nibble itself => unit => term list"
   316 where
   317   "enum_term_of_nibble = (%_ _. map Code_Evaluation.term_of (Enum.enum :: nibble list))"
   318 
   319 instance ..
   320 
   321 end
   322 
   323 
   324 instantiation char :: check_all
   325 begin
   326 
   327 definition
   328   "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 ()))))"
   329 
   330 definition enum_term_of_char :: "char itself => unit => term list"
   331 where
   332   "enum_term_of_char = (%_ _. map Code_Evaluation.term_of (Enum.enum :: char list))"
   333 
   334 instance ..
   335 
   336 end
   337 
   338 
   339 instantiation option :: (check_all) check_all
   340 begin
   341 
   342 definition
   343   "check_all f = f (Code_Evaluation.valtermify (None :: 'a option)) orelse check_all (%(x, t). f (Some x, %_. Code_Evaluation.App
   344     (Code_Evaluation.Const (STR ''Option.option.Some'')
   345       (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a),  Typerep.Typerep (STR ''Option.option'') [Typerep.typerep TYPE('a)]])) (t ())))"
   346 
   347 definition enum_term_of_option :: "'a option itself => unit => term list"
   348 where
   349   "enum_term_of_option = (% _ _. (Code_Evaluation.term_of (None :: 'a option)) # (map (Code_Evaluation.App (Code_Evaluation.Const (STR ''Option.option.Some'')
   350       (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a),  Typerep.Typerep (STR ''Option.option'') [Typerep.typerep TYPE('a)]]))) (enum_term_of (TYPE('a)) ())))"
   351 
   352 instance ..
   353 
   354 end
   355 
   356 
   357 instantiation Enum.finite_1 :: check_all
   358 begin
   359 
   360 definition
   361   "check_all f = f (Code_Evaluation.valtermify Enum.finite_1.a\<^isub>1)"
   362 
   363 definition enum_term_of_finite_1 :: "Enum.finite_1 itself => unit => term list"
   364 where
   365   "enum_term_of_finite_1 = (%_ _. [Code_Evaluation.term_of Enum.finite_1.a\<^isub>1])"
   366 
   367 instance ..
   368 
   369 end
   370 
   371 instantiation Enum.finite_2 :: check_all
   372 begin
   373 
   374 definition
   375   "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))"
   376 
   377 definition enum_term_of_finite_2 :: "Enum.finite_2 itself => unit => term list"
   378 where
   379   "enum_term_of_finite_2 = (%_ _. map Code_Evaluation.term_of (Enum.enum :: Enum.finite_2 list))"
   380 
   381 instance ..
   382 
   383 end
   384 
   385 instantiation Enum.finite_3 :: check_all
   386 begin
   387 
   388 definition
   389   "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)))"
   390 
   391 definition enum_term_of_finite_3 :: "Enum.finite_3 itself => unit => term list"
   392 where
   393   "enum_term_of_finite_3 = (%_ _. map Code_Evaluation.term_of (Enum.enum :: Enum.finite_3 list))"
   394 
   395 instance ..
   396 
   397 end
   398 
   399 
   400 
   401 subsection {* Defining combinators for any first-order data type *}
   402 
   403 definition catch_match :: "term list option => term list option => term list option"
   404 where
   405   [code del]: "catch_match t1 t2 = (SOME t. t = t1 \<or> t = t2)"
   406 
   407 code_const catch_match 
   408   (SML "(_) handle Match => _")
   409 
   410 use "Tools/smallvalue_generators.ML"
   411 
   412 setup {* Smallvalue_Generators.setup *}
   413 
   414 declare [[quickcheck_tester = exhaustive]]
   415 
   416 hide_fact orelse_def catch_match_def
   417 no_notation orelse (infixr "orelse" 55)
   418 hide_const (open) orelse catch_match mk_map_term check_all_n_lists
   419 
   420 end