/
:-typelist�---[];a;b;[list|list].%predicatesignatures::-app(list,list, :-typelist�---[];a;b;[list|list].%predicatesignatures::-app(list,list,

:-typelist---[];a;b;[list|list].%predicatesignatures::-app(list,list, - PDF document

yoshiko-marsland
yoshiko-marsland . @yoshiko-marsland
Follow
463 views
Uploaded On 2016-08-16

:-typelist---[];a;b;[list|list].%predicatesignatures::-app(list,list, - PPT Presentation

FinallyfortheSCCde ningq1oneobtainstypeelematypeelist1xTJ x0 1x195x6 Tdx 00elemelist1typeeblist2xTJ x0 1x195x6 Tdx 00elemeblist2b ID: 449906

Finally fortheSCCde ningq/1oneobtains::-typeelem---a.%:-typeelist1&#x]TJ ;� -1;.95; Td;&#x [00;---[elem|elist1];[].:-typeeblist2&#x]TJ

Share:

Link:

Embed:

Download Presentation from below link

Download Pdf The PPT/PDF document ":-typelist---[];a;b;[list|list].%predic..." 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

:-typelist�---[];a;b;[list|list].%predicatesignatures::-app(list,list,list).:-p(list).Notethatthelisttypeisnotthestandardone.Thereasonisthatapp/3iscalledoncewithlistsofa'sandb'sandoncewithlistswhoseelementsaretheformerlists.Thewell-typingconstraint,statingthatbothcallsmusthavethesamesignatureasthepredicateapp/3enforcestheaboveunnaturalsolution.Hence,themonomorphictypeinferenceisnotsointerestingforlargeprogramsastheylikelyusemanydi erenttypeinstancesofbasepredicates.InalanguagewithpolymorphictypessuchasMercury,[6],onetypicallydeclaresapp/3ashavingtypeapp(list(T),list(T),list(T)).The rstcallinstantiatesthetypeparameterTwiththeatypeelemde nedaselem�---a;bwhilethesecondcallinstantiatesTwithlist(elem).Thesetsoftermsdenotedbythesepolymorphictypeslist(elem)andlist(list(elem))arepropersubsetsofthemonomorphictypelist.Forin-stance,theterm[a|b]isoftypelist,butnotoftypelist(elem).Hence,polymorphictypesallowforamoreaccuratecharacterizationofprogramterms.Theworkin[1]alsosketchestheinferenceofapolymorphicwell-typing.However,therulespresentedthereareincomplete.Wereferto[4]foracom-prehensiveset.Inthispaper,werevisittheproblemofinferringapolymorphictyping.However,weimposetherestrictionthatcallstoapredicatethatoccurinsidethestronglyconnectedcomponent(SCC)thatde nesthepredicate(forsimplicitywerefertothemasrecursivecalls)havethesametypesignatureasthepredicate.Othercalls,appearinghigherinthecallgraphoftheprogramhaveatypesignaturethatisapolymorphicinstanceofthede nition'stype.Themotivationoftherestrictionisthatitcanbecomputationallyverydemandingwhenarecursivecallisallowedtohaveatypethatisatrueinstanceofthede nition'stype.Henglein[2]showedthattypecheckinginsuchasettingisun-decidable,andSchrijversandBruynooghe[4]havestrongindicationsofsimilarundecidabilityfortypeinference.Appliedontheaboveprogramfragment,oneobtainsthefollowingwell-typing::-typeelem�---a;b.:-typelist1(T)�---[];[T|list1(T)].:-typelist2(T)�---[];[T|list2(T)].:-app(list1(T),list2(T),list2(T)).:-p(list2(list2(elem)).:-callapp1(list1(elem),list2(elem),list2(elem)).:-callapp2(list1(list2(elem)),list2(list2(elem)),list2(list2(elem))).Bothlist1andlist2arerenamingsofthestandardlisttype,hencethiswell-typingisequivalenttowhatonewoulddeclareinalanguagesuchasMercury.Asforthetypesignaturesofthecalls,callappireferstothatoftheithcall.In Finally,fortheSCCde ningq/1oneobtains::-typeelem�---a.%:-typeelist1&#x]TJ ;� -1;.95; Td;&#x [00;---[elem|elist1];[].:-typeeblist2&#x]TJ ;� -1;.95; Td;&#x [00;---[elem|eblist2];b.%:-typeelistlist1&#x]TJ ;� -1;.95; Td;&#x [00;---[eblist2|elistlist1];[].:-typeelistlist2&#x]TJ ;� -1;.95; Td;&#x [00;---[eblist2|elistlist2];[].%signatures:-q(elistlist2).:-callapp1(elist1,eblist2,eblist2).:-callapp2(elistlist1,elistlist2,elistlist2).Thisrevealsthateblist2isnotastandardlistandthatitisthe rstcalltoapp/3thatemploysthistype.ThisexampleshowsthattheSCCbasedpolymorphicanalysisprovidesmoreusefulinformationthanthetruepolymorphicone.Itisinterestinginanotheras-pect.Itgivesuptheusualconceptunderlyingpolymorphictypingthateachpred-icateshouldhaveauniqueprincipaltypingandthatallcallsshouldhaveatypethatisaninstanceofit.Indeed,thetypesofthe rstandsecondcallareequiv-alenttoinstancesofthetypesignaturesapp(list1(T),blist2(T),blist2(T))andapp(list1(T),list2(T),list2(T))respectively,wherethetypeslist1(T)andlist2(T)arethestandardpolymorphiclisttypesbutblist2(T)isde nedasblist2(T)&#x]TJ ;� -1;.95; Td;&#x [00;---[T|eblist2(T)];b.Ourcontributionsarethefollowing:{WeproposeanewandecientpolymorphictypeanalysisbasedonanSCCbySCCtraversaloftheprogram.{Wecompareourapproachwithtwootheranalyses,acheapbutinaccuratemonomorphicanalysisandanaccuratebutexpensivepolymorphicanalysis.{Oursmallevaluationshowstherespectivemeritsofthedi erentanalyses.Intherestofthisabstract,wedescribethedi erentwell-typingsandtheirinferenceinmoredetailandweendwithasmallevaluationoftheirmerits.2ProblemStatementandBackgroundKnowledgeLogicProgramsOursyntaxoflogicprogramsisde nedasfollows:Program:={Clause};Clause:=Atom':-'Goal;Goal:=Atom|'('Goal,Goal')'|Term'='Term|'true';Atom:=Pred'('Term','...','Term')';Term:=Var|Functor'('Term','...','Term')';Pred,FunctorandVarrefertosetsofpredicatesymbols,functionsymbolsandvariablesrespectively.Elementsofthe rsttwosetsaredenotedwithstringsstartingwithalowercase,whereaselementsofVarstartwithanuppercase. (MonoCall)p(1;:::;n)2��`ti:i �`p(t1;:::;tn):(RecCall)p(1;:::;n)2��`ti:i �`p(t1;:::;tn):(PolyCall)p(1;:::;n)2��`ti:0i0i=i �`p(t1;:::;tn):(SCCCall)�`ti:0i(:-type0i�!:::)2��0[f :-type0i�!:::g[fp(01;:::;0n)g`subprog(p=n): �`p(t1;:::;tn): Fig.2.TheCallRulesThedi erentwaysofwell-typingacallaregiveninFigure2.Forthemonomor-phicanalysis,thewell-typingofacallisidenticaltothatofthepredicateintheenvironment(MonoCallrule).Fortheothertwoanalyses,thisonlyholdsfortherecursivecalls(RecCallrule).Thepolymorphicanalysisrequiresthatthetypeofanon-recursivecallisaninstance(undertypesubstitution)ofthetypeofthepredicate(PolyCallrule),whiletheSCCbasedanalysis(SCCCallrule)requiresthatthewell-typingofthecallin�|whichisp(01;:::;0n)|issuchthatthereexistsatypingenvironment(thatcanbedi erentfrom�)withthefollowingproperties:thesubprogramde ningthepredicate(subprog(p=n))iswell-typedin�0andthepredicatesignatureofp=nisp(01;:::;0n)itself.Notethatthisimpliesthatthereexistsapolymorphictypesignatureforp=nsuchthatp(01;:::;0n)isaninstanceofit;however,thatpolymorphictypecanbedi erentfordi erentcalls.Inallthreeanalyses,weareinterestedinminimalsolutions.Informally:fewercasesinatyperuleisbetterandonetypeisbetterthananother,whenthelatterisequivalenttoaninstanceoftheformer.3TheMonomorphicTypeAnalysisThemonomorphictypesystemissimple.Itrequiresthatallcallstoapredicatehaveexactlythesametypingasthesignature(ruleMonoCallinFigure2).Themonomorphictypeinference( rstdescribedin[1])consistsofthreephases:(1)Derivetypeconstraintsfromtheprogramtext.(2)Normalize(orsolve)thetypeconstraints.(3)Extracttypede nitionsandtypesignaturesfromthenormalizedconstraints.Apracticalimplementationmayinterleavethesephases.Inparticular,(1)maybeinterleavedwith(2)viaincrementalconstraintsolving.Wediscussthethreephasesinmoredetailbelow.Phase1:TypeConstraintDerivationForthepurposeofconstraintderivationweassumethatadistincttypeisassociatedwitheveryoccurrenceofaterm.Inaddition,everyde nedpredicatephasanassociatedtypesignaturep( ) {Atype thatdoesnotappearasthe rstargumentinaconstraintgetsasitstypeexpressionauniquetypevariableA.{Atypethatappearsonthelhsofaconstraintisassignedauniquetypenamet.ThistypenamehasasitsargumentsthetypevariablesAiofcorrespondingtypes isuchthat i1arethetypeexpressionsofthei.Thetypede nitionsfollowfromthetypeexpressionsinastraightforwardmanner.Foreachtypewithtypeexpressiont( A)wegetatypede nitiont( A)�!:::.Thisde nitioncontainsonecasef(:::)foreachconstraintf( ),wheretheargumentexpressionsarethetypeexpressionsofthetypes .PropertiesItisfairlyeasytoseethatbytheaboveapproachwegetasound,completeandterminatingalgorithm.Ofparticularinterestisthetimecomplexityofnormalization:Theorem1(TimeComplexity).Thenormalizationalgorithmhasanear-lineartimecomplexityO(n (n)),wherenistheprogramsizeand istheinverseAckermannfunction.The (n)factorfollowsfromtheUnifstep,ifimplementedwiththeoptimalunion- ndalgorithm.4ThePolymorphicTypeAnalysisThepolymorphictypesystemrelaxesthemonomorphicone.Thetypesignatureofnon-recursivepredicatecallsisaninstanceofthepredicatesignature,ratherthanbeingidenticaltoit.In[4],atypeinferenceisdescribedthatallowsthisalsoforrecursivecalls.constraintderivationtothecurrentsetting).Ithasacomplexsetofrulesbecausethereispropagationoftypeinformationbetweenpredicatesignatureandcallsignatureinbothdirections.Anewcaseinatyperulefromatypeinthesignatureofacallispropagatedtothecorrespondingtypeinthesignatureofthede nitionandinturnpropagatedtothecorrespondingtypesinthesignatureofallothercalls.There,itcanpotentiallyinteractwithotherconstraints,leadingtoyetanothercaseandtriggeringanewroundofpropagation.Experimentsindicateacubictimecomplexity.5TheSCCTypeAnalysisWesummarizebrie ytheprocedurefortheSCC-basedtypeinference.Thestronglyconnectedcomponents(SCCs)ofaprogramaresetsofpredicates,whereeachcomponentiseitherasingletoncontaininganon-recursivepredi-cateoramaximalsetofmutuallyrecursivepredicates.ThereisapartialorderontheSCCs;forcomponentss1ands2,s1s2i somepredicateins1depends(possiblyindirectly)onapredicateins2. 1I.e., iisreachablefromusingtheconstraints. Anotherscalablebenchmark(AppendixCof[5])provokestheworst-casequadraticbehavioroftheSCCanalysis,whichisstillmuchbetterthanthecubicbehaviorandconstantfactorsofthepolymorphicanalysis.7ConclusionandFutureWorkWithintheframeworkofpolymorphicwell-typingsofprograms,itiscustomarytohaveauniqueprincipaltypesignatureforpredicatede nitionsandtypesig-naturesofcalls(fromoutsidetheSCCde ningthepredicate)thatareinstancesoftheprincipaltype.WehavepresentedanovelSCC-basedtypeanalysisthatgivesuptheconceptofauniqueprincipaltypeandinsteadallowsdi erentcallstohavetypesignaturesthatareinstancesofdi erentwell-typingsofthepred-icatede nition.Thiso erstwoadvantages.Firstly,itismuchmoreecientthanatruepolymorphicanalysisandisonlyslightlymoreexpensivethanamonomorphicone.Inpractice,itscaleslinearlywithprogramsize.Secondly,whenanunexpectedcaseappearsinatyperule(whichmayhintataprogramerror),itiseasyto gureoutwhetheritisduetothepredicatede nitionortoaparticularcall.Thisinformationcannotbereconstructedfromtheinferredtypesinthepolymorphicandthemonomorphicanalyses.InfutureworkweplantoinvestigatethequalityofthenewanalysisbyperformingtypeinferenceonMercuryprogramswherealltypeinformationhasbeenremovedandcomparingtheinferredtypeswiththeoriginalones.References1.M.Bruynooghe,J.P.Gallagher,andW.VanHumbeeck.Inferenceofwell-typingforlogicprogramswithapplicationtoterminationanalysis.InC.HankinandI.Siveroni,editors,StaticAnalysis,SAS2005,Proceedings,volume3672ofLectureNotesinComputerScience,pages35{51.Springer,2005.2.F.Henglein.Typeinferencewithpolymorphicrecursion.ACMTrans.Program.Lang.Syst.,15(2):253{289,1993.3.A.MycroftandR.A.O'Keefe.ApolymorphictypesystemforProlog.Arti cialIntelligence,23(3):295{307,1984.4.T.SchrijversandM.Bruynooghe.Polymorphicalgebraicdatatypereconstruction.InA.BossiandM.J.Maher,editors,Proceedingsofthe8thInternationalACMSIGPLANConferenceonPrinciplesandPracticeofDeclarativeProgramming,July10-12,2006,Venice,Italy,pages85{96,2006.5.T.Schrijvers,J.Gallagher,andM.Bruynooghe.Frommonomorphictopolymorphicwell-typingsandbeyond,extendedreport.TechnicalReportCW518,Dept.Comp.Sc.,KatholiekeUniversiteitLeuven,2008.6.Z.Somogyi,F.Henderson,andT.Conway.TheexecutionalgorithmofMercury,anecientpurelydeclarativelogicprogramminglanguage.JournalofLogicPro-gramming,29(1-3):17{64,1996.