src/HOL/Random_Pred.thy
author wenzelm
Tue Sep 01 22:32:58 2015 +0200 (2015-09-01)
changeset 61076 bdc1e2f0a86a
parent 60758 d8d85a8172b5
child 67091 1393c2340eec
permissions -rw-r--r--
eliminated \<Colon>;
haftmann@51126
     1
haftmann@51126
     2
(* Author: Lukas Bulwahn, TU Muenchen *)
haftmann@51126
     3
wenzelm@60758
     4
section \<open>The Random-Predicate Monad\<close>
haftmann@51126
     5
haftmann@51126
     6
theory Random_Pred
haftmann@51126
     7
imports Quickcheck_Random
haftmann@51126
     8
begin
haftmann@51126
     9
haftmann@51143
    10
fun iter' :: "'a itself \<Rightarrow> natural \<Rightarrow> natural \<Rightarrow> Random.seed \<Rightarrow> ('a::random) Predicate.pred"
haftmann@51126
    11
where
haftmann@51126
    12
  "iter' T nrandom sz seed = (if nrandom = 0 then bot_class.bot else
haftmann@51126
    13
     let ((x, _), seed') = Quickcheck_Random.random sz seed
haftmann@51126
    14
   in Predicate.Seq (%u. Predicate.Insert x (iter' T (nrandom - 1) sz seed')))"
haftmann@51126
    15
haftmann@51143
    16
definition iter :: "natural \<Rightarrow> natural \<Rightarrow> Random.seed \<Rightarrow> ('a::random) Predicate.pred"
haftmann@51126
    17
where
haftmann@51126
    18
  "iter nrandom sz seed = iter' (TYPE('a)) nrandom sz seed"
haftmann@51126
    19
haftmann@51126
    20
lemma [code]:
haftmann@51126
    21
  "iter nrandom sz seed = (if nrandom = 0 then bot_class.bot else
haftmann@51126
    22
     let ((x, _), seed') = Quickcheck_Random.random sz seed
haftmann@51126
    23
   in Predicate.Seq (%u. Predicate.Insert x (iter (nrandom - 1) sz seed')))"
haftmann@51126
    24
   unfolding iter_def iter'.simps [of _ nrandom] ..
haftmann@51126
    25
haftmann@51126
    26
type_synonym 'a random_pred = "Random.seed \<Rightarrow> ('a Predicate.pred \<times> Random.seed)"
haftmann@51126
    27
haftmann@51126
    28
definition empty :: "'a random_pred"
haftmann@51126
    29
  where "empty = Pair bot"
haftmann@51126
    30
haftmann@51126
    31
definition single :: "'a => 'a random_pred"
haftmann@51126
    32
  where "single x = Pair (Predicate.single x)"
haftmann@51126
    33
haftmann@51126
    34
definition bind :: "'a random_pred \<Rightarrow> ('a \<Rightarrow> 'b random_pred) \<Rightarrow> 'b random_pred"
haftmann@51126
    35
  where
haftmann@51126
    36
    "bind R f = (\<lambda>s. let
haftmann@51126
    37
       (P, s') = R s;
haftmann@51126
    38
       (s1, s2) = Random.split_seed s'
haftmann@51126
    39
     in (Predicate.bind P (%a. fst (f a s1)), s2))"
haftmann@51126
    40
haftmann@51126
    41
definition union :: "'a random_pred \<Rightarrow> 'a random_pred \<Rightarrow> 'a random_pred"
haftmann@51126
    42
where
haftmann@51126
    43
  "union R1 R2 = (\<lambda>s. let
haftmann@51126
    44
     (P1, s') = R1 s; (P2, s'') = R2 s'
haftmann@51126
    45
   in (sup_class.sup P1 P2, s''))"
haftmann@51126
    46
haftmann@51126
    47
definition if_randompred :: "bool \<Rightarrow> unit random_pred"
haftmann@51126
    48
where
haftmann@51126
    49
  "if_randompred b = (if b then single () else empty)"
haftmann@51126
    50
haftmann@51143
    51
definition iterate_upto :: "(natural \<Rightarrow> 'a) => natural \<Rightarrow> natural \<Rightarrow> 'a random_pred"
haftmann@51126
    52
where
haftmann@51126
    53
  "iterate_upto f n m = Pair (Predicate.iterate_upto f n m)"
haftmann@51126
    54
haftmann@51126
    55
definition not_randompred :: "unit random_pred \<Rightarrow> unit random_pred"
haftmann@51126
    56
where
haftmann@51126
    57
  "not_randompred P = (\<lambda>s. let
haftmann@51126
    58
     (P', s') = P s
haftmann@51126
    59
   in if Predicate.eval P' () then (Orderings.bot, s') else (Predicate.single (), s'))"
haftmann@51126
    60
haftmann@51126
    61
definition Random :: "(Random.seed \<Rightarrow> ('a \<times> (unit \<Rightarrow> term)) \<times> Random.seed) \<Rightarrow> 'a random_pred"
haftmann@51126
    62
  where "Random g = scomp g (Pair o (Predicate.single o fst))"
haftmann@51126
    63
haftmann@51126
    64
definition map :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a random_pred \<Rightarrow> 'b random_pred"
haftmann@51126
    65
  where "map f P = bind P (single o f)"
haftmann@51126
    66
haftmann@51126
    67
hide_const (open) iter' iter empty single bind union if_randompred
haftmann@51126
    68
  iterate_upto not_randompred Random map
haftmann@51126
    69
haftmann@51126
    70
hide_fact iter'.simps
haftmann@51126
    71
  
haftmann@51126
    72
hide_fact (open) iter_def empty_def single_def bind_def union_def
haftmann@51126
    73
  if_randompred_def iterate_upto_def not_randompred_def Random_def map_def 
haftmann@51126
    74
haftmann@51126
    75
end
haftmann@51126
    76