caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Row-polymorphic type declarations
Date: Tue, 10 Jun 2014 11:25:23 +0200	[thread overview]
Message-ID: <20140610092523.GB31598@frosties> (raw)
In-Reply-To: <CAPOA5_7swJ1tQ+S63TAHE+hXxQp8uqiesHJAFxkcNvSxgjQ_6g@mail.gmail.com>

On Mon, Jun 09, 2014 at 11:50:22AM -0700, Jordan W wrote:
> I'd like to hear the answers to some questions asked on Twitter, that Anil
> began replying to:
> 
> The original question was: Does anyone know why OCaml doesn't allow
> defining types with polymorphic row variables: <red: int; ..> or [> `Red ]
> but will infer them?
> 
> Anil's answer was to use:
> 
>     type 'a t constraint 'a = [> `Red ]
>     type 'a t constraint 'a = <x:int; ..>
> 
> Here are the follow up questions that could still use some clarification:
> 
> 1. Does anyone know how can we later instantiate the polymorphic row
> variables ".." with particular rows (both for obj methods and variants).
> Any good docs on this? How do we assert that another type/expression is
> equivalent to 'a t with 'a being a particular set of rows.
> 
> Could someone give an example with a 2D point type
> 
> type 'a twoDimensionalPoint constraint 'a = <x:int; y:int; ..>;;
> 
> How would we create another type threeDimensionalPoint that we assert is a
> two dimensional point with the polymorphic row variables being set to
> `z:int`? I understand type inference/checking automates most of this, but I
> often like to explicitly write type definitions to verify my own
> understanding of the program/inference.

I think you can't assert that in the definition of the
threeDimensionalPoint type. But you can add a dummy function that
takes a threeDimensionalPoint and returns it as a twoDimensionalPoint.
If the types can't be unified then the function will give an error.
 
> 3. Also curious why annotations in arguments don't require acknowledgment
> of row polymorphism
> 
>   let f (o:<x:int; y:int; ..>) = o#x + o#y;;

Not sure what you ask there. You are annotating a type. period.
 
> 4. It appears there are two ways to write the row-polymorphic type
> annotation:
> type 'a t = ([> `Red ] as 'a) and type 'a t constraint 'a = [> `Red ]. Why
> are there two ways and what are the advantages?

I'm not sure if both are identical and in what cases you can only use
the one form or the other. But I think only the constraint syntax can
be used when declaring the type of functions.

MfG
	Goswin

  reply	other threads:[~2014-06-10  9:25 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-06-09 18:50 Jordan W
2014-06-10  9:25 ` Goswin von Brederlow [this message]
2014-06-10 14:13 ` Leo White
2014-06-11  2:20 ` Jacques Garrigue

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=20140610092523.GB31598@frosties \
    --to=goswin-v-b@web.de \
    --cc=caml-list@inria.fr \
    /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).