Haskell default superclass instances

I want to take some of the templates from Num declarations for several custom classes (call them Monomial and Polynomial). Instead of writing

instance Num (Monomial) where
    f - g = f + (negate g)
    abs _ = undefined

Is there any way around this? I came across standard instances of the superclass and something called the strathclyde haskell extension , which, if implemented, will allow me to write something like

class SimpleNum a => Num a where
    (+) :: a -> a -> a -- standard ring stuff
    (*) :: a -> a -> a
    one :: a
    zero :: a
    instance Num (SimpleNum a) where
        f - g = f + (negate g)
        abs _ = undefined

What is the usual / easy way to handle this?

+3
source share
1 answer

The usual ways to solve this problem is to perform at least one or more of the following actions:

  • Steal a lot.

  • Write helper functions as follows:

 
simpleMinus fg = f + (negate g)
+2

All Articles