Generic Haskell practice and theory

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

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

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

资源描述

GenericHaskell:practiceandtheoryRalfHinze1andJohanJeuring2,31Institutf¨urInformatikIII,Universit¨atBonnR¨omerstraße164,53117Bonn,Germanyralf@informatik.uni-bonn.de~ralf/2InstituteofInformationandComputingSciences,UtrechtUniversityP.O.Box80.089,3508TBUtrecht,TheNetherlandsjohanj@cs.uu.nl~johanj/3OpenUniversity,Heerlen,TheNetherlandsAbstract.GenericHaskellisanextensionofHaskellthatsupportstheconstructionofgenericprograms.TheselecturenotesdescribethebasicconstructsofGenericHaskellandhighlighttheunderlyingtheory.Genericprogrammingaimsatmakingprogrammingmoreeffectivebymak-ingitmoregeneral.Genericprogramsoftenembodynon-traditionalkindsofpolymorphism.GenericHaskellisanextensionofHaskell[38]thatsupportstheconstructionofgenericprograms.GenericHaskelladdstoHaskellthenotionofstructuralpolymorphism,theabilitytodefineafunction(oratype)byinductiononthestructureoftypes.Suchafunctionisgenericinthesensethatitworksnotonlyforaspecifictypebutforawholeclassoftypes.Typicalexamplesincludeequality,parsingandprettyprinting,serialising,ordering,hashing,andsoon.ThelecturenotesonGenericHaskellareorganizedintotwoparts.Thisfirstpartmotivatestheneedforgenericity,describesthebasicconstructsofGenericHaskell,putsGenericHaskellintoperspective,andhighlightstheunderlyingtheory.Thesecondpartentitled“GenericHaskell:applications”delvesdeeperintothelanguagediscussingthreenon-trivialapplicationsofGenericHaskell:genericdictionaries,compressingXMLdocuments,andagenericversionofthezipperdatatype.Thefirstpartisorganizedasfollows.Section1providessomebackgrounddiscussingtypesystemsingeneralandthetypesystemofHaskellinparticular.Furthermore,itmotivatesthebasicconstructsofGenericHaskell.Section2takesacloserlookatgenericdefinitionsandshowshowtodefinesomepopulargenericfunctions.Section3highlightsthetheoryunderlyingGenericHaskellanddiscussesitsimplementation.Section4concludes.1IntroductionThissectionmotivatesandintroducesthebasicconstructsofGenericHaskell.Westartbylookingattypesystems.2R.Hinze,J.JeuringAbasicknowledgeofHaskellisdesirable,asalltheexamplesaregiveneitherinHaskellorinGenericHaskell.1.1TypesystemsSafelanguagesMostprogrammersprobablyagreethatlanguagesafetyisagoodthing.Languagesafetyisquiteacolorfultermmeaningdifferentthingstodifferentpeople.HereareafewdefinitionstakenfromPierce’sexcellenttextbook“TypesandProgrammingLanguages”[40].–Asafelanguageisonethatmakesitimpossibletoshootyourselfinthefootwhileprogramming.–Asafelanguageisonethatprotectsitsownabstractions.–Asafelanguageisonethatthatpreventsuntrappederrorsatruntime.–Asafelanguageiscompletelydefinedbyitsprogrammer’smanual.Thedefinitionsputemphasisondifferentaspectsoflanguagesafety.Quiteclearly,allofthesearedesirablepropertiesofaprogramminglanguage.Now,languagesafetycanbeachievedbystatictypechecking,bydynamictypechecking,or—andthisisthemostcommoncase—byacombinationofstaticanddynamicchecks.ThelanguageHaskellservesasanexampleofthelatterapproach:passinganintegertoalist-processingfunctioniscapturedstaticallyatcompiletimewhiletakingthefirstelementoftheemptylistresultsinarun-timeerror.StaticanddynamictypingItiswidelyacceptedthatstatictypesystemsareindispensableforbuildinglargeandreliablesoftwaresystems.Themostcitedbenefitsofstatictypinginclude:–Programmingerrorsaredetectedatanearlystage.–Typesystemsenforcedisciplinedprogramming.–Typespromoteabstraction(abstractdatatypes,modulesystems).–Typesprovidemachine-checkabledocumentation.However,typesystemsarealwaysconservative:theymustnecessarilyrejectprogramsthatbehavewellatruntime.Inasense,genericprogrammingisaboutextendingtheboundariesofstatictypesystems.Thisisthereasonwhytheselecturenoteshavelittletoofferforaddictsofdynamicallytypedlanguages.Aswewillsee,mostgenericprogramscanbereadilyimplementedinadynamicallycheckedlanguage.(Conceptually,adynamiclanguageoffersoneuniversaldatatype;programmingafunctionthatworksforaclassofdatatypesisconsequentlyanon-issue.)PolymorphictypesystemsPolymorphismcomplementstypesecuritybyflex-ibility.PolymorphictypesystemsliketheHindley-Milnersystem[33]allowtheGenericHaskell:practiceandtheory3definitionoffunctionsthatbehaveuniformlyoveralltypes.Astandardexampleisthelengthfunctionthatcomputesthelengthofalist.dataLista=Nil|Consa(Lista)length::∀a.Lista→IntlengthNil=0length(Consaas)=1+lengthasThefirstlinedeclaresthelistdatatype,whichisparametricinthetypeoflistelements.Thefunctionlengthhappenstobeinsensitivetotheelementtype.Thisissignalledbytheuniversalquantifierinlength’stypesignature(read:Lista→Intisavalidtypeoflengthforalltypesa).ThoughthisisnotHaskell98syntax,wewillwritepolymorphictypesalwaysusingexplicitqualifiers.Mostreadersprobablyknowtheuniversalquantifierfrompredicatelogic.Indeed,thereisaclosecorrespondencebetweenpolymorphictypesystemsandsystemsofhigher-orderlogic,see[47].Inlightofthiscorrespondencewenotethatthequantifierinlength’stypesignatureissecond-orderasitrangesoversets(ifwenaivelyequatetypeswithsets).However,evenpolymorphictypesystemsaresometimeslessflexiblethanonewouldwish.Forinstance,itisnotpossi

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

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

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

×
保存成功