tweag / monad-bayes

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

MMorph to replace `hoist` #192

Open reubenharry opened 2 years ago

reubenharry commented 2 years ago

hoist looks like it should subsumed by MMorph. (I recall problems with this previously but will try again)

reubenharry commented 2 years ago

Here's the problem I'm having with using MMorph:

In Population.hs, hoist has type

hoist ::
  Monad n =>
  (forall x. m x -> n x) ->
  Population m a ->
  Population n a
hoist f = fromWeightedList . f . population

But in MFunctor, it has type

hoist ::
  Monad m =>
  (forall x. m x -> n x) ->
  Population m a ->
  Population n a
hoist f = fromWeightedList . f . population
turion commented 2 years ago

That's a sign that hoist here isn't actually the simplest function it could be. You're using fromWeightedList which incurs this constraint, but that function does too much. You should instead compose the more straightforward hoist functions for ListT and StateT. The only issue remaining that I can see is that your ListT is long deprecated and thus not an instance of MFunctor anymore. That's a separate issue though and could be circumvented by using mapListT.

reubenharry commented 2 years ago

That all sounds sensible. We'd just have to make sure that the new semantics of hoist is right, since it is critical for the inference algorithms (see for instance its use in Inference/PMMH).