|<< Prev||- Up -|
Constants, numbers, and variables are the building blocks: now we need to know how to fit them together to make complex terms. Recall that complex terms are often called structures.
Complex terms are build out of a functor followed by a sequence of arguments. The arguments are put in ordinary brackets, separated by commas, and placed after the functor. The functor must be an atom. That is, variables cannot be used as functors. On the other hand, arguments can be any kind of term.
Now, we've already seen lots of examples of complex terms when we looked at KB1 -- KB5. For example,
playsAirGuitar(jody) is a complex term: its functor is
playsAirGuitar and its argument is
jody. Other examples are
loves(vincent,mia) and, to give an example containing a variable,
But note that the definition allows far more complex terms than this. In fact, it allows us to to keep nesting complex terms inside complex terms indefinitely (that is, it is a recursive definition). For example
is a perfectly ok complex term. Its functor is
hide, and it has two arguments: the variable
X, and the complex term
father(father(father(butch))). This complex term has
father as its functor, and another complex term, namely
father(father(butch)), as its sole argument. And the argument of this complex term, namely
father(butch), is also complex. But then the nesting ``bottoms out'', for the argument here is the constant
As we shall see, such nested (or recursively structured) terms enable us to represent many problems naturally. In fact the interplay between recursive term structure and variable matching is the source of much of Prolog's power.
The number of arguments that a complex term has is called its arity. For instance,
woman(mia) is a complex term with arity 1, while
loves(vincent,mia) is a complex term with arity 2.
Arity is important to Prolog. Prolog would be quite happy for us to define two predicates with the same functor but with a different number of arguments. For example, we are free to define a knowledge base that defines a two place predicate
love (this might contain such facts as
love(vincent,mia)), and also a three place
love predicate (which might contain such facts as
love(vincent,marcellus,mia)). However, if we did this, Prolog would treat the two place
love and the three place
love as completely different predicates.
When we need to talk about predicates and how we intend to use them (for example, in documentation) it is usual to use a suffix
/ followed by a number to indicate the predicate's arity. To return to KB2, instead of saying that it defines predicates
we should really say that it defines predicates
And Prolog can't get confused about a knowledge base containing the two different love predicates, for it regards the
love/2 predicate and the
love/3 predicate as completely distinct.
|<< Prev||- Up -|