src/HOL/Quickcheck.thy
author kuncar
Fri Dec 09 18:07:04 2011 +0100 (2011-12-09)
changeset 45802 b16f976db515
parent 45801 5616fbda86e6
child 46311 56fae81902ce
permissions -rw-r--r--
Quotient_Info stores only relation maps
     1 (* Author: Florian Haftmann & Lukas Bulwahn, TU Muenchen *)
     2 
     3 header {* A simple counterexample generator performing random testing *}
     4 
     5 theory Quickcheck
     6 imports Random Code_Evaluation Enum
     7 uses
     8   ("Tools/Quickcheck/quickcheck_common.ML")
     9   ("Tools/Quickcheck/random_generators.ML")
    10 begin
    11 
    12 notation fcomp (infixl "\<circ>>" 60)
    13 notation scomp (infixl "\<circ>\<rightarrow>" 60)
    14 
    15 setup {* Code_Target.extend_target ("Quickcheck", (Code_Runtime.target, K I)) *}
    16 
    17 subsection {* Catching Match exceptions *}
    18 
    19 axiomatization catch_match :: "'a => 'a => 'a"
    20 
    21 code_const catch_match 
    22   (Quickcheck "(_) handle Match => _")
    23 
    24 subsection {* The @{text random} class *}
    25 
    26 class random = typerep +
    27   fixes random :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
    28 
    29 
    30 subsection {* Fundamental and numeric types*}
    31 
    32 instantiation bool :: random
    33 begin
    34 
    35 definition
    36   "random i = Random.range 2 \<circ>\<rightarrow>
    37     (\<lambda>k. Pair (if k = 0 then Code_Evaluation.valtermify False else Code_Evaluation.valtermify True))"
    38 
    39 instance ..
    40 
    41 end
    42 
    43 instantiation itself :: (typerep) random
    44 begin
    45 
    46 definition random_itself :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('a itself \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
    47   "random_itself _ = Pair (Code_Evaluation.valtermify TYPE('a))"
    48 
    49 instance ..
    50 
    51 end
    52 
    53 instantiation char :: random
    54 begin
    55 
    56 definition
    57   "random _ = Random.select chars \<circ>\<rightarrow> (\<lambda>c. Pair (c, \<lambda>u. Code_Evaluation.term_of c))"
    58 
    59 instance ..
    60 
    61 end
    62 
    63 instantiation String.literal :: random
    64 begin
    65 
    66 definition 
    67   "random _ = Pair (STR '''', \<lambda>u. Code_Evaluation.term_of (STR ''''))"
    68 
    69 instance ..
    70 
    71 end
    72 
    73 instantiation nat :: random
    74 begin
    75 
    76 definition random_nat :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> (nat \<times> (unit \<Rightarrow> Code_Evaluation.term)) \<times> Random.seed" where
    77   "random_nat i = Random.range (i + 1) \<circ>\<rightarrow> (\<lambda>k. Pair (
    78      let n = Code_Numeral.nat_of k
    79      in (n, \<lambda>_. Code_Evaluation.term_of n)))"
    80 
    81 instance ..
    82 
    83 end
    84 
    85 instantiation int :: random
    86 begin
    87 
    88 definition
    89   "random i = Random.range (2 * i + 1) \<circ>\<rightarrow> (\<lambda>k. Pair (
    90      let j = (if k \<ge> i then Code_Numeral.int_of (k - i) else - Code_Numeral.int_of (i - k))
    91      in (j, \<lambda>_. Code_Evaluation.term_of j)))"
    92 
    93 instance ..
    94 
    95 end
    96 
    97 
    98 subsection {* Complex generators *}
    99 
   100 text {* Towards @{typ "'a \<Rightarrow> 'b"} *}
   101 
   102 axiomatization random_fun_aux :: "typerep \<Rightarrow> typerep \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> term)
   103   \<Rightarrow> (Random.seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> Random.seed) \<Rightarrow> (Random.seed \<Rightarrow> Random.seed \<times> Random.seed)
   104   \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
   105 
   106 definition random_fun_lift :: "(Random.seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> Random.seed)
   107   \<Rightarrow> Random.seed \<Rightarrow> (('a\<Colon>term_of \<Rightarrow> 'b\<Colon>typerep) \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
   108   "random_fun_lift f = random_fun_aux TYPEREP('a) TYPEREP('b) (op =) Code_Evaluation.term_of f Random.split_seed"
   109 
   110 instantiation "fun" :: ("{equal, term_of}", random) random
   111 begin
   112 
   113 definition random_fun :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed" where
   114   "random i = random_fun_lift (random i)"
   115 
   116 instance ..
   117 
   118 end
   119 
   120 text {* Towards type copies and datatypes *}
   121 
   122 definition collapse :: "('a \<Rightarrow> ('a \<Rightarrow> 'b \<times> 'a) \<times> 'a) \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a" where
   123   "collapse f = (f \<circ>\<rightarrow> id)"
   124 
   125 definition beyond :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral" where
   126   "beyond k l = (if l > k then l else 0)"
   127 
   128 lemma beyond_zero:
   129   "beyond k 0 = 0"
   130   by (simp add: beyond_def)
   131 
   132 lemma random_aux_rec:
   133   fixes random_aux :: "code_numeral \<Rightarrow> 'a"
   134   assumes "random_aux 0 = rhs 0"
   135     and "\<And>k. random_aux (Suc_code_numeral k) = rhs (Suc_code_numeral k)"
   136   shows "random_aux k = rhs k"
   137   using assms by (rule code_numeral.induct)
   138 
   139 subsection {* Deriving random generators for datatypes *}
   140 
   141 use "Tools/Quickcheck/quickcheck_common.ML" 
   142 use "Tools/Quickcheck/random_generators.ML"
   143 setup Random_Generators.setup
   144 
   145 
   146 subsection {* Code setup *}
   147 
   148 code_const random_fun_aux (Quickcheck "Random'_Generators.random'_fun")
   149   -- {* With enough criminal energy this can be abused to derive @{prop False};
   150   for this reason we use a distinguished target @{text Quickcheck}
   151   not spoiling the regular trusted code generation *}
   152 
   153 code_reserved Quickcheck Random_Generators
   154 
   155 no_notation fcomp (infixl "\<circ>>" 60)
   156 no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
   157 
   158 subsection {* The Random-Predicate Monad *} 
   159 
   160 fun iter' ::
   161   "'a itself => code_numeral => code_numeral => code_numeral * code_numeral => ('a::random) Predicate.pred"
   162 where
   163   "iter' T nrandom sz seed = (if nrandom = 0 then bot_class.bot else
   164      let ((x, _), seed') = random sz seed
   165    in Predicate.Seq (%u. Predicate.Insert x (iter' T (nrandom - 1) sz seed')))"
   166 
   167 definition iter :: "code_numeral => code_numeral => code_numeral * code_numeral => ('a::random) Predicate.pred"
   168 where
   169   "iter nrandom sz seed = iter' (TYPE('a)) nrandom sz seed"
   170 
   171 lemma [code]:
   172   "iter nrandom sz seed = (if nrandom = 0 then bot_class.bot else
   173      let ((x, _), seed') = random sz seed
   174    in Predicate.Seq (%u. Predicate.Insert x (iter (nrandom - 1) sz seed')))"
   175 unfolding iter_def iter'.simps[of _ nrandom] ..
   176 
   177 type_synonym 'a randompred = "Random.seed \<Rightarrow> ('a Predicate.pred \<times> Random.seed)"
   178 
   179 definition empty :: "'a randompred"
   180   where "empty = Pair (bot_class.bot)"
   181 
   182 definition single :: "'a => 'a randompred"
   183   where "single x = Pair (Predicate.single x)"
   184 
   185 definition bind :: "'a randompred \<Rightarrow> ('a \<Rightarrow> 'b randompred) \<Rightarrow> 'b randompred"
   186   where
   187     "bind R f = (\<lambda>s. let
   188        (P, s') = R s;
   189        (s1, s2) = Random.split_seed s'
   190      in (Predicate.bind P (%a. fst (f a s1)), s2))"
   191 
   192 definition union :: "'a randompred \<Rightarrow> 'a randompred \<Rightarrow> 'a randompred"
   193 where
   194   "union R1 R2 = (\<lambda>s. let
   195      (P1, s') = R1 s; (P2, s'') = R2 s'
   196    in (sup_class.sup P1 P2, s''))"
   197 
   198 definition if_randompred :: "bool \<Rightarrow> unit randompred"
   199 where
   200   "if_randompred b = (if b then single () else empty)"
   201 
   202 definition iterate_upto :: "(code_numeral => 'a) => code_numeral => code_numeral => 'a randompred"
   203 where
   204   "iterate_upto f n m = Pair (Code_Numeral.iterate_upto f n m)"
   205 
   206 definition not_randompred :: "unit randompred \<Rightarrow> unit randompred"
   207 where
   208   "not_randompred P = (\<lambda>s. let
   209      (P', s') = P s
   210    in if Predicate.eval P' () then (Orderings.bot, s') else (Predicate.single (), s'))"
   211 
   212 definition Random :: "(Random.seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> Random.seed) \<Rightarrow> 'a randompred"
   213   where "Random g = scomp g (Pair o (Predicate.single o fst))"
   214 
   215 definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a randompred \<Rightarrow> 'b randompred)"
   216   where "map f P = bind P (single o f)"
   217 
   218 hide_fact
   219   random_bool_def random_bool_def_raw
   220   random_itself_def random_itself_def_raw
   221   random_char_def random_char_def_raw
   222   random_literal_def random_literal_def_raw
   223   random_nat_def random_nat_def_raw
   224   random_int_def random_int_def_raw
   225   random_fun_lift_def random_fun_lift_def_raw
   226   random_fun_def random_fun_def_raw
   227   collapse_def collapse_def_raw
   228   beyond_def beyond_def_raw beyond_zero
   229   random_aux_rec
   230 
   231 hide_const (open) catch_match random collapse beyond random_fun_aux random_fun_lift
   232 
   233 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 
   234 hide_type (open) randompred
   235 hide_const (open) iter' iter empty single bind union if_randompred iterate_upto not_randompred Random map
   236 
   237 end