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