I swear this has worked for me before, I must be missing something. I’m trying to build a predicate that uses tail recursion to keep repeating without growing the stack, but mine keeps growing it.
I thought SWI Prolog would optimize the case below but it doesn’t seem to be…
stack_level(Level) :- % See if the stack is growing by getting the stack level prolog_current_frame(Me), prolog_frame_attribute(Me, level, Level). startTest :- assert(a(1)), assert(a(1)), assert(a(1)). stackTest :- stack_level(Level), writeln(Level), a(X), retract(a(X)), !, stackTest.
?- startTest,stackTest. 12 14 15 16 false.