An introduction to Prolog

Programs and Questions

A program in Prolog is simply a knowledge base (KB) of true sentences and facts. The knowledge base is written in a separate file and loaded into Prolog with the command:
[filename].
The square brackets and period are necessary.

Once the program/KB is loaded you ask questions of Prolog, which it answers by backward chaining through the KB.
Questions are presented as sentences. Prolog answers yes or no and lists any substitutions it made to make the query true.

Note that yes means the sentence is true given Prolog's current KB (and the required substitutions). However, no only means that the sentence cannot be proven with Prolog's current KB

Example:
The program/KB is
parent(bob, joe).
parent(bob, tim).
parent(sue, joe).
parent(joe, sally).

We can ask questions like:
parent(sue, joe).
And the response will be
Yes

Or we could ask
parent(X, joe).
Prolog will respond
X = bob and pause. Type a semicolon and Prolog will continue with
X = sue and pause again. Type a semicolon again and Prolog will continue with
No. There are no more substitutions that make parent(x, joe) true.

If we ask
parent(bob, sally).
Prolog will respond
No

You can also ask compound questions (using a comma for AND and a semicolon for OR). The question:
parent(X,tim), parent(X,joe).
gives
X = bob ; (the user types the semicolon)
No

The question:
parent(X,tim); parent(X,sally).
Gives
X = bob ;
X = joe ;
No

Basic syntax

Implication and Clauses

In Prolog implies is written as :- and is read from right to left.
The universal quantifier is assumed for all variables and a comma is used for AND.

Example:
" x Cat(x) Ù Tame(X) Þ Pet(x) would be written as

pet(X) :- cat(X),tame(X).

Sentences are apparently written 'backwards' to reflect the fact that Prolog uses backwards chaining. Thus, the above sentence could be read as ‘to prove that X is a pet prove that X is a cat and that X is tame.
(Note sentences in Prolog end in a period. Variables must start with a capital letter or an underscore and objects and relations begin with a lower-case letter.)

A semicolon is used for OR.
Example:
" x Cat(x) Ú Tiger(x) Þ Predator(x) would be written as
predator(X) :- cat(X);tiger(X).

However, sentences using an OR are normally written as two separate sentences.
predator(X) :- cat(X).
predator(X) :- tiger(X).

Sentences must be Horn clauses, that is each sentence must be an atomic sentence or an implication with an atomic consequence.

Naming relationships, properties and objects

Relationships, properties and objects must be strings of letters, digits, and underscores beginning with a lower case letter. Examples: bob, dog1, x_7, isA, parentOf.
Strings of characters in single quotes are used if you want to begin with an upper case letter. Examples: 'Bob', 'Sue'

For relations and properties the name of the relationship or property is given first, with the object, or objects, in parentheses and separated by commas.
Examples: isA(dog1,animal), isFurry(cat).
Objects- dog1, cat, animal
Relationship- is Property- isFurry

The programmer decides on the meaning, the semantics, of the relationships and properties.
Typically a relationship like relation(X,Y) is read X is relation to/of/for Y.

Variables

Variables are strings of letters, digits, and underscores beginning with an upper-case letter or an underscore. Examples: X, Answer, _X, _Y7.

Variables that are only used in a clause once can be replaced by an underscore. Two underscores in a clause are not matched.
Example:
hasAChild(X) :- parent(X,_) If X is a parent of anyone, then X has a child.

Advanced Programs and Questions

Consider the KB given in the first section:
parent(bob, joe).
parent(bob, tim).
parent(sue, joe).
parent(joe, sally).

For this KB to be useful we need more general rules than simply X is the parent of Y. For example, we might add to our KB:
offspring(X,Y) :- parent(Y,X).
which can be read as
" X,Y parent(Y,X) Þ offspring(X,Y)
or as
Y is the parent of X implies that X is the offspring of Y.

Now if we ask:
offspring(X, bob).
Prolog gives
X = joe ;
X = tim ;
No

Remember that implication is only one way, even if it appears that it should work both ways. Consider the following KB:
offspring(X,Y) :- parent(Y,X).
offspring(bob, mary). '‘bob is th eoffspring o mary.' If we ask Prolog:
parent(X,bob).
It responds
No
Prolog knows that a parent relationship implies an offspring relationship, but the opposite is not necessarily true.

For example:
mammal(X) :- cat(X).
X is a cat implies X is a mammal is true, but the opposite is not necessarily true.