Advanced Programming Languages (שיטות תכנות מתקדמות)
Table of Contents
- 1 Staff
- 2 Topics
- 3 Languages used in the course
- 4 Code for the APL course
- 5 Literature
- 6 Outline of lectures
- 6.1 Lecture 1
- 6.2 Lecture 2
- 6.3 Lecture 3
- 6.4 Lecture 4
- 6.5 Lecture 5
- 6.6 Lecture 6
- 6.7 Lecture 7
- 6.8 Lecture 8
- 6.9 – class cancelled
- 6.10 – class cancelled
- 6.11 Lecture 9
- 6.12 Lecture 10
- 6.13 Lecture 11
- 6.14 Lecture 12
- 6.15 Lecture 13
- 6.16 Lecture 14
- 6.17 Lecture 15
- 6.18 Lecture 16
- 6.19 Lecture 17
- 6.20 Lecture 18
- 6.21 – class cancelled
- 6.22 Lecture 19
- 7 Coursework
- 8 How your grade is computed
1 Staff
| Position | Name | Office | Office Hours |
|---|---|---|---|
| Lecturer | Mayer Goldberg | 37:106 | Tuesdays, 2-4pm |
| TA | Valery Frolov | 58:-105 | Sundays, 2-4pm |
2 Topics
- The Lambda Calculus
- Functional Programming
- Object Oriented Programming
- Threaded Code
3 Languages used in the course
3.1 Scheme
Scheme is the main programming language for the first part of the course. It is a semi-functional, late-binding, dynamically-typed, interactive programming language. It is the most suitable language for working with the untyped lambda calculus. For this course, I'm going to ask you to work with Petite Chez Scheme, which is avaiable for free on most common platforms. You are welcome to try out other Scheme systems (there are even a couple for the iPhone! ☺ ), but for the purpose of the assignments, please make sure your code runs on Chez. Click here to go to my Scheme page.
3.2 ML
ML is the second language we shall be using the the first part of the course. It is a semi-functional, early-binding, statically-typed interactive programming language. We shall be using it in our discussions on types, and perhaps in other places as well. Click here to go to my ML page.
3.3 Smalltalk
3.4 Forth
3.5 Erlang
4 Code for the APL course
- Code for mapping over cross products. (2011-05-11)
- Code demonstrating the Duff Device. (2011-05-10)
- Threaded code embedded in Scheme. (2011-05-10)
- Examples of monads used for printing, pseudo-random number generators, gensym, in Scheme (2011-05-03)
- Lots of examples of converting Scheme functions from direct style, through CPS, representation-independent CPS, register machines, and stack machines (2011-04-08)
- The input stream package (sources, examples) (2011-04-08)
- A super-fast implementation of the Fibonacci function (sources) (2011-04-08)
- Code demonstrating that CPS is not, in itself, does not improve performance (sources) (2011-04-08)
- Fixed-point combinators in the SML (sources) (2011-04-08)
- Elementary Church numeral arithmetic in Scheme (source, examples) (2011-02-22)
- A parser for the lambda-calculus, in SML/NJ (source, instructions)
- A toy interpreter for Scheme (in SML)
- Anonymous functions in ML can be recursive
- APL-like iota function in SML
- Creating random objects in SML
- Differentiable functions in SML
- Differentiating Expressions in SML
- Generating all binary patterns for \(n\) bits in SML
- Interlisp-like version of map (mapcar) in SML
- Merge Sort in SML
- Multiple fixed-point combinators in SML
- Quicksort in SML
- Reading the contents of a file into a string in SML
- Scanning, reading, printing, and equality for Scheme sexprs in SML
5 Literature
- Books on Scheme
- Books on ML
- Books on Erlang
- The Lambda Calculus: Its Syntax and Semantics
- Threaded Interpretive Languages: Their Design and Implementation
- To Mock a Mockingbird
- My tutorial on the lambda-calculus
- My tutorial on monads
6 Outline of lectures
6.1 <2011-02-21 Mon> Lecture 1
- Introduction to functional programming
- The syntax of the \(\lambda\)-calculus
- \(\alpha\)-equivalence, \(\beta\)-reduction, \(\eta\)-reduction
- The notion of confluence, and the first and second Church-Rosser theorems
- Introduction to \(\lambda\)-definability
- Church numerals
- Successor on Church numerals
- Multiplication on Church numerals
- Exponentiation on Church numerals
- Super-exponentiations, and the Ackermann-Péter function
6.2 <2011-02-23 Wed> Lecture 2
- Continuing \(\lambda\)-definability
- Review of Church numerals and the functions we defined on them
- The Ackermann-Péter function, and why it is second-order primitive recursive
- Boolean values, Boolean functions
- The Zero-predicate & related functions
- Pairs, triples, and the projections on them
- Factorial, Fibonacci, and the predecessor functions
- The monus function
- <=?, >=?, <?, >?, <>?, =?
6.3 <2011-02-28 Mon> Lecture 3
- Closing notes on \(\lambda\)-definability
- Defining alternate numeral systems
- Adding reflective capabilities to our representation of ordered \(n\)-tuples
- More on the syntax of the \(\lambda\)-calculus
- Free variables, bound variables
- Combinators
- Introduction to fixed points (part I)
- The motivation: Difficulty to define recursive functions
- Combinators
- Global names as macros in the meta-language of the \(\lambda\)-calculus
- The inappropriateness of the substitution model in recursive definitions
- Fixed points over real numbers
- Programs as fixed points
- The notion of leastness and least fixed-points in programming languages
- Recursive functions as solutions to fixed-point equations
- An in-place derivation of a fixed-point combinator
- \(\mathrm{Factorial} \rightarrow F \rightarrow G \rightarrow H\)
- \(G\) in the C programming language
- The
(void *)type as a hack to facilitate recursive types in C
- The
- The recursive function as a fixed point, the fixed point as \((H H)\)
- Defining \(H\) in terms of \(F\)
- Composition
- Abstracting \(F\): Curry's fixed-point combinator
- Applicative order vs normal order
- Monadic, dyadic, triadic, and variadic eta-expansion (\(\eta^{-1}\)) of \((x x)\).
- Simple uses of fixed-point combinators
- Defining recursive functions
- Defining circular structures
- The motivation: Difficulty to define recursive functions
6.4 <2011-03-02 Wed> Lecture 4
- Introduction to fixed points (part II)
- Turing's fixed-point combinator
- The infinitude (אין-סופיות) of fixed-point combinators
- More on relations
- The one-step relation \(\rightarrow_{R}\)
- The reflexive-transitive closure of the one-step: the multi-step relation \(\rightarrow\hskip-1.5em\rightarrow_{R}\)
- The the reflexive-symmetric-transitive closure of the one-step: the equality relation \(=_{R}\)
- A formal definition of the \(\beta\) relation
- A formal definition of the \(\eta\) relation
- A formal definition of the \(\beta\eta\) relation
- The finitude of \(\beta\eta\)-equality (equality on \(\lambda\)-terms)
- The recursive enumerability of fixed-point combinators
- Böhm-trees, equality-by-extension
- Non-standard fixed-point combinators & observational equivalence
- "Happy Birthday" fixed-point combinators
6.5 <2011-03-07 Mon> Lecture 5
- Introduction to fixed points (part III)
- Multiple fixed-point combinators
- Curry's construction
- Turing's construction
- Smullyan's construction
- A construction based on \(n\)-tuples
- The infinitude of multiple fixed-point combinators
- Re-visiting the construction of circular structures with multiple
loops, the expansion of
letrec, etc.
- Multiple fixed-point combinators
6.6 <2011-03-09 Wed> Lecture 6
- Set-equality vs \(\beta\eta\)-equality
- A set generated by another set
- The closure under application
- Church numerals
- Projections
- Bases
- Definition
- Abstraction algorithm
- The basis \(\mathfrak{B} = \{I, K, B, C, S\}\)
- Proof
- The abstraction algorithm
- The basis \(\mathfrak{B}^{\mathrm{II}} = \{S, K\}\)
- Proof
- The abstraction algorithm
- The basis \(\mathfrak{B}^{\mathrm{III}} = \{I, J, K\}\)
- Proof
- The abstraction algorithm
- One-point bases
- Proof
- The abstraction algorithm
- Proofs by contradiction
- The Schönfinkel meta-game
6.7 <2011-03-14 Mon> Lecture 7
- The \(\lambda{I}\beta\eta\)-calculus
- Motivation: How Church tried to capture the notion of strictness, namely, that \(f(\bot) = \bot\)
- The "natural" basis for the \(\lambda{I}\beta\eta\)-calculus: \(\{I, B, C, S\}\)
- Church's original: \(\{I = \lambda x.x, J = \lambda xyzt.xy(xtz)\}\) basis and how to think in terms of it.
6.8 <2011-03-16 Wed> Lecture 8
- Closure under application
- Online G\"odelization
- 1-point bases for Scheme (\(\lambda\)-calculus with finitely-many constants)
- Solution to the tragic squares problem
6.9 <2011-03-21 Mon> – class cancelled
6.10 <2011-03-23 Wed> – class cancelled
6.11 <2011-03-28 Mon> Lecture 9
- Introduction to type theory
- Finding the simple type of an expression
- The Curry-Howard Isomorphism
- The simply-typed \(\lambda\)-calculus
- The polymorphically-typed \(\lambda\)-calculus
- Ackermann's function has second-order Church numerals
6.12 <2011-03-30 Wed> Lecture 10
- Type theory – Continued
- Circular types
- \(\lambda x.(x x)\) and its type
- Embedding recursive function types using recursive data types
- Self-application in SML/NJ
- Fixed-point combinators in SML/NJ
- Run-time type information & reflection
kar,kdr,cons,
isNull, isPair, isNumber, etc.
6.13 <2011-04-04 Mon> Lecture 11
- Introduction to CPS
- Control in procedural languages
- Control in functional languages vs object-oriented languages
- Continuations and their equivalent in OOPLs
- Continuations & the tail-call optimization
- Different kinds of continuations
- Multiple continuations
- Continuations of several arguments
6.14 <2011-04-06 Wed> Lecture 12
- Representation independence
- Defunctionalization (of the continuation)
- The stack machine transformation
- The continuation tag as a return address
- The
apply-kprocedure as a dispatcher over symbolic addresses - Please have a look at this page for worked-out examples of the steps to transform programs from direct style to stack machines
6.15 <2011-04-11 Mon> Lecture 13
- Examples of the stack machine transformation
- Interleaving two functions
- Implementation of a multi-threaded kernel in Scheme
- The
call/cc(call-with-current-continuation) procedure- Contexts, prompts, \(\lambda^{\!\!{}^{>}}\)
- How to use
call/cc - The escaper procedure: the non-tail-recursive version vs. the tail-recursive version
6.16 <2011-04-13 Wed> Lecture 14
- Examples of CPS (now that the computer is back)
- A defunctionalized version of Ackermann's function, in BASIC -- a language without recursion or local state
- Fast Fibonacci using continuations
- Dwandawa: Computing discrete convolutions with one pass
- Using discrete convolutions to multiply numbers, using Vedic algorithms
- More on
call/cc- Backtracking, computing alternatives, the
one-ofprocedure - How to implement
call/ccin the interpreter
- Backtracking, computing alternatives, the
6.17 <2011-04-27 Wed> Lecture 15
- Introduction to Monads
- Threading a computation with a store
- Packaging value & store
- Sequencing
- Examples
- The store monad
- The IO monad
- The random monad
- The gensym monad
- Introduction to threaded code
- Direct threading
- Indirect threading
- Address interpretation
6.18 <2011-05-02 Mon> Lecture 16
- More on threaded code
- Threaded code & binding time
- Significance for recursive procedures
- TILs with early binding
- TILs with late binding
- The implementation of threads
- Threads vs continuations
- Threaded code & multi-threading
- Threaded code as a compilation technique
- Threaded code & binding time
6.19 <2011-05-04 Wed> Lecture 17
- More on threaded code
- The definition of
WHILEin thethreaded.scmfile
- The definition of
- The Duff Device
- Coroutines in C
- Cannot be used to write a re-entrant recursive procedure
- Back to functional programming
- Mapping functions
- Map in LISP, Scheme
- Map in Interlisp
- Map with index
andmap,ormap- Filter
- Partitions
- Map in LISP, Scheme
- Mapping functions
6.20 <2011-05-11 Wed> Lecture 18
- More on mapping functions in Scheme
- Cross products (think how this is related to problem set #2 (!)
- Streams
- Finite & infinite
- Mapping over streams
- Interesting examples
- Stream of prime numbers
- Streams of regular expressions (problem set #2)
- Computable real numbers implemented as streams (option, contact me if this interests you)
6.21 <2011-05-16 Mon> – class cancelled
6.22 <2011-05-18 Wed> Lecture 19
- Introduction to Erlang (I)
- Physically interacting with the Erlang process
- Sessions & their names
- The Erlang REPL
- The Erlang datatypes
- The Erlang source file
- The module system: importing & exporting functions
- Loading Erlang source files
7 Coursework
7.1 Problem set #1
Construct the expression \(P\) that satisfies the equation below, for Church numerals \(c_k, c_n\), where \(0 \leq k \leq n \in \mathbb{N}\): \begin{eqnarray*} P\ c_n\ c_k & = & \lambda x_1 \cdots x_n f_1 \cdots f_n . f_k(x_1 x_1 \cdots x_n f_1 \cdots f_n) \cdots (x_n x_1 \cdots x_n f_1 \cdots f_n) \end{eqnarray*} Now do notice that \(n, k\) are indecies both on the right-hand side as well as on the left-hand side of the above equation.
The motivation for this exercise is that once you have \(P\), it is straightforward to define a variadic version of Turing's multiple fixed-point combinators.
7.2 Problem set #2
- Create a monad (in Scheme) to model random-access file IO. You
should support the operations:
open/create,close,put-char,get-char,set-file-position,get-file-position. The file could be modelled via a string, but may not use any side effects on that string. This last restriction implies that you may not be able to capture true random-access.
7.3 Problem set #3
- Create a program (in one of Scheme, ML, Erlang, Java, Python), that takes some representation for a regular expression (that defines a set of strings), and returns a stream of all those strings. The set of strings can be empty, finite or infinite. The language for regular expressions should include support for constants, and operations for catenation, disjunction, and the Kleene star. You are welcome to support additional, derived operations.
7.4 Take-home final
Please solve 2 problems out of the following:
- The number of Combinators
Write a procedure that take the number \(n\) and returns the number of combinators of length \(n\). For this problem, the \textit{length} of a combinator is defined as follows:
\begin{eqnarray*}\def\Size#1{\mid #1 \mid} \Size{\nu} & = & 1 \\ \Size{(P Q)} & = & 1 + \Size{P} + \Size{Q} \\ \Size{(\lambda \nu . P)} & = & 1 + \Size{P} \end{eqnarray*}Your code will be tested on large numbers. For example, the number of combinators of length 300 is
473477381975190304152771173386219140737139330572727481574 786077988437776485374930756297967309535888631641055198100 186060372310059690213121901539467263248370729333568234992 881170746462932742300417445775187087736611038605424576071 564968041698609452989548022519111112309976379200605183913 879075157976088494
- Address Interpretation
Implement your own threaded, interactive language (TIL) using address interpretation. The implementation language should be C (not C++). Your system should support basic arithmetic & Boolean operations, conditionals (an
if-then-else-like mechanism), function calls.
- Object-Oriented Programming in C
Use C macros to write your own object-oriented extension to C. You should be able to define classes and methods. You do not need to support inheritance, thought hat would be nice. You should not go outside the C programming language (e.g., use M4 or Scheme for pre-processing).
Demonstrate your object-oriented extension using some non-trivial example.
8 How your grade is computed
The coursework shall consist of 3 brief assignments and a take-home final. Their weights are as follows:
| Problem set #1 | 20% |
| Problem set #2 | 20% |
| Problem set #3 | 20% |
| Take-home final | 40% |
| TOTAL | 100% |