Logic Programming: Parsing. Difference Lists, DCGs Alan Smaill Oct - - PowerPoint PPT Presentation

logic programming parsing difference lists dcgs
SMART_READER_LITE
LIVE PREVIEW

Logic Programming: Parsing. Difference Lists, DCGs Alan Smaill Oct - - PowerPoint PPT Presentation

N I V E U R S E I H T T Y O H F G R E U D I B N Logic Programming: Parsing. Difference Lists, DCGs Alan Smaill Oct 15 2015 Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 1/26 Today N I V


slide-1
SLIDE 1

T H E U N I V E R S I T Y O F E D I N B U R G H

Logic Programming:

  • Parsing. Difference Lists, DCGs

Alan Smaill Oct 15 2015

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 1/26

slide-2
SLIDE 2

T H E U N I V E R S I T Y O F E D I N B U R G H

Today

Context Free Grammars (review) Parsing in Prolog Definite Clause Grammars (DCGs)

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 2/26

slide-3
SLIDE 3

T H E U N I V E R S I T Y O F E D I N B U R G H

Context Free Grammars

A simple CFG: S

  • > NP VP

NP

  • > DET N

VP

  • > VI | VT NP

DET -> the N

  • > cat | dog | food

VI

  • > meows | barks

VT

  • > bites | eats

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 3/26

slide-4
SLIDE 4

T H E U N I V E R S I T Y O F E D I N B U R G H

Recognising grammatical sentences

Yes:

“the cat meows” “the cat bites the dog” “the dog eats the food”

No

“cat the cat cat” “dog bites meows”

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 4/26

slide-5
SLIDE 5

T H E U N I V E R S I T Y O F E D I N B U R G H

Generation Example

This uses a proof tree, rather than a search tree.

S

  • > NP VP

NP

  • > DET N

VP

  • > VI | VT NP

DET -> the N

  • > cat | dog | food

VI

  • > meows | barks

VT

  • > bites | eats

S NP DET the N cat VP VI meows

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 5/26

slide-6
SLIDE 6

T H E U N I V E R S I T Y O F E D I N B U R G H

A simpler CFG

T

  • > c

T

  • > aTb

In Prolog, with lists of characters: t([c]). t(S) :- t(S1), append([a],S1,S2), append(S2,[b],S).

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 6/26

slide-7
SLIDE 7

T H E U N I V E R S I T Y O F E D I N B U R G H

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

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 7/26

slide-8
SLIDE 8

T H E U N I V E R S I T Y O F E D I N B U R G H

A better way?

Clearly, we need to guess when we’re generating –

but also guess when we’re parsing an unknown sequence

This is inefficient — lots of backtracking!

Reordering goals doesn’t help much

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 8/26

slide-9
SLIDE 9

T H E U N I V E R S I T Y O F E D I N B U R G H

An even simpler CFG (again)

T

  • > c

T

  • > aTb

In Prolog, with accumulators: t([c|L],L). t([a|L1],M) :- t(L1,[b|M])

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 9/26

slide-10
SLIDE 10

T H E U N I V E R S I T Y O F E D I N B U R G H

using accumulator version

?- t(L,[]). L = [c]. L = [a,c,b]. L = [a,a,c,b,b]. ...

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 10/26

slide-11
SLIDE 11

T H E U N I V E R S I T Y O F E D I N B U R G H

Difference Lists

A difference list is a pair (t,X), where t is a list term with the shape [t1,t2,..tn|X], and X is a variable. Difference lists correspond to normal lists as follows: normal list difference list [t1, t2, . . . , tn] ([t1, t2, . . . , tn|X], X) Here we need to be careful that different difference lists use different Prolog variables! Difference lists are important because they allow much more efficient list operations.

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 11/26

slide-12
SLIDE 12

T H E U N I V E R S I T Y O F E D I N B U R G H

Difference lists cdt

Some examples: Empty difference list: (X,X) n-element difference list: ([a1,a2,..an|X],X), Appending difference lists (t,X) and (u,Y): — simply unify X and u — yields (t[u/X],Y) eg, append ([1,2|X],X) to ([3,4|Y],Y); unify X=[3,4|Y], and obtain ([1,2,3,4|Y],Y).

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 12/26

slide-13
SLIDE 13

T H E U N I V E R S I T Y O F E D I N B U R G H

Difference Lists ctd

Sometimes, people work with just the first part of the difference list (above, [t1,t2,..tn|X]); need to be careful that the variable really is a variable when called. We can write append of difference lists simply by using a different

  • representation. Let’s take Z/X for a difference list (where

Z=[t1,t2,..tn]) above; here / is already available as an infix

  • perator. Then difference list append is simply:

dl_append( X/Y, Y/Z, X/Z ).

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 13/26

slide-14
SLIDE 14

T H E U N I V E R S I T Y O F E D I N B U R G H

Difference Lists ctd

This is correct and efficient when we really are dealing with difference lists, and the first and second are inputs. Because there is a single clause, there can only be one solution, if there is any solution; so this will not give all solutions in mode dl append(-,-,+). ?- dl_append( [1,2|Y]/Y,[3,4|Z]/Z,Ans). Y = [3,4|Z], Ans = [1,2,3,4|Z]/Z; no ?- dl_append( A, B, [1,2,3]/Z ). A = [1,2,3]/_A, B = _A/Z ? ; no

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 14/26

slide-15
SLIDE 15

T H E U N I V E R S I T Y O F E D I N B U R G H

Difference lists reverse

Compare: %% basic reverse, no optimisation naive_reverse([],[]). naive_reverse([X|Xs],Ys) :- naive_reverse(Xs,Rs), append(Rs,[X],Ys). %% difference lists used in second argument %% of reverse_dl reverse_dl([],T\T). reverse_dl([X|Xs],Rs\T) :- reverse_dl(Xs,Rs\[X|T]).

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 15/26

slide-16
SLIDE 16

T H E U N I V E R S I T Y O F E D I N B U R G H

An even simpler CFG (again)

T

  • > c

T

  • > aTb

In Prolog, with difference lists: t(L,M) :- L = [c|M]. t(L,M) :- L = [a|L1], t(L1,M1), M1 = [b|M]

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 16/26

slide-17
SLIDE 17

T H E U N I V E R S I T Y O F E D I N B U R G H

Definite Clause Grammars

Parsing using DCGs is so useful that Prolog has built-in syntax for it: t --> [c]. t --> [a], t, [b]. translates to: t(L,M) :- L = [c|M]. t(L,M) :- L = [a|L1], t(L1,M1), M1 = [b|M].

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 17/26

slide-18
SLIDE 18

T H E U N I V E R S I T Y O F E D I N B U R G H

DCG syntax

Rules have the form nonterm --> body Body terms are:

terminal lists [t1,...,tn] (may be []) nonterminals s,t,u . . . sequential composition body1,body2 alternative choice body1; body2

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 18/26

slide-19
SLIDE 19

T H E U N I V E R S I T Y O F E D I N B U R G H

Using DCG version

DCG is translated to difference lists version, so used in the same way. ?- t(L,[]). L = [c]. L = [a,c,b]. L = [a,a,c,b,b]

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 19/26

slide-20
SLIDE 20

T H E U N I V E R S I T Y O F E D I N B U R G H

using DCG ctd

We can also use the built-ins phrase/2, phrase/3: when the first argument is a non-terminal from the grammar, this generates corresponding examples (in difference list form in the second case). ?- phrase(t,L). L = [c]. L = [a,c,b]. ?- phrase(t,L,M). L = [c|M]. L = [a,c,b|M].

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 20/26

slide-21
SLIDE 21

T H E U N I V E R S I T Y O F E D I N B U R G H

larger 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].

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 21/26

slide-22
SLIDE 22

T H E U N I V E R S I T Y O F E D I N B U R G H

DCGs with tests

DCG clause bodies can also contain tests, written as an arbitrary Prolog goal in curly brackets: {Goal} Example: n --> [Word], {noun(Word)}. noun(dog). noun(cat).

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 22/26

slide-23
SLIDE 23

T H E U N I V E R S I T Y O F E D I N B U R G H

DCGs with recursion

Left recursion, as usual, leads to non-termination: exp --> exp,[+],exp Avoid by using right recursion and fall-through exp --> simple_exp,[+],exp. exp --> simple_exp.

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 23/26

slide-24
SLIDE 24

T H E U N I V E R S I T Y O F E D I N B U R G H

DCGs with parameters

Non-terminals in DCGs can have parameters: t(0) --> [c]. t(succ(N)) --> [a], t(N), [b]

Can keep track of depth of nesting in terms.

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 24/26

slide-25
SLIDE 25

T H E U N I V E R S I T Y O F E D I N B U R G H

DCGs and parameters > CFGs

With parameters, we go outside the expressiveness of CFGs. u(N) --> n(N,a), n(N,b), n(N,c). n(0,X) --> []. n(succ(N),X) --> [X], n(N,X). This characterises a set of expressions that has no CFG description. (what set?)

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 25/26

slide-26
SLIDE 26

T H E U N I V E R S I T Y O F E D I N B U R G H

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 as a tutorial exercise.

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 26/26

slide-27
SLIDE 27

T H E U N I V E R S I T Y O F E D I N B U R G H

Further Reading

LPN, chs 7–8: more difference list examples and translation of DCGs to Prolog Next time: search techniques: depth-first, iterative deepening, breadth-first, best-first.

Alan Smaill Logic Programming: Parsing. Difference Lists, DCGs Oct 15 2015 27/26