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.

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

since recovery of the original value requires <strong>on</strong>ly subtracti<strong>on</strong> rather than<br />

divisi<strong>on</strong>.<br />

In general the values of the different alternative types occupy different<br />

amounts of storage, so the shorter values have to be "padded out" to<br />

equalise the lengths, thus observing the c<strong>on</strong>venient rule that elementary<br />

data types occupy a fixed amount of storage. In later chapters it will be seen<br />

that this padding can often be omitted when the value is a comp<strong>on</strong>ent of<br />

some larger structure.<br />

A local car A foreign car<br />

,Ford -~l make<br />

RUR157D Jregnumber<br />

of tag<br />

CARH J owner<br />

1 SePt 1968 I first registrati<strong>on</strong><br />

iFiat I make<br />

7 - 27-19 regnumber<br />

tag,<br />

I Italy origin<br />

!<br />

L ......... j padding (sometimes omitted)<br />

FIG. 2. Representati<strong>on</strong> of cars<br />

In present-day programming practice, it is quite comm<strong>on</strong> to omit the tag<br />

field in the representati<strong>on</strong> of uni<strong>on</strong>s. In order to operate correctly <strong>on</strong> such a<br />

representati<strong>on</strong>, the programmer needs to "know" from other c<strong>on</strong>siderati<strong>on</strong>s<br />

what the interpretati<strong>on</strong> of the value ought to be, since it is not possible to<br />

find out from the value itself. If his belief is mistaken, this is not detectable<br />

either by a runtime or compile-time check. Since the effect of such an error<br />

will depend <strong>on</strong> details of bitpattern representati<strong>on</strong>, it will give rise to results<br />

unpredictable in terms of the abstracti<strong>on</strong>s with which the programmer is<br />

working. It would therefore in general seem advisable to use tag fields and<br />

compile-time checkable case discriminati<strong>on</strong>s as standard programming<br />

practice, to be bypassed <strong>on</strong>ly in excepti<strong>on</strong>al circumstances.<br />

5.3. EXAMPLE<br />

We return to the c<strong>on</strong>text of the example in secti<strong>on</strong> 3.3, the analysis of<br />

language text into meaningful symbols. We wish to give a rigorous abstract<br />

definiti<strong>on</strong> of what these symbols are.<br />

type symbol =<br />

(realc<strong>on</strong>st: real,<br />

integerc<strong>on</strong>st: integer,<br />

identifier: ident,<br />

basic'delimiter);<br />

where we will leave the type ident undefined for the time being, and assume<br />

that the delimiters are defined by enumerati<strong>on</strong>.

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

Saved successfully!

Ooh no, something went wrong!