Efficient object-oriented programming in prolog

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

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

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

资源描述

EcientObject-OrientedProgramminginPrologPeterSchachteGeorgesSaabTechnicalReport93/29EcientObject-OrientedProgramminginPrologPeterSchachteGeorgesSaabpets@cs.mu.OZ.AUgs@quintus.comDepartmentofComputerScienceQuintusCorporationTheUniversityofMelbourne2100GengRoadParkville,Victoria3052PaloAlto,California94301AustraliaUSAAbstractWediscussclasses,apackageforecientobject-orientedprogramminginProlog.Succes-siveversionsaredescribed,alongwithadiscussionoftheproblemsencounteredwhenusingtheinitialversiontobuildapplicationproducts,andthesolutionsdevisedtocorrecttheseproblems.Twoexampleswhichusethepackageareincluded.TheworkdescribedhereinwasdonewhilethisauthorwasatQuintusCorporation.1IntroductionObjectorientedprogrammingoersmanyacknowledgedbenetsforapplicationdevelopers.En-capsulationaidsinprovidingstructuralclarity,inheritanceprovidesreusibility,andclassmodellingcanhelptheaspiringdeveloperanalyzethecomponentsthatmakeuptheapplication.1ThesewereexactlythebenetsthatQuintusdesiredwhenwebeganworkonWorkPro,aClient/Serverap-plicationwhichprovidesgraphicalinterfacestorelationaldatabasescombinedwithruleswritteninProlog.ManyoftheexistingobjectsystemsavailabletoQuintushadsevereperformancepenalties2,sowedecidedtobuildanewsystemdesignedwithdeliverable-applicationperformanceinmind.Inthispaper,wepresenttheresultsofthiseort.2TheObjectsPackage{AprecursortoClassesWhenwebeganworkonourQuintusWorkproapplication,weknewthatafull-featuredgraphicaluserinterface(GUI)wouldbeessential.Wealsobelievedthatsuchaninterfacewouldbeeasiesttobuildandmaintainusinganobject-orientedprogrammingstyle.Unfortunately,alltheobjectorientedPrologextensionsweknewofatthetimeeithermain-tainedstateusingProlog’sdatabase,ordidn’tallowobjectstobedestructivelymodiedatall.3TheseapproacheswouldbetoocostlyfortheheavyuseweanticipatedfortheGUI.WewantedeciencyapproachingthatofaC++program,andwerewillingtodeclarethetypesofthedatastoredineachobject’sslots.Quintushasapackage,the\structspackage[9],whichallowsonetodeclareClanguagedatastructures,andtoaccessandsetmembersofthesedatastructures.QuintusProloghasspeciallow-levelsupportforaccessingandsettingmemory,whichallowsthestructspackagetoinspectandmodifydatastructuresfairlyquickly.[5]WhatwereallywantedforbuildingourGUIwasanobjectorientedversionofthestructspackage.Sowebuiltone,andcalledittheobjectspackage.2.1ImplementationStrategyTheapproachwetookinbuildingtheobjectspackagewastotransformclassdenitionsintostructspackagedatastructuredenitions,usingProlog’stermexpansionfeature.Thisallowedustousethestructspackagedirectlytoaccessandmodifyobjects.2.1.1MessageDispatchWeusedasimpledispatchmechanismtoimplementmessagesending.Thismechanismtakesadvantageofthefactthatthestructspackage,andthereforetheobjectspackage,representapointertoadatastructure(objects)asatermwhosefunctorisitstype(class)andwhosesoleargumentistheaddressoftheactualdata.WhenamessageM,withnameNandarityAissenttoobjectO,weexecuteagoalsend(O,M).EachclassChasaclauseforthesend/2multilepredicatewhichtakesadvantageofrstargumentindexingtodispatchontheclassoftheobjecttoapredicatewhichhandlesallmessagesforthatclass.Thispredicateagainusesrstargumentindexingtodispatchonthemessagetoanotherpredicate,whichimplementstheactualmethod.Thatis,foreachclassC,there’saclausesend(C(Addr),Msg):-sendC(Msg,C(Addr)).andforeachmessagenamedNofarityAdenedforclassC,thereisaclause1Formoreinformationonthebenetsofobject-orientedprogramming,pleaseseeadedicatedtextsuchasRumbaugh[11]orBooch[2]2Thepackagesthatwehadaccesstowhenwebeganthisprojectin1990wereQuintusProWindows[6],ProTalk[8],andProlog++[13].Sincethistimeseveralnewpackageshavebecomeavailable,includingL&O[4],SicstusObjects[1],QuintecObjects[10]andOL(P)[3].Ithasnotyetbeenpossibletocomparethesesystemswiththeclassespackage.3ExamplesofofthesesortsofsystemsincludeProTalk[8]andProlog++[13].1sendC(N(X1,:::,XA),Obj):-sendCN(X1,:::,XA,Obj).aswellasclausesforsendCN/A+1implementingthemethod.Alloftheseclausesaregeneratedfromtheusersmethoddenitionusingtermexpansion.2.1.2InheritanceInheritanceisimplementedquitesimply.Sincewerequirethatasuperclassbecompiledbeforecompilingitssubclasses,wecankeeptrackofthemethodsdenedbythesuperclassandthemeth-odsdenedbythesubclasses.ForeachmethoddenedbyanancestorclassSandnotdened(shadowed)bytheclassCitself,wegenerateaninheritanceclausethatdispatchestoS’scodeforthatmessage:sendC(N(X1,:::,XA),Obj):-sendSN(X1,:::,XA,Obj).2.1.3SendSuperWealsoimplementsendsuper,whichallowsamethodtoinvokethemethoditshadowsdirectly.Thispermitstheusertoimplementamethodwhichmustdojustalittlemoreworkthanthesuperclass’smethodbyinvokingthatmethodfromthebodyofnewclass’smethod,sotheuserneedonlywritethecodethatisdierent.2.1.4ObjectConstructorsandDestructorsAnobjectiscreatedbycallingthenew_instance/2procedure,passingatermdescribingtheobjecttocreate.Theprinciplefunctorofthistermmustbetheclassoftheobjecttobecreated,andthearguments(ifany)arepassedasparameterstothecreationmethodfortheclass.Thismethodmayinitializeanyslots,createotherobjects,ordowhateverotherinitiali

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

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

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

×
保存成功