src/HOL/Random.thy
author haftmann
Tue May 19 13:57:32 2009 +0200 (2009-05-19)
changeset 31203 5c8fb4fd67e0
parent 31196 82ff416d7d66
child 31205 98370b26c2ce
permissions -rw-r--r--
moved Code_Index, Random and Quickcheck before Main
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@31203
     6
imports Code_Index List
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
definition split_seed :: "seed \<Rightarrow> seed \<times> seed" where
haftmann@26038
    46
  "split_seed s = (let
haftmann@26038
    47
     (v, w) = s;
haftmann@26038
    48
     (v', w') = snd (next s);
haftmann@26265
    49
     v'' = inc_shift 2147483562 v;
haftmann@26038
    50
     s'' = (v'', w');
haftmann@26265
    51
     w'' = inc_shift 2147483398 w;
haftmann@26038
    52
     s''' = (v', w'')
haftmann@26038
    53
   in (s'', s'''))"
haftmann@26038
    54
haftmann@26038
    55
haftmann@26265
    56
subsection {* Base selectors *}
haftmann@22528
    57
haftmann@30500
    58
fun iterate :: "index \<Rightarrow> ('b \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a) \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a" where
haftmann@30495
    59
  "iterate k f x = (if k = 0 then Pair x else f x o\<rightarrow> iterate (k - 1) f)"
haftmann@22528
    60
haftmann@30500
    61
definition range :: "index \<Rightarrow> seed \<Rightarrow> index \<times> seed" where
haftmann@30495
    62
  "range k = iterate (log 2147483561 k)
haftmann@30495
    63
      (\<lambda>l. next o\<rightarrow> (\<lambda>v. Pair (v + l * 2147483561))) 1
haftmann@29823
    64
    o\<rightarrow> (\<lambda>v. Pair (v mod k))"
haftmann@26265
    65
haftmann@26265
    66
lemma range:
haftmann@30495
    67
  "k > 0 \<Longrightarrow> fst (range k s) < k"
haftmann@30495
    68
  by (simp add: range_def scomp_apply split_def del: log.simps iterate.simps)
haftmann@26038
    69
haftmann@29823
    70
definition select :: "'a list \<Rightarrow> seed \<Rightarrow> 'a \<times> seed" where
haftmann@29823
    71
  "select xs = range (Code_Index.of_nat (length xs))
haftmann@29823
    72
    o\<rightarrow> (\<lambda>k. Pair (nth xs (Code_Index.nat_of k)))"
haftmann@29823
    73
     
haftmann@26265
    74
lemma select:
haftmann@26265
    75
  assumes "xs \<noteq> []"
haftmann@26265
    76
  shows "fst (select xs s) \<in> set xs"
haftmann@26265
    77
proof -
haftmann@29815
    78
  from assms have "Code_Index.of_nat (length xs) > 0" by simp
haftmann@26265
    79
  with range have
haftmann@29815
    80
    "fst (range (Code_Index.of_nat (length xs)) s) < Code_Index.of_nat (length xs)" by best
haftmann@26265
    81
  then have
haftmann@29815
    82
    "Code_Index.nat_of (fst (range (Code_Index.of_nat (length xs)) s)) < length xs" by simp
haftmann@26265
    83
  then show ?thesis
haftmann@29823
    84
    by (simp add: scomp_apply split_beta select_def)
haftmann@26265
    85
qed
haftmann@22528
    86
haftmann@31180
    87
primrec pick :: "(index \<times> 'a) list \<Rightarrow> index \<Rightarrow> 'a" where
haftmann@31180
    88
  "pick (x # xs) i = (if i < fst x then snd x else pick xs (i - fst x))"
haftmann@31180
    89
haftmann@31180
    90
lemma pick_member:
haftmann@31180
    91
  "i < listsum (map fst xs) \<Longrightarrow> pick xs i \<in> set (map snd xs)"
haftmann@31180
    92
  by (induct xs arbitrary: i) simp_all
haftmann@31180
    93
haftmann@31180
    94
lemma pick_drop_zero:
haftmann@31180
    95
  "pick (filter (\<lambda>(k, _). k > 0) xs) = pick xs"
haftmann@31180
    96
  by (induct xs) (auto simp add: expand_fun_eq)
haftmann@31180
    97
haftmann@31203
    98
lemma pick_same:
haftmann@31203
    99
  "l < length xs \<Longrightarrow> Random.pick (map (Pair 1) xs) (Code_Index.of_nat l) = nth xs l"
haftmann@31203
   100
proof (induct xs arbitrary: l)
haftmann@31203
   101
  case Nil then show ?case by simp
haftmann@31203
   102
next
haftmann@31203
   103
  case (Cons x xs) then show ?case by (cases l) simp_all
haftmann@31203
   104
qed
haftmann@31203
   105
haftmann@31180
   106
definition select_weight :: "(index \<times> 'a) list \<Rightarrow> seed \<Rightarrow> 'a \<times> seed" where
haftmann@31180
   107
  "select_weight xs = range (listsum (map fst xs))
haftmann@31180
   108
   o\<rightarrow> (\<lambda>k. Pair (pick xs k))"
haftmann@31180
   109
haftmann@31180
   110
lemma select_weight_member:
haftmann@31180
   111
  assumes "0 < listsum (map fst xs)"
haftmann@31180
   112
  shows "fst (select_weight xs s) \<in> set (map snd xs)"
haftmann@31180
   113
proof -
haftmann@31180
   114
  from range assms
haftmann@31180
   115
    have "fst (range (listsum (map fst xs)) s) < listsum (map fst xs)" .
haftmann@31180
   116
  with pick_member
haftmann@31180
   117
    have "pick xs (fst (range (listsum (map fst xs)) s)) \<in> set (map snd xs)" .
haftmann@31180
   118
  then show ?thesis by (simp add: select_weight_def scomp_def split_def) 
haftmann@31180
   119
qed
haftmann@31180
   120
haftmann@31203
   121
lemma select_weigth_drop_zero:
haftmann@31203
   122
  "Random.select_weight (filter (\<lambda>(k, _). k > 0) xs) = Random.select_weight xs"
haftmann@31203
   123
proof -
haftmann@31203
   124
  have "listsum (map fst [(k, _)\<leftarrow>xs . 0 < k]) = listsum (map fst xs)"
haftmann@31203
   125
    by (induct xs) auto
haftmann@31203
   126
  then show ?thesis by (simp only: select_weight_def pick_drop_zero)
haftmann@31203
   127
qed
haftmann@31203
   128
haftmann@31203
   129
lemma select_weigth_select:
haftmann@31203
   130
  assumes "xs \<noteq> []"
haftmann@31203
   131
  shows "Random.select_weight (map (Pair 1) xs) = Random.select xs"
haftmann@31203
   132
proof -
haftmann@31203
   133
  have less: "\<And>s. fst (Random.range (Code_Index.of_nat (length xs)) s) < Code_Index.of_nat (length xs)"
haftmann@31203
   134
    using assms by (intro range) simp
haftmann@31203
   135
  moreover have "listsum (map fst (map (Pair 1) xs)) = Code_Index.of_nat (length xs)"
haftmann@31203
   136
    by (induct xs) simp_all
haftmann@31203
   137
  ultimately show ?thesis
haftmann@31203
   138
    by (auto simp add: select_weight_def select_def scomp_def split_def
haftmann@31203
   139
      expand_fun_eq pick_same [symmetric])
haftmann@31203
   140
qed
haftmann@31203
   141
haftmann@29823
   142
definition select_default :: "index \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> seed \<Rightarrow> 'a \<times> seed" where
haftmann@29823
   143
  [code del]: "select_default k x y = range k
haftmann@29823
   144
     o\<rightarrow> (\<lambda>l. Pair (if l + 1 < k then x else y))"
haftmann@26265
   145
haftmann@26265
   146
lemma select_default_zero:
haftmann@26265
   147
  "fst (select_default 0 x y s) = y"
haftmann@29823
   148
  by (simp add: scomp_apply split_beta select_default_def)
haftmann@26038
   149
haftmann@26265
   150
lemma select_default_code [code]:
haftmann@29823
   151
  "select_default k x y = (if k = 0
haftmann@29823
   152
    then range 1 o\<rightarrow> (\<lambda>_. Pair y)
haftmann@29823
   153
    else range k o\<rightarrow> (\<lambda>l. Pair (if l + 1 < k then x else y)))"
haftmann@29823
   154
proof
haftmann@29823
   155
  fix s
haftmann@29823
   156
  have "snd (range (Code_Index.of_nat 0) s) = snd (range (Code_Index.of_nat 1) s)"
haftmann@29823
   157
    by (simp add: range_def scomp_Pair scomp_apply split_beta)
haftmann@29823
   158
  then show "select_default k x y s = (if k = 0
haftmann@29823
   159
    then range 1 o\<rightarrow> (\<lambda>_. Pair y)
haftmann@29823
   160
    else range k o\<rightarrow> (\<lambda>l. Pair (if l + 1 < k then x else y))) s"
haftmann@29823
   161
    by (cases "k = 0") (simp_all add: select_default_def scomp_apply split_beta)
haftmann@26265
   162
qed
haftmann@22528
   163
haftmann@26265
   164
haftmann@26265
   165
subsection {* @{text ML} interface *}
haftmann@22528
   166
haftmann@22528
   167
ML {*
haftmann@26265
   168
structure Random_Engine =
haftmann@22528
   169
struct
haftmann@22528
   170
haftmann@26038
   171
type seed = int * int;
haftmann@22528
   172
haftmann@22528
   173
local
haftmann@26038
   174
haftmann@26265
   175
val seed = ref 
haftmann@26265
   176
  (let
haftmann@26265
   177
    val now = Time.toMilliseconds (Time.now ());
haftmann@26038
   178
    val (q, s1) = IntInf.divMod (now, 2147483562);
haftmann@26038
   179
    val s2 = q mod 2147483398;
haftmann@26265
   180
  in (s1 + 1, s2 + 1) end);
haftmann@26265
   181
haftmann@22528
   182
in
haftmann@26038
   183
haftmann@26038
   184
fun run f =
haftmann@26038
   185
  let
haftmann@26265
   186
    val (x, seed') = f (! seed);
haftmann@26038
   187
    val _ = seed := seed'
haftmann@26038
   188
  in x end;
haftmann@26038
   189
haftmann@22528
   190
end;
haftmann@22528
   191
haftmann@22528
   192
end;
haftmann@22528
   193
*}
haftmann@22528
   194
haftmann@31180
   195
hide (open) type seed
haftmann@31180
   196
hide (open) const inc_shift minus_shift log "next" seed_invariant split_seed
haftmann@31180
   197
  iterate range select pick select_weight select_default
haftmann@31180
   198
haftmann@29823
   199
no_notation fcomp (infixl "o>" 60)
haftmann@29823
   200
no_notation scomp (infixl "o\<rightarrow>" 60)
haftmann@29823
   201
haftmann@26038
   202
end
haftmann@28145
   203