20.07.2013 Views

Notes on computational linguistics.pdf - UCLA Department of ...

Notes on computational linguistics.pdf - UCLA Department of ...

Notes on computational linguistics.pdf - UCLA Department of ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Stabler - Lx 185/209 2003<br />

?- X is 2+3.<br />

X = 5<br />

Yes<br />

?- X is 2ˆ3.<br />

X = 8<br />

Yes<br />

Using this predicate, length is defined this way:<br />

length([],0).<br />

length([_|L],N) :- length(L,N0), N is N0+1.<br />

The first axiom says that the empty sequence has length 0. The sec<strong>on</strong>d axiom says that any list has length N if<br />

the result <strong>of</strong> removing the first element <strong>of</strong> the list has length N0 and N is N0+1. Since these axioms are already<br />

“built in” we can use them immediately with goals like this:<br />

?- length([a,b,1,2],N).<br />

N = 4<br />

Yes<br />

?- length([a,b,[1,2]],N).<br />

N = 3<br />

Yes<br />

We can do a lot with these basic ingredients, but first we should understand what’s going <strong>on</strong>.<br />

This standard approach to sequences or lists may seem odd at first. The empty list is a named by [], and<br />

n<strong>on</strong>-empty lists are represented as the denotati<strong>on</strong>s <strong>of</strong> the period (<strong>of</strong>ten pr<strong>on</strong>ounced “c<strong>on</strong>s” for “c<strong>on</strong>structor”),<br />

which is a binary functi<strong>on</strong> symbol. A list with <strong>on</strong>e element is denoted by c<strong>on</strong>s <strong>of</strong> that element and the empty list.<br />

For example, .(a,[]) denotes the sequence which has just the <strong>on</strong>e element a. And.(b,.(a,[])) denotes the<br />

sequence with first element b and sec<strong>on</strong>d element a. For c<strong>on</strong>venience, we use [b,a] as an alternative notati<strong>on</strong><br />

for the clumsier .(b,.(a,[])), and we use [A|B] as an alternative notati<strong>on</strong> for .(A,B).<br />

Examples:<br />

If we apply {X↦frege,Y↦[]} to the list [X|Y], wegetthelist[frege].<br />

If we apply {X↦frege,Y↦[hilbert]} to the list [X|Y], wegetthelist[frege,hilbert].<br />

[X|Y] and [frege,hilbert] match after the substituti<strong>on</strong><br />

{X↦frege,Y↦[hilbert]}.<br />

Using this notati<strong>on</strong>, we presented an axiomatizati<strong>on</strong> <strong>of</strong> the member relati<strong>on</strong>. Another basic thing we need<br />

to be able to do is to put two sequences together, “c<strong>on</strong>catenating” or “appending” them. We can accordingly<br />

define a 3-place relati<strong>on</strong> we call append with the following two simple axioms: 8<br />

append([],L,L).<br />

append([E|L0],L1,[E|L2]) :- append(L0,L1,L2).<br />

The first axiom says that, for all L, the result <strong>of</strong> appending the empty list and L is L. The sec<strong>on</strong>d axiom says<br />

that, for all E, L0, L1, andL2, the result <strong>of</strong> appending [E|L0] with L1 is [E|L2] if the result <strong>of</strong> appending<br />

8 These axioms are “built in” to SWI-prolog – they are already there, and the system will not let you redefine this relati<strong>on</strong>. However,<br />

you could define the same relati<strong>on</strong> with a different name like myappend.<br />

19

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!