Prolog Lab Exercise
Prolog Lab Exercise
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:
What is Prolog?
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?
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.
Atomelephant b, abcXYZ, x_123
Also, any series of arbitrary characters enclosed in a single quote denotes an atom.
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.
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.
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).
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
below:
Mother Relationship
7
Sister Relationship
mother(M,N): par
ent(M,N), female(M).
=N.
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).
parent(somit,komal).
parent(somit,manish).
male(komal).
male(manish).
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:
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.
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