[mercury-users] Re: BOUNCE mercury-users at cs.mu.OZ.AU: Non-member submission from [Peter Hawkins <hawkinsp at cs.mu.OZ.AU>]
Ralph Becket
rafe at cs.mu.OZ.AU
Wed Mar 17 19:23:55 AEDT 2004
Hi,
it's necessary to sign up to the mercury-users mailing list before
posting to it (your message narrowly avoided being discarded as spam.)
Peter Hawkins <hawkinsp at cs.mu.OZ.AU>, Wednesday, 17 March 2004:
>
> Hi...
>
> I have two quick questions about mercury language features:
>
> 1) Is there some way of doing non-logical I/O in order to do printf style
> debugging?
Yes, but we'd rather not tell you about it!
I've attached a module unsafe.m which should do what you ask, but its
use is strictly at your own risk.
> 2) If I have an arbitrary term, say [1,2,3,4,5], is there a
> way of getting a string representation of that term? I'd like a
> representation similar to the one that would be produced by io__print/3,
> except to a string, not to a stream. (I'm trying to make my error messages as
> produced using the error/1 predicate a little more verbose).
[Ahhh, I thought I'd checked that patch in...]
I'm just about to check in a patch to the string library module adding a
function string/1 which turns arbitrary values into strings. I think
this is what you're after - it should appear in a ROTD in the next day
or two.
Your other option is to use the pprint library module which implements a
pretty printer. pprint should be your first port of call for
non-trivial structured output.
-- Ralph
-------------- next part --------------
%------------------------------------------------------------------------------%
% unsafe.m
% Ralph Becket <rafe at cs.mu.oz.au>
% Wed Jan 30 11:10:10 EST 2002
% vim: ft=mercury ff=unix ts=4 sw=4 et tw=0 wm=0
%
% Unsafe IO.
%
%------------------------------------------------------------------------------%
:- module unsafe.
:- interface.
:- import_module io, list, string.
% Support arbitrary unsafe IO operations without the need to
% thread the IO state in. Intended largely for debugging only.
%
:- pred io(pred(io, io)).
:- mode io(pred(di, uo) is det) is det.
% Useful shortcuts for writing debugging messages.
%
:- pred format(string, list(io__poly_type)).
:- mode format(in, in) is det.
:- pred format(output_stream, string, list(io__poly_type)).
:- mode format(in, in, in) is det.
:- pred print(T).
:- mode print(in) is det.
:- pred print(output_stream, T).
:- mode print(in, in) is det.
%------------------------------------------------------------------------------%
%------------------------------------------------------------------------------%
:- implementation.
%------------------------------------------------------------------------------%
io(P) :- P(unsafe_io_state, _).
%------------------------------------------------------------------------------%
format(Fmt, Args) :- io(io__format(Fmt, Args)).
format(Stream, Fmt, Args) :- io(io__format(Stream, Fmt, Args)).
%------------------------------------------------------------------------------%
print(T) :- io(io__print(T)).
print(Stream, T) :- io(io__print(Stream, T)).
%------------------------------------------------------------------------------%
:- func unsafe_io_state = io.
:- mode unsafe_io_state = uo is det.
:- pragma foreign_proc(
"C",
unsafe_io_state = (_IO::uo),
[will_not_call_mercury, thread_safe, promise_pure],
""
).
%------------------------------------------------------------------------------%
%------------------------------------------------------------------------------%
More information about the users
mailing list