Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
Continuations
CS242Lecture6
AlexAikenCS242Lecture6
HW1 Comments
• PeoplewereveryengagedwithHW1• Officehourswerebusy!
• Overallpeopledidwell
• Recurringissues• Expressionsaretrees,notstrings.
• ExampleSKIIcannotbereducedtoSI• Datatypeencoding
AlexAikenCS242Lecture6
Today’s Variant of Lambda Calculus …
e→x|λx.e|ee|i|e+e
AlexAikenCS242Lecture6
Start Simple
• Howdoweevaluatee+e’?
• Firstevaluateetoavaluex• Secondevaluatee’toavaluey• Thirdcomputex+y
• Notethatthisdescriptionfixesanorderofevaluation• Couldevaluatee’andtheneinstead
AlexAikenCS242Lecture6
Explicit Order of Evaluation
Wecanrewritetheexpressiontomaketheorderofevaluationexplicit:
(λx.(x+e’)e)Goingonestepfurther:(λx.((λy.x+y)e’))e
AlexAikenCS242Lecture6
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
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
Comments
letx=einlety=e’inletz=x+yinzCanbereadasasequentialprogramx=ey=e’z=x+y
AlexAikenCS242Lecture6
Comments
letx=einlety=e’inletz=x+yinzNote:• Theorderofevaluationisexplicit• Everyintermediateresulthasaname
AlexAikenCS242Lecture6
Back to the First Step
Recallthefirststepofthetransformation:
(λx.x+e’)eWhichisequivalenttoletx=eine’
AlexAikenCS242Lecture6
Continuations
letx=eine’Wecanviewthisassplittingtheprogramintotwosequentiallyorderedparts:• Thecomputationofx=e• Thecontinuatione’whichrepresentsthecomputationoftherestoftheprogram
AlexAikenCS242Lecture6
What is a Continuation?
Recallletx=eine’ó(λx.e’)eAcontinuationisafunctionthattakesavalueasanargumentandevaluatesthe“restoftheprogram”.
AlexAikenCS242Lecture6
Continuation Passing Style
• Rewritetheprogramusingcontinuations
• Eachcontinuation• Performsjustoneprimitivestepofthecomputation• Andthenpassestheresulttoanothercontinuation
AlexAikenCS242Lecture6
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
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
Continuations
• ContinuationsarelikestatementlabelsinC• Syntactically,namesapointintheprogram• Semantically,namesthecomputationthatexecutesbyjumpingtothatpoint
• Bysystematicallyusingcontinuations,we• Maketheorderofevaluationexplicit• Giveanametoeveryintermediatevalue• Nameeverystep(continuation)ofthecomputation
AlexAikenCS242Lecture6
Continuation Passing Style Transformation
DefineC(e,k)tobethetranslationofewithcontinuationkintocontinuationpassingstyleSosemantically,C(e,k)=kei.e.,evaluateeandpassthevaluetoktoruntherestoftheprogram.ButofcoursewewanttoconverteintoCPSstyle,too…
AlexAikenCS242Lecture6
CPS Transformation: Constants and Variables
Easycasesfirst!C(i,k)=kiC(x,k)=kxForanintegerorvariablethereisnofurthertranslationtodo,justpassthevaluedirectlytothecontinuation.
AlexAikenCS242Lecture6
CPS Transformation: Addition
C(e+e’,k)=C(e,λv.C(e’,λv’.k(v+v’))Note:Thevariablesvandv’mustbefresh.
AlexAikenCS242Lecture6
CPS Transformation: Abstraction
C(λx.e,k)=?Herekisthecontinuationofthefunctiondefinition.Wealsowanttotranslatethebodyeofthefunction.Whatisthecontinuationofthefunctionbody?Problem:Thefunctioniscalledatadifferentpointthanitisdefined,sothecontinuationforthebodyisdifferentandnotanargumenttothetranslation.
AlexAikenCS242Lecture6
CPS Transformation: Abstraction
C(λx.e,k)=k(λk’.λx.C(e,k’))Idea:Simplydefinethetranslationofthefunctiontofirsttakeacontinuationk’andthentakethefunctionargument.Thecontinuationwhenthefunctionisappliedisk’,whichweuseinthetranslationofthefunctionbody.Noticehowthetwocontinuationskandk’capturethetworelevantpointsinafunction’slife:Whenitisdefinedandwhenitisapplied.
AlexAikenCS242Lecture6
CPS Transformation: Application
C(ee’,k)=C(e,λf.C(e’,λv.fkv))Thetranslationisfullydeterminedbytwothings:Weevaluateeandthene’.Notethestructuralsimilaritytoaddition,theotherconstructwithtwosubexpressions.TheexpressioneevaluatestoaCPS-transformedfuntionf,requiringacontinuationkandavaluevasarguments.
AlexAikenCS242Lecture6
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
Reminder
Whenreadinglambdaexpressions,thescopeofanabstractionλx.eextendsasfartotherightaspossible
• Allthewaytotheendoftheexpression• Oruntilblockedbyarightparenthesisλf.λx.λy.fyx=λf.λx.λy.(fyx)isverydifferentfromλf.λx.(λy.fy)x AlexAikenCS242Lecture6
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
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
Complete Programs
ForafullprogramP,theinitialcontinuationistheidentifyfunctionI.SotheCPStransformationofPisCPS(P,I)
AlexAikenCS242Lecture6
Discussion
• TheCPStransformationisimportantinlanguageimplementations• Veryconvenienttohaveaprogramrepresentationwhereeveryintermediateresultisnamed.
• Butwecangoastepfurtherandmakeitusefultotheprogrammer• Bymakingcontinuationsavailableasprogramvalues
AlexAikenCS242Lecture6
Call/CC
e→x|λx.e|ee|i|e+e|call/ccλk.e|resumeke
Call/cccallsitsfunctionargumentwiththecurrentcontinuation.Resumepassesthevalueofitsexpressionargumenttoitscontinuation
argument.
AlexAikenCS242Lecture6
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
Example
call/ccλk.1+(resumek0)Whatistheresultofthisprogram?
AlexAikenCS242Lecture6
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
Discussion
Thisprogramsimulatesan“abort”or“exit”statement• Capturethecontinuationatthestartoftheprogram• Invokingthatcontinuationatanypointwillterminatethecomputation
AlexAikenCS242Lecture6
Discussion
• Ingeneralcontinuationscanbeusedtoresumeexecutionfromanarbitrarypointintheprogram
• Canimplementmanynon-localcontroloperations• Exceptions• Backtracking• Setjmp/longjmp• Co-routines• …
AlexAikenCS242Lecture6
Discussion
• Afewlanguagesexposecall/ccorsomethingsimilar• Scheme,Racket
• Butprogrammerscanalsocodecontinuation-passingstyledirectly• Oftenusedasasoftwarearchitecturedevice• E.g.,event-drivensystems
• Plusesandminuses• Makesprogramcontrolintofirst-classvalues,whichisnecessaryforprogramsthatneedtoprogrammaticallymanipulatetheflowofcontrol
• Turnsprograms“insideout”• Contagious:Affectsthestructureoftheentireprogram
AlexAikenCS242Lecture6