src/HOL/Quickcheck.thy
author haftmann
Wed Jun 10 15:04:32 2009 +0200 (2009-06-10)
changeset 31603 fa30cd74d7d6
parent 31483 88210717bfc8
child 31607 674348914ebc
permissions -rw-r--r--
revised interpretation combinator for datatype constructions
     1 (* Author: Florian Haftmann, TU Muenchen *)
     2 
     3 header {* A simple counterexample generator *}
     4 
     5 theory Quickcheck
     6 imports Random Code_Eval
     7 uses ("Tools/quickcheck_generators.ML")
     8 begin
     9 
    10 notation fcomp (infixl "o>" 60)
    11 notation scomp (infixl "o\<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 i o\<rightarrow>
    27     (\<lambda>k. Pair (if (k div 2 = 0) then Code_Eval.valtermify True else Code_Eval.valtermify False))"
    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_Eval.valtermify TYPE('a))"
    38 
    39 instance ..
    40 
    41 end
    42 
    43 instantiation char :: random
    44 begin
    45 
    46 definition
    47   "random _ = Random.select chars o\<rightarrow> (\<lambda>c. Pair (c, \<lambda>u. Code_Eval.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_Eval.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_Eval.term)) \<times> Random.seed" where
    67   "random_nat i = Random.range (i + 1) o\<rightarrow> (\<lambda>k. Pair (
    68      let n = Code_Numeral.nat_of k
    69      in (n, \<lambda>_. Code_Eval.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) o\<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_Eval.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 :: "(code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> Random.seed)
    97   \<Rightarrow> code_numeral \<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 i = random_fun_aux TYPEREP('a) TYPEREP('b) (op =) Code_Eval.term_of (f i) Random.split_seed"
    99 
   100 instantiation "fun" :: ("{eq, term_of}", "{type, 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 = random_fun_lift random"
   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 o\<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 code_reserved Quickcheck Quickcheck_Generators
   133 
   134 code_const random_fun_aux (Quickcheck "Quickcheck'_Generators.random'_fun")
   135   -- {* With enough criminal energy this can be abused to derive @{prop False};
   136   for this reason we use a distinguished target @{text Quickcheck}
   137   not spoiling the regular trusted code generation *}
   138 
   139 
   140 hide (open) const collapse beyond random_fun_aux random_fun_lift
   141 
   142 no_notation fcomp (infixl "o>" 60)
   143 no_notation scomp (infixl "o\<rightarrow>" 60)
   144 
   145 end