LECTURE 21 Logic Programming. LOGIC PROGRAMMING RESOLUTION STRATEGIES.
Logic Programming
description
Transcript of Logic Programming
![Page 1: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/1.jpg)
Logic Programming
Lecture 6: Parsing and Definite Clause Grammars
![Page 2: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/2.jpg)
James Cheney Logic Programming November 5, 2009
Outline for today
•Context Free Grammars (review)
•Parsing in Prolog
•Definite Clause Grammars
![Page 3: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/3.jpg)
James Cheney Logic Programming November 5, 2009
Context Free Grammars
• A simple context free grammar
S ⟶ NP VP
NP ⟶ DET N
VP ⟶ VI | VT NP
DET ⟶ the
N ⟶ cat | dog | food
VI ⟶ meows | barks
VT ⟶ bites | eats
![Page 4: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/4.jpg)
James Cheney Logic Programming November 5, 2009
Recognizing grammatical sentences•Yes:
• "the cat meows"
• "the cat bites the dog"
• "the dog eats the food"
•No:
• "the cat"
• "dog bites meows"
![Page 5: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/5.jpg)
James Cheney Logic Programming November 5, 2009
Generation example
thethe catcat meowsmeows
S ⟶ NP VP
NP ⟶ DET N
VP ⟶ VI | VT NP
DET ⟶ the
N ⟶ cat | dog | food
VI ⟶ meows | barks
VT ⟶ bites | eats
DETDET NN
NPNP
SS
VPVP
VIVI
![Page 6: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/6.jpg)
James Cheney Logic Programming November 5, 2009
An even simpler context-free
grammarS c S a S b⟶ ⟶
•In Prolog:
s([c]).
s(S) :- s(S1),
append([a],S1,S2),
append(S2,[b],S).
![Page 7: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/7.jpg)
James Cheney Logic Programming November 5, 2009
Using append to parse
s(L) :- np(L1), vp(L2), append(L1,L2,L).
np(L) :- det(L1), n(L2), append(L1,L2,L).
vp(L) :- vi(L) ;
vt(L1), np(L2), append(L1,L2,L).
det([the]). det([a]).
n([cat]). n([dog]). n([food]).
vi([meows]). vi([barks]).
vt([bites]). vt([eats]).
![Page 8: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/8.jpg)
James Cheney Logic Programming November 5, 2009
A better way?
•Obviously, need to guess when we're generating
•But we also guess when we're parsing known sequence
•This is inefficient (lots of backtracking!)
•Reordering goals doesn't help much.
![Page 9: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/9.jpg)
James Cheney Logic Programming November 5, 2009
An idea:Accumulators
•Want to use input data to guide search
•Parse using two parameters L,M
• such that M is a suffix of L
•L = list "before" parsing item
•M = "remainder" of list after parsing item
![Page 10: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/10.jpg)
James Cheney Logic Programming November 5, 2009
An even simpler context-free
grammar (mark II)S c S a S b⟶ ⟶
•In Prolog, using accumulators:
s([c|L],L).
s([a|L1],M) :- s(L1,[b|M]).
![Page 11: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/11.jpg)
James Cheney Logic Programming November 5, 2009
Using accumulator version
?- s(L,[]).
L = [c].
L = [a,c,b].
L = [a,a,c,b,b].
...
![Page 12: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/12.jpg)
James Cheney Logic Programming November 5, 2009
Difference lists• A difference list is a pair (t,X) where
• t is a list term [t1,...,tn|X]
• X is a variable
• Empty difference list: (X,X)
• Constant difference list: ([a1,...,an|X],X)
• Appending difference lists (t,X) and (u,Y):
• Simply unify X and u! Yields (t[u/X],Y)
![Page 13: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/13.jpg)
James Cheney Logic Programming November 5, 2009
An even simpler context-free
grammar (mark III)S c S a S b⟶ ⟶
•In Prolog, using difference lists:
s(L,M) :- L = [c|M].
s(L,M) :- L = [a|L1],
s(L1,M1),
M1 = [b|M].
![Page 14: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/14.jpg)
James Cheney Logic Programming November 5, 2009
Definite clause grammars
• Parsing using difference lists is so useful that Prolog has built-in syntax for it:
s --> [c].
s --> [a], S, [b].
• translates to:
s(L,M) :- L = [c|M].
s(L,M) :- L = [a|L1],
s(L1,M1),
M1 = [b|M].
![Page 15: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/15.jpg)
James Cheney Logic Programming November 5, 2009
DCG syntax
•Rules of form nonterm --> body
•Body terms are:
• terminal lists [t1,...,tn] (may be [])
• nonterminals s,t,u,....
• sequential composition body1, body2
• alternative choice body1 ; body2
![Page 16: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/16.jpg)
James Cheney Logic Programming November 5, 2009
Using DCG version• DCGs translated to difference list
implementation
• So they are used the same way:
?- s(L,[]).
L = [c].
L = [a,c,b].
L = [a,a,c,b,b].
...
![Page 17: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/17.jpg)
James Cheney Logic Programming November 5, 2009
Using DCG version (II)
• Can also use built-in phrase/[2,3]
?- phrase(s,L,M).
L = [c|M].
L = [a,c,b|M].
?- phrase(s,L).
L = [c].
L = [a,c,b].
![Page 18: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/18.jpg)
James Cheney Logic Programming November 5, 2009
Large example revisited
s --> np, vp.
np --> det, n.
vp --> vi ; vt, np.
det --> [the] ; [a].
n --> [cat] ; [dog] ; [food].
vi --> [meows] ; [barks].
vt --> [bites] ; [eats].
![Page 19: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/19.jpg)
James Cheney Logic Programming November 5, 2009
DCGs and recursion
•Left recursion (as usual) leads to nontermination:
exp --> exp,[+],exp.
•Avoid by using right recursion:
exp --> simple_exp,[+],exp.
![Page 20: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/20.jpg)
James Cheney Logic Programming November 5, 2009
DCGs with parameters
• Nonterminals in DCGs can have parameters.
s(0) --> c.
s(succ(N)) --> [a], s(N), [b].
• Keeps track of depth of nesting.
![Page 21: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/21.jpg)
James Cheney Logic Programming November 5, 2009
DCGs + parameters ≥ CFGs
•With parameters we can parse non-CFGs:
s(N) --> n(N,a),
n(N,b),
n(N,c).
n(0,X) --> [].
n(succ(N),X) --> [X], n(N,X).
![Page 22: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/22.jpg)
James Cheney Logic Programming November 5, 2009
Parsing with parse trees
•"the cat meows"
• S (NP (DET (the), N(cat)), VP (VI (meows))
•"the cat bites the dog"
• S(NP (DET (the), N(cat), VP (VT (bites), NP(DET(the), N(dog)))
•Can build parse trees using parameters
• Look for this in a tutorial exercise...
![Page 23: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/23.jpg)
James Cheney Logic Programming November 5, 2009
DCGs with tests•DCG clause bodies can also contain:
• tests {pred}
• where pred is an arbitrary Prolog goal
•Example:
n --> [Word], {noun(Word)}.
noun(dog). noun(cat). noun(food).
![Page 24: Logic Programming](https://reader036.fdocuments.in/reader036/viewer/2022070417/56815302550346895dc1227f/html5/thumbnails/24.jpg)
James Cheney Logic Programming November 5, 2009
Next time
•Further reading:
• LPN, ch. 7-8
•Next time:
•Advanced search techniques
•depth-first, iterative deepening, breadth-first, best-first