90

Given:

data Foo =
  FooString String
  …

class Fooable a where --(is this a good way to name this?)
  toFoo :: a -> Foo

I want to make String an instance of Fooable:

instance Fooable String where
  toFoo = FooString

GHC then complains:

Illegal instance declaration for `Fooable String'
    (All instance types must be of the form (T t1 ... tn)
     where T is not a synonym.
     Use -XTypeSynonymInstances if you want to disable this.)
In the instance declaration for `Fooable String'

If instead I use [Char]:

instance Fooable [Char] where
  toFoo = FooString

GHC complains:

Illegal instance declaration for `Fooable [Char]'
   (All instance types must be of the form (T a1 ... an)
    where a1 ... an are type *variables*,
    and each type variable appears at most once in the instance head.
    Use -XFlexibleInstances if you want to disable this.)
In the instance declaration for `Fooable [Char]'

Question:

  • Why can I not make String and instance of a typeclass?
  • GHC seems willing to let me get away with this if I add an extra flag. Is this a good idea?
3
  • 7
    This is the kind of questions I upvote and mark as favorite because otherwise I know in a near future I would be asking it ;) May 9, 2011 at 19:52
  • 3
    Regarding the extra flag: it's probably a good idea, as long as you trust GHC and understand what the flag does. Yesod comes to mind: it encourages you to always use the OverloadedStrings pragma when writing Yesod apps, and QuasiQuotes are a necessity for Yesod routing rules. Note that instead of a flag at compile time, you can also put {-# LANGUAGE FlexibleInstances #-} (or any other pragma) at the top of your .hs file.
    – Dan Burton
    May 10, 2011 at 1:39
  • Take a look at The constraint trick for instances. If you want a String instance and not an instance for lists you would actually write instance Char ~ char => Fooable [char] where .. but usually you will want to follow Lemming's advice. Another approach is to enable OverloadedStrings and define: instance IsString Foo where fromString :: String -> Foo; isString = FooString Aug 27, 2021 at 16:58

4 Answers 4

69

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
    ...
2
  • 4
    Lets say for sake of argument that I did indeed want instance Fooable [a] as well. Is there a way to make the toFoo function behave differently if a is a Char? May 9, 2011 at 23:37
  • 7
    @John: There is an extension -XOverlappingInstances which will allow this, and pick the most specific instance. See the GHC user's guide for details.
    – hammar
    May 9, 2011 at 23:50
19

You're running into two limitations of classic Haskell98 typeclasses:

  • they disallow type synonyms in instances
  • they disallow nested types that don't in turn contain type variables.

These onerous restrictions are lifted by two language extensions:

  • -XTypeSynonymInstances

which allows you to use type synoyms (like String for [Char]), and:

  • -XFlexibleInstances

which lift the restrictions on instance types being of the form T a b .. where the parameters are type variables. The -XFlexibleInstances flag allows the head of the instance declaration to mention arbitrary nested types.

Note that lifting these restrictions can sometimes lead to overlapping instances, at which point, an additional language extension might be needed to resolve the ambiguity, allowing GHC to pick an instance for you.


References::

5

FlexibleInstances are not a good answer in most cases. Better alternatives are wrapping the String in a newtype or introduce a helper class like so:

class Element a where
   listToFoo :: [a] -> Foo

instance Element Char where
   listToFoo = FooString

instance Element a => Fooable [a] where
   toFoo = listToFoo

See also: http://www.haskell.org/haskellwiki/List_instance

2

Adding to these answers, if you are not comfortable with lifting the restrictions, there may be cases where it could make sense to wrap your String in a newtype, which can be an instance of a class. The tradeoff would be potential ugliness, having to wrap and unwrap in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.