[ Pobierz całość w formacie PDF ]
.Thisallows truly open programming, in which independently-written modulescan come together at run time and interact with each other.It also makesprogram development scalable, i.e., extremely large programs can be dividedinto modules that can be compiled individually without recompiling othermodules.This is harder to do with static typing because the type disciplinemust be enforced across module boundaries." Dynamic typing shortens the turnaround time between an idea and itsimplementation.It enables an incremental development environment thatis part of the run-time system.It allows to test programs or programfragments even when they are in an incomplete or inconsistent state." Static typing allows to catch more program errors at compile time.Thestatic type declarations are a partial specification of the program, i.e., theyspecify part of the program s behavior.The compiler s type checker veri-fies that the program satisfies this partial specification.This can be quitepowerful.Modern static type systems can catch a surprising number ofsemantic errors." Static typing allows a more efficient implementation.Since the compiler hasmore information about what values a variable can contain, it can choose amore efficient representation.For example, if a variable is of boolean type,the compile can implement it with a single bit.In a dynamically-typedlanguage, the compiler cannot always deduce the type of a variable.Whenit cannot, then it usually has to allocate a full memory word, so that anypossible value (or a pointer to a value) can be accommodated." Static typing can improve the security of a program.Secure ADTs can beconstructed based solely on the protection offered by the type system.Unfortunately, the choice between dynamic and static typing is most often basedon emotional ( gut ) reactions, not on rational argument.Adherents of dynamictyping relish the expressive freedom and rapid turnaround it gives them andcriticize the reduced expressiveness of static typing.On the other hand, adherentsof static typing emphasize the aid it gives them for writing correct and efficientprograms and point out that it finds many program errors at compile time.LittleCopyright © 2001-3 by P.Van Roy and S.Haridi.All rights reserved.108 Declarative Computation Modelhard data exists to quantify these differences.In our experience, the differencesare not great.Programming with static typing is like word processing with aspelling checker: a good writer can get along without it, but it can improve thequality of a text.Each approach has a role in practical application development.Static typ-ing is recommended when the programming techniques are well-understood andwhen efficiency and correctness are paramount.Dynamic typing is recommendedfor rapid development and when programs must be as flexible as possible, suchas application prototypes, operating systems, and some artificial intelligence ap-plications.The choice between static or dynamic typing does not have to be all or noth-ing.In each approach, a bit of the other can be added, gaining some of its ad-vantages.For example, different kinds of polymorphism (where a variable mighthave values of several different types) add flexibility to statically-typed function-al and object-oriented languages.It is an active research area to design statictype systems that capture as much as possible of the flexibility of dynamic typesystems, while encouraging good programming style and still permitting compiletime verification.The computation models given in this book are all subsets of the Oz lan-guage, which is dynamically typed.One research goal of the Oz project is toexplore what programming techniques are possible in a computation model thatintegrates several programming paradigms.The only way to achieve this goal iswith dynamic typing.When the programming techniques are known, then a possible next step is todesign a static type system.While research in increasing the functionality andexpressiveness of Oz is still ongoing in the Mozart Consortium, the Alice projectat Saarland University in Saarbrücken, Germany, has chosen to add a static typesystem.Alice is a statically-typed language that has much of the expressivenessof Oz.At the time of writing, Alice is interoperable with Oz (programs canbe written partly in Alice and partly in Oz) since it is based on the Mozartimplementation.2.8 Exercises1.Consider the following statement:proc {P X}if X>0 then {P X-1} endendIs the identifier occurrence ofPin the procedure body free or bound? Justifyyour answer.Hint: this is easy to answer if you first translate to kernelsyntax.Copyright © 2001-3 by P.Van Roy and S.Haridi.All rights reserved.2.8 Exercises 1092.Section 2.4 explains how a procedure call is executed.Consider the followingprocedure MulByN:declare MulByN N inN=3proc {MulByN X ?Y}Y=N*Xendtogether with the call{MulByN A B}.Assume that the environment at thecall contains {A ’! 10, B ’! x1}.When the procedure body is executed, themapping N ’! 3 is added to the environment.Why is this a necessary step?In particular, would not N ’! 3 already exist somewhere in the environmentat the call? Would not this be enough to ensure that the identifierNalreadymaps to 3? Give an example where N does not exist in the environmentat the call.Then give a second example where N does exist there, but isbound to a different value than 3.3.If a function body has an if statement with a missing else case, then anexception is raised if the if condition is false.Explain why this behavioris correct.This situation does not occur for procedures.Explain why not.4.This exercise explores the relationship between the if statement and thecase statement.(a) Define the if statement in terms of the case statement.This showsthat the conditional does not add any expressiveness over patternmatching.It could have been added as a linguistic abstraction.(b) Define the case statement in terms of the if statement, using theoperations Label, Arity, and ´.´ (feature selection).This shows that the if statement is essentially a more primitive version ofthe case statement.5.This exercise tests your understanding of the full case statement.Giventhe following procedure:proc {Test X}case Xof a|Z then {Browse ´case´(1)}[] f(a) then {Browse ´case´(2)}[] Y|Z andthen Y==Z then {Browse ´case´(3)}[] Y|Z then {Browse ´case´(4)}[] f(Y) then {Browse ´case´(5)}else {Browse ´case´(6)} endendCopyright © 2001-3 by P.Van Roy and S.Haridi.All rights reserved.110 Declarative Computation ModelWithout executing any code, predict what will happen when you feed{Test[b c a]},{Test f(b(3))},{Test f(a)},{Test f(a(3))},{Test f(d)},{Test [a b c]}, {Test [c a b]}, {Test a|a}, and {Test ´|´(a bc)}.Use the kernel translation and the semantics if necessary to make thepredictions.After making the predictions, check your understanding byrunning the examples in Mozart.6.Given the following procedure:proc {Test X}case X of f(a Y c) then {Browse ´case´(1)}else {Browse ´case´(2)} endendWithout executing any code, predict what will happen when you feed:declare X Y {Test f(X b Y)}Same for:declare X Y {Test f(a Y d)}Same for:declare X Y {Test f(X Y d)}Use the kernel translation and the semantics if necessary to make the predic-tions.After making the predictions, check your understanding by runningthe examples in Mozart.Now run the following example:declare X Yif f(X Y d)==f(a Y c) then {Browse ´case´(1)}else {Browse ´case´(2)} endDoes this give the same result or a different result than the previous exam-ple? Explain the result.7
[ Pobierz całość w formacie PDF ]