From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id QAA23668 for caml-red; Fri, 2 Feb 2001 16:25:55 +0100 (MET) 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 CAA29755 for ; Fri, 2 Feb 2001 02:45:09 +0100 (MET) Received: from shell5.ba.best.com (shell5.ba.best.com [206.184.139.136]) by nez-perce.inria.fr (8.11.1/8.10.0) with ESMTP id f121j8509609 for ; Fri, 2 Feb 2001 02:45:08 +0100 (MET) Received: from localhost (bpr@localhost) by shell5.ba.best.com (8.9.3/8.9.2/best.sh) with ESMTP id RAA00797; Thu, 1 Feb 2001 17:45:00 -0800 (PST) Date: Thu, 1 Feb 2001 17:45:00 -0800 (PST) From: Brian Rogoff To: Juergen Pfitzenmaier cc: caml-list@inria.fr Subject: Re: # and polymorphic variants In-Reply-To: <200102010037.BAA12641@sunstroke.informatik.uni-tuebingen.de> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: weis@pauillac.inria.fr You need to use a "let rec" so that the function f1 is defined in f (or replace the "and"s with "let"s) and use aliases. The first should be obvious, the need for aliases seems necessary to get the coercion. Is this what you want? type t1 = [ `A of int ];; type t2 = [ `B of string ];; type t = [ `A of int | `B of string ];; let rec f1 (`A (x : int)) = print_int x and f2 (`B (x : string)) = print_string x and f (x : t) = match x with | #t1 as x -> f1 x | #t2 as x -> f2 x;; -- Brian On Thu, 1 Feb 2001, Juergen Pfitzenmaier wrote: > Dear ocaml users, > consider the following example: > > type t1 = [ `A of int ];; > type t2 = [ `B of string ];; > type t = [ `A of int | `B of string ];; > > let f1 (`A (x : int)) = > print_int x > and f2 (`B (x : string)) = > print_string x > and f (x : t) = > match x with > | #t1 -> f1 x (* this is not allowed !! *) > | #t2 -> f2 x;; > > The compiler can't constrain the type t of x to t1/t2 in the call to f1/f2. > And an coercion like in > ... > match x with > | #t1 -> f1 (x :> t1) > ... > is not allowed. So I see only one solution: > ... > match x with > | #t1 -> f1 (Obj.magic(x) :> t1) (* I don't like magic *) > ... > But I would like to see something clean like giving a name to the # pattern > ... > match x with > | #t1 y -> f1 y (* (y : t1) would have the same value as x *) > ... > or like > ... > match x with > | #t1 -> f1 x (* x is constrained to type t1 *) > ... > > Any comments ? > > -- pfitzen > > >