caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Christophe Dehlinger" <christophedehlinger@gmail.com>
To: "Remi Vanicat" <remi.vanicat@gmail.com>
Cc: "O'Caml Mailing List" <caml-list@inria.fr>
Subject: Re: [Caml-list] Unexpected '_a problem
Date: Wed, 2 Aug 2006 09:03:47 +0200	[thread overview]
Message-ID: <f24432a00608020003h398c3111hf3fa27554256696e@mail.gmail.com> (raw)
In-Reply-To: <6b8a91420608011509s696233cew8d499f315aa0cba9@mail.gmail.com>

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

On 8/2/06, Remi Vanicat <remi.vanicat@gmail.com > wrote:
>
> 2006/8/1, Andreas Rossberg <AndreasRossberg@web.de>:
> > "Chris King" < colanderman@gmail.com> wrote:
> > >
> > > # let create_foo () = object method foo (_: 'a) = () end;;
> > > val create_foo : unit -> < foo : 'a -> unit > = <fun>
> > > # let a = create_foo ();;
> > > val a : < bar : '_a -> unit > = <obj>
> > >
> > > the compiler determines a is monomorphic, since 'a is in a
> > > contravariant position.  But why, when 'a is placed in a covariant
> > > position:
> >
> > This has nothing to do with contravariance, nor with subtyping or
> objects at
> > all. What you observe is the infamous "value restriction": roughly, a
> > definition can only be polymorphic when its right-hand side is
> syntactically
> > a value (i.e. a function, tuple, constant, etc or combination thereof).
> In
> > your case it's an application.


This is Wright's value restriction, that is used in SML (maybe in
Alice ML too ?). OCaml
uses Garrigue's relaxed value restriction, which does care about variance
(see http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/papers/morepoly-long.pdf<http://wwwfun.kurims.kyoto-u.ac.jp/%7Egarrigue/papers/morepoly-long.pdf>).
I haven't read that paper recently, but IIRC Garrigue's value restriction
allows generalisation of some of the type parameters that are in covariant
position.


Nope :
> # let f () = [];;
> val f : unit -> 'a list = <fun>
> # let x = f ();;
> val x : 'a list = []
>
> I have an application, but as it is covariant, the compiler lift the
> value restriction, and make it covariant. But for a reson I don't
> fully understood, one cannot do:
>
> # let f () (_ : 'a -> unit) = ();;
> val f : unit -> ('a -> unit) -> unit = <fun>
> # f ();;
> - : ('_a -> unit) -> unit = <fun>
>
> and have full polymorphism (when f() type is covariant).


IIRC, when binding to something that is not a value (like here, where the
toplevel output is bound to the application f()), type parameters that are
on the left of arrows are not generalized, regardless of variance; if the
type system allowed generalization of all covariant types it would lose its
principality property.

Note that
> this will work:
> # type +'a t = Foo of ((('a -> unit) -> unit));;
> type 'a t = Foo of (('a -> unit) -> unit)
> # let f () = Foo (fun _ -> ());;
> val f : unit -> 'a t = <fun>
> # f ();;
> - : 'a t = Foo <fun>
> (we have full polymorphisme here, with a code that is mostly
> equivalent to the first one, but not completely).


Maybe algebraic constructors are a special case of function application ?
After all, they can never "go wrong", can they ?

_______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>

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

  parent reply	other threads:[~2006-08-02  7:03 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-08-01 20:47 Chris King
2006-08-01 21:20 ` [Caml-list] " Andreas Rossberg
2006-08-01 22:09   ` Remi Vanicat
2006-08-02  7:00     ` Alain Frisch
2006-08-02 17:57       ` Chris King
2006-08-04  2:42         ` Jacques Garrigue
2006-08-04 14:18           ` Chris King
2006-08-02  7:03     ` Christophe Dehlinger [this message]
2006-08-02  8:07       ` Andreas Rossberg

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=f24432a00608020003h398c3111hf3fa27554256696e@mail.gmail.com \
    --to=christophedehlinger@gmail.com \
    --cc=caml-list@inria.fr \
    --cc=remi.vanicat@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).