Next: , Up: Unique modes   [Contents]


5.1 Destructive update

In addition to the insts mentioned above (‘free’, ‘ground’, and ‘bound(…)’), Mercury also provides “unique” insts ‘unique’ and ‘unique(…)’ which are like ‘ground’ and ‘bound(…)’ respectively, except that they carry the additional constraint that there can only be one reference to the corresponding value. There is also an inst ‘dead’ which means that there are no references to the corresponding value, so the compiler is free to generate code that reuses that value. There are three standard modes for manipulation unique values:

% unique output
:- mode uo == free >> unique.

% unique input
:- mode ui == unique >> unique.

% destructive input
:- mode di == unique >> dead.

Mode ‘uo’ is used to create a unique value. Mode ‘ui’ is used to inspect a unique value without losing its uniqueness. Mode ‘di’ is used to deallocate or reuse the memory occupied by a value that will not be used.

Note that a value is not considered ‘unique’ if it might be needed on backtracking. This means that unique modes are generally only useful for code whose determinism is ‘det’ or ‘cc_multi’ (see Determinism).

Unlike ‘bound’ instantiatedness trees, there is no alternative syntax for ‘unique’ instantiatedness trees.