ii. Free and bound identifier occurrences (p. 64) ------------------------------------------ FREE AND BOUND IDENTIFIER USES def: an identifier *occurs free* in a statement iff does not contain a use of that refers to a declaration of within def: an identifier *occurs bound* in a statment iff contains a use of that refers to a declaration of within FV({proc {$ X ?Y} Y=X end F1 Z}) = {F1, Z} BV({proc {$ X ?Y} Y=X end F1 Z}) = {X, Y} FV({{{proc {$ X ?Y} Y=proc {$ X ?Y} Y={X F} end end} F Z} F1 Z1}) = {F, Z, F1, Z1} BV({{{proc {$ X ?Y} Y=proc {$ X ?Y} Y={X F} end end} F Z} F1 Z1}) = {Y,X} ------------------------------------------ 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 FV( proc {$ N R} proc {$ N R2} R2=3 end end ) = {} BV( proc {$ N R} proc {$ N R2} R2=3 end end ) = { R2 } ------------------------------------------ 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 free: none bound: X {G {Tail X}} free: X, G, Tail bound: none fun {$ X} {G {Tail X}} end free: G, Tail bound: X fun {$ G} fun {$ X} {G {Tail X}} end end free: Tail bound: G, X ------------------------------------------ What's the difference between an identifier being bound in an expression and a location being bound in the store? - bound in an expression is a static property, governed by declarations. - bound (determined) in the store is a dynamic property, governed by the program's execution. Can an identifier that is free in an expression refer to a location that has a determined value in the store? ------------------------------------------ FORMAL DEFINITIONS FOR THE KERNEL % FV() "the set of free identifiers in " FV(skip) = {} FV( ) = FV() U FV() FV(local in end) = FV() - {} FV( = ) = {, } FV( = ) = {} U FVE() FV(if then else end) = {x} U FV() U FV() FV(case of then else end) = {} U FVE() U (FV() - FVE()) U FV() % FVE() "the set of free identifiers in " FVE() = {} FVE() = {} FVE((: ... )) = {, ..., } FVE(proc {$ ... } end) = FV() - {, ..., } % BV() "the set of bound identifiers in " BV(skip) = {} BV( ) = BV() U BV() BV(local in end) = BV() U (FV() intersect {}) e.g., BV(local Y in skip end) = {} e.g., BV(local Y in R=Y end) = {} U ({R,Y} intersect {Y}) = {Y} BV( = ) = {} BV( = ) = BVE() BV(if then else end) = BV() U BV() BV(case of then else end) = BV() U (FV() intersect FVE()) U BV() BV(case Q of '|'(1:H 2:T) then '|'(1:3 2:T) else H end) = {} U ({T} intersect {H, T}) U {} = {T} % BVE() "the set of bound identifiers in " BVE() = {} BVE() = {} BVE((: ... )) = {} BVE(proc {$ ... } end) = BV() U (FV() intersect {, ..., }) BVE(proc {$ Y R} R=Y end) = {} U ({R, Y} intersect {Y,R}) = {Y,R} BVE(proc {$ Y R} R=R end) = {} U ({R} intersect {Y,R}) = {R} BVE(proc {$ Z} Z=3 end) = {} U ({Z} intersect {Z}) = {Z} BVE(proc {$ Y R} proc {$ Z} Z=3 end end) = {Z} U {} = {Z} ------------------------------------------ How would you generalize these to more complex expressions?