caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: n8gray@gmail.com
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Labels and polymorphism
Date: Mon, 19 Mar 2007 10:15:00 +0900 (JST)	[thread overview]
Message-ID: <20070319.101500.03115026.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <aee06c9e0703081542o36715b5brfe55a291b56f84c6@mail.gmail.com>

From: "Nathaniel Gray" <n8gray@gmail.com>
[...]
> You could do it by using the rule that applying an unlabeled argument
> in a labeled context always binds to that label, but applying a
> labeled argument in an unlabeled context (or one where the labels
> don't match) defers the application until the label is found.  (Maybe
> this is what you're hinting at?)  So if you define f like this:
>   let f a b ~c x ~y ~z = z
> Then you could apply it in one of these ways:
>   f 1 2 3 4 5 6
>   f 1 2 3 4 ~z:6 ~y:5
>   f ~c:3 ~z:6 1 2 4 5
> But not this way:
>   f 1 2 4 ~c:3 5 6  (* Too late -- c is already bound to 4 *)
>   f 1 2 ~y:5 4 ~c:3 6  (* Ditto *)
> 
> As a practical matter you would probably want to label a suffix of
> your arguments for maximum flexibility.  You would lose the ability to
> do *some* (dare I say unimportant?) partial applications, but this
> approach seems much more intuitive to me.  Mainly it has the advantage
> that adding labels to a function would never break existing calls to
> that function.  This property seems highly desirable to me -- so
> desirable that I'm downright astonished it isn't already true.

Sorry for the very late answer...

What about your idea. I could like it, but I see two problems.
The first one is that (as you point yourself) it requires that labeled
arguments come after unlabeled ones. If you look at the labelings in
ListLabels, you will see that generally the opposite is true: labeled
arguments come first. For instance, with your proposal it would become
impossible to write "ListLabels.map l ~f:(fun .........)", which is
one of the nice uses of labels.
There is a good reason for labeled arguments to come first: they are
the ones who provide useful partial applications. And it is generally
more efficient to do partial applications in the right order.
(Actually, one could argue that in a new language, we could choose a
different approach. But OCaml existed before labels, so this cannot be
easily changed in an existing language.)
The second problem is more fundamental: adding labels to a function
cannot be done without breaking compatibility. This is because
labels must match exactly when you pass a function as argument to
another function. So, while we want to make the use of labels not too
intrusive, it is impossible to make them completely transparent. This
reduces the relative value of a small increment in non-intrusiveness,
as it cannot be perfect anyway.

We could of course thing of other approaches, which would allow
out-of-order applications while being close to your proposal.
But first, a small word on why it is more complicated than it seems at
first sight. The difficulty is that we want the semantics not to
depend on types. That is, we should define a semantics for application
which works without looking at the type of the function applied. This
may seems contradictory, as the type must be known for compilation,
but this is a highly desirable property in a language where most types
are inferred. This may be annoying to have the compiler tell you that
some application is not allowed while it is semantically OK, but this
would be terrible if it accepted it, but resulted in an undefined
behaviour, depending on an hidden behaviour of type inference.

Finally, the rationale behind the current strategy is:
* keep full compatibility with a semantics where all labels would have
  to be always written. This semantics was used in early versions of
  ocaml, and is well understood.
* make it less intrusive by allowing ommiting labels in most
  applications (as long as this can be made compatible with the above
  semantics.)
This explains why this is not _all_ applications.
There could be other choices. This one privileges continuity. I don't
believe there is an ideal solution, as desired properties vary from
one person to another.

Jacques Garrigue


  reply	other threads:[~2007-03-19  1:15 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-08 18:31 Nathaniel Gray
2007-03-08 19:14 ` [Caml-list] " Eric Cooper
2007-03-08 19:40 ` Roland Zumkeller
2007-03-08 23:42   ` Nathaniel Gray
2007-03-19  1:15     ` Jacques Garrigue [this message]
2007-03-19 23:53       ` Nathaniel Gray
2007-03-20  0:51         ` Jacques Garrigue
2007-03-08 23:30 ` skaller

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=20070319.101500.03115026.garrigue@math.nagoya-u.ac.jp \
    --to=garrigue@math.nagoya-u.ac.jp \
    --cc=caml-list@inria.fr \
    --cc=n8gray@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).