100% found this document useful (4 votes)
2K views12 pages

AI Lab Manual 1

This document introduces Prolog programming and provides examples of representing facts and rules in Prolog. It discusses that Prolog is a logical programming language well-suited for symbolic problems involving objects and relations. It then provides examples of representing facts using predicates, defines rules as conditional relationships, and discusses how to write queries to test the facts and rules. Sample facts and rules are given for examples like "Ram likes mango" and "Lili is happy if she dances". Steps for writing, saving, and running Prolog programs are also outlined.

Uploaded by

Shaller Taye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (4 votes)
2K views12 pages

AI Lab Manual 1

This document introduces Prolog programming and provides examples of representing facts and rules in Prolog. It discusses that Prolog is a logical programming language well-suited for symbolic problems involving objects and relations. It then provides examples of representing facts using predicates, defines rules as conditional relationships, and discusses how to write queries to test the facts and rules. Sample facts and rules are given for examples like "Ram likes mango" and "Lili is happy if she dances". Steps for writing, saving, and running Prolog programs are also outlined.

Uploaded by

Shaller Taye
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Introduction to Artificial Intelligence Lab Manual

Introduction to Artificial Intelligence Lab Manual


Prolog programming

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:

 Intelligent Database Retrieval


 Natural Language Understanding
 Machine Learning
 Robot Planning
 Automation System
 Problem Solving

Prolog language basically has three different elements

FACTS, RULES AND QUERIES

These are the building blocks of logic programming. Programming in PROIOG is


accomplished by creating a database of facts and rules about objects, their
properties, and their relationships to other objects. Queries then can be posed
about the objects and valid conclusions will be determined and returned by the

Target Group: G5 Software Engineering Prepared by Melaku M Page 1


Introduction to Artificial Intelligence Lab Manual

program. Responses to user queries are determined through a form of inference


control known as resolution.

FOR EXAMPLE:

A) FACTS

We can define fact as an explicit relationship between objects, and properties


these objects might have. So facts are unconditionally true in nature. These are
actually statements that we have to consider as true.

a. Ram likes mango.

b. Seema is a girl.

c. Bill likes Cindy.

d. Rose is red.

e. Priya can cook and dance.

So these are some facts that are unconditionally true. These are actually statements
that we have to consider as true.

Following are some guidelines to write facts:

 Names of properties/relationships begin with lower case letters.

 The relationship name appears as the first term.

 Objects appear as comma-separated arguments within parentheses.

 A period "." must end a fact.

Target Group: G5 Software Engineering Prepared by Melaku M Page 2


Introduction to Artificial Intelligence Lab Manual

 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’).

 phoneno(kebede, 1122334455). is also called a predicate or clause.

Syntax:

relation(object1,object2...).

Example: Following is an example of the above concept:

 likes(ram ,mango).
 girl(seema).
 red(rose).
 likes(bill,juice).
 can_cook(priya), can_dance(priya).

B) RULES

We can define rule as an implicit relationship between objects. So rules are


conditionally true. So when one associated condition is true, then the predicate is
also true. Rules are extinctions of facts that contain conditional clauses. To satisfy
a rule these conditions should be met. Suppose we have some rules as given
below:

 Lili is happy if she dances.


 Tom is hungry if he is searching for food.
 Jack and Bili are friends if both of them love to play cricket.
 Ryan will go to play if school is closed, and he is free.

Target Group: G5 Software Engineering Prepared by Melaku M Page 3


Introduction to Artificial Intelligence Lab Manual

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

rule_name(object1, object2, ...) :- fact/rule(object1, object2, ...)

Example: Representing the above rules as follows

 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.

OBJECTIVE: Write simple fact for following

a. Ram likes mango.

b. Seema is a girl.

c. Bill likes Cindy.

d. Rose is red.

e. Priya can cook and dance.

f. John owns gold.

Target Group: G5 Software Engineering Prepared by Melaku M Page 4


Introduction to Artificial Intelligence Lab Manual

Lab 1.1: Prolog Programs, representing facts

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.

Steps to execute prolog programs

Step 1: Install SWI-prolog in your computer and launch 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 1: Creating new files to write the prolog program

Target Group: G5 Software Engineering Prepared by Melaku M Page 5


Introduction to Artificial Intelligence Lab Manual

Figure 2: Giving file name as “fact”

Target Group: G5 Software Engineering Prepared by Melaku M Page 6


Introduction to Artificial Intelligence Lab Manual

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 4: Consulting the file into prolog

Target Group: G5 Software Engineering Prepared by Melaku M Page 7


Introduction to Artificial Intelligence Lab Manual

Step 4: Click open button and the file opened.

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

Figure 6: Asking queries to prolog system

?-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

 Next, suppose we want to know “Who likes Cindy?”. The corresponding


query/question would be:

?-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:

Target Group: G5 Software Engineering Prepared by Melaku M Page 9


Introduction to Artificial Intelligence Lab Manual

?-likes(Who, cindy).

Who= bill

 Queries: “Who owns what?”

?-owns(Who,What). /* Note: Who and What is used as variable. */

Who= john

What= gold

OUTCOME: Student will understand how to write simple facts using prolog.

Target Group: G5 Software Engineering Prepared by Melaku M Page 10


Introduction to Artificial Intelligence Lab Manual

Defining Rules
OBJECTIVE: Writing facts and rules

 Lili is happy if she dances.


 Tom is hungry if he is searching for food.
 Jack and Bili are friends if both of them love to play cricket.
 Ryan will go to play if school is closed, and he is free.

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.

Target Group: G5 Software Engineering Prepared by Melaku M Page 11


Introduction to Artificial Intelligence Lab Manual

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.

Queries: Who is the friend of jack?

?- 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).

true. //True: means lili is happy.

Queries: Are jack and bili friends?

?- friends(jack, bili).

true. //True: means jack and bili are friends.

Queries: Are Kebede and bili friends?

?- friends(kebede, bili).

False. // False: means kebede and bili are not friends.

Target Group: G5 Software Engineering Prepared by Melaku M Page 12

You might also like