csc405 Prolog Lab 1
csc405 Prolog Lab 1
Faculty of Science
CSC 405 – Artificial Intelligence
Prolog Laboratory Sheet I
Prolog is a logical and a declarative programming language. It is one of the two most frequently used symbolic programming
languages for artificial intelligence (the other one is LISP – List Processing). Prolog is short for PROgramming in LOGic.
There are only three basic constructs in Prolog: facts, rules, and queries. A collection of facts and rules is called a knowledge
base. Prolog programming is all about writing knowledge bases, i.e., Prolog programs simply are collections of facts and rules
which describe some collection of relationships that we find interesting. We use a Prolog program by posing queries, i.e., by
asking questions about the information stored in the knowledge base. Note that this is very different from the non-declarative
programming paradigm in which we tell the computer what to do.
There are several Prolog packages available; we shall be using SWI Prolog. We shall use SWI Prolog's interactive interpreter to
execute our Prolog programs one line at a time. But Prolog programs can also be compiled to a machine-dependent executable
that can be run in an environment without Prolog installed.
Running a program under the interactive interpreter allows the user to list it and to make full use of the debugger on it.
Compiling a program to native code makes it possible to obtain a stand alone executable, with a reduced size and optimized for
speed. However, it is not possible to make full use of the debugger on a program compiled to native-code. Nor is it possible to
list the program. In general, it is preferable to run a program under the interpreter for debugging and then use the native-code
compiler to produce an autonomous executable.
The Prolog interpreter is also known as the top level. When you run Prolog you get a prompt that waits for you to type in bits of
Prolog code, and then execute them.
?-
The Prolog interpreter is now running and waiting for you to type in some commands. Writing programs in Prolog is a cycle
involving the following steps:
a. Write/Edit the program in a text-editor
b. Save the program in the text editor
c. Tell Prolog to read in the program
d. If Prolog gives you errors, go back to step 1 and fix them
e. Test it - if it doesn't do what you expected, go back to step 1
3. Type the following program using your text editor. Add at least one blank line at the end of the program. Save the
program as intro.pl
likes(mary,food).
likes(mary,wine).
likes(john,wine).
likes(john,mary).
Take note of the following:
Prolog file names have the default extension .pl
Prolog variables begin with uppercase letters while constants begin with lowercase letters. This is the reverse of the
notation used in logic.
A comma is used to separate arguments, and a full stop marks the end of a sentence.
Running a query
KB currently comprises four facts. Rules can be added by using the if operator. We also introduce the and and or operators.
The equivalent symbols for these operators in Prolog are:
if :-
and , (i.e., comma). We saw earlier that the comma is also used to separate arguments
or ; (i.e., semi colon)
The implication symbol in Prolog is :- Unlike in logic however, Polog reverses the order of implications. Hence Q :- P is
equivalent to the sentence P Q.
Add the rules given below to KB. Do this one at a time. Each time you add a rule, save the file, then load it into Prolog, and
finally run each of the queries given in Step 2 above.
John likes anything that Mary likes
Phrase this as: John likes something if Mary likes (that same) something
John likes anyone who likes wine
Phrase this as: John likes someone if that someone likes wine
1. sister, brother
2. aunt, uncle
3. grandparent, cousin