I don't know about the category theory part, but the Haskell part is definitely not well typed with your original implementation and original type signature.
Considering
free :: (Functor t, Monad m) => (β a. ta β ma) β (β a. Free ta β ma)
when you map the pattern by Free tfta , you get
tfta :: t (Free ta) f :: forall a. ta -> ma free :: (forall a. ta -> ma) -> forall a. Free ta -> ma
And thus,
free f :: forall a. Free ta -> ma
leading to
fmap (free f) :: forall a. t (Free ta) -> t (ma)
So, to collapse this t (ma) into the desired ma , you need to apply f to it (to "turn t into m "), and then use the fact that m is a monad:
f . fmap (free f) :: forall a. t (Free ta) -> m (ma) join . f . fmap (free f) :: forall a. t (Free ta) -> ma
which means that you can correct your original definition by changing the second free branch:
{-# LANGUAGE RankNTypes, UnicodeSyntax #-} import Control.Monad.Free import Control.Monad free :: (Functor t, Monad m) => (β a. ta β ma) β (β a. Free ta β ma) free f (Pure a) = return a free f (Free tfta) = join . f . fmap (free f) $ tfta
These are typechecks, and probably maybe this is what you want :)
source share