Print debugging statement: Difference between revisions

add Mercury
(Added Perl example)
(add Mercury)
Line 117:
"q" at line 35 type '*main.point'
value: &main.point{x:2, y:3}
</pre>
 
=={{header|Mercury}}==
 
Mercury has [https://mercurylang.org/information/doc-latest/mercury_ref/Trace-goals.html trace goals] which can be used within pure code, can summon <code>!IO</code> for use within the goal, can be made conditional off of compile-time or runtime flags, and which are pretty free with what they can do.
 
Together with data functors like <code>$module</code>, <code>$pred</code>, <code>$line</code>, trace goals can be used for debugging print statements as in the following example. Together with the [https://mercurylang.org/information/doc-latest/mercury_library/require.html require] module's utilities, trace goals can be used for assert() statements or pre/post assertions.
 
<lang Mercury>:- module add.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module int, io, string, list.
 
:- func add(int, int) = int.
A `add` B = C :-
C = A + B,
trace [io(!IO), compile_time(grade(debug))] (
io.format("%s - %s(%d): %d `add` %d = %d\n",
[s($grade), s($pred), i($line), i(A), i(B), i(C)], !IO)
).
 
main(!IO) :-
2 `add` 7 = N,
io.print_line(N, !IO).</lang>
 
{{out}} (with a non-debug grade)
<pre>
9
</pre>
 
{{out}} (with a debug grade)
<pre>
asm_fast.par.gc.debug.stseg - function `add.add'/2(13): 2 `add` 7 = 9
9
</pre>
 
Anonymous user