[Haskell-cafe] Pulling a monad out of a functor

Stefan O'Rear stefanor at cox.net
Mon Mar 5 23:46:58 EST 2007

On Mon, Mar 05, 2007 at 10:12:44PM -0600, Bryan Burgers wrote:
> Haskellers,
> Recently, I have come across a couple of situations in which I need to
> "pull a monad out of a functor" so to speak. The first time it was
> needed, I didn't think much of it, but the second time started me
> thinking whether there is already an existing construct for this. The
> type signatures I have in mind are:
> pull :: (Monad m, Functor f) => f (m a) -> m (f a)
> fmapM :: (Monad m, Functor f) => (a -> m b) -> f a -> m (f b)
> fmapM f = pull . fmap f
> You may notice that these are already defined for the List functor as
> 'sequence' and 'mapM'. It seems to me that if this were a class, the
> implementor would be defining a traversing order by defining pull; in
> the list case this traversing sequence is clearly defined as head to
> tail.
> I am wondering if there is already a construct for this that I am not
> aware of (a class, a way to defined it using monads and functors that
> I am not aware of, an idiom)? Is this something that frequently
> happens for others, or is it a sign a few of my designs have been
> flawed recently?


pull is sequence, fmapM is mapM (also see the sequenceA, traverse
functions for the more general case of applicative functors vs. monads)


More information about the Haskell-Cafe mailing list