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

RnMss ForHaskellMailList rnmss.hs at gmail.com
Tue Sep 8 03:35:08 UTC 2015


No, I didn't know there such wiki page. Thank you.

2015-09-08 4:05 GMT+08:00 Alexey Muranov <alexey.muranov at gmail.com>:

> You saw this, right?
>
> https://wiki.haskell.org/Nitpicks#Base-related_nitpicks
>
> (Just in case, maybe it was you who included this topic.)
>
> Alexey.
>
>
> On Monday, September 7, 2015 at 4:08:54 PM UTC+2, DreamApart AtHaskells
> 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:
>>
>>     *data* Vec3 = Vec3 Float Float Float
>>
>> The additive (and subtractive) operation is obvious, but not for a
>> multiplication.
>>
>> *  Note*
>>
>>     (*) :: a -> a -> a   -- not avaliable for a Vec3
>>
>>     scale :: Float -> Vec3 -> Vec3
>>     dot :: Vec3 -> Vec3 -> Float
>>
>> We cannot define a (+) alone for a type, so we got many different
>> functions
>> from different libs, like mappend, mplus, plus, (<+>), (.+.), (+.),
>> which all reads
>> "plus".
>>
>> My opinion is that the methods of Num should be seperated into different
>> type
>> classes, so we don't need to invent a new symbol for a new lib.
>>
>> A feasible design is:
>>
>>     *import* Prelude *hiding* ( Num (..), Monoid (..), sum )
>>
>>     *class* SemiGroup a *where*
>>         (+)         :: a -> a -> a
>>
>>     *class* SemiGroup a => Monoid a *where*
>>         zero        :: a
>>
>>     *class* Monoid a => Group a *where*
>>         negate      :: a -> a
>>         (-)         :: a -> a -> a
>>
>>         x - y    = x + negate y
>>         negate x = zero - x
>>
>>     *class* Group a => Ring a *where   *-- not sure about the name ...
>>         (*)         :: a -> a -> a
>>
>>     *class* Ring a => Num a *where*
>>         abs         :: a -> a
>>         signum      :: a -> a
>>         fromInteger :: Integer -> a
>>
>>     sum :: (Foldable t, Monoid a) => t a -> a
>>     sum = foldl' (+) zero
>>
>>     -- for compatibility
>>     mempty = zero
>>     mappend = (+)
>>     mconcat = sum
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20150908/37f163ec/attachment.html>


More information about the Haskell-Cafe mailing list