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