Picat Logic Programming and Unification 3 — Questions and Answers
Question 1: In Picat, how does the `=>` rule differ from a logic predicate clause written with `:-`?
- => is for arithmetic only; :- is for logic
- => commits to the first matching rule (no backtracking into it); :- allows backtracking (Correct answer)
- => requires all arguments to be ground; :- does not
- => is used only in modules; :- is global
Correct answer: => commits to the first matching rule (no backtracking into it); :- allows backtracking
A => (deterministic) rule commits once matched — no alternative clauses are tried on failure — while :- clauses participate in full backtracking.
Question 2: Which Picat built-in decomposes a compound term into its functor and arguments?
- functor/3 and arg/3 (Correct answer)
- =../2 (univ) only
- split/3
- decompose/2
Correct answer: functor/3 and arg/3
functor(T, F, A) extracts functor F and arity A, while arg(N, T, Arg) extracts the Nth argument of T.
Question 3: What does the Picat goal `T =.. [foo, 1, 2]` do?
- Fails because =.. only works on atoms
- Unifies T with the compound term foo(1,2) (Correct answer)
- Splits T into a list of characters
- Creates an arithmetic expression
Correct answer: Unifies T with the compound term foo(1,2)
The univ operator =.. constructs or decomposes a term: [foo,1,2] as the right-hand side builds foo(1,2).
Question 4: In Picat logic predicates, what is the purpose of `call/1` or `call/N`?
- Evaluates an arithmetic expression
- Calls a goal stored in a variable at runtime (meta-call) (Correct answer)
- Imports a module
- Defines a new predicate dynamically
Correct answer: Calls a goal stored in a variable at runtime (meta-call)
call/N allows a term held in a variable to be invoked as a goal, enabling higher-order programming.
Question 5: What does the `once/1` built-in do in Picat?
- Runs a goal once and fails if it has no solutions
- Runs a goal and commits to its first solution, discarding other choice points (Correct answer)
- Runs a goal in a separate thread
- Repeats a goal until failure
Correct answer: Runs a goal and commits to its first solution, discarding other choice points
once(Goal) succeeds at most once: it runs Goal and keeps only the first solution, cutting away alternatives.
Question 6: In Picat, which of the following correctly uses assert to add a fact at runtime?
- assert(likes(mary, food)) (Correct answer)
- assert := likes(mary, food)
- likes(mary, food) := assert
- assert[likes(mary, food)]
Correct answer: assert(likes(mary, food))
assert(Fact) dynamically adds Fact to the knowledge base during program execution.
Question 7: What is 'goal conjunction' in Picat logic predicates, and how is it written?
- A list of goals executed in parallel, written as [G1, G2]
- Sequential goal execution where both must succeed, written as G1, G2 (Correct answer)
- A disjunction where one must succeed, written as G1 ; G2
- A conditional written as G1 -> G2
Correct answer: Sequential goal execution where both must succeed, written as G1, G2
Conjunction (G1, G2) means G1 must succeed first, then G2 is attempted; if either fails the conjunction fails.
In Picat, how does the `=>` rule differ from a logic predicate clause written with `:-`?