[mercury-users] User-defined Extension of builtin__copy?

Zoltan Somogyi zs at cs.mu.OZ.AU
Wed Sep 14 14:01:53 AEST 2005


On 13-Sep-2005, Kral Stefan <skral at mips.complang.tuwien.ac.at> wrote:
> Currently working on GMP bindings for Mercury,
> I am missing some way of telling the compiler
> how a (unique) copy of some value (of a 
> foreign type) can be created.
> 
> So, what I would like to write is something like
>   :- pragma foreign_type("C", gmpint, "GMP_Int *")
> 	    where equality is gmpint_equals,
> 	          comparison is gmpint_compare,
> 		  copy is gmpint_copy.
> 
> I think this problem only arises if the Mercury compiler 
> does not know about the internal representation of that 
> particular type (ie. only for foreign types).
> 
> Is there a way to solve the problem?

I am not sure which of several related problems you actually want to solve.
I am pretty sure in each case the answer is "yes", but the solutions of
course do difer.

If you have an value X of type gmpint that is actually unique but the
Mercury compiler can't prove to itself that it is unique, you can
convince it of that by calling unsafe_promise_unique. After the call

	unsafe_promise_unique(X, UniqueX)

the compiler will believe UniqueX to be unique. The call "copy(X, UniqueX)"
will also tell the compiler that UniqueX is unique, but it will try to
copy X, and as you discovered. Copying values of foreign types is not allowed
by default, but you can override this by attaching two assertions to
the foreign_type declaration:

   :- pragma foreign_type("C", gmpint, "GMP_Int *",
   		[can_pass_as_mercury_type, stable])
 	    where equality is gmpint_equals,
 	          comparison is gmpint_compare.

The first assertion, can_pass_as_mercury_type, says that values of this type
fit into an MR_Word, the basic type of everything (abstract machine registers,
stack slots etc) in the Mercury implementation. Since an MR_Word is an
integer the same size as a pointer, it seems "GMP_Int *" qualifies.
The second, stable, says that either the foreign type is either an integer
(which GMP_Int * by itself isn't) or a pointer to memory whose contents
will never change. I don't know whether the second part is true or not,
but I expect you do.

It you can't say that gmpint is stable, then you can consider simply
replacing any code that copies gmpints, or terms containing gmpints,
with code that (falsely, if necessary) promises them to be unique. Whether
that will work depends on why you want gmpints to be unique in the first
place; you'd have to tell me more about that.

What implementation did you have in mind for gmpint_copy?

Zoltan Somogyi <zs at cs.mu.OZ.AU> http://www.cs.mu.oz.au/~zs/
Department of Computer Science and Software Engineering, Univ. of Melbourne

--------------------------------------------------------------------------
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