[mercury-users] Debug messages

obo at ruk.cuni.cz obo at ruk.cuni.cz
Fri Feb 18 02:50:29 AEDT 2005


Hi.

I'm using the unsafe.m module wrapped by my debugstr/1 predicate. See the 
modules attached.

Cheers, O.

On Thu, 17 Feb 2005, Elmar Haneke wrote:

> Hi,
>
> how can I make my program output some trace-data without using the io_state?
>
> For debugging pruposes it is very much more convenient to place an 
> 'print("Was here")' to some code where no IO-state is passed or IO is not 
> possible due to backtracking..
>
> Elmar
> --------------------------------------------------------------------------
> 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
> --------------------------------------------------------------------------
>

--
Ondrej Bojar (mailto:obo at cuni.cz)
http://www.cuni.cz/~obo
-------------- next part --------------
%-----------------------------------------------------------------------------%
% Copyright (C) 1997 The University of Melbourne.
% This file may only be copied under the terms of the GNU Library General
% Public License - see the file COPYING.LIB in the Mercury distribution.
%-----------------------------------------------------------------------------%
% File: unsafe.m
% Author: fjh
% Stability: low
%-----------------------------------------------------------------------------%

/*
** WARNING: the procedures defined in this module are non-logical.
**          They may have side effects, they may violate type safety,
**	    they may interfere with certain memory management strategies,
**	    and in general they may do lots of nasty things.
**	    They may not work with future release of the Mercury compiler,
**	    or with other Mercury implementations.
**          Use only as a last resort, and only with great care!
**
** You have been warned.
*/

%-----------------------------------------------------------------------------%

:- module unsafe.
:- interface.
:- import_module io.

/*
** unsafe_perform_io/1 performs I/O, in an unsafe manner.
** It can be used to call a goal that does I/O or has
** side effects from a context where you do not have an io__state.
** It can be useful for printf-style debugging.
** But backtracking over a call to `unsafe_perform_io'
** can be very dangerous indeed, because with certain
** memory allocation policies it can result in dangling pointers.
*/
:- impure pred unsafe_perform_io(pred(io__state, io__state)).
:- mode unsafe_perform_io(pred(di, uo) is det) is det.
:- mode unsafe_perform_io(pred(di, uo) is cc_multi) is det.

/*
** The function unsafe_promise_ground/1 can be used to assert to the
** compiler that a particular value of inst `any' is in fact ground.
** The assertion is *not* checked.  If it is false, all hell may break out.
*/
:- func unsafe_promise_ground(T::in(any)) = (T::out) is det.

%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%

:- implementation.

:- pragma c_header_code("#include ""unsafe.mh""").

%-----------------------------------------------------------------------------%

:- pragma c_code(unsafe_promise_ground(X::in(any)) = (Y::out), "Y = X;").

%-----------------------------------------------------------------------------%

:- pragma c_code(
unsafe_perform_io(P::(pred(di, uo) is det)),
	may_call_mercury,
"{
	call_io_pred_det(P);
}").
:- pragma c_code(
unsafe_perform_io(P::(pred(di, uo) is cc_multi)),
	may_call_mercury,
"{
	call_io_pred_cc_multi(P);
}").

:- pred call_io_pred(pred(io__state, io__state), io__state, io__state).
:- mode call_io_pred(pred(di, uo) is det, di, uo) is det.
:- mode call_io_pred(pred(di, uo) is cc_multi, di, uo) is cc_multi.

:- pragma export(call_io_pred(pred(di, uo) is det, di, uo),
		"call_io_pred_det").
:- pragma export(call_io_pred(pred(di, uo) is cc_multi, di, uo),
		"call_io_pred_cc_multi").

call_io_pred(P) --> P.

%-----------------------------------------------------------------------------%
-------------- next part --------------
% Debugstr pred for printing a string anywhere
% nonlogical, impure, unsafe

:- module debugstr.

:- interface.
:- import_module int, string, list.

% Dump to stderr
:- pred debugformat(string::in, list(string__poly_type)::in) is det.
:- pred debugstr(T::in) is det.
:- pred debugstr(T::in, S::in) is det.
:- pred debugnl(T::in) is det.
:- pred debugnl is det.
:- pred debugstats is det.
  % Benchmarking.

% The same predicates, but threading the aku in DCG notation
:- pred debugstr(T, Aku, Aku).
:- mode debugstr(in, in, out) is det.
:- mode debugstr(in, di, uo) is det.
:- pred debugstr(T, S, Aku, Aku).
:- mode debugstr(in, in, in, out) is det.
:- mode debugstr(in, in, di, uo) is det.
:- pred debugnl(T, Aku, Aku).
:- mode debugnl(in, in, out) is det.
:- mode debugnl(in, di, uo) is det.
:- pred debugnl(Aku, Aku).
:- mode debugnl(in, out) is det.
:- mode debugnl(di, uo) is det.
:- pred debugstats(Aku, Aku).
:- mode debugstats(in, out) is det.
:- mode debugstats(di, uo) is det.

% Dump to stdout
:- pred dump(T::in) is det.
:- pred dump(T::in, S::in) is det.

:- pred sleep(int::in) is det.


:- implementation.

:- import_module std_util.
:- import_module io.
:- import_module unsafe.
:- import_module benchmarking.

debugformat(F, Vars) :-
  debugstr(string__format(F, Vars)`with_type`string).

:- pragma promise_pure(debugstr/1).
debugstr(Anything) :-
  % true.
  impure unsafe_perform_io(stderrprint(Anything)).

:- pragma promise_pure(dump/1).
dump(Anything) :-
  % true.
  impure unsafe_perform_io(print(Anything)).

:- pragma promise_pure(debugstats/0).
debugstats :-
  % true.
  impure report_stats.


:- pred stderrprint(T::in, io__state::di, io__state::uo) is det.
stderrprint(Anything) -->
  io__stderr_stream(StdErr),
  io__print(StdErr, Anything).

debugnl(Anything) :-
  debugstr(Anything),
  debugnl.

debugstr(Comment, Anything) :-
  debugstr(Comment),
  debugnl(Anything).


debugstr(A, Aku, Aku) :- debugstr(A).
debugstr(A, B, Aku, Aku) :- debugstr(A, B).
debugnl(A, Aku, Aku) :- debugnl(A).
debugnl(Aku, Aku) :- debugnl.
debugstats(Aku, Aku) :- debugstats.

dump(Comment, Anything) :-
  dump(Comment),
  dump(Anything),
  dump("\n").

:- import_module string.

debugnl :- debugstr("\n").


:- pragma c_code(sleep(W::in), [will_not_call_mercury],
     " sleep(W); ").



More information about the users mailing list