Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Control.Monad.Morph.hoist have a Monad constraint?

Control.Monad.Morph includes

class MFunctor t where
  hoist :: Monad m => (forall a. m a -> n a) -> t m b -> t n b

As far as I can tell, none of the included instances use the Monad m constraint. How might one do so? Are there valid instances that use the constraint (it's a bit hard for me to imagine how, given that hoist id = id)? What's the significance of the constraint being on m and not n?

like image 659
dfeuer Avatar asked Oct 08 '16 03:10

dfeuer


1 Answers

Control.Monad.Morph is a spinoff from pipes, so I'd guess it's there because the MFunctor instance for Proxy needs it... And sure enough it's used there.

instance MFunctor (Proxy a' a b' b) where
    hoist nat p0 = go (observe p0)
      where
        go p = case p of
            Request a' fa  -> Request a' (\a  -> go (fa  a ))
            Respond b  fb' -> Respond b  (\b' -> go (fb' b'))
            M          m   -> M (nat (m >>= \p' -> return (go p')))
            Pure    r      -> Pure r

I don't think it's needed though. m >>= return . f is fmap f m. It probably should be a Functor constraint and that code predates the implementation of the monad-applicative proposal.

like image 135
Cirdec Avatar answered Oct 12 '22 00:10

Cirdec