1 / 91

Programming Language Concepts, COSC-3308-01 Lecture 4  

Programming Language Concepts, COSC-3308-01 Lecture 4  . Procedures, last call optimization. Reminder of the last lecture. Kernel language linguistic abstraction data types variables and partial values statements and expressions Kernel language semantics Use operational semantics

bevan
Download Presentation

Programming Language Concepts, COSC-3308-01 Lecture 4  

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Language Concepts, COSC-3308-01Lecture 4   Procedures, last call optimization CS2104, Lecture 4

  2. Reminder of the last lecture • Kernel language • linguistic abstraction • data types • variables and partial values • statements and expressions • Kernel language semantics • Use operational semantics • Aid programmer in reasoning and understanding • The model is a sort of an abstract machine, but leaves out details about registers and explicit memory address • Aid implementer to do an efficient execution on a real machine CS2104, Lecture 4

  3. Concepts • Single-assignment store • Environment • Semantic statement • Execution state • Computation • (last lecture) Statements execution of: • skip and sequential composition • variable declaration • store manipulation • conditional CS2104, Lecture 4

  4. Overview • Computing with procedures • lexical scoping • closures • procedures as values • procedure call • Introduction of • properties of the abstract machine • last call optimization • full syntax to kernel syntax CS2104, Lecture 4

  5. Procedures • Defining procedures • how about external references? • when and which variables matter? • Calling procedures • what do the variables refer to? • how to pass parameters? • how about external references? • where to continue the execution? CS2104, Lecture 4

  6. Identifiers in Procedures P = proc {$ X Y} if X>Y then Z=1 else Z=0 end end • X and Y are called (formal) parameters • Z is called an external reference CS2104, Lecture 4

  7. Identifiers in Procedures proc {P X Y} if X>Y then Z=1 else Z=0 end end • X and Y are called (formal) parameters • Z is called an external reference • More familiar variant CS2104, Lecture 4

  8. Free and Bound Identifiers local Z in ifX>Ythen Z=1 else Z=0 end end • X and Y are free (variable) identifiers in this statement • Z is a bound (variable) identifier in this statement CS2104, Lecture 4

  9. Free and Bound Identifiers localZin if X>Y then Z=1 else Z=0 end end • X and Y are free variable identifiers in this statement (declared outside) • Z is a bound variable identifier in this statement (declared inside) Declaration Occurrence CS2104, Lecture 4

  10. Free and Bound Occurrences • An occurrence of X is bound, if it is inside the body of either local, proc or case. localXin …X… end proc {$ …X…}in …X… end case Y of f(X) then …X… end • An occurrence of X is free in a statement, if it is not a bound occurrence. CS2104, Lecture 4

  11. Free Identifiers and Free Occurrences • Free occurrences can only exist in incomplete program fragments, i.e., statements that cannot run. • In a running program, it is always true that every identifier occurrence is bound. That is, it is in closed-form. CS2104, Lecture 4

  12. Free Identifiers and Free Occurrences A1=15 A2=22 B=A1+A2 • The identifiers occurrences A1, A2, and B, are free. • This statement cannot be run. CS2104, Lecture 4

  13. Free Identifiers and Free Occurrences local A1 A2 in A1=15 A2=22 B=A1+A2 end • The identifier occurrences A1 and A2 are bound and the occurrence B is free. • This statement still cannot be run. CS2104, Lecture 4

  14. Free Identifiers and Free Occurrences local B in local A1 A2 in A1=15 A2=22 B=A1+A2 end {Browse B} end • This is in closed-form since it has no free identifier occurrences. • It can be executed! CS2104, Lecture 4

  15. Procedures proc {Max X Y ?Z} % "?" is just a comment if X>=Y then Z=X else Z=Y end end {Max 15 22 C} • When Max is called, the identifiers X, Y, and Z are bound to 15, 22, and the unbound variable referenced by C. • Can this code be executed? CS2104, Lecture 4

  16. Procedures • No, because Max and C are free identifiers! local Max C in proc {Max X Y ?Z} if X>=Y then Z=X else Z=Y end end {Max 15 22 C} {Browse C} end CS2104, Lecture 4

  17. Procedures with external references proc {LB X ?Z} if X>=Y then Z=X else Z=Y end end • The identifier Y is not one of the procedure arguments. • Where does Y come from? The value of Y is taken when the procedure is defined. • This is a consequence of static scoping. CS2104, Lecture 4

  18. Procedures with external references local Y LB in Y=10 proc {LB X ?Z} if X>=Y then Z=X else Z=Y end end local Y=3 Z1 in {LB 5 Z1} end end • Call {LB 5 Z} bind Z to 10. • Binding of Y=3 when LB is called is ignored. • Binding of Y=10 when the procedure is defined is important. CS2104, Lecture 4

  19. Lexical Scoping or Static Scoping • The meaning of an identifier like X is determined by the innermost local statement that declares X. • The area of the program where X keeps this meaning is called the scope of X. • We can find out the scope of an identifier by inspecting the text of the program. • This scoping rule is called lexical scoping or static scoping. CS2104, Lecture 4

  20. Lexical Scoping or Static Scoping local X in X=15 local X in X=20 {Browse X} end {Browse X} end E1 ={Xx1} E2={Xx2} • There is just one identifier, X, but at different points during the execution, it refers to different variables (x1and x2). CS2104, Lecture 4

  21. Lexical Scoping localZin Z=1 proc {P X Y} Y=X+Zend end • A procedure value is often called a closure because it contains an environment as well as a procedure definition. CS2104, Lecture 4

  22. Dynamic versus Static Scoping • Static scope. • The variable corresponding to an identifier occurrence is the one defined in the textually innermost declaration surrounding the occurrence in the source program. • Dynamic scope. • The variable corresponding to an identifier occurrence is the one in the most-recent declaration seen during the execution leading up to the current statement. CS2104, Lecture 4

  23. Dynamic scoping versus static scoping local P Q in proc {Q X} {Browse stat(X)} end proc {P X} {Q X} end local Q in proc {Q X} {Browse dyn(X)} end {P hello} end end • What should this display, stat(hello) or dyn(hello)? • Static scoping says that it will display stat(hello), because P uses the version of Q that exists at P’s definition. CS2104, Lecture 4

  24. Contextual Environment • When defining procedure, construct contextual environment • maps all external references… • …to values at the time of definition • Procedure definition creates a closure • pair of procedure and contextual environment • this closure is written to store CS2104, Lecture 4

  25. Example of Contextual Environment • The first part (red colour) • {Inc X} will be bound to the value X+1 because Z is bound to 1 • Display: 3 • The second part (blue colour) • {Inc X} is still bound to the value X+1 • Display: 3 local Inc in local Z = 1 in proc {Inc X Y} Y = X + Z end local Y in {Inc 2 Y} {Browse Y} end end local Z = 2 in local Y in {Inc 2 Y} {Browse Y} end end end CS2104, Lecture 4

  26. Environment Projection (last lecture) • Given: Environment E E | {x1, …, xn} is new environment E’ where only mappings for {x1, …, xn} are retained from E CS2104, Lecture 4

  27. Procedure Declaration • Semantic statement is (proc {x y1 … yn} s end, E) • Formal parameters y1, …, yn • External references z1, …, zm • Contextual environment CE = E | {z1, …, zm} CS2104, Lecture 4

  28. Procedure Declaration • Semantic statement is (proc {x y1 … yn} s end, E) with E(x) = x • Create procedure value (closure or lexically scoped closure) (proc {$ y1 … yn} s end, E | {z1, …, zm}) in the store and bind it to x CS2104, Lecture 4

  29. Procedure Call • Values for • external references • actual parameters (actual arguments) must be available to called procedure (“callee”) • As usual: construct new environment • start from contextual environment for external references • adjoin actual parameters CS2104, Lecture 4

  30. Procedure Call • Semantic statement is ({x y1 … yn}, E) where • E(x) is to be called • y1, …, yn are actual parameters (arguments) • E(y1), …, E(yn) are actual entities (values) • Activation condition • E(x) is determined CS2104, Lecture 4

  31. Procedure Call • Semantic statement is ({x y1 … yn}, E) • If the activation condition is false, then • suspend the execution • If E(x) is not a procedure value, then • raise an error • If E(x) is a procedure value, but with different number of arguments (≠ n), then • raise an error CS2104, Lecture 4

  32. Procedure Call • If semantic statement is ({x y1 … yn}, E) with E(x) = (proc {$ w1…wn} s end, CE) • then push (s, CE + {w1E(y1), …, wnE(yn)}) CS2104, Lecture 4

  33. Executing a Procedure Call • If the activation condition “E(x) is determined” is true • if E(x) matches ({x y1 … yn}, E) (proc {$ w1…wn} s end, CE) (s, CE + {w1E(y1), …, wnE(yn)})   + + ST ST CS2104, Lecture 4

  34. Summary so far • Procedure values • go to store • combine procedure body and contextual environment • contextual environment defines external references • contextual environment is defined by lexical scoping • Procedure call • checks for the right type • passes arguments by environments • contextual environment for external references CS2104, Lecture 4

  35. Simple Example local P in local Y in local Z in Z=1 proc {P X} Y=X end {P Z} end end end • We shall reason that X, Y and Z will be bound to 1 CS2104, Lecture 4

  36. Simple Example local P Y Z in Z=1 proc {P X} Y=X end {P Z} end • We will cheat: do all declarations in one step • real implementations cheat as well… • Exercise: Define the execution of a local statement that introduces multiple variables simultaneously CS2104, Lecture 4

  37. Simple Example ([(local P Y Z in Z=1 proc {P X} Y=X end {P Z} end, )], ) • Initial execution state CS2104, Lecture 4

  38. Simple Example ([(local P Y Z in Z=1 proc {P X} Y=X end {P Z} end, )], ) • Statement CS2104, Lecture 4

  39. Simple Example ([(local P Y Z in Z=1 proc {P X} Y=X end {P Z} end, )], ) • Empty environment CS2104, Lecture 4

  40. Simple Example ([(local P Y Z in Z=1 proc {P X} Y=X end {P Z} end, )], ) • Semantic statement CS2104, Lecture 4

  41. Simple Example ([(local P Y Z in Z=1 proc {P X} Y=X end {P Z} end, )], ) • Semantic stack CS2104, Lecture 4

  42. Simple Example ([(local P Y Z in Z=1 proc {P X} Y=X end {P Z} end, )], ) • Empty store CS2104, Lecture 4

  43. Simple Example: local ([(local P Y Z in Z=1 proc {P X} Y=X end {P Z} end, )], ) • Create new store variables • Extend the environment CS2104, Lecture 4

  44. Simple Example ([(Z=1 proc {P X} Y=X end {P Z}, {Pp, Yy, Zz})], {p, y, z}) CS2104, Lecture 4

  45. Simple Example ([(Z=1 proc {P X} Y=X end {P Z}, {Pp, Yy, Zz})], {p, y, z}) • Split sequential composition CS2104, Lecture 4

  46. Simple Example ([(Z=1, {Pp, Yy, Zz}), (proc {P X} Y=X end {P Z}, {Pp, Yy, Zz})], {p, y, z}) • Split sequential composition CS2104, Lecture 4

  47. Simple Example ([(proc {P X} Y=X end {P Z}, {Pp, Yy, Zz})], {p, y, z=1}) • Variable-value assignment CS2104, Lecture 4

  48. Simple Example ([(proc {P X} Y=X end, {Pp, Yy, Zz}), ({P Z}, {Pp, Yy, Zz})], {p, y, z=1}) • Split sequential composition CS2104, Lecture 4

  49. Simple Example ([(proc {P X} Y=Xend, {Pp, Yy, Zz}), ({P Z}, {Pp, Yy, Zz})], {p, y, z=1}) • Procedure definition • external reference Y • formal argument X • Contextual environment {Yy} • Write procedure value to store CS2104, Lecture 4

  50. Simple Example ([({P Z}, {Pp, Yy, Zz})], { p = (proc {$ X} Y=Xend, {Yy}), y, z=1}) • Procedure call: use p • Note: p is a value like any other variable. It is the semantic statement (proc {$ X} Y=Xend, {Yy}) • Environment • start from {Y y} • adjoin {X z} CS2104, Lecture 4

More Related