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

Olaf Klinke olf at aatal-apotheke.de
Sat Sep 12 12:39:53 UTC 2015

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. 
As to affine structures, Martin Escardo and Alex Simpson have a paper showing that the unit interval is the free cancellative midpoint algebra over two generators. They have Haskell code for this [1]. 
And yes, a typeclass is not a mathematical category, but Haskell is a language that lets you formalise quite a bit of category theory, and this is a cool thing to have. You can read a paper based on category theory and obtain executable code with very little effort. There are now entire Ph.D. theses written in Agda. 

To me it seems that the solution to the Num typeclass problem would be to have sub-typeclass statements that let you introduce aliases for the operations, as so: 

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

class Group a where
  (*) :: a -> a -> a
  unit :: a
  inverse :: a -> a

superclass Semigroup Group where
  (<>) = (*)

But wait, we do have a mechanism for aliases: It's called newtype! Conclusion: If you want to express that any Num instance is a Group, do so with a newtype. Data.Monoid is a good example for this. 

-- Olaf

[1] http://www.cs.bham.ac.uk/~mhe/.talks/map2011/martin_map.pdf

More information about the Haskell-Cafe mailing list