13.11.2014 Views

Introduction to Computational Linguistics

Introduction to Computational Linguistics

Introduction to Computational Linguistics

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.

8. Combina<strong>to</strong>rs 22<br />

You can of course now define<br />

(61)<br />

# let p x y = k i x y;;<br />

val p = ’a −> ’b −> ’b = <br />

and this defines the function pxy = y. Notice that you cannot ask OCaML <strong>to</strong><br />

evaluate a function abstractly. This is because it does not know that when you ask<br />

k i x y the letters x and y are variables. There are no free variables!<br />

So, you can apply combina<strong>to</strong>rs <strong>to</strong> concrete values. You cannot calculate abstractly<br />

using the inbuilt functions of OCaML. One thing you can do, however, is<br />

check whether an expression is typable. For example, the combina<strong>to</strong>r SII has no<br />

type. This is because no matter how we assign the primitive types, the function is<br />

not well–defined. OCaML has this <strong>to</strong> say:<br />

(62)<br />

# s i i;;<br />

This expression has type (’a −> ’b’) −> ’a −> ’b but<br />

is used here with type (’a -> ’b) -> ’a<br />

How does it get there? It matches the definition of s and its first argument i.<br />

Since its first argument must be of type ’a -> ’b -> ’c, this can only be the<br />

case if ’a = ’b -> ’c. Applying the function yields an expression of type (’a<br />

-> ’b) -> ’a -> ’c. Hence, the next argument must have type (’b -> ’c)<br />

-> ’b. This is what OCaML expects <strong>to</strong> find. It communicates this using ’a<br />

and ’b in place of ’b and ’c, since these are variables and can be renamed at<br />

will. (Yes, OCaML does use variables, but these are variables over types, not over<br />

actual objects.) However, if this is the case, and the argument is actually i, then<br />

the argument actually has type (’b -> ’c) -> ’b -> ’c. Once again, since<br />

OCaML did some renaming, it informs us that the argument has type (’a -><br />

’b) -> ’a -> ’b. The types do not match, so OCaML rejects the input as not<br />

well formed.<br />

Now, even though OCaML does not have free variables, it does have variables.<br />

We have met them already. Every let–clause defines some function, but this<br />

definition involves saying what it does on its arguments. We have defined i, k,<br />

and s in this way. The identifiers x, y and z that we have used there are unbound<br />

outside of the let–clause. Now notice that upon the definition of k, OCaML<br />

issued its type as ’a -> ’b -> ’a. This means that it is a function from objects

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

Saved successfully!

Ooh no, something went wrong!