繁体   English   中英

如何在序言中追溯谓词?

[英]how to trace a predicate in prolog?

这是获取列表排列的谓词。 有人可以向我解释如何追踪该谓词吗? 我正在使用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).

这是在SWI Prolog中使用trace的示例。

输入代码:

?- [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.

运行跟踪。 在问号处按“ Enter” ? “爬行”(采取步骤):

?- 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]

如您所料,此跟踪显示perm递归调用自身,直到到达输入列表[1,2,3]的空尾( [] )为止。 然后,它显示在这些递归调用之后的insert调用,以及这些调用中出现的参数。 _Gnnn变量是Call上未实例化的参数,该CallExit上看到的子句中实例化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM