As Jeff observed, the problem is the rule:
add( A, B, C ) :- add( B, A, C ).
In general, this is a rule that expresses something that you want to be true, but that doesn't help you solve a goal. The query add(1,2,X)
by this rule leads to the subquery add(2,1,X)
, which leads to the subquery add(1,2,X)
: SLD resoltuion can spend forever on this branch (if it has no other rules of higher priority, and it doesn't spot that the rule doesn't make progress) without getting anywhere. You should only use such rule with conditions (e.g. strictlylessthan (B,A)
) that ensure the rule is only applicable when it can do useful work. The issue of these kinds of rules is a reason why Prolog is not really a declarative language.
To regain commutativity, you'll need to add the rul:
add (0, [A], A).
Your add predicate is kind of odd: add([1],0,1)
is true, as is add([1],[0],1)
, but add([0],1,1)
is not, nor is add([1],[0],[1])
. add
is perfectly meaningful from a computational point of view, but is it really what you want?