src/HOL/ex/CodeRandom.thy
changeset 21404 eb85850d3eb7
parent 21192 5fe5cd5fede7
child 21545 54cc492d80a9
equal deleted inserted replaced
21403:dd58f13a8eb4 21404:eb85850d3eb7
    28 
    28 
    29 axiomatization
    29 axiomatization
    30   random_seed :: "randseed \<Rightarrow> nat"
    30   random_seed :: "randseed \<Rightarrow> nat"
    31 
    31 
    32 definition
    32 definition
    33   random :: "nat \<Rightarrow> randseed \<Rightarrow> nat \<times> randseed"
    33   random :: "nat \<Rightarrow> randseed \<Rightarrow> nat \<times> randseed" where
    34   "random n s = (random_seed s mod n, random_shift s)"
    34   "random n s = (random_seed s mod n, random_shift s)"
    35 
    35 
    36 lemma random_bound:
    36 lemma random_bound:
    37   assumes "0 < n"
    37   assumes "0 < n"
    38   shows "fst (random n s) < n"
    38   shows "fst (random n s) < n"
    43 
    43 
    44 lemma random_random_seed [simp]:
    44 lemma random_random_seed [simp]:
    45   "snd (random n s) = random_shift s" unfolding random_def by simp
    45   "snd (random n s) = random_shift s" unfolding random_def by simp
    46 
    46 
    47 definition
    47 definition
    48   select :: "'a list \<Rightarrow> randseed \<Rightarrow> 'a \<times> randseed"
    48   select :: "'a list \<Rightarrow> randseed \<Rightarrow> 'a \<times> randseed" where
    49   [simp]: "select xs = (do
    49   [simp]: "select xs = (do
    50       n \<leftarrow> random (length xs);
    50       n \<leftarrow> random (length xs);
    51       return (nth xs n)
    51       return (nth xs n)
    52     done)"
    52     done)"
    53   select_weight :: "(nat \<times> 'a) list \<Rightarrow> randseed \<Rightarrow> 'a \<times> randseed"
    53 definition
       
    54   select_weight :: "(nat \<times> 'a) list \<Rightarrow> randseed \<Rightarrow> 'a \<times> randseed" where
    54   [simp]: "select_weight xs = (do
    55   [simp]: "select_weight xs = (do
    55       n \<leftarrow> random (foldl (op +) 0 (map fst xs));
    56       n \<leftarrow> random (foldl (op +) 0 (map fst xs));
    56       return (pick xs n)
    57       return (pick xs n)
    57     done)"
    58     done)"
    58 
    59 
   121     unfolding select_weight_def sum_length pick_nth_random_do
   122     unfolding select_weight_def sum_length pick_nth_random_do
   122     by simp
   123     by simp
   123 qed
   124 qed
   124 
   125 
   125 definition
   126 definition
   126   random_int :: "int \<Rightarrow> randseed \<Rightarrow> int * randseed"
   127   random_int :: "int \<Rightarrow> randseed \<Rightarrow> int * randseed" where
   127   "random_int k = (do n \<leftarrow> random (nat k); return (int n) done)"
   128   "random_int k = (do n \<leftarrow> random (nat k); return (int n) done)"
   128 
   129 
   129 lemma random_nat [code]:
   130 lemma random_nat [code]:
   130   "random n = (do k \<leftarrow> random_int (int n); return (nat k) done)"
   131   "random n = (do k \<leftarrow> random_int (int n); return (nat k) done)"
   131 unfolding random_int_def by simp
   132 unfolding random_int_def by simp