12.1 Introduction
In the last chapter, we saw that operators like countofall, setofall, choose, and if allow us to use views in defining evaluable terms. In this chapter, we look at the opposite, viz. how to use evaluable functions in defining view relations.
12.2 evaluate
The evaluate relation is used to represent equations involving predefined functions. For example, we would write evaluate(plus(times(3,3),times(2,3),1),16) to represent the equation 3^2+2x3+1=16. If height is a binary predicate relating a rectangle and its height and if width is a binary predicate relating the rectangle and its width, we can define the area of the rectangle as shown below. The area of X is A if the height of X is H and the width of X is W and A is the result of multiplying H and W.
| goal(X,A) :- height(X,H) & width(X,W) & evaluate(times(H,W),A) |
As with other operators, there are syntactic restrictions on the use of evaluate. (1) The first of argument to evaluate must be an evaluable term (with variables), (2) all variables in the first argument must be bound in prior subgoals or any rule in which it is used, and (3) the second argument must be a primitive.
12.3 Reversibility
One advantage of conceptualizing functions as relations and defining those relations using view rules is that those relations are reversible - we can supply any subset of arguments and compute the values of all remaining arguments. This is not possible with function definitions because they are computed form the bottom-up.
|