Re: [Haskell-cafe] exceptions vs. Either

A lot of programming errors come from failure to correctly validate. I remember being taught to validate on input _and_ validate when you use a value. So I would say do both - use maybe for input functions, and exceptions for when you use a value. You can use the type system to make this more efficient. Imagine you want an integer between 10 and 20, you have a function like: newtype ValidInt = Valid Int validateInt :: Int -> Maybe ValidInt validateInt x | x>=10 && x<=20 = Just (Valid x) validateInt _ = Nothing Now when you validate on input - you produce an error if you get "Nothing" and in all the functions you use the value you use ValidInt instead of Int. This way you use the type system to differentiate between a valid int (does not need to be rechecked) and a normal int (which would need to be validated). Of course you must make sure you don't write more than one constructor function for ValidInt. This suggests a form of constrained constructor would be useful... Whilst this can be done in Haskell, it is not easy and involves reifying the value to a type, and using a type class to constrain the data statement. Really here we are dealing with dependant types, and this is much easier in a dependantly typed language like Epigram. Keean.

A lot of programming errors come from failure to correctly validate.
This was actually nicely illustrated in my program: I assumed that digitToInt accepted '0'..'9' and wanted to rely on it throwing. After puzzling over out of range errors (other functions expected digitToInt to be in the 0..9 range) I figured it out and inserted an explicit check. Unfortunately there is no static check for pre and post conditions, unless you manually add them to the type system, as you suggest. But that's work ;) Wrting a monad bind for Either that returns Left immediately and binds Right would make Either style error propagation easier. But it seems like it would over sequence: do a' <- checka a b' <- checkb b return $ process a' b' is rather messier looking than 'process (checka a) (checkb b)'. It doesn't really matter what order 'a' and 'b' are checked, what I really want is the monad-style magic plumbing. I suppose I should just wrap 'process' in a liftM2 and stop worrying about it.
Of course you must make sure you don't write more than one constructor function for ValidInt. This suggests a
Can't you do this at the module level by exporting the ValidInt type, but not the constructor? Of course, then you have to stick it in its own module, and simple range validation is getting even more heavyweight... In response to the "mysterious head exceptions" thread, isn't there a way to compile with profiling and then get the rts to give a traceback on exception?

On 03-Aug-2004, Evan LaForge
In response to the "mysterious head exceptions" thread, isn't there a way to compile with profiling and then get the rts to give a traceback on exception?
There is, but it doesn't really work properly, due to - lazy evaluation - tail call optimization - lack of line numbers in the traceback -- Fergus J. Henderson | "I have always known that the pursuit Galois Connections, Inc. | of excellence is a lethal habit" Phone: +1 503 626 6616 | -- the last words of T. S. Garp.
participants (3)
-
Evan LaForge
-
Fergus Henderson
-
MR K P SCHUPKE