tweag / monad-bayes

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

Question: Free density monad vs. state density monad #263

Open turion opened 1 year ago

turion commented 1 year ago

In the docs we describe two density monads (https://github.com/tweag/monad-bayes/blob/master/docs/docs/usage.md#density):

Monad-bayes offers two implementations, in Control.Monad.Bayes.Density.State and Control.Monad.Bayes.Density.Free. The first is slow but easy to understand, the second is more sophisticated, but faster.

In the code we have both variants, but only the free density monad (supposedly faster) is used outside this part:

https://github.com/tweag/monad-bayes/blob/31579b3e454ea19f7b5b5f12f34486e754066d31/src/Control/Monad/Bayes/Traced/Common.hs#L84

@reubenharry @idontgetoutmuch Is it fair to say that realistically, we will only ever use the free density monad, and the state density monad is only for didactic purposes? If so, I'd vote for removing the corresponding code and simplifying the leftovers, and keep the state density only in the docs, which would be slightly adapted.

reubenharry commented 1 year ago

That is fair to say, and I don't oppose that.

idontgetoutmuch commented 1 year ago

I tried to use the state version instead of the free version but get an unsatisfied constraint. If here:

https://github.com/tweag/monad-bayes/blob/free-vs-state/src/Control/Monad/Bayes/Traced/Basic.hs#L26

you import import Control.Monad.Bayes.Density.State (Density) then I get

src/Control/Monad/Bayes/Traced/Basic.hs:60:19: error:
    • Could not deduce (MonadDistribution Identity)
        arising from a use of ‘random’
      from the context: MonadDistribution m
        bound by the instance declaration
        at src/Control/Monad/Bayes/Traced/Basic.hs:59:10-60
    • In the first argument of ‘Traced’, namely ‘random’
      In the expression: Traced random (fmap singleton random)
      In an equation for ‘random’:
          random = Traced random (fmap singleton random)
   |
60 |   random = Traced random (fmap singleton random)

Can anyone explain what is happening? I presume at some point the state version worked.

reubenharry commented 1 year ago

Hmm, actually not entirely sure it ever worked. This is perhaps irrelevant since it looks like we'll remove this. The error looks familiar though.