ESSENTIALS OF
PROGRAMMING
LANGUAGES
THIRD EDITION
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
E
S
S
E
N
T
I
A
L
S
O
F
I
P
R
O
G
R
A
M
M
N
G
L
A
N
G
U
A
G
E
S
T
H
I
R
D
E
D
I
T
I
O
N
F
r
i
e
d
m
a
n
a
n
d
W
a
n
d
The MIT Press
Massachusetts Institute of Technology
Cambridge, Massachusetts 02142
http://mitpress.mit.edu
978-0-262-06279-4
Daniel P. Friedman and Mitchell Wand
M
D
D
A
L
I
M
9
5
5
4
7
2
3
/
2
2
/
0
8
C
Y
A
N
M
A
G
Y
E
L
O
B
L
A
C
K
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 LATEX 2ε 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.
cm.
p.
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
10 9 8 7 6 5 4 3 2 1
2007039723
Contents
Foreword by Hal Abelson
Preface
Acknowledgments
1 Inductive Sets of Data
1.1 Recursively Specified Data
1.2 Deriving Recursive Programs
1.3 Auxiliary Procedures and Context Arguments
1.4 Exercises
2 Data Abstraction
2.1 Specifying Data via Interfaces
2.2 Representation Strategies for Data Types
2.3 Interfaces for Recursive Data Types
2.4 A Tool for Defining Recursive Data Types
2.5 Abstract Syntax and Its Representation
3 Expressions
3.1 Specification and Implementation Strategy
3.2 LET: A Simple Language
3.3 PROC: A Language with Procedures
3.4 LETREC: A Language with Recursive Procedures
3.5 Scoping and Binding of Variables
3.6 Eliminating Variable Names
3.7 Implementing Lexical Addressing
ix
xv
xxi
1
1
12
22
25
31
31
35
42
45
51
57
57
60
74
82
87
91
93
vi
Contents
4 State
4.1 Computational Effects
4.2 EXPLICIT-REFS: A Language with Explicit References
4.3 IMPLICIT-REFS: A Language with Implicit References
4.4 MUTABLE-PAIRS: A Language with Mutable Pairs
4.5 Parameter-Passing Variations
5 Continuation-Passing Interpreters
5.1 A Continuation-Passing Interpreter
5.2 A Trampolined Interpreter
5.3 An Imperative Interpreter
5.4 Exceptions
5.5 Threads
6 Continuation-Passing Style
6.1 Writing Programs in Continuation-Passing Style
6.2 Tail Form
6.3 Converting to Continuation-Passing Style
6.4 Modeling Computational Effects
7 Types
7.1 Values and Their Types
7.2 Assigning a Type to an Expression
7.3 CHECKED: A Type-Checked Language
7.4 INFERRED: A Language with Type Inference
8 Modules
8.1 The Simple Module System
8.2 Modules That Declare Types
8.3 Module Procedures
9 Objects and Classes
9.1 Object-Oriented Programming
9.2 Inheritance
9.3 The Language
9.4 The Interpreter
9.5 A Typed Language
9.6 The Type Checker
103
103
104
113
124
130
139
141
155
160
171
179
193
193
203
212
226
233
235
238
240
248
275
276
292
311
325
326
329
334
336
352
358
Contents
A For Further Reading
B The SLLGEN Parsing System
B.1 Scanning
B.2 Parsing
B.3 Scanners and Parsers in SLLGEN
Bibliography
Index
vii
373
379
379
382
383
393
401