Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's similar to fmap for monadic values?

Tags:

haskell

This should be easy for Haskell pros..

I've got a Maybe value,

> let a = Just 5

I can print it:

> print a
Just 5

But I want to apply an I/O action to the inside of the Maybe. The only way I've figured out how to do this without using case is:

> maybe (return ()) print a
5

However, this seems too verbose. First of all, return () is specific to the I/O monad, so I have to come up with a different "zero" for each monad I want to try this trick in.

I want to basically map an I/O action (print) onto the Maybe value and print it if it is Just, or don't do anything if it is Nothing. I want to express it somehow like,

> fmap print a

But this doesn't work since print is an IO action:

No instance for (Show (IO ()))

I tried Applicative, but can't figure out if there's a way to express it:

> print <$> a
No instance for (Show (IO ()))

Obviously I'm a bit confused about monads-inside-monads.. can anyone tell me the right way to most succinctly express this?

Thanks.

like image 771
Steve Avatar asked Feb 07 '11 21:02

Steve


2 Answers

pelotom's answer is the straightforward one. But not the fun one! sequence is the Haskell function that one can think of as flipping the order of type constructors between a list and a monad.

sequence :: (Monad m) => [m a] -> m [a]

Now what you want is, so to speak, to flip the order of type constructors between a Maybe and a monad. Data.Traversable exports a sequence function with just that capacity!

Data.Traversable.sequence :: (Traversable t, Monad m) => t (m a) -> m (t a)

This can specialize to Maybe (IO ()) -> IO (Maybe ()) like in your example.

Hence:

Prelude Data.Traversable> Data.Traversable.sequence (fmap print $ Nothing)
Nothing

Prelude Data.Traversable> Data.Traversable.sequence (fmap print $ Just 123)
123
Just ()

Note that there's also a sequenceA function which is slightly more general, working not just on Monads but all Applicatives.

So why use this approach? For Maybe the approach that takes it apart explicitly is fine. But what about a bigger data structure -- a Map for example? In that case, traverse, sequenceA and friends from Data.Traversable can be real handy.

Edit: as Ed'ka notes, traverse :: Applicative f => (a -> f b) -> t a -> f (t b) and so one can just write traverse print $ Just 123.

like image 186
sclv Avatar answered Sep 30 '22 03:09

sclv


First of all, return () is specific to the I/O monad, so I have to come up with a different "zero" for each monad I want to try this trick in.

return () is actually quite generic, as can be seen by its type:

Prelude> :t return ()
return () :: (Monad m) => m ()

I see nothing wrong with the maybe (return ()) print a approach.

like image 44
Tom Crockett Avatar answered Sep 30 '22 02:09

Tom Crockett