From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by yquem.inria.fr (Postfix) with ESMTP id 968FFBC84 for ; Thu, 31 Mar 2005 02:37:46 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j2V0bkB8032701 for ; Thu, 31 Mar 2005 02:37:46 +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 CAA16990 for ; Thu, 31 Mar 2005 02:37:45 +0200 (MET DST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j2V0bhqq005001 for ; Thu, 31 Mar 2005 02:37:44 +0200 Received: from localhost (suiren [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.13.1/8.13.1) with ESMTP id j2V0bXEZ011967; Thu, 31 Mar 2005 09:37:33 +0900 (JST) Date: Thu, 31 Mar 2005 09:37:24 +0900 (JST) Message-Id: <20050331.093724.70528102.garrigue@math.nagoya-u.ac.jp> To: yminsky@cs.cornell.edu, yminsky@gmail.com Cc: caml-list@inria.fr Subject: Re: [Caml-list] When is a function polymorphic? From: Jacques Garrigue In-Reply-To: <891bd33905033014311636570a@mail.gmail.com> References: <891bd33905033014311636570a@mail.gmail.com> X-Mailer: Mew version 4.2 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at concorde with ID 424B465A.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 424B4657.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 yaron:01 minsky:01 yminsky:01 typechecker:01 unifying:01 pat:01 polymorphic:01 idiom:01 typing:01 typing:01 jacques:01 jacques:01 argument:01 patterns:02 X-Spam-Checker-Version: SpamAssassin 3.0.2 (2004-11-16) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.2 X-Spam-Level: From: Yaron Minsky > I don't understand the following results. It seems like these two > examples should have the same type. In this example, there isn't much > of a difference between the two cases, but there are cases where this > idiom is quite convenient. Any idea how to salvage it? > > # function Some x -> Some () | None as x -> x;; > - : 'a option -> unit option = > # function Some x -> Some () | x -> x;; > - : unit option -> unit option = The typechecker does something special about "as x" patterns. Namely, rather than unifying the type of x with the type of the whole input, it types the aliased pattern twice, and only unifies type parameters which appear in the pattern. So this means that # function Some _ as x -> x | None -> Some () ;; - : unit option -> unit option = since the type parameter appears in the argument to Some, while # function Some x -> Some () | None as x -> x;; - : 'a option -> unit option = since it doesn't appear in None. This clever typing only happens with " as " patterns, so your second example gets the standard result (it is assumed that x could be anything, including Some, as the typing does not use the result of the exhaustiveness analysis.) Jacques Garrigue