Programming Language Principles and Paradigms Release 0.3-Alpha
Total Page:16
File Type:pdf, Size:1020Kb
Programming Language Principles and Paradigms Release 0.3-alpha Amir Kamil Feb 22, 2021 CONTENTS I Foundations1 1 Introduction 3 2 Basic Python 5 2.1 Variables.................................................5 2.2 Basic Data Structures..........................................6 2.3 Compound Statements..........................................8 2.4 Function Definitions...........................................8 2.5 Class Definitions.............................................9 2.6 Modules................................................. 10 2.7 Executing a Module........................................... 10 3 Basic Elements 12 3.1 Levels of Description........................................... 12 3.2 Entities, Objects, and Variables..................................... 14 3.3 L-Values and R-Values.......................................... 14 3.4 Expressions................................................ 15 3.5 Statements................................................ 16 4 Names and Environments 17 4.1 Blocks.................................................. 18 4.2 Name Lookup.............................................. 19 4.3 Nested Inline Blocks........................................... 20 4.4 Scope in Functions............................................ 21 4.5 Static Scope............................................... 22 4.6 Dynamic Scope.............................................. 24 4.7 Point of Declaration or Definition.................................... 25 4.8 Implementation Strategies........................................ 26 5 Control Flow 28 5.1 Expression Sequencing.......................................... 28 5.2 Statement Sequences........................................... 29 5.3 Unstructured Transfer of Control.................................... 30 5.4 Structured Control............................................ 31 5.5 Exceptions................................................ 34 6 Memory Management 36 6.1 Storage Duration Classes......................................... 36 6.2 Value and Reference Semantics..................................... 38 6.3 RAII and Scope-Based Resource Management............................. 41 6.4 Garbage Collection............................................ 43 i 7 Grammars 47 7.1 Regular Expressions........................................... 47 7.2 Context-Free Grammars......................................... 49 7.3 Grammars in Programming Languages................................. 51 II Functional Programming 56 8 Functions 58 8.1 Keyword Arguments........................................... 58 8.2 Default Arguments............................................ 59 8.3 Variadic Functions............................................ 60 8.4 Parameter Passing............................................ 61 8.5 Evaluation of Function Calls....................................... 64 9 Introduction to Scheme 65 9.1 Expressions................................................ 65 9.2 Definitions................................................ 66 9.3 Compound Values............................................ 67 9.4 Symbolic Data.............................................. 69 10 Recursion 71 10.1 Activation Records............................................ 71 10.2 Tail Recursion.............................................. 73 11 Higher-Order Functions 75 11.1 Function Objects............................................. 75 11.2 Functions as Parameters......................................... 77 11.3 Nested Functions............................................. 78 12 Lambda Functions 83 12.1 Scheme.................................................. 83 12.2 Python.................................................. 84 12.3 Java.................................................... 85 12.4 C++.................................................... 85 12.5 Common Patterns............................................ 87 13 Continuations 92 13.1 Restricted Continuations......................................... 92 13.2 First-Class Continuations........................................ 96 III Theory 104 14 Lambda Calculus 106 14.1 Non-Terminating Computation..................................... 109 14.2 Normal-Order Evaluation........................................ 109 14.3 Encoding Data.............................................. 110 14.4 Recursion................................................. 115 14.5 Equivalent Models............................................ 116 15 Operational Semantics 118 15.1 Language................................................. 119 15.2 States and Transitions.......................................... 119 15.3 Expressions................................................ 120 ii 15.4 Statements................................................ 122 15.5 Examples................................................. 124 15.6 Operational Semantics for Lambda Calculus.............................. 124 16 Formal Type Systems 127 16.1 Variables................................................. 128 16.2 Functions................................................. 130 16.3 Subtyping................................................. 131 16.4 Full Typing Rules............................................ 134 IV Data Abstraction 136 17 Functional Data Abstraction 138 17.1 Pairs and Lists.............................................. 138 17.2 Message Passing............................................. 140 17.3 Lists................................................... 140 17.4 Dictionaries................................................ 142 17.5 Dispatch Dictionaries.......................................... 143 18 Object-Oriented Programming 145 18.1 Members................................................. 145 18.2 Access Control.............................................. 146 18.3 Kinds of Methods............................................ 147 18.4 Nested and Local Classes........................................ 149 18.5 Implementation Strategies........................................ 150 19 Inheritance and Polymorphism 153 19.1 Types of Inheritance........................................... 153 19.2 Class Hierarchies............................................. 155 19.3 Method Overriding............................................ 156 19.4 Implementing Dynamic Binding..................................... 160 19.5 Multiple Inheritance........................................... 162 20 Static Analysis 168 20.1 Types................................................... 168 20.2 Control-Flow Analysis.......................................... 171 21 Dynamic Typing 174 22 Generics 176 22.1 Implicit Parametric Polymorphism.................................... 176 22.2 Explicit Parametric Polymorphism.................................... 176 22.3 Duck Typing............................................... 183 23 Modules and Namespaces 185 23.1 Translation Units............................................. 185 23.2 Modules, Packages, and Namespaces.................................. 186 23.3 Linkage.................................................. 189 23.4 Information Hiding............................................ 189 23.5 Initialization............................................... 191 V Declarative Programming 193 24 Logic Programming 195 iii 24.1 Prolog.................................................. 196 24.2 Unification and Search.......................................... 200 24.3 The Cut Operator............................................. 209 24.4 Negation................................................. 210 24.5 Examples................................................. 211 25 Constraints and Dependencies 215 25.1 Constraint Logic Programming..................................... 215 25.2 Make................................................... 219 VI Metaprogramming 222 26 Macros and Code Generation 224 26.1 Scheme Macros............................................. 227 26.2 CPP Macros............................................... 228 26.3 Code Generation............................................. 232 27 Template Metaprogramming 234 27.1 Pairs................................................... 235 27.2 Numerical Computations......................................... 239 27.3 Templates and Function Overloading.................................. 242 27.4 SFINAE................................................. 243 27.5 Ensuring a Substitution Failure..................................... 244 27.6 Variadic Templates............................................ 246 28 Example: Multidimensional Arrays 250 28.1 Points................................................... 250 28.2 Domains................................................. 252 28.3 Arrays.................................................. 254 28.4 Stencil.................................................. 256 28.5 Nested Iteration............................................. 257 VII Concurrent Programming 260 29 Parallel Computing 262 29.1 Parallelism in Python........................................... 263 29.2 The Problem with Shared State..................................... 264 29.3 When No Synchronization is Necessary................................. 265 29.4 Synchronized Data Structures...................................... 266 29.5 Locks................................................... 267 29.6 Barriers.................................................. 268 29.7 Message Passing............................................. 268 29.8 Application Examples.......................................... 269 29.9 Synchronization Pitfalls........................................