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