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 AAA06370; Mon, 29 Mar 2004 00:13:24 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f 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 AAA06857 for ; Mon, 29 Mar 2004 00:13:23 +0200 (MET DST) Received: from ptb-relay03.plus.net (ptb-relay03.plus.net [212.159.14.214]) by nez-perce.inria.fr (8.12.10/8.12.10) with ESMTP id i2SME3KW024248 for ; Mon, 29 Mar 2004 00:14:03 +0200 Received: from [80.229.56.224] (helo=chetara) by ptb-relay03.plus.net with esmtp (Exim) id 1B7iWw-0008fn-5n for caml-list@inria.fr; Sun, 28 Mar 2004 22:13:22 +0000 From: Jon Harrop Organization: University of Cambridge To: caml-list@inria.fr Subject: [Caml-list] Extensible graphs Date: Sun, 28 Mar 2004 22:00:57 +0000 User-Agent: KMail/1.5.4 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200403282300.57264.jdh30@cam.ac.uk> X-Miltered: at nez-perce by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; extensible:01 rec:01 routines:02 node:02 node:02 match:02 explicitly:03 inheritance:03 object:03 slightly:03 types:03 types:03 bunch:03 data:03 data:03 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk X-Status: X-Keywords: X-UID: 399 Hi! I'm writing code which I wish to sell in object form and I'd like it to contain a basic representation of a graph which can be extended. This basic graph might be something like: type leaf = A | B type node = Leaf of leaf | Group of node list I'll be defining a bunch of functions which act on a graph, e.g.: let rec leaf_count n = match n with Leaf _ -> 1 | Group l -> List.fold_left (+) 0 (List.map leaf_count l) People who use this code are likely to want to make a slightly more complicated graph which contains, say, an extra leaf type, an extra node type and more functions which act on the new type of graph, equivalent to this: type leaf = A | B | C type node = Leaf of leaf | FunkyGroup of node list | Group of node list Adding new functions which use the existing data types is easy, but I can't see any way to allow them to add new node types without requiring them to reimplement everything, or at least explicitly call the old routines from any new ones when they are used with the old data types. I've also tried using inheritance by deriving everything from an ABC "node". But this just replaces this problem with another problem. If the types of node are all derived from a "node" ABC then you can easily add new types but you can't easily add new (method) functions to all types. Is factoring out as much code as possible the best I can do, or is there a better way to approach this problem? Cheers, Jon. ------------------- 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