/
Concatenate,Reverse and Map Vanish For Free Concatenate,Reverse and Map Vanish For Free

Concatenate,Reverse and Map Vanish For Free - PDF document

yoshiko-marsland
yoshiko-marsland . @yoshiko-marsland
Follow
369 views
Uploaded On 2017-03-20

Concatenate,Reverse and Map Vanish For Free - PPT Presentation

ThisspecialformmoreovercanbeobtainedautomaticallybyusingChitilsapproachoflistabstractionthroughtypeinference67Forexampleournewmethodreplacestheabovepartfunctionbythefollowingpart8aaBoo ID: 329678

ThisspecialformmoreovercanbeobtainedautomaticallybyusingChitil'sapproachoflistabstractionthroughtypein-ference[6 7].Forexample ournewmethodreplacestheabovepart-functionbythefollowing:part?::8a:(a!Boo

Share:

Link:

Embed:

Download Presentation from below link

Download Pdf The PPT/PDF document "Concatenate,Reverse and Map Vanish For F..." 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

Concatenate,ReverseandMapVanishForFreeJanisVoigtl¨anderDepartmentofComputerScienceDresdenUniversityofTechnology01062Dresden,Germanyvoigt@tcs.inf.tu-dresden.deAbstracteintroduceanewtransformationmethodtoeliminateintermedi-atedatastructuresoccurringinfunctionalprogramsduetorepeatedlistconcatenationsandotherdatamanipulations(additionallyex-empliedwithlistreversalandmappingoffunctionsoverlists).Thegeneralideaistouniformlyabstractfromdataconstructorsandmanipulatingoperationsbymeansofrank-2polymorphiccombina-torsthatexploitalgebraicpropertiesoftheseoperationstoprovideanoptimizedimplementation.Thecorrectnessoftransformationsisprovedbyusingthefreetheoremsderivablefromparametricpoly-morphictypes.CategoriesandSubjectDescriptorsD.1.1[ProgrammingTechniques]:Applicative(Functional)Pro-gramming;D.3.3[ProgrammingLanguages]:LanguageCon-structsandFeatures—abstractdatatypes,polymorphism;D.3.4[ProgrammingLanguages]:Processors—optimization;F.3.1[LogicsandMeaningsofPrograms]:SpecifyingandVerifyingandReasoningaboutPrograms;F.3.3[LogicsandMeaningsofPrograms]:StudiesofProgramConstructs—typestructureGeneralTermsLanguages,Algorithms,DesignKeywordsCombinators,correctnessproofs,denotationalsemantics,listab-straction,parametricity,programtransformation,rank-2types,shortcutdeforestation,theconcatenatevanishes,theoremsforfreeResearchsupportedbythe“DeutscheForschungsgemeinschaft”undergrantKU1290/2-1andbythe“GesellschaftvonFreundenundF¨orderernderTUDresden”withatravelgrant.Permissiontomakedigitalorhardcopiesofallorpartofthisworkforpersonalorclassroomuseisgrantedwithoutfeeprovidedthatcopiesarenotmadeordistributedforprotorcommercialadvantageandthatcopiesbearthisnoticeandthefullcitationontherstpage.Tocopyotherwise,torepublish,topostonserversortoredistributetolists,requirespriorspecicpermissionand/orafee.ICFP'02,October4-6,2002,Pittsburgh,Pennsylvania,USA.Copyright2002ACM1-58113-487-8/02/0010...$5.00Thisistheauthor'sversionofthework.Thedenitiveversionisavailablefromhttp://doi.acm.org/10.1145/581478.581481.1IntroductionConsiderthefollowingdenitioninthefunctionalprogramminglanguageHaskell,implementingthepartitionofalistlaccordingtosomepredicatep:part::8a:(a!Bool)![a]![a]partpl=letf[]z=zf(x:xs)z=ifpxthenx:(fxsz)elsefxs(z++[x])infl[]Whileservingasagoodspecicationofpart,thisdenitionhasaninefcientruntimebehaviorduetorepeatedconcatenateoperations(++)onintermediatelistsinthesecondargumentpositionoff.Since++requiresruntimelinearinthelengthofitsleftargument,thisoverheadcanbeconsiderable.Similarefciencyproblemsduetoamodularprogrammingstylecanalsobecausedbyotherdatamanipulatingoperationsoftenusedinspecications.Inordertooptimizefunctiondenitionsthatmakeextensiveuseof++,Hughes[12]proposedanalternativelistrepresentationsup-portingefcientconcatenations.Therefor,alistxsisrepresentedasthefunction(lys!xs++ys).Itislefttotheprogrammertode-cidewhereconversionbetweenthetworepresentationsshouldbeperformed,hencethetransformationisnotverysystematic.Wadler[27]presentedanalgorithmictransformationthatintro-ducesaccumulatingparametersandcanachievemany—butnotall—oftheeffectsofHughes'approach.Forexample,itisnotapplicabletotheabovepart-function.Thecontributionofthispaperisanoptimizationtechniqueforelim-inatingconcatenateoperationswiththefollowingcharacteristics:ItisapplicabletomoreprogramsthanWadler'smethod(cf.Sections6and7.2).Itextendstoageneralmethodologyforeliminatingalsootherdatamanipulatingoperationsthanlistconcatenation(cf.Sections4and5).Itproducesfunctiondenitionsthatcloselyresembletheorig-inalspecications,andthusdoesnothamperreadabilityandmaintainabilityoftransformedprograms.Ithasasoundsemanticbasisasitfacilitatesconcisecorrect-nessproofsoftransformationsbyusingWadler'sfreetheo-rems[28].Itrequiresnocompilermodication,onlythatfunctiondef-initionsarewritteninaspecialform(similarlytousingbuildforshortcutdeforestation[10]). ThisspecialformmoreovercanbeobtainedautomaticallybyusingChitil'sapproachoflistabstractionthroughtypein-ference[6,7].Forexample,ournewmethodreplacestheabovepart-functionbythefollowing:part?::8a:(a!Bool)![a]![a]part?pl=vanish++(lnca!letf[]z=zf(x:xs)z=ifpxthenx`c`(fxsz)elsefxs(z`a`(x`c`n))infln)Thisdenitiontakesadvantageofarank-2polymorphicfunctionvanish++thatisprovidedinFigure1.Alltheprogrammerneedstoknowinordertobenetfromourconcatenateeliminationmethodisacertainlawaboutvanish++thatwillbeprovedinSection3.1.WewillshowinSection4howtosystematicallydevelopsuchreusablevanish-combinatorsalsoforotherdatamanipulationsthanlistconcatenation.vanish++::8a:(8b:b!(a!b!b)!(b!b!b)!b)![a]vanish++g=gid(lxhys!x:(hys))()[]Figure1.Denitionofvanish++.Notethattheabovedenitionofpart?closelyresemblestheoriginalspecicationofpart,ifwereadn,`c`and`a`as[],:and++,respectively.Butwhiletheoriginalfunctionparthasaquadraticworst-casetimecomplexity(disregardingthetimeforexecutingp)inthelengthofitsinputlistl,theobtainedpart?isonlyoflineartimecomplexity,asexempliedinthefollowingmeasurements1:n=300050007000900011000parteven[1::n]0.41.12.23.55.6part?even[1::n]0.0040.0060.0090.0120.015Thefollowingprerequisitesarenecessarytoapplyourmethod:1.Weneedatypesystemthatsupportsrank-2polymor-phism[17],whichisthecase,e.g.,formostcurrentHaskellimplementations,butalsoforMetaML[25].2.Wehavetobelieveinthevalidityoffreetheoremsforthefunctionallanguageunderconsideration.Althoughthecor-rectnessofthesefreetheoremsreliesonrelationallyparamet-ricmodels[23]—whicharenotknowntoexistformod-ernfunctionallanguages—Pitts'recentproof[22]oftheex-istenceofsuchmodelsforlambdacalculiwithhigher-orderpolymorphicfunctionsandxpointrecursionjustiesthisas-sumption.Forexample,Johann[14]usedPitts'resulttojus-tifyshortcutdeforestation—thecorrectnessofwhichalsodependsonparametricity—forlanguageslikeHaskell.AlthoughweuseHaskellthroughoutthispaper,ourmethodologyisalsoapplicabletootherlazyorstrictfunctionallanguagesmeetingtheabovetwoprerequisites.Theremainderofthispaperisorganizedasfollows.Section2con-siderstheusedfunctionallanguageanditssemantics.Section31Theruntimes(inseconds)shownhereandinthefollowingta-blesweremeasuredonaSPARCworkstationusingtheprolingcapabilitiesoftheoptimizingGlasgowHaskellCompiler.presentsourtechniqueforeliminatingconcatenateoperationsanddiscussesitsapplicationonexamples.Section4usestheelimina-tionoflistreversaltoillustratethegeneralmethodology.Section5presentstheresultofapplyingthismethodologytoeliminate++,reverseandmap.Section6showshowtoeliminateconcatenateoperationsalsofromproducersofnestedlists.Section7comparesourmethodwithrelatedworkandSection8concludes.2FunctionalLanguageWeusethepureandlazyfunctionallanguageHaskell[2].De-nitionsofHaskellfunctionsthatweusethroughoutthepaperaregiveninFigure2.Exceptforswapandbuild,thesefunctionsarepartofthestandardHaskellprelude,thoughtheactualde-nitionsmaydifferbetweenHaskellimplementations.Ourperfor-mancemeasurementswillusetheGlasgowHaskellCompiler'spre-ludedenitions,whichmightbemoreefcientbutaresemanticallyequivalenttotheclearerdenitionsinFigure2.(++)::8a:[a]![a]![a][]++ys=ys(x:xs)++ys=x:(xs++ys)reverse::8a:[a]![a]reversel=letf[]ys=ysf(x:xs)ys=fxs(x:ys)infl[]map::8ab:(a!b)![a]![b]mapf[]=[]mapf(x:xs)=(fx):(mapfxs)id::8a:a!aidu=u()::8abg:(b!g)!(a!b)!a!g(f1f2)u=f1(f2u)fst::8ab:(a;b)!afst(h1;h2)=h1snd::8ab:(a;b)!bsnd(h1;h2)=h2swap::8ab:(a;b)!(b;a)swap(h1;h2)=(h2;h1)foldr::8ab:(a!b!b)!b![a]!bfoldrcn[]=nfoldrcn(x:xs)=x`c`(foldrcnxs)build::8a:(8b:(a!b!b)!b!b)![a]buildg=g(:)[]Figure2.SomeHaskellfunctions.2.1SemanticsUnfortunately,thereisnotyetaformalsemanticsforthewholeofHaskell,independentofanyconcreteimplementation.Neverthe- less,itiscommonpracticetouseadenotationalstyleforreasoningaboutHaskellprograms.Wealsofollowthisapproachandverybrieyrecallthenecessarynotions,inparticulartheapproximationvbetweenvaluesofthesametype,interpretedas“lessorequallydenedas”,andthevalue?ateverytype,interpretedas“unde-ned”.Thereaderwhoismainlyinterestedinthedevelopmentandapplicationofourtransformationmethodratherthanintheformalproofsmightwanttoskiptherestofthissection—aswellsastheappendices—andonlyconsiderthelawsinFigure3,whereallfreevariablesareuniversallyquantiedovertheappropriatetypes.?u=?(1)?++xs=?(2)(xs++ys)++zs=xs++(ys++zs)(3)xs++[]=xs(4)fst(swaph)=sndh(5)snd(swaph)=fsth(6)reverse?=?(7)reverse(reversexs)vxs(8)reverse(x:xs)=(reversexs)++[x](9)reverse(xs++ys)v(reverseys)++(reversexs)(10)mapf(xs++ys)=(mapfxs)++(mapfys)(11)mapf(mapkxs)=map(fk)xs(12)reverse(mapkxs)=mapk(reversexs)(13)mapf?=?(14)mapidxs=xs(15)foldr(:)[]xs=xs(16)foldrcn(buildg)=gcn(17)Figure3.Somesimplelaws.Inthespiritofdenotationalsemantics[24],typesareconsideredassetsequippedwiththepartialorderv,theleastelement?,andlimitsofallnon-emptychains.Arelationbetweensuchpointedcompletepartialordersiscalledstrictifitcontainsthepair(?;?).Arelationiscalledcontinuousifthelimitsoftwochainsofpair-wiserelatedelementsareagainrelated.Astrictandcontinuousre-lationiscalledadmissible.Afunctioniscalledmonotonicifitpre-servestheapproximationorder.AllfunctionsdenableinHaskellaremonotonicandcontinuous.Notethatinthelaws(8)and(10)fromFigure3theapproximationvmaynotbereplacedbyequality,becausexsmightbeaninnitelistinbothcases,giving?ontheleft-handsidesbutnotnecessarilyontheright-handsides.Further,notethatlaw(17)—whichisattheheartofshortcutde-forestationforHaskelllists[10]—doesnotnecessarilyholdifgisdenedusingstrictevaluationwiththepolymorphicprimitiveseq(seethecounterexampleinAppendixB).Thisisso,becauseseq—beingavailableateverytype—weakensthefreetheoremsforHaskell.Forthesamereason,wewillfornowassumethatseqisnotusedintheprogramstobetransformed.Sincestrictdatatypesandthestrictapplicationfunction$!aredenedintermsofseq,theywillalsobeexcluded.Then,inAppendixB,wewillconsiderhowtheproofsofourtheoremsfareinthepresenceofthestrictevaluationprimitive,andwillseethat—incontrasttoshortcutde-forestation—ouroptimizationsarenothinderedbyallowingseqintothefunctionallanguage.3MoreConcatenatesVanishInthissectionwepresentourtechniqueforeliminatingcallstothelistconcatenationfunction.Werstgiveatheoremwithformalproofandthenconsiderexamplestoillustratehowprogramsareoptimizedbyapplyingthistheorem.3.1ATheoremForFreeConsidertherank-2polymorphicfunctionvanish++asdenedinFigure1.Inthefollowingwepresentatheoremthatgivesanal-ternative—butequivalent—semanticsforvanish++.Theproofofthisresultexploitsthefreetheoremthatcomeswitheverypoly-morphictype[28],andisgiveninasimilarstyleasthecorrectnessproofforshortcutdeforestationin[10].Notethattherelationcho-seninthefreetheoremmustbeadmissible(strictandcontinuous),becauseweconsiderprogramsinafunctionallanguagewithx-pointrecursionandpossiblenon-termination.THEOREM1.ForeveryxedtypeAandfunctiong::8b:b!(A!b!b)!(b!b!b)!bholds:g[](:)(++)=vanish++g(18)PROOF.Thefreetheoremassociatedwithg'stypeisthatforeverychoiceoftypesBandB0,valuesn::B,n0::B0,c::A!B!B,c0::A!B0!B0,a::B!B!Banda0::B0!B0!B0,andanadmissiblerelationRBB0,thefollowingimplicationholds:(n;n0)2R^(8x::A;(l;l0)2R:(cxl;c0xl0)2R)^(8(l1;l01)2R;(l2;l02)2R:(al1l2;a0l01l02)2R))(gnca;gn0c0a0)2R:HerethepolymorphicfunctiongissilentlyinstantiatedattypeBintheapplicationton,canda,andattypeB0intheapplicationton0,c0anda0.InstantiatingtheabovewithB=[A],B0=[A]![A],n=[],n0=id,c=(:),c0=(lxhys!x:(hys)),a=(++)anda0=(),weobtain:([];id)2R^(8x::A;(l;l0)2R:(x:l;(lxhys!x:(hys))xl0)2R)^(8(l1;l01)2R;(l2;l02)2R:(l1++l2;l01l02)2R))(g[](:)(++);gid(lxhys!x:(hys))())2R:ConsidertherelationR=f(p;q)2BB0j8u::[A]:p++u=qug:Fromlaws(1)and(2)followsthestrictnessofR.ContinuityofRfollowsfromthefactsthat(++)isacontinuousfunctionandthatfunctionapplicationisalsocontinuous.Hence,Risadmissibleand thuscanbeusedintheaboveimplication.Then,thethreeconjunctsinthepreconditionofthisimplicationreadasfollows:(i).8u::[A]:[]++u=idu(ii).foreveryx::A,l::[A]andl0::[A]![A]:(8u::[A]:l++u=l0u))(8u::[A]:(x:l)++u=(lxhys!x:(hys))xl0u)(iii).foreveryl1;l2::[A]andl01;l02::[A]![A]:(8u1::[A]:l1++u1=l01u1)^(8u2::[A]:l2++u2=l02u2))(8u::[A]:(l1++l2)++u=(l01l02)u):Conditions(i)and(ii)followfromthedenitionsof(++)andidandbybeta-reduction;condition(iii)followsfromlaw(3)andthedenitionof().Theinstantiatedfreetheoremthusimplies8u::[A]:(g[](:)(++))++u=gid(lxhys!x:(hys))()u;whichforu=[]gives—usinglaw(4)andthedenitionofvanish++—thefollowingcalculation:g[](:)(++)=(g[](:)(++))++[]=gid(lxhys!x:(hys))()[]=vanish++g:NotehowintheaboveprooftherelationRcapturestheessenceofHughes'pairoffunctionsrepandabs[12].3.2ApplyingtheTheoremTheorem1canbeusedtooptimizelistproducersfromwhichalllistconstructorsandoccurrencesof(++)havebeenabstracteduni-formly.Thisproperabstractionisguaranteedbythepolymorphicargumenttypeofvanish++.Hence,iftheHaskelltype-checkerac-ceptsanexpressionvanish++g,thenthisexpressionreallyhasthesamesemanticsasg[](:)(++),butmightbedramaticallymoreef-cientduetoanoptimizedimplementationofconcatenation.Wegivetwoexamplesintheremainderofthissection.Example1.Considerthefollowingdenitions,implementingtherecursivesolutiontothe“TowersofHanoi”problem:dataPos=LjMjRhanoi::Int![(Pos;Pos)]hanoit=letf1pqr=(p;q):[]f(s+1)pqr=(fsprq)++((p;q):(fsrqp))inftLRMAbstractingfromthelistconstructors[]and(:),andfrom(++),thefunctionhanoicanalsobewrittenas:hanoit=(lnca!letf1pqr=(p;q)`c`nf(s+1)pqr=(fsprq)`a`((p;q)`c`(fsrqp))inftLRM)[](:)(++)Usinglaw(18)fromTheorem1,thisisequivalentto:hanoi?::Int![(Pos;Pos)]hanoi?t=vanish++(lnca!letf1pqr=(p;q)`c`nf(s+1)pqr=(fsprq)`a`((p;q)`c`(fsrqp))inftLRM)Thestandardcompiler-built-insimplifyingtechniquesinliningandbeta-reduction[21]areenoughtoautomaticallyobtainthevarianthanoi?t=(letf1pqr=lys!(p;q):ysf(s+1)pqr=lys!fsprq((p;q):(fsrqpys))inftLRM)[]thatcloselycorresponds—bylet-oating[20]—totheresultofapplyingWadler'sintroductionofaccumulatingparameters[27]tohanoi(asshowninSection7.2)andhenceenjoysthesameruntimebenets.3Asafurtherexampleforourtechnique,weconsiderwhatwecandoaboutafunctionthatwastermedsillyin[27]andcouldnotbeoptimizedthere.Example2.Considerthefollowingdenition:atten::8a:[[a]]![a]attenl=letsillyx[]=xsillyx(y:ys)=silly(x++y)ysinsilly[]lIfwetrytousevanish++bysimplyabstractingfromthevisiblelistconstructors[]and(++),an“Inferredtypeislesspolymorphicthanexpected”-errorisproducedbythetype-checker.Thisguidesustoadditionallyabstractfromthelistconstructorsofyinthesecondequationofsilly,byusinglaw(16)asfollows:attenl=letsillyx[]=xsillyx(y:ys)=silly(x++(foldr(:)[]y))ysinsilly[]lNotethattheadditionaltraversalofywithfoldr(:)[]makesthedenitionlessefcient.However,wecannowusevanish++type-correctlyandobtain:atten?::8a:[[a]]![a]atten?l=vanish++(lnca!letsillyx[]=xsillyx(y:ys)=silly(x`a`(foldrcny))ysinsillynl)Performancemeasurementsshowthatthisdenitionisnotsosillyafterall.Infact,thefunctionatten—withaworst-casetimecomplexityquadraticinthesizeofitsargument—hasbeentransformedtoatten?,whichhasthesamesemanticsbutonlyrequireslineartime:n=1000300050007000atten[[i]ji [1::n]]0.181.64.59.0atten?[[i]ji [1::n]]0.0030.0080.0140.019Itwouldbeinterestingtomaketheguidancebythetype-checkersuggestedabovemoreexplicit.3Sofar,wehavepresentedthreeexamples(forpart,hanoiandatten)ofapplyingourmethodbyhand.Theapproachtolistab-stractionwastoreplacesomeoccurrencesoflistconstructors[],(:)and(++)byvariables,andthentousetherank-2polymorphictype-checkerfordetectingwhetherthisreplacementwassufcienttoexpressthelistproducerwithvanish++.Chitil[6]reducedtheproblemoflistabstractionforshortcutdeforestationtoadecidablepartialtypeinferenceproblem.Thus,heobtainedalinear-timeal-gorithmtoderivebuild-formsoflistproducersbyabstractionfrom[]and(:).Byadditionallytreating(++)asjustanotherlistcon-structor,Chitil'sideacanbeextendedtoautomaticallyderivethedesiredvanish++-formsforouroptimizationtechnique.Thedeci-sionforwhichfunctionstoattemptanautomaticabstraction,couldcomefromtheuserviaapragmathatwouldcausethecompilertocheckwhichdatamanipulatingoperationsareusedinsideagivendenitionandwhetheracorrespondingvanish-combinatorexists. Alternatively,vanish++andrelatedcombinators—e.g.,fromthenextsections—maybeconsciouslyusedwhenwritingnewpro-grams.Forexample,aprogrammermightwanttoimplementalistproducingalgorithmthatismostnaturallyexpressedusingrepeatedconcatenationsormaps.Beingawareofthedangerofinefcienciesduetointermediateresults,shecanchoosetheappropriatevanish-functionfromapre-denedmodulecontaininglotsofsuchcom-binatorsfordifferentdatamanipulations.Notethatinordertodoso,theuserdoesnotneedtoknowtheactualdenitionsofthosevanish-functions,butonlyaspecicationoftheirsemanticsaspro-videdbystatementslikeinTheorems1,2,3and4ofthispaper.4AGeneralMethodologyInthissectionwedemonstratethatthemethodfromtheprevioussectionisaninstanceofamoregeneralnewmethodologyforsup-plyingdatatypeswithcheapversionsoffrequentlyusedmanipulat-ingoperations.Thiswillbeexempliedbyaddingtothestandardlistdatatypesupportforreversinglistswithoutefciencypenaltiesduetorepeatedreversals.Asanexamplewherethisisuseful,con-siderthefollowingna¨vedenitionofafunctionforshufingalist:shufe::8a:[a]![a]shufe[]=[]shufe(x:xs)=x:(reverse(shufexs))Sinceeveryapplicationofreversetakestimelinearinthelengthofitsargument,theruntimeofshufeisquadraticinthelengthoftheinputlist.Withthetechniquedevelopedinthenextthreesubsec-tionswewillobtainaversionofshufeneedingonlylinearruntime.4.1FreezingandAbstractionFirstly,weintroducearicherdatatypewithanadditionalconstruc-torfortheadditionaloperationthatwewanttosupport,andadaptshufetoproduceavalueofthatrichertype:dataLista=NiljConsa(Lista)jRev(Lista)shufe0::8a:[a]!Listashufe0[]=Nilshufe0(x:xs)=Consx(Rev(shufe0xs))Thisstepofintroducingdataconstructorsfor“externalfunctions”—here:Revforreverse—isinspiredbythatof“freezing”inK¨uhnemannetal.[15].Inordertoexposetheseconstructorsforlaterfusion,weusethefollowingbuildList-function:buildList::8a:(8b:b!(a!b!b)!(b!b)!b)!ListabuildListg=gNilConsRevtouniformlyabstracttheconstructorsofLista,giving:shufe00::8a:[a]!Listashufe00l=buildList(lncr!letf[]=nf(x:xs)=cx(r(fxs))infl)4.2EfcientConversionSinceweareinterestedinavalueoftheoriginaltype[a]andnotoftheenricheddatatypeLista,weneedaconversionfunctionfromtheenrichedtothestandardlisttype.Usingthefollowingfunction:foldList::8a:Lista!(8b:b!(a!b!b)!(b!b)!b)foldListNilncr=nfoldList(Consxxs)ncr=cx(foldListxsncr)foldList(Revxs)ncr=r(foldListxsncr)atrivialsolutionwouldbe:convert::8a:Lista![a]convertl=foldListl[](:)reverseHowever,thisisclearlynotagoodsolution,becausethecomposedfunctionconvertshufe00willbelessefcientthantheoriginalshufe,asthetraversalwithfoldListonlycausesadditionalruntimeoverheadwhilestillrepeatedapplicationsofreverseoccur.Hence,wehavetoimproveconvertbyinventinganewfunctionconvert?thatcanperformatleastthesameconversionsmoreef-ciently.Moreprecisely,wewanttohavethesemanticpropertyconvertvconvert?andconvert?shouldhaveabetterruntimeefciencythanconvert.Thesolutionintheparticularcaseunderconsiderationhereistointroducetwomutuallyrecursivefunctionswithaccumulatingpa-rametersasfollows2:convert?::8a:Lista![a]convert?l=leth1Nilys=ysh1(Consxxs)ys=x:(h1xsys)h1(Revxs)ys=h2xsysh2Nilys=ysh2(Consxxs)ys=h2xs(x:ys)h2(Revxs)ys=h1xsysinh1l[]Inordertobeabletoapplyshortcutdeforestationinthenextsub-section,weexpressthisasa—higher-orderandtupled—foldList:convert?l=fst(foldListllys!(ys;ys))(lxhys!(x:(fst(hys));snd(h(x:ys))))(lhys!swap(hys))[])4.3FusionFollowingthedevelopmentsfromtheprevioustwosubsections,theshufingofalistl—ofthestandardlisttype—canbeperformedbycomputingconvert?(shufe00l).Now,wecalculatebyinlining:convert?(shufe00l)=fst(foldList(shufe00l)(lys!(ys;ys))(lxhys!(x:(fst(hys));snd(h(x:ys))))(lhys!swap(hys))[])=fst(foldList(buildList(lncr!letf[]=nf(x:xs)=cx(r(fxs))infl))(lys!(ys;ys))(lxhys!(x:(fst(hys));snd(h(x:ys))))(lhys!swap(hys))[]):2Aninterestingalternativeistoreuseanalreadyexistingvanish-combinatorforexpressinganewconversion,inthiscase:convert?l=vanish++(lnca!leth1Nil=nh1(Consxxs)=x`c`(h1xs)h1(Revxs)=h2xsh2Nil=nh2(Consxxs)=(h2xs)`a`(x`c`n)h2(Revxs)=h1xsinh1l) Byshortcutdeforestation[14]fortheListtypeconstructor,weknowthatforeverytypeAandfunctiongwithg::(8b:b!(A!b!b)!(b!b)!b)thefollowingfusionlawholds:foldList(buildListg)=g:Hence,theabovecalculationcanbecontinued:=fst((lncr!letf[]=nf(x:xs)=cx(r(fxs))infl)(lys!(ys;ys))(lxhys!(x:(fst(hys));snd(h(x:ys))))(lhys!swap(hys))[]):Inthenextsubsectionwewillseethatthiscalculatedprogramshuf-esthelistlmuchmoreefcientlythantheoriginalshufe-functiondid.Thereasonisthattheoriginaloccurrencesoflistreversehavebeenreplacedbyanoptimizedimplementationmakinguseofthefactthattwoconsecutivereversalscanceleachother.4.4ForGeneralUseSimilarlytothedevelopmentforshufeinthelastthreesubsec-tionswecouldalsoproceedforotherlistproducingfunctionsthatusereverse.Inordertoincreasemodularity,weintroducethevanishrev-combinatorinFigure4bygeneralizingthecalculationresultintheprevioussubsection.vanishrev::8a:(8b:b!(a!b!b)!(b!b)!b)![a]vanishrevg=fst(g(lys!(ys;ys))(lxhys!(x:(fst(hys));snd(h(x:ys))))(lhys!swap(hys))[])Figure4.Denitionofvanishrev.Then,wecanreplaceshufefromthebeginningofthissectionby:shufe?::8a:[a]![a]shufe?l=vanishrev(lncr!letf[]=nf(x:xs)=cx(r(fxs))infl)Runtimesofshufeandshufe?arecomparedinthefollowingtable:n=200040006000800010000shufe[1::n]0.331.32.85.08.0shufe?[1::n]0.0050.010.0160.020.025Thedevelopmentsofarreliedonthefactthatindeedwehaveconvertvconvert?(andonthecorrectnessofshortcutdeforesta-tion).Insteadofprovingthisauxiliaryfact,wedirectlygivearesultsimilartoTheorem1thatcanthenbeusedtotransformshufeintoshufe?withoutneedingthecalculationsinthepreviousthreesub-sections.ThosecalculationsandtheListdatatypeonlyappearedforillustratingourgeneralmethodology(assummarizedinthenextsubsection)ofhowtoobtainoptimizingvanish-combinators.THEOREM2.ForeveryxedtypeAandfunctiong::8b:b!(A!b!b)!(b!b)!bholds:g[](:)reversevvanishrevg(19)Thetheoremisaconsequenceofthefreetheoremforthetypeofg.Itsproofusingthelaws(2)–(9)fromFigure3isverysimilarto—buteasierthan—theproofofTheorem3inAppendixA.Thatiswhyweomitithere.Weonlynotethattheapproximationvemerges,becauseinlazyfunctionalprogramminglanguages,whereinnitedatastructuresmightbepresent,wehavereversereversevid—cf.law(8)—butnotreversereverse=id.Thisisnotharmful,becausewecanstillsafelyoptimize(g[](:)reverse)to(vanishrevg),whichis“atleastasdened”,wheneverthisreplacementistype-correct.Forourexamplethisimpliesshufevshufe?,andthereplacementshufe?isnotonlymoreefcient,butalsohas—incertaincontexts—abetterterminationbehaviorthantheoriginalspecicationofshufe.Forexample,wehave:shufe[1::]=1:?vshufe?[1::]=1:3:5:7:9:11:4.5TheMethodologySummarizedWeinformallysummarizeourmethodologyofeliminatingmanip-ulatingoperationsf1;:::;fnforsomealgebraicdatatypeDbythefollowingsteps:1.ExtendDtoanewdatatypeD0withadditionalconstruc-torsforthemanipulatingoperationsf1;:::;fnandexpressD-producersasbuildsforthisextendeddatatypeD0,freezingf1;:::;fnasconstructors(compareSection4.1).2.Inventanefcientconversionfunctionconvert?D0!DfromD0toDandexpressitusingafoldoverD0(compareSection4.2).3.Introduceavanish-functionthatcomposesD0-producerswithconvert?D0!D:vanishf1;:::;fng=convert?D0!D(buildD0g);andusefusionofbuildD0vs.foldD0toeliminatetheinter-mediateresultoftypeD0inthisdenition.Finally,usethefreetheoremofg'stypetoprovethatvanishf1;:::;fnhasthein-tendedsemantics—i.e.,issuitableasreplacementofbuildD0forexpressingD-producersthatusef1;:::;fn—thusjustify-ingthechoiceofconvert?D0!D(compareSections4.3and4.4).Thekeystepthatrequiresingenuityhereisofcoursetheinventionofanefcientconversionfunction,whichnaturallydependsonthesemanticsoftheinvolvedoperationsf1;:::;fn.Whileitisunlikelythatonecanndageneralstrategyreplacingthiscreativestep,thechanceofsuccessisratherhighiff1;:::;fnarerelatedbyarichalgebraictheoryofequationsorapproximations.Inparticular,oneshouldconsidersuchrelationshipsforwhichoneofthetwosidesofanequation(orthemoredenedsideofanapproximation)ismorefavorablewithrespecttoefciency.Forexample,thederivationofvanish++wasguidedbylaw(3),whereasthegivenimplementationoftheconvert?-functioninSection4.2wasmotivatedbylaw(8)inFigure3.Law(12)playsasimilarroleforoptimizingrepeatedapplicationsofmapinthenextsection. 5TheConcatenate,ReverseandMapVanishInthissectionwepresenttheresultofapplyingourmethodology—assummarizedinSection4.5—tothestandardlistdatatype,eliminatingthemanipulatingoperations(++),reverseandmap3.Weobtainthe—ratherscary—denitionofvanish++;rev;mapinFigure5.However,oncewehaveprovedthefollowingtheorem,thereisnoneedfortheuserofourcombinatorlibrary[1]tobeawareofthisdenition.THEOREM3.ForeveryxedtypeAandfunctiong::8b:b!(A!b!b)!(b!b!b)!(b!b)!((A!A)!b!b)!bholds:g[](:)(++)reversemapvvanish++;rev;mapg(20)ThetheoremisprovedinAppendixAasadirectconsequenceofthefreetheoremforg'stype,usingdenitionsfromFigure2andthelaws(2)–(15).WeshowhowthetheoremisappliedinanexamplethatwillalsobeusedforcomparisonwithalisteliminationapproachbyshortcutdeforestationforpolymorphicallyrecursiveworkersinSection7.1.Example3.Considerthefollowingspecicationofafunctioninitsthatreturnsthelistofinitialsegmentsofitsargumentlist—e.g.,inits[1::4]=[[];[1];[1;2];[1;2;3];[1;2;3;4]]—:inits::8a:[a]![[a]]inits[]=[[]]inits(x:xs)=[]:(map(x:)(initsxs))Byabstractingfromthelistconstructorsoftheouterresultlist—includingmap—andusingTheorem3,initscanbereplacedby:inits?::8a:[a]![[a]]inits?l=vanish++;rev;map(lncarm!letf[]=[]`c`nf(x:xs)=[]`c`(m(x:)(fxs))infl)Notethatabstractingalsofromtheconstructors[]and(:)oftheinnerlistswouldnotbetype-correct4.Runtimesofinitsandinits?tocomputethesamenestedlistsarecomparedinthefollowingtable:n=10002000300040005000inits[1::n]0.351.33.26.09.0inits?[1::n]0.080.30.71.32.0Thesemeasurementsshowanimprovementbyaconstantfactor,becauseinits?avoidstherepeatedmappingoffunctionsoftheform(x:)overintermediatelistsbetweenrecursivecalls.Inliningthedenitionofvanish++;rev;mapwouldshowthatinsteadtheinitial3Onlymappingoffunctionsf::A!AforsometypeAiscon-sidered,becauseotherwisevanish++;rev;mapwouldrequirepolymor-phicrecursionandarank-3typewithquanticationovertypecon-structors.4Unfortunately,themessagesproducedbycurrentHaskellim-plementationsincaseofinsufcientpolymorphismareoftennotveryhelpfulindiagnosingsucherrors.Moreadvancedtypeexpla-nationandcorrectiontechniquesexist—see,e.g.,[29,8,18]—butnotinthepresenceofhigher-rankedpolymorphism.segmentsareaccumulatedinafunctionallistrepresentation.Sincetheoverallnumberof(:)-operationsrequiredforbuildingthenestedoutputlistisstillquadraticinthelengthoftheinputlist,theasymp-toticcomplexityisnotchangedhere.36TheConcatenateVanishesfromNestedListsAlreadyHughesnotedthatnotallinstancesofhisconcatenateop-timizationbyrepresentinglistsasfunctions[12]canbeachievedthroughintroductionofaccumulatingparameters.Asanexampleheconsidersaninefcientfunctioneldsforbreakingupalistofcharactersintoalistofwords:elds::[Char]![[Char]]eldss=letf[]=[]f(x:xs)jx==00=fxsjotherwise=g[x]xsgw(x:xs)jx==00=g(w++[x])xsgwxs=w:(fxs)infsAlsoourvanish++;rev;map-combinatorcannoteliminatetherepeatedconcatenationsthatbuildupthesinglewordsintheresultlist.Thereasonisthattheseconcatenationstakeplaceontheinnerlistsofelds'result,incontrasttotheexampleinitsfromthelastsection,wherethemapsovertheouterlistwhereeliminated.Ofcourse,weshouldnotexpectasuccessfuloptimizationhere,becausevanish++andvanish++;rev;mapweredevelopedtoeliminatefunctioncallsto(++)::[a]![a]![a]fromalistproducerwithresulttype[a]andnotfromonewithresulttype[[a]].Therealizationthatwehavetodealwiththisasanewdatatypedifferentfrom[a]pavesthewaytosuccessforoptimizingalsocon-catenationsontheinnerlists.Namely,wecanapplyourmethodol-ogyassummarizedinSection4.5toeliminatemanipulatingopera-tions(++)::[a]![a]![a]and(++)::[[a]]![[a]]![[a]]fromproducersofnestedlistsofthealgebraicdatatype[[a]].Therefor,wesimplyapplythetrickofreplacinglistsbyfunctionstwice,onceontheinnerlistsandonceontheouterlist.Theout-comeisthecombinatorvanish++;++inFigure6,withsemanticsasgivenbythetheorembelow.vanish++;++::8a:(8bg:b!(a!b!b)!(b!b!b)!g!(b!g!g)!(g!g!g)!g)![[a]]vanish++;++g=gid(lxhys!x:(hys))()id(lhhsyss!(h[]):(hsyss))()[]Figure6.Denitionofvanish++;++.THEOREM4.ForeveryxedtypeAandfunctiong::8bg:b!(A!b!b)!(b!b!b)!g!(b!g!g)!(g!g!g)!gholds:g[](:)(++)[](:)(++)=vanish++;++g(21)Theproofofthistheorem—usingthefreetheoremofg'stypeandthelaws(1)–(4)fromFigure3—isonlyslightlymoredifcultthantheproofofTheorem1,butisomittedhereduetospaceconstraints. vanish++;rev;map::8a:(8b:b!(a!b!b)!(b!b!b)!(b!b)!((a!a)!b!b)!b)![a]vanish++;rev;mapg=fst(g(lfys!(ys;ys))(lxhfys!((fx):(fst(hfys));snd(hf((fx):ys))))(lh1h2fys!(fst(h1f(fst(h2fys)));snd(h2f(snd(h1fys)))))(lhfys!swap(hfys))(lkhfys!h(fk)ys)id[])Figure5.Denitionofvanish++;rev;map.Inordertoshowhowvanish++;++eliminatesconcatenateoperationsfromproducersofnestedlists,weapplyittoelds.Example4.Considerthefunctiondenitionofeldsfromabove.Abstractingfromthelistconstructors[],(:)and(++)type-correctlyandusinglaw(21)fromTheorem4,thisdenitionisequivalenttothemuchmoreefcient:elds?::[Char]![[Char]]elds?s=vanish++;++(lncannccaa!letf[]=nnf(x:xs)jx==00=fxsjotherwise=g(x`c`n)xsgw(x:xs)jx==00=g(w`a`(x`c`n))xsgwxs=w`cc`(fxs)infs)Byinliningthedenitionofvanish++;++andperformingsomebeta-reductionsandlet-oating,weobtainthevariantelds?s=letf[]=idf(x:xs)jx==00=fxsjotherwise=g(lys!x:ys)xsgw(x:xs)jx==00=g(w(lys!x:ys))xsgwxs=lyss!(w[]):(fxsyss)infs[]whichdiffersfromHughes'resultonlyinthatthefunctionalrepre-sentationisalsousedforproductionoftheouterlist.37ComparisonwithRelatedWork7.1ShortcutDeforestationGill[9]givesanexampleofarecursivefunctionconsumingitsownresultviafoldrandshowshowaworker/wrapperschemeofshort-cutdeforestationcanimprovetheasymptotictimecomplexityofthisfunctionbyremovingintermediatelistsbetweenrecursivecallswithlaw(17).Inasimilarway,Chitil[7]usestherepresentationofmapasafoldrtotransformthespecicationofinitsfromExam-ple3intoessentiallythefollowing:inits0::8a:[a]![[a]]inits0l=letfcn[]=[]`c`nfcn(x:xs)=[]`c`(f(lysyss!(x:ys)`c`yss)nxs)inf(:)[]lThisisachievedbyrstlysplittinginitsintoawrapperandaworkerthatusespolymorphicrecursion,andthenapplyingshortcutdefor-estationinsidetheworker.Whilethereisnoobviousconnectionbetweeninits?andinits0,theirruntimesarecomparable:n=10002000300040005000inits?[1::n]0.080.30.71.32.0inits0[1::n]0.070.30.81.22.0However,inits?leavesroomforfurtherimprovementbyusingamorespecializedvanishmap-combinatorinsteadofthefunctionvanish++;rev;map.Notethatsuchaswitchfromusingonevanish-combinatortousingamoregeneraloramorespeciconeisusuallyeasy,inthiscasebydroppingaandrfromthelistofl-abstractedvariablesinthedenitionofinits?.Ontheotherhand,Chitil'smethoddoesnotworkforfunctionslike,e.g.,partandelds.Also,hediscouragestheuseofhisownap-proach(toautomaticallyobtaininits0bytypeinferencebasedde-forestation)inthefollowingwords:“Thispoweris,however,adouble-edgedsword.Asmallsyntacticchangeofaprogram[:::]maycausedefor-estationtobenolongerapplicable,andthuschangetheasymptoticcomplexityoftheprogram.Itmayhencebearguedthatsuchfar-reachingmodicationsshouldbelefttotheprogrammer.”Ourapproachcanprovidetheprogrammerwithexactlythiscontrol.Moreover,itsolvestheprobleminSection3.4.3of[7],becauseourmethodcanoptimizethefoldr-formofthena¨velistreverse:rev::8a:[a]![a]revl=foldr(lxh!h++[x])[]lbytransformingitinto:rev?::8a:[a]![a]rev?l=vanish++(lnca!foldr(lxh!h`a`(x`c`n))nl)=foldr(lxhys!h(x:ys))idl[]Thus,ourmethodhaseliminatedtheinefcientconcatenationswithoutdestroyingthepossibilityofusingshortcutdeforestationwithrev?aslistconsumer.Ofcourse,ourtechniquedoesnotsupersedeshortcutdeforestation,becauselaw(17)isalsoapplicabletoremoveintermediatelistsinavarietyofcasesthatarenotaddressedbyourapproachofeliminat-ingparticulardatamanipulatingoperations.Rather,thetwotech-niquescomplementeachotherquitewell.Notehowever,thatpro-gramsusingthevanish-combinatorsbenetfromouroptimizationmethodwithoutrequiringanyadditionalsupportfromthecompiler,whereasoptimizationbyshortcutdeforestationcruciallydependsonacompilerthatmakessomenontrivialefforttoapplylaw(17)—oranequivalentofit—atasmanyplacesaspossible.Svenningsson[26]proposesanewdestroy=unfoldr-rulethatgivesrisetofurtherpossibilitiesofintermediatelistremovalandisinmanyrespectsadualoflaw(17).Sinceourvanish-combinatorsvarytheideaofshortcutdeforestation—byabstractingnotonlyoverdataconstructors,butalsooverotherdatamanipulatingoper-ations—itmightbeinterestingtoinvestigatewhetheradualvaria-tionofthedestroy=unfoldr-techniquealsoexists.7.2AccumulatingParametersWadler[27]presentedatransformationthateliminatesconcatenateoperationsbyintroducingaccumulatingparameters.Forexample, thefunctionhanoifromExample1isimprovedtohanoi0bygener-alizingthelocalfunctionftof++withanadditionallistargumentandapplyingasetofrewritelawstotheright-handsidesofana¨vedenitionoff++,nallyyielding:hanoi0::Int![(Pos;Pos)]hanoi0t=letf++1pqrys=(p;q):ysf++(s+1)pqrys=f++sprq((p;q):(f++srqpys))inf++tLRM[]Thesuccessofthismethoddependsonthecharacterizationoffunc-tionsascreative.Forexample,anattemptedapplicationtothein-troductoryexamplepartproducesthefollowingdenition:part0::8a:(a!Bool)![a]![a]part0pl=letf++[]zys=z++ysf++(x:xs)zys=ifpxthenx:(f++xszys)elsef++xs(z++[x])ysinf++l[][]Notethatheretheconcatenatedoesnotvanish.Thecharacteriza-tionofpartasplagiarizingcomessomewhatasasurprise,becausepartclearlydoesallocateeachconscellinitsresultandthusshouldbemanageable—andissowithournewmethod.Ontheotherhand,itiseasytoseethatallfunctionsfulllingWadler'ssyntactictestforcreativityalsoallowthetype-correctab-stractionfromlistconstructors[],(:)and(++),andcanthusalsobetransformedbyourtechnique.Hence,vanish++capturesthein-troductionofanaccumulatingparameterascanbeseenbycompar-ingtheabovefunctionhanoi0withthenaldenitionobtainedforhanoi?inExample1.Albertetal.[3]achievetheeffectsofWadler'stransformationforfunctionallogicprogramsbytemporarilyintroducingdifference-liststhatarethenreplacedbyaccumulatingparameters.Theirtech-niquefailsforthesameexamplesasWadler's,henceitwouldbeinterestingtostudyourmethodalsointhemoregeneralframeworkoffunctionallogicprogramminglanguages.K¨uhnemannetal.[15]generalizelistconcatenationtotreesub-stitutionfunctionsandeliminatesuchsubstitutionfunctionsfromtop-downtreetransducermodulesbyintroducingaccumulatingpa-rameters.Their“integrationstep”couldalsoberealizedintheframeworkofourmethodology,thusgeneralizingtheiraccumula-tiontechniquetoarbitraryfunctionsthatproducetrees.Bird[4],Huetal.[11]usecalculationalmethodstoderiveaccumu-lativeprograms—intheirsettinghigher-orderfoldsoveralgebraicdatatypes—fromrst-orderfolds,suchastransformingrevfromtheprevioussubsectionintorev?(whichnoneoftheotheraccu-mulationmethodsdiscussedhereachieves).Theirtechniqueshaveawiderscopeinderivingnewalgorithmsratherthan“just”elimi-natinginefcientfunctioncalls.Ontheotherhand,theycanonlyhandleprogramsexpressedwithaxedsetofrecursionoperators,andmust—ofcourse—failwhereintroductionofaccumulatingparameterscannotremovetheinefciencies.FurtherapproachesforderivingaccumulativealgorithmsarecomparedbyBoiten[5].7.3AbstractDataTypesOurstepsof“freezing”and“efcientconversion”havesimilargoalsasthetransitionfromatermrepresentationtoacontext-passingrepresentationinHughes'methodologyforimplementingdomain-speciclanguages[13].Infact,onemightconceive,e.g.,ourdevelopmentsinSections4.1–4.3astheconstructionofyetan-othernovelrepresentationoflistsinthesenseof[12],butthistimewithsupportforefcientreversalinsteadofconcatenation.Hence,thedescribedmethodologyisanotherwaytosynthesizeefcientimplementationsofabstractdatatypesfromtheknowledgeaboutalgebraicpropertiesoftheinvolvedoperations.WhileHughesusesthisknowledgetoguidethechoiceofthecontextsfromwhichtoabstract,weemployitinthedenitionofconvert?-functions.Therank-2polymorphicvanish-combinatorsencodesuchefcientimplementationsbyintroducingaformofanonymousabstractdatatypes.Thislocalencapsulationlimitscross-functionoptimization,butisthekeytoprovingthecorrectnessoftransformationsbyfreetheorems.Inparticular,therearenoanaloguesinHughes'method-ologytoourTheorems1–4thatembodyeachoptimizationinasin-glerule.Itisunclearhowotherencodingsofabstractdatatypescouldbeemployedsemanticallytodosuchproofsoreventofor-malizetheessenceoftransformationsinsuchaconciseway.Also,theexpressionofoptimizationsbyuniformabstractionfromdataconstructorsandmanipulatingfunctionspavesthewaytoautoma-tionusingChitil'sapproachofdataabstractionbytypeinference.8ConclusionInthispaperwedevelopedacombinatorlibrarytooptimizelistproducersinvolvingtheoperations++,reverseandmap.Thein-troducedmethodologyisalsoapplicabletoeliminateotherlistma-nipulations—e.g.,thewell-knownlter-function[1]—andfornestedlistsandotheralgebraicdatatypes.Wewouldliketofurtherinvestigatetheinterplaybetweenourtech-niqueandtheeliminationofintermediateresultsbyshortcutdefor-estation.AsseeninSection7.1,a“good”consumer(oneconsum-ingitsargumentuniformlywithfoldr)remains“good”afterapply-ingourtechnique.Moreover,wecanstraightforwardlyexpressthevanish-functionsinbuild-form,forexample:vanish++g=build(lcn!gid(lxhys!x`c`(hys))()n)Then,everyfunctionexpressedwithvanish++isa“good”producer(oneproducingitsresultuniformlywithbuild).Forexample,part?becomesa“good”producer,whilepartwasnone.Furthermore,onecandualizeChitil'sapproachof“deforestingfunctionsthatconsumetheirownresultwithfoldr”to“deforestingfunctionsthatconstructtheirownargumentwithbuild”,whichisthenalsoappli-cabletotheexamplespartandelds.Wehaveobservedvariousexampleswhereourmethodleadstodra-maticefciencyimprovements.However,ageneralstatementabouttherelationbetweentheruntimesoforiginalandtransformedpro-gramsishardtomake.Moran&Sands[19]arguethatWadler'saccumulationtechniquewillneverdegradeefciencybymorethanaconstantfactor.Webelievethatthesameistrueforourmoregen-eraltechniqueofconcatenateeliminationandwouldliketoinvesti-gatethisclaimformally.Forcomputingtheasymptotictimecom-plexityoftransformedprogramsthatusevanish++;rev;map,therun-timeforperformingtheoriginal++-,reverse-andmap-operationscanbeconsideredasconstantratherthanlinearinthelengthsoftheliststheyconsume.Inotherapplicationsofourmethodologytheefciencyofcoursedependsonthequalityofthechosenimple-mentationforconvert?.9AcknowledgmentsIwanttothankClausJ¨urgensenandArminK¨uhnemannforcom-mentsandusefulsuggestionsondraftsofthispaper.DiscussionswithOlafChitilandJosefSvenningssonandremarksbytheICFP refereesalsoprovidedinterestinginsightsandinspirationforfutureresearch.References[1]http://wwwtcs.inf.tu-dresden.de/voigt/Vanish.lhs.[2]TheHaskell98Report.http://haskell.org/onlinereport.[3]E.Albert,C.Ferri,F.Steiner,andG.Vidal.Improvingfunc-tionallogicprogramsbydifference-lists.InAdvancesinCom-putingScience,Penang,Malaysia,Proceedings,volume1961ofLNCS,pages237–254.Springer-Verlag,2000.[4]R.Bird.Thepromotionandaccumulationstrategiesintrans-formationalprogramming.ACMTrans.onProg.Lang.andSystems,6:487–504,1984.AddendumIbid.,7:490–492,1985.[5]E.Boiten.Themanydisguisesofaccumulation.TechnicalReport91-26,Dept.ofInformatics,UniversityofNijmegen,1991.[6]O.Chitil.Typeinferencebuildsashortcuttodeforestation.InInternationalConferenceonFunctionalProgramming,Paris,France,Proceedings,pages249–260.ACMPress,1999.[7]O.Chitil.Type-InferenceBasedDeforestationofFunctionalPrograms.PhDthesis,RWTHAachen,2000.[8]O.Chitil.Compositionalexplanationoftypesandalgorith-micdebuggingoftypeerrors.InInternationalConferenceonFunctionalProgramming,Florence,Italy,Proceedings,pages193–204.ACMPress,2001.[9]A.Gill.CheapDeforestationforNon-strictFunctionalLan-guages.PhDthesis,UniversityofGlasgow,1996.[10]A.Gill,J.Launchbury,andS.PeytonJones.Ashortcuttodeforestation.InFunctionalProgrammingLanguagesandComputerArchitecture,Copenhagen,Denmark,Proceedings,pages223–232.ACMPress,1993.[11]Z.Hu,H.Iwasaki,andM.Takeichi.Calculatingaccumula-tions.NewGenerationComputing,17:153–173,1999.[12]J.Hughes.Anovelrepresentationoflistsanditsapplica-tiontothefunction“reverse”.InformationProcessingLetters,22:141–144,1986.[13]J.Hughes.Thedesignofapretty-printinglibrary.InAdvancedFunctionalProgramming,volume925ofLNCS,pages53–96.Springer-Verlag,1995.[14]P.Johann.Shortcutfusion:Provedandimproved.InSe-mantics,Applications,andImplementationofProgramGen-eration,Florence,Italy,Proceedings,volume2196ofLNCS,pages47–71.Springer-Verlag,2001.[15]A.K¨uhnemann,R.Gl¨uck,andK.Kakehi.Relatingaccumula-tiveandnon-accumulativefunctionalprograms.InRewritingTechniquesandApplications,Utrecht,TheNetherlands,Pro-ceedings,volume2051ofLNCS,pages154–168.Springer-Verlag,2001.[16]J.LaunchburyandR.Paterson.Parametricityandunbox-ingwithunpointedtypes.InEuropeanSymposiumonPro-gramming,Link¨oping,Sweden,Proceedings,volume1058ofLNCS,pages204–218.Springer-Verlag,1996.[17]D.Leivant.Polymorphictypeinference.InPrinciplesofPro-grammingLanguages,Austin,Texas,Proceedings,pages88–98.ACMPress,1983.[18]B.McAdam.RepairingTypeErrorsinFunctionalPrograms.PhDthesis,UniversityofEdinburgh,2002.[19]A.MoranandD.Sands.Improvementinalazycontext:Anoperationaltheoryforcall-by-need.InPrinciplesofProgram-mingLanguages,SanAntonio,Texas,Proceedings,pages43–56.ACMPress,1999.[20]S.PeytonJones,W.Partain,andA.Santos.Let-oating:Movingbindingstogivefasterprograms.InInternationalConferenceonFunctionalProgramming,Philadelphia,Penn-sylvania,Proceedings,pages1–12.ACMPress,1996.[21]S.PeytonJonesandA.Santos.Atransformation-basedopti-miserforHaskell.Sci.ofComput.Prog.,32:3–47,1998.[22]A.Pitts.Parametricpolymorphismandoperationalequiva-lence.Math.Struct.Comput.Sci.,10:321–359,2000.[23]J.Reynolds.Types,abstractionandparametricpolymor-phism.InInformationProcessing,Paris,France,Proceed-ings,pages513–523.ElsevierSciencePublishersB.V.,1983.[24]D.Schmidt.DenotationalSemantics:AMethodologyforLanguageDevelopment.AllynandBacon,1986.[25]T.Sheard,Z.Benaissa,andM.Martel.Introductiontomulti-stageprogrammingusingMetaML.http://cse.ogi.edu/sheard/papers/manual.ps.[26]J.Svenningsson.Shortcutfusionforaccumulatingparametersandzip-likefunctions.InInternationalConferenceonFunc-tionalProgramming,Pittsburgh,Pennsylvania,Proceedings.ACMPress,2002.[27]P.Wadler.Theconcatenatevanishes.Note,UniversityofGlasgow,1987(revised,1989).[28]P.Wadler.Theoremsforfree!InFunctionalProgram-mingLanguagesandComputerArchitecture,London,Eng-land,Proceedings,pages347–359.ACMPress,1989.[29]J.Yang,G.Michaelson,P.Trinder,andJ.Wells.Improvedtypeerrorreporting.InImplementationofFunctionalLan-guages,Aachen,Germany,DraftProceedings,pages71–86,2000.AProofofTheorem3PROOF.Thefreetheoremassociatedwithg'stypeisthatforeverychoiceoftypesBandB0,valuesn::B,n0::B0,c::A!B!B,c0::A!B0!B0,a::B!B!B,a0::B0!B0!B0,r::B!B,r0::B0!B0,m::(A!A)!B!Bandm0::(A!A)!B0!B0,andanadmissiblerelationRBB0,thefollowingholds:(n;n0)2R^(8x::A;(l;l0)2R:(cxl;c0xl0)2R)^(8(l1;l01)2R;(l2;l02)2R:(al1l2;a0l01l02)2R)^(8(l;l0)2R:(rl;r0l0)2R)^(8k::A!A;(l;l0)2R:(mkl;m0kl0)2R))(gncarm;gn0c0a0r0m0)2R:InstantiatingthiswithB=[A],n=[],c=(:),a=(++),r=reverse,m=map,B0=(A!A)![A]!([A];[A])and:n0=(lfys!(ys;ys))c0=(lxhfys!((fx):(fst(hfys));snd(hf((fx):ys))))a0=(lh1h2fys!(fst(h1f(fst(h2fys)));snd(h2f(snd(h1fys)))))r0=(lhfys!swap(hfys))m0=(lkhfys!h(fk)ys); weobtain(modulobeta-equality):([];(lfys!(ys;ys)))2R^(8x::A;(l;l0)2R:(x:l;(lfys!((fx):(fst(l0fys));snd(l0f((fx):ys)))))2R)^(8(l1;l01)2R;(l2;l02)2R:(l1++l2;(lfys!(fst(l01f(fst(l02fys)));snd(l02f(snd(l01fys))))))2R)^(8(l;l0)2R:(reversel;(lfys!swap(l0fys)))2R)^(8k::A!A;(l;l0)2R:(mapkl;(lfys!l0(fk)ys))2R))(g[](:)(++)reversemap;gn0c0a0r0m0)2R:Ifwechoosetheadmissiblerelation5R=f(p;q)j8f::A!A;u::[A]:(mapfp)++uvfst(qfu)^(mapf(reversep))++uvsnd(qfu)g;thentheconjunctsinthepreconditionofthisimplicationreadasfollows(modulosomebeta-reductionsandthedenitionsoffstandsnd):(i).8f::A!A;u::[A]:(mapf[])++uvu^(mapf(reverse[]))++uvu(ii).foreveryx::A,l::Bandl0::B0:(8f::A!A;v::[A]:(mapfl)++vvfst(l0fv)^(mapf(reversel))++vvsnd(l0fv)))(8f::A!A;u::[A]:(mapf(x:l))++uv(fx):(fst(l0fu))^(mapf(reverse(x:l)))++uvsnd(l0f((fx):u)))(iii).foreveryl1;l2::Bandl01;l02::B0:(8f::A!A;v::[A]:(mapfl1)++vvfst(l01fv)^(mapf(reversel1))++vvsnd(l01fv)^(mapfl2)++vvfst(l02fv)^(mapf(reversel2))++vvsnd(l02fv)))(8f::A!A;u::[A]:(mapf(l1++l2))++uvfst(l01f(fst(l02fu)))^(mapf(reverse(l1++l2)))++uvsnd(l02f(snd(l01fu))))(iv).foreveryl::Bandl0::B0:(8f::A!A;v::[A]:(mapfl)++vvfst(l0fv)^(mapf(reversel))++vvsnd(l0fv)))(8f::A!A;u::[A]:(mapf(reversel))++uvfst(swap(l0fu))^(mapf(reverse(reversel)))++uvsnd(swap(l0fu)))5Again,itiseasytoseethat(?;?)2Rbylaws(7),(14)and(2).ContinuityofRisshownbyusingthefactsthatthere-lationviscontinuousandthattheinvolvedHaskell-functionsaremonotonicandcontinuous.(v).foreveryk::A!A,l::Bandl0::B0:(8f0::A!A;v::[A]:(mapf0l)++vvfst(l0f0v)^(mapf0(reversel))++vvsnd(l0f0v)))(8f::A!A;u::[A]:(mapf(mapkl))++uvfst(l0(fk)u)^(mapf(reverse(mapkl)))++uvsnd(l0(fk)u)):Usingreexivityandtransitivityofvandmonotonicityofthein-volvedHaskellfunctions,theseveconditionscanbeestablishedasfollows:(i).bythedenitionsofmap,(++)andreverse(ii).bythedenitionsofmapand(++)andthelaws(9),(11)and(3)(iii).bythelaws(11),(3)and(10)(iv).bythelaws(5),(8)and(6)(v).bythelaws(12)and(13).Asconsequenceoftheinstantiatedfreetheoremwethusobtain:(g[](:)(++)reversemap;gn0c0a0r0m0)2R:UsingourdenitionofR,fromthisfollows8f::A!A;u::[A]:(mapf(g[](:)(++)reversemap))++uvfst(gn0c0a0r0m0fu);whichforf=idandu=[]implies—usinglaws(4)and(15)andthedenitionofvanish++;rev;map—thefollowingcalculation:g[](:)(++)reversemap=(mapid(g[](:)(++)reversemap))++[]vfst(gn0c0a0r0m0id[])=vanish++;rev;mapg:BIntroducingStrictEvaluationTheequalitystatedinlaw(18)fromTheorem1doesnotneces-sarilyholdassuchifgmakesuseofthestrictevaluationprimitiveseq::8ab:a!b!b,denedinHaskell98bythefollowingequations:seq?b=?seqab=b;ifa6=?ThereasonisthatfortherelationRusedintheproofwehave(?;lys!?)2R,butfrom(p;q)2Rdoesnotnecessarilyfol-low(seq?p;seq(lys!?)q)2R,becauseseqdistinguishesbe-tween?and(lys!?).Hence,seqdoesnotmapR-relatedargu-mentstoR-relatedresults(asitshouldifwewanttoexploitpara-metricity),andthusRisnotsuitableforusageinthefreetheoremofg'stypeifgmaybedenedusingseq.ThisproblemcanbexedwithananalogousstrategyasusedinSection7of[28]toenrichthefunctionallanguagewiththexpointprimitivewhilepreservingparametricity,namelybyimposingfur-therrequirementsontherelationsusedinfreetheorems.Thatistosay,wemustrestrictourselvestorelationsthatrespectseq,inthesensethatforsuchrelationsAandBitmustfollowfrom(a;a0)2Aand(b;b0)2Bthatalso(seqab;seqa0b0)2B.Thiscanbeguar-anteedifeveryrelationXXX0usedintheproofcontainsallpairs(?;x0)withx0::X0,butnopair(x;?)withx6=?. Hence,weextendtherelationalinterpretationofbasetypes—andalsoofthexedtypeA—fromidentityrelationstothepartialor-dervandenlargeRtoR0=f(p;q)2BB0j8u::[A]:p++uvqug;whereadmissibilityofR0followsfromadmissibilityofvandmonotonicityandcontinuityof(++).Theinstantiatedfreetheoremthenreadsasfollows:([];id)2R0^(8x;x0::A;(l;l0)2R0:xvx0)(x:l;(lxhys!x:(hys))x0l0)2R0)^(8(l1;l01)2R0;(l2;l02)2R0:(l1++l2;l01l02)2R0))(g[](:)(++);gid(lxhys!x:(hys))())2R0:WithsimilarreasoningasintheproofofTheorem1—additionallyusingreexivityandtransitivityofvandmonotonicityof(:)and(++)—wecanvalidatethethreeconjunctsinthepreconditionandobtainfromtheimpliedconsequencethefollowingreplacementforlaw(18):g[](:)(++)vvanish++g(22)Likewise,theproofofTheorem4canbeadaptedtogiveinthepresenceofseq:g[](:)(++)[](:)(++)vvanish++;++g(23)Hence,ourconcatenateeliminationisstillapplicable,itjustbe-comesanapproximation—beingatleastasdened,butpotentiallyimprovingterminationbehavior—insteadofasemanticequality.NotethatthisisalreadythecaseforHughes'technique[12],becauseof:seq?b=?vb=seq(lys!?++ys)b=seq(rep?)b:ThestatementsofTheorems2and3arenotaffectedbythepresenceofseq,becausetheirproofsalreadyuserelationsRthatfullltheaboverestrictionsanditisstraightforwardtoadaptthoseproofstousethepartialordervinsteadofidentityasrelationalinterpretationforbasetypesandforA.Thus,oursituationismuchbetterthanthatofshortcutdeforesta-tion,whichinthepresenceofunrestricteduseofseqcantransformterminatingprogramsintonon-terminatingones,forexample:foldr?[](buildseq)=foldr?[](seq(:)[])=foldr?[][]=[];butafterapplicationoflaw(17)tofoldr?[](buildseq):seq?[]=?:Thisawofshortcutdeforestationmayberemediedusingmorere-strictedqualiedtypesforbuildorseq,similarlytothestrategyofLaunchbury&Paterson[16]foravoidingstrictnesssideconditionsonfreetheorems.Thisapproachmightalsobeapplicableinourset-tingtocontroltheuseofseqandthuspreservelaws(18)and(21)insteadof(22)and(23).Whilerightinspirit,thediscussioninthisappendixdoesnotcoverallthesubtletiesthatthepresenceofseqentailsforproofsbasedonfreetheorems.Foracorrecttreatment,consultthefollowingpapers:P.JohannandJ.Voigtl¨ander.Freetheoremsinthepresenceofseq.InPrinciplesofProgram-mingLanguages,Venice,Italy,Proceedings,volume39(1)ofSIGPLANNotices,pages99–110.ACMPress,2004.P.JohannandJ.Voigtl¨anderTheimpactofseqonfreetheorems-basedprogramtransforma-tions.FundamentaInformaticae,69:63–102,2006.