I defined a function id without type annotations:
# let id x = x;;
val id : 'a -> 'a = <fun>
# id 123;;
- : int = 123
# id true;;
- : bool = true
Everything is working as expected.
I defined id again, this time with type annotations:
# let id (x : 'a) : 'a = x;;
val id : 'a -> 'a = <fun>
# id 123;;
- : int = 123
# id true;;
- : bool = true
Everything is working as expected.
Then, I defined id again in a local expression without type annotations:
# let id x = x in
let _ = id 123 in
id true;;
- : bool = true
Everything is working as expected.
Then, I defined id again in a local expression with type annotations:
# let id (x : 'a) : 'a = x in
let _ = id 123 in
id true;;
Error: This expression has type bool but an expression was expected of type
int
Surprise! There is an error!
Why does the error occur? How does the last definition differ from the first three? I don't see how the type annotations are causing the error.