AI Lab Manual 1
AI Lab Manual 1
PROLOG stands for Programming in Logic — an idea that emerged in the early
1970’s to use logic as programming language. It is a logical and declarative
programming language. PROLOG is a programming language for symbolic, non-
numeric computation. It is especially well suited for solving problems that involve
objects and relations between objects. This is the main reason to use Prolog as the
programming language in Artificial Intelligence, where symbol manipulation
and inference manipulation are the fundamental tasks.
Prolog is used in various domains. Following are some of the domains used:
FOR EXAMPLE:
A) FACTS
b. Seema is a girl.
d. Rose is red.
So these are some facts that are unconditionally true. These are actually statements
that we have to consider as true.
Objects also begin with lower case letters. They also can begin with digits
(like 1234), and can be strings of characters enclosed in quotes e.g.
color(penink, ‘red’).
Syntax:
relation(object1,object2...).
likes(ram ,mango).
girl(seema).
red(rose).
likes(bill,juice).
can_cook(priya), can_dance(priya).
B) RULES
So these are some rules that are conditionally true, so when the right hand side is
true, then the left hand side is also true.
Syntax
happy(lili) :- dances(lili).
hungry(tom) :- search_for_food(tom).
friends(jack, bili) :- lovesCricket(jack), lovesCricket(bili).
goToPlay(ryan) :- isClosed(school), free(ryan).
C) QUERIES/QUESTIONS
Queries are some questions on the relationships between objects and object
properties. So question can be anything. In order to run a prolog program, we need
some questions, and those questions can be answered by the given facts and rules.
b. Seema is a girl.
d. Rose is red.
likes(ram, mango).
girl(seema).
likes(bill, cindy).
red(rose).
can_cook(priya).
can_dance(priya).
owns(john, gold).
This is a syntactically correct program, and after having compiled it we can ask the
questions (or queries in proper Prolog jargon/language) about it.
Step 2: Create one file (extension is *.pl) by clicking on “file” then choosing
“new” menu option, next give file name eg. “fact.pl” and copy the above code i.e.
lab 1.1 program and paste it on newly opened file as shown in figure 3.
Figure 3: Now new file name “fact.pl” was created & write the program.
Step 3: To run the program, load/insert the file into prolog system by clicking on
“file” button then choose “Consult” option. After that open the intended or
required file from the location where you have saved it prior.
Figure 5: Here, file named “fact.pl” is opened. After this step we start writing
questions/queries to prolog systems as described below.
Target Group: G5 Software Engineering Prepared by Melaku M Page 8
Introduction to Artificial Intelligence Lab Manual
?-likes(ram,What).
Note: What is used as a variable. In place of What you can use another variable like X.
The query/question likes(ram, What) (i.e., the question “Ram likes what?”) and
prolog system answers with mango, because the fact likes(ram, mango) has
previously been inserted to the Prolog system.
?-likes(ram,What).
What= mango
?-likes(Who, cindy).
Who is a variable. We could also have chosen any other name for it, as long
as it starts with a capital letter. The Prolog interpreter replies as follows:
?-likes(Who, cindy).
Who= bill
Who= john
What= gold
OUTCOME: Student will understand how to write simple facts using prolog.
Defining Rules
OBJECTIVE: Writing facts and rules
Lab 1.2: Writing a syntactically correct facts & rules for the above statements
/* Facts */
dances(lili).
search_for_food(tom).
lovesCricket(jack).
lovesCricket(bili).
isClosed(school).
free(ryan).
/* Rules */
happy(X) :- dances(X).
hungry(X) :- search_for_food(X).
friends(X, Y) :- lovesCricket(X), lovesCricket(Y).
goToPlay(W):- isClosed(X), free(W).
Now we copy and save the above lab 1.2 program in the separate file, for example
file named as “rule.pl”. Then insert/consult the files into prolog systems by
clicking on “file” then select “consult” button from menu options and choose the
file named “rule.pl” from where that you have saved it prior.
After consulting the file into prolog system we can ask the question as follows:
Note: Before running these programs please try to understand efficiently how the
above statement is converted into facts and rule sets. If you understand the
relationship between facts and rules clearly, it is easy to interact with the code.
?- friends(jack, X).
X is a variable. We can also choose any other name for it, as long as it starts
with a capital letter. The Prolog interpreter replies as follows:
?- friends(jack, X).
X = bili
Queries: Is a lili happy?
?- happy(lili).
?- friends(jack, bili).
?- friends(kebede, bili).