A sample revised prelude for numeric classes
Marcin 'Qrczak' Kowalczyk
13 Feb 2001 19:47:09 GMT
Tue, 13 Feb 2001 14:01:25 -0500, Dylan Thurston <email@example.com> pisze:
> Consequences: you cannot, e.g., raise a Double to an Integer power
> without an explicit conversion or calling a different function (or
> declaring your own instance). Is this acceptable?
I don't like it: (-3::Double)^2 should be 9, and generally x^(2::Integer)
should be x*x for all types of x where it makes sense. Same for Int.
(**) does not work for negative base. Neither of (^) and (**) is
a generalization of the other: the knowledge that an exponent is
restricted to integers widens the domain of the base.
x^2 = x*x cannot actually work for any x in Num, or whatever the class
of (*) is called, if (^) is not defined inside the same class. This
is because (^) is unified with (^^): the unified (^) should use recip
if available, but be partially defined without it if it's not available.
So I propose to put (^) together with (*). With a default definition
of course. It means "apply (*) the specified number of times", and
for fractional types has a meaning extended to negative exponents.
(^) is related to (*) as discussed times or scale is related to (+).
(**):: a -> a -> a, together with other analytic functions. Sorry,
the fact that they are written the same in conventional math is not
enough to force their unification against technical reasons. It's
not bad: we succeeded in unification of (^) and (^^).
__("< Marcin Kowalczyk * firstname.lastname@example.org http://qrczak.ids.net.pl/
^^ SYGNATURA ZASTĘPCZA