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