
On Montag, 6. Juni 2011, 05:07, Arlen Christian Mart Cuss wrote:
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.
Note however, that `expected' and `inferred' are not intrisic, which is which depends on the order of type-checking. For example: Prelude> do { c <- "string"; putChar c; } <interactive>:1:20: Couldn't match expected type `[b]' against inferred type `IO ()' In the expression: putChar c In the expression: do c <- "string" putChar c In the definition of `it': it = do c <- "string" putChar c so typechecking here proceeds left-to-right. If it proceeded right-to-left, you'd get an expected type of `IO a' and an inferred type of `[Char]'. Also note that ghc-7.* says: Couldn't match expected type `[b0]' with actual type `IO ()' i.e., uses `actual' instead of `inferred' in the hope that that is less confusing.