src/HOL/Quickcheck_Random.thy
author haftmann
Thu Feb 14 15:27:10 2013 +0100 (2013-02-14)
changeset 51126 df86080de4cb
parent 50046 src/HOL/Quickcheck.thy@0051dc4f301f
child 51143 0a2371e7ced3
permissions -rw-r--r--
reform of predicate compiler / quickcheck theories:
implement yieldn operations uniformly on the ML level -- predicate compiler uses negative integers as parameter to yieldn, whereas code_numeral represents natural numbers!
avoid odd New_ prefix by joining related theories;
avoid overcompact name DSequence;
separated predicate inside random monad into separate theory;
consolidated name of theory Quickcheck
     1 (* Author: Florian Haftmann & Lukas Bulwahn, TU Muenchen *)
     2 
     3 header {* A simple counterexample generator performing random testing *}
     4 
     5 theory Quickcheck_Random
     6 imports Random Code_Evaluation Enum
     7 begin
     8 
     9 notation fcomp (infixl "\<circ>>" 60)
    10 notation scomp (infixl "\<circ>\<rightarrow>" 60)
    11 
    12 setup {* Code_Target.extend_target ("Quickcheck", (Code_Runtime.target, K I)) *}
    13 
    14 subsection {* Catching Match exceptions *}
    15 
    16 axiomatization catch_match :: "'a => 'a => 'a"
    17 
    18 code_const catch_match 
    19   (Quickcheck "((_) handle Match => _)")
    20 
    21 subsection {* The @{text random} class *}
    22 
    23 class random = typerep +
    24   fixes random :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
    25 
    26 
    27 subsection {* Fundamental and numeric types*}
    28 
    29 instantiation bool :: random
    30 begin
    31 
    32 definition
    33   "random i = Random.range 2 \<circ>\<rightarrow>
    34     (\<lambda>k. Pair (if k = 0 then Code_Evaluation.valtermify False else Code_Evaluation.valtermify True))"
    35 
    36 instance ..
    37 
    38 end
    39 
    40 instantiation itself :: (typerep) random
    41 begin
    42 
    43 definition
    44   random_itself :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('a itself \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
    45 where "random_itself _ = Pair (Code_Evaluation.valtermify TYPE('a))"
    46 
    47 instance ..
    48 
    49 end
    50 
    51 instantiation char :: random
    52 begin
    53 
    54 definition
    55   "random _ = Random.select (Enum.enum :: char list) \<circ>\<rightarrow> (\<lambda>c. Pair (c, \<lambda>u. Code_Evaluation.term_of c))"
    56 
    57 instance ..
    58 
    59 end
    60 
    61 instantiation String.literal :: random
    62 begin
    63 
    64 definition 
    65   "random _ = Pair (STR '''', \<lambda>u. Code_Evaluation.term_of (STR ''''))"
    66 
    67 instance ..
    68 
    69 end
    70 
    71 instantiation nat :: random
    72 begin
    73 
    74 definition random_nat :: "code_numeral \<Rightarrow> Random.seed
    75   \<Rightarrow> (nat \<times> (unit \<Rightarrow> Code_Evaluation.term)) \<times> Random.seed"
    76 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)
   104   \<Rightarrow> (Random.seed \<Rightarrow> Random.seed \<times> Random.seed)
   105   \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
   106 
   107 definition random_fun_lift :: "(Random.seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> Random.seed)
   108   \<Rightarrow> Random.seed \<Rightarrow> (('a\<Colon>term_of \<Rightarrow> 'b\<Colon>typerep) \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
   109 where
   110   "random_fun_lift f =
   111     random_fun_aux TYPEREP('a) TYPEREP('b) (op =) Code_Evaluation.term_of f Random.split_seed"
   112 
   113 instantiation "fun" :: ("{equal, term_of}", random) random
   114 begin
   115 
   116 definition
   117   random_fun :: "code_numeral \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
   118   where "random i = random_fun_lift (random i)"
   119 
   120 instance ..
   121 
   122 end
   123 
   124 text {* Towards type copies and datatypes *}
   125 
   126 definition collapse :: "('a \<Rightarrow> ('a \<Rightarrow> 'b \<times> 'a) \<times> 'a) \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a"
   127   where "collapse f = (f \<circ>\<rightarrow> id)"
   128 
   129 definition beyond :: "code_numeral \<Rightarrow> code_numeral \<Rightarrow> code_numeral"
   130   where "beyond k l = (if l > k then l else 0)"
   131 
   132 lemma beyond_zero: "beyond k 0 = 0"
   133   by (simp add: beyond_def)
   134 
   135 
   136 definition (in term_syntax) [code_unfold]:
   137   "valterm_emptyset = Code_Evaluation.valtermify ({} :: ('a :: typerep) set)"
   138 
   139 definition (in term_syntax) [code_unfold]:
   140   "valtermify_insert x s = Code_Evaluation.valtermify insert {\<cdot>} (x :: ('a :: typerep * _)) {\<cdot>} s"
   141 
   142 instantiation set :: (random) random
   143 begin
   144 
   145 primrec random_aux_set
   146 where
   147   "random_aux_set 0 j = collapse (Random.select_weight [(1, Pair valterm_emptyset)])"
   148 | "random_aux_set (Code_Numeral.Suc i) j =
   149     collapse (Random.select_weight
   150       [(1, Pair valterm_emptyset),
   151        (Code_Numeral.Suc i,
   152         random j \<circ>\<rightarrow> (%x. random_aux_set i j \<circ>\<rightarrow> (%s. Pair (valtermify_insert x s))))])"
   153 
   154 lemma [code]:
   155   "random_aux_set i j =
   156     collapse (Random.select_weight [(1, Pair valterm_emptyset),
   157       (i, random j \<circ>\<rightarrow> (%x. random_aux_set (i - 1) j \<circ>\<rightarrow> (%s. Pair (valtermify_insert x s))))])"
   158 proof (induct i rule: code_numeral.induct)
   159   case zero
   160   show ?case by (subst select_weight_drop_zero [symmetric])
   161     (simp add: random_aux_set.simps [simplified])
   162 next
   163   case (Suc i)
   164   show ?case by (simp only: random_aux_set.simps(2) [of "i"] Suc_code_numeral_minus_one)
   165 qed
   166 
   167 definition "random_set i = random_aux_set i i"
   168 
   169 instance ..
   170 
   171 end
   172 
   173 lemma random_aux_rec:
   174   fixes random_aux :: "code_numeral \<Rightarrow> 'a"
   175   assumes "random_aux 0 = rhs 0"
   176     and "\<And>k. random_aux (Code_Numeral.Suc k) = rhs (Code_Numeral.Suc k)"
   177   shows "random_aux k = rhs k"
   178   using assms by (rule code_numeral.induct)
   179 
   180 subsection {* Deriving random generators for datatypes *}
   181 
   182 ML_file "Tools/Quickcheck/quickcheck_common.ML" 
   183 ML_file "Tools/Quickcheck/random_generators.ML"
   184 setup Random_Generators.setup
   185 
   186 
   187 subsection {* Code setup *}
   188 
   189 code_const random_fun_aux (Quickcheck "Random'_Generators.random'_fun")
   190   -- {* With enough criminal energy this can be abused to derive @{prop False};
   191   for this reason we use a distinguished target @{text Quickcheck}
   192   not spoiling the regular trusted code generation *}
   193 
   194 code_reserved Quickcheck Random_Generators
   195 
   196 no_notation fcomp (infixl "\<circ>>" 60)
   197 no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
   198     
   199 hide_const (open) catch_match random collapse beyond random_fun_aux random_fun_lift
   200 
   201 hide_fact (open) collapse_def beyond_def random_fun_lift_def
   202 
   203 end
   204