Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like...
Transcript of Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like...
![Page 1: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/1.jpg)
Continuations
CS242Lecture6
AlexAikenCS242Lecture6
![Page 2: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/2.jpg)
HW1 Comments
• PeoplewereveryengagedwithHW1• Officehourswerebusy!
• Overallpeopledidwell
• Recurringissues• Expressionsaretrees,notstrings.
• ExampleSKIIcannotbereducedtoSI• Datatypeencoding
AlexAikenCS242Lecture6
![Page 3: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/3.jpg)
Today’s Variant of Lambda Calculus …
e→x|λx.e|ee|i|e+e
AlexAikenCS242Lecture6
![Page 4: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/4.jpg)
Start Simple
• Howdoweevaluatee+e’?
• Firstevaluateetoavaluex• Secondevaluatee’toavaluey• Thirdcomputex+y
• Notethatthisdescriptionfixesanorderofevaluation• Couldevaluatee’andtheneinstead
AlexAikenCS242Lecture6
![Page 5: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/5.jpg)
Explicit Order of Evaluation
Wecanrewritetheexpressiontomaketheorderofevaluationexplicit:
(λx.(x+e’)e)Goingonestepfurther:(λx.((λy.x+y)e’))e
AlexAikenCS242Lecture6
![Page 6: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/6.jpg)
Explicit Order of Evaluation
Wecanrewritee+e’tomaketheorderofevaluationexplicit:(λx.(x+e’)e)Goingonestepfurther:(λx.((λy.x+y)e’))eAndonemorestep:(λx.((λy.(λz.z)(x+y))e’))e
AlexAikenCS242Lecture6
![Page 7: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/7.jpg)
A More Readable Version
Recall(λx.e)e’=letx=e’ineThen(λx.((λy.(λz.z)(x+y))e’))eCanberewrittenasletx=einlety=e’inletz=x+yinz
AlexAikenCS242Lecture6
![Page 8: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/8.jpg)
Comments
letx=einlety=e’inletz=x+yinzCanbereadasasequentialprogramx=ey=e’z=x+y
AlexAikenCS242Lecture6
![Page 9: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/9.jpg)
Comments
letx=einlety=e’inletz=x+yinzNote:• Theorderofevaluationisexplicit• Everyintermediateresulthasaname
AlexAikenCS242Lecture6
![Page 10: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/10.jpg)
Back to the First Step
Recallthefirststepofthetransformation:
(λx.x+e’)eWhichisequivalenttoletx=eine’
AlexAikenCS242Lecture6
![Page 11: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/11.jpg)
Continuations
letx=eine’Wecanviewthisassplittingtheprogramintotwosequentiallyorderedparts:• Thecomputationofx=e• Thecontinuatione’whichrepresentsthecomputationoftherestoftheprogram
AlexAikenCS242Lecture6
![Page 12: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/12.jpg)
What is a Continuation?
Recallletx=eine’ó(λx.e’)eAcontinuationisafunctionthattakesavalueasanargumentandevaluatesthe“restoftheprogram”.
AlexAikenCS242Lecture6
![Page 13: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/13.jpg)
Continuation Passing Style
• Rewritetheprogramusingcontinuations
• Eachcontinuation• Performsjustoneprimitivestepofthecomputation• Andthenpassestheresulttoanothercontinuation
AlexAikenCS242Lecture6
![Page 14: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/14.jpg)
Back to the Example
Recallwetranslatede+e’to(λx.((λy.(λz.z)(x+y))e’))ek0=λw.k1ek1=λx.k2e’k2=λy.k3(x+y)k3=λz.z
AlexAikenCS242Lecture6
![Page 15: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/15.jpg)
Back to the Example
Recallwetranslatede+e’to(λx.((λy.(λz.z)(x+y))e’))ek0=λw.k1ek1=λx.k2e’k2=λy.k3(x+y)k3=λz.z
k0:letx=eink1:lety=e’ink2:letz=x+yink3:z
AlexAikenCS242Lecture6
![Page 16: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/16.jpg)
Continuations
• ContinuationsarelikestatementlabelsinC• Syntactically,namesapointintheprogram• Semantically,namesthecomputationthatexecutesbyjumpingtothatpoint
• Bysystematicallyusingcontinuations,we• Maketheorderofevaluationexplicit• Giveanametoeveryintermediatevalue• Nameeverystep(continuation)ofthecomputation
AlexAikenCS242Lecture6
![Page 17: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/17.jpg)
Continuation Passing Style Transformation
DefineC(e,k)tobethetranslationofewithcontinuationkintocontinuationpassingstyleSosemantically,C(e,k)=kei.e.,evaluateeandpassthevaluetoktoruntherestoftheprogram.ButofcoursewewanttoconverteintoCPSstyle,too…
AlexAikenCS242Lecture6
![Page 18: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/18.jpg)
CPS Transformation: Constants and Variables
Easycasesfirst!C(i,k)=kiC(x,k)=kxForanintegerorvariablethereisnofurthertranslationtodo,justpassthevaluedirectlytothecontinuation.
AlexAikenCS242Lecture6
![Page 19: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/19.jpg)
CPS Transformation: Addition
C(e+e’,k)=C(e,λv.C(e’,λv’.k(v+v’))Note:Thevariablesvandv’mustbefresh.
AlexAikenCS242Lecture6
![Page 20: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/20.jpg)
CPS Transformation: Abstraction
C(λx.e,k)=?Herekisthecontinuationofthefunctiondefinition.Wealsowanttotranslatethebodyeofthefunction.Whatisthecontinuationofthefunctionbody?Problem:Thefunctioniscalledatadifferentpointthanitisdefined,sothecontinuationforthebodyisdifferentandnotanargumenttothetranslation.
AlexAikenCS242Lecture6
![Page 21: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/21.jpg)
CPS Transformation: Abstraction
C(λx.e,k)=k(λk’.λx.C(e,k’))Idea:Simplydefinethetranslationofthefunctiontofirsttakeacontinuationk’andthentakethefunctionargument.Thecontinuationwhenthefunctionisappliedisk’,whichweuseinthetranslationofthefunctionbody.Noticehowthetwocontinuationskandk’capturethetworelevantpointsinafunction’slife:Whenitisdefinedandwhenitisapplied.
AlexAikenCS242Lecture6
![Page 22: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/22.jpg)
CPS Transformation: Application
C(ee’,k)=C(e,λf.C(e’,λv.fkv))Thetranslationisfullydeterminedbytwothings:Weevaluateeandthene’.Notethestructuralsimilaritytoaddition,theotherconstructwithtwosubexpressions.TheexpressioneevaluatestoaCPS-transformedfuntionf,requiringacontinuationkandavaluevasarguments.
AlexAikenCS242Lecture6
![Page 23: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/23.jpg)
Continuation Passing Style Transformation
C(x,k)=kxC(λx.e,k)=k(λk’.λx.C(e,k’))C(ee’,k)=C(e,λf.C(e’,λv.fkv))C(i,k)=kiC(e+e’,k)=C(e,λv.C(e’,λv’.k(v+v’))
AlexAikenCS242Lecture6
![Page 24: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/24.jpg)
Reminder
Whenreadinglambdaexpressions,thescopeofanabstractionλx.eextendsasfartotherightaspossible
• Allthewaytotheendoftheexpression• Oruntilblockedbyarightparenthesisλf.λx.λy.fyx=λf.λx.λy.(fyx)isverydifferentfromλf.λx.(λy.fy)x AlexAikenCS242Lecture6
![Page 25: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/25.jpg)
AnExample
C((λx.x+1)2,k0)=C(λx.x+1,λf.C(2,λv0.fk0v0))=C(λx.x+1,λf.((λv0.fk0v0)2))=(λf.((λv0.fk0v0)2))λk1.λx.C(x+1,k1)=(λf.((λv0.fk0v0)2))λk1.λx.C(x,λv1.C(1,λv2.k1v1+v2))=(λf.((λv0.fk0v0)2))λk1.λx.C(x,λv1.(λv2.k1v1+v2)1)=(λf.((λv0.fk0v0)2))λk1.λx.(λv1.(λv2.k1v1+v2)1)x
AlexAikenCS242Lecture6
![Page 26: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/26.jpg)
Evaluation
(λf.((λv0.fk0v0)2))λk1.λx.(λv1.(λv2.k1v1+v2)1)x→(λv0.(λk1.λx.(λv1.(λv2.k1v1+v2)1)x)k0v0)2→(λk1.λx.(λv1.(λv2.k1v1+v2)1)x)k02→(λx.(λv1.(λv2.k0v1+v2)1)x)2→(λv1.(λv2.k0v1+v2)1)2(λv2.k02+v2)1k02+1k03
AlexAikenCS242Lecture6
![Page 27: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/27.jpg)
Complete Programs
ForafullprogramP,theinitialcontinuationistheidentifyfunctionI.SotheCPStransformationofPisCPS(P,I)
AlexAikenCS242Lecture6
![Page 28: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/28.jpg)
Discussion
• TheCPStransformationisimportantinlanguageimplementations• Veryconvenienttohaveaprogramrepresentationwhereeveryintermediateresultisnamed.
• Butwecangoastepfurtherandmakeitusefultotheprogrammer• Bymakingcontinuationsavailableasprogramvalues
AlexAikenCS242Lecture6
![Page 29: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/29.jpg)
Call/CC
e→x|λx.e|ee|i|e+e|call/ccλk.e|resumeke
Call/cccallsitsfunctionargumentwiththecurrentcontinuation.Resumepassesthevalueofitsexpressionargumenttoitscontinuation
argument.
AlexAikenCS242Lecture6
![Page 30: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/30.jpg)
Call/CC
C(x,k)=kxC(λx.e,k)=k(λk’.λx.C(e,k’))C(ee’,k)=C(e,λf.C(e’,λv.fkv))C(i,k)=kiC(e+e’,k)=C(e,λv.C(e’,λv’.k(v+v’))C(call/ccλx.e,k)=(λx.C(e,k))kC(resumeke,k’)=C(e,k)
AlexAikenCS242Lecture6
![Page 31: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/31.jpg)
Example
call/ccλk.1+(resumek0)Whatistheresultofthisprogram?
AlexAikenCS242Lecture6
![Page 32: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/32.jpg)
Translation and Evaluation
C(call/ccλk.1+(resumek0),I)=(λk.C(1+(resumek0),I))I=(λk.C(1,λm.C(resumek0,λn.I(m+n))))I=(λk.C(1,λm.C(0,k)))I=(λk.C(1,λm.k0))I=(λk.(λm.k0)1)I→(λm.I0)1→I0→0
AlexAikenCS242Lecture6
![Page 33: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/33.jpg)
Discussion
Thisprogramsimulatesan“abort”or“exit”statement• Capturethecontinuationatthestartoftheprogram• Invokingthatcontinuationatanypointwillterminatethecomputation
AlexAikenCS242Lecture6
![Page 34: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/34.jpg)
Discussion
• Ingeneralcontinuationscanbeusedtoresumeexecutionfromanarbitrarypointintheprogram
• Canimplementmanynon-localcontroloperations• Exceptions• Backtracking• Setjmp/longjmp• Co-routines• …
AlexAikenCS242Lecture6
![Page 35: Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,](https://reader035.fdocuments.in/reader035/viewer/2022071510/612f1a371ecc515869433ae0/html5/thumbnails/35.jpg)
Discussion
• Afewlanguagesexposecall/ccorsomethingsimilar• Scheme,Racket
• Butprogrammerscanalsocodecontinuation-passingstyledirectly• Oftenusedasasoftwarearchitecturedevice• E.g.,event-drivensystems
• Plusesandminuses• Makesprogramcontrolintofirst-classvalues,whichisnecessaryforprogramsthatneedtoprogrammaticallymanipulatetheflowofcontrol
• Turnsprograms“insideout”• Contagious:Affectsthestructureoftheentireprogram
AlexAikenCS242Lecture6