[ Pobierz całość w formacie PDF ]
.It has to do with a callinside a data structure (record, tuple, or list).Here is an example:Ys={F X}|{Map Xr F}In this case, the translation puts the nested calls after the bind operation:local Y Yr inYs=Y|Yr{F X Y}{Map Xr F Yr}endThis ensures that the recursive call is last.Section 2.4.6 explains why this isimportant for execution efficiency.The full Map function is defined as follows:fun {Map Xs F}case Xsof nil then nil[] X|Xr then {F X}|{Map Xr F}endendMap applies the function F to all elements of a list and returns the result.Hereis an example call:{Browse {Map [1 2 3 4] fun {$ X} X*X end}}This displays [1 4 9 16].The definition of Map translates as follows to thekernel language:proc {Map Xs F ?Ys}case Xs of nil then Ys=nilelse case Xs of X|Xr thenlocal Y Yr inYs=Y|Yr{F X Y}{Map Xr F Yr}endend endendCopyright © 2001-3 by P.Van Roy and S.Haridi.All rights reserved.88 Declarative Computation ModelinterStatement ::= statement| declare { declarationPart }+[ interStatement ]| declare { declarationPart }+ in interStatementdeclarationPart ::= variable | pattern ´=´ expression | statementTable 2.8: Interactive statement syntax"Browse" procedure value "Browse" procedure value"X" x1 unbound x1 unbound"Y" x2 unbound x2 unbound"X" x3 unbound"Y" x4 unboundResult of first declare X Y Result of second declare X YFigure 2.19: Declaring global variablesThe dataflow variable Yr is used as a placeholder for the result in the recursivecall {Map Xr F Yr}.This lets the recursive call be the last call.In our model,this means that the recursion executes with the same space and time efficiencyas an iterative construct like a while loop.2.5.3 Interactive interface (the declare statement)The Mozart system has an interactive interface that allows to introduce programfragments incrementally and execute them as they are introduced.The fragmentshave to respect the syntax of interactive statements, which is given in Table 2.8.An interactive statement is either any legal statement or a new form, thedeclarestatement.We assume that the user feeds interactive statements to the systemone by one.(In the examples given throughout this book, thedeclarestatementis often left out.It should be added if the example declares new variables.)The interactive interface allows to do much more than just feed statements.It has all the functionality needed for software development.Appendix A givesa summary of some of this functionality.For now, we assume that the user justknows how to feed statements.The interactive interface has a single, global environment.The declarestatement adds new mappings to this environment.It follows that declare canCopyright © 2001-3 by P.Van Roy and S.Haridi.All rights reserved.2.5 From kernel language to practical language 89only be used interactively, not in standalone programs.Feeding the followingdeclaration:declare X Ycreates two new variables in the store, x1 and x2.and adds mappings from X andY to them.Because the mappings are in the global environment we say that Xand Y are global variables or interactive variables.Feeding the same declarationa second time will cause X and Y to map to two other new variables, x3 and x4.Figure 2.19 shows what happens.The original variables, x1 and x2, are still in thestore, but they are no longer referred to by X and Y.In the figure, Browse mapsto a procedure value that implements the browser.The declarestatement addsnew variables and mappings, but leaves existing variables in the store unchanged.Adding a new mapping to an identifier that already maps to a variable maycause the variable to become inaccessible, if there are no other references to it.If the variable is part of a calculation, then it is still accessible from within thecalculation.For example:declare X YX=25declare AA=person(age:X)declare X YJust after the binding X=25, X maps to 25, but after the second declare XY it maps to a new unbound variable.The 25 is still accessible through theglobal variable A, which is bound to the record person(age:25).The recordcontains 25 because X mapped to 25 when the binding A=person(age:X) wasexecuted.The seconddeclare X Ychanges the mapping ofX, but not the recordperson(age:25) since the record already exists in the store.This behavior ofdeclare is designed to support a modular programming style.Executing aprogram fragment will not cause the results of any previously-executed fragmentto change.There is a second form of declare:declare X Y in stmtwhich declares two global variables, as before, and then executes stmt.Thedifference with the first form is that stmt declares no variables (unless it containsa declare).The BrowserThe interactive interface has a tool, called the Browser, which allows to look intothe store.This tool is available to the programmer as a procedure called Browse.The procedure Browsehas one argument.It is called as {Browse expr }, whereexpr is any expression.It can display partial values and it will update thedisplay whenever the partial values are bound more.Feeding the following:{Browse 1}Copyright © 2001-3 by P.Van Roy and S.Haridi.All rights reserved.90 Declarative Computation ModelFigure 2.20: The Browserdisplays the integer 1.Feeding:declare Y in{Browse Y}displays just the name of the variable, namely Y.No value is displayed.Thismeans that Y is currently unbound.Figure 2.20 shows the browser window afterthese two operations.If Y is bound, e.g., by doing Y=2, then the browser willupdate its display to show this binding.Dataflow executionWe saw earlier that declarative variables support dataflow execution, i.e., anoperation waits until all arguments are bound before executing.For sequentialprograms this is not very useful, since the program will wait forever.On theother hand, it is useful for concurrent programs, in which more than one instruc-tion sequence can be executing at the same time.An independently-executinginstruction sequence is called a thread.Programming with more than one threadis called concurrent programming; it is introduced in Chapter 4.All examples in this chapter execute in a single thread.To be precise, eachprogram fragment fed into the interactive interface executes in its own thread.This lets us give simple examples of dataflow execution in this chapter.Forexample, feed the following statement:declare A B C inC=A+B{Browse C}This will display nothing, since the instruction C=A+B blocks (both of its argu-ments are unbound).Now, feed the following statement:A=10This will bind A, but the instruction C=A+B still blocks since B is still unbound.Finally, feed the following:Copyright © 2001-3 by P.Van Roy and S.Haridi.All rights reserved.2.6 Exceptions 91B=200This displays 210 in the browser.Any operation, not just addition, will blockif it does not get enough input information to calculate its result.For example,comparisons can block
[ Pobierz całość w formacie PDF ]
-
Menu
- Index
- May Peter Wyspa Lewis 2 Czlowiek z Wyspy Lewis
- Peter Charles Hoffer The Brave New World, A History of Early America Second Edition (2006)
- Peter Zarrow China in War and Revolution, 1895 1949 (2005)
- Peter Berling Dzieci Graala 02 Krew królów
- Hamilton Peter F. Swit nocy 2.2 Widmo Alchemika Konflikt
- May Peter Wyspa Lewis 1 Czarny Dom
- Peter Berling Dzieci Graala Krew krolow
- Robert Jordan Oko Swiata t.1
- Martel Yann Zycie Pi.BLACK
- Sedziwoj DZIEKONSKI
- zanotowane.pl
- doc.pisz.pl
- pdf.pisz.pl
- niewolnicy.xlx.pl