0% found this document useful (0 votes)
36 views6 pages

13 Prolog

prlog 1

Uploaded by

huzaifaazeem48
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views6 pages

13 Prolog

prlog 1

Uploaded by

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

DEPARTMENT OF COMPUTER SCIENCE

( Rachna College of Engineering and Technology Gujranwala )

ARTIFICIAL INTELLIGENCE

Name: Registration No:

LAB 13

INTRODUCTION TO PROLOG
Prolog is a programming language particularly well suited to logic and artificial intelligence
programming. "Prolog" in fact stands for "Programming in Logic." In this brief introduction we
will try to give you a little taste of Prolog without bogging you down with a great deal of
technical jargon. By the end of this section you should be able to use Prolog and write some little
programs that give you a feel for the language.
A person uses a computer programming language to direct a computer to perform desired tasks.
A Prolog program is likely to look a little different than a typical C program. The programmer
tells the computer less the "how" than the "what." For example, in Prolog the programmer might
begin by telling the computer a bunch of "facts." The facts can be about characteristics of people
or things in the world ("Spot is frisky"), about relations of such things ("John is the father of
Susan"), and about "rules" pertaining to such facts ("Scott is the grandfather of Susan" is true if
"Scott is father the John" is true and "John is the father of Susan" is true).
The Prolog program could then be used to ask the computer about the facts already given and the
computer would be able to provide answers. Given the facts in the previous paragraph, if the
computer is asked "Is John the father of Susan?" it would reply "Yes." If asked, "Is John the
father of James" it would reply "No" because it was not given that fact or other facts from which
it could be deduced.
Of course, Prolog, like any other common programming language, will not be able to process
ordinary English sentences such as those given above; rather it requires the programmer to write
statements in a particular way . The programmer must know how to phrase Prolog sentences
properly.
We will use SWI-Prolog to write and run the Prolog Programs. First write all the facts in a file
having extension .pl. After that consult your written program by choosing name of your program
in SWI-Prolog Compiler and then Ask queries and prolog will Answer you according to the
given facts.
Now after consulting StudentsProfessor.pl we will ask question and will get answer by Prolog.

Let's tell the computer some facts about a family we know. We'll put the Prolog statement after
the English statement.
English Prolog
John is the father of Susan. father(john,susan).
John is the husband of Martha. husband(john,martha).
John eats pizza. eats(john,pizza).
Susan eats pizza. eats(susan,pizza).
Martha eats pizza. eats(martha,pizza).
Susan eats oranges. eats(susan,oranges).
John bought pizza for Martha. bought(john,pizza,martha).
Susan is tired. tired(susan).
Let's continue with examples, but this time with questions you can ask Prolog. Below are sample
English questions, their Prolog equivalent, and the answer from Prolog. Assume we have loaded
the above facts into Prolog already.
English question Prolog (at prompt ?-) Prolog responds
Is John the father of Susan? father(john,susan). yes (or true)
Is Susan tired? tired(susan). yes
Is Martha tired? tired(martha). no (or false)
Who is tired? tired(X). X = susan
Who is the husband of Martha? husband(X,martha). X = john
Who eats pizza? eats(X,pizza). X = john
Who else eats pizza? merely hit spacebar X = martha
Who else eats pizza? hit spacebar again X = susan
Who else eats pizza? hit spacebar yet again no
Rules
Now that we understand the structure of simple Prolog statements and queries, we can
investigate some more complicated statements. We can give Prolog some facts about
conditionals, that is, what happens if something else is the case.
English Prolog
Martha is the wife of John, if John is the husband of wife(martha, john) :- husband (john,
Martha. martha).
John eats a pepperoni pizza. eats (john, pizza (pepperoni)).
X and Y are parents of Z if X is the mother of Z and Y is parent(X,Y,Z) :- mother(X,Z) ,
the father of Z father(Y,Z).
Sparky barks at anyone who walks quickly. barks(sparky,X) :- walks(X,quickly).
A equals B A = B.
A does not equal B not(A=B).
General Structure of Prolog Statements:

Example 1 :
Below food table shows the facts, rules, goals and their english meanings.
Facts English meanings
food(burger). // burger is a food
food(sandwich). // sandwich is a food
food(pizza). // pizza is a food
lunch(sandwich). // sandwich is a lunch
dinner(pizza). // pizza is a dinner
Rules
// Every food is a meal OR
meal(X) :- food(X). Anything is a meal if it is a
food
Queries / Goals
?- food(pizza). // Is pizza a food?
// Which food is meal and
?- meal(X), lunch(X).
lunch?
?- dinner(sandwich). // Is sandwich a dinner?

Example 2 :
Below student-professor relation table shows the facts, rules, goals and their english meanings.
Facts English meanings
studies(charlie, csc135). // charlie studies csc135
studies(olivia, csc135). // olivia studies csc135
studies(jack, csc131). // jack studies csc131
studies(arthur, csc134). // arthur studies csc134
teaches(kirke, csc135). // kirke teaches csc135
teaches(collins, csc131). // collins teaches csc131
teaches(collins, csc171). // collins teaches csc171
teaches(juniper, csc134). // juniper teaches csc134
Rules
professor(X, Y) :- teaches(X, C), studies(Y, C). // X is a professor of Y if X teaches C and Y
studies C.
Queries / Goals

?- studies(charlie, What). // charlie studies what? OR What does charlie study?


?- professor(kirke, Students). // Who are the students of professor kirke.
?- studies(Who, csc135).
?- studies(charlie, Which), teaches(Who,Which), write('charlie studies '), write(Which), write('
and professor '), write(Who), write(' teaches '), write(Which).

LAB TASK

Now that you have an idea of how Prolog programming works, try out some exercises to get a
better feel for Prolog.

1. Given:

If John feels hungry, then he eats quickly. If he eats quickly, he gets heartburn. If he gets
heartburn, he takes medicine. John feels hungry.

Conclusion: Given the above facts, what can we conclude about John taking medicine?
Does he or doesn't he?

2. Many Prolog textbooks use family relationships to illustrate Prolog. Can you construct a
family?
Given:
Pete is the parent of Mike.
Pete is the parent of Julie.
Pete is the parent of Amanda.
Mary is the parent of Mike.
Mary is the parent of Julie.
Mary is the parent of Amanda.
Mary is female.
Julie is female.
Amanda is female.
Mike is male.
Pete is male.
Mike is the sibling of Julie.
Mike is the sibling of Amanda.
Amanda is the sibling of Mike.
Julie is the sibling of Mike.
A male parent is a father.
A female parent is a mother.
A male sibling of someone is a brother of that person, unless the person in question is
yourself.
A female sibling of someone is a sister of that person, unless the person in question is
yourself.

Ask Prolog:

Who is a sister of Mike?


Who is a father of Julie.
Is Amanda a sister of Julie?
Is Mike a sister of anyone?

You might also like