Prolog also has syntax for if-then, so
x(A, C), x(A, B), x(B, C); not(x(A, C)).
can essentially be re-written as x(A, C) -> x(A, B), x(B, C); true.
(This saves testing `x(A, C)` twice. Also, `->` implicitly cuts, presenting backtracking over `x(A, C)`; but that shouldn't matter here.) However, this set-up mistakenly has `x(A, C)` as the antecedent; I think you actually want it as the consequent.though be careful how you do this -- it's easy to end up with a divergent program, as prolog tries to:
* prove X(A,B)
* realizes that it can prove this if it can prove transitive(X, A, B, C), and then prove X(B,A)
* realizes that it can prove this if it can prove transitive(X, B, A, C), and then prove X(A,B)
* ...
Re:A Short Introduction to Prolog
by swannodette on Friday August 05, 2011 @12:12PM
(#2850558)
Such behavior is easily avoided with tabling.
In formal logic, "A if B" is just syntactic sugar for "(B && A) || (!B)" or in English: when B is true, A is true. (But when B is false, A could be either true or false.) So you could rewrite your expression as (edit: fixed):