[mercury-users] Managing a collection of unique objects

Lee Naish lee at cs.mu.oz.au
Mon Dec 1 17:13:39 AEDT 1997

In message <19971130171603.25904 at mundook.cs.mu.OZ.AU>Fergus wrote:
>% uniq mutvars
>	% uniq_mutvar(T, S):
>	% a mutable variable holding either a unique value of type T,
>	% or no value, in store S
>:- type uniq_mutvar(T, S).
>	% create a new unique mutable variable,
>	% initially holding no value.
>:- pred init_uniq_mutvar(mutvar(T, S), store(S), store(S)).
>:- mode init_uniq_mutvar(out, di, uo) is det.
>	% create a new unique mutable variable,
>	% initialized with the specified value
>:- pred new_uniq_mutvar(T, mutvar(T, S), store(S), store(S)).
>:- mode new_uniq_mutvar(di, out, di, uo) is det.
>	% retrieve the value stored in a given mutable variable,
>	% if any, and (to ensure uniqueness) update the store
>	% so that the variable no longer holds a value.
>:- pred fetch_uniq_mutvar(mutvar(T, S), maybe(T), store(S), store(S)).
>:- mode fetch_uniq_mutvar(in, uo, di, uo) is det.
>	% set the value stored in a given unique mutable variable
>:- pred set_uniq_mutvar(mutvar(T, S), T, store(S), store(S)).
>:- mode set_uniq_mutvar(in, di, di, uo) is det.

I can't comment on the low level details of all this, but there are some
high level design insights (due to Ueda, I believe) I would like to share.

If you are interested in collections of items and uniqueness of them
(numbers of references/single treadding/...) then its nice for the low
level operations (eg, library predicates) to preserve the numbers of
references to items as much as possible.  An operation which does this
and is often very useful is "swap" or "replace + return the old value".


More information about the users mailing list