A program which never crashes (even when a function calls "error")

[It is a philosophical question, not a practical programming problem.] I'm used, in imperative programming languages with exceptions (like Python) to call any function without fear of stopping the program because I can always catch the exceptions with things like (Python): while not over: try: code which may raise an exception... except Exception e: do something clever How to do it in Haskell? How can I call functions like Prelude.head while being sure my program won't stop, even if I call head on an empty list (thus calling "error")?

On 8/1/06, Stephane Bortzmeyer
How to do it in Haskell? How can I call functions like Prelude.head while being sure my program won't stop, even if I call head on an empty list (thus calling "error")?
Try looking at Control.Exception. For example:
module Test where
import Control.Exception import Prelude hiding (catch)
example = (do print (head (tail "a")) return "ok") `catch` (\e -> do putStrLn ("Caught exception: " ++ show e) return "error")
produces: *Test> z <- example Caught exception: Prelude.head: empty list *Test> z "error" This might be the beginning of what you want. /g

On Tue, Aug 01, 2006 at 08:52:06AM +0200, Stephane Bortzmeyer wrote:
To: haskell-cafe@haskell.org From: Stephane Bortzmeyer
Date: Tue, 1 Aug 2006 08:52:06 +0200 Subject: [Haskell-cafe] A program which never crashes (even when a function calls "error") [It is a philosophical question, not a practical programming problem.]
I'm used, in imperative programming languages with exceptions (like Python) to call any function without fear of stopping the program because I can always catch the exceptions with things like (Python):
while not over: try: code which may raise an exception... except Exception e: do something clever
How to do it in Haskell? How can I call functions like Prelude.head while being sure my program won't stop, even if I call head on an empty list (thus calling "error")?
in haskell98, you can't. if you cannot prove a list will always be non-empty, you should use pattern matching instead of head. one disadvantage of exceptions is that the byte code tends to be slow and ugly and hard to generate, in particular in pure lazy languages. but admittedly sometimes exceptions are cool. therefore ghc comes with a quite sophisticated and mature exception handling library. http://www.haskell.org/ghc/docs/latest/html/libraries/base/Control-Exception... looks a little different from python, but should do the trick. cheers, matthias

On Tue, 1 Aug 2006, Stephane Bortzmeyer wrote:
[It is a philosophical question, not a practical programming problem.]
I'm used, in imperative programming languages with exceptions (like Python) to call any function without fear of stopping the program because I can always catch the exceptions with things like (Python):
while not over: try: code which may raise an exception... except Exception e: do something clever
How to do it in Haskell? How can I call functions like Prelude.head while being sure my program won't stop, even if I call head on an empty list (thus calling "error")?
Catching errors is quite a hack. If the head of an empty list is requested, then this is considered a programming error, not a user error. Thus the best is to stop the program. Don't mix programming errors with problems caused by users (that is, exceptions).

Hello Stephane, Tuesday, August 1, 2006, 10:52:06 AM, you wrote:
except Exception e:
don't look at anything except than "Tackling the awkward squad: monadic input/output, concurrency, exceptions, and foreign-language calls in Haskell" http://research.microsoft.com/Users/simonpj/papers/marktoberdorf/marktoberdo... -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

Stephane Bortzmeyer wrote:
[It is a philosophical question, not a practical programming problem.]
I'm used, in imperative programming languages with exceptions (like Python) to call any function without fear of stopping the program because I can always catch the exceptions with things like (Python):
while not over: try: code which may raise an exception... except Exception e: do something clever
How to do it in Haskell? How can I call functions like Prelude.head while being sure my program won't stop, even if I call head on an empty list (thus calling "error")?
Here's another way of looking at it, that I've grown fond of. If your program is a total function, then there should be no exceptions. That is, if you properly model the world, in all its messiness, then you can write a function that maps every instance of the world to some valid output, even if that output is ``Sorry.'' It might seem a daunting task, but liberal use of the Maybe class from the ground up helps. We have suffered through quite a bit of this with our hardware detector, where unexpected situations are the norm. My colleague David Fox has spent considerable time computing reasonable answers in seemingly impossible situations, to the point where you cannot turn around without bumping into a Maybe construct.
participants (6)
-
Bulat Ziganshin
-
Clifford Beshers
-
Henning Thielemann
-
J. Garrett Morris
-
Matthias Fischmann
-
Stephane Bortzmeyer