Name: Anonymous 2007-10-30 19:37
So I wrote this sweet code to determine the arity of a function in Haskell:
But it won't work. GHC complains about overlapping instances, and I kind of understand where its coming from; there is some ambiguity there. Does anyone know how to make this work?
I've also run into other problems with overlapping instances. For example, I know the Show class has to use a hack to get around the ambiguity between (Show a) => [a] and [Char]. Why can't the compiler just choose the most specific instance?
class Arity a where
arity :: a -> Integer
instance (Arity b) => Arity (a -> b) where
arity f = 1 + arity $ f undefined
instance Arity (a -> b) where
arity _ = 1But it won't work. GHC complains about overlapping instances, and I kind of understand where its coming from; there is some ambiguity there. Does anyone know how to make this work?
I've also run into other problems with overlapping instances. For example, I know the Show class has to use a hack to get around the ambiguity between (Show a) => [a] and [Char]. Why can't the compiler just choose the most specific instance?