Next: , Previous: , Up: Unique modes   [Contents]


5.2 Backtrackable destructive update

“Well it just so happens that your friend here is only mostly dead.
There’s a big difference between mostly dead and all dead…
Now, mostly dead is slightly alive.
Now, all dead — well, with all dead, there’s usually only one thing that you can do.”

“What’s that?”

“Go through his clothes and look for loose change!”

— from the movie “The Princess Bride”.

To allow for backtrackable destructive updates — that is, updates whose effect is undone on backtracking, perhaps by recording the overwritten values on a “trail” so that they can be restored after backtracking — Mercury also provides “mostly unique” modes. The insts mostly_unique and mostly_dead are equivalent to unique and dead, except that only references which will be encountered during forward execution are counted — it is OK for mostly_unique or mostly_dead values to be needed again on backtracking.

Mercury defines some standard modes for manipulating “mostly unique” values, just as it does for unique values:

% mostly unique output
:- mode muo == free >> mostly_unique.

% mostly unique input
:- mode mui == mostly_unique >> mostly_unique.

% mostly destructive input
:- mode mdi == mostly_unique >> mostly_dead.