[Haskell-cafe] does try force evaluation of IO value

34 views
Skip to first unread message

s9gf...@gmail.com

unread,
Oct 5, 2012, 6:02:03 AM10/5/12
to haskel...@haskell.org
if i have some lazy io action, assuming (maction :: IO a) which may raise
exceptions while running, will "try" force evaluation of "a" to determine is
exception raised or not ?
Sorry for my broken english.

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

Roman Cheplyaka

unread,
Oct 5, 2012, 6:33:44 AM10/5/12
to s9gf...@gmail.com, haskel...@haskell.org
* s9gf...@gmail.com <s9gf...@gmail.com> [2012-10-05 16:02:03+0600]
> if i have some lazy io action, assuming (maction :: IO a) which may raise
> exceptions while running, will "try" force evaluation of "a" to determine is
> exception raised or not ?
> Sorry for my broken english.

No, it won't force the return value. To do that, use evaluate
http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Exception-Base.html#g:7

Roman

s9gf...@gmail.com

unread,
Oct 5, 2012, 6:52:52 AM10/5/12
to haskel...@haskell.org
> No, it won't force the return value. To do that, use evaluate
> http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Exc
> eption-Base.html#g:7
But what is happening when exception is raised when consuming value from
result of "try" ?
try has signature
try :: Exception e => IO a -> IO (Either e a)
so it must return either exception value either evaluated value and must not
raise exceptions, but how can "try" return (Right a) wthout making sure that
action will not raise any exception ?

Roman Cheplyaka

unread,
Oct 5, 2012, 7:07:12 AM10/5/12
to s9gf...@gmail.com, haskel...@haskell.org
* s9gf...@gmail.com <s9gf...@gmail.com> [2012-10-05 16:52:52+0600]
> > No, it won't force the return value. To do that, use evaluate
> > http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Exc
> > eption-Base.html#g:7
> But what is happening when exception is raised when consuming value from
> result of "try" ?
> try has signature
> try :: Exception e => IO a -> IO (Either e a)
> so it must return either exception value either evaluated value and must not
> raise exceptions, but how can "try" return (Right a) wthout making sure that
> action will not raise any exception ?

There's no promise that the returned lazy value won't throw any
exceptions. Example:

Prelude Control.Exception> r <- try (return $ error "bam") :: IO (Either SomeException Int)
Prelude Control.Exception> r
Right *** Exception: bam

try will only catch exceptions that arise from executing the IO action.

If you need to be sure that no exception will be raised, you need to
force it yourself, like this:

Prelude Control.Exception> r <- try (evaluate $ error "bam") :: IO (Either SomeException Int)
Prelude Control.Exception> r
Left bam

If the return type is a lazy structure, you may also need to use deepseq
('evaluate' only evaluates to WHNF).

Roman

s9gf...@gmail.com

unread,
Oct 5, 2012, 7:19:21 AM10/5/12
to haskel...@haskell.org
> Prelude Control.Exception> r <- try (return $ error "bam") :: IO (Either
> SomeException Int) Prelude Control.Exception> r
> Right *** Exception: bam
Wow, this is realy breaking news for me. Where can i read more about this
magic ?

Roman Cheplyaka

unread,
Oct 5, 2012, 7:53:37 PM10/5/12
to s9gf...@gmail.com, haskel...@haskell.org
* s9gf...@gmail.com <s9gf...@gmail.com> [2012-10-05 17:19:21+0600]
> > Prelude Control.Exception> r <- try (return $ error "bam") :: IO (Either
> > SomeException Int) Prelude Control.Exception> r
> > Right *** Exception: bam
> Wow, this is realy breaking news for me. Where can i read more about this
> magic ?

See chapter 19 of Real World Haskell, especially section "Laziness and
Exception Handling".
http://book.realworldhaskell.org/read/error-handling.html

Roman
Reply all
Reply to author
Forward
0 new messages