Denotational Semantics ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ A METHODOLOGY FOR LANGUAGE DEVELOPMENT ____________________________________________________________________________ ____________________________________________________________________________ David A. Schmidt Copyright notice: Copyright 1997 by David A. Schmidt. Permission to reproduce this materialmustbeobtainedfromtheauthor. Author’s address: David Schmidt, Department of Computing and Information Sciences, 234 NicholsHall,KansasStateUniversity,Manhattan,KS66506. [email protected] Contents Preface viii Chapter0 INTRODUCTION ________________________________________________________________________________________________ 1 MethodsforSemanticsSpecification 2 SuggestedReadings 3 Chapter1 SYNTAX ______________________________________________________________________________________________________________ 5 1.1 AbstractSyntaxDefinitions 9 1.2 MathematicalandStructuralInduction 12 SuggestedReadings 15 Exercises 15 Chapter2 SETS, FUNCTIONS,ANDDOMAINS ______________________________________________________________ 17 2.1 Sets 17 2.1.1 ConstructionsonSets 18 2.2 Functions 20 2.2.1 RepresentingFunctionsasSets 21 2.2.2 RepresentingFunctionsasEquations 24 2.3 SemanticDomains 25 2.3.1 SemanticAlgebras 25 SuggestedReadings 27 Exercises 27 Chapter3 DOMAINTHEORYI:SEMANTICALGEBRAS ____________________________________________ 30 3.1 PrimitiveDomains 30 3.2 CompoundDomains 34 3.2.1 Product 34 3.2.2 DisjointUnion 35 3.2.3 FunctionSpace 39 3.2.4 LiftedDomainsandStrictness 42 iii iv Contents 3.3 RecursiveFunctionDefinitions 44 3.4 RecursiveDomainDefinitions 46 3.5 Summary 46 SuggestedReadings 48 Exercises 48 Chapter4 BASICSTRUCTUREOFDENOTATIONALDEFINITIONS ________________________ 54 4.1 TheValuationFunction 54 4.2 FormatofaDenotationalDefinition 57 4.3 ACalculatorLanguage 59 SuggestedReadings 63 Exercises 63 Chapter5 IMPERATIVELANGUAGES ____________________________________________________________________________ 66 5.1 ALanguagewithAssignment 66 5.1.1 ProgramsareFunctions 72 5.2 AnInteractiveFileEditor 73 5.2.1 InteractiveInputandPartialSyntax 78 5.3 ADynamicallyTypedLanguagewithInputandOutput 80 5.4 AlteringthePropertiesofStores 82 5.4.1 DelayedEvaluation 82 5.4.2 RetainingMultipleStores 86 5.4.3 NoncommunicatingCommands 87 SuggestedReadings 88 Exercises 88 Chapter6 DOMAINTHEORYII:RECURSIVELYDEFINEDFUNCTIONS ________________ 94 6.1 SomeRecursivelyDefinedFunctions 95 6.2 PartialOrderings 98 6.3 ContinuousFunctions 102 6.4 LeastFixedPoints 103 6.5 DomainsareCpos 104 6.6 Examples 109 6.6.1 FactorialFunction 109 6.6.2 CopyoutFunction 110 6.6.3 DoubleRecursion 111 6.6.4 SimultaneousDefinitions 111 v 6.6.5 TheWhile-Loop 112 6.6.6 SoundnessofHoare’sLogic 115 6.7 ReasoningaboutLeastFixedPoints 117 SuggestedReadings 119 Exercises 120 Chapter7 LANGUAGESWITHCONTEXTS ____________________________________________________________________ 125 7.1 ABlock-StructuredLanguage 127 7.1.1 Stack-ManagedStorage 134 7.1.2 TheMeaningsofIdentifiers 136 7.2 AnApplicativeLanguage 137 7.2.1 ScopingRules 140 7.2.2 Self-Application 143 7.2.3 RecursiveDeclarations 145 7.3 CompoundDataStructures 148 SuggestedReadings 154 Exercises 154 Chapter8 ABSTRACTION,CORRESPONDENCE,ANDQUALIFICATION ________________ 160 8.1 Abstraction 161 8.1.1 RecursiveBindings 164 8.2 Parameterization 165 8.2.1 Polymorphism andTyping 167 8.3 Correspondence 170 8.4 Qualification 171 8.5 Orthogonality 173 SuggestedReadings 174 Exercises 174 Chapter9 CONTROLASASEMANTICDOMAIN __________________________________________________________ 178 9.1 Continuations 178 9.1.1 OtherLevelsofContinuations 181 9.2 ExceptionMechanisms 182 9.3 BacktrackingMechanisms 183 9.4 CoroutineMechanisms 183 9.5 UnrestrictedBranchingMechanisms 187 vi Contents 9.6 TheRelationshipbetweenDirectandContinuationSemantics 193 SuggestedReadings 195 Exercises 195 Chapter10 IMPLEMENTATIONOFDENOTATIONALDEFINITIONS ________________________ 199 10.1 AGeneralMethodofImplementation 199 10.1.1 TheSISandSPS Systems 200 10.2 StaticSemanticsProcessing 201 10.3 TheStructureoftheEvaluator 203 10.3.1 AStack-BasedEvaluator 204 10.3.2 PSP andAppel’sSystem 208 10.4 Combinator-BasedSemanticNotations 209 10.4.1 ThePlumbandCERESSystems 211 10.5 TransformationsontheSemanticDefinition 212 10.5.1 First-OrderDataObjects 212 10.5.2 GlobalVariables 214 10.5.3 ControlStructures 217 10.6 ImplementationofContinuation-BasedDefinitions 218 10.6.1 TheCGPandVDMMethods 222 10.7 CorrectnessofImplementationandFullAbstraction 223 SuggestedReadings 225 Exercises 226 Chapter11 DOMAINTHEORYIII:RECURSIVEDOMAINSPECIFICATIONS __________ 230 11.1 ReflexiveDomainsHaveInfiniteElements 230 11.2 TheInverseLimitConstruction 234 11.3 Applications 241 11.3.1 LinearLists 242 11.3.2 Self-ApplicativeProcedures 243 11.3.3 RecursiveRecordStructures 244 SuggestedReadings 245 Exercises 245 Chapter12 NONDETERMINISMANDCONCURRENCY ________________________________________________ 250 12.1 Powerdomains 251 12.2 TheGuardedCommandLanguage 251 vii 12.3 ConcurrencyandResumptionSemantics 254 12.4 AnAlternativeSemanticsforConcurrency 258 12.5 ThePowerdomainStructure 265 12.5.1 DiscretePowerdomains 266 12.5.2 GeneralPowerdomains 268 SuggestedReadings 274 Exercises 274 Bibliography 217 Preface Denotational semantics is a methodology for giving mathematical meaning to programming languages and systems. It was developed by Christopher Strachey’s Programming Research GroupatOxfordUniversityinthe1960s.Themethodcombinesmathematicalrigor,duetothe work of Dana Scott, with notational elegance, due to Strachey. Originally used as an analysis tool, denotational semantics has grown in use as a tool for language design and implementa- tion. Thisbookwaswrittento makedenotationalsemanticsaccessibleto awideraudienceand to update existing texts in the area. I havepresentedthe topic from an engineeringviewpoint, emphasizing the descriptional and implementational aspects. The relevantmathematics is also included, for it gives rigor and validity to the method and provides a foundation for further research. Thebookis intendedasatutorial forcomputing professionalsandasatext foruniversity courses at the upper undergraduate or beginning graduate level. The reader should be acquainted with discrete structures and one or more general purpose programming languages. Experiencewithanapplicative-stylelanguagesuchasLISP,ML,orSchemeisalsohelpful. CONTENTSOFTHEBOOK __________________________________________________________________________________________________ The Introduction and Chapters 1 through 7 form the core of the book. The Introduction pro- vides motivation and a brief survey of semantics specification methods. Chapter 1 introduces BNF, abstract syntax, and structural induction. Chapter 2 lists those concepts of set theory thatarerelevanttosemanticdomaintheory.Chapter3coverssemanticdomains,thevaluesets used in denotational semantics. The fundamental domains and their related operations are presented. Chapter 4 introduces basic denotational semantics. Chapter 5 covers the semantics ofcomputerstorageandassignmentasfoundinconventionalimperativelanguages. Nontradi- tional methods of store evaluation are also considered. Chapter 6 presents least fixed point semantics, which is used for determining the meaning of iterative and recursive definitions. The related semantic domain theory is expanded to include complete partial orderings; ‘‘predomains’’ (complete partial orderings less ‘‘bottom’’ elements) are used. Chapter 7 cov- ersblockstructureanddatastructures. Chapters8through12presentadvancedtopics. Tennent’sanalysisofproceduralabstrac- tionandgeneralbindingmechanismsisusedasafocalpointforChapter8.Chapter9analyzes forms of imperative control and branching. Chapter 10 surveys techniques for converting a denotational definition into a computer implementation. Chapter 11 contains an overview of Scott’s inverse limit construction for building recursively defined domains. Chapter 12 closes thebookwithanintroductiontomethodsforunderstandingnondeterminismandconcurrency. Throughout the book I have consistently abused the noun ‘‘access,’’treating it as a verb. Also,‘‘iff’’abbreviatesthephrase‘‘ifandonlyif.’’ viii Preface ix ORGANIZATIONOFACOURSE ________________________________________________________________________________________ Thebookcontainsmorematerialthan what canbe comfortablycoveredin one term. A course plan should include the core chapters; any remaining time can be used for Chapters 8 through 12, which areindependent of one another and can be read in any order. The core can be han- dledasfollows: • Present the Introduction first. You may wish to give a one lecture preview of Chapter 4. A preview motivates the students to carefully study the material in the background Chapters1through3. • CoverallofChapters1and2,astheyareshortandintroducecrucialconcepts. • Use Chapter 3 as a ‘‘referencemanual.’’ You may wish to start at the summary Section 3.5 and outline the structures of semantic domains. Next, present examples of semantic algebrasfromthebodyofthechapter. • Cover all of Chapter 4 and at least Sections 5.1 and 5.4 from Chapter 5. If time allows, coverallofChapter5. • Summarize Chapter 6 in one or two lectures for an undergraduatecourse. This summary canbetakenfromSection6.1. Agraduatecourseshouldcoverallofthechapter. • CoverasmuchofChapter7aspossible. REFERENCESANDEXERCISES ________________________________________________________________________________________ Following each chapter is a short list of references that suggests further reading. Each refer- enceidentifies theauthorandtheyearofpublication. Lettersa, b, c,andsoon,areusedifthe author has multiple references for a year. The references are compiled in the bibliography in the back of the book. I have tried to make the bibliography current and complete, but this appears to be an impossible task, and I apologize to those researchers whose efforts I have unintentionallyomitted. Exercises are provided for each chapter. The order of a chapter’s exercises parallels the order of presentation of the topics in the chapter. The exercises are not graded according to difficulty; an hour’s effort on a problem will allow the reader to make that judgment and will alsoaiddevelopmentofintuitions aboutthesignificantproblemsinthearea. ACKNOWLEDGEMENTS ____________________________________________________________________________________________________ Many people deserve thanks for their assistance, encouragement, and advice. In particular, I thank Neil Jones for teaching me denotational semantics; Peter Mosses for answering my questions; Robin Milner for allowing me to undertake this project while under his employ; Paul Chisholm for encouraging me to write this book and for reading the initial draft; Allen Stoughton formanystimulating discussions; Colin Stirling forbeing an agreeableofficemate; andmyparents,family,andfriendsinKansas,foralmosteverythingelse. John Sulzycki of Allyn and Bacon deserves special thanks for his interest in the project, x Preface and Laura Cleveland, Sue Freese, and Jane Schulman made the book’s production run smoothly. The reviewers Jim Harp, Larry Reeker, Edmond Schonberg, and Mitchell Wand contributed numerous useful suggestions. (I apologize for the flaws that remain in spite of theirefforts.)Thoseinstructorsandtheirstudents who used preliminarydraftsas texts deserve thanks;theyareJim Harp,AustinMelton,Colin Stirling, DavidWise,andtheirstudentsatthe universities of Lowell, Kansas State, Edinburgh, and Indiana, respectively. My students at Edinburgh,IowaState,andKansasStatealsocontributedusefulsuggestionsandcorrections. Finally, the text would not have been written had I not been fortunate enough to spend several years in Denmark and Scotland. I thank the people at Aarhus University, Edinburgh University, Heriot-Watt University, and The Fiddler’s Arms for providing stimulating and congenialenvironments. Iwouldbepleasedtoreceivecommentsandcorrectionsfromthereadersofthisbook.