Why does Haskell not allow Type Synonyms when declaring class instances?

TypeSynonymInstances is perfectly safe. Since anything potentially fancy like partially applied type synonyms is disallowed, it has exactly the same effect as typing out the the right hand side of the type synonym in the instance head, i.e.

type Foo a = ([a], [a])
instance Bar (Foo a)

is the same as

instance Bar ([a], [a])

However, note that both instances require FlexibleInstances since they contain nested type constructors as well as repeated type variables. In general, this will often be the case after expanding type synonyms.

I think may be the reason why they’re disallowed by default.

However, FlexibleInstances is also a perfectly safe extension. The worst it can do is cause a compile-time error if you try to define overlapping instances such as

instance Xyzzy String
instance Xyzzy [a]

As for why FlexibleInstances is not available by default, I can only guess that it’s to simplify the language. The standard rules for instance heads ensures that the only way instance definitions can overlap is if the type constructors in the instance heads are identical, while with FlexibleInstances checking for overlaps is slightly more difficult.

Leave a Comment