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=none autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr 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 B4C24BB83 for ; Fri, 1 Sep 2006 19:31:09 +0200 (CEST) Received: from orion.metastack.com (no-dns-yet.demon.co.uk [80.177.38.218] (may be forged)) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id k81HV4ip028417 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL) for ; Fri, 1 Sep 2006 19:31:09 +0200 Received: from treble (cpc2-cmbg6-0-0-cust535.cmbg.cable.ntl.com [81.107.34.24]) (authenticated bits=0) by orion.metastack.com (8.13.4/8.13.3) with ESMTP id k81H9S7e011008 (version=TLSv1/SSLv3 cipher=RC4-MD5 bits=128 verify=NO) for ; Fri, 1 Sep 2006 18:09:29 +0100 From: "David Allsopp" To: "OCaml List" Subject: Polymorphic variants question Date: Fri, 1 Sep 2006 18:31:00 +0100 Organization: MetaStack Solutions Ltd. Message-ID: <012901c6cdec$64edf490$6a7ba8c0@treble> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Mailer: Microsoft Office Outlook 11 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2962 Thread-Index: AcbN7GRcC2fUut22QXWzfJcxr4id2g== X-Miltered: at concorde with ID 44F86E58.001 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; variants:01 variants:01 constructors:01 val:01 bool:01 bool:01 lexers:01 high-level:01 lexers:01 subset:01 lexer:01 type-checker:01 polymorphic:01 polymorphic:01 let:03 Forgive the potentially obvious question --- I'm not very familiar with polymorphic variants but I think that they're what I want in this situation! Suppose I'm dealing with three constructors `A, `B and `C and I have a function f that's supposed to take either `A or `C and return any of `A, `B or `C. If I write: let f x = if x = `A then (true, `B) else (false, x) then I get the type val f : ([> `A | `B] as 'a) -> bool * 'a Now, if I try to constrain it to what I'm after with let (f : [`A | `C] -> bool * [`A | `B | `C]) = fun x -> ... then I get a type error unless I change (false, x) to (false, id x) with let id = function `A -> `A | `C -> `C Is there a better way of writing this? I'm using this in the context of several interrelated lexers where `A, `B and `C are high-level states and certain lexers can only be called in a subset of those states but each lexer may yield any value for the next-state. I'd quite like to eliminate the id x bit since it's only there to "separate" x from the return value for the type-checker. Thanks! David