src/HOL/ex/Random.thy
author haftmann
Wed, 12 Mar 2008 19:38:14 +0100
changeset 26265 4b63b9e9b10d
parent 26261 b6a103ace4db
child 26589 43cb72871897
permissions -rw-r--r--
separated Random.thy from Quickcheck.thy
     1 (*  ID:         $Id$
     2     Author:     Florian Haftmann, TU Muenchen
     3 *)
     4 
     5 header {* A HOL random engine *}
     6 
     7 theory Random
     8 imports State_Monad Code_Index
     9 begin
    10 
    11 subsection {* Auxiliary functions *}
    12 
    13 definition
    14   inc_shift :: "index \<Rightarrow> index \<Rightarrow> index"
    15 where
    16   "inc_shift v k = (if v = k then 1 else k + 1)"
    17 
    18 definition
    19   minus_shift :: "index \<Rightarrow> index \<Rightarrow> index \<Rightarrow> index"
    20 where
    21   "minus_shift r k l = (if k < l then r + k - l else k - l)"
    22 
    23 function
    24   log :: "index \<Rightarrow> index \<Rightarrow> index"
    25 where
    26   "log b i = (if b \<le> 1 \<or> i < b then 1 else 1 + log b (i div b))"
    27 by pat_completeness auto
    28 termination
    29   by (relation "measure (nat_of_index o snd)")
    30     (auto simp add: index)
    31 
    32 
    33 subsection {* Random seeds *}
    34 
    35 types seed = "index \<times> index"
    36 
    37 primrec
    38   "next" :: "seed \<Rightarrow> index \<times> seed"
    39 where
    40   "next (v, w) = (let
    41      k =  v div 53668;
    42      v' = minus_shift 2147483563 (40014 * (v mod 53668)) (k * 12211);
    43      l =  w div 52774;
    44      w' = minus_shift 2147483399 (40692 * (w mod 52774)) (l * 3791);
    45      z =  minus_shift 2147483562 v' (w' + 1) + 1
    46    in (z, (v', w')))"
    47 
    48 lemma next_not_0:
    49   "fst (next s) \<noteq> 0"
    50 apply (cases s)
    51 apply (auto simp add: minus_shift_def Let_def)
    52 done
    53 
    54 primrec
    55   seed_invariant :: "seed \<Rightarrow> bool"
    56 where
    57   "seed_invariant (v, w) \<longleftrightarrow> 0 < v \<and> v < 9438322952 \<and> 0 < w \<and> True"
    58 
    59 lemma if_same:
    60   "(if b then f x else f y) = f (if b then x else y)"
    61   by (cases b) simp_all
    62 
    63 (*lemma seed_invariant:
    64   assumes "seed_invariant (index_of_nat v, index_of_nat w)"
    65     and "(index_of_nat z, (index_of_nat v', index_of_nat w')) = next (index_of_nat v, index_of_nat w)"
    66   shows "seed_invariant (index_of_nat v', index_of_nat w')"
    67 using assms
    68 apply (auto simp add: seed_invariant_def)
    69 apply (auto simp add: minus_shift_def Let_def)
    70 apply (simp_all add: if_same cong del: if_cong)
    71 apply safe
    72 unfolding not_less
    73 oops*)
    74 
    75 definition
    76   split_seed :: "seed \<Rightarrow> seed \<times> seed"
    77 where
    78   "split_seed s = (let
    79      (v, w) = s;
    80      (v', w') = snd (next s);
    81      v'' = inc_shift 2147483562 v;
    82      s'' = (v'', w');
    83      w'' = inc_shift 2147483398 w;
    84      s''' = (v', w'')
    85    in (s'', s'''))"
    86 
    87 
    88 subsection {* Base selectors *}
    89 
    90 function
    91   range_aux :: "index \<Rightarrow> index \<Rightarrow> seed \<Rightarrow> index \<times> seed"
    92 where
    93   "range_aux k l s = (if k = 0 then (l, s) else
    94     let (v, s') = next s
    95   in range_aux (k - 1) (v + l * 2147483561) s')"
    96 by pat_completeness auto
    97 termination
    98   by (relation "measure (nat_of_index o fst)")
    99     (auto simp add: index)
   100 
   101 definition
   102   range :: "index \<Rightarrow> seed \<Rightarrow> index \<times> seed"
   103 where
   104   "range k = (do
   105      v \<leftarrow> range_aux (log 2147483561 k) 1;
   106      return (v mod k)
   107    done)"
   108 
   109 lemma range:
   110   assumes "k > 0"
   111   shows "fst (range k s) < k"
   112 proof -
   113   obtain v w where range_aux:
   114     "range_aux (log 2147483561 k) 1 s = (v, w)"
   115     by (cases "range_aux (log 2147483561 k) 1 s")
   116   with assms show ?thesis
   117     by (simp add: range_def run_def mbind_def split_def del: range_aux.simps log.simps)
   118 qed
   119 
   120 definition
   121   select :: "'a list \<Rightarrow> seed \<Rightarrow> 'a \<times> seed"
   122 where
   123   "select xs = (do
   124      k \<leftarrow> range (index_of_nat (length xs));
   125      return (nth xs (nat_of_index k))
   126    done)"
   127 
   128 lemma select:
   129   assumes "xs \<noteq> []"
   130   shows "fst (select xs s) \<in> set xs"
   131 proof -
   132   from assms have "index_of_nat (length xs) > 0" by simp
   133   with range have
   134     "fst (range (index_of_nat (length xs)) s) < index_of_nat (length xs)" by best
   135   then have
   136     "nat_of_index (fst (range (index_of_nat (length xs)) s)) < length xs" by simp
   137   then show ?thesis
   138     by (auto simp add: select_def run_def mbind_def split_def)
   139 qed
   140 
   141 definition
   142   select_default :: "index \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> seed \<Rightarrow> 'a \<times> seed"
   143 where
   144   [code func del]: "select_default k x y = (do
   145      l \<leftarrow> range k;
   146      return (if l + 1 < k then x else y)
   147    done)"
   148 
   149 lemma select_default_zero:
   150   "fst (select_default 0 x y s) = y"
   151   by (simp add: run_def mbind_def split_def select_default_def)
   152 
   153 lemma select_default_code [code]:
   154   "select_default k x y = (if k = 0 then do
   155      _ \<leftarrow> range 1;
   156      return y
   157    done else do
   158      l \<leftarrow> range k;
   159      return (if l + 1 < k then x else y)
   160    done)"
   161 proof (cases "k = 0")
   162   case False then show ?thesis by (simp add: select_default_def)
   163 next
   164   case True then show ?thesis
   165     by (simp add: run_def mbind_def split_def select_default_def expand_fun_eq range_def)
   166 qed
   167 
   168 
   169 subsection {* @{text ML} interface *}
   170 
   171 ML {*
   172 structure Random_Engine =
   173 struct
   174 
   175 type seed = int * int;
   176 
   177 local
   178 
   179 val seed = ref 
   180   (let
   181     val now = Time.toMilliseconds (Time.now ());
   182     val (q, s1) = IntInf.divMod (now, 2147483562);
   183     val s2 = q mod 2147483398;
   184   in (s1 + 1, s2 + 1) end);
   185 
   186 in
   187 
   188 fun run f =
   189   let
   190     val (x, seed') = f (! seed);
   191     val _ = seed := seed'
   192   in x end;
   193 
   194 end;
   195 
   196 end;
   197 *}
   198 
   199 end