Discussion:
Specialized exponentiation operators
Add Reply
David Feuer
2018-11-01 17:24:17 UTC
Reply
Permalink
We currently have

(^) :: (Num a, Integral b) => a -> b -> a
(^^) :: (Fractional a, Integral b) => a -> b -> a
stimes :: (Semigroup a, Integral b) => b -> a -> a

These are very general, but the ergonomics are terrible. The trouble
is that in each case, the `b` type variable is a (constrained) type
that appears only in one argument and not in the result. In the
extremely common case where the exponent is a literal, we rely on
defaulting to fix `b ~ Integer`. When the exponent is very small, it
will then be rewritten to a simple multiplication. Otherwise, it will
pay the price of bignum arithmetic, whether that's required or not.

Is there a canonical package providing versions of these functions
with the exponent specialized to `Int` and/or `Word`? If not, where
might such fit well?
Carter Schonwald
2018-11-01 17:58:56 UTC
Reply
Permalink
some specializations / optimized instances might be found in
http://hackage.haskell.org/package/arithmoi

cheers
-Carter
Post by David Feuer
We currently have
(^) :: (Num a, Integral b) => a -> b -> a
(^^) :: (Fractional a, Integral b) => a -> b -> a
stimes :: (Semigroup a, Integral b) => b -> a -> a
These are very general, but the ergonomics are terrible. The trouble
is that in each case, the `b` type variable is a (constrained) type
that appears only in one argument and not in the result. In the
extremely common case where the exponent is a literal, we rely on
defaulting to fix `b ~ Integer`. When the exponent is very small, it
will then be rewritten to a simple multiplication. Otherwise, it will
pay the price of bignum arithmetic, whether that's required or not.
Is there a canonical package providing versions of these functions
with the exponent specialized to `Int` and/or `Word`? If not, where
might such fit well?
_______________________________________________
Libraries mailing list
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Loading...