11/23/2015cs2104, lecture 41 programming language concepts, cosc-3308-01 lecture 4 procedures, last...
TRANSCRIPT
![Page 1: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/1.jpg)
04/21/23 CS2104, Lecture 4 1
Programming Language Concepts, COSC-3308-01Lecture 4
Procedures, last call optimization
![Page 2: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/2.jpg)
04/21/23 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
![Page 3: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/3.jpg)
04/21/23 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
![Page 4: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/4.jpg)
04/21/23 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
![Page 5: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/5.jpg)
04/21/23 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?
![Page 6: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/6.jpg)
04/21/23 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
![Page 7: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/7.jpg)
04/21/23 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
![Page 8: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/8.jpg)
04/21/23 CS2104, Lecture 4 8
Free and Bound Identifiers
local Z in
if X>Y then 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
![Page 9: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/9.jpg)
04/21/23 CS2104, Lecture 4 9
Free and Bound Identifiers
local Z in
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
![Page 10: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/10.jpg)
04/21/23 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.
local X in …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.
![Page 11: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/11.jpg)
04/21/23 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.
![Page 12: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/12.jpg)
04/21/23 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.
![Page 13: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/13.jpg)
04/21/23 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.
![Page 14: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/14.jpg)
04/21/23 CS2104, Lecture 4 14
Free Identifiers and Free Occurrenceslocal 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!
![Page 15: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/15.jpg)
04/21/23 CS2104, Lecture 4 15
Procedures
proc {Max X Y ?Z} % "?" is just a comment
if X>=Y then Z=X else Z=Y endend{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?
![Page 16: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/16.jpg)
04/21/23 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
![Page 17: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/17.jpg)
04/21/23 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.
![Page 18: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/18.jpg)
04/21/23 CS2104, Lecture 4 18
Procedures with external referenceslocal 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} endend 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.
![Page 19: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/19.jpg)
04/21/23 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.
![Page 20: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/20.jpg)
04/21/23 CS2104, Lecture 4 20
Lexical Scoping or Static Scopinglocal X in
X=15
local X in
X=20
{Browse X}
end
{Browse X}
end
There is just one identifier, X, but at different points during the execution, it refers to different variables (x1
and x2).
E2={Xx2}E1 ={Xx1}
![Page 21: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/21.jpg)
04/21/23 CS2104, Lecture 4 21
Lexical Scoping
local Z in
Z=1
proc {P X Y} Y=X+Z end
end A procedure value is often called a closure
because it contains an environment as well as a procedure definition.
![Page 22: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/22.jpg)
04/21/23 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.
![Page 23: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/23.jpg)
04/21/23 CS2104, Lecture 4 23
Dynamic scoping versus static scopinglocal 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} endend
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.
![Page 24: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/24.jpg)
04/21/23 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
![Page 25: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/25.jpg)
04/21/23 CS2104, Lecture 4 25
Example of Contextual Environmentlocal 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 endend
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
![Page 26: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/26.jpg)
04/21/23 CS2104, Lecture 4 26
Environment Projection (last lecture) Given: Environment E
E | {x1, …, xn}
is new environment E’ where only mappings for {x1, …, xn} are retained from E
![Page 27: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/27.jpg)
04/21/23 CS2104, Lecture 4 27
Procedure Declaration
Semantic statement is
(proc {x y1 … yn} s end, E)
Formal parameters y1, …, yn
External references z1, …, zm
Contextual environment
CE = E | {z1, …, zm}
![Page 28: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/28.jpg)
04/21/23 CS2104, Lecture 4 28
Procedure Declaration
Semantic statement is
(proc {x y1 … yn} s end, E)
with E(x) = x
Create procedure value (closure or lexically scoped closure)
(proc {$ y1 … yn} s end,
E | {z1, …, zm})
in the store and bind it to x
![Page 29: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/29.jpg)
04/21/23 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
![Page 30: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/30.jpg)
04/21/23 CS2104, Lecture 4 30
Procedure Call Semantic statement is
({x y1 … yn}, E)
where E(x) is to be called y1, …, yn are actual parameters (arguments) E(y1), …, E(yn) are actual entities (values)
Activation condition E(x) is determined
![Page 31: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/31.jpg)
04/21/23 CS2104, Lecture 4 31
Procedure Call Semantic statement is
({x y1 … yn}, 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
![Page 32: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/32.jpg)
04/21/23 CS2104, Lecture 4 32
Procedure Call
If semantic statement is
({x y1 … yn}, E)
with
E(x) = (proc {$ w1…wn} s end, CE)
then push
(s, CE + {w1E(y1), …, wnE(yn)})
![Page 33: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/33.jpg)
04/21/23 CS2104, Lecture 4 33
Executing a Procedure Call
(proc {$ w1…wn} s end, CE)
ST
+ +
If the activation condition “E(x) is determined” is true if E(x) matches ({x y1 … yn}, E)
(s, CE + {w1E(y1),
…, wnE(yn)})
ST
![Page 34: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/34.jpg)
04/21/23 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
![Page 35: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/35.jpg)
04/21/23 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
![Page 36: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/36.jpg)
04/21/23 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
![Page 37: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/37.jpg)
04/21/23 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
![Page 38: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/38.jpg)
04/21/23 CS2104, Lecture 4 38
Simple Example
([(local P Y Z in Z=1
proc {P X} Y=X end
{P Z}
end, )],
)
Statement
![Page 39: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/39.jpg)
04/21/23 CS2104, Lecture 4 39
Simple Example
([(local P Y Z in Z=1
proc {P X} Y=X end
{P Z}
end, )],
)
Empty environment
![Page 40: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/40.jpg)
04/21/23 CS2104, Lecture 4 40
Simple Example
([(local P Y Z in Z=1
proc {P X} Y=X end
{P Z}
end, )],
)
Semantic statement
![Page 41: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/41.jpg)
04/21/23 CS2104, Lecture 4 41
Simple Example
([(local P Y Z in Z=1
proc {P X} Y=X end
{P Z}
end, )],
)
Semantic stack
![Page 42: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/42.jpg)
04/21/23 CS2104, Lecture 4 42
Simple Example
([(local P Y Z in Z=1
proc {P X} Y=X end
{P Z}
end, )],
)
Empty store
![Page 43: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/43.jpg)
04/21/23 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
![Page 44: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/44.jpg)
04/21/23 CS2104, Lecture 4 44
Simple Example
([(Z=1 proc {P X} Y=X end
{P Z}, {Pp, Yy, Zz})],
{p, y, z})
![Page 45: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/45.jpg)
04/21/23 CS2104, Lecture 4 45
Simple Example
([(Z=1 proc {P X} Y=X end
{P Z}, {Pp, Yy, Zz})],
{p, y, z})
Split sequential composition
![Page 46: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/46.jpg)
04/21/23 CS2104, Lecture 4 46
Simple Example
([(Z=1, {Pp, Yy, Zz}),
(proc {P X} Y=X end {P Z}, {Pp, Yy, Zz})],
{p, y, z})
Split sequential composition
![Page 47: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/47.jpg)
04/21/23 CS2104, Lecture 4 47
Simple Example
([(proc {P X} Y=X end {P Z}, {Pp, Yy, Zz})],
{p, y, z=1})
Variable-value assignment
![Page 48: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/48.jpg)
04/21/23 CS2104, Lecture 4 48
Simple Example
([(proc {P X} Y=X end, {Pp, Yy, Zz}),
({P Z}, {Pp, Yy, Zz})],
{p, y, z=1})
Split sequential composition
![Page 49: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/49.jpg)
04/21/23 CS2104, Lecture 4 49
Simple Example
([(proc {P X} Y=X end, {Pp, Yy, Zz}),
({P Z}, {Pp, Yy, Zz})],
{p, y, z=1})
Procedure definition external reference Y formal argument X
Contextual environment {Yy} Write procedure value to store
![Page 50: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/50.jpg)
04/21/23 CS2104, Lecture 4 50
Simple Example
([({P Z}, {Pp, Yy, Zz})], { p = (proc {$ X} Y=X end, {Yy}),
y, z=1})
Procedure call: use p Note: p is a value like any other variable. It is the
semantic statement (proc {$ X} Y=X end, {Yy}) Environment
start from {Y y} adjoin {X z}
![Page 51: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/51.jpg)
04/21/23 CS2104, Lecture 4 51
Simple Example
([(Y=X, {Yy, Xz})],
{ p = (proc {$ X} Y=X end, {Yy}), y, z=1})
Variable-variable assignment Variable for Y is y Variable for X is z
![Page 52: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/52.jpg)
04/21/23 CS2104, Lecture 4 52
Simple Example
([],
{ p = (proc {$ X} Y=X end, {Yy}), y=1, z=1})
Voila! The semantic stack is in the run-time state
terminated, since the stack is empty
![Page 53: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/53.jpg)
04/21/23 CS2104, Lecture 4 53
Discussion
Procedures take the values upon definition Application automatically restores them Not possible in Java, C, C++
procedure/function/method just code environment is lacking Java needs an object to do this one of the most powerful concepts in computer
science pioneered in Algol 68
![Page 54: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/54.jpg)
04/21/23 CS2104, Lecture 4 54
Summary so far
Procedures are values as anything else! Will allow breathtaking programming
techniques With environments it is easy to understand
what is the value for an identifier
![Page 55: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/55.jpg)
04/21/23 CS2104, Lecture 4 55
Properties of theAbstract Machine
![Page 56: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/56.jpg)
04/21/23 CS2104, Lecture 4 56
What Can We Use the Abstract Machine for? Proving properties Understanding runtime Understanding memory requirements
Abstract Machine is a model for computation implementations will refine model
![Page 57: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/57.jpg)
04/21/23 CS2104, Lecture 4 57
Exploiting the Abstract Machine We can define runtime of a statement s
the number of execution steps to execute s
We can understand how much memory execution requires semantic statements on the semantic stack number of nodes in the store
What is really in the store?
![Page 58: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/58.jpg)
04/21/23 CS2104, Lecture 4 58
Exploiting the Abstract Machine We can proof:
local X in local Y in s end end
executes the same as
local Y in local X in s end end
![Page 59: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/59.jpg)
04/21/23 CS2104, Lecture 4 59
Garbage Collection
A store variable x is alive, iff a semantic statement refers to x (occurs in
environment), or there exists an alive store variable y and y is bound
to a data structure containing x
All data structures which are not alive can be safely removed by garbage collection happens from time to time
![Page 60: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/60.jpg)
04/21/23 CS2104, Lecture 4 60
Last Call Optimization
![Page 61: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/61.jpg)
04/21/23 CS2104, Lecture 4 61
How Does Recursion Work?
local P in
P = proc {$} {P} end
{P}
end
Program will run forever Contextual environment of P will map P to
procedure value
![Page 62: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/62.jpg)
04/21/23 CS2104, Lecture 4 62
![Page 63: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/63.jpg)
04/21/23 CS2104, Lecture 4 63
Recursion at Work
([(local P in P = proc {$} {P} end
{P}
end, )], ) Initial state
![Page 64: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/64.jpg)
04/21/23 CS2104, Lecture 4 64
Recursion at Work
([(P = proc {$} {P} end {P}, {Pp})], {p})
After execution of local
![Page 65: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/65.jpg)
04/21/23 CS2104, Lecture 4 65
Recursion at Work
([(P = proc {$} {P} end, {Pp}), ({P}, {Pp})], {p})
After execution of sequential composition
![Page 66: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/66.jpg)
04/21/23 CS2104, Lecture 4 66
Recursion at Work
([({P}, {Pp})], {p=(proc {$} {P} end, {Pp})})
After execution of procedure definition external reference of body of P: P contextual environment: {Pp}
![Page 67: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/67.jpg)
04/21/23 CS2104, Lecture 4 67
Recursion at Work
([({P}, {Pp})], {p=(proc {$} {P} end, {Pp})})
After execution of procedure definition external reference of body of P: P contextual environment: {Pp}
Environment creates self reference
![Page 68: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/68.jpg)
04/21/23 CS2104, Lecture 4 68
Recursion at Work
([({P}, {Pp})], {p=(proc {$} {P} end, {Pp})})
Will continue forever Stack will never grow! Runs in constant space
called iterative computation
![Page 69: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/69.jpg)
04/21/23 CS2104, Lecture 4 69
Another Spinning Program
local Q in
Q = proc {$} {Q} {Q} end
{Q}
end Program will run forever Contextual environment of Q will map Q to
procedure value
![Page 70: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/70.jpg)
04/21/23 CS2104, Lecture 4 70
Some Steps…
([({Q}, {Qq})], {q=(proc {$} {Q} {Q} end, {Qq})})
After execution of local sequential composition procedure definition
![Page 71: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/71.jpg)
04/21/23 CS2104, Lecture 4 71
Procedure Call (1)
([({Q}, {Qq}), ({Q}, {Qq})], {q=(proc {$} {Q} {Q} end, {Qq})})
After execution of procedure call no arguments new environment is the same
contextual environment + argument environment {Qq} { }
![Page 72: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/72.jpg)
04/21/23 CS2104, Lecture 4 72
Procedure Call (2)
([({Q}, {Qq}), ({Q}, {Qq}), ({Q}, {Qq})], {q=(proc {$} {Q} {Q} end, {Qq})})
Stack grows with each step!
![Page 73: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/73.jpg)
04/21/23 CS2104, Lecture 4 73
Recursion: Summary
Iterative computations run in constant space
This concept is also called last call optimization no space needed for last call in procedure body
![Page 74: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/74.jpg)
04/21/23 CS2104, Lecture 4 74
Two Functions…
N+M =
fun {SADD N M} %% returns N+M for positive N %% Slow ADDition if N==0 then M else 1+{SADD N-1 M} endend
fun {FADD N M} %% returns N+M for positive N %% Fast ADDition if N==0 then M else {FADD N-1 M+1} endend
0)1(1
0
NifMN
NifM
![Page 75: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/75.jpg)
04/21/23 CS2104, Lecture 4 75
Questions
Which one is faster?
Which one uses less memory?
Why?
How?
![Page 76: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/76.jpg)
04/21/23 CS2104, Lecture 4 76
Answers…
Transform to kernel language
See how they compute
Answer the questions
![Page 77: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/77.jpg)
04/21/23 CS2104, Lecture 4 77
Procedure: SADD
proc {SADD N M NM} if N==0 then NM=M else local N1 in N1=N-1 local NM1 in {SADD N1 M NM1} NM=1+NM1 end end endend
![Page 78: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/78.jpg)
04/21/23 CS2104, Lecture 4 78
Procedure: SADDproc {SADD N M NM}
if N==0 then NM=M
else local N1 in
N1=N-1
local NM1 in
{SADD N1 M NM1}
NM=1+NM1
end
end
end
end
procedure call after recursive
call
![Page 79: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/79.jpg)
04/21/23 CS2104, Lecture 4 79
How Does SADD Compute?
local X Y Z in
X=4 Y=3
proc {SADD …} … end
{SADD X Y Z}
end
![Page 80: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/80.jpg)
04/21/23 CS2104, Lecture 4 80
Sketch for SADD
([({SADD X Y Z}, …)], …)
([({SADD N1 M NM1}, …),
(NM = 1 + NM1, …)], …)
([({SADD N1 M NM1},…),
(NM = 1 + NM1, …)
(NM = 1 + NM1, …)], …) …
![Page 81: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/81.jpg)
04/21/23 CS2104, Lecture 4 81
Procedure: FADD
proc {FADD N M NM} if N==0 then NM=M else local N1 in local M1 in N1=N-1 M1=M+1 {FADD N1 M1 NM} end end endend
![Page 82: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/82.jpg)
04/21/23 CS2104, Lecture 4 82
Procedure: FADD
proc {FADD N M NM} if N==0 then NM=M else local N1 in local M1 in N1=N-1 M1=M+1 {FADD N1 M1 NM} end end endend
recursive call is last call
![Page 83: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/83.jpg)
04/21/23 CS2104, Lecture 4 83
Sketch for FADD
([({FADD X Y Z}, …)], …)
([({FADD N1 M1 NM}, …)], …)
([({FADD N1 M1 NM}, …)], …)
([({FADD N1 M1 NM}, …)], …) …
![Page 84: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/84.jpg)
04/21/23 CS2104, Lecture 4 84
SADD versus FADD SADD uses stack space depending on its
argument
FADD uses constant stack space iterative computation thanks to last call optimization
Techniques for achieving iterative computations: accumulators (later)
![Page 85: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/85.jpg)
04/21/23 CS2104, Lecture 4 85
Summary so far: Most Important Concepts Single-assignment variables
partial values Abstract machine
a tool for understanding computations a model of computation based on environments supports last call optimization
Procedures with contextual environment
![Page 86: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/86.jpg)
04/21/23 CS2104, Lecture 4 86
Abstract Machine General approach to explain how a
programming language computes model for computation
Can serve as base for implementation pioneered by Prolog D.H.D. Warren,
1980’s many other languages including Oz recent: JVM (Java) SUN
CLR (C#, …) Microsoft
![Page 87: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/87.jpg)
04/21/23 CS2104, Lecture 4 87
Goal
Programming as an engineering/scientific discipline
An engineer can understand abstract machine apply programming techniques develop replicate approach with abstract machine
programs and techniques
![Page 88: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/88.jpg)
04/21/23 CS2104, Lecture 4 88
Summary 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
![Page 89: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/89.jpg)
04/21/23 CS2104, Lecture 4 89
Reading suggestions
Chapter 2, Sections 2.4, 2.5, 2.6, 2.7 from [van Roy,Haridi; 2004]
Exercises 2.9.4-2.9.12 from [van Roy,Haridi; 2004]
![Page 90: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/90.jpg)
04/21/23 CS2104, Lecture 4 90
Coming up next
Iteration, Recursion, Exceptions, Type Notation, and Design Methodology
Chapter 2, Sections 2.4, 2.5, 2.6, 2.7 from [van Roy,Haridi; 2004]
Chapter 3, Sections 3.2-3.9
![Page 91: 11/23/2015CS2104, Lecture 41 Programming Language Concepts, COSC-3308-01 Lecture 4 Procedures, last call optimization](https://reader034.vdocuments.net/reader034/viewer/2022051621/56649f395503460f94c56be9/html5/thumbnails/91.jpg)
04/21/23 CS2104, Lecture 4 91
Thank you for your attention!
Questions?