Professional Documents
Culture Documents
and
Logic Programming
Propositions &Sentences
• A proposition is a declarative sentence.
• e.g. Woff is a dog.
• e.g. Jim is the husband of Mary.
• A proposition is represented by a logical symbol.
• P: Woff is a dog.
• R: Jim is the husband of Mary.
• Sentences: True and False are both logical constants.
• Logical constants are sentences.
• e.g. True is a propositional logic sentence.
• Logical symbols are sentences.
• e.g. P is a propositional logic sentence.
Formal Grammar
• Sentence ::= AtomicSentence |
ComplexSentence
• AtomicSentence ::= True | False | P | Q |
…
• ComplexSentence ::= (Sentence) |
Sentence Connective Sentence | ØSentence
• Connective ::= Ù | Ú | Þ | Û
Horn Clauses
• A Horn Clause is a sentence in the
following form
• P1 Ù P2 Ù … Ù Pn Þ Q
An Example of a rule is
overlap(X, Y) :- member(M, X), member(M, Y)
Here lists X and Y overlap if there is some member M
that is a member of both X and Y.
In order to deduce this we need Prolog to answer
queries.
How Prolog came into Existence ?
• The concept of logic programming is linked historically to
Prolog (Programming Logic).
• Prolog was first applied to natural language Processing.
Later it has been used for specifying algorithms, searching
databases, writing compilers. It can do all applications for
which Lisp might be used.
• Prolog is effective for applications involving pattern
matching, backtrack searching or incomplete information.
• In short, labor in Logic Programming is denoted by
link(bcpl, c)
Introduction to Prolog
• Knowledge Base
• dog(X) :- barks(X),
wags_tail(X)barks(woff)barks(spot)wags_tail(
woff)
• Queries
• ?- dog(woff) => yes
• ?- dog(spot) => no
• Means no more matches found.
Using Variables
• Knowledge Base
• dog(X) :- barks(X),
wags_tail(X)barks(woff)barks(spot)wags_ta
il(woff)
• Query
• ?- dog(Y) => Y = woff
Prolog’s Search Algorithm
• Prolog uses a goal directed search of the KB
• Depth first search is used
• Query clauses are used as goals and
searched left to right
• KB clauses are searched in the order they
occur in the KB
• Goals are matched to the head of clauses
• Terms must unify based upon variable
substitution before they match
Unification
head conditions
A fact is a special case of a rule. A fact has no head and
conditions.
path(L, L) ---> fact . We take a path of zero links to be from L
to itself.
Path(L, M) :- link(L, X), path(X, M)
A path from L to M begins with a link to some X and
continues along the path from X to M.
Unification : Deduction in Prolog is based on concept of
unification.
?- f(X, b) = f(a, Y)
Here, f(a, b) is an instance of f(X, b) because it is obtained by
substituting subterm ‘a’ for variable X in f(X, b). Similarly
f(a, b) is an instance of f(a, Y) because it is obtained by
replacing subterm ‘b’ for variable Y in f(a, Y)
X=a Y=b
For Example, g(a,a) is an instance of g(x,x) .
However, g(a,b) is not an instance of g(x,x) because we cannot
substitute ‘a’ for one occurrence of x and ‘b’ for another
occurrence of x.
Arithmetic
• X = 2+3 ---> simply binds variable X to term 2+3
• X is 2+3 ---> The Infix operator evaluates expression and
binds 5 to X.
?- X is 2+3 , X = 5
yes
?- X is 2+3 , X = 2+3
no
fails because 2+3 does not unify with 5. Term 2+3 is the
application of operator + to the arguments 2 and 3, whereas 5
is simply the integer 5. Hence, 2+3 does not unify with 5.
Data Structures In Prolog
Prolog supports several notations for writing Lisp-like lists.
They sweeten the syntax without adding any new capabilities.
Lists in Prolog :
• The simplest way to write a list is to enumerate its elements
• The list containing three atoms a, b and c is written as [a,b,c]
• Empty List is written as [ ]
• Unification can be used to extract the components of a list.
for example :
?- [H | T] = [a,b,c] ---> H = a , T = [b, c] .
• In Prolog any tree can be written as term, any term can be
written as tree.
Example : Term node (K, S, T) can be represented as tree.
K
S T
nonleaf(leaf, leaf)
nonleaf(nonleaf(leaf,leaf),nonleaf(leaf,leaf)
example:
?- X = [1,2,3] , member(a,X).
X is a list of [1,2,3]. Is ‘a’ a member of X ?
?- member(a,X) , X = [1,2,3].
find all solution where a is member of X. ‘a’ can
be anywhere in X, so finds infinite possibilities for
a, but doesn’t find match in X.
Variable as place holders.
open list - list with variable at end
closed list - no variable at end
Example:
[ a, b | X ] ==> open list
[ a , b ] ==> closed list
Prolog uses machine generated
variables to represent variable at the
end of list. Uses underscore + integer.
example:
?- L = [ a , b | X ]
L = [ a , b | _1]
X = _1
X = _1
L = [ a , b | _1 ]
X = _1 = [ c , Y ] = [ c | _2 ]
L = [ a , b | _1] = [ a , b | [ c | 2 ] ]
= [ a , b , c | _2]
in queue q( L,E )
L = open list
E = end marker ( some suffix of L )
content = elements of L, but not in E
Queue Manipulation Cont.
Rules:
setup( q( X , X ) ).
enter( A , q( X , Y ) , q( X , Z ) :- Y = [ A | Z ].
leave( A , q( X , Z ) , q( X , Y ) :- Y = [ A | Y ].
wrapup( q( [ ] , [ ] ) ).
Example:
?- setup( Q )
Q = q( X , X )
Q = q( _1 , _1 )
enter( A , q ( X,Y) , q(X,Z) ) :- Y = [A | Z].
In plain English:
?- enter( a , Q , R )
rule: enter( a , q( X, Y) , q( X, Z ) ) :- Y = [ a | Z ]
enter( a , q( _1 , _1) , q( X,Z ) )
X = _1 , Y = _1
Y binds to [ a | Z ] , Z = new marker _2
X = _1 = Y = [ a | _ 2 ]
R = q ( X , Z ) --> q ( [ a | _2 ] , _2 )
Now R = q( [ a | _2 ] , _2 )
?- enter( b , R , S )
rule: enter( b , q ( X ,Y ) , q ( X , Z ) ) :- Y = [ b | Z ]
enter( b , q ( [ a | _2 ] , _2 ) , q ( X , Z ) )
X = [ a | _2 ] , Y = _2
Y = [ b | Z ] , where Z = _3
Y = _2 = [ b | _3 ]
X = [ a | _2 ] = [ a | [ b | _3] ] = [ a , b | _3 ]
S=q(X,Z)
S = q( [ a,b | _3 ] , _3 )
Leave( A , q( X , Z ) , q( Y , Z ) ) :- X = [ A | Y ].
In plain English:
Example:
S = q( [ a , b | _3] , _3 )
?- leave( X , S , T )
leave( elemX , q( X,Z) , q(Y,Z) ) :- X = [elemX | Y ]
leave( elemX , q( [a,b | _3] , _3 ) , q( Y,Z ))
X = [a,b | _3 ] , Z = _3
X = [ elemX | Y ] = [ a,b | _3]
elemX = a ,Y = [ b | _3 ]
T = q( Y,Z ) = q( [b |_3] , _3 )
More examples
T = q( [ b | _3 ] , _3 )
?- leave( Y , T , U )
leave( elemY , q( X,Z ) , q( Y,Z) ) :- X = [ elemY | Y]
leave( elemY , q( [ b | _3 ] , _3) , q( Y,Z ) )
X = [ b | _3 ] = [ elemY | Y ]
elemY = b
Y = _3
U = q( _3 , _3 )
wrapup( q( [ ] , [ ] ) )
?- wrapup( U )
assigns empty list to U
U was q( _3 , _3 )
wrapup( U )
U = q( [ ] , [ ] )
_3 = [ ]
Difference List
example:
[ a, b ] can be written as
- dl( [ a,b] , [ ] )
- dl( [ a,b,c] , [c] )
- dl( [ a,b | E ] , E ),…,etc
Control in Prolog
algorithm = logic + control
What is Ys ?
Substitute [ a,b,c] for Y
answer: Ys = [ a,b,c]
What is Zs ?
Zs = Z , because no substitution
general unifier
Example:
T1 = append( [ ] , Y ,Y)
T2 = append( [ ] , [a | V] , [a,b,c] )
3. Substitute
found rule:
suffix( Y,Z ) :- append( X,Y,Z )
find substitution:
suffix( [a] , L ) :- append( X,Y,Z)
s = { Y -> [a] , Z -> L }, X = variable _1
found rule:
append( [ ] , Y , Y )
find substitution:
append( _1 , [a] , L ) = append( [ ] ,Y,Y)
_1 = [ ]
Y = [ a ] , Y = L ,so L = [a]
Done with suffix() part, so now have only
prefix( L , [ a,b,c] ),
but L = [a] from previous, so we have
rule: append( [ ] , Y , Y )
so , Y = E and Y = [ a , b | E ]
E = Y = [ a , b | E ] = [ a , b | [ a , b | E ] ] = ...
rule:
not( X ) :- X , ! , fail
not( _ ).
Example:
?- X = 2 , not ( X=1 )
- do X = 2 , so X unifies with 2.
- left with not ( 2 = 1 )
- use rule to get: 2 = 1 , ! , fail
- 2 = 1 fails, not reaching the cut !
- so tries next rule not( _ ) and succeeds
?- not( X = 1 ) , X = 2
?- not( X=1 )
?- not( X=1 ) , X = 1
Applying a Rule to a Goal
A :- B1, B2, …, Bn
• A rule applies to a
subgoal G if its head A unifies with G
• Variables in the rule are renamed before
unification to keep them distinct from
variables in the subgoal.
61
A computation that succeeds without backtracking
GOAL
Suffix([a],L),prefix(L,[a,b,c]).
suffix([a],L) if append(_1,[a],L).
Append(_1,[a],L),prefix(L,[a,b,c]).
{_1[],L[a]} append([],[a],[a]).
Prefix([a],[a,b,c]).
prefix([a],[a,b,c]) if append([a],_2,[a,b,c])
append([a],_2,[a,b,c]).
prefix([a],[a,b,c]) if append([],_2,[b,c])
Append([],_2,[b,c]).
{_2[b,c]} append([],[b,c],[b,c])
yes
62
Prolog Search Trees
63
Goal Order Changes Solutions
64
Cuts
• A cut prunes or “cuts out” and unexplored
part of a Prolog search tree.
• Cuts can therefore be used to make a
computation more efficient by eliminating
futile searching and backtracking.
• Cuts can also be used to implement a form
of negation
65
Cuts
• A cut, written as !, appears as a condition
within a rule. When rule
B :- C1,…, Cj-1, !,Cj+1,…,Ck
66
A cut as the First Condition
• Consider rules of the form
B :- !, C.
67
Example
b,G
b :- c.
b :- d. X
b :- e.
c,G !,d,G
d,G e,G
b :- c.
b :- !,d.
b :- e.
d,G
68
Example
a(X) a(X)
• ?-a(X).
b e b e
c d Yes !c d Yes
Yes
backtrack X=2 c X=2
X=1
backtrack
a(1) :- b; a(1) :- b;
a(2) :- e; a(2) :- e;
b :- c. b :- !,c.
b :- d. b :- d.
69
The Effect of Cut
• As mentioned earlier, when a rule
B :- C1,…, Cj-1, !,Cj+1,…,Ck
71
is to eliminate all but the first guess.
a(X) :- b(X). a(X) :- b(X).
a(X) :- f(X). a(X) :- f(X).
b(X) :- g(X),v(X). b(X) :- g(X),!,v(X).
b(X) :- X = 4, v(X). b(X) :- X = 4, v(X).
g(1). g(1).
g(2). g(2).
g(3). g(3).
v(X). v(X).
f(5) f(5)
(a) (b)
72
a(Z) a(Z)
v(1)
(a) (b) 73
Negation as Failure
• The not operator in Prolog is implemented
by the rules
not(X) :- X, !, fail.
not(_).
74