11.04.2015 Views

Newsletter #9-#10 - Moorecad.com

Newsletter #9-#10 - Moorecad.com

Newsletter #9-#10 - Moorecad.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Generjc Routines ano Variable Types in PASCAL<br />

07/£7/7720~<br />

Gen~rjc koutin~s ann vAriaoLe lyoes in PASCAL<br />

U7/27/(72Uo<br />

generate a run time test. Tnis, holds, tOo, if the additional predicate<br />

is not e,aluable at <strong>com</strong>pile time.<br />

--------------------------------<br />

In _egbreit"s ECL system there exists a campi ler as well as an<br />

interpreter. both fully <strong>com</strong>patible. Each may call the other as a<br />

suoroutine. Therefore the <strong>com</strong>piler is able to e,aluate parts of a<br />

campi lation un't (routine) and to use the ,alue instead of the form.<br />

So predicates of a generic form may be e,aluated by tne campi ler and<br />

a campi le time selection may be done. In the generic form carried<br />

o,er to P~SCAL-GVr, preoicates are not allowed. There are two reasons<br />

for this aecision:<br />

1. we ha,e no interpreter in our system. Therefore, predicates are<br />

not e,aluable at <strong>com</strong>pile-time, and a run time selection is necessary<br />

for each call of a generic routine with predicates, e,en if<br />

the co,ering of all argument types is asserted. 50 the numeer of<br />

possible run ti~e errors increases.<br />

2. by design, ~ decision in a generic form '5 a decision deeending<br />

on the types, nOt on the 'alues of the arguments. Accoroingly,<br />

ereciicates in a generic form snould De eredicates on types only.<br />

The type classes that are oefined implicitly by predicates,<br />

howe'er, dO not ha,e suCh a specific structure that the <strong>com</strong>piler<br />

is aole to hanole them (e.g. aLL one-Dimensional arrays). The<br />

<strong>com</strong>O; ler .ill nDt be able tD determ'ne any <strong>com</strong>ponent statically.<br />

Therefore a static type cheCking .i II no De possiele inside the<br />

brancn and sa the advantages of tne generic ferm w; II ee lest.<br />

Union types in the sense of EL1, no~e,er, fit to the reguirements<br />

of PASCAL. First, tne structure of eaCh alternati,e is kno.n to the<br />

<strong>com</strong>piler, so there is no difference to a normal PASCAL type after<br />

the selection of one single alternat;,e ;n a generic form. second,<br />

the type constancy during tne lifetime of a union-typee parameter<br />

allows a stack implement.tion of sUCh parameters. when the proce-<br />

Dure is entereD, the defjnite type with its length oe<strong>com</strong>es known<br />

(Since this happens at r~n time, runtime type descriptors ha,e to be<br />

generated by the <strong>com</strong>piler). Since the length is unchangeaDle, an<br />

aooress on the staCk may be Computed for eaCh union-typee parameter<br />

and the argument ,a lues may ue copiee. Access to the parameter<br />

,alues is ;noirect ,ia the <strong>com</strong>pile-time-<strong>com</strong>putable local aedress,<br />

~here a poihter fO tne run-time-<strong>com</strong>putable real adoress ;s to be<br />

storeD. Since .e are able to put uniDn-typed parameters on the<br />

stack as oPPDsea to the PASCAL heap (.here flexible-length parameters<br />

.oulD ha,e to De pUt), there are nD problems with RELEASE<strong>com</strong>manas<br />

of the user. So union-typee parameters do not mess UP<br />

PASCAL"s storage management SCheme.<br />

The demand for static type cneCK;ng implies that eaCh generic<br />

par.met~r is fi.ea to a aetined, camp; ler-kno.n type (including a<br />

union type) at the entry into a generic branCh. If that type is a<br />

union, operatiDns on tne parameter are restricteo to assignments and<br />

eQuality t~sts inside the oranCh, s,nce static tyDe checking requires<br />

IUlly f;xee operano types for any Otner operation. This restriction<br />

tDrces a progra~~er to .rite oo.n reDeatedly si~ilar branches for<br />

similar, Out different tyues (e.~. array of ;nt~gervs. array of<br />

real). Tne PASCAL can,entiun of ident,fyinq a type oy jts na~e,<br />

nOt vy its structure, aisenaolps uS to oefine arrays of un;ons and<br />

sO to hanDle si~ilar structures in a single Dr~nCh, since we then<br />

hao to ha,e ,ariables 01 type array of unions. uniDn-typed ,ariables,<br />

nD.e,er, will not be alluweD, since (a) each ,ariable must<br />

ha,e an unchangeaole type ltnere is no chance Df a postponement of<br />

type choice as .ith parameters) ane (n) union-typed ,ariables .ould<br />

impose further neea fnr run time type cneCkS. So the oisad,antage<br />

of multiple .riting ~c~n can not oe remedieu oy using unions. we<br />

.ill see later that there is a slight impro,ement oy use of ,ariable<br />

types.<br />

.ith the gj,en restrictions, the generic form is easily transferaule<br />

to PASCAL. Tnus, a PASCAL-GVT prOcedure bOdY may ae either<br />

a normal PASCAL proceDure Dr a generic form. The only ,iolation of<br />

static ty~e checking ay the use Df a gener,c form may occur if only<br />

oartjal cD,erin~ is gi,en at <strong>com</strong>pile time and thus a run time check<br />

is needed tor branch selection. If at run time the cDmo,natiDn Df<br />

argument types daes not fit to any of the branChes, a type-dependent<br />

run time error will result, ,iDlating the principle of static<br />

tyoe checKing. Tnis, nowever, onlt oCCurS at a well-defined interface,<br />

wnere the user MOy expect it. ~esides, tn~ number of run t;me<br />

branch selections .ill narmally be small.<br />

-.------<br />

tyPe JNT.~R.¥ ~ array l,..J of INTEGE~;<br />

UNION s one~f \1~TtGE", INTAkRAY);<br />

nrOceoure P (P~RA: uNION; PA~B: oneof (CHAR, INTEGER»;<br />

generic (PAkA,PARb) of<br />

lINTAR"AY,ChAkJ Deg;n ena;<br />

lINTEGER,INTE&EkJ cegin end<br />

end;<br />

Types 8S values<br />

--------------.<br />

In eASlAL, typeS are statiC oescrlPtions of the structure of .<br />

class Of oDjects. In EL1, nowe,er, type generators are callable<br />

functions and deliver a type ,alue. Ihe <strong>com</strong>Di ler e,aluates such<br />

generators unCer assistance Of the interpreter. Consequently, useruefined<br />

tyDe functions as .ell a5 type ,ariables are permitted. If<br />

a type functiDn is not e'aluaole at <strong>com</strong>pile time, a call to the<br />

interpreter is generatee, i,e. type checKing is oelegated to run time.<br />

rYDP v8r;acles may be -frozen', i.e. p.vaLu¥tea in an interpreted<br />

en,ironment of 8 cDmpllation stev, and tneir ,alue may oe used as a<br />

type constant jn the <strong>com</strong>pilation unit. "untrozen" type ,ariables,<br />

aqain, require type checKing at run time. The facilities of evaluating<br />

tYDe functions and freezinG tyoe ,arlaoles enable. the <strong>com</strong>piler<br />

to nail ao~n variable typ~s. Tne ninaing of routines to typts<br />

;s trar'~fered from pro9rsmm1ng t~mc to campi le time, Out an inte~-<br />

~retec pnvirQnment has to oe ne involvea in this process.

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

Saved successfully!

Ooh no, something went wrong!