이 추적 메타 인터프리터가 있습니다. 그것은 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.
그것은 한 가지를 제외하고 잘 작동하고 있습니다. 함수 just_do_it(In, Out, Query)
에있는 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)
속편 : http://stackoverflow.com/questions/27235148/implementing-cut-in-tracing-meta-interpreter-prolog/ – false