From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 30BADBC84 for ; Thu, 31 Mar 2005 04:43:17 +0200 (CEST) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j2V2hGl1015191 for ; Thu, 31 Mar 2005 04:43:16 +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 EAA20642 for ; Thu, 31 Mar 2005 04:43:16 +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 j2V2hEQF015181 for ; Thu, 31 Mar 2005 04:43:15 +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 j2V2h19u015351; Thu, 31 Mar 2005 11:43:02 +0900 (JST) Date: Thu, 31 Mar 2005 11:42:53 +0900 (JST) Message-Id: <20050331.114253.48852731.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: <891bd339050330165142478f37@mail.gmail.com> References: <891bd33905033014311636570a@mail.gmail.com> <20050331.093724.70528102.garrigue@math.nagoya-u.ac.jp> <891bd339050330165142478f37@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 nez-perce with ID 424B63C4.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at nez-perce with ID 424B63C2.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; 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 variants:01 abbreviation:01 ...:98 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 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