Proposal: Num instance for tuples

Sjoerd Visscher sjoerd at w3future.com
Sat Oct 20 12:24:07 CEST 2012


Maybe we could add default implementations using generics? Then such an instance would be one "deriving instance" line away.

Sjoerd

On Oct 18, 2012, at 11:58 AM, Twan van Laarhoven <twanvl at gmail.com> wrote:

> Dear list,
> 
> 
> One piece of utility code that I find myself writing over and over again is the pointwise Num instance for tuples, i.e.
> 
>    instance (Num a, Num b) => Num (a,b) where
>        fromInteger x = (fromInteger x, fromInteger x)
>        (a,b) + (a',b') = (a + a', b + b')
>        (a,b) - (a',b') = (a - a', b - b')
>        (a,b) * (a',b') = (a * a', b * b')
>        negate (a,b) = (negate a, negate b)
>        abs (a,b) = (abs a, abs b)
>        signum (a,b) = (signum a, signum b)
> 
> I therefore propose that this instance be added to the base library. If we do that, the equivalent instances for (,,) (,,,) etc. should perhaps also be added, as well as instances for other numeric classes such as Fractional and Floating.
> 
> 
> Discussion period: 2 weeks, ends 2012-11-01.
> 
> 
> Twan
> 
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://www.haskell.org/mailman/listinfo/libraries




More information about the Libraries mailing list