4 ms·
Nope, in vanilla Prolog you'd have to type "X(A,C) :- X(A,B), X(B,C)." for every single X you want to be transitive. In formal logic, "A if B" is just syntacti
by sp332 15y ago
Nope, in vanilla Prolog you'd have to type "X(A,C) :- X(A,B), X(B,C)." for every single X you want to be transitive.
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):
transitive(X, A, B, C) :- (X(A,B), X(B,C), X(A,C)); not(X(A,B), X(B,C)).
- JadeNB 15y agoProlog 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.
- deleted 15y ago[deleted]
- jimwise 15y agothough 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) * ...
- swannodette 15y agoSuch behavior is easily avoided with tabling.
- jimwise 15y agoThere are a couple of ways around this -- all of which fall under "be careful". :-)