src/HOL/Quickcheck.thy
author bulwahn
Tue Jul 17 10:39:39 2012 +0200 (2012-07-17)
changeset 48273 65233084e9d7
parent 46976 80123a220219
child 48891 c0eafbd55de3
permissions -rw-r--r--
improved equality optimisation in Quickcheck
bulwahn@41922
     1
(* Author: Florian Haftmann & Lukas Bulwahn, TU Muenchen *)
haftmann@26265
     2
bulwahn@41922
     3
header {* A simple counterexample generator performing random testing *}
haftmann@26265
     4
haftmann@26265
     5
theory Quickcheck
bulwahn@40650
     6
imports Random Code_Evaluation Enum
bulwahn@41928
     7
uses
bulwahn@45718
     8
  ("Tools/Quickcheck/quickcheck_common.ML")
bulwahn@41928
     9
  ("Tools/Quickcheck/random_generators.ML")
haftmann@26265
    10
begin
haftmann@26265
    11
haftmann@37751
    12
notation fcomp (infixl "\<circ>>" 60)
haftmann@37751
    13
notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@31179
    14
bulwahn@45718
    15
setup {* Code_Target.extend_target ("Quickcheck", (Code_Runtime.target, K I)) *}
bulwahn@45718
    16
bulwahn@45718
    17
subsection {* Catching Match exceptions *}
bulwahn@45718
    18
bulwahn@45801
    19
axiomatization catch_match :: "'a => 'a => 'a"
bulwahn@45718
    20
bulwahn@45718
    21
code_const catch_match 
bulwahn@48273
    22
  (Quickcheck "((_) handle Match => _)")
haftmann@31179
    23
haftmann@26265
    24
subsection {* The @{text random} class *}
haftmann@26265
    25
haftmann@28335
    26
class random = typerep +
haftmann@31205
    27
  fixes random :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
haftmann@26265
    28
haftmann@26267
    29
haftmann@31260
    30
subsection {* Fundamental and numeric types*}
haftmann@31179
    31
haftmann@31179
    32
instantiation bool :: random
haftmann@31179
    33
begin
haftmann@31179
    34
haftmann@31179
    35
definition
haftmann@37751
    36
  "random i = Random.range 2 \<circ>\<rightarrow>
haftmann@32657
    37
    (\<lambda>k. Pair (if k = 0 then Code_Evaluation.valtermify False else Code_Evaluation.valtermify True))"
haftmann@31179
    38
haftmann@31179
    39
instance ..
haftmann@31179
    40
haftmann@31179
    41
end
haftmann@31179
    42
haftmann@31179
    43
instantiation itself :: (typerep) random
haftmann@31179
    44
begin
haftmann@31179
    45
wenzelm@46975
    46
definition
wenzelm@46975
    47
  random_itself :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('a itself \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
wenzelm@46975
    48
where "random_itself _ = Pair (Code_Evaluation.valtermify TYPE('a))"
haftmann@31179
    49
haftmann@31179
    50
instance ..
haftmann@31179
    51
haftmann@31179
    52
end
haftmann@31179
    53
haftmann@31483
    54
instantiation char :: random
haftmann@31483
    55
begin
haftmann@31483
    56
haftmann@31483
    57
definition
haftmann@37751
    58
  "random _ = Random.select chars \<circ>\<rightarrow> (\<lambda>c. Pair (c, \<lambda>u. Code_Evaluation.term_of c))"
haftmann@31483
    59
haftmann@31483
    60
instance ..
haftmann@31483
    61
haftmann@31483
    62
end
haftmann@31483
    63
haftmann@31483
    64
instantiation String.literal :: random
haftmann@31483
    65
begin
haftmann@31483
    66
haftmann@31483
    67
definition 
haftmann@32657
    68
  "random _ = Pair (STR '''', \<lambda>u. Code_Evaluation.term_of (STR ''''))"
haftmann@31483
    69
haftmann@31483
    70
instance ..
haftmann@31483
    71
haftmann@31483
    72
end
haftmann@31483
    73
haftmann@31179
    74
instantiation nat :: random
haftmann@31179
    75
begin
haftmann@31179
    76
wenzelm@46975
    77
definition random_nat :: "code_numeral \<Rightarrow> Random.seed
wenzelm@46975
    78
  \<Rightarrow> (nat \<times> (unit \<Rightarrow> Code_Evaluation.term)) \<times> Random.seed"
wenzelm@46975
    79
where
haftmann@37751
    80
  "random_nat i = Random.range (i + 1) \<circ>\<rightarrow> (\<lambda>k. Pair (
haftmann@31205
    81
     let n = Code_Numeral.nat_of k
haftmann@32657
    82
     in (n, \<lambda>_. Code_Evaluation.term_of n)))"
haftmann@31194
    83
haftmann@31194
    84
instance ..
haftmann@31194
    85
haftmann@31194
    86
end
haftmann@31194
    87
haftmann@31194
    88
instantiation int :: random
haftmann@31194
    89
begin
haftmann@31194
    90
haftmann@31194
    91
definition
haftmann@37751
    92
  "random i = Random.range (2 * i + 1) \<circ>\<rightarrow> (\<lambda>k. Pair (
haftmann@31205
    93
     let j = (if k \<ge> i then Code_Numeral.int_of (k - i) else - Code_Numeral.int_of (i - k))
haftmann@32657
    94
     in (j, \<lambda>_. Code_Evaluation.term_of j)))"
haftmann@31179
    95
haftmann@31179
    96
instance ..
haftmann@31179
    97
haftmann@30945
    98
end
haftmann@31179
    99
haftmann@31260
   100
haftmann@31260
   101
subsection {* Complex generators *}
haftmann@31260
   102
haftmann@31603
   103
text {* Towards @{typ "'a \<Rightarrow> 'b"} *}
haftmann@31603
   104
haftmann@31603
   105
axiomatization random_fun_aux :: "typerep \<Rightarrow> typerep \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> term)
wenzelm@46975
   106
  \<Rightarrow> (Random.seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> Random.seed)
wenzelm@46975
   107
  \<Rightarrow> (Random.seed \<Rightarrow> Random.seed \<times> Random.seed)
haftmann@31603
   108
  \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
haftmann@31603
   109
haftmann@31622
   110
definition random_fun_lift :: "(Random.seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> Random.seed)
wenzelm@46975
   111
  \<Rightarrow> Random.seed \<Rightarrow> (('a\<Colon>term_of \<Rightarrow> 'b\<Colon>typerep) \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
wenzelm@46975
   112
where
wenzelm@46975
   113
  "random_fun_lift f =
wenzelm@46975
   114
    random_fun_aux TYPEREP('a) TYPEREP('b) (op =) Code_Evaluation.term_of f Random.split_seed"
haftmann@31603
   115
haftmann@38857
   116
instantiation "fun" :: ("{equal, term_of}", random) random
haftmann@31603
   117
begin
haftmann@31603
   118
wenzelm@46975
   119
definition
wenzelm@46975
   120
  random_fun :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
wenzelm@46975
   121
  where "random i = random_fun_lift (random i)"
haftmann@31603
   122
haftmann@31603
   123
instance ..
haftmann@31603
   124
haftmann@31603
   125
end
haftmann@31603
   126
haftmann@31603
   127
text {* Towards type copies and datatypes *}
haftmann@31603
   128
wenzelm@46975
   129
definition collapse :: "('a \<Rightarrow> ('a \<Rightarrow> 'b \<times> 'a) \<times> 'a) \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a"
wenzelm@46975
   130
  where "collapse f = (f \<circ>\<rightarrow> id)"
haftmann@31223
   131
wenzelm@46975
   132
definition beyond :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
wenzelm@46975
   133
  where "beyond k l = (if l > k then l else 0)"
haftmann@31260
   134
wenzelm@46975
   135
lemma beyond_zero: "beyond k 0 = 0"
haftmann@31267
   136
  by (simp add: beyond_def)
haftmann@31267
   137
bulwahn@46311
   138
wenzelm@46975
   139
definition (in term_syntax) [code_unfold]:
wenzelm@46975
   140
  "valterm_emptyset = Code_Evaluation.valtermify ({} :: ('a :: typerep) set)"
wenzelm@46975
   141
wenzelm@46975
   142
definition (in term_syntax) [code_unfold]:
wenzelm@46975
   143
  "valtermify_insert x s = Code_Evaluation.valtermify insert {\<cdot>} (x :: ('a :: typerep * _)) {\<cdot>} s"
bulwahn@46311
   144
bulwahn@46311
   145
instantiation set :: (random) random
bulwahn@46311
   146
begin
bulwahn@46311
   147
bulwahn@46311
   148
primrec random_aux_set
bulwahn@46311
   149
where
bulwahn@46311
   150
  "random_aux_set 0 j = collapse (Random.select_weight [(1, Pair valterm_emptyset)])"
wenzelm@46975
   151
| "random_aux_set (Code_Numeral.Suc i) j =
wenzelm@46975
   152
    collapse (Random.select_weight
wenzelm@46975
   153
      [(1, Pair valterm_emptyset),
wenzelm@46975
   154
       (Code_Numeral.Suc i,
wenzelm@46975
   155
        random j \<circ>\<rightarrow> (%x. random_aux_set i j \<circ>\<rightarrow> (%s. Pair (valtermify_insert x s))))])"
bulwahn@46311
   156
bulwahn@46311
   157
lemma [code]:
wenzelm@46975
   158
  "random_aux_set i j =
wenzelm@46975
   159
    collapse (Random.select_weight [(1, Pair valterm_emptyset),
wenzelm@46975
   160
      (i, random j \<circ>\<rightarrow> (%x. random_aux_set (i - 1) j \<circ>\<rightarrow> (%s. Pair (valtermify_insert x s))))])"
bulwahn@46311
   161
proof (induct i rule: code_numeral.induct)
bulwahn@46311
   162
  case zero
bulwahn@46311
   163
  show ?case by (subst select_weight_drop_zero[symmetric])
bulwahn@46311
   164
    (simp add: filter.simps random_aux_set.simps[simplified])
bulwahn@46311
   165
next
huffman@46547
   166
  case (Suc i)
bulwahn@46311
   167
  show ?case by (simp only: random_aux_set.simps(2)[of "i"] Suc_code_numeral_minus_one)
bulwahn@46311
   168
qed
bulwahn@46311
   169
wenzelm@46975
   170
definition "random_set i = random_aux_set i i"
bulwahn@46311
   171
bulwahn@46311
   172
instance ..
bulwahn@46311
   173
bulwahn@46311
   174
end
bulwahn@46311
   175
haftmann@31483
   176
lemma random_aux_rec:
haftmann@31483
   177
  fixes random_aux :: "code_numeral \<Rightarrow> 'a"
haftmann@31483
   178
  assumes "random_aux 0 = rhs 0"
huffman@46547
   179
    and "\<And>k. random_aux (Code_Numeral.Suc k) = rhs (Code_Numeral.Suc k)"
haftmann@31483
   180
  shows "random_aux k = rhs k"
haftmann@31483
   181
  using assms by (rule code_numeral.induct)
haftmann@31483
   182
bulwahn@45718
   183
subsection {* Deriving random generators for datatypes *}
bulwahn@45718
   184
bulwahn@45718
   185
use "Tools/Quickcheck/quickcheck_common.ML" 
bulwahn@41922
   186
use "Tools/Quickcheck/random_generators.ML"
bulwahn@41923
   187
setup Random_Generators.setup
haftmann@34968
   188
haftmann@34968
   189
haftmann@34968
   190
subsection {* Code setup *}
blanchet@33561
   191
bulwahn@41935
   192
code_const random_fun_aux (Quickcheck "Random'_Generators.random'_fun")
haftmann@34968
   193
  -- {* With enough criminal energy this can be abused to derive @{prop False};
haftmann@34968
   194
  for this reason we use a distinguished target @{text Quickcheck}
haftmann@34968
   195
  not spoiling the regular trusted code generation *}
haftmann@34968
   196
bulwahn@41935
   197
code_reserved Quickcheck Random_Generators
haftmann@34968
   198
haftmann@37751
   199
no_notation fcomp (infixl "\<circ>>" 60)
haftmann@37751
   200
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@34968
   201
haftmann@34968
   202
subsection {* The Random-Predicate Monad *} 
bulwahn@33250
   203
bulwahn@35880
   204
fun iter' ::
wenzelm@46975
   205
  "'a itself => code_numeral => code_numeral => code_numeral * code_numeral
wenzelm@46975
   206
    => ('a::random) Predicate.pred"
bulwahn@35880
   207
where
bulwahn@35880
   208
  "iter' T nrandom sz seed = (if nrandom = 0 then bot_class.bot else
bulwahn@35880
   209
     let ((x, _), seed') = random sz seed
bulwahn@35880
   210
   in Predicate.Seq (%u. Predicate.Insert x (iter' T (nrandom - 1) sz seed')))"
bulwahn@35880
   211
wenzelm@46975
   212
definition iter :: "code_numeral => code_numeral => code_numeral * code_numeral
wenzelm@46975
   213
  => ('a::random) Predicate.pred"
bulwahn@35880
   214
where
bulwahn@35880
   215
  "iter nrandom sz seed = iter' (TYPE('a)) nrandom sz seed"
bulwahn@35880
   216
bulwahn@35880
   217
lemma [code]:
bulwahn@35880
   218
  "iter nrandom sz seed = (if nrandom = 0 then bot_class.bot else
bulwahn@35880
   219
     let ((x, _), seed') = random sz seed
bulwahn@35880
   220
   in Predicate.Seq (%u. Predicate.Insert x (iter (nrandom - 1) sz seed')))"
bulwahn@35880
   221
unfolding iter_def iter'.simps[of _ nrandom] ..
bulwahn@35880
   222
bulwahn@42163
   223
type_synonym 'a randompred = "Random.seed \<Rightarrow> ('a Predicate.pred \<times> Random.seed)"
bulwahn@33250
   224
bulwahn@33250
   225
definition empty :: "'a randompred"
bulwahn@33250
   226
  where "empty = Pair (bot_class.bot)"
bulwahn@33250
   227
bulwahn@33250
   228
definition single :: "'a => 'a randompred"
bulwahn@33250
   229
  where "single x = Pair (Predicate.single x)"
bulwahn@33250
   230
bulwahn@33250
   231
definition bind :: "'a randompred \<Rightarrow> ('a \<Rightarrow> 'b randompred) \<Rightarrow> 'b randompred"
bulwahn@33250
   232
  where
bulwahn@33250
   233
    "bind R f = (\<lambda>s. let
bulwahn@33250
   234
       (P, s') = R s;
bulwahn@33250
   235
       (s1, s2) = Random.split_seed s'
bulwahn@33250
   236
     in (Predicate.bind P (%a. fst (f a s1)), s2))"
bulwahn@33250
   237
bulwahn@33250
   238
definition union :: "'a randompred \<Rightarrow> 'a randompred \<Rightarrow> 'a randompred"
bulwahn@33250
   239
where
bulwahn@33250
   240
  "union R1 R2 = (\<lambda>s. let
bulwahn@33250
   241
     (P1, s') = R1 s; (P2, s'') = R2 s'
krauss@44845
   242
   in (sup_class.sup P1 P2, s''))"
bulwahn@33250
   243
bulwahn@33250
   244
definition if_randompred :: "bool \<Rightarrow> unit randompred"
bulwahn@33250
   245
where
bulwahn@33250
   246
  "if_randompred b = (if b then single () else empty)"
bulwahn@33250
   247
bulwahn@36049
   248
definition iterate_upto :: "(code_numeral => 'a) => code_numeral => code_numeral => 'a randompred"
bulwahn@36049
   249
where
haftmann@46664
   250
  "iterate_upto f n m = Pair (Predicate.iterate_upto f n m)"
bulwahn@36049
   251
bulwahn@33250
   252
definition not_randompred :: "unit randompred \<Rightarrow> unit randompred"
bulwahn@33250
   253
where
bulwahn@33250
   254
  "not_randompred P = (\<lambda>s. let
bulwahn@33250
   255
     (P', s') = P s
bulwahn@33250
   256
   in if Predicate.eval P' () then (Orderings.bot, s') else (Predicate.single (), s'))"
bulwahn@33250
   257
bulwahn@33250
   258
definition Random :: "(Random.seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> Random.seed) \<Rightarrow> 'a randompred"
bulwahn@33250
   259
  where "Random g = scomp g (Pair o (Predicate.single o fst))"
bulwahn@33250
   260
bulwahn@33250
   261
definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a randompred \<Rightarrow> 'b randompred)"
bulwahn@33250
   262
  where "map f P = bind P (single o f)"
bulwahn@33250
   263
bulwahn@45801
   264
hide_fact
wenzelm@46976
   265
  random_bool_def
wenzelm@46976
   266
  random_itself_def
wenzelm@46976
   267
  random_char_def
wenzelm@46976
   268
  random_literal_def
wenzelm@46976
   269
  random_nat_def
wenzelm@46976
   270
  random_int_def
wenzelm@46976
   271
  random_fun_lift_def
wenzelm@46976
   272
  random_fun_def
wenzelm@46976
   273
  collapse_def
wenzelm@46976
   274
  beyond_def
wenzelm@46976
   275
  beyond_zero
bulwahn@45801
   276
  random_aux_rec
bulwahn@45801
   277
bulwahn@45801
   278
hide_const (open) catch_match random collapse beyond random_fun_aux random_fun_lift
bulwahn@45801
   279
wenzelm@46975
   280
hide_fact (open) iter'.simps iter_def empty_def single_def bind_def union_def
wenzelm@46975
   281
  if_randompred_def iterate_upto_def not_randompred_def Random_def map_def 
wenzelm@36176
   282
hide_type (open) randompred
wenzelm@46975
   283
hide_const (open) iter' iter empty single bind union if_randompred
wenzelm@46975
   284
  iterate_upto not_randompred Random map
haftmann@31267
   285
haftmann@31179
   286
end