src/HOL/Library/Random.thy
author haftmann
Fri May 15 16:39:16 2009 +0200 (2009-05-15)
changeset 31180 dae7be64d614
parent 30500 072daf3914c0
permissions -rw-r--r--
hide names in theory Random
haftmann@29815
     1
(* Author: Florian Haftmann, TU Muenchen *)
haftmann@22528
     2
haftmann@26265
     3
header {* A HOL random engine *}
haftmann@22528
     4
haftmann@22528
     5
theory Random
haftmann@29823
     6
imports Code_Index
haftmann@22528
     7
begin
haftmann@22528
     8
haftmann@29823
     9
notation fcomp (infixl "o>" 60)
haftmann@29823
    10
notation scomp (infixl "o\<rightarrow>" 60)
haftmann@29823
    11
haftmann@29823
    12
haftmann@26265
    13
subsection {* Auxiliary functions *}
haftmann@26265
    14
haftmann@29823
    15
definition inc_shift :: "index \<Rightarrow> index \<Rightarrow> index" where
haftmann@26265
    16
  "inc_shift v k = (if v = k then 1 else k + 1)"
haftmann@26265
    17
haftmann@29823
    18
definition minus_shift :: "index \<Rightarrow> index \<Rightarrow> index \<Rightarrow> index" where
haftmann@26265
    19
  "minus_shift r k l = (if k < l then r + k - l else k - l)"
haftmann@26265
    20
haftmann@29823
    21
fun log :: "index \<Rightarrow> index \<Rightarrow> index" where
haftmann@26265
    22
  "log b i = (if b \<le> 1 \<or> i < b then 1 else 1 + log b (i div b))"
haftmann@26265
    23
haftmann@30495
    24
haftmann@26265
    25
subsection {* Random seeds *}
haftmann@26038
    26
haftmann@26038
    27
types seed = "index \<times> index"
haftmann@22528
    28
haftmann@29823
    29
primrec "next" :: "seed \<Rightarrow> index \<times> seed" where
haftmann@26265
    30
  "next (v, w) = (let
haftmann@26265
    31
     k =  v div 53668;
haftmann@26265
    32
     v' = minus_shift 2147483563 (40014 * (v mod 53668)) (k * 12211);
haftmann@26265
    33
     l =  w div 52774;
haftmann@26265
    34
     w' = minus_shift 2147483399 (40692 * (w mod 52774)) (l * 3791);
haftmann@26265
    35
     z =  minus_shift 2147483562 v' (w' + 1) + 1
haftmann@26265
    36
   in (z, (v', w')))"
haftmann@26265
    37
haftmann@26265
    38
lemma next_not_0:
haftmann@26265
    39
  "fst (next s) \<noteq> 0"
haftmann@29823
    40
  by (cases s) (auto simp add: minus_shift_def Let_def)
haftmann@26265
    41
haftmann@29823
    42
primrec seed_invariant :: "seed \<Rightarrow> bool" where
haftmann@26265
    43
  "seed_invariant (v, w) \<longleftrightarrow> 0 < v \<and> v < 9438322952 \<and> 0 < w \<and> True"
haftmann@26265
    44
haftmann@29823
    45
lemma if_same: "(if b then f x else f y) = f (if b then x else y)"
haftmann@26265
    46
  by (cases b) simp_all
haftmann@26265
    47
haftmann@29823
    48
definition split_seed :: "seed \<Rightarrow> seed \<times> seed" where
haftmann@26038
    49
  "split_seed s = (let
haftmann@26038
    50
     (v, w) = s;
haftmann@26038
    51
     (v', w') = snd (next s);
haftmann@26265
    52
     v'' = inc_shift 2147483562 v;
haftmann@26038
    53
     s'' = (v'', w');
haftmann@26265
    54
     w'' = inc_shift 2147483398 w;
haftmann@26038
    55
     s''' = (v', w'')
haftmann@26038
    56
   in (s'', s'''))"
haftmann@26038
    57
haftmann@26038
    58
haftmann@26265
    59
subsection {* Base selectors *}
haftmann@22528
    60
haftmann@30500
    61
fun iterate :: "index \<Rightarrow> ('b \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a) \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a" where
haftmann@30495
    62
  "iterate k f x = (if k = 0 then Pair x else f x o\<rightarrow> iterate (k - 1) f)"
haftmann@22528
    63
haftmann@30500
    64
definition range :: "index \<Rightarrow> seed \<Rightarrow> index \<times> seed" where
haftmann@30495
    65
  "range k = iterate (log 2147483561 k)
haftmann@30495
    66
      (\<lambda>l. next o\<rightarrow> (\<lambda>v. Pair (v + l * 2147483561))) 1
haftmann@29823
    67
    o\<rightarrow> (\<lambda>v. Pair (v mod k))"
haftmann@26265
    68
haftmann@26265
    69
lemma range:
haftmann@30495
    70
  "k > 0 \<Longrightarrow> fst (range k s) < k"
haftmann@30495
    71
  by (simp add: range_def scomp_apply split_def del: log.simps iterate.simps)
haftmann@26038
    72
haftmann@29823
    73
definition select :: "'a list \<Rightarrow> seed \<Rightarrow> 'a \<times> seed" where
haftmann@29823
    74
  "select xs = range (Code_Index.of_nat (length xs))
haftmann@29823
    75
    o\<rightarrow> (\<lambda>k. Pair (nth xs (Code_Index.nat_of k)))"
haftmann@29823
    76
     
haftmann@26265
    77
lemma select:
haftmann@26265
    78
  assumes "xs \<noteq> []"
haftmann@26265
    79
  shows "fst (select xs s) \<in> set xs"
haftmann@26265
    80
proof -
haftmann@29815
    81
  from assms have "Code_Index.of_nat (length xs) > 0" by simp
haftmann@26265
    82
  with range have
haftmann@29815
    83
    "fst (range (Code_Index.of_nat (length xs)) s) < Code_Index.of_nat (length xs)" by best
haftmann@26265
    84
  then have
haftmann@29815
    85
    "Code_Index.nat_of (fst (range (Code_Index.of_nat (length xs)) s)) < length xs" by simp
haftmann@26265
    86
  then show ?thesis
haftmann@29823
    87
    by (simp add: scomp_apply split_beta select_def)
haftmann@26265
    88
qed
haftmann@22528
    89
haftmann@31180
    90
primrec pick :: "(index \<times> 'a) list \<Rightarrow> index \<Rightarrow> 'a" where
haftmann@31180
    91
  "pick (x # xs) i = (if i < fst x then snd x else pick xs (i - fst x))"
haftmann@31180
    92
haftmann@31180
    93
lemma pick_member:
haftmann@31180
    94
  "i < listsum (map fst xs) \<Longrightarrow> pick xs i \<in> set (map snd xs)"
haftmann@31180
    95
  by (induct xs arbitrary: i) simp_all
haftmann@31180
    96
haftmann@31180
    97
lemma pick_drop_zero:
haftmann@31180
    98
  "pick (filter (\<lambda>(k, _). k > 0) xs) = pick xs"
haftmann@31180
    99
  by (induct xs) (auto simp add: expand_fun_eq)
haftmann@31180
   100
haftmann@31180
   101
definition select_weight :: "(index \<times> 'a) list \<Rightarrow> seed \<Rightarrow> 'a \<times> seed" where
haftmann@31180
   102
  "select_weight xs = range (listsum (map fst xs))
haftmann@31180
   103
   o\<rightarrow> (\<lambda>k. Pair (pick xs k))"
haftmann@31180
   104
haftmann@31180
   105
lemma select_weight_member:
haftmann@31180
   106
  assumes "0 < listsum (map fst xs)"
haftmann@31180
   107
  shows "fst (select_weight xs s) \<in> set (map snd xs)"
haftmann@31180
   108
proof -
haftmann@31180
   109
  from range assms
haftmann@31180
   110
    have "fst (range (listsum (map fst xs)) s) < listsum (map fst xs)" .
haftmann@31180
   111
  with pick_member
haftmann@31180
   112
    have "pick xs (fst (range (listsum (map fst xs)) s)) \<in> set (map snd xs)" .
haftmann@31180
   113
  then show ?thesis by (simp add: select_weight_def scomp_def split_def) 
haftmann@31180
   114
qed
haftmann@31180
   115
haftmann@29823
   116
definition select_default :: "index \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> seed \<Rightarrow> 'a \<times> seed" where
haftmann@29823
   117
  [code del]: "select_default k x y = range k
haftmann@29823
   118
     o\<rightarrow> (\<lambda>l. Pair (if l + 1 < k then x else y))"
haftmann@26265
   119
haftmann@26265
   120
lemma select_default_zero:
haftmann@26265
   121
  "fst (select_default 0 x y s) = y"
haftmann@29823
   122
  by (simp add: scomp_apply split_beta select_default_def)
haftmann@26038
   123
haftmann@26265
   124
lemma select_default_code [code]:
haftmann@29823
   125
  "select_default k x y = (if k = 0
haftmann@29823
   126
    then range 1 o\<rightarrow> (\<lambda>_. Pair y)
haftmann@29823
   127
    else range k o\<rightarrow> (\<lambda>l. Pair (if l + 1 < k then x else y)))"
haftmann@29823
   128
proof
haftmann@29823
   129
  fix s
haftmann@29823
   130
  have "snd (range (Code_Index.of_nat 0) s) = snd (range (Code_Index.of_nat 1) s)"
haftmann@29823
   131
    by (simp add: range_def scomp_Pair scomp_apply split_beta)
haftmann@29823
   132
  then show "select_default k x y s = (if k = 0
haftmann@29823
   133
    then range 1 o\<rightarrow> (\<lambda>_. Pair y)
haftmann@29823
   134
    else range k o\<rightarrow> (\<lambda>l. Pair (if l + 1 < k then x else y))) s"
haftmann@29823
   135
    by (cases "k = 0") (simp_all add: select_default_def scomp_apply split_beta)
haftmann@26265
   136
qed
haftmann@22528
   137
haftmann@26265
   138
haftmann@26265
   139
subsection {* @{text ML} interface *}
haftmann@22528
   140
haftmann@22528
   141
ML {*
haftmann@26265
   142
structure Random_Engine =
haftmann@22528
   143
struct
haftmann@22528
   144
haftmann@26038
   145
type seed = int * int;
haftmann@22528
   146
haftmann@22528
   147
local
haftmann@26038
   148
haftmann@26265
   149
val seed = ref 
haftmann@26265
   150
  (let
haftmann@26265
   151
    val now = Time.toMilliseconds (Time.now ());
haftmann@26038
   152
    val (q, s1) = IntInf.divMod (now, 2147483562);
haftmann@26038
   153
    val s2 = q mod 2147483398;
haftmann@26265
   154
  in (s1 + 1, s2 + 1) end);
haftmann@26265
   155
haftmann@22528
   156
in
haftmann@26038
   157
haftmann@26038
   158
fun run f =
haftmann@26038
   159
  let
haftmann@26265
   160
    val (x, seed') = f (! seed);
haftmann@26038
   161
    val _ = seed := seed'
haftmann@26038
   162
  in x end;
haftmann@26038
   163
haftmann@22528
   164
end;
haftmann@22528
   165
haftmann@22528
   166
end;
haftmann@22528
   167
*}
haftmann@22528
   168
haftmann@31180
   169
hide (open) type seed
haftmann@31180
   170
hide (open) const inc_shift minus_shift log "next" seed_invariant split_seed
haftmann@31180
   171
  iterate range select pick select_weight select_default
haftmann@31180
   172
haftmann@29823
   173
no_notation fcomp (infixl "o>" 60)
haftmann@29823
   174
no_notation scomp (infixl "o\<rightarrow>" 60)
haftmann@29823
   175
haftmann@26038
   176
end
haftmann@28145
   177