Maybe monad and computations

Hello, I'm often using the Maybe monad to combine Maybe computations one after the other without conditionals. But I'm not sure how to do it, when I have several operations which return Maybe and I want all the indiviual values in the end, not a combination of the values. Currently I do: let allTogether = do ma <- doA mb <- doB return (ma, mb) case allTogether of Nothing -> ... Just (a, b) -> ... This way in this case I have one case instead of two, however I'm sure there must be a nicer way to achieve this result? Thank you! Emmanuel

Hi,
Can you provide little more details or code snippet as It is not
clear what you want to achieve.
Thanks
Divyanshu Ranjan
On Sat, Sep 7, 2013 at 1:19 AM, Emmanuel Touzery
Hello,
I'm often using the Maybe monad to combine Maybe computations one after the other without conditionals.
But I'm not sure how to do it, when I have several operations which return Maybe and I want all the indiviual values in the end, not a combination of the values.
Currently I do:
let allTogether = do ma <- doA mb <- doB return (ma, mb) case allTogether of Nothing -> ... Just (a, b) -> ...
This way in this case I have one case instead of two, however I'm sure there must be a nicer way to achieve this result?
Thank you!
Emmanuel
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

The `sequence`, `mapM`, and `mapM_` functions may be what you are looking for.
let allTogether = sequence [doA, doB, doC, doZ]
case allTogether of
Nothing -> ...
Just as -> ...
Use `maybe` and `fromMaybe` to avoid case statements:
fromMaybe "failure!" $ do
as <- sequence [doA, doB, doC, doZ]
return $ "Success: " ++ show (length as)
The `catMaybes` function can be used to execute each computation in
the list even if some of them return nothing.
On Fri, Sep 6, 2013 at 12:49 PM, Emmanuel Touzery
Hello,
I'm often using the Maybe monad to combine Maybe computations one after the other without conditionals.
But I'm not sure how to do it, when I have several operations which return Maybe and I want all the indiviual values in the end, not a combination of the values.
Currently I do:
let allTogether = do ma <- doA mb <- doB return (ma, mb) case allTogether of Nothing -> ... Just (a, b) -> ...
This way in this case I have one case instead of two, however I'm sure there must be a nicer way to achieve this result?
Thank you!
Emmanuel
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- -- Michael Steele

It seems like he wants something like sequence, but for tuples. Say:
sequence2 :: Monad m => (m a, m b) -> m (a, b)
sequence2 (ma, mb) = ma >>= \a -> mb >>= \b -> return (a, b)
But without knowing the use case it's hard to know whether or not this
could be done simply with "sequence" and mapM*
Nick
On Fri, Sep 6, 2013 at 2:28 PM, Michael Steele
The `sequence`, `mapM`, and `mapM_` functions may be what you are looking for.
let allTogether = sequence [doA, doB, doC, doZ] case allTogether of Nothing -> ... Just as -> ...
Use `maybe` and `fromMaybe` to avoid case statements:
fromMaybe "failure!" $ do as <- sequence [doA, doB, doC, doZ] return $ "Success: " ++ show (length as)
The `catMaybes` function can be used to execute each computation in the list even if some of them return nothing.
Hello,
I'm often using the Maybe monad to combine Maybe computations one after
On Fri, Sep 6, 2013 at 12:49 PM, Emmanuel Touzery
wrote: the other without conditionals.
But I'm not sure how to do it, when I have several operations which return Maybe and I want all the indiviual values in the end, not a combination of the values.
Currently I do:
let allTogether = do ma <- doA mb <- doB return (ma, mb) case allTogether of Nothing -> ... Just (a, b) -> ...
This way in this case I have one case instead of two, however I'm sure there must be a nicer way to achieve this result?
Thank you!
Emmanuel
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- -- Michael Steele
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Thank you (and the others) for tips. I'll check the suggestions but yes
sequence requires that all items have the same type so that sequence2 would
work better but feels un-idiomatic.
I needed such code several times in several contexts, so i assumed the
answer would be obvious. Maybe i'm structuring my code the wrong way for
haskell, i'll rethink those functions. Thanks for now!
Emmanuel
On Sep 6, 2013 10:37 PM, "Nicholas Vanderweit"
It seems like he wants something like sequence, but for tuples. Say:
sequence2 :: Monad m => (m a, m b) -> m (a, b) sequence2 (ma, mb) = ma >>= \a -> mb >>= \b -> return (a, b)
But without knowing the use case it's hard to know whether or not this could be done simply with "sequence" and mapM*
Nick
On Fri, Sep 6, 2013 at 2:28 PM, Michael Steele
wrote: The `sequence`, `mapM`, and `mapM_` functions may be what you are looking for.
let allTogether = sequence [doA, doB, doC, doZ] case allTogether of Nothing -> ... Just as -> ...
Use `maybe` and `fromMaybe` to avoid case statements:
fromMaybe "failure!" $ do as <- sequence [doA, doB, doC, doZ] return $ "Success: " ++ show (length as)
The `catMaybes` function can be used to execute each computation in the list even if some of them return nothing.
On Fri, Sep 6, 2013 at 12:49 PM, Emmanuel Touzery
wrote: Hello,
I'm often using the Maybe monad to combine Maybe computations one after the other without conditionals.
But I'm not sure how to do it, when I have several operations which return Maybe and I want all the indiviual values in the end, not a combination of the values.
Currently I do:
let allTogether = do ma <- doA mb <- doB return (ma, mb) case allTogether of Nothing -> ... Just (a, b) -> ...
This way in this case I have one case instead of two, however I'm sure there must be a nicer way to achieve this result?
Thank you!
Emmanuel
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- -- Michael Steele
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Hmm I've been looking some more. Turns out I'm pretty sure... What I've
been looking for is liftMx.
In this case, liftM2.
liftM2 ... doA doB
where "..." represents the rest of the computation as it did in the example
snippet I sent in my first email.
In the case of the Maybe monad, "..." will be called with the "Just" values
and only if both doA and doB are not Nothing.
and for "n" operations, there is "ap", I'll look at that one too.
emmanuel
On Fri, Sep 6, 2013 at 10:43 PM, Emmanuel Touzery
Thank you (and the others) for tips. I'll check the suggestions but yes sequence requires that all items have the same type so that sequence2 would work better but feels un-idiomatic. I needed such code several times in several contexts, so i assumed the answer would be obvious. Maybe i'm structuring my code the wrong way for haskell, i'll rethink those functions. Thanks for now!
Emmanuel On Sep 6, 2013 10:37 PM, "Nicholas Vanderweit"
wrote: It seems like he wants something like sequence, but for tuples. Say:
sequence2 :: Monad m => (m a, m b) -> m (a, b) sequence2 (ma, mb) = ma >>= \a -> mb >>= \b -> return (a, b)
But without knowing the use case it's hard to know whether or not this could be done simply with "sequence" and mapM*
Nick
On Fri, Sep 6, 2013 at 2:28 PM, Michael Steele
wrote: The `sequence`, `mapM`, and `mapM_` functions may be what you are looking for.
let allTogether = sequence [doA, doB, doC, doZ] case allTogether of Nothing -> ... Just as -> ...
Use `maybe` and `fromMaybe` to avoid case statements:
fromMaybe "failure!" $ do as <- sequence [doA, doB, doC, doZ] return $ "Success: " ++ show (length as)
The `catMaybes` function can be used to execute each computation in the list even if some of them return nothing.
On Fri, Sep 6, 2013 at 12:49 PM, Emmanuel Touzery
wrote: Hello,
I'm often using the Maybe monad to combine Maybe computations one after the other without conditionals.
But I'm not sure how to do it, when I have several operations which return Maybe and I want all the indiviual values in the end, not a combination of the values.
Currently I do:
let allTogether = do ma <- doA mb <- doB return (ma, mb) case allTogether of Nothing -> ... Just (a, b) -> ...
This way in this case I have one case instead of two, however I'm sure there must be a nicer way to achieve this result?
Thank you!
Emmanuel
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- -- Michael Steele
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Hi,
In that case you can use <$> and <*>.
Your code will look like, .... <$> doA <*> doB where ... is the
rest of the computation
Thanks
Divyanshu Ranjan
On Sat, Sep 7, 2013 at 2:34 AM, Emmanuel Touzery
Hmm I've been looking some more. Turns out I'm pretty sure... What I've been looking for is liftMx.
In this case, liftM2.
liftM2 ... doA doB
where "..." represents the rest of the computation as it did in the example snippet I sent in my first email.
In the case of the Maybe monad, "..." will be called with the "Just" values and only if both doA and doB are not Nothing.
and for "n" operations, there is "ap", I'll look at that one too.
emmanuel
On Fri, Sep 6, 2013 at 10:43 PM, Emmanuel Touzery
wrote: Thank you (and the others) for tips. I'll check the suggestions but yes sequence requires that all items have the same type so that sequence2 would work better but feels un-idiomatic. I needed such code several times in several contexts, so i assumed the answer would be obvious. Maybe i'm structuring my code the wrong way for haskell, i'll rethink those functions. Thanks for now!
Emmanuel
On Sep 6, 2013 10:37 PM, "Nicholas Vanderweit"
wrote: It seems like he wants something like sequence, but for tuples. Say:
sequence2 :: Monad m => (m a, m b) -> m (a, b) sequence2 (ma, mb) = ma >>= \a -> mb >>= \b -> return (a, b)
But without knowing the use case it's hard to know whether or not this could be done simply with "sequence" and mapM*
Nick
On Fri, Sep 6, 2013 at 2:28 PM, Michael Steele
wrote: The `sequence`, `mapM`, and `mapM_` functions may be what you are looking for.
let allTogether = sequence [doA, doB, doC, doZ] case allTogether of Nothing -> ... Just as -> ...
Use `maybe` and `fromMaybe` to avoid case statements:
fromMaybe "failure!" $ do as <- sequence [doA, doB, doC, doZ] return $ "Success: " ++ show (length as)
The `catMaybes` function can be used to execute each computation in the list even if some of them return nothing.
On Fri, Sep 6, 2013 at 12:49 PM, Emmanuel Touzery
wrote: Hello,
I'm often using the Maybe monad to combine Maybe computations one after the other without conditionals.
But I'm not sure how to do it, when I have several operations which return Maybe and I want all the indiviual values in the end, not a combination of the values.
Currently I do:
let allTogether = do ma <- doA mb <- doB return (ma, mb) case allTogether of Nothing -> ... Just (a, b) -> ...
This way in this case I have one case instead of two, however I'm sure there must be a nicer way to achieve this result?
Thank you!
Emmanuel
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- -- Michael Steele
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Many also use the Control.Applicative syntax for this sort of thing (most
types that have a Monad instance also have an Applicative instance):
(,) <$> ma <*> mb
which is equivalent to:
do
a <- ma
b <- mb
return (a, b)
On Fri, Sep 6, 2013 at 2:04 PM, Emmanuel Touzery
Hmm I've been looking some more. Turns out I'm pretty sure... What I've been looking for is liftMx.
In this case, liftM2.
liftM2 ... doA doB
where "..." represents the rest of the computation as it did in the example snippet I sent in my first email.
In the case of the Maybe monad, "..." will be called with the "Just" values and only if both doA and doB are not Nothing.
and for "n" operations, there is "ap", I'll look at that one too.
emmanuel
On Fri, Sep 6, 2013 at 10:43 PM, Emmanuel Touzery
wrote: Thank you (and the others) for tips. I'll check the suggestions but yes sequence requires that all items have the same type so that sequence2 would work better but feels un-idiomatic. I needed such code several times in several contexts, so i assumed the answer would be obvious. Maybe i'm structuring my code the wrong way for haskell, i'll rethink those functions. Thanks for now!
Emmanuel On Sep 6, 2013 10:37 PM, "Nicholas Vanderweit"
wrote: It seems like he wants something like sequence, but for tuples. Say:
sequence2 :: Monad m => (m a, m b) -> m (a, b) sequence2 (ma, mb) = ma >>= \a -> mb >>= \b -> return (a, b)
But without knowing the use case it's hard to know whether or not this could be done simply with "sequence" and mapM*
Nick
On Fri, Sep 6, 2013 at 2:28 PM, Michael Steele
wrote: The `sequence`, `mapM`, and `mapM_` functions may be what you are looking for.
let allTogether = sequence [doA, doB, doC, doZ] case allTogether of Nothing -> ... Just as -> ...
Use `maybe` and `fromMaybe` to avoid case statements:
fromMaybe "failure!" $ do as <- sequence [doA, doB, doC, doZ] return $ "Success: " ++ show (length as)
The `catMaybes` function can be used to execute each computation in the list even if some of them return nothing.
On Fri, Sep 6, 2013 at 12:49 PM, Emmanuel Touzery
wrote: Hello,
I'm often using the Maybe monad to combine Maybe computations one after the other without conditionals.
But I'm not sure how to do it, when I have several operations which return Maybe and I want all the indiviual values in the end, not a combination of the values.
Currently I do:
let allTogether = do ma <- doA mb <- doB return (ma, mb) case allTogether of Nothing -> ... Just (a, b) -> ...
This way in this case I have one case instead of two, however I'm sure there must be a nicer way to achieve this result?
Thank you!
Emmanuel
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- -- Michael Steele
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Hello,
And I tried to apply it to the haskell fay compiler, but failed:
http://stackoverflow.com/questions/18667530/dont-understand-this-liftm2-beha...
Not sure whether it's a fay bug or something about the fay monad or liftM2
which i don't understand.
emmanuel
On Sep 6, 2013 11:04 PM, "Emmanuel Touzery"
Hmm I've been looking some more. Turns out I'm pretty sure... What I've been looking for is liftMx.
In this case, liftM2.
liftM2 ... doA doB
where "..." represents the rest of the computation as it did in the example snippet I sent in my first email.
In the case of the Maybe monad, "..." will be called with the "Just" values and only if both doA and doB are not Nothing.
and for "n" operations, there is "ap", I'll look at that one too.
emmanuel
On Fri, Sep 6, 2013 at 10:43 PM, Emmanuel Touzery
wrote: Thank you (and the others) for tips. I'll check the suggestions but yes sequence requires that all items have the same type so that sequence2 would work better but feels un-idiomatic. I needed such code several times in several contexts, so i assumed the answer would be obvious. Maybe i'm structuring my code the wrong way for haskell, i'll rethink those functions. Thanks for now!
Emmanuel On Sep 6, 2013 10:37 PM, "Nicholas Vanderweit"
wrote: It seems like he wants something like sequence, but for tuples. Say:
sequence2 :: Monad m => (m a, m b) -> m (a, b) sequence2 (ma, mb) = ma >>= \a -> mb >>= \b -> return (a, b)
But without knowing the use case it's hard to know whether or not this could be done simply with "sequence" and mapM*
Nick
On Fri, Sep 6, 2013 at 2:28 PM, Michael Steele
wrote: The `sequence`, `mapM`, and `mapM_` functions may be what you are looking for.
let allTogether = sequence [doA, doB, doC, doZ] case allTogether of Nothing -> ... Just as -> ...
Use `maybe` and `fromMaybe` to avoid case statements:
fromMaybe "failure!" $ do as <- sequence [doA, doB, doC, doZ] return $ "Success: " ++ show (length as)
The `catMaybes` function can be used to execute each computation in the list even if some of them return nothing.
On Fri, Sep 6, 2013 at 12:49 PM, Emmanuel Touzery
wrote: Hello,
I'm often using the Maybe monad to combine Maybe computations one after the other without conditionals.
But I'm not sure how to do it, when I have several operations which return Maybe and I want all the indiviual values in the end, not a combination of the values.
Currently I do:
let allTogether = do ma <- doA mb <- doB return (ma, mb) case allTogether of Nothing -> ... Just (a, b) -> ...
This way in this case I have one case instead of two, however I'm sure there must be a nicer way to achieve this result?
Thank you!
Emmanuel
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- -- Michael Steele
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Hello Emmanuel,
And I tried to apply it to the haskell fay compiler, but failed: http://stackoverflow.com/questions/18667530/dont-understand-this-liftm2-beha...
Not sure whether it's a fay bug or something about the fay monad or liftM2 which i don't understand.
The point of lifting is, that you lift an operation into an other "context", so that the operation itself hasn't to operate in the same "context". I can't see how even your GHC version of 'operation' should work, because in both cases it should be something like: liftM2 (++) getValue1 getValue2 or in applicative style (++) <$> getValue1 <*> getValue2 Greetings, Daniel

Well I'm trying to combine two monads here, Maybe and either the Fay or the
IO monad. In the example code I just put return but otherwise in my real
code "operation" has side-effects and needs the Fay monad in the case of
the Fay program.
I understand how to use lift if "operation" is pure, without using monads,
but I have this trouble when it operates in another monad. I thought I
understood it, but looking at the difference in behaviour in the examples I
posted, maybe I don't.
Emmanuel
On Sat, Sep 7, 2013 at 9:11 AM, Daniel Trstenjak wrote: Hello Emmanuel, And I tried to apply it to the haskell fay compiler, but failed: http://stackoverflow.com/questions/18667530/dont-understand-this-liftm2-beha... Not sure whether it's a fay bug or something about the fay monad or liftM2
which i don't understand. The point of lifting is, that you lift an operation into an other
"context", so
that the operation itself hasn't to operate in the same "context". I can't see how even your GHC version of 'operation' should work,
because in both cases it should be something like: liftM2 (++) getValue1 getValue2 or in applicative style (++) <$> getValue1 <*> getValue2 Greetings,
Daniel _______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners

I got an helpful answer on stackoverflow that probably explains it all.
Otherwise if i add the type, i'll have to bind it to a concrete monad due
to the lack of typeclasses, that's why i didn't put a type. Thanks again.
On Sep 7, 2013 9:50 AM, "Daniel Trstenjak"
I thought I understood it, but looking at the difference in behaviour in the examples I posted, maybe I don't.
I would try to add a type signature for liftM2, perhaps you're than getting a more helpful error message.
Greetings, Daniel _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Am 07.09.2013 um 10:01 schrieb Emmanuel Touzery
I got an helpful answer on stackoverflow that probably explains it all. Otherwise if i add the type, i'll have to bind it to a concrete monad due to the lack of typeclasses, that's why i didn't put a type. Thanks again.
Ok, that explains a lot. But if you need this kind of operation a lot, you could still have something like: liftMaybe2 :: (a -> b -> c) -> Maybe a -> Maybe b -> Maybe c liftMaybe2 f (Just a) (Just b) = Just $ f a b liftMaybe2 _ _ _ = Nothing Greetings, Daniel

I can't see how even your GHC version of 'operation' should work ...
Ok, the GHC version puts the IO action into the Maybe and returns it then from 'process'. There's really no need for this extra wrapping of the string into the IO monad. I know nothing about Fay, so I don't know why this extra wrapping shouldn't work for Fay. Fay has an instance for Monad, right? Greetings, Daniel

Yes, I agree there is no need in the GHC version to wrap it with return,
you can imagine that "operation" instead asks the user through the command
line which operation to do, (++) or just pick the first or the second
string for instance. In that case it would really need the IO monad and
that is my situation.
I didn't take the time to code it for the purpose of the example but that
is my problem. In that case liftM2 offers me a solution, I can use it also
to call functions operating in another monad than the one the parameters
are in. In Fay I have that problem with Fay monad, and I don't know why.
It's more a Fay question, but Fay normally uses GHC for typecheck so I
thought it's maybe something with the Fay monad that is obvious for someone
more knowledgeable than me.
emmanuel
On Sat, Sep 7, 2013 at 9:38 AM, Daniel Trstenjak wrote: I can't see how even your GHC version of 'operation' should work ... Ok, the GHC version puts the IO action into the Maybe and returns it then
from 'process'. There's really no need for this extra wrapping of the
string
into the IO monad. I know nothing about Fay, so I don't know why this extra wrapping shouldn't
work for Fay. Fay has an instance for Monad, right? Greetings,
Daniel _______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners

I know nothing about Fay, so I don't know why this extra wrapping shouldn't work for Fay. Fay has an instance for Monad, right?
well when you write haskell code that you'll compile with Fay you can't use typeclasses. So AFAIK no, the "Fay" that I use in the Fay code doesn't implement the Monad typeclass. OK, so it seems my Haskell code is OK, and probably my stackoverflow question is legitimate about whether this valid Haskell code can work on Fay. Btw I used stackoverflow because the Fay website states that they use it as a preferred channel for support. Thank you for the insights! Emmanuel
participants (6)
-
Bob Ippolito
-
Daniel Trstenjak
-
divyanshu ranjan
-
Emmanuel Touzery
-
Michael Steele
-
Nicholas Vanderweit