Proposal: Num instance for tuples

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

On Thu, 18 Oct 2012, Twan van Laarhoven wrote:
One piece of utility code that I find myself writing over and over again is the pointwise Num instance for tuples, i.e.
For my taste, writing 2 :: (Int, Integer) abs (x,y) should yield a compiler error and the compiler should not try to interpret it in some way. For the cases where you need these instances I suggest you use custom data types.

On 18/10/2012 11:07, Henning Thielemann wrote:
On Thu, 18 Oct 2012, Twan van Laarhoven wrote:
One piece of utility code that I find myself writing over and over again is the pointwise Num instance for tuples, i.e.
For my taste, writing
2 :: (Int, Integer)
abs (x,y)
should yield a compiler error and the compiler should not try to interpret it in some way. For the cases where you need these instances I suggest you use custom data types.
Agreed, -1 from me. Cheers, Simon

On 10/19/12 7:29 AM, Simon Marlow wrote:
On 18/10/2012 11:07, Henning Thielemann wrote:
On Thu, 18 Oct 2012, Twan van Laarhoven wrote:
One piece of utility code that I find myself writing over and over again is the pointwise Num instance for tuples, i.e.
For my taste, writing
2 :: (Int, Integer)
abs (x,y)
should yield a compiler error and the compiler should not try to interpret it in some way. For the cases where you need these instances I suggest you use custom data types.
Agreed, -1 from me.
Agreed, another -1. -- Live well, ~wren

On Thu, Oct 18, 2012 at 11:58:17AM +0200, Twan van Laarhoven 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.
While I see *some* value in being able to perform addition, etc. on tuples, I still think that e.g. something like fst (2 :: Point) can easily lead to code obfuscation. For that reason, -1. Cheers, Simon

-1 for the same reason, there is no canonical interpretation of 1 as a tuple (could be (1,1), (0,1), (1,0) or s.th. else, no obvious choice...) On 18.10.2012 12:25, Simon Hengel wrote:
On Thu, Oct 18, 2012 at 11:58:17AM +0200, Twan van Laarhoven 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.
While I see *some* value in being able to perform addition, etc. on tuples, I still think that e.g. something like
fst (2 :: Point)
can easily lead to code obfuscation.
For that reason, -1.
Cheers, Simon
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries
-- Andreas Abel <>< Du bist der geliebte Mensch. Theoretical Computer Science, University of Munich Oettingenstr. 67, D-80538 Munich, GERMANY andreas.abel@ifi.lmu.de http://www2.tcs.ifi.lmu.de/~abel/

Unlike the rest of the comments so far, I am
+1
to this proposal.
It has the only reasonable semantics.
Sent from my iPad
On Oct 18, 2012, at 5:58 AM, Twan van Laarhoven
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@haskell.org http://www.haskell.org/mailman/listinfo/libraries

On Thu, Oct 18, 2012 at 7:34 AM, Edward Kmett
Unlike the rest of the comments so far, I am
+1
to this proposal.
It has the only reasonable semantics.
There are at least two reasonable choices. The first is this. The second is given by the fact that for any Num a and Applicative f, we can write Num (f a) by lifting all the operations. I suppose the one proposed here might be more commonly used. -- Dan

Very well, I withdraw my support. I can see the (,) m Applicative version
as being useful to people as well.
-1
On Thu, Oct 18, 2012 at 5:57 AM, Dan Doel
On Thu, Oct 18, 2012 at 7:34 AM, Edward Kmett
wrote: Unlike the rest of the comments so far, I am
+1
to this proposal.
It has the only reasonable semantics.
There are at least two reasonable choices. The first is this. The second is given by the fact that for any Num a and Applicative f, we can write Num (f a) by lifting all the operations.
I suppose the one proposed here might be more commonly used.
-- Dan

On Thu, Oct 18, 2012 at 11:58:17AM +0200, Twan van Laarhoven 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.
I am +1 for this proposal, but only as long as these instances are not exported by the Prelude. They are useful when you know you want them but will confuse newbies -- based on evidence from #haskell, where lambdabot has such instances in scope. -Brent

On Thu, 18 Oct 2012, Brent Yorgey wrote:
I am +1 for this proposal, but only as long as these instances are not exported by the Prelude. They are useful when you know you want them but will confuse newbies -- based on evidence from #haskell, where lambdabot has such instances in scope.
Please no orphan instances - I think a pair type that supports arithmetics deserves a custom type.

On 19 October 2012 04:57, Henning Thielemann
On Thu, 18 Oct 2012, Brent Yorgey wrote:
I am +1 for this proposal, but only as long as these instances are not exported by the Prelude. They are useful when you know you want them but will confuse newbies -- based on evidence from #haskell, where lambdabot has such instances in scope.
Please no orphan instances - I think a pair type that supports arithmetics deserves a custom type.
Agreed. Another -1 on the proposal from me. -- Ivan Lazar Miljenovic Ivan.Miljenovic@gmail.com http://IvanMiljenovic.wordpress.com

On Thu, Oct 18, 2012 at 4:47 PM, Ivan Lazar Miljenovic
On 19 October 2012 04:57, Henning Thielemann
wrote: On Thu, 18 Oct 2012, Brent Yorgey wrote:
I am +1 for this proposal, but only as long as these instances are not exported by the Prelude. They are useful when you know you want them but will confuse newbies -- based on evidence from #haskell, where lambdabot has such instances in scope.
Please no orphan instances - I think a pair type that supports arithmetics deserves a custom type.
Agreed. Another -1 on the proposal from me.
I am also with Henning, and a -1 for the proposal. If it's too confusing to export by default, then the instance belongs to a less general type. There are appropriate types that have the desired behavior all over hackage. I am currently partial to V2 from the linear package. Anthony

Our V2 doesn't *quite* match, since V2 requires the two fields to have the
same type, but I agree. =)
On Thu, Oct 18, 2012 at 2:36 PM, Anthony Cowley
On Thu, Oct 18, 2012 at 4:47 PM, Ivan Lazar Miljenovic
wrote: On 19 October 2012 04:57, Henning Thielemann
wrote: On Thu, 18 Oct 2012, Brent Yorgey wrote:
I am +1 for this proposal, but only as long as these instances are not exported by the Prelude. They are useful when you know you want them but will confuse newbies -- based on evidence from #haskell, where lambdabot has such instances in scope.
Please no orphan instances - I think a pair type that supports
arithmetics
deserves a custom type.
Agreed. Another -1 on the proposal from me.
I am also with Henning, and a -1 for the proposal. If it's too confusing to export by default, then the instance belongs to a less general type. There are appropriate types that have the desired behavior all over hackage. I am currently partial to V2 from the linear package.
Anthony
_______________________________________________ Libraries mailing list Libraries@haskell.org http://www.haskell.org/mailman/listinfo/libraries

Brent Yorgey
On Thu, Oct 18, 2012 at 11:58:17AM +0200, Twan van Laarhoven 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.
I am +1 for this proposal, but only as long as these instances are not exported by the Prelude. They are useful when you know you want them but will confuse newbies -- based on evidence from #haskell, where lambdabot has such instances in scope.
So, put it in a package. It has nothing to do in the Prelude. -- lelf

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.
-1 from me. While this is a sensible instance, it has the potential to cause great confusion. It will make bug hunting that much harder when you accidentally put a numeric literal where a tuple of distinct numbers is supposed to be. Put the instance in its own package, and those that want it can just install the package. -- Dan Burton

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
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@haskell.org http://www.haskell.org/mailman/listinfo/libraries

I use these instances and similar ones from NumInstances [1] quite a bit. I
also frequently use another pattern for applicative functors, via
applicative-numbers [2]. I don't mind pulling these instances from a
package, though they are orphans.
It does trouble me a bit that the two patterns overlap and disagree on
pairs.
For pairs, I like fromInteger n = (fromInteger n, fromInteger n). Then, for
instance, 1 * (a,b) = (a,b).
-- Conal
[1] http://hackage.haskell.org/package/NumInstances
[2] http://hackage.haskell.org/package/applicative-numbers
On Thu, Oct 18, 2012 at 2:58 AM, Twan van Laarhoven
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
______________________________**_________________ Cvs-ghc mailing list Cvs-ghc@haskell.org http://www.haskell.org/**mailman/listinfo/cvs-ghchttp://www.haskell.org/mailman/listinfo/cvs-ghc

On 18/10/12 11:58, Twan van Laarhoven 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.
Discussion period: 2 weeks, ends 2012-11-01.
The discussion period is over, and it seems that most people are against this proposal. Votes are -9 and +2. The NumInstance package on hackage provides these instances for those who want them. Twan [1] http://hackage.haskell.org/package/NumInstances
participants (15)
-
Andreas Abel
-
Anthony Cowley
-
Anton Nikishaev
-
Brent Yorgey
-
Conal Elliott
-
Dan Burton
-
Dan Doel
-
Edward Kmett
-
Henning Thielemann
-
Ivan Lazar Miljenovic
-
Simon Hengel
-
Simon Marlow
-
Sjoerd Visscher
-
Twan van Laarhoven
-
wren ng thornton