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