monad-bayes icon indicating copy to clipboard operation
monad-bayes copied to clipboard

Question: Free density monad vs. state density monad

Open turion opened this issue 1 year ago • 3 comments

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.

turion avatar Mar 09 '23 11:03 turion

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

reubenharry avatar Mar 09 '23 14:03 reubenharry

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.

idontgetoutmuch avatar Mar 11 '23 13:03 idontgetoutmuch

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.

reubenharry avatar Mar 11 '23 23:03 reubenharry