Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell - How to combine two monadic Maybe functions into a single function

Let's say I have these two data records, X and Y and following two functions:

f1 :: IO (Maybe [X])
f2 :: X -> IO (Maybe Y)

I need to call the f1 first and then, for each element of returned list (stored in IO (Maybe)) call the f2, which would result in something like IO (Maybe [IO (Maybe Y)]). How can I compose them to get something meaningful, such as

result :: Maybe (IO [Y])

or

result :: IO (Maybe [Y])

?

Thanks a lot for any help :-)

like image 799
xwinus Avatar asked Sep 10 '19 09:09

xwinus


3 Answers

Essentially it's using fmap sequenceA . sequenceA . fmap f2.

Using do syntax and breaking it down one step at a time:

result and result' gives you the same result.

data X = X
data Y = Y

f1 :: IO (Maybe [X])
f1 = undefined

f2 :: X -> IO (Maybe Y)
f2 = undefined

result :: IO (Maybe [Y])
result = do
 f1' <- f1
 case f1' of
   Just f1'' -> do
     let a = fmap f2 f1'' :: [IO (Maybe Y)]
     let b = sequenceA a :: IO [Maybe Y]
     fmap sequenceA b :: IO (Maybe [Y])
   Nothing -> pure Nothing

result' :: IO (Maybe [Y])
result' = do
 f1' <- f1
 case f1' of
   Just f1'' -> do
     fmap sequenceA . sequenceA . fmap f2 $ f1''
   Nothing -> pure Nothing
like image 161
Chris Stryczynski Avatar answered Oct 02 '22 14:10

Chris Stryczynski


A candidate could be:

result :: IO (Maybe [Y])
result = f1 >>= fmap sequenceA . mapM f2 . concat

Here concat is a function concat :: Foldable f => f [a] -> [a] that will convert a Nothing to an empty list, and a Just xs to xs.

We can then make a mapM f2 to generate an IO [Maybe a], and fmap :: Functor f => (a -> b) -> f a -> f b with sequenceA :: (Applicative f, Traversable t) => t (f a) -> f (t a) to convert an IO [Maybe Y] to an IO (Maybe [Y]).

sequenceA will return a Nothing if the list contains one or more Nothings, and return a Just xs if the list contains only Justs with xs the values that originally have been wrapped in Justs.

like image 38
Willem Van Onsem Avatar answered Oct 02 '22 14:10

Willem Van Onsem


note: this answer is not very suitable for Haskell newcomers because it involves a monad transformer.

Once we start mixing IO with the processing and generation of lists, I tend to jump straight away to the Stream monad transformer from streaming, that allows you to cleanly interleave the execution of IO actions with the "yielding" of values to be consumed downstream. In a way, Stream is an "effectful list" that performs effects every time we "extract" a value from it.

Consider this version of f1:

import Streaming
import qualified Streaming.Prelude as S
import Data.Foldable (fold)

f1' :: Stream (Of X) IO ()
f1' = do
    mxs <- lift f1
    S.each (fold mxs)  

lift promotes an IO a action to a Stream (Of x) a that doesn’t yield anything, but returns a as the "final value" of the Stream. (Streams yield zero or more values when consumed, and return a final value of a different type once they are exhausted).

Streaming.Prelude.each takes anything that can be converted to a list and returns a Stream that yields the element of the list. Basically, it promotes pure lists to effectful lists.

And Data.Foldable.fold is working here with the type fold :: Maybe [a] -> [a] to get rid of that Maybe.

Here's the corresponding version of f2:

f2' :: X -> Stream (Of Y) IO ()
f2' x = do
   ys <- lift (f2 x)
   S.each ys

Combining them is quite simple, thanks to Streaming.Prelude.for.

result' :: Stream (Of Y) IO ()
result' = S.for f1' f2'

With functions like Streaming.Prelude.take, we could read one Y from the result without having to perform the effects required by the next Y. (We do need to read all the Xs in one go though, because the f1 we are given already does that).

If we want to get all the Ys, we can do it with Streaming.Prelude.toList_:

result :: IO [Y]
result = S.toList_ result'
like image 40
danidiaz Avatar answered Oct 02 '22 13:10

danidiaz