caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Gerd Stolpmann <info@gerd-stolpmann.de>
To: Arnaud Spiwack <aspiwack@lix.polytechnique.fr>
Cc: Gabriel Scherer <gabriel.scherer@gmail.com>,
	Gregory Malecha <gmalecha@gmail.com>,
	Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>,
	 Mailing List OCaml <caml-list@inria.fr>
Subject: Re: [Caml-list] "Type constructor b would escape its scope"
Date: Mon, 28 Mar 2016 23:00:31 +0200	[thread overview]
Message-ID: <1459198831.4777.28.camel@e130.lan.sumadev.de> (raw)
In-Reply-To: <CAMoPVjePETiJ-9ks=Lq+3U6hWAuqUc_=0MqHS6XQqfVD7xfa2g@mail.gmail.com>

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

Am Montag, den 28.03.2016, 21:20 +0200 schrieb Arnaud Spiwack:
> 
> On 28 March 2016 at 10:07, Gabriel Scherer <gabriel.scherer@gmail.com>
> wrote:
>         (* the difference by "(type a) ..." and "type a . ..." is that
>         the latter
>            allows polymorphic recursion (just as "'a . ..."), so this
>         example fails
>            with just (type a) *)
>         
>  
> 
> So here's a question which has been nagging me for a while: is there
> any occasion where one may prefer to use the `(type a)` or the `'a.`
> forms over the `type a.` (apart for syntactical reasons)? If there is
> I'd be really interested in seeing an example, for I can't come up
> with one (especially for `(type a)`). If there aren't, what are the
> obstacles to turn the `(type a)` syntax into a synonymous to the `type
> a.` syntax? (I'm guessing that the `'a.` variant would be
> significantly harder).

Originally, I thought (type a) is just like plain 'a, only that the
variable appears inside the function as abstract type. At least the
manual suggests that. But looking closer, there are restrictions, and
these make (type a) unattractive. E.g.

# let f (x:'a) : 'a = match x with (p,q) -> (p,q);;       
val f : 'a * 'b -> 'a * 'b = <fun>

but

# let f (type a) (x:a) : a = match x with (p,q) -> (p,q);;
Error: This pattern matches values of type 'a * 'b
       but a pattern was expected which matches values of type a

so you cannot push any constraint to it, as if it was all-quantified.
Given that, the question is really whether there is any application left
that could not also be expressed by "type a.". But maybe I just don't
know? What about regular recursions? Tried to construct something, and I
was hoping that "type a." would force me to add hinting, but to no
avail:

# type 'a mylist = Nil | Cons of 'a * 'a mylist;;
# let rec last (type t) l = match l with Nil -> raise Not_found |
Cons(x,Nil) -> x | Cons(x,l') -> last l';;
val last : 'a mylist -> 'a = <fun>
# let rec last : type t . t mylist -> t = fun l -> match l with Nil ->
raise Not_found | Cons(x,Nil) -> x | Cons(x,l') -> last l';;
val last : 'a mylist -> 'a = <fun>

(Not that this is a good example at all, because it doesn't exploit the
type name t anywhere.)

Gerd

-- 
------------------------------------------------------------
Gerd Stolpmann, Darmstadt, Germany    gerd@gerd-stolpmann.de
My OCaml site:          http://www.camlcity.org
Contact details:        http://www.camlcity.org/contact.html
Company homepage:       http://www.gerd-stolpmann.de
------------------------------------------------------------


[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

      parent reply	other threads:[~2016-03-28 21:00 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-27  6:19 Gregory Malecha
2016-03-27  7:04 ` Leo White
2016-03-27 18:52   ` Gregory Malecha
2016-03-28  1:12     ` Jacques Garrigue
2016-03-28  4:20       ` Gregory Malecha
2016-03-28  8:07         ` Gabriel Scherer
2016-03-28 15:35           ` Gregory Malecha
2016-03-28 19:20           ` Arnaud Spiwack
2016-03-28 20:51             ` Leo White
2016-03-28 21:00             ` Gerd Stolpmann [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=1459198831.4777.28.camel@e130.lan.sumadev.de \
    --to=info@gerd-stolpmann.de \
    --cc=aspiwack@lix.polytechnique.fr \
    --cc=caml-list@inria.fr \
    --cc=gabriel.scherer@gmail.com \
    --cc=garrigue@math.nagoya-u.ac.jp \
    --cc=gmalecha@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).