tweag / monad-bayes

A library for probabilistic programming in Haskell.
MIT License
407 stars 62 forks source link

Lazy sampler is not a monad? #212

Open turion opened 1 year ago

turion commented 1 year ago

Is the lazy sampler actually a monad? I believe it doesn't satisfy the monad laws:

ghci> import Control.Monad.Bayes.Sampler.Lazy
ghci> import System.Random
ghci> g <- newStdGen
ghci> let prog = (Control.Monad.Bayes.Class.random :: Control.Monad.Bayes.Sampler.Lazy.Sampler Double) >>= return
ghci> let prog' = (Control.Monad.Bayes.Class.random :: Control.Monad.Bayes.Sampler.Lazy.Sampler Double)
ghci> runSampler prog $ randomTree g
0.7318877896125477
ghci> runSampler prog' $ randomTree g
0.23172949590893044

Even worse, it's not even a functor with liftM:

ghci> runSampler (liftM id prog) $ randomTree g
0.6649028878458447

This is because depending on how we construct the program, the random number generator gets split or not. But this is not coassociative (there are no nontrivial comonoids in Haskell) and thus the monad laws don't hold.

reubenharry commented 1 year ago

This sounds like an issue to take up with Sam Staton and his students - this code comes from their work on LazyPPL, and since they're into denotational semantics, this seems like a thing they'd care about. I've previously been in touch with them, so definitely something we could just ask them.