35
Continuations CS242 Lecture 6 Alex Aiken CS 242 Lecture 6

Continuationsstanford.edu/class/cs242/materials/lectures/Lecture06.pdf• Continuations are like statement labels in C • Syntactically, names a point in the program • Semantically,

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

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,

Discussion

•  Afewlanguagesexposecall/ccorsomethingsimilar•  Scheme,Racket

•  Butprogrammerscanalsocodecontinuation-passingstyledirectly•  Oftenusedasasoftwarearchitecturedevice•  E.g.,event-drivensystems

•  Plusesandminuses•  Makesprogramcontrolintofirst-classvalues,whichisnecessaryforprogramsthatneedtoprogrammaticallymanipulatetheflowofcontrol

•  Turnsprograms“insideout”•  Contagious:Affectsthestructureoftheentireprogram

AlexAikenCS242Lecture6