[Haskell-cafe] Thoughts about redesigning "Num" type class
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?
> (Just in case, maybe it was you who included this topic.)
> On Monday, September 7, 2015 at 4:08:54 PM UTC+2, DreamApart AtHaskells
>> 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
>> * 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
>> from different libs, like mappend, mplus, plus, (<+>), (.+.), (+.),
>> which all reads
>> My opinion is that the methods of Num should be seperated into different
>> 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...
More information about the Haskell-Cafe