[mercury-users] Mode System Hassles

Robert Ernst Johann JESCHOFNIK rejj at cat.cs.mu.OZ.AU
Wed Oct 20 09:58:47 AEST 1999


On Tue, 19 Oct 1999, Ralph Becket wrote:

Hello Ralph.

I also have just recently run across the exact same problem. Fortunately,
I live (heh) in the same building as the Mercury guys, So I was able to
corner both Zoltan and Tom and beg for help.

It turns out that (at least for the moment) a nasty hack is required.

> If X is an aarray object, then `Y = array__lookup(X, 123)' leaves Y with
> inst ground, rather than uniq_array which is what I need if I'm going to
> do anything with Y!

Here is what I have done:

:- type polyhedron --->
	polyhedron(array(vertex), list(face)).

% The instantiation for a unique polyhedron
:- inst uniq_polyhedron == unique(polyhedron(uniq_array, ground)).

:- mode polyhedron_di == di(uniq_polyhedron).
:- mode polyhedron_uo == out(uniq_polyhedron).
:- mode polyhedron_ui == in(uniq_polyhedron).

:- type world == array(polyhedron).

% The instantiation for a world of unique polyhedrons (The entire scene)
:- inst world_uniq_polys == uniq_array(uniq_polyhedron).

:- mode world_di == di(world_uniq_polys).
:- mode world_uo == out(world_uniq_polys).
:- mode world_ui == in(world_uniq_polys).

So.. I have a "world", which is an array of polyhedrons, each of which is
an array. I require that both are unique, so I can use destructive update
with array__set, otherwise I will start using up memory like crazy.

I originally tried playing with unsafe_promise_unique/2, however it
returns an object of instantiatedness `unique', not uniq_array or
uniq_polyhedron.

So, here is the nasty hack to get around this.

% This is a nasty hack to ensure uniqueness of a polyhedron. This is required
% because nested unique modes are currently not supported by the Mercury
% compiler.
% Thanks to Tom Conway for this.
:- func u_p(polyhedron) = polyhedron.
:- mode (u_p(in) = polyhedron_uo) is det.

:- pragma c_code(u_p(A::in) = (B::polyhedron_uo),
		 [will_not_call_mercury, thread_safe],
		 "B = A;"
		 ).

then, you can just use u_p(X) wherever you would have used X.
 


Hope it helps,
Rob
--------------------------------------------------------------------------
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