\comment{Some Useful Built-in Predicates} CORAL provides a collection of ``built-in'' predicates\index{builtin predicates} that can be used in programs just like ordinary (user-defined) predicates. Many of these have been discussed in other sections already. \subsection {Arithmetic Built-Ins} Arithmetic expressions\index{arithmetic expressions} can be constructed using +, -, *, /, {\tt mod} (for modulus) using the usual infix syntax. In addition, {\tt abs} is a binary predicate that binds its second argument to the absolute value of its first argument (which must always be bound when the literal is evaluated). Another useful predicate is the ternary {\tt pow}. The expression pow(A,B,C) succeeds with $A^B ~=~ C$. It can be evaluated with any pair of the arguments bound; thus, in addition to computing $A^B$, it can be used to compute the Bth root of C and log C to the base A. Some important guidelines for using arithmetic predicates are discussed in Section \ref{chap:start}. \subsection {Multiset Operators} A number of operators are provided for manipulating multiset values\index{multisets}. These are discussed in Section \ref{chap:declse} and in the overview \cite{rsss93:coraloverview}. We list these operators here for convenience: {\tt member, unionsum, unionmax, inter, difference, subset, make\_set, create\_set, add\_elem, count, sum, avg, min, max, prod.} We note that an arithmetic expression cannot be listed as an element of a set. For example, the following, typed in at the command line, would cause an error: {\tt \noindent $n>$ ?U = {4, 7*9}. } However, {\tt \noindent $n>$ ?U = {4,7,9}, prod(U,X). } results in X being bound to 252. (By the way, such command line interaction is a good way to get familiar with the suite of built-ins.) \subsection {Metaprogramming in CORAL} Some Prolog-style builtins for metaprogramming\index{metaprogramming} are available in CORAL. These include: {\em member, call, univ, functor, is\_string, is\_num, is\_const, is\_var, is\_functor, and is\_list}. Type $help(builtins)$ to get more information on these builtins. The predicate $member$ can be used to solve goals whose predicate name is only known at run-time. When a literal $member(X, A1,A2, ... , An)$ is evaluated, X must be bound to a relation (or relation name) of arity n, \footnote{X can also be bound to a multiset; see \ref{chap:declse} for a discussion of $member$ used this way.} and the remaining arguments can be any terms. This generates a goal $?X(A1,A2, ... , An)$. The relation $X$ can be base or derived. In the latter case, any binding of the A's is used in evaluating the goal; thus only the relevant portion of $X$ is computed. The predicate $call$ is similar to $member$, with slightly different syntax. The literal $call(X(A1,A2,...,An))$ generates the goal $?X(A1,A2, ... , An)$; $X$ must be bound to a predicate name (not a set). While $member$ is more versatile in some ways, $call$ can be used in conjunction with $univ$ to solve goals whose arglist (and even arity) is only known at run-time. The suite of routines of the form ``is\_ ...'' test whether an argument is of a particular kind; for example, $?is\_var(A)$ succeeds if A is a variable. These should be used in materialized modules with some caution; re-ordering of literals is possible during fixpoint evaluation, and, for example, a variable that is expected to be unbound gets bound, or vice-versa. The builtin $univ$ is useful for two distinct tasks: (1) to extract the arguments of a functor term, and (2) to create a functor term whose functor name is only available at run-time. The builtin $functor$, which is closely related, extracts the functor name and arity from a functor term. {\tt \noindent $n>$ ?X=p, member(X,U). } \subsection {Miscellaneous Operators} Bitwise or, xor, and, rshift and lshift operators are provided, with C syntax. There is also a special built-in called {\em fail} that has arity 0. This can be used to force a rule to fail. Logically, it is identical to a relation with no tuples, but is a little more efficient in practice. It is very useful, for example, if we want to time a query and suppress printing of answers: % RR - it would be better to use 'coral -a empty' or % assign(answer_style,2). % as intelligent backtracking will cause this time to be wrong. % % Need a different example of a good reason for 'fail'. {\tt \noindent $n>$ ?anc(X,Y), fail. } This would result in the entire anc relation being computed (assuming that a module defining this predicate has already been consulted), but no X,Y bindings being printed.