[mercury-users] Uniqueness and store.m

Ralph Becket rafe at cs.mu.OZ.AU
Sat Dec 31 11:43:57 AEDT 2005


To clarify what Mark suggested, instead of writing

:- type stype(S) ---> n(int)    % integer
        ; s(string)             % string
        ; p(                    % a pair of references
            store_ref(stype(S), S),
            store_ref(stype(S), S)).

main(!IO) :-
        new(S0),
        new_ref(n(5), Rn, S0, S1),
        new_ref(s("abc"), Rs, S1, S2),
        new_ref(p(Rn, Rs), _Rp, S2, _S3).

you could write

:- type stype(S) ---> n(int)    % integer
        ; s(string)             % string
        ; p(                    % a pair of references
            generic_mutvar(stype(S), S),
            generic_mutvar(stype(S), S)).

main(!IO) :-
        new(S0),
        new_mutvar(n(5), Rn, S0, S1),
        new_mutvar(s("abc"), Rs, S1, S2),
        new_mutvar(p(Rn, Rs), _Rp, S2, _S3).

You can avoid having to name each store state by using a local state
variable:

main(!IO) :-
	some [!S] (
		new(!:S),
		new_mutvar(n(5), Rn, !S),
		new_mutvar(s("abc"), Rs, !S),
		new_mutvar(p(Rn, Rs), _Rp, !S)
	).

A mutvar still gives you O(1) access/update times.  The difference being
that with a ref you can update the individual fields of the referent
structure.  refs have not been very well tested at all and, as you have
found out, are sufficiently awkward in practice that you should only use
them as a last resort (i.e., when you have optimised everything else in
your program and still need to improve efficiency).

-- Ralph
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list