0% found this document useful (0 votes)
84 views41 pages

Prolog Sav 2023

This document provides an introduction to the Prolog programming language. It discusses that Prolog is a logical and declarative language. It also mentions some popular Prolog compilers. The rest of the document discusses key concepts of Prolog like facts and rules, recursion, lists, variables, unification, and backtracking. It provides examples of using Prolog to find factorials, sums, Fibonacci series, and manipulating lists. It also explains concepts like cuts, negation as failure, and the fail predicate.

Uploaded by

Tushar Panchal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views41 pages

Prolog Sav 2023

This document provides an introduction to the Prolog programming language. It discusses that Prolog is a logical and declarative language. It also mentions some popular Prolog compilers. The rest of the document discusses key concepts of Prolog like facts and rules, recursion, lists, variables, unification, and backtracking. It provides examples of using Prolog to find factorials, sums, Fibonacci series, and manipulating lists. It also explains concepts like cuts, negation as failure, and the fail predicate.

Uploaded by

Tushar Panchal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Introduction to

Prolog
Prof. S A Vahora
Introduction

Prolog or PROgramming in LOGics is a


logical and declarative programming
language.
Compilers
• PDC Visual Prolog
• SWI Prolog
• Bin-Prolog
• K-Prolog
• Prolog ++
Introduction
• Prolog is a language that is useful for doing symbolic and logic-based
computation.
• It’s declarative: very different from imperative style programming like
Java, C++, Python
• A program is partly like a database but much more powerful since we
can also have general rules to infer new facts!
• A Prologinterpretercan follow these facts/rules and answer queriesby
sophisticated search.
Introduction
• In a declarative language the programmer specifies a goal to be achieved and
then the Prolog system works out how to achieve it.
• Rather than describing how to compute a solution, a program consists of a data
base of facts and logical relationships (rules).
• The user asks a question to obtain a solution of a problem.
• When asked a question, the run time system searches through the data base of
facts and rules to determine (by logical deduction) the answer.
• Prolog is a declarative language, which means that a program consists of data
based on the facts and rules, i.e., relationships among these facts.
• Prolog is used some areas like natural language processing, artificial
intelligence, expert systems, automated reasoning, etc.
Introduction
Let’s do a test drive!
• Here is a simple Prolog program saved in a file named family.pl
Let’s do a test drive!
• Here is a simple Prolog program saved in a file named family.pl

• A fact/rule (statement) ends with “.” and white space ignored


• read :-after rule head as “if”. Read commain body as “and”
• Comment a line with % or use /* */ for multi-line comments
Prolog running
Prolog Output and Queries
Syntax
Syntax
Facts and Rules
Facts and Rules
Operation Of Prolog
• A query is a sequence of predicates
• predicate1, predicate2, ..., predicatek
• Prolog tries to prove that this sequence of predicates is true using the
facts and rules in the Prolog Program.
• In proving the sequence it performs the computation you want.
Example
Example
Example

Backtracking
Recursion in prolog
• Any function which calls itself is called recursive function.
• In Prolog, recursion appears when a predicate contain a goal that refers to itself. 
• This simply means a program calls itself typically until some final point is reached.
• In Prolog and in any language, a recursive definition always has at least two parts.
• A first fact that act like a stopping condition and a rule that call itself simplified.
• At each level the first fact is checked. If the fact is true then the recursion ends. If
not the recursion continue. 
• A recursive rule must never call itself with the same arguments. If that happens
then the program will never end.
Find factorial of given number
fact.pl
fact(0,Result) :-
Result is 1.

fact(N,Result) :-
N > 0,
N1 is N-1,
fact(N1,Result1),
Result is Result1*N.

Output
?fact(2,F).
F = 2
?fact(3,F).
F = 6
Find the sum of first N natural numbers
Number.pl
sum(0,0).
sum(N,R):-
N > 0,
N1 is N-1,
sum(N1,R1),
R is R1+N.

Output
?sum(4,SUM).
SUM = 10
Print Fibonacci series
Fibonacci.pl
fibonacci(1).
fibonacci(N) :-
N1 = N - 1,
N1 >= 0,!,
fibonacci(N1),
write(F1," ,"),
F = F1 + N.

Output
?fibonacci(2).
0 1
?fibonacci(3).
0 1 2
List
• it is a finite sequence of elements.
• A list in PROLOG is a structure of the form
[t1,t2,t3 … tn]
• The elements in list organised in two section head and tail.
• The direct access to only one element call head , while the rest forms
the list called the Tail.
[Head|Tail]
• where Head is a single element, while Tail is list.
check weather an object X is member of list L or not.

Output
List.pl
?find(a,[a,b,c]).
find(X,[X|TAIL]). true
find(X,[_|TAIL]):- find(X,TAIL). ?find(d,[a,b,c]).
false
Calculating the number of items of a given
list.
Output
List.pl
? length([a,b,c],N).
length([],0). N = 3
length([_|TAIL],N) :- ? length([],0).
length(TAIL,N1),N is N1 +1. N = 0
Find the nth element of a given list.

Output
List.pl
? match([a,b,c],0,N).
match([H|_],0,H). N = a
match([_|T],N,H) :-
N > 0,
N1 is N-1,
match(T,N1,H).
Merge two List

Output
List.pl
?:con([a,b,c],[1,2],N).
con([],L1,L1). N = [a,b,c,1,2]
con([X|Tail],L2,[X|Tail1]):-
con(Tail,L2,Tail1).
Operation
A query predicate matches a rule head or
fact (either one with variables) if
If the predic a te names and arities match then
each of the k-terms much match. So for foo(t1,
t2, t3) to match foo(s1, s2, s3) we must have that
t1 matches s1, t2 matches s2, and t3 matches t3.
During this matching process we might have
to “bind” some of the variables to make
the terms match.
These bindings are then passed on into the
new query (consisting of the rule body and
the left over query predic a tes).
Variable Matching (Unification)
Two terms (with variables match if):
If both are constants (identifiers, numbers, or
strings)
and are identic a l.
If one or both are bound variables then they
match
if what the variables are bound to match.
X and mary where X is bound to the value mary will
match.
X and Y where X is bound to mary and Y is bound to
mary
will match,
X and ann where X is bound to mary will not match.
Variable Matching (Unification)
If one of the terms is an unbound variable
then they match AND we bind the variable
to the term.
X and mary where X is unbound match and
make X
bound to mary.
X and Y where X is unbound and Y is bound to
mary
match and make X bound to mary.
X and Y where both X and Y are unbound
match and make X bound to Y (or vice
versa).
Variable Matching (Unification)
If the two terms are struc tures
t = f(t1, t2, ..., tk)
s = g(s1, s2, ..., sn)
Then these two terms matc h if
the identifiers “f” and “g” are identic al.
They both have identic al arity (k=n)
Each of the terms ti, si match (recursively).
E.g.
date(X, may, 1900) and date(3, may, Y) matc h and make X
bound
to 3 and Y bound to 1900.
equal(2 + 2, 3 + 1) and equal(X + Y, Z) match and make X
bound to
1, Y bound to 2, and Z bound to “3+1”.
Note that to then evaluate Z by using “is”.
date(f(X,a), may, g(a,b)) and date(Z, may, g(Y,Q)) match and
make Z bound to “f(X,a)”, Y bound to a, and Q bound to b.
Note we can bind a variable to a term containing another variable!
The predicate “=” shows what Prolog unifies!
Unification Examples
Which of the following are
unifiable?
Term1 Term 2 Bindings if unifiable
X f(a,b) X=f(a,b)
f(X,a) g(X,a)
3 2+1 No! use is if want 3
book(X,1) book(Z)
[1,2,3] [X|Y] X=1, Y=[2,3]
[a,b,X] [Y|[3,4]]
[a|X] [X|Y] X=a Y=a improper list

X(a,b) f(Z,Y)
[X|Y|Z] [a,b,c,d] X=a. Y=b, Z=[c,d]
Comparison operators
Prolog cut !

• Prolog cut is a goal written as in ! which will always


succeed but can’t be backtracked.
• Cut in Prolog can prevent unnecessary backtracking,
which includes finding extra solutions and avoiding
unnecessary computations in Prolog. 
• Cut in Prolog should be used carefully, as users
insert cuts in an experimental manner into code
that does not work as required.
Prolog cut !

cut.pl Output
a(X) :- b(X),!,c(X). ?:a(x).
a(X) :- d(X). X = 1

b(1).
b(4).
c(1).
c(3).
d(4).
Fail
Fail.pl
• Negation as Failure a(X) :- b(X),c(X),fail.
a(X) :- d(X).
• Perform failure when condition does not satisfy.
• Fail predicate simply fails the rule. b(1).
b(4).
• A typical use of fail is a negation of a predicate c(1).
c(3).
• When Prolog fails, it tries to backtrack. Thus fail can be d(4).

viewed as an instruction to force backtracking.


Output
Goal:a(x).
X = 4
Prolog cut !
Prolog cut !

You might also like