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

M Farkas-Dyck strake888 at gmail.com
Sat Sep 12 22:14:37 UTC 2015


On 12/09/2015 at 14:39:53 +0200, Olaf Klinke wrote:
> I sencond the statement that semirings might be a useful typeclass to have. Another example: Non-negative reals with infinity, the values that a measure can take. Nevertheless the biggest issue is what _symbol_ to use for the operations. We know that a ring is a semigroup in two ways, and no matter what semigroup structure we think the ring to "originate" from, we're going to annoy the other half of users who thought it the other way around. Some semigroups are thought of as (+), others as (*). Picking one of these symbols for all semigroups will be counter-intuitive for some structures. 

Consider this:

class Semigroup a where
    (<>) :: a -> a -> a

newtype Sum a = Sum a

newtype Product a = Product a

(+) :: Semigroup (Sum a) => a -> a -> a
(*) :: Semigroup (Product a) => a -> a -> a

class Semigroup a => Abelian a

-- with ConstraintKinds
type Semiring a = (Abelian (Sum a), Semigroup (Product a))


More information about the Haskell-Cafe mailing list