src/HOL/Smallcheck.thy
author bulwahn
Fri Dec 10 11:42:04 2010 +0100 (2010-12-10)
changeset 41104 013adf7ebd96
parent 41085 a549ff1d4070
child 41105 a76ee71c3313
permissions -rw-r--r--
removing unneccassary sort constraints
bulwahn@40420
     1
(* Author: Lukas Bulwahn, TU Muenchen *)
bulwahn@40420
     2
bulwahn@40420
     3
header {* Another simple counterexample generator *}
bulwahn@40420
     4
bulwahn@40420
     5
theory Smallcheck
bulwahn@40420
     6
imports Quickcheck
bulwahn@40420
     7
uses ("Tools/smallvalue_generators.ML")
bulwahn@40420
     8
begin
bulwahn@40420
     9
bulwahn@40420
    10
huffman@40620
    11
subsection {* small value generator type classes *}
bulwahn@40420
    12
bulwahn@40420
    13
class small = term_of +
bulwahn@40420
    14
fixes small :: "('a \<Rightarrow> term list option) \<Rightarrow> code_numeral \<Rightarrow> term list option"
bulwahn@40420
    15
bulwahn@40420
    16
instantiation unit :: small
bulwahn@40420
    17
begin
bulwahn@40420
    18
bulwahn@40639
    19
definition "small f d = f ()"
bulwahn@40420
    20
bulwahn@40420
    21
instance ..
bulwahn@40420
    22
bulwahn@40420
    23
end
bulwahn@40420
    24
bulwahn@40420
    25
instantiation int :: small
bulwahn@40420
    26
begin
bulwahn@40420
    27
bulwahn@40420
    28
function small' :: "(int => term list option) => int => int => term list option"
bulwahn@40420
    29
where "small' f d i = (if d < i then None else (case f i of Some t => Some t | None => small' f d (i + 1)))"
bulwahn@40420
    30
by pat_completeness auto
bulwahn@40420
    31
bulwahn@40420
    32
termination 
bulwahn@40420
    33
  by (relation "measure (%(_, d, i). nat (d + 1 - i))") auto
bulwahn@40420
    34
bulwahn@40420
    35
definition "small f d = small' f (Code_Numeral.int_of d) (- (Code_Numeral.int_of d))"
bulwahn@40420
    36
bulwahn@40420
    37
instance ..
bulwahn@40420
    38
bulwahn@40420
    39
end
bulwahn@40420
    40
bulwahn@40420
    41
instantiation prod :: (small, small) small
bulwahn@40420
    42
begin
bulwahn@40420
    43
bulwahn@40420
    44
definition
bulwahn@40420
    45
  "small f d = small (%x. small (%y. f (x, y)) d) d"
bulwahn@40420
    46
bulwahn@40420
    47
instance ..
bulwahn@40420
    48
bulwahn@40420
    49
end
bulwahn@40420
    50
bulwahn@40899
    51
subsection {* full small value generator type classes *}
bulwahn@40639
    52
bulwahn@40639
    53
class full_small = term_of +
bulwahn@40639
    54
fixes full_small :: "('a * (unit => term) \<Rightarrow> term list option) \<Rightarrow> code_numeral \<Rightarrow> term list option"
bulwahn@40639
    55
bulwahn@40639
    56
instantiation unit :: full_small
bulwahn@40639
    57
begin
bulwahn@40639
    58
bulwahn@40639
    59
definition "full_small f d = f (Code_Evaluation.valtermify ())"
bulwahn@40639
    60
bulwahn@40639
    61
instance ..
bulwahn@40639
    62
bulwahn@40639
    63
end
bulwahn@40639
    64
bulwahn@40639
    65
instantiation int :: full_small
bulwahn@40639
    66
begin
bulwahn@40639
    67
bulwahn@40639
    68
function full_small' :: "(int * (unit => term) => term list option) => int => int => term list option"
bulwahn@40639
    69
  where "full_small' f d i = (if d < i then None else (case f (i, %_. Code_Evaluation.term_of i) of Some t => Some t | None => full_small' f d (i + 1)))"
bulwahn@40639
    70
by pat_completeness auto
bulwahn@40639
    71
bulwahn@40639
    72
termination 
bulwahn@40639
    73
  by (relation "measure (%(_, d, i). nat (d + 1 - i))") auto
bulwahn@40639
    74
bulwahn@40639
    75
definition "full_small f d = full_small' f (Code_Numeral.int_of d) (- (Code_Numeral.int_of d))"
bulwahn@40639
    76
bulwahn@40639
    77
instance ..
bulwahn@40639
    78
bulwahn@40639
    79
end
bulwahn@40639
    80
bulwahn@40639
    81
instantiation prod :: (full_small, full_small) full_small
bulwahn@40639
    82
begin
bulwahn@40899
    83
bulwahn@40639
    84
definition
bulwahn@40639
    85
  "full_small f d = full_small (%(x, t1). full_small (%(y, t2). f ((x, y),
bulwahn@40639
    86
    %u. Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.term_of (Pair :: 'a => 'b => ('a * 'b))) (t1 ())) (t2 ()))) d) d"
bulwahn@40639
    87
bulwahn@40639
    88
instance ..
bulwahn@40639
    89
bulwahn@40639
    90
end
bulwahn@40639
    91
bulwahn@40639
    92
instantiation "fun" :: ("{equal, full_small}", full_small) full_small
bulwahn@40639
    93
begin
bulwahn@40639
    94
bulwahn@40639
    95
fun full_small_fun' :: "(('a => 'b) * (unit => term) => term list option) => code_numeral => code_numeral => term list option"
bulwahn@40639
    96
where
bulwahn@40639
    97
  "full_small_fun' f i d = (if i > 1 then
bulwahn@40639
    98
    full_small (%(a, at). full_small (%(b, bt).
bulwahn@40639
    99
      full_small_fun' (%(g, gt). f (g(a := b),
bulwahn@40899
   100
        (%_. let T1 = (Typerep.typerep (TYPE('a)));
bulwahn@40899
   101
                 T2 = (Typerep.typerep (TYPE('b)))
bulwahn@40899
   102
             in
bulwahn@40899
   103
               Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.App
bulwahn@40899
   104
                 (Code_Evaluation.Const (STR ''Fun.fun_upd'')
bulwahn@40899
   105
                    (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''fun'') [T1, T2],
bulwahn@40899
   106
                       Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''fun'') [T1, T2]]]]))
bulwahn@40899
   107
               (gt ())) (at ())) (bt ())))) (i - 1) d) d) d
bulwahn@40899
   108
  else (if i > 0 then
bulwahn@40899
   109
    full_small (%(b, t). f (%_. b, %_. Code_Evaluation.Abs (STR ''x'') (Typerep.typerep TYPE('a)) (t ()))) d else None))"
bulwahn@40639
   110
bulwahn@40639
   111
definition full_small_fun :: "(('a => 'b) * (unit => term) => term list option) => code_numeral => term list option"
bulwahn@40639
   112
where
bulwahn@40639
   113
  "full_small_fun f d = full_small_fun' f d d" 
bulwahn@40639
   114
bulwahn@40639
   115
bulwahn@40639
   116
instance ..
bulwahn@40639
   117
bulwahn@40639
   118
end
bulwahn@40639
   119
bulwahn@41085
   120
subsubsection {* A smarter enumeration scheme for functions over finite datatypes *}
bulwahn@41085
   121
bulwahn@41085
   122
bulwahn@41085
   123
class check_all = enum + term_of +
bulwahn@41085
   124
fixes check_all :: "('a * (unit \<Rightarrow> term) \<Rightarrow> term list option) \<Rightarrow> term list option"
bulwahn@41085
   125
bulwahn@41085
   126
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
   127
where
bulwahn@41085
   128
  "check_all_n_lists f n =
bulwahn@41085
   129
     (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
   130
bulwahn@41104
   131
instantiation "fun" :: ("{equal, check_all}", check_all) check_all
bulwahn@41085
   132
begin
bulwahn@41085
   133
bulwahn@41085
   134
definition mk_map_term :: "'a list \<Rightarrow> (unit \<Rightarrow> term list) \<Rightarrow> (unit \<Rightarrow> typerep) \<Rightarrow> unit \<Rightarrow> term"
bulwahn@41085
   135
where
bulwahn@41085
   136
  "mk_map_term domm rng T2 =
bulwahn@41085
   137
     (%_. let T1 = (Typerep.typerep (TYPE('a)));
bulwahn@41085
   138
              T2 = T2 ();
bulwahn@41085
   139
              update_term = (%g (a, b).
bulwahn@41085
   140
                Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.App
bulwahn@41085
   141
                 (Code_Evaluation.Const (STR ''Fun.fun_upd'')
bulwahn@41085
   142
                   (Typerep.Typerep (STR ''fun'') [Typerep.Typerep (STR ''fun'') [T1, T2],
bulwahn@41085
   143
                      Typerep.Typerep (STR ''fun'') [T1, Typerep.Typerep (STR ''fun'') [T2, Typerep.Typerep (STR ''fun'') [T1, T2]]]])) g) (Code_Evaluation.term_of a)) b)
bulwahn@41085
   144
          in
bulwahn@41085
   145
             List.foldl update_term (Code_Evaluation.Abs (STR ''x'') T1 (Code_Evaluation.Const (STR ''HOL.undefined'') T2)) (zip domm (rng ())))"
bulwahn@41085
   146
bulwahn@41085
   147
definition
bulwahn@41085
   148
  "check_all f = check_all_n_lists (\<lambda>(ys, yst). f (the o map_of (zip (Enum.enum\<Colon>'a list) ys), mk_map_term (Enum.enum::'a list) yst (%_. Typerep.typerep (TYPE('b))))) (Code_Numeral.of_nat (length (Enum.enum :: 'a list)))"
bulwahn@41085
   149
bulwahn@41085
   150
instance ..
bulwahn@41085
   151
bulwahn@41085
   152
end
bulwahn@41085
   153
bulwahn@41085
   154
instantiation bool :: check_all
bulwahn@41085
   155
begin
bulwahn@41085
   156
bulwahn@41085
   157
definition
bulwahn@41085
   158
  "check_all f = (case f (Code_Evaluation.valtermify False) of Some x' \<Rightarrow> Some x' | None \<Rightarrow> f (Code_Evaluation.valtermify True))"
bulwahn@41085
   159
bulwahn@41085
   160
instance ..
bulwahn@41085
   161
bulwahn@41085
   162
end
bulwahn@41085
   163
bulwahn@41085
   164
instantiation prod :: (check_all, check_all) check_all
bulwahn@41085
   165
begin
bulwahn@41085
   166
bulwahn@41085
   167
definition
bulwahn@41085
   168
  "check_all f = check_all (%(x, t1). check_all (%(y, t2). f ((x, y), %_. Code_Evaluation.App (Code_Evaluation.App (Code_Evaluation.term_of (Pair :: 'a => 'b => ('a * 'b))) (t1 ())) (t2 ()))))"
bulwahn@41085
   169
bulwahn@41085
   170
instance ..
bulwahn@41085
   171
bulwahn@41085
   172
end
bulwahn@41085
   173
bulwahn@41085
   174
instantiation Enum.finite_1 :: check_all
bulwahn@41085
   175
begin
bulwahn@41085
   176
bulwahn@41085
   177
definition
bulwahn@41085
   178
  "check_all f = f (Code_Evaluation.valtermify Enum.finite_1.a\<^isub>1)"
bulwahn@41085
   179
bulwahn@41085
   180
instance ..
bulwahn@41085
   181
bulwahn@41085
   182
end
bulwahn@41085
   183
bulwahn@41085
   184
instantiation Enum.finite_2 :: check_all
bulwahn@41085
   185
begin
bulwahn@41085
   186
bulwahn@41085
   187
definition
bulwahn@41085
   188
  "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
   189
bulwahn@41085
   190
instance ..
bulwahn@41085
   191
bulwahn@41085
   192
end
bulwahn@41085
   193
bulwahn@41085
   194
instantiation Enum.finite_3 :: check_all
bulwahn@41085
   195
begin
bulwahn@41085
   196
bulwahn@41085
   197
definition
bulwahn@41085
   198
  "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
   199
bulwahn@41085
   200
instance ..
bulwahn@41085
   201
bulwahn@41085
   202
end
bulwahn@41085
   203
bulwahn@41085
   204
bulwahn@41085
   205
huffman@40620
   206
subsection {* Defining combinators for any first-order data type *}
bulwahn@40420
   207
bulwahn@40899
   208
definition orelse :: "'a option => 'a option => 'a option"
bulwahn@40899
   209
where
bulwahn@40899
   210
  [code_unfold]: "orelse x y = (case x of Some x' => Some x' | None => y)"
bulwahn@40899
   211
bulwahn@40899
   212
bulwahn@40420
   213
definition catch_match :: "term list option => term list option => term list option"
bulwahn@40420
   214
where
bulwahn@40420
   215
  [code del]: "catch_match t1 t2 = (SOME t. t = t1 \<or> t = t2)"
bulwahn@40420
   216
bulwahn@40420
   217
code_const catch_match 
bulwahn@40420
   218
  (SML "(_) handle Match => _")
bulwahn@40420
   219
bulwahn@40420
   220
use "Tools/smallvalue_generators.ML"
bulwahn@40420
   221
bulwahn@40420
   222
setup {* Smallvalue_Generators.setup *}
bulwahn@40420
   223
bulwahn@40915
   224
declare [[quickcheck_tester = exhaustive]]
bulwahn@40915
   225
bulwahn@40899
   226
hide_fact orelse_def catch_match_def
bulwahn@41085
   227
hide_const (open) orelse catch_match mk_map_term check_all_n_lists
bulwahn@40420
   228
bulwahn@40420
   229
end