Hello,
I am interested in using Poly/ML's Weak structure ( http://polyml.org/documentation/Reference/Weak.html) for hash-consing. The documentation would perhaps suggest that this is not an intended use of Weak, so I'd like to ask the list whether it is suitable and, if not, whether there are alternatives.
An abstract of the problem is as follows. For a certain datatype, t, we want there to be at most one copy of any value of type t created by applying a constructor to unique arguments. To achieve this, we implement "hash-consing" constructors for t, which check whether the desired value exists already and return it if so, and only otherwise create a new value. To be able to check which values exist already, we maintain a hash table of all existing values. To allow values to be garbage collected when they are no longer needed, we make the hash table store only weak references to the values.
The Weak structure allows weak references to be made to other references, but not to other non-reference values. Thus, the values in the hash table can be of type (t ref option ref), as produced by Weak.weak, but not of type (t option ref). Thus, on the face of it, it seems like using Poly/ML's Weak implementation will require unnecessary references and indirections. Is that indeed the case, or is Poly/ML smart about avoiding unnecessary indirections somehow?
For hash-consing, we are not using Weak in the way described in its documentation, wherein the contents of the reference don't matter and the reference itself is only used as a token to track an external resource. Is there, perhaps, an alternative interface to Poly/ML's garbage collector more suitable for hash-consing?
Cheers, Ramana