Denotational Semantics ______
Total Page:16
File Type:pdf, Size:1020Kb
Denotational Semantics ____________________________________________________________________________ ____________________________________________________________________________ ____________________________________________________________________________ A METHODOLOGY FOR LANGUAGE DEVELOPMENT ____________________________________________________________________________ ____________________________________________________________________________ David A. Schmidt Copyright notice: Copyright 1997 by David A. Schmidt. Permission to reproduce this material must be obtained from the author. Author’s address: David Schmidt, Department of Computing and Information Sciences, 234 Nichols Hall, Kansas State University, Manhattan, KS 66506. [email protected] Contents Preface viii Chapter 0 INTRODUCTION _______________________________________________________________________________________________ 1 Methods for Semantics Specification 2 Suggested Readings 3 Chapter 1 SYNTAX ______________________________________________________________________________________________________________ 5 1.1 Abstract Syntax Definitions 9 1.2 Mathematical and Structural Induction 12 Suggested Readings 15 Exercises 15 Chapter 2 SETS, FUNCTIONS, AND DOMAINS ______________________________________________________________ 17 2.1 Sets 17 2.1.1 Constructions on Sets 18 2.2 Functions 20 2.2.1 Representing Functions as Sets 21 2.2.2 Representing Functions as Equations 24 2.3 Semantic Domains 25 2.3.1 Semantic Algebras 25 Suggested Readings 27 Exercises 27 Chapter 3 DOMAIN THEORY I: SEMANTIC ALGEBRAS ____________________________________________ 30 3.1 Primitive Domains 30 3.2 Compound Domains 34 3.2.1 Product 34 3.2.2 Disjoint Union 35 3.2.3 Function Space 39 3.2.4 Lifted Domains and Strictness 42 iii iv Contents 3.3 Recursive Function Definitions 44 3.4 Recursive Domain Definitions 46 3.5 Summary 46 Suggested Readings 48 Exercises 48 Chapter 4 BASIC STRUCTURE OF DENOTATIONAL DEFINITIONS _______________________ 54 4.1 The Valuation Function 54 4.2 Format of a Denotational Definition 57 4.3 A Calculator Language 59 Suggested Readings 63 Exercises 63 Chapter 5 IMPERATIVE LANGUAGES ___________________________________________________________________________ 66 5.1 A Language with Assignment 66 5.1.1 Programs are Functions 72 5.2 An Interactive File Editor 73 5.2.1 Interactive Input and Partial Syntax 78 5.3 A Dynamically Typed Language with Input and Output 80 5.4 Altering the Properties of Stores 82 5.4.1 Delayed Evaluation 82 5.4.2 Retaining Multiple Stores 86 5.4.3 Noncommunicating Commands 87 Suggested Readings 88 Exercises 88 Chapter 6 DOMAIN THEORY II: RECURSIVELY DEFINED FUNCTIONS _______________ 94 6.1 Some Recursively Defined Functions 95 6.2 Partial Orderings 98 6.3 Continuous Functions 102 6.4 Least Fixed Points 103 6.5 Domains are Cpos 104 6.6 Examples 109 6.6.1 Factorial Function 109 6.6.2 Copyout Function 110 6.6.3 Double Recursion 111 6.6.4 Simultaneous Definitions 111 v 6.6.5 The While-Loop 112 6.6.6 Soundness of Hoare’s Logic 115 6.7 Reasoning about Least Fixed Points 117 Suggested Readings 119 Exercises 120 Chapter 7 LANGUAGES WITH CONTEXTS ___________________________________________________________________ 125 7.1 A Block-Structured Language 127 7.1.1 Stack-Managed Storage 134 7.1.2 The Meanings of Identifiers 136 7.2 An Applicative Language 137 7.2.1 Scoping Rules 140 7.2.2 Self-Application 143 7.2.3 Recursive Declarations 145 7.3 Compound Data Structures 148 Suggested Readings 154 Exercises 154 Chapter 8 ABSTRACTION, CORRESPONDENCE, AND QUALIFICATION ______________ 160 8.1 Abstraction 161 8.1.1 Recursive Bindings 164 8.2 Parameterization 165 8.2.1 Polymorphism and Typing 167 8.3 Correspondence 170 8.4 Qualification 171 8.5 Orthogonality 173 Suggested Readings 174 Exercises 174 Chapter 9 CONTROL AS A SEMANTIC DOMAIN _________________________________________________________ 178 9.1 Continuations 178 9.1.1 Other Levels of Continuations 181 9.2 Exception Mechanisms 182 9.3 Backtracking Mechanisms 183 9.4 Coroutine Mechanisms 183 9.5 Unrestricted Branching Mechanisms 187 vi Contents 9.6 The Relationship between Direct and Continuation Semantics 193 Suggested Readings 195 Exercises 195 Chapter 10 IMPLEMENTATION OF DENOTATIONAL DEFINITIONS _______________________ 199 10.1 A General Method of Implementation 199 10.1.1 The SIS and SPS Systems 200 10.2 Static Semantics Processing 201 10.3 The Structure of the Evaluator 203 10.3.1 A Stack-Based Evaluator 204 10.3.2 PSP and Appel’s System 208 10.4 Combinator-Based Semantic Notations 209 10.4.1 The Plumb and CERES Systems 211 10.5 Transformations on the Semantic Definition 212 10.5.1 First-Order Data Objects 212 10.5.2 Global Variables 214 10.5.3 Control Structures 217 10.6 Implementation of Continuation-Based Definitions 218 10.6.1 The CGP and VDM Methods 222 10.7 Correctness of Implementation and Full Abstraction 223 Suggested Readings 225 Exercises 226 Chapter 11 DOMAIN THEORY III: RECURSIVE DOMAIN SPECIFICATIONS __________ 230 11.1 Reflexive Domains Have Infinite Elements 230 11.2 The Inverse Limit Construction 234 11.3 Applications 241 11.3.1 Linear Lists 242 11.3.2 Self-Applicative Procedures 243 11.3.3 Recursive Record Structures 244 Suggested Readings 245 Exercises 245 Chapter 12 NONDETERMINISM AND CONCURRENCY ________________________________________________ 250 12.1 Powerdomains 251 12.2 The Guarded Command Language 251 vii 12.3 Concurrency and Resumption Semantics 254 12.4 An Alternative Semantics for Concurrency 258 12.5 The Powerdomain Structure 265 12.5.1 Discrete Powerdomains 266 12.5.2 General Powerdomains 268 Suggested Readings 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 Group at Oxford University in the 1960s. The method combines mathematical rigor, due to the 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. This book was written to make denotational semantics accessible to a wider audience and to update existing texts in the area. I have presented the topic from an engineering viewpoint, emphasizing the descriptional and implementational aspects. The relevant mathematics is also included, for it gives rigor and validity to the method and provides a foundation for further research. The book is intended as a tutorial for computing professionals and as a text for university 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. Experience with an applicative-style language such as LISP, ML, or Scheme is also helpful. CONTENTS OF THE BOOK ________________________________________________________________________________________________ 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 that are relevant to semantic domain theory. Chapter 3 covers semantic domains, the value sets used in denotational semantics. The fundamental domains and their related operations are presented. Chapter 4 introduces basic denotational semantics. Chapter 5 covers the semantics of computer storage and assignment as found in conventional imperative languages. 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- ers block structure and data structures. Chapters 8 through 12 present advanced topics. Tennent’s analysis of procedural abstrac- tion and general binding mechanisms is used as a focal point for Chapter 8. Chapter 9 analyzes 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 the book with an introduction to methods for understanding nondeterminism and concurrency. Throughout the book I have consistently abused the noun ‘‘access,’’ treating it as a verb. Also, ‘‘iff’’ abbreviates the phrase ‘‘if and only if.’’ viii Preface ix ORGANIZATION OF A COURSE ________________________________________________________________________________________ The book contains more material than what can be comfortably covered in one term. A course plan should include the core chapters; any remaining time can be used for Chapters 8 through 12, which are independent of one another and can be read in any order. The core can be han- dled as follows: • 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 Chapters 1 through 3. • Cover all of Chapters 1 and 2, as they are short and introduce crucial concepts. • Use Chapter 3 as a ‘‘reference manual.’’ You may wish to start at the summary Section 3.5 and outline the structures of semantic domains. Next,