SlideShare una empresa de Scribd logo
1 de 55
Descargar para leer sin conexión
Logic Programming 
Tarunpreet Bhatia 
CSED, Thapar University
Logic 
•Logic is not concerned with what is true. Logic is the study of what follows from what. What conclusions follow from a set of premises. 
•It can be defined as study of principles of correct reasoning. 
•The main thing we study in logic are principles governing the validity of arguments and check that whether certain conclusion follows from some given assumption. 
•Consider: 
Alice likes everyone who likes logic 
Alice likes logic 
------------------------------------------------ 
Alice likes himself. 
Is this argument valid? How do you know? 
•The logic process takes in some information called premises and produces some outputs called conclusions.
Symbolic Logic 
•Method of representing logical expressions through the use of symbols and variables, rather than in ordinary language. 
•It provides the benefit of removing the ambiguity that is generally seen in ordinary languages like English. 
•Its normally divided into two branches: 
1.Propositional Logic 
2.Predicate Logic
Propositional Logic 
•Simplest form of formal logic. 
•All statements made are called propositions. 
•The FORMAL LOGIC is concerned with syntax of statements and not with their semantics. 
•It deals with manipulation of logical variables which represents proposition. 
•Propositional Logic is concerned with the subset of declarative sentences that can be either true or false. 
•Propositional Logic takes only two values, either TRUE or FALSE.
Syntax of Propositional Logic 
1.Letters A, B,….Z and these letters with subscripted numerals are well-formed atomic propositions. 
2.If A and B are well-formed propositions so are 
1.~A (Negation of A) 
2.(A & B) (Conjunction of A with B) 
3.(A V B) (Inclusive Disjunction of A with B) 
4.(A  B) (A implies B) 
5.(A↔ B) (Material biconditional of A with B) 
6.(A B) (Exclusive Disjunction of A with B) 
7.(A | B) (Joint Denial of A with B) 
8.(A↓ B) (Disjoint Denial of A with B)
Semantics of Logical Propositions 
Truth tables for Logical Connectives 
A 
B 
~A 
~B 
A V B 
A & B 
A  B 
A ↔ B 
A B 
A | B 
A↓ B 
T 
T 
F 
F 
T 
T 
T 
T 
F 
F 
F 
F 
T 
T 
F 
T 
F 
T 
F 
T 
T 
F 
T 
F 
F 
T 
T 
F 
F 
F 
T 
T 
F 
F 
F 
T 
T 
F 
F 
T 
T 
F 
T 
T 
Example: 
Let A be a Proposition: The machine is defective. 
Let B be a Proposition: The production is less.
Why we use Propositional Logic? 
•Its easier to check formulas. 
•We can exploit the Boolean nature for efficient reasoning. 
•Its easier to incrementally add formulas. 
•It can be extended infinitely to many variables using logical quantifiers.
First-order Predicate Logic 
•First-order predicate calculus (FOPL) was developed by logicians to extend the expressiveness of Propositional Logic. 
•It is generalization of propositional logic that permits reasoning about world entities (objects) as well as classes and subclasses of objects. 
•Prolog is also based on FOPL. 
•Predicate logic uses variables and quantifiers which is not present in propositional logic.
Why First-Order Predicate Logic ? 
•Suppose we are having 2 statements , based on which we have to draw a conclusion. 
Statement 1: All students must take Java. 
Statement 2: John is a student. 
•According to human inference, 
John must take Java. 
but not according to Propositional logic. (Disadvantage)
First-Order Predicate Logic 
•0 is a natural number 
Natural(0) 
•For all x, if x is a natural number, then so is successor of x 
For all x, natural (x)  natural (successor(x)) 
•2 is a natural number 
Natural(2) 
•-1 is a natural number 
Natural(-1)
First-order predicate calculus 
First-order predicate calculus classifies the different parts of such statements as follows: 
1.Constants 
2.Predicates 
3.Functions 
4.Variables 
5.Connectives 
6.Quantifiers 
7.Punctuation Symbols
Predicates 
•These are names for functions that are true or false, like Boolean functions in a program. 
•Defined as a relation that binds two atoms together. 
•Example: Amit likes sweets. 
likes(amit, areoplanes). 
amit and sweets are atoms; likes is a predicate 
•Predicates can take a number of arguments. 
•In Example , the predicate natural takes one argument. 
natural(n).
Predicates 
•Its possible to have a function as an argument 
•Eg. Ravi’s father is Rani’s father 
father(father(ravi), rani). 
Father is a predicate and father(ravi) is a function to indicate Ravi’s father.
a. Constants 
•These are usually numbers or names. 
•Sometimes they are called atoms, since they 
cannot be broken down into subparts. 
•Example 
natural(0). 
0 is a constant
b. Variables 
•Stands for Quantities that are yet Unspecified. 
•Example 
valuable(X) 
X is a variable.
c. Functions 
•First-order predicate calculus distinguishes between functions and predicates. 
•Predicates - true or false and all other are functions which represent non-Boolean values.
Quantifiers 
•Declares the scope or range of variables in a logical expression. 
•Two basic quantifiers used in logic: 
A. Universal Quantifier ( ) 
B. Existential quantifier (Ǝ) 
•The statement: 
For all x, natural(x) → natural(successor(x)) 
means that for every x in the universe, if x is a natural number, then the successor of x is also a natural number. 
• A universal quantifier is used to state that a relationship among predicates is true for all things in the universe named by the variable x.
Quantifiers… 
•If a is a variable, then is read as: 
1.for all a 
2.for each a 
3.for every a
Quantifiers 
•There is also the existential quantifier, there exists (Ǝ), as in the following statement: 
•there exists x, natural(x). 
This statement means that there exists an x such that x is a natural number. 
•An existential quantifier is used to state that a predicate is true of at least one thing in the universe, indicated by the variable x.
Quantifiers… 
•If b is a variable, then Ǝ is read as: 
1.there exists a b 
2.for some b 
3.for atleast one b
Precedence of Connectives 
~ highest 
& 
V 
 
↔ lowest
Some points to remember 
•Arguments to predicates and functions can only be terms which is a combination of variables, constants and functions. 
•Terms cannot contain predicates, quantifiers or connectives.
What is Prolog? 
•Prolog is the most widely used language to have been inspired by logic programming research. 
•It is a declarative language. 
•Prolog is computer programming language, used for solving problems that involves objects and relationships between objects. 
•A Prolog program can also be seen as a relational database containing rules as well as facts.
Structure of Logic Programs 
•Programs consist of procedures. 
•Procedures consist of clauses. Clauses are statements about what is true about a problem, instead of instructions how to accomplish the solution. 
•Each clause is a fact or a rule. 
•Programs are executed by posing queries. 
•The Prolog system uses the clauses to work out how to accomplish the solution by searching through the space of possible solutions. 
•Computer Programming in Prolog consists of: 
–Declaring some facts about objects and their relationships. 
–Declaring some rules about objects and their relationships. 
–Asking questions (goals) about objects and their relationships.
Example 
elephant(george). 
elephant(mary). 
elephant(X) :- grey(X), mammal(X), hasTrunk(X). 
Procedure for elephant 
Predicate 
Clauses 
Rule 
Facts
Example 
?- elephant(george). 
yes 
?- elephant(jane). 
no 
Queries 
Replies
Why Rules? 
•Rule is an extension of fact with added conditions that have to be specified for it to be true. 
•Rule is much more compact than a list of facts. 
•When facts depend on a group of other facts. 
•Example: John likes all people who likes sweets. 
Way 1: Write down separate facts as below – 
likes(james, sweets). 
likes(john, james). 
likes(john, joe). 
likes(john, david). 
….. So on
Rules… 
Way 2: John likes any object provided it is a person. 
In Prolog, 
likes(john, X) :- like(X, sweets). 
Head 
Body 
If
Example 
First-order predicate calculus for the following logical statements 
•A horse is a mammal. 
•A human is a mammal. 
•Mammals have four legs and no arms, or two legs and two arms. 
•A horse has no arms. 
•A human has arms. 
•A human has no legs. 
•A human has two arms. 
•A human has two legs.
Example 
To write a rule, X is a sister of Y 
•X is a female. 
•X has mother M and father F and 
•Y has the same mother and father as X does. 
sister_of(X, Y) :- 
female(X), 
parents(X, M, F), 
parents(Y, M, F).
Example 
•To define a rule “X is a grandfather of Y, if X is a father of Z and Z is a parent of Y ” using logic programming convention, then we write 
grandfather(X, Y) :- father(X, Z) , parent(Z, Y). 
•X is a sibling of Y if they both have the same parent. 
sibling(X, Y) :- parent(Z, X) , parent(Z, Y).
Types of Query 
1.In Ground query , the goal(s) contains constants. Answer to ground query is either yes or no depending upon whether it is a logical consequence of a logic program or not. 
2.In Non – Ground query, the goal(s) should have at least one variable as an argument.
Simple Queries 
a.Ground Query: “Is raman a grandfather of manu ?” 
?- grandfather(raman, manu). 
b. Non ground: 
“Does there exist X such that X is a father of manu ?” {Who is father of manu?} 
?- father(X, robert).
Conjunctive Queries 
a.Ground Query 
“Is raman father of robert and robert is a father of mike? 
?- father(raman, robert), father(robert, mike). 
Answer: yes 
b. Non Ground Query 
?- father(raman, X), father(X, mike). 
Answer: X = robert
Inference Rule 
•Ways of deriving or proving new statements from a given set of statements. 
•Example 
a b, b  c 
We can derive a  c
Horn Clauses 
•Horn clause (named after its inventor Alfred Horn) is a statement of the form: 
a1 and a2 and a3 . . . and an → b 
where the ai are only allowed to be simple statements involving no connectives. 
•Thus, there are no or connectives and no quantifiers in Horn clauses. 
•b is called the head of the clause, and the a1 . . . , an is the body of 
•the clause. 
•In the Horn clause, the number of ai’s may be 0, in which case the Horn clause has the form: 
→ b 
•Such a clause means that b is always true. In other words, b is an axiom and is usually written without the connective →. Such clauses are sometimes also called facts.
Example 
Consider the following statement: 
x is a grandparent of y if x is the parent of someone who is the parent of y. 
Translating this into predicate calculus, we get 
grandparent (x, y) for all x, for all y, (there exists z, parent(x, z) and parent(z, y)). 
As a Horn clause this is expressed simply as: 
grandparent(x, y) parent(x, z) and parent(z, y) .
Example 
Consider the following statement: 
For all x, if x is a mammal then x has two or four legs. 
Translating in predicate calculus, we get: 
for all x, mammal(x) → legs(x, 2) or legs(x, 4). 
This may be approximated by the following Horn clauses: 
mammal(x) and not legs(x, 2) → legs(x, 4). 
mammal(x) and not legs(x, 4) → legs(x, 2).
Resolution 
•Resolution says that if we have two Horn clauses, and we can match the head of the first Horn clause with one of the statements in the body of the second clause, then the first clause can be used to replace its head in the second clause by its body. 
In symbols, if we have Horn clauses: 
a ← a1, . . . , an. 
b ← b1, . . . , bm. 
and bi matches a, then we can infer the clause: 
b ← b1, . . . , bi-1, a1, . . . , an, bi+1, . . . , bm.
•The system attempts to apply resolution by matching one of the goals in the body of the headless clause with the head of a known clause. It then replaces the matched goal with the body of that clause, creating a new list of goals, which it continues to modify in the same way. The new goals are called subgoals. 
•If the system succeeds eventually in eliminating all goals— thus deriving the empty Horn clause—then the original statement has been proved. 
•In symbols, if we have the goal: ← a. 
•and the clause a ← a1, . . . , an, then resolution replaces the original goal a with the subgoals: 
← a1, . . . , an.
Unification 
•To match statements that contain variables, we must set the variables equal to terms so that the statements become identical and can be canceled from both sides. This process of pattern matching to make statements identical is called unification, and variables that are set equal to patterns are said to be instantiated. 
•Unification is the process by which variables are instantiated, or allocated memory and assigned values, so that patterns match during resolution. It is also the process of making two terms the same in some sense. 
•The built in Prolog operator '=' can be used to unify two terms.
Examples 
?- me = me. 
yes 
?- me = you. 
no 
?- me = X. 
X = me 
?- f(a, X) = f(Y, b). 
X = b 
Y = a
?- f(X) = g(X). 
no 
?- f(X) = f(a, b). 
no 
?- f(a, g(X)) = f(Y, b). 
no 
?- f(a, g(X)) = f(Y, g(b)). 
X = b 
Y = a
Unification algorithm for Prolog 
1. A constant unifies only with itself: me = me succeeds but me = you fails. 
2. A variable that is uninstantiated unifies with anything and becomes instantiated 
to that thing. 
3. A structured term (i.e., a function applied to arguments) unifies with another term only if it has the same function name and the same number of arguments, and the arguments can be unified recursively. 
Thus, f(a, X) unifies with 
f(Y, b) by instantiating X to b and Y to a. 
A variation on case 2 is when two uninstantiated variables are unified: 
?- X = Y. 
X = _23 
Y = _23 
The number printed on the right-hand side—in this case, indicates an internal memory location set aside for that variable. Thus, unification causes uninstantiated 
variables to share memory—that is, to become aliases of each other.
Example 
Given the rules and facts: 
legs(x, 2) ← mammal(x), arms(x, 2). 
legs(x, 4) ← mammal(x), arms(x, 0). 
mammal(horse). 
arms(horse, 0). 
if we supply the query: 
← legs(horse,4). 
We get X= horse as output
Example 
For example, given the Horn clauses: 
ancestor(x, y) ← parent(x, z), ancestor(z, y). 
ancestor(x, x). 
parent(amy, bob). 
if we provide the query: 
← ancestor(x, bob). 
We get X = bob , X = amy
Prolog Arithmetic 
?- write(3 + 5). 
3 + 5 
To force the evaluation of an arithmetic term, a new operation is required: the built- in predicate is. 
?- X is 3 + 5, write(X). 
X = 8 
?- 3 + 4 = 4 + 3. 
no 
To get equality of values, we must force evaluation using is, for example, by writing the predicate: 
valequal(Term1, Term2) :- 
X is Term1, Y is Term2, X = Y. 
We would then get: 
?- valequal(3 + 4, 4 + 3). 
yes
?- foo(a,Y) = foo(X,b). 
** Instantiation of variables may occur in either of the terms to be unified ** 
Y=b 
X=a yes 
Example
?- foo(a,b) = foo(X,X). 
** In this case there is no unification because foo(X,X) must have the same 1st and 2nd arguments ** 
no 
Example
Example 
?- 2*3+4 = X+Y. 
** The term 2*3+4 has principal functor + 
X=2*3 and therefore unifies X+Y with X instantiated to 2*3 and Y instantiated to 4 ** 
yes
Example 
?- [a,b,c] = [X,Y,Z]. 
** Lists unify just like other terms ** 
X=a Y=b Z=c 
yes
Example 
Do the following pairs of items unify (match) ? 
eats(fred,tomatoes) 
eats(WHOM,WHAT) 
Yes WHOM = fred and WHAT = tomatoes.
Example 
cd(29,beatles,sgt_pepper). 
cd(A,B,help). 
No sgt_pepper and help do not unify 
f(X,Y) 
f(P,P) 
Yes X = P and Y = P. A variable (such as X) can be bound to another variable (such as P). In this case we can also infer that X = Y
Example 
f(X,a) 
f(a,X) 
Yes X = a
Example 
likes(jane,X) 
likes(X,jim) 
No X can not be bound to both jane and jim 
f(foo,L) 
f(A,A) 
Yes A = foo and A = L. Hence L = foo 
A and L are variables

Más contenido relacionado

La actualidad más candente

Artificial Intelligence Notes Unit 1
Artificial Intelligence Notes Unit 1 Artificial Intelligence Notes Unit 1
Artificial Intelligence Notes Unit 1 DigiGurukul
 
Lecture 06 production system
Lecture 06 production systemLecture 06 production system
Lecture 06 production systemHema Kashyap
 
Intro automata theory
Intro automata theory Intro automata theory
Intro automata theory Rajendran
 
ProLog (Artificial Intelligence) Introduction
ProLog (Artificial Intelligence) IntroductionProLog (Artificial Intelligence) Introduction
ProLog (Artificial Intelligence) Introductionwahab khan
 
Query processing and optimization (updated)
Query processing and optimization (updated)Query processing and optimization (updated)
Query processing and optimization (updated)Ravinder Kamboj
 
Knowledge representation in AI
Knowledge representation in AIKnowledge representation in AI
Knowledge representation in AIVishal Singh
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy methodhodcsencet
 
Introduction to prolog
Introduction to prologIntroduction to prolog
Introduction to prologHarry Potter
 
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptracha49
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithmsGanesh Solanke
 
KNOWLEDGE REPRESENTATION ISSUES.ppt
KNOWLEDGE REPRESENTATION ISSUES.pptKNOWLEDGE REPRESENTATION ISSUES.ppt
KNOWLEDGE REPRESENTATION ISSUES.pptSuneethaChittineni
 

La actualidad más candente (20)

Np cooks theorem
Np cooks theoremNp cooks theorem
Np cooks theorem
 
Artificial Intelligence Notes Unit 1
Artificial Intelligence Notes Unit 1 Artificial Intelligence Notes Unit 1
Artificial Intelligence Notes Unit 1
 
Lecture 06 production system
Lecture 06 production systemLecture 06 production system
Lecture 06 production system
 
Intro automata theory
Intro automata theory Intro automata theory
Intro automata theory
 
ProLog (Artificial Intelligence) Introduction
ProLog (Artificial Intelligence) IntroductionProLog (Artificial Intelligence) Introduction
ProLog (Artificial Intelligence) Introduction
 
Query processing and optimization (updated)
Query processing and optimization (updated)Query processing and optimization (updated)
Query processing and optimization (updated)
 
Fundamental of Algorithms
Fundamental of Algorithms Fundamental of Algorithms
Fundamental of Algorithms
 
Knowledge representation in AI
Knowledge representation in AIKnowledge representation in AI
Knowledge representation in AI
 
daa-unit-3-greedy method
daa-unit-3-greedy methoddaa-unit-3-greedy method
daa-unit-3-greedy method
 
Introduction to prolog
Introduction to prologIntroduction to prolog
Introduction to prolog
 
UNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.pptUNIT-1-PPTS-DAA.ppt
UNIT-1-PPTS-DAA.ppt
 
Analysis of algorithms
Analysis of algorithmsAnalysis of algorithms
Analysis of algorithms
 
Daa notes 1
Daa notes 1Daa notes 1
Daa notes 1
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Disk scheduling
Disk schedulingDisk scheduling
Disk scheduling
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
KNOWLEDGE REPRESENTATION ISSUES.ppt
KNOWLEDGE REPRESENTATION ISSUES.pptKNOWLEDGE REPRESENTATION ISSUES.ppt
KNOWLEDGE REPRESENTATION ISSUES.ppt
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Knowledge representation
Knowledge representationKnowledge representation
Knowledge representation
 

Similar a Logic programming (1)

Logic in Predicate and Propositional Logic
Logic in Predicate and Propositional LogicLogic in Predicate and Propositional Logic
Logic in Predicate and Propositional LogicArchanaT32
 
Chaps 1-3-ai-prolog
Chaps 1-3-ai-prologChaps 1-3-ai-prolog
Chaps 1-3-ai-prologsaru40
 
First Order Logic
First Order LogicFirst Order Logic
First Order LogicMianMubeen3
 
10 logic+programming+with+prolog
10 logic+programming+with+prolog10 logic+programming+with+prolog
10 logic+programming+with+prologbaran19901990
 
Foundations of Knowledge Representation in Artificial Intelligence.pptx
Foundations of Knowledge Representation in Artificial Intelligence.pptxFoundations of Knowledge Representation in Artificial Intelligence.pptx
Foundations of Knowledge Representation in Artificial Intelligence.pptxkitsenthilkumarcse
 
Foundation_Logic_1.pptx discrete mathematics
Foundation_Logic_1.pptx discrete mathematicsFoundation_Logic_1.pptx discrete mathematics
Foundation_Logic_1.pptx discrete mathematicsSherwinSangalang3
 
ARTIFICIAL INTELLIGENCE---UNIT 4.pptx
ARTIFICIAL INTELLIGENCE---UNIT 4.pptxARTIFICIAL INTELLIGENCE---UNIT 4.pptx
ARTIFICIAL INTELLIGENCE---UNIT 4.pptxRuchitaMaaran
 
Ch2 (8).pptx
Ch2 (8).pptxCh2 (8).pptx
Ch2 (8).pptxDeyaHani
 
predicateLogic.ppt
predicateLogic.pptpredicateLogic.ppt
predicateLogic.pptMUZAMILALI48
 
Propositional logic(part 2)
Propositional logic(part 2)Propositional logic(part 2)
Propositional logic(part 2)SURBHI SAROHA
 
AI NOTES ppt 4.pdf
AI NOTES ppt 4.pdfAI NOTES ppt 4.pdf
AI NOTES ppt 4.pdfARMANVERMA7
 
Prolog Programming : Basics
Prolog Programming : BasicsProlog Programming : Basics
Prolog Programming : BasicsMitul Desai
 

Similar a Logic programming (1) (20)

Logic in Predicate and Propositional Logic
Logic in Predicate and Propositional LogicLogic in Predicate and Propositional Logic
Logic in Predicate and Propositional Logic
 
Chaps 1-3-ai-prolog
Chaps 1-3-ai-prologChaps 1-3-ai-prolog
Chaps 1-3-ai-prolog
 
Prolog final
Prolog finalProlog final
Prolog final
 
First Order Logic
First Order LogicFirst Order Logic
First Order Logic
 
10 logic+programming+with+prolog
10 logic+programming+with+prolog10 logic+programming+with+prolog
10 logic+programming+with+prolog
 
Foundations of Knowledge Representation in Artificial Intelligence.pptx
Foundations of Knowledge Representation in Artificial Intelligence.pptxFoundations of Knowledge Representation in Artificial Intelligence.pptx
Foundations of Knowledge Representation in Artificial Intelligence.pptx
 
Foundation_Logic_1.pptx discrete mathematics
Foundation_Logic_1.pptx discrete mathematicsFoundation_Logic_1.pptx discrete mathematics
Foundation_Logic_1.pptx discrete mathematics
 
ARTIFICIAL INTELLIGENCE---UNIT 4.pptx
ARTIFICIAL INTELLIGENCE---UNIT 4.pptxARTIFICIAL INTELLIGENCE---UNIT 4.pptx
ARTIFICIAL INTELLIGENCE---UNIT 4.pptx
 
Chaps 1-3-ai-prolog
Chaps 1-3-ai-prologChaps 1-3-ai-prolog
Chaps 1-3-ai-prolog
 
Ch2 (8).pptx
Ch2 (8).pptxCh2 (8).pptx
Ch2 (8).pptx
 
predicateLogic.ppt
predicateLogic.pptpredicateLogic.ppt
predicateLogic.ppt
 
continuity of module 2.pptx
continuity of module 2.pptxcontinuity of module 2.pptx
continuity of module 2.pptx
 
Propositional logic(part 2)
Propositional logic(part 2)Propositional logic(part 2)
Propositional logic(part 2)
 
artficial intelligence
artficial intelligenceartficial intelligence
artficial intelligence
 
AI NOTES ppt 4.pdf
AI NOTES ppt 4.pdfAI NOTES ppt 4.pdf
AI NOTES ppt 4.pdf
 
Module_5_1.pptx
Module_5_1.pptxModule_5_1.pptx
Module_5_1.pptx
 
Plc part 4
Plc  part 4Plc  part 4
Plc part 4
 
AI-09 Logic in AI
AI-09 Logic in AIAI-09 Logic in AI
AI-09 Logic in AI
 
Prolog Programming : Basics
Prolog Programming : BasicsProlog Programming : Basics
Prolog Programming : Basics
 
AI Lesson 17
AI Lesson 17AI Lesson 17
AI Lesson 17
 

Más de Nitesh Singh

Más de Nitesh Singh (20)

Risk taking and emotions
Risk taking and emotionsRisk taking and emotions
Risk taking and emotions
 
Project report RAILWAY TICKET RESERVATION SYSTEM SAD
Project report RAILWAY TICKET RESERVATION SYSTEM SADProject report RAILWAY TICKET RESERVATION SYSTEM SAD
Project report RAILWAY TICKET RESERVATION SYSTEM SAD
 
The real comedy behind comedy
The real comedy behind comedyThe real comedy behind comedy
The real comedy behind comedy
 
Project report Rs Dry celaners
Project report Rs Dry celaners Project report Rs Dry celaners
Project report Rs Dry celaners
 
BIG DATA ANALYSIS
BIG DATA ANALYSISBIG DATA ANALYSIS
BIG DATA ANALYSIS
 
Udp vs-tcp
Udp vs-tcpUdp vs-tcp
Udp vs-tcp
 
Routing protocols-network-layer
Routing protocols-network-layerRouting protocols-network-layer
Routing protocols-network-layer
 
Routers vs-switch
Routers vs-switchRouters vs-switch
Routers vs-switch
 
New udp
New udpNew udp
New udp
 
I pv4 format
I pv4 formatI pv4 format
I pv4 format
 
I pv4 addressing
I pv4 addressingI pv4 addressing
I pv4 addressing
 
Hub vs-switch
Hub vs-switchHub vs-switch
Hub vs-switch
 
Ftp
FtpFtp
Ftp
 
Email ftp
Email ftpEmail ftp
Email ftp
 
Www and http
Www and httpWww and http
Www and http
 
Transmission main
Transmission mainTransmission main
Transmission main
 
Ta 104-topology
Ta 104-topologyTa 104-topology
Ta 104-topology
 
Ta 104-topology (1)
Ta 104-topology (1)Ta 104-topology (1)
Ta 104-topology (1)
 
Ta 104-tcp
Ta 104-tcpTa 104-tcp
Ta 104-tcp
 
Ta 104-media-3
Ta 104-media-3Ta 104-media-3
Ta 104-media-3
 

Logic programming (1)

  • 1. Logic Programming Tarunpreet Bhatia CSED, Thapar University
  • 2. Logic •Logic is not concerned with what is true. Logic is the study of what follows from what. What conclusions follow from a set of premises. •It can be defined as study of principles of correct reasoning. •The main thing we study in logic are principles governing the validity of arguments and check that whether certain conclusion follows from some given assumption. •Consider: Alice likes everyone who likes logic Alice likes logic ------------------------------------------------ Alice likes himself. Is this argument valid? How do you know? •The logic process takes in some information called premises and produces some outputs called conclusions.
  • 3. Symbolic Logic •Method of representing logical expressions through the use of symbols and variables, rather than in ordinary language. •It provides the benefit of removing the ambiguity that is generally seen in ordinary languages like English. •Its normally divided into two branches: 1.Propositional Logic 2.Predicate Logic
  • 4. Propositional Logic •Simplest form of formal logic. •All statements made are called propositions. •The FORMAL LOGIC is concerned with syntax of statements and not with their semantics. •It deals with manipulation of logical variables which represents proposition. •Propositional Logic is concerned with the subset of declarative sentences that can be either true or false. •Propositional Logic takes only two values, either TRUE or FALSE.
  • 5. Syntax of Propositional Logic 1.Letters A, B,….Z and these letters with subscripted numerals are well-formed atomic propositions. 2.If A and B are well-formed propositions so are 1.~A (Negation of A) 2.(A & B) (Conjunction of A with B) 3.(A V B) (Inclusive Disjunction of A with B) 4.(A  B) (A implies B) 5.(A↔ B) (Material biconditional of A with B) 6.(A B) (Exclusive Disjunction of A with B) 7.(A | B) (Joint Denial of A with B) 8.(A↓ B) (Disjoint Denial of A with B)
  • 6. Semantics of Logical Propositions Truth tables for Logical Connectives A B ~A ~B A V B A & B A  B A ↔ B A B A | B A↓ B T T F F T T T T F F F F T T F T F T F T T F T F F T T F F F T T F F F T T F F T T F T T Example: Let A be a Proposition: The machine is defective. Let B be a Proposition: The production is less.
  • 7. Why we use Propositional Logic? •Its easier to check formulas. •We can exploit the Boolean nature for efficient reasoning. •Its easier to incrementally add formulas. •It can be extended infinitely to many variables using logical quantifiers.
  • 8. First-order Predicate Logic •First-order predicate calculus (FOPL) was developed by logicians to extend the expressiveness of Propositional Logic. •It is generalization of propositional logic that permits reasoning about world entities (objects) as well as classes and subclasses of objects. •Prolog is also based on FOPL. •Predicate logic uses variables and quantifiers which is not present in propositional logic.
  • 9. Why First-Order Predicate Logic ? •Suppose we are having 2 statements , based on which we have to draw a conclusion. Statement 1: All students must take Java. Statement 2: John is a student. •According to human inference, John must take Java. but not according to Propositional logic. (Disadvantage)
  • 10. First-Order Predicate Logic •0 is a natural number Natural(0) •For all x, if x is a natural number, then so is successor of x For all x, natural (x)  natural (successor(x)) •2 is a natural number Natural(2) •-1 is a natural number Natural(-1)
  • 11. First-order predicate calculus First-order predicate calculus classifies the different parts of such statements as follows: 1.Constants 2.Predicates 3.Functions 4.Variables 5.Connectives 6.Quantifiers 7.Punctuation Symbols
  • 12. Predicates •These are names for functions that are true or false, like Boolean functions in a program. •Defined as a relation that binds two atoms together. •Example: Amit likes sweets. likes(amit, areoplanes). amit and sweets are atoms; likes is a predicate •Predicates can take a number of arguments. •In Example , the predicate natural takes one argument. natural(n).
  • 13. Predicates •Its possible to have a function as an argument •Eg. Ravi’s father is Rani’s father father(father(ravi), rani). Father is a predicate and father(ravi) is a function to indicate Ravi’s father.
  • 14. a. Constants •These are usually numbers or names. •Sometimes they are called atoms, since they cannot be broken down into subparts. •Example natural(0). 0 is a constant
  • 15. b. Variables •Stands for Quantities that are yet Unspecified. •Example valuable(X) X is a variable.
  • 16. c. Functions •First-order predicate calculus distinguishes between functions and predicates. •Predicates - true or false and all other are functions which represent non-Boolean values.
  • 17. Quantifiers •Declares the scope or range of variables in a logical expression. •Two basic quantifiers used in logic: A. Universal Quantifier ( ) B. Existential quantifier (Ǝ) •The statement: For all x, natural(x) → natural(successor(x)) means that for every x in the universe, if x is a natural number, then the successor of x is also a natural number. • A universal quantifier is used to state that a relationship among predicates is true for all things in the universe named by the variable x.
  • 18. Quantifiers… •If a is a variable, then is read as: 1.for all a 2.for each a 3.for every a
  • 19. Quantifiers •There is also the existential quantifier, there exists (Ǝ), as in the following statement: •there exists x, natural(x). This statement means that there exists an x such that x is a natural number. •An existential quantifier is used to state that a predicate is true of at least one thing in the universe, indicated by the variable x.
  • 20. Quantifiers… •If b is a variable, then Ǝ is read as: 1.there exists a b 2.for some b 3.for atleast one b
  • 21. Precedence of Connectives ~ highest & V  ↔ lowest
  • 22. Some points to remember •Arguments to predicates and functions can only be terms which is a combination of variables, constants and functions. •Terms cannot contain predicates, quantifiers or connectives.
  • 23. What is Prolog? •Prolog is the most widely used language to have been inspired by logic programming research. •It is a declarative language. •Prolog is computer programming language, used for solving problems that involves objects and relationships between objects. •A Prolog program can also be seen as a relational database containing rules as well as facts.
  • 24. Structure of Logic Programs •Programs consist of procedures. •Procedures consist of clauses. Clauses are statements about what is true about a problem, instead of instructions how to accomplish the solution. •Each clause is a fact or a rule. •Programs are executed by posing queries. •The Prolog system uses the clauses to work out how to accomplish the solution by searching through the space of possible solutions. •Computer Programming in Prolog consists of: –Declaring some facts about objects and their relationships. –Declaring some rules about objects and their relationships. –Asking questions (goals) about objects and their relationships.
  • 25. Example elephant(george). elephant(mary). elephant(X) :- grey(X), mammal(X), hasTrunk(X). Procedure for elephant Predicate Clauses Rule Facts
  • 26. Example ?- elephant(george). yes ?- elephant(jane). no Queries Replies
  • 27. Why Rules? •Rule is an extension of fact with added conditions that have to be specified for it to be true. •Rule is much more compact than a list of facts. •When facts depend on a group of other facts. •Example: John likes all people who likes sweets. Way 1: Write down separate facts as below – likes(james, sweets). likes(john, james). likes(john, joe). likes(john, david). ….. So on
  • 28. Rules… Way 2: John likes any object provided it is a person. In Prolog, likes(john, X) :- like(X, sweets). Head Body If
  • 29. Example First-order predicate calculus for the following logical statements •A horse is a mammal. •A human is a mammal. •Mammals have four legs and no arms, or two legs and two arms. •A horse has no arms. •A human has arms. •A human has no legs. •A human has two arms. •A human has two legs.
  • 30. Example To write a rule, X is a sister of Y •X is a female. •X has mother M and father F and •Y has the same mother and father as X does. sister_of(X, Y) :- female(X), parents(X, M, F), parents(Y, M, F).
  • 31. Example •To define a rule “X is a grandfather of Y, if X is a father of Z and Z is a parent of Y ” using logic programming convention, then we write grandfather(X, Y) :- father(X, Z) , parent(Z, Y). •X is a sibling of Y if they both have the same parent. sibling(X, Y) :- parent(Z, X) , parent(Z, Y).
  • 32. Types of Query 1.In Ground query , the goal(s) contains constants. Answer to ground query is either yes or no depending upon whether it is a logical consequence of a logic program or not. 2.In Non – Ground query, the goal(s) should have at least one variable as an argument.
  • 33. Simple Queries a.Ground Query: “Is raman a grandfather of manu ?” ?- grandfather(raman, manu). b. Non ground: “Does there exist X such that X is a father of manu ?” {Who is father of manu?} ?- father(X, robert).
  • 34. Conjunctive Queries a.Ground Query “Is raman father of robert and robert is a father of mike? ?- father(raman, robert), father(robert, mike). Answer: yes b. Non Ground Query ?- father(raman, X), father(X, mike). Answer: X = robert
  • 35. Inference Rule •Ways of deriving or proving new statements from a given set of statements. •Example a b, b  c We can derive a  c
  • 36. Horn Clauses •Horn clause (named after its inventor Alfred Horn) is a statement of the form: a1 and a2 and a3 . . . and an → b where the ai are only allowed to be simple statements involving no connectives. •Thus, there are no or connectives and no quantifiers in Horn clauses. •b is called the head of the clause, and the a1 . . . , an is the body of •the clause. •In the Horn clause, the number of ai’s may be 0, in which case the Horn clause has the form: → b •Such a clause means that b is always true. In other words, b is an axiom and is usually written without the connective →. Such clauses are sometimes also called facts.
  • 37. Example Consider the following statement: x is a grandparent of y if x is the parent of someone who is the parent of y. Translating this into predicate calculus, we get grandparent (x, y) for all x, for all y, (there exists z, parent(x, z) and parent(z, y)). As a Horn clause this is expressed simply as: grandparent(x, y) parent(x, z) and parent(z, y) .
  • 38. Example Consider the following statement: For all x, if x is a mammal then x has two or four legs. Translating in predicate calculus, we get: for all x, mammal(x) → legs(x, 2) or legs(x, 4). This may be approximated by the following Horn clauses: mammal(x) and not legs(x, 2) → legs(x, 4). mammal(x) and not legs(x, 4) → legs(x, 2).
  • 39. Resolution •Resolution says that if we have two Horn clauses, and we can match the head of the first Horn clause with one of the statements in the body of the second clause, then the first clause can be used to replace its head in the second clause by its body. In symbols, if we have Horn clauses: a ← a1, . . . , an. b ← b1, . . . , bm. and bi matches a, then we can infer the clause: b ← b1, . . . , bi-1, a1, . . . , an, bi+1, . . . , bm.
  • 40. •The system attempts to apply resolution by matching one of the goals in the body of the headless clause with the head of a known clause. It then replaces the matched goal with the body of that clause, creating a new list of goals, which it continues to modify in the same way. The new goals are called subgoals. •If the system succeeds eventually in eliminating all goals— thus deriving the empty Horn clause—then the original statement has been proved. •In symbols, if we have the goal: ← a. •and the clause a ← a1, . . . , an, then resolution replaces the original goal a with the subgoals: ← a1, . . . , an.
  • 41. Unification •To match statements that contain variables, we must set the variables equal to terms so that the statements become identical and can be canceled from both sides. This process of pattern matching to make statements identical is called unification, and variables that are set equal to patterns are said to be instantiated. •Unification is the process by which variables are instantiated, or allocated memory and assigned values, so that patterns match during resolution. It is also the process of making two terms the same in some sense. •The built in Prolog operator '=' can be used to unify two terms.
  • 42. Examples ?- me = me. yes ?- me = you. no ?- me = X. X = me ?- f(a, X) = f(Y, b). X = b Y = a
  • 43. ?- f(X) = g(X). no ?- f(X) = f(a, b). no ?- f(a, g(X)) = f(Y, b). no ?- f(a, g(X)) = f(Y, g(b)). X = b Y = a
  • 44. Unification algorithm for Prolog 1. A constant unifies only with itself: me = me succeeds but me = you fails. 2. A variable that is uninstantiated unifies with anything and becomes instantiated to that thing. 3. A structured term (i.e., a function applied to arguments) unifies with another term only if it has the same function name and the same number of arguments, and the arguments can be unified recursively. Thus, f(a, X) unifies with f(Y, b) by instantiating X to b and Y to a. A variation on case 2 is when two uninstantiated variables are unified: ?- X = Y. X = _23 Y = _23 The number printed on the right-hand side—in this case, indicates an internal memory location set aside for that variable. Thus, unification causes uninstantiated variables to share memory—that is, to become aliases of each other.
  • 45. Example Given the rules and facts: legs(x, 2) ← mammal(x), arms(x, 2). legs(x, 4) ← mammal(x), arms(x, 0). mammal(horse). arms(horse, 0). if we supply the query: ← legs(horse,4). We get X= horse as output
  • 46. Example For example, given the Horn clauses: ancestor(x, y) ← parent(x, z), ancestor(z, y). ancestor(x, x). parent(amy, bob). if we provide the query: ← ancestor(x, bob). We get X = bob , X = amy
  • 47. Prolog Arithmetic ?- write(3 + 5). 3 + 5 To force the evaluation of an arithmetic term, a new operation is required: the built- in predicate is. ?- X is 3 + 5, write(X). X = 8 ?- 3 + 4 = 4 + 3. no To get equality of values, we must force evaluation using is, for example, by writing the predicate: valequal(Term1, Term2) :- X is Term1, Y is Term2, X = Y. We would then get: ?- valequal(3 + 4, 4 + 3). yes
  • 48. ?- foo(a,Y) = foo(X,b). ** Instantiation of variables may occur in either of the terms to be unified ** Y=b X=a yes Example
  • 49. ?- foo(a,b) = foo(X,X). ** In this case there is no unification because foo(X,X) must have the same 1st and 2nd arguments ** no Example
  • 50. Example ?- 2*3+4 = X+Y. ** The term 2*3+4 has principal functor + X=2*3 and therefore unifies X+Y with X instantiated to 2*3 and Y instantiated to 4 ** yes
  • 51. Example ?- [a,b,c] = [X,Y,Z]. ** Lists unify just like other terms ** X=a Y=b Z=c yes
  • 52. Example Do the following pairs of items unify (match) ? eats(fred,tomatoes) eats(WHOM,WHAT) Yes WHOM = fred and WHAT = tomatoes.
  • 53. Example cd(29,beatles,sgt_pepper). cd(A,B,help). No sgt_pepper and help do not unify f(X,Y) f(P,P) Yes X = P and Y = P. A variable (such as X) can be bound to another variable (such as P). In this case we can also infer that X = Y
  • 55. Example likes(jane,X) likes(X,jim) No X can not be bound to both jane and jim f(foo,L) f(A,A) Yes A = foo and A = L. Hence L = foo A and L are variables