haskell / haskell/error-messages

Better error messages for the monomorphism restriction

Open
#40 6 comments 2 reactions 0 assignees View on GitHub
status:Composing error message tool:GHC type:error-message
Dominant language
No language data
Stars
76
Forks
19
PR merge metrics
No merged PRs in 30d

Description

Today [Competitive_Ad2539 on Reddit asks why type inference fails for this code](https://reddit.com/r/haskellquestions/comments/sp9u4a/why_did_the_type_inference_failed_here/):

```haskell
showLine = (++ "\n") . concat
```

The error message is (with GHC 9.2.1 and 9.0.2):

```
T.hs:1:24: error:
* Ambiguous type variable `t0' arising from a use of `concat'
prevents the constraint `(Foldable t0)' from being solved.
Relevant bindings include
showLine :: t0 [Char] -> [Char] (bound at T.hs:1:1)
Probable fix: use a type annotation to specify what `t0' should be.
These potential instances exist:
instance Foldable (Either a) -- Defined in `Data.Foldable'
instance Foldable Maybe -- Defined in `Data.Foldable'
instance Foldable ((,) a) -- Defined in `Data.Foldable'
...plus two others
...plus 26 instances involving out-of-scope types
(use -fprint-potential-instances to see them all)
* In the second argument of `(.)', namely `concat'
In the expression: (++ "\n") . concat
In an equation for `showLine': showLine = (++ "\n") . concat
|
1 | showLine = (++ "\n") . concat
| ^^^^^^
```

Experts might see at a glance that this is probably caused by the monomorphism restriction, but that is not at all obvious from this error message.

I would propose adding a note in the error message that the monomorphism restriction prevents it from inferring the most general type.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.