Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 1.347 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
1.347
Dung lượng
20,68 MB
Nội dung
DesignConceptsinProgrammingLanguages FRANKLYN TURBAK AND DAVID GIFFORD WITH MARK A. SHELDON DesignConceptsinProgrammingLanguages TURBAK,GIFFORD, AND SHELDON COMPUTER SCIENCE/PROGRAMMING LANGUAGES Hundreds of programminglanguages are in use today—scripting languages for Internet commerce, user interface programming tools, spreadsheet mac- ros, page format specification languages, and many others. Designing a programming language is a metaprogramming activity that bears certain similarities to programmingin a regular language, with clarity and simplicity even more important than in ordinary programming. This comprehensive text uses a simple and concise framework to teach key ideas inprogramming lan- guage design and implementation. The book’s unique approach is based on a family of syntactically simple pedagogical languages that allow students to explore programming language concepts systematically. It takes as its prem- ise and starting point the idea that when language behaviors become incred- ibly complex, the description of the behaviors must be incredibly simple. The book presents a set of tools (a mathematical metalanguage, abstract syntax, operational and denotational semantics) and uses it to explore a comprehensive set of programming language design dimensions, including dynamic semantics (naming, state, control, data), static semantics (types, type reconstruction, polymorphism, effects), and pragmatics (compilation, garbage collection). The many examples and exercises offer students oppor- tunities to apply the foundational ideas explained inthe text. Specialized topics and code that implements many of the algorithms and compilation methods inthe book can be found on the book’s Web site, along with such additional material as a section on concurrency and proofs of the theorems inthe text. The book is suitable as a text for an introductory graduate or ad- vanced undergraduate programminglanguages course; it can also serve as a reference for researchers and practitioners. DesignConceptsinProgrammingLanguages FRANKLYN TURBAK AND DAVID GIFFORD WITH MARK A. SHELDON Franklyn Turbak is Associate Professor inthe Computer Science Department at Wellesley College. David Gifford is Professor of Computer Science and Engineering at MIT. Mark A. Sheldon is Visiting Assistant Professor inthe Computer Science Department at Wellesley College. “There is a paucity of good graduate-level textbooks on the foundations of programming languages, no more than four or five inthe last two decades. Nothing to compare with the profusion of excellent texts inthe other core areas of computer science, such as algorithms or operating systems. This new textbook by Franklyn Turbak, David Gifford, and Mark Sheldon—comprehensive, thorough, pedagogically innovative, impeccably written and organized—greatly enriches the area of programminglanguages and will be an important reference for years to come.” Assaf Kfoury Department of Computer Science, Boston University “This book is an excellent, systematic exploration of ideas and techniques inprogramming language theory. The book carefully, but without wasting time on extraneous compli- cations, explains operational and denotational semantic techniques and their application to many aspects of programming language design. It will be of great value for graduate courses and for independent study.” Gary T. Leavens School of Electrical Engineering and Computer Science, University of Central Florida THEMITPRESS MASSACHUSETTS INSTITUTE OF TECHNOLOGY CAMBRIDGE, MASSACHUSETTS 02142 HTTP://MITPRESS.MIT.EDU COVER PHOTOGRAPH: DAVID GIFFORD 978-0-262-20175-9 On the cover is an inuksuk, a signpost used by the Inuit inthe Arctic to provide guidance in vast wilderness. The concise semantics, type rules, effect rules, and compilation transforms in this book have been valuable inuksuit to the authors intheprogramming language landscape. MD DALIM #970524 6/24/08 CYAN MAG YELO BLK DesignConceptsinProgrammingLanguagesDesignConceptsinProgrammingLanguages Franklyn Turbak and David Gifford with Mark A. Sheldon TheMITPress Cambridge, Massachusetts London, England c 2008 Massachusetts Institute of Technology All rights reserved. No part of this book may be reproduced in any form by any electronic or mechanical means (including photocopying, recording, or information storage and retrieval) without permission in writing from the publisher. MITPress books may be purchased at special quantity discounts for business or sales promotional use. For information, please email special sales@mitpress.mit.edu or write to Special Sales Department, TheMIT Press, 55 Hayward Street, Cambridge, MA 02142. This book was set in L A T E X by the authors, and was printed and bound inthe United States of America. Library of Congress Cataloging-in-Publication Data Turbak, Franklyn A. Designconceptsinprogramminglanguages / Franklyn A. Turbak and David K. Gifford, with Mark A. Sheldon. p. cm. Includes bibliographical references and index. ISBN 978-0-262-20175-9 (hardcover : alk. paper) 1. Programminglanguages (Electronic computers). I. Gifford, David K., 1954–. II. Sheldon, Mark A. III. Title. QA76.7.T845 2008 005.1—dc22 2008013841 10987654321 Brief Contents Preface xix Acknowledgments xxi I Foundations 1 1 Introduction 3 2Syntax 19 3 Operational Semantics 45 4 Denotational Semantics 113 5 Fixed Points 163 II Dynamic Semantics 205 6 FL: A Functional Language 207 7 Naming 307 8 State 383 9 Control 443 10 Data 539 III Static Semantics 615 11 Simple Types 617 12 Polymorphism and Higher-order Types 701 13 Type Reconstruction 769 14 Abstract Types 839 15 Modules 889 16 Effects Describe Program Behavior 943 IV Pragmatics 1003 17 Compilation 1005 18 Garbage Collection 1119 A A Metalanguage 1147 B Our Pedagogical Languages 1197 References 1199 Index 1227 Contents Preface xix Acknowledgments xxi I Foundations 1 1 Introduction 3 1.1 ProgrammingLanguages 3 1.2 Syntax, Semantics, and Pragmatics 4 1.3 Goals 6 1.4 PostFix: A Simple Stack Language 8 1.4.1 Syntax 8 1.4.2 Semantics 9 1.4.3 The Pitfalls of Informal Descriptions 14 1.5 Overview of the Book 15 2Syntax 19 2.1 Abstract Syntax 20 2.2 Concrete Syntax 22 2.3 S-Expression Grammars Specify ASTs 23 2.3.1 S-Expressions 23 2.3.2 The Structure of S-Expression Grammars 24 2.3.3 Phrase Tags 30 2.3.4 Sequence Patterns 30 2.3.5 Notational Conventions 32 2.3.6 Mathematical Foundation of Syntactic Domains 36 2.4 The Syntax of PostFix 39 3 Operational Semantics 45 3.1 The Operational Semantics Game 45 3.2 Small-step Operational Semantics (SOS) 49 3.2.1 Formal Framework 49 3.2.2 Example: An SOS for PostFix 52 3.2.3 Rewrite Rules 54 3.2.4 Operational Execution 58 viii Contents 3.2.5 Progress Rules 62 3.2.6 Context-based Semantics 71 3.3 Big-step Operational Semantics 75 3.4 Operational Reasoning 79 3.5 Deterministic Behavior of EL 80 3.6 Termination of PostFix Programs 84 3.6.1 Energy 84 3.6.2 The Proof of Termination 86 3.6.3 Structural Induction 88 3.7 Safe PostFix Transformations 89 3.7.1 Observational Equivalence 89 3.7.2 Transform Equivalence 92 3.7.3 Transform Equivalence Implies Observational Equivalence 96 3.8 Extending PostFix 100 4 Denotational Semantics 113 4.1 The Denotational Semantics Game 113 4.2 A Denotational Semantics for EL 117 4.2.1 Step 1: Restricted ELMM 117 4.2.2 Step 2: Full ELMM 120 4.2.3 Step 3: ELM 124 4.2.4 Step 4: EL 127 4.2.5 A Denotational Semantics Is Not a Program 128 4.3 A Denotational Semantics for PostFix 131 4.3.1 A Semantic Algebra for PostFix 131 4.3.2 A Meaning Function for PostFix 134 4.3.3 Semantic Functions for PostFix: the Details 142 4.4 Denotational Reasoning 145 4.4.1 Program Equality 145 4.4.2 Safe Transformations: A Denotational Approach 147 4.4.3 Technical Difficulties 150 4.5 Relating Operational and Denotational Semantics 150 4.5.1 Soundness 151 4.5.2 Adequacy 157 4.5.3 Full Abstraction 159 4.5.4 Operational versus Denotational: A Comparison 161 Contents ix 5 Fixed Points 163 5.1 The Fixed Point Game 163 5.1.1 Recursive Definitions 163 5.1.2 Fixed Points 166 5.1.3 The Iterative Fixed Point Technique 168 5.2 Fixed Point Machinery 174 5.2.1 Partial Orders 174 5.2.2 Complete Partial Orders (CPOs) 182 5.2.3 Pointedness 185 5.2.4 Monotonicity and Continuity 187 5.2.5 The Least Fixed Point Theorem 190 5.2.6 Fixed Point Examples 191 5.2.7 Continuity and Strictness 197 5.3 Reflexive Domains 201 5.4 Summary 203 II Dynamic Semantics 205 6 FL: A Functional Language 207 6.1 Decomposing Language Descriptions 207 6.2 The Structure of FL 208 6.2.1 FLK: The Kernel of the FL Language 209 6.2.2 FL Syntactic Sugar 218 6.2.3 The FL Standard Library 235 6.2.4 Examples 239 6.3 Variables and Substitution 244 6.3.1 Terminology 244 6.3.2 Abstract Syntax DAGs and Stoy Diagrams 248 6.3.3 Alpha-Equivalence 250 6.3.4 Renaming and Variable Capture 251 6.3.5 Substitution 253 6.4 An Operational Semantics for FLK 258 6.4.1 FLK Evaluation 258 6.4.2 FLK Simplification 270 6.5 A Denotational Semantics for FLK 275 6.5.1 Semantic Algebra 275 6.5.2 Valuation Functions 280 6.6 The Lambda Calculus 290 [...]... and the code is no longer any fun to read or update Designing a new programming language is a kind of metalevel programming activity that is just as much fun as programmingin a regular language (if not more so) You will discover that clarity and simplicity are even more important in language design than they are in ordinary programming Today hundreds of programming languages are in use — whether they... cases, the mini -languages are so pared down that they are hardly suitable for serious programming activities Nevertheless, these languages embody all of the key ideas inprogramminglanguages Their simplicity saves us from getting bogged down in needless complexity in our explorations of semantics and pragmatics And like good modular building blocks, the components of the mini -languages are designed... Pragmatics Programminglanguages are traditionally viewed in terms of three facets: 1 Syntax — the form of programminglanguages 2 Semantics — the meaning of programminglanguages 3 Pragmatics — the implementation of programming languages Here we briefly describe these facets Syntax Syntax focuses on the concrete notations used to encode programming language phrases Consider a phrase that indicates the sum... specifies the mapping between the structure of a programming language phrase and what the phrase means Such phrases have no inherent meaning: their meaning is determined only inthe context of a system for interpreting their structure For example, consider the following expression tree: * + 1 10 11 Suppose we interpret the nodes labeled 1, 10, and 11 as the usual decimal notation for numbers, and the nodes... capability to their end users Elements of programming language design are even found in “ordinary” programming For instance, consider designing the interface to a collection data structure What is a good way to encapsulate an iteration idiom over the elements of such a collection? The issues faced in this problem are similar to those in adding a looping construct to a programming language The goal of... minilanguages for our source and target languages For our implementation language, we will use the mathematical metalanguage described in Appendix A However, we strongly encourage readers to build working interpreters and translators for the mini -languages in their favorite real-world programming languages Metaprogramming — writing programs that manipulate other programs — is perhaps the most exciting... programming language implementation techniques and program improvement strategies rather than focus on squeezing the last ounce of performance out of a particular computer architecture We will discuss programming language features inthe context of several minilanguages Each of these is a simple programming language that captures the essential features of a class of existing programminglanguages In. .. context Using and implementing novel programming language concepts will further enhance your understanding The Web Supplement contains interpreters for various pedagogical languages used inthe book, and there are many implementationbased exercises that will help forge connections between theory and practice To the Teacher We teach the highlights of the material in this book in 24 lectures over a 14week... cover all of the detail inthe book The Web Supplement contains an example lecture schedule, reading assignments, and problem sets In addition, theMIT OpenCourseWare site at ocw .mit. edu contains material from previous versions of 6.821 This book can be used to teach many different kinds of courses, including an introduction to semantics (Chapters 1–5), essential concepts of programming languages (Chapters... “snapped together” to create practical languages Issues of semantics and pragmatics are important for reasoning about properties of programming languages and about particular programs in these languages We will also discuss them inthe context of two fundamental strategies for programming language implementation: interpretation and translation Inthe interpretation approach, a program written in a source . inuksuit to the authors in the programming language landscape. MD DALIM #970524 6/24/08 CYAN MAG YELO BLK Design Concepts in Programming Languages Design Concepts in Programming Languages Franklyn. SCIENCE /PROGRAMMING LANGUAGES Hundreds of programming languages are in use today—scripting languages for Internet commerce, user interface programming tools, spreadsheet mac- ros, page format specification languages, . Design Concepts in Programming Languages FRANKLYN TURBAK AND DAVID GIFFORD WITH MARK A. SHELDON Design Concepts in Programming Languages TURBAK,GIFFORD, AND SHELDON COMPUTER SCIENCE/PROGRAMMING