I. Kernel Language for the Declarative Computation Model A. motivation (2) What does programming involve? ------------------------------------------ PARTS OF PROGRAMMING ------------------------------------------ B. Defining languages (2.1) 1. how languages are defined ------------------------------------------ HOW LANGUAGES ARE DEFINED ------------------------------------------ 2. syntax ------------------------------------------ EXTENDED BACKUS-NAUR FORM (EBNF) Example ::= { } ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ------------------------------------------ 3. semantics (2.1.2) ------------------------------------------ SEMANTICS Goals: - simple - mathematical - reason about correctness - reason about efficiency Kernel Language Approach: [ Practical Language ] | | translation (desugaring) v [ Kernel Language ] E.g., fun {Sqr X} X * X end -translates-to-> proc {Sqr X Y} {Number.'*' X X Y} end ------------------------------------------ How to define the kernel language? What characteristics needed for kernel language? Why? Is C a kernel language? C++? a. linguistic abstractions What is a linguistic abstraction? ------------------------------------------ LINGUISTIC ABSTRACTIONS ------------------------------------------ What are some linguistic abstractions in C++ or Java? b. syntactic sugar What is a syntactic sugar? How is it different from a linguistic abstraction? ------------------------------------------ SYNTACTIC SUGARS ------------------------------------------ What are some syntactic sugars in C++ and Java? c. Examples ------------------------------------------ EXAMPLES OF DESUGARING LINGUISTIC ABSTRACTIONS AND SUGARS fun {Discrim A B C} fun {Square N} N*N end in {Square A} + {Square B} - 4.0*A*C end {Browse {Discrim 5.0 4.0 3.0}} --> Discrim = proc {$ A B C Res} local Square in Square = proc {$ N R} {Number.'*' N N R} end local A2 in local B2 in {Square A A2} {Square B B2} local A2B2 in {Number.'+' A2 B2 A2B2} local A4 in local Four in Four=4.0 {Number.'*' A Four A4} local A4C in {Number.'*' A4 C A4C} {Number.'-' A2B2 A4C Res} end end end end end end end end local Temp in local Five in local Four in local Three in Five=5.0 Four=4.0 Three=3.0 {Discrim Five Four Three Temp} {Browse Temp} end end end end ------------------------------------------ ------------------------------------------ FOR YOU TO DO Desugar the following: fun {Inc N} N+1 end {Browse {Inc 3}} ------------------------------------------ C. declarative computation model details (2.2-2.5) 1. data and the store ------------------------------------------ VALUE CREATION: WHAT IS SHOWN? local X Y Z in %% Value creation X=3 Y=oh#kay Z=zrec(z1:99 32) %% atoms vs. identifiers {Browse x} {Browse X} {Browse Y} {Browse Z} end ------------------------------------------ ------------------------------------------ FOR YOU TO DO % What's shown by the following? local D in D=drec(3 feature: atom) {Show rec(feature:3)} {Show D} end ------------------------------------------ 2. single-assignment store (2.2) ------------------------------------------ SINGLE-ASSIGNMENT STORE (2.2) Oz's = operation (tell) unifies the two sides making them identical if possible local X1 X2 X3 X4 X5 X6 in {Browse store(x1:X1 x2:X2 x3:X3 x4:X4 x5:X5 x6:X6)} {Delay 5000} X1=X2 {Delay 2000} X3=X2 {Delay 2000} X5=name(label:7) {Delay 2000} X6=name(label:X1) {Delay 2000} X1=X4 {Delay 2000} X5=X6 end ------------------------------------------ ------------------------------------------ FOR YOU TO DO % What's shown by the following? local Y1 Y2 Y3 Y4 Y5 Y6 Y7 in {Browse store(y1:Y1 y2:Y2 y3:Y3 y4:Y4 y5:Y5 y6:Y6 y7:Y7)} Y1=4020 Y2=Y3 Y4=aRecord(first:Y5 second: 1234) Y6=aRecord(second:Y7 first:999) Y4=Y6 Y4=Y2 end ------------------------------------------ How can we picture or model this? ------------------------------------------ NOTATION FOR SINGLE-ASSIGNMENT STORES Example 1: s = { x1, x2 = x3 } means s(x1) = {x1} // x1 is undetermined s(x2) = {x2, x3} s(x3) = {x2, x3} pictured as: x1 [ *-]--->[undetermined {x1}] x2 [ *-]--->[undetermined {x2, x3}] x3 [ *-]-/ Example 2: { x1 = 541, x2 = 3|4|nil, x3 = 3|4|nil, x4 } pictured as: x1 [541] x2 [ *-]--->[ 3|*-]->[ 4| *-]-> nil x3 [ *-]-- ??? (depends) x4 [ *-]--->[undetermined {x4}] ------------------------------------------ How would you implement this? What is a value store? a. environment (2.2.4-5) What does the following show? ------------------------------------------ ENVIRONMENT AND SCOPING EXAMPLES local X Y in X=4 Y=5 {Show 'first X = '#X} {Show 'first Y = '#Y} local X Y in X=11 Y=22 {Show 'inner X = '#X} {Show 'inner Y = '#Y} end {Show 'second X = '#X} {Show 'second Y = '#Y} end local X XVal Add AddPairs in X=4 XVal = proc {$ ?R} R=X end Add = fun {$ X Y} X+Y end fun {AddPairs X Y} case X of X1#X2 then case Y of Y1#Y2 then (X1+Y1)#(X2+Y2) end end end {Show '{XVal} = '#{XVal}} {Show '{Add X Y} = '#{Add 20 4000}} {Show '{AddPairs 50#21 40#20} = ' # {AddPairs 50#21 40#20}} end ------------------------------------------ ------------------------------------------ CASE AND SCOPING % What does the following do? local BadAddPairs in fun {BadAddPairs X Y} case X of X#Y then case Y of X#Y then (X+Y)#(X+Y) else canthappen end end end {Show '{BadAddPairs 50#21 40#20} = ' # {BadAddPairs 50#21 40#20}} end ------------------------------------------ How can we formally model these examples? ------------------------------------------ SUMMARY OF NOTATIONS AND MODEL Example: Y = 4020 Now have environment: E(Y) = x1 store: s(x1) = 4020 Pictured: E s Y [ *-]----> x1 [4020] Notation E = {Y --> x1} s = {x1 = 4020} ------------------------------------------ What is the value of Y? What is dereferencing? b. partial values (2.2.6) ------------------------------------------ PARTIAL VALUES (2.2.6) def: A *partial value* is a data structure that may contain undetermined locations. Example: local X Y in X = person(name:"George" age: Y) end What environment and store does this produce? ------------------------------------------ What happens after the binding Y = "25" is done? ------------------------------------------ COMPATABILITY OF VALUES Program identifiers can be unified with (other) identifiers, which makes them the same (or checks that they are, fails if can't) if they are compatible: X = Y Pictured: X [ *-]-----> x1 [ *-]---> [undetermined {x1, x2}] / Y [ *-]-----> x2 [ *-]-/ Whenever one value is determined, the other variable identifier also sees it. local X Y in X = Y Y = 25 {Browse X} end ------------------------------------------ c. dataflow execution (2.2.8) ------------------------------------------ DATAFLOW EXECUTION What if a location is used before it is determined? 1. Get garbage from memory 2. Get default value for type (0) 3. Give error message and stop 4. Illegal, complain at compile time 5. Wait until location's value is determined ------------------------------------------ Why do these? What languages do these? What does Oz do? 3. Kernel Language (2.3) a. Syntax (2.3.1) ------------------------------------------ KERNEL LANGUAGE SYNTAX (TABLES 2.1, 2.2) ::= skip | | local in end | = | = | if then else end | case of then else end | '{' {} '}' ::= ::= | | ::= | ::= | ( {:} ) ::= proc '{' $ {} '}' end ::= | ::= | | ::= true | false ------------------------------------------ Why would you include all of these in the language? Why leave out some things, e.g., = ? What is a case statement like in C, C++, or Java? why records? b. Values and types (2.3.2) ------------------------------------------ TYPES def: a *type* is a set of values with a set of operations Types in the Oz's declarative model Value Number Int Char Float Record Tuple Literal Bool Atom ... List String ... Procedure ... ... ------------------------------------------ What does v has type T mean? What does a subtype mean in this interpretation of types? What is dynamic typing? Why do it? i. Basic types (2.3.3) ii. Records (2.3.4-5) ------------------------------------------ RECORDS (2.3.4) Creation newrec(field1: Val1) Operations: ------------------------------------------ What's the operation for creating a record? What are the operations on records? What are these kinds of operations called in Java? What does == do for records? What is the difference between X = Y and X == Y ? iii. Procedures (2.3.4-5) ------------------------------------------ PROCEDURES Creation: proc { $ X Y } Y = X+X end Operations: ------------------------------------------ What are the operations on procedures? 4. Kernel Language Semantics (2.4) a. basic concepts (2.4.1) i. Static Scoping ------------------------------------------ DECLARATIONS def: In the declarative kernel, an identifier is declared by: 1. a local statement of form local in end e.g., Y is declared in: local Y in R=Y end 2. the identifiers in the of a case statments then where ::= ::= | ( {:} ) e.g., Y is declared in: case Z of foo(ab:Y) then R=Y else R=nil end 3. the formal parameter of a procedure value of form proc '{' $ {} '}' end e.g., Y and R are declared in: proc {$ Y ?R} R=Y end In each of the above, the *region* where such a declaration of may be referred to by uses of is the statement . ------------------------------------------ What other declaration sites are there in the sugared part of Oz? ------------------------------------------ DECLARATIONS IN SUGARED OZ SYNTAX local foo(ab: Y cd: Z) = Q in ... end case Q of foo(ab: Y) then ... [] bar(cd: Z) then... else ... end fun {Id X} X end fun {Head X|_} X end ------------------------------------------ ------------------------------------------ STATIC SCOPING def: In *static scoping*, each identifier, X, def: In *dynamic scoping*, each identifier, X, Example: local X F T in local X in X=2 F = proc {$ Y ?Z} Z=X+Y end end X=1 {F 10 T} {Browse T} end ------------------------------------------ What does the example give with each kind of scoping? ------------------------------------------ PICTURE WITH DYNAMIC SCOPING ------------------------------------------ In the example, does it matter if X=1 occurs after the declaration of F? What is the meaning of the procedure local X in X = 541 proc {$ Y Z} Z = X+Y end end with dynamic scoping? ii. Free and bound identifier occurrences (p. 64) ------------------------------------------ FREE AND BOUND IDENTIFIER USES def: an identifier *occurs free* in a statement iff def: an identifier *occurs bound* in a statment iff contains a use of that refers to a declaration of within . {proc {$ X ?Y} Y=X end F1 Z} {{{proc {$ X ?Y} Y=proc {$ X ?Y} Y={X F} end end} F Z} F1 Z1} ------------------------------------------ How would you define free and bound identifier uses in expressions? in the first expression, what does X refer to? ------------------------------------------ EXAMPLES F, F1 occur free in: F1 {F F1} proc {$ B ?Res} Res=F end B, B1 occur bound in: proc {$ B ?Res} Res = B end proc {$ B1 ?R1} R1= proc {$ B ?R} R={B1 B} end end There can be a mix: {proc {$ B R} R=B end F} ^ ^ bound-/ \-free occurrence occurrence The same identifier can occur both ways: {fun {$ N} N end N} ^ ^ bound-/ \-free occurrence occurrence Identifiers that are free in a subexpression may be bound in a larger expression fun {$ F} {fun {$ B} {B F1} end F} end Identifiers must be used to be bound proc {$ N R} proc {$ N R2} R2=3 end end fun {$ N} 3 end ------------------------------------------ So if N occurs free in an expression, does that mean it doesn't occur bound? ------------------------------------------ FOR YOU TO DO What are the (a) free, and (b) bound identifiers in ... fun {$ X} fun {$ Y} X end end {G {Tail X}} fun {$ X} {G {Tail X}} end fun {$ G} fun {$ X} {G {Tail X}} end end ------------------------------------------ What's the difference between an identifier being bound in an expression and a location being bound in the store? Can an identifier that is free in an expression refer to a location that has a determined value in the store? ------------------------------------------