opPow, opDollar

Matti Niemenmaa see_signature at for.real.address
Sun Nov 8 01:58:25 PST 2009


Stewart Gordon wrote:
> Andrei Alexandrescu wrote:
>> Matti Niemenmaa wrote:
>>> Haskell has three exponentiation operators in the standard library: 
>>> ^, ^^, and **. They are for non-negative integral exponents, integral 
>>> exponents, and floating-point exponents respectively.
>>
>> I wonder whether that's an illustration of the power or of the failure 
>> of function overloading. (Seriously.)
> 
> I'm not sure either.  I don't speak Haskell, but my guess is that ^ and 
> ^^ were meant to cut out the confusion that would happen if Word32 ^ 
> Word32 (what weird naming conventions Haskell has!) returned an integer 
> type but Int32 ^ Int32 returned a floating point type.
> 
> But why it needs yet another for floating-point exponents, I don't know. 
>  Maybe Haskell supports only IFTI rather than true function overloading.

It's essentially because Haskell has separate type classes (kiiiinda 
like D interfaces... I won't go into that topic) for integers, 
fractional numbers, and floating-point numbers. In D the types of those 
three operators could be something like:

anyinteger     ^(anyinteger    base, anyinteger  exponent);
anyfractional ^^(anyfractional base, anyinteger  exponent);
anyfloating   **(anyfloating   base, anyfloating exponent);

A noteworthy fractional is the Rational type, a ratio of two integral 
values. Note that 0.5 is a valid Rational: it's 1/2. Note, still, that 
0.5 ** 0.5 is no longer a valid Rational: it's the square root of 1/2. 
This is why ^^ is separate: fractionals can be safely raised to integer 
exponents, but if you take a fractional and raise it to a fractional 
power, you might not get a fractional back.



More information about the Digitalmars-d mailing list