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