[Haskell-cafe] "Natural" polymorphism for n*(n+1)/2

Tom Smeding x at tomsmeding.com
Wed Dec 16 23:30:29 UTC 2020


Good point, that would allow the original, desired function to be written 'f :: Num a => a -> a; f n = abs (n * (n + 1))'. However, that doesn't do the right thing when 'a' is Int or Float.

Any candidate 'f' would need to be written, all auxiliary functions inlined, in terms of the operations of Num. Since there is no way to do that when a ~ Int (since the Num Int instance doesn't have backdoors like this), it won't work. My polynomial example really only gives a conceptual idea why it _shouldn't_ work.

Assuming I'm not missing something; it's late here.

- Tom

-------- Original Message --------

On 17 Dec 2020, 00:13, Henning Thielemann < lemming at henning-thielemann.de> wrote:

On Wed, 16 Dec 2020, Tom Smeding wrote:

> You say 'abs x = x/2', but what's that (/)? For example, what is 'abs'

> supposed to give when called on (the representation of) the polynomial

> X^2 + 3X + 2?

I meant it this way:

instance (Fractional a) => Num (Polynomial a) where

abs = fmap (/2)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20201216/387f268a/attachment-0001.html>


More information about the Haskell-Cafe mailing list