From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id 20E7ABB84 for ; Wed, 2 Aug 2006 09:03:49 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.6/8.13.6) with ESMTP id k7273mpJ021676 for ; Wed, 2 Aug 2006 09:03:48 +0200 Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id JAA07746 for ; Wed, 2 Aug 2006 09:03:48 +0200 (MET DST) Received: from ug-out-1314.google.com (ug-out-1314.google.com [66.249.92.172]) by nez-perce.inria.fr (8.13.6/8.13.6) with ESMTP id k7273lJu021671 for ; Wed, 2 Aug 2006 09:03:47 +0200 Received: by ug-out-1314.google.com with SMTP id c2so170968ugf for ; Wed, 02 Aug 2006 00:03:47 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:to:subject:cc:in-reply-to:mime-version:content-type:references; b=GHxJIqyLXAUcRzcdAds7+syPiHlugvhlEeXAlLLMkiWcdtJ3/GyCiL30AZKAsFwFj5gDZp5Vx/hA1E+ey8WxXUh0KxvTdipaXv0GdX9oJSt3AbajLwNypz26GQw9uwN7dl4FJ3Bi9OVwKcbnKCt74nEgI/Ygicd44VHgl0bQ+gY= Received: by 10.66.244.10 with SMTP id r10mr646550ugh; Wed, 02 Aug 2006 00:03:47 -0700 (PDT) Received: by 10.66.242.17 with HTTP; Wed, 2 Aug 2006 00:03:47 -0700 (PDT) Message-ID: Date: Wed, 2 Aug 2006 09:03:47 +0200 From: "Christophe Dehlinger" To: "Remi Vanicat" Subject: Re: [Caml-list] Unexpected '_a problem Cc: "O'Caml Mailing List" In-Reply-To: <6b8a91420608011509s696233cew8d499f315aa0cba9@mail.gmail.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_80107_24367220.1154502227217" References: <875c7e070608011347y62d2dd21lc55390cd4ffd00a1@mail.gmail.com> <004801c6b5b0$47811030$15b2a8c0@wiko> <6b8a91420608011509s696233cew8d499f315aa0cba9@mail.gmail.com> X-j-chkmail-Score: MSGID : 44D04E54.000 on nez-perce : j-chkmail score : X : 0/20 1 X-j-chkmail-Score: MSGID : 44D04E53.000 on nez-perce : j-chkmail score : XX : 0/20 2 X-Miltered: at nez-perce with ID 44D04E54.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 44D04E53.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; christophe:01 rossberg:01 foo:01 foo:01 val:01 val:01 compiler:01 subtyping:01 sml:01 ocaml:01 variance:01 wwwfun:01 wwwfun:01 iirc:01 compiler:01 X-Spam-Checker-Version: SpamAssassin 3.0.3 (2005-04-27) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=HTML_40_50,HTML_MESSAGE, RCVD_BY_IP autolearn=disabled version=3.0.3 ------=_Part_80107_24367220.1154502227217 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 8/2/06, Remi Vanicat wrote: > > 2006/8/1, Andreas Rossberg : > > "Chris King" < colanderman@gmail.com> wrote: > > > > > > # let create_foo () = object method foo (_: 'a) = () end;; > > > val create_foo : unit -> < foo : 'a -> unit > = > > > # let a = create_foo ();; > > > val a : < bar : '_a -> unit > = > > > > > > 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). 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 = > # 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 = > # f ();; > - : ('_a -> unit) -> unit = > > 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 = > # f ();; > - : 'a t = Foo > (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 > ------=_Part_80107_24367220.1154502227217 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline

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 ). 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

------=_Part_80107_24367220.1154502227217--