does ex06 even do any reference counting? That seems to be mostly about the qualitative properties of RC.
Anyway, what I would like to discuss a bit is the implementation of the reference counting interpreter.
The short version is … the implementation of that interpreter is not very good.
This goes beyond your example (which I did not check btw.) and more than specific bugs, I think the whole concept of RC is not very clear in the interpreter. The first problem is, that each Value has a mutable field for the reference count. Disregarding the fact, that mutable state in an interpreter is always a code smell, it also makes very little sense to store the count together with the value. How many pointers exist to a particular location in the store is a property of either those pointers, or the store itself, not of the stored value.
The second problem (maybe as a result of the first), is that even values that are not stored in the store have a reference count. The way the interpreter works, is that each time a value is produces you increase the refcount of the produced value by one. E.g., `Num(2)` produces a new value so the value has a ref count of 1, and `Id('x)` produces the value bound to 'x, so you increase the refcount of that value by one. And each time a value is consumed, the refcount is decreased by one, i.e., `Add(Num(1), Num(2))` consumes the two numbers, reducing their value by 1. Note that the store is not involved in any of these operations.
Going by the second point, the bug you mentioned is very likely related to `SetBox` consuming the value of `Id('box)` but not decreasing the count of the consumed value. But I would recommend not using this particular interpreter as an example of good craftsmanship.
Now, if someone wants to improve the quality of the interpreters as part of an extended tutor position, a Praktikum in der Lehre, or a small lab project, please come talk to us