Principles of Programming Languages 2017

Principles of Programming Languages 2017

Ben-Gurion University of the Negev Faculty of Natural Science Department of Computer Science Principles of Programming Languages 2017 Lecture Notes March 10, 2017 Many thanks to Tamar Pinhas, Ami Hauptman, Eran Tomer, Barak Bar-Orion, Azzam Maraee, Yaron Gonen, Ehud Barnea, Rotem Mairon, Igal Khitron, Rani Etinger, Ran Anner, Tal Achimeir, Michael Elhadad, Michael Frank for their great help in preparing these notes and the associated code. Contents Introduction 1 1 Practical Functional Programming 3 1.1 Programming Paradigms . .3 1.1.1 Relation between Programming Paradigms and Programming Languages4 1.1.2 Dimensions of Variability across Programming Paradigms . .4 1.1.3 Key Programming Paradigms . .5 1.2 Functional Programming . .6 1.2.1 Expressions and Values . .6 1.2.2 No Side Effects . .7 1.2.3 Higher Order Functions . .7 1.2.4 Advantages of FP . .8 1.3 Using Javascript to Illustrate FP . .9 1.3.1 From Imperative to Procedural . .9 1.3.2 Structured Programming . 10 1.3.3 Procedural Programming . 11 1.3.4 Testing Requirements . 14 1.3.5 Problems with the Procedural Paradigm . 16 1.3.6 Concurrency . 17 1.3.7 Declarative vs. Procedural . 18 1.3.8 Functional Abstractions . 19 1.3.9 Reasoning about Code . 23 1.4 Summary . 26 1.4.1 Programming Paradigms . 26 1.4.2 Functional Programming . 26 1.4.3 Semantics of Programming Languages . 27 II Introduction What the Course is About This course studies principles underlying the design of programming languages. It has four main objectives: • Learning principles of programming languages: elements of programming languages; abstraction means; formal definition; concrete syntax, abstract syntax, operational semantics; program correctness – type checking and type inference systems. Key con- cepts in describing programming languages will emerge such as substitution, scope, reduction and structural induction. These tools will help us draft first proofs of cor- rectness for programs and program transformations. • Describing program execution by studying evaluators: interpreters, program trans- formers and compilers. • Comparing programming paradigms: Functional programming, Logic programming and Imperative programming. • Learning principles of program design: Abstraction, contracts, modular architecture, testing. The unifying underlying objective is to understand how programming languages work, why they are designed the way they are, and what good programming practices they en- courage. Understanding these principles of programming languages will help us learn new languages, compare existing languages, choose the right language for a given task, and build our own language when needed. The course is a mixture of theory and practice. Theoretical topics are supported by implemented software, and course assignments involve programming. It combines two main threads: • Meta-programming: describing and developing software tools that manipulate pro- grams - parse them, evaluate them, transform them, translate from one language to another; reason about code to prove that it has some desirable properties (related to 1 Contents PPL17 correctness or performance) in a predictable manner. Specifically, we will learn how to develop interpreters for a functional language, and a program transformer which infers types of an untyped program and rewrites it into a typed form. We will also develop an interpreter for a logic programming language. • Good programming practices: encourage good programming practices and un- derstand their importance through examples, and by applying them to develop meta- programming tools. The parsers, interpreters and transformers of programs we will develop will be practical examples of good programming. A tool we will use to understand the structure of programming languages will be to com- pare different languages. Specifically, we will compare Javascript (and some of its variants such as Typescript), Scheme and Prolog. We will also refer to Java but without using it. Course Outline 1. Chapter 1: Practical functional programming in Typescript: (a) programming paradigms (b) elements of functional programming: higher-order functions, anonymous func- tions, closures, currying, lazy evaluation, immutable data, recursion. (c) advantages of functional programming (d) types, type checking, type inference (e) compound data structures, functional processing of JSON (f) typing functions, closures (g) currying, partial evaluation 2. Chapter 2: Operational Semantics and Interpreters 3. Chapter 3: Program Transformations: A Type Inference System 4. Chapter 4: Abstraction over Control: Asynchronous programming, Generators, Lazy Data Structures, Continuations 5. Chapter 5: Logic Programming; Logic Programming Interpreter 2 Chapter 1 Practical Functional Programming In this chapter, we introduce the functional programming paradigm and explain its benefits. We illustrate the recommended practices with the Typescript programming language - which is an extension of Javascript which supports an advanced type system. We introduce the notion of type systems, type checking and type inference on practical examples. We illustrate through examples closures, higher order functions, currying, and recursive programming. 1.1 Programming Paradigms A programming paradigm is a way of programming - that recommends “preferred practices” and discourages or makes impossible “risky practice.” References: • http://cs.lmu.edu/ ray/notes/paradigms/ • Wikipedia on Programming Paradigms There exist multiple different programming paradigms: • Imperative — Control flow is an explicit sequence of commands - mainly defined in contrast to “declarative”. • Declarative — Programs state the result you want, not how to get it - leaves flexibility to the language runtime to achieve the goal in optimized ways (for example, SQL and spreadsheets are declarative programming environments). • Structured — Programs have clean, goto-free, nested control structures - arose in reaction to “goto hell” spaghetti code. • Procedural — Imperative programming organized around hierarchies of nested pro- cedure calls. 3 Chapter 1. Practical Functional Programming PPL17 • Functional — Computation proceeds by (nested) function calls that avoid any global state mutation and through the definition of function composition. • Object-Oriented — Computation is effected by sending messages to objects; objects encapsulate state and exhibit behavior. • Event-Driven — Control flow is determined by asynchronous actions in reaction to events (from humans, sensors or other computations). • Logic (Rule-based) — Programmer specifies a set of facts and rules, and an engine infers the answers to questions. 1.1.1 Relation between Programming Paradigms and Programming Lan- guages Programming Languages, by the way they are designed, make some programming paradigms easy to follow. When this is the case, we say that the language belongs to the paradigm (for example: Scheme is a functional programming language). In other words, program- ming paradigms are a way to classify programming languages (a paradigm is a family of programming languages that have similar properties). A given programming language can support more than one programming paradigm. For example, C++ and Java are multi-paradigm languages, which support the Object-Oriented paradigm, the Procedural paradigm, and in recent versions some aspect of the Functional paradigm. 1.1.2 Dimensions of Variability across Programming Paradigms Paradigms are distinguished by programming practices they encourage and forbid (or make difficult). Historically, new paradigms emerge in reaction to problems faced over time by standard practitioners. The motivating forces and programming practices that are concerned by programming paradigms include: • Control flow: how execution flows within the program (sequence and branches, in concurrent threads, in reactive manner, declarative) • Code Organization: how code is organized into a hiearchy of units (expressions, functions, modules, packages) and how these units are organized. • Performance: how code can be run fast, use less resources (RAM, disk, network), behave better (responsive, scalable) at runtime. • Coupling and Reuse: how easily code can be reused in different contexts 4 Chapter 1. Practical Functional Programming PPL17 • Testing: how easy it is to test and verify that code is correct. • Syntax: how natural, brief, readable is the expression of code given the syntax of the language. Can the syntax of language be extended by the programmer. • Domain: to which application domain is the paradigm best applicable (server-side processing, database, GUI front-end, control system). 1.1.3 Key Programming Paradigms Each paradigm puts emphasis on specific programming techniques which came to help on certain aspects of code quality. For example: • Structured programming: encourages the use of structured control flow tools such as if/elseif/else, while - and to avoid uncontrolled goto. • Procedural programming: encourages the use of small units of codes, called pro- cedures, which encapsulate well-defined commands. Procedures interact through well defined interfaces published by each procedure (the contract of the procedure, includ- ing the signature of which arguments it expects, and which return value it returns), and local variables are used inside each procedure without affecting the state of the program outside the scope of the procedures. Key programming constructs associated to this paradigm are procedures, local vari- ables. The key driving force behind procedural and modular programming was the desire

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    29 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us