src/HOL/Random.thy
author haftmann
Tue, 19 May 2009 13:57:32 +0200
changeset 31203 5c8fb4fd67e0
parent 31196 82ff416d7d66
child 31205 98370b26c2ce
permissions -rw-r--r--
moved Code_Index, Random and Quickcheck before Main
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 29806
diff changeset
     1
(* Author: Florian Haftmann, TU Muenchen *)
22528
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
     2
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
     3
header {* A HOL random engine *}
22528
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
     4
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
     5
theory Random
31203
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
     6
imports Code_Index List
22528
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
     7
begin
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
     8
29823
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
     9
notation fcomp (infixl "o>" 60)
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
    10
notation scomp (infixl "o\<rightarrow>" 60)
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
    11
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
    12
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    13
subsection {* Auxiliary functions *}
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    14
29823
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
    15
definition inc_shift :: "index \<Rightarrow> index \<Rightarrow> index" where
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    16
  "inc_shift v k = (if v = k then 1 else k + 1)"
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    17
29823
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
    18
definition minus_shift :: "index \<Rightarrow> index \<Rightarrow> index \<Rightarrow> index" where
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    19
  "minus_shift r k l = (if k < l then r + k - l else k - l)"
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    20
29823
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
    21
fun log :: "index \<Rightarrow> index \<Rightarrow> index" where
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    22
  "log b i = (if b \<le> 1 \<or> i < b then 1 else 1 + log b (i div b))"
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    23
30495
haftmann
parents: 29823
diff changeset
    24
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    25
subsection {* Random seeds *}
26038
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
    26
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
    27
types seed = "index \<times> index"
22528
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
    28
29823
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
    29
primrec "next" :: "seed \<Rightarrow> index \<times> seed" where
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    30
  "next (v, w) = (let
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    31
     k =  v div 53668;
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    32
     v' = minus_shift 2147483563 (40014 * (v mod 53668)) (k * 12211);
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    33
     l =  w div 52774;
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    34
     w' = minus_shift 2147483399 (40692 * (w mod 52774)) (l * 3791);
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    35
     z =  minus_shift 2147483562 v' (w' + 1) + 1
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    36
   in (z, (v', w')))"
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    37
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    38
lemma next_not_0:
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    39
  "fst (next s) \<noteq> 0"
29823
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
    40
  by (cases s) (auto simp add: minus_shift_def Let_def)
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    41
29823
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
    42
primrec seed_invariant :: "seed \<Rightarrow> bool" where
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    43
  "seed_invariant (v, w) \<longleftrightarrow> 0 < v \<and> v < 9438322952 \<and> 0 < w \<and> True"
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    44
29823
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
    45
definition split_seed :: "seed \<Rightarrow> seed \<times> seed" where
26038
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
    46
  "split_seed s = (let
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
    47
     (v, w) = s;
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
    48
     (v', w') = snd (next s);
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    49
     v'' = inc_shift 2147483562 v;
26038
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
    50
     s'' = (v'', w');
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    51
     w'' = inc_shift 2147483398 w;
26038
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
    52
     s''' = (v', w'')
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
    53
   in (s'', s'''))"
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
    54
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
    55
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    56
subsection {* Base selectors *}
22528
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
    57
30500
072daf3914c0 dropped spurious `quote` tags
haftmann
parents: 30495
diff changeset
    58
fun iterate :: "index \<Rightarrow> ('b \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a) \<Rightarrow> 'b \<Rightarrow> 'a \<Rightarrow> 'b \<times> 'a" where
30495
haftmann
parents: 29823
diff changeset
    59
  "iterate k f x = (if k = 0 then Pair x else f x o\<rightarrow> iterate (k - 1) f)"
22528
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
    60
30500
072daf3914c0 dropped spurious `quote` tags
haftmann
parents: 30495
diff changeset
    61
definition range :: "index \<Rightarrow> seed \<Rightarrow> index \<times> seed" where
30495
haftmann
parents: 29823
diff changeset
    62
  "range k = iterate (log 2147483561 k)
haftmann
parents: 29823
diff changeset
    63
      (\<lambda>l. next o\<rightarrow> (\<lambda>v. Pair (v + l * 2147483561))) 1
29823
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
    64
    o\<rightarrow> (\<lambda>v. Pair (v mod k))"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    65
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    66
lemma range:
30495
haftmann
parents: 29823
diff changeset
    67
  "k > 0 \<Longrightarrow> fst (range k s) < k"
haftmann
parents: 29823
diff changeset
    68
  by (simp add: range_def scomp_apply split_def del: log.simps iterate.simps)
26038
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
    69
29823
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
    70
definition select :: "'a list \<Rightarrow> seed \<Rightarrow> 'a \<times> seed" where
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
    71
  "select xs = range (Code_Index.of_nat (length xs))
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
    72
    o\<rightarrow> (\<lambda>k. Pair (nth xs (Code_Index.nat_of k)))"
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
    73
     
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    74
lemma select:
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    75
  assumes "xs \<noteq> []"
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    76
  shows "fst (select xs s) \<in> set xs"
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    77
proof -
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 29806
diff changeset
    78
  from assms have "Code_Index.of_nat (length xs) > 0" by simp
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    79
  with range have
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 29806
diff changeset
    80
    "fst (range (Code_Index.of_nat (length xs)) s) < Code_Index.of_nat (length xs)" by best
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    81
  then have
29815
9e94b7078fa5 mandatory prefix for index conversion operations
haftmann
parents: 29806
diff changeset
    82
    "Code_Index.nat_of (fst (range (Code_Index.of_nat (length xs)) s)) < length xs" by simp
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    83
  then show ?thesis
29823
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
    84
    by (simp add: scomp_apply split_beta select_def)
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
    85
qed
22528
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
    86
31180
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
    87
primrec pick :: "(index \<times> 'a) list \<Rightarrow> index \<Rightarrow> 'a" where
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
    88
  "pick (x # xs) i = (if i < fst x then snd x else pick xs (i - fst x))"
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
    89
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
    90
lemma pick_member:
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
    91
  "i < listsum (map fst xs) \<Longrightarrow> pick xs i \<in> set (map snd xs)"
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
    92
  by (induct xs arbitrary: i) simp_all
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
    93
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
    94
lemma pick_drop_zero:
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
    95
  "pick (filter (\<lambda>(k, _). k > 0) xs) = pick xs"
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
    96
  by (induct xs) (auto simp add: expand_fun_eq)
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
    97
31203
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
    98
lemma pick_same:
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
    99
  "l < length xs \<Longrightarrow> Random.pick (map (Pair 1) xs) (Code_Index.of_nat l) = nth xs l"
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   100
proof (induct xs arbitrary: l)
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   101
  case Nil then show ?case by simp
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   102
next
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   103
  case (Cons x xs) then show ?case by (cases l) simp_all
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   104
qed
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   105
31180
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
   106
definition select_weight :: "(index \<times> 'a) list \<Rightarrow> seed \<Rightarrow> 'a \<times> seed" where
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
   107
  "select_weight xs = range (listsum (map fst xs))
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
   108
   o\<rightarrow> (\<lambda>k. Pair (pick xs k))"
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
   109
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
   110
lemma select_weight_member:
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
   111
  assumes "0 < listsum (map fst xs)"
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
   112
  shows "fst (select_weight xs s) \<in> set (map snd xs)"
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
   113
proof -
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
   114
  from range assms
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
   115
    have "fst (range (listsum (map fst xs)) s) < listsum (map fst xs)" .
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
   116
  with pick_member
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
   117
    have "pick xs (fst (range (listsum (map fst xs)) s)) \<in> set (map snd xs)" .
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
   118
  then show ?thesis by (simp add: select_weight_def scomp_def split_def) 
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
   119
qed
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
   120
31203
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   121
lemma select_weigth_drop_zero:
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   122
  "Random.select_weight (filter (\<lambda>(k, _). k > 0) xs) = Random.select_weight xs"
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   123
proof -
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   124
  have "listsum (map fst [(k, _)\<leftarrow>xs . 0 < k]) = listsum (map fst xs)"
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   125
    by (induct xs) auto
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   126
  then show ?thesis by (simp only: select_weight_def pick_drop_zero)
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   127
qed
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   128
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   129
lemma select_weigth_select:
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   130
  assumes "xs \<noteq> []"
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   131
  shows "Random.select_weight (map (Pair 1) xs) = Random.select xs"
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   132
proof -
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   133
  have less: "\<And>s. fst (Random.range (Code_Index.of_nat (length xs)) s) < Code_Index.of_nat (length xs)"
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   134
    using assms by (intro range) simp
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   135
  moreover have "listsum (map fst (map (Pair 1) xs)) = Code_Index.of_nat (length xs)"
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   136
    by (induct xs) simp_all
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   137
  ultimately show ?thesis
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   138
    by (auto simp add: select_weight_def select_def scomp_def split_def
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   139
      expand_fun_eq pick_same [symmetric])
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   140
qed
5c8fb4fd67e0 moved Code_Index, Random and Quickcheck before Main
haftmann
parents: 31196
diff changeset
   141
29823
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
   142
definition select_default :: "index \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> seed \<Rightarrow> 'a \<times> seed" where
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
   143
  [code del]: "select_default k x y = range k
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
   144
     o\<rightarrow> (\<lambda>l. Pair (if l + 1 < k then x else y))"
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
   145
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
   146
lemma select_default_zero:
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
   147
  "fst (select_default 0 x y s) = y"
29823
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
   148
  by (simp add: scomp_apply split_beta select_default_def)
26038
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
   149
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
   150
lemma select_default_code [code]:
29823
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
   151
  "select_default k x y = (if k = 0
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
   152
    then range 1 o\<rightarrow> (\<lambda>_. Pair y)
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
   153
    else range k o\<rightarrow> (\<lambda>l. Pair (if l + 1 < k then x else y)))"
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
   154
proof
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
   155
  fix s
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
   156
  have "snd (range (Code_Index.of_nat 0) s) = snd (range (Code_Index.of_nat 1) s)"
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
   157
    by (simp add: range_def scomp_Pair scomp_apply split_beta)
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
   158
  then show "select_default k x y s = (if k = 0
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
   159
    then range 1 o\<rightarrow> (\<lambda>_. Pair y)
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
   160
    else range k o\<rightarrow> (\<lambda>l. Pair (if l + 1 < k then x else y))) s"
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
   161
    by (cases "k = 0") (simp_all add: select_default_def scomp_apply split_beta)
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
   162
qed
22528
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
   163
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
   164
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
   165
subsection {* @{text ML} interface *}
22528
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
   166
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
   167
ML {*
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
   168
structure Random_Engine =
22528
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
   169
struct
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
   170
26038
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
   171
type seed = int * int;
22528
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
   172
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
   173
local
26038
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
   174
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
   175
val seed = ref 
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
   176
  (let
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
   177
    val now = Time.toMilliseconds (Time.now ());
26038
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
   178
    val (q, s1) = IntInf.divMod (now, 2147483562);
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
   179
    val s2 = q mod 2147483398;
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
   180
  in (s1 + 1, s2 + 1) end);
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
   181
22528
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
   182
in
26038
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
   183
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
   184
fun run f =
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
   185
  let
26265
4b63b9e9b10d separated Random.thy from Quickcheck.thy
haftmann
parents: 26261
diff changeset
   186
    val (x, seed') = f (! seed);
26038
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
   187
    val _ = seed := seed'
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
   188
  in x end;
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
   189
22528
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
   190
end;
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
   191
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
   192
end;
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
   193
*}
8501c4a62a3c cleaned up HOL/ex/Code*.thy
haftmann
parents:
diff changeset
   194
31180
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
   195
hide (open) type seed
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
   196
hide (open) const inc_shift minus_shift log "next" seed_invariant split_seed
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
   197
  iterate range select pick select_weight select_default
dae7be64d614 hide names in theory Random
haftmann
parents: 30500
diff changeset
   198
29823
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
   199
no_notation fcomp (infixl "o>" 60)
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
   200
no_notation scomp (infixl "o\<rightarrow>" 60)
0ab754d13ccd session Reflecion renamed to Decision_Procs, moved Dense_Linear_Order there
haftmann
parents: 29815
diff changeset
   201
26038
55a02586776d towards quickcheck
haftmann
parents: 24994
diff changeset
   202
end
28145
af3923ed4786 dropped "run" marker in monad syntax
haftmann
parents: 28042
diff changeset
   203