In the previous sections of this chapter, we've talked about Prolog "matching answers to questions", "finding a match", "matching conditions with facts", "matching variables with constants", and so on. In this section we explain what we mean when we use the term "match."
There are several ways Prolog can match one thing to another. Obviously, identical structures match each other;
parent(joe,tammy) matches parent(joe,tammy).
However, a match usually involves one or more free variables. For example, with X free,
parent(joe,X) matches parent(joe,tammy)
and X takes on (is bound to) the value tammy.
If X is already bound, it acts exactly like a constant. Thus, if X is bound to the value tammy, then
parent(joe,X) matches parent(joe,tammy) but
parent(joe,X) would
not match parent(joe,millie)
The second instance doesn't match because, once a variable becomes bound, its value can't change.
How could a variable, bindings already be bound when Prolog tries to match it with something? Remember that variables don't store values--they only stay bound for the length of time needed to find (or try to find) one solution to one goal. So the only way a variable could be bound before trying a match is that the goal involves more than one step, and the variable became bound in a previous step. For example,
parent(joe,X), parent(X,jenny)
is a legitimate goal; it means, "Find someone who is a child of Joe and a parent of Jenny." Here X will already be bound when the subgoal parent(X,jenny) is reached. If there is no solution to parent(X,jenny), Prolog will unbind X and go back and try to find another solution to parent(joe,X), then see if parent(X,jenny) will work with the new value of X.
Two free variables can even match each other. For example,
parent(joe,X) matches parent(joe,Y)
binding the variables X and Y to each other. As long as the binding lasts, X and Y are treated as a single variable, and if one of them gets a value the other one will immediately have the same value. When free variables are bound to each other like this, they're called pointers, shared free sharing variables. Some really powerful programming techniques involve binding together variables that were originally separate.
In Prolog, variable bindings (values) are passed in two ways: in and out. The direction in which a value is passed is referred to as its flow pattern. When a variable is passed into a clause, it is an input , argument, signified by (i); when passed out of a clause, a variable is an output argument, signified by (o).
These are the ideas we've introduced in this chapter:
1. A Prolog program is made up of clauses, which conceptually are two types of phrases: facts and rules.
Facts are relations or properties that you, the programmer, know to be true.
Rules are dependent relations; they allow Prolog to infer one piece of information from another.
2. Facts have the general form:
property(object1, object2, ..., objectN)
or
relation(object1, object2, ..., objectN)
where a property is a property of the objects and a relation is a relation between the objects. As far as Prolog programming goes, the distinction doesn't exist and we will refer to both as relations in this book.
3. Each fact given in a program consists of either a relation that affects one or more objects or a property of one or more objects. For example, in the Prolog fact
likes(tom, baseball).
the relation is likes, and the objects are tom and baseball; Tom likes baseball. Also, in the fact
left_handed(benjamin)
the property is left_handed and the object is benjamin; in other words, Benjamin is left-handed.
4. Rules have the general form Head:- Body, which looks like this in a program:
relation(object,object,...,object):-
relation(object,...,object),
.
.
relation(object,...,object).
5. You are free to choose names for the relations and objects in your programs, subject to the following constraints:
Object names must begin with a lower-case letter, followed by any number of characters; characters are upper-case or lower-case letters, digits, and underscores.
Properties and relation names must start with a lower-case letter, followed by any combination of letters, digits, and underscore characters.
6. A predicate is the symbolic name (identifier) for a relation and a sequence of arguments. A Prolog program is a sequence of clauses and directives, and a procedure is a sequence of clauses defining a predicate. Clauses that belong to the same predicate must follow one another.
7. Variables enable you to write general facts and rules and ask general questions.
Variable names in Visual Prolog must begin with a capital letter or an underscore character (_), after which you can use any number of letters (upper-case or lower-case), digits, or underscores.
Variables in Prolog get their values by being matched to constants in facts or rules. Until it gets a value, a variable is said to be free; when it gets a value, it becomes bound.
You can't store information globally by binding a value to a variable, because a variable is only bound within a clause.
8. If you only need certain information from a query, you can use anonymous variables to ignore the values you don't need. In Prolog, the anonymous variable is represented by a lone underscore (_).
The anonymous variable can be used in place of any other variable; it matches anything. The anonymous variable will never get set to a value.
9. Asking Prolog questions about the facts in your program is known as querying the Prolog system; the query is commonly called a goal. Prolog tries to satisfy a goal (answer the query) by starting at the top of the facts, looking at each fact until it reaches the bottom.
10. A compound goal is a goal made up of two or more parts; each part of the compound goal is called a subgoal. Compound goals can be conjunctions (subgoal A and subgoal B) or disjunctions (subgoal A or subgoal B).
11. Comments make your programs easier to read; you can enclose a comment with delimiters /* like this */ or precede a single-line comment with a percent sign, % like this.
12. There are several ways Prolog can match one thing to another:
Identical structures match each other.
A free variable matches a constant or a previously-bound variable (and becomes bound to that value).
Two free variables can match (and be bound to) each other. As long as the binding lasts, they are treated as a single variable; if one gets a value the other will immediately have the same value.