Use functional dependencies to give more specific error messages
An example from http://www.reddit.com/r/haskell/comments/2gryy8/stdlib_monad_error_messages_unhelpful/:
f ::(Eq a) => a -> (Int, a) -> Writer [(Int, a)] (Int, a)
f y (n,x) | y == x = return (n+1, x)
| otherwise = do tell (n,x)
return (1,y)
Could not deduce (MonadWriter
(Int, a) (WriterT [(Int, a)] Data.Functor.Identity.Identity))
arising from a use of ‘tell’
from the context (Eq a)
bound by the type signature for
f :: Eq a => a -> (Int, a) -> Writer [(Int, a)] (Int, a)
at 180.hs:42:5-57
In a stmt of a 'do' block: tell (n, x)
In the expression:
do { tell (n, x);
return (1, y) }
In an equation for ‘f’:
f y (n, x)
| y == x = return (n + 1, x)
| otherwise
= do { tell (n, x);
return (1, y) }
GHC could realize that the class MonadWriter m w
has a functional dependency w -> m
and notice that there is an instance MonadWriter w (WriterT w m1)
whose m
-part unifies with that of the needed constraint. Therefore the error cannot be a missing instance, and GHC could give a more friendly error message like
Couldn't match expected type `[(Int, a)]'
with actual type `(Int, a)'
when unifying the instance `MonadWriter w (WriterT w m1)'
with the constraint `MonadWriter (Int, a) (WriterT [(Int, a)] Identity)'
arising from a use of 'tell'
In a stmt of a 'do' block: tell (n, x)
In the expression:
...
(Or, if necessary, Could not deduce [(Int, a)] ~ (Int, a) from the context (Eq a) ...
)
Trac metadata
Trac field | Value |
---|---|
Version | 7.8.3 |
Type | FeatureRequest |
TypeOfFailure | OtherFailure |
Priority | normal |
Resolution | Unresolved |
Component | Compiler (Type checker) |
Test case | |
Differential revisions | |
BlockedBy | |
Related | |
Blocking | |
CC | |
Operating system | |
Architecture |