/
Yield Mainstream Delimited Continuations Roshan P Yield Mainstream Delimited Continuations Roshan P

Yield Mainstream Delimited Continuations Roshan P - PDF document

liane-varnes
liane-varnes . @liane-varnes
Follow
402 views
Uploaded On 2015-05-21

Yield Mainstream Delimited Continuations Roshan P - PPT Presentation

James Indiana University Bloomington Indiana USA rpjamesindianaedu Amr Sabry Indiana University Bloomington Indiana USA sabryindianaedu Abstract Many mainstream languages have operators named yield that share common semantic roots but di er signi642 ID: 71119

James Indiana University Bloomington

Share:

Link:

Embed:

Download Presentation from below link

Download Pdf The PPT/PDF document "Yield Mainstream Delimited Continuations..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Yield:MainstreamDelimitedContinuationsR.P.James,andA.Sabrysuspendedforgood.Intheabsenceofbreak,onemaybeledtothinkofyieldasmerelytheinvocationofanimplicitfunctionwhichistheloopbody.Howeverunderthatinterpretationbreakwouldbeanon-localescape.Inadditiontobreak,theloopbodycanalsoexecutereturnwhichwillcausecontroltogotothecallerofuseFibonacci.Tosummarize,yieldallowsafunctiontooutputvaluestoitscallingcontext.Stateispreservedwhileyieldingandresumingasuspendediteratorcausesittocontinuefromthepointofyielding.Aninnitesequenceofvaluesmaybeyieldedandthecalleronlyresumestheiteratoratitsdiscretion.2.2RubyWiththeseinsightsfromC#,let'slookatyieldinRuby[TH00].Rubyisa“ducktyped”languageprimarilydevelopedbyYukihiroMatsumoto.MuchofthepresentdaypopularityofyieldisattributedtoitsimplementationinRuby,whichwasinturnmotivatedbyyieldinCLUandcontrolblocksinSmalltalk.Theimportantdi erencebetweenRubyandC#isthatyieldinRubyreturnsavalue,whereasinC#thereisnosuchreturnvalue.FurtherinRubytheiteratorcanreturnanalresultattermination,whichisdistinguishedfromyieldedvalues.WeillustrateyieldusingtheinjectfunctionbelowwhichisasimplicationofitsstandardRubylibraryimplementation.TheinjectfunctionfoldsoveraRubyobject.definject(state)self.eachfjvjstate=yield(v,state)gstateenddefuseInject()total=[1..1000].inject(0)fjv,sumjifprime?(v)sum+velsesumendgendThecomponentsoftheobjectselfareenumeratedbyeach.Theiteratorinjectyieldseachcompo-nentofselfandthecurrentstate.Thevariablestateisupdatedwiththevaluereturnedbyyield.Afterthecomponentsofthecurrentobjecthavebeenenumerated,injectreturnstheaccumulatedstate.Thecaller,useInject,invokestheinjectmethodofanarrayobjectandbuildsthesumofprimes.Theblockconstruct,syntacticallyfjvariablejbodyg,isRuby'sequivalentoftheC#foreach.Thisusageofyieldsimulateshigher-orderprogramminginanimperativecontext.However,thecodeblockfollowingtheinjectinvocationisnotjustalambdaabstraction;likethebodyoftheforeachloopinC#,theblockcanexecuteabreakorareturn.Summarizing,iteratorsinRubytakeinputfromtheircallingcontexttoresumetheircomputation,i.e.,unlikeC#,eachinvocationofyieldinRubyreturnsavalue.Further,yieldingcomputationscanalsoreturnavalueoncompletion.Theexecutionoftheiteratorissuspendedeachtimeityields.Theiteratormaintainsstateandwhenresumed,behavesexactlyasiftheoperatoryieldhadreturned.Whattheiteratordoesnextonresumptioncandependonitsinputvalue.Thereturnvalueofaniteratorisdistinguishedfromayieldedvalue,inparticulartheinput,outputandreturnvaluesofaniteratorcanhavedi erenttypes.InRubynomenclatureiteratorsaresometimesreferredtoasasymmetriccoroutines,onesthatarerestrictedtotransfercontrolonlytotheircallingcontext.2.3JavaScriptJavaScript1.7[Moz06,Fla06]introducedayieldoperatorthatisyettoberatiedintheECMAspeci-cation[ECM99]ofthelanguage.Inthepreviousexamples,weusedyieldalongwithaloopconstructthatinteractedwithoneiteratoratatime.Thisusageissometimesreferredtoasinternaliterators,inthesensethatthecaller'sinteractionwiththeiteratorisencapsulatedbytheloopconstruct.Thesuspended3 Yield:MainstreamDelimitedContinuationsR.P.James,andA.Sabryinprinciple,thereturnvaluecouldbeencodedasthelastyieldedvalueorbysomeotherconvention.However,inpracticethisisinconvenientsinceonewouldhavetoalwayskeeptrackofthelastyieldedvalue.CLU[Lis93]hassupportforanoutputonlyvariadicyieldandaforloopconstructforconsumingsuchiterators.Italsoattemptstocapturethetypeoftheyieldedvaluesinthetypesignatureofiterators.Sather[MOSS96,Omo91]hasanoutputonlyyieldconstructwithweakerrestrictionsontheusagewithinloopconstructs.Duetotheabsenceofaformaldescriptionofthesemantics,weareunsureoftheprecisenatureoftheserestrictions.C![MSB03,BMS05]andF#[Sym03]haveyieldreturnstatements,muchlikeC#'s,thatarepri-marilyusedtomodelpotentiallyinnitelazylists/streams.TheC!typesystemisenhancedwithtypessuchasintandint+toindicateifthesearestreamsofzeroormoreintegers.Thereareotherlanguages,suchasGroovy[KGK+07],Icon[GG83],thatimplementyieldoriterator-likeconstructsthatwehavenotdetailedhere.2.5TowardsageneralizedyieldInsummary,aniteratormayperformsomearbitrarilycomplexcomputation.Itsconsumer,thecallingcontextoftheiterator,isabstractedfromthedetailsofthiscomputation.Dually,theiteratordoesnotknowanythingaboutthecomputationperformedbyitscallingcontext,orevenifitwillberesumedagain.Theyieldoperatorseparatescontrolowconcernsandallowsustoencapsulateiteratorsandtheircallersintoreusablesoftwarecomponents.Basedonourbriefsurvey,weunifyyieldanditeratorpropertiesfromthevariouslanguages,intro-ducingtypesasappropriate.Aniteratorcanbethoughtofasasub-process,aspecialcomputation,thatisrestrictedtocommunicatewithitscallingcontextusingyield.Inthecourseofitscomputationitcanyieldoutmanyvaluesoftypeoandreceiveinputsoftypei.Whenaniteratorterminatesitreturnsanalvalueoftyper.HencewecangiveiteratorstheabstracttypeYieldior.Theconstructyieldisadelim-itedIOoperationavailabletosuchiteratorsthatenablethemtocommunicatewiththeircallingcontext.Eachinvocationofyieldsuspendsthestateofitsiteratorandsuspendediteratorsarerst-classvalues.Resumingasuspendediteratorisatthediscretionofthecallingcontextandthevalueusedtoresumetheiteratorbecomesavailableasthereturnvalueofthesuspendingyield.Asingleyieldstatementmaybeseenasthesimplestpossibleinterestingiteratorwiththeabstracttypeo!Yieldioi,allowingmoreinterestingiteratorstobebuiltbycomposition.ApurecomputationincontrastdoesnotdoanysuchIO,andonlyreturnsaresult.Diagrammatically,ifwevisualizepurefunctionst1!t2andcomputationstasshowntotheleft,thenwecanvisualizeaniteratorYieldiorandtheyieldoperatoroftypeo!Yieldioiasshowntotheright: Arstattempttoformalizeyieldmightbetosimplyaddyieldtothecall-by-value-calculus.How-everthisisquicklyseentobeinadequate.Themaincomplicationistodecidehowtoseparatetheiteratorfromitscaller.Tothisend,weintroduceadelimitingoperatorcalledruntoexplicitlyseparatetheitera-torfromitscallingcontext.Theoperatorrunmarkstheboundaryofaniteratoranddelimitstheactionofyield.Theargumenttorunisanopaquecomputationthatcanyieldanditreturnsaconcretedatastructurethatthecallingcontextcaninteractwith.Thisimmediatelysuggestsamonadicencapsulation5 Yield:MainstreamDelimitedContinuationsR.P.James,andA.Sabryforthee ectfuliteratorcomputationswithyieldastheonlye ectoperatorofthemonad.Sincerunmarkstheboundaryofthise ect,itcanbeusedastherunoperationthatescapesthemonad.Indeedtheimperativelanguagesthatwehaveconsideredheredohavesucharundelimiter,whichisusuallyimplicit,andisapparentinthefactthatmethodsthatyieldaretreateddi erentlyfrommethodsthatdon't,requiringtheformertobecalledinaspecialmanner.IntheC#andRubyexamples,theequivalentofrunwashiddenintheimplementationoftheloopconstructandinJavaScriptitwasimplicitintheiter1=bonacci()assignment.InC#,GetEnumerator()istheequivalentofrunandittakesanopaquecomputationoftypeIEnumerableT&#x]TJ/;ྒྷ ; .96;& T; 6.;̶ ;� Td;&#x [00;toaninteractiveobjectoftypeIEnumeratorT&#x]TJ/;ྒྷ ; .96;& T; 6.;̶ ;� Td;&#x [00;.TheinterfaceIEnumeratorexposesmethodsforretrievingthecurrentyieldedvalue(Current)andforadvancingtheiteratorcomputation(MoveNext).WecanuseGetEnumerator()explicitlyasfollows:IEnumeratorint&#x]TJ/;&#x 7.9;܁ ;&#xTf 5;&#x.021;&#x 0 T; [0;iter=fibonacci().GetEnumerator();while(iter.MoveNext())fConsole.WriteLine(iter.Current);g3AMonadicyieldWearenowreadytointroducethesyntaxofamonadicmeta-languagewithyield.ThislanguageisMoggi'smonadicmetalanguage(MML)extendedwiththeopaquemonadictypeYieldiorandatypeIteratoriorforinteractingwithiterators:types;t;i;o;r=bjt!tjIteratoriorjYieldiorexpressions;e=xjx:eje1e2jResultejSuspeejcaseee1e2jreturnejdox e;ejyieldejruneevaluationcontexts;E=jE[dox ;e]Thesetofexpressionsincludesapuresubsetconsistingofvariables,functions,applications,Result,Susp,andcase,andacomputationalsubsetconsistingofreturn,do,andyield.Theoperatorrunactsastheinterfacebetweenpureandcomputationalexpressions.Thereareseveralpossibletypesystemsofvaryingexpressivepower(seeSection3.1).Wepresentbelowasimpletypesysteminwhichthetypesiandoarexedforeachiterator:�(x)=t �`x:tvar�;x:t1`e:t2 �`x:e:t1!t2lambda�`e1:t1!t2�`e2:t1 �`e1e2:t2application�`e:r �`Resulte:Iteratoriorresult�`e1:o�`e2:i!Iteratorior �`Suspe1e2:Iteratoriorsusp�`e:Iteratorior�`e1:o!(i!Iteratorior)!t�`e2:r!t �`caseee1e2:tcase�`e:r �`returne:Yieldiorreturn�`e1:Yieldior0�;x:r0`e2:Yieldior �`dox e1;e2:Yieldiordo�`e:o �`yielde:Yieldioiyield�`e:Yieldior �`rune:IteratoriorrunAscanbeseenfromthetypesystem,theinteractivetypeIteratoriorisaparticularsumtypethat,foreaseofpresentation,wehavehardwiredintothetypesystem.ThecaseconstructistheeliminatorforthistypeandconstructorsSuspandResultarethetwointroductionsforthistype.Evaluationisspeciedasthepurerelation!whichcanbeappliednon-deterministicallyinanysubexpressionandthemonadicreduction7!whichforcesreductionstohappeninacertainorder.6 Yield:MainstreamDelimitedContinuationsR.P.James,andA.Sabryonecommonlyndsstatementsexpressingthelimitednatureoftherelevantyieldcomparedto“true”continuations.Asurprisingresultofgeneralizingyieldisitsconnectiontotraditionaldelimitedcontin-uations:wecangiveencodingsofyield-runintermsofthetraditionalshift-resetandviceversa.Thefollowingdenitionsrealizeyieldandrunintermsofthetraditionalcontroloperators:runereset(dox e;return(Resultx))yieldeshift(k:return(Suspek))Arunexpressioncorrespondstoacontroldelimiter.ThebodyeisevaluatedunderthisdelimiteranditsvalueiswrappedintheconstructorResultbeforeitisreturned.Ayieldexpressionsimplycapturesthecontinuationuptotheclosestdelimiterandimmediatelyreturnsthiscontinuationaspartofasuspension.Theonlycomplexityforthereverseencodingarisesfromthefactthatthecontinuationcapturedbyyieldisonlyaccessiblethroughthesuspensionobjecttothecontextoftheiterator.Henceitisnecessarytohavealittle“interpreter”torecursivelyunwindtheiteratoronesuspensionatatimeinordertoextracttheunderlyingdelimitedcontinuation.shifteyieldeinterpiter=caseiterreseteinterp(rune)(fk:reset(f(i:interp(ki))))(r:r)Itispossibletoprovethattheencodingsabove(inbothdirections)properlyimplementthedesiredoperators.Inordertomaketheproofaccessible,wepresentthesemanticsofshift-resetinthesamemonadicstyleasthesemanticsforyield/run.Theinterestingrulesarethefollowing:resete!e0ifhe;i7!hreturne0;iresete!reset(e0(x:resetE[returnx]))ifhe;i7!hshifte0;EiItisastraightforwardexercisetocheckthatthetranslationofeachpairofoperatorsisconsistentwiththesemanticsoftheotherpair.Weomitthestraightforwardcalculations.Operatorsshift-resetandyieldhavemultipletypesystems.Thechoiceoftypesystemforonesetofoperatorsa ectstypesa ordablebytheotherviatranslation.Whilethefullrelationshipbetweenthetypesystemsoftheseoperatorsneedsfurtherinvestigation,someconnectionscanbemade.Thesimpletypesystemforyieldallowsforthefollowingshift-resetwithshift:((a!ans)!SRaansans)!SRaansaandreset:SRaansans!ans.typeSRiansr=Yieldi((i��ans)��Ciansans)rdataCiansr=CfunC::SRiansrgshift::((a��ans)��SRaansans)��SRaansashifte=yield(C.e)reset::SRaansans��ansresete=interp(rune)whereinterp(Suspfk)=reset$unC$f$ni��interp(ki)interp(Resultr)=rThemainrestrictionofthissystemisthatitxesboththeanswertypeansandtheinputtypeaofthecontinuations.Thefactthattheinputtypeisxedisanartifactofthefactthatiandoarexedforaniterator.Withthemoreexpressiveparametrictypesystemforyieldthisrestrictioncanberemovedallowingforshift:((a!ans)!SRansans)!SRansaandreset:SRansans!ans.typeSRansr=YieldIn(Outans)rdataIna=InfunIn::agdataOutansa=Out(a��ans)��SRansansshift::((a��ans)��SRansans)��SRansashifte=(yield(Oute))��=(return.unIn)reset::SRansans��ansresete=interp(rune)whereinterp(Susp(Outf)k)=reset$f$ni��interp(k(Ini))interp(Resultr)=rHerewehaveaxedanswertypeansbutcontinuationsofanyinputtypecanbecaptured.HandlingfullanswertypepolymorphismasinAsaiandKameyama[AK07]isatopicoffuturework.10 Yield:MainstreamDelimitedContinuationsR.P.James,andA.Sabry[BWD96]CarlBruggeman,OscarWaddell,andR.KentDybvig.Representingcontrolinthepresenceofone-shotcontinuations.InPLDI,pages99–107,1996.[CHO99]W.D.Clinger,A.H.Hartheimer,andE.M.Ost.ImplementationStrategiesforrst-classContinuations.Higher-OrderandSymbolicComputation,12(1):7–45,1999.[Dan00]OlivierDanvy.FormalizingImplementationStrategiesforrst-classContinuations.InESOP,pages88–103,2000.[DJS07]R.KentDyvbig,SimonPeytonJones,andAmrSabry.Amonadicframeworkfordelimitedcontinua-tions.J.Funct.Program.,17(6):687–730,2007.[dMI09]AnaL´uciadeMouraandRobertoIerusalimschy.Revisitingcoroutines.ACMTrans.Program.Lang.Syst.,31(2),2009.[ECM99]ECMA.262:ECMAScriptLanguageSpecication.ECMA,1999.[ECM06]ECMA.334:C#languagespecication.ECMA,June2006.[Fel88]MatthiasFelleisen.Thetheoryandpracticeofrst-classprompts.InPOPL,pages180–190,1988.[FH85]DanielP.FriedmanandChristopherT.Haynes.Constrainingcontrol.InPOPL,pages245–254,1985.[Fla06]D.Flanagan.JavaScript:TheDenitiveGuide.O'ReillyMedia,Inc.,2006.[GG83]R.E.GriswoldandM.T.Griswold.TheIconprogramminglanguage.Prentice-Hall,1983.[GHJV95]ErichGamma,RichardHelm,RalphJohnson,andJohnVlissides.Designpatterns:elementsofreusableobject-orientedsoftware.Addison-WesleyProfessional,1995.[HDI94]RobertHieb,R.KentDybvig,andClaudeW.AndersonIII.Subcontinuations.LispandSymbolicComputation,7(1),1994.[HE06]DaveHermanandBrendanEich.SpecifyingECMAScriptviaML.http://lambda-the-ultimate.org/node/1784,2006.[JL03]SimonL.PeytonJonesandRalfL¨ammel.Scrapyourboilerplate.InAPLAS,2003.[JMPS05]B.Jacobs,E.Meijer,F.Piessens,andW.Schulte.Iteratorsrevisited:Proofrulesandimplementation.InWorkshoponFormalTechniquesforJava-likePrograms(FTfJP),2005.[KBD98]SanjeevKumar,CarlBruggeman,andR.KentDybvig.Threadsyieldcontinuations.LispandSymbolicComputation,10(3),1998.[KGK+07]D.K¨onig,A.Glover,P.King,G.Laforge,andJ.Skeet.GroovyinAction.Manning,2007.[Lis93]BarbaraLiskov.AHistoryofCLU.InHOPLPreprints,pages133–147,1993.[MOSS96]StephanMurer,StephenM.Omohundro,DavidStoutamire,andClemensA.Szyperski.Iterationab-stractioninsather.ACMTrans.Program.Lang.Syst.,18(1),1996.[Moz06]Mozilla.org.Javascript1.7.https://developer.mozilla.org/en/New_in_JavaScript_1.7,2006.[MS04]EugenioMoggiandAmrSabry.Anabstractmonadicsemanticsforvaluerecursion.ITA,38(4),2004.[MSB03]ErikMeijer,WolframSchulte,andGavinBierman.Programmingwithcircles,trianglesandrectangles.InInXMLConferenceandExposition,2003.[Omo91]StephenM.Omohundro.Thesatherlanguageandlibraries.InTOOLS,pages439–440,1991.[Sym03]DonSyme.F#homepage.http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/,2003.[TH00]D.ThomasandA.Hunt.ProgrammingRuby:thepragmaticprogrammer'sguide.Addison-WesleyLongmanPublishingCo.,Inc.Boston,MA,USA,2000.[vRD03]G.vanRossumandF.L.Drake.PythonLanguageReferenceManual.NetworkTheory,2003.[vRE05]G.vanRossumandP.Eby.PEP342:CoroutinesviaEnhancedGenerators.PythonSoftwareFounda-tion,2005.[YvR01]K.P.YeeandG.vanRossum.PEP234:Iterators.PythonSoftwareFoundation,2001.12