Slices at the end of recursive predicates in Prolog

pred(Args). pred(Args) :- goalA, goalB, !, pred(Args). pred(Args) :- goalC, goalD, !, pred(Args). 

Normally, I would write a recursive predicate that deals with memory performance, something in accordance with the code snippet above. When using a slice used to attempt to optimize tail call optimization. I recently went through a large prolog code base and found several examples where the reduction actually occurs after a recursive call, and not immediately in front of it. Presumably, this leads to the prevention of tail call optimization, and not to its help.

My question is: can I move a section after a recursive call directly in front of it without affecting the meaning of the program? It is assumed that in each section of the predicate there is a cut in the same relative location.

Now I thought about this a little more, I think the answer is โ€œoptionalโ€, but rewrote all the code with a cut before the call and found that the test suite still passes, Iโ€™m also wondering if there could be some other esoteric reason for writing predicates like this. Or is it just bad coding?

+4
source share
1 answer

I guess it could be bad coding (or a misunderstanding of what the author is doing)

I say this because I made the same mistake myself:

https://mailbox.iai.uni-bonn.de/mailman/public/swi-prolog/2009/001540.html

http://xonix.habrahabr.ru/blog/60430/ (warning in Russian)

But people on the SWI mailing list have confirmed that the tail recursive code path is correct

 head :- <guard goals>, !, <deterministic stuff>, head. head :- ... 
+2
source

All Articles