Re: [Haskell-cafe] Catch multiple exceptions using 'Control.Exception'

2013-07-07 Thread Albert Y. C. Lai

On 13-07-06 12:12 PM, Nikita Karetnikov wrote:

Is there an open sum type or a closed existential type?


OCaml has open sum types.

I haven't really seen closed existential types. (I have seen some 
approximations, but each lacks one last bit.)



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


Re: [Haskell-cafe] Catch multiple exceptions using 'Control.Exception'

2013-07-06 Thread Nikita Karetnikov
 Here you go:

   import Control.Exception
   import Data.Typeable

   syncExceptions :: SomeException - Maybe SomeException
   syncExceptions e
 | Just _ - cast e :: Maybe AsyncException = Nothing
 | otherwise = Just e

Thanks, but it doesn't work as expected:

*Main syncExceptions $ toException StackOverflow
Just stack overflow

It should return 'Nothing' instead:

*Main cast $ toException StackOverflow :: Maybe AsyncException
Nothing

I've received another solution, and it works fine.  I also have other
questions (see below).

 Start of forwarded message 
From: Albert Y. C. Lai tre...@vex.net

[...]

import Control.Exception

syncExceptions :: SomeException - Maybe SomeException
syncExceptions s = case fromException s :: Maybe AsyncException of
 Nothing - Just s
 Just _ - Nothing

testsync = tryJust syncExceptions (throwIO DivideByZero)
(is caught a Left)

testasync = tryJust syncExceptions (throwIO StackOverflow)
(is uncaught)

The change from the old scheme to the new scheme is, conceptually, from 
a closed sum type to an open existential type (SomeException); 
programmatically, from pattern-matching to type-casing aka downcasting 
(using fromException).

Therefore, changes to all call sites may be inevitable after all. For 
example:

   lr - tryJust syncExceptions action
   case lr of
 Right n - print n
 Left (IOException _) - putStrLn it's I/O
 Left (ArithException _) - putStrLn it's arith
 _ - putStrLn others

must be changed to:

   lr - tryJust syncExceptions action
   case lr of
 Right n - print n
 Left s - case fromException s :: Maybe IOException of
   Just _ - putStrLn it's I/O
   Nothing - case fromException s :: Maybe ArithException of
 Just _ - putStrLn it's arith
 Nothing - putStrLn others

at which point it is no longer clear that preserving syncExceptions 
implies minimum global change. It may be better off to use catches 
afterall. Or still use tryJust but every call site uses a tailored-made 
predicate for that site rather than thinking that one single 
syncExceptions fits all.
 End of forwarded message 

Could anyone elaborate on the following terms: a closed sum type and
an open existential type?  What is the meaning of the words open and
closed?  Is there an open sum type or a closed existential type?

Also, I thought that a sum type [1] should only have two value
constructors:

data Add a b = AddL a | AddR b

or

data Either a b = Left a | Right b

Is 'Exception' [2] a sum type?  If so, is it because of the associative
law?

[1] 
http://chris-taylor.github.io/blog/2013/02/10/the-algebra-of-algebraic-data-types/
[2] 
http://hackage.haskell.org/packages/archive/base/4.1.0.0/doc/html/Control-OldException.html


pgp3cyhQ1WilY.pgp
Description: PGP signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Catch multiple exceptions using 'Control.Exception'

2013-07-06 Thread Roman Cheplyaka
* Nikita Karetnikov nik...@karetnikov.org [2013-07-06 20:12:58+0400]
  Here you go:
 
import Control.Exception
import Data.Typeable
 
syncExceptions :: SomeException - Maybe SomeException
syncExceptions e
  | Just _ - cast e :: Maybe AsyncException = Nothing
  | otherwise = Just e
 
 Thanks, but it doesn't work as expected:
 
 *Main syncExceptions $ toException StackOverflow
 Just stack overflow
 
 It should return 'Nothing' instead:
 
 *Main cast $ toException StackOverflow :: Maybe AsyncException
 Nothing
 
 I've received another solution, and it works fine.  I also have other
 questions (see below).

Ah yes, fromException is the right function, of course. Sorry for
confusion.

 Could anyone elaborate on the following terms: a closed sum type and
 an open existential type?  What is the meaning of the words open and
 closed?  Is there an open sum type or a closed existential type?

Closed means that the set of possible constructors is, well, closed —
you have to decide on it when you're defining your type and you can't
later add anything to it.

Open means the opposite. A classical example of an open type is any type
in an OO language with subtyping, such as Java. You can extend the
Object type without limitations, without modifying the Object type
itself.

Exceptions are very similar in this regard — you can add new classes of
exceptions without a need to mess with the Control.Exception module,
just by defining a couple of methods.

For more details you may want to read An Extensible Dynamically-Typed
Hierarchy of Exceptions.

 Also, I thought that a sum type [1] should only have two value
 constructors:

 ...
 
 [1] 
 http://chris-taylor.github.io/blog/2013/02/10/the-algebra-of-algebraic-data-types/

That's just different terminology.
When talking about Haskell, we usually call any algebraic type with
multiple constructors a sum type.

Roman

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


[Haskell-cafe] Catch multiple exceptions using 'Control.Exception'

2013-07-03 Thread Nikita Karetnikov
I'm trying to update a package that uses 'Control.OldException' (works
with GHC 6.10.4).  Here is a relevant (and simplified) part:

import Control.OldException

-- | A predicate matching synchronous exceptions.
syncExceptions :: Exception - Maybe Exception
syncExceptions (AsyncException _) = Nothing
syncExceptions e  = Just e

throwAsync :: IO a
throwAsync = throwIO $ AsyncException StackOverflow

throwArith :: IO a
throwArith = throwIO $ ArithException DivideByZero

'syncExceptions' is usually used like this:

*Main tryJust syncExceptions throwArith
Left divide by zero
*Main tryJust syncExceptions throwAsync  -- pass through
*** Exception: stack overflow

The above doesn't work with GHC 7.6.3 because 'Control.OldException' [1]
was removed.  And 'Control.Exception' doesn't have the 'Exception' type.

Is there a way to adapt 'syncExceptions' to work with
'Control.Exception'?

[1] 
http://hackage.haskell.org/packages/archive/base/4.1.0.0/doc/html/Control-OldException.html


pgpxks_kAP6YQ.pgp
Description: PGP signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Catch multiple exceptions using 'Control.Exception'

2013-07-03 Thread Erik Hesselink
Perhaps you can use `catches` [0]?

Erik

[0] 
http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Exception.html#v:catches

On Wed, Jul 3, 2013 at 12:47 PM, Nikita Karetnikov
nik...@karetnikov.org wrote:
 I'm trying to update a package that uses 'Control.OldException' (works
 with GHC 6.10.4).  Here is a relevant (and simplified) part:

 import Control.OldException

 -- | A predicate matching synchronous exceptions.
 syncExceptions :: Exception - Maybe Exception
 syncExceptions (AsyncException _) = Nothing
 syncExceptions e  = Just e

 throwAsync :: IO a
 throwAsync = throwIO $ AsyncException StackOverflow

 throwArith :: IO a
 throwArith = throwIO $ ArithException DivideByZero

 'syncExceptions' is usually used like this:

 *Main tryJust syncExceptions throwArith
 Left divide by zero
 *Main tryJust syncExceptions throwAsync  -- pass through
 *** Exception: stack overflow

 The above doesn't work with GHC 7.6.3 because 'Control.OldException' [1]
 was removed.  And 'Control.Exception' doesn't have the 'Exception' type.

 Is there a way to adapt 'syncExceptions' to work with
 'Control.Exception'?

 [1] 
 http://hackage.haskell.org/packages/archive/base/4.1.0.0/doc/html/Control-OldException.html

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


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


Re: [Haskell-cafe] Catch multiple exceptions using 'Control.Exception'

2013-07-03 Thread Nikita Karetnikov
 Perhaps you can use `catches` [0]?

Maybe, but my idea is to replace 'syncExceptions' with a similar
function.  Otherwise, it'll be necessary to change (at least) all
functions that use 'syncExceptions'.  I'd like to avoid that.


pgp3z2Qli9UKD.pgp
Description: PGP signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Catch multiple exceptions using 'Control.Exception'

2013-07-03 Thread Roman Cheplyaka
* Nikita Karetnikov nik...@karetnikov.org [2013-07-03 15:50:16+0400]
  Perhaps you can use `catches` [0]?
 
 Maybe, but my idea is to replace 'syncExceptions' with a similar
 function.  Otherwise, it'll be necessary to change (at least) all
 functions that use 'syncExceptions'.  I'd like to avoid that.

Here you go:

  import Control.Exception
  import Data.Typeable

  syncExceptions :: SomeException - Maybe SomeException
  syncExceptions e
| Just _ - cast e :: Maybe AsyncException = Nothing
| otherwise = Just e

Roman

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