[Haskell-cafe] Functional Dependencies and
miguelimo38 at yandex.ru
Mon Aug 24 07:29:46 EDT 2009
> However, I do not really understand the cause of the original problem. Why
> do I need the functional dependency to make this work ?
Suppose you have an instance:
instance Pixel MyPixel where...
instance Image MyImage MyPixel where
width i = countPixels i
And somebody (not necessarily you) makes another instance:
instance Pixel CustomPixel where...
instance Image MyImage CustomPixel where
width i = 0
Now, take any image
im :: MyImage
Then you'd expect that
width im :: Int
But now there are TWO implementations of "width :: MyImage -> Int" and
no way for compiler to figure out which one to use.
GHC sort of foresees this problem. By adding a functional dependency you
instruct it to forbid the second declaration, when the first one is
present, thus removing the problem.
More information about the Haskell-Cafe