F#: Can someone explain my compiler error?-Collection of common programming errors

Let’s try to reproduce how the compiler tries to infer types here.

let rec Foo(a,b) =
    match a () with
    | None -> Some(b)
    | Some(c) -> Some(Foo(c,b))

“Ok, so I see a (). a must be a function from unit to some type, I don’t know which one yet. I’ll call it 'a.”

a : unit -> 'a

“The result of a () is matched with None/Some patterns. So 'a must be a 'b option and c has the type 'b.” (Again, 'b stands for an unknown, as of yet, type).

a : unit -> 'b option
с : 'b

“No functions or methods are called on b (except Some, which doesn’t narrow the type down, and Foo, the type of which we don’t know so far). I’ll denote its type by 'c.”

a : unit -> 'b option
b : 'c
c : 'b

Foo returns Some(b) in one of the branches, so the return type must be 'c option.”

Foo : (unit -> 'b option) * 'c -> 'c option

“Am I done yet? No, I need to check that all types in the expression make sense. Let’s see, in the Some(c) case, Some(Foo(c,b)) is returned. So Foo(c,b) : 'c. Since Foo returns an option, I know 'c must be 'd option for some 'd, and b : 'd. Wait, I already have b : 'c, that is, b : 'd option. 'd and 'd option have to be the same type, but this is impossible! There must be an error in the definition. I need to report it.” So it does.

Originally posted 2013-11-09 23:04:41.