
Marcin Kowalczyk wrote:
Wed, 7 Feb 2001 16:17:38 -0500, Peter Douglass
pisze: What I have in mind is to remove division by zero as an untypable expression. The idea is to require div, /, mod to take NonZeroNumeric values in their second argument. NonZeroNumeric values could be created by functions of type: Number a => a -> Maybe NonZeroNumeric or something similar.
IMHO it would be impractical.
The first part of my question (not contained in your reply) is whether it is feasible to disable a developer's access to the "unsafe" numerical operations. Whether or not an individual developer chooses to do so is another matter.
Often I know that the value is non-zero, but it is not statically determined,
If you "know" the value is non-zero before run-time, then that is statically determined. Otherwise, you don't "know" that.
so it would just require uglification by doing that conversion and then coercing Maybe NonZeroNumeric to NonZeroNumeric.
Ugliness is in the eye of the beholder I suppose. For some applications, every division should be preceded by an explicit test for zero, or the denominator must be "known" to be non-zero by the way in which it was created. Forcing a developer to extract a NonZeroNumeric value from a Maybe NonZeroNumeric value seems equivalent to me.
It's bottom anyway when the value is 0, but bottom would come from Maybe coercion instead of from quot, so it only gives a worse error message.
It is possible that the developer writes a function which returns a nonZeroNumeric value which actually has a value of zero. However, the value of requiring division to have a nonZeroNumeric denominator is to catch at compile time the "error" of failing to scrutinize (correctly or incorrectly) for zero. For most commercial software, the quality of run-time error messages is far less important than their absence.
It's so easy to define partial functions that it would not buy much for making it explicit outside quot.
Haskell does not have subtypes so a coercion from NonZeroNumeric to plain Numbers would have to be explicit as well, even if logically it's just an injection.
If one is aiming to write code which cannot fail at run-time, then extra work must be done anyway. The only question is whether the language will support such a discipline.
Everybody assumes that quot has a symmetric type as in all other languages, but in your proposal quot's arguments come from completely disjoint worlds.
If it is optional but not required that a developer may disable unsafe division, then developers who expect arithmetic to work in the usual way will not be disappointed.
Moreover, 1/0 is defined on IEEE Doubles (e.g. in ghc): infinity.
This solution doesn't always help with code safety. Thanks for the response. --PeterD

Thu, 8 Feb 2001 10:51:58 -0500, Peter Douglass
The first part of my question (not contained in your reply) is whether it is feasible to disable a developer's access to the "unsafe" numerical operations.
import Prelude hiding (quot, rem, (/) {- etc. -}) import YourPrelude -- which defines substitutes You can "disable" it now. You cannot disable them entirely - anyone can define present functions in terms of your functions if he really wants.
Whether or not an individual developer chooses to do so is another matter.
Why only quot? There are many other ways to write bottom: head [] (\(x:xs) -> (x,xs)) [] let x = x in x log (-1) asin 2 error "foo"
If you "know" the value is non-zero before run-time, then that is statically determined.
I know but the compiler does not know, and I have no way to convince it.
It is possible that the developer writes a function which returns a nonZeroNumeric value which actually has a value of zero. However, the value of requiring division to have a nonZeroNumeric denominator is to catch at compile time the "error" of failing to scrutinize (correctly or incorrectly) for zero.
IMHO it would be more painful than useful.
For most commercial software, the quality of run-time error messages is far less important than their absence.
It would not avoid them if the interface does not give a place to report the error: average xs = sum xs / case checkZero (length xs) of Just notZero -> notZero Nothing -> error "This should never happen" is not any more safe than average xs = sum xs / length xs and I can report bad input without trouble now: average xs = case length xs of 0 -> Nothing l -> Just (sum xs / l) -- __("< Marcin Kowalczyk * qrczak@knm.org.pl http://qrczak.ids.net.pl/ \__/ ^^ SYGNATURA ZASTÊPCZA QRCZAK
participants (2)
-
Peter Douglass
-
qrczak@knm.org.pl