Lecture 21-25
Lecture 21-25
BITS Pilani
Hyderabad
Campus
Pilani Campus
BITS Pilani
Hyderabad Campus
Pilani Campus
Logic Programming
Topics
Introduction
A Brief Introduction to Predicate Calculus
Predicate Calculus and Proving Theorems
An Overview of Logic Programming
The Origins of Prolog
The Basic Elements of Prolog
Deficiencies of Prolog
Applications of Logic Programming
Introduction
Logic programming languages, sometimes called
declarative programming languages
Express programs in a form of symbolic logic
Use a logical inferencing process to produce results
Declarative rather than procedural:
Only specification of results are stated (not detailed procedures for producing
them)
Proposition
A logical statement that may or may not be true
Consists of objects and relationships of objects to each other
Symbolic Logic
Logic which can be used for the basic needs of formal logic:
Express propositions
Express relationships between propositions
Describe how new propositions can be inferred from other propositions
Object Representation
Objects in propositions are represented by simple terms:
either constants or variables
Constant: a symbol that represents an object
Variable: a symbol that can represent different objects at
different times
Different from variables in imperative languages
Compound Terms
Atomic propositions consist of compound terms
Compound term: one element of a mathematical relation,
written like a mathematical function
Mathematical function is a mapping
Examples:
student(jon)
like(Bob, orange)
student(Mary)
Forms of a Proposition
Propositions can be stated in two forms:
Fact: proposition is assumed to be true
Query: truth of proposition is to be determined
Compound proposition:
Have two or more atomic propositions
Propositions are connected by operators
Logical Operators
Name
Symbol Exampl
e
Meaning
negation
not a
conjunction
ab
a and b
disjunction
ab
a or b
equivalence
ab
ab
ab
a is
equivalent to
b
a implies b
b implies a
implication
Quantifiers
Name
Example
Meaning
universal
X.P
existential
X.P
Example:
Clausal Form
Too many ways to state the same thing
Use a standard form for propositions
Clausal form:
B1 B2 Bn A1 A2 Am
means if all the As are true, then at least one B is true
Resolution
Example
AnotherExample:
Resolution
Unification: finding values for variables in propositions that
allows matching process to succeed
Instantiation: assigning temporary values to variables to
allow unification to succeed
After instantiating a variable with a value, if matching fails,
may need to backtrack and instantiate with a different
value
Theorem Proving
Basis for logic programming
When propositions used for resolution, only restricted form
can be used
Horn clause - can have only two forms
Headed: single atomic proposition on left side
Headless: empty left side (used to state facts)
Programming is nonprocedural
Programs do not state how a result is to be computed, but rather the form of the
result
In order to provide the capability to the computer, of how the results will be
computed, logic programming languages provide relevant information (through
predicate calculus) and inference mechanism (through resolution).
University of Edinburgh
Automated theorem proving
Terms
Edinburgh Syntax
Term: a constant, variable, or structure
Constant: an atom or an integer
Atom: symbolic value of Prolog
Atom consists of either:
a string of letters, digits, and underscores beginning with a lowercase letter
a string of printable ASCII characters delimited by apostrophes
Fact Statements
Used for the hypotheses
Headless Horn clauses
female(shelley).
male(bill).
father(bill, jake).
Rule Statements
Used for the hypotheses
Headed Horn clause
Right side: antecedent (if part)
May be single term or conjunction
Example Rules
ancestor(mary,shelley):- mother(mary,shelley).
Goal Statements
For theorem proving, theorem is in form of proposition that
we want system to prove or disprove goal statement
Same format as headless Horn clause
man(fred)
Conjunctive propositions and propositions with variables
are also legal goals, proved using unification procedure.
father(X,mike)
Approaches
Bottom-up resolution, forward chaining
Begin with facts and rules of database and attempt to find
sequence that leads to goal
Works well with a large set of possibly correct answers
Example
Forwardchainingwouldmatchthefirstpropositionwiththerightsideofthe
secondrule(father(X))throughinstantiationofXtobobandthenmatching
theleftsideofthesecondprepositiontogoal.
Backwardchainingwouldmatchthegoalwiththeleftsideofthesecond
preposition(man(X))throughinstantiationofXtobobandthenmatchthe
rightsideofthesecondpreposition(father(bob))withthefirstpreposition.
Subgoal Strategies
When goal has more than one subgoal, can use either
Depth-first search: find a complete proof for the first subgoal before working on
others
Breadth-first search: work on all subgoals in parallel
Backtracking
With a goal with multiple subgoals, if fail to show truth of
one of subgoals, reconsider previous subgoal to find an
alternative solution: backtracking
Begin search where previous search left off
Can take lots of time and space because may find all
possible proofs to every subgoal
Example:
Simple Arithmetic
Prolog supports integer variables and integer arithmetic
is operator: takes an arithmetic expression as right
operand and variable as left operand
A is B / 17 + C
All variables in the expression must be already
instantiated but the left-side variable cannot be
previously instantiated.
Not the same as an assignment statement!
Example
speed(ford,100).
speed(chevy,105).
speed(dodge,95).
speed(volvo,80).
time(ford,20).
time(chevy,21).
time(dodge,24).
time(volvo,24).
distance(X,Y) :- speed(X,Speed),
time(X,Time),
Y is Speed * Time.
Query:
Trace
Built-in structure that displays instantiations at each step
Tracing model of execution - four events:
Example
SymbolsinthetracethatbeginwithUnderscorecharacterareinternal
variablesusedtostoreinstantiatedvalues.
Firstcolumnofthetraceindicatesthesubgoalwhosematchiscurrently
attempted.
Secondcolumnindicatesthecalldepthofthematchingprocess.
Thirdcolumnindicatesthecurrentaction.
BITS Pilani, Hyderabad
Pilani Campus
Campus
Controlflowmodel
BITS Pilani, Hyderabad
Pilani Campus
Campus
List Structures
Other basic data structure (besides atomic propositions we
have already seen): list
List is a sequence of any number of elements
Elements can be atoms, atomic propositions, or other
terms (including other lists)
[apple, prune, grape, kumquat]
[]
(empty list)
[X | Y]
(head X and tail Y)
Exampleoflistcreation:
(contd..)
Dismantling lists:
Creating lists:
If Element_1 is instantiated to pickle and List_2 to [peanut, popcorn],
then the list [pickle, peanut, popcorn] is created in above case.
Equivalent lists:
Append Example
append([], List, List).
append([Head | List_1], List_2, [Head |
List_3]) :append (List_1, List_2, List_3).
Trace:
(Contd..)
Append in Prolog is more flexible, it can be used to find
which 2 lists can be appended to get [a,b,c]:
Reverse Example
reverse([], []).
reverse([Head | Tail], List) :reverse (Tail, Result),
append (Result, [Head], List).
(contd..)
Deficiencies of Prolog
Resolution order control:
Example:
(contd..)
Cut operator is used in generate and test programs- it is
used to avoid revisiting rejected solutions that may be
useless to backtrack and explore.
It is similar to GOTO in imperative languages.
Prolog has this deficiency of the ability to tamper with the
control flow which is not inline with logic programming.
Deficiencies of Prolog
Theclosedworldassumption
Limitation due to database incompleteness: Prolog is a true
/ fail system.
Thenegationproblem
TheProlognotoperatorisnotequivalenttoalogicalNOT
operator.(NOTmeansthatitsoperandisprovablytrue).
Example:
Ifthenotsucceeds,itdoesnotnecessarilymeanthatXis
notequaltoY,ratheritmeansthatresolutioncannotprove
fromthedatabasethatXisthesameasY.
BITS Pilani, Hyderabad
Pilani Campus
Campus
Differentresultwillbeproducedanuninstantiatedvariable.
Reason:FormoftheHornclauseinPrologpreventsnegative
conclusions.
BITS Pilani, Hyderabad
Pilani Campus
Campus
Deficiencies of Prolog
Intrinsiclimitations.
Example,inthesortingproblem,thereisnowayof
knowinghowtosortthantotestallpermutationsofthe
givenlisttofindthesortedlist:Generationofefficient
algorithmsforthispurpose.
Prologprogram:
Summary