In this section, we present some terminology which is both useful and necessary when talking about Prolog programs. The terminology introduced is:
Head, Entails, Body
The syntax of a rule is as follows:
head :- body.
The symbol ":-" is pronounced "Entails" for reasons that will become clear in a moment.
What precedes the ":-" 'entails' symbol is the head of the rule, while what follows the 'entails' symbol is the body of the rule. The rule is terminated with a period.
Looking at our example rule:
same_semester(A,B) :- student(A,S), student(B,S).
we see that "same_semester(A,B)" is the head, while "student(A,S),student(B,S)" is the body of the rule.
A rule can be read as "The body entails the head." Put another way, "We have proved the head if we can prove the body." We will get back to this in the chapter on Prolog's solution strategy.
A head can be any Prolog+CG data except variables, lists, and numeric constants.
The body of a rule consists of one or more sub-goals. We have already met the term "subgoal" in connection with queries. For queries, we have seen that a query is made up of one or more subgoal, such as the following query:
?- student(Lisa, 5), student(John, 3).
This query contains two subgoals: "student(Lisa, 5)" and "student(John, 3)". The subgoals are separated by a comma.
Likewise, the body of a rule consists of one or more subgoals. The subgoals are separated by commas. The body of the example rule is:
with two subgoals.
We have already met facts, such as:
The syntax of facts is as follows:
Meaning of facts (for the specially interested)
A fact is a head followed by a period.
Facts are really a special kind of rule: There is an invisible 'entails' (":-") symbol, followed by "true". Thus what a fact really means is, for example:
student(name(Lisa), semester(5)) :- true.
which means "We have proved that Lisa is a student in the fifth semester if we can prove true." Since we can always prove "true", we can always prove that Lisa is a student in the fifth semester. Thus it is a "fact" that Lisa is a student in the fifth semester.
We are now in a position to say what a clause is. The clause is the basic programming unit in Prolog. A clause is simply either a fact or a rule. Thus a fact is a clause, and a rule is a clause. The term "clause" is a higher-level concept than either "fact" or "rule". We can draw the following type-hierarchy:
When using a structure as a subgoal in either a query or the body of a rule, that usage is called an "invocation". For example, when asking the following question:
?- same_semester(name(Lisa), X).
we say that we invoke the rule with the functor "same_semester" and arity 2.
The term "invoke" comes from a Latin word which means "call upon". Therefore, we also say that we call a rule when we invoke it.
A predicate is a collection of clauses whose heads have the same functor and arity. Thus a predicate is a collection of facts and rules whose heads have the same functor (e.g., student) and arity (e.g., 2). Thus, in the example program, student/2 is a predicate. A predicate can be made up of both facts and rules, so we are not restricted to predicates that consist solely of facts, as in the example program.
We have introduced the terminology Head, Entails, Body, Subgoal, Fact, Clause, Invocation, and Predicate. All of these terms are useful when talking about Prolog programs.
Now that we have the terminology to talk about Prolog programs, we will look at the rules for when a specific variable name refers to the same variable. We must not assume that just because a variable name occurs more than once, then the variable name refers to the same variable.
PrevLite: 11.1 Example of a rule
NextLite: 11.3 Variable scoping rule
Prev: 11.1 Example of a rule
Up: 11 Rules
Next: 11.3 Variable scoping rule