From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id OAA04899; Wed, 26 Nov 2003 14:32:41 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id OAA05345 for ; Wed, 26 Nov 2003 14:32:40 +0100 (MET) Received: from morgon.inria.fr (morgon.inria.fr [128.93.8.33]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id hAQDWU118605; Wed, 26 Nov 2003 14:32:31 +0100 (MET) Received: (from remy@localhost) by morgon.inria.fr (8.11.6/8.11.6) id hAQDb5X11692; Wed, 26 Nov 2003 14:37:05 +0100 To: Tom.Hirschowitz@ens-lyon.fr Cc: caml-list@inria.fr Subject: Re: [Caml-list] sum types with objects References: <16324.62497.607234.359869@paille.cri2000.ens-lyon.fr> From: Didier Remy Date: 26 Nov 2003 14:37:05 +0100 In-Reply-To: <16324.62497.607234.359869@paille.cri2000.ens-lyon.fr> Message-ID: User-Agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 didier:01 morgon:01 hirschowitz:01 hirschowitz:01 ens-lyon:01 inference:01 beeing:01 beeing:01 didier:01 htmlman:01 bool:01 ocaml:01 caml:01 typechecking:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Tom Hirschowitz writes: > when trying to implement lists with objects and classes, I run into > the following issue. > > The idea is to define a class type list, parameterized over the type > 'a of elements, and containing the methods is_nil, hd, tl, map. Then, > I planned to define two classes nil and cons for building objects of > type list. See polymorphic methods in the manual. http://caml.inria.fr/ocaml/htmlman/manual005.html#ss:polymorphic-methods Still, you should use fold and not map: class type ['a] list = object method is_nil : bool method hd : 'a method tl : 'a list method fold : 'b. ('a -> 'b -> 'b) -> 'b -> 'b end;; exception Nil;; class ['a] nil = object (self : 'a #list) method is_nil = true method hd = raise Nil method tl = raise Nil method fold f x = x end;; class ['a] cons h t = object (self : 'a #list) method is_nil = false method hd = h method tl : 'a list = t method fold f x = f self#hd (self#tl#fold f x) end;; Otherwise, your are trying to define a type abbreviation type ('a, ...) #list = < hd : 'a tl : 'a list map : 'b. ('a -> 'b) -> ('b) list; ... > and 'b list = ('b, <>) list which is not allowed because the parameter of list in the body is not the same as the one in the declaration. Type inference will enforce 'b = 'a, which is not what you want. This is not just a matter of typechecking, though. In particular, if you had in mind an implementation of class cons of the form class cons h t = ... method map f = new cons (f self#hd) (self#tl#map f) end;; you would then have problems inheriting, since the constructor "new cons" is here the one of the class "cons" beeing defined and not the one of a subclass. Hence, you would also need to abstract cons over the constructor function of the subclass of cons beeing considered, which becomes closer to (but still less general than) what fold actually does. Didier ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners