100% found this document useful (1 vote)
205 views27 pages

Introduction To PROLOG: Manish Khare M.Sc. (CS), M.Tech. (CSE)

Prolog is a logic programming language used for artificial intelligence and natural language processing. It is declarative rather than procedural, with programs expressed through logical facts and rules. Prolog was first developed in 1972 and remains popular for applications like expert systems, robot planning, automated reasoning, and more. Prolog programs consist of a database of facts and rules, and queries to test relationships between objects.

Uploaded by

chaudharysanty
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
205 views27 pages

Introduction To PROLOG: Manish Khare M.Sc. (CS), M.Tech. (CSE)

Prolog is a logic programming language used for artificial intelligence and natural language processing. It is declarative rather than procedural, with programs expressed through logical facts and rules. Prolog was first developed in 1972 and remains popular for applications like expert systems, robot planning, automated reasoning, and more. Prolog programs consist of a database of facts and rules, and queries to test relationships between objects.

Uploaded by

chaudharysanty
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Introduction to PROLOG

Manish Khare
M.Sc. (CS), M.Tech. (CSE)
What is PROLOG (Programming in Logic)
• Prolog is a general purpose logic
programming language associated with artificial
intelligence and computational linguistics.
• Prolog has its roots in formal logic, and unlike many
other programming languages, Prolog is declarative:
The program logic is expressed in terms of relations,
represented as facts and rules. A computation is
initiated by running a query over these relations.
• The language was first conceived by a group
around Alain Colmerauer in Marseille, France, in the
early 1970s and the first Prolog system was developed
in 1972 by Alain Colmerauer and Phillipe Roussel.
• Prolog was one of the first logic programming
languages, and remains among the most popular
such languages today, with many free and
commercial implementations available. While
initially aimed at natural language processing, the
language has since then stretched far into other
areas like theorem proving, expert systems,
games, automated answering
systems, ontologies and sophisticated control
systems. Modern Prolog environments support
the creation of graphical user interfaces, as well
as administrative and networked applications.
• Prolog is a Declarative Programming Language.
• A declarative Programming language means that
rather than describing how to compute a
solution, a program consists of a database of
facts and logical relationships(rules), which
describes the relationship, which hold for the
given application, rather than running a program
to obtain a solution, the user asks a question
Features of PROLOG
• Rule Based Programming: The rule-based
programming allows the program code to be
written in a form which is more declarative
than procedural.
• Built in Pattern Matching: It has an important
feature of built-in-pattern.
• Backtracking Execution: Backtracking provides
for the flow of control in the program.
Application of Prolog
• Intelligent Data Base Retrieval.
• Natural Language Understanding.
• Expert Systems.
• Specification Language.
• Machine Learning.
• Robot Planning.
• Automated Reasoning.
• Problem Solving.
How to Write Program in Prolog
• The procedure of writing and running a prolog
program usually follows the following steps:
1. The programmer starts by using an editor to construct
a file which consists of the statement that make up the
program, when the program in completed, the file is
saved.
2. A prolog interpreter is loaded into the memory of the
computer.
3. The program in loaded into the interpreter.
4. Feeding statements to the prolog interpreter tests the
program. The interpreter tries to prove the statements
using the statements in the program
Structure of Prolog Program
• A prolog program consists of a database of
facts and rules, and queries (questions)
– Facts
– Rules
– Query
– Variable: Must begin with an upper case letter.
– Constants: numbers, begin with lower case letter,
or enclosed in single quotes.
Facts
• Facts either consists of a particular item or a relation
between items.
• A facts is everyday language is often a proposition like
“The Dog Ran”.
• Facts Describe explicit relationship between objects
and properties objects might have.
• E.g. Anushka Likes Pizza.
Sky Has Color Blue.
bigger (rabbit, ant)
This states, quite intuitively, the fact that a rabbit is
bigger than an ant.
Rules
• Facts have some simple rules of syntax:
 Facts should always begin with lowercase letter and end
with full stop.
 The facts themselves can consist of any letter or number
combinations, as well as the underscore (_) character.
 However, names containing the characters -,+,*,/ or other
mathematical operators should be avoided.
 Rules allows as to make conditional statements about our
world. Each rule can have several variation, called clauses.
These clauses give us different choices about how to
perform inference about our world.
Queries
•A query in prolog is the Action of asking the program about information
contained within its data base.
•Queries usually occur in the interactive mode.
•After a program is loaded, you will receive the query prompt,
?-
•you can ask the program a question such as
?- 'It is sunny'.
and it will respond with the answer
Yes
?-
•A yes means that the information in the data base is consistent with the subject
of query. If the data base does not contain sufficient information to answer a
query, then it answers the query with a No.
•E.g. ?-’It is rainy’.
•No
Variables
• The arguments in a query are matched (or unified in prolog) to
select the appropriate rule:
• Suppose we had the following fact in our database:
likes(Manish,mangoes)
• How do we ask what Manish likes. We could type in something like:
?-likes(Manish,what)
• However Prolog will say no. The reason for this is that what does
not match with mangoes. In order to match arguments in this way
we must use a variable.
• The process of matching items with variables is known as
unification.
Variables(contd..)
• Suppose we want to ask, “What fruit does Manish Like”?
• This could be written as:
Is there a fruit, X, that Manish likes?
The variable X stand for an object, which the questioner does not know
about yet.
• To answer the question, Prolog has to find out the value of X, if it
exists. As long as we do not know the value of a variable it is said to
be unbound. When a value is found, the variable is said to bind to
that value.
• Rule for the variable:
The name of a variable must begin with a capital letter or an
underscore character “_”.
Variables(contd..)
• To ask Prolog to find the fruit, which Manish likes, the following
query is entered:
likes(Manish, fruit)?
Fruit=mango output from Prolog
• Prolog can find all possible ways to answer a query, unless you
explicitly tell it not to. Variables are distinguished by starting with
a capital letter. Here are some examples:
X /* a capital letter */
Variable /* a word-it be made up or either case of letters */
My_name /*we can link words together via ‘_’ (Underscore) */
Types
• Prolog provides for numbers, atoms, lists,
tuples and pattern. The types of objects that
can be passed as arguments are defined
Simple types.
Type Predicates.
Arithmetic Operators.
Expressions.
Boolean Predicates.
Logical Operators.
Simple Types
•Simple types are implementation dependent in prolog however, most
implementations provide the simple types summarized in following
table.
•The Boolean constant are not usually passed by parameters but are
propositions. The constant fail is useful in forcing the generation of all
solutions. Variables are character strings beginning with a capital
letter. Atom are either quoted character string or unquoted string
beginning with small letter.
Type Values
Boolean True, fail
Integer Integers
Real Floating point number
Variable Variables
atom Character sequences
Type Predicates
• The following built in predicates are used to determine
the type of a parameter:
Predicate Checks IF
var (V) V is a variable
nonvar (NV) NV in not a variable
atom(A) A is an Atom
integer(l) I is an integer
real(R) R is a floating point number
number(N) N is an integer or real
atomic(A) A is an atom or a number
functor(T,F,A) T is a term with functor F
and arity A.
T=..L T is a term, L is a list
clause(H,T) H:-T is a rule in the program
Arithmetic Operators
• Prolog provides the standards arithmetic
operations as summarized in following tables:

Symbol Operation
+ Addition
- Subtraction
* multiplication
/ real division
// integer division
mod modulus
** power
Expressions
• Arithmetic expressions are evaluated with the
built in predicate is which is used as an infix
operator in the following form

variable is expression,
?-X is 3*4.
X=12
yes
Boolean Predicates
• Prolog Symbol Operation Action
provides A?=B unifiable A and B are unifiable but A and B does not
unify
more
A=B Unify Unify A and B if possible
general
A\+=B Not unifiable
comparison
operators, A==B Identical Does not unify A and B
which A\+==B Not identical
compare A=:=B Equal (value) Evaluates A and B to determine if equal
terms, and A=\+=B Not equal (value)
predicates A<B Less than (numeric)
to test for A<=B Less or equal (numeric)
unifiabilty
A>B Greater than (numeric)
and
A>=B Greater or equal (numeric)
whether
terms are A@<B Less than (terms)
identical: A@<=B Less or equal (terms)
A@>B Greater than (terms)
A@>=B Greater or equal (terms)
Logical Operators
• Predicates are functions, which return a
Boolean value. Thus the logical operators are
built in to the language. The comma on the
right side of a rule is logical conjunction. The
symbol :- is a logical implication. In addition
prolog provides negation and disjunction
operators. The logical operators are used in
the definition of rules.
Logical Operators (Contd..)
a:-b.
% a if b.
a:-b,c. Symbol Operation

%a if b and c. not Negation


a:-b;c. \+ Not provide
%a if b or c. , Logical conjunction
a:-\++b. : Logical disjunction
%a if b is not possible.
:- Logical implication
a:-not b.
-> If then else
%a if b fails.
a:-b->c;d.
%a if(if b then c else d)
Search
• Suppose that we have the following database:
likes(Manish, Mangoes).
likes(mayur, oranges).
likes(satish,apples).
• So far we have only been able to ask if Manish likes specific things. Suppose that
instead a question like, “what are all things that mayur likes” is to be asked. To
answer this we can use variables again. Thus we can type in the query
?-likes(mayur, fruit).
• As we have seen earlier, Prolog will answer with
fruit=oranges
• This is because it has found the first clause in the database. At this point Prolog
allows us to ask if there are other possible solution.
• The mechanism for finding multiple solutions is called backtracking. This is an
essential mechanism in Prolog.
Backtracking
• Backtracking actually means following a different branch of
the same path, which was left behind going backwards. It is
curcial feature of Prolog and facilitates the proper
implementation of many problem solution.

• During proof search Prolog keeps track of choice points, i.e.


situation where there is more than one possible match.
Whenever the chosen path ultimately turns out to be a
failure, the system can jump back to the last chosen point
and try the next alternatives. This process is known
backtracking.

You might also like