src/HOL/Quickcheck_Random.thy
author paulson <lp15@cam.ac.uk>
Mon Feb 22 14:37:56 2016 +0000 (2016-02-22)
changeset 62379 340738057c8c
parent 61799 4cf66f21b764
child 62979 1e527c40ae40
permissions -rw-r--r--
An assortment of useful lemmas about sums, norm, etc. Also: norm_conv_dist [symmetric] is now a simprule!
     1 (* Author: Florian Haftmann & Lukas Bulwahn, TU Muenchen *)
     2 
     3 section \<open>A simple counterexample generator performing random testing\<close>
     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 \<open>Code_Target.add_derived_target ("Quickcheck", [(Code_Runtime.target, I)])\<close>
    13 
    14 subsection \<open>Catching Match exceptions\<close>
    15 
    16 axiomatization catch_match :: "'a => 'a => 'a"
    17 
    18 code_printing
    19   constant catch_match \<rightharpoonup> (Quickcheck) "((_) handle Match => _)"
    20 
    21 subsection \<open>The \<open>random\<close> class\<close>
    22 
    23 class random = typerep +
    24   fixes random :: "natural \<Rightarrow> Random.seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
    25 
    26 
    27 subsection \<open>Fundamental and numeric types\<close>
    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 :: "natural \<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 :: "natural \<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 = nat_of_natural 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 int (nat_of_natural (k - i)) else - (int (nat_of_natural (i - k))))
    91      in (j, \<lambda>_. Code_Evaluation.term_of j)))"
    92 
    93 instance ..
    94 
    95 end
    96 
    97 instantiation natural :: random
    98 begin
    99 
   100 definition random_natural :: "natural \<Rightarrow> Random.seed
   101   \<Rightarrow> (natural \<times> (unit \<Rightarrow> Code_Evaluation.term)) \<times> Random.seed"
   102 where
   103   "random_natural i = Random.range (i + 1) \<circ>\<rightarrow> (\<lambda>n. Pair (n, \<lambda>_. Code_Evaluation.term_of n))"
   104 
   105 instance ..
   106 
   107 end
   108 
   109 instantiation integer :: random
   110 begin
   111 
   112 definition random_integer :: "natural \<Rightarrow> Random.seed
   113   \<Rightarrow> (integer \<times> (unit \<Rightarrow> Code_Evaluation.term)) \<times> Random.seed"
   114 where
   115   "random_integer i = Random.range (2 * i + 1) \<circ>\<rightarrow> (\<lambda>k. Pair (
   116      let j = (if k \<ge> i then integer_of_natural (k - i) else - (integer_of_natural (i - k)))
   117       in (j, \<lambda>_. Code_Evaluation.term_of j)))"
   118 
   119 instance ..
   120 
   121 end
   122 
   123 
   124 subsection \<open>Complex generators\<close>
   125 
   126 text \<open>Towards @{typ "'a \<Rightarrow> 'b"}\<close>
   127 
   128 axiomatization random_fun_aux :: "typerep \<Rightarrow> typerep \<Rightarrow> ('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> ('a \<Rightarrow> term)
   129   \<Rightarrow> (Random.seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> Random.seed)
   130   \<Rightarrow> (Random.seed \<Rightarrow> Random.seed \<times> Random.seed)
   131   \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
   132 
   133 definition random_fun_lift :: "(Random.seed \<Rightarrow> ('b \<times> (unit \<Rightarrow> term)) \<times> Random.seed)
   134   \<Rightarrow> Random.seed \<Rightarrow> (('a::term_of \<Rightarrow> 'b::typerep) \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
   135 where
   136   "random_fun_lift f =
   137     random_fun_aux TYPEREP('a) TYPEREP('b) (op =) Code_Evaluation.term_of f Random.split_seed"
   138 
   139 instantiation "fun" :: ("{equal, term_of}", random) random
   140 begin
   141 
   142 definition
   143   random_fun :: "natural \<Rightarrow> Random.seed \<Rightarrow> (('a \<Rightarrow> 'b) \<times> (unit \<Rightarrow> term)) \<times> Random.seed"
   144   where "random i = random_fun_lift (random i)"
   145 
   146 instance ..
   147 
   148 end
   149 
   150 text \<open>Towards type copies and datatypes\<close>
   151 
   152 definition collapse :: "('a \<Rightarrow> ('a \<Rightarrow> 'b \<times> 'a) \<times> 'a) \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a"
   153   where "collapse f = (f \<circ>\<rightarrow> id)"
   154 
   155 definition beyond :: "natural \<Rightarrow> natural \<Rightarrow> natural"
   156   where "beyond k l = (if l > k then l else 0)"
   157 
   158 lemma beyond_zero: "beyond k 0 = 0"
   159   by (simp add: beyond_def)
   160 
   161 
   162 definition (in term_syntax) [code_unfold]:
   163   "valterm_emptyset = Code_Evaluation.valtermify ({} :: ('a :: typerep) set)"
   164 
   165 definition (in term_syntax) [code_unfold]:
   166   "valtermify_insert x s = Code_Evaluation.valtermify insert {\<cdot>} (x :: ('a :: typerep * _)) {\<cdot>} s"
   167 
   168 instantiation set :: (random) random
   169 begin
   170 
   171 fun random_aux_set
   172 where
   173   "random_aux_set 0 j = collapse (Random.select_weight [(1, Pair valterm_emptyset)])"
   174 | "random_aux_set (Code_Numeral.Suc i) j =
   175     collapse (Random.select_weight
   176       [(1, Pair valterm_emptyset),
   177        (Code_Numeral.Suc i,
   178         random j \<circ>\<rightarrow> (%x. random_aux_set i j \<circ>\<rightarrow> (%s. Pair (valtermify_insert x s))))])"
   179 
   180 lemma [code]:
   181   "random_aux_set i j =
   182     collapse (Random.select_weight [(1, Pair valterm_emptyset),
   183       (i, random j \<circ>\<rightarrow> (%x. random_aux_set (i - 1) j \<circ>\<rightarrow> (%s. Pair (valtermify_insert x s))))])"
   184 proof (induct i rule: natural.induct)
   185   case zero
   186   show ?case by (subst select_weight_drop_zero [symmetric])
   187     (simp add: random_aux_set.simps [simplified] less_natural_def)
   188 next
   189   case (Suc i)
   190   show ?case by (simp only: random_aux_set.simps(2) [of "i"] Suc_natural_minus_one)
   191 qed
   192 
   193 definition "random_set i = random_aux_set i i"
   194 
   195 instance ..
   196 
   197 end
   198 
   199 lemma random_aux_rec:
   200   fixes random_aux :: "natural \<Rightarrow> 'a"
   201   assumes "random_aux 0 = rhs 0"
   202     and "\<And>k. random_aux (Code_Numeral.Suc k) = rhs (Code_Numeral.Suc k)"
   203   shows "random_aux k = rhs k"
   204   using assms by (rule natural.induct)
   205 
   206 subsection \<open>Deriving random generators for datatypes\<close>
   207 
   208 ML_file "Tools/Quickcheck/quickcheck_common.ML" 
   209 ML_file "Tools/Quickcheck/random_generators.ML"
   210 
   211 
   212 subsection \<open>Code setup\<close>
   213 
   214 code_printing
   215   constant random_fun_aux \<rightharpoonup> (Quickcheck) "Random'_Generators.random'_fun"
   216   \<comment> \<open>With enough criminal energy this can be abused to derive @{prop False};
   217   for this reason we use a distinguished target \<open>Quickcheck\<close>
   218   not spoiling the regular trusted code generation\<close>
   219 
   220 code_reserved Quickcheck Random_Generators
   221 
   222 no_notation fcomp (infixl "\<circ>>" 60)
   223 no_notation scomp (infixl "\<circ>\<rightarrow>" 60)
   224     
   225 hide_const (open) catch_match random collapse beyond random_fun_aux random_fun_lift
   226 
   227 hide_fact (open) collapse_def beyond_def random_fun_lift_def
   228 
   229 end