[Haskell-cafe] <|> that short-circuits in IO ?

27 views
Skip to first unread message

Semen Trygubenko / Семен Тригубенко

unread,
Oct 30, 2014, 9:42:51 AM10/30/14
to Haskell-Cafe
Dear Haskell-Cafe,

Is there a way to get Control.Applicative.<|> to short-circuit when combining two IO actions?

E.g.

> let a = putStrLn "A" >> return (Left "hehe")
> let b = putStrLn "B" >> return (Right 42)

> Prelude> liftA2 (<|>) a b

A
B
Right 42

> liftA2 (<|>) b a

B
A
Right 42

(In the latter case I don't want A in the output…)

Many thanks for your help,
Semen



--
Семен Тригубенко http://trygub.com

Roman Cheplyaka

unread,
Oct 30, 2014, 9:50:19 AM10/30/14
to haskel...@haskell.org
On 30/10/14 15:42, Semen Trygubenko / Семен Тригубенко wrote:
> Dear Haskell-Cafe,
>
> Is there a way to get Control.Applicative.<|> to short-circuit when combining two IO actions?
>
> E.g.
>
>> let a = putStrLn "A" >> return (Left "hehe")
>> let b = putStrLn "B" >> return (Right 42)
>
>> Prelude> liftA2 (<|>) a b
>
> A
> B
> Right 42
>
>> liftA2 (<|>) b a
>
> B
> A
> Right 42
>
> (In the latter case I don't want A in the output…)

Wrap it into ExceptT (from the latest transformers), as in

runExceptT $ ExceptT a <|> ExceptT b

Roman

signature.asc

Kim-Ee Yeoh

unread,
Oct 31, 2014, 5:23:19 PM10/31/14
to Roman Cheplyaka, haskel...@haskell.org

On Thu, Oct 30, 2014 at 8:50 PM, Roman Cheplyaka <ro...@ro-che.info> wrote:
>> liftA2 (<|>) b a
>
> B
> A
> Right 42
>
> (In the latter case I don't want A in the output…)

Wrap it into ExceptT (from the latest transformers), as in

  runExceptT $ ExceptT a <|> ExceptT b

With the latest transformers, I still get

B
A
Right 42

i.e. the A hasn't been eliminated.

What am I missing?

-- Kim-Ee

Semen Trygubenko / Семен Тригубенко

unread,
Oct 31, 2014, 7:43:28 PM10/31/14
to Kim-Ee Yeoh, haskel...@haskell.org
In the actual use case
ErrorT was already wrapping IO and that's what I was combining with <|>, so,
for now, lacking the latest transformers, I have written a custom infix operator.

I haven't had a chance to test Roman's solution and assumed it to be correct.

So your observation is a valuable "data point" — thank you!

Apologies, I should have tested this myself sooner,
S.

Chris Wong

unread,
Nov 1, 2014, 2:10:04 AM11/1/14
to Kim-Ee Yeoh, Haskell Cafe
> With the latest transformers, I still get
>
> B
> A
> Right 42
>
> i.e. the A hasn't been eliminated.
>
> What am I missing?

Can you show us your code?

Looking at the source:

http://hackage.haskell.org/package/transformers-0.4.1.0/docs/src/Control-Monad-Trans-Except.html#line-169
http://hackage.haskell.org/package/transformers-0.4.1.0/docs/src/Control-Monad-Trans-Error.html#line-217

both ExceptT and ErrorT have short-circuiting <|> and mplus.

Chris
_______________________________________________
Haskell-Cafe mailing list
Haskel...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Roman Cheplyaka

unread,
Nov 1, 2014, 6:39:08 AM11/1/14
to Kim-Ee Yeoh, haskel...@haskell.org

I don't know what you're missing, but when I run the following code, it
doesn't print A.

import Control.Monad.Except
import Control.Applicative

a = putStrLn "A" >> return (Left "hehe")

b = putStrLn "B" >> return (Right 42)

main = runExceptT $ ExceptT b <|> ExceptT a

Roman

Kim-Ee Yeoh

unread,
Nov 2, 2014, 9:01:50 AM11/2/14
to haskel...@haskell.org
Chris, thank you for the links. I now understand ExceptT a lot better.

Roman, thank you for the full program. What happened was I copy-pasted

runExceptT $ ExceptT a <|> ExceptT b

from your email into ghci. But you said the latest transformers was
required, so I did the install. Finally, I saw an "A" printed out and
went "Hey, that's not right!"

Returning to the mail client, I referred back to Semen's original
query and copy-pasted from there.

But really it was A then B that I saw and not B then A.

So mea culpa.

Truth be told, all that while I was figuring out how to get Semen's
"liftA2 (<|>)" to work and suffered cognitively from the lazy I/O
solution I pursued.

-- Kim-Ee
Reply all
Reply to author
Forward
0 new messages