caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Kaustuv Chaudhuri <kaustuv.chaudhuri@inria.fr>
To: caml-list <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] mutable and polymorphism
Date: Wed, 15 Sep 2010 22:44:39 +0200	[thread overview]
Message-ID: <AANLkTimVgNJEbbEzF8_5y4cp4umWYjCwUDup6ooqNf0X@mail.gmail.com> (raw)
In-Reply-To: <AANLkTimdJ7De5FhG=EsG=Tv83msUzMkV_AYhnCHjx=4x@mail.gmail.com>

On Wed, Sep 15, 2010 at 9:38 PM, Radu Grigore <radugrigore@gmail.com> wrote:
> In any case, I'm more interested in an explanation of what happens,

As you are probably aware, ML-style languages have a so-called "value
restriction" on polymorphism, which in its most vanilla form can be
stated simply as: only values can have a polymorphic type. The
expression

   let x = e in fun y -> ()                                       (1)

is not a value, meaning that it can reduce further, and therefore
the vanilla value restriction says that it cannot have a polymorphic
type.

Now, some very clever people have reasoned that in certain cases the
expression (1) is indistinguishable from a value and by Leibniz's
principle of equality of indistinguishables should therefore be
allowed to have the polymorphic type its equivalent value does.

As an example, one such situation is if e is built from only pure
constructors (i.e., constructors with no mutable fields) and values,
such as:

   let x = () in fun y -> ()

One might even say that (1) should be treated as a value when it
is equivalent to the value expression:

   fun y -> let x = e in ()

There are a number of scenarios in which OCaml can deduce that a
certain non-value expression can have a polymorphic type. For a
comprehensive list of such situations, you can read Jacques Garrigue's
paper on this topic, which also has a great section on the history of
the value restriction [1]. However, OCaml's implementation is
conservative -- it doesn't cover all cases where this fun/let
permutation doesn't change the denotation.

You can easily discover that one kind of expression that OCaml doesn't
allow for this "value-interpretation" of (1) is if e is a function
application. After all, this expression

   let x = infinite_loop () in fun y -> ()

and this:

   fun y -> let x = infinite_loop () in ()

are easily distinguished.

That your function is called "ref" instead of "infinite_loop" is
irrelevant. Mutation is a red herring. You would get the same result
for a completely pure expressions for e; for example:

   # let z = let x = fst (1, 2) in fun y -> () ;;
   val z : '_a -> unit = <fun>

-- Kaustuv

[1] http://caml.inria.fr/pub/papers/garrigue-value_restriction-fiwflp04.ps.gz


  reply	other threads:[~2010-09-15 20:44 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-15 17:10 Radu Grigore
2010-09-15 17:40 ` [Caml-list] " Andreas Rossberg
2010-09-15 17:59   ` Radu Grigore
2010-09-15 19:10     ` Goswin von Brederlow
2010-09-15 19:38       ` Radu Grigore
2010-09-15 20:44         ` Kaustuv Chaudhuri [this message]
2010-09-17  7:31         ` Goswin von Brederlow
2010-09-17 14:04 ` Radu Grigore
2010-09-17 14:08   ` Radu Grigore

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=AANLkTimVgNJEbbEzF8_5y4cp4umWYjCwUDup6ooqNf0X@mail.gmail.com \
    --to=kaustuv.chaudhuri@inria.fr \
    --cc=caml-list@yquem.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).