book - Programming Languages Application and Interpretation...

Info icon This preview shows pages 1–5. Sign up to view the full content.

Programming Languages: Application and Interpretation Version Second Edition Shriram Krishnamurthi November 16, 2012 1
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Contents 1 Introduction 7 1.1 Our Philosophy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.2 The Structure of This Book . . . . . . . . . . . . . . . . . . . . . . . 7 1.3 The Language of This Book . . . . . . . . . . . . . . . . . . . . . . 7 2 Everything (We Will Say) About Parsing 10 2.1 A Lightweight, Built-In First Half of a Parser . . . . . . . . . . . . . 10 2.2 A Convenient Shortcut . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.3 Types for Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.4 Completing the Parser . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.5 Coda . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3 A First Look at Interpretation 13 3.1 Representing Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . 14 3.2 Writing an Interpreter . . . . . . . . . . . . . . . . . . . . . . . . . . 14 3.3 Did You Notice? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 3.4 Growing the Language . . . . . . . . . . . . . . . . . . . . . . . . . 16 4 A First Taste of Desugaring 16 4.1 Extension: Binary Subtraction . . . . . . . . . . . . . . . . . . . . . 17 4.2 Extension: Unary Negation . . . . . . . . . . . . . . . . . . . . . . . 18 5 Adding Functions to the Language 19 5.1 Defining Data Representations . . . . . . . . . . . . . . . . . . . . . 19 5.2 Growing the Interpreter . . . . . . . . . . . . . . . . . . . . . . . . . 21 5.3 Substitution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 5.4 The Interpreter, Resumed . . . . . . . . . . . . . . . . . . . . . . . . 23 5.5 Oh Wait, There’s More! . . . . . . . . . . . . . . . . . . . . . . . . . 25 6 From Substitution to Environments 25 6.1 Introducing the Environment . . . . . . . . . . . . . . . . . . . . . . 26 6.2 Interpreting with Environments . . . . . . . . . . . . . . . . . . . . . 27 6.3 Deferring Correctly . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 6.4 Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 6.4.1 How Bad Is It? . . . . . . . . . . . . . . . . . . . . . . . . . 30 6.4.2 The Top-Level Scope . . . . . . . . . . . . . . . . . . . . . . 31 6.5 Exposing the Environment . . . . . . . . . . . . . . . . . . . . . . . 31 7 Functions Anywhere 31 7.1 Functions as Expressions and Values . . . . . . . . . . . . . . . . . . 32 7.2 Nested What? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 7.3 Implementing Closures . . . . . . . . . . . . . . . . . . . . . . . . . 37 7.4 Substitution, Again . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 7.5 Sugaring Over Anonymity . . . . . . . . . . . . . . . . . . . . . . . 39 2
Image of page 2
8 Mutation: Structures and Variables 41 8.1 Mutable Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 8.1.1 A Simple Model of Mutable Structures . . . . . . . . . . . . 41 8.1.2 Scaffolding . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 8.1.3 Interaction with Closures . . . . . . . . . . . . . . . . . . . . 43 8.1.4 Understanding the Interpretation of Boxes . . . . . . . . . . . 44 8.1.5 Can the Environment Help? . . . . . . . . . . . . . . . . . . 46 8.1.6 Introducing the Store . . . . . . . . . . . . . . . . . . . . . . 48 8.1.7 Interpreting Boxes . . . . . . . . . . . . . . . . . . . . . . . 49 8.1.8 The Bigger Picture . . . . . . . . . . . . . . . . . . . . . . . 54 8.2 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 8.2.1 Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . 57 8.2.2 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 8.2.3 Interpreting Variables . . . . . . . . . . . . . . . . . . . . . . 58 8.3 The Design of Stateful Language Operations . . . . . . . . . . . . . . 59 8.4 Parameter Passing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 9 Recursion and Cycles: Procedures and Data 62 9.1 Recursive and Cyclic Data . . . . . . . . . . . . . . . . . . . . . . . 62 9.2 Recursive Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 9.3 Premature Observation . . . . . . . . . . . . . . . . . . . . . . . . . 65 9.4 Without Explicit State . . . . . . . . . . . . . . . . . . . . . . . . . . 66 10 Objects 67 10.1 Objects Without Inheritance . . . . . . . . . . . . . . . . . . . . . . 67 10.1.1 Objects in the Core . . . . . . . . . . . . . . . . . . . . . . . 68 10.1.2 Objects by Desugaring . . . . . . . . . . . . . . . . . . . . . 69 10.1.3 Objects as Named Collections . . . . . . . . . . . . . . . . . 69 10.1.4 Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . 70 10.1.5 State . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 10.1.6 Private Members . . . . . . . . . . . . . . . . . . . . . . . . 71 10.1.7 Static Members . . . . . . . . . . . . . . . . . . . . . . . . . 72 10.1.8 Objects with Self-Reference . . . . . . . . . . . . . . . . . . 72 10.1.9 Dynamic Dispatch . . . . . . . . . . . . . . . . . . . . . . . 74 10.2 Member Access Design Space . . . . . . . . . . . . . . . . . . . . . 75 10.3 What (Goes In) Else? . . . . . . . . . . . . . . . . . . . . . . . . . . 75 10.3.1 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 10.3.2 Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 10.3.3 Multiple Inheritance . . . . . . . . . . . . . . . . . . . . . . 78 10.3.4 Super-Duper! . . . . . . . . . . . . . . . . . . . . . . . . . . 79 10.3.5 Mixins and Traits . . . . . . . . . . . . . . . . . . . . . . . . 79 3
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

11 Memory Management 81 11.1 Garbage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 11.2 What is “Correct” Garbage Recovery? . . . . . . . . . . . . . . . . . 81 11.3 Manual Reclamation . . . . . . . . . . . . . . . . . . . . . . . . . . 82 11.3.1 The Cost of Fully-Manual Reclamation . . . . . . . . . . . . 82 11.3.2 Reference Counting . . . . . . . . . . . . . . . . . . . . . . 83 11.4 Automated Reclamation, or Garbage Collection . . . . . . . . . . . . 84 11.4.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 11.4.2 Truth and Provability . . . . . . . . . . . . . . . . . . . . . . 85 11.4.3 Central Assumptions . . . . . . . . . . . . . . . . . . . . . . 85 11.5 Convervative Garbage Collection . . . . . . . . . . . . . . . . . . . . 86 11.6 Precise Garbage Collection . . . . . . . . . . . . . . . . . . . . . . . 87 12 Representation Decisions 87 12.1 Changing Representations . . . . . . . . . . . . . . . . . . . . . . . 87 12.2 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 12.3 Changing Meaning . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 12.4 One More Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 13 Desugaring as a Language Feature 91 13.1 A First Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 13.2 Syntax Transformers as Functions . . . . . . . . . . . . . . . . . . . 93 13.3 Guards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95 13.4 Or: A Simple Macro with Many Features . . . . . . . . . . . . . . . 95 13.4.1 A First Attempt . . . . . . . . . . . . . . . . . . . . . . . . . 95 13.4.2 Guarding Evaluation . . . . . . . . . . . . . . . . . . . . . . 97 13.4.3 Hygiene . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 13.5 Identifier Capture . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 13.6 Influence on Compiler Design . . . . . . . . . . . . . . . . . . . . . 101 13.7 Desugaring in Other Languages . . . . . . . . . . . . . . . . . . . . 101 14 Control Operations 102 14.1 Control on the Web . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 14.1.1
Image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '12
  • VandanaDevi
  • Paradise Lost , Type system, unary negation, Desugaring

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern