Why doesn't this code work:
class Foo a b c | a b -> c where
foo :: a -> b -> c
instance Foo Int Int Int where
foo a b = a + b
ghci > foo 4 4 -- This produces run time error
And by using functional dependency, why the following code produces compile time error:
instance Foo Float Float Int where
foo a b = a + b
I know that the above instance is an crazy example, but isn't the aim of functional dependency to help the type checker in resolving these issues ?
Actually it did resolve the ambiguity. The problem is that
4 :: Num a => a
so GHC can't decide that you want to usefoo :: Int -> Int -> Int
. Now if instead you didSince now it is clear which instance we want to use. To make this clearer, suppose we had
Now we could do
since once GHC has filled in all the type variables not on the right side of an
->
, it can fill in the rest.