Picat Functions vs. Predicates Questions and Answers — Questions and Answers
Question 1: What is the primary characteristic that distinguishes a Picat function from a Picat predicate?
- Functions can only be used for mathematical calculations, while predicates handle logic.
- Functions are defined using the `function` keyword, whereas predicates use the `pred` keyword.
- Functions must always succeed and return exactly one value, while predicates can fail, succeed, or provide multiple answers via backtracking. (Correct answer)
- Functions cannot call predicates, but predicates can call functions.
Correct answer: Functions must always succeed and return exactly one value, while predicates can fail, succeed, or provide multiple answers via backtracking.
In Picat, a function is a specialized form of a predicate. The key difference is that a function is deterministic; it must always succeed and yield a single return value. Predicates, on the other hand, are more general: they can succeed (once or multiple times through backtracking) or they can fail.
Question 2: A developer needs to implement a feature that finds all possible paths between two nodes in a graph. Which Picat construct is more suitable for this scenario and why?
- A function, because it can efficiently calculate and return a single list containing all the paths.
- A predicate, because it can use backtracking to find and yield each path as a separate solution. (Correct answer)
- A function, because graph traversal is a purely computational task that should not fail.
- A predicate, because functions are not allowed to be recursive, which is necessary for pathfinding.
Correct answer: A predicate, because it can use backtracking to find and yield each path as a separate solution.
A predicate is the ideal choice for finding all possible paths because this task is inherently non-deterministic. A predicate can be defined with backtrackable rules (using `?=>`) to explore different branches of the graph. Each time a path is found, the predicate succeeds, and backtracking allows the search to continue for other potential paths.
Question 3: Which of the following statements about Picat functions is FALSE?
- A function call can be used directly within an arithmetic expression.
- A function is considered a special kind of predicate.
- A function can be defined with backtrackable rules (`?=>`) to provide multiple return values. (Correct answer)
- If a function call does not match any of its defining rules, it will raise an exception.
Correct answer: A function can be defined with backtrackable rules (`?=>`) to provide multiple return values.
Functions in Picat are deterministic and must return exactly one value. They cannot be defined with backtrackable rules (`?=>`), as this operator is specifically for non-deterministic predicates that can yield multiple solutions through backtracking. Functions always succeed once or raise an exception if no rule matches.
Question 4: Consider the following Picat code snippet: ```picat main => X = my_calc(5), println(X). my_calc(N) = R, N > 0 => R = N * 2. ``` What is the role of `my_calc/1` in this program?
- It is a non-deterministic predicate that binds `R` to `N * 2`.
- It is a function that returns the value of `N * 2`. (Correct answer)
- It is a predicate that checks if `N` is greater than 0.
- It is a constraint that enforces `R` to be twice the value of `N`.
Correct answer: It is a function that returns the value of `N * 2`.
The syntax `my_calc(N) = R` defines a function named `my_calc` with one input argument `N` and a return value `R`. The expression `R = N * 2` calculates the return value. The call `my_calc(5)` evaluates to 10, which is then assigned to `X`.
Question 5: When is it more appropriate to use a predicate instead of a function in Picat?
- When you need to perform a simple, deterministic calculation that returns a single value.
- When the primary purpose is to check if a certain condition is true or false without returning a specific value. (Correct answer)
- When you want to guarantee that the operation will never fail and always produce a result.
- When you want to embed the call directly into a mathematical formula for conciseness.
Correct answer: When the primary purpose is to check if a certain condition is true or false without returning a specific value.
Predicates are ideal for relational logic, where the outcome is success or failure. If the goal is simply to verify a condition (e.g., checking for membership in a list, `member/2`), a predicate is the natural choice. It succeeds if the condition holds and fails otherwise. While a function could be forced to return a boolean, a predicate more directly expresses this logic.
Question 6: How does Picat handle a call to a function for which no pattern-matching rule applies?
- The call fails silently, and the program continues.
- The call succeeds and returns a `null` or `undefined` value.
- The call enters an infinite loop.
- The call raises an exception and terminates the program. (Correct answer)
Correct answer: The call raises an exception and terminates the program.
Unlike predicates which can simply fail, a function in Picat is expected to always succeed with one answer. If a function is called with arguments that do not match any of its defining rules, it cannot produce a value. This situation is treated as an error, and Picat will raise an exception.
What is the primary characteristic that distinguishes a Picat function from a Picat predicate?