Why does this work: power(_,0,1) :- !. power(X,Y,Z) :- Y1 is Y - 1, power(X,Y1,Z1), Z is X * Z1. And this gives a stack overflow exception?