0% found this document useful (0 votes)
19 views

csc405 Prolog Lab 1

Uploaded by

Sigala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

csc405 Prolog Lab 1

Uploaded by

Sigala
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

University of Buea

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.

Starting SWI Prolog


1. Go to the command prompt and type swipl/prolog
2. After a while, you should see the following screen display:
Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 6.6.6)
Copyright (c) 1990-2013 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?-

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.

Denis L. Nkweteyim Artificial Intelligence


4. Load the program by typing in the Prolog window:
[intro].
A message similar to the following is displayed:
% intro compiled 0.00 sec, 8 clauses
true.

This step must be repeated everytime the program is modified.


No error message is displayed; this indicates that Prolog has checked your code and found no errors. If you get an error
message, you should check that you have typed in the code correctly.

Running a query

1. Type the following query at the top level:


likes(mary,food).
You should see the following output:
true
The true that is displayed means that proposition likes(mary, food) is true. Notice that the Prolog prompt is not
displayed. This is because there are more likes propositions in the KB involving mary. At this point, you have 3 options:
i) type ; for the next solution, or
ii) type Return to stop and display no further solutions.
Repeat the query and try each one of these options. If Prolog returns false, then there are no further true propositions
in KB.
2. Type each of the following queries, and make sure you understand the results
likes(mary,wine).
likes(john,wine).
likes(john,mary).
3. When you're finished you should leave Prolog by typing halt.

Facts and Rules


1. Restart Prolog and load the file intro.pl.
2. Try the following queries (recall that Prolog variables start with upper case letters).
likes(john,X).
likes(mary,X).
likes(Y,food).
likes(Y,wine).

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

Denis L. Nkweteyim Artificial Intelligence


Family Tree Example
This example deals with the kinship domain, for example:
James I
|
|
+----------------+-----------------+
| |
Charles I Elizabeth
| |
| |
+----------+------------+ |
| | | |
Catherine Charles II James II Sophia
|
|
|
George I
Key in the following program and save is as family.pl.
% male(P) is true when P is male. This is an example of a comment
male(james1).
male(charles1).
male(charles2).
male(james2).
male(george1).
% female(P) is true when P is female
female(catherine).
female(elizabeth).
female(sophia).
% parent(P,C) is true when P is the parent of C
parent(james1, charles1).
parent(james1, elizabeth).
parent(charles1, charles2).
parent(charles1, catherine).
parent(charles1, james2).
parent(elizabeth, sophia).
parent(sophia, george1).

Formulate the following queries


Was George I the parent of Charles I?
Query: parent(george1, charles1).
Who was Charles I's parent?
Query: parent(Parent, charles1).
%note in the example above that Parent with uppercase P is a variable; we
could have used X instead, or any other identifier that begins with an
uppercase letter.
Who were the children of Charles I?
Query: parent(charles1, Child).
Try adding the following rules to the program, and check the results using appropriate queries:
M is the mother of P if she is a parent of P and is female
F is the father of P if he is a parent of P and is male
X is a sibling of Y if they both have the same parent.
Add rules for the following relations, testing each one of them as the rule is added. Note that the connection between predicates
should be made by sharing variables (and not by embedding one predicate inside another).

1. sister, brother
2. aunt, uncle
3. grandparent, cousin

Denis L. Nkweteyim Artificial Intelligence

You might also like