Skip to content

Instantly share code, notes, and snippets.

@ee08b397
Last active August 29, 2015 14:26
Show Gist options
  • Save ee08b397/0b4965650dc5b3c643da to your computer and use it in GitHub Desktop.
Save ee08b397/0b4965650dc5b3c643da to your computer and use it in GitHub Desktop.
execution order does matter for prolog, top-down; ancestor22 can cause infinite recursion
/*
a
/ \
b d
|
c
|
e
*/
parent(a, b).
parent(a, d).
parent(b, c).
parent(c, e).
ancestor11(X, X).
ancestor11(X, Y) :- parent(Z, Y), ancestor11(X, Z).
ancestor12(X, Y) :- parent(Z, Y), ancestor12(X, Z).
ancestor12(X, X).
ancestor21(X, X).
ancestor21(X, Y) :- ancestor21(X, Z), parent(Z, Y).
ancestor22(X, Y) :- ancestor22(X, Z), parent(Z, Y).
ancestor22(X, X).
ancestor31(X, Y) :- parent(X,Y).
ancestor31(X, Y) :- parent(X, Z), ancestor31(Z, Y).
ancestor32(X, Y) :- parent(X, Z), ancestor32(Z, Y).
ancestor32(X, Y) :- parent(X,Y).
@mullachv
Copy link

Why do you have ancestor(X,X)? That is not correct anyway

@ee08b397
Copy link
Author

you're right, but the point here for 21 and 22 are demonstration of the order, and possible infinite recursion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment