[Haskell-cafe] Thoughts about redesigning "Num" type class

Brandon Allbery allbery.b at gmail.com
Mon Sep 7 14:47:46 UTC 2015


On Mon, Sep 7, 2015 at 10:08 AM, DreamApart AtHaskells <rnmss.hs at gmail.com>
wrote:

> The class Num defines the method functions like (+), (-), (*), negate, ...
> But they are actually different concepts.
>
> We often meet situations that we want an addition for some type, but
> not a multiplication. For example:
>

I think the concept you're reaching for here is Monoid. However, there is
a(t least one) complication you did not consider:

(+) is assuredly a monoid.
But so is (*)! (Try looking at it through logarithms.)
Unfortunately, a given type can have only a single instance of a given
typeclass.

There is in fact a way to design Num "rationally", via number theory; I
suggest you at least familiarize yourself with that. (This will bring you
face to face with the above issue, as it turns out that Num requires two
monoids.) It turns out that this is not necessarily a good idea. But see
http://hackage.haskell.org/package/numeric-prelude for an implementation of
it.

-- 
brandon s allbery kf8nh                               sine nomine associates
allbery.b at gmail.com                                  ballbery at sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150907/3ed022af/attachment.html>


More information about the Haskell-Cafe mailing list