[Haskell-beginners] Expected vs. Inferred Type
Arlen Christian Mart Cuss
celtic at sairyx.org
Mon Jun 6 05:07:53 CEST 2011
On Sun, 2011-06-05 at 17:44 -0800, Christopher Howard wrote:
> Could someone explain the difference between an expected type and an
> inferred type as mentioned in a compiler error message? I've never heard
> definitions proper, and so I tend to get confused when the compiler says
> there is a difference between the type it expected and the type it inferred.
Hi Christopher,
The expected type is the type expected by the compiler in that position;
e.g. the argument of `not' should be a Bool:
> :t not
not :: Bool -> Bool
>
Hence in the expression `not x', `x' is *expected* to be a Bool.
The inferred (or actual) type is the type that Haskell has determined
the value *actually* is. For instance:
> not "abc"
<interactive>:1:5:
Couldn't match expected type `Bool' with actual type `[Char]'
In the first argument of `not', namely `"abc"'
In the expression: not "abc"
In an equation for `it': it = not "abc"
>
The actual type of the argument to `not' is [Char], but the compiler
needs `Bool'.
tl;dr: "expected" is what the compiler wants, "inferred" is what you
gave it.
More information about the Beginners
mailing list