1. Trang chủ
  2. » Giáo Dục - Đào Tạo

essentials of programming languages 3rd edition apr 2008

433 398 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 433
Dung lượng 3,41 MB

Nội dung

ESSENTIALS OF PROGRAMMING LANGUAGES Daniel P. Friedman and Mitchell Wand THIRD EDITION ESSENTIALS OF PROGRAMMING LANGUAGES THIRD EDITION Friedman and Wand MD DALIM 955472 3/22/08 CYAN MAG YELO BLACK computer science/programming languages Essentials of Programming Languages third edition Daniel P. Friedman and Mitchell Wand 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 program text) to express the semantics of many essential language elements in a way that is both clear 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 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 Professor of Computer Science at Northeastern University. “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 emphasis on types as contracts for defining procedure interfaces, which is quite important for many students.” —Gary T. Leavens, School of Electrical Engineering and Computer Science, University of Central Florida “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- 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.” —Marc L. Smith, Department of Computer Science, Vassar College The MIT Press Massachusetts Institute of Technology Cambridge, Massachusetts 02142 http://mitpress.mit.edu 978-0-262-06279-4 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 special_sales@mitpress.mit.edu or write to Special Sales Department, The MIT Press, 55 Hayward Street, Cambridge, MA 02142. This book was set in L A T E X2 ε 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 j ust 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. [...]... () ∈ List -of- Int n ∈ Int l ∈ List -of- Int (n l) ∈ List -of- Int These three definitions are equivalent We can show how to use them to generate some elements of List -of- Int 1 () is a list of integers, because of property 1 of definition 1.1.4 or the first rule of definition 1.1.5 2 (14 ()) is a list of integers, because of property 2 of definition 1.1.4, since 14 is an integer and () is a list of integers... instance of the second rule for List -of- Int 14 ∈ Int () ∈ List -of- Int (14 ()) ∈ List -of- Int 5 1.1 Recursively Specified Data 3 (3 (14 ())) is a list of integers, because of property 2, since 3 is an integer and (14 ()) is a list of integers We can write this as another instance of the second rule for List -of- Int 3 ∈ Int (14 ()) ∈ List -of- Int (3 (14 ())) ∈ List -of- Int 4 (-7 (3 (14 ()))) is a list of. .. Benjamin Pierce offered a number of insightful observations after teaching from the first edition, almost all of which we have incorporated Gary Leavens provided exceptionally thorough and valuable comments on early drafts of the second edition, including a large number of detailed suggestions for change Stephanie Weirich found a subtle bug in the type inference code of the second edition of chapter 7... If you do this, you will change your view of your programming, and your view of yourself as a programmer You’ll come to see yourself as a designer of languages rather than only a user of languages, as a person who chooses the rules by which languages are put together, rather than only a follower of rules that other people have chosen Postscript to the Third Edition The foreword above was written only... craft the interpreters that bring these languages to life These are the skills you will learn from this book Hal Abelson Cambridge, Massachusetts September 2007 Preface Goal This book is an analytic study of programming languages Our goal is to provide a deep, working understanding of the essential concepts of programming languages These essentials have proved to be of enduring importance; they form a... List -of- Int could be written using “ | ” as List -of- Int ::= () | (Int List -of- Int) Another shortcut is the Kleene star, expressed by the notation { .}∗ When this appears in a right-hand side, it indicates a sequence of any number of instances of whatever appears between the braces Using the Kleene star, the definition of List -of- Int is simply List -of- Int ::= ({Int}∗ ) This includes the possibility of. .. (list of integers, bottom-up) The set List -of- Int is the smallest set of Scheme lists satisfying the following two properties: 1 () ∈ List -of- Int, and 2 if n ∈ Int and l ∈ List -of- Int, then (n l) ∈ List -of- Int Here we use the infix “.” to denote the result of the cons operation in Scheme The phrase (n l) denotes a Scheme pair whose car is n and whose cdr is l Definition 1.1.5 (list of integers, rules of. .. members of List -of- Int We can also combine the rules to get a picture of the entire chain of reasoning that shows that (-7 (3 (14 ()))) ∈ List -of- Int The tree-like picture below is called a derivation or deduction tree 14 ∈ N 3∈N -7 ∈ N () ∈ List -of- Int (14 ()) ∈ List -of- Int (3 (14 ())) ∈ List -of- Int (-7 (3 (14 ()))) ∈ List -of- Int Exercise 1.1 [ ] Write inductive definitions of the following... rules -of- inference version 4 1 Inductive Sets of Data Let us see how this works on some other examples Definition 1.1.3 (list of integers, top-down) A Scheme list is a list of integers if and only if either 1 it is the empty list, or 2 it is a pair whose car is an integer and whose cdr is a list of integers We use Int to denote the set of all integers, and List -of- Int to denote the set of lists of integers... binary tree, as defined in definition 1.1.7 Then t contains an odd number of nodes Proof: The proof is by induction on the size of t, where we take the size of t to be the number of nodes in t The induction hypothesis, IH(k), is that any tree of size ≤ k has an odd number of nodes We follow the usual prescription for an inductive proof: we first prove that IH(0) is true, and we then prove that whenever . 02142 http://mitpress .mit. edu 978-0-262-06279-4 Essentials of Programming Languages third edition Essentials of Programming Languages third edition Daniel P. Friedman Mitchell Wand The MIT Press Cambridge,. used as the basis for the material in the second part of the chapter. The second part combines the ideas of the type checker of chapter 7 with those of the object-oriented language of the first. as a designer of languages rather than only a user of languages, as a person who chooses the rules by which languages are put together, rather than only a follower of rules that other people have

Ngày đăng: 11/06/2014, 13:24

TỪ KHÓA LIÊN QUAN

w