II. Notes on Data Structuring * - Cornell University
II. Notes on Data Structuring * - Cornell University
II. Notes on Data Structuring * - Cornell University
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
112 c.A.R. HOARE<br />
On c<strong>on</strong>structing a value of a discriminated uni<strong>on</strong> type, it is necessary to<br />
name the alternative type from which the value originated:<br />
patience card (red (spade, Jack)).<br />
This will automatically cause the value "red" to be assigned to the tag field<br />
of the result.<br />
A particular car may be denoted by<br />
car (Ford, "RUR157D",<br />
local (me, date (1, Sept, 1968))).<br />
In order to access and operate <strong>on</strong> the informati<strong>on</strong> encoded as a dis-<br />
criminated uni<strong>on</strong>, it is necessary to c<strong>on</strong>vert it back to its original type.<br />
This may be accomplished by thec<strong>on</strong>venti<strong>on</strong> of using the label of this type<br />
as if it were a selector, e.g."<br />
cardl, wild<br />
carl. foreign<br />
figl. tri<br />
is of type (joker 1, joker 2)<br />
is of type (origin' country)<br />
is of type T<br />
If the c<strong>on</strong>stituent type is a Cartesian product, its selectors may be validly<br />
applied to the resulting value, using the c<strong>on</strong>venti<strong>on</strong> that the operator<br />
associates to the left.<br />
cardl, normal, r<br />
carl. local, owner<br />
figl. circ. diameter<br />
If the programmer attempts to c<strong>on</strong>vert a discriminated uni<strong>on</strong> value<br />
back to a type from which it did not originate, this is a serious programming<br />
error, which could lead to meaningless results. This error can be detected<br />
<strong>on</strong>ly by a runtime check, which tests the tag field whenever such a c<strong>on</strong>versi<strong>on</strong><br />
is explicitly or implicitly invoked. Such a check is timec<strong>on</strong>suming and when<br />
it fails, highly inc<strong>on</strong>venient. We therefore seek a notati<strong>on</strong>al technique which<br />
will guarantee that this error can never occur in a running program; and<br />
the guarantee is given by merely inspecting the text, without any knowledge<br />
of the runtime values being processed. Such a guarantee could be given by an<br />
automatic compiler, if available.<br />
The proposed notati<strong>on</strong>al technique is a mixture between the with c<strong>on</strong>-<br />
structi<strong>on</strong> for Cartesian products and the case c<strong>on</strong>structi<strong>on</strong> for discriminati<strong>on</strong>.<br />
Suppose that a value sv of uni<strong>on</strong> type is to be processed in <strong>on</strong>e of several