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 JAA19038; Sun, 24 Oct 2004 09:52:46 +0200 (MET DST) 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 JAA19217 for ; Sun, 24 Oct 2004 09:52:44 +0200 (MET DST) Received: from mail.rsise.anu.edu.au (mail.rsise.anu.edu.au [150.203.208.4]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id i9O7qfX9022993 (version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO) for ; Sun, 24 Oct 2004 09:52:44 +0200 Received: from localhost (localhost [127.0.0.1]) by mail.rsise.anu.edu.au (Postfix) with ESMTP id 2DF7015829 for ; Sun, 24 Oct 2004 17:52:38 +1000 (EST) Received: from mail.rsise.anu.edu.au ([150.203.208.4]) by localhost (mail [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 29651-10 for ; Sun, 24 Oct 2004 17:52:38 +1000 (EST) Received: from pulp.anu.edu.au (pulp.rsise.anu.edu.au [150.203.208.49]) by mail.rsise.anu.edu.au (Postfix) with ESMTP id 138B3154E7 for ; Sun, 24 Oct 2004 17:52:37 +1000 (EST) Received: by pulp.anu.edu.au (Postfix, from userid 1560) id 5A96525B791; Sun, 24 Oct 2004 17:51:02 +1000 (EST) Date: Sun, 24 Oct 2004 17:51:02 +1000 From: Pietro Abate To: ocaml ml Subject: [Caml-list] set of sets ... Message-ID: <20041024075102.GA9493@pulp.anu.edu.au> Mail-Followup-To: Pietro Abate , ocaml ml Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Operating-System: GNU/Linux X-Organization: Research School of Information Science and Engineering (Australian National University) User-Agent: Mutt/1.5.6+20040907i X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at mail.rsise.anu.edu.au X-Miltered: at concorde with ID 417B5F49.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; pietro:01 abate:01 pietro:01 abate:01 extensible:01 extensible:01 val:01 struct:01 val:01 struct:01 failwith:01 downcast:01 ints:01 inherit:01 inherit:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk Hi all, I'm trying to write an extensible data structure where I can add new types with minimal effort, but I'm kinda stuck... my goal is to have a mixed list as: let a1 = (new set :> mixtype1 store);; let a2 = new setofset;; let a3 = (new intlist :> mixtype1 store);; let l = [a1;a2;a3] this doesn't work, but just to give you an idea... this is my approach. Is there a better way of doing it ? First I defined a virtual class and a couple of auxiliary types. The mixtype should be user extensible and the ext_rep should give me a uniform representation of all my objects in terms of lists. type mixtype = [ | `Int of int ] ;; type 'el ext_rep = | El of 'el list | Cont of 'el ext_rep list ;; class virtual ['mt] store = object(self : 'store) method virtual assign : 'store -> unit method virtual add : 'mt -> unit method virtual del : 'mt -> unit method virtual access : 'mt ext_rep method virtual copy : 'store end ;; (* The first class is a int list and it's easy... *) class intlist = object inherit [mixtype] store val mutable data = [] method data = data method assign store = data <- store#data method add e = data <- e::data method del e = () method access = El(data) method copy = {< data = data >} end ;; (* now I want to have set of int: *) module OriginalSet = Set module Set = OriginalSet.Make ( struct type t = mixtype let compare = compare end );; class set = object inherit [mixtype] store val mutable data = Set.empty method data = data method assign store = data <- store#data method add e = data <- Set.add e data method del e = () method access = El(Set.elements data) method copy = {< data = data >} end ;; (* now I want to extend my mixtype, add a new type to use the set of int class and define a set of sets of ints... This should give a good deal of flexibility as to add a new type I just need to subclass store and add a new mixtype that is more general... *) type mixtype1 = [ |`Set of set |mixtype ];; module SetofSet = OriginalSet.Make ( struct type t = mixtype1 let compare = compare end );; class setofset = object inherit [mixtype1] store val mutable data = SetofSet.empty method data = data method assign store = data <- store#data method add e = data <- SetofSet.add e data method del e = () method access = Cont ( List.map (function |`Set e -> (e#access: mixtype ext_rep :> mixtype1 ext_rep) |#mixtype -> failwith "wrong type" ) (SetofSet.elements data) ) method copy = {< data = data >} end ;; (* so far, so good. Now I want to put these three objects in a list and downcast them to the store class (using mixtype1 that should be a super-type of mixtype) *) (* I tried many combinations, but with poor results... this doesn't work .... It says: Type mixtype = [ `Int of int ] is not compatible with type 'b = [> `Int of int | `Set of set ] The first variant type does not allow tag(s) `Set. This simple coercion was not fully general. Consider using a double coercion. But I don't understand why, as mixtype1 should be more general than mixtype... *) let a1 = (new set :> mixtype1 store);; let a2 = new setofset;; let a3 = (new intlist :> mixtype1 store);; let l = [a1;a2;a3] thanks, p -- ++ "All great truths begin as blasphemies." -George Bernard Shaw ++ Please avoid sending me Word or PowerPoint attachments. See http://www.fsf.org/philosophy/no-word-attachments.html ------------------- 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