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