At 14:40 +0100 98/06/10, Simon L Peyton Jones wrote:
>Here's a reasonable design for exceptions in Haskell:
A think one can use a monadic approach, as a monad
(X, unit_X, bind_X): HaskellX -> HaskellX
where HaskellX is and extension of Haskell with exceptions.
>* A value of Haskell type T can be
> EITHER one of the values we know and love
> (bottom, or constructor, or function,
> depending on T),
>
> OR it can be a set of exceptional values.
So this says that X(T) = T | Exception, where Exception is a type in
HaskellX which labels objects in Haskell as exceptions. This monad is such
as old Haskell code can always be run in the new HaskellX.
>* raise :: String -> a
> (raise s) returns a single exceptional value, named by string s
I would suggest that an exception could not just be a string, but any
value in HaskellX.
>* All strict operations (case, +, etc) return the union of
> the exceptional values returned by their strict arguments
> For example, if both arguments to "+" return an exceptional value
> then "+" returns both. Similarly, any strict context.
Actually, if a function f in Haskell has type T, and is altered so that
it raises an exception, then its type becomes T | Exception. If a function
g: A -> B is altered to raise an exception, there is a difference between
the types
A -> (B | Exception) and (A | Exception -> B | Exception), but the monad
is such that one can always simplify to the latter, and one can use the
abbreviation
(A -> B) | Exception
for the latter.
So the type handling mechanism should not need to be that much more
complicated: Just replace T with T | Exception if the function raises an
exception. Functions that do not raise an exception can always be extended
to this, T | Exception, on the fly when encountering an exception via the
monad proeprties, so these functions need not get an altered type.
>* handle :: (String -> IO a) -> IO a -> IO a
> (handle h a) tries to perform the action a.
> If doing so delivers a set of exceptional values then
> apply the exception handler h to the string that names
> one of them. It is not defined which of the exceptional
> values is picked.
Then handle() should not only handle strings and IO, but any Exception
(of course), and any action a.
The handle function must be able to determine if it can handle the
exception, so it should have a function f: Exception -> Boolean as an extra
argument; the exception is handled only if this evaluates to "true" for the
exception it handles.
Hans Aberg
* Email: Hans Aberg <mailto:[EMAIL PROTECTED]>
* Home Page: <http://www.matematik.su.se/~haberg/>
* AMS member listing: <http://www.ams.org/cml/>