Under consideration for publication in J. Functional Programming 1 The Logic of Demand in H
- 格式:pdf
- 大小:455.61 KB
- 文档页数:52
UnderconsiderationforpublicationinJ.FunctionalProgramming1
TheLogicofDemandinHaskell
WILLIAML.HARRISON
DepartmentofComputerScienceUniversityofMissouriColumbia,Missouri
RICHARDB.KIEBURTZ
PacificSoftwareResearchCenterOGISchoolofScience&EngineeringOregonHealth&ScienceUniversity
Abstract
Haskellisafunctionalprogramminglanguagewhoseevaluationislazybydefault.However,Haskellalsoprovidespatternmatchingfacilitieswhichaddamodicumofeagernesstoitsotherwiselazydefaultevaluation.Thismixedor“non-strict”semanticscanbequitedifficulttoreasonwith.Thispaperintroducesaprogramminglogic,P-logic,whichneatlyformalizesthemixedevaluationinHaskellpattern-matchingasalogic,therebysimplifyingthetaskofspecifyingandverifyingHaskellprograms.InP-logic,aspectsofdemandarereflectedorrepresentedwithinboththepredicatelanguageanditsmodeltheory,allowingforexpressiveandcomprehensibleprogramverification.
1Introduction
AlthoughHaskellisknownasalazyfunctionallanguagebecauseofitsdefaulteval-
uationstrategy,itcontainsanumberoflanguageconstructsthatforceexceptions
tothatstrategy.Amongthesefeaturesarepattern-matching,datatypestrictness
annotationsandtheseqprimitive.Thesemanticsofpattern-matchingarefurther
enrichedbyirrefutablepatternannotations,whichmaybeembeddedwithinpat-
terns.TheinteractionbetweenHaskell’sdefaultlazyevaluationanditspattern-
matchingissurprisinglycomplicated.Althoughitofferstheprogrammerafacility
forfinecontrolofdemand(Harrisonetal.,2002),itisperhapstheaspectofthe
Haskelllanguageleastwellunderstoodbyitscommunityofusers.Inthispaper,we
characterizethecontrolofdemandfirstinadenotationalsemanticsandthenina
verificationlogiccalled“P-logic”.
P-logic1isamodallogicbaseduponthefamiliarGentzen-stylesequentcalculus
(Girard,1989).P-logicisexpressivedirectlyoverHaskellexpressions—theterm
languageofthelogicisHaskell98.Thetwomodalitiesofthelogic,calledweakand
1ThenameP-logicistakenfromtheProgramaticaproject(www.cse.ogi.edu/PacSoft/projects/programatica)atOGI.2WilliamL.HarrisonandRichardB.Kieburtz
strong,determinewhetherapredicateisinterpretedbyasetofnormalizedvalues
ofitstype(thestronginterpretation)orbyasetofcomputationsofitstype,which
mayormaynotterminate(theweakinterpretation).Thestrongmodalityisused
tocharacterizepropertiesofanexpressionoccuringinastrictcontextofaprogram,
orofanexpressionconstructedinnormalform.Theweakmodalitycanbeusedto
characterizepropertiesofanexpressionoccuringinanon-strictcontext.
ThispaperintroducesthefragmentofP-logicthatprovidesverificationcondi-
tionsforacorefragmentofHaskell,includingabstraction,applicationandcaseex-
pressions(withoutguards).Italsoprovidesaself-containeddescriptionofatyped,
denotationalsemanticsforthisHaskellfragment.ThesemanticsfortheHaskell
fragmentisbasedonanextensiontothetypeframessemanticsofthesimply-typed
lambdacalculus(Gunter,1992;Mitchell,2000)andiscloselyrelatedtoanearlier
treatment(Harrisonetal.,2002).Thissemanticsconstitutesthecoreofadeno-
tationalsemanticsforHaskell98,thewholeofwhichwillbepublishedinsequel
articles.
BecauseHaskellpatternsaffordfinecontrolofdemand,itisnotpossibletogive
completeverificationconditionsforpatternedabstractionsorcaseexpressionsin
afinitesetofspecificrules.InthepresentationofP-logic,wegivethelogical
inferencerulesforpatternsbydefiningverificationconditiongenerators—functions
onthetermstructureofpatternswhichconstructpatternpredicates.Averification
conditionforapropertyofaHaskellcasebranchisderivedbyapplyingaverification
conditiongeneratortoitspatternandthelistofpredicatesthatitsvariablesare
assumedtosatisfy.Itgeneratesapredicatecharacterizingtermsthatcanmatchthe
patternwithitsassumedproperties.Verificationconditiongeneratorsarewritten
asHaskellfunctionsinaprototypeimplementationofP-logic.
Theremainderofthepaperproceedsasfollows.Section2givesanoverviewofthe
Haskellfragmentweconsiderhere.Thisfragmentcontainsthelanguageconstructs
thatmostdirectlymakeuseofpattern-matching.Section3containsbackground
informationforoursemanticmodel:typeframesemanticsandthesimplemodelof
MLpolymorphism(Ohori,1989b;Ohori,1989a).Section4summarizestheformal
semanticsofthisfragmentandSection5presentsthefragmentofP-logicthatdeals
withHaskell’sfinecontrolofdemand.SoundnessoftheP-logicinferencerulesis
establishedinSection6andSection7discussessomealternativeapproachesto
verificationlogics.Section8summarizesourconclusions.
2AHaskellfragmentanditsinformalsemantics
ThissectiondescribesthefragmentofHaskellweconsiderinthispaper.Thisfrag-
ment,whosesyntaxisgiveninFigure1,isrepresentativeoftheHaskellconstructs
thatdependonpattern-matchingandstrictnessannotations.Itconstitutesanearly-
completecorelanguageforHaskellexpressions,omittingguardedexpressions,type
classesandoverloadedoperators.Section2.2givesaninformaloverviewofthe