Introduction to Computational Linguistics
Introduction to Computational Linguistics
Introduction to Computational Linguistics
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 9<br />
An alternative <strong>to</strong> the pair construc<strong>to</strong>r is the construc<strong>to</strong>r for records. The latter<br />
is very useful. It is most instructive <strong>to</strong> look at a particular example:<br />
(20)<br />
type car = {brand : string; vintage : int;<br />
used :<br />
bool};;<br />
This defines a type structure called car, which has three components: a brand, a<br />
vintage and a usedness value. On the face of it, a record can be replicaed with<br />
products. However, notice that records can have any number of entries, while a<br />
pair must consist of exactly two. So, the record type car can be replicated by<br />
either string * (int * bool) or by (string * int) * bool.<br />
But there are also other differences. One is that the order in which you give<br />
the arguments is irrelevant. The other is that the names of the projections can be<br />
defined by yourself. For example, you can declare that mycar is a car, by issuing<br />
(21)<br />
# let mycar = {brand = "honda"; vintage = 1977;}<br />
used = false};;<br />
This will bind mycar <strong>to</strong> the element of type car. Moreover, the expression<br />
mycar.brand has the value honda. (To communicate that, it has <strong>to</strong> be enclosed<br />
in quotes, of course. Otherwise it is mistaken for an identifier.) It is not legal <strong>to</strong><br />
omit the specification of some of the values. Try this, for example:<br />
(22)<br />
# let mycar = {brand = "honda"};;<br />
Some record field labels are undefined:<br />
vintage used;;<br />
This is not a warning: the object named ”mycar” has not been defined.<br />
(23)<br />
# mycar.brand;;<br />
Unbound value mycar<br />
If you look carefully, you will see that OCaML has many more type construc<strong>to</strong>rs,<br />
some of which are quite intricate. One is the disjunction, written |. We shall<br />
use the more suggestive ∪. We have<br />
(24) M α∪β = M α ∪ M β<br />
Notice that by definition objects of type α are also objects of type M α∪β . Hence<br />
what we said before about types keeping everything distinct is not accurate. In