我有这个追踪元解释器。它是为 swi-prolog 编写的。
trace(Goal):-
trace(Goal, 0).
trace(true, _Depth):-!, true.
trace(fail, _Depth):-!, fail.
trace(A > B, _Depth):-!, A > B.
trace(A < B, _Depth):-!, A < B.
trace(A <= B, _Depth):-!, A <= B.
trace(A >= B, _Depth):-!, A >= B.
trace(A = B, _Depth):-!, A = B.
trace(A is B, _Depth):-!, A is B.
trace(!, _Depth):-!, fail.
trace(not(A), _Depth):-!, not(A).
trace((Goal1, Goal2), Depth):-!,
trace(Goal1, Depth),
trace(Goal2, Depth).
trace(Goal, Depth):-
display('Call: ', Goal, Depth),
clause(Goal, Body),
Depth1 is Depth + 1,
trace(Body, Depth1),
display('Exit: ', Goal, Depth),
display_redo(Goal, Depth).
trace(Goal, Depth):-
display('Fail: ', Goal, Depth),
fail.
display(Message, Goal, Depth):-
tab(Depth), write(Depth), write(': '), write(Message),
write(Goal), nl.
display_redo(Goal, Depth):-
true
;
display('Redo: ', Goal, Depth),
fail.
just_do_it(In, Out, Query):-
consult(In),
tell(Out),
call_with_depth_limit(findall(Query, trace(Query), _Solutions), 30, _XMessage),
writeln(_XMessage),
writeln(_Solutions),
told,
unload_file(In),
true.
它工作正常,除了一件事外它应该如何工作。当我Query
在函数中有变量时just_do_it(In, Out, Query)
,输出是绑定变量。有没有办法在跟踪的进一步步骤中取消绑定变量,以便我可以看到它何时被绑定,何时不被绑定?
示例输出:
0: Call: a(_G1085,_G1085,_G1087)
0: Exit: a(3,3,7)
0: Redo: a(3,3,7)
1: Call: b(_G1087,_G1085)
1: Exit: b(7,3)
1: Redo: b(7,3)
1: Exit: b(5,1)
1: Redo: b(5,1)
1: Fail: b(_G1087,_G1085)
0: Fail: a(_G1085,_G1085,_G1087)
我希望拥有的示例输出:
0: Call: a(_G1085,_G1085,_G1087)
0: Exit: a(3,3,7)
0: Redo: a(_G1085,_G1085,_G1087)
1: Call: b(_G1087,_G1085)
1: Exit: b(7,3)
1: Redo: b(_G1087,_G1085)
1: Exit: b(5,1)
1: Redo: b(_G1087,_G1085)
1: Fail: b(_G1087,_G1085)
0: Fail: a(_G1085,_G1085,_G1087)