caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jeremy Yallop <yallop@gmail.com>
To: Peter Frey <pjfrey@sympatico.ca>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Unifying buildt-in types with polymorphic types
Date: Thu, 15 May 2014 21:28:26 +0100	[thread overview]
Message-ID: <CAAxsn=GbAD-fVgi-ShbdgCDe=+wg4e1UNLurqERVRcVnHjp5TQ@mail.gmail.com> (raw)
In-Reply-To: <BLU0-SMTP8A20169962ECB3568E690A3360@phx.gbl>

On 15 May 2014 20:24, Peter Frey <pjfrey@sympatico.ca> wrote:
> I need all streams to have the same signature 'StreamOps'.

It's not possible with the current definition of StreamOps.  The
difficulty is that the stream type and the element type vary
non-uniformly in your various stream implementations:

   StmLzy defines streams of type 'a t = Cons of 'a * 'a t Lazy.t
                  with elements of 'a

   StmAry defines streams of type 'a t = int * 'a cursor
                  with elements of 'a

   StmStr defines streams of type 'a t = int * 'a cursor
                  with elements of char

but the signature leaves only the stream type 'a t unspecified,
providing no way for the element type to vary in the implementation.

One solution is to change the StreamOps signature to include both the
element type and the stream type.  Once you've added an element type
to the signature there's no need to parameterize the stream type:

   module type StreamOps = sig
     type elt (* the element type *)
     type t   (* the stream type *)
     val head : t -> elt
   end

The implementations of StreamOps then become functors over the element
type.  This is the approach used in the standard library to define
maps and sets (http://caml.inria.fr/pub/docs/manual-ocaml/libref/Set.Make.html).
 For example, you might define LazyStream as follows

   module LazyStream (E : sig type elt end)
      : StreamOps with type elt = E.elt =
   struct
     type elt = E.elt
     type t = Cons of elt * t Lazy.t
     let head (Cons (h, _)) = h
   end

The element type of StmStr doesn't vary, so there's no need to make it
a functor:

   module StmStr : StreamOps with type elt = char =
   struct
     type elt = char
     type cursor = { ofs: int; lim: int; data: string }
     type t = int * cursor
     let get s i = String.get s.data i
     let head (i, s) = get s i
   end

I hope that helps,

Jeremy.

  reply	other threads:[~2014-05-15 20:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-05-15 19:24 Peter Frey
2014-05-15 20:28 ` Jeremy Yallop [this message]
2014-05-16 21:41   ` Peter Frey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAAxsn=GbAD-fVgi-ShbdgCDe=+wg4e1UNLurqERVRcVnHjp5TQ@mail.gmail.com' \
    --to=yallop@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=pjfrey@sympatico.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).