Files
mercury/tests/debugger/mutrec.m
Zoltan Somogyi 59b11f84ce Update the debugger test directory.
Replace __ with . as the module qualifier symbol.

Replace references to io.state with just io.

Replace DCGs with state variables.

Replace (C->T;E) syntax for if-then-elses with (if C then T else E) syntax.

Replace if-then-elses with switches when possible and where this does not
affect what is being tested.

Replace separate pred and mode declarations with predmode declarations.

Put predicate and function declarations just before the definition
of the predicate or function.

Delete unneeded module qualifications on predicate and function declarations
and definitions.

Update .exp files (and if needed, .inp files) for the line number changes
that result from the above.

For tests that have more than one .exp file and where one of those files
is affected by the above, add a section to the source file header that says
which .exp file is for what grade, with XXXs for the (as yet) unknown parts.
2018-08-28 21:20:59 +10:00

284 lines
6.0 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ts=4 sw=4 et ft=mercury
%---------------------------------------------------------------------------%
:- module mutrec.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
%---------------------------------------------------------------------------%
:- import_module int.
:- import_module list.
:- type step
---> self
; mut(int)
; down.
main(!IO) :-
Steps1 = [self, mut(1), mut(3), self, mut(2), mut(2), self, down,
mut(1), mut(3), mut(3), mut(3), mut(3), self, self, down,
self, mut(2), mut(2), self, mut(3), self, down],
Steps2 = [self, mut(1), mut(3), self, mut(2), mut(2), self, down,
mut(1), mut(3), mut(3), mut(3), mut(3), self, self, down,
self, self, self, self, mut(3), self, down],
test(Steps1, RevStrs1),
test(Steps2, RevStrs2),
test_output(RevStrs1, !IO),
test_output(RevStrs2, !IO).
:- pred test(list(step)::in, list(string)::out) is det.
test(Steps, RevStrs) :-
p1(Steps, [], RevStrs).
:- pred test_output(list(string)::in, io::di, io::uo) is det.
test_output(RevStrs, !IO) :-
list.reverse(RevStrs, Strs),
(
Strs = [],
io.write_string("[]\n", !IO)
;
Strs = [HeadStr | TailStrs],
io.write_string("[", !IO),
io.write_string(HeadStr, !IO),
write_comma_strings(TailStrs, !IO),
io.write_string("]\n", !IO)
).
:- pred write_comma_strings(list(string)::in, io::di, io::uo) is det.
write_comma_strings([], !IO).
write_comma_strings([Str | Strs], !IO) :-
io.write_string(", ", !IO),
io.write_string(Str, !IO),
write_comma_strings(Strs, !IO).
%---------------------------------------------------------------------------%
:- pred p1(list(step)::in, list(string)::in, list(string)::out) is det.
p1([], A, A).
p1([Step | Steps], A, R) :-
B = ["p1" | A],
(
Step = self,
p1(Steps, B, R)
;
Step = mut(N),
( if N = 2 then
p2(Steps, B, R)
else
p3(Steps, B, R)
)
;
Step = down,
q1(Steps, B, R)
).
:- pred p2(list(step)::in, list(string)::in, list(string)::out) is det.
p2([], A, A).
p2([Step | Steps], A, R) :-
B = ["p2" | A],
(
Step = self,
p2(Steps, B, R)
;
Step = mut(N),
( if N = 1 then
p1(Steps, B, R)
else
p3(Steps, B, R)
)
;
Step = down,
q1(Steps, B, R)
).
:- pred p3(list(step)::in, list(string)::in, list(string)::out) is det.
p3([], A, A).
p3([Step | Steps], A, R) :-
B = ["p3" | A],
(
Step = self,
p3(Steps, B, R)
;
Step = mut(N),
( if N = 1 then
p1(Steps, B, R)
else
p2(Steps, B, R)
)
;
Step = down,
q1(Steps, B, R)
).
%---------------------------------------------------------------------------%
:- pred q1(list(step)::in, list(string)::in, list(string)::out) is det.
q1([], A, A).
q1([Step | Steps], A, R) :-
B = ["q1" | A],
(
Step = self,
q1(Steps, B, R)
;
Step = mut(N),
( if N = 2 then
q2(Steps, B, R)
else
q3(Steps, B, R)
)
;
Step = down,
r0a(Steps, B, R)
).
:- pred q2(list(step)::in, list(string)::in, list(string)::out) is det.
q2([], A, A).
q2([Step | Steps], A, R) :-
B = ["q2" | A],
(
Step = self,
q2(Steps, B, R)
;
Step = mut(N),
( if N = 1 then
q1(Steps, B, R)
else
q3(Steps, B, R)
)
;
Step = down,
r0b(Steps, B, R)
).
:- pred q3(list(step)::in, list(string)::in, list(string)::out) is det.
q3([], A, A).
q3([Step | Steps], A, R) :-
B = ["q3" | A],
(
Step = self,
q3(Steps, B, R)
;
Step = mut(N),
( if N = 1 then
q1(Steps, B, R)
else
q2(Steps, B, R)
)
;
Step = down,
r0c(Steps, B, R)
).
%---------------------------------------------------------------------------%
:- pred r0a(list(step)::in, list(string)::in, list(string)::out) is det.
r0a(Steps, A, R) :-
B = ["r0a" | A],
r0b(Steps, B, R).
:- pred r0b(list(step)::in, list(string)::in, list(string)::out) is det.
r0b(Steps, A, R) :-
B = ["r0b" | A],
r0c(Steps, B, R).
:- pred r0c(list(step)::in, list(string)::in, list(string)::out) is det.
r0c(Steps, A, R) :-
B = ["r0c" | A],
r1(Steps, B, R).
:- pred r1(list(step)::in, list(string)::in, list(string)::out) is det.
r1([], A, A).
r1([Step | Steps], A, R) :-
B = ["r1" | A],
(
Step = self,
r1(Steps, B, R)
;
Step = mut(N),
( if N = 2 then
r2(Steps, B, R)
else
r3(Steps, B, R)
)
;
Step = down,
R = B
).
:- pred r2(list(step)::in, list(string)::in, list(string)::out) is det.
r2([], A, A).
r2([Step | Steps], A, R) :-
B = ["r2" | A],
(
Step = self,
r2(Steps, B, R)
;
Step = mut(N),
( if N = 1 then
r1(Steps, B, R)
else
r3(Steps, B, R)
)
;
Step = down,
s(1, B, R)
).
:- pred r3(list(step)::in, list(string)::in, list(string)::out) is det.
r3([], A, A).
r3([Step | Steps], A, R) :-
B = ["r3" | A],
(
Step = self,
r3(Steps, B, R)
;
Step = mut(N),
( if N = 1 then
r1(Steps, B, R)
else
r2(Steps, B, R)
)
;
Step = down,
s(3, B, R)
).
%---------------------------------------------------------------------------%
:- pred s(int::in, list(string)::in, list(string)::out) is det.
s(N, A, R) :-
B = ["s" | A],
( if N > 0 then
s(N-1, B, R)
else
R = B
).
%---------------------------------------------------------------------------%