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