caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Goswin von Brederlow <goswin-v-b@web.de>
To: Radu Grigore <radugrigore@gmail.com>
Cc: Andreas Rossberg <rossberg@mpi-sws.org>, caml-list@yquem.inria.fr
Subject: Re: [Caml-list] mutable and polymorphism
Date: Wed, 15 Sep 2010 21:10:06 +0200	[thread overview]
Message-ID: <878w32g6wh.fsf@frosties.localdomain> (raw)
In-Reply-To: <AANLkTi=-iuKEJeJbfyDJLbCzUCVF9i-0M8Ev8ohvm=w+@mail.gmail.com> (Radu Grigore's message of "Wed, 15 Sep 2010 18:59:01 +0100")

Radu Grigore <radugrigore@gmail.com> writes:

> On Wed, Sep 15, 2010 at 6:40 PM, Andreas Rossberg <rossberg@mpi-sws.org> wrote:
>> Have a look at this variant of c.ml and its "potential" if it type-checked:
>>>  let f = let xs = ref [] in fun x -> xs := x :: !xs in f 1; f 'a'
>
> Interesting. If I would be a type checker, here's what I'd do.
> 1. "ref []" tells me that (xs : '_a), because I know refs can't be polymorphic.
> 2. "x :: !xs" tells me that (xs : '_a list) and (x : '_a)
> 3. "x -> x :: !xs" tells me that (f : '_a -> '_a list)
> 4. "f 1" tells me that (f : int -> int list)
> 5. "f 'a'" tells me... OOPS
>
> Moreover, I'd have similar thoughts about a variant of a.ml (and a.ml
> does typecheck!)
>   let xs = ref [] in let f = fun x -> xs:=x::!xs in f 1; f 'a'

In your original a.ml you didn't use the reference inside f so f was
polymorphic. That is was then further below the scope of a reference
didn't matter since the application of f was too.

> On the other hand, here's what I'd do for the original c.ml, which
> with a few names added is
>   let f = let x = ref () in fun y -> () in f 1; f 'a'
> 1. "ref ()" tells me that (x : unit ref)
> 2. "y -> ()" tells me that (f : 'a -> unit)

2. "y -> ()" tells me that (f : '_a -> unit)

Because the compiler is stupid and things with a ref can't
polymorphic. It isn't smart enough to see that the type of the ref is
independent of the type of the function and there could remain
polymorphic.

> 3. "f 1" returns ()
> 4. "f 'a'" return ()
>
> In other words, I would have thought that in your example the problem
> is that you tried to use a polymorphic reference. (And this problem
> even appears in the FAQ.)

The solution is called lifting I think?

Anyway, you need to move the polymorphic argument outside the scope of
the reference:

let f y = let x = ref () in (fun y -> ()) y in f 1; f 'a'

> regards,
>   radu

MfG
        Goswin


  reply	other threads:[~2010-09-15 19:10 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 [this message]
2010-09-15 19:38       ` Radu Grigore
2010-09-15 20:44         ` Kaustuv Chaudhuri
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=878w32g6wh.fsf@frosties.localdomain \
    --to=goswin-v-b@web.de \
    --cc=caml-list@yquem.inria.fr \
    --cc=radugrigore@gmail.com \
    --cc=rossberg@mpi-sws.org \
    /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).