loetzsch09understanding.pdf

(453 KB) Pobierz
UnderstandingtheDynamicsofComplexLispPrograms
MartinLoetzsch
(AI-Lab,VrijeUniversiteitBrussel,Belgium
martin.loetzsch@gmail.com)
JorisBleys
(AI-Lab,VrijeUniversiteitBrussel,Belgium
jorisb@arti.vub.ac.be)
PieterWellens
(AI-Lab,VrijeUniversiteitBrussel,Belgium
pieter@arti.vub.ac.be)
Abstract:RecentadvancesinwebtechnologiesandtheavailabilityofrobustLisp
librariessupportingthemhavemadeitpossibletothinkofnewwaysofunderstanding
anddebugginglargeapplications.Inthispaper,wewilldiscusstwobasicideasfor
assessingandverifyingthebehaviourofLispprograms.First,weproposetouseaweb
browserforgraphicallydisplayingdebugoutputinasimilarbutmoreversatileway
astheLisplistenerisnormallyusedtoprintoutputtraces.Andsecond,wepropose
amethodforcreatingHTMLvisualisationsofcomplexdataandcontrolstructures
thatdon’ttradeinlevelofdetailforreadability.WewillintroduceGTFL(aGraphical
TerminalForLisp),whichwehaveimplementedbasedonthesetwoideas,anddiscuss
itsapplications.
1Introduction
Whenthebehaviourofasystemisdrivenbyunpredictableexternaldataorwhen
itemergesfromtheinterplayofmanydierentprocessesandrepresentations,
itisveryhardtokeeptrackoftheunderlyingcomplexdynamicsexhibitedby
thesystem.Evenwhenthebehaviourisasexpecteditmightbefarfromtrivial
tounderstandorshowwhythisisthecase,orforexamplehowitcouldbe
furtherrefined.Evidently,whenthebehaviourisnotthedesiredone,itisusually
veryhardtotrackdownthepartsofthesystemsthatrequiremodificationto
yieldmoredesireddynamics.Addingtothechallengeisthefactthatoftensome
subsystemscan’tbetestedinisolationbecausetheyareembeddedinarecurrent
networkofotherprocessesandconsequentlythefunctioningofthesystemcan
onlybeinvestigatedasawhole.Thesesortsofdicultiesinunderstandingthe
underlyingdynamicsofcomplexcodeareacommonprobleminmanyareasof
computersciencesuchasrobotics,artificialintelligenceanddistributedsystems.
Althougheverybodyhastheirownfavouritestyleofdebuggingwehaveob-
servedthatmostLisphackersuseacombinationoffourmajortechniquesfor
analysingthebehaviouroftheirprograms.Thefirstone,whichwecallthetrac-
ingapproach,consistsofprintingtracestotheLisplistenereitherbydirectly
addingprintstatementstothecodeorbyusingbuilt-intracefacilitiesorother
custommechanisms.Theadvantagesofthistechniquearethat(1)itisvery
easytodo(andinfactthisiswhatalmosteverybodywholearnstoprogram
startswith,forareviewsee[McCauleyetal.2008])and(2)itallowstomonitor
programexecutionatalmostanylevelofdetail(fromveryhigh-leveldynamics
onlytonear-completeinformation).Whenthelevelofdetailishighhowever,
theoutputrapidlybecomesunmanageable.Furthermore,plaintextinaLisp
listenerisnoteasytoreadandcanonlybepresentedlinearlywhichmakesit
impracticalforgettinganoverviewof,ora‘feeling’for,thedynamicsofthe
system.Andfinally,changingthelevelofdetailrequirestocommentoutprint
statementsinthecode(badstyle)ortoswitchonandotracingfacilities.
Asecondmethodistoretrospectivelyanalysethestateofasystembyin-
spectingLispobjects–eitherusinginspectortoolsofLispenvironmentsorby
directlycallingchainsofaccessorfunctionsonanobject.Inspectionhasthead-
vantagethatitdoesnotrequiretochangeorwriteanycodeandthecomplete
stateoftheobjectcanbeanalysed.Butinspectionisonlyapplicabletothefinal
resultsofaparticularalgorithmorprogram(exceptwhenusingbreakpoints).
Ingeneral,changesofdatastructurescan’tbeeasilyobservedusinginspection.
Additionally,itrequirestohaveaccesstotheproblematicinstanceinquestion
andoftenitisunclearwhichoneofthethousandsofobjectstoinspect.Finally,
inordertoquerydetailsofdeeplyhierarchicaldatastructures,aprogrammer
needstoperformahighnumberofmanualsteps,whichpreventsinspectionto
beusefulinmanycases.
Third,manyLispimplementationsanddevelopmentenvironmentsprovide
mechanismsformanuallysteppingthroughcodeeitherbyusingthebuiltinstep
facilitiesorbyinvokingthestepperdirectlyfromthecode.Controlstructures
canbetraversedandfunctionparameters,returnvaluesandlocalvariablescan
usuallybefurtherinvestigatedwithinspectors.Thiscertainlyisveryhelpfulfor
findinglogicalmistakesinsmallpiecesofcodesuchassignerrors,wrongloop
exitconditionsandsoforth,butinordertomakeuseofsteppingonehasto
knowwhichpartofthecodetolookat.Steppingthroughacompleteapplication
justforexploringitsdynamicswouldtakehoursandisthusimpracticable.
Alasttechniqueistocreatevisualisations,eitherwithinacustomGUIor
byusingspecialisedgraphicslibraries(e.g.gnuplot).Visualisationsaregreat
togetanintuitionofanalgorithm’sdynamicsbecauseourmindunderstands
graphicalrepresentationsmuchbetterthantextoutput,especiallyfornumerical
data[Kosslyn2006].That’swhyinareassuchasroboticsitisexceptionalto
evenwriteprogramswithouthavinggraphicalmeanstoverifythatthesystem
behavesasexpected(ideallyevenforeachintermediatestep).Visualisationsare
costly,becauseittakestimetoimplementthem,buttheyoftenpayointhelong
runwhentheycanbecomeaninvaluableeyeintothesystem’sinternals.Aclear
disadvantageisthatvisualrepresentationsonlyallowforalowtomediumlevelof
detail:complexdatastructureshavetobetransformedintotwo-dimensional(or
sometimes3D)representations,involvingconstraintssuchasavailablewindow
sizeandarequiredvisualclaritysothattherepresentationremainsreadable.
Furthermore,despitemanyrecentdevelopmentsincross-platformGUIAPIs,
thereisnonethatreallylooksgoodandthateasilyworksinallmajorCLs.And
unlesstheLispprograminquestionalreadyhasagraphicaluserinterface,there
isalotofoverheadindealingwithwindows,menusandotherUIelements,event
handlingandtheinteractionwiththeactualcodetomonitor.Thisisusually
toomuchoverheadwhenthegoalisto‘justdrawsomething’.Additionally,Lisp
isoftenusedforsymbolicprogramming,whichinvolvestextualandhierarchical
datastructures,whichinturnarehardtovisualise.Althoughvirtuallyevery
graphiclibraryhasmeanstodisplaytextonthescreen,theresponsibilityfor
arrangingtextblocks(estimatingwidthsanheightsoftextareasdependingon
availablespace,avoidingoverlap,reflowingmulti-linetext)isusuallyinthehand
oftheprogrammer.
Weproposeanoveltechniqueforanalysinganddebuggingcodeonthesystem
level,whichcombinesmanyofthemainadvantagesofthepreviouslyintroduced
techniques,whileatthesametimeremovingsomeoftheirrespectivedisadvan-
tages.
2GTFL-AGraphicalTerminalforLisp
WeproposetheGTFL(aGraphicalTerminalforLisp)libraryasatoolaimed
atLispprogrammersthatwanttounderstandthedynamicsoftheircode,es-
peciallywhentheprogramistoocomplextodebugbyprintingtexttracesto
thelistenerorbyusinginspectionandsteppingtools.GTFLrunsonallmajor
CLimplementations,isfree(BSD-Stylelicense)anditsdocumentationtogether
withthesourcecodecanbefoundat[Loetzsch2009].
GTFLisaterminalinthesensethatcontentispushedbyaLispprogram
toaclientandappearsthereinthesameorderasitwassent.Whatmightbe
surprising–butaswillbeshownhasgreatconsequences–theclientisaHTML
pagerunninginawebbrowser(seeFigure1).GTFLusestheHunchentootweb
server[Weitz2006]todelivertheclientpageandHT-AJAX[Marshak2007]to
exchangerequestsandcontentbetweentheclientandtheLispenvironment.
AfterinstallingGTFL(eitherbydownloadingitfrom[Loetzsch2009]orvia
asdf-install)andloadingthelibrarywithasdf,theinternalwebserverneedsto
bestartedwith:
(start-gtfl)
Figure1:GTFLinaction.ALispenvironment(left)hasGTFLloadedandsends
outputtotheclient.TheclientHTMLpage(right)showstheoutputasitis
sent.
TheclientHTMLpageisnowaccessibleat http://localhost:8000 (adierent
addressandportcanbespecifiedaswell).SinceGTFLusesquiteadvancedweb
technologies,werecommendtousearecentversionofSafari,FirefoxorChrome.
Thepageisinitiallyemptyexceptforasmall“reset”button.Sendingoutputto
theclientisquitesimple(seealsoexamplesinFigure1):
(gtfl-out(:p"hello"(:span:style"color:red;""world!"))
(:p"a"(:b"second")"paragraph"))
Theoutputimmediatelyappearsontheclientpageandlookslikethis:
hello world!
asecondparagraph
Themacro gtfl-out takesnumbersofs-expressionsasargumentsandrenders
themintoactualHTMLcodeusingCL-WHO[Weitz2003].Therearemany
otherLisplibrariesprovidingthisfunctionalityandingeneralwebrelatedLisp
toolsgiveusersthefreedomtouseanyofthem,butwehadtoselectonebecause
GTFLalsoprovidestoolsforcreatingcomplexHTMLconstructs.CL-WHOwas
ourfirstchoicebecauseitseemstobeoneofthewiderknownonesandbecause
itdoesn’tcreateintermediatestructuresatrun-time,makingitveryecient.
Behindthescenes,GTFLpushestherenderedHTMLcodesentby gtfl-out
onaninternal‘requests’list.Intheclientpage,aJavascriptbasedeventloop
usesAjaxcallstoquerythatlistontheLispsideevery200ms.Wheneverthere
issomethingnewtodisplay,itissenttotheclientpageasaresponsetosuch
anAjaxcall.
942516133.001.png
Withthisbasic‘terminal’functionality,GTFLcanalreadybeusedforde-
buggingwiththetracingtechniquedescribedabove.Theuserisonlyrequiredto
changetherelevantprintstatementsinthecodeto gtfl-out calls.Themajor
advantageisthattextcanbedisplayedindierentcolors,amultitudeoffont
stylesandsizesandwithbackgroundsorborders,whichallhelpinmakingthe
outputmuchmorereadablethanplaintext.
Furthermore,GTFLisgraphicalbecauseHTMLis.Inadditiontothepossi-
bilitiesofbasicfontformattingmentionedabove,HTMLtablesandotherblock
elementscanbeusedtostructuredataandtolayoutrepresentations.Contem-
poraryHTMLrenderingenginesareextremelygoodindistributingtheavailable
browserwindowwidthamongrecursivelynestedchildelements(andinreflow-
ingthelayoutwhenthewindowsizechangesorwhenmoredataisaddedtothe
page).Atrivialexampleforthisis:
(defparameter*text*"contentthatautomaticallyadaptsitswidth
accordingtotheavailablespace")
(gtfl-out
(:table:cellspacing"5":cellpadding"5"
(:tr(:td:style"background-color:yellow"(princ*text*))
(:td:style"border:1pxsolidblack"(princ*text*)))))
HTMLstyleattributesareusedtodisplay *text* indierentgraphicalstyles
twotimesnexttoeachother.Moreimportantly,theHTMLrendererremoves
theburdenofdecidinghowwidetodrawthetextblocksfromtheprogrammer:
Exploitingthisversatility,GTFLcontainsfunctionalityfordrawingtreesin
HTML.Thisisagainatrivialexample(forexamplesof‘real’treesseeFigures
1and2):
(gtfl-out
(draw-node-with-children
(who-lambda(:div:style"border:1pxsolid#000"
(:b(princ*text*))))
(list(who-lambda(:div:style"border:1pxdottedblue"
942516133.002.png
Zgłoś jeśli naruszono regulamin