caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jon Harrop <jdh30@cam.ac.uk>
To: caml-list@inria.fr
Subject: [Caml-list] Extensible graphs
Date: Sun, 28 Mar 2004 22:00:57 +0000	[thread overview]
Message-ID: <200403282300.57264.jdh30@cam.ac.uk> (raw)


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


             reply	other threads:[~2004-03-28 22:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-03-28 22:00 Jon Harrop [this message]
2004-03-31  9:11 ` Diego Olivier Fernandez Pons
2004-04-07  1:26   ` Jon Harrop

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=200403282300.57264.jdh30@cam.ac.uk \
    --to=jdh30@cam.ac.uk \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).