Given the following set of prolog clauses: father(X,Y) : parent(X,Y), male(X),…
2015
Given the following set of prolog clauses:
father(X,Y) :
parent(X,Y),
male(X),
parent(Sally, Bob),
parent(Jim, Bob),
parent(Alice, Jane),
male(Bob),
male(Jim),
female(Salley),
female(Alice)
How many atoms are matched to the variable 'X' before the query father(X, Jane) reports a Result?
- A.
1
- B.
2
- C.
3
- D.
4
Attempted by 35 students.
Show answer & explanation
Correct answer: A
Key rule: father(X, Y) :- parent(X, Y), male(X).
Step-by-step reasoning:
Prolog first looks for parent facts with the second argument = Jane. The facts given are parent(Sally, Bob), parent(Jim, Bob), and parent(Alice, Jane). Only parent(Alice, Jane) matches.
So Prolog sets X = Alice and then evaluates the second part of the rule, male(X), i.e. male(Alice).
male(Alice) fails because Alice is declared female. Therefore the father predicate has no successful solution for Y = Jane, but X was matched once during the attempt.
Answer: 1 — Prolog matches X = Alice once before reporting that there is no father for Jane.