ebook img

Principles of Programming Languages PDF

2009·0.88 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Principles of Programming Languages

Principles of Programming Languages Version 0.7 Mike Grant Zachary Palmer Scott Smith http://www.cs.jhu.edu/~scott/pl/book/dist ii Copyright (cid:13)c 2002-2009 Scott F. Smith. This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. This document was last compiled on November 30, 2011. Contents Preface vii 1 Introduction 1 1.1 The Pre-History of Programming Languages . . . . . . . . . . . . 1 1.2 A Brief Early History of Languages . . . . . . . . . . . . . . . . . 2 1.3 This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 2 Operational Semantics 5 2.1 A First Look at Operational Semantics. . . . . . . . . . . . . . . 5 2.2 BNF grammars and Syntax . . . . . . . . . . . . . . . . . . . . . 6 2.2.1 Operational Semantics for Logic Expressions . . . . . . . 7 2.2.2 Abstract Syntax . . . . . . . . . . . . . . . . . . . . . . . 12 2.2.3 Operational Semantics and Interpreters . . . . . . . . . . 15 2.3 The F(cid:91) Programming Language . . . . . . . . . . . . . . . . . . . 17 2.3.1 F(cid:91) Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2.3.2 Variable Substitution . . . . . . . . . . . . . . . . . . . . 19 2.3.3 Operational Semantics for F(cid:91) . . . . . . . . . . . . . . . . 23 2.3.4 The Expressiveness of F(cid:91) . . . . . . . . . . . . . . . . . . 31 2.3.5 Russell’s Paradox and Encoding Recursion . . . . . . . . 35 2.3.6 Call-By-Name Parameter Passing . . . . . . . . . . . . . . 40 2.3.7 F(cid:91) Abstract Syntax . . . . . . . . . . . . . . . . . . . . . 41 2.4 Operational Equivalence . . . . . . . . . . . . . . . . . . . . . . . 46 2.4.1 Defining Operational Equivalence . . . . . . . . . . . . . . 47 2.4.2 Properties of Operational Equivalence . . . . . . . . . . . 48 2.4.3 Examples of Operational Equivalence . . . . . . . . . . . 49 2.4.4 The λ-Calculus . . . . . . . . . . . . . . . . . . . . . . . . 51 3 Tuples, Records, and Variants 53 3.1 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 3.2 Records . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 3.2.1 Record Polymorphism . . . . . . . . . . . . . . . . . . . . 55 3.2.2 The F(cid:91)R Language. . . . . . . . . . . . . . . . . . . . . . 56 3.3 Variants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 3.3.1 Variant Polymorphism . . . . . . . . . . . . . . . . . . . . 58 iii iv CONTENTS 3.3.2 The F(cid:91)V Language . . . . . . . . . . . . . . . . . . . . . 58 4 Side Effects: State and Exceptions 61 4.1 State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 4.1.1 The F(cid:91)S Language . . . . . . . . . . . . . . . . . . . . . . 63 4.1.2 Cyclical Stores . . . . . . . . . . . . . . . . . . . . . . . . 68 4.1.3 The “Normal” Kind of State . . . . . . . . . . . . . . . . 69 4.1.4 Automatic Garbage Collection . . . . . . . . . . . . . . . 70 4.2 Environment-Based Interpreters. . . . . . . . . . . . . . . . . . . 71 4.3 The F(cid:91)SR Language . . . . . . . . . . . . . . . . . . . . . . . . . 72 4.3.1 Multiplication and Factorial . . . . . . . . . . . . . . . . . 73 4.3.2 Merge Sort . . . . . . . . . . . . . . . . . . . . . . . . . . 74 4.4 Exceptions and Other Control Operations . . . . . . . . . . . . . 77 4.4.1 Interpreting Return . . . . . . . . . . . . . . . . . . . . . 78 4.4.2 The F(cid:91)X Language. . . . . . . . . . . . . . . . . . . . . . 80 4.4.3 Implementing the F(cid:91)X Interpreter . . . . . . . . . . . . . 81 4.4.4 Efficient Implementation of Exceptions. . . . . . . . . . . 83 5 Object-Oriented Language Features 85 5.1 Encoding Objects in F(cid:91)SR . . . . . . . . . . . . . . . . . . . . . 88 5.1.1 Simple Objects . . . . . . . . . . . . . . . . . . . . . . . . 88 5.1.2 Object Polymorphism . . . . . . . . . . . . . . . . . . . . 90 5.1.3 Information Hiding . . . . . . . . . . . . . . . . . . . . . . 91 5.1.4 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 5.1.5 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . 93 5.1.6 Dynamic Dispatch . . . . . . . . . . . . . . . . . . . . . . 95 5.1.7 Static Fields and Methods . . . . . . . . . . . . . . . . . . 96 5.2 The F(cid:91)OB Language . . . . . . . . . . . . . . . . . . . . . . . . . 97 5.2.1 Concrete Syntax . . . . . . . . . . . . . . . . . . . . . . . 98 5.2.2 A Direct Interpreter . . . . . . . . . . . . . . . . . . . . . 100 5.2.3 Translating F(cid:91)OB to F(cid:91)SR . . . . . . . . . . . . . . . . . 101 6 Type Systems 105 6.1 An Overview of Types . . . . . . . . . . . . . . . . . . . . . . . . 106 6.2 TF(cid:91): A Typed F(cid:91) Variation . . . . . . . . . . . . . . . . . . . . . 109 6.2.1 Design Issues . . . . . . . . . . . . . . . . . . . . . . . . . 109 6.2.2 The TF(cid:91) Language . . . . . . . . . . . . . . . . . . . . . . 110 6.3 Type Checking . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 6.4 Types for an Advanced Language: TF(cid:91)SRX . . . . . . . . . . . 115 6.5 Subtyping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 6.5.1 Motivation . . . . . . . . . . . . . . . . . . . . . . . . . . 119 6.5.2 The STF(cid:91)R Type System: TF(cid:91) with Records and Sub- typing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 6.5.3 Implementing an STF(cid:91)R Type Checker . . . . . . . . . . 122 6.5.4 Subtyping in Other Languages . . . . . . . . . . . . . . . 123 6.6 Type Inference and Polymorphism . . . . . . . . . . . . . . . . . 123 CONTENTS v 6.6.1 Type Inference and Polymorphism . . . . . . . . . . . . . 123 6.6.2 An Equational Type System: EF(cid:91) . . . . . . . . . . . . . 124 6.6.3 PEF(cid:91): EF(cid:91) with Let Polymorphism . . . . . . . . . . . . 129 6.7 Constrained Type Inference . . . . . . . . . . . . . . . . . . . . . 131 7 Concurrency 135 7.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 7.1.1 The Java Concurrency Model . . . . . . . . . . . . . . . . 137 7.2 The Actor Model and AF(cid:91)V . . . . . . . . . . . . . . . . . . . . 137 7.2.1 Syntax of AF(cid:91)V . . . . . . . . . . . . . . . . . . . . . . . 138 7.2.2 An Example . . . . . . . . . . . . . . . . . . . . . . . . . 139 7.2.3 Operational Semantics of Actors . . . . . . . . . . . . . . 140 7.2.4 The Local Rules . . . . . . . . . . . . . . . . . . . . . . . 140 7.2.5 The Global Rule . . . . . . . . . . . . . . . . . . . . . . . 142 7.2.6 The Atomicity of Actors . . . . . . . . . . . . . . . . . . . 142 8 Compilation by Program Transformation 143 8.1 Closure Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . 144 8.1.1 The Official Closure Conversion . . . . . . . . . . . . . . . 147 8.2 A-Translation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 8.2.1 The Official A-Translation . . . . . . . . . . . . . . . . . . 150 8.3 Function Hoisting. . . . . . . . . . . . . . . . . . . . . . . . . . . 152 8.4 Translation to C . . . . . . . . . . . . . . . . . . . . . . . . . . . 154 8.4.1 Memory Layout. . . . . . . . . . . . . . . . . . . . . . . . 155 8.4.2 The toC translation . . . . . . . . . . . . . . . . . . . . . 160 8.4.3 Compilation to Assembly code . . . . . . . . . . . . . . . 163 8.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163 8.6 Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164 8.7 Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . . . . 164 A FbDK: The F(cid:91) Development Kit 165 A.1 Installing the FbDK . . . . . . . . . . . . . . . . . . . . . . . . . 165 A.2 Using Fb and FbSR . . . . . . . . . . . . . . . . . . . . . . . . . . 166 A.2.1 The Toplevel . . . . . . . . . . . . . . . . . . . . . . . . . 166 A.2.2 File-Based Intrepretation . . . . . . . . . . . . . . . . . . 167 A.3 The FbDK Source Code . . . . . . . . . . . . . . . . . . . . . . . 167 A.3.1 $FbDK SRC/src/fbdk.ml. . . . . . . . . . . . . . . . . . . 168 A.3.2 $FbDK SRC/src/application.ml . . . . . . . . . . . . . . 169 A.3.3 $FbDK SRC/src/Fb/fb.ml . . . . . . . . . . . . . . . . . . 171 A.3.4 $FbDK SRC/src/Fb/fbast.ml . . . . . . . . . . . . . . . . 172 A.3.5 $FbDK SRC/src/Fb/fbpp.ml . . . . . . . . . . . . . . . . 172 A.3.6 Scanning and Parsing Concrete Syntax. . . . . . . . . . . 173 A.3.7 Writing an Interpreter . . . . . . . . . . . . . . . . . . . . 173 Bibliography 177 vi CONTENTS Index 179 Preface This book is an introduction to the study of programming languages. The materialhasevolvedfromlecturenotesusedinaprogramminglanguagescourse for juniors, seniors, and graduate students at Johns Hopkins University [22]. The book treats programming language topics from a foundational, but not formal, perspective. It is foundational in that it focuses on core concepts in language design such as functions, records, objects, and types and not directly on applied languages such as C, C++, or Java. We show how the particular core concepts are realized in these modern languages, and so the reader should emerge from this book with a stronger sense of how they are structured. The book is not formal in the sense that no theorems are proved about programminglanguages. Wedo,however,useseveraltechniquesthatareuseful in the formal study of programming languages, including operational semantics and type systems. With these techniques we can define more carefully how programs should behave. The OCaml Language The Caml programming language [16] is used throughout the book, and assign- ments related to the book are best written in Caml. Caml is a modern dialect of ML which has the advantages of being reliable, fast, free, and available on just about any platform through http://caml.inria.fr. This book does not provide an introduction to Caml, and we recommend the following resources for learning the basics: • The OCaml Manual [16], in particular the first two sections of Part I and the first two sections of part IV. • Introduction to OCaml by Jason Hickey [13]. The OCaml manual is complete but terse. Hickey’s book may be your antidote if you want a more descriptive explanation than that provided in the manual. vii viii PREFACE The FbDK ComplementingthebookistheF(cid:91)DevelopmentKit, FbDK.ItisasetofCaml utilities and interpreters for designing and experimenting with the toy F(cid:91) and F(cid:91)SR languages defined in the book. It is available from the book homepage athttp://www.cs.jhu.edu/~scott/pl/book/dist, andisdocumentedinAp- pendix A. Background Needed The book assumes familiarity with the basics of Caml, including the module system (but not the objects, the “O” in OCaml). Beyond that there is no ab- solute prerequisite, but knowledge of C, C++, and Java is helpful because many of the topics in this book are implemented in these languages. The compiler presentedinchapter8producesCcodeasitstarget, andsoaverybasicknowl- edgeofCwillbeneededtoimplementthecompiler. Morenebulously, acertain “mathematical maturity” greatly helps in understanding the concepts, some of which are deep. for this reason, previous study of mathematics, formal logic and other foundational topics in Computer Science such as automata theory, grammars, and algorithms will be a great help. Chapter 1 Introduction General-purpose computers have the amazing property that a single piece of hardware can do any computation imaginable. Before general-purpose comput- ers existed, there were special-purpose computers for arithmetic calculations, which had to be manually reconfigured to carry out different calculations. A general-purpose computer, on the other hand, has the configuration informa- tionforthecalculationinthecomputermemoryitself,intheformofaprogram. The designers realized that if they equipped the computer with the program instructions to access an arbitrary memory location, instructions to branch to a differentpart ofthe programbased ona condition, and theabilityto perform basic arithmetic, then any computation they desired to perform was possible within the limits of how much memory, and patience waiting for the result, they had. These initial computer programs were in machine language, a sequence of bit patterns. Humans understood this language as assembly language, a tex- tual version of the bit patterns. So, these machine languages were the first programming languages, and went hand-in-hand with general-purpose comput- ers. So, programming languages are a fundamental aspect of general-purpose computing, in contrast with e.g., networks, operating systems, and databases. 1.1 The Pre-History of Programming Languages The concept of general-purpose programming in fact predates the development of computers. In the field of mathematical logic in the early 20th century, lo- gicians created their own programming languages. Their motivation originally sprang from the concept of a proof system, a set of rules in which logical truths could be derived, mechanically. Since proof rules can be applied mechanically, allofthelogicallytruefactscanbemechanicallyenumeratedbyapersonsitting there applying all of the rules in every order possible. This means the set of provable truths are recursively enumerable. Logicians including Frege, Church, and Curry wanted to create a more general theory of logic and proof; this led 1 2 CHAPTER 1. INTRODUCTION Churchtodefinetheλ-calculusin1932,anabstractlanguageoffunctionswhich also defined a logic. The logic turned out to be inconsistent, but by then logi- cians had discovered that the idea of a theory of functions and their (abstract) computations was itself of interest. They found that some interesting logical properties (such as the collection of all truths in certain logical systems) were in fact not recursively enumerable, meaning no computer program could ever enumerate them all. So, the notion of general-purpose computation was first exploredintheabstractbylogicians,andonlylaterbycomputerdesigners. The λ-calculus is in fact a general-purpose programming language, and the concept of higher-order functions, introduced in the Lisp programming language in the 1960’s, was derived from the higher-order functions found in the λ-calculus. 1.2 A Brief Early History of Languages There is a rich history of programming languages that is well worth reading about; here we provide a terse overview. The original computer programming languages, as mentioned above, were so-called machine languages: the human and computer programmed in same language. Machinelanguageisgreatforcomputersbutnotsogreatforhumans since the instructions are each very simple and so many, many instructions are required. High-level languages were introduced for ease of programmability by humans. FORTRAN was the first high-level language, developed in 1957 by a team led by Backus at IBM. FORTRAN programs were translated (compiled) into machine language to be executed. They didn’t run as fast as hand-coded machinelanguageprograms,butFORTRANnonethelesscaughtonveryquickly becauseFORTRANprogrammersweremuchmoreproductive. Aswarmofearly languagesfollowed: ALGOLin’58, Lispintheearly60’s, PL/1inthelate60’s, and BASIC in 1966. Languages have developed on many fronts, but there is arguably a major thread of evolution of languages in the following tiers: 1. Machine language: program directly in the language of the computer 2. FORTRAN, BASIC, C, Pascal, ...: first-order functions, nested control structures, arrays. 3. Lisp, Scheme, ML: higher-order functions, automated garbage collection, memory safety; strong typing in ML Object-oriented language development paralleled this hierarchy. 1. (There was never an object-oriented machine language) 2. Simula67wastheoriginalobject-orientedlanguage,createdforsimulation. ItwasFORTAN-likeotherwise. C++isanotherfirst-orderobject-oriented language.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.