question on recursive types

From: Markus Mottl (mottl@miss.wu-wien.ac.at)
Date: Sun Mar 21 1999 - 20:47:55 MET


From: Markus Mottl <mottl@miss.wu-wien.ac.at>
Message-Id: <199903211947.UAA20606@miss.wu-wien.ac.at>
Subject: question on recursive types
To: caml-list@inria.fr (OCAML)
Date: Sun, 21 Mar 1999 20:47:55 +0100 (MET)

Hello,

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 (Stream.next s) self s done with Stream.Failure -> ()
  end

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 "bla.ml", 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 (Stream.next s) self s done with Stream.Failure -> ()
  end

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, mottl@miss.wu-wien.ac.at, http://miss.wu-wien.ac.at/~mottl



This archive was generated by hypermail 2b29 : Sun Jan 02 2000 - 11:58:21 MET