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

Prolog Lab Exercise

The document serves as a lab manual for an Introduction to Artificial Intelligence course focusing on Prolog, a logic programming language. It covers Prolog syntax, terms, clauses, and examples of coding, particularly in the context of creating family trees and relationships. By the end of the course, students are expected to implement various AI techniques using Prolog effectively.

Uploaded by

kedir
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)
3 views

Prolog Lab Exercise

The document serves as a lab manual for an Introduction to Artificial Intelligence course focusing on Prolog, a logic programming language. It covers Prolog syntax, terms, clauses, and examples of coding, particularly in the context of creating family trees and relationships. By the end of the course, students are expected to implement various AI techniques using Prolog effectively.

Uploaded by

kedir
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/ 14

Department of Computer Science

Introduction to Artificial Intelligence -Prolog Lab Manual


Topic to be covered

 Introduction to Prolog
 Prolog syntax.
 Examples on prolog coding

I. Introduction to Prolog
Prolog (PROgramming with LOGic) is a logic programming language widely utilized in
Artificial Intelligence. It is a high-level programming language that enables the user to build
programs by stating what they want the program to do rather than how it should do it. Due to
Prolog's grounding in first-order predicate logic, it is intended to be more analogous to human
deductive reasoning than other programming languages.

AI is an undergraduate-level course intended to provide students with all the technical skills
needed to implement Artificial Intelligence techniques in compact and efficient Prolog programs.
By the end of the module, students will be able to construct Prolog programs of all sizes and
complexity that implement a range of AI techniques. The students will be able to utilize the skills
learned during this course in many areas:

 Natural Language Processing and Generation,


 Knowledge Representation and Reasoning,
 Database Search,
 Problem Solving,
 Planning,
 Cognitive Modelling,
 Logic and Theorem Proving,
 as well as providing computational skills that can be utilized in their MSc project in the
future.

What is Prolog?

 Is one of the most widely used programming languages in AI research. As opposed to


imperative languages such as C and Java (OO)is a declarative programming language.
1
(When implementing a solution to a problem instead of specifying how to achieve a
certain goal in a certain situation, we specify what the situation and goal are, and let the
prolog interpret the real solution for us.
 Prolog is very useful in some problem areas, like AI, NLP, databases, … etc., but it is
pretty useless in others like graphics or numerical algorithms.
 Programming in prolog means describing the world. Using such programs means asking
prolog questions about the previously described world. The simplest way of describing
the world is by stating facts, like this one:
bigger(elephant, horse).
bigger(horse, donkey).
bigger(donkey, dog).
bigger(donkey, monkey).

 This states, the fact that an elephant is bigger than a horse. (Whether the world described
by a prolog program has nothing to do with our real world, is of course it is entirely up to
the programmer.)
 After having compiled it, we can ask the Prolog system questions or queries in proper
Prolog jargon) about it. Here is an example.
 The bigger(donkey, dog). That is the question is (“Is a donkey bigger than a dog succeeds
because the fact bigger(donkey, dog) has been communicated to the Prolog system
before.
 Now, is a monkey bigger than an elephant?

II. Prolog Syntax


Terms

The central data structure in Prolog is that of a term. There are terms of four kinds:

 Atoms
 Numbers
 Variables and
 Compound terms
i) Atoms
Atoms are usually strings made up of lower- and upper-case letters, digits, and the
underscore, starting with a lowercase letter. The following are all valid Prolog atoms.
Atomelephant b, abcXYZ, x_123
Also, any series of arbitrary characters enclosed in a single quote denotes an atom.

‘This is a prolog item’

ii) . Numbers: can be in integers or in float.


iii) Variables
Variables are strings of letters, digits, and the underscore, starting with a capital letter or an
underscore.

Variables X, Elephant, _4711, X_1_2, MyVariable, _

The last one of the above examples (the single underscore) constitutes a special case. It is
called the anonymous variable and it is used when the value of a variable is of no particular
interest.
iv)Compound terms
 Compound terms are made up of a functor (a prolog atom) and a number of
arguments. (prolog terms i.e. atoms, numbers, variables, or other compound terms)
enclosed in parentheses and separated by a comma. The following are some
examples of compound terms.
Is_bigger(horse,X). f(g(X,_), 7), ’My Functor’(dog).
 It is important not to put any blank characters between the functor and the opening
parenthesis, or Prolog won’t understand what you are trying to say.
 In prolog the word functor, is used to refer to the atom at the start of the structure,
along with its arity, i.e. the number of arguments it takes.
For example, likes(mary, pizza).
Likes/2 is the functor (like is a functor which has 2 arguments)
Example ? functor(likes(mary,pizza),Name, Arity).

Name= likes
Arity=2

2. Clauses

Prolog programs are made up of facts and rules. Facts and rules are called clauses.

 Facts
3
A fact is a predicate followed by a rule. A predicate is the name given to the word occurring
before the bracket in a fact or rule.

Example: bigger(cat, rat).

Its reads as, Cat is bigger than rat.

 Rule
 Rules allow us to understand that a property or relationship holds based on
preconditions.
 A rule consists of a head (a predicate) and a body (a sequence of predicates
separated by a comma). Head and body are separated by the sign: - and, like
every prolog expression a rule has to be terminated by dot.
 Examples: for all X and Y
If X is the mother of Y then
X is a parent of Y
 Here the prolog clauses is:
Parent(X,Y) :- mother(X,Y). are called rules.
(Head) (body)
 Rule
 There is an important difference between facts and rules. A fact like parent (tom,
liz). is something that is unconditionally true. On the other hand, rules specify
things that are true if some conditions are satisfied.

III. Prolog Syntax

3. Recursive Rules (Predecessor relations)


 This relation will be defined in terms of parent relation.
 The whole definition can be expressed with two rules. The first rule
will define the direct (immediate) predecessors and the second rule the
indirect predecessors. We say that some X is an indirect predecessor
of some Z. if, there is a parentship chain of people between X and Z
as illustrated in the below figure.

(a) (b)

 The first rule as in Fig (a), is simple and can be formulated as:
for all X and Z,
X is a predecessor of Z if
X is a parent of Z.
 This is straightforwardly translated into Prolog as:
predecessor (X, Z): - parent (X, Z).
 The second rule can be formulated as:
For all X and Z.
X is a predecessor of Z if
there is a Y such that
(1) X is a parent of Y and
(2) Y is a predecessor/parent of Z.
 This is straightforwardly translated into Prolog as:
predecessor (X, Z): - parent (X, Y), parent/predecessor (Y, Z).

IV. Some Examples on Prolog Code


 Write a program to create a database for hobbies of different person.
 First open text file save that in filename .pl
 Prolog Program

% hobbies of different persons.


hobby(abebe, gamming).
hobby(aster, dancing).
hobby(desta, painting).
hobby(Ahmed, cycling).

Write the following statement in the prolog Query and check their answers.
1. hobby(rahel, movie). Answer: True
2. hobby(ahmed, painting). Answer: False
3. hobby(X, cycling). Answer: X=ahmed
4. hobby(Y, dancing). Answer: Y=aster
5. hobby(Z, gamming). Answer: Z=abebe
6. hobby(W, painting). Answer W=desta
7. hobby(X, movie). Answer= rahel
5
8. hobby(X,Y). check the answer use “;” to check answers

Prolog Family Tree


Updated April 5, 2023

Introduction to Prolog Family Tree


The prolog family tree, defined as the prolog, is a logical

programming language with symbolic and non-numeric data

processing. It is especially well suited for solving problems that


require objects and relationships between the objects. It is a

user-friendly programming language because it uses logic in its

programming, prolog family trees can be constructed by using

the facts, rules, and queries, collection of facts and rules is

called a knowledge-base, which describe the relationship of

their objects. The prolog programming is all about knowledge-

base and by posing queries means by asking questions to

prolog about the information stored in knowledge-base.

Syntax of Prolog Family Tree


The prolog syntax for mother-sister relationship is given as

below:

Mother Relationship

7
Sister Relationship

 mother(M,N): par

ent(M,N), female(M).

 sister(M,N): parent(O,M), parent(O,N), female(M), M\=

=N.

We can also define:

 father(M,N): parent(M,N),female(M).
 haschild(M): parent(M,_).
 brother(M,N): parent(O,M), parent(O,N), male(M),M\==N.
Where _(underscore) indicates that it is an anonymous variable.
We can also write the syntax for some other relationships as
follows:
 grandparent(M,N): parent(M,O), parent(O,N).
 grandmother(M,O): mother(M,N), parent(N,O).

 grandfather(M,O): father(M,N), parent(N,O).


 wife(M,N): parent(M,O), parent(N,O), female(M),male(N).
 uncle(M,O): brother(M,N), parent(N,O).
How to Create a Family Tree in
Prolog?
The program in prolog specifies the relationship between
objects and the properties of objects; the family trees tell us
how to construct a database of family. The database also
contains facts and rules; let us consider the example “Sumit
has a car.” We can declare the original relationship between
two objects where one object is Sumit. Another object is a car;
if we say, “does Sumit own a car?” there are many types of
relationships; some of them are ruled by using rules in the
program, we can find the relationship between objects used,
and it is not defined as a fact. Tree diagrams are very good in
representations the information is clearly mentioned, and due
to that, users can understand easily, our programs in prolog are
the sets of clauses.
Brother relationship:

 If they both are male.

 If they have the same parent.

Suppose we have some clauses to illustrate the relationship:

 parent(somit,komal).

 parent(somit,manish).

 male(komal).

 male(manish).

 brother(X,Y): parent(Z,X), parent(Z, Y), male(X), male(Y)

From the above clauses, we can conclude that b and c are


brothers, but we can create 3 combinations that are true,

9
combinations are (komal,komal), (komal,manish), (manish,manish) but actually
(komal,komal) and (manish,manish) are not real brothers because they are same brothers.
Let us see the working of the family tree by considering an example that can be formed from the
prolog family tree. We want to make a family tree and as per our definition that can be mapped
into facts and rules, so that we can run some queries on them,
The sample family tree is given below:

Here we have one family tree, so from the above


sample of the family tree, there are some
relationships between them; from the above family tree diagram, we can say
that ‘C’ is a child of A and B, which means that A is a parent of C and B is
also a parent of C, B also has one child D, C has one brother D, whose parent
is also A and B, so we can make predicates to call families as follows, C has
two child E and F and F also has a child G, following above family tree we
have written some clauses below, and every clause must be terminated by a
full stop(.).

 parent(A,C).
 parent(B,C).
 parent(B,D).
 parent(C,E).
 parent(C,F).
 parent(F,G).

From the above examples, we can illustrate some important points that we
can define parent relation by taking n-number of objects which is based on
the given information of family tree so that user can easily pose the query to
prolog system about relations which are defined in programs, the prolog
programs terminated by clauses and that consists of clauses, the arguments
of relations are of a constant type or related to any general object as we
given above, objects are of atom or variables types.

Examples of Prolog Family Tree


Different examples are mentioned below:

Example #1
Knowledge base
Code:
female(pammi).

female(lizza).

female(patty).

female(anny).

male(jimmy).

male(bobby).

male(tomy).

male(pitter).

parent(pammi,bobby).

parent(tomy,bobby).

parent(tomy,lizza).

parent(bobby,anny).

parent(bobby,patty).

parent(patty,jimmy).

parent(bobby,pitter).

parent(pitter,jimmy).

mother(X,Y):- parent(X,Y),female(X).

father(X,Y):- parent(X,Y),male(X).

11
haschild(X):- parent(X,_).

sister(X,Y):- parent(Z,X),parent(Z,Y),female(X),X\==Y.

brother(X,Y):-parent(Z,X),parent(Z,Y),male(X),X\==Y.

Input:

parent(X,jimmy).

mother(X,Y).

haschild(X).

sister(X,Y).

Output:

Above is the sample prolog program; the prolog program has three sections,
mainly domains, the second one is the predicate, and the third is clauses,
under domain declaration can be done, so under the predicate section we
can declare all the predicates in which the clauses will be defined, so we
have given names in the above program, we have also declared some
knowledge base, the clauses are the facts in prolog program, we have taken
some facts, and some rules will be the members of the respective clauses to
show the relationship between parent, mother, and about has child, it is a
knowledge base family tree, the outputs are as shown in the screenshot.
Example #2
Code:
female(trisha).
female(joseph).
male(rahil).
male(karim).
male(kabil).
male(riya).
parent(rahil,karim).
parent(rahil,kabil).
parent(karim,trisha).
parent(karim,joseph).
son(X,Y):-male(X) ,parent(Y,X).
daughter(X, Y):- female(X) ,parent(Y ,X).
sibling(X, Y):- parent(Z, X) ,parent(Z,Y), X \= Y.
Input:
son(karim,rahil).
son(joseph,karim).
daughter(trisha,kabil).
daughter(riya,kabir).
sibling(karim,kabil).

Output:
In the above program, we have illustrated the family tree in the form of facts
and rules; we have written rules to get son, daughter, and sibling so that to
describe the relationship between objects.

Conclusion
In the above article, we conclude that in prolog relation, the specification is
between the objects and the properties of the object, as we have seen in this
article we can make relationship easily by making family trees and
illustrating them, in prolog program the family trees can be illustrated by
taking facts, rules and by posing queries.

13

You might also like