next up previous contents index
Next: Analysing and constructing atoms Up: Built-in predicates Previous: Term reading and writing   Contents   Index

Analysing and Constructing Terms

functor3?Term, ?Functor, ?Arity Succeeds if Term is a term with functor Functor and arity Arity. If Term is a variable it is unified with a new term holding only variables. functor3 silently fails on instantiation faults2.7If Term is an atom or number, Functor will be unified with Term and arity will be unified with the integer 0 (zero). arg3?Arg, ?Term, ?Value Term should be instantiated to a term, Arg to an integer between 1 and the arity of Term. Value is unified with the Arg-th argument of Term. Arg may also be unbound. In this case Value will be unified with the successive arguments of the term. On successful unification, Arg is unified with the argument number. Backtracking yields alternative solutions.2.8The predicate arg3 fails silently if $\arg{Arg} = 0$ or $\arg{Arg} > \mbox{\em arity}$ and raises the exception domain_errornot_less_then_zero, Arg if $\arg{Arg} <
0$.

setarg3+Arg, +Term, +Value Extra-logical predicate. Assigns the Arg-th argument of the compound term Term with the given Value. The assignment is undone if backtracking brings the state back into a position before the setarg3 call.

This predicate may be used for destructive assignment to terms, using them as and extra-logical storage bin. ?Term?List List is a list which head is the functor of Term and the remaining arguments are the arguments of the term. Each of the arguments may be a variable, but not both. This predicate is called `Univ'. Examples:
\begin{code}
?- foo(hello, X) =.. List.
\par List = [foo, hello, X]
\par ?- Term =.. [baz, foo(1)]
\par Term = baz(foo(1))
\end{code}

numbervars4+Term, +Functor, +Start, -End Unify the free variables of Term with a term constructed from the atom Functor with one argument. The argument is the number of the variable. Counting starts at Start. End is unified with the number that should be given to the next variable. Example:
\begin{code}
?- numbervars(foo(A, B, A), this_is_a_variable, 0, End).
\par A = this_is_a_variable(0)
B = this_is_a_variable(1)
End = 2
\end{code}

In Edinburgh Prolog the second argument is missing. It is fixed to be $VAR. free_variables2+Term, -List Unify List with a list of variables, each sharing with a unique variable of Term. For example:
\begin{code}
?- free_variables(a(X, b(Y, X), Z), L).
\par L = [G367, G366, G371]
X = G367
Y = G366
Z = G371
\end{code}

copy_term2+In, -Out Make a copy of term In and unify the result with Out. Ground parts of In are shared by Out. Provided In and Out have no sharing variables before this call they will have no sharing variables afterwards. copy_term2 is semantically equivalent to:
\begin{code}
copy_term(In, Out) :-
recorda(copy_key, In, Ref),
recorded(copy_key, Out, Ref),
erase(Ref).
\end{code}


next up previous contents index
Next: Analysing and constructing atoms Up: Built-in predicates Previous: Term reading and writing   Contents   Index
Dr. Richard Botting 2001-12-12