caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: kaspar.rohrer@gmail.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Functions over polymorphic variants
Date: Tue, 23 Mar 2010 15:11:40 +0900 (JST)	[thread overview]
Message-ID: <20100323.151140.125875815.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <D9CF4E35-720D-4CB2-B213-4B4B61CEA32E@bluewin.ch>

From: Kaspar Rohrer <kaspar.rohrer@gmail.com>
> I am tinkering with streams of polymorphic variants at the
> moment. One thing that I would like to do is to write a stream
> transformer (not sure this is the correct terminology) that simply
> drops certain values of an open polymorphic type, and have the type
> of the function reflect this.

Sorry, you can't.
This is a design choice with ocaml polymorphic variants, that the row
variable is kept hidden. This has the advantage of simplicity, but it
makes impossible to abstract on the row variable itself, like you want
here.

> This is trivial with a closed polymorphic type:
> 
> type abc = [`a | `b | `c]
> 
> let transform (stream : [< `x | abc] Stream.t) : [> abc] Stream.t =
>  let rec fold () =
>      match Stream.next stream with
> 	| `x -> Stream.slazy fold
> 	| #abc as x -> Stream.icons x (Stream.slazy fold)
>    with
> 	Stream.Failure -> Stream.sempty
>  in
>    fold ()
> 
> However, I fail to see how the same function could be implemented so it accepts an open polymorphic type as it's input.
> I.e. is there a way to express something like this (which is not valid OCaml)
> 
> let transform : [> `x | 'a] Stream.t -> [> 'a] Stream.t
> 
> Does this even make sense?

This makes sense, but would require a different type system.
With ocaml, you must keep to closed types.
This should be sufficient for concrete examples, but you cannot build
polymorphic combinators.

Note. In designing a type system where you would be able to express
such "differential" types, poinder the problem: what is the type of
transform composed with itself.

Jacques Garrigue


  reply	other threads:[~2010-03-23  6:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-23  2:53 Kaspar Rohrer
2010-03-23  6:11 ` Jacques Garrigue [this message]
2010-03-23 19:46   ` [Caml-list] " Kaspar Rohrer

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=20100323.151140.125875815.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=kaspar.rohrer@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;
as well as URLs for NNTP newsgroup(s).