Picat Logic Programming and Unification 2 — Questions and Answers
Question 1: In Picat, what happens when unification of two terms fails during pattern matching in a clause head?
- The program throws a runtime exception
- Picat backtracks and tries the next matching clause (Correct answer)
- The variable is bound to a default value
- The predicate succeeds with a partial match
Correct answer: Picat backtracks and tries the next matching clause
When a clause head fails to unify with the call, Picat backtracks and attempts the next clause whose head can unify.
Question 2: Which Picat operator is used to check structural equality without performing unification?
- =
- == (Correct answer)
- :=
- =..
Correct answer: ==
The == operator tests if two terms are identical in structure and variable bindings without unifying them.
Question 3: In Picat logic predicates, what does the goal `X = f(Y), Y = 3` result in for X?
- X = f(Y) (Y unbound)
- X = f(3) (Correct answer)
- A unification error
- X = 3
Correct answer: X = f(3)
After Y unifies with 3, the earlier binding X = f(Y) propagates so X becomes f(3).
Question 4: What is the role of the anonymous variable `_` in Picat unification?
- It matches only atoms
- It matches any term and is never shared across occurrences (Correct answer)
- It matches only numbers
- It is an alias for nil
Correct answer: It matches any term and is never shared across occurrences
Each occurrence of _ is a distinct fresh variable that matches anything but shares no binding with other _ occurrences.
Question 5: Which of the following Picat goals will succeed?
- f(1,2) = f(2,1)
- f(X,X) = f(1,2)
- f(X,Y) = f(1,2) (Correct answer)
- f(1) = f(1,2)
Correct answer: f(X,Y) = f(1,2)
f(X,Y) = f(1,2) succeeds by binding X=1 and Y=2; the other options fail due to structural or value mismatches.
Question 6: In Picat, what does the `not` (or `\+`) operator do in a logic predicate context?
- Negates arithmetic expressions
- Succeeds if the goal cannot be proved (negation as failure) (Correct answer)
- Forces backtracking unconditionally
- Cuts all choice points
Correct answer: Succeeds if the goal cannot be proved (negation as failure)
\+ Goal succeeds if Goal fails (negation as failure), with no side-effect bindings retained.
Question 7: What is the occurs check, and does Picat's default unification perform it?
- Checks if a variable occurs in a term before binding; yes, always
- Prevents binding X to a term containing X to avoid circular structures; no, not by default (Correct answer)
- Verifies that all variables are ground; yes, always
- Checks argument count; no, arity is implicit
Correct answer: Prevents binding X to a term containing X to avoid circular structures; no, not by default
The occurs check prevents creating circular (infinite) terms by checking X ∉ T before X=T, but Picat (like most Prolog-family systems) skips it by default for performance.
In Picat, what happens when unification of two terms fails during pattern matching in a clause head?