Picat General 4 — Questions and Answers
Question 1: What is a 'guard' in a Picat rule, and where does it appear?
- A boolean condition placed between the head and => that must hold for the rule to fire (Correct answer)
- A constraint posted in the body after solve
- A type annotation on arguments
- An exception handler clause
Correct answer: A boolean condition placed between the head and => that must hold for the rule to fire
A guard is an optional boolean test in the rule head, written before `=>`, that refines when a particular clause applies.
Question 2: Which Picat predicate retrieves all solutions of a goal as a list?
- findall(Template, Goal, List) (Correct answer)
- bagof(Template, Goal, List)
- collect(Goal, List)
- all_solutions(Goal, List)
Correct answer: findall(Template, Goal, List)
`findall/3` collects every Template instantiation for which Goal succeeds into List, returning [] if Goal always fails.
Question 3: In Picat, what does the `@` symbol signify when used in a term like `[H|T]@List`?
- It is not standard syntax; Picat uses [H|T] without @
- It aliases the whole list to List while also destructuring (Correct answer)
- It represents string concatenation
- It marks a lazy evaluation thunk
Correct answer: It aliases the whole list to List while also destructuring
Picat supports as-patterns with `@`, so `[H|T]@List` matches a non-empty list, binding H to the head, T to the tail, and List to the entire structure simultaneously.
Question 4: What distinguishes a Picat `function` (using = in the head) from a `predicate`?
- A function must return exactly one value via the last argument position, while a predicate succeeds or fails (Correct answer)
- Functions use backtracking; predicates do not
- Functions are compiled to C; predicates are interpreted
- There is no distinction — they compile identically
Correct answer: A function must return exactly one value via the last argument position, while a predicate succeeds or fails
In Picat, a function definition uses `f(Args) = Value => Body`, returning a deterministic value, whereas a predicate succeeds or fails without an explicit return.
Question 5: Which constraint in Picat's `cp` module ensures all variables in a list take distinct values?
- all_different(Vars) (Correct answer)
- distinct(Vars)
- no_overlap(Vars)
- unique(Vars)
Correct answer: all_different(Vars)
`all_different/1` posts a global constraint requiring every variable in the list to be assigned a different value.
Question 6: How does Picat handle arithmetic evaluation — must you use a special operator?
- Yes, use X = Y evaluates; without it, expressions remain unevaluated terms (Correct answer)
- No, = automatically evaluates arithmetic
- Arithmetic is always lazy and evaluated on demand
- Use eval(Expr, Result) for all arithmetic
Correct answer: Yes, use X = Y evaluates; without it, expressions remain unevaluated terms
In Picat, `X = Expr` evaluates arithmetic when Expr contains numeric operations, but unlike Prolog you do not need a separate `is` operator — the = operator evaluates arithmetic expressions automatically.
Question 7: What is the purpose of the `table(+, +, min)` mode declaration in Picat?
- It memoizes the predicate, keeping only the answer with the minimum value for the last argument (Correct answer)
- It restricts the predicate to accept only ground inputs
- It enables parallel evaluation across cores
- It sorts the tabled answers by the last column
Correct answer: It memoizes the predicate, keeping only the answer with the minimum value for the last argument
Mode declarations like `table(+,+,min)` instruct the tabling engine to retain only the optimal (minimum) tabled result for each input combination, enabling dynamic-programming optimizations.
What is a 'guard' in a Picat rule, and where does it appear?