[Haskell-beginners] Multi-parameter type classes and ambiguous type variables...

Stuart Hungerford stuart.hungerford at gmail.com
Fri Mar 20 21:39:37 UTC 2015


Hi,

As a learning exercise I'm modelling some algebraic structures as
Haskell typeclasses. Some of these are multi-parameter type classes.
Here's a very simplified version of the type class relationships:

class MM a where
  one :: a

class AM a where
  zero :: a

class (AM a, MM a) => SR a

class (AM a) => AG a where
  inv :: a -> a

class (SR a) => R a where
  neg :: a -> a

class (R r, AG g) => M r g where
  sca :: r -> g -> g

check :: (Eq g, M r g) => g -> Bool
check x = sca one x == x

The problem I have is that GHC is finding the "r" type variable in the
"check" function ambiguous. Given my still limited Haskell knowledge
I'm not surprised this is happening. What I would like to know is how
experienced Haskellers handle this situation in practice: is there an
idiomatic way of disambiguating "r" or is it a sign of poor type class
design?

Thanks,

Stu


More information about the Beginners mailing list