From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id C743EBC0B for ; Wed, 17 Jan 2007 07:44:20 +0100 (CET) Received: from ipmail01.adl2.internode.on.net (ipmail01.adl2.internode.on.net [203.16.214.140]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l0H6iIQp017273 for ; Wed, 17 Jan 2007 07:44:19 +0100 Received: from ppp27-234.lns1.syd6.internode.on.net (HELO rosella) ([59.167.27.234]) by ipmail01.adl2.internode.on.net with ESMTP; 17 Jan 2007 16:39:08 +1030 X-IronPort-AV: i="4.13,198,1167571800"; d="scan'208"; a="74981624:sNHT18436157446" Subject: Re: [Caml-list] Polymorphic Variants From: skaller To: Jacques GARRIGUE Cc: tom.primozic@gmail.com, caml-list@yquem.inria.fr In-Reply-To: <20070117.111927.2004173151.garrigue@math.nagoya-u.ac.jp> References: <20070117.111927.2004173151.garrigue@math.nagoya-u.ac.jp> Content-Type: text/plain Date: Wed, 17 Jan 2007 17:09:01 +1100 Message-Id: <1169014142.8941.111.camel@rosella.wigram> Mime-Version: 1.0 X-Mailer: Evolution 2.6.1 Content-Transfer-Encoding: 7bit X-Miltered: at discorde with ID 45ADC5C2.001 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; variants:01 annotations:01 explicitely:01 annotations:01 compiler:01 variants:01 subset:01 failwith:01 afaik:01 failwith:01 recursive:01 compiler:01 nodes:01 sourceforge:01 polymorphic:01 On Wed, 2007-01-17 at 11:19 +0900, Jacques GARRIGUE wrote: > If you mean that, without type annotations, types and errors become > very hard to read, this is true, but the manual explicitely encourages > to add type annotations :-) You are not kidding .. and even type annotations don't always help. I regularly get 100-200 line errors, even in cases where the compiler clearly knows which tag is causing the problem. But I would not give up the polymorphic variants despite this pain, because the alternative is now unthinkable. Basically, with ordinary variants if you have some subset of cases you can handle it easily but verbosely with morphisms: type X = | A | B | C | D type Y = | AA | BB let XtoY x : Y = match x with | A -> AA | B -> BB | _ -> failwith "blah" let YtoX y : X = match y with | AA -> A | BB -> B With polymorphic variants these morphisms are created automatically, and AFAIK the embedding (YtoX) has zero cost. You can also do: type X = [`A | `B | `C | `D] type Y = [`A | `B] let XtoY x: Y = match x with | #Y as y -> (y:X:>Y) | _ -> failwith "khg" let YtoX y: X = (y:Y:>X) This is mainly "syntactic sugar". The recursive case is where polymorphic variants really shine, eg a compiler "expression" type with various "sub expression" types where there are constraints satisfied recursively by all nodes. (These things are still far too hard to define with multiple parameters though .. ;( -- John Skaller Felix, successor to C++: http://felix.sf.net