caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jean Saint-Remy <jeansaintremy@yahoo.com>
To: "caml-list@inria.fr" <caml-list@inria.fr>
Subject: [Caml-list] explicit polymorphic types in record fields
Date: Mon, 16 Feb 2015 00:06:32 +0000 (UTC)	[thread overview]
Message-ID: <419599633.6386715.1424045192831.JavaMail.yahoo@mail.yahoo.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2484 bytes --]

Hi,
I was reading the ocaml manual section 1.5 where an explicit polymorphic type can be declared.  I wanted to know if the trailing dot '.' is a special case "marker" only and does not have anything to do with how we distinguish floating point multiplication, division, subtraction, and addition, and nothing to do with accessing the elements of an array. Without the trailing dot we cannot declare any such polymorphic fields no how, no way. I was also surprised to find this was possible.

[The manual says] In some special cases, you may need to store a polymorphic function ina data structure, keeping its polymorphism. Without user-providedtype annotations, this is not allowed, as polymorphism is onlyintroduced on a global level. However, you can give explicitlypolymorphic types to record fields.# type idref = { mutable id: 'a. 'a -> 'a };;
type idref = { mutable id : 'a. 'a -> 'a; }

# let r = {id = fun x -> x};;
val r : idref = {id = <fun>}

# let g s = (s.id 1, s.id true);;
val g : idref -> int * bool = <fun>

# r.id <- (fun x -> print_string "called id\n"; x);;
- : unit = ()

# g r;;
called id
called id
- : int * bool = (1, true)
I am having difficulty reading the first line, the type declaration "type idref = { mutable id : 'a. 'a -> 'a } ;;"  When I look at the way we are accessing the field values on line 3, it does look like accessing member fields in other languages and it does superficially resemble array indexing as in "let str = "abcdef" ;; str.[1] ;; -: char = 'b'".  The two ways we are using the dot '.' are very distinct. Is that correct? The second is the common place indexing, whereas the first is just a "marker".  I could not declare the fields for example as a polymorphic tuple for instance: "type idref = { mutable 'a * 'b -> 'a }" nor "type idref = { mutable 'a. 'b -> 'a }". The latter is just nonsense, the 'b is undefined. But we could have a two field record by just repeating the type definitions. "let idref = { mutable 'a. 'a -> 'a; mutable 'b. 'b -> 'b }". The polymorphic type does not have any restriction, it appears we could have any fundamental type we wanted. The type 'a variable must be repeated, it is not enough to say "type idref = { mutable 'a. -> 'a };;" Is it correct then to read the declaration in this way: the type 'a polymorphic type as a polymorphic variable of type 'a. I know it sounds redundant, but that is how I am reading the syntax.
Thank you for clarifying this for me.
jean

[-- Attachment #2: Type: text/html, Size: 4090 bytes --]

             reply	other threads:[~2015-02-16  0:09 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-16  0:06 Jean Saint-Remy [this message]
2015-02-16 13:14 ` Gerd Stolpmann
2015-02-16 13:15 ` Gerd Stolpmann
2015-02-16 13:39 ` Ben Millwood

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=419599633.6386715.1424045192831.JavaMail.yahoo@mail.yahoo.com \
    --to=jeansaintremy@yahoo.com \
    --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).