Scope, Functions, nd Storge Mngement Block-structured lnguges nd stck storge In-le Blocks (previous set of overheds) ctivtion records storge for locl, glol vriles First-order functions (previous set of overheds) prmeter pssg til recursion nd itertion Higher-order functions devitions from stck disciple lnguge expressiveness => implementtion complexity Higher-order functions Lnguge fetures Functions pssed s rguments Functions tht return functions from nested locks Need to mt environment of function Simpler cse Function pssed s rgument Need poter to ctivtion record higher up stck More complicted second cse Function returned s result of function cll Need to keep ctivtion record of returng function L. Dillon, CSE 452, Fll 2008 1 L. Dillon, CSE 452, Fll 2008 2 Pss function s rgument Sttic scope for function rguments vl x = ; vl x= Hello, world There re two declrtions for x. Which one is to e used for ech occurrence of x? vl x = ; : vl x= Hello, world H.. ()?? (To reduce clutter, I m not showg CLs ARs; when omitted, you my ssume CL references the AR directly ove.) L. Dillon, CSE 452, Fll 2008 3 How is the ccess lk for h set up? It needs to reference the AR declrg the ctul rgument But the ctul rgument cn e different for different clls to g Cnnot determe stticlly how to fd the declrg AR L. Dillon, CSE 452, Fll 2008 4
Solution: Closures Exmple: function rgument Represent function vlue y closure, consistg of pir <l, i> where l pots to the AR declrg the function i.e. the glol referencg environment needed when the function is clled i pots to the first struction of the function code i.e., the structions to execute when the function is clled When function represented y closure is clled, Allocte new ctivtion record for the cll ( the usul mnner) Set the ccess lk the new ctivtion record to the poter l from the closure: new AR := l L. Dillon, CSE 452, Fll 2008 5 : vl x = ; vl x= Hello, world () To itilize function, set it to the closure formed us L. Dillon, CSE 452, Fll 2008 6 the current vlue of the when the function is clled this AR will e used to estlish the clled function s glol referencg environment the loction of the function code (the function code is not -le with the code for the lock) Exmple: function rgument Exmple: function rgument : vl x = ; vl x= Hello, world () : vl x = ; vl x= Hello, world () To itilize function, set it to the closure formed us the current vlue of the when the function is clled this AR will e used to estlish the clled function s glol referencg environment the loction of the function code (the function code is not -le with the code for the lock) L. Dillon, CSE 452, Fll 2008 7 To itilize function, set it to the closure formed us the current vlue of the when the function is clled this AR will e used to estlish the clled function s glol referencg environment the loction of the function code (the function code is not -le with the code for the lock) L. Dillon, CSE 452, Fll 2008 8
Exmple: function rgument Exmple: function rgument : vl x = ; vl x= Hello, world () L. Dillon, CSE 452, Fll 2008 9 Initilize the AL the AR for the cll to g To itilize function, set it to the closure formed us use the glol environment lk (first component) of the vlue ound to g. Pss the the current functionl vlue of the rgument when the function is clled this AR will e forml used prmeter to estlish := the the ctul clled rgument function s glol referencg environment In the thexmple: loction of the function code (the function code is not -le with how the re code the ARs for the with lock) the closures for f & g found? : vl x = ; vl x= Hello, world () L. Dillon, CSE 452, Fll 2008 10 To itilize Trnsfer control function, to gset it to the closure formed us the current Set EP vlue to reference of the the when se of the the function new ARis clled this AR will e used Set to estlish PC to first the struction clled function s code for glol g referencg environment the loction use the of IP the (second function component) code (the function of the closure code is ound not -le to g with the code for the lock) Exmple: function rgument Exmple: function rgument : vl x = ; vl x= Hello, world () : vl x = ; vl x= Hello, world () To itilize function, set it to the closure formed us Initilize the current the AL vlue of the the AR for when the the cll function to h is clled this AR will e use used the to glol estlish environment the clled lk function s (first component) glol referencg of the closure environment ound to h. the loction of the function code (the function code is not -le with the code for the lock) L. Dillon, CSE 452, Fll 2008 11 To itilize Trnsfer control function, to hset it to the closure formed us the current Set EP vlue to reference of the the when se of the the function new ARis clled this AR will e used Set to estlish PC to first the struction clled function s code for glol h referencg environment the loction use the of IP the of function the closure code ound (the function to h code is not -le with the code for the lock) L. Dillon, CSE 452, Fll 2008 12
Exmple: function rgument Summr Function rguments : vl x = ; vl x= Hello, world () L. Dillon, CSE 452, Fll 2008 13 To itilize function, set it to the closure formed us thehow current re vlue the of the ARs contg when the function the is dgs clled this AR will e used to estlish the clled function s glol referencg environment for y nd x found? the loction of the function code (the function code is not -le with the code for the lock) To represent function, use closure contg A lk to the glol referencg environment which to evlute the function ody A lk to the function code When function is clled AL new AR := the lk to the glol referencg environment When redy to trnsfer control: PC := the lk to the function code All ALs pot up stck (if stck grows down) My skip over ARs to fd glol vriles My still dellocte ARs usg stck (lifo) order L. Dillon, CSE 452, Fll 2008 14 Hrder cse: return function s result Lnguge feture Functions tht return new functions Need to mt environment of function Exmple fun compose(f,g) = (fn x => g(f x)); Function creted dynmiclly Vlues of free vriles the function ody re determed t run time function vlue is closure Exmple - dttype Trns = DEP of rel WTD of rel; - fun mk_cct_lncer(it: rel) = = = vl mt = ref it; = fun l (DEP x) = (mt :=!mt + x;!mt) = l (WTD x) = (mt :=!mt - x;!mt) = = l = - vl lnce = mk_cct_lncer(100.00); vl lnce = fn : Trns -> rel - lnce (WTD 25.00); vl it = 75.0 : rel - - mp lnce [WTD 30.0, DEP 200.00, WTD 65.00]; vl it = [45.0,245.0,180.0] : rel list L. Dillon, CSE 452, Fll 2008 15 L. Dillon, CSE 452, Fll 2008 16
Exmple Exmple : fun mk_cct_lncer(it: rel) = vl mt = ref it; fun l (DEP x) = (mt :=!mt + x;!mt) l (WTD x) = (mt :=!mt - x;!mt) l vl lnce = mk_cct_lncer(100.00); lnce (WTD 25.00); lnce (DEP 10.00); lnce (DEP 15.00); fun mk_(it: rel) = : vl mt = ref it; fun l (DEP x) = (mt :=!mt + x;!mt) l (WTD x) = (mt :=!mt - x;!mt) l vl lce = mk_(100.00); lce (WTD 25.00); lce (DEP 10.00); lce (DEP 15.00); (mk_...) (lce...) mk_: lce: it: 100. mt: l: 25. L. Dillon, CSE 452, Fll 2008 17 L. Dillon, CSE 452, Fll 2008 18 Exmple Exmple fun mk_(it: rel) = : vl mt = ref it; fun l (DEP x) = (mt :=!mt + x;!mt) l (WTD x) = (mt :=!mt - x;!mt) l vl lce = mk_(100.00); lce (WTD 25.00); lce (DEP 10.00); (mk_...) (lce...) mk_: lce: it: 100. mt: l: 25. : fun mk_(it: rel) = vl mt = ref it; fun l (DEP x) = (mt :=!mt + x;!mt) l (WTD x) = (mt :=!mt - x;!mt) l vl lce = mk_(100.00); lce (WTD 25.00); lce (DEP 10.00); (mk_...) (lce...) mk_: lce: it: 100. mt: l: 25. lce (DEP 15.00); lce (DEP 15.00); L. Dillon, CSE 452, Fll 2008 19 L. Dillon, CSE 452, Fll 2008 20
Exmple Exmple fun mk_(it: rel) = : vl mt = ref it; fun l (DEP x) = (mt :=!mt + x;!mt) l (WTD x) = (mt :=!mt - x;!mt) l vl lce = mk_(100.00); lce (WTD 25.00); lce (DEP 10.00); lce (DEP 15.00); (mk_...) (lce...) mk_: lce: it: 100. mt: l: 25. 100.0 fun mk_(it: rel) = : vl mt = ref it; fun l (DEP x) = (mt :=!mt + x;!mt) l (WTD x) = (mt :=!mt - x;!mt) l vl lce = mk_(100.00); lce (WTD 25.00); lce (DEP 10.00); lce (DEP 15.00); (mk_...) (lce...) mk_: lce: it: 100. mt: l: 25. 100.0 Wht would normlly hppen next nd wht is the next sttement to execute? Why cn t we use the norml procedure this sitution? L. Dillon, CSE 452, Fll 2008 21 L. Dillon, CSE 452, Fll 2008 22 Exmple Exmple : fun mk_(it: rel) = vl mt = ref it; fun l (DEP x) = (mt :=!mt + x;!mt) l (WTD x) = (mt :=!mt - x;!mt) l vl lce = mk_(100.00); lce (WTD 25.00); lce (DEP 10.00); lce (DEP 15.00); (mk_...) (lce...) mk_: lce: it: 100. mt: l: 25. 100.0 How re the ARs declrg x nd mt found? Wht ARs re on the sttic ch? the dynmic ch? Wht is the effect of executg the next two expressions? fun mk_(it: rel) = : vl mt = ref it; fun l (DEP x) = (mt :=!mt + x;!mt) l (WTD x) = (mt :=!mt - x;!mt) l vl lce = mk_(100.00); lce (WTD 25.00); lce (DEP 10.00); lce (DEP 15.00); (mk_...) (lce...) mk_: lce: it: 100. mt: l: 25. You should fish up this exmple... 100.0 75.0 L. Dillon, CSE 452, Fll 2008 23 L. Dillon, CSE 452, Fll 2008 24
Closures We progrmmg Useful for event hndlers We progrmm function AppendButton(conter, nme, messge) { vr tn = document.creteelement(ʻuttonʼ); tn.nerhtml = nme; tn.onclick = function (evt) { lert(messge); } conter.ppendchild(tn); } Environment poter s the utton s click hndler fd the messge to disply Summr returng functions s results Use closure to mt sttic environment My need to keep ctivtion records fter return Stck (lifo) order fils! Possile stck implementtion Forget out explicit delloction Put ctivtion records on hep Invoke grge collector s needed Not s totlly crzy s is sounds L. Dillon, CSE 452, Fll 2008 25 L. Dillon, CSE 452, Fll 2008 26 Summry of scope issues Block-structured lnguge uses stck of ctivtion records Activtion records cont prmeters, locl vrs, Also poters to enclosg scope Severl different prmeter pssg mechnisms Til clls my e optimized Function prmeters/results require closures Closure environment poter is used on function cll Stck delloction my fil if function is returned from cll Closures re not needed if functions re not nested locks L. Dillon, CSE 452, Fll 2008 27