src/HOL/Random.thy
changeset 60758 d8d85a8172b5
parent 59058 a78612c67ec0
child 61799 4cf66f21b764
equal deleted inserted replaced
60757:c09598a97436 60758:d8d85a8172b5
     1 
     1 
     2 (* Author: Florian Haftmann, TU Muenchen *)
     2 (* Author: Florian Haftmann, TU Muenchen *)
     3 
     3 
     4 section {* A HOL random engine *}
     4 section \<open>A HOL random engine\<close>
     5 
     5 
     6 theory Random
     6 theory Random
     7 imports List Groups_List
     7 imports List Groups_List
     8 begin
     8 begin
     9 
     9 
    10 notation fcomp (infixl "\<circ>>" 60)
    10 notation fcomp (infixl "\<circ>>" 60)
    11 notation scomp (infixl "\<circ>\<rightarrow>" 60)
    11 notation scomp (infixl "\<circ>\<rightarrow>" 60)
    12 
    12 
    13 
    13 
    14 subsection {* Auxiliary functions *}
    14 subsection \<open>Auxiliary functions\<close>
    15 
    15 
    16 fun log :: "natural \<Rightarrow> natural \<Rightarrow> natural" where
    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))"
    17   "log b i = (if b \<le> 1 \<or> i < b then 1 else 1 + log b (i div b))"
    18 
    18 
    19 definition inc_shift :: "natural \<Rightarrow> natural \<Rightarrow> natural" where
    19 definition inc_shift :: "natural \<Rightarrow> natural \<Rightarrow> natural" where
    21 
    21 
    22 definition minus_shift :: "natural \<Rightarrow> natural \<Rightarrow> natural \<Rightarrow> natural" where
    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)"
    23   "minus_shift r k l = (if k < l then r + k - l else k - l)"
    24 
    24 
    25 
    25 
    26 subsection {* Random seeds *}
    26 subsection \<open>Random seeds\<close>
    27 
    27 
    28 type_synonym seed = "natural \<times> natural"
    28 type_synonym seed = "natural \<times> natural"
    29 
    29 
    30 primrec "next" :: "seed \<Rightarrow> natural \<times> seed" where
    30 primrec "next" :: "seed \<Rightarrow> natural \<times> seed" where
    31   "next (v, w) = (let
    31   "next (v, w) = (let
    43      v'' = inc_shift 2147483562 v;
    43      v'' = inc_shift 2147483562 v;
    44      w'' = inc_shift 2147483398 w
    44      w'' = inc_shift 2147483398 w
    45    in ((v'', w'), (v', w'')))"
    45    in ((v'', w'), (v', w'')))"
    46 
    46 
    47 
    47 
    48 subsection {* Base selectors *}
    48 subsection \<open>Base selectors\<close>
    49 
    49 
    50 fun iterate :: "natural \<Rightarrow> ('b \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a) \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a" where
    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)"
    51   "iterate k f x = (if k = 0 then Pair x else f x \<circ>\<rightarrow> iterate (k - 1) f)"
    52 
    52 
    53 definition range :: "natural \<Rightarrow> seed \<Rightarrow> natural \<times> seed" where
    53 definition range :: "natural \<Rightarrow> seed \<Rightarrow> natural \<times> seed" where
   134     by (auto simp add: select_weight_def select_def scomp_def split_def
   134     by (auto simp add: select_weight_def select_def scomp_def split_def
   135       fun_eq_iff pick_same [symmetric] less_natural_def)
   135       fun_eq_iff pick_same [symmetric] less_natural_def)
   136 qed
   136 qed
   137 
   137 
   138 
   138 
   139 subsection {* @{text ML} interface *}
   139 subsection \<open>@{text ML} interface\<close>
   140 
   140 
   141 code_reflect Random_Engine
   141 code_reflect Random_Engine
   142   functions range select select_weight
   142   functions range select select_weight
   143 
   143 
   144 ML {*
   144 ML \<open>
   145 structure Random_Engine =
   145 structure Random_Engine =
   146 struct
   146 struct
   147 
   147 
   148 open Random_Engine;
   148 open Random_Engine;
   149 
   149 
   175   in x end;
   175   in x end;
   176 
   176 
   177 end;
   177 end;
   178 
   178 
   179 end;
   179 end;
   180 *}
   180 \<close>
   181 
   181 
   182 hide_type (open) seed
   182 hide_type (open) seed
   183 hide_const (open) inc_shift minus_shift log "next" split_seed
   183 hide_const (open) inc_shift minus_shift log "next" split_seed
   184   iterate range select pick select_weight
   184   iterate range select pick select_weight
   185 hide_fact (open) range_def
   185 hide_fact (open) range_def