Type checker's expected and inferred types (reformatted)
David Menendez
dave at zednenem.com
Sat Oct 24 00:50:57 EDT 2009
On Fri, Oct 23, 2009 at 9:46 PM, Isaac Dupree
<ml at isaac.cedarswampstudios.org> wrote:
> C Rodrigues wrote:
>>
>> fun1 produces the error message:
>> Couldn't match expected type `Maybe a' against inferred type `IO ()'
>> In the first argument of `(>>=)', namely `bar'
>>
>>
>> fun2 produces the error message:
>> Couldn't match expected type `IO ()' against inferred type `Maybe ()'
>> In a stmt of a 'do' expression: bar
>>
>>
>> It's confusing because 'bar' is inferred to have type Maybe (), even
>> though it's explicitly declared to be an IO ().
>
> Which message do you prefer? I couldn't tell which it was.
>
> For myself, I never understood the difference between "expected" and
> "inferred": it works better for me to just think "there were at least two
> different ways that determined the 'type' of this expression, and the
> results contradicted each other, and here are two of those results. Now,
> dear user, go and look at your code to intuit *what* those ways of
> determining the type might have been" (or sometimes it's easier just to look
> for mistakes, ignoring the particular details of the error)
The expected type is what the context wants (it's *ex*ternal). The
inferred type is what the expression itself has (it's *in*ternal).
So inferring the type Maybe () for bar seems wrong. I'm guessing it's
a bug in the way do-expressions are handled. Note that this doesn't
happen if the bar is last.
Prelude> :t let fooThen m = foo >> m in fooThen (do undefined; bar)
<interactive>:1:51:
Couldn't match expected type `Maybe b'
against inferred type `IO ()'
In the expression: bar
In the first argument of `fooThen', namely
`(do undefined
bar)'
In the expression:
fooThen
(do undefined
bar)
Prelude> :t do foo; bar
<interactive>:1:8:
Couldn't match expected type `Maybe b'
against inferred type `IO ()'
In the expression: bar
In the expression:
do foo
bar
Prelude> :t do foo; bar; foo
<interactive>:1:8:
Couldn't match expected type `IO ()'
against inferred type `Maybe ()'
In a stmt of a 'do' expression: bar
In the expression:
do foo
bar
foo
--
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>
More information about the Glasgow-haskell-users
mailing list