Picat Pattern Matching Rules 3 — Questions and Answers
Question 1: What is the result of matching the pattern `f(X, X)` against the term `f(3, 4)` in Picat?
- Match fails because X cannot unify with both 3 and 4 (Correct answer)
- X is bound to 3 and the second argument is ignored
- X is bound to 4
- Match succeeds with X unbound
Correct answer: Match fails because X cannot unify with both 3 and 4
When the same variable appears twice in a pattern, both positions must unify to the same value; since 3 ≠ 4, the match fails.
Question 2: In Picat, which rule type is searched in order and allows multiple clauses to be tried on backtracking?
- Non-deterministic rules using `?=>` (Correct answer)
- Deterministic rules using `=>`
- Function rules using `=`
- Constraint rules using `#=>`
Correct answer: Non-deterministic rules using `?=>`
Non-deterministic rules (`?=>`) allow Picat to backtrack and try subsequent clauses when the current one fails.
Question 3: How do you write an as-pattern in Picat to bind the whole list while also matching its head?
- L@[H|_] (Correct answer)
- L=[H|_]
- H@L
- [H|_] as L
Correct answer: L@[H|_]
The `@` syntax (e.g., `L@[H|_]`) creates an as-pattern, binding L to the full term while destructuring it simultaneously.
Question 4: In Picat, if two clauses have the same head pattern and neither has a guard, which one executes?
- The first textually defined clause (Correct answer)
- The last textually defined clause
- Both execute in parallel
- A compiler error is raised
Correct answer: The first textually defined clause
Picat searches clauses top-to-bottom; the first matching clause wins (and with `=>` commits, preventing backtracking to the second).
Question 5: What does the pattern `{X, Y, Z}` match in Picat?
- A tuple with exactly three elements (Correct answer)
- A set containing X, Y, and Z
- A list of three elements
- A struct named tuple
Correct answer: A tuple with exactly three elements
Curly braces in Picat denote tuples, so `{X, Y, Z}` matches a three-element tuple and binds each position.
Question 6: Which Picat construct allows pattern matching with conditions in a single expression rather than separate clauses?
- The `cond` expression with `=>` branches (Correct answer)
- The `switch` statement
- The `case` keyword
- The `match` macro
Correct answer: The `cond` expression with `=>` branches
Picat's `cond` expression evaluates branches with pattern-like conditions using `=>`, enabling inline conditional dispatch.
Question 7: When a Picat predicate clause head contains a compound term like `node(Left, Val, Right)`, what does matching against `node(_, 5, _)` verify?
- That the second field of the node structure equals 5, ignoring the other fields (Correct answer)
- That the structure has exactly one field equal to 5
- That Left and Right are both 5
- That the entire term equals 5
Correct answer: That the second field of the node structure equals 5, ignoring the other fields
Anonymous variables `_` match anything, so `node(_, 5, _)` succeeds only when the middle field unifies with 5.
What is the result of matching the pattern `f(X, X)` against the term `f(3, 4)` in Picat?