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

27 рдмрд╛рд░ рджреЗрдЦрд╛ рдЧрдпрд╛
рдирд╣реАрдВ рдкрдврд╝реЗ рдЧрдП рдкрд╣рд▓реЗ рдореИрд╕реЗрдЬ рдкрд░ рдЬрд╛рдПрдВ

Semen Trygubenko / ╨б╨╡╨╝╨╡╨╜ ╨в╤А╨╕╨│╤Г╨▒╨╡╨╜╨║╨╛

рдирд╣реАрдВ рдкрдврд╝реА рдЧрдИ,
30 рдЕрдХреНрддреВре░ 2014, 9:42:51 am30/10/14
рдИрдореЗрд▓ рдкрд╛рдиреЗ рд╡рд╛рд▓рд╛ 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

рдирд╣реАрдВ рдкрдврд╝реА рдЧрдИ,
30 рдЕрдХреНрддреВре░ 2014, 9:50:19 am30/10/14
рдИрдореЗрд▓ рдкрд╛рдиреЗ рд╡рд╛рд▓рд╛ 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

рдирд╣реАрдВ рдкрдврд╝реА рдЧрдИ,
31 рдЕрдХреНрддреВре░ 2014, 5:23:19 pm31/10/14
рдИрдореЗрд▓ рдкрд╛рдиреЗ рд╡рд╛рд▓рд╛ 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 / ╨б╨╡╨╝╨╡╨╜ ╨в╤А╨╕╨│╤Г╨▒╨╡╨╜╨║╨╛

рдирд╣реАрдВ рдкрдврд╝реА рдЧрдИ,
31 рдЕрдХреНрддреВре░ 2014, 7:43:28 pm31/10/14
рдИрдореЗрд▓ рдкрд╛рдиреЗ рд╡рд╛рд▓рд╛ 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

рдирд╣реАрдВ рдкрдврд╝реА рдЧрдИ,
1 рдирд╡ре░ 2014, 2:10:04 am1/11/14
рдИрдореЗрд▓ рдкрд╛рдиреЗ рд╡рд╛рд▓рд╛ 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

рдирд╣реАрдВ рдкрдврд╝реА рдЧрдИ,
1 рдирд╡ре░ 2014, 6:39:08 am1/11/14
рдИрдореЗрд▓ рдкрд╛рдиреЗ рд╡рд╛рд▓рд╛ 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

рдирд╣реАрдВ рдкрдврд╝реА рдЧрдИ,
2 рдирд╡ре░ 2014, 9:01:50 am2/11/14
рдИрдореЗрд▓ рдкрд╛рдиреЗ рд╡рд╛рд▓рд╛ 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
рд╕рднреА рдкреНрд░рд╖рдХреЛрдВ рдХреЛ рдЙрддреНрддрд░ рджреЗрдВ
рд▓реЗрдЦрдХ рдХреЛ рдЙрддреНрддрд░ рджреЗрдВ
рдЖрдЧреЗ рднреЗрдЬреЗрдВ
0 рдирдпрд╛ рдореИрд╕реЗрдЬ