[mercury-users] New User: Debug prints from Mercury? Printing "free" variables?

Fergus Henderson fjh at cs.mu.OZ.AU
Mon May 7 23:09:25 AEST 2001

On 07-May-2001, Ondrej Bojar <oboj7042 at ss1000.ms.mff.cuni.cz> wrote:
> I know Prolog quite well, but I'm not quite familiar with the way that
> Mercury handles io. The hardest problem for me is to make debug prints.
> I am used to do debug using debug prints -- print anywhere anything and
> check, if it is what I wanted. I guess this is quite tricky with the
> "single nonbacktracking thread of state_of_the_world variables"...

Well, there is a way.  You can use unsafe_perform_io, which is defined in the
module `unsafe' in the trailed_update directory of the extras package.
(The simplest way of installing this is to just copy unsafe.m into the
same directory as the code you are trying to debug.)

Using `unsafe_perform_io', instead of e.g.

	:- func square(int) = int.
	square(X) = X * X.
you could write

	:- import_module unsafe.

	:- func square(int) = int.
	:- pragma promise_pure(square/1).
	square(X) = Square :-
		% XXX These calls to unsafe_perform_io are just for debugging
		impure unsafe_perform_io(print("entering square/2\n")),
		impure unsafe_perform_io(print("X = ")),
		impure unsafe_perform_io(print(X)),
		impure unsafe_perform_io(nl),
		Square = X * X,
		impure unsafe_perform_io(print("Square = ")),
		impure unsafe_perform_io(print(Square)),
		impure unsafe_perform_io(nl),
		impure unsafe_perform_io(print("leaving square/2...\n")).

However, this approach is generally not recommended.

> Is there another good way of debugging.

Yes, use mdb.  See the Debugging chapter in the Mercury User's Guide.

If you have any feedback about mdb and/or its documentation,
we'd like to hear it.

> Moreover, I would like to be able to print out "free" variables -- in a
> similar way to Prolog ( _1234 for an unnamed variable). Again, I guess,
> there is no way to do it, as  read somewhere in the manual, that Mercury
> actually never has a variable uninstatiated...

Yes, there's no way to print out any useful information about the values of
"free" variables, since they don't have any value.  If you really want to
print *something* for a free variable you could use a procedure like this one

	:- pred print_free_variable(var::unused, io__state::di, io__state::uo)
		is det.

	print_free_variable(_) --> print("_").

but I doubt if that would be very useful!

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