Assignment One
Assignment One
A Prolog program consists of a number of clauses. Each clause is either a fact or a rule. After a
Prolog program is loaded (or consulted) in a Prolog interpreter, users can submit goals or
queries, and the Prolog intepreter will give results (answers) according to the facts and rules.
Facts
A fact must start with a predicate (which is an atom) and end with a fullstop. The predicate may
be followed by one or more arguments which are enclosed by parentheses. The arguments can be
atoms (in this case, these atoms are treated as constants), numbers, variables or lists. Arguments
are separated by commas.
If we consider the arguments in a fact to be objects, then the predicate of the fact describes a
property of the objects.
In a Prolog program, a presence of a fact indicates a statement that is true. An absence of a fact
indicates a statement that is not true. See the following example:
yes
?- rainy. /* There is an error because there is no predicate "rainy". */
Erroneous result.
?- father(john, mary).
yes
?- mother(susan, mary). /* This cannot be deduced. */
no
?- father(john, susan). /* This cannot be deduced. */
no
Rules
1|Page
A rule can be viewed as an extension of a fact with added conditions that also have to be
satisfied for it to be true. It consists of two parts. The first part is similar to a fact (a predicate
with arguments). The second part consists of other clauses (facts or rules which are separated by
commas) which must all be true for the rule itself to be true. These two parts are separated by
":-". You may interpret this operator as "if" in English.
Take Rule 3 as an example. It means that "granfather(X, Y)" is true if both "father(X, Z)"
and "parent(Z, X)" are true. The comma between the two conditions can be considered as a
logical-AND operator.
You may see that both Rules 1 and 2 start with "parent(X, Y)". When will "parent(X, Y)" be
true? The answer is any one of these two rules is true. This means that "parent(X, Y)" is true
when "father(X, Y)" is true, or "mother(X, Y)" is true.
As you can see from Rules 3 to 5, predicates that only appear in rules but not facts (in this case,
parent) can also form conditions of other rules.
Goals
A goal is a statement starting with a predicate and probably followed by its arguments. In a valid
goal, the predicate must have appeared in at least one fact or rule in the consulted program, and
the number of arguments in the goal must be the same as that appears in the consulted program.
Also, all the arguemnts (if any) are constants.
The purpose of submitting a goal is to find out whether the statement represented by the goal is
true according to the knowledge database (i.e. the facts and rules in the consulted program). This
is similar to proving a hypothesis - the goal being the hypothesis, the facts being the axioms and
the rules being the theorems.
Queries
A query is a statement starting with a predicate and followed by its arguments, some of which
are variables. Similar to goals, the predicate of a valid query must have appeared in at least one
fact or rule in the consulted program, and the number of arguments in the query must be the
same as that appears in the consulted program.
The purpose of submitting a query is to find values to substitute into the variables in the query
such that the query is satisfied. This is similar to asking a question, asking for "what values will
make my statement true".
The following examples show how goals and queries are evaluated. The program used is
Program 3. Results are shown in red.
?- parent(susan, mary).
This is a goal proving that "Susan is a parent of Mary". From Fact 12 and Rule 2,
yes we can see that it is true.
?- parent(ray, peter).
This is a goal proving that "Ray is a parent of Peter". Since no facts and rules
no support it, this statement is disproved.
?- yeye(X, susan).
This is a query asking for the person who is the "yeye" of Susan. We cannot find
no the solution from the program, so the Prolog interpreter returns no.
?- mama(amy, X). This is a query asking for the person who calls Amy "mama". From the program,
we can see that both Peter and Mary are solutions. Therefore the Prolog
X = peter ; interpreter display both answers.
X = mary ; Since the Prolog interpreter can only display one solution at a time, a semicolon
(this is software dependent) is entered to ask for the next solution. If there is no
no more solution, no will be returned.
?- gunggung(X, Y). This is a query asking for: "X is a 'gunggung' of Y'. Who are X and Y?" From
3|Page
X = jack
Y = peter ;
the program, we can see that X is Jack, while Y can be Peter or Mary. Therefore
X = jack the Prolog interpreter displays both set of results.
Y = mary ;
no
See the next page to see how the Prolog interpreter deals with goals and queries.
4|Page
In addition to self-defined predicates, Prolog also provides built-in predicates. The following are
some common built-in predicates:
Note that each of the built-in predicates above have two arguments, one on the left of the
predicate and one on the right (just similar to other programming languages). However, user-
defined predicates (as well as some other built-in predicates) must come before their arguments.
Also note the difference between is and = . See the following goals:
?- 4 = 4.
Obvious.
yes
?- 4 is 4.
Obvious.
yes
?- 4 = 1 + 3.
The answer is no because the left hand side (4) is not identical to right hand side (1 + 3).
no
?- 4 is 1 + 3.
yes
5|Page