I was trying to understand why the Haskell show considers a list of characters other than a list, for example. whole even without FlexibleInstances Pragma.
After reading the show documentation, I realized that I really do not understand how Haskell selects methods for instances of type classes.
Consider the following code:
class MyShow a where myShow :: a -> String myShowList :: [a] -> String myShowTuple :: (a, b) -> String myShowList xs = "Default List Implementation" myShowTuple t = "Default Tuple Implementation" instance MyShow Char where myShow c = "One Char" myShowList xs = "List of Chars" myShowTuple t = "Char Tuple" instance MyShow Int where myShow n = "One Int" myShowList xs = "List of Integers" myShowTuple t = "Int Tuple" instance MyShow Float where myShow n = show n instance (MyShow a) => MyShow [a] where myShow = myShowList instance (MyShow a) => MyShow (a, b) where myShowTuple t = "foo" myShow = myShowTuple
Now, if I call, for example,
myShow (5::Int,5::Int)
I would expect Haskell to think, "Oh, myShow got a tuple as an argument. Let's see which ones I should call." and selects the latter, which in turn will result in "foo" . Obviously, this is not so. It seems that Haskell considers the contents of the tuple (namely type a ) and decides to call the appropriate method, which results in "Int Tuple" .
Why is this?
source share