fix tail recursion for logic programs