It has to do with the types being inferred in your expressions.
same type since both branches of an `if` expression must be same type
↓ ↓
let h x y z = if x then y else z
↑
must be `bool` since its in the place of a condition
So we end up with
val h : bool -> 'a -> 'a -> 'a
↑ ↑ ↑ ↑
x y z return type
same type
With the second expression, its similar only one of the variables isn't used, so its type can potentially be anything (isn't constrained to any rules in your expression definition).
unused, could be any type
| are the same variable, therefore same type
↓ ↓ ↓
let i x y z = if x then y else y
↑
must be `bool` since its in the place of a condition
So we end up with
val h : bool -> 'a -> 'b -> 'a
↑ ↑ ↑ ↑
x y z return type
Here, both 'a
and 'b
represents some arbitrary type that aren't necessarily the same. For example, 'a
could be any type, but all 'a
's are the same type. Same goes for 'b
.
'a
and 'b
could be the same type, but they don't have to be. z
in your second expression is of type b'
since it doesn't have any restrictions on what type it can be, therefore it doesn't have to be type 'a
like it does in the first expression.
if
expression. The second function is not using its last argument. so the constraint does not apply to it. Its type can therefore be different from the type of the second argument. – n. 1.8e9-where's-my-share m.