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.

NOTES ON DATA STRUCTURING 111<br />

Every car has a make and regnumber but <strong>on</strong>ly local cars have an owner or<br />

first registrati<strong>on</strong> date; and <strong>on</strong>ly foreign cars have an origin.<br />

A third example is the definiti<strong>on</strong> of geometric figures, which in some<br />

applicati<strong>on</strong> might be categorised as either rectangles, triangles, or circles<br />

type figure = (positi<strong>on</strong>: point; rect: R, tri: T, circ: C).<br />

The method of specifying the figure varies in each case. For a rectangle, the<br />

angle of inclinati<strong>on</strong> of <strong>on</strong>e of the sides is given, together with the two lengths<br />

of the sides"<br />

type R = (inclinati<strong>on</strong>:angle; side 1, side 2:real).<br />

A triangle is specified by the angle of inclinati<strong>on</strong> and length of <strong>on</strong>e of its<br />

sides together with the angles formed between it and the other two sides"<br />

type T = (inclinati<strong>on</strong>" angle; side" real; anglel, angle2" angle).<br />

For a circle, all that is necessary is to specify the diameter as a real number.<br />

type C = (diameter: real).<br />

When a type is defined as the uni<strong>on</strong> of several other types, it is important<br />

to recognise that its values must be c<strong>on</strong>sidered wholly distinct from those<br />

of any of the types in terms of which it is defined. Otherwise there would be<br />

an immediate violati<strong>on</strong> of the rule that each value bel<strong>on</strong>gs to <strong>on</strong>ly <strong>on</strong>e type.<br />

Thus the uni<strong>on</strong> of types must be clearly distinguished from the normal<br />

c<strong>on</strong>cept of set uni<strong>on</strong>. Furthermore, for each element of the uni<strong>on</strong> type, it is<br />

possible to determine from which of the c<strong>on</strong>stituent types it originated, even<br />

if the same type has been repeated several times. For example, a double pack<br />

of cards used for playing patience may be defined as the uni<strong>on</strong> of two packs,<br />

i.e.,<br />

type patience card = (red: cardface, blue: cardface).<br />

Each value of type patience card is clearly marked as having originated either<br />

from the red pack or from the blue pack, even if perhaps in the real world<br />

the colours of the backs are the same. This fact explains the use of the term<br />

"discriminated uni<strong>on</strong>" to apply to this form of type definiti<strong>on</strong>. It folloWs that<br />

the cardinality of a discriminated uni<strong>on</strong> is always the sum of the cardinalities<br />

of its c<strong>on</strong>stituent types.<br />

5.1. MANIPULATION<br />

Any value of a discriminated uni<strong>on</strong> carries with it a tag field indicating<br />

which of the particular c<strong>on</strong>stituent types it originated from; <strong>on</strong> assignment<br />

this is copied, and <strong>on</strong> a test of equality, the tag fields must be the same if the<br />

values are to be equal.

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

Saved successfully!

Ooh no, something went wrong!