Skip to content

Instantly share code, notes, and snippets.

@clayrat
Created August 17, 2012 15:36
Show Gist options
  • Save clayrat/3379978 to your computer and use it in GitHub Desktop.
Save clayrat/3379978 to your computer and use it in GitHub Desktop.
prob-monad-sz
import scalaz._
object distribMonad {
import Scalaz._
import scala.util.Random
abstract class Dist[A] {
def support: List[A]
def gen: Random => (A, Random)
def expect: (A => Double) => Double
}
def DistMonad = new Monad[Dist] {
def pure[A](a: => A): Dist[A] = new Dist[A] {
def support = List(a)
def gen(g: Random) = (a, g)
def expect(f: (A => Double)) = f(a)
}
def bind[A,B](da:Dist[A],fdb:A=>Dist[B]):Dist[B] = new Dist[B] {
def support = (for (a <- da.support) yield fdb(a).support).flatten
def gen(g: Random) = da.gen(g) match { case (a, g1) => (fdb(a)).gen(g1)}
def expect(f: (B => Double)) = da.expect {a => (fdb(a)).expect(f)}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment