Why can I not make String an instance of a typeclass?

This is because String is just a type alias for [Char], which is just the application of the type constructor [] on the type Char, so this would be of the form ([] Char). which is not of the form (T a1 .. an) because Char is not a type variable.

The reason for this restriction is to prevent overlapping instances. For example, let’s say you had an instance Fooable [Char], and then someone later came along and defined an instance Fooable [a]. Now the compiler won’t be able to figure out which one you wanted to use, and will give you an error.

By using -XFlexibleInstances, you’re basically promising to the compiler that you won’t define any such instances.

Depending on what you’re trying to accomplish, it might be better to define a wrapper:

newtype Wrapper = Wrapper String
instance Fooable Wrapper where
    ...

Leave a Comment