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 mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by yquem.inria.fr (Postfix) with ESMTP id 56F62BBC1 for ; Sun, 16 Mar 2008 05:15:11 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ao0BACo73EfRVYT5kmdsb2JhbACQfwEBAQEHBAQJChaQD4VT X-IronPort-AV: E=Sophos;i="4.25,507,1199660400"; d="scan'208";a="23821627" Received: from an-out-0708.google.com ([209.85.132.249]) by mail4-smtp-sop.national.inria.fr with ESMTP; 16 Mar 2008 05:15:10 +0100 Received: by an-out-0708.google.com with SMTP id b15so1444556ana.102 for ; Sat, 15 Mar 2008 21:15:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from:sender:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition:x-google-sender-auth; bh=2pGYJKrpxeZD/ik4fhKf2tacbuNjtErSavzoP7EzGiY=; b=G86yZhUUqMZAPZFn03A0JirA0G6u3Hfbcph5nUrrX/+7GsnUO/NcJApAjXhPGZpr7ZXieV95FEn9Sn0kiE78fhHY9Ozo/oeCk0nc5CfylISQozfQXUlNhr2ktz+8NxqD76hF9ud10WP3ywt43ZQLwpkK7kHj1QjltAyKL0ERZCc= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:sender:to:subject:mime-version:content-type:content-transfer-encoding:content-disposition:x-google-sender-auth; b=kG1XZgBw5vNZu+lLbDORy7w7UHlLT3Rq0RzLBaPeCsXAikteQgtjuivzKuQc8X4xzhfbcnDTQOJ3H7GNHfr+z3L8+vpgLXIyUMsOVLRtOxhtR8VZj+3tVsxfOdO+7fFgmyOFe2zUr3Ck/Zd1w48a1wey68qzmxH56S67Asf9COM= Received: by 10.100.171.10 with SMTP id t10mr28058296ane.104.1205640909784; Sat, 15 Mar 2008 21:15:09 -0700 (PDT) Received: by 10.100.46.2 with HTTP; Sat, 15 Mar 2008 21:15:09 -0700 (PDT) Message-ID: <1ef034530803152115n27d005d4kf7a136309bc90f9d@mail.gmail.com> Date: Sat, 15 Mar 2008 21:15:09 -0700 From: "Erick Tryzelaar" Sender: erick.tryzelaar@gmail.com To: "OCaml List" Subject: help with open vs closed polymorphic variants MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline X-Google-Sender-Auth: ecdc81abe29e1926 X-Spam: no; 0.00; variants:01 variants:01 foo:01 foo:01 baz:01 baz:01 subset:01 sourceforge:01 polymorphic:01 polymorphic:01 variant:02 variant:02 expression:02 subsets:02 tree:02 I'm not sure if this is the right term for describing polymorphic variants, but it seems to me that there are two ways to work with a complex hierarchy of polymorphic types to get something that's similar to inheritance. First is to use what I call closed variants, where you build the tree bottom up like this: type foo2 = [`Foo2];; type foo = [foo2 | `Foo];; type bar = [`Bar];; type baz = [`Baz];; type value = [foo|bar|baz];; The open variants are the other way around: type value = [`value];; type foo = [value|`Foo];; type bar = [value|`Bar];; type baz = [value|`Baz];; type foo2 = [foo|`Foo2];; These both let you call a function on a subset of variants. For closed variants, you can do: type 'a t = T of 'a;; let f (x:[< foo] t) = ();; f (T (`Foo :> foo));; f (T (`Foo2 :> foo2));; but this is a type error: "f (T (`Bar :> bar))". Likewise with open variants: type 'a t = T of 'a;; let f (x:[> `Foo] t) = ();; f (T (`Foo :> foo));; f (T (`Foo2 :> foo2));; With "f (T (`Bar :> bar))" being an error as well. However, say we wanted to allow for a function that works on two subsets of variant tree. I can do this with closed variants: let f (x:[< foo|bar] t) = ();; f (T (`Foo :> foo));; f (T (`Foo2 :> foo2));; f (T (`Bar :> bar));; But now "f (T (`Baz :> baz))" is a type error. However, I can't figure out if there's an equivalent with open variants. The naive solution doesn't work: # let f (x:[< `Foo|`Bar] t) = ();; # f (make_foo ());; This expression has type foo t but is here used with type ([< `Foo ] as 'a) t Type foo = [ `Foo | `value ] is not compatible with type 'a The second variant type does not allow tag(s) `value It doesn't even type the first call. Is this impossible?