Picat Logic Programming and Unification 5 — Questions and Answers
Question 1: In Picat, what is the difference between `=` and `is` for the expression `X = 2 + 3`?
- Both bind X to 5
- = unifies X with the unevaluated term 2+3; is evaluates it and binds X to 5 (Correct answer)
- is unifies structurally; = evaluates
- Both fail unless X is already bound
Correct answer: = unifies X with the unevaluated term 2+3; is evaluates it and binds X to 5
= performs structural unification, so X = 2+3 binds X to the compound term +(2,3), while X is 2+3 evaluates and binds X to 5.
Question 2: When Picat's unification encounters `f(X,X) = f(1,2)`, what is the result?
- X = 1 on the first occurrence, X = 2 on the second
- Unification fails because X cannot be both 1 and 2 (Correct answer)
- X = [1,2]
- X = 1 and the second argument is ignored
Correct answer: Unification fails because X cannot be both 1 and 2
Since X appears twice in the pattern, both arguments must unify to the same value; 1 ≠ 2, so unification fails.
Question 3: In Picat logic programming, what does 'resolution' refer to?
- The screen pixel density of the debugger
- The inference step that matches a goal with a clause head and produces new subgoals (Correct answer)
- The process of sorting a list of answers
- The mechanism for catching exceptions
Correct answer: The inference step that matches a goal with a clause head and produces new subgoals
Resolution is the core inference step: a goal is matched against a clause head via unification, and the body of that clause becomes new subgoals.
Question 4: Which of the following correctly defines a Picat logic predicate for list membership?
- member(X,[X|_]). member(X,[_|T]) :- member(X,T). (Correct answer)
- member(X,L) => X in L.
- member(X,L) := foreach(E in L, E==X).
- member(X,L) :- L contains X.
Correct answer: member(X,[X|_]). member(X,[_|T]) :- member(X,T).
The two-clause recursive definition with a base case (head match) and recursive case (check tail) is the standard logic programming idiom for membership.
Question 5: In Picat, what does the `\=` operator test?
- Arithmetic inequality
- That two terms cannot be unified (unification would fail) (Correct answer)
- Logical negation of a predicate call
- Structural inequality after evaluation
Correct answer: That two terms cannot be unified (unification would fail)
X \= Y succeeds if X and Y cannot be unified; it is equivalent to \+(X = Y) and does not bind variables.
Question 6: What is the 'depth-first search' strategy in Picat's logic programming execution model?
- Picat tries all clauses simultaneously and returns the fastest
- Picat fully explores one clause branch before trying alternatives (Correct answer)
- Picat uses breadth-first search by default
- Picat uses a heuristic to choose the best clause
Correct answer: Picat fully explores one clause branch before trying alternatives
Picat (like Prolog) uses depth-first search: it commits to the first matching clause and explores it fully before backtracking to try alternatives.
Question 7: In Picat, which combination of goals demonstrates disjunction in a logic predicate body?
- goal1, goal2
- goal1 ; goal2 (Correct answer)
- goal1 | goal2
- goal1 or goal2
Correct answer: goal1 ; goal2
The semicolon (;) is the disjunction operator in Picat logic predicates, succeeding if either goal1 or goal2 succeeds.
In Picat, what is the difference between `=` and `is` for the expression `X = 2 + 3`?