caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Christopher L Conway <cconway@cs.nyu.edu>
To: caml-list@inria.fr
Subject: Immutable cyclic data structures via a
Date: Sat, 13 Dec 2008 21:45:07 +0000 (UTC)	[thread overview]
Message-ID: <loom.20081213T213803-424@post.gmane.org> (raw)

--- In ocaml_beginners@yahoogroups.com, Andrew Myers <andru@...> wrote:
> [Note from Chris: I'm reposting this from the beginner's list, because I'm
interested in an answer and more likely to get it here... A declaration of a
graph data structure might be:] 
>
> type node = edge list
> and edge = {from: node; to_: node}
>
> You'll have trouble initializing that data structure, though. Maybe
> better to add mutability somewhere, e.g.
>
> type node = {mutable outgoing: edge list}
> and edge = {from: node; to_: node}
>
> Then you can create all the nodes first and backpatch them with all the
> edges.

This makes me wonder: if you add mutable fields to a type in order to
build a cyclic data structure, but you intend to use the data
structure immutably once it is built, is there any way to "freeze" the
value, producing an immutable cyclic structure? This is a common
pattern, similar to Bloch's Builder pattern for Java.[1]

You might have an operation, call it "unmute", that changes the type
of the structure. There would need to be a corresponding type-level
operation, call it "UNMUTE", that strips the "mutable" qualifier from
mutable fields. E.g.,

val unmute : 'a -> UNMUTE('a)

and

UNMUTE(node) = { outgoing : edge list }

for node as defined above.

I'm pretty confident this isn't possible in plain-vanilla OCaml. You
can do something similar (at a high cost in ugliness) using a
variation of "Tying the Knot" a la Haskell.[2]

Is anybody aware of a language extension that does this, or a similar
feature in another language?

Regards,
Chris

[1]: http://www.screaming-penguin.com/node/7598 "The essence of the
enhanced builder pattern in Java"
[2]: http://www.haskell.org/haskellwiki/Tying_the_Knot



             reply	other threads:[~2008-12-13 21:45 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-12-13 21:45 Christopher L Conway [this message]
2008-12-13 23:30 ` [Caml-list] " Jeremy Yallop
2008-12-13 23:43   ` Jeremy Yallop
2008-12-14  1:47 ` Edgar Friendly
2008-12-14  9:10   ` Francois Pottier
2008-12-14 14:29     ` blue storm
2008-12-14 20:06 ` Richard Jones

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=loom.20081213T213803-424@post.gmane.org \
    --to=cconway@cs.nyu.edu \
    --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).