[Haskell-cafe] Extending the idea of a general Num to other types?

Simon Peyton-Jones simonpj at microsoft.com
Wed Sep 5 04:56:18 EDT 2007

| >         when you come across a case where GHC produces an
| >         unhelpful message, send it in, along with the program
| >         that produced it,
| Contents of test/error.hs:
|     f x s = x + show s
| Error message from GHCi:
|     test/error.hs:2:8:
|         No instance for (Num String)
|           arising from use of `+' at test/error.hs:2:8-17
|         Possible fix: add an instance declaration for (Num String)
|         In the expression: x + (show s)
|         In the definition of `f': f x s = x + (show s)
| >         your suggestion for the error message you'd like to have seen.
| Suggestion:
| As is, with removal the "Possible fix", as it is often misleading (i.e.
| here, the programmer clearly meant to use '++' and not '+'.

Is your suggestion specific to String?  E.g. if I wrote

        data Complex = MkC Float Float

        real :: Float -> Complex
        real f = MkC f 0

        f x s = x + real 1

then I really might have intended to use Complex as a Num type, and the suggestion is precisely on target.  I'd be interested to know this particular "helpful suggestion" on GHC's part is more misleading than useful.  What do others think?

| rephrase to something like "String is not an instance of Num"?  For a
| newbie, it may not be clear that Num is the class and String is the
| type.

Good point.  Not so easy for multi-parameter type classes! E.g. No instance for (Bar String Int).  So we could have

        String is not an instance of class Foo  -- single param
        No instance for (Bar String Int)                -- multi-param

Would that be better (single-param case is easier), or worse (inconsistent)?


More information about the Haskell-Cafe mailing list