20.03.2013 Views

II. Notes on Data Structuring * - Cornell University

II. Notes on Data Structuring * - Cornell University

II. Notes on Data Structuring * - Cornell University

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.

110 c.A.R. HOARE<br />

Each value of type pokercard corresp<strong>on</strong>ds either to an ordered pair with<br />

comp<strong>on</strong>ents indicating suit and rank; or else it corresp<strong>on</strong>ds to <strong>on</strong>e of the<br />

two jokers in the enumerati<strong>on</strong> type.<br />

In specifying a c<strong>on</strong>stant of a discriminated uni<strong>on</strong> type, it is necessary to<br />

indicate to which of the alternative types the value denoted is intended to<br />

bel<strong>on</strong>g. This is d<strong>on</strong>e by writing the name of the alternative explicitly, for<br />

example:<br />

pokercard (normal (heart, Jack))<br />

denotes a value from the first alternative, whereas<br />

pokercard (wild (joker 2))<br />

denotes a value from the sec<strong>on</strong>d alternative. In general, it is c<strong>on</strong>venient to<br />

omit the type name, where the type can be inferred from c<strong>on</strong>text.<br />

A sec<strong>on</strong>d example of a discriminated uni<strong>on</strong> might be found in the main-<br />

tenance of a register of all cars in a country. Cars may be distinguished as<br />

local cars owned by residents of the country, and visitor cars brought into<br />

the country temporarily by n<strong>on</strong>-residents. The informati<strong>on</strong> required is rather<br />

different in the two cases. In both cases the number and the make of the car<br />

is c<strong>on</strong>sidered relevant. However, for a local car, the name of the owner of<br />

the car is required, and the date <strong>on</strong> which the car was first registered in that<br />

owner's name. For visitor cars, this informati<strong>on</strong> is not relevant:all that is<br />

required is the standard three-letter abbreviati<strong>on</strong> of the name of the country<br />

of origin. Thus the definiti<strong>on</strong> of the two alternative types of car might be:<br />

type local car = (make: manufacturer; regnumber: carnumber;<br />

owner: pers<strong>on</strong>; first registrati<strong>on</strong>: date);<br />

type visitor car = (make" manufacturer; regnumber" carnumber;<br />

origin'country);<br />

Now it is possible to define a type covering both kinds of car:<br />

type car = (local:localcar,<br />

foreign:foreign car).<br />

But here it is inc<strong>on</strong>venient to define the structure of tocal and foreign cars<br />

separately; and we would like to take advantage of the fact that several of<br />

their comp<strong>on</strong>ents are the same. This may be d<strong>on</strong>e by bringing the comm<strong>on</strong><br />

comp<strong>on</strong>ents in fr<strong>on</strong>t of both alternatives:<br />

type car -~ (make: manufacturer;<br />

regnumber: carnumber;<br />

(local: (owner:pers<strong>on</strong>;<br />

first registrati<strong>on</strong>: date),<br />

foreign: (origin: country))<br />

).

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

Saved successfully!

Ooh no, something went wrong!