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