Hello --

I've encountered a typing error that I'm not sure how to track down. I assume that what I'm doing is running up against the limitations of the type system, so I assume there is some technical detail that I am missing. Here's my problem:

I have defined the following type:

(* essentially the eliminator for lists *)
type ('a,'b) result_stream =
  ('a -> ('a,'b) result_stream -> 'b) ->
  (unit -> 'b) -> 'b

(* I have the following function, which seems to type check *)
let stream_declarations (type a)
: (internal_result,a) result_stream = ...

(* I am unable to implement this function *)
let generic_search_stream (type b) (glnumopt : int option)
: (internal_result, b) result_stream =
  stream_declarations

Ocaml complains with the following error message:

Error: This expression has type
         (internal_result, b) result_stream =
           (internal_result -> (internal_result, b) result_stream -> b) ->
           (unit -> b) -> b
       but an expression was expected of type
         (internal_result, b) result_stream =
           (internal_result -> (internal_result, b) result_stream -> b) ->
           (unit -> b) -> b
       The type constructor b would escape its scope

Since the types match up exactly, I can only assume that this has something to do with the type declaration (type b) but I can't see what is wrong. My expectation is that the scope of [b] ends at the end of the [generic_search_stream] function and is used as the type parameter to [stream_declarations], this seems to not be the case though.

Wondering if OCaml requires arguments in order to instantiate polymorphic functions, I tried to eta-expand the definition as follows:

let generic_search_stream (type b) (glnumopt : int option)
: (internal_result, b) result_stream =
  fun cons nil -> stream_declarations cons nil

With this code, the "error" is with 'cons' and says:

Error: This expression has type
         internal_result -> (internal_result, b) result_stream -> b
       but an expression was expected of type
         internal_result -> (internal_result, b) result_stream -> b
       Type
         (internal_result, b) result_stream =
           (internal_result -> (internal_result, b) result_stream -> b) ->
           (unit -> b) -> b
       is not compatible with type
         (internal_result, b) result_stream =
           (internal_result -> (internal_result, b) result_stream -> b) ->
           (unit -> b) -> b 
       The type constructor b would escape its scope

Essentially the same error, but it doesn't seem to lead me to any potential solutions.

Can someone explain this error to me?

Thanks.

--
gregory malecha