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.pet(X) :- cat(X),tame(X).
Sentences are apparently written 'backwards' to reflex 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. 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. The programmer decides on the meaning, the semantics, of the relationships and properties. 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.
Advanced Programs and Questions 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: Now if we ask: Remember that implication is only one way, even if it appears that it should work both ways.
Consider the following KB: For example:
Strings of characters in single quotes are used if you want to begin with an upper case letter.
Examples: 'Bob', 'Sue'
Examples: isA(dog1,animal), isFurry(cat).
Objects- dog1, cat, animal
Relationship- is
Typically a relationship like relation(X,Y) is read X is relation to/of/for Y.
Example:
hasAChild(X) :- parent(X,_) If X is a parent of anyone, then X has a child.
parent(bob, joe).
parent(bob, tim).
parent(sue, joe).
parent(joe, sally).
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.
offspring(X, bob).
Prolog gives
X = joe ;
X = tim ;
No
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.
mammal(X) :- cat(X).
X is a cat implies X is a mammal is true, but the opposite is not necessarily true.