1 QuestionWhy are there several notions of power in Haskell, namely
-- typically for integers (^) :: (Num a, Integral b) => a -> b -> a -- typically for rationals (^^) :: (Fractional a, Integral b) => a -> b -> a -- typically for floating-point numbers (**) :: Floating a => a -> a -> a
The reason is that there is no implementation of the power function that can cover all exotic choices for basis and exponent while being both efficient and accurate.
See this StackOverflow question for details.
2.1 Type inference reasons
In mathematical notation, the human reader is clever enough to to tell which definition of the power function is applicable in a given context. In Haskell, doing so would drastically complicate type inference. In some other languages such as C++, operator overloading is used to work around this problem, but this approach does not work for Haskell's numeric type classes.
2.2 Mathematical reasons
If we assume the most general types for both basis and exponent, namely complex numbers, the result of the power is no longer unique. In particular, all possible solutions of say 1x, where x is irrational, is dense in the complex unit circle.But even for real numbers there are problems: to calculate
Prelude> (-1)**2 :: Double 1.0 Prelude> (-1)**(2 + 1e-15 - 1e-15) :: Double NaN
3 Power function in Numeric Prelude
One can refine the set of power functions further as it is done in the Numeric Prelude. In this library, the more general the basis the less general the exponent and vice versa:
|basis type||provides||symbol||exponent type||definition|
|integer||multiplication and division|
|an algebraic field||
|rational||list of polynomial zeros (length = denominator of the exponent)|
|any ring of characteristic zero with inverses for integers and a notion of limit||exponential series and logarithm|
- examples for rings are: Polynomials, Matrices, Residue classes
- examples for fields: Fractions of polynomials (rational functions), Residue classes with respect to irreducible divisors, in fact we do not need fields, we only need the division and associativity, thus invertible Matrices are fine
4 See also
- Haskell-Cafe: Proposal for restructuring Number classes