[m-dev.] tabling, trailing and impure references

David Overton dmo at cs.mu.OZ.AU
Mon Aug 4 11:19:18 AEST 2003


Hi,

I have a few questions about the implementation of tabling.  We are
wondering whether it is possible to implement global variables (for HAL)
using reference.m / nb_reference.m and tabling.  For example, consider
the following code.

	:- module testref.

	:- interface.

	:- import_module io.

	:- pred main(io::di, io::uo) is det.

	:- implementation.

	:- import_module nb_reference.
	:- import_module std_util.

	:- func my_ref = nb_reference(int).

	:- pragma memo(my_ref/0).
	:- pragma promise_pure(my_ref/0).

	my_ref = R :-
		impure new_nb_reference(0, R).

	:- pragma promise_pure(main/2).

	main -->
		{ semipure value(my_ref, N1) },
		io__write_int(N1), nl,
		{ impure update(my_ref, 42) },
		{ semipure value(my_ref, N2) },
		io__write_int(N2), nl,

		(
			{ impure update(my_ref, 84) },
			{ semidet_fail }
		->
			[]
		;
			{ semipure value(my_ref, N3) },
			io__write_int(N3), nl
		).

Here we use the function my_ref/0 as a kind of "global variable" whose
value can be updated using impure code.  This relies on the
`:- pragma memo' to ensure that calls to my_ref always return the same
reference.  This code compiles and prints

	0
	42
	84

which is the expected answer.  If I change my_ref to return a
reference(int) instead of nb_reference(int), it prints

	0
	42
	42

which is also what is expected.

Question 1:	How reliable is this approach?  Does the
		`:- pragma memo' guarantee that my_ref will
		always return the same reference?

Question 2:	The reference manual says: "if you enable the use of
		trailing ... then any use of tabling will result in a
		"Sorry, not implemented" error at runtime."
		
		I get no such error from the above example in grade
		asm_fast.gc.tr.  Does this mean the reference manual is
		out of date, or have I just been lucky?

Our current implementation of global variables uses a C global variable
of type ME_Reference.  For each of these, we need to arrange for
initialisation code to be called at program startup.  The approach I've
described above seems to be simpler and cleaner, if it will work.  


David
-- 
David Overton                  Uni of Melbourne     +61 3 8344 1354
dmo at cs.mu.oz.au                Monash Uni (Clayton) +61 3 9905 5779
http://www.cs.mu.oz.au/~dmo    Mobile Phone         +61 4 0337 4393
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list