In Prolog, I would like to implement a trivial logical induction predicate i
as (1):
i(0).
i(N) :- i(N-1).
or (2):
i(0).
i(N+1) :- i(N).
But this doesn't work; with (1), query i(3).
causes a stack overflow; with (2), i(3).
returns false.
Instead, I have to do (3):
i(0).
i(N) :- M is N-1, i(M).
Naively, the difference between (3) and (1) seems like a trivial syntactic shift, and declaring intermediate variables feels like a hassle in bigger programs. What's the reason behind the Prolog compiler rejecting (1) and (2), does a work-around exist, and why do (1) and (2) behave differently?