2
votes

I'm sort of a beginner to Prolog and I have a quick question. I've written a sister_of/2 predicate that goes like this:

sister_of(X, Y) :-
    female(X),
    parents(X, Z, W) == parents(Y,Z,W),
    X \= Y.

I read this as X is a sister of Y if X is a female and the parents of X which are Z W are the same parents of Y, Z W and X and Y are not the same person. This isn't running for some reason though, so it must be a syntax issue, some insight would be fantastic. Thank you.

2

2 Answers

2
votes

Prolog is not a functional language, it's a relational language. You define predicates (i.e. relations), not functions. Thus, your parents(X, Z, W) == parents(Y,Z,W) is just comparing two terms, parents(X, Z, W) and parents(Y,Z,W) for equality. Assuming that a parents/3 predicate is also defined, you want something like:

sister_of(X, Y) :-
    female(X),
    parents(X, Z, W),
    parents(Y, Z, W),
    X \= Y.
1
votes

Well, you began the construction of reasoning alright, but failed during the comparison. Here's what you've missed:

sister_of(X, Y) :-
    female(X),
    parents(X, Z, W),
    parents(Y, Z, W),
    X \= Y.

Now the reasoning goes as follows:

  1. X is a sister of Y;
  2. if X is a female;
  3. if X has parents Z and W;
  4. if Y has parents Z and W;
  5. and, of course, X is not Y, so that X is not a sister of itself.

Notice that, the comparison you performed earlier is not necessary (and in fact, does not mean what you expected), since Z and W become limited to be the parents of X in step 3. This means that, Z and W already have a determined value after step 3 -- they are bound, in other words.

After that, in step 4, Y can only assume the same value of X, or some other value that makes parents(Y, Z, W) true. Finally, you remove the cases where X == Y in step 5, which gives you a valid definition of sister_of(X, Y).