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 685DEBC84 for ; Thu, 31 Mar 2005 06:04:29 +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 j2V44S3G019729 for ; Thu, 31 Mar 2005 06:04:28 +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 GAA22154 for ; Thu, 31 Mar 2005 06:04:28 +0200 (MET DST) Received: from wproxy.gmail.com (wproxy.gmail.com [64.233.184.195]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j2V44RTl021659 for ; Thu, 31 Mar 2005 06:04:27 +0200 Received: by wproxy.gmail.com with SMTP id 69so407329wri for ; Wed, 30 Mar 2005 20:04:27 -0800 (PST) DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:reply-to:to:subject:cc:in-reply-to:mime-version:content-type:content-transfer-encoding:references; b=f5qNH0fZaPuhq8LtNENj+mrVr5tYDMMhLV4tcoq7lYNlE/VEAHsr4Jp9K2/C6jS6xrK5s/rsXcnTEu525GhrlvvGO3XFGDdduf8Qg3r6Frh44jcgtcHHQF2g+E1Xl/dYdvDTmONWKV3BsP/Pas0rOknI8hOJ0Sg4146lUDlN6Jk= Received: by 10.54.21.76 with SMTP id 76mr241819wru; Wed, 30 Mar 2005 20:04:27 -0800 (PST) Received: by 10.54.2.79 with HTTP; Wed, 30 Mar 2005 20:04:27 -0800 (PST) Message-ID: <891bd33905033020045cad3ce2@mail.gmail.com> Date: Wed, 30 Mar 2005 23:04:27 -0500 From: Yaron Minsky Reply-To: yminsky@cs.cornell.edu To: Jacques Garrigue Subject: Re: [Caml-list] When is a function polymorphic? Cc: caml-list@inria.fr In-Reply-To: <20050331.114253.48852731.garrigue@math.nagoya-u.ac.jp> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit References: <891bd33905033014311636570a@mail.gmail.com> <20050331.093724.70528102.garrigue@math.nagoya-u.ac.jp> <891bd339050330165142478f37@mail.gmail.com> <20050331.114253.48852731.garrigue@math.nagoya-u.ac.jp> X-Miltered: at concorde with ID 424B76CC.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 424B76CB.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; yaron:01 minsky:01 yminsky:01 caml-list:01 yaron:01 minsky:01 yminsky:01 notation:01 foo:01 converts:01 foo:01 notation:01 checker:01 compiler:01 wheter:01 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=RCVD_BY_IP autolearn=disabled version=3.0.2 X-Spam-Level: Interesting. I guess this is best understood as a limitation of the type-checking algorithm. Does anyone know if there are any plans to remove this limitation? Are there fundamental reasons why it would be difficult to do so? Yaron On Thu, 31 Mar 2005 11:42:53 +0900 (JST), Jacques Garrigue wrote: > From: Yaron Minsky > > > I think I screwed up the original examples a bit. I think the effect > > I'm looking at doesn't depend on the "as" notation in particular. > > Here's another example that doesn't use "as": > > > > # function Some x -> Some () | None -> None;; > > - : 'a option -> unit option = > > # function Some x -> Some () | x -> x;; > > - : unit option -> unit option = > > > > The reason I want this is for the following example. Consider some > > complicated union type with a single parameter: > > > > type 'a foo = A of 'a | B of int | C of string * string | ... | ZZ of float > > > > I want a function that converts an 'a foo to a unit foo. I tried to > > write it this way: > > > > function A _ -> A () | x -> x > > > > But this ends up having type: unit foo -> unit foo, which isn't what I > > want at all. Any idea of how to achieve this cleanly? > > The effect you are looking for does depend on the "as" notation. > Here is how to write it: > > function A _ -> A () | (B _ | C _ | ... | ZZ _) as x -> x > > Note that the exhaustivity checker will correctly detect that you > second case is complete, and the pattern-matching compiler will just > branch on wheter the tag is A or not. I.e. no code will be produced to > check the second pattern. > > As others pointed, polymorphic variants allow you to write it in a > shorter way, because you can define a type abbreviation for the > remaining cases, but in the end it uses the same "as" mechanism. > > Jacques Garrigue >