Specialized exponentiation operators

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Specialized exponentiation operators

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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Specialized exponentiation operators

Carter Schonwald
some specializations / optimized instances might be found in http://hackage.haskell.org/package/arithmoi

cheers
-Carter

On Thu, Nov 1, 2018 at 1:24 PM David Feuer <[hidden email]> wrote:
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries