PPL UNIT-6
UNIT -6
LOGIC PROGRAMMING LANGUAGES
INTROUCTION
Programming that uses symbolic logic is called Logic programming Language ( or) Declarative
Language.
The symbolic logic uses First Order Predicate Logic.
The entire symbolic logic consists of Propositions.
Symbolic logic is the way of representing logical expressions through the use of
symbols and variables, rather than in ordinary language. This has the benefit of
removing the ambiguity that normally accompanies ordinary languages, such as
English, and allows easier operation.
Proposition:
Proposition is logical statement that may ( or) may not be True. It consists of objects and
relations of objects to each other.
Object : Object is an entity used in the Proposition that can be a constant ( or) a variable.
Constant is a direct name value.
Variable is a symbol that can represent different objects at different times.
Atomic Proposition:
The simplest form of Proposition is called Atomic Proposition that consists of compound term.
A compound term has two parts:
1. Functor: It is the function symbol that names the relation.
2. Ordered list of parameters ( tuples).
Example:
Student ( John) -------- meaning is ‘John is student.’
Likes ( John, Apples) -----------meaning is ‘John likes Apples’.
Where 1 st is the tuple and 2nd is the two tuple.
Compound Proposition : Combine two or more Propositions.
Logical connectors / operators:
The Propositions are connected with the following operators.
S.No Symbol Example Meaning
1. ( Negation) a Not a
2. (conjunction) a b a and b
3. (disjunction) a b a or b
4. (Equvalence) a b a equal to b
5. ( implies) a b a implies b
6. (implies) a b b implies a
Principles of Programming Languages - Unit-VI Kallam Haranadhareddy Institute of Technology, CSE
Dept., Page 1
PPL UNIT-6
Quantifiers:
In the Propositions, we are using quantifier symbol with the variables to quantity the
meaning of the variable.
There are two quantifiers: Universal and Existential
Let x is a variable, p is a Proposition.
Name Example Meaning
Universal xp For all x, p is true
Existential xp There exit a value of x such that p is true.
Resolution and Unification:
Resolution: The derivation of new statements is called as Resolution. In other words, making a
simple inference from a pair of Horn classes is called Resolution.
This principle is similar to the idea of transitivity in Algebra.
Example 1:
a b
b c
then a c is true.
Example 2:
speaks ( Mary, English)
talkswith ( x,y) speaks (x, L). speaks ( y, L) x y
talkswith ( Mary, y) speaks ( Mary, English) speaks ( y, English) Mary y
Unification:
The process of binding variables to values is called Unification.
Rules of Unification:
1. A constant unifies only with itself.
2. Two structures unifies if they are same type.
3. A variable unifies with anything.
Examples:
1. parent (x, y) and parent ( Albert, Edward )
These unifies x=Albert, y= Edward.
2. Parent ( x, Edward) and parent ( Albert , y)
These unifies x= Albert , y= Edward
3. Parent (x , y) and Female ( x)
We cannot unify because both structures are different.
Horn clause:
Proposition can be started as Horn clauses.
Principles of Programming Languages - Unit-VI Kallam Haranadhareddy Institute of Technology, CSE
Dept., Page 2
PPL UNIT-6
In almost all Logic programming languages, propositions are written in a standard form known
as Horn clause .
Horn clause consist of Head ( H) and body ( B1,B2………..Bn).
The following is general form of Horn clause
H:- B1, B2………Bn
When Bi of all true, we can assume that ‘H’ is true
Antecedent Consequent
[ B1, B2,…….Bn] (H)
The right hand side part [ B1, B2, …….Bn] is called Antecedent.
The left hand side (H) is called Consequernt.
The ( ,) operator between [ B1 to Bn] is assumed as AND ( n) operator.
Antecedent is called as if part and consequent is called as then part.
Horn clauses can have two forms.
1. Headed Horn Clause:
There should be a single atomic proposition on left side (I e) Head.
Ex 1: Parent ( x, y) :- Mother ( x,y)
If x is Mother of y is true then x is parent of y is true
Ex 2: Grandparent (x , z):- parent (x, y), parent (y, z)
if x is parent of y and y is parent of z then x is Grand parent of z.
2. Headless Horn clause:
If the Horn clause contains only the right hand side part without Head ( or) left part is called
Head less Horn clause. This is useful to state the facts.
Ex: Student ( Edward)
Likes ( John, apple).
Applications of LOGIC programming Language:
LOGIC programming has many application areas.
Relational data bases.
Natural Language Processing ( NLP)
Expert System.
Symbolic Equation solving
Planning
Prototyping
Simulation
Programming Language implementation.
PROLOG:
PROLOG means Programming in Logic. It is the most widely used logic programming
language.
Prolog is developed at university of Aix – Marseille and university of Edinburgh in 1970’s
PROLOG is good for the following:
1. Knowledge Representation
2. Natural Language Processing ( NLP)
Principles of Programming Languages - Unit-VI Kallam Haranadhareddy Institute of Technology, CSE
Dept., Page 3
PPL UNIT-6
3. Expert Systems and etc….
Knowledge representation (KR) is the field of artificial intelligence (AI)
dedicated to representing information about the world in a form that a computer
system can utilize to solve complex tasks such as diagnosing a medical condition
or having a dialog in a natural language.
Natural Language Processing (NLP) is a sub-field of Artificial Intelligence that
is focused on enabling computers to understand and process human languages, to
get computers closer to a human-level understanding of language.
Expert Systems(ES) are the computer applications developed to solve complex
problems in a particular domain, at the level of extra-ordinary human intelligence
and expertise. Expert Systems are capable of − Advising, Instructing and
assisting human in decision making, Demonstrating, Deriving a solution,
Diagnosing, Explaining, Interpreting input, Predicting results, Justifying the
conclusion, Suggesting alternative options to a problem
Elements of PROLOG Language:
The basic elements of PROLOG:
1. Term
2. Fact
3. Rule
4. Goal statement ( query)
1. Term: PROLOG statement is constructed from terms.
A term is a constant, a variable or a structure
Constant is either an atom (string of characters starts with lower case letter) or an
Integer.
Examples: 300, PPL
Variable name begin with Upper case letter. We are binding a value to the variable ( i.e)
called Instantiation.
Structure represents atomic proposition.
It’s syntax:
Functor ( List of parameters)
Example: student ( Albert)
2. Fact: These are simple proposition statements that are assumed to be TRUE
Example: Student ( Albert)
Mother (Mary, John)
The fact of above statement is Albert is Student and Mary is Mother of John.
3. Rule: The Rule statement has two parts.
The Right side is the ‘if’ part (antecedent)
The left side is the ‘then‘ part ( consequent)
The syntax:
Consequent: - Antecedent
The meaning of above statement is
If the Antecedent part is ‘TRUE’ then the consequent of the statement also to be ‘TRUE”.
Example:
parent ( x, y) :- mother (x,y)
Principles of Programming Languages - Unit-VI Kallam Haranadhareddy Institute of Technology, CSE
Dept., Page 4
PPL UNIT-6
If x is the mother of y then x is parent of y
4. Goal statement ( query):
The ‘PROLOG’ proposition end with . (dot) is called a Goal statement.
The Goal statement is either ‘TRUE’ or ‘FALSE’.
Example:
Student (Albert).
The system will give either ‘TRUE’ or ‘ FALSE.
Example prolog program:
man(socrates).
mortal(X) :- man(X).
?- mortal(socrates).
Strings starting with an uppercase character or an underscore are variables and can change their
value. In our example socrates is an atom and the uppercase X a variable. Also, note that the
full stop after each clause is mandatory.
The first line reads as “Socrates is a man”, it is a base clause, which represents a simple fact.
The second line is a rule and translates to, “X is mortal if X is a man” or, “All men are mortal.”
This rule is for determining when its input X is “mortal”. Rules are a key concept of the language
and allow us to make general statements about objects and their relationships. They consist of a
head and a body connected by the turnstile symbol :- (pronounced “if”).
The third line reads as “Is Socrates mortal?”, the ?- is the Prolog prompt for a question. This is
goal statement.
The result of ?- mortal(socrates). is true.
The result of ?- mortal(plato). is false.
Consider the following clause:
?- mortal(X).
That is, “Who (X) is mortal?“. Prolog will respond X = socrates and bound Socrates to the variable X.
The above is called query statement that finds the values.
Database manipulation predicate commands in
PROLOG:
PROLOG Language supports the manipulation of data base files. It has so many commands.
1. assert (x)
This commands adds ‘x’ to the data base file.
For syntactic reasons, if x is not a base clause, use this command as assert ((x))
2. asserta (x)
This command adds ‘x’ to the data base in front of other clauses of this predicate.
3. assertz (x)
This command adds ‘x’ to the data base after other clauses of this predicate.
4. Retract (x)
This command removes ‘x’ from the data base. For syntactic reasons, if x is not a base clause,
use this command as Retract ((x))
5. Abolish (F, a)
This command removes all the clauses with Function ‘F’ from the data base.
6. Clause ( x, v)
This command find a clause in the data base whose head ( is in the left side) matches x whose
body ( is in right side) matches v.
7. Save (F)
Principles of Programming Languages - Unit-VI Kallam Haranadhareddy Institute of Technology, CSE
Dept., Page 5
PPL UNIT-6
Save the entire program state on file ‘F’.
8. Restore (F)
Replace the program state with the 1 on File ‘F’.
9. Listing
It shows all the clauses in the Data base.
Examples:
PROLOG has mainly four data base manipulation commands:
Assert
Retract
Asserta
Assertz
The following are the examples for the above commands. Let us assume we start with the empty
data base.
? Listing
yes
After the listing command PROLOG will simply respond ‘yes’; the listing is ( of course ) is
empty.
Suppose we now give the following command
? assert (happy(Mia))
The above command inserts happy (Mia) in the data base.
The following is the listing command to see whether the above statement is added or not.
? Listing
happy (Mia)
yes.
Suppose we then made 4 assert commands to insert 4 sentences.
?assert (happy(Vincent))
yes
? assert (happy(Marcellus))
yes
?assert(happy(Albert))
yes
?assert(happy(Vincent))
yes
And then ask for a listing
? Listing
happy(Mia)
happy(Vincent)
happy(Marcellus)
happy(Albert)
happy(Vincent)
yes
We can also insert the rules along with the above “facts” in the data base.
Example: The following insert the rule naive(x):- happy(x)
? assert (naive (x) :- happy (x))
yes
Principles of Programming Languages - Unit-VI Kallam Haranadhareddy Institute of Technology, CSE
Dept., Page 6
PPL UNIT-6
Now the database has fact statements and 1 Rules statement.
The following is the example for the retract command (i.e) useful to delete the sentences.
? Retract (happy( Marcellus))
yes
?Listing
happy(mia)
happy (Vincent)
happy (Albert)
happy (Vincent)
naivec (x):- happy (x)
yes.
After listing, suppose if we try to delete happy (Vincent) sentence
? retract (happy (Vincent ))
yes
Note that the 1st occurrence of happy (Vincent) was removed.
?Listing
happy(mia)
happy(Albert)
happy(Vincent)
naive (x):- happy (x)
yes
To remove all sentences which are starting with happy., then the following is the example.
?retract(happy(x))
If removes all the facts except the rule.
?Listing
naive(x):- happy(x)
yes
Example for asserta and assertz
Asserta insert the sentence at the beginning and assertz insert the sentence at the end of the
database.
Examples:
?assert(p(b))
yes
?assertz (p(c))
yes
?asserta(p(a))
yes
?Listing
p(a)
p(b)
p(c )
yes
Comparison of Logic programming ( PROLOG) and
Functional Languages:
Logic Language Functional Language
1.It is declarative Language based on symbolic 1. It is high level programming based on
logic. mathematical functions.
Principles of Programming Languages - Unit-VI Kallam Haranadhareddy Institute of Technology, CSE
Dept., Page 7
PPL UNIT-6
2.Programming is non-procedural. 2. Programming is procedural
3.In this Programming, do not specify correctly 3.In this Programming, programmer instruct
how a result is to be computed. the computer on exactly how the computation
is to be done.
4.Eg: PROLOG 4.Eg: LISP, SCHEME, ML
PROLOG program:
The following is an example Prolog program to implement family tree with PROLOG
statements. The family tree includes Father, Mother, Son, Daughter, Grand Father, for the Facts
like Male, Female, Parent, brother, sister.
Sankar Umabai
Sadhana
Satish
Mary Amar
Sourav swathi
Prasanth
PROLOG Program:
male(shankar).
male(Amar).
male(satish).
male(saurabh).
male(prashant).
female(umabai).
female(Mary).
female(sadhana).
female(swati).
parent(shankar,umabai,Amar).
parent(shankar,umabai,satish).
parent(Amar,Mary,prashant).
parent(satish,sadhana,saurabh).
parent(satish,sadhana,swati).
brother(Amar,satish).
brother(satish,Amar).
brother(prashant,saurabh).
Principles of Programming Languages - Unit-VI Kallam Haranadhareddy Institute of Technology, CSE
Dept., Page 8
PPL UNIT-6
brother(saurabh,prashant).
sister(swati,saurabh).
sister(swati,prashant).
father(X,Y) :- parent(X,Z,Y).
mother(X,Y) :- parent(Z,X,Y).
son(X,Y,Z) :- male(X),father(Y,X),mother(Z,X).
daughter(X,Y,Z) :- female(X),father(Y,X),mother(Z,X).
wife(X,Y) :- female(X),parent(Y,X,Z).
grandfather(X,Y) :- male(X),father(X,Z),father(Z,Y).
uncle(X,Y) :- male(X),brother(X,Z),father(Z,Y).
aunt(X,Y) :- wife(X,Z),uncle(Z,Y).
cousin(X,Y) :- father(Z,X),brother(Z,W),father(W,Y).
ancestor(X,Y,Z) :- parent(X,Y,Z).
ancestor(X,Y,Z) :- parent(X,Y,W),ancestor(W,U,Z).
********************** Output *************************
?- father(X,Y). ?- mother(X,Y). ?- parent(X,Y,Z).
X = shankar X = umabai X = shankar
Y = Amar ; Y = Amar ; Y = umabai
Z = Amar ;
X = shankar X = umabai
Y = satish ; Y = satish ; X = shankar
Y = umabai
X = Amar X = Mary Z = satish ;
Y = prashant ; Y = prashant ;
X = Amar
X = satish X = sadhana Y = Mary
Y = saurabh ; Y = saurabh ; Z = prashant ;
X = satish X = sadhana X = satish
Y = swati ; Y = swati ; Y = sadhana
No Z = saurabh ;
No
X = satish
?- son(prashant,X,Y). ?- grandfather(X,Y). Y = sadhana
Z = swati ;
X = Amar X = shankar No
Y = Mary Y = prashant ;
Yes
X = shankar
Y = saurabh ;
X = shankar
?-daughter(swati,X,Y). Y = swati ;
X = satish No
Y = sadhana
Yes Principles of Programming Languages - Unit-VI Kallam Haranadhareddy Institute of Technology, CSE
Dept., Page 9
PPL UNIT-6
Multi-paradigm programming language
Programming Paradigm is way or style of Programming.
There are different programming paradigms:
1. Imperative paradigm
2. Functional paradigm
3. Logic paradigm
4. Object Oriented paradigm
A multi-paradigm programming language is a programming languages that supports more than
one programming paradigm.
The idea of a multiparadigm language is to provide a framework in which programmers can
work in a variety of styles, freely intermixing constructs from different paradigms.
The design goal of such languages is to allow programmers to use the best tool for a job,
admitting that no one paradigm solves all problems in the easiest or most efficient way.
Examples would be CommonLisp, RacketScheme, OzLanguage, ObjectiveCaml, Delphi,
ScalaLanguage and (debatably) CeePlusPlus.
Principles of Programming Languages - Unit-VI Kallam Haranadhareddy Institute of Technology, CSE
Dept., Page 10