From: Peter Frey <pjfrey@sympatico.ca>
To: Jeremy Yallop <yallop@gmail.com>
Cc: Caml List <caml-list@inria.fr>
Subject: Re: [Caml-list] Unifying buildt-in types with polymorphic types
Date: Fri, 16 May 2014 17:41:20 -0400 [thread overview]
Message-ID: <BLU0-SMTP744C1A4412709211A7D786A3310@phx.gbl> (raw)
In-Reply-To: <CAAxsn=GbAD-fVgi-ShbdgCDe=+wg4e1UNLurqERVRcVnHjp5TQ@mail.gmail.com>
On Thu, 2014-05-15 at 21:28 +0100, Jeremy Yallop wrote:
> 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.
>
This helped a lot, thank you.
(Typing is hard; I have been programming since 1963 and always had a
clear idea what I was doing, because previous languages translated
statements into code; Assemblers (many) Cobol Fortran Snobol Prolog
Pascal Cs ... Now the type checker translates types into types...
perhaps I am getting to old...)
thanks again
Peter
prev parent reply other threads:[~2014-05-16 21:41 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
2014-05-16 21:41 ` Peter Frey [this message]
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=BLU0-SMTP744C1A4412709211A7D786A3310@phx.gbl \
--to=pjfrey@sympatico.ca \
--cc=caml-list@inria.fr \
--cc=yallop@gmail.com \
/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