Challenge yourself with this logic programming quiz focused on Prolog concepts, including predicates, recursion, unification, backtracking, and data representation. Enhance your understanding of Prolog syntax, inference mechanisms, and logical reasoning in declarative programming.
In Prolog, which of the following correctly represents a rule that defines 'ancestor' recursively, given 'parent' facts?
Explanation: The correct answer shows the standard Prolog syntax using ':-' for rules and a recursive definition, which is how ancestor relationships are commonly expressed. The other options are incorrect as they either misuse syntax with '=' or 'if', mix logical connectors improperly, or do not capture recursion accurately. Option B fails to express recursion, and options C and D do not follow Prolog's rule format.
What is the primary role of unification in Prolog’s pattern matching during query resolution?
Explanation: Unification in Prolog binds variables to values so that terms become identical, which is essential for matching queries with facts and rules. Grouping predicates—option B—is more about code organization, not pattern matching. Sorting facts (option C) and operator precedence (option D) are unrelated to the unification process.
When executing the query 'likes(alice, X).' in a Prolog program, how does backtracking affect the search for solutions?
Explanation: Backtracking allows Prolog to systematically explore alternatives for X, finding every possible solution to the query. Returning only the first value (option B) contradicts Prolog’s exhaustive search, unless explicitly stopped. Random selection (option C) is never used in backtracking, and preventing assignment (option D) is the opposite of variable binding in Prolog.
Which statement best describes how lists are represented in Prolog, such as [a, b, c]?
Explanation: Prolog represents lists as recursive data structures using the dot operator (a head and a tail), such as '.'(a, '.'(b, '.'(c, []))). Parentheses (option B) are not used for lists, curly braces (option C) are not valid list syntax, and declaring elements as separate facts (option D) does not represent lists or their structure.
If a Prolog predicate is defined as sibling(X, Y), what is the arity of this predicate and how is it generally written?
Explanation: The predicate sibling(X, Y) takes two arguments, so its arity is 2 and is denoted sibling/2 in Prolog notation. sibling/1 (option B) implies only one argument. sibling-2 (option C) and sibling_two (option D) are not standard ways to indicate arity in Prolog.