[Haskell-cafe] Associated types for number coercion

Conrad Parker conrad at metadecks.org
Wed Mar 20 00:54:10 CET 2013


On 20 March 2013 06:58, Christopher Done <chrisdone at gmail.com> wrote:
> From the paper Fun with Type Funs, it's said:
>
>> One compelling use of such type functions is to make type
>> coercions implicit, especially in arithmetic. Suppose we want to be able to
>> write add a b to add two numeric values a and b even if one is an Integer
>> and the other is a Double (without writing fromIntegral explicitly).
>
> And then an Add class is defined which can dispatch at the type-level
> to appropriate functions which resolve two types into one, with a
> catch-all case for Num.
>
> Has anyone put this into a package, for all common arithmetic
> operations? I would use it. Doing arithmetic stuff in Haskell always
> feels labored because of having constantly convert between number
> types.

hmatrix takes this approach with a Mul typeclass for combinations of
Vector and Matrix multiplication, defined for things that can
implement Product (real and Complex Doubles and Floats).

http://hackage.haskell.org/packages/archive/hmatrix/0.14.1.0/doc/html/Numeric-Container.html

I think it'd be interesting for numeric stuff to have implicit
conversion to Double, using a class as you suggest which doesn't
support Integral or bitops.

Conrad.



More information about the Haskell-Cafe mailing list