caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* question on recursive types
@ 1999-03-21 19:47 Markus Mottl
  1999-03-22 12:53 ` Jerome Vouillon
  0 siblings, 1 reply; 2+ messages in thread
From: Markus Mottl @ 1999-03-21 19:47 UTC (permalink / raw)
  To: OCAML

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




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: question on recursive types
  1999-03-21 19:47 question on recursive types Markus Mottl
@ 1999-03-22 12:53 ` Jerome Vouillon
  0 siblings, 0 replies; 2+ messages in thread
From: Jerome Vouillon @ 1999-03-22 12:53 UTC (permalink / raw)
  To: Markus Mottl, OCAML


Hello,

On Sun, Mar 21, 1999 at 08:47:55PM +0100, Markus Mottl wrote:
> 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
[...]
> 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.
[...]

The usual trick to avoid explicitely recursive types is to use a type
constructor:

    type 'a t = Stream of ('a -> 'a t -> unit) Stream.t;;
    class transformer = object (self)
      method apply_strm s =
        try
          while true do (Stream.next s) self (Stream s) done
        with Stream.Failure -> ()
    end;;

-- Jérôme




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1999-03-22 14:38 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-03-21 19:47 question on recursive types Markus Mottl
1999-03-22 12:53 ` Jerome Vouillon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).