Created
August 17, 2012 15:36
-
-
Save clayrat/3379978 to your computer and use it in GitHub Desktop.
prob-monad-sz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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