Sie sind auf Seite 1von 4

Prolog | An Introduction

Introduction :

Prolog is a logic programming language. It has important role in artificial intelligence. Unlike
many other programming languages, Prolog is intended primarily as a declarative
programming language. In prolog, logic is expressed as relations (called as Facts and Rules).
Core heart of prolog lies at the logic being applied. Formulation or Computation is carried out
by running a query over these relations.

 Prolog stands for Programming in logic. It is used in artificial intelligence

 Prolog is a declarative programming language.
For example: While implementing the solution for a given problem, instead of
specifying the ways to achieve a certain goal in a specific situation, user needs to
specify about the situation (rules and facts) and the goal (query). After these stages,
Prolog interpreter derives the solution.
 Prolog is useful in AI, NLP, databases but useless in other areas such as graphics or
numerical algorithms.

Key Features :
1. Unification : The basic idea is, can the given terms be made to represent the same
2. Backtracking : When a task fails, prolog traces backwards and tries to satisfy previous
3. Recursion : Recursion is the basis for any search in program.

Prolog facts

 A fact is something that seems to be true.

For example: It's raining.
 In Prolog, facts are used to form the statements. Facts consist of a specific item or
relation between two or more items.

How to convert English to prolog facts using facts and rules?

It is very simple to convert English sentence into Prolog facts. Some examples are explained
in the following table.

English Statements Prolog Facts

Dog is barking barking(dog)
Jaya likes food if it is delicious. likes( Jaya, Food):-delicious(Food)
In the above table, the statement 'Dog is barking' is a fact, while the statement 'Jaya likes food if it
is delicious' is called rule. In this statement, variable like 'Food' has a first letter in capital, because
its value came from previous fact. The symbol ':-' is used to denote that “Jaya likes delicious food”.

Arithmetic Operations in Prolog

 Prolog provides the facility for arithmetic operations.

 As per the requirement of the user, arithmetic operations can be divided into some special
purpose integer predicates and a series of general predicates for integer, floating point and
rational arithmetic.
 The general arithmetic predicates are handled by the expressions.
 An expression is either a function or a simple number.
 Prolog arithmetic is slightly different than other programming languages.
For example:
?- X is 2 + 1.

In the above example, 'is' is used as a special predefined operator.

The basic arithmetic operators are given in the following table:

Sr. No Operator Explanation

1 X+Y The sum of 'X' and 'Y'
2 X-Y the difference of 'X' and 'Y'
3 X*Y The product of 'X' and 'Y'
4 X/Y The quotient of 'X' and 'Y'
5 X^Y 'X' to the power of 'Y'
6 -X Negation of 'X'
7 abs(X) Absolute value of 'X'
8 sqrt(X) The square root of X
9 sin(X) The sine of X
10 cos(X) The cos of X

Operator precedence

 If there is more than one operator in the arithmetic expression such as A-B*C+D, then the
prolog decides an order in which the operator should be applied.
 Prolog gives numerical value for each operator, operators with high precedence like '*' and
'/' are applied before operators with relatively low precedence values like '+' and '-'.
 Operator with same precedence value ('*' or '/') and ('+' or '-') should be applied from left to
 So, the expression A-B*C+D can be written as A-(B*C)+D
Matching and Unification in Prolog
Definition: The two terms are said to be matched, if they are equal or if they consist of variables
representing the resulting equal terms.
Prolog matches expressions in structural way. So,
?- 3 + 2= 5
Note: In prolog '=' means matches with.

Consider the following example,?- X + 3 = 2 * Y

But the following expressions will match because they have same structure.

xpression 1:
?- X + Y = 2 + 3

Expression 2:
?- 2 + Y = X + 3

Prolog Lists:

 Lists are the finite sequence of elements.

 Prolog uses […] to build a list.
 The notation [X|Y] represents that the first element is X and second element is Y (X is head
and Y is tail).
 Prolog has some special notation for lists:
I) [a] [honda, maruti, renault]
ii) [a,b,c) [pen, pencil, notebook]
iii) [] represents the empty list.

Example 1: Pattern Matching in Lists

?- [a,b] = [a,X]
?- [a,b] = [X]

Example 2:
Consider the following lists:
[a, b, c, d, e, f, g]
[apple, pear, bananas, breadfruit]
[ ] this is an empty list
Now, consider some comparisons of lists:
[a,b,c] matches with [Head|Tail] resulting in Head=a and Tail=[b,c]
[a] matches with [H|T] resulting in H=a and T=[]
[a,b,c] matches with [a|T] resulting in T=[b,c]
[a,b,c] doesn't match with [b|T]
[] doesn't match with [H|T]
[] match with []. Hence, two empty lists get matched with each other.