Skip to content

Instantly share code, notes, and snippets.

@clayrat
Created August 17, 2012 15:35
Show Gist options
  • Save clayrat/3379950 to your computer and use it in GitHub Desktop.
Save clayrat/3379950 to your computer and use it in GitHub Desktop.
prob-monad-h
module Dist where
import System.Random
data Dist a = Dist {
support :: [a],
gen :: StdGen -> (a, StdGen),
expect :: (a -> Float) -> Float
}
instance Monad Dist where
return a = Dist
{
support = [a],
gen = \g -> (a, g),
expect = \f -> f a
}
da >>= fdb = Dist
{
support = concat [support (fdb a) | a <- support da],
gen = \g -> let (a, g') = (gen da g) in (gen (fdb a) g'),
expect = \f -> expect da (\a -> expect (fdb a) f)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment