Proposal: Num instance for (a -> b)

relevant reddit comment thread: https://www.reddit.com/r/haskell/comments/9vtis5/the_universe_of_discourse_i... instance Num b => Num (a -> b) where f + g = \x -> f x + g x f * g = \x -> f x * g x f - g = \x -> f x - g x negate f = \x -> negate (f x) abs f = \x -> abs (f x) signum f = \x -> signum (f x) fromInteger i = \x -> fromInteger (f x)

On Sat, 10 Nov 2018, Daniel Cartwright wrote:
relevant reddit comment thread:https://www.reddit.com/r/haskell/comments/9vtis5/the_universe_of_discourse_i... urce=reddit-android
https://wiki.haskell.org/index.php?title=Num_instance_for_functions&oldid=36632 In short: It would make 2(x+y) no longer a type error but equivalent to 2. We would lose a lot of type safety for little syntactic gain.

On Sun, 11 Nov 2018, Henning Thielemann wrote:
On Sat, 10 Nov 2018, Daniel Cartwright wrote:
relevant reddit comment thread:https://www.reddit.com/r/haskell/comments/9vtis5/the_universe_of_discourse_i... urce=reddit-android
https://wiki.haskell.org/index.php?title=Num_instance_for_functions&oldid=36632
In short: It would make 2(x+y) no longer a type error but equivalent to 2. We would lose a lot of type safety for little syntactic gain.
Btw. before adding more Wat instances please implement the GHC warning about such instances: https://ghc.haskell.org/trac/ghc/ticket/11796

I suggest adding this as an orphan instance (Like Show (a -> b)) in a
seperate module (say, Numeric.Function).
2018년 11월 11일 (일) 14:01에 Henning Thielemann
On Sun, 11 Nov 2018, Henning Thielemann wrote:
On Sat, 10 Nov 2018, Daniel Cartwright wrote:
relevant reddit comment thread: https://www.reddit.com/r/haskell/comments/9vtis5/the_universe_of_discourse_i... urce=reddit-android
https://wiki.haskell.org/index.php?title=Num_instance_for_functions&oldid=36632
In short: It would make 2(x+y) no longer a type error but equivalent to
2. We
would lose a lot of type safety for little syntactic gain.
Btw. before adding more Wat instances please implement the GHC warning about such instances: https://ghc.haskell.org/trac/ghc/ticket/11796 _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

On Sun, 11 Nov 2018, Dannyu NDos wrote:
I suggest adding this as an orphan instance (Like Show (a -> b)) in a seperate module (say, Numeric.Function).
https://hackage.haskell.org/package/NumInstances It would not save much, because in a reasonably big project you will somewhen import a package that transitively depends on the module. E.g. 'diagrams-canvas' depends on NumInstances.

On 11/11/18, Henning Thielemann
On Sun, 11 Nov 2018, Henning Thielemann wrote:
On Sat, 10 Nov 2018, Daniel Cartwright wrote:
relevant reddit comment thread:https://www.reddit.com/r/haskell/comments/9vtis5/the_universe_of_discourse_i... urce=reddit-android
https://wiki.haskell.org/index.php?title=Num_instance_for_functions&oldid=36632
In short: It would make 2(x+y) no longer a type error but equivalent to 2. We would lose a lot of type safety for little syntactic gain.
Btw. before adding more Wat instances please implement the GHC warning about such instances: https://ghc.haskell.org/trac/ghc/ticket/11796
This is my feeling as well. Tom

-1, per the very confusing errors that would ensue.
If this behavior is desired, you can use a newtype wrapper. As it happens,
this fits the pattern of ANum http://hackage.haskell.org/package/ANum.
(Any Applicative can be made an instance of Num in this way.)
-- Dan Burton
On Sun, Nov 11, 2018 at 12:28 AM Tom Murphy
On 11/11/18, Henning Thielemann
wrote: On Sun, 11 Nov 2018, Henning Thielemann wrote:
On Sat, 10 Nov 2018, Daniel Cartwright wrote:
relevant reddit comment thread:
urce=reddit-android
https://wiki.haskell.org/index.php?title=Num_instance_for_functions&oldid=36632
In short: It would make 2(x+y) no longer a type error but equivalent to
https://www.reddit.com/r/haskell/comments/9vtis5/the_universe_of_discourse_i... 2.
We would lose a lot of type safety for little syntactic gain.
Btw. before adding more Wat instances please implement the GHC warning about such instances: https://ghc.haskell.org/trac/ghc/ticket/11796
This is my feeling as well.
Tom _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

ANum seems to be just Data.Monoid.Ap.
Also, I can see not wanting to worsen the error messages, though it is
worth pointing out that we already have a Monoid instance with the same
semantics, and a similar potential for confusing error messages.
On Sun, Nov 11, 2018, 1:36 AM Dan Burton -1, per the very confusing errors that would ensue. If this behavior is desired, you can use a newtype wrapper. As it happens,
this fits the pattern of ANum http://hackage.haskell.org/package/ANum.
(Any Applicative can be made an instance of Num in this way.) -- Dan Burton On Sun, Nov 11, 2018 at 12:28 AM Tom Murphy On 11/11/18, Henning Thielemann On Sun, 11 Nov 2018, Henning Thielemann wrote: On Sat, 10 Nov 2018, Daniel Cartwright wrote: relevant reddit comment
thread: https://www.reddit.com/r/haskell/comments/9vtis5/the_universe_of_discourse_i... urce=reddit-android https://wiki.haskell.org/index.php?title=Num_instance_for_functions&oldid=36632 In short: It would make 2(x+y) no longer a type error but equivalent to 2. We
would lose a lot of type safety for little syntactic gain. Btw. before adding more Wat instances please implement the GHC warning
about such instances:
https://ghc.haskell.org/trac/ghc/ticket/11796 This is my feeling as well. Tom
_______________________________________________
Libraries mailing list
Libraries@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries _______________________________________________
Libraries mailing list
Libraries@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

Monoid doesn't have fromInteger -like function. For example, we don't have FromString b => FromString (a -> b) instance. Unfortunately fromInteger is part of Num, so comparison with Monoid is unjust. Sent from my iPhone
On 11 Nov 2018, at 23.44, Daniel Cartwright
wrote: ANum seems to be just Data.Monoid.Ap. Also, I can see not wanting to worsen the error messages, though it is worth pointing out that we already have a Monoid instance with the same semantics, and a similar potential for confusing error messages.
On Sun, Nov 11, 2018, 1:36 AM Dan Burton
If this behavior is desired, you can use a newtype wrapper. As it happens, this fits the pattern of ANum. (Any Applicative can be made an instance of Num in this way.)
-- Dan Burton
On Sun, Nov 11, 2018 at 12:28 AM Tom Murphy
wrote: On 11/11/18, Henning Thielemann wrote: On Sun, 11 Nov 2018, Henning Thielemann wrote:
On Sat, 10 Nov 2018, Daniel Cartwright wrote:
relevant reddit comment thread:https://www.reddit.com/r/haskell/comments/9vtis5/the_universe_of_discourse_i... urce=reddit-android
https://wiki.haskell.org/index.php?title=Num_instance_for_functions&oldid=36632
In short: It would make 2(x+y) no longer a type error but equivalent to 2. We would lose a lot of type safety for little syntactic gain.
Btw. before adding more Wat instances please implement the GHC warning about such instances: https://ghc.haskell.org/trac/ghc/ticket/11796
This is my feeling as well.
Tom _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

I'm not quite sure the comparison is unjust - i was referring to +,-,*
giving strange error messages, not necessarily fromInteger. Recall that the
instance is Monoid b => Monoid (a -> b).
An example is a user might mistakenly type `mempty "text"`, which is just
`const mempty`, though this might not be what they mean (GHC 8.6 will give
the hole in `f :: Text -> Text; f x = _ x;` a suggestion of `mempty`, which
might certainly be confusing to a beginner). Similarly `2 4` might parse 2
as being applied to 4, if (a -> b) had a Num instance (correct me if i'm
wrong in saying thats how such a string might be parsed).
On Sun, Nov 11, 2018, 5:10 PM Oleg Grenrus Monoid doesn't have fromInteger -like function. For example, we don't have
FromString b => FromString (a -> b) instance. Unfortunately fromInteger is part of Num, so comparison with Monoid is
unjust. Sent from my iPhone On 11 Nov 2018, at 23.44, Daniel Cartwright ANum seems to be just Data.Monoid.Ap.
Also, I can see not wanting to worsen the error messages, though it is
worth pointing out that we already have a Monoid instance with the same
semantics, and a similar potential for confusing error messages. On Sun, Nov 11, 2018, 1:36 AM Dan Burton -1, per the very confusing errors that would ensue. If this behavior is desired, you can use a newtype wrapper. As it
happens, this fits the pattern of ANum
http://hackage.haskell.org/package/ANum. (Any Applicative can be made
an instance of Num in this way.) -- Dan Burton On Sun, Nov 11, 2018 at 12:28 AM Tom Murphy On 11/11/18, Henning Thielemann On Sun, 11 Nov 2018, Henning Thielemann wrote: On Sat, 10 Nov 2018, Daniel Cartwright wrote: relevant reddit comment
thread: https://www.reddit.com/r/haskell/comments/9vtis5/the_universe_of_discourse_i... urce=reddit-android https://wiki.haskell.org/index.php?title=Num_instance_for_functions&oldid=36632 In short: It would make 2(x+y) no longer a type error but equivalent to 2. We
would lose a lot of type safety for little syntactic gain. Btw. before adding more Wat instances please implement the GHC warning
about such instances:
https://ghc.haskell.org/trac/ghc/ticket/11796 This is my feeling as well. Tom
_______________________________________________
Libraries mailing list
Libraries@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries _______________________________________________
Libraries mailing list
Libraries@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries _______________________________________________
Libraries mailing list
Libraries@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

Sorry, the parsing is tied to fromInteger. So i see what you mean about the
poor errors not being as comparable. Henning and another brought up a good
point, that either users ought to be warned or it would go in its own
module ala Text.Show.Function.
On Sun, Nov 11, 2018, 5:30 PM Daniel Cartwright I'm not quite sure the comparison is unjust - i was referring to +,-,*
giving strange error messages, not necessarily fromInteger. Recall that the
instance is Monoid b => Monoid (a -> b). An example is a user might mistakenly type `mempty "text"`, which is just
`const mempty`, though this might not be what they mean (GHC 8.6 will give
the hole in `f :: Text -> Text; f x = _ x;` a suggestion of `mempty`, which
might certainly be confusing to a beginner). Similarly `2 4` might parse 2
as being applied to 4, if (a -> b) had a Num instance (correct me if i'm
wrong in saying thats how such a string might be parsed). On Sun, Nov 11, 2018, 5:10 PM Oleg Grenrus Monoid doesn't have fromInteger -like function. For example, we don't
have FromString b => FromString (a -> b) instance. Unfortunately fromInteger is part of Num, so comparison with Monoid is
unjust. Sent from my iPhone On 11 Nov 2018, at 23.44, Daniel Cartwright ANum seems to be just Data.Monoid.Ap.
Also, I can see not wanting to worsen the error messages, though it is
worth pointing out that we already have a Monoid instance with the same
semantics, and a similar potential for confusing error messages. On Sun, Nov 11, 2018, 1:36 AM Dan Burton -1, per the very confusing errors that would ensue. If this behavior is desired, you can use a newtype wrapper. As it
happens, this fits the pattern of ANum
http://hackage.haskell.org/package/ANum. (Any Applicative can be made
an instance of Num in this way.) -- Dan Burton On Sun, Nov 11, 2018 at 12:28 AM Tom Murphy On 11/11/18, Henning Thielemann On Sun, 11 Nov 2018, Henning Thielemann wrote: On Sat, 10 Nov 2018, Daniel Cartwright wrote: > relevant reddit comment
> thread: https://www.reddit.com/r/haskell/comments/9vtis5/the_universe_of_discourse_i... > urce=reddit-android https://wiki.haskell.org/index.php?title=Num_instance_for_functions&oldid=36632 In short: It would make 2(x+y) no longer a type error but equivalent to 2. We
would lose a lot of type safety for little syntactic gain. Btw. before adding more Wat instances please implement the GHC warning
about such instances:
https://ghc.haskell.org/trac/ghc/ticket/11796 This is my feeling as well. Tom
_______________________________________________
Libraries mailing list
Libraries@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries _______________________________________________
Libraries mailing list
Libraries@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries _______________________________________________
Libraries mailing list
Libraries@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

It would be nice if the Semiring/Ring[1] parts of Num were pulled into its
own class. The overloading of integer literals to functions does seem
disruptive.
[1]:
http://hackage.haskell.org/package/semirings-0.2.1.1/docs/Data-Semiring.html
On Sun, Nov 11, 2018, 5:42 PM Daniel Cartwright Sorry, the parsing is tied to fromInteger. So i see what you mean about
the poor errors not being as comparable. Henning and another brought up a
good point, that either users ought to be warned or it would go in its own
module ala Text.Show.Function. On Sun, Nov 11, 2018, 5:30 PM Daniel Cartwright I'm not quite sure the comparison is unjust - i was referring to +,-,*
giving strange error messages, not necessarily fromInteger. Recall that the
instance is Monoid b => Monoid (a -> b). An example is a user might mistakenly type `mempty "text"`, which is just
`const mempty`, though this might not be what they mean (GHC 8.6 will give
the hole in `f :: Text -> Text; f x = _ x;` a suggestion of `mempty`, which
might certainly be confusing to a beginner). Similarly `2 4` might parse 2
as being applied to 4, if (a -> b) had a Num instance (correct me if i'm
wrong in saying thats how such a string might be parsed). On Sun, Nov 11, 2018, 5:10 PM Oleg Grenrus Monoid doesn't have fromInteger -like function. For example, we don't
have FromString b => FromString (a -> b) instance. Unfortunately fromInteger is part of Num, so comparison with Monoid is
unjust. Sent from my iPhone On 11 Nov 2018, at 23.44, Daniel Cartwright ANum seems to be just Data.Monoid.Ap.
Also, I can see not wanting to worsen the error messages, though it is
worth pointing out that we already have a Monoid instance with the same
semantics, and a similar potential for confusing error messages. On Sun, Nov 11, 2018, 1:36 AM Dan Burton -1, per the very confusing errors that would ensue. If this behavior is desired, you can use a newtype wrapper. As it
happens, this fits the pattern of ANum
http://hackage.haskell.org/package/ANum. (Any Applicative can be
made an instance of Num in this way.) -- Dan Burton On Sun, Nov 11, 2018 at 12:28 AM Tom Murphy On 11/11/18, Henning Thielemann On Sun, 11 Nov 2018, Henning Thielemann wrote: > On Sat, 10 Nov 2018, Daniel Cartwright wrote:
>
>> relevant reddit comment
>> thread: https://www.reddit.com/r/haskell/comments/9vtis5/the_universe_of_discourse_i... >> urce=reddit-android
>
>
https://wiki.haskell.org/index.php?title=Num_instance_for_functions&oldid=36632
>
> In short: It would make 2(x+y) no longer a type error but
equivalent to 2.
> We
> would lose a lot of type safety for little syntactic gain. Btw. before adding more Wat instances please implement the GHC
warning
about such instances:
https://ghc.haskell.org/trac/ghc/ticket/11796 This is my feeling as well. Tom
_______________________________________________
Libraries mailing list
Libraries@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries _______________________________________________
Libraries mailing list
Libraries@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries _______________________________________________
Libraries mailing list
Libraries@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

The overloading of integer literals to functions does seem disruptive.
Most, if not all, of my objection has to do with the concern that integer
literals (and floating point literals, if functions also got a comparable
Floating instance) could be accidentally used as functions. Everything else
about these potential instances seems benign.
-- Dan Burton
On Sun, Nov 11, 2018 at 5:46 PM Daniel Cartwright
It would be nice if the Semiring/Ring[1] parts of Num were pulled into its own class. The overloading of integer literals to functions does seem disruptive.
[1]: http://hackage.haskell.org/package/semirings-0.2.1.1/docs/Data-Semiring.html
On Sun, Nov 11, 2018, 5:42 PM Daniel Cartwright
Sorry, the parsing is tied to fromInteger. So i see what you mean about the poor errors not being as comparable. Henning and another brought up a good point, that either users ought to be warned or it would go in its own module ala Text.Show.Function.
On Sun, Nov 11, 2018, 5:30 PM Daniel Cartwright
I'm not quite sure the comparison is unjust - i was referring to +,-,* giving strange error messages, not necessarily fromInteger. Recall that the instance is Monoid b => Monoid (a -> b).
An example is a user might mistakenly type `mempty "text"`, which is just `const mempty`, though this might not be what they mean (GHC 8.6 will give the hole in `f :: Text -> Text; f x = _ x;` a suggestion of `mempty`, which might certainly be confusing to a beginner). Similarly `2 4` might parse 2 as being applied to 4, if (a -> b) had a Num instance (correct me if i'm wrong in saying thats how such a string might be parsed).
On Sun, Nov 11, 2018, 5:10 PM Oleg Grenrus
Monoid doesn't have fromInteger -like function. For example, we don't have FromString b => FromString (a -> b) instance.
Unfortunately fromInteger is part of Num, so comparison with Monoid is unjust.
Sent from my iPhone
On 11 Nov 2018, at 23.44, Daniel Cartwright
wrote: ANum seems to be just Data.Monoid.Ap. Also, I can see not wanting to worsen the error messages, though it is worth pointing out that we already have a Monoid instance with the same semantics, and a similar potential for confusing error messages.
On Sun, Nov 11, 2018, 1:36 AM Dan Burton
-1, per the very confusing errors that would ensue.
If this behavior is desired, you can use a newtype wrapper. As it happens, this fits the pattern of ANum http://hackage.haskell.org/package/ANum. (Any Applicative can be made an instance of Num in this way.)
-- Dan Burton
On Sun, Nov 11, 2018 at 12:28 AM Tom Murphy
wrote: On 11/11/18, Henning Thielemann
wrote: > > On Sun, 11 Nov 2018, Henning Thielemann wrote: > >> On Sat, 10 Nov 2018, Daniel Cartwright wrote: >> >>> relevant reddit comment >>> thread: https://www.reddit.com/r/haskell/comments/9vtis5/the_universe_of_discourse_i... >>> urce=reddit-android >> >> https://wiki.haskell.org/index.php?title=Num_instance_for_functions&oldid=36632 >> >> In short: It would make 2(x+y) no longer a type error but equivalent to 2. >> We >> would lose a lot of type safety for little syntactic gain. > > Btw. before adding more Wat instances please implement the GHC warning > about such instances: > https://ghc.haskell.org/trac/ghc/ticket/11796 This is my feeling as well.
Tom _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
_______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
_______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

Oleg Grenrus wrote:
Monoid doesn't have fromInteger -like function. For example, we don't have FromString b => FromString (a -> b) instance.
Monoid does have `mempty` though, with the unfortunate property that mempty [1,2] /= mempty `mappend` [1,2] But I'm less worried about this than in the case of Num; I occasionally copy formulas from a CAS into Haskell code and then add the missing * operations manually; I estimate that about half of the time I miss one instance and am rescued by the type checker. So to me, `2 (x + y)` no longer being a type error would have practical implications. -1 on the proposal from me. Cheers, Bertram

Daniel Cartwright
ANum seems to be just Data.Monoid.Ap. Also, I can see not wanting to worsen the error messages, though it is worth pointing out that we already have a Monoid instance with the same semantics, and a similar potential for confusing error messages.
The existence of a previous bad decision doesn’t seem to me to be a good reason to make another one. -- Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk http://www.chaos.org.uk/~jf/Stuff-I-dont-want.html (updated 2014-04-05)

I'm not sure I would label it a bad decision, but the concerns here make
sense.
On Mon, Nov 12, 2018, 5:16 AM Jon Fairbairn Daniel Cartwright ANum seems to be just Data.Monoid.Ap.
Also, I can see not wanting to worsen the error messages, though it is
worth pointing out that we already have a Monoid instance with the same
semantics, and a similar potential for confusing error messages. The existence of a previous bad decision doesn’t seem to me to
be a good reason to make another one. --
Jón Fairbairn Jon.Fairbairn@cl.cam.ac.uk
http://www.chaos.org.uk/~jf/Stuff-I-dont-want.html (updated 2014-04-05) _______________________________________________
Libraries mailing list
Libraries@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries

I'm +1 on this, since it is conventional to write addition (and others)
over function as such in most math textbooks.
2018년 11월 11일 (일) 13:48에 Daniel Cartwright
relevant reddit comment thread: https://www.reddit.com/r/haskell/comments/9vtis5/the_universe_of_discourse_i...
instance Num b => Num (a -> b) where f + g = \x -> f x + g x f * g = \x -> f x * g x f - g = \x -> f x - g x negate f = \x -> negate (f x) abs f = \x -> abs (f x) signum f = \x -> signum (f x) fromInteger i = \x -> fromInteger (f x) _______________________________________________ Libraries mailing list Libraries@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
participants (8)
-
Bertram Felgenhauer
-
Dan Burton
-
Daniel Cartwright
-
Dannyu NDos
-
Henning Thielemann
-
Jon Fairbairn
-
Oleg Grenrus
-
Tom Murphy