[英]how to trace a predicate in prolog?
This is a predicate that get permutations of a list. 这是获取列表排列的谓词。 Can somebody explain to me how to trace this predicate?
有人可以向我解释如何追踪该谓词吗? I am using SWI.
我正在使用SWI。
perm([H|T],L) :- perm(T,P) , insert(H,P,L).
perm([],[]).
insert(X,L,[X|L]).
insert(X,[H|T],[H|T1]) :- insert(X,T,T1).
Here's an example of using trace
in SWI Prolog. 这是在SWI Prolog中使用
trace
的示例。
Entering the code: 输入代码:
?- [user].
|: perm([H|T],L) :- perm(T,P) , insert(H,P,L).
|: perm([],[]).
|:
|: insert(X,L,[X|L]).
|: insert(X,[H|T],[H|T1]) :- insert(X,T,T1).
|: % user://1 compiled 0.01 sec, 6 clauses
true.
Running a trace. 运行跟踪。 Press "Enter" at the question marks
?
在问号处按“ Enter”
?
to "creep" (take a step): “爬行”(采取步骤):
?- trace.
true.
[trace] ?- perm([1,2,3], L).
Call: (6) perm([1, 2, 3], _G366) ? creep
Call: (7) perm([2, 3], _G445) ? creep
Call: (8) perm([3], _G445) ? creep
Call: (9) perm([], _G445) ? creep
Exit: (9) perm([], []) ? creep
Call: (9) insert(3, [], _G446) ? creep
Exit: (9) insert(3, [], [3]) ? creep
Exit: (8) perm([3], [3]) ? creep
Call: (8) insert(2, [3], _G449) ? creep
Exit: (8) insert(2, [3], [2, 3]) ? creep
Exit: (7) perm([2, 3], [2, 3]) ? creep
Call: (7) insert(1, [2, 3], _G366) ? creep
Exit: (7) insert(1, [2, 3], [1, 2, 3]) ? creep
Exit: (6) perm([1, 2, 3], [1, 2, 3]) ? creep
L = [1, 2, 3]
As you might expect, this trace shows that perm
calls itself recursively until it gets to the empty tail ( []
) of the input list [1,2,3]
. 如您所料,此跟踪显示
perm
递归调用自身,直到到达输入列表[1,2,3]
的空尾( []
)为止。 It then shows calls to insert
which follow those recursive calls, along with the arguments occurring in those calls. 然后,它显示在这些递归调用之后的
insert
调用,以及这些调用中出现的参数。 The _Gnnn
variables are uninstantiated arguments on a Call
that gets instantiated in the clause which you see on the Exit
. _Gnnn
变量是Call
上未实例化的参数,该Call
在Exit
上看到的子句中实例化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.