8

I'm writing a clojure program which parses XML. As a part of this, I wish to create a tree of the nodes in the XML document, based on the clojure.xml/parse function. However I'd like the tree to be bi-directional - that is, each node has a list of children, and a pointer to its parent. There is only one problem: all data is immutable, and so I can't 'add' a pointer to the parent without changing the child, thus making the parent's pointer useless.

I've found this answer: How can one create cyclic (and immutable) data structures in Clojure without extra indirection?

The solution suggested there seems to be creating a separate index map, which refers to the objects inside. This seems like a huge amount of work for a much worse solution. I'd have no problem for the tree to be mutable during construction, however I can't figure out how can it be done. Is there really no way to get a cyclic pointer in clojure?

Thanks!

Community
  • 1
  • 1
Gilthans
  • 1,656
  • 1
  • 18
  • 23
  • 2
    The proper way of handling XML in a pure FP setting is to use zippers. http://clojuredocs.org/clojure_core/clojure.zip/xml-zip – Fred Foo Feb 14 '12 at 12:16

3 Answers3

5

It's logically impossible to make pure immutable structures cyclic, since by adding a parent or child pointer you would be mutating the structure.

There is a hack that works though I'm not sure I'd recommend it: you can put atoms inside Clojure data structures and then mutate these to make the necessary links. e.g.

(def parent {:id 1 :children (atom nil) :parent (atom nil)})

(def child  {:id 2 :children (atom nil) :parent (atom nil)}) 

(swap! (:children parent) conj child)
(reset! (:parent child) parent)

;; test it works
(:id @(:parent child))
=> 1

This is nasty in all sorts of ways:

  • It will cause your REPL to stack overflow if you try and print one of these because the REPL isn't expecting cyclic data structures.
  • It is mutable, so you lose all the maintainability and concurrency benefits of immutable data structures (which is one of the nicest things about Clojure!)
  • You'll need to take a full copy of a node if you want to duplicate it (e.g. building a new XML document), since it isn't an immutable value any more.
  • Dereferencing all the atoms can get messy as you navigate the structure.
  • You'll confuse people who are used to idiomatic Clojure.

So, it is possible if you really want to do it......... though I personally think you are still much better off in the long run if you make your document representation properly immutable. You could perhaps use something more like XPath style locations in the document if you want to navigate the structure.

mikera
  • 105,238
  • 25
  • 256
  • 415
  • Thanks - this seems to be what I wanted. Although I'm not really happy with how clojure is handling this... – Gilthans Feb 14 '12 at 11:21
  • 2
    Clojure is a pretty "opinionated" language and you are strongly encouraged to go down the immutable route. Better to embrace it than to fight it if you want to enjoy the full advantages of Clojure, I think..... and in the long run I believe this approach will help us all make better software. – mikera Feb 14 '12 at 11:45
  • 1
    "It's logically impossible to make pure immutable structures cyclic" -- in Clojure, perhaps. In Haskell, this is possible, at least to a limited extent. But +1 for the rest of the answer. – Fred Foo Feb 14 '12 at 12:12
  • hey @mikera, what would be the problem of using (transient []) for both :children and :parent while constructing the graph and before returning it just set everything to (persistent)? besides not being able to print such a structure it shouldn't be a problem, right? – carocad Apr 22 '16 at 15:35
2

Have you considered using zippers?

Zippers are designed for allowing for working with tree like structures effectively. It includes basic operations like looking at children and at the parent of a node while also allowing easily iterating through the structure.

Zippers are fairly generic and included functions already allow creating them from XML. An example on the Other Libraries page offers a good initial picture of how to work with them.

For an XML zipper, you'll want to use

(clojure.zip/xml-zip (clojure.xml/parse file))

to create the initial structure. When you're done, just call root to get the end structure.

deterb
  • 3,994
  • 1
  • 28
  • 33
0

Something like this could work:

(defprotocol TreeItemConstruction
  (add-child [this child]))

(deftype MyTree
  [parent ^:unsynchronized-mutable children]
  TreeItemConstruction
  (add-child [this child]
    (locking this
      (set! children (conj children child)))))

(defn my-tree
  [parent]
  (->MyTree parent []))

(def root (my-tree nil))
(def children (repeatedly 5 #(my-tree root)))
(doseq [child children] (add-child root child))

Make the protocol not part of the public API and never touch the mutable field after construction and you basically have an immutable tree. However modifying said tree after construction will be hard. YMMV.

kotarak
  • 17,099
  • 2
  • 49
  • 39