|
1 |
|
2 (* Author: Lukas Bulwahn, TU Muenchen *) |
|
3 |
|
4 theory Random_Sequence |
|
5 imports Quickcheck DSequence |
|
6 begin |
|
7 |
|
8 types 'a random_dseq = "code_numeral \<Rightarrow> code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('a DSequence.dseq \<times> Random.seed)" |
|
9 |
|
10 definition empty :: "'a random_dseq" |
|
11 where |
|
12 "empty = (%nrandom size. Pair (DSequence.empty))" |
|
13 |
|
14 definition single :: "'a => 'a random_dseq" |
|
15 where |
|
16 "single x = (%nrandom size. Pair (DSequence.single x))" |
|
17 |
|
18 definition bind :: "'a random_dseq => ('a \<Rightarrow> 'b random_dseq) \<Rightarrow> 'b random_dseq" |
|
19 where |
|
20 "bind R f = (\<lambda>nrandom size s. let |
|
21 (P, s') = R nrandom size s; |
|
22 (s1, s2) = Random.split_seed s' |
|
23 in (DSequence.bind P (%a. fst (f a nrandom size s1)), s2))" |
|
24 |
|
25 definition union :: "'a random_dseq => 'a random_dseq => 'a random_dseq" |
|
26 where |
|
27 "union R1 R2 = (\<lambda>nrandom size s. let |
|
28 (S1, s') = R1 nrandom size s; (S2, s'') = R2 nrandom size s' |
|
29 in (DSequence.union S1 S2, s''))" |
|
30 |
|
31 definition if_random_dseq :: "bool => unit random_dseq" |
|
32 where |
|
33 "if_random_dseq b = (if b then single () else empty)" |
|
34 |
|
35 definition not_random_dseq :: "unit random_dseq => unit random_dseq" |
|
36 where |
|
37 "not_random_dseq R = (\<lambda>nrandom size s. let |
|
38 (S, s') = R nrandom size s |
|
39 in (DSequence.not_seq S, s'))" |
|
40 |
|
41 fun Random :: "(code_numeral \<Rightarrow> Random.seed \<Rightarrow> (('a \<times> (unit \<Rightarrow> term)) \<times> Random.seed)) \<Rightarrow> 'a random_dseq" |
|
42 where |
|
43 "Random g nrandom = (%size. if nrandom <= 0 then (Pair DSequence.empty) else |
|
44 (scomp (g size) (%r. scomp (Random g (nrandom - 1) size) (%rs. Pair (DSequence.union (DSequence.single (fst r)) rs)))))" |
|
45 |
|
46 definition map :: "('a => 'b) => 'a random_dseq => 'b random_dseq" |
|
47 where |
|
48 "map f P = bind P (single o f)" |
|
49 |
|
50 (* |
|
51 hide const DSequence.empty DSequence.single DSequence.eval |
|
52 DSequence.map_seq DSequence.bind DSequence.union DSequence.if_seq DSequence.not_seq |
|
53 DSequence.map |
|
54 *) |
|
55 |
|
56 hide (open) const empty single bind union if_random_dseq not_random_dseq Random map |
|
57 |
|
58 hide type DSequence.dseq random_dseq |
|
59 hide (open) fact empty_def single_def bind_def union_def if_random_dseq_def not_random_dseq_def Random_def map_def |
|
60 |
|
61 end |