src/HOL/Random_Sequence.thy
author bulwahn
Fri Oct 21 11:17:14 2011 +0200 (2011-10-21)
changeset 45231 d85a2fdc586c
parent 42163 392fd6c4669c
child 50055 94041d602ecb
permissions -rw-r--r--
replacing code_inline by code_unfold, removing obsolete code_unfold, code_inline del now that the ancient code generator is removed
bulwahn@34948
     1
bulwahn@34948
     2
(* Author: Lukas Bulwahn, TU Muenchen *)
bulwahn@34948
     3
bulwahn@34948
     4
theory Random_Sequence
bulwahn@34948
     5
imports Quickcheck DSequence
bulwahn@34948
     6
begin
bulwahn@34948
     7
bulwahn@42163
     8
type_synonym 'a random_dseq = "code_numeral \<Rightarrow> code_numeral \<Rightarrow> Random.seed \<Rightarrow> ('a DSequence.dseq \<times> Random.seed)"
bulwahn@34948
     9
bulwahn@34948
    10
definition empty :: "'a random_dseq"
bulwahn@34948
    11
where
bulwahn@34948
    12
  "empty = (%nrandom size. Pair (DSequence.empty))"
bulwahn@34948
    13
bulwahn@34948
    14
definition single :: "'a => 'a random_dseq"
bulwahn@34948
    15
where
bulwahn@34948
    16
  "single x = (%nrandom size. Pair (DSequence.single x))"
bulwahn@34948
    17
bulwahn@34948
    18
definition bind :: "'a random_dseq => ('a \<Rightarrow> 'b random_dseq) \<Rightarrow> 'b random_dseq"
bulwahn@34948
    19
where
bulwahn@34948
    20
  "bind R f = (\<lambda>nrandom size s. let
bulwahn@34948
    21
     (P, s') = R nrandom size s;
bulwahn@34948
    22
     (s1, s2) = Random.split_seed s'
bulwahn@34948
    23
  in (DSequence.bind P (%a. fst (f a nrandom size s1)), s2))"
bulwahn@34948
    24
bulwahn@34948
    25
definition union :: "'a random_dseq => 'a random_dseq => 'a random_dseq"
bulwahn@34948
    26
where
bulwahn@34948
    27
  "union R1 R2 = (\<lambda>nrandom size s. let
bulwahn@34948
    28
     (S1, s') = R1 nrandom size s; (S2, s'') = R2 nrandom size s'
bulwahn@34948
    29
  in (DSequence.union S1 S2, s''))"
bulwahn@34948
    30
bulwahn@34948
    31
definition if_random_dseq :: "bool => unit random_dseq"
bulwahn@34948
    32
where
bulwahn@34948
    33
  "if_random_dseq b = (if b then single () else empty)"
bulwahn@34948
    34
bulwahn@34948
    35
definition not_random_dseq :: "unit random_dseq => unit random_dseq"
bulwahn@34948
    36
where
bulwahn@34948
    37
  "not_random_dseq R = (\<lambda>nrandom size s. let
bulwahn@34948
    38
     (S, s') = R nrandom size s
bulwahn@34948
    39
   in (DSequence.not_seq S, s'))"
bulwahn@34948
    40
bulwahn@34948
    41
fun Random :: "(code_numeral \<Rightarrow> Random.seed \<Rightarrow> (('a \<times> (unit \<Rightarrow> term)) \<times> Random.seed)) \<Rightarrow> 'a random_dseq"
bulwahn@34948
    42
where
bulwahn@34948
    43
  "Random g nrandom = (%size. if nrandom <= 0 then (Pair DSequence.empty) else
bulwahn@34948
    44
     (scomp (g size) (%r. scomp (Random g (nrandom - 1) size) (%rs. Pair (DSequence.union (DSequence.single (fst r)) rs)))))"
bulwahn@34948
    45
bulwahn@34948
    46
definition map :: "('a => 'b) => 'a random_dseq => 'b random_dseq"
bulwahn@34948
    47
where
bulwahn@34948
    48
  "map f P = bind P (single o f)"
bulwahn@34948
    49
bulwahn@34948
    50
(*
wenzelm@36176
    51
hide_const DSequence.empty DSequence.single DSequence.eval
bulwahn@34948
    52
  DSequence.map_seq DSequence.bind DSequence.union DSequence.if_seq DSequence.not_seq
bulwahn@34948
    53
  DSequence.map
bulwahn@34948
    54
*)
bulwahn@34948
    55
wenzelm@36176
    56
hide_const (open) empty single bind union if_random_dseq not_random_dseq Random map
bulwahn@34948
    57
wenzelm@36176
    58
hide_type DSequence.dseq random_dseq
wenzelm@36176
    59
hide_fact (open) empty_def single_def bind_def union_def if_random_dseq_def not_random_dseq_def Random.simps map_def
bulwahn@34948
    60
bulwahn@34948
    61
end