Consider following rules: 1) append([], Y, Y). 2) append([H|X], Y, [H|Z]) :- append(X, Y, Z). 3) prefix(X, Z) :- append(X, Y, Z). 4) suffix(Y, Z) :- append(X, Y, Z). and the query suffix([c, d, e], [a, b, c, d, e]). which we can easily see is 'YES' since the first list is indeed a suffix of the second list. Now we will trace it. Rules 1, 2, and 3 definitely do not apply. Let's try rule 4, plugging in what we know for the given variables: 4) suffix([c, d, e], [a, b, c, d, e]) :- append(X, [c, d, e], [a, b, c, d, e]). by letting Y = [c, d, e] and Z = [a, b, c, d, e] So our suffix query is true if the right hand side (the append) is true. Now we need to test the append clause the same way. Rule 1 does not apply since [c, d, e] is not equal to [a, b, c, d, e], though it would be possible to make X = []. Let's try rule 2, again plugging in what we know for the given variables: 2) append([a|X], [c, d, e], [a|b, c, d, e]) :- append(X, [c, d, e], [b, c, d, e]). by letting H = a, Y = [c, d, e], and Z = [b, c, d, e] So our left hand side append is true if the right hand side append is true. Now we need to test the right hand side append. Rule 1 does not apply since [c, d, e] is not equal to [b, c, d, e], though it would be possible to make X = []. Let's try rule 2, again plugging in what we know for the given variables: 2) append([b|X], [c, d, e], [b|c, d, e]) :- append(X, [c, d, e], [c, d, e]). by letting H = b, Y = [c, d, e], and Z = [c, d, e] So our left hand side append is true if the right hand side append is true. Now we need to test the right hand side append. Guess what, rule 1 applies! Here it is, again plugging in what we know for the given variables: 1) append([], [c, d, e], [c, d, e]). by letting X = [] and Y = [c, d, e] This means the preceding two appends are true and ultimately the original suffix is true also, and we get the answer 'yes'.