src/HOL/Quickcheck_Exhaustive.thy
author bulwahn
Fri Mar 11 15:21:13 2011 +0100 (2011-03-11)
changeset 41918 d2ab869f8b0b
parent 41916 80060d5f864a
child 41920 d4fb7a418152
permissions -rw-r--r--
replacing naming of small by 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/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 = (if i > 1 then
    95     exhaustive (%(a, at). exhaustive (%(b, bt).
    96       exhaustive_fun' (%(g, gt). f (g(a := b),
    97         (%_. let T1 = (Typerep.typerep (TYPE('a)));
    98                  T2 = (Typerep.typerep (TYPE('b)))
    99              in
   100                Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.App
   101                  (Code_Evaluation.Const (STR ''Fun.fun_upd'')
   102                     (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''fun'') [T1, T2],
   103                        Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''fun'') [T1, T2]]]]))
   104                (gt ())) (at ())) (bt ())))) (i - 1) d) d) d
   105   else (if i > 0 then
   106     exhaustive (%(b, t). f (%_. b, %_. Code_Evaluation.Abs (STR ''x'') (Typerep.typerep TYPE('a)) (t ()))) d else None))"
   107 
   108 definition exhaustive_fun :: "(('a => 'b) * (unit => term) => term list option) => code_numeral => term list option"
   109 where
   110   "exhaustive_fun f d = exhaustive_fun' f d d" 
   111 
   112 instance ..
   113 
   114 end
   115 
   116 subsubsection {* A smarter enumeration scheme for functions over finite datatypes *}
   117 
   118 class check_all = enum + term_of +
   119   fixes check_all :: "('a * (unit \<Rightarrow> term) \<Rightarrow> term list option) \<Rightarrow> term list option"
   120   fixes enum_term_of :: "'a itself \<Rightarrow> unit \<Rightarrow> term list"
   121   
   122 fun check_all_n_lists :: "(('a :: check_all) list * (unit \<Rightarrow> term list) \<Rightarrow> term list option) \<Rightarrow> code_numeral \<Rightarrow> term list option"
   123 where
   124   "check_all_n_lists f n =
   125      (if n = 0 then f ([], (%_. [])) else check_all (%(x, xt). check_all_n_lists (%(xs, xst). f ((x # xs), (%_. (xt () # xst ())))) (n - 1)))"
   126 
   127 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"
   128 where
   129   "mk_map_term T1 T2 domm rng =
   130      (%_. let T1 = T1 ();
   131               T2 = T2 ();
   132               update_term = (%g (a, b).
   133                 Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.App
   134                  (Code_Evaluation.Const (STR ''Fun.fun_upd'')
   135                    (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''fun'') [T1, T2],
   136                       Typerep.Typerep (STR ''fun'') [T1,
   137                         Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''fun'') [T1, T2]]]]))
   138                         g) a) b)
   139           in
   140              List.foldl update_term (Code_Evaluation.Abs (STR ''x'') T1 (Code_Evaluation.Const (STR ''HOL.undefined'') T2)) (zip (domm ()) (rng ())))"
   141 
   142 instantiation "fun" :: ("{equal, check_all}", check_all) check_all
   143 begin
   144 
   145 definition
   146   "check_all f =
   147     (let
   148       mk_term = mk_map_term (%_. Typerep.typerep (TYPE('a))) (%_. Typerep.typerep (TYPE('b))) (enum_term_of (TYPE('a)));
   149       enum = (Enum.enum :: 'a list)
   150     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)))"
   151 
   152 definition enum_term_of_fun :: "('a => 'b) itself => unit => term list"
   153 where
   154   "enum_term_of_fun = (%_ _. let
   155     enum_term_of_a = enum_term_of (TYPE('a));
   156     mk_term = mk_map_term (%_. Typerep.typerep (TYPE('a))) (%_. Typerep.typerep (TYPE('b))) enum_term_of_a
   157   in map (%ys. mk_term (%_. ys) ()) (Enum.n_lists (length (enum_term_of_a ())) (enum_term_of (TYPE('b)) ())))"
   158  
   159 instance ..
   160 
   161 end
   162 
   163 
   164 instantiation unit :: check_all
   165 begin
   166 
   167 definition
   168   "check_all f = f (Code_Evaluation.valtermify ())"
   169 
   170 definition enum_term_of_unit :: "unit itself => unit => term list"
   171 where
   172   "enum_term_of_unit = (%_ _. [Code_Evaluation.term_of ()])"
   173 
   174 instance ..
   175 
   176 end
   177 
   178 
   179 instantiation bool :: check_all
   180 begin
   181 
   182 definition
   183   "check_all f = (case f (Code_Evaluation.valtermify False) of Some x' \<Rightarrow> Some x' | None \<Rightarrow> f (Code_Evaluation.valtermify True))"
   184 
   185 definition enum_term_of_bool :: "bool itself => unit => term list"
   186 where
   187   "enum_term_of_bool = (%_ _. map Code_Evaluation.term_of (Enum.enum :: bool list))"
   188 
   189 instance ..
   190 
   191 end
   192 
   193 
   194 instantiation prod :: (check_all, check_all) check_all
   195 begin
   196 
   197 definition
   198   "check_all f = check_all (%(x, t1). check_all (%(y, t2). f ((x, y),
   199     %u. let T1 = (Typerep.typerep (TYPE('a)));
   200             T2 = (Typerep.typerep (TYPE('b)))
   201     in Code_Evaluation.App (Code_Evaluation.App (
   202       Code_Evaluation.Const (STR ''Product_Type.Pair'') 
   203       (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Product_Type.prod'') [T1, T2]]]))
   204       (t1 ())) (t2 ()))))"
   205 
   206 definition enum_term_of_prod :: "('a * 'b) itself => unit => term list"
   207 where
   208   "enum_term_of_prod = (%_ _. map (%(x, y).
   209        let T1 = (Typerep.typerep (TYPE('a)));
   210            T2 = (Typerep.typerep (TYPE('b)))
   211        in Code_Evaluation.App (Code_Evaluation.App (
   212          Code_Evaluation.Const (STR ''Product_Type.Pair'') 
   213            (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Product_Type.prod'') [T1, T2]]])) x) y)
   214      (Enum.product (enum_term_of (TYPE('a)) ()) (enum_term_of (TYPE('b)) ())))  "
   215 
   216 instance ..
   217 
   218 end
   219 
   220 
   221 instantiation sum :: (check_all, check_all) check_all
   222 begin
   223 
   224 definition
   225   "check_all f = (case check_all (%(a, t). f (Inl a, %_. 
   226      let T1 = (Typerep.typerep (TYPE('a)));
   227          T2 = (Typerep.typerep (TYPE('b)))
   228        in Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inl'') 
   229            (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])) (t ()))) of Some x' => Some x'
   230              | None => check_all (%(b, t). f (Inr b, %_. let
   231                  T1 = (Typerep.typerep (TYPE('a)));
   232                  T2 = (Typerep.typerep (TYPE('b)))
   233                in Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inr'') 
   234                  (Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])) (t ()))))"
   235 
   236 definition enum_term_of_sum :: "('a + 'b) itself => unit => term list"
   237 where
   238   "enum_term_of_sum = (%_ _.
   239      let
   240        T1 = (Typerep.typerep (TYPE('a)));
   241        T2 = (Typerep.typerep (TYPE('b)))
   242      in
   243        map (Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inl'') 
   244              (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])))
   245              (enum_term_of (TYPE('a)) ()) @
   246        map (Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inr'') 
   247              (Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])))
   248              (enum_term_of (TYPE('b)) ()))"
   249 
   250 instance ..
   251 
   252 end
   253 
   254 instantiation nibble :: check_all
   255 begin
   256 
   257 definition
   258   "check_all f =
   259     f (Code_Evaluation.valtermify Nibble0) orelse
   260     f (Code_Evaluation.valtermify Nibble1) orelse
   261     f (Code_Evaluation.valtermify Nibble2) orelse
   262     f (Code_Evaluation.valtermify Nibble3) orelse
   263     f (Code_Evaluation.valtermify Nibble4) orelse
   264     f (Code_Evaluation.valtermify Nibble5) orelse
   265     f (Code_Evaluation.valtermify Nibble6) orelse
   266     f (Code_Evaluation.valtermify Nibble7) orelse
   267     f (Code_Evaluation.valtermify Nibble8) orelse
   268     f (Code_Evaluation.valtermify Nibble9) orelse
   269     f (Code_Evaluation.valtermify NibbleA) orelse
   270     f (Code_Evaluation.valtermify NibbleB) orelse
   271     f (Code_Evaluation.valtermify NibbleC) orelse
   272     f (Code_Evaluation.valtermify NibbleD) orelse
   273     f (Code_Evaluation.valtermify NibbleE) orelse
   274     f (Code_Evaluation.valtermify NibbleF)"
   275 
   276 definition enum_term_of_nibble :: "nibble itself => unit => term list"
   277 where
   278   "enum_term_of_nibble = (%_ _. map Code_Evaluation.term_of (Enum.enum :: nibble list))"
   279 
   280 instance ..
   281 
   282 end
   283 
   284 
   285 instantiation char :: check_all
   286 begin
   287 
   288 definition
   289   "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 ()))))"
   290 
   291 definition enum_term_of_char :: "char itself => unit => term list"
   292 where
   293   "enum_term_of_char = (%_ _. map Code_Evaluation.term_of (Enum.enum :: char list))"
   294 
   295 instance ..
   296 
   297 end
   298 
   299 
   300 instantiation option :: (check_all) check_all
   301 begin
   302 
   303 definition
   304   "check_all f = f (Code_Evaluation.valtermify (None :: 'a option)) orelse check_all (%(x, t). f (Some x, %_. Code_Evaluation.App
   305     (Code_Evaluation.Const (STR ''Option.option.Some'')
   306       (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a),  Typerep.Typerep (STR ''Option.option'') [Typerep.typerep TYPE('a)]])) (t ())))"
   307 
   308 definition enum_term_of_option :: "'a option itself => unit => term list"
   309 where
   310   "enum_term_of_option = (% _ _. (Code_Evaluation.term_of (None :: 'a option)) # (map (Code_Evaluation.App (Code_Evaluation.Const (STR ''Option.option.Some'')
   311       (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a),  Typerep.Typerep (STR ''Option.option'') [Typerep.typerep TYPE('a)]]))) (enum_term_of (TYPE('a)) ())))"
   312 
   313 instance ..
   314 
   315 end
   316 
   317 
   318 instantiation Enum.finite_1 :: check_all
   319 begin
   320 
   321 definition
   322   "check_all f = f (Code_Evaluation.valtermify Enum.finite_1.a\<^isub>1)"
   323 
   324 definition enum_term_of_finite_1 :: "Enum.finite_1 itself => unit => term list"
   325 where
   326   "enum_term_of_finite_1 = (%_ _. [Code_Evaluation.term_of Enum.finite_1.a\<^isub>1])"
   327 
   328 instance ..
   329 
   330 end
   331 
   332 instantiation Enum.finite_2 :: check_all
   333 begin
   334 
   335 definition
   336   "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))"
   337 
   338 definition enum_term_of_finite_2 :: "Enum.finite_2 itself => unit => term list"
   339 where
   340   "enum_term_of_finite_2 = (%_ _. map Code_Evaluation.term_of (Enum.enum :: Enum.finite_2 list))"
   341 
   342 instance ..
   343 
   344 end
   345 
   346 instantiation Enum.finite_3 :: check_all
   347 begin
   348 
   349 definition
   350   "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)))"
   351 
   352 definition enum_term_of_finite_3 :: "Enum.finite_3 itself => unit => term list"
   353 where
   354   "enum_term_of_finite_3 = (%_ _. map Code_Evaluation.term_of (Enum.enum :: Enum.finite_3 list))"
   355 
   356 instance ..
   357 
   358 end
   359 
   360 
   361 
   362 subsection {* Defining combinators for any first-order data type *}
   363 
   364 definition catch_match :: "term list option => term list option => term list option"
   365 where
   366   [code del]: "catch_match t1 t2 = (SOME t. t = t1 \<or> t = t2)"
   367 
   368 code_const catch_match 
   369   (SML "(_) handle Match => _")
   370 
   371 use "Tools/exhaustive_generators.ML"
   372 
   373 setup {* Exhaustive_Generators.setup *}
   374 
   375 declare [[quickcheck_tester = exhaustive]]
   376 
   377 hide_fact orelse_def catch_match_def
   378 no_notation orelse (infixr "orelse" 55)
   379 hide_const (open) orelse catch_match mk_map_term check_all_n_lists
   380 
   381 end