This little problem raises two challenges for compiler developers:
(a) improving diagnostic messages (a perennial issue), and (b) (semi)
automating error correction.  In this case the diagnostic message
is pretty good, except that is doesn't indicate where the IO term
is or where the [] term is that doesn't match.  This is the critical
piece of information Malcolm added to explain the problem.

It seems that lifting would be a natural extension to the type
unification process to allow automating "corrections".  When you
find several possible lifting functions, of course, you are in
trouble.  But then you could add that information to the diagnostic
message.  This may be trying too hard to help the fat fingered
programmer (aka, yours truly) who simply entered an incorrect
expression.  Reactions anybody?

Thanks to Malcolm and the other responders for such clear explanations
and clean solutions.

-- Reg

>> getDirectoryContents "." >>= filter (\(x:_) -> x /= '.');
>> [65] Cannot unify types:
>>     Prelude.IO
>> and (Prelude.[])
>
>The problem is that `filter's result type is [a], not
>(IO [a]) which its use as an argument to >>= requires.
>The fix is easy: lift its result into the monad (using `return').
>
>  getDirectoryContents "." >>= return . filter (\(x:_) -> x /= '.')
>


Reply via email to