src/HOL/Quickcheck_Exhaustive.thy
author bulwahn
Thu Jan 12 10:19:33 2012 +0100 (2012-01-12)
changeset 46193 55a4769d0abe
parent 45925 cd4243c025bb
child 46305 8ea02e499d53
permissions -rw-r--r--
adding exhaustive instances for type constructor set
bulwahn@40420
     1
(* Author: Lukas Bulwahn, TU Muenchen *)
bulwahn@40420
     2
bulwahn@41916
     3
header {* A simple counterexample generator performing exhaustive testing *}
bulwahn@40420
     4
bulwahn@41918
     5
theory Quickcheck_Exhaustive
bulwahn@40420
     6
imports Quickcheck
bulwahn@45925
     7
uses
bulwahn@45925
     8
  ("Tools/Quickcheck/exhaustive_generators.ML")
bulwahn@45925
     9
  ("Tools/Quickcheck/abstract_generators.ML")
bulwahn@40420
    10
begin
bulwahn@40420
    11
bulwahn@41916
    12
subsection {* basic operations for exhaustive generators *}
bulwahn@41105
    13
bulwahn@41105
    14
definition orelse :: "'a option => 'a option => 'a option" (infixr "orelse" 55)
bulwahn@41105
    15
where
bulwahn@41105
    16
  [code_unfold]: "x orelse y = (case x of Some x' => Some x' | None => y)"
bulwahn@40420
    17
bulwahn@41916
    18
subsection {* exhaustive generator type classes *}
bulwahn@40420
    19
bulwahn@41916
    20
class exhaustive = term_of +
bulwahn@45724
    21
  fixes exhaustive :: "('a \<Rightarrow> (bool * term list) option) \<Rightarrow> code_numeral \<Rightarrow> (bool * term list) option"
bulwahn@42310
    22
  
bulwahn@42310
    23
class full_exhaustive = term_of +
bulwahn@45722
    24
  fixes full_exhaustive :: "('a * (unit => term) \<Rightarrow> (bool * term list) option) \<Rightarrow> code_numeral \<Rightarrow> (bool * term list) option"
bulwahn@40420
    25
bulwahn@42310
    26
instantiation code_numeral :: full_exhaustive
bulwahn@40639
    27
begin
bulwahn@40639
    28
bulwahn@45722
    29
function full_exhaustive_code_numeral' :: "(code_numeral * (unit => term) => (bool * term list) option) => code_numeral => code_numeral => (bool * term list) option"
bulwahn@42304
    30
  where "full_exhaustive_code_numeral' f d i =
bulwahn@42304
    31
    (if d < i then None
bulwahn@42304
    32
    else (f (i, %_. Code_Evaluation.term_of i)) orelse (full_exhaustive_code_numeral' f d (i + 1)))"
bulwahn@42304
    33
by pat_completeness auto
bulwahn@42304
    34
bulwahn@42304
    35
termination
bulwahn@42304
    36
  by (relation "measure (%(_, d, i). Code_Numeral.nat_of (d + 1 - i))") auto
bulwahn@42304
    37
bulwahn@42304
    38
definition "full_exhaustive f d = full_exhaustive_code_numeral' f d 0"
bulwahn@42304
    39
bulwahn@42310
    40
instance ..
bulwahn@42310
    41
bulwahn@42310
    42
end
bulwahn@42310
    43
bulwahn@42310
    44
instantiation code_numeral :: exhaustive
bulwahn@42310
    45
begin
bulwahn@42310
    46
bulwahn@45724
    47
function exhaustive_code_numeral' :: "(code_numeral => (bool * term list) option) => code_numeral => code_numeral => (bool * term list) option"
bulwahn@41916
    48
  where "exhaustive_code_numeral' f d i =
bulwahn@41916
    49
    (if d < i then None
bulwahn@42304
    50
    else (f i orelse exhaustive_code_numeral' f d (i + 1)))"
bulwahn@41231
    51
by pat_completeness auto
bulwahn@41231
    52
bulwahn@42304
    53
termination
bulwahn@41231
    54
  by (relation "measure (%(_, d, i). Code_Numeral.nat_of (d + 1 - i))") auto
bulwahn@41231
    55
bulwahn@41916
    56
definition "exhaustive f d = exhaustive_code_numeral' f d 0"
bulwahn@41231
    57
bulwahn@41231
    58
instance ..
bulwahn@41231
    59
bulwahn@41231
    60
end
bulwahn@41231
    61
bulwahn@41916
    62
instantiation nat :: exhaustive
bulwahn@41231
    63
begin
bulwahn@41231
    64
bulwahn@42304
    65
definition "exhaustive f d = exhaustive (%x. f (Code_Numeral.nat_of x)) d"
bulwahn@42304
    66
bulwahn@42310
    67
instance ..
bulwahn@42310
    68
bulwahn@42310
    69
end
bulwahn@42310
    70
bulwahn@42310
    71
instantiation nat :: full_exhaustive
bulwahn@42310
    72
begin
bulwahn@42310
    73
bulwahn@42304
    74
definition "full_exhaustive f d = full_exhaustive (%(x, xt). f (Code_Numeral.nat_of x, %_. Code_Evaluation.term_of (Code_Numeral.nat_of x))) d"
bulwahn@41231
    75
bulwahn@41231
    76
instance ..
bulwahn@41231
    77
bulwahn@41231
    78
end
bulwahn@41231
    79
bulwahn@41916
    80
instantiation int :: exhaustive
bulwahn@40639
    81
begin
bulwahn@40639
    82
bulwahn@45724
    83
function exhaustive' :: "(int => (bool * term list) option) => int => int => (bool * term list) option"
bulwahn@42304
    84
  where "exhaustive' f d i = (if d < i then None else (f i orelse exhaustive' f d (i + 1)))"
bulwahn@40639
    85
by pat_completeness auto
bulwahn@40639
    86
bulwahn@40639
    87
termination 
bulwahn@40639
    88
  by (relation "measure (%(_, d, i). nat (d + 1 - i))") auto
bulwahn@40639
    89
bulwahn@41916
    90
definition "exhaustive f d = exhaustive' f (Code_Numeral.int_of d) (- (Code_Numeral.int_of d))"
bulwahn@40639
    91
bulwahn@42310
    92
instance ..
bulwahn@42310
    93
bulwahn@42310
    94
end
bulwahn@42310
    95
bulwahn@42310
    96
instantiation int :: full_exhaustive
bulwahn@42310
    97
begin
bulwahn@42310
    98
bulwahn@45722
    99
function full_exhaustive' :: "(int * (unit => term) => (bool * term list) option) => int => int => (bool * term list) option"
bulwahn@42304
   100
  where "full_exhaustive' f d i = (if d < i then None else (case f (i, %_. Code_Evaluation.term_of i) of Some t => Some t | None => full_exhaustive' f d (i + 1)))"
bulwahn@42304
   101
by pat_completeness auto
bulwahn@42304
   102
bulwahn@42304
   103
termination 
bulwahn@42304
   104
  by (relation "measure (%(_, d, i). nat (d + 1 - i))") auto
bulwahn@42304
   105
bulwahn@42304
   106
definition "full_exhaustive f d = full_exhaustive' f (Code_Numeral.int_of d) (- (Code_Numeral.int_of d))"
bulwahn@42304
   107
bulwahn@40639
   108
instance ..
bulwahn@40639
   109
bulwahn@40639
   110
end
bulwahn@40639
   111
bulwahn@41916
   112
instantiation prod :: (exhaustive, exhaustive) exhaustive
bulwahn@40639
   113
begin
bulwahn@40899
   114
bulwahn@40639
   115
definition
bulwahn@42304
   116
  "exhaustive f d = exhaustive (%x. exhaustive (%y. f ((x, y))) d) d"
bulwahn@42304
   117
bulwahn@42310
   118
instance ..
bulwahn@42310
   119
bulwahn@42310
   120
end
bulwahn@42310
   121
bulwahn@42310
   122
instantiation prod :: (full_exhaustive, full_exhaustive) full_exhaustive
bulwahn@42310
   123
begin
bulwahn@42310
   124
bulwahn@42304
   125
definition
bulwahn@42304
   126
  "full_exhaustive f d = full_exhaustive (%(x, t1). full_exhaustive (%(y, t2). f ((x, y),
bulwahn@41719
   127
    %u. let T1 = (Typerep.typerep (TYPE('a)));
bulwahn@41719
   128
            T2 = (Typerep.typerep (TYPE('b)))
bulwahn@41719
   129
    in Code_Evaluation.App (Code_Evaluation.App (
bulwahn@41719
   130
      Code_Evaluation.Const (STR ''Product_Type.Pair'') 
bulwahn@41719
   131
      (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Product_Type.prod'') [T1, T2]]]))
bulwahn@41719
   132
      (t1 ())) (t2 ()))) d) d"
bulwahn@40639
   133
bulwahn@40639
   134
instance ..
bulwahn@40639
   135
bulwahn@40639
   136
end
bulwahn@40639
   137
bulwahn@46193
   138
instantiation set :: (exhaustive) exhaustive
bulwahn@46193
   139
begin
bulwahn@46193
   140
bulwahn@46193
   141
fun exhaustive_set
bulwahn@46193
   142
where
bulwahn@46193
   143
  "exhaustive_set f i = (if i = 0 then None else (f {} orelse exhaustive_set (%A. f A orelse Quickcheck_Exhaustive.exhaustive (%x. if x \<in> A then None else f (insert x A)) (i - 1)) (i - 1)))"
bulwahn@46193
   144
bulwahn@46193
   145
instance ..
bulwahn@46193
   146
bulwahn@46193
   147
end
bulwahn@46193
   148
bulwahn@46193
   149
definition (in term_syntax) "valterm_emptyset = Code_Evaluation.valtermify ({} :: ('a :: typerep) set)"
bulwahn@46193
   150
definition (in term_syntax) "valtermify_insert x s = Code_Evaluation.valtermify insert {\<cdot>} (x :: ('a :: typerep * _)) {\<cdot>} s"
bulwahn@46193
   151
bulwahn@46193
   152
instantiation set :: (full_exhaustive) full_exhaustive
bulwahn@46193
   153
begin
bulwahn@46193
   154
bulwahn@46193
   155
fun full_exhaustive_set 
bulwahn@46193
   156
where
bulwahn@46193
   157
  "full_exhaustive_set f i = (if i = 0 then None else (f valterm_emptyset orelse full_exhaustive_set (%A. f A orelse Quickcheck_Exhaustive.full_exhaustive (%x. if fst x \<in> fst A then None else f (valtermify_insert x A)) (i - 1)) (i - 1)))"
bulwahn@46193
   158
bulwahn@46193
   159
instance ..
bulwahn@46193
   160
bulwahn@46193
   161
end
bulwahn@46193
   162
bulwahn@46193
   163
hide_const valterm_emptyset valtermify_insert
bulwahn@46193
   164
bulwahn@41916
   165
instantiation "fun" :: ("{equal, exhaustive}", exhaustive) exhaustive
bulwahn@40639
   166
begin
bulwahn@40639
   167
bulwahn@45724
   168
fun exhaustive_fun' :: "(('a => 'b) => (bool * term list) option) => code_numeral => code_numeral => (bool * term list) option"
bulwahn@42304
   169
where
bulwahn@42304
   170
  "exhaustive_fun' f i d = (exhaustive (%b. f (%_. b)) d)
bulwahn@42304
   171
   orelse (if i > 1 then
bulwahn@42304
   172
     exhaustive_fun' (%g. exhaustive (%a. exhaustive (%b.
bulwahn@42304
   173
       f (g(a := b))) d) d) (i - 1) d else None)"
bulwahn@42304
   174
bulwahn@45724
   175
definition exhaustive_fun :: "(('a => 'b) => (bool * term list) option) => code_numeral => (bool * term list) option"
bulwahn@40639
   176
where
bulwahn@42304
   177
  "exhaustive_fun f d = exhaustive_fun' f d d" 
bulwahn@42304
   178
bulwahn@42310
   179
instance ..
bulwahn@42310
   180
bulwahn@42310
   181
end
bulwahn@42310
   182
bulwahn@42310
   183
instantiation "fun" :: ("{equal, full_exhaustive}", full_exhaustive) full_exhaustive
bulwahn@42310
   184
begin
bulwahn@42304
   185
bulwahn@45722
   186
fun full_exhaustive_fun' :: "(('a => 'b) * (unit => term) => (bool * term list) option) => code_numeral => code_numeral => (bool * term list) option"
bulwahn@42304
   187
where
bulwahn@42304
   188
  "full_exhaustive_fun' f i d = (full_exhaustive (%(b, t). f (%_. b, %_. Code_Evaluation.Abs (STR ''x'') (Typerep.typerep TYPE('a)) (t ()))) d)
bulwahn@42117
   189
   orelse (if i > 1 then
bulwahn@42304
   190
     full_exhaustive_fun' (%(g, gt). full_exhaustive (%(a, at). full_exhaustive (%(b, bt).
bulwahn@42117
   191
       f (g(a := b),
bulwahn@42117
   192
         (%_. let A = (Typerep.typerep (TYPE('a)));
bulwahn@42117
   193
                  B = (Typerep.typerep (TYPE('b)));
bulwahn@42117
   194
                  fun = (%T U. Typerep.Typerep (STR ''fun'') [T, U])
bulwahn@42117
   195
              in
bulwahn@42117
   196
                Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.App
bulwahn@42117
   197
                  (Code_Evaluation.Const (STR ''Fun.fun_upd'') (fun (fun A B) (fun A (fun B (fun A B)))))
bulwahn@42117
   198
                (gt ())) (at ())) (bt ())))) d) d) (i - 1) d else None)"
bulwahn@40639
   199
bulwahn@45722
   200
definition full_exhaustive_fun :: "(('a => 'b) * (unit => term) => (bool * term list) option) => code_numeral => (bool * term list) option"
bulwahn@40639
   201
where
bulwahn@42304
   202
  "full_exhaustive_fun f d = full_exhaustive_fun' f d d" 
bulwahn@40639
   203
bulwahn@40639
   204
instance ..
bulwahn@40639
   205
bulwahn@40639
   206
end
bulwahn@40639
   207
bulwahn@41085
   208
subsubsection {* A smarter enumeration scheme for functions over finite datatypes *}
bulwahn@41085
   209
bulwahn@41085
   210
class check_all = enum + term_of +
bulwahn@45722
   211
  fixes check_all :: "('a * (unit \<Rightarrow> term) \<Rightarrow> (bool * term list) option) \<Rightarrow> (bool * term list) option"
bulwahn@41177
   212
  fixes enum_term_of :: "'a itself \<Rightarrow> unit \<Rightarrow> term list"
bulwahn@41177
   213
  
bulwahn@45722
   214
fun check_all_n_lists :: "(('a :: check_all) list * (unit \<Rightarrow> term list) \<Rightarrow> (bool * term list) option) \<Rightarrow> code_numeral \<Rightarrow> (bool * term list) option"
bulwahn@41085
   215
where
bulwahn@41085
   216
  "check_all_n_lists f n =
bulwahn@41085
   217
     (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
   218
bulwahn@41177
   219
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
   220
where
bulwahn@41177
   221
  "mk_map_term T1 T2 domm rng =
bulwahn@41177
   222
     (%_. let T1 = T1 ();
bulwahn@41085
   223
              T2 = T2 ();
bulwahn@41085
   224
              update_term = (%g (a, b).
bulwahn@41085
   225
                Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.App
bulwahn@41085
   226
                 (Code_Evaluation.Const (STR ''Fun.fun_upd'')
bulwahn@41085
   227
                   (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''fun'') [T1, T2],
bulwahn@41177
   228
                      Typerep.Typerep (STR ''fun'') [T1,
bulwahn@41177
   229
                        Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''fun'') [T1, T2]]]]))
bulwahn@41177
   230
                        g) a) b)
bulwahn@41085
   231
          in
bulwahn@41177
   232
             List.foldl update_term (Code_Evaluation.Abs (STR ''x'') T1 (Code_Evaluation.Const (STR ''HOL.undefined'') T2)) (zip (domm ()) (rng ())))"
bulwahn@41177
   233
bulwahn@41177
   234
instantiation "fun" :: ("{equal, check_all}", check_all) check_all
bulwahn@41177
   235
begin
bulwahn@41085
   236
bulwahn@41085
   237
definition
bulwahn@41177
   238
  "check_all f =
bulwahn@41177
   239
    (let
bulwahn@41177
   240
      mk_term = mk_map_term (%_. Typerep.typerep (TYPE('a))) (%_. Typerep.typerep (TYPE('b))) (enum_term_of (TYPE('a)));
bulwahn@41177
   241
      enum = (Enum.enum :: 'a list)
bulwahn@41177
   242
    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
   243
bulwahn@41177
   244
definition enum_term_of_fun :: "('a => 'b) itself => unit => term list"
bulwahn@41177
   245
where
bulwahn@41177
   246
  "enum_term_of_fun = (%_ _. let
bulwahn@41177
   247
    enum_term_of_a = enum_term_of (TYPE('a));
bulwahn@41177
   248
    mk_term = mk_map_term (%_. Typerep.typerep (TYPE('a))) (%_. Typerep.typerep (TYPE('b))) enum_term_of_a
bulwahn@41177
   249
  in map (%ys. mk_term (%_. ys) ()) (Enum.n_lists (length (enum_term_of_a ())) (enum_term_of (TYPE('b)) ())))"
bulwahn@41177
   250
 
bulwahn@41085
   251
instance ..
bulwahn@41085
   252
bulwahn@41085
   253
end
bulwahn@41085
   254
bulwahn@41105
   255
bulwahn@41105
   256
instantiation unit :: check_all
bulwahn@41105
   257
begin
bulwahn@41105
   258
bulwahn@41105
   259
definition
bulwahn@41105
   260
  "check_all f = f (Code_Evaluation.valtermify ())"
bulwahn@41105
   261
bulwahn@41177
   262
definition enum_term_of_unit :: "unit itself => unit => term list"
bulwahn@41177
   263
where
bulwahn@41177
   264
  "enum_term_of_unit = (%_ _. [Code_Evaluation.term_of ()])"
bulwahn@41177
   265
bulwahn@41105
   266
instance ..
bulwahn@41105
   267
bulwahn@41105
   268
end
bulwahn@41105
   269
bulwahn@41105
   270
bulwahn@41085
   271
instantiation bool :: check_all
bulwahn@41085
   272
begin
bulwahn@41085
   273
bulwahn@41085
   274
definition
bulwahn@41085
   275
  "check_all f = (case f (Code_Evaluation.valtermify False) of Some x' \<Rightarrow> Some x' | None \<Rightarrow> f (Code_Evaluation.valtermify True))"
bulwahn@41085
   276
bulwahn@41177
   277
definition enum_term_of_bool :: "bool itself => unit => term list"
bulwahn@41177
   278
where
bulwahn@41177
   279
  "enum_term_of_bool = (%_ _. map Code_Evaluation.term_of (Enum.enum :: bool list))"
bulwahn@41177
   280
bulwahn@41085
   281
instance ..
bulwahn@41085
   282
bulwahn@41085
   283
end
bulwahn@41085
   284
bulwahn@41105
   285
bulwahn@41085
   286
instantiation prod :: (check_all, check_all) check_all
bulwahn@41085
   287
begin
bulwahn@41085
   288
bulwahn@41085
   289
definition
bulwahn@41719
   290
  "check_all f = check_all (%(x, t1). check_all (%(y, t2). f ((x, y),
bulwahn@41719
   291
    %u. let T1 = (Typerep.typerep (TYPE('a)));
bulwahn@41719
   292
            T2 = (Typerep.typerep (TYPE('b)))
bulwahn@41719
   293
    in Code_Evaluation.App (Code_Evaluation.App (
bulwahn@41719
   294
      Code_Evaluation.Const (STR ''Product_Type.Pair'') 
bulwahn@41719
   295
      (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Product_Type.prod'') [T1, T2]]]))
bulwahn@41719
   296
      (t1 ())) (t2 ()))))"
bulwahn@41085
   297
bulwahn@41177
   298
definition enum_term_of_prod :: "('a * 'b) itself => unit => term list"
bulwahn@41177
   299
where
bulwahn@41719
   300
  "enum_term_of_prod = (%_ _. map (%(x, y).
bulwahn@41719
   301
       let T1 = (Typerep.typerep (TYPE('a)));
bulwahn@41719
   302
           T2 = (Typerep.typerep (TYPE('b)))
bulwahn@41719
   303
       in Code_Evaluation.App (Code_Evaluation.App (
bulwahn@41719
   304
         Code_Evaluation.Const (STR ''Product_Type.Pair'') 
bulwahn@41719
   305
           (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Product_Type.prod'') [T1, T2]]])) x) y)
bulwahn@41719
   306
     (Enum.product (enum_term_of (TYPE('a)) ()) (enum_term_of (TYPE('b)) ())))  "
bulwahn@41177
   307
bulwahn@41085
   308
instance ..
bulwahn@41085
   309
bulwahn@41085
   310
end
bulwahn@41085
   311
bulwahn@41105
   312
bulwahn@41105
   313
instantiation sum :: (check_all, check_all) check_all
bulwahn@41105
   314
begin
bulwahn@41105
   315
bulwahn@41105
   316
definition
bulwahn@41722
   317
  "check_all f = (case check_all (%(a, t). f (Inl a, %_. 
bulwahn@41722
   318
     let T1 = (Typerep.typerep (TYPE('a)));
bulwahn@41722
   319
         T2 = (Typerep.typerep (TYPE('b)))
bulwahn@41722
   320
       in Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inl'') 
bulwahn@41722
   321
           (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])) (t ()))) of Some x' => Some x'
bulwahn@41722
   322
             | None => check_all (%(b, t). f (Inr b, %_. let
bulwahn@41722
   323
                 T1 = (Typerep.typerep (TYPE('a)));
bulwahn@41722
   324
                 T2 = (Typerep.typerep (TYPE('b)))
bulwahn@41722
   325
               in Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inr'') 
bulwahn@41722
   326
                 (Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])) (t ()))))"
bulwahn@41105
   327
bulwahn@41177
   328
definition enum_term_of_sum :: "('a + 'b) itself => unit => term list"
bulwahn@41177
   329
where
bulwahn@41722
   330
  "enum_term_of_sum = (%_ _.
bulwahn@41722
   331
     let
bulwahn@41722
   332
       T1 = (Typerep.typerep (TYPE('a)));
bulwahn@41722
   333
       T2 = (Typerep.typerep (TYPE('b)))
bulwahn@41722
   334
     in
bulwahn@41722
   335
       map (Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inl'') 
bulwahn@41722
   336
             (Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])))
bulwahn@41722
   337
             (enum_term_of (TYPE('a)) ()) @
bulwahn@41722
   338
       map (Code_Evaluation.App (Code_Evaluation.Const (STR ''Sum_Type.Inr'') 
bulwahn@41722
   339
             (Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''Sum_Type.sum'') [T1, T2]])))
bulwahn@41722
   340
             (enum_term_of (TYPE('b)) ()))"
bulwahn@41177
   341
bulwahn@41105
   342
instance ..
bulwahn@41105
   343
bulwahn@41105
   344
end
bulwahn@41105
   345
bulwahn@41105
   346
instantiation nibble :: check_all
bulwahn@41105
   347
begin
bulwahn@41105
   348
bulwahn@41105
   349
definition
bulwahn@41105
   350
  "check_all f =
bulwahn@41105
   351
    f (Code_Evaluation.valtermify Nibble0) orelse
bulwahn@41105
   352
    f (Code_Evaluation.valtermify Nibble1) orelse
bulwahn@41105
   353
    f (Code_Evaluation.valtermify Nibble2) orelse
bulwahn@41105
   354
    f (Code_Evaluation.valtermify Nibble3) orelse
bulwahn@41105
   355
    f (Code_Evaluation.valtermify Nibble4) orelse
bulwahn@41105
   356
    f (Code_Evaluation.valtermify Nibble5) orelse
bulwahn@41105
   357
    f (Code_Evaluation.valtermify Nibble6) orelse
bulwahn@41105
   358
    f (Code_Evaluation.valtermify Nibble7) orelse
bulwahn@41105
   359
    f (Code_Evaluation.valtermify Nibble8) orelse
bulwahn@41105
   360
    f (Code_Evaluation.valtermify Nibble9) orelse
bulwahn@41105
   361
    f (Code_Evaluation.valtermify NibbleA) orelse
bulwahn@41105
   362
    f (Code_Evaluation.valtermify NibbleB) orelse
bulwahn@41105
   363
    f (Code_Evaluation.valtermify NibbleC) orelse
bulwahn@41105
   364
    f (Code_Evaluation.valtermify NibbleD) orelse
bulwahn@41105
   365
    f (Code_Evaluation.valtermify NibbleE) orelse
bulwahn@41105
   366
    f (Code_Evaluation.valtermify NibbleF)"
bulwahn@41105
   367
bulwahn@41177
   368
definition enum_term_of_nibble :: "nibble itself => unit => term list"
bulwahn@41177
   369
where
bulwahn@41177
   370
  "enum_term_of_nibble = (%_ _. map Code_Evaluation.term_of (Enum.enum :: nibble list))"
bulwahn@41177
   371
bulwahn@41105
   372
instance ..
bulwahn@41105
   373
bulwahn@41105
   374
end
bulwahn@41105
   375
bulwahn@41105
   376
bulwahn@41105
   377
instantiation char :: check_all
bulwahn@41105
   378
begin
bulwahn@41105
   379
bulwahn@41105
   380
definition
bulwahn@41105
   381
  "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
   382
bulwahn@41177
   383
definition enum_term_of_char :: "char itself => unit => term list"
bulwahn@41177
   384
where
bulwahn@41177
   385
  "enum_term_of_char = (%_ _. map Code_Evaluation.term_of (Enum.enum :: char list))"
bulwahn@41177
   386
bulwahn@41105
   387
instance ..
bulwahn@41105
   388
bulwahn@41105
   389
end
bulwahn@41105
   390
bulwahn@41105
   391
bulwahn@41105
   392
instantiation option :: (check_all) check_all
bulwahn@41105
   393
begin
bulwahn@41105
   394
bulwahn@41105
   395
definition
bulwahn@41178
   396
  "check_all f = f (Code_Evaluation.valtermify (None :: 'a option)) orelse check_all (%(x, t). f (Some x, %_. Code_Evaluation.App
bulwahn@41178
   397
    (Code_Evaluation.Const (STR ''Option.option.Some'')
bulwahn@41178
   398
      (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a),  Typerep.Typerep (STR ''Option.option'') [Typerep.typerep TYPE('a)]])) (t ())))"
bulwahn@41105
   399
bulwahn@41177
   400
definition enum_term_of_option :: "'a option itself => unit => term list"
bulwahn@41177
   401
where
bulwahn@41722
   402
  "enum_term_of_option = (% _ _. (Code_Evaluation.term_of (None :: 'a option)) # (map (Code_Evaluation.App (Code_Evaluation.Const (STR ''Option.option.Some'')
bulwahn@41722
   403
      (Typerep.Typerep (STR ''fun'') [Typerep.typerep TYPE('a),  Typerep.Typerep (STR ''Option.option'') [Typerep.typerep TYPE('a)]]))) (enum_term_of (TYPE('a)) ())))"
bulwahn@41177
   404
bulwahn@41105
   405
instance ..
bulwahn@41105
   406
bulwahn@41105
   407
end
bulwahn@41105
   408
bulwahn@41105
   409
bulwahn@41085
   410
instantiation Enum.finite_1 :: check_all
bulwahn@41085
   411
begin
bulwahn@41085
   412
bulwahn@41085
   413
definition
bulwahn@41085
   414
  "check_all f = f (Code_Evaluation.valtermify Enum.finite_1.a\<^isub>1)"
bulwahn@41085
   415
bulwahn@41177
   416
definition enum_term_of_finite_1 :: "Enum.finite_1 itself => unit => term list"
bulwahn@41177
   417
where
bulwahn@41177
   418
  "enum_term_of_finite_1 = (%_ _. [Code_Evaluation.term_of Enum.finite_1.a\<^isub>1])"
bulwahn@41177
   419
bulwahn@41085
   420
instance ..
bulwahn@41085
   421
bulwahn@41085
   422
end
bulwahn@41085
   423
bulwahn@41085
   424
instantiation Enum.finite_2 :: check_all
bulwahn@41085
   425
begin
bulwahn@41085
   426
bulwahn@41085
   427
definition
bulwahn@41085
   428
  "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
   429
bulwahn@41177
   430
definition enum_term_of_finite_2 :: "Enum.finite_2 itself => unit => term list"
bulwahn@41177
   431
where
bulwahn@41177
   432
  "enum_term_of_finite_2 = (%_ _. map Code_Evaluation.term_of (Enum.enum :: Enum.finite_2 list))"
bulwahn@41177
   433
bulwahn@41085
   434
instance ..
bulwahn@41085
   435
bulwahn@41085
   436
end
bulwahn@41085
   437
bulwahn@41085
   438
instantiation Enum.finite_3 :: check_all
bulwahn@41085
   439
begin
bulwahn@41085
   440
bulwahn@41085
   441
definition
bulwahn@41085
   442
  "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
   443
bulwahn@41177
   444
definition enum_term_of_finite_3 :: "Enum.finite_3 itself => unit => term list"
bulwahn@41177
   445
where
bulwahn@41177
   446
  "enum_term_of_finite_3 = (%_ _. map Code_Evaluation.term_of (Enum.enum :: Enum.finite_3 list))"
bulwahn@41177
   447
bulwahn@41085
   448
instance ..
bulwahn@41085
   449
bulwahn@41085
   450
end
bulwahn@41085
   451
bulwahn@42195
   452
subsection {* Bounded universal quantifiers *}
bulwahn@41085
   453
bulwahn@42195
   454
class bounded_forall =
bulwahn@42195
   455
  fixes bounded_forall :: "('a \<Rightarrow> bool) \<Rightarrow> code_numeral \<Rightarrow> bool"
bulwahn@42195
   456
bulwahn@42305
   457
subsection {* Fast exhaustive combinators *}
bulwahn@42305
   458
bulwahn@42305
   459
class fast_exhaustive = term_of +
bulwahn@42305
   460
  fixes fast_exhaustive :: "('a \<Rightarrow> unit) \<Rightarrow> code_numeral \<Rightarrow> unit"
bulwahn@42305
   461
bulwahn@45818
   462
axiomatization throw_Counterexample :: "term list => unit"
bulwahn@45818
   463
axiomatization catch_Counterexample :: "unit => term list option"
bulwahn@42305
   464
bulwahn@42305
   465
code_const throw_Counterexample
bulwahn@42305
   466
  (Quickcheck "raise (Exhaustive'_Generators.Counterexample _)")
bulwahn@42305
   467
code_const catch_Counterexample
bulwahn@42305
   468
  (Quickcheck "(((_); NONE) handle Exhaustive'_Generators.Counterexample ts => SOME ts)")
bulwahn@42305
   469
bulwahn@45450
   470
subsection {* Continuation passing style functions as plus monad *}
bulwahn@45450
   471
  
bulwahn@45450
   472
type_synonym 'a cps = "('a => term list option) => term list option"
bulwahn@45450
   473
bulwahn@45450
   474
definition cps_empty :: "'a cps"
bulwahn@45450
   475
where
bulwahn@45450
   476
  "cps_empty = (%cont. None)"
bulwahn@45450
   477
bulwahn@45450
   478
definition cps_single :: "'a => 'a cps"
bulwahn@45450
   479
where
bulwahn@45450
   480
  "cps_single v = (%cont. cont v)"
bulwahn@45450
   481
bulwahn@45450
   482
definition cps_bind :: "'a cps => ('a => 'b cps) => 'b cps" 
bulwahn@45450
   483
where
bulwahn@45450
   484
  "cps_bind m f = (%cont. m (%a. (f a) cont))"
bulwahn@45450
   485
bulwahn@45450
   486
definition cps_plus :: "'a cps => 'a cps => 'a cps"
bulwahn@45450
   487
where
bulwahn@45450
   488
  "cps_plus a b = (%c. case a c of None => b c | Some x => Some x)"
bulwahn@45450
   489
bulwahn@45450
   490
definition cps_if :: "bool => unit cps"
bulwahn@45450
   491
where
bulwahn@45450
   492
  "cps_if b = (if b then cps_single () else cps_empty)"
bulwahn@45450
   493
bulwahn@45450
   494
definition cps_not :: "unit cps => unit cps"
bulwahn@45450
   495
where
bulwahn@45450
   496
  "cps_not n = (%c. case n (%u. Some []) of None => c () | Some _ => None)"
bulwahn@45450
   497
bulwahn@45750
   498
type_synonym 'a pos_bound_cps = "('a => (bool * term list) option) => code_numeral => (bool * term list) option"
bulwahn@45450
   499
bulwahn@45450
   500
definition pos_bound_cps_empty :: "'a pos_bound_cps"
bulwahn@45450
   501
where
bulwahn@45450
   502
  "pos_bound_cps_empty = (%cont i. None)"
bulwahn@45450
   503
bulwahn@45450
   504
definition pos_bound_cps_single :: "'a => 'a pos_bound_cps"
bulwahn@45450
   505
where
bulwahn@45450
   506
  "pos_bound_cps_single v = (%cont i. cont v)"
bulwahn@45450
   507
bulwahn@45450
   508
definition pos_bound_cps_bind :: "'a pos_bound_cps => ('a => 'b pos_bound_cps) => 'b pos_bound_cps" 
bulwahn@45450
   509
where
bulwahn@45450
   510
  "pos_bound_cps_bind m f = (%cont i. if i = 0 then None else (m (%a. (f a) cont i) (i - 1)))"
bulwahn@45450
   511
bulwahn@45450
   512
definition pos_bound_cps_plus :: "'a pos_bound_cps => 'a pos_bound_cps => 'a pos_bound_cps"
bulwahn@45450
   513
where
bulwahn@45450
   514
  "pos_bound_cps_plus a b = (%c i. case a c i of None => b c i | Some x => Some x)"
bulwahn@45450
   515
bulwahn@45450
   516
definition pos_bound_cps_if :: "bool => unit pos_bound_cps"
bulwahn@45450
   517
where
bulwahn@45450
   518
  "pos_bound_cps_if b = (if b then pos_bound_cps_single () else pos_bound_cps_empty)"
bulwahn@45450
   519
bulwahn@45450
   520
datatype 'a unknown = Unknown | Known 'a
bulwahn@45450
   521
datatype 'a three_valued = Unknown_value | Value 'a | No_value
bulwahn@45450
   522
bulwahn@45450
   523
type_synonym 'a neg_bound_cps = "('a unknown => term list three_valued) => code_numeral => term list three_valued"
bulwahn@45450
   524
bulwahn@45450
   525
definition neg_bound_cps_empty :: "'a neg_bound_cps"
bulwahn@45450
   526
where
bulwahn@45450
   527
  "neg_bound_cps_empty = (%cont i. No_value)"
bulwahn@45450
   528
bulwahn@45450
   529
definition neg_bound_cps_single :: "'a => 'a neg_bound_cps"
bulwahn@45450
   530
where
bulwahn@45450
   531
  "neg_bound_cps_single v = (%cont i. cont (Known v))"
bulwahn@45450
   532
bulwahn@45450
   533
definition neg_bound_cps_bind :: "'a neg_bound_cps => ('a => 'b neg_bound_cps) => 'b neg_bound_cps" 
bulwahn@45450
   534
where
bulwahn@45450
   535
  "neg_bound_cps_bind m f = (%cont i. if i = 0 then cont Unknown else m (%a. case a of Unknown => cont Unknown | Known a' => f a' cont i) (i - 1))"
bulwahn@45450
   536
bulwahn@45450
   537
definition neg_bound_cps_plus :: "'a neg_bound_cps => 'a neg_bound_cps => 'a neg_bound_cps"
bulwahn@45450
   538
where
bulwahn@45450
   539
  "neg_bound_cps_plus a b = (%c i. case a c i of No_value => b c i | Value x => Value x | Unknown_value => (case b c i of No_value => Unknown_value | Value x => Value x | Unknown_value => Unknown_value))"
bulwahn@45450
   540
bulwahn@45450
   541
definition neg_bound_cps_if :: "bool => unit neg_bound_cps"
bulwahn@45450
   542
where
bulwahn@45450
   543
  "neg_bound_cps_if b = (if b then neg_bound_cps_single () else neg_bound_cps_empty)"
bulwahn@45450
   544
bulwahn@45450
   545
definition neg_bound_cps_not :: "unit pos_bound_cps => unit neg_bound_cps"
bulwahn@45450
   546
where
bulwahn@45750
   547
  "neg_bound_cps_not n = (%c i. case n (%u. Some (True, [])) i of None => c (Known ()) | Some _ => No_value)"
bulwahn@45450
   548
bulwahn@45450
   549
definition pos_bound_cps_not :: "unit neg_bound_cps => unit pos_bound_cps"
bulwahn@45450
   550
where
bulwahn@45450
   551
  "pos_bound_cps_not n = (%c i. case n (%u. Value []) i of No_value => c () | Value _ => None | Unknown_value => None)"
bulwahn@45450
   552
bulwahn@45925
   553
subsection {* Defining generators for any first-order data type *}
bulwahn@40420
   554
bulwahn@45697
   555
axiomatization unknown :: 'a
bulwahn@45697
   556
bulwahn@45697
   557
notation (output) unknown  ("?")
bulwahn@45684
   558
 
bulwahn@41920
   559
use "Tools/Quickcheck/exhaustive_generators.ML"
bulwahn@40420
   560
bulwahn@41918
   561
setup {* Exhaustive_Generators.setup *}
bulwahn@40420
   562
bulwahn@43882
   563
declare [[quickcheck_batch_tester = exhaustive]]
bulwahn@40915
   564
bulwahn@45925
   565
subsection {* Defining generators for abstract types *}
bulwahn@45925
   566
bulwahn@45925
   567
use "Tools/Quickcheck/abstract_generators.ML"
bulwahn@45925
   568
bulwahn@45733
   569
hide_fact orelse_def
bulwahn@41105
   570
no_notation orelse (infixr "orelse" 55)
bulwahn@45818
   571
bulwahn@45818
   572
hide_fact
bulwahn@45818
   573
  exhaustive'_def
bulwahn@45818
   574
  exhaustive_code_numeral'_def
bulwahn@45818
   575
bulwahn@45818
   576
hide_const (open)
bulwahn@45818
   577
  exhaustive full_exhaustive exhaustive' exhaustive_code_numeral' full_exhaustive_code_numeral'
bulwahn@45818
   578
  throw_Counterexample catch_Counterexample
bulwahn@45818
   579
  check_all enum_term_of
bulwahn@45818
   580
  orelse unknown mk_map_term check_all_n_lists
bulwahn@40420
   581
bulwahn@45450
   582
hide_type (open) cps pos_bound_cps neg_bound_cps unknown three_valued
bulwahn@45450
   583
hide_const (open) cps_empty cps_single cps_bind cps_plus cps_if cps_not
bulwahn@45450
   584
  pos_bound_cps_empty pos_bound_cps_single pos_bound_cps_bind pos_bound_cps_plus pos_bound_cps_if pos_bound_cps_not
bulwahn@45450
   585
  neg_bound_cps_empty neg_bound_cps_single neg_bound_cps_bind neg_bound_cps_plus neg_bound_cps_if neg_bound_cps_not
bulwahn@45450
   586
  Unknown Known Unknown_value Value No_value
bulwahn@45450
   587
bulwahn@45450
   588
end