src/HOL/Quickcheck.thy
author bulwahn
Thu Dec 01 22:14:35 2011 +0100 (2011-12-01)
changeset 45718 8979b2463fc8
parent 45178 fe9993491317
child 45721 d1fb55c2ed65
permissions -rw-r--r--
quickcheck random can also find potential counterexamples;
moved catch_match definition;
split quickcheck setup;
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@45718
    19
definition catch_match :: "term list option => term list option => term list option"
bulwahn@45718
    20
where
bulwahn@45718
    21
  [code del]: "catch_match t1 t2 = (SOME t. t = t1 \<or> t = t2)"
bulwahn@45718
    22
bulwahn@45718
    23
code_const catch_match 
bulwahn@45718
    24
  (Quickcheck "(_) handle Match => _")
haftmann@31179
    25
haftmann@26265
    26
subsection {* The @{text random} class *}
haftmann@26265
    27
haftmann@28335
    28
class random = typerep +
haftmann@31205
    29
  fixes random :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
haftmann@26265
    30
haftmann@26267
    31
haftmann@31260
    32
subsection {* Fundamental and numeric types*}
haftmann@31179
    33
haftmann@31179
    34
instantiation bool :: random
haftmann@31179
    35
begin
haftmann@31179
    36
haftmann@31179
    37
definition
haftmann@37751
    38
  "random i = Random.range 2 \<circ>\<rightarrow>
haftmann@32657
    39
    (\<lambda>k. Pair (if k = 0 then Code_Evaluation.valtermify False else Code_Evaluation.valtermify True))"
haftmann@31179
    40
haftmann@31179
    41
instance ..
haftmann@31179
    42
haftmann@31179
    43
end
haftmann@31179
    44
haftmann@31179
    45
instantiation itself :: (typerep) random
haftmann@31179
    46
begin
haftmann@31179
    47
haftmann@31205
    48
definition random_itself :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('a itself \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
haftmann@32657
    49
  "random_itself _ = Pair (Code_Evaluation.valtermify TYPE('a))"
haftmann@31179
    50
haftmann@31179
    51
instance ..
haftmann@31179
    52
haftmann@31179
    53
end
haftmann@31179
    54
haftmann@31483
    55
instantiation char :: random
haftmann@31483
    56
begin
haftmann@31483
    57
haftmann@31483
    58
definition
haftmann@37751
    59
  "random _ = Random.select chars \<circ>\<rightarrow> (\<lambda>c. Pair (c, \<lambda>u. Code_Evaluation.term_of c))"
haftmann@31483
    60
haftmann@31483
    61
instance ..
haftmann@31483
    62
haftmann@31483
    63
end
haftmann@31483
    64
haftmann@31483
    65
instantiation String.literal :: random
haftmann@31483
    66
begin
haftmann@31483
    67
haftmann@31483
    68
definition 
haftmann@32657
    69
  "random _ = Pair (STR '''', \<lambda>u. Code_Evaluation.term_of (STR ''''))"
haftmann@31483
    70
haftmann@31483
    71
instance ..
haftmann@31483
    72
haftmann@31483
    73
end
haftmann@31483
    74
haftmann@31179
    75
instantiation nat :: random
haftmann@31179
    76
begin
haftmann@31179
    77
haftmann@32657
    78
definition random_nat :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> (nat \<times> (unit \<Rightarrow> Code_Evaluation.term)) \<times> Random.seed" where
haftmann@37751
    79
  "random_nat i = Random.range (i + 1) \<circ>\<rightarrow> (\<lambda>k. Pair (
haftmann@31205
    80
     let n = Code_Numeral.nat_of k
haftmann@32657
    81
     in (n, \<lambda>_. Code_Evaluation.term_of n)))"
haftmann@31194
    82
haftmann@31194
    83
instance ..
haftmann@31194
    84
haftmann@31194
    85
end
haftmann@31194
    86
haftmann@31194
    87
instantiation int :: random
haftmann@31194
    88
begin
haftmann@31194
    89
haftmann@31194
    90
definition
haftmann@37751
    91
  "random i = Random.range (2 * i + 1) \<circ>\<rightarrow> (\<lambda>k. Pair (
haftmann@31205
    92
     let j = (if k \<ge> i then Code_Numeral.int_of (k - i) else - Code_Numeral.int_of (i - k))
haftmann@32657
    93
     in (j, \<lambda>_. Code_Evaluation.term_of j)))"
haftmann@31179
    94
haftmann@31179
    95
instance ..
haftmann@31179
    96
haftmann@30945
    97
end
haftmann@31179
    98
haftmann@31260
    99
haftmann@31260
   100
subsection {* Complex generators *}
haftmann@31260
   101
haftmann@31603
   102
text {* Towards @{typ "'a \<Rightarrow> 'b"} *}
haftmann@31603
   103
haftmann@31603
   104
axiomatization random_fun_aux :: "typerep \<Rightarrow> typerep \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> term)
haftmann@31603
   105
  \<Rightarrow> (Random.seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> Random.seed) \<Rightarrow> (Random.seed \<Rightarrow> Random.seed \<times> Random.seed)
haftmann@31603
   106
  \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
haftmann@31603
   107
haftmann@31622
   108
definition random_fun_lift :: "(Random.seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> Random.seed)
haftmann@31622
   109
  \<Rightarrow> Random.seed \<Rightarrow> (('a\<Colon>term_of \<Rightarrow> 'b\<Colon>typerep) \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
haftmann@32657
   110
  "random_fun_lift f = random_fun_aux TYPEREP('a) TYPEREP('b) (op =) Code_Evaluation.term_of f Random.split_seed"
haftmann@31603
   111
haftmann@38857
   112
instantiation "fun" :: ("{equal, term_of}", random) random
haftmann@31603
   113
begin
haftmann@31603
   114
haftmann@31603
   115
definition random_fun :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
haftmann@31622
   116
  "random i = random_fun_lift (random i)"
haftmann@31603
   117
haftmann@31603
   118
instance ..
haftmann@31603
   119
haftmann@31603
   120
end
haftmann@31603
   121
haftmann@31603
   122
text {* Towards type copies and datatypes *}
haftmann@31603
   123
haftmann@31260
   124
definition collapse :: "('a \<Rightarrow> ('a \<Rightarrow> 'b \<times> 'a) \<times> 'a) \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a" where
haftmann@37751
   125
  "collapse f = (f \<circ>\<rightarrow> id)"
haftmann@31223
   126
haftmann@31260
   127
definition beyond :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral" where
haftmann@31260
   128
  "beyond k l = (if l > k then l else 0)"
haftmann@31260
   129
haftmann@31267
   130
lemma beyond_zero:
haftmann@31267
   131
  "beyond k 0 = 0"
haftmann@31267
   132
  by (simp add: beyond_def)
haftmann@31267
   133
haftmann@31483
   134
lemma random_aux_rec:
haftmann@31483
   135
  fixes random_aux :: "code_numeral \<Rightarrow> 'a"
haftmann@31483
   136
  assumes "random_aux 0 = rhs 0"
haftmann@31483
   137
    and "\<And>k. random_aux (Suc_code_numeral k) = rhs (Suc_code_numeral k)"
haftmann@31483
   138
  shows "random_aux k = rhs k"
haftmann@31483
   139
  using assms by (rule code_numeral.induct)
haftmann@31483
   140
bulwahn@45718
   141
subsection {* Deriving random generators for datatypes *}
bulwahn@45718
   142
bulwahn@45718
   143
use "Tools/Quickcheck/quickcheck_common.ML" 
bulwahn@41922
   144
use "Tools/Quickcheck/random_generators.ML"
bulwahn@41923
   145
setup Random_Generators.setup
haftmann@34968
   146
haftmann@34968
   147
haftmann@34968
   148
subsection {* Code setup *}
blanchet@33561
   149
bulwahn@41935
   150
code_const random_fun_aux (Quickcheck "Random'_Generators.random'_fun")
haftmann@34968
   151
  -- {* With enough criminal energy this can be abused to derive @{prop False};
haftmann@34968
   152
  for this reason we use a distinguished target @{text Quickcheck}
haftmann@34968
   153
  not spoiling the regular trusted code generation *}
haftmann@34968
   154
bulwahn@41935
   155
code_reserved Quickcheck Random_Generators
haftmann@34968
   156
haftmann@37751
   157
no_notation fcomp (infixl "\<circ>>" 60)
haftmann@37751
   158
no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
haftmann@34968
   159
haftmann@34968
   160
subsection {* The Random-Predicate Monad *} 
bulwahn@33250
   161
bulwahn@35880
   162
fun iter' ::
bulwahn@35880
   163
  "'a itself => code_numeral => code_numeral => code_numeral * code_numeral => ('a::random) Predicate.pred"
bulwahn@35880
   164
where
bulwahn@35880
   165
  "iter' T nrandom sz seed = (if nrandom = 0 then bot_class.bot else
bulwahn@35880
   166
     let ((x, _), seed') = random sz seed
bulwahn@35880
   167
   in Predicate.Seq (%u. Predicate.Insert x (iter' T (nrandom - 1) sz seed')))"
bulwahn@35880
   168
bulwahn@35880
   169
definition iter :: "code_numeral => code_numeral => code_numeral * code_numeral => ('a::random) Predicate.pred"
bulwahn@35880
   170
where
bulwahn@35880
   171
  "iter nrandom sz seed = iter' (TYPE('a)) nrandom sz seed"
bulwahn@35880
   172
bulwahn@35880
   173
lemma [code]:
bulwahn@35880
   174
  "iter nrandom sz seed = (if nrandom = 0 then bot_class.bot else
bulwahn@35880
   175
     let ((x, _), seed') = random sz seed
bulwahn@35880
   176
   in Predicate.Seq (%u. Predicate.Insert x (iter (nrandom - 1) sz seed')))"
bulwahn@35880
   177
unfolding iter_def iter'.simps[of _ nrandom] ..
bulwahn@35880
   178
bulwahn@42163
   179
type_synonym 'a randompred = "Random.seed \<Rightarrow> ('a Predicate.pred \<times> Random.seed)"
bulwahn@33250
   180
bulwahn@33250
   181
definition empty :: "'a randompred"
bulwahn@33250
   182
  where "empty = Pair (bot_class.bot)"
bulwahn@33250
   183
bulwahn@33250
   184
definition single :: "'a => 'a randompred"
bulwahn@33250
   185
  where "single x = Pair (Predicate.single x)"
bulwahn@33250
   186
bulwahn@33250
   187
definition bind :: "'a randompred \<Rightarrow> ('a \<Rightarrow> 'b randompred) \<Rightarrow> 'b randompred"
bulwahn@33250
   188
  where
bulwahn@33250
   189
    "bind R f = (\<lambda>s. let
bulwahn@33250
   190
       (P, s') = R s;
bulwahn@33250
   191
       (s1, s2) = Random.split_seed s'
bulwahn@33250
   192
     in (Predicate.bind P (%a. fst (f a s1)), s2))"
bulwahn@33250
   193
bulwahn@33250
   194
definition union :: "'a randompred \<Rightarrow> 'a randompred \<Rightarrow> 'a randompred"
bulwahn@33250
   195
where
bulwahn@33250
   196
  "union R1 R2 = (\<lambda>s. let
bulwahn@33250
   197
     (P1, s') = R1 s; (P2, s'') = R2 s'
krauss@44845
   198
   in (sup_class.sup P1 P2, s''))"
bulwahn@33250
   199
bulwahn@33250
   200
definition if_randompred :: "bool \<Rightarrow> unit randompred"
bulwahn@33250
   201
where
bulwahn@33250
   202
  "if_randompred b = (if b then single () else empty)"
bulwahn@33250
   203
bulwahn@36049
   204
definition iterate_upto :: "(code_numeral => 'a) => code_numeral => code_numeral => 'a randompred"
bulwahn@36049
   205
where
bulwahn@36049
   206
  "iterate_upto f n m = Pair (Code_Numeral.iterate_upto f n m)"
bulwahn@36049
   207
bulwahn@33250
   208
definition not_randompred :: "unit randompred \<Rightarrow> unit randompred"
bulwahn@33250
   209
where
bulwahn@33250
   210
  "not_randompred P = (\<lambda>s. let
bulwahn@33250
   211
     (P', s') = P s
bulwahn@33250
   212
   in if Predicate.eval P' () then (Orderings.bot, s') else (Predicate.single (), s'))"
bulwahn@33250
   213
bulwahn@33250
   214
definition Random :: "(Random.seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> Random.seed) \<Rightarrow> 'a randompred"
bulwahn@33250
   215
  where "Random g = scomp g (Pair o (Predicate.single o fst))"
bulwahn@33250
   216
bulwahn@33250
   217
definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a randompred \<Rightarrow> 'b randompred)"
bulwahn@33250
   218
  where "map f P = bind P (single o f)"
bulwahn@33250
   219
wenzelm@36176
   220
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
   221
hide_type (open) randompred
wenzelm@36176
   222
hide_const (open) random collapse beyond random_fun_aux random_fun_lift
bulwahn@36049
   223
  iter' iter empty single bind union if_randompred iterate_upto not_randompred Random map
haftmann@31267
   224
haftmann@31179
   225
end