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