src/HOL/Smallcheck.thy
author bulwahn
Fri Dec 10 11:42:05 2010 +0100 (2010-12-10)
changeset 41105 a76ee71c3313
parent 41104 013adf7ebd96
child 41177 810a885decee
permissions -rw-r--r--
adding check_all instances for a few more finite types in smallcheck
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
bulwahn@40639
   121
instance ..
bulwahn@40639
   122
bulwahn@40639
   123
end
bulwahn@40639
   124
bulwahn@41085
   125
subsubsection {* A smarter enumeration scheme for functions over finite datatypes *}
bulwahn@41085
   126
bulwahn@41085
   127
bulwahn@41085
   128
class check_all = enum + term_of +
bulwahn@41085
   129
fixes check_all :: "('a * (unit \<Rightarrow> term) \<Rightarrow> term list option) \<Rightarrow> term list option"
bulwahn@41085
   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@41104
   136
instantiation "fun" :: ("{equal, check_all}", check_all) check_all
bulwahn@41085
   137
begin
bulwahn@41085
   138
bulwahn@41085
   139
definition mk_map_term :: "'a list \<Rightarrow> (unit \<Rightarrow> term list) \<Rightarrow> (unit \<Rightarrow> typerep) \<Rightarrow> unit \<Rightarrow> term"
bulwahn@41085
   140
where
bulwahn@41085
   141
  "mk_map_term domm rng T2 =
bulwahn@41085
   142
     (%_. let T1 = (Typerep.typerep (TYPE('a)));
bulwahn@41085
   143
              T2 = T2 ();
bulwahn@41085
   144
              update_term = (%g (a, b).
bulwahn@41085
   145
                Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.App
bulwahn@41085
   146
                 (Code_Evaluation.Const (STR ''Fun.fun_upd'')
bulwahn@41085
   147
                   (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''fun'') [T1, T2],
bulwahn@41085
   148
                      Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''fun'') [T1, T2]]]])) g) (Code_Evaluation.term_of a)) b)
bulwahn@41085
   149
          in
bulwahn@41085
   150
             List.foldl update_term (Code_Evaluation.Abs (STR ''x'') T1 (Code_Evaluation.Const (STR ''HOL.undefined'') T2)) (zip domm (rng ())))"
bulwahn@41085
   151
bulwahn@41085
   152
definition
bulwahn@41085
   153
  "check_all f = check_all_n_lists (\<lambda>(ys, yst). f (the o map_of (zip (Enum.enum\<Colon>'a list) ys), mk_map_term (Enum.enum::'a list) yst (%_. Typerep.typerep (TYPE('b))))) (Code_Numeral.of_nat (length (Enum.enum :: 'a list)))"
bulwahn@41085
   154
bulwahn@41085
   155
instance ..
bulwahn@41085
   156
bulwahn@41085
   157
end
bulwahn@41085
   158
bulwahn@41105
   159
bulwahn@41105
   160
instantiation unit :: check_all
bulwahn@41105
   161
begin
bulwahn@41105
   162
bulwahn@41105
   163
definition
bulwahn@41105
   164
  "check_all f = f (Code_Evaluation.valtermify ())"
bulwahn@41105
   165
bulwahn@41105
   166
instance ..
bulwahn@41105
   167
bulwahn@41105
   168
end
bulwahn@41105
   169
bulwahn@41105
   170
bulwahn@41085
   171
instantiation bool :: check_all
bulwahn@41085
   172
begin
bulwahn@41085
   173
bulwahn@41085
   174
definition
bulwahn@41085
   175
  "check_all f = (case f (Code_Evaluation.valtermify False) of Some x' \<Rightarrow> Some x' | None \<Rightarrow> f (Code_Evaluation.valtermify True))"
bulwahn@41085
   176
bulwahn@41085
   177
instance ..
bulwahn@41085
   178
bulwahn@41085
   179
end
bulwahn@41085
   180
bulwahn@41105
   181
bulwahn@41085
   182
instantiation prod :: (check_all, check_all) check_all
bulwahn@41085
   183
begin
bulwahn@41085
   184
bulwahn@41085
   185
definition
bulwahn@41085
   186
  "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
   187
bulwahn@41085
   188
instance ..
bulwahn@41085
   189
bulwahn@41085
   190
end
bulwahn@41085
   191
bulwahn@41105
   192
bulwahn@41105
   193
instantiation sum :: (check_all, check_all) check_all
bulwahn@41105
   194
begin
bulwahn@41105
   195
bulwahn@41105
   196
definition
bulwahn@41105
   197
  "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
   198
             | None => check_all (%(b, t). f (Inr b, %_. Code_Evaluation.App (Code_Evaluation.term_of (Inr :: 'b => 'a + 'b)) (t ()))))"
bulwahn@41105
   199
bulwahn@41105
   200
instance ..
bulwahn@41105
   201
bulwahn@41105
   202
end
bulwahn@41105
   203
bulwahn@41105
   204
instantiation nibble :: check_all
bulwahn@41105
   205
begin
bulwahn@41105
   206
bulwahn@41105
   207
definition
bulwahn@41105
   208
  "check_all f =
bulwahn@41105
   209
    f (Code_Evaluation.valtermify Nibble0) orelse
bulwahn@41105
   210
    f (Code_Evaluation.valtermify Nibble1) orelse
bulwahn@41105
   211
    f (Code_Evaluation.valtermify Nibble2) orelse
bulwahn@41105
   212
    f (Code_Evaluation.valtermify Nibble3) orelse
bulwahn@41105
   213
    f (Code_Evaluation.valtermify Nibble4) orelse
bulwahn@41105
   214
    f (Code_Evaluation.valtermify Nibble5) orelse
bulwahn@41105
   215
    f (Code_Evaluation.valtermify Nibble6) orelse
bulwahn@41105
   216
    f (Code_Evaluation.valtermify Nibble7) orelse
bulwahn@41105
   217
    f (Code_Evaluation.valtermify Nibble8) orelse
bulwahn@41105
   218
    f (Code_Evaluation.valtermify Nibble9) orelse
bulwahn@41105
   219
    f (Code_Evaluation.valtermify NibbleA) orelse
bulwahn@41105
   220
    f (Code_Evaluation.valtermify NibbleB) orelse
bulwahn@41105
   221
    f (Code_Evaluation.valtermify NibbleC) orelse
bulwahn@41105
   222
    f (Code_Evaluation.valtermify NibbleD) orelse
bulwahn@41105
   223
    f (Code_Evaluation.valtermify NibbleE) orelse
bulwahn@41105
   224
    f (Code_Evaluation.valtermify NibbleF)"
bulwahn@41105
   225
bulwahn@41105
   226
instance ..
bulwahn@41105
   227
bulwahn@41105
   228
end
bulwahn@41105
   229
bulwahn@41105
   230
bulwahn@41105
   231
instantiation char :: check_all
bulwahn@41105
   232
begin
bulwahn@41105
   233
bulwahn@41105
   234
definition
bulwahn@41105
   235
  "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
   236
bulwahn@41105
   237
instance ..
bulwahn@41105
   238
bulwahn@41105
   239
end
bulwahn@41105
   240
bulwahn@41105
   241
bulwahn@41105
   242
instantiation option :: (check_all) check_all
bulwahn@41105
   243
begin
bulwahn@41105
   244
bulwahn@41105
   245
definition
bulwahn@41105
   246
  "check_all f = f (Code_Evaluation.valtermify (None :: 'a option)) orelse check_all (%(x, t). f (Some x, %_. Code_Evaluation.App (Code_Evaluation.term_of (Some :: 'a => 'a option)) (t ())))"
bulwahn@41105
   247
bulwahn@41105
   248
instance ..
bulwahn@41105
   249
bulwahn@41105
   250
end
bulwahn@41105
   251
bulwahn@41105
   252
bulwahn@41085
   253
instantiation Enum.finite_1 :: check_all
bulwahn@41085
   254
begin
bulwahn@41085
   255
bulwahn@41085
   256
definition
bulwahn@41085
   257
  "check_all f = f (Code_Evaluation.valtermify Enum.finite_1.a\<^isub>1)"
bulwahn@41085
   258
bulwahn@41085
   259
instance ..
bulwahn@41085
   260
bulwahn@41085
   261
end
bulwahn@41085
   262
bulwahn@41085
   263
instantiation Enum.finite_2 :: check_all
bulwahn@41085
   264
begin
bulwahn@41085
   265
bulwahn@41085
   266
definition
bulwahn@41085
   267
  "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
   268
bulwahn@41085
   269
instance ..
bulwahn@41085
   270
bulwahn@41085
   271
end
bulwahn@41085
   272
bulwahn@41085
   273
instantiation Enum.finite_3 :: check_all
bulwahn@41085
   274
begin
bulwahn@41085
   275
bulwahn@41085
   276
definition
bulwahn@41085
   277
  "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
   278
bulwahn@41085
   279
instance ..
bulwahn@41085
   280
bulwahn@41085
   281
end
bulwahn@41085
   282
bulwahn@41085
   283
bulwahn@41085
   284
huffman@40620
   285
subsection {* Defining combinators for any first-order data type *}
bulwahn@40420
   286
bulwahn@40420
   287
definition catch_match :: "term list option => term list option => term list option"
bulwahn@40420
   288
where
bulwahn@40420
   289
  [code del]: "catch_match t1 t2 = (SOME t. t = t1 \<or> t = t2)"
bulwahn@40420
   290
bulwahn@40420
   291
code_const catch_match 
bulwahn@40420
   292
  (SML "(_) handle Match => _")
bulwahn@40420
   293
bulwahn@40420
   294
use "Tools/smallvalue_generators.ML"
bulwahn@40420
   295
bulwahn@40420
   296
setup {* Smallvalue_Generators.setup *}
bulwahn@40420
   297
bulwahn@40915
   298
declare [[quickcheck_tester = exhaustive]]
bulwahn@40915
   299
bulwahn@40899
   300
hide_fact orelse_def catch_match_def
bulwahn@41105
   301
no_notation orelse (infixr "orelse" 55)
bulwahn@41085
   302
hide_const (open) orelse catch_match mk_map_term check_all_n_lists
bulwahn@40420
   303
bulwahn@40420
   304
end