[Haskell-cafe] "Natural" polymorphism for n*(n+1)/2
Tom Smeding
x at tomsmeding.com
Wed Dec 16 22:41:53 UTC 2020
I'm not sure what "sound" means if the documentation[1] for Num states that the only reasonable expectation is that of a ring.
However, if we are to have laws for signum and abs, then I would expect that 'abs n = n * signum n'. My definitions satisfy that law.
Furthermore for polynomials, I don't think there is a useful definition of 'abs' where 'abs n' has a higher degree than 'n' itself. Assuming that 'abs' does not increase the degree, I don't think there are any other _useful_ definitions of abs and signum than the ones I gave: multiplication in Z[X] will never decrease the degree, so the degree of 'n * signum n' is at least that of 'n', and therefore the degree of 'abs n' is the same as that of 'n'; therefore 'signum n' is a constant polynomial (without X'es). With that restriction the only reasonable choice I see is 'signum = const 1'.
But even if you take 'abs' and 'signum' to be absolutely wild functions, to be able to write n*(n+1)/2 you need to define your division operator. There is certainly not one in base that will work for 'Int -> Int' as well as 'Float -> Float' as well as 'Poly Integer -> Poly Integer'. And in particular not abs nor signum. :)
- Tom
[1]: https://hackage.haskell.org/package/base-4.14.1.0/docs/Prelude.html#t:Num
-------- Original Message --------
On 16 Dec 2020, 23:26, Henning Thielemann < lemming at henning-thielemann.de> wrote:
On Wed, 16 Dec 2020, Tom Smeding wrote:
> @Douglas:
> What about the ring of polynomials over the integers, i.e. Z[X]? We can certainly define a Num instance for that
> if we set 'signum _ = 1' and 'abs = id'. 'fromInteger' then injects constant polynomials.
I also thought about polynomials, but Num is a Ring plus 'signum' and
'abs'. Are your definitions ob 'signum' and 'abs' sound? Are there other
sound definitions that may allow the n*(n+1)/2 magic?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20201216/85ddfcc6/attachment.html>
More information about the Haskell-Cafe
mailing list