[Haskell-cafe] A question about functional dependencies

Matthew Brecknell haskell at brecknell.org
Thu Mar 29 01:33:38 EDT 2007

oleg at pobox.com:
> [...] The above two instances show there
> exists a model of T where the functional dependency is
> violated. That's why both GHC 6.4 and Hugs reject the instance. Again,
> it is a mystery why GHC 6.6 accepts it.

Actually, GHC 6.6 does reject cases like the one discussed in this
thread, but the check is not performed at the point of instance
declaration. Instead, it is deferred until the point of use. For

> {-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances #-}
> data Any a = Any
> class Foo a b | a -> b where foo :: a -> b
> instance Foo (Any a) b where foo = undefined
> test1 :: Char
> test1 = foo (Any :: Any Int)

GHC 6.6 compiles the above without error. But add this:

> test2 :: Bool
> test2 = foo (Any :: Any Int)

And we get:

Couldn't match expected type `Char' against inferred type `Bool'
When using functional dependencies to combine
  Foo (Any Int) Bool, arising from use of `foo' at fd.hs:16:8-27
  Foo (Any Int) Char, arising from use of `foo' at fd.hs:13:8-27

More information about the Haskell-Cafe mailing list