Title: Declarative Computation Model Memory management, Exceptions, From kernel to practical language VRH 2
1Declarative Computation ModelMemory management,
Exceptions, From kernel to practical language
(VRH 2.5-2.7)
- Carlos Varela
- RPI
- Adapted with permission from
- Seif Haridi
- KTH
- Peter Van Roy
- UCL
2From the kernel languageto a practical language
- Interactive interface
- the declare statement and the global environment
- Extend kernel syntax to give a full, practical
syntax - nesting of partial values
- implicit variable initialization
- expressions
- nesting the if and case statements
- andthen and orelse operations
- Linguistic abstraction
- functions
3The interactive interface (declare)
- The interactive interface is a program that has a
single global environment - declare X Y
- Augments (and overrides) the environment with new
mappings for X and Y - Browse X
- Inspects the store and shows partial values, and
incremental changes
4The interactive interface (declare)
procedurevalue
Store
Browse
R
value
X
Y
a
b
Environment
5declare X Y
procedurevalue
Store
Browse
F
value
X
a
Y
b
Environment
xi
unbound
unbound
xi1
6Syntactic extensions
- Nested partial values
- person(name George age25)
- local A B in A George B25 person(nameA
ageB) end - Implicit variable initialization
- local ?pattern? ?expression? in ?statement? end
- Exampleassume T has been defined, then
- local tree(keyA leftB rightC valueD) T in
?statement? end - is the same as
- local A B C D in T
tree(keyA leftB rightC valueD)
ltstatementgt end
7Extracting fields in local statement
- declare T
-
- T tree(keyseif age48 professionprofessor)
-
- local
- tree(keyA ...) T
- in
- ?statement?
- end
8Nested if and case statements
- Observe a pair notation is 1 2, is the tuple
(1 2) - case Xs Ysof nil Ys then ?s?1
- Xs nil then ?s?2 (XXr) (YYr) andthen
XltY then ?s?3 - else ?s?4 end
- Is translated intocase Xs of nil then ?s?1else
case Ys of nil then ?s?2 else case
Xs of XXr then case Ys of YYr then if
XltY then ?s?3 else ?s?4 end else ?s?4 end
else ?s?4 end endend
9Expressions
- An expression is a sequence of operations that
returns a value - A statement is a sequence of operations that does
not return a value. Its effect is on the store,
or outside of the system (e.g. read/write a file) - 1111 X1111
expression
statement
10Functions as linguistic abstraction
- F X1 ... Xn R
- R F X1 ... Xn
proc F X1 ... Xn R?statement?R
?expression? end
fun F X1 ... Xn?statement??expression? end
?statement?
?statement?
11Nesting in data structures
- Ys F XMap Xr F
- Is unnested to
- local Y Yr in Ys YYr F X Y Map Xr F
Yrend - The unnesting of the calls occurs after the data
structure
12Functional nesting
- Nested notations that allows expressions as well
as statements - local R in F X1 ... Xn R Q R ...end
- Is written as (equivalent to)
- Q F X1 ... Xn ...
expression
statement
13Conditional expressions
R if ?expr?1 then ?expr?2 else ?expr?3 end
if ?expr?1 thenR ?expr?2 else R ?expr?3 end
?expression?
?statement?
fun Max X Y if XgtY then X else Y end end
proc Max X Y R R ( if XgtY then X else
Y end ) end
14Example
fun Max X Y if XgtY then X else Y end end
proc Max X Y R R ( if XgtY then X else
Y end ) end
proc Max X Y R if XgtY then R X else R Y
end end
15andthen and orelse
if ?expr?1 then ?expr?2 else false end
?expr?1 andthen ?expr?2
if ?expr?1 then true else ?expr?2 end
?expr?1 orelse ?expr?2
16Function calls
Observe
local R1 R2 inR1 F2 XR2 F3 Y F1 R1
R2 end
F1 F2 X F3 Y
The arguments of a function are evaluatedfirst
from left to right
17A complete example
fun Map Xs Fcase Xsof nil then nil XXr
then F XMap Xr Fend end
proc Map Xs F Yscase Xsof nil then Ys
nil XXr then Y Yr in Ys YYr F X
Y Map Xr F Yrend end
18Back to semantics
- Efficient loops in the declarative model
- recursion used for looping
- is efficient because of last call optimization
- memory management and garbage collection
- Exceptions
- Functional programming
19Last call optimization
- Consider the following procedureproc Loop10
I if I 10 then skip else Browse
I Loop10 I1 endend - This procedure does not increase the size of the
STACK - It behaves like a looping construct
20Last call optimization
ST (Loop10 0, E0) ST (Browse I,
I?i0,...) (Loop10 I1, I?i0,...)
? i00, ... ST (Loop10 I1,
I?i0,...) ? i00, ... ST (Browse I,
I?i1,...) (Loop10 I1, I?i1,...)
? i00, i11,...
- proc Loop10 I if I 10 then
skip else Browse I Loop10 I1 endend
21Garbage collection
- proc Loop10 I if I 10 then
skip else Browse I Loop10 I1 endend
ST (Browse I, I?ik,...) (Loop10
I1, I?ik,...) ? i00, i11,...,
ik-ik-1, ikk,...
Garbage collection is an algorithm (a task) that
removes from memory (store) all cells that are
not accessible from the stack
ST (Browse I, I?ik,...) (Loop10
I1, I?ik,...) ? ikk, ...
22The memory use cycle
- Active memory is what the program needs to
continue execution (semantic stack reachable
part of store) - Memory that is no longer needed is of two kinds
- Can be immediately deallocated (i.e., semantic
stack) - Simply becomes inactive (i.e., store)
- Reclaiming inactive memory is the hardest part of
memory management - Garbage collection is automatic reclaiming
Allocate/ deallocate
Active
Free
Become inactive (program execution)
Reclaim (garbage collection)
Inactive
23The Mozart Garbage Collector
- Copying dual-space algorithm
- Advantage Execution time is proportional to the
active memory size - Disadvantage Half of the total memory is
unusable at any given time
24Exceptions
- How to handle exceptional situations in the
program? - Examples
- divide by 0
- opening a nonexistent file
- Some errors are programming errors
- Some errors are imposed by the external
environment - Exception handling statements allow programs to
handle and recover from errors
25Exceptions
- Exception handling statements allow programs to
handle and recover from errors - The error confinement principle
- Define your program as a structured layers of
components - Errors are visible only internally and a recovery
procedure corrects the errors either errors are
not visible at the component boundary or are
reported (nicely) to a higher level - In one operation, exit from arbitrary depth of
nested contexts - Essential for program structuring else programs
get complicated (use boolean variables
everywhere, etc.)
26Basic concepts
- A program that encounters an error (exception)
should transfer execution to another part, the
exception handler and give it a (partial) value
that describes the error - try ?s?1 catch ?x? then ?s?2 end
- raise ?x? end
- Introduce an exception marker on the semantic
stack - The execution is equivalent to ?s?1 if it
executes without raising an error - Otherwise, ?s?1 is aborted and the stack is
popped up to the marker, the error value is
transferred through ?x?, and ?s?2 is executed
27Exceptions (Example)
- fun Eval E if IsNumber E then Eelse case
E of plus(X Y) then Eval XEval Y
times(X Y) then Eval XEval Y else raise
illFormedExpression(E) end endend - end
plus
times
5
6
4
28Exceptions (Example)
- try Browse Eval plus(5 6) Browse Eval
plus(times(5 5) 6) Browse Eval plus(minus(5
5) 6) - catch illFormedExpression(E) thenSystem.showInfo
illegal expresion E - end
29Try semantics
- The semantic statement is (try ?s?1 catch ?y?
then ?s?2 end, E) - Push the semantic statement (catch ?y? then ?s?2
end, E) on ST - Push (?s?1 , E) on ST
- Continue to next execution step
30Raise semantics
- The semantic statement is (raise ?x? end, E)
- Pop elements off ST looking for a catch
statement - If a catch statement is found, pop it from the
stack - If the stack is emptied and no catch is found,
then stop execution with the error message
Uncaught exception - Let (catch ?y? then ?s? end, Ec) be the catch
statement that is found - Push (?s? , Ecltygt?E(ltxgt)) on ST
- Continue to next execution step
31Catch semantics
- The semantic statement is (catch ?x? then ?s?
end, E) - Continue to next execution step (like skip)
32Full exception syntax
- Exception statements (expressions) with multiple
patterns and finally clause - ExampleFH OpenFile xxxxxtry Process
File FHcatch X then System.showInfo
Exception when processing Xfinally
CloseFile FH end
33Strictly functional
- Restrict the language to make the language
functional (I.e.without dataflow variables) - Language similar to Scheme (dynamically typed
functional language) - This is done by disallowing variable declaration
(without initialization) and disallowing
procedural syntax - Only use implicit variable initialization
- Only use functions
34Exercises
- What do you expect to happen if you try to
execute the following statement? Try to answer
without actually executing it! - local T tree(keyA leftB rightC valueD) in
- A 1
- B 2
- C 3
- D 4
- end
- The C language has a facility to declare static
variables in a function. Read about them if you
dont know what they are. Now, conceptually
describe How can you add a similar facility to
Oz? Will you add it as an extension of the
kernel language or as a linguistic abstraction?
(You need not follow the same syntax as C. Your
semantics should be approximately the same)
35Exercises
- Any realistic computer system has a memory cache
for fast access to frequently used data. Read
about caches if you dont know what they are. Can
you think of any issues with garbage collection
in a system that has a memory cache? - Do problems 3, 9 and 12 in Section 2.9
- Read Sections 3.7 and 6.4