Skip to content

Better error messages by adding impossible instances #42

@tomjaguarpaw

Description

@tomjaguarpaw

It was raised on Reddit that the error for 1 + [2, 3] is inscrutable:

Prelude> 1+[2,3]

<interactive>:1:1: error:
    • Non type-variable argument in the constraint: Num [a]
      (Use FlexibleContexts to permit this)
    • When checking the inferred type
        it :: forall a. (Num a, Num [a]) => [a]

I think the ultimate problem is, rather than any particular processing that the compiler does of error messages, that instance Num [a] should be explicitly forbidden rather the left unspecified. That is, the following seems to read much better

> 1 + [2,3]
<interactive>:7:1: error:
    • [a] is not an instance of Num
    • When checking the inferred type
        it :: forall a. ((TypeError ...), Num a) => [a]

which I achieve with

> :set -XUndecidableInstances -XTypeFamilies -XDataKinds -XTypeOperators
> import GHC.TypeLits
> import Data.Kind
> type family E t :: Constraint
> type instance E [a] = TypeError (ShowType [a] :<>: Text " is not an instance of Num")
> instance E [a] => Num [a] where
<interactive>:6:10: warning: [-Wmissing-methods]
...

Ultimately this would need to go through the CLC process, but I'm presenting it to the error-messages first to receive their expert opinion on whether this change would be worthwhile.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions