haskell - Why does `hoist` constrain the type parameter of this monad? -
i have function composes 2 monads:
comp :: monad m => m -> m b -> m b and 2 instances of such monads, on "inside" mfunctor,
ms :: monad m => m string ms = undefined tma :: (monad m, mfunctor t) => t m tma = undefined now if try compose ms tma:
tmas = hoist (\ma -> comp ma ms) tma i getting error:
not deduce (a ~ [char]) context (monad m, mfunctor t) bound inferred type of comp :: (monad m, mfunctor t) => t m b @ coroutine.hs:607:1-40 `a' rigid type variable bound type expected context: m -> m @ coroutine.hs:607:8 expected type: m actual type: m string which states a in ms has of arbitrary type: ms :: monad m => m a.
why , there way compose tma monads of specific parameters.
i can see signature of hoist is:
hoist :: (monad m, mfunctor t) => (forall a. m -> n a) -> t m b -> t n b but cannot picture how forall affecting trying do, if has effect.
switch order of arguments comp this:
tmas = hoist (\ma -> comp ms ma) tma -- or more simply: tmas = hoist (comp ms) tma the reason type of comp is:
comp :: (monad m) => m -> m b -> m b if set, ms second argument, b type-checks string , get:
(`comp` ms) :: (monad m) => m -> m string ... if set ms first argument, a type-checks string get:
(ms `comp`) :: (monad m) => m b -> m b that latter type correct type hoist since b universally quantified (i.e. "forall"ed).
to answer question correctness, answer universal quantification guarantees argument hoist modifies monad layer , not monad return value. however, if it's intention modify return value, too, hoist not want.
Comments
Post a Comment