From: Leo White <lpw25@cam.ac.uk>
To: Jordan W <jordojw@gmail.com>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Row-polymorphic type declarations
Date: Tue, 10 Jun 2014 15:13:06 +0100 [thread overview]
Message-ID: <y2ar42wvonh.fsf@kingston.cl.cam.ac.uk> (raw)
In-Reply-To: <CAPOA5_7swJ1tQ+S63TAHE+hXxQp8uqiesHJAFxkcNvSxgjQ_6g@mail.gmail.com> (Jordan W.'s message of "Mon, 9 Jun 2014 11:50:22 -0700")
> 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.
You can never get your hands on the row variable itself, you can only
ever refer to polymorphic types containing row variables. For example,
in your definition of `twoDimensionalPoint` you capture the row variable
`..` by instead refering the the polymorphic type `<x: int; y: int; ..>`.
Similarly, you cannot instantiate the row variable directly, but instead
you must instantiate a polymorphic type containing the row variable.
For your example, I can think of a few ways to specify this.
1. Using a constraint on a `twoDimensionalPoint`:
type 'a threeDimensionalPoint = 'a twoDimensionalPoint
constraint 'a = <z: int; ..>
2. Using two constraints on the type parameter:
type 'a threeDimensionalPoint = 'a
constraint 'a = 'a twoDimensionalPoint
constraint 'a = <z: int; ..>;;
3. Since your example uses object types, class types provide an alternative
mechanism:
class type twoDimensionalPoint = object
method x: int
method y: int
end
class type threeDimensionalPoint = object
inherit twoDimensionalPoint
method z: int
end
which allows you to use the `#t` syntax to refer to the polymorphic
object types:
let foo (p : #twoDimensionalPoint) = p#x + p#y
let bar (p : #threeDimensionalPoint) = p#z + foo p
A similar thing can also be achieved for polymorphic variants:
type t = [`X of int]
type s = [t | `Y of int]
let foo (v : [> t]) =
match v with
| `X i -> i
| _ -> 0
let bar (v : [> s]) =
match v with
| `Y i -> i
| x -> foo x
> 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;;
The row polymorphism is acknowledged by the `..` which represents the
row variable.
> 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?
They are completely equivalent.
Regards,
Leo
next prev parent reply other threads:[~2014-06-10 14:13 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
2014-06-10 14:13 ` Leo White [this message]
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=y2ar42wvonh.fsf@kingston.cl.cam.ac.uk \
--to=lpw25@cam.ac.uk \
--cc=caml-list@inria.fr \
--cc=jordojw@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