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

Lecture 21-25

This document provides an introduction to logic programming and the logic programming language Prolog. It discusses key concepts in logic programming such as predicate calculus, terms, propositions, clauses, resolution, and backtracking. Prolog uses predicate calculus and resolution to perform logical inference to prove or disprove queries, using a backward chaining search strategy. Key elements of Prolog include facts, rules, and goal statements expressed as clauses to represent hypotheses and theorems to be proved or disproved through logical inference.

Uploaded by

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

Lecture 21-25

This document provides an introduction to logic programming and the logic programming language Prolog. It discusses key concepts in logic programming such as predicate calculus, terms, propositions, clauses, resolution, and backtracking. Prolog uses predicate calculus and resolution to perform logical inference to prove or disprove queries, using a backward chaining search strategy. Key elements of Prolog include facts, rules, and goal statements expressed as clauses to represent hypotheses and theorems to be proved or disproved through logical inference.

Uploaded by

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

BITS Pilani

BITS Pilani
Hyderabad
Campus
Pilani Campus

Dr. Lavika Goel


Assistant Professor
Department of CSIS

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

BITS Pilani, Hyderabad


Pilani Campus
Campus

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)

BITS Pilani, Hyderabad


Pilani Campus
Campus

Proposition
A logical statement that may or may not be true
Consists of objects and relationships of objects to each other

BITS Pilani, Hyderabad


Pilani Campus
Campus

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

Particular form of symbolic logic used for logic programming


called predicate calculus

BITS Pilani, Hyderabad


Pilani Campus
Campus

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

BITS Pilani, Hyderabad


Pilani Campus
Campus

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

BITS Pilani, Hyderabad


Pilani Campus
Campus

Parts of a Compound Term


Compound term composed of two parts
Functor: function symbol that names the relationship
Ordered list of parameters (tuple)

Examples:
student(jon)
like(Bob, orange)
student(Mary)

BITS Pilani, Hyderabad


Pilani Campus
Campus

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

BITS Pilani, Hyderabad


Pilani Campus
Campus

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

BITS Pilani, Hyderabad


Pilani Campus
Campus

Quantifiers
Name

Example

Meaning

universal

X.P

For all X, P is true

existential

X.P

There exists a value of X


such that P is true

Example:

BITS Pilani, Hyderabad


Pilani Campus
Campus

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

Antecedent: right side


Consequent: left side
Example:

BITS Pilani, Hyderabad


Pilani Campus
Campus

Predicate Calculus and Proving


Theorems
A use of propositions is to discover new theorems that can
be inferred from known axioms and theorems
Resolution: an inference principle that allows inferred
propositions to be computed from given propositions,
propositions should be in clausal form.

BITS Pilani, Hyderabad


Pilani Campus
Campus

Resolution

BITS Pilani, Hyderabad


Pilani Campus
Campus

Example

AnotherExample:

BITS Pilani, Hyderabad


Pilani Campus
Campus

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

BITS Pilani, Hyderabad


Pilani Campus
Campus

Proof by Contradiction: Refutation


Hypotheses: a set of pertinent propositions
Goal: negation of theorem stated as a proposition
Theorem is proved by finding an inconsistency

BITS Pilani, Hyderabad


Pilani Campus
Campus

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)

Most propositions can be stated as Horn clauses

BITS Pilani, Hyderabad


Pilani Campus
Campus

Overview of Logic Programming


Declarative semantics
There is a simple way to determine the meaning of each statement (propositions
in symbolic logic)
Simpler than the semantics of imperative languages

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).

BITS Pilani, Hyderabad


Pilani Campus
Campus

Example: Sorting a List


Describe the characteristics of a sorted list, not the
process of rearranging a list
sort(old_list, new_list) permute (old_list, new_list)
sorted (new_list)
sorted (list) j such that 1 j < n, list(j) list (j+1)

BITS Pilani, Hyderabad


Pilani Campus
Campus

The Origins of Prolog


University of Aix-Marseille
Natural language processing

University of Edinburgh
Automated theorem proving

BITS Pilani, Hyderabad


Pilani Campus
Campus

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

BITS Pilani, Hyderabad


Pilani Campus
Campus

Terms: Variables and Structures


Variable: any string of letters, digits, and underscores
beginning with an uppercase letter
Instantiation: binding of a variable to a value
Lasts only as long as it takes to satisfy one complete goal

Structure: represents atomic proposition


functor(parameter list)

BITS Pilani, Hyderabad


Pilani Campus
Campus

Fact Statements
Used for the hypotheses
Headless Horn clauses
female(shelley).
male(bill).
father(bill, jake).

BITS Pilani, Hyderabad


Pilani Campus
Campus

Rule Statements
Used for the hypotheses
Headed Horn clause
Right side: antecedent (if part)
May be single term or conjunction

Left side: consequent (then part)


Must be single term

Conjunction: multiple terms separated by logical AND


operations (implied), expressed using commas

BITS Pilani, Hyderabad


Pilani Campus
Campus

Example Rules
ancestor(mary,shelley):- mother(mary,shelley).

Can use variables (universal objects) to generalize


meaning:
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).
The = operator succeeds if its both operands are
the same. Example, X=Y.
The not operator succeeds if its operand fails.
Example, not(X=Y).

BITS Pilani, Hyderabad


Pilani Campus
Campus

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)

BITS Pilani, Hyderabad


Pilani Campus
Campus

Inferencing Process of Prolog


Queries are called goals
If a goal is a compound proposition, each of the atomic
propositions is a subgoal
To prove a goal is true, must find a chain of inference
rules and/or facts. For goal Q, the inferencing procedure
must find either Q or a sequence of propositions P1,
P2,..Pn such that:

Process of proving a subgoal called matching, satisfying,


or resolution
BITS Pilani, Hyderabad
Pilani Campus
Campus

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

Top-down resolution, backward chaining


Begin with goal and attempt to find sequence that leads to set of
facts in database
Works well with a small set of possibly correct answers

Prolog implementations use backward chaining

BITS Pilani, Hyderabad


Pilani Campus
Campus

Example

Forwardchainingwouldmatchthefirstpropositionwiththerightsideofthe
secondrule(father(X))throughinstantiationofXtobobandthenmatching
theleftsideofthesecondprepositiontogoal.
Backwardchainingwouldmatchthegoalwiththeleftsideofthesecond
preposition(man(X))throughinstantiationofXtobobandthenmatchthe
rightsideofthesecondpreposition(father(bob))withthefirstpreposition.

BITS Pilani, Hyderabad


Pilani Campus
Campus

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

Prolog uses depth-first search


Can be done with fewer computer resources

BITS Pilani, Hyderabad


Pilani Campus
Campus

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:

BITS Pilani, Hyderabad


Pilani Campus
Campus

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!

BITS Pilani, Hyderabad


Pilani Campus
Campus

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:

BITS Pilani, Hyderabad


Pilani Campus
Campus

Trace
Built-in structure that displays instantiations at each step
Tracing model of execution - four events:

Call (beginning of attempt to satisfy goal)


Exit (when a goal has been satisfied)
Redo (when backtrack occurs)
Fail (when goal fails)

BITS Pilani, Hyderabad


Pilani Campus
Campus

Example

SymbolsinthetracethatbeginwithUnderscorecharacterareinternal
variablesusedtostoreinstantiatedvalues.
Firstcolumnofthetraceindicatesthesubgoalwhosematchiscurrently
attempted.
Secondcolumnindicatesthecalldepthofthematchingprocess.
Thirdcolumnindicatesthecurrentaction.
BITS Pilani, Hyderabad
Pilani Campus
Campus

Example (with backtracking)


likes(jake,chocolate).
likes(jake,apricots).
likes(darcie,licorice).
likes(darcie,apricots).

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:

BITS Pilani, Hyderabad


Pilani Campus
Campus

(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:

BITS Pilani, Hyderabad


Pilani Campus
Campus

Append Example
append([], List, List).
append([Head | List_1], List_2, [Head |
List_3]) :append (List_1, List_2, List_3).
Trace:

BITS Pilani, Hyderabad


Pilani Campus
Campus

(Contd..)
Append in Prolog is more flexible, it can be used to find
which 2 lists can be appended to get [a,b,c]:

BITS Pilani, Hyderabad


Pilani Campus
Campus

Reverse Example
reverse([], []).
reverse([Head | Tail], List) :reverse (Tail, Result),
append (Result, [Head], List).

BITS Pilani, Hyderabad


Pilani Campus
Campus

(contd..)

BITS Pilani, Hyderabad


Pilani Campus
Campus

Deficiencies of Prolog
Resolution order control:

Allows the user to control the sub-goal ordering during


resolution thus creating efficient programs.
Allows control of backtracking using the cut operator (!). Subgoals to the left of the cut operator in a compound goal cannot
be re-satisfied through backtracking.
Use is to make programs efficient by not attempting to re-satisfy
sub-goals that presumably could not result in a complete proof.

Example:

BITS Pilani, Hyderabad


Pilani Campus
Campus

(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.

BITS Pilani, Hyderabad


Pilani Campus
Campus

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

Negation problem: Example

Differentresultwillbeproducedanuninstantiatedvariable.
Reason:FormoftheHornclauseinPrologpreventsnegative
conclusions.
BITS Pilani, Hyderabad
Pilani Campus
Campus

Deficiencies of Prolog
Intrinsiclimitations.
Example,inthesortingproblem,thereisnowayof
knowinghowtosortthantotestallpermutationsofthe
givenlisttofindthesortedlist:Generationofefficient
algorithmsforthispurpose.

Prologprogram:

BITS Pilani, Hyderabad


Pilani Campus
Campus

Applications of Logic Programming


Relational database management systems
Relational calculus is a form of symbolic logic.
Query languages (SQL) based on relational calculus are nonprocedural.
Simple tables of information can be described by Prolog structures, and
relationships between tables can be described by Prolog rules.
The retrieval process is inherent in the resolution operation.
Prolog can replace the DDL, DML, and Query Language which are usually
embedded in an imperative language.
Deductive capability is an advantage but logical inferences take longer than
ordinary table look-up methods.

BITS Pilani, Hyderabad


Pilani Campus
Campus

Applications of Logic Programming


Expert systems
Expert systems consists of a database of facts, an inferencing
process, some heuristics about the domain, and some friendly
human interface.
Logic programming can help deal with the problem of
incompleteness of the database using its default inference rules.
Idea is to use resolution as the basis of query processing- Prologs
ability to add facts and rules to provide the learning capability, and
to use its trace facility to inform the user of the "reasoning" behind
a given result.
Prolog is missing the ability to query the user for additional
information when it is needed. e.g., Expert System Construction
System APES.

BITS Pilani, Hyderabad


Pilani Campus
Campus

Applications of Logic Programming


Natural language processing
e.g., Intelligent databases, Intelligent knowledge-based systems.
Forms of logic programming have been found to be equivalent to
context-free grammars for describing language syntax.
Proof procedures have been found to be equivalent to certain parsing
strategies (e.g., use of backward chaining resolution to parse
sentences whose structures are described by context-free grammars).
Some kinds of semantics of natural languages can be made clear by
modeling the languages with logic programming. (e.g., sets of
sentences can be expressed in clausal form).

BITS Pilani, Hyderabad


Pilani Campus
Campus

Summary

Symbolic logic provides basis for logic programming


Logic programs should be nonprocedural
Prolog statements are facts, rules, or goals
Resolution is the primary activity of a Prolog interpreter
Although there are a number of drawbacks with the
current state of logic programming it has been used in a
number of areas

BITS Pilani, Hyderabad


Pilani Campus
Campus

You might also like