Haskell simply has two newtype wrapper for any type that actually is an instance of Num
(includes Int
type) typeclass: Sum
and Product
. So, Sum
is a monoid under addition and Product
type is a monoid under multiplication. Taken from actual source:
newtype Sum a = Sum { getSum :: a }
deriving (Eq, Ord, Read, Show, Bounded, Generic, Generic1, Num)
instance Num a => Monoid (Sum a) where
mempty = Sum 0
Sum x `mappend` Sum y = Sum (x + y)
newtype Product a = Product { getProduct :: a }
deriving (Eq, Ord, Read, Show, Bounded, Generic, Generic1, Num)
instance Num a => Monoid (Product a) where
mempty = Product 1
Product x `mappend` Product y = Product (x * y)