computer science/programming languages PROGRAMMING LANGUAGES ESSENTIALS OF Essentials of Programming Languages third edition Daniel P. Friedman and Mitchell Wand ESSENTIALS OF This book provides students with a deep, working understanding of the essential concepts of program- ming languages. Most of these essentials relate to the semantics, or meaning, of program elements, and the text uses interpreters (short programs that directly analyze an abstract representation of the THIRD EDITION program text) to express the semantics of many essential language elements in a way that is both clear PROGRAMMING and executable. The approach is both analytical and hands-on. The book provides views of program- ming languages using widely varying levels of abstraction, maintaining a clear connection between the high-level and low-level views. Exercises are a vital part of the text and are scattered throughout; the text explains the key concepts, and the exercises explore alternative designs and other issues. The complete LANGUAGES Scheme code for all the interpreters and analyzers in the book can be found online through The MIT Press website. For this new edition, each chapter has been revised and many new exercises have been added. Significant additions have been made to the text, including completely new chapters on modules and continuation-passing style. Essentials of Programming Languages can be used for both graduate and un- dergraduate courses, and for continuing education courses for programmers. Daniel P. Friedman is Professor of Computer Science at Indiana University and is the author of many books published by The MIT Press, including The Little Schemer (fourth edition, 1995), The Seasoned Schemer (1995), A Little Java, A Few Patterns (1997), each of these coauthored with Matthias Felleisen, and The Reasoned Schemer (2005), coauthored with William E. Byrd and Oleg Kiselyov. Mitchell Wand is THIRD EDITION Professor of Computer Science at Northeastern University. MD DALIM 955472 3/22/08 “With lucid prose and elegant code, this book provides the most concrete introduction to the few build- ing blocks that give rise to a wide variety of programming languages. I recommend it to my students and look forward to using it in my courses.” —Chung-chieh Shan, Department of Computer Science, Rutgers University “Having taught from EOPL for several years, I appreciate the way it produces students who understand the terminology and concepts of programming languages in a deep way, not just from reading about the concepts, but from programming them and experimenting with them. This new edition has an increased and Wand Friedman emphasis on types as contracts for defining procedure interfaces, which is quite important for many CYAN students.” —Gary T. Leavens, School of Electrical Engineering and Computer Science, University of Central Florida MAG “I’ve found the interpreters-based approach for teaching programming languages to be both compelling and rewarding for my students. Exposing students to the revelation that an interpreter for a program- YELO ming language is itself just another program opens up a world of possibilities for problem solving. The third edition of Essentials of Programming Languages makes this approach of writing interpreters more accessible than ever.” BLACK —Marc L. Smith, Department of Computer Science, Vassar College 978-0-262-06279-4 The MIT Press Daniel P. Friedman and Mitchell Wand Massachusetts Institute of Technology Cambridge, Massachusetts 02142 http://mitpress.mit.edu Essentials of Programming Languages third edition Essentials of Programming Languages third edition Daniel P. Friedman Mitchell Wand The MIT Press Cambridge, Massachusetts London, England © 2008 Daniel P. Friedman and Mitchell Wand 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. MIT Press books may be purchased at special quantity discounts for business or sales promotional use. For information, please email [email protected] or write to Special Sales Department, The MIT Press, 55 Hayward Street, Cambridge, MA 02142. This book was set in LATEX2ε by the authors, and was printed and bound in the United States of America. Library of Congress Cataloging-in-Publication Data Friedman, Daniel P. Essentials of programming languages / Daniel P. Friedman, Mitchell Wand. —3rd ed. p. cm. Includes bibliographical references and index. ISBN 978-0-262-06279-4 (hbk. : alk. paper) 1. Programming Languages (Electronic computers). I. Wand, Mitchell. II. Title. QA76.7.F73 2008 005.1—dc22 2007039723 10987654321 Contents Foreword by Hal Abelson ix Preface xv Acknowledgments xxi 1 Inductive Sets of Data 1 1.1 Recursively Specified Data 1 1.2 Deriving Recursive Programs 12 1.3 Auxiliary Procedures and Context Arguments 22 1.4 Exercises 25 2 Data Abstraction 31 2.1 Specifying Data via Interfaces 31 2.2 Representation Strategies for Data Types 35 2.3 Interfaces for Recursive Data Types 42 2.4 A Tool for Defining Recursive Data Types 45 2.5 Abstract Syntax and Its Representation 51 3 Expressions 57 3.1 Specification and Implementation Strategy 57 3.2 LET: A Simple Language 60 3.3 PROC: A Language with Procedures 74 3.4 LETREC: A Language with Recursive Procedures 82 3.5 Scoping and Binding of Variables 87 3.6 Eliminating Variable Names 91 3.7 Implementing Lexical Addressing 93 vi Contents 4 State 103 4.1 Computational Effects 103 4.2 EXPLICIT-REFS: A Language with Explicit References 104 4.3 IMPLICIT-REFS: A Language with Implicit References 113 4.4 MUTABLE-PAIRS: A Language with Mutable Pairs 124 4.5 Parameter-Passing Variations 130 5 Continuation-Passing Interpreters 139 5.1 A Continuation-Passing Interpreter 141 5.2 A Trampolined Interpreter 155 5.3 An Imperative Interpreter 160 5.4 Exceptions 171 5.5 Threads 179 6 Continuation-Passing Style 193 6.1 Writing Programs in Continuation-Passing Style 193 6.2 Tail Form 203 6.3 Converting to Continuation-Passing Style 212 6.4 Modeling Computational Effects 226 7Types 233 7.1 Values and Their Types 235 7.2 Assigning a Type to an Expression 238 7.3 CHECKED: A Type-Checked Language 240 7.4 INFERRED: A Language with Type Inference 248 8 Modules 275 8.1 The Simple Module System 276 8.2 Modules That Declare Types 292 8.3 Module Procedures 311 9 Objects and Classes 325 9.1 Object-Oriented Programming 326 9.2 Inheritance 329 9.3 The Language 334 9.4 The Interpreter 336 9.5 A Typed Language 352 9.6 The Type Checker 358 Contents vii A For Further Reading 373 B The SLLGEN Parsing System 379 B.1 Scanning 379 B.2 Parsing 382 B.3 Scanners and Parsers in SLLGEN 383 Bibliography 393 Index 401 Foreword This book brings you face-to-face with the most fundamental idea in com- puter programming: The interpreter for a computer language is just another program. It sounds obvious, doesn’t it? But the implications are profound. If you are a computational theorist, the interpreter idea recalls Gödel’s discovery of the limitations of formal logical systems, Turing’s concept of a universal computer, and von Neumann’s basic notion of the stored-program machine. If you are a programmer, mastering the idea of an interpreter is a source of great power. It provokes a real shift in mindset, a basic change in the way you think about programming. I did a lot of programming before I learned about interpreters, and I pro- duced some substantial programs. One of them, for example, was a large data-entry and information-retrieval system written in PL/I. When I imple- mented my system, I viewed PL/I as a fixed collection of rules established by some unapproachable group of language designers. I saw my job as not to modify these rules, or even to understand them deeply, but rather to pick through the (very) large manual, selecting this or that feature to use. The notion that there was some underlying structure to the way the language was organized, and that I might want to override some of the language design- ers’ decisions, never occurred to me. I didn’t know how to create embedded sublanguages to help organize my implementation, so the entire program seemed like a large, complex mosaic, where each piece had to be carefully shaped and fitted into place, rather than a cluster of languages, where the pieces could be flexibly combined. If you don’t understand interpreters, you can still write programs; you can even be a competent programmer. But you can’t be a master. x Foreword There are three reasons why as a programmer you should learn about interpreters. First, you will need at some point to implement interpreters, perhaps not interpreters for full-blown general-purpose languages, but interpreters just the same. Almost every complex computer system with which people inter- act in flexible ways—a computer drawing tool or an information-retrieval system, for example—includes some sort of interpreter that structures the interaction. These programs may include complex individual operations— shading a region on the display screen, or performing a database search— but the interpreter is the glue that lets you combine individual operations into useful patterns. Can you use the result of one operation as the input to another operation? Can you name a sequence of operations? Is the name local or global? Can you parameterize a sequence of operations, and give names to its inputs? And so on. No matter how complex and polished the individual operations are, it is often the quality of the glue that most directly determines the power of the system. It’s easy to find examples of programs with good individual operations, but lousy glue; looking back on it, I can see that my PL/I database program certainly had lousy glue.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages433 Page
-
File Size-