Possible improvements include:
boost::variant
to implement Temp
and Label
Temp
and Label
clearly implement a
union
in the sense of C. But C++ virtually forbids objects in
classes: only pod is allowed, this is why our design does not
use it.
Some people have worked hard to implement union
à la C++, i.e.,
with type safety, polymorphism etc. These union are called
“discriminated unions” or “variants” to follow the vocabulary
introduced by Caml. See the papers from Andrei Alexandrescu:
Discriminated Unions (i),
Discriminated Unions (ii),
Discriminated Unions (iii) for an introduction to the techniques. We
would use boost::variant
(voir Boost.org) if this material was
not too advanced for first year students.
I strongly encourage you to read these enlightening articles.
Tree
creates new nodes for equal
expressions; for instance two uses of the variable foo
lead to
two equal instantiations of tree::Temp
. The same applies to more
complex constructs such as the same translation if foo
is
actually a frame resident variable etc. Because memory consumption may
have a negative impact on performances, it is desirable to implement
maximal sharing: whenever a Tree
is needed, we first check
whether it already exists and then reuse it. This must be done
recursively: the translation of (x + x) * (x + x) should have a
single instantiation of x + x instead of two, but also a single
instantiation of x instead of four.
Node sharing makes some algorithms, such as rewriting, more complex,
especially wrt memory management. Garbage collection is almost
required, but fortunately the node of Tree
are reference counted!
Therefore, almost everything is ready to implement maximal node sharing.
Voir spot, for an explanation on how this approach was successfully
implemented. See
the ATermLibrary for a general implementation of maximally shared trees.