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=1.3 required=5.0 tests=SPF_FAIL 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 EE22FBBC1 for ; Tue, 19 Feb 2008 14:45:07 +0100 (CET) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAAO5rukfAXQInh2dsb2JhbACQTQEBAQgKKYEUnC8 X-IronPort-AV: E=Sophos;i="4.25,376,1199660400"; d="scan'208";a="22791911" Received: from concorde.inria.fr ([192.93.2.39]) by mail4-smtp-sop.national.inria.fr with ESMTP; 19 Feb 2008 14:45:08 +0100 Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by concorde.inria.fr (8.13.6/8.13.6) with ESMTP id m1JDj7JA021291 (version=TLSv1/SSLv3 cipher=RC4-SHA bits=128 verify=OK) for ; Tue, 19 Feb 2008 14:45:07 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AgAAALNrukdQW+UCh2dsb2JhbACQTQEBAQgKKYEUnDI X-IronPort-AV: E=Sophos;i="4.25,376,1199660400"; d="scan'208";a="8275347" Received: from main.gmane.org (HELO ciao.gmane.org) ([80.91.229.2]) by mail1-smtp-roc.national.inria.fr with ESMTP; 19 Feb 2008 14:45:06 +0100 Received: from root by ciao.gmane.org with local (Exim 4.43) id 1JRSmM-00011F-9b for caml-list@inria.fr; Tue, 19 Feb 2008 13:45:02 +0000 Received: from 93.71.103-84.rev.gaoland.net ([84.103.71.93]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 19 Feb 2008 13:45:02 +0000 Received: from vanicat by 93.71.103-84.rev.gaoland.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 19 Feb 2008 13:45:02 +0000 X-Injected-Via-Gmane: http://gmane.org/ To: caml-list@inria.fr From: Remi Vanicat Subject: Re: polymorphic variants and promotion Date: Tue, 19 Feb 2008 13:48:28 +0100 Organization: none Message-ID: <871w796rj7.dlv@maison.homelinux.org> References: <926565e50802190223r456d0ec2we4b8a4f51b134f42@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 93.71.103-84.rev.gaoland.net Mail-Copy-To: never User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux) Cancel-Lock: sha1:wvUFxnGm1LWx//VgSmFe0UXTTog= Sender: news X-Miltered: at concorde with ID 47BADD63.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; variants:01 lri:01 variants:01 val:01 val:01 a's:01 compiler:01 polymorphic:01 polymorphic:01 typing:01 functions:01 remi:01 int:01 int:01 writes:01 "Johannes Kanig" writes: > Hi, > > I ran into the following problem using polymorphic variants. Suppose I have > 2 functions f and g (the function definitions don't make much sense - they > are just there for typing purposes) : > > # let f (`A a) (`A i) = `A i ;; > val f : [< `A of 'a ] -> [< `A of 'b ] -> [> `A of 'b ] = > # let g (`A a) = (`B a) ;; > val g : [< `A of 'a ] -> [> `B of 'a ] = > > For simplicity I assume that the type variables all instantiate to "int". > Now, I want to construct a function h that takes an accumulator a and a list > of `A's (for example [`A 1; `A 2; ...]) that produces either an object of > type `A of int or an object of type `B of int in the following way: > > # let h a = function > | [] -> g a > | xs -> List.fold_left f a xs;; > > But this doesn't type: > > This expression has type [ `A of 'a ] but is here used with type > [> `B of 'a ] > The first variant type does not allow tag(s) `B Well, you have to explicitly coerce : let h a = function | [] -> g a | xs -> (List.fold_left f a xs : [ `A of _ ] :> [> `A of _ | `B of _ ]) ;; What I don't understand is why this : let h a = function | [] -> g a | xs -> (List.fold_left f a xs :> [> `A of _ | `B of _ ]) ;; do not work ? I understood that we need to explicitly coerce, but the [ `A of _ ] is just the information the compiler will find by itself, why must we give it to it again ? -- RĂ©mi Vanicat