src/HOL/Library/Random.thy
author haftmann
Fri Feb 06 15:15:32 2009 +0100 (2009-02-06)
changeset 29823 0ab754d13ccd
parent 29815 9e94b7078fa5
child 30495 a5f1e4f46d14
permissions -rw-r--r--
session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
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@26265
    24
subsection {* Random seeds *}
haftmann@26038
    25
haftmann@26038
    26
types seed = "index \<times> index"
haftmann@22528
    27
haftmann@29823
    28
primrec "next" :: "seed \<Rightarrow> index \<times> seed" where
haftmann@26265
    29
  "next (v, w) = (let
haftmann@26265
    30
     k =  v div 53668;
haftmann@26265
    31
     v' = minus_shift 2147483563 (40014 * (v mod 53668)) (k * 12211);
haftmann@26265
    32
     l =  w div 52774;
haftmann@26265
    33
     w' = minus_shift 2147483399 (40692 * (w mod 52774)) (l * 3791);
haftmann@26265
    34
     z =  minus_shift 2147483562 v' (w' + 1) + 1
haftmann@26265
    35
   in (z, (v', w')))"
haftmann@26265
    36
haftmann@26265
    37
lemma next_not_0:
haftmann@26265
    38
  "fst (next s) \<noteq> 0"
haftmann@29823
    39
  by (cases s) (auto simp add: minus_shift_def Let_def)
haftmann@26265
    40
haftmann@29823
    41
primrec seed_invariant :: "seed \<Rightarrow> bool" where
haftmann@26265
    42
  "seed_invariant (v, w) \<longleftrightarrow> 0 < v \<and> v < 9438322952 \<and> 0 < w \<and> True"
haftmann@26265
    43
haftmann@29823
    44
lemma if_same: "(if b then f x else f y) = f (if b then x else y)"
haftmann@26265
    45
  by (cases b) simp_all
haftmann@26265
    46
haftmann@29823
    47
definition split_seed :: "seed \<Rightarrow> seed \<times> seed" where
haftmann@26038
    48
  "split_seed s = (let
haftmann@26038
    49
     (v, w) = s;
haftmann@26038
    50
     (v', w') = snd (next s);
haftmann@26265
    51
     v'' = inc_shift 2147483562 v;
haftmann@26038
    52
     s'' = (v'', w');
haftmann@26265
    53
     w'' = inc_shift 2147483398 w;
haftmann@26038
    54
     s''' = (v', w'')
haftmann@26038
    55
   in (s'', s'''))"
haftmann@26038
    56
haftmann@26038
    57
haftmann@26265
    58
subsection {* Base selectors *}
haftmann@22528
    59
haftmann@29823
    60
function range_aux :: "index \<Rightarrow> index \<Rightarrow> seed \<Rightarrow> index \<times> seed" where
haftmann@26038
    61
  "range_aux k l s = (if k = 0 then (l, s) else
haftmann@26038
    62
    let (v, s') = next s
haftmann@26038
    63
  in range_aux (k - 1) (v + l * 2147483561) s')"
haftmann@26038
    64
by pat_completeness auto
haftmann@26038
    65
termination
haftmann@29815
    66
  by (relation "measure (Code_Index.nat_of o fst)")
haftmann@26038
    67
    (auto simp add: index)
haftmann@22528
    68
haftmann@29823
    69
definition range :: "index \<Rightarrow> seed \<Rightarrow> index \<times> seed" where
haftmann@29823
    70
  "range k = range_aux (log 2147483561 k) 1
haftmann@29823
    71
    o\<rightarrow> (\<lambda>v. Pair (v mod k))"
haftmann@26265
    72
haftmann@26265
    73
lemma range:
haftmann@26265
    74
  assumes "k > 0"
haftmann@26265
    75
  shows "fst (range k s) < k"
haftmann@26265
    76
proof -
haftmann@26265
    77
  obtain v w where range_aux:
haftmann@26265
    78
    "range_aux (log 2147483561 k) 1 s = (v, w)"
haftmann@26265
    79
    by (cases "range_aux (log 2147483561 k) 1 s")
haftmann@26265
    80
  with assms show ?thesis
haftmann@29823
    81
    by (simp add: scomp_apply range_def del: range_aux.simps log.simps)
haftmann@26265
    82
qed
haftmann@26038
    83
haftmann@29823
    84
definition select :: "'a list \<Rightarrow> seed \<Rightarrow> 'a \<times> seed" where
haftmann@29823
    85
  "select xs = range (Code_Index.of_nat (length xs))
haftmann@29823
    86
    o\<rightarrow> (\<lambda>k. Pair (nth xs (Code_Index.nat_of k)))"
haftmann@29823
    87
     
haftmann@26265
    88
lemma select:
haftmann@26265
    89
  assumes "xs \<noteq> []"
haftmann@26265
    90
  shows "fst (select xs s) \<in> set xs"
haftmann@26265
    91
proof -
haftmann@29815
    92
  from assms have "Code_Index.of_nat (length xs) > 0" by simp
haftmann@26265
    93
  with range have
haftmann@29815
    94
    "fst (range (Code_Index.of_nat (length xs)) s) < Code_Index.of_nat (length xs)" by best
haftmann@26265
    95
  then have
haftmann@29815
    96
    "Code_Index.nat_of (fst (range (Code_Index.of_nat (length xs)) s)) < length xs" by simp
haftmann@26265
    97
  then show ?thesis
haftmann@29823
    98
    by (simp add: scomp_apply split_beta select_def)
haftmann@26265
    99
qed
haftmann@22528
   100
haftmann@29823
   101
definition select_default :: "index \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> seed \<Rightarrow> 'a \<times> seed" where
haftmann@29823
   102
  [code del]: "select_default k x y = range k
haftmann@29823
   103
     o\<rightarrow> (\<lambda>l. Pair (if l + 1 < k then x else y))"
haftmann@26265
   104
haftmann@26265
   105
lemma select_default_zero:
haftmann@26265
   106
  "fst (select_default 0 x y s) = y"
haftmann@29823
   107
  by (simp add: scomp_apply split_beta select_default_def)
haftmann@26038
   108
haftmann@26265
   109
lemma select_default_code [code]:
haftmann@29823
   110
  "select_default k x y = (if k = 0
haftmann@29823
   111
    then range 1 o\<rightarrow> (\<lambda>_. Pair y)
haftmann@29823
   112
    else range k o\<rightarrow> (\<lambda>l. Pair (if l + 1 < k then x else y)))"
haftmann@29823
   113
proof
haftmann@29823
   114
  fix s
haftmann@29823
   115
  have "snd (range (Code_Index.of_nat 0) s) = snd (range (Code_Index.of_nat 1) s)"
haftmann@29823
   116
    by (simp add: range_def scomp_Pair scomp_apply split_beta)
haftmann@29823
   117
  then show "select_default k x y s = (if k = 0
haftmann@29823
   118
    then range 1 o\<rightarrow> (\<lambda>_. Pair y)
haftmann@29823
   119
    else range k o\<rightarrow> (\<lambda>l. Pair (if l + 1 < k then x else y))) s"
haftmann@29823
   120
    by (cases "k = 0") (simp_all add: select_default_def scomp_apply split_beta)
haftmann@26265
   121
qed
haftmann@22528
   122
haftmann@26265
   123
haftmann@26265
   124
subsection {* @{text ML} interface *}
haftmann@22528
   125
haftmann@22528
   126
ML {*
haftmann@26265
   127
structure Random_Engine =
haftmann@22528
   128
struct
haftmann@22528
   129
haftmann@26038
   130
type seed = int * int;
haftmann@22528
   131
haftmann@22528
   132
local
haftmann@26038
   133
haftmann@26265
   134
val seed = ref 
haftmann@26265
   135
  (let
haftmann@26265
   136
    val now = Time.toMilliseconds (Time.now ());
haftmann@26038
   137
    val (q, s1) = IntInf.divMod (now, 2147483562);
haftmann@26038
   138
    val s2 = q mod 2147483398;
haftmann@26265
   139
  in (s1 + 1, s2 + 1) end);
haftmann@26265
   140
haftmann@22528
   141
in
haftmann@26038
   142
haftmann@26038
   143
fun run f =
haftmann@26038
   144
  let
haftmann@26265
   145
    val (x, seed') = f (! seed);
haftmann@26038
   146
    val _ = seed := seed'
haftmann@26038
   147
  in x end;
haftmann@26038
   148
haftmann@22528
   149
end;
haftmann@22528
   150
haftmann@22528
   151
end;
haftmann@22528
   152
*}
haftmann@22528
   153
haftmann@29823
   154
no_notation fcomp (infixl "o>" 60)
haftmann@29823
   155
no_notation scomp (infixl "o\<rightarrow>" 60)
haftmann@29823
   156
haftmann@26038
   157
end
haftmann@28145
   158