/
2M.P.JonesItishardforHaskellresearcherstoexplorenewtypesystemextensio 2M.P.JonesItishardforHaskellresearcherstoexplorenewtypesystemextensio

2M.P.JonesItishardforHaskellresearcherstoexplorenewtypesystemextensio - PDF document

kittie-lecroy
kittie-lecroy . @kittie-lecroy
Follow
370 views
Uploaded On 2015-11-22

2M.P.JonesItishardforHaskellresearcherstoexplorenewtypesystemextensio - PPT Presentation

4MPJones DescriptionSymbolType kindkKindtypeconstructortcTycontypevariablevTyvar xedfgenericgtypetTypeclasscClassinstanceitInstpredicatepqPreddeferredd ID: 201339

4M.P.Jones DescriptionSymbolType kindk;:::Kindtypeconstructortc;:::Tycontypevariablev;:::Tyvar{` xed'f;:::{`generic'g;:::typet;:::Typeclassc;:::Classinstanceit;:::Instpredicatep;q;:::Pred{`deferred'd;

Share:

Link:

Embed:

Download Presentation from below link

Download Pdf The PPT/PDF document "2M.P.JonesItishardforHaskellresearchers..." 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

2M.P.JonesItishardforHaskellresearcherstoexplorenewtypesystemextensions,oreventostudyusabilityissuesthatarisewiththepresenttypesystemsuchasthesearchforbettertypeerrordiagnostics.Workintheseareasrequiresaclearunderstandingofthetypesystemand,ideally,aplatformonwhichtobuildandexperimentwithprototypeimplementations.TheexistingHaskellimplementationsarenotsuitableforthis(andwerenotintendedtobe):thenutsandboltsofatypesystemareeasilyobscuredbytheuseofcleverdatastructuresandoptimizations,orbytheneedtointegratesmoothlywithotherpartsofanimplementation.ThispaperpresentsaformaldescriptionoftheHaskelltypesystemusingtheno-tationofHaskellitselfasaspeci cationlanguage.Indeed,thesourcecodeforthispaperisitselfanexecutableHaskellprogramthatispassedthroughacustompre-processorandthenthroughLATEXtoobtainthetypesetversion.ThetypecheckerisavailableinsourceformontheInternetathttp://www.cse.ogi.edu/~mpj/thih/.WehopethatthiswillserveasaresourcefortheHaskellcommunity,andthatitwillbeasigni cantstepinaddressingtheproblemsdescribedpreviously.OneaudiencewhoseneedsmaynotbeparticularlywellmetbythispaperareresearchersinprogramminglanguagetypesystemswhodonothaveexperienceofHaskell.(Ofcourse,weencourageanyoneinthatpositiontolearnmoreaboutHaskell!)Indeed,wedonotfollowthetraditionalrouteinsuchsettingswherethetypesystemmight rstbepresentedinitspurestform,andthenrelatedtoamoreconcretetypeinferencealgorithmbysoundnessandcompletenesstheorems.Here,wedealonlywithtypeinference.Itdoesnotevenmakesensetoaskifouralgorithmcomputes`principal'types:suchaquestionrequiresacomparisonbetweentwodi erentpresentationsofatypesystem,andweonlyhaveone.Nevertheless,webelievethatourspeci cationcouldberecastinastandard,type-theoreticmannerandusedtodevelopapresentationofHaskelltypinginamoretraditionalstyle.ThecodepresentedherecanbeexecutedwithanyHaskellsystem,butourpri-marygoalshavebeenclarityandsimplicity,andtheresultingcodeisnotintendedtobeanecientimplementationoftypeinference.Indeed,insomeplaces,ourchoiceofrepresentationmayleadtosigni cantoverheadsandduplicatedcomputa-tion.Itwouldbeinterestingtotrytoderiveamoreecient,butprovablycorrectimplementationfromthespeci cationgivenhere.Wehavenotattemptedtodothisbecauseweexpectthatitwouldobscurethekeyideasthatwewanttoemphasize.Itthereforeremainsasatopicforfuturework,andasatesttoassesstheapplicabilityofprogramtransformationandsynthesistomodestlysizedHaskellprograms.AnothergoalofthispaperistogiveascompleteadescriptionoftheHaskelltypesystemaspossible,whilealsoaimingforconciseness.Forthistobepossible,wehaveassumedthatcertaintransformationsandcheckswillhavebeenmadepriortotypechecking,andhencethatwecanworkwithamuchsimplerabstractsyntaxthanthefullsource-levelsyntaxofHaskellwouldsuggest.Asweargueinformallyatvariouspointsinthepaper,wedonotbelievethattherewouldbeanysigni cantdicultyinextendingoursystemtodealwiththemissingconstructs.Allofthefundamentalcomponents,includingthethorniestaspectsofHaskelltyping, 4M.P.Jones DescriptionSymbolType kindk;:::Kindtypeconstructortc;:::Tycontypevariablev;:::Tyvar{` xed'f;:::{`generic'g;:::typet;:::Typeclassc;:::Classinstanceit;:::Instpredicatep;q;:::Pred{`deferred'd;:::{`retained'r;:::quali edtypeqt;:::QualTypeclassenvironmentce;:::ClassEnvschemesc;:::Schemesubstitutions;:::Substuni eru;:::Substassumptiona;:::Assump identi eri;:::Idliterall;:::Literalpatternpat;:::Patexpressione;f;:::Expralternativealt;:::Altbindinggroupbg;:::BindGroup Fig.1.NotationalConventionspresentationassimpleaspossible,someaspectsoftheproblemsthatwearetryingtoaddresshaveinherentcomplexityortechnicaldepththatcannotbeside-stepped.Inshort,thispaperwillprobablynotbeusefulasatutorialintroductiontoHindley-Milnerstyletypeinference!2PreliminariesForsimplicity,wepresentthecodeforourtypecheckerasasingleHaskellmodule.Theprogramusesonlyahandfulofstandardpreludefunctions,likemap,concat,all,any,mapM,etc.,andafewoperationsfromtheListandMonadlibraries:moduleTypingHaskellInHaskellwhereimportList(nub;(nn);intersect;union;partition)importMonad(msum)Forthemostpart,ourchoiceofvariablenamesfollowsthenotationalconventionssetoutinFigure1.Atrailingsonavariablenameusuallyindicatesalist.Numericsucesorprimesareusedasfurtherdecorationwherenecessary.Forexample,weusekork0forakind,andksorks0foralistofkinds.Thetypesandtermsappearing 6M.P.JonesdataTycon=TyconIdKindderivingEqThisde nitionalsoincludestypesoftheformTGenn,whichrepresent`generic'orquanti edtypevariables.TheonlyplacewhereTGenvaluesareusedisintherepresentationoftypeschemes,whichwillbedescribedinSection8.Thefollowingexamplesshowhowstandardprimitivedatatypesarerepresentedastypeconstants:tUnit=TCon(Tycon\()"Star)tChar=TCon(Tycon\Char"Star)tInt=TCon(Tycon\Int"Star)tInteger=TCon(Tycon\Integer"Star)tFloat=TCon(Tycon\Float"Star)tDouble=TCon(Tycon\Double"Star)tList=TCon(Tycon\[]"(KfunStarStar))tArrow=TCon(Tycon\�(-)"(KfunStar(KfunStarStar)))tTuple2=TCon(Tycon\(,)"(KfunStar(KfunStarStar)))AfullHaskellcompilerorinterpretermightstoreadditionalinformationwitheachtypeconstant|suchasthethelistofconstructorfunctionsforanalgebraicdatatype|butsuchdetailsarenotneededduringtypechecking.MorecomplextypesarebuiltupfromconstantsandvariablesusingtheTApconstructor.Forexample,therepresentationforthetypeInt![a]isasfollows:TAp(TAptArrowtInt)(TAptList(TVar(Tyvar\a"Star)))Wedonotprovidearepresentationfortypesynonyms,assuminginsteadthattheyhavebeenfullyexpandedbeforetypechecking.Forexample,theStringtype|asynonymfor[Char]|isrepresentedas:tString::TypetString=listtCharItisalwayspossibleforanimplementationtoexpandsynonymsinthiswaybecauseHaskellpreventstheuseofasynonymwithoutitsfullcomplementofarguments.Moreover,theprocessisguaranteedtoterminatebecauserecursivesynonymde -nitionsareprohibited.Inpractice,however,implementationsarelikelytoexpandsynonymsmorelazily:insomecases,typeerrordiagnosticsmaybeeasiertoun-derstandiftheydisplaysynonymsratherthanexpansions.Weendthissectionwiththede nitionofafewhelperfunctions.The rstthreeprovidesimplewaystoconstructfunction,list,andpairtypes,respectively:in xr4`fn`fn::Type!Type!Typea`fn`b=TAp(TAptArrowa)blist::Type!Typelistt=TAptListt 8M.P.Jonescomponents|inanaturalway.Thissuggeststhatweoverloadtheoperationtoapplyasubstitutionsothatitcanworkondi erenttypesofobject:classTypestwhereapply::Subst!t!ttv::t![Tyvar]Ineachcase,thepurposeofapplyingasubstitutionisthesame:Toreplaceeveryoccurrenceofatypevariableinthedomainofthesubstitutionwiththecorre-spondingtype.Wealsoincludeafunctiontvthatreturnsthesetoftypevariables(i.e.,Tyvars)appearinginitsargument,listedinorderof rstoccurrence(fromlefttoright),withnoduplicates.Thede nitionsoftheseoperationsforTypeareasfollows:instanceTypesTypewhereapplys(TVaru)=caselookupusofJustt!tNothing!TVaruapplys(TAplr)=TAp(applysl)(applysr)applyst=ttv(TVaru)=[u]tv(TAplr)=tvl`union`tvrtvt=[]Itisstraightforward(anduseful!)toextendtheseoperationstoworkonlists:instanceTypesa)Types[a]whereapplys=map(applys)tv=nub:concat:maptvTheapplyfunctioncanbeusedtobuildmorecomplexsubstitutions.Forexample,compositionofsubstitutions,satisfyingapply(s1@@s2)=applys1:applys2,canbede nedusing:in xr4@@(@@)::Subst!Subst!Substs1@@s2=[(u;applys1t)j(u;t) s2]++s1Wecanalsoforma`parallel'compositions1++s2oftwosubstitutionss1ands2,buttheresultisleft-biasedbecausebindingsins1takeprecedenceoveranybindingsforthesamevariablesins2.Foramoresymmetricversionofthisoperation,weuseamergefunction,whichchecksthatthetwosubstitutionsagreeateveryvariableinthedomainofbothandhenceguaranteesthatapply(s1++s2)=apply(s2++s1).Clearly,thisisapartialfunction,whichwere ectbyarrangingformergetoreturnitsresultinamonad,usingthestandardfailfunctiontoprovideastringdiagnostic 10M.P.JonesThevarBindfunctionisusedforthespecialcaseofunifyingavariableuwithatypet.At rstglance,onemightthinkthatwecouldjustusethesubstitution(u7!t)forthis.Inpractice,however,testsarerequiredtoensurethatthisisvalid,includingan`occurscheck'(u`elem`tvt)andatesttoensurethatthesubstitutioniskind-preserving:varBindutjt==TVaru=returnnullSubstju`elem`tvt=fail\occurscheckfails"jkindu==kindt=fail\kindsdonotmatch"jotherwise=return(u7!t)Inthefollowingsections,wewillalsomakeuseofanoperationcalledmatchingthatiscloselyrelatedtouni cation.Giventwotypest1andt2,thegoalofmatchingisto ndasubstitutionssuchthatapplyst1=t2.Becausethesubstitutionisappliedonlytoonetype,thisoperationisoftendescribedasone-waymatching.Thecalculationofmatchingsubstitutionsisimplementedbyafunction:match::Monadm)Type!Type!mSubstMatchingfollowsthesamepatternasuni cation,exceptthatitusesmergeratherthan@@tocombinesubstitutions,anditdoesnotallowbindingofvariablesint2:match(TAplr)(TApl0r0)=dosl matchll0sr matchrr0mergeslsrmatch(TVaru)tjkindu==kindt=return(u7!t)match(TContc1)(TContc2)jtc1==tc2=returnnullSubstmatcht1t2=fail\typesdonotmatch"7TypeClasses,PredicatesandQuali edTypesOneofthemostunusualfeaturesoftheHaskelltypesystem,atleastincomparisontothoseofotherpolymorphicallytypedlanguageslikeML,isthesupportthatitprovidesfortypeclasses.DescribedbyWadlerandBlott(1989)asageneralmech-anismthatsubsumesseveralad-hocformsofoverloading,typeclasseshavefoundmanyuses(and,sometimes,abuses!)inthetenyearssincetheywereintroduced.Asigni cantportionofthecodepresentedinthispaper,particularlyinthissection,isneededtodescribethehandlingoftypeclassesinHaskell.(Ofcourse,typeclassesarenottheonlysourceofcomplexity.Thetreatmentofmixedimplicitandexplicittyping,mutuallyrecursivebindings,andpatternmatching|whichareoftenelidedinmoretheoreticalpresentations|arealsosigni cantcontributors,asistheextralevelofdetailandprecisionthatisneededinexecutablecode.)7.1BasicDe nitionsAHaskelltypeclasscanbethoughtofasasetoftypes(ofsomeparticularkind),eachofwhichsupportsacertaincollectionofmemberfunctionsthatarespeci ed 12M.P.Jonessuperclass,andanothercontaininganentryforeachinstancedeclaration:typeClass=([Id];[Inst])typeInst=QualPredForexample,asimpli edversionofthestandardHaskellclassOrdmightbede-scribedbythefollowingvalueoftypeClass:([\Eq"];[[]:)IsIn\Ord"tUnit;[]:)IsIn\Ord"tChar;[]:)IsIn\Ord"tInt;[IsIn\Ord"(TVar(Tyvar\a"Star));IsIn\Ord"(TVar(Tyvar\b"Star))]:)IsIn\Ord"(pair(TVar(Tyvar\a"Star))(TVar(Tyvar\b"Star)))])ThisstructurecapturesthefactthatEqisasuperclassofOrd(theonlyoneinfact),andlistsfourinstancedeclarationsfortheunit,character,integer,andpairtypes(ifaandbareinOrd,then(a;b)isalsoinOrd).Ofcourse,thisisonlyafractionofthelistofOrdinstancesthatarede nedinthefullHaskellprelude.Onlythedetailsthatareneededfortypeinferenceareincludedintheserepresentations.AfullHaskellimplementationwouldneedtostoreadditionalinformationforeachdeclaration,suchasthelistofmemberfunctionsforeachclassanddetailsoftheirimplementationsineachparticularinstance.7.2ClassEnvironmentsTheinformationprovidedbytheclassandinstancedeclarationsinagivenprogramcanbecapturedbyaclassenvironmentoftype:dataClassEnv=ClassEnvfclasses::Id!MaybeClass;defaults::[Type]gTheclassescomponentinaClassEnvvalueisapartialfunctionthatmapsidenti- erstoClassvalues(ortoNothingifthereisnoclasscorrespondingtothespeci edidenti er).Wede nehelperfunctionssuperandinststoextractthelistofsuper-classidenti ers,andthelistofinstances,respectively,foraclassnameiinaclassenvironmentce:super::ClassEnv!Id![Id]supercei=caseclassesceiofJust(is;its)!isinsts::ClassEnv!Id![Inst]instscei=caseclassesceiofJust(is;its)!itsThesefunctionsareintendedtobeusedonlyincaseswhereitisknownthattheclassiisde nedintheenvironmentce.Insomecases,thisconditionmightbeguaranteedbystaticanalysispriortotypechecking.Alternatively,wecanresorttoadynamiccheckbytestingde ned(classescei)beforeapplyingeitherfunction. 14M.P.JonesSomereaderswillrecognizethisasaspecialcaseofthemoregeneralKleislicompo-sitionoperator;withoutthetypedeclaration,thede nitiongivenherewouldworkforanymonadandforanyelementtypes,notjustforMaybeandClassEnv.Toaddanewclasstoanenvironment,wemustcheckthatthereisnotalreadyaclasswiththesamename,andthatallofthenamedsuperclassesarealreadyde ned.ThisisasimplewayofenforcingHaskell'srestrictionthatthesuperclasshierarchybeacyclic.Ofcourse,inpractice,itwillbenecessarytotopologicallysortthesetofclassdeclarationsinaprogramtodetermineasuitableordering;anycyclesinthehierarchywilltypicallybedetectedatthisstage.addClass::Id![Id]!EnvTransformeraddClassiiscejde ned(classescei)=fail\classalreadydefined"jany(not:de ned:classesce)is=fail\superclassnotdefined"jotherwise=return(modifycei(is;[]))Forexample,wecandescribethee ectoftheclassdeclarationsintheHaskellpreludeusingthefollowingtransformer:addPreludeClasses::EnvTransformeraddPreludeClasses=addCoreClasses:&#x]TJ/;ø 9;&#x.963;&#x Tf ;.74; 0 ;&#xTd[0;addNumClassesThisde nitionbreaksdownthesetofstandardHaskellclassesintotwoseparatepieces.Thecoreclassesaredescribedasfollows:addCoreClasses::EnvTransformeraddCoreClasses=addClass\Eq"[]:&#x]TJ/;ø 9;&#x.963;&#x Tf ;.74; 0 ;&#xTd[0;addClass\Ord"[\Eq"]:&#x]TJ/;ø 9;&#x.963;&#x Tf ;.74; 0 ;&#xTd[0;addClass\Show"[]:&#x]TJ/;ø 9;&#x.963;&#x Tf ;.74; 0 ;&#xTd[0;addClass\Read"[]:&#x]TJ/;ø 9;&#x.963;&#x Tf ;.74; 0 ;&#xTd[0;addClass\Bounded"[]:&#x]TJ/;ø 9;&#x.963;&#x Tf ;.74; 0 ;&#xTd[0;addClass\Enum"[]:&#x]TJ/;ø 9;&#x.963;&#x Tf ;.74; 0 ;&#xTd[0;addClass\Functor"[]:&#x]TJ/;ø 9;&#x.963;&#x Tf ;.74; 0 ;&#xTd[0;addClass\Monad"[]Thehierarchyofnumericclassesiscapturedseparatelyinthefollowingde nition:addNumClasses::EnvTransformeraddNumClasses=addClass\Num"[\Eq";\Show"]:&#x]TJ/;ø 9;&#x.963;&#x Tf ;.74; 0 ;&#xTd[0;addClass\Real"[\Num";\Ord"]:&#x]TJ/;ø 9;&#x.963;&#x Tf ;.74; 0 ;&#xTd[0;addClass\Fractional"[\Num"]:&#x]TJ/;ø 9;&#x.963;&#x Tf ;.74; 0 ;&#xTd[0;addClass\Integral"[\Real";\Enum"]:&#x]TJ/;ø 9;&#x.963;&#x Tf ;.74; 0 ;&#xTd[0;addClass\RealFrac"[\Real";\Fractional"]:&#x]TJ/;ø 9;&#x.963;&#x Tf ;.74; 0 ;&#xTd[0;addClass\Floating"[\Fractional"]:&#x]TJ/;ø 9;&#x.963;&#x Tf ;.74; 0 ;&#xTd[0;addClass\RealFloat"[\RealFrac";\Floating"]Toaddanewinstancetoaclass,wemustcheckthattheclasstowhichtheinstanceappliesisde ned,andthatthenewinstancedoesnotoverlapwithanypreviously 16M.P.Jonestheheadofaninstanceshouldconsistofatypeconstructorappliedtoasequenceofdistincttypevariablearguments.Becausetheseconditionshavenodirectimpactontypechecking,andbecausetheyarestraightforwardbuttedioustoverify,wehavechosennottoincludetestsforthemhere,andinsteadassumethattheyhavebeencheckedduringstaticanalysispriortotypechecking.7.3EntailmentInthissection,wedescribehowclassenvironmentscanbeusedtoanswerquestionsaboutwhichtypesareinstancesofparticularclasses.Moregenerally,weconsiderthetreatmentofentailment:givenapredicatepandalistofpredicatesps,ourgoalistodeterminewhetherpwillholdwheneverallofthepredicatesinpsaresatis ed.Inthespecialcasewherep=IsInitandps=[],thisamountstodeterminingwhethertisaninstanceoftheclassi.Inthetheoryofquali edtypes(Jones,1992),assertionslikethisarecapturedusingjudgementsoftheformps``p;weuseadi erentnotationhere|theentailfunctionthatisde nedattheendofthissection|tomakethedependenceonaclassenvironmentexplicit.Asa rststep,wecanaskhowinformationaboutsuperclassesandinstancescanbeusedindependentlytohelpreasonaboutentailments.Forexample,ifatypeisaninstanceofaclassi,thenitmustalsobeaninstanceofanysuperclassesofi.Hence,usingonlysuperclassinformation,wecanbesurethat,ifagivenpredicatepholds,thensotoomustallofthepredicatesinthelistbySuperp:bySuper::ClassEnv!Pred![Pred]bySupercep@(IsInit)=p:concat[bySuperce(IsIni0t)ji0 supercei]ThelistbySupercepmaycontainduplicates,butitwillalwaysbe nitebecauseoftherestrictionthatthesuperclasshierarchyisacyclic.Nextweconsiderhowinformationaboutinstancescanbeused.Ofcourse,foragivenpredicatep=IsInit,wecan ndallthedirectlyrelevantinstancesinaclassenvironmentcebylookingininstscei.Aswehaveseen,individualinstancedeclarationsaremappedintoclausesoftheformps:)h.Theheadpredicatehdescribesthegeneralformofinstancesthatcanbeconstructedfromthisdeclara-tion,andwecanusematchPredtodeterminewhetherthisinstanceisapplicabletothegivenpredicatep.Ifitisapplicable,thenmatchingwillreturnasubstitutionu,andtheremainingsubgoalsaretheelementsofmap(applyu)ps.Thefollowingfunctionusestheseideastodeterminethelistofsubgoalsforagivenpredicate:byInst::ClassEnv!Pred!Maybe[Pred]byInstcep@(IsInit)=msum[tryInstitjit instscei]wheretryInst(ps:)h)=dou matchPredhpJust(map(applyu)ps)ThemsumfunctionusedherecomesfromthestandardMonadlibrary,andreturnsthe rstde nedelementinalistofMaybevalues;iftherearenode nedelementsinthelist,thenitreturnsNothing.BecauseHaskellpreventsoverlappinginstances, 18M.P.Jonesofa,eachoneofthesepredicatesholdsif,andonlyif,Eqaholds.Noticethat,insomecases,anattempttosimplifytypecomponents|forexample,byreplacingEq(a,b)with(Eqa,Eqb)|mayincreasethenumberofpredicatesinthelist.Theextenttowhichsimpli cationslikethisareusedinasystemofquali edtypeshasanimpactontheimplementationandperformanceofoverloadinginpracticalsystems(Jones,1992;Chapter7).InHaskell,however,thedecisionsaremadeforusbyasyntacticrestrictionthatforcesustosimplifypredicatesuntilweobtaintypesinakindof`head-normalform'.Thisterminologyismotivatedbysimilaritieswiththeconceptofhead-normalformsin-calculus.Moreprecisely,thesyntaxofHaskellrequiresclassargumentstobeoftheformvt1:::tn,wherevisatypevariable,andt1,...,tnaretypes(andn0).Thefollowingfunctionallowsustodeterminewhetheragivenpredicatemeetstheserestrictions:inHnf::Pred!BoolinHnf(IsInct)=hnftwherehnf(TVarv)=Truehnf(TContc)=Falsehnf(TApt )=hnftPredicatesthatdonot tthispatternmustbebrokendownusingbyInst.Insomecases,thiswillresultinpredicatesbeingeliminatedaltogether.Inothers,wherebyInstfails,itwillindicatethatapredicateisunsatis able,andwilltriggeranerrordiagnostic.Thisprocessiscapturedinthefollowingde nition:toHnfs::Monadm)ClassEnv![Pred]!m[Pred]toHnfsceps=dopss mapM(toHnfce)psreturn(concatpss)toHnf::Monadm)ClassEnv!Pred!m[Pred]toHnfcepjinHnfp=return[p]jotherwise=casebyInstcepofNothing!fail\contextreduction"Justps!toHnfscepsAnotherwaytosimplifyalistofpredicatesistoreducethenumberofelementsthatitcontains.Thereareseveralwaysthatthismightbeachieved:byremovingduplicates(e.g.,reducing(Eqa,Eqa)toEqa);byeliminatingpredicatesthatarealreadyknowntohold(e.g.,removinganyoccurrencesofNumInt);orbyusingsuperclassinformation(e.g.,reducing(Eqa,Orda)toOrda).Ineachcase,thereducedlistofpredicates,isequivalenttotheinitiallist,meaningthatallthepredicatesinthe rstwillbesatis edif,andonlyif,allofthepredicatesinthesecondaresatis ed.Thesimpli cationalgorithmthatwewillusehereisbasedontheobservationthatapredicatepinalistofpredicates(p:ps)canbeeliminatedifpisentailedbyps.Asaspecialcase,thiswilleliminateduplicatedpredicates:ifpisrepeatedinps,thenitwillalsobeentailedbyps.Theseideasareusedinthefollowingde nitionofthesimplifyfunction,whichloopsthrougheachpredicateinthelistandusesanaccumulatingparametertobuildupthe nalresult.Eachtime 20M.P.Jonesbesurethattherewillbenovariablecaptureproblemsinthefollowingde nition:instanceTypesSchemewhereapplys(Forallksqt)=Forallks(applysqt)tv(Forallksqt)=tvqtTypeschemesareconstructedbyquantifyingaquali edtypeqtwithrespecttoalistoftypevariablesvs:quantify::[Tyvar]!QualType!Schemequantifyvsqt=Forallks(applysqt)wherevs0=[vjv tvqt;v`elem`vs]ks=mapkindvs0s=zipvs0(mapTGen[0::])Notethattheorderofthekindsinksisdeterminedbytheorderinwhichthevariablesvappearintvqt,andnotbytheorderinwhichtheyappearinvs.So,forexample,theleftmostquanti edvariableinatypeschemewillalwaysberepresentedbyTGen0.Byinsistingthattypeschemesareconstructedinthisway,weobtainauniquecanonicalformforSchemevalues.Thisisimportantbecauseitmeansthatwecantestwhethertwotypeschemesarethesame|forexample,todeter-minewhetheraninferredtypeagreeswithadeclaredtype|usingHaskell'sderivedequality,andwithouthavingtoimplementmorecomplextestsfor -equivalence.Inpractice,wesometimesneedtoconvertaTypeintoaSchemewithoutaddinganyqualifyingpredicatesorquanti edvariables.Forthisspecialcase,wecanusethefollowingfunctioninsteadofquantify:toScheme::Type!SchemetoSchemet=Forall[]([]:)t)Tocompleteourdescriptionoftypeschemes,weneedtobeabletoinstantiatethequanti edvariablesinSchemevalues.Infact,forthepurposesoftypeinference,weonlyneedthespecialcasethatinstantiatesatypeschemewithfreshtypevariables.WethereforedeferfurtherdescriptionofinstantiationtoSection10wherethemechanismsforgeneratingfreshtypevariablesareintroduced.9AssumptionsAssumptionsaboutthetypeofavariablearerepresentedbyvaluesoftheAssumpdatatype,eachofwhichpairsavariablenamewithatypescheme:dataAssump=Id:�:SchemeOnceagain,wecanextendtheTypesclasstoallowtheapplicationofasubsti-tutiontoanassumption:instanceTypesAssumpwhereapplys(i:�:sc)=i:�:(applyssc)tv(i:�:sc)=tvsc 22M.P.JonesForclarity,wede netheoperationthatextendsthesubstitutionasaseparatefunction,eventhoughitisusedonlyhereinthede nitionofunify:extSubst::Subst!TI()extSubsts0=TI(nsn!(s0@@s;n;()))Overall,thedecisiontohidethecurrentsubstitutionintheTImonadmakesthepresentationoftypeinferencemuchclearer.Inparticular,itavoidsheavyuseofapplyeverytimeanextensionis(ormighthavebeen)computed.Thereisonlyoneprimitivethatdealswiththeintegerportionofthestate,usingitincombinationwithenumIdtogenerateanewtypevariableofaspeci edkind:newTVar::Kind!TITypenewTVark=TI(nsn!letv=Tyvar(enumIdn)kin(s;n+1;TVarv))OneplacewherenewTVarisusefulisininstantiatingatypeschemewithnewtypevariablesofappropriatekinds:freshInst::Scheme!TI(QualType)freshInst(Forallksqt)=dots mapMnewTVarksreturn(insttsqt)Thestructureofthisde nitionguaranteesthattshasexactlytherightnumberoftypevariables,andeachwiththerightkind,tomatchks.Hence,ifthetypeschemeiswell-formed,thenthequali edtypereturnedbyfreshInstwillnotcontainanyunboundgenericsoftheformTGenn.Thede nitionreliesonanauxiliaryfunctioninst,whichisavariationofapplythatworksongenericvariables.Inotherwords,insttstreplaceseachoccurrenceofagenericvariableTGennintwithts!!n.Itisconvenienttobuildupthede nitionofinstusingoverloading:classInstantiatetwhereinst::[Type]!t!tinstanceInstantiateTypewhereinstts(TAplr)=TAp(insttsl)(insttsr)instts(TGenn)=ts!!ninsttst=tinstanceInstantiatea)Instantiate[a]whereinstts=map(instts)instanceInstantiatet)Instantiate(Qualt)whereinstts(ps:)t)=insttsps:)insttstinstanceInstantiatePredwhereinstts(IsInct)=IsInc(insttst)11TypeInferenceWiththissectionwehavereachedtheheartofthepaper,detailingouralgorithmfortypeinference.Itisherethatwe nallyseehowthemachinerythathasbeenbuiltupinearliersectionsisactuallyputtouse.Wedevelopthecompletealgorithmin 24M.P.Jonessions.WewillrepresentpatternsusingvaluesofthePatdatatype:dataPat=PVarIdjPWildcardjPAsIdPatjPLitLiteraljPNpkIdIntegerjPConAssump[Pat]APVaripatternmatchesanyvalueandbindstheresulttothevariablei.APWildcardpattern,correspondingtoanunderscore inHaskellsyntax,matchesanyvalue,butdoesnotbindanyvariables.Apatternoftheform(PAsipat),knownasan\as-pattern"andwrittenusingthesyntaxi@patinHaskell,bindsthevariableitoanyvaluethatmatchesthepatternpat,whilealsobindinganyvariablesthatappearinpat.APLitlpatternmatchesonlytheparticularvaluedenotedbytheliterall.Apattern(PNpkik)isan(n+k)pattern,whichmatchesanypositiveintegralvaluemthatisgreaterthanorequaltok,andbindsthevariableitothedi erence(m�k).Finally,apatternoftheformPConapatsmatchesonlyvaluesthatwerebuiltusingtheconstructorfunctionawithasequenceofargumentsthatmatchespats.WeusevaluesaoftypeAssumptorepresentconstructorfunctions;allthatwereallyneedfortypecheckingisthetype,althoughthenameisusefulfordebugging.Afullimplementationwouldstoreadditionaldetails,suchasarity,andusethistocheckthatconstructorfunctionsinpatternsarealwaysfullyapplied.MostHaskellpatternshaveadirectrepresentationinPat,butextensionswouldbeneededtoaccountforpatternsusinglabeled elds.Thisisnotdicult,butaddssomecomplexity,whichweprefertoavoidinthispresentation.Typeinferenceforpatternshastwogoals:Tocalculateatypeforeachboundvariable,andtodeterminewhattypeofvaluesthewholepatternmightmatch.Thisleadsustolookforafunction:tiPat::Pat!TI([Pred];[Assump];Type)Notethatwedonotneedtopassinalistofassumptionshere;byde nition,anyoccurrenceofavariableinapatternwouldhideratherthanrefertoavariableofthesamenameinanenclosingscope.Foravariablepattern,PVari,wejustreturnanewassumption,bindingitoafreshtypevariable.tiPat(PVari)=dov newTVarStarreturn([];[i:�:toSchemev];v)Haskelldoesnotallowmultipleuseofanyvariableinapattern,sowecanbesurethatthisisthe rstandonlyoccurrenceofithatwewillencounterinthepattern.Wildcardsaretypedinthesamewayexceptthatwedonotneedtocreateanewassumption:tiPatPWildcard=dov newTVarStarreturn([];[];v)Totypeanas-patternPAsipat,wecalculateasetofassumptionsandatypefor 26M.P.Jones11.3ExpressionsNextwedescribetypeinferenceforexpressions,representedbytheExprdatatype:dataExpr=VarIdjLitLiteraljConstAssumpjApExprExprjLetBindGroupExprTheVarandLitconstructorsareusedtorepresentvariablesandliterals,respec-tively.TheConstconstructorisusedtodealwithnamedconstants,suchastheconstructororselectorfunctionsassociatedwithaparticulardatatypeorthemem-berfunctionsthatareassociatedwithaparticularclass.WeusevaluesoftypeAssumptosupplyanameandtypescheme,whichisalltheinformationthatweneedforthepurposesoftypeinference.FunctionapplicationisrepresentedusingtheApconstructor,whileLetisusedtorepresentletexpressions.(Notethatthede nitionoftheBindGrouptype,usedheretorepresentbindinggroups,willbedelayedtoSection11.6.3.)Ofcourse,Haskellhasamuchrichersyntaxofexpressions|whichincludes-abstractions,caseexpressions,conditionals,listcomprehensions,anddo-notation|buttheyallhavesimpletranslationsintoExprvalues.Forexample,a-expressionlike�\x-ecanberewrittenusingalocalde -nitionasletfx=einf,wherefisanewvariable.Typeinferenceforexpressionsisquitestraightforward:tiExpr::InferExprTypetiExprceas(Vari)=dosc ndias(ps:)t) freshInstscreturn(ps;t)tiExprceas(Const(i:�:sc))=do(ps:)t) freshInstscreturn(ps;t)tiExprceas(Litl)=do(ps;t) tiLitlreturn(ps;t)tiExprceas(Apef)=do(ps;te) tiExprcease(qs;tf) tiExprceasft newTVarStarunify(tf`fn`t)tereturn(ps++qs;t)tiExprceas(Letbge)=do(ps;as0) tiBindGroupceasbg(qs;t) tiExprce(as0++as)ereturn(ps++qs;t)The nalcasehere,forLetexpressions,usesthefunctiontiBindGrouppresentedinSection11.6.3,togeneratealistofassumptionsas0forthevariablesde nedinbg.Allofthesevariablesareinscopewhenwecalculateatypetforthebodye,whichalsoservesasthetypeofthewholeexpression. 28M.P.JonesTounderstandthebasicproblem,supposethatwehaverunthetypecheckeroverthebodyofafunctionhtoobtainalistofpredicatespsandatypet.Atthispoint,toobtainthemostgeneralresult,wecouldinferatypeforhbyformingthequali edtypeqt=(ps:)t)andthenquantifyingoveranyvariablesinqtthatdonotappearintheassumptions.Whilethisispermittedbythetheoryofquali edtypes,itisoftennotthebestthingtodoinpractice.Forexample:Thelistpscanoftenbesimpli edusingthecontextreductionprocessde-scribedinSection7.4.ThiswillalsoensurethatthesyntacticrestrictionsofHaskellaremet,requiringallpredicatestobeinhead-normalform.Someofthepredicatesinpsmaycontainonly` xed'variables(i.e.,variablesappearingintheassumptions),soincludingthoseconstraintsintheinferredtypewillnotbeofanyuse(Jones,1992;Section6.1.4).Thesepredicatesshouldbe`deferred'totheenclosingbindings.Someofthepredicatesinpscouldresultinambiguity,andrequiredefaultingtoavoidatypeerror.ThisaspectofHaskell'stypesystemwillbedescribedshortlyinSection11.5.1.Inthispaperweuseafunctioncalledsplittoaddresstheseissues.Forthesituationdescribedpreviouslywherewehaveinferredatypetandalistofpredicatespsforafunctionh,wecanusesplittorewriteandbreakpsintoapair(ds;rs)ofdeferredpredicatesdsand`retained'predicatesrs.Thepredicatesinrswillbeusedtoformaninferredtype(rs:)t)forh,whilethepredicatesindswillbepassedoutasconstraintstotheenclosingscope.Weusethefollowingde nitionforsplit:split::Monadm)ClassEnv![Tyvar]![Tyvar]![Pred]!m([Pred];[Pred])splitcefsgsps=dops0 reducecepslet(ds;rs)=partition(all(`elem`fs):tv)ps0rs0 defaultedPredsce(fs++gs)rsreturn(ds;rsnnrs0)Inadditiontoalistofpredicatesps,thesplitfunctionisparameterizedbytwolistsoftypevariables.The rst,fs,speci esthesetof` xed'variables,whicharejustthevariablesappearingfreeintheassumptions.Thesecond,gs,speci esthesetofvariablesoverwhichwewouldliketoquantify;fortheexampleabove,itwouldjustbethevariablesin(tvtnnfs).Itispossibleforpstocontainvariablesthatarenotineitherfsorgs(andhencenotintheparameter(fs++gs)thatispassedtodefaultedPreds).InSection11.5.1wewillseethatthisisanindicationofambiguity.Therearethreestagesinthesplitfunction,correspondingdirectlytothethreepointslistedpreviously.The rststageusesreducetoperformcontextreduction.Thesecondstageusesthestandardpreludefunctionpartitiontoidentifythede-ferredpredicates,ds;thesearejustthepredicatesinps0thatcontainonly xedtypevariables.ThethirdstagedetermineswhetheranyofthepredicatesinrsshouldbeeliminatedusingHaskell'sdefaultingmechanism,andproducesalistofallsuchpredicatesinrs0.Hencethe nalsetofretainedpredicatesisproducedbytheexpressionrsnnrs0inthelastlineofthede nition. 30M.P.Jonesdefaultingistobindeachambiguoustypevariablevtoamonotypet.Thetypetmustbechosensothatallofthepredicatesinpsthatinvolvevwillbesatis edoncethasbeensubstitutedforv.Thefollowingfunctioncalculatesthelistofambiguousvariablesandpairseachonewiththelistofpredicatesthatmustbesatis edbyanychoiceofadefault:typeAmbiguity=(Tyvar;[Pred])ambiguities::ClassEnv![Tyvar]![Pred]![Ambiguity]ambiguitiescevsps=[(v; lter(elemv:tv)ps)jv tvpsnnvs]Givenoneofthesepairs(v;qs),andasspeci edbytheHaskellreport(PeytonJones&Hughes,1999;Section4.3.4),defaultingispermittedif,andonlyif,allofthefollowingconditionsaresatis ed:AllofthepredicatesinqsareoftheformIsInc(TVarv)forsomeclassc.Atleastoneoftheclassesinvolvedinqsisastandardnumericclass.Thelistoftheseclassnamesisprovidedbyaconstant:numClasses::[Id]numClasses=[\Num";\Integral";\Floating";\Fractional";\Real";\RealFloat";\RealFrac"]Alloftheclassesinvolvedinqsarestandardclasses,de nedeitherinthestandardpreludeorstandardlibraries.Again,thelistoftheseclassnamesisprovidedbyaconstant:stdClasses::[Id]stdClasses=[\Eq";\Ord";\Show";\Read";\Bounded";\Enum";\Ix";\Functor";\Monad";\MonadPlus"]++numClassesThatthereisatleastonetypeinthelistofdefaulttypesfortheenclosingmodulethatisaninstanceofalloftheclassesmentionedinqs.The rstsuchtypewillbeselectedasthedefault.ThelistofdefaulttypescanbeobtainedfromaclassenvironmentbyusingthedefaultsfunctionthatwasdescribedinSection7.2.Theseconditionsarecapturedrathermoresuccinctlyinthefollowingde nition,whichweusetocalculatethecandidatesforresolvingaparticularambiguity:candidates::ClassEnv!Ambiguity![Type]candidatesce(v;qs)=[t0jletis=[ijIsInit qs]ts=[tjIsInit qs];all((TVarv)==)ts;any(`elem`numClasses)is;all(`elem`stdClasses)is;t0 defaultsce;all(entailce[])[IsInit0ji is]]Ifcandidatesreturnsanemptylistforagivenambiguity,thendefaultingcannotbeappliedtothecorrespondingvariable,andtheambiguitycannotbeavoided.Onthe 32M.P.Jones11.6.1ExplicitlyTypedBindingsThesimplestcaseisforexplicitlytypedbindings,eachofwhichisdescribedbythenameofthefunctionthatisbeingde ned,thedeclaredtypescheme,andthelistofalternativesinitsde nition:typeExpl=(Id;Scheme;[Alt])HaskellrequiresthateachAltinthede nitionofagivenidenti erhasthesamenumberofleft-handsidearguments,butwedonotneedtoenforcethathere.Typeinferenceforanexplicitlytypedbindingisfairlyeasy;weneedonlycheckthatthedeclaredtypeisvalid,anddonotneedtoinferatypefrom rstprinciples.Tosupporttheuseofpolymorphicrecursion(Henglein,1993;Kfouryetal.,1993),wewillassumethatthedeclaredtypingforiisalreadyincludedintheassumptionswhenwecallthefollowingfunction:tiExpl::ClassEnv![Assump]!Expl!TI[Pred]tiExplceas(i;sc;alts)=do(qs:)t) freshInstscps tiAltsceasaltsts getSubstletqs0=applysqst0=applystfs=tv(applysas)gs=tvt0nnfssc0=quantifygs(qs0:)t0)ps0= lter(not:entailceqs0)(applysps)(ds;rs) splitcefsgsps0ifsc==sc0thenfail\signaturetoogeneral"elseifnot(nullrs)thenfail\contexttooweak"elsereturndsThiscodebeginsbyinstantiatingthedeclaredtypeschemescandcheckingeachalternativeagainsttheresultingtypet.Whenallofthealternativeshavebeenprocessed,theinferredtypeforiisqs0:)t0.Ifthetypedeclarationisaccurate,thenthisshouldbethesame,uptorenamingofgenericvariables,astheoriginaltypeqs:)t.Ifthetypesignatureistoogeneral,thenthecalculationofsc0willresultinatypeschemethatismorespeci cthanscandanerrorwillbereported.Inthemeantime,wemustdischargeanypredicatesthatweregeneratedwhilecheckingthelistofalternatives.Predicatesthatareentailedbythecontextqs0canbeeliminatedwithoutfurtherado.Anyremainingpredicatesarecollectedinps0andpassedasargumentstosplitalongwiththeappropriatesetsof xedandgenericvariables.Ifthereareanyretainedpredicatesaftercontextreduction,thenanerrorisreported,indicatingthatthedeclaredcontextistooweak. 34M.P.Jonesusesplittobreaktheinferredpredicatesinps0intoalistofdeferredpredicatesdsandretainedpredicatesrs.Thelistgscollectsallthegenericvariablesthatappearinoneormoreoftheinferredtypests0,butnotinthelistfsof xedvariables.Notethatadi erentlistispassedtosplit,includingonlyvariablesthatappearinalloftheinferredtypes.Thisisimportantbecauseallofthosetypeswilleventuallybequali edbythesamesetofpredicates,andwedonotwantanyoftheresultingtypeschemestobeambiguous.The nalstepbeginswithatesttoseeifthemonomor-phismrestrictionshouldbeapplied,andthencontinuestocalculateanassumptioncontainingtheprincipaltypesforeachofthede nedvalues.Foranunrestrictedbinding,thisissimplyamatterofqualifyingovertheretainedpredicatesinrsandquantifyingoverthegenericvariablesings.Ifthebindinggroupisrestricted,thenwemustdeferthepredicatesinrsaswellasthoseinds,andhencewecanonlyquantifyovervariablesingsthatdonotappearinrs.11.6.3CombinedBindingGroupsHaskellrequiresaprocessofdependencyanalysistobreakdowncompletesetsofbindings|eitheratthetop-levelofaprogram,orwithinalocalde nition|intothesmallestpossiblegroupsofmutuallyrecursivede nitions,andorderedsothatnogroupdependsonthevaluesde nedinlatergroups.Thisisnecessarytoobtainthemostgeneraltypespossible.Forexample,considerthefollowingfragmentfromastandardpreludeforHaskell:foldrfa(x:xs)=fx(foldrfaxs)foldrfa[]=aandxs=foldr(&&)TruexsIfthesede nitionswereplacedinthesamebindinggroup,thenwewouldnotobtainthemostgeneralpossibletypeforfoldr;alloccurrencesofavariablearerequiredtohavethesametypeateachpointwithinthede ningbindinggroup,whichwouldleadtothefollowingtypeforfoldr:(Bool�-Bool�-Bool)�-Bool�-[Bool]�-BoolToavoidthisproblem,weneedonlynoticethatthede nitionoffoldrdoesnotdependinanywayon&&,andhencewecanplacethetwofunctionsinseparatebindinggroups,inferring rstthemostgeneraltypeforfoldr,andthenthecorrecttypeforand.Inthepresenceofexplicitlytypedbindings,wecanre nethedependencyanalysisprocessalittlefurther.Forexample,considerthefollowingpairofbindings:f::Eqa�=a�-Boolfx=(x==x)||gTruegy=(y)||fTrueAlthoughthesebindingsaremutuallyrecursive,wedonotneedtoinfertypesforfandgatthesametime.Instead,wecanusethedeclaredtypeofftoinferatype:g::Orda&#x=y-5;─=a&#x=y-5;─-Boolandthenusethistocheckthebodyoff,ensuringthatitsdeclaredtypeiscorrect. 36M.P.Jonesthatincludesoneasrestricted.Soitmayseemthatthede nitionofrestrictedbindinggroupsinthispaperisnotquiteaccurate.However,ifweusetranslationsassuggestedhere,thenitturnsouttobeequivalent:eveniftheprogrammersuppliesexplicittypesignaturesforxandyintheoriginalprogram,thetranslationwillstillcontainanimplicitlytypedbindingforthenewvariablenv.Now,atlast,wearereadytopresentthealgorithmfortypeinferenceofacom-pletebindinggroup,asimplementedbythefollowingfunction:tiBindGroup::InferBindGroup[Assump]tiBindGroupceas(es;iss)=doletas0=[v:�:scj(v;sc;alts) es](ps;as00) tiSeqtiImplsce(as0++as)issqss mapM(tiExplce(as00++as0++as))esreturn(ps++concatqss;as00++as0)Thestructureofthisde nitionisquitestraightforward.Firstweformalistofassumptionsas0foreachoftheexplicitlytypedbindingsinthegroup.Next,weusethistocheckeachgroupofimplicitlytypedbindings,extendingtheassumptionsetfurtherateachstage.Finally,wereturntotheexplicitlytypedbindingstoverifythateachofthedeclaredtypesisacceptable.Indealingwiththelistofimplicitlytypedbindinggroups,weusethefollowingutilityfunction,whichtypechecksalistofbindinggroupsandaccumulatesassumptionsasitrunsthroughthelist:tiSeq::Inferbg[Assump]!Infer[bg][Assump]tiSeqticeas[]=return([];[])tiSeqticeas(bs:bss)=do(ps;as0) ticeasbs(qs;as00) tiSeqtice(as0++as)bssreturn(ps++qs;as00++as0)11.6.4Top-levelBindingGroupsAtthetop-level,aHaskellprogramcanbethoughtofasalistofbindinggroups:typeProgram=[BindGroup]Eventhede nitionsofmemberfunctionsinclassandinstancedeclarationscanbeincludedinthisrepresentation;theycanbetranslatedintotop-level,explicitlytypedbindings.Thetypeinferenceprocessforaprogramtakesalistofassumptionsgivingthetypesofanyprimitives,andreturnsasetofassumptionsforanyvariables.tiProgram::ClassEnv![Assump]!Program![Assump]tiProgramceasbgs=runTI$do(ps;as0) tiSeqtiBindGroupceasbgss getSubstrs reducece(applysps)s0 defaultSubstce[]rsreturn(apply(s0@@s)as0)ThiscompletesourpresentationoftheHaskelltypesystem. 38M.P.JonesMilner,R.(1978).Atheoryoftypepolymorphisminprogramming.JournalofComputerandSystemSciences,17(3).PeytonJones,Simon,&Hughes,John(eds).(1999).ReportontheProgrammingLanguageHaskell98,ANon-strictPurelyFunctionalLanguage.Availablefromhttp://www.haskell.org/definition/.PeytonJones,Simon,Jones,Mark,&Meijer,Erik.(1997).Typeclasses:Exploringthedesignspace.Proceedingsofthesecondhaskellworkshop.Robinson,J.A.(1965).Amachine-orientedlogicbasedontheresolutionprinciple.JournaloftheAssociationforComputingMachinery,12.Wadler,P.(1992).Theessenceoffunctionalprogramming(invitedtalk).Pages1{14of:ConferencerecordoftheNineteenthannualACMSIGPLAN-SIGACTsymposiumonPrinciplesofProgrammingLanguages.Wadler,P.,&Blott,S.(1989).Howtomakeadhocpolymorphismlessadhoc.Pages60{76of:Proceedingsof16thACMSymposiumonPrinciplesofProgrammingLanguages.