13.11.2014 Views

Introduction to Computational Linguistics

Introduction to Computational Linguistics

Introduction to Computational Linguistics

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

3. Welcome To The Typed Universe 7<br />

Each type is interpreted by particular elements. Typically, the elements of different<br />

types are different from each other. (This is when no type conversion occurs.<br />

But see below.) If x is of type α and y is of type β α then x is distinct from<br />

y. Here is how it is done. We interpret each type for b ∈ B by a set M b in<br />

such a way that M a ∩ M b = ∅ if a b. Notice, for example, that OCaML<br />

has a basic type char and a basic type string, which get interpreted by the<br />

set of ASCII–characters, and strings of ASCII–characters, respectively. We may<br />

construe strings over characters as function from numbers <strong>to</strong> characters. Then,<br />

even though a single character is thought of in the same way as the string of<br />

length 1 with that character, they are now physically distinct. The string k, for<br />

example, is the function f : {0} → M char such that f (0) = k. OCaML makes<br />

sure you respect the difference by displaying the string as "k" and the character<br />

as ’k’. The quotes are not part of the object, they tell you what its type is.<br />

Given two types α and β we can form the type of functions from α–objects <strong>to</strong><br />

β–objects. These functions have type α → β. Hence, we say that<br />

(14) M α→β = { f : f is a function from M α <strong>to</strong> M β }<br />

For example, we can define a function f by saying that f (x) = 2x + 3. The way <strong>to</strong><br />

do this in OCaML is by issuing<br />

(15)<br />

# let f x = (2 * x) + 3;;<br />

val f :<br />

int −> int = <br />

Now we understand better what OCaML is telling us. It says that the value of the<br />

symbol f is of type int -> int because it maps integers <strong>to</strong> integers, and that<br />

it is a function. Notice that OCaML inferred the type from the definition of the<br />

function. We can take a look how it does that.<br />

First, if x is an element of type α → β and y is an element of type α, then x is<br />

a function that takes y as its argument. In this situation the string for x followed<br />

by the string for y (but separated by a blank) is well–formed for OCaML, and it is<br />

of type β. OCaML allows you <strong>to</strong> enclose the string in brackets, and sometimes it<br />

is even necessary <strong>to</strong> do so. Hence, f 43 is well–formed, as is (f 43) or even (f<br />

(43)). Type that in and OCaML answers:<br />

(16)<br />

# f 43;;<br />

- : int = 89

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

Saved successfully!

Ooh no, something went wrong!