You are on page 1of 17

PROLOG

1.1 Background And History

Prolog was invented in the early seventies at the University of


Marseille. Prolog stands for PROgramming in LOGic. It is a
logic language that is particularly used by programs that use
non-numeric objects. For this reason it is a frequently used
language in Artificial Intelligence where manipulation of
symbols is a common task. Prolog differs from the most
common programmings languages because it is declarativre
language. Traditional programming languages are said to be
procedural. This means that the programmer specify how to
solve a problem. In declarative languages the programmers
only give the problem and the language find himself how to
solve the problem.
Although it can be, and often is, used by itself, Prolog
complements traditional languages when used together in the
same application.
 1.2 Special Features

 Prolog is a rich collection of data structures in the


language and human reasoning, and a powerful
notation for encoding end-user applications. It has
its logical and declarative aspects, interpretive
natur, compactness, and inherent modularity.
 Intelligent Systems - programs which perform
useful tasks by utilizing artificaial intelligence
techniques.
 Expert Systems - intelligent systems which
reproduce decision-making at the level of a
human expert.
 Natural Language Systems - which can analys and
respond to statements made in ordinary language
as opposed to approved keywords or menu
selections.
 Relational Database System

 1.3 Fields of Application

 Prolog is the highest level general-purpose
language widely used today. It is taught with a
strong declarative emphasis on thinking of the
logical relations between objects or entities
relevant to a given problem, rather than on
procedural steps necessary to solve it. The
system decides the way to solve the problem,
including the sequences of instructions that the
computer must go through to solve it. It is easier
to say what we want done and leave it to the
computer to do it for us. Since a major criterion in
the commercial world today is speed of
performance, Prolog is an ideal prototyping
language. Its concept makes use of parallel
architectures. It solves problems by searching a
knowledge base (or more correctly a database)
which would be greatly improved if several
processors are made to search different parts of
the database
 2.1 Constants and variables

 A Prolog variable can be instantiated to any


term, regardless of whether the term is a
constant, a variable or a structure. During
goal execution, Prolog variables are
instantiated to terms by unification and
therefore assume all the attributes of these
terms. This instantiation remains in force
during goal execution and is not undone until
backtracking takes place. Any attempt to
instantiate an already instantiated variable to
another value will lead to the failure of the
unification and will initiate backtracking to
the last choice point. If two variables are
instantiated to each other, they will be
regarded as identical in subsequent
processing.

 

 2.2 Data types


 Prolog's single data type is the term. Terms are


either atoms, numbers, variables or compound terms.

 An atom is a general-purpose name with no inherent meaning. Examples
of atoms include x, blue, 'Taco', and 'some atom'.
 Numbers can be floats or integers.
 Variables are denoted by a string consisting of letters, numbers and
underscore characters, and beginning with an upper-case letter or
underscore. Variables closely resemble variables in logic in that they
are placeholders for arbitrary terms.
 A compound term is composed of an atom called a "functor" and a
number of "arguments", which are again terms. Compound terms are
ordinarily written as a functor followed by a comma-separated list of
argument terms, which is contained in parentheses. The number of
arguments is called the term's parity.
 An atom can be regarded as a compound term witharity zero. Examples
of compound terms are truck_year('Mazda',
1986) and 'Person_Friends'(zelda,[tom,jim]).
 Special cases of compound terms:
 A List is an ordered collection of terms. It is denoted by square brackets
with the terms separated by commas or in the case of the empty
list, []. For example [1,2,3] or[red,green,blue].
 Strings: A sequence of characters surrounded by quotes is equivalent to

 2.3 DATA STRUCTURES


 All data types are inherently abstract.


 A data type is defined by giving an
algebraic description of the properties
of its operators.
Prolog provides no data constructors.
 Prolog provides a built-in operator and
some syntactic simplifications for
representing lists.
 Strings are represented internally as lists.
 Example of a user-defined data type: set.

 

 3.1 Character set


 

  The Prolog character set consists of


the following classes of characters: 
 Letters, digits, underscore character
 Special characters
 Prolog-specific characters
 Delimiters

 3.2 Operators

 Operators provide a way of specifying an
alternate input/output syntax for structures
with one or two arguments.
 There are a number of predefined operators in
Prolog, such as: +, /, *, -.
 We emphasize that declaring operators only
serves to alter the way structures may look on
input or output. Once inside the Prolog
system, all structures are kept in the same
internal form.
 If an operator is declared to be used between its
two arguments, we say it is an infix operator.
If it is to be used before its single argument
then it is a prefix operator; if it is to be used
after its argument it is a postfix operator.
Operators may be declared to be both infix
and either pre- or post- fix, in this case they
are called mixed operators.
 3.3 Structure of Prolog Programs

 Every entry into the interpreter is


a goal that Prolog tries to satisfy.
 Every goal in Prolog ends with ``.''.
 A Prolog program consists of a database
of facts, rules, and queries. This
program is, in essence, a knowledge
base.

 4.1.1 Expression and Operator Precedence

 The Prolog built-in predicate op serves to define the Type and
Precedence of infix and postfix, and prefix operators in Prolog
terms.

 Syntax:
 
 :- op(+Precedence, +Type, :Name).

 The first piece of information required for each operator (whether
pre, in or post -fix) is a number between 1 and 1200 called
the precedence of the operator.
  When combining different operators together, the principal
functor of a term represented by a series of operators is the
operator with highest precedence.
 For example, suppose + is defined to have precedence 500
and * is defined to have precedence 400. Consider:
 a + b * cWe start reading from the left. + has higher precedence,
so it must be the principal functor of the constructed term.
Therefore the term must be:
 '+'(a, '*'(b, c))This corresponds naturally to the high school
algebra rule "do multiplications first".

 4.1.2 Conditional Expression

 Operator which attempts goals conditionally  ?-
X -> Y; Z. Keywords: or, conditional,
conditional statement, arrow, operator,
symbol  

 POPLOG Prolog allows for a simple form of
"conditional statement" in Prolog programs.
The goal: 

 ?- X -> Y; Z. means if X can be satisfied (only
consider its first solution), attempt Y,
otherwise attempt Z.

 Note: The conditional cannot appear in a top-
level goal.
 4.2.1 Selection Statement

 We axiomatize the Prolog selection rule which
always selects the leftmost literal in a goal.
We introduce a new completion of a logic
program which we call the ?-completion of the
program. The ?-completion is formulated as a
first-order theory in a language extended by
new predicate symbols which express
success, failure and left-termination of
queries. The main results of the paper are the
following. If a query succeeds, fails or is left-
terminating under the Prolog selection rule,
then the corresponding formula in the
extended language is provable from the ?-
completion. Conversely, if a logic program
and a query are correct with respect to some
mode assignment and if one can prove in
the ?-completion that the query succeeds and
is leftterminating, then the goal is successful
and Prolog, using its depth first search, will
compute an answer substitution for the goal.
 4.2.2 Recursion & Iteration
 

  A piece of code which executes


repeatedly based on the different
inputs. In general, before solving any
large problem first, we need to divide
the problem and then you need to
solve either using iterative way or
recursion way.

 Iteration sample:

unsigned long FactorialI(int Num)


{

int Result = 1;
 

for ( ; Num > 0; Num-- )

Result = Result * Num ;

return(Result);

}

This function will take 'Num' as parameter and executes the

same code inside the loop till it satisfies exit condition


i.e 'Num > 0'.
For ex: If we pass 5 as parameter to this function it will

execute as following:
Result * Num ->     1*5 =     5 (5 is stored in 'Result' variable)

Result * Num ->     5*4 =   20 (20 is stored in 'Result' variable)

Result * Num ->   20*3 =   60 (60 is stored in 'Result' variable)

Result * Num ->   60*2 = 120 (120 is stored in 'Result'

variable)
Result * Num -> 120*1 = 120 (120 is stored in 'Result'
Sample Logic In Prolog
 love(cats)
love(dogs)
animalsthatilove(X):-love(X)
will show:
cats
dogs
loverelations(cats,dogs)
loveofrelated(X):-loverelations(cats,X)
will show:
dogs

End
of
Presentation

You might also like