Some Considerations on a Calculus with Weak Refere

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

SomeConsiderationsonaCalculuswithWeakReferencesKevinDonnellyBostonUniversitykevind@cs.bu.eduAssafJ.KfouryBostonUniversitykfoury@cs.bu.eduJuly27,2005AbstractWeakreferencesarereferencesthatdonotpreventtheobjecttheypointtofrombeinggarbagecollected.Mostrealisticlanguages,includingJava,SML/NJ,andOCamltonameafew,havesomefacilityforprogrammingwithweakreferences.Weakreferencesareusedinimplementingidiomslikememoizingfunctionsandhash-consinginordertoavoidpotentialmemoryleaks.However,thesemanticsofweakreferencesinmanylanguagesarenotclearlyspecified.Withoutaformalsemanticsforweakreferencesitbecomesimpossibletoprovethecorrectnessofimplementationsmakinguseofthisfeature.PreviousworkbyHallettandKfouryextendsλgc,alanguageformodelinggarbagecollection,toλweak,asimilarlanguagewithweakreferences.Usingthispreviouslyformalizedsemanticsforweakreferences,weconsidertwoissuesrelatedtowell-behavednessofprograms.Firstly,weprovideanew,simplerproofofthewell-behavednessofthesyntacticallyrestrictedfragmentofλweakdefinedpreviously.Secondly,wegiveanaturalsemanticcri-terionforwell-behavednessmuchbroaderthanthesyntacticrestriction,whichisusefulasprincipleforprogrammingwithweakreferences.Furthermoreweextendtheresult,provedinpreviouslyofλgc,whichallowsonetousetype-inferencetocollectsomereachableobjectsthatareneverused.Weprovethatthisresultholdsofourlanguage,andweextendthisresulttoallowthecollectionofweakly-referencedreachablegarbagewithoutincurringthecomputationaloverheadsometimesassociatedwithcollectingweakbindings(e.g.theneedtorecomputeamemoizedfunction).Lastlyweuseextendthesemanticframeworktomodelthekey/valueweakreferencesfoundinHaskellandweprovetheHaskellissemanticsequivalenttoasimplersemanticsduetothelackofside-effectsinourlanguage.1IntroductionTheabilitytoconciselyspecifyandformallyprovethecorrectnessofgarbagecollectionstrategieswasanimportantcontributionofMorrisett,FelleisenandHarper’sλgc.Bymodelingtheheapasasetofmutuallyrecursivedefinitions,thesemanticsofagarbagecollectionstrategycanbespecifiedasarewriterulewhichremovesbindingsfromthemutuallyrecursivesetwithoutalteringprogrambehavior.Theadditionofweakreferenceschangesthissituationinthatprogrambehaviorcandependonhowgarbagecollectionisemployed.However,thisdoesnotnegatetheusefulnessofhavingahigh-levelformalmodelwhichspecifieshowgarbagecollectionandweakreferencesinteract.Infact,some(perhapsinformal)modeliscriticaltowritingcorrectimplementationsusingweakreferences.Aformalmodel,whichextendsλgcwiththemeanstointroduceandconditionallydereferenceweakreferences,isintroducedin[HK05].Inthislanguage,asinλgc,allvaluesareallocatedto,andstayontheheapduringevaluation(unlessgarbagecollected).Forthisreasonitmakessensetocreateaweakreferencetoanyprogramvalue.Thesemanticsgivenessentiallymatchesthesemanticsforweakreferences1inSML/NJ[SML].Becauseofcompileroptimizationslikecommonsubexpressionelimination,theactualidentityofimmutableobjectsisnotstaticallyknown,andassuchthedocumentationclaimsthesemanticsis“ambiguous.”ThesemanticsusedinthispaperisnotambiguousandifanSMLprogrammerusesthissemanticshisprogramswillbehaveasintended,andwedonotneedtoworryaboutobjectidentitybecausethesemanticsdoesnotguaranteethatanyobjectwillbegarbagecollectedatanygiventime.Thecontributionsofthispaperareseveral.Firstly,weprovideamuchsimplerandcompleteproofofthewell-behavednessofasyntacticallyrestrictedclassofprograms.Theoriginalproofwasquitecomplicatedandonlyprovedapartofthetheorem.Secondly,weprovideanaturalsemanticcriterionforwell-behavednesswhichismuchbroaderthanthesyntacticrestriction.Thiscriterionisausefulprincipleforprogrammingwithweakreferences,andweuseittoargueforthecorrectnessofanimplementationofmemoizingfunctionapplication.Thirdly,weextendtheresultfrom[MFH95]whichallowstheuseoftype-inferencetocollectsomeobjectswhicharereachablebutneverused.Theextendedresultadditionallyallowstombstoningofweakreferencestothisreachablegarbage,withoutincurringtheruntimeoverheadsometimesassociatedwithtombstoningaweakreference.Lastly,weshowtheflexibilityoftheframeworkbyextendingittoformalizeamodelofthekey/valueweakreferencesfoundinHaskell.Thepaperisorganizedasfollows.InSection2weintroducethesyntaxandsemanticsofλweak.InSection3weexplainthesyntacticrestrictionthatweimposetoassureuniquenessofprogramresult.InSection4weprovethatrestrictedprogramshaveauniqueresult.InSection5wegiveanaturalsemanticcriterionforwell-behavedness.InSection6weprovethecorrectnessofatransformationthatusestypeinferencetobothdoextragarbagecollection,andtombstoneweakreferenceswithoutrequiringtheextracomputationthatmightnormallybenecessary.InSection7weformalizethesemanticsofthekey/valueweakreferencesfoundinHaskellandproveitequivalenttoasimplersemanticsintheabsenseofside-effects.2Modelingweakreferences:λweakInthissectionwegivetheformalsyntaxandsemanticsofλweak.SyntaxofλweakThesyntaxofλweak(giveninFigure1)isthatofastandardprogramminglanguagebasedontheλ-calculusalongwithadditionalprimitivesforintroducingweakreferencesanddoingconditionalweakdereferencing.Aλweakexpressioniseitheravariable(x),aninteger(i)

1 / 17
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功