you miss the else branch of X>1
, and if you remove it, you get a result:
...
sum(N,M,R) :- X is M-1, sum(N,X,R1), pow(N,X,R2), R is (R1+R2).
...
?- sum(2,3,X).
X = 15 ;
^C
but the program doesn't terminate (^C used to interrupt the loop).
I would rewrite using an accumulator, to get the better efficiency allowed by LCO (Last Call Optimization) and the built in pow:
sum(N,M,R) :- sum(N,M,0,R).
sum(N,M,A,R) :-
( M > 0
-> A1 is A + N^M, %% N**M,
M1 is M-1,
sum(N,M1,A1,R)
; R is A + 1
).
edit SWI-Prolog documentation about operator (**)/2 is incorrect: better to use (^)/2, as remarked by @false