Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
question on recursive types
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 1999-03-22 (07:53)
From: Markus Mottl <mottl@m...>
Subject: question on recursive types

I am trying to find a useful approach for sending streams of messages
to objects and have come across the following problem (code example):

  class transformer = object (self)
    method apply_strm s =
      try while true do ( s) self s done with Stream.Failure -> ()

Method "apply_strm" gets a stream of functions which take as additional
parameters the "self"-object and the rest of the stream.

These functions are called and they again invoke some method in the
"self"-object with the rest of the stream as final parameter (not shown in
example - unnecessary for demonstrating problem).

This approach would allow very flexible interpretation/execution of
message streams as (e.g.) they might be generated by parsers.

Unfortunately, this results in a recursive type, because the elements of
the stream are functions that have to accept the same type of streams
as parameter. I have already tried to explicitely specify the type of
the stream, but it seems impossible to get rid of the error:

  File "", line 3, characters 43-44:
  This expression has type
    (< apply_strm : 'a -> 'b; .. > -> 'c -> 'd) Stream.t as 'a
  but is here used with type 'c

How can I (if possible at all) tell the compiler that 'c and 'a are
actually the same type? It doesn't work to use "as" for binding the type
of the stream to an identifier and use this one in place of 'c. E.g.:

  class transformer = object (self : 'self)
    method apply_strm (s: (('self -> 'a -> unit) Stream.t as 'a)) =
      try while true do ( s) self s done with Stream.Failure -> ()

The problem can be shown with a shorter, different and now probably not
very useful expression:

  let f (h::t) = h t;;

Best regards,
Markus Mottl

Markus Mottl,,