SlideShare a Scribd company logo
2
Most read
7
Most read
11
Most read
Logic Programming
• We now examine a radically different paradigm for programming:
declarative programming
– rather than writing control constructs (loops, selection statements,
subroutines)
– you specify knowledge and how that knowledge is to be applied
through a series of rules
– the programming language environment uses one or more built-in
methods to reason over the knowledge and prove things (or answer
questions)
• in logic programming, the common approach is to apply the methods of
resolution and unification
• While these languages have numerous flaws, they can build
powerful problem solving systems with little programming
expertise
– they have been used extensively in AI research
– Developed by Alain Colmerauer, Phillippe Roussell, and Robert
Kowlalski.
Terminology
• Logic Programming is a specific type of a more general
class: production systems (also called rule-based
systems)
– a production system is a collection of facts (knowledge), rules
(which are another form of knowledge) and control strategies
– we often refer to the collection of facts /axioms (what we
know) as working memory
– Facts-Predicate
– the rules are simple if-then statements where the condition
tests values stored in working memory and the action (then
clause) manipulates working memory (adds new facts, deletes
old facts, modifies facts)
Terminology
– the control strategies help select among a set of rules that
match – that is, if multiple rules have matching conditions, the
control strategies can help decide which rule we select this
time through
– there are other control strategies as well – whether we work
from conditions to conclusions or from conclusions to
conditions (forward, backward chaining respectively)
Logic Programming
• Also known as declarative programming
• Mostly synonymous with the Prolog language because it
is the only widely used language for logic programming
• A declarative program does not have code, instead it
defines two pieces of knowledge
– facts – statements that are true
– rules – if-then statements that are truth preserving
• We use the program to prove if a statement is true and/or
answer questions
• The reason this works is that Prolog has built-in problem
solving processes called resolution and unification
– in fact Prolog is not so much a programming language as it is a
tool, but it does have some programming language features that
make it mildly programmable
Background for Logic
• A proposition is a logical statement that is only made if it is true
– Today is Tuesday
– The Earth is round
• A proposition is a declarative sentence that is either true, or
false. but not both (at the same time).
• To represent propositions, propositional variables are used.
• Symbolic logic uses propositions to express ideas, relationships
between ideas and to generate new ideas based on the given
propositions
Background for Logic
• Propositions will either be true (if stated) or something to prove or
disprove (determine if it is true) – we do not include statements
which are false
• Two forms of propositions are
– Atomic propositions
– Compound terms (multiple propositions connected through the logical
operators of and, or, not, and implies)
– •Propositions are represented by a predicate applied to a tuple of terms. A
predicate represents a property of or relation between terms that can be true
or false: Brother(John, Fred), Left-of(Square1, Square2)
• For symbolic logic, we use 1st
order predicate calculus
– statements include predicates like round(x) where this is true if we can find
an x that makes it true such as round(Earth) or round(x) when x = Earth
– you might think of a predicate as a Boolean function except that rather than
returning T or F, it finds an x (quantifier) that makes it true
Logic Operators – Equivalence means that
both expressions have
identical truth tables
– Implication is like an if-
then statement
• if a is true then b is true
• note that this does not
necessarily mean that if
a is false that b must also
be false
– Universal quantifier says
that this is true no matter
what x is
– Existential quantifier says
that there is an X that
fulfills the statement
Name Symbol Example Meaning
negation   a not a
conjunction  a  b a and b
disjunction  a  b a or b
equivalence  a  b a is equivalent
to b
implication 

a  b
a  b
a implies b
b implies a
universal X.P For all X, P is
true
existential X.P There exists a
value of X
such that P is
true

 X.(woman(X)  human(X))
– if X is a woman, then X is a human
X.(mother(mary, X)  male(X))
– Mary has a son (X)


Clausal Form
• To use resolution, all statements must be in clausal form
– B1  B2  …  Bn  A1  A2  …  Am
• B1 or B2 or … or Bn is true if A1and A2 and … and Am are all true
– the left hand side is the consequent (what we are trying to prove) and the
right hand side is the antecedent (what conditions must hold true)
– notice we have turned the if-then statement around,
– We must modify our knowledge so that:
• existential quantifiers are not required (eliminate all of them)
• universal quantifiers are implied (by replacing each with a specific
variable)
• no negations (all negations must be removed)
– We break down our statements to have a single item on the left
• the above statement is broken into multiple statements such as
– B1  A1  A2  …  Am
– B2  A1  A2  …  Am …
– Bn  A1  A2  …  Am
• note that propositions and predicates by themselves are already in clausal
form, such as round(Earth) and Sunny
Example Statements
Consider the following knowledge:
Bob is Fred’s father  father(Bob, Fred)
Sue is Fred’s mother  mother(Sue, Fred)
Barbara is Fred’s sister  sister(Barbara, Fred)
Jerry is Bob’s father  father(Jerry, Bob)
And the following rules:
A person’s father’s father is the person’s grandfather
A person’s father or mother is that person’s parent
A person’s sister or bother is that person’s sibling
If a person has a parent and a sibling, then the sibling has the same parent
These might be captured in first-order predicate calculus as:
x, y, z : if father(x, y) and father(y, z) then grandfather(x, z)
x, y : if father(x, y) or mother(x, y) then parent(x, y)
x, y : if sister(x, y) or brother(x, y) then sibling(x, y) and sibling(y, x)
x, y, z : if parent(x, y) and sibling(y, z) then parent(x, z)
We would rewrite these as
grandfather(x, z)  father(x, y) and father(y, z)
parent(x, y)  father(x, y)
parent(x, y)  mother(x, y) etc




Resolution and Unification
• Given a collection of knowledge
– we will want to prove certain statements are true or answer questions
• For instance, from the previous example, we might ask
– who is Bob’s grandfather?
– is Sue Barbara’s parent?
• How can this be done? Through backward chaining through
rules
• Here is how backward chaining works
– we want to prove that A is true
– find a rule with A on its LHS and whatever is on the rule’s RHS must
now be proven to be true, so we add the items on the RHS to a list of
things we are trying to prove
– repeat until
• we match something that we know is true to our list, then remove the item
• we run out of items on our list, then we are done, we have proven A is true
• To complicate matters, predicates (e.g., round(X)) need to be
unified, that is, to prove round(X) is true, we have to find some
X where we know it is true, for instance, round(Earth)
Complete Logic Example
Assume that we know the following about pets:
poodle(COCOA)
setter(BIG)
terrier(SCOTTY)
dog(X)  poodle(X) (poodles are dogs)
dog(X)  setter(X) (setters are dogs)
dog(X)  terrier(X) (terriers are dogs)
small(X)  poodle(X) (poodles are small)
small(X)  terrier(X) (terriers are small)
big(X)  setter(X) (setters are big)
pet(X)  dog(X) (dogs are pets)
indoorpet(X)  pet(X) and small(X)
(small pets are indoor pets)
outdoorpet(X)  pet(X) and big(X)
(big pets are outdoor pets)
If we want to find out what would
make a good indoor pet, we ask
indoorpet(?)
This requires finding pet(X) and small(X)
(find an X to make both predicates true)
pet(X) is implied by dog(X),
dog(X) is implied by terrier(X),
SCOTTY is a terrier so SCOTTY
is a dog so SCOTTY is a pet
Can we find if SCOTTY is small?
small(SCOTTY) is implied
by terrier(SCOTTY) which we
already know is true, therefore,
since terrier(SCOTTY) is true, small(SCOTTY)
and pet(SCOTTY)
are true, so indoorpet(SCOTTY) is
True
Continuing with this process
will also prove that indoorpet(COCOA)
is true.
PROLOG
• PROLOG is a programming language that allows the
programmer to specify declarative statements only
– declarative statements (things you are declaring) fall into 2
categories
• predicates/propositions that are true
• clauses (truth preserving rules in clausal form)
– once specified, the programmer then introduces questions to be
answered
• PROLOG uses resolution (backward chaining) and unification to
perform the problem solving automatically
• PROLOG was developed in France and England in the
late 70s
– the intent was to provide a language that could accommodate
logic statements and has largely been used in AI but also to a
lesser extent as a database language or to solve database
related problems
Elements of Prolog
• Terms – constant, variable, structure
– constants are atoms or integers (atoms are like those symbols found
in Lisp)
– variables are not bound to types, but are bound to values when
instantiated (via unification)
– an instantiation will last as long as it takes to complete a goal
• proving something is true, or reaching a dead end with the current instantiation
– structures are predicates and are represented as
• functor(parameter list) where functor is the name of the predicate
• All statements in Prolog consist of clauses
– headed clauses are rules
– headless clauses are statements that are always true
• in reality, a headless clause is a rule whose condition is always true
– all clauses must end with a period
Rules
• All rules are stated in Horn clause form
– the consequence comes first
• the symbol :- is used to separate the consequence from the antecedent
– And is denoted by , and Or is denoted by ; or separating the rule
into two separately rules
– variables in rules are indicated with upper-case letters
– rules end with a .
– examples:
• parent(X, Y) :- mother(X, Y).
• parent(X, Y) :- father(X, Y).
• grandparent(X, Z) :- parent(X, Y), parent(Y, Z).
• sibling(X, Y) :- mother(M, X), mother(M, Y), father(F, X), father(F, Y).
– we can use _ as a wildcard meaning this is true if we can find
any clause that fits
• father(X) :- father(X, _), male(X).
– X is a father if X is male and is someone’s father
Other Language Features
• Assignment statements are available using the is operator
– A is B / 17 + C.
• this works if B and C are instantiated and A is not
• however, is does not work like a true assignment statement
– you can not do x is x + y – this can never be true!
– we might use the assignment operator in a rule such as
• distance(X,Y) :- speed(X,Speed), time(X,Time), Y is Speed * Time
• List structures are also available using [ ] marks
– as in new_list([apple, prune, grape, kumquat]).
– this is not a binding of new_list to the values, but instead
new_list is a predicate with a true instance of the predicate
being the parameter [apple, prune, grape, kumquat]
• lists can also be represented as a head and tail using | to separate the two
parts similar to how Lisp uses CAR and CDR
More Prolog Examples
predecessor(X,Y) :- parent(X,Y); parent(X,Z), predecessor(Z,Y).
// X is a predecessor of Y if X is Y’s parent or
// if X is parent of someone else who is a predecessor of Y
Using Not:
dog(X) :- poodle(X).
dog(X) :- terrier(X).
likes(X,Y) :- dog(X), dog(Y), not (X=Y).
// can also be written as X == Y
Database example: imagine we have a collection of terms:
record(name, yearborn, salary)
Successful person is someone who either makes > $50000 in salary
or was born after 1980 and is making more than $40000.
success(X) :- record(X, Y, Z), Z > 50000;
record(X, Y, Z), Y > 1980, Z > 40000.
Notice the use of “not”
here – in Prolog, x != y is
available but ~foo(x) is not
That is, we only declare
statements that are true, we
cannot declare the negation
of statements that are false
Additional Prolog Examples
Defining Max:
max(X,Y,M) :- X > Y, M is X.
max(X,Y,M) :- Y >= X, M is Y.
Defining GCD:
gcd(X,Y,D) :- X=Y, D is X.
gcd(X,Y,D) :- X<Y, Y1 is Y - X, gcd(X, Y1, D).
gcd(X,Y,D) :- X>Y, gcd(Y, X, D).
Two List examples
Defining Length:
length([ ], 0). // empty list has a length of 0
length([ _ | Tail, N) :- length(Tail, N1), N is 1 + N1. // a list that has an
// item _ and a Tail is length N if the length of Tail is N1 where N = 1 + N1
Sum of the items in a list:
sum([ ], 0). // sum of an empty list is 0
sum([X | Tail], S) :- sum(Tail, S1), S is X + S1.
Advantages of Prolog
Deriving the permutations of a list List:
perm(List,[H|Perm]):-delete(H,List,Rest),perm(Rest,Perm).
perm([ ],[ ]).
delete(X,[X|T],T).
delete(X,[H|T],[H|NT]):-delete(X,T,NT).
Sorting a list of values stored in List:
insert_sort(List,Sorted):-i_sort(List,[],Sorted).
i_sort([ ],Acc,Acc).
i_sort([H|T],Acc,Sorted):-insert(H,Acc,NAcc),i_sort(T,NAcc,Sorted).
insert(X,[Y|T],[Y|NT]):-X>Y,insert(X,T,NT).
insert(X,[Y|T],[X,Y|T]):-X=<Y.
insert(X,[],[X]).
A naïve sort (inefficient, but simple):
naive_sort(List,Sorted):-perm(List,Sorted),is_sorted(Sorted).
is_sorted([ ]).
is_sorted([ _ ]).
is_sorted([X,Y|T]):-X=<Y,is_sorted([Y|T]).
• There are several advantages to using Prolog
– ability to create automated problem solvers merely by listing knowledge
– a shortcut way to build and query a database
– solving significantly difficult problems with minimal code:
Deficiencies of Prolog
• Lack of control structures
– Prolog offers built-in control of resolution and unification
• you often have to force a problem into the resolution mold to solve it with Prolog (most
problems cannot or should not be solved in this way)
• Inefficiencies of resolution
– resolution, as a process, is intractable (O(2n
) for n clauses)
• useful heuristics could be applied to reduce the complexity, but there is no way to apply
heuristics in Prolog
– they would just be additional rules that increases the size of n!
• Closed world assumption
– in any form of logic reasoning, if something is not known, it is assumed to be
false and everything is either true or false
• Negation is difficult to represent
– since there is no NOT in Prolog, how do we represent NOT?
• recall that anything explicitly stated must be true so we cannot specify NOT something as
something would then be false
• we can represent A != B, but we cannot represent ~dog(X).

More Related Content

PDF
Logic programming (1)
PPT
Predlogic
PDF
10 logic+programming+with+prolog
PPT
Propositional Logic and Pridicate logic
PPT
knowledge representation in artificial intellegence.ppt
PPT
01bkb04p.ppt
PPTX
ARTIFICIAL INTELLIGENCE---UNIT 4.pptx
PPT
PPL subject for engineering students ppt
Logic programming (1)
Predlogic
10 logic+programming+with+prolog
Propositional Logic and Pridicate logic
knowledge representation in artificial intellegence.ppt
01bkb04p.ppt
ARTIFICIAL INTELLIGENCE---UNIT 4.pptx
PPL subject for engineering students ppt

Similar to Logical Programming Paradigm for programming Languages. (20)

PPTX
Module4_AI 4th semester engineering.pptx
PPTX
Basic Knowledge Representation and Reasonong
PPTX
Logic in Predicate and Propositional Logic
PPT
PropositionalLogic.ppt
PPT
aiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiai
PPT
IDCB-IntroductionIDCB-IntroductionIDCB-IntroductionIDCB-IntroductionIDCB-Intr...
PPT
predicateLogic.ppt
PDF
BCS515B Module 5 vtu notes : Artificial Intelligence Module 5.pdf
PPTX
An introduction to Prolog language slide
PPT
Prolog basics
PPTX
Prolog final
PPT
PPTX
ProLog (Artificial Intelligence) Introduction
PPT
Unit III Knowledge Representation in AI K.Sundar,AP/CSE,VEC
PDF
PPTX
Natural language processing: word senses and relations
PPT
1019Lec1.ppt
PPTX
chpater 2 class eleven Lectuare-2.1.pptx
PDF
16_FirstOrderLogic.p_4_moduleModuleNotespdf
PDF
unit-3 First half..pdf nice ppt helps in ai intelligence
Module4_AI 4th semester engineering.pptx
Basic Knowledge Representation and Reasonong
Logic in Predicate and Propositional Logic
PropositionalLogic.ppt
aiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiaiai
IDCB-IntroductionIDCB-IntroductionIDCB-IntroductionIDCB-IntroductionIDCB-Intr...
predicateLogic.ppt
BCS515B Module 5 vtu notes : Artificial Intelligence Module 5.pdf
An introduction to Prolog language slide
Prolog basics
Prolog final
ProLog (Artificial Intelligence) Introduction
Unit III Knowledge Representation in AI K.Sundar,AP/CSE,VEC
Natural language processing: word senses and relations
1019Lec1.ppt
chpater 2 class eleven Lectuare-2.1.pptx
16_FirstOrderLogic.p_4_moduleModuleNotespdf
unit-3 First half..pdf nice ppt helps in ai intelligence
Ad

More from SwatiAtulJoshi (6)

PPTX
UNIVERSAL_HUMAN_VLAUES2_human aspirations1.pptx
PPTX
universal_human Values _1_As suggested by AICTEaj.pptx
PDF
Kinematics for robotics inverse and forward
PPTX
Restoring and nonrestoring division.pptx
PPT
How to do file-handling - in C language
PPTX
Introduction to Object oriented Programming basics
UNIVERSAL_HUMAN_VLAUES2_human aspirations1.pptx
universal_human Values _1_As suggested by AICTEaj.pptx
Kinematics for robotics inverse and forward
Restoring and nonrestoring division.pptx
How to do file-handling - in C language
Introduction to Object oriented Programming basics
Ad

Recently uploaded (20)

PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PPTX
L1 - Introduction to python Backend.pptx
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Softaken Excel to vCard Converter Software.pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Introduction to Artificial Intelligence
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
CHAPTER 2 - PM Management and IT Context
PPTX
Transform Your Business with a Software ERP System
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
System and Network Administration Chapter 2
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Design an Analysis of Algorithms I-SECS-1021-03
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
wealthsignaloriginal-com-DS-text-... (1).pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
L1 - Introduction to python Backend.pptx
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Softaken Excel to vCard Converter Software.pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Introduction to Artificial Intelligence
Understanding Forklifts - TECH EHS Solution
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
CHAPTER 2 - PM Management and IT Context
Transform Your Business with a Software ERP System
2025 Textile ERP Trends: SAP, Odoo & Oracle
Design an Analysis of Algorithms II-SECS-1021-03
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Operating system designcfffgfgggggggvggggggggg
System and Network Administration Chapter 2

Logical Programming Paradigm for programming Languages.

  • 1. Logic Programming • We now examine a radically different paradigm for programming: declarative programming – rather than writing control constructs (loops, selection statements, subroutines) – you specify knowledge and how that knowledge is to be applied through a series of rules – the programming language environment uses one or more built-in methods to reason over the knowledge and prove things (or answer questions) • in logic programming, the common approach is to apply the methods of resolution and unification • While these languages have numerous flaws, they can build powerful problem solving systems with little programming expertise – they have been used extensively in AI research – Developed by Alain Colmerauer, Phillippe Roussell, and Robert Kowlalski.
  • 2. Terminology • Logic Programming is a specific type of a more general class: production systems (also called rule-based systems) – a production system is a collection of facts (knowledge), rules (which are another form of knowledge) and control strategies – we often refer to the collection of facts /axioms (what we know) as working memory – Facts-Predicate – the rules are simple if-then statements where the condition tests values stored in working memory and the action (then clause) manipulates working memory (adds new facts, deletes old facts, modifies facts)
  • 3. Terminology – the control strategies help select among a set of rules that match – that is, if multiple rules have matching conditions, the control strategies can help decide which rule we select this time through – there are other control strategies as well – whether we work from conditions to conclusions or from conclusions to conditions (forward, backward chaining respectively)
  • 4. Logic Programming • Also known as declarative programming • Mostly synonymous with the Prolog language because it is the only widely used language for logic programming • A declarative program does not have code, instead it defines two pieces of knowledge – facts – statements that are true – rules – if-then statements that are truth preserving • We use the program to prove if a statement is true and/or answer questions • The reason this works is that Prolog has built-in problem solving processes called resolution and unification – in fact Prolog is not so much a programming language as it is a tool, but it does have some programming language features that make it mildly programmable
  • 5. Background for Logic • A proposition is a logical statement that is only made if it is true – Today is Tuesday – The Earth is round • A proposition is a declarative sentence that is either true, or false. but not both (at the same time). • To represent propositions, propositional variables are used. • Symbolic logic uses propositions to express ideas, relationships between ideas and to generate new ideas based on the given propositions
  • 6. Background for Logic • Propositions will either be true (if stated) or something to prove or disprove (determine if it is true) – we do not include statements which are false • Two forms of propositions are – Atomic propositions – Compound terms (multiple propositions connected through the logical operators of and, or, not, and implies) – •Propositions are represented by a predicate applied to a tuple of terms. A predicate represents a property of or relation between terms that can be true or false: Brother(John, Fred), Left-of(Square1, Square2) • For symbolic logic, we use 1st order predicate calculus – statements include predicates like round(x) where this is true if we can find an x that makes it true such as round(Earth) or round(x) when x = Earth – you might think of a predicate as a Boolean function except that rather than returning T or F, it finds an x (quantifier) that makes it true
  • 7. Logic Operators – Equivalence means that both expressions have identical truth tables – Implication is like an if- then statement • if a is true then b is true • note that this does not necessarily mean that if a is false that b must also be false – Universal quantifier says that this is true no matter what x is – Existential quantifier says that there is an X that fulfills the statement Name Symbol Example Meaning negation   a not a conjunction  a  b a and b disjunction  a  b a or b equivalence  a  b a is equivalent to b implication   a  b a  b a implies b b implies a universal X.P For all X, P is true existential X.P There exists a value of X such that P is true   X.(woman(X)  human(X)) – if X is a woman, then X is a human X.(mother(mary, X)  male(X)) – Mary has a son (X)  
  • 8. Clausal Form • To use resolution, all statements must be in clausal form – B1  B2  …  Bn  A1  A2  …  Am • B1 or B2 or … or Bn is true if A1and A2 and … and Am are all true – the left hand side is the consequent (what we are trying to prove) and the right hand side is the antecedent (what conditions must hold true) – notice we have turned the if-then statement around, – We must modify our knowledge so that: • existential quantifiers are not required (eliminate all of them) • universal quantifiers are implied (by replacing each with a specific variable) • no negations (all negations must be removed) – We break down our statements to have a single item on the left • the above statement is broken into multiple statements such as – B1  A1  A2  …  Am – B2  A1  A2  …  Am … – Bn  A1  A2  …  Am • note that propositions and predicates by themselves are already in clausal form, such as round(Earth) and Sunny
  • 9. Example Statements Consider the following knowledge: Bob is Fred’s father  father(Bob, Fred) Sue is Fred’s mother  mother(Sue, Fred) Barbara is Fred’s sister  sister(Barbara, Fred) Jerry is Bob’s father  father(Jerry, Bob) And the following rules: A person’s father’s father is the person’s grandfather A person’s father or mother is that person’s parent A person’s sister or bother is that person’s sibling If a person has a parent and a sibling, then the sibling has the same parent These might be captured in first-order predicate calculus as: x, y, z : if father(x, y) and father(y, z) then grandfather(x, z) x, y : if father(x, y) or mother(x, y) then parent(x, y) x, y : if sister(x, y) or brother(x, y) then sibling(x, y) and sibling(y, x) x, y, z : if parent(x, y) and sibling(y, z) then parent(x, z) We would rewrite these as grandfather(x, z)  father(x, y) and father(y, z) parent(x, y)  father(x, y) parent(x, y)  mother(x, y) etc    
  • 10. Resolution and Unification • Given a collection of knowledge – we will want to prove certain statements are true or answer questions • For instance, from the previous example, we might ask – who is Bob’s grandfather? – is Sue Barbara’s parent? • How can this be done? Through backward chaining through rules • Here is how backward chaining works – we want to prove that A is true – find a rule with A on its LHS and whatever is on the rule’s RHS must now be proven to be true, so we add the items on the RHS to a list of things we are trying to prove – repeat until • we match something that we know is true to our list, then remove the item • we run out of items on our list, then we are done, we have proven A is true • To complicate matters, predicates (e.g., round(X)) need to be unified, that is, to prove round(X) is true, we have to find some X where we know it is true, for instance, round(Earth)
  • 11. Complete Logic Example Assume that we know the following about pets: poodle(COCOA) setter(BIG) terrier(SCOTTY) dog(X)  poodle(X) (poodles are dogs) dog(X)  setter(X) (setters are dogs) dog(X)  terrier(X) (terriers are dogs) small(X)  poodle(X) (poodles are small) small(X)  terrier(X) (terriers are small) big(X)  setter(X) (setters are big) pet(X)  dog(X) (dogs are pets) indoorpet(X)  pet(X) and small(X) (small pets are indoor pets) outdoorpet(X)  pet(X) and big(X) (big pets are outdoor pets) If we want to find out what would make a good indoor pet, we ask indoorpet(?) This requires finding pet(X) and small(X) (find an X to make both predicates true) pet(X) is implied by dog(X), dog(X) is implied by terrier(X), SCOTTY is a terrier so SCOTTY is a dog so SCOTTY is a pet Can we find if SCOTTY is small? small(SCOTTY) is implied by terrier(SCOTTY) which we already know is true, therefore, since terrier(SCOTTY) is true, small(SCOTTY) and pet(SCOTTY) are true, so indoorpet(SCOTTY) is True Continuing with this process will also prove that indoorpet(COCOA) is true.
  • 12. PROLOG • PROLOG is a programming language that allows the programmer to specify declarative statements only – declarative statements (things you are declaring) fall into 2 categories • predicates/propositions that are true • clauses (truth preserving rules in clausal form) – once specified, the programmer then introduces questions to be answered • PROLOG uses resolution (backward chaining) and unification to perform the problem solving automatically • PROLOG was developed in France and England in the late 70s – the intent was to provide a language that could accommodate logic statements and has largely been used in AI but also to a lesser extent as a database language or to solve database related problems
  • 13. Elements of Prolog • Terms – constant, variable, structure – constants are atoms or integers (atoms are like those symbols found in Lisp) – variables are not bound to types, but are bound to values when instantiated (via unification) – an instantiation will last as long as it takes to complete a goal • proving something is true, or reaching a dead end with the current instantiation – structures are predicates and are represented as • functor(parameter list) where functor is the name of the predicate • All statements in Prolog consist of clauses – headed clauses are rules – headless clauses are statements that are always true • in reality, a headless clause is a rule whose condition is always true – all clauses must end with a period
  • 14. Rules • All rules are stated in Horn clause form – the consequence comes first • the symbol :- is used to separate the consequence from the antecedent – And is denoted by , and Or is denoted by ; or separating the rule into two separately rules – variables in rules are indicated with upper-case letters – rules end with a . – examples: • parent(X, Y) :- mother(X, Y). • parent(X, Y) :- father(X, Y). • grandparent(X, Z) :- parent(X, Y), parent(Y, Z). • sibling(X, Y) :- mother(M, X), mother(M, Y), father(F, X), father(F, Y). – we can use _ as a wildcard meaning this is true if we can find any clause that fits • father(X) :- father(X, _), male(X). – X is a father if X is male and is someone’s father
  • 15. Other Language Features • Assignment statements are available using the is operator – A is B / 17 + C. • this works if B and C are instantiated and A is not • however, is does not work like a true assignment statement – you can not do x is x + y – this can never be true! – we might use the assignment operator in a rule such as • distance(X,Y) :- speed(X,Speed), time(X,Time), Y is Speed * Time • List structures are also available using [ ] marks – as in new_list([apple, prune, grape, kumquat]). – this is not a binding of new_list to the values, but instead new_list is a predicate with a true instance of the predicate being the parameter [apple, prune, grape, kumquat] • lists can also be represented as a head and tail using | to separate the two parts similar to how Lisp uses CAR and CDR
  • 16. More Prolog Examples predecessor(X,Y) :- parent(X,Y); parent(X,Z), predecessor(Z,Y). // X is a predecessor of Y if X is Y’s parent or // if X is parent of someone else who is a predecessor of Y Using Not: dog(X) :- poodle(X). dog(X) :- terrier(X). likes(X,Y) :- dog(X), dog(Y), not (X=Y). // can also be written as X == Y Database example: imagine we have a collection of terms: record(name, yearborn, salary) Successful person is someone who either makes > $50000 in salary or was born after 1980 and is making more than $40000. success(X) :- record(X, Y, Z), Z > 50000; record(X, Y, Z), Y > 1980, Z > 40000. Notice the use of “not” here – in Prolog, x != y is available but ~foo(x) is not That is, we only declare statements that are true, we cannot declare the negation of statements that are false
  • 17. Additional Prolog Examples Defining Max: max(X,Y,M) :- X > Y, M is X. max(X,Y,M) :- Y >= X, M is Y. Defining GCD: gcd(X,Y,D) :- X=Y, D is X. gcd(X,Y,D) :- X<Y, Y1 is Y - X, gcd(X, Y1, D). gcd(X,Y,D) :- X>Y, gcd(Y, X, D). Two List examples Defining Length: length([ ], 0). // empty list has a length of 0 length([ _ | Tail, N) :- length(Tail, N1), N is 1 + N1. // a list that has an // item _ and a Tail is length N if the length of Tail is N1 where N = 1 + N1 Sum of the items in a list: sum([ ], 0). // sum of an empty list is 0 sum([X | Tail], S) :- sum(Tail, S1), S is X + S1.
  • 18. Advantages of Prolog Deriving the permutations of a list List: perm(List,[H|Perm]):-delete(H,List,Rest),perm(Rest,Perm). perm([ ],[ ]). delete(X,[X|T],T). delete(X,[H|T],[H|NT]):-delete(X,T,NT). Sorting a list of values stored in List: insert_sort(List,Sorted):-i_sort(List,[],Sorted). i_sort([ ],Acc,Acc). i_sort([H|T],Acc,Sorted):-insert(H,Acc,NAcc),i_sort(T,NAcc,Sorted). insert(X,[Y|T],[Y|NT]):-X>Y,insert(X,T,NT). insert(X,[Y|T],[X,Y|T]):-X=<Y. insert(X,[],[X]). A naïve sort (inefficient, but simple): naive_sort(List,Sorted):-perm(List,Sorted),is_sorted(Sorted). is_sorted([ ]). is_sorted([ _ ]). is_sorted([X,Y|T]):-X=<Y,is_sorted([Y|T]). • There are several advantages to using Prolog – ability to create automated problem solvers merely by listing knowledge – a shortcut way to build and query a database – solving significantly difficult problems with minimal code:
  • 19. Deficiencies of Prolog • Lack of control structures – Prolog offers built-in control of resolution and unification • you often have to force a problem into the resolution mold to solve it with Prolog (most problems cannot or should not be solved in this way) • Inefficiencies of resolution – resolution, as a process, is intractable (O(2n ) for n clauses) • useful heuristics could be applied to reduce the complexity, but there is no way to apply heuristics in Prolog – they would just be additional rules that increases the size of n! • Closed world assumption – in any form of logic reasoning, if something is not known, it is assumed to be false and everything is either true or false • Negation is difficult to represent – since there is no NOT in Prolog, how do we represent NOT? • recall that anything explicitly stated must be true so we cannot specify NOT something as something would then be false • we can represent A != B, but we cannot represent ~dog(X).

Editor's Notes

  • #1: The entire science oft he reasoning is based on the logic. Logic programming essentially consists of rules and facts. Informally we can say that algorithm=logic + control. Logic specifies facts rules indicate what the algorithm does. The control refers to ho the algorithm can be implemented by applying the rules in a particular sequence. Use deduction to answer queries . programmer has to provide rules and facts and then programming language provides the way of applying those rules in a particular sequence so that the algorithm can be implemented .
  • #2: We explore logic programming first, and then we move on to production systemsA production system consists of: 1.A knowledge base, also called a rule base containing production rules, or productions. 2.A database, contains facts simplest kind of statements … they also state relation held between objects e.g.Professor(swati, DESH). Arelation also called as predicate. 3.A rule interpreter, also called a rule application module to control the entire production system. Production rules are the units of knowledge of the form: IF conditions THEN actions  a statement accepted as true as the basis for argument or inference / assumptions /universal truth 
  • #3: We explore logic programming first, and then we move on to production systems
  • #4:  declarative because programs consist of declarations rather than assignments and control flow statements.
  • #5: What is a proposition? A proposition is the basic building block of logic. It is defined as a declarative sentence that is either True or False, but not both. The Truth Value of a proposition is True(denoted as T) if it is a true statement, and False(denoted as F) if it is a false statement. For Example, The sun rises in the East and sets in the West. 2. 1 + 1 = 2 3. 'b' is a vowel.All of the above sentences are propositions, where the first two are Valid(True) and the third one is Invalid(False). Some sentences that do not have a truth value or may have more than one truth value are not propositions. For Example, To represent propositions, propositional variables are used. By Convention, these variables are represented by small alphabets such as . Propositional Logic, or PL, is the most fundamental area of logic. That's why it's sometimes also referred to as zeroth-order logic — everything else (in logic) begins from here. Simply put, it's the area of logic that deals with propositions. Symbolic logic can be used to express propositions, to express the relationships between propositions, and to describe how new propositions can be inferred from other propositions that are assumed to be true. �         The particular form of symbolic logic that is used for logic programming is called predicate calculus.
  • #6: An atomic proposition is a statement or assertion that must be true or false. Examples of atomic propositions are:c++ is easy. “5 is a prime” and “program terminates” and and which cannot be broken down into other simpler sentences. A compound proposition is a proposition that can be divided into simpler, atomic propositions. E.g. C is easy and Python is high-level and Java is challenging. The area of logic which deals with propositions is called propositional calculus or propositional logic. It also includes producing new propositions using existing ones. Propositions constructed using one or more propositions are called compound propositions. The propositions are combined together using Logical Connectives or Logical Operators. First-order logic (FOL) refers to logic in which the predicate of a sentence or statement can only refer to a single subject. It is also known as first-order predicate calculus or first-order functional calculus. ∀x Cat(x) ⇒ Owns(Mary,x) Says that Mary owns all the cats in the universe. Also true if there are no cats in the universe
  • #7: Negation: It means the opposite of the original statement. If p is a statement, then the negation of p is denoted by ~p and read as 'it is not the case that p.' So, if p is true then ~ p is false and vice versa. Example: If statement p is Paris is in France, then ~ p is 'Paris is not in France'. P - ~P  Conjunction: It means Anding of two statements. If p, q are two statements, then "p and q" is a compound statement, denoted by p ∧ q and referred as the conjunction of p and q. The conjunction of p and q is true only when both p and q are true. Otherwise, it is false. AND GATE Disjunction: It means Oring of two statements. If p, q are two statements, then "p or q" is a compound statement, denoted by p ∨ q and referred to as the disjunction of p and q. The disjunction of p and q is true whenever at least one of the two statements is true, and it is false only when both p and q are false.  Implication / if-then (⟶): An implication p⟶q is the proposition "if p, then q." It is false if p is true and q is false. The rest cases are true.  The universal quantifier symbol is denoted by the ∀, which means "for all". Suppose P(x) is used to indicate predicate, and D is used to indicate the domain of x. The universal statement will be in the form "∀x ∈ D, P(x)". The main purpose of a universal statement is to form a proposition.  every x" phrase is known as the universal quantifier.  The phrase "there exists an x such that" is known as the existential quantifier,
  • #8: The above is a formal description for clauses, we will skip this mathematically defined approach and just look at some examples A clause is a group of words that contains both a subject and a predicate. Charlie runs. There's a subject; there's a predicate. It's a clause. A clause can be empty (defined from an empty set of literals). The empty clause is denoted by various symbols such as ∅. in logic, a clause is a finite disjunction of literals. A Mathematical Literal is an atomic formula (�) or a negated atomic formula (¬�).  In simple words resolution is inference mechanism. when you resolve two clauses you get one new clause. Another easy example, we have two sentences (1) All women like shopping. (2) Olivia is a woman. Now we ask query 'Who likes shopping'. So, by resolving above sentences we can have one new sentence Olivia likes shopping. 
  • #9: The rules at the bottom would be read as: if x is y’s father and y is z’s father than x is z’s grandfather if x is y’s father than x is y’s parent if x is y’s mother than x is y’s parent The terms grandfather, mother, father and parent have no intrinsic meaning in Prolog, they are merely symbols that we as humans would interpret. A logic system could just as easily have been written as pie(x, z)  goober(x, y) and piglet(y, z) Also note that we as humans add the interpretation for the relationship between parameters – for instance, a person could just as easily have interpreted the first rule to be that if y is the father of x and z is the father of y, then z is the grandfather of x. However, we usually apply parameters in a left-to-right manner.
  • #10: words resolution is inference mechanism, Resolution as Refutation - means proof by contradiction using resolution. Like for every proof by contradiction, we start with assuming and proving that opposite of the given will be true and then we show that this will lead to the contradiction. Resolution is used, if there are various statements are given, and we need to prove a conclusion of those statements. Unification is a key concept in proofs by resolutions. Resolution is a single inference rule which can efficiently operate on the conjunctive normal form or clausal form. Conjunctive Normal Form: A sentence represented as a conjunction of clauses is said to be conjunctive normal form or CNF. Resolution NOTE: the term resolution is not really used appropriately here. A lot of people say that Prolog does resolution. In a literal definition, resolution means that you introduce the negation of what you are trying to prove and then show the new item leads to a contradiction. For instance, if I want to prove that Sue is Barbara’s parent, or parent(Sue, Barbara) then I would introduce ~parent(Sue, Barbara) and if I could then use this to negate other facts so that I wind up with no facts remaining, this is a contradiction and therefore ~parent(Sue, Barbara) is false, so ~~parent(Sue, Barbara) = parent(Sue, Barbara) is true. Unfortunately, Prolog does not do this. It does something called backward chaining. In backward chaining, we might have rules like this and we want to prove A is true knowing E is true: if A then B if B then C if C then D if D then E We know E is true, so that tells us D must be true, and if D is true then C must be true and if C is true then B must be true and if B is true then A must be true.
  • #14: Rules are supposed to be truth preserving. If they are not, then anything we prove in the Prolog system constructed may not actually be true. This restricts the rules we can use to primarily those that are definitional – for instance based on inheritance. We define a dog to be an animal, so we can say: animal(x) :- dog(x). (if x is a dog then x is an animal) But we cannot say dog(x) :- small(x). because there are dogs that are not small. A lot of the type of problem solving that we do in AI uses rules that are associational in nature, not definitions, so for instance we might want to say: if the item is small and barks, then it is a dog. But this may not always be true, it could be a small child playing around, or a toy dog that makes a barking sound.
  • #15: You can skip this. Its interesting to see how awkward some things are in Prolog, but this merely demonstrates what we already said, Prolog is not a real programming language. You might take a look at the example in section 16.6.6 that demonstrates among other things, using an “assignment statement” to compute distance.
  • #16: What does it mean that ~foo(x) is not available? This is one of Prolog’s drawbacks as a problem solving language. Consider the following simple logic: If you are a parent, you are either a mother or a father A mother is female A father is male If you are not female, you are male (and we can include the reverse) Imagine that you know that someone is a parent and not male, can you prove that the person is a mother? The knowledge above would be written like this: parent(x) :- mother(x). parent(x) :- father(x). female(x) :- mother(x). male(x) :- father(x). female(x) :- NOT male(x). male(x) :- NOT female(x). However, NOT is not available in Prolog so the last two rules would not be writable and therefore we would not be able to prove the simple logic as asked above.