Alf said:
* Jon Harrop:
Not really. E.g. your example concerned a self-referential expression.
When the self-referential nature becomes a problem, e.g. not easily
modelled via weak or raw pointers, then that indicates that the
structure used to represent the problem domain is not well chosen, i.e.
will likely lead to problems also in other processing (not just garbage
collection).
Graph theory is a very elegant branch of pure mathematics. Garbage collected
languages can represent graphs and graph-theoretic algorithms with
simplicity, clarity and robustness.
The fact that C++ cannot do this in no way diminishes graph theory as a
useful tool. Graph theory remains of fundamental importance in computer
science.
It's like an infinitely recursive function.
Not "infinitely" recursive. A recursive function is an ideal example of
something that would be most elegantly represented by a cyclic graph. I did
exactly this in my example interpreter here:
http://www.ffconsultancy.com/free/ocaml/interpreter.html
The pattern match in the "eval" function creates a cyclic graph in the host
language to represent a recursive function in the target language:
| ELetRec(var, arg, body, rest) ->
let rec vars = (var, VClosure(arg, vars, body)) :: vars in
eval vars rest
Some
languages (e.g. data flow languages) can handle infinite recursion
easily,
Almost all languages (including C++) can handle recursion.
but that doesn't mean it's a good idea: it's usually an
indication of some thinko, a much less than perfect implementation.
If you believe it is imperfect then how do you think it can be improved?
C++ RAII is a trade-off, involving some (design) work being delegated to
the programmer. I think that's generally a Good Thing(TM), because when
the language is capable of dealing with any messy structure, messy
structures are likely to abound.
You are implying that C++ can deal with structures that other languages
cannot. Can you elaborate?
The examples I've given don't look messy to me.
However, I grant that in some problem
domains such structures /may/ be practically necessary, and the question
is still whether some example can be found where that is so.
Here are some examples from various disciplines:
You already mentioned the example of representing recursive functions in
compilers and interpreters, e.g. for register allocation:
http://citeseer.ist.psu.edu/492550.html
In graphics, meshes are most elegantly represented as co-cyclic graphs of
vertex, edge and triangle connectivity. Functions that act upon meshes are
ubiquitous in graphics programming:
http://www.actapress.com/PDFViewer.aspx?paperId=29408
Scene graphs are the most common high-level representation of 3D worlds and
these can be cyclic:
http://www.techfak.uni-bielefeld.de.../4a.RT3DCGVR-Graphics-pipeline-and-graphs.pdf
Higher-dimensional multiresolution representations and adaptive subdivisions
can also benefit from cyclicity.
In symbolic computing, computer algebra packages have very similar structure
to compilers and interpreters and must be able to rewrite cyclic graphs
representing symbolic expressions:
http://journals.cambridge.org/production/action/cjoGetFulltext?fulltextid=455532
In biological scientific computing, any relationship network (e.g. metabolic
pathways, gene expression relationships) can be cyclic and algorithms for
storing and manipulating these must be able to handle cyclic graphs:
http://216.239.59.104/search?q=cach...lic+graph"++leroy&hl=en&ct=clnk&cd=8&ie=UTF-8
In physical scientific computing, graph theory is used in many subjects,
e.g. to study the topology of amorphous materials at the atomic scale, e.g.
Franzblau's shortest path ring statistics applied to the structure of
silica glass in my own PhD thesis:
http://www.ffconsultancy.com/free/thesis.html
Finally, I should probably mention that implementing a garbage collector is
another application of graph theory. Although this is a funny example to
bring up, it underlines my point that writing code to collect graphs in C++
is literally writing your own garbage collector.
As you can see, cyclic-graph theory and computer programming have many
overlaps. Consequently, it is beneficial to use a language that allows such
things to be represented succinctly and efficiently.