I'm trying to generate all pairs X, Y that sum to given number Z using the following predicates:
genN(0).
genN(X) :-
genN(Xprev),
X is Xprev + 1.
sum2(X, Y, Z) :-
X + Y =:= Z.
allSum2(X, Y, Z) :-
genN(X),
X < Z,
genN(Y),
Y < Z,
sum2(X, Y, Z).
I'm using
genN
to generate all natural numbers
sum2
checks if given 3 numbers X, Y, Z
then X + Y = Z
.
Then the logic of allSum2
is to
generate all pairs X and Y such that X
and Y
are smaller than Z
and
to check if they sum up to Z
.
Unfortunately I get stuck in generating infinitely many Ys
and I do not understand why.
Can someone explain it to me?