On Thu, Mar 6, 2008 at 9:52 PM, Ryan Ingram <ryani.spam@gmail.com> wrote:
This is actually a general issue with the way typeclasses are defined in Haskell; the accepted solution is what the "Show" typeclass does:
class C a where c :: a cList :: [a] cList = [c,c]
instance C Char where c = 'a' cList = "a" -- replaces instance for String above
instance C a => C [a] where c = cList cc = c :: String
I don't really like this solution; it feels like a hack and it relies on knowing when you define the typeclass what sort of overlap you expect.
The pattern above can be thought of as a specialized version of this: class C a where c :: a class ListOfC a where listOfC :: [a] instance (ListOfC a) => C [a] where c = listOfC This works for an arbitrary number of type constructors (good!), but requires a ListOfC instance for everything you might want to put into a list (cumbersome!). You can make things slightly less verbose by putting a sensible default in ListOfC, class (C a) => ListOfC a where listOfC :: [a] listOfC = default_c -- Dave Menendez <dave@zednenem.com> <http://www.eyrie.org/~zednenem/>