You are on page 1of 25

Logic Programming

Logic programming is characterized by programming with relations and inference.


A logic program consists of a set of axioms and a goal statement. The rules of inference are
applied to determine whether the axioms are sufficient to ensure the truth of the goal
statement. The execution of a logic program corresponds to the construction of a proof of the
goal statement from the axioms.
In the logic programming model, the programmer is responsible for specifying the basic logical
relationships and does not specify the way the inference rules are applied. Thus
Logic + Control = Algorithms
Logic programming is based on tuples. Predicates are abstractions and generalization of
the data type of tuples. Recall, a tuple is an element of
S0 × S1 × ... × Sn
The squaring function for natural numbers may be written as a set of tuples as follows:
{(0,0), (1,1), (2,4) ...}
Such a set of tuples is called a relation and in this case the tuples define the squaring relation.
sqr = {(0,0), (1,1), (2,4) ...}
Abstracting to the name sqr and generalizing an individual tuple we can define the squaring relation
as:
sqr = (x,x2)
Parameterizing the name gives:
sqr(X,Y) <-- Y is } X*X
In the logic programming language Prolog this would be written as:
sqr(X,Y) <-- Y is } X*X.
In the logic programming language Prolog this would be written as:
sqr(X,Y) <-- Y is } X*X.
Note: that the set of tuples is named sqr and that the parameters are X and Y. Prolog does not
evaluate its arguments unless required, so the expression Y is X*X forces the evaluation of
X*X and unifies the answer with Y. The Prolog code
P <-- Q.
may be read in several ways; it could be read P where Q or P if Q. In this latter form it is a variant of
the first-order predicate calculus known as Horn clause logic. A complete reading of the sqr predicate
the point of view of logic is for every X and Y, Y is the sqr of X if Y is X*X.
Logic programming has many application areas:
● Relational Data Bases
● Natural Language Interfaces
● Expert Systems
● Symbolic Equation solving
● Planning
● Prototyping
● Simulation
● Programming Language Implementation
Syntax
There are just four constructs: constants, variables, function symbols, predicate symbols,
and two logical connectives, the comma (and) and the implication symbol.
Horn Clause Logic
P ∈ Programs
C ∈ Clauses
Q ∈ Queries
A ∈ Atoms
T ∈ Terms
X ∈ Variables
P ::= C... Q...
C ::= G [ ← G1 [ ∧ G2]...] .
G ::= A [ ( T [,T]... ) ]
T ::= X | A [ ( T [,T]... ) ]
Q ::= G [,G]... ?
CLAUSE, FACT, RULE, QUERY, FUNCTOR, ARITY, ORDER, UNIVERSAL QUANTIFICATION, EXISTENTIAL
QUANTIFICATION, RELATIONS
In logic, relations are named by predicate symbols chosen from a prescribed vocabulary.
Knowledge about the relations is then expressed by sentences constructed from predicates,
connectives, and formulas. An n-ary predicate is constructed from prefixing an n-tuple with an
n-ary predicate symbol.
Semantics
• The operational semantics of logic programs correspond to logical inference.
• The declarative semantics of logic programs are derived from the term model commonly
referred to as the Her brand base.
• The denotational semantics of logic programs are defined in terms of a function which assigns
meaning to the program.
There is a close relation between the axiomatic semantics of imperative programs and logic programs. A
logic program to sum the elements of a list could be written as follows.
sum([Nth],Nth).
sum([Ith|Rest],Ith + Sum Rest) ← sum(Rest,Sum Rest).
Operational Semantics
The operational semantics of a logic program can be described in terms of logical inference using
unification and the inference rule resolution. The following logic program illustrates logical inference.
a.
b ← a.
b?
We can conclude b by modus ponens given that b ← a and a. Alternatively, if b is assumed to be false
then from b ← a and modus tollens we infer ¬ a but since a is given we have a contradiction and b
must hold. The following program illustrates unification.
parent of(a,b).
parent of(b,c).
ancestor of(Anc,Desc) ← parent of(Anc,Desc).
ancestor of(Anc,Desc) ← parent of(Anc,Interm) ∧
ancestor of(Interm,Desc).
parent of(a,b)?
ancestor of(a,b)?
ancestor of(a,c)?
ancestor of(X,Y)?
Denotational Semantics
• Denotational semantics assigns meanings to programs based on associating with the
program a function over the domain computed by the program.
• The meaning of the program is defined as the least fixed point 0f the function, if it exists.
Logic Programming (Horn Clause Logic) – Operational Semantics
Abstract Syntax:
P ∈ Programs
C ∈ Clauses
Q ∈ Queries
T ∈ Terms
A ∈ Atoms
X ∈ Variables
P ::= (C | Q)...
C ::= G [ ← G1 [ ∧ G2]...] .
G ::= A [ ( T [,T]... ) ]
T ::= X | A [ ( T [,T]... ) ]
Q ::= G [,G]... ?
Semantic Domains:
β ∈ B = Bindings
∈ E = Environment
Semantic Functions:
R ∈ Q → B → B + (B × { yes ) + { no }
U∈C×C→B→B
Semantic Equations:
R[[?]]β, = (β, yes)
R[[G]]β, = β
0
were
G0 ∈ , U[[G, G0
]]β = β
0
R[[G]]β, = R[[B]]β
0
were
(G0 ← B) ∈ , U[[G, G0
]]β = β
0
R[[G1, G2]]β, = R[[B, G2]](R[[G1]]β, ),
R[[ G ]]β, = no
where no other rule applies
The Logical Variable
The logical variable, terms and lists are the basic data structures in logic programming.
Here is a definition of the relation between the prefix and suffixes of a list. The relation is named concat
because it may be viewed as defining the result of appending two lists to get the third list.
concat([ ],[ ])
concat([H|T],L,[H|TL]) ← concat(T,L,TL)
Arithmetic
Terms are simply patterns they may not have a value in and of themselves.
For example, here is a definition of the relation between two numbers and their product.
times(X,Y,X×Y)
Prolog not equal Logic Programming
Prolog is not logic programming. The execution model for Prolog omits the occurs check, searches the
rules sequentially, and selects goals sequentially. Backtracking, infinite search trees .
As in functional programming, lists are an important data structure logic programming. The empty list is
represented by [ ], a list of n elements by [X1, ..., Xn] and the first i elements of a list and the rest of the
list by [X1, ..., Xi |R]. In addition, data structures of arbitrary complexity may be constructed from the
terms.
THANK YOU

You might also like