Prolog 中递归谓词末尾的剪切
pred(Args).
pred(Args) :-
goalA,
goalB,
!,
pred(Args).
pred(Args) :-
goalC,
goalD,
!,
pred(Args).
通常我会编写一个与内存性能相关的递归谓词,类似于上面的代码片段。使用剪切来尝试强制进行尾部调用优化。我最近浏览了一个大型的 prolog 代码库,并发现了一些示例,其中剪切实际上是在递归调用之后而不是紧接在递归调用之前。据推测,这会阻止尾调用优化的发生,而不是帮助它。
我的问题是我可以将递归调用之后的剪切移到递归调用之前,而不影响程序的含义吗?这是假设谓词的每个子句的相同相对位置都有一个切口。
现在我又想了一些,我想也许答案是“不一定”,但是在调用之前用剪切重写了所有代码并发现测试套件仍然通过,我也想知道是否还有其他深奥的原因来编写这样的谓词。或者只是糟糕的编码?
pred(Args).
pred(Args) :-
goalA,
goalB,
!,
pred(Args).
pred(Args) :-
goalC,
goalD,
!,
pred(Args).
Normally I would write a recursive predicate that was concerned with memory performance something along the lines of the above snippet of code. With the cut being used to try to force tail call optimization to occur. I have recently been going through a large prolog code base and have found some examples where the cut is actually after the recursive call rather than immediately before it. Presumably this has the effect of preventing tail call optimization from happening rather than assisting it.
My question is can I move the cut from after the recursive call to immediately prior to it without affecting the meaning of the program? This is assuming that there is a cut in the same relative location for each clause of the predicate.
Now I've been thinking about it some more, I'm thinking maybe the answer is "not necessarily", but having rewritten all the code with the cut before the call and finding that the test suite is still passing, I'm also wondering if there might be some other esoteric reason for writing predicates like this. Or is it just bad coding?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是,这可能是错误的编码(或误解了作者在做什么)
我这么说是因为我自己也犯过同样的错误:
https://mailbox.iai.uni-bonn.de/mailman/public/swi-prolog/2009/001540。 html
http://xonix.habrahabr.ru/blog/60430/ (警告,俄语)
但是来自 SWI 邮件列表的人确认尾递归代码的正确方法是
My guess is that it might be bad coding (or misunderstanding what author was doing)
I'm saying this, because I, myself, did once the same error:
https://mailbox.iai.uni-bonn.de/mailman/public/swi-prolog/2009/001540.html
http://xonix.habrahabr.ru/blog/60430/ (warning, in russian)
But people from SWI mailing-list confirmed that right way of tail-recursive code is