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