deanie

An embedded probabilistic programming language.
git clone git://git.jtobin.io/deanie.git
Log | Files | Refs | README | LICENSE

RVar.hs (718B)


      1 
      2 module Deanie.RVar (
      3     rvar
      4 
      5   -- * re-exported
      6 
      7   , sample
      8   ) where
      9 
     10 import Control.Monad
     11 import Deanie.Language
     12 import Data.Random hiding (rvar)
     13 import qualified Data.Random.Distribution.Bernoulli as RF
     14 import qualified Data.Random.Distribution.Beta as RF
     15 import qualified Data.Random.Distribution.Gamma as RF
     16 import qualified Data.Random.Distribution.Normal as RF
     17 
     18 rvar :: Program a -> RVar a
     19 rvar = iterM $ \case
     20     ProgramF (InL term) -> evalAlg term
     21     ProgramF (InR term) -> join (runAp rvar term)
     22   where
     23     evalAlg = \case
     24       BernoulliF p k  -> RF.bernoulli p >>= k
     25       BetaF a b k     -> RF.beta a b >>= k
     26       GammaF a b k    -> RF.gamma a b >>= k
     27       GaussianF m s k -> RF.normal m s >>= k
     28