[Haskell-cafe] Control.Exception Funny

Claus Reinke claus.reinke at talk21.com
Sat Nov 29 07:01:49 EST 2008


>> CE.catch :: (CE.Exception e) => IO a -> (e -> IO a) -> IO a
> 
>> foo d = CE.catch (openFile d ReadMode >> return ())
>>                  (\e -> hPutStr stderr ("Couldn't open "++ d ++": " ++ show e))

btw, if your handler cannot return the same type as your action, is this
the right place to catch the exceptions?
 
>> Run.hs:70:8:
>>     Ambiguous type variable `e' in the constraint:
>>       `CE.Exception e'
>>         arising from a use of `CE.catch' at Run.hs:(70,8)-(71,78)
>>     Probable fix: add a type signature that fixes these type variable(s)
> 
> Now I think I never used to get this under 6.8.2 but I don't easily have
> a 6.8.2 to try it out on.

That would be the new extensible exceptions - instead of a single 
non-extendable exception type (no ambiguities), there's now an
extendable class of exceptions.
 
> Doing what the compiler suggests doesn't work for obvious reasons:
> 
>> foo :: CE.Exception e => FilePath -> IO ()
>> foo d = CE.catch (openFile d ReadMode >> return ())
>>                  (\e -> hPutStr stderr ("Couldn't open "++ d ++": " ++ show e))
> 
>> Run.hs:69:0:
>>     Ambiguous constraint `CE.Exception e'
>>         At least one of the forall'd type variables mentioned by the constraint
>>         must be reachable from the type after the '=>'
>>     In the type signature for `foo':
>>       foo :: (CE.Exception e) => FilePath -> IO ()

The suggestion was to fix the type 'e'. Neither your signature, nor your
exception handler do that. I found the documentation less than helpful
for this recent switch, but if you look at the instances of the Exception
class:

http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Exception.html#1

you'll see 'IOException' listed, so 'show (e::IOException)' might do
what you want.
 
> There seems to be a ticket for it
> (http://hackage.haskell.org/trac/ghc/ticket/2819) but this doesn't give
> a suggested example that compiles.

I've annotated the ticket. Please check whether the suggested 
explanation would be helpful, and report any other places that
have not been updated to the new exception system.

Claus



More information about the Haskell-Cafe mailing list