Given the following set of Prolog clauses: father(X, Y) :- parent(X, Y), male(X). parent(sally, bob).…
Prolog evaluates the rule body parent(X, Y), male(X) from left to right and searches clauses from top to bottom. For the query father(X, jane), the first subgoal parent(X,…
Choose your answer
Given the following set of Prolog clauses:
father(X, Y) :-
parent(X, Y),
male(X).
parent(sally, bob).
parent(jim, bob).
parent(alice, jane).
parent(thomas, jane).
male(bob).
male(jim).
male(thomas).
female(sally).
female(alice).How many atoms are matched to the variable X and rejected on backtracking before the query father(X, jane) reports a successful result?
Answer and explanation
Correct answer A
Prolog evaluates the rule body parent(X, Y), male(X) from left to right and searches clauses from top to bottom. For the query father(X, jane), the first subgoal parent(X, jane) initially matches parent(alice, jane), binding X = alice. Next, male(alice) is tested and fails, causing Prolog to reject alice and backtrack to parent(thomas, jane), where X = thomas satisfies male(thomas) and succeeds. Thus, exactly 1 atom (alice) is matched to X and rejected before the successful result is reported.
Practice for these exams
-
UGC NET Computer Science and Applications