We also applied the approach to biological modeling, as can be found in the paper “Modeling Genome Evolution with a DSEL for Probabilistic Programming” which made LtU back when it was fresh. This one is also on the website.

]]>Does nbsp work? (testing)

]]>Here’s the last bit of my code again with the less-than replaced by the unicode symbol “precedes”

die = optionP [(n,1/6) | n ≺- [1..6]]

test = collect $ liftM2 (+) die die

{-# OPTIONS_GHC -fglasgow-exts #-}

import Control.Monad.Writer

import Data.Ratio

import Data.Monoid

import qualified Data.Map as M

newtype Dist a = D (WriterT Probability [] a)

deriving (Functor, Monad)

newtype Probability = P Rational

deriving (Eq, Ord, Show, Num, Fractional)

instance Monoid Probability where

mempty = 1

mappend = (*)

mconcat = product

runDist :: Dist a -> [(a, Probability)]

runDist (D x) = runWriterT x

optionP :: [(a, Probability)] -> Dist a

optionP xs = D (WriterT xs)

(??) p = sum . map snd . filter (p . fst) . runDist

collect :: (Ord a) => Dist a -> Dist a

collect x = optionP . M.toList . M.fromListWith (+) $ runDist x

die = optionP [(n,1/6) | n

]]>Using ratios would seem a lot more intuitive to me. I’d rather see 1%6 instead of 16.7% for the probability of any given number on a six sided die.

Might it make sense to parameterize the Probability class so it could use any of the Fractional numbers? (Probability Float, or Probability Rational, etc)

]]>> dice = D [(1,1/6), (2,1/6), (3,1/6),(4,1/6),(5,1/6),(6/16)]

It’s either a weird (DWIM-eval) typo or it really works, since you also wrote this:

> fmap (*2) dice == D [ 2*x, p | (x,p)

]]>