Академический Документы
Профессиональный Документы
Культура Документы
AMITY UNIVERSITY
----------UTTAR PRADESH----------
Submitted to:
Submitted By:
Name:
Lecturer
Enrollment No.:
Class:
ASET
INDEX
S.
No.
Program
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Signatur
e
INTRODUCTION TO PROLOG
Prolog is a computer programming language that is based on logic. Most computer
languages are based on the steps needed to solve a problem. The Prolog
language, on the other hand, is a "declarative" language which indicates the
logical relationships between entities. The way to solve the problem is left to the
computer.
The name Prolog comes from the French PROgrammation en LOGique, that is,
PROgramming LOGic. Prolog is used for solving problems that involve objects
and the relationships between objects.
It is widely used to implement symbolic inferences in the context of artificial
intelligence.
The Prolog is a logic programming language based on two mains mechanisms: the
backtracking and the unification.
The backtracking is the fact to go from the wanted goal, to look for the rules of
which the goal is the conclusion, then, by taking the conditions of this rules as new
under goal, redo the search recursively, and so build a base with all the facts found.
Finally, there only remain to unify the found facts with the required goal.
The unification is the fact to try to make two assertions equals (generally, a fact and a
rule) by giving to the variables, that they contain, some values.
It is a clear and a readable language for any user, and writing a program with it is
easy. It is especially used to build some systems on the natural languages or some
expert systems.
THE DOMAINS
In Prolog, the "domains" section allows us to define the types which will be used in
the program.
There are several predefined types:
symbol
A symbol is a succession of characters which must begin with a lower case character.
In order to be able to make a symbol begin by an upper case character, or to put some
blank spaces in it, the symbol must be encapsulated with two double quotes ("").
Examples of symbols:
example
an-other-example
again_a_symbol
"A_symbol_with_upper_case"
"a symbol with blank spaces"
integer
The integers are all the integer numbers between -32768 and +32767
real
The reals are the real numbers between +/- 1e-307 et +/- 1e+308
Examples of reals :
4
-97456
42.87
Remark : In Prolog, the decimal separator is the dot character (.)
char
These are all the character. A char is always written between two simple quotes (' ').
Example : 'c'.
string
These are the character strings. A string is always written between two double quotes
(" ").
Example : "hello world".
An example of types declarations.
domains
my_int = integer /* dfinition of a simple type my_int which is of the type integer */
list_string = string* /* the character '*' after the type to indicate that a list is defined,
here a string list */
THE PREDICATES
The predicates express a relation between their arguments, or a property of one of
these.
For example, look the predicate father.
father(pierre, emma) indicate that a relation exists between pierre and emma, this
relation is father.
An other type of predicate would be, for example, positive(3). This predicate would
indicate whether 3 is positive or not.
In short, in Prolog, the predicates are the "functions".
In the predicates section of a Prolog written program, the non predefined predicates
are declared like the following example:
predicates
father(symbol,symbol) /* father takes two parameters of symbol type */
positive(integer) /* positive takes one parameter of the integer type */
There remains only the definition of the rules of the predicates and the facts, these are
the clauses.
THE CLAUSES
The clauses is a set of facts and rules. This set of clauses forms the knowledge base
which
will
be
used
by
the
program
to
reason.
At the time of the call of a predicate, Prolog will try to solve it, so it look for the rules
and the facts associated to this predicate and will solve all that is necessary.
Progressively with its demonstrations, Prolog makes go up the results of all its
demonstrations, and give as answer all the solutions of the question asked at the
beginning.
Few rules must be followed to write a good program with Prolog:
at least one clause must be written for a declared predicate;
all the clauses concerning a same predicate must be gathered;
each clause (fact or rule) ends with a dot.
A fact is a relation between two or more objects.
Example:
father(pierre, emma) /* this fact affirms that pierre is emma's father */
A fact is always true.
A rule is a conditional relation. A rule has a specific form which is:
conclusion IF condition(s).
The conclusion is proven only if all the conditions are true.
A rule can have several conditions, so we will use the keywords AND and OR to
separate them.
The keywords IF, AND and OR are very often used, so we will use these
abbreviations:
IF :AND ,
OR ;
Example:
Let us consider that, in the predicates section, we have declared the predicates father
and grand_father, there are the clauses:
predicates
father(symbol, symbol)
grand_father(symbol, symbol)
clauses
father(jean, paul).
father(paul, marie).
grand_father(X,Y) IF father(X,Z) AND father(Z,Y).
Comments: In this example, all the clauses linked to the predicate father are facts
because they are not unconditional. The clause linked to grand_father is a rule. You
will notice the use of some symbols beginning with an upper case character(X, Y &
Z). In Prolog, any symbol beginning with an upper case character is considered as a
variable. So X, Y and Z can take all the values in their definition domain fixed in the
predicates section (here symbol). Now, let us translate the rule grand_father. X is the
grand_father of Y if X is the father of Z and that Z is the father of Y. It is also possible
to work with some variables in the rules in order to modify them.
THE GOALS
The goals allow people to question the program. There exist two types of goals, the
internal goals and the external goals.
The internal goals can't print a result on the screen nor make go up the solutions of
the problem. The internal goals are often used to carry out some operations that must
be done before to call an external goal (like the initialization of a graphic interface,
the creation of a dynamic data base).
The internal goals are written in the goals section of the program.
The external goals are taped in the "dialog" windows of the interpreter (for example
Borland Turbo Prolog 2.0).
An external goal is a call to our program. Prolog will solve this goal, it will make
all the solutions go up, and it will print a result if it exists.
Examples of external goals
father(jean, marie) /* ask the program if jean is the father of marie */
father(X, marie) /* ask the program if marie has one or several fathers and give all the
solutions */
The use of the variable allow Prolog to make all the solution go up.
father(jean, X) /* allow us to know all the children of jean, if they exist */
Software used
Borland Turbo Prolog 2.0
Domains
P=Symbol.
Predicates
Born(P,integer).
Male(P).
Female(P).
Parent(P,P).
Father(P).
Father(P,P).
Mother(P,P).
Sister(P,P).
Brother(P,P).
Son(P,P).
Daughter(P,P).
Uncle(P,P).
Aunt(P,P).
Child(P,P).
WifeOf(P,P).
HusbandOf(P,P).
GrandFather(P,P).
GrandMother(P,P).
Person(symbol).
Age(symbol,integer).
Elder_Brother(P,P).
Constants
Now=2008.
Clauses
Person(X) if Male(X) ; Female(X).
Age(X,A) if Person(X) , Born(X,Y).
Born("rajiv",1999).
Born("sanjay",1950).
Father("motilal" , "jawaharlal").
Father("jawaharlal" , "indira").
Father("feroz" , "rajiv").
Father("feroz" , "sanjay").
Father("rajiv" , "rahul").
Father("rajiv" , "priyanka").
Father(X) if Father(X,_).
Male(rahul)
WifeOf("menka" , "sanjay").
WifeOf("sanjay" , "rajiv").
WifeOf("indira" , "feroz").
Female(priyanka).
Female(X):- WifeOf(Y,X).
Elder_Brother(A,B) if eelder_brother.
Parent(X,Y) :- Father(X,Y),Mother(X,Y).
Mother(X,Y) :- WifeOf(X,Z),Father(Z,Y).
Child(X,Y) :- Parent(Y,X).
Son(X,Y) :- Child(X,Y) , Male(X).
Daughter(X,Y) :- Child(X,Y) , Female(X).
Brother(X,Y) :- Father(Z,X) , Father(Z,Y), Male(X) , not(X=Y).
Sister(X,Y) :- Father(Z,X) , Father(Z,Y), Female(X) , not(X=Y).
Aunt(X,Y) if Sister(X,Z) , Parent(Z,Y).
Aunt(X,Y) :- WifeOf(X,Z) , Uncle(Z,Y).
Grandfather(X,Y) :- Parent(X,Z), Parent(Z,Y) , Male(X).
Grandmother(X,Y) :- Parent(X,Z), Parent(Z,Y) , Female(X).
Output: