tweag / monad-bayes

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

Generalize from Double #191

Open reubenharry opened 2 years ago

reubenharry commented 2 years ago

Use type families in type classes to move from Double to an instance dependent number type. (Seems to work, just needs to be polished.)

idontgetoutmuch commented 1 year ago

I also tried to do this but without type families but have so far not succeeded. Here's a gist containing everything necessary in one file: https://github.com/tweag/monad-bayes/blob/Generalising-the-sampler/test/gist.hs.

I did also try your (@reubenharry) type family approach but was foxed by the type error given by this function

mhTrans' :: (StatefulGen g m, MonadReader g m) =>
             MonadDistribution m => Weighted (Density Identity) a -> Trace a -> m (Trace a)
mhTrans' m = mhTrans (hoistW (hoistD (return . runIdentity)) m)

I notice that you (@reubenharry) also didn't complete this: https://github.com/tweag/monad-bayes/blob/generalizeFromDouble2/src/Control/Monad/Bayes/Traced/Common.hs#L98.

-- | A variant of 'mhTrans' with an external sampling monad.
mhTrans' :: MonadSample m => Weighted (Density Identity) a -> Trace (Real m) a -> m (Trace (Real m) a)
mhTrans' m = undefined -- mhTrans (Weighted.hoist (FreeSampler.hoist (return . runIdentity)) m)

If either you (@reubenharry) or @turion had any ideas, they would be gratefully received.

PS I added in some code to do AD.

PPS My plan is to implement https://arxiv.org/pdf/2106.10238.pdf and then https://arxiv.org/pdf/2211.01100.pdf.