object identity

Fergus Henderson fjh at cs.mu.oz.au
Sat Apr 26 17:53:39 AEST 1997


Here's a nifty approach to the problem of object identity.

		% An object(T) is an abstract thing that
		% has an identity, and a value of type T.
		% Objects with the same identity have the same value,
		% but objects with the same value can have different
		% identities.
		% Objects are unifiable and comparable;
		% those operations are both O(1), since they need
		% only compare the object identities, not the values.
	:- type object(T).
	
		% `object_value(Object, Value)' is true iff
		% `Object' is an object with value `Value'.
	:- pred object_value(object(T), T).
	:- mode object_value(in, out) is det.		% this is O(1)

OK, fine so far... now here's the interesting bit:

	:- mode object_value(out, in) is cc_multi.	% this is also O(1)

This of course has a very large number of possible applications...

With the conservative garbage collector, the implementation is trivial:

	:- implementation.

	:- type object(T) == c_pointer.

	:- pragma c_code(object_value(O::in, V::out), will_not_call_mercury, "
		V = O;
	").
	:- pragma c_code(object_value(O::out, V::in), will_not_call_mercury, "
		O = V;
	").

For accurate garbage collection, the implementation is slightly more
difficult, but still quite easy.

Comments?

-- 
Fergus Henderson <fjh at cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3         |     -- the last words of T. S. Garp.



More information about the developers mailing list