I'm trying to make a typeclass for signed numerical types. Here's my code:
{-# LANGUAGE TypeFamilies, FlexibleContexts, UndecidableInstances #-}
data Sign = Negative | Zero | Positive
deriving (Eq, Ord, Read, Show)
class Signed a where
sign :: a -> Sign
instance Signed Integer where
sign = undefined
This compiles, but I'd like to adapt this code to work on any Integral a
.
instance (Integral a) => Signed a where
sign = undefined
At which point it fails to compile.
I've checked Haskell type family instance with type constraints, but that seems to be addressing a different problem from mine. I don't think there's a syntax error, in my code.
OverlappingInstances
, I think. On the other hand, I think puttingsign
in a class may be a mistake in this case - just define a top-level functionsign :: Integral a => a -> Sign
. – Benjamin Hodgsoninstance Signed Integer
still compiles, butinstance (Integral a) => ...
still fails. – Fried BriceFlexibleInstances
too. With that, it compiles right away. (GHC's error messages are usually quite specific about which extensions you need to turn on.) – Benjamin Hodgson