Lecture notes for COMP 105 (Programming Languages)

Table of Contents

14 Jan 2015: Introduction to Comp 105

What Programming Languages isn't

What if the course were called "Cooking"?

The same thing for programming languages:

What Programming Languages is

If you're going to enjoy the course,

Why so many languages?

Topic of study: the stuff software is made of

Conclusion: make it easier to write programs that really work

What do the professionals say?

In public: a language should express computations

So, no more uncivilized low-level C, and no more C++ that no mortal being can understand

In the pub: professionals are keenly interested in their code

Your language can make you or break you. - Compiler assignments at Princeton

Optional reading

Cultural enrichment: Paul Graham, especially the "Blub paradox"

What can you get out of Comp 105?

Bonus: preparation for advanced study (This course serves two audiences)

How will it work?

Intellectual tools you need to understand, evaluate, and use languages effectively

Notations of the trade (source of precision, further study)

Learn by doing:

Just as intellectually challenging as COMP 40, but in an entirely different direction.

Great languages begin with great features

Language shapes your thinking

There aren't that many great features, and you will see them over and over

You'll choose features, and therefore languages, to fit your needs

Some are more powerful than others

In Comp 105,

OK, what are the great ideas?

To say more with less (expressive power):

To promote reliability and reuse:

Describing it all precisely:

Course logistics and administration

Waitlist

You must get Norman's book

You won't need the book on ML for about a month

Homework

Homework will be frequent and challenging:

Both individual and pair work:

Arc of the homework looks something like this:

Assignment Difficulty
impcore one star
opsem two stars
scheme three stars
hofs four stars

And it's more or less four-star homeworks from there on out.

Lesson: don't make decisions based on the first couple of homeworks!

The role of lectures

In a 100-level course, you are responsible for your own learning

Recitations

Questions and answers on Piazza

Other policies and procedures on the web

What am I called?

Call me "Kathleen," "Professor Fisher", or "Profesor."

Introducing our common framework

Our common framework

Goal: eliminate superficial differences

Imperative programming with an IMPerative CORE:

Idea of LISP syntax

Parenthesized prefix syntax:

Examples:

(+ 2 2)
(if (isbound? x rho) (lookup rho x) (error 99))

Syntactic structure of Impcore

Two syntactic categories: definitions, expressions (no statements!)

An Impcore program is a sequence of definitions

(define mod (m n) (- m (* n (/ m n))))

Compare

int mod (int m, int n) { 
  return m - n * (m / n); 
}

Impcore variable definition

Example

(val n 99)

Compare

int n = 99;

Also, expressions at top level (definition of it)

Impcore expressions

No statements means expression-oriented:

(if e1 e2 e3)
(while e1 e2)
(set x e)
(begin e1 ... en)
(f e1 ... en)

Each one has a value and may have side effects!

Functions are primitive (+ - * / = < > print)
or defined with (define f ...).

The only type of data is "machine integer" (deliberate oversimplification)

Scoping rules for Impcore

Scopes also called "name spaces"; we will call them "environments" because that's the pointy-headed theory term---and if you want to read some of the exciting papers, pointy-headed theory has to be second nature.

Names known in "environments"

Ways to talk about meanings of names:

Impcore vars in 2 environments: globals, formals

There are no local variables

Functions live in their own environment (not shared with variables)

Environmental abuse

Abuse of separate name spaces:

-> (val f 33)
33
-> (define f (x) (+ x x))
f
-> (f f)
66

Recursion: a review

Ways a recursive function could decompose a natural number n.

  1. Peel back one (Peano numbers):

    n = 0
    n = m + 1,    m is also a natural number
  2. Split into two pieces:

    n = 0
    n = k + (n - k)    0 < k < n   (everything gets smaller)
  3. Sequence of decimal digits (see study problems on digits)

    n = d,               where 0 <= d < 10
    n = 10 * m + d,      where 0 <= d < 10 and m > 0

To do your homework problems, which I recommend starting today, you'll need to invent at least one more.

Abstract Syntax

Programming-languages people are wild about compositionality.

Example of compositionality: syntax (grammar)

Programming languages more orderly than natural language.

Example of non-compositionality

21 Jan 2015: Introduction to Semantics

Announcements

Homework 1 is due tonight at midnight.

Homework 2 will be available starting tomorrow.

See Jerett or Robert if you have questions about grading.

In assignment 1, your function names much match names requested in problem statement exactly or you will get zero credit because of auto-grading.

Operational Semantics

Norman Ramsey and Geoff Mainland put together some Beamer slides explaining operational semantics for Impcore.

26 Jan 2015: Semantics Review and Metatheory

Review operational semantics of function application and work through a simple derivation using the Beamer slides on operational semantics.

Metatheory

We'll use some Beamer slides on Metatheory that Norman Ramsey put together.

Examples of using metatheory

TeX slide

then

TeX slide
Formalizing when an expression contain set
We can just look at the syntax, or we can make a proof system:
TeX slide

and also

TeX slide

Notice that set is the only construct that changes the environment.

28 Jan 2015: Winter Storm Juno

No class because of snow.

2 Feb 2015: Winter Storm Reprise

No class because of snow.

4 Feb 2015: Intro to Scheme

For a new language, five powerful questions

As a lens for understanding, you can ask these questions about any language:

  1. What is the abstract syntax? What are the syntactic categories, and what are the terms in each category?

  2. What are the values? What do expressions/terms evaluate to?

  3. What environments are there? That is, what can names stand for?

  4. How are terms evaluated? What are the judgments? (What are the rules?)

  5. What's in the initial basis? Primitives and otherwise, what is built in?

Introduction to Scheme

Question 2: what are the values?

Two new kinds of data:

Picture of a cons cell: (cons 3 (cons ( 2 '())))

Scheme Values

Values are S-expressions.

An S-expression is a symbol or a literal integer or a literal Boolean or a list of S-expressions.

A list of S-expressions is either - the empty list '() - an S-expression followed by a list of S-expressions

S-Exp operators

Like any other abstract data type, S-Exps have:

N.B. creators + producers = constructors

Examples of S-Expression operators

(cons 'a '())
(cons 'a '(b c))
(null? '(b c))

The symbol ' is pronounced "tick." It indicates that what follows is a literal.

Lists

Subset of S-Expressions.

Can be defined via inference rules:

TeX slide TeX slide

Constructors: '(),cons

Observers: null?, pair?, car, cdr (also known as "first" and "rest", "head" and "tail", and many other names)

Algebraic laws of lists:

(null? '()) == #t
(null? (cons v vs)) == #f
(car (cons v vs)) == v
(cdr (cons v vs)) == vs

Combine creators/producers with observers

Can use laws to prove properties of code and to write better code.

Recursive functions for recursive types

Any list is therefore constructed with nil or with cons.

Example: length

9 Feb 2015: More Snow

11 Feb 2015: More Scheme

Where have we been? (impcore and semantics)

A new way of thinking about recursion

Structure of the input drives the structure of the code.

To discover recursive functions, write algebraic laws:

exp x  0      = 1
exp x (n + 1) = x * (exp x n)

Can you find a direction in which something gets smaller?

Another example:

binary 0 = 0
binary (2n + b) = 10 * binary n + b

Reading these equations, which direction gets smaller? What's the corresponding code?

Lots of new math

No new programming-language ideas

Where are we going? (uScheme)

Recursion and composition:

Lists

Can be defined via inference rules:

TeX slide TeX slide

Why are lists useful?

These "cheap and cheerful" representations are less efficient than balanced search trees, but are very easy to implement and work with---see the book.

The only thing new here is automatic memory management. Everything else you could do in C. (You can have automatic memory management in C as well.)

Algebraic laws to design list functions

Using informal math notation with .. for "followed by" and e for the empty sequence, we have these laws:

xs .. e         = xs
e .. ys         = ys
(z .. zs) .. ys = z .. (zs .. ys)
xs .. (y .. ys) = (xs .. y) .. ys

The underlying operations are append, cons, and snoc. Which ..'s are which?

What is append in Scheme notation? (See slide)

          *** Equations and function for append ***
(append '()         ys) == ys
(append (cons z zs) ys) == (cons z (append zs ys))

(define append (xs ys)
  (if (null? xs) ys
      (cons (car xs) (append (cdr xs) ys))))

Why does it terminate?

Other laws:

(append (list1 x) ys) = ???

The major cost center is cons

Allocation is the big cost.

How many cons cells are allocated?

Claim: Cost (append xs ys) = |xs|

Induction Principle for List(A)

IF

  1. IH ('())

  2. If a in A and IH(as) then IH (cons a as)

THEN

 Forall as in List(A), IH(as)  

Proof by induction on xs.

Base case: xs = '()

 (append '() ys) returns ys with 0 allocated cons cells. 

Induction case: xs = (cons z zs)

 car xs = z  and  cdr xs = zs
 cost (append (cons z zs) ys)) = 
 cost (cons z (append zs ys)) = 
 1 + cost (append zs ys) = 
    By IH, cost (append zs ys) = |zs|
 1 + |zs| =
|xs|

Can also do induction on length of xs.

Conclusion: Cost of append is linear in length of first argument.

List reversal

What about list reversal?

reverse '() = '()
reverse (x .. xs) = reverse xs .. reverse x = reverse xs .. x

And the code?

          *** Naive list reversal ***
(define reverse (xs)
   (if (null? xs) '()
       (append (reverse (cdr xs))
               (list1 (car xs)))))

How many cons cells are allocated?

The method of accumulating parameters

Let's try a new algebraic law:

reverse (x .. xs) .. zs = reverse xs .. x .. zs = reverse xs .. (cons x zs)
reverse '() .. zs       = zs

The code

          *** Reversal by accumulating parameters ***
(define revapp (xs zs)
   (if (null? xs) zs
       (revapp (cdr xs) 
               (cons (car xs) zs))))

(define reverse (xs) (revapp xs '()))

Parameter zs is the accumulating parameter.
(A powerful, general technique.)

Association lists represent finite maps (not to be covered in class)

Implementation: list of key-value pairs

'((k1 v1) (k2 v2) ... (kn vn))

Picture with spine of cons cells, car, cdar, caar, cadar.

          *** A-list example ***
    -> (find 'Building 
             '((Course 105) (Building Anderson) 
               (Instructor Fisher)))
    Anderson
    -> (val ksf (bind 'Office 'Halligan-205
                (bind 'Courses '(105)
                (bind 'Email 'comp105-staff '()))))
    ((Email comp105-staff) 
     (Courses (105)) 
     (Office Halligan-205))
    -> (find 'Office ksf) 
    Halligan-205
    -> (find 'Favorite-food ksf)
    ()

Notes:

Algebraic laws of association lists

          *** Laws of assocation lists ***
(find k (bind k v l)) = v
(find k (bind k' v l)) = (find k l), provided k != k'
(find k '()) =  '() --- bogus!

Handy new feature of Scheme: let binding

          *** Introduce local names into environment ***
(let ((x1 e1)
      ...
      (xn en))
    e)

Evaluate e1 through en, bind answers to x1, ... xn

Also let* (one at a time) and letrec (local recursive functions)

Note that we really have definititions and it might be easier to read if McCarthy had actually used definition syntax, which you'll see in ML, Haskell, and other functional languages:

          *** What McCarthy should have done ***
(let ((val x1 e1)
      ...
      (val xn en))
   e)

18 Feb 2015: Lambda

Announcements

In-class evaluations: Monday, Feburary 23. Be here!

From Impcore to uScheme

Things that should offend you about Impcore:

All these problems have one solution: lambda

Anonymous, first-class functions

From Church's lambda-calculus:

(lambda (x) (+ x x))

"The function that maps x to x plus x"

At top level, like define. (Or more accurately, define is a synonym for lambda.)

In general, \x.E or (lambda (x) E)

The ability to "capture" free variables is what makes it interesting.

First-class, nested functions

(lambda (x) (+ x y))  ; means what??

What matters is that y can be a parameter or a let-bound variable of an enclosing function.

First example: finding roots

          *** Function escapes! ***
-> (define to-the-n-minus-k (n k)
      (let
        ((x-to-the-n-minus-k (lambda (x) 
                                (- (exp x n) k))))
        x-to-the-n-minus-k))
-> (val x-cubed-minus-27 (to-the-n-minus-k 3 27))
-> (x-cubed-minus-27 2)
-19

to-the-n-minus-k is a higher-order function because it returns another (escaping) function as a result.

          *** No need to name the escaping function ***
-> (define to-the-n-minus-k (n k)
      (lambda (x) (- (exp x n) k)))



-> (val x-cubed-minus-27 (to-the-n-minus-k 3 27))
-> (x-cubed-minus-27 2)
-19



          *** Application: a zero-finder ***
(define findzero-between (f lo hi)
   ; binary search
   (if (>= (+ lo 1) hi)
       hi
       (let ((mid (/ (+ lo hi) 2)))
          (if (< (f mid) 0)
              (findzero-between f mid hi)
              (findzero-between f lo mid)))))
(define findzero (f) (findzero-between f 0 100))

findzero-between is also a higher-order function because it takes another function as an argument. But nothing escapes; you can do this in C.

          *** Cube root of 27 and square root of 16 ***
-> (findzero (to-the-n-minus-k 3 27))                                    
3
-> (findzero (to-the-n-minus-k 2 16))
4

Exercises!!

          *** Lambda questions ***
(define combine (p? q?)
   (lambda (x) (if (p? x) (q? x) #f)))

(define divvy (p? q?)
   (lambda (x) (if (p? x) #t (q? x))))

(val c-p-e (combine prime? even?))
(val d-p-o (divvy   prime? odd?))

(c-p-e 9) == ?            (d-p-o 9) == ?
(c-p-e 8) == ?            (d-p-o 8) == ?
(c-p-e 7) == ?            (d-p-o 7) == ?



          *** Wait for it ***
...



          *** Lambda answers ***
(define conjoin (p? q?)
   (lambda (x) (if (p? x) (q? x) #f)))

(define disjoin (p? q?)
   (lambda (x) (if (p? x) #t (q? x))))

(val c-p-e (conjoin prime? even?))
(val d-p-o (disjoin prime? odd?))

(c-p-e 9) == #f           (d-p-o 9) == #t
(c-p-e 8) == #f           (d-p-o 8) == #f
(c-p-e 7) == #f           (d-p-o 7) == #t

Escaping functions

"Escape" means "outlive the activation in which the lambda was evaluated."

Escaping and lifetimes are some of those universal decisions every programmer has to think about.

          *** An ``escaping'' function ***
-> (define to-the-n-minus-k (n k)
      (lambda (x) (- (exp x n) k)))

Where are n and k stored???

C programmers do this explicitly with malloc

In a language with first-class, nested functions, storage of escaping values is part of the semantics of lambda.

Closures represent escaping functions:
TeX slide

          *** What's the picture for conjunction? ***
(define conjoin (p? q?)
   (lambda (x) (if (p? x) (q? x) #f)))

Closure for conjunction:
TeX slide

Higher-order functions!

Preview: in math, what is the following equal to?

(f o g)(x) == ???

Another algebraic law, another function:

          *** Functions create new functions ***
-> (define o (f g) (lambda (x) (f (g x))))
-> (define even? (n) (= 0 (mod n 2)))
-> (val odd? (o not even?))
-> (odd? 3)
-> (odd? 4)

Don't forget `(o not null?)'

          *** Classic functional technique: Currying ***
-> (val positive? (lambda (y) (< 0 y)))
-> (positive? 3)
-> (val <-curried (lambda (x) (lambda (y) (< x y))))
-> (val positive? (<-curried 0)) 
                          ; "partial application"
-> (positive? 0)

No need to Curry by hand!:
TeX slide

No need to Curry by hand!:
TeX slide

What's the algebraic law for curry?

 ...   (curry f) ...    =  ... f ...

Keeping in mind all you can do with a function is apply it?

Bonus content: Lambda as an abstraction barrier

          *** Bonus content: vulnerable variables? ***
-> (val seed 1)
-> (val rand (lambda ()
      (set seed (mod (+ (* seed 9) 5) 1024)))))
-> (rand)
14
-> (rand)
131
-> (set seed 1)
1
-> (rand)
14


          *** Bonus: Lambda as abstraction barrier! ***
-> (val mk-rand (lambda (seed)
     (lambda ()
       (set seed (mod (+ (* seed 9) 5) 1024))))))
-> (val rand (mk-rand 1))
-> (rand)
14
-> (rand)
131
-> (set seed 1)
error: set unbound variable seed
-> (rand)
160

In-class exercise

          *** Exercises ***
-> (map     ((curry +) 3) '(1 2 3 4 5))
???
-> (exists? ((curry =) 3) '(1 2 3 4 5))
???
-> (filter  ((curry >) 3) '(1 2 3 4 5))
???                        ; tricky

Wait for it:
TeX slide

          *** Answers ***
-> (map     ((curry +) 3) '(1 2 3 4 5))
(4 5 6 7 8)
-> (exists? ((curry =) 3) '(1 2 3 4 5))
-> (filter  ((curry >) 3) '(1 2 3 4 5)) 
(1 2)

Truth about S-expressions and functions consuming functions

Q: Can you do case analysis on a function?

A: No!

Q: So what can you do then?

A: Apply it!

Proving properties about functions

Recursive function consuming A is related to proof about A

19 February 2015: Higher-order functions

Goal: start with functions on elements, wind up with functions on lists

Capture common patterns of computation or algorithms

Folds also called reduce, accum, "catamorphism"

List search: exists?

Algorithm encapsulated: linear search

Algebraic law on the board:

(exists? p? '())          == ???
(exixts? p? '(cons a as)) == ???

          *** Defining exists? ***
-> (define exists? (p? xs)
      (if (null? xs)
          (if (p? (car xs)) 
              (exists? p? (cdr xs)))))
-> (exists? pair? '(1 2 3))
-> (exists? pair? '(1 2 (3)))
-> (exists? ((curry =) 0) '(1 2 3))
-> (exists? ((curry =) 0) '(0 1 2 3))

Filter:
TeX slide

List selection: filter

Problem: Give me a list of numbers; return only the even elements.

What are the laws?

(filter even? '())          == ???
(filter even? '(cons m ms)) == ???


          *** Defining filter ***
-> (define filter (p? xs)
     (if (null? xs)
       '()
       (if (p? (car xs))
         (cons (car xs) (filter p? (cdr xs)))
         (filter p? (cdr xs)))))
-> (filter (lambda (n) (>  n 0)) '(1 2 -3 -4 5 6))
(1 2 5 6)
-> (filter (lambda (n) (<= n 0)) '(1 2 -3 -4 5 6))
(-3 -4)
-> (filter ((curry <)  0) '(1 2 -3 -4 5 6))
(1 2 5 6)
-> (filter ((curry >=) 0) '(1 2 -3 -4 5 6))
(-3 -4)








          *** List filtering: composition revisited ***
-> (val positive? ((curry <) 0))
<procedure>
-> (filter positive?         '(1 2 -3 -4 5 6))
(1 2 5 6)
-> (filter (o not positive?) '(1 2 -3 -4 5 6))
(-3 -4)

Map:
TeX slide

"Lifting" functions to lists: map

Algorithm encapsulated: do this to every element

Problem: square every element of a list.

What are the laws?

(map square '())         ==
(map square (cons n ns)) ==

          *** Defining map ***
-> (define map (f xs)
     (if (null? xs)
       '()
       (cons (f (car xs)) (map f (cdr xs)))))
-> (map number? '(3 a b (5 6)))
(#t #f #f #f)
-> (map ((curry *) 100) '(5 6 7))
(500 600 700)
-> (val square* ((curry map) (lambda (n) (* n n))))
<procedure>
-> (square* '(1 2 3 4 5))
(1 4 9 16 25)

Foldr:
TeX slide

The universal list function: fold

Algebraic laws for foldr:
TeX slide

Code for foldr:
TeX slide

Another view of operator folding:
TeX slide

In-class exercise

Exercise:
TeX slide

Wait for it:
TeX slide

Answer:
TeX slide

Tail calls

A call in tail position.

What is tail position?

Tail position is defined inductively:

Idea: The last thing that happens

Anything in tail position is the last thing executed!

Key idea is tail-call optimization!

Tail-call optimization:
TeX slide

Example of tail position:
TeX slide

Example of tail position:
TeX slide

Question: how much stack space is used by the call?

Another example of tail position:
TeX slide

Another example of tail position:
TeX slide

Question: how much stack space is used by the call?

Question:
TeX slide

Continuations

Direct style - last action is to return a value

Continuation-passing style - last action is to "throw" value to a continuation

Simulate with a tail call.

How functions finish:
TeX slide

Motivating example: from existence to witness

Problem in interface design:
TeX slide

Ideas?

Bad choices:

Good choice:

Solution: new interface:
TeX slide

          *** Coding \lit{witness} with continuations ***
(define witness-cps (p? xs succ fail)
   (if (null? xs)
       (fail)
       (let ((x (car xs)))
         (if (p? x)
             (succ x)
             (witness-cps p? (cdr xs) succ fail)))))

Tail calls: continuations, recursion:
TeX slide

Question: how much stack space is used by the call?

Example use: instructor lookup:
TeX slide

          *** Simple continuations at work ***
-> (val 2015s '((Fisher 105)(Hescott 170)(Chow 116)))
-> (instructor-info 'Fisher 2015s)
(Fisher teaches 105)
-> (instructor-info 'Chow 2015s)
(Chow teaches 116)
-> (instructor-info 'Souvaine 2015s)
(Souvaine is-not-on-the-list)

23 February 2015: Continuations, Semantics, and Real-World Scheme

Plan for today:

  1. Explore continuations for search

  2. Analyze uScheme from the semantic point of view

  3. Midterm evaluations

Working Example

          *** Exercise: Show a satisfying assignment if one exists ***
(val f1 '(and x y z w p q (not x)))

(val f2 '(not (or x y)))

(val f3 '(not (and x y z)))

(val f4 '(and (or x y z) 
              (or (not x) (not y) (not z))))

Wait for it ...:
TeX slide

          *** Satisfying assignments ***
(val f1 '(and x y z w p q (not x))) ; NONE

(val f2 '(not (or x y))) 
                  ; { x |-> #f, y |-> #f }

(val f3 '(not (and x y z))) 
                  ; { x |-> #f, ... }
(val f4 '(and (or x y z) 
              (or (not x) (not y) (not z))))
              ; { x |-> #f, y |-> #t, ... }

New syntax, new environment, new semantics

First three of five questions: Syntax, values, environments

Key changes from Impcore

{uscheme vs impcore}:
TeX slide

{Evaluation judgment}:
TeX slide

{Evaluation rules}:
TeX slide

{Implementation of closures}:
TeX slide

{Applying closures}:
TeX slide

{Locations in closures}:
TeX slide

Closure optimizations

25 Feb 2015: Scheme Wrap-up; Introduction to ML

Lisp and Scheme retrospective

{uscheme and the Five Questions}:
TeX slide

Common Lisp, Scheme:

Down sides:

Bottom line: it's all about lambda

Bonus content: Scheme as it really is

  1. Macros!
  2. Cond expressions (solve nesting problem)
  3. Mutation
  4. ...

Macros!

A Scheme program is just another S-expression

Conditional expressions

          *** Real Scheme Conditionals ***
(cond (c1 e1)    ; if c1 then e1
      (c2 e2)    ; else if c2 then e2
       ...            ...
      (cn en))   ; else if cn then en

; Syntactic sugar---'if' is a macro:
(if e1 e2 e3) == (cond (e1 e2)
                       (#t e3))

Macros

Real Scheme: macros

A Scheme program is an S-expression

Mutation

Real Scheme: mutation

Not only variables can be mutated

Mutate heap-allocated cons cell:

(set-car! '(a b c) 'd)  => (d b c)

Circular lists, sharing, avoids allocation

Introduction to ML

Apply your new knowledge in Standard ML:

Much less intellectual effort

Lectures on ML:

  1. Algebraic types and pattern matching
  2. A touch of types
  3. Everything else

Meta: Not your typical introduction to a new language

ML Overview

Designed for programs, logic, symbolic data

Theme: talking about data

Board: ML = uScheme + pattern matching + exceptions + static types

Three new ideas:

  1. Pattern matching is big and important. You will like it.
  2. Exceptions are easy
  3. Static types get two to three weeks in their own right.

And lots of new concrete syntax!

Board: Let's do some examples

ML---The Five Questions

Syntax: expressions, definitions, patterns, types

Values: num/string/bool, record/tuple, algebraic data

Environments: names stand for values (and types)

Evaluation: uScheme + case and pattern matching

Initial Basis: medium size; emphasizes lists

(Question Six: type system---a coming attraction)

A note about books

Ullman is easy to digest

Ullman is clueless about good style

Suggestion:

Algebraic data types

Tidbits:

Defining algebraic types

Board:

Exegesis (on board):

2 Mar 2015: Algebraic Data Types; Case Statements; Exceptions

pdf slides

Algebraic Datatypes

Enumerated types

Datatypes can define an enumerated type and associated values.

datatype suit = heart | diamond | spade | club

Here suit is the name of a new type.

The data constructors heart, dimaond, spade, and club are the values of type suit.

Data constructors are separated by vertical bars.

Pattern matching

Datatypes are deconstructed using pattern matching.

fun toString heart = "heart"
  | toString diamond = "diamond"
  | toString spade = "spade"
  | toString club = "club"

val suitName = toString heart

But wait, there's more: Data constructors can take arguments!

datatype IntTree = Leaf | Node of int * IntTree * IntTree

IntTree is the name of a new type.

There are two data constructors: Leaf and Node.

Nodes take a tuple of three arguments: a value at the node, and left and right subtrees.

The keyword of separates the name of the data constructor and the type of its argument.

When fully applied, data constructors have the type of the defining datatype (ie, IntTree).

Building values with constructors

We build values of type IntTree using the associated constructors: (Draw on board)

 val tempty = Leaf
 val t1 = Node (1, tempty, tempty)
 val t2 = Node (2, t1, t1)
 val t3 = Node (3, t2, t2)

Deconstruct values with pattern matching

(The @ symbol denotes append in ML)

fun inOrder Leaf = []
  | inOrder (Node (v, left, right)) = 
       (inOrder left) @ [v] @ (inOrder right)

val il3 = inOrder t3

fun preOrder Leaf = []
  | preOrder (Node (v, left, right)) = 
       v :: (preOrder left) @ (preOrder right)

val pl3 = inOrder t3

IntTree is monomorphic because it has a single type.

But wait, there's still more: Polymorphic datatypes!

Polymorphic datatypes are written using type variables that can be instantiated with any type.

datatype 'a tree = Child | Parent of 'a * 'a tree * 'a tree

tree is a type constructor (written in post-fix notation), which means it produces a type when applied to a type argument.

Examples:

'a is a type variable: it can represent any type.

It is introduced on the left-hand of the = sign. References on the right-hand side are types.

Child and Parent are data constructors.

Child takes no arguments, and so has type 'a tree

When given a value of type 'a and two 'a trees, Parent produces a 'a tree

Constructors build tree values

val empty = Child
val tint1 = Parent (1, empty, empty)
val tint2 = Parent (2, tint1, tint1)
val tint3 = Parent (3, tint2, tint2)

val tstr1 = Parent ("a", empty, empty)
val tstr2 = Parent ("b", tstr1, tstr1)
val tstr3 = Parent ("c", tstr2, tstr2)

Pattern matching deconstructs tree values

fun inOrder Child = []
  | inOrder (Parent (v, left, right)) = 
       (inOrder left) @ [v] @ (inOrder right)

fun preOrder Child = []
  | preOrder (Parent (v, left, right)) = 
       v :: (preOrder left) @ (preOrder right)

Functions inOrder and preOrder are polymorphic: they work on any value of type 'a tree. 'a is a type variable and can be replaced with any type.

Environments

Datatype declarations introduce names into:

  1. the type environment: suit, IntTree, tree

  2. the value environment: heart, Leaf, Parent

Inductive

Datatype declarations are inherently inductive:

Datatype Exercise

TeX slide

Wait for it ...

          *** Exercise answers ***
datatype sx1 = ATOM1 of atom
             | LIST1 of sx1 list

datatype sx2 = ATOM2 of atom
             | PAIR2 of sx2 * sx2

Additional language support for algebraic types: case expressions

Eliminate values of algebraic types

New language construct case (an expression)

fun length xs =
  case xs
    of []      => 0
     | (x::xs) => 1 + length xs

At top level, fun better than case

When possible, write

fun length []      = 0
  | length (x::xs) = 1 + length xs

case works for any datatype

 fun toStr t = 
     case t 
       of Leaf => "Leaf"
        | Node(v,left,right) => "Node"

But often pattern matching is better style:

 fun toStr' Leaf = "Leaf"
   | toStr' (Node (v,left,right)) = "Node"

Talking type theory: Introduction and elimination constructs

Part of learning any new field: talk to people in their native vocabulary

It's like knowing what to say when somebody sneezes.

Types and their uses:
Type Produce Consume
Introduce Eliminate
arrow (function) Function definition or Lambda (fn) Application
algebraic datatype Apply constructor Case or Pattern match
tuple (e1, ..., en) Case or Pattern match!

Example pattern matches on a tuple:

val (x,y) = (1,2)

val (left, pivot, right) = split xs

Exceptions: Handling unusual circumstances

Syntax:

Informal Semantics:

Bonus Content: ML traps and pitfalls

ML Traps and pitfalls:
TeX slide

          *** Order of clauses matters ***

fun take n (x::xs) = x :: take (n-1) xs
  | take 0 xs      = []
  | take n []      = []

(* what goes wrong? *)


          *** Gotcha --- overloading ***
- fun plus x y = x + y;
> val plus = fn : int -> int -> int
- fun plus x y = y + y : real;
> val plus = fn : real -> real -> real

Gotcha --- equality types:
TeX slide

Gotcha --- parentheses

Put parentheses around anything with |

case, handle, fn

Function application has higher precedence than any infix operator

Bonus content (seen in examples)

Syntactic sugar for lists

          *** Syntactic sugar for lists ***
- 1 :: 2 :: 3 :: 4 :: nil; (* :: associates to the right *)
> val it = [1, 2, 3, 4] : int list

- "the" :: "ML" :: "follies" :: [];
> val it = ["the", "ML", "follies"] : string list

> concat it;
val it = "theMLfollies" : string

Bonus content: ML from 10,000 feet

ML from 10,000 feet:
TeX slide

Environments

The value environment

Names bound to immutable values

Immutable ref and array values point to mutable locations

ML has no binding-changing assignment

Definitions add new bindings (hide old ones):

val pattern = exp
val rec pattern = exp
fun ident patterns = exp
datatype ... = ...

Nesting environments

At top level, definitions

Definitions contain expressions:

def ::= val pattern = exp

Expressions contain definitions:

exp ::= let defs in exp end

Sequence of defs has let-star semantics

Patterns

What is a pattern?

pattern ::= variable
          | wildcard
          | value-constructor [pattern]
          | tuple-pattern
          | record-pattern
          | integer-literal
          | list-pattern

Design bug: no lexical distinction between

Workaround: programming convention

Functions

Function pecularities: 1 argument

Each function takes 1 argument, returns 1 result

For "multiple arguments," use tuples!

 fun factorial n =
   let fun f (i, prod) = 
         if i > n then prod else f (i+1, i*prod)
   in  f (1, 1)
   end

 fun factorial n =  (* you can also Curry *)
   let fun f i prod = 
         if i > n then prod else f (i+1) (i*prod)
   in  f 1 1
   end

Tuples are "usual and customary."

Mutual recursion:
TeX slide

Types

Syntax of ML types:
TeX slide

Syntax of ML types:
TeX slide

Polymorphic types:
TeX slide

Old and new friends:
TeX slide

4 Mar 2015: Types

pdf slides

Announcements

The midterm exam:

Plan for today

Type systems

What kind of value do we have?

 n + 1

 "hello" ^ "world"

 (fn n => n * (n - 1))

 if p then 1 else 0

Questions type systems can answer:

Questions type systems generally cannot answer:

Static vs. Dynamic Checking

Most languages use a combination of static and dynamic checks

Static:

Dynamic:

What is a type?

Source of new language ideas for next 20 years

Needed if you want to understand advanced designs (or create your own)

Type system and checker for a simple language

Q: What context do we need to evaluate an expression?

Q: Do we need all the same context to decide on a type?

Q: What do we need then?

Define a datatype for expressions with

Language of expressions

Numbers and Booleans:

datatype exp = ARITH of arithop * exp * exp
             | CMP   of relop   * exp * exp
             | LIT   of int
             | IF    of exp     * exp * exp
and      arithop = PLUS | MINUS | TIMES | ...
and      relop   = EQ | NE | LT | LE | GT | GE

datatype ty = INTTY | BOOLTY

Examples to rule out

Can't add an integer and a boolean:

3 + (3 < 99)

(ARITH(PLUS, LIT 3, CMP (LT, LIT 3, LIT 99)))

Can't compare an integer and a boolean

(3 < (4 = 24))

CMP (LT, LIT 3, CMP(EQ (LIT 4, LIT 24)))

Inference rules to define a type system

Rule for arithmetic operators

Informal example:

|- 3 : int    |- 5 : int
------------------------------------------------------------
|- 3 + 5 : int

General form:

|- e1 : int    |- e2 : int
------------------------------------------------------------
|- ARITH ( _ , e1, e2) : int

Rule for comparisons

Informal example:

|- 7 : int    |- 10 : int
------------------------------------------------------------
|- 7 < 10 : bool

General form:

|- e1 : int    |- e2 : int
------------------------------------------------------------
|- CMP ( _ , e1, e2) : bool

Rule for literals

Informal example:

|- 14 : int

General form:

-----------------------------------
|- LIT (n) : int

Rule for conditionals:

Informal example:

|- true : bool    
|- 3    : int
|- 42   : int      
------------------------------------------------------------
|- IF (true, 3, 42) : int

General form:

|- e : bool    
|- e1 : tau1   
|- e2 : tau2      tau1 equiv tau2
------------------------------------------------------------
|- IF ( e, e1, e2) : tau1

Experience shows it is better to test two types for equivalence than to write rule with same type appearing twice.

Typing rules let us read off what a type checker needs to do.

Type checker

val tc : exp -> ty
exception IllTyped
fun tc (ARITH (_, e1, e2)) = 
       case (tc e1, tc e2) 
       of (INTTY, INTTY) => INTTY
        | _              => raise IllTyped
  | tc (CMP (_, e1, e2)) = 
       case (tc e1, tc e2) 
       of (INTTY, INTTY) => BOOLTY
        | _              => raise IllTyped
  | tc (LIT _) = INTTY
  | tc (IF (e,e1,e2)) = 
       case (tc e, tc e1, tc e2) 
       of (BOOLTY, tau1, tau2) => 
           if eqType(tau1, tau2) 
           then tau1 else raise IllTyped
        | _                    => raise IllTyped

An implementor's trick: If you see identical types in a rule,

Review

This is a big chunk of what language designers do.

9 Mar 2015: Finishing Types Introduction;Midterm Review

pdf slides

Announcements

Today

Typing Rules: Contexts and Term Variables

Add variables and let binding to our language, what happens?

Extended language of expressions

Numbers and Booleans:

datatype exp = ARITH of arithop * exp * exp
             | CMP   of relop   * exp * exp
             | LIT   of int
             | IF    of exp     * exp * exp
             | VAR   of name
             | LET   of name    * exp * exp
and      arithop = PLUS | MINUS | TIMES | ...
and      relop   = EQ | NE | LT | LE | GT | GE

datatype ty = INTTY | BOOLTY

What could go wrong with a variable?

Key idea: Type environment (Gamma) tracks the types of variables.

Rule for var

x in domain Gamma        tau = Gamma(x) 
------------------------------------------------------------
Gamma |- VAR x : tau

Rule for let

Gamma         |- e  : tau
Gamma{x->tau} |- e' : tau'   
------------------------------------------------------------
Gamma |- LET x = e in e' : tau'

Type Checker

Type checker needs Gamma -- gives type of each "term variable".

val tc : ty env -> exp -> ty
fun tc Gamma (ARITH ... ) =  <as before>
  | tc Gamma (VAR x)      =
      case Gamma (x) 
        of Some tau => tau
         | None     => raise IllTyped
  | tc Gamma (LET x, e1, e2) = 
        let tau1 = tc Gamma e1
        in  tc (extend Gamma x tau1) e2
        end 

Functions

Introduction:

Gamma{x->tau1} |- e : tau2   
------------------------------------------------------------
Gamma |- fn x : tau1 => e  : tau1 -> tau2

Elimination:

Gamma |- e  : tau1 -> tau2   
Gamma |- e1 : tau1
------------------------------------------------------------
Gamma |- e e1 : tau2

Midterm Review

Plan on:

Recursion and Induction

Understanding a language: Key Questions

  1. What is the abstract syntax?

  2. What are the values?

  3. What are the environments?

  4. How does evaluation happen?

  5. What is the initial basis?

  6. What are the types?

First-class functions

Local bindings

Data structures and associated operations

Pattern matching

Handling exceptional circumstances: Exceptions

Cost Models and Optimizations

Operational semantics

23 Mar 2015: Type Checking: Monomoprhic & Polymorphic

pdf slides

Announcements

Where we've been and where we're going

New watershed in the homework

What's next is much more sophisticated type systems, with an infinite number of types. We'll focus on two questions:

We'll look at these questions in two contexts: monomorphic and polymorphic languages.

Monomorphic vs Polymorphic Types

Monomorphic types have "one shape."

Polymorphic types have "many shapes."

Design and implementation of monomorphic languages

Language designer's agenda:

Here's how it works:

  1. Every new variety of type requires special syntax

  2. We get three kinds of typing rules: formation, introduction, and elimination

  3. Implementation is a straightforward application of what you already know.

Question: If I add lists to a language, how many new types am I introducing?

Type formation

Examples: Well-formed types

These are types:

Examples: Not yet types, or not types at all

These "types in waiting" don't classify any terms

These are utter nonsense

Type formation rules

We need a way to classify type expressions into:

Type constructors

Technical name for "types in waiting"

Given zero or more arguments, produce a type:

More complex type constructors:

What's a good type?:
TeX slide

Type judgments for monomorphic system

Two judgments:

Monomorphic type rules

Type rules for variables:
TeX slide

Type rules for control:
TeX slide

Notice: one rule for if!!

Classic types for data structures

Product types: both x and y:
TeX slide

(At run time, identical to cons, car, cdr)

Arrow types: function from x to y:
TeX slide

Arrow types: function from x to y:
TeX slide

Typical syntactic support for types

Explicit types on lambda and define:

Abstract syntax:

datatype exp = ...
 | LAMBDA of (name * tyex) list * exp
    ...
datatype def = ...
 | DEFINE of name * tyex * ((name * tyex) list * exp)
    ...

Array types: array of x:
TeX slide

Array types continued:
TeX slide

Typing Rule Exercise

References (similar to C/C++ pointers):
TeX slide

Wait for it ...

Reference Types:
TeX slide

Coding the arrow-introduction rule

TeX slide

* Type-checking LAMBDA * datatype exp = LAMBDA of (name * tyex) list * exp ... fun ty (Gamma, LAMBDA (formals, body)) = let val Gamma' = (* body gets new env *) foldl (fn ((n, ty), g) => bind (n, ty, g)) Gamma formals val bodytype = ty(Gamma', body) val formaltypes = map (fn (n, ty) => ty) formals in funtype (formaltypes, bodytype) end

25 Mar 2015: Polymorphic Type Checking

pdf slides

Plan

Monday: Typed Impcore

Today: TypedUScheme

Polymorphic Type Checking

Type formation: Composing types

Typed Impcore:

Standard ML:

Can't add new syntactic forms and new type formation rules for every new type.

Representing type constructors generically:
TeX slide

Question: How would you represent an array of pairs of integers?:
TeX slide

Question: How would you represent an array of pairs of booleans?:
TeX slide

Well-formed types

We still need to classify type expressions into:

Idea:

Type formation through kinds

Type formation through kinds:
TeX slide

Use kinds to give arities:
TeX slide

The kinding judgment:
TeX slide

Kinding rules for types:
TeX slide

Limitations of monomorphic type systems

Monomorphic types are limiting

Each new type constructor requires

Monomorphism hurts programmers too

Monomorphism leads to code duplication

User-defined functions are monomorphic:

(define int lengthI ((list int) l)
   (if (null? l) 0 (+ 1 (lengthI (cdr l)))))
(define int lengthB ((list bool) l)
   (if (null? l) 0 (+ 1 (lengthB (cdr l)))))
(define int lengthS ((list sym) l)
   (if (null? l) 0 (+ 1 (lengthS (cdr l)))))

Quantified types

Quantified types:
TeX slide

Representing quantified types

Two new alternatives for tyex:

datatype tyex
  = TYCON  of name
  | CONAPP of tyex * tyex list 
  | FORALL of name list * tyex
  | TYVAR  of name

Kinding rules for quantified types:
TeX slide

          *** Programming with these types ***

Substitute for quantified variables

-> length
<proc> : (forall ('a) (function ((list 'a)) int))
-> (@ length int)
<proc> : (function ((list int)) int)
-> (length '(1 2 3))
type error: function is polymorphic; instantiate before applying
-> ((@ length int) '(1 2 3))
3 : int


          *** Substitute what you like ***
-> length
<proc> : (forall ('a) (function ((list 'a)) int))
-> (@ length bool)
<proc> : (function ((list bool)) int)
-> ((@ length bool) '(#t #f))
2 : int


          *** More ``Instantiations'' ***
-> (val length-int (@ length int))
length-int : ((list int) -> int)
-> (val cons-bool (@ cons bool))
cons-bool : ((bool (list bool)) ->
                                (list bool))
-> (val cdr-sym (@ cdr sym))
cdr-sym : ((list sym) -> (list sym))
-> (val empty-int (@ '() int))
() : (list int)

Bonus instantiation:

-> map
<proc> : 
  (forall ('a 'b) 
      (function ((function ('a) 'b) 
                 (list 'a)) 
           (list 'b)))
-> (@ map int bool)
<proc> : (function ((function (int) bool) 
                    (list int)) 
              (list bool))

Create your own!

Abstract over unknown type using type-lambda

  -> (val id (type-lambda ('a)
                (lambda (('a x)) x)))
  id : (forall ('a) ('a -> 'a)) 

'a is type parameter (an unknown type)

This feature is parametric polymorphism

Two forms of abstraction:

term type
lambda function (arrow)
type-lambda forall

Power comes at notational cost

Function composition

-> val o (type-lambda ('a 'b 'c)                                                                         

   (lambda ((('b -> 'c) f)                                                                            
            (('a -> 'b) g))                                                                           
     (lambda (('a x)) (f (g x))))))                                                                   

o : (forall ('a 'b 'c) 
       (('b -> 'c) ('a -> 'b) -> ('a -> 'c)))                                    

Aka o :

Type rules for polymorphism

Instantiate by substitution:
TeX slide

Generalize with type-lambda:
TeX slide

          *** A phase distinction embodied in code ***

-> (val x 3)
3 : int
-> (val y (+ x x))
6 : int

fun checkThenEval (d, (delta, gamma, rho)) =
  let val (gamma', tystring)  = elabdef (d, gamma, delta)
      val (rho',   valstring) = evaldef (d, rho)
      val _ = print (valstring ^ " : " ^ tystring)
  in  (delta, gamma', rho')
  end

Three environments --- what happens?:
TeX slide

Three environments revealed:
TeX slide

Exercise: Three environments:
TeX slide

30 Mar 2015: Type Inference

pdf slides

Plan

Intuition

Constraints

Introducing foralls (Wednesday)

Key Ideas:

Why Study?

New topic: Type inference:
TeX slide

          *** What type inference accomplishes ***
-> (define     double (x)       (+ x x))
double                         ;; uScheme
-> (define int double ((int x)) (+ x x))
double : (int -> int)          ;; Typed uSch.
-> (define     double (x)       (+ x x))
double : int -> int            ;; nano-ML




          *** What else type inference accomplishes ***
-> ((@ cons bool) #t ((@ cons bool) #f (@ '() bool)))
(#t #f) : (list bool)    ;; typed uScheme
-> (   cons       #t (   cons       #f    '()      ))
(#t #f) : bool list      ;; nano-ML

Key ideas:

  1. For each unknown type, introduce a fresh type variable

  2. Enforce equality constraints

  3. Introduce type-lambda at let/val bindings

{Examples}:
TeX slide

Let's do an example on the board

(val-rec double (lambda (x) (+ x x)))

What do we know?

Key idea: record the constraint in a typing judgement

a2 = int /\ a2 = int, { double : a1, x : a2 } |- (+ x x) : int

Example: if

Example:

Inferring polymorphic types

let val app2 = (lambda (f x y)
                  (begin
                    (f x)
                    (f y)))



          *** Exercise: Give the type of cc ***

let val cc = (lambda (nss) (car (car nss)))



          *** Exercise: Give the type of cc ***

let val cc = (lambda (nss) (car (car nss)))

forall 'a . 'a list list -> 'a

Formalizing Type Inference

Formalizing Type Inference

Sad news: Full type inference for polymorphism is not decidable.

Solution: Parameters have monomorphic types.

Consequence: Polymorphic functions are not first class.

          *** Hindley-Milner types ***
datatype ty
  = TYCON  of name        
  | CONAPP of ty * ty list
  | TYVAR  of name        

datatype type_scheme
  = FORALL of name list * ty

Key ideas:
TeX slide

Key ideas repeated:
TeX slide

{Type inference}:
TeX slide

{Apply rule}:
TeX slide

{Exercise: Begin Rule}:
TeX slide

{Exercise: Begin Rule}:
TeX slide

{Type inference, operationally}:
TeX slide

Writing the constraint solver

          *** Solve these constraints! ***
datatype con = ~   of ty  * ty
             | /\  of con * con
             | TRIVIAL
infix 4 ~
infix 3 /\

{Solving Constraints}:
TeX slide

When is a constraint satisfied?:
TeX slide

{Examples}:
TeX slide

Board: which of these have solutions?

'a ~ int
'a ~ int list
'a ~ int -> int
'a ~ 'a
'a ~ tau        (arbitrary tau)

Board: which of these have solutions?

int ~ bool
int list ~ bool list

Board: which of these have solutions?

'a * int ~ bool * 'b
'a * int ~ bool -> 'b

Question: in solving tau1 ~ tau2, how many potential cases are there to considerer?

Question: how are you going to handle each case?

{Solving Constraint Conjunctions}:
TeX slide

What you know and can do after this lecture

So far

After this lecture, you can write solve, a function which, given a constraint C, has one of three outcomes:

You can also write a type inferencer ty for everything except let forms. (Coming Wednesday)

1 Apr 2015: Instantiation and Generalization

pdf slides

Plan

Type inference review

Moving from type schema to types (instantiation)

Moving from types to type schema (generalization)

Type Inference:
TeX slide

Key Idea:
TeX slide

Judgement forms:
TeX slide

Formalizing Type Inference:
TeX slide

From Type Schema to Types

Moving between type schema and types:
TeX slide

From Type Schema to Types:
TeX slide

Why the freshness requirement?

Consider

Gamma = {fst : forall 'a 'b. 'a * 'b -> 'a, y : 'ay}

??, Gamma |- if (y, fst 2 3, 4) : ??

Imagine we ignore the freshness constraint when instantiating fst:

fst : 'ay * 'b -> 'ay

From if, we get the constraints:

'ay ~ bool

'ay ~ int

which aren't satisfiable. Which means this code would be erroneously flagged as an error.

Correct typing:

 'ay ~ bool, Gamma |- if (y, fst 2 3, 4) : int

Why the distinctnes requirement?

fst : 'a * 'a -> 'a

Gamma |- fst 2 #t

Application rule yields constraints:

'a ~ int

'a ~ bool

which aren't satisfiable. Which means this code would also be erroneously flagged as an error.

Correct typing:

Gamma |- fst 2 #t : int

From Types to Type Schema

From Types to Type Schema:
TeX slide

Generalize Function:
TeX slide

First Candidate VAL rule:
TeX slide

Example:
TeX slide

Second Candidate VAL rule:
TeX slide

VAL rule:
TeX slide

          *** Let Examples ***
(lambda (ys)
   (let ((s (lambda (x) (cons x '()))))
      (pair (s 1) (s #t))))

(lambda (ys)
   (let ((extend (lambda (x) (cons x ys))))
      (pair (extend 1) (extend #t))))

(lambda (ys)
   (let ((extend (lambda (x) (cons x ys))))
      (extend 1)))

Let:
TeX slide

Idempotence:
TeX slide

LetRec:
TeX slide

Forall things

Managing Quantified types
val and val-rec let, letrec, ... lambda
FORALL contains all variables (because none are free in the context) FORALL contains variables not free in the context FORALL is empty
Generalize over all variables (because none are free in the context) Generalize over variables not free in the context Never generalize

6 Apr 2015: Object-orientation and Smalltalk

pdf slides

Today

Review

Let:
TeX slide

          *** Example for Inference ***
(val g (lambda (x) 
        (let ((f (lambda (y) 
                 (pair (cons y '()) (+ x 1))))) 
             (f x))))

> g : int -> int list * int

VAL rule:
TeX slide

Preliminaries

Where have we been?

What about big programs?

An area of agreement and a great divide:

                      INFORMATION HIDING
                          /         \
                         /           \
                  reuse /             \ modular reasoning
                       /               \
                      /                 \
                   OBJECTS            MODULES

Two kinds of reuse:

Don't conflate them!

History of objects

We know that mixing code and data can create powerful abstractions (function closures)

Objects are another way to mix code and data

Pioneers were Nygaard and Dahl, who added objects to Algol 60, producing SIMULA-67, the first object-oriented language

What's an object?

Agglutination containing

A lot like a closure

What are objects good at?

Not really useful for building small things

If you build a big, full-featured abstraction, you can easily use inheritance to build another, similar abstraction. Very good at adding new kinds of things that behave similarly to existing things.

For your homework, you'll take a Smalltalk system that has three kinds of numbers, and you'll add a fourth kind of number.

What's hard about objects?

If you do anything at all interesting, your control flow becomes smeared out over half a dozen classes, and your algorithms are nearly impossible to understand.

Smalltalk

Why Smalltalk?

8 Apr 2015: Object-orientation and Smalltalk

pdf slides

Today

Smalltalk Introduction, continued

The Six Questions

Impcore syntax:
TeX slide

Smalltalk syntax:
TeX slide

Smalltalk syntax:
TeX slide

Message passing

Look at SEND

N.B. BLOCK and LITERAL are special objects.

          *** Example: A bank account ***

-> (use finance.smt)
<class FinancialHistory>
<class DeductibleHistory>
-> (val account (initialBalance: FinancialHistory 1000))
<FinancialHistory>
-> (deposit:from: account 400 #salary)
1400
-> (withdraw:for: account 50 #plumber)
1350
-> (cashOnHand account)
1350

Protocol --- the interface to an object

Protocol is Smalltalk term of art:

Ruby dialect "duck typing" is a statement about protocols

Protocol is determined by the class of which an object is an instance

TeX slide

Arities:

Every object gets not just the protocol but the implementations from its class. And a class can inherit from its superclass (more undefined terms) all the way up to class Object.

          *** Simple examples ***
-> true
<True>
-> True
<class True>
-> Object
<class Object>
-> (isNil 3)
<False>
-> (isNil nil)
<True>
-> (println nil)
nil
nil

Smalltalk's initial basis

Blocks and Booleans

Blocks are closures

Blocks are objects

Booleans use continuation-passing style

Protocol for Booleans:
TeX slide

Booleans implemented with two classes True and False

Classes True and False:
TeX slide

Method dispatch in the Booleans

Protocol for Booleans:
TeX slide

Board - Method dispatch

To answer a message:

  1. Consider the class of the receiver

  2. Is the method with that name defined?

  3. If so, use it

  4. If not, repeat with the superclass

Run out of superclasses?

"Message not understood"

{ message dispatched to class }:
TeX slide

13 Apr 2015: Smalltalk Conclusion and Intro to Information Hiding Using Modules

` pdf slides

Today

  1. Smalltalk Wrap-up

  2. Modules for Information Hiding

Blocks

          *** Blocks manage loops ***
-> (val x 10)
-> (val y 20)
-> (whileTrue: [(<= x (* 10 y))]
      [(set x (* x 3))])
nil
-> x
270

Protocol for blocks:
TeX slide

Collections

Goal of objects is reuse

Key to successful reuse is a well-designed class hierarchy

``Collection hierarchy'':
TeX slide

Collections:
TeX slide

Collection mutators:
TeX slide

Collection observers:
TeX slide

Collection iterators:
TeX slide

Implementing Collections

Implementing collections:
TeX slide

Reusable methods:
TeX slide

Question: what's the most efficient way to find the size of a list?

Question: what's the most efficient way to find the size of an array?

{ method}:
TeX slide

{The four crucial {} methods}:
TeX slide

Example collection - Sets

TeX slide

Most subclass methods work by delegating all or part of work to list members

N.B. Set is a client of List, not a subclass!

Next example highlight: class method and super!

TeX slide

Subtyping

Key strategy for reuse in object-oriented languages: subtype polymorphism

Subtyping mathematically:
TeX slide

Subtyping is not inheritance:
TeX slide

Double Dispatch

Typical object-orientation:

What if you need to choose code based on both receiver and argument?

Solution: method name encodes both operation and type of argument

Examples:

addIntegerTo:
addFloatTo:

In class Float:

(method + (anInteger) (addFloatTo: anInteger self))

In class Integer:

(method addFloatTo: (aFloat) (<<code to add an Integer and a Float>>)

Consider evaluation of:

(+ 5.5 3)

Sends + message to object 5.5 with argument 3

Sends addFloatTo: message to 3 with argument 5.5

Code to add an Integer and a Float is executed.

Metaclasses

                                             Object!
                                               /
                                              /
                                           Class
                                            /
                                           /
                        Object ===> Object's metaclass
                          /              /
                         /              /
                   Collection ===> Collection's metaclass
                       /              /
                      /              /
     students ====> Set ========> Set's metaclass
                    /              /
                   /              /
 alphanum ====> CharSet ====> CharSet's metaclass

Things of note in Smalltalk

  1. Wide interfaces, reused

  2. Algorithms smeared out over multiple classes

  3. Behavioral subtyping, aka "Duck typing"

  4. Old wine in new bottles

    • Exceptions (usage of blocks)

    • Higher-order methods

    • Polymorphic methods

  5. Initialization

  6. Double dispatch

Information Hiding and Module Systems

15 Apr 2015: Information Hiding Using Modules

pdf slides

Today

  1. Modules: The concept

  2. Modules in Standard ML

    1. Signatures

    2. Structures

    3. Functors

Reading:

Information hiding using objects

Smalltalk

(Other object-oriented languages have elaborate controls for public/private)

Information hiding, really?

Problem: inheritance violates abstraction and modularity

Modules and separate compilation

Why modules?

Unlocking the final door for building large software systems

Modules overview

Functions of a true modules system:

Real modules include separately compilable interfaces and implementations

Interfaces

Collect declarations

Things typically declared:

Terminology: a module is a client of the interfaces it depends on

Roles of interfaces in programming:

The best-proven technology for structuring large systems.

Ways of thinking about interfaces

Two approaches to writing interfaces

Interface "projected" from implementation:

Full interfaces:

Module Implementations

Standard ML Modules

The Perl of module languages?

What we've been using so far is the core language

Modules are a separate language layered on top.

ML module terminology

Interface is a signature

Implementation is a structure

Generic module is a functor

Structures and functors match signature

Analogy: Signatures are the ``types'' of structures.

Signature basics

Signature says what's in a structure

Specify types (w/kind), values (w/type), exceptions.

Ordinary type examples:

    type t        // abstract type, kind *
    eqtype t
    type t = ...  // 'manifest' type
    datatype t = ...

Type constructors work too

    type 'a t     // abstract, kind * => *   
    eqtype 'a t
    type 'a t = ...
    datatype 'a t = ...

ML Modules examples, part I

          *** Signature example: Integers ***
signature INTEGER = sig
  eqtype int             (* <-- ABSTRACT type *)
  val ~   : int -> int
  val +   : int * int -> int
  val -   : int * int -> int
  val *   : int * int -> int
  val div : int * int -> int
  val mod : int * int -> int
  val >   : int * int -> bool
  val >=  : int * int -> bool
  val <   : int * int -> bool
  val <=  : int * int -> bool
  val compare : int * int -> order
  val toString   : int    -> string
  val fromString : string -> int option
end

Implementations of integers

A selection...

structure Int    :> INTEGER
structure Int31  :> INTEGER  (* optional *)
structure Int32  :> INTEGER  (* optional *)
structure Int64  :> INTEGER  (* optional *)
structure IntInf :> INTEGER  (* optional *)

What about natural numbers?

signature NATURAL = sig
   ...
end

And bignums!

functor 
    BignumFn(Nat:NATURAL) :> INTEGER = ...

ML Modules examples, part II

          *** Queues in Standard ML: A Signature ***
signature QUEUE = sig
  type 'a queue    (* another abstract type *)
  exception Empty

  val empty : 'a queue
  val put : 'a * 'a queue -> 'a queue
  val get : 'a queue -> 'a * 'a queue   (* raises Empty *)

  (* LAWS:  get(put(a, empty))     ==  (a, empty)
            ...
   *)
end


          *** Queues in Standard ML: A Structure ***
structure Queue :> QUEUE = struct
  type 'a queue = 'a list
  exception Empty

  val empty = []
  fun put (q, x) = q @ [x]
  fun get [] = raise Empty
    | get (x :: xs) = (x, xs)


  (* LAWS:  get(put(a, empty))     ==  (a, empty)
            ...
   *)
end



          *** Dot notation to access elements ***
structure Queue :> QUEUE = struct
  type 'a queue = 'a list
  exception Empty

  val empty = []
  fun put (q, x) = q @ [x]
  fun get [] = raise Empty
    | get (x :: xs) = (x, xs)
end

fun single (x:'a) : 'a Queue.queue = 
   Queue.put(Queue.empty, x)

Exercise: Signature for a Stack

structure Stack = struct
   type 'a stack = 'a list
   exception Empty
   val empty = []
   val push = op ::
   fun pop [] = raise Empty
     | pop (tos::rest) = tos
end

Exercise: Signature for a Stack

signature STACK = sig
   type 'a stack 
   exception Empty
   val empty : 'a stack
   val push  : 'a * 'a stack -> 'a stack
   val pop   : 'a stack -> 'a 
end

Functors

A Functor is a function on modules.

functor AddSingle(Q:QUEUE) = 
   struct
     structure Queue = Q
     fun single x = Q.put (Q.empty, x)
   end

Instantiating Functors

Functor applications are evaluated at compile-time.

functor AddSingle(Q:QUEUE) = 
    struct
     structure Queue = Q
     fun single x = Q.put (Q.empty, x)
   end

structure QueueS  = AddSingle(Queue)
structure EQueueS = AddSingle(EQueue)

where EQueue is a more efficient implementation

22 Apr 2015: Modules Example and Lambda Calculus Intro

pdf slides

Announce

Please complete on-line survey!

Today

  1. SML: Extended Example
  2. Lambda Calculus

Extended example: Error-tracking Interpreter

Error-tracking Interpreter

An Extended Example

What's going on in this example:

Error modules, board: Three common implementations (all included in recitation)

Your obligations: two types, three functions, algebraic laws

          *** Classic ``accumulator'' for errors ***
signature ERROR = sig
  type error   (* a single error *)
  type summary (* summary of what errors occurred *)

  val nothing : summary                  (* no errors *)
  val <+> : summary * summary -> summary (* combine *)

  val oneError : error -> summary

  (* laws:   nothing <+> s == s
             s <+> nothing == s
             s1 <+> (s2 <+> s3) == (s1 <+> s2) <+> s3    
                                        // associativity
   *)
end

Computations Abstraction

Ambitious! (will make your head hurt a bit)

Computations either:

Errors must be threaded through everything :-(

          *** Combining generic computations ***
signature COMPUTATION = sig
  type 'a comp    (* Computation! When run, results in
                     value of type 'a or error summary *)

  val succeeds : 'a -> 'a comp   (* a computation 
                                    without errors *)

  val <$> : ('a -> 'b) * 'a comp -> 'b comp
               (* apply a pure function to a computation *)
  val <*> : ('a -> 'b) comp * 'a comp -> 'b comp
                      (* application inside computations *)

  val >>= : 'a comp * ('a -> 'b comp) -> 'b comp
                 (* computation followed by continuation *)
end

Board:

eval e1 + eval e2

op + (eval e1, eval e2)

curry op + (eval e1) (eval e2)

curry op + <$> eval e1 <*> eval e2

Note:

eval e1 : int comp

curry op + : int comp -> int comp -> int comp

<$>: (int comp -> (int comp -> int comp)) * (int comp) -> (int comp -> int comp)

<*> (int comp -> int comp) * int comp -> int comp

curry op + <$> eval e1 : (int comp -> int comp)



          *** {Buckets of \emph{generic} algebraic laws} ***
  succeeds a >>= k  == k a                  // identity
  comp >>= succeeds == comp                 // identity
  comp >>= (fn x => k x >>= h) == (comp >>= k) >>= h  
                                          // associativity
  succeeds f <*> succeeds x == succeeds (f x)  // success
  ...


          *** Environments using ``computation'' ***
signature COMPENV = sig
  type 'a env   (* environment mapping strings
                   to values of type 'a *)
  type 'a comp  (* computation of 'a or
                   an error summary *)

  val lookup : string * 'a env -> 'a comp
end


          *** Payoff! ***
functor InterpFn(structure Error : ERROR
                 structure Comp  : COMPUTATION
                 structure Env   : COMPENV
                 val zerodivide : Error.error
                 val error : Error.error -> 'a Comp.comp
  (* LOOK --> *) sharing type Comp.comp = Env.comp) =
struct
  fun curry f x y = f (x, y)

  val (<*>, <$>, >>=) = (Comp.<*>, Comp.<$>, Comp.>>=)
  infix 4 <$>
  infix 3 <*> >>=
  ...
end


          *** Build an intepreter, continued ***
datatype exp = LIT of int
             | VAR of string
             | PLUS of exp * exp
             | DIV  of exp * exp
fun eval (e, rho) =
 let fun ev(LIT n) = Comp.succeeds n
       | ev(VAR x) = Env.lookup (x, rho)
       | ev(PLUS (e1, e2)) = curry op + <$> ev e1 <*> ev e2
       | ev(DIV (e1, e2))  = ev e1 >>= (fn n1 =>
                             ev e2 >>= (fn n2 =>
                             case n2
                               of 0 => error zerodivide
                                | _ => Comp.succeeds
                                              (n1 div n2)))
 in  ev e
 end



          *** {Extend a signature with \lit{include}} ***
signature ERRORCOMP = sig
  include COMPUTATION
  structure Error : ERROR             (* <-- LOOK *)
  datatype 'a result = SUCC of 'a
                     | ERR  of Error.summary
  val run : 'a comp -> 'a result      (* <-- LOOK *)
  val error : Error.error -> 'a comp
end



          *** {Let's build \lit{ERRORCOMP}} ***
functor ErrorCompFn(structure Error : ERROR) :> 
  ERRORCOMP where type Error.error   = Error.error
              and type Error.summary = Error.summary
=
struct
  structure Error = Error
  datatype 'a result = SUCC of 'a
                     | ERR  of Error.summary

  type 'a comp = 'a result
  fun run comp = comp

  ... succeeds ...
  ... error ...

Lambda Calculus

The world's simplest reasonable programming language

Only three syntactic forms:

M ::= x | \x.M | M M'

Everything is programming with functions

First example:

(\x.\y.x) M N --> (\y.M) N --> M

Crucial: argument N is never evaluated (could have an infinite loop)

Two laws:

27 Apr 2015: Lambda Calculus

pdf slides

Announcements

- Please fill out on-line course evaluations

Today

- Lambda calculus as universal language of computation

Why study lambda calculus?

Programming with lambda calculus

Alert to the reading: Wikipedia is reasonably good on this topic

Everything is continuation-passing style

Q: Who remembers the boolean equation solver?

Q: What classes of results could it produce?

Q: How were the results delivered?

Q: How shall we do Booleans?

Coding Booleans

Booleans take two continuations:

true  = \x.\y.x
false = \x.\y.y

if M then N else P = ???

if = \b.\t.\e.b t e

Coding Pairs

Coding Lists

Coding numbers: Church Numerals

Wikipedia good: "Church numerals"

Key Idea: The value of a numeral is the number of times it applies its argument function.

Encoding natural numbers:
TeX slide

Church Numerals:
TeX slide

          *** Church Numerals in $\lambda$ ***
zero  = \f.\x.x;
succ  = \n.\f.\x.f (n f x);
plus  = \n.\m.n succ m;
times = \n.\m.n (plus m) zero;
 ...
-> four;
\f.\x.f (f (f (f x)))
-> three;
\f.\x.f (f (f x))
-> times four three;
\f.\x.f (f (f (f (f (f (f (f (f (f (f (f x)))))))))))

Question: What's missing from this picture?

Answer: We're missing recursive functions.

Astonishing fact: we don't need letrec or val-rec

Fixed points, recursion

What solves this equation?:
TeX slide

Exercise

Is there a solution? Is it unique? If so, what is it?

f1 = \n.\m.(eq? n m) n 
              (plus n (f1 (succ n) m));

f2 = \n.f2 (isZero? n 100 (pred n));

f3 = \xs.xs nil (\z.\zs.cons 0 (f3 zs));

f4 = \xs.\ys.f4 ys xs;

Wait for it...:
TeX slide

Exercise answers

f1 = \n.\m.(eq? n m) n 
              (plus n (f1 (succ n) m));
    ; sigma (sum from n to m)

f2 = \n.f2 (isZero? n 100 (pred n));
    ; no unique solution (any constant f2)

f3 = \xs.xs nil (\z.\zs.cons 0 (f3 zs));
    ; map (const 0)

f4 = \xs.\ys. f4 xs ys;
    ; not unique: constant functions, commutative ops

Recursion = Fixed point:
TeX slide

Suppose gf F = F. Proof that F is factorial.

For all n, gf F n = n!, by induction:

F 0 = gf F 0 = 1
F n
  = { by assumption }
gf F n 
  = { definition of gf }
if n = 0 then 1 else n * F (n-1)
  = { assumption, n > 0 }
n * F (n-1)
  = { induction hypothesis }
n * (n-1)!
  = { definitiion of factorial }
n!

Now you do it

          *** Conversion to fixed point ***

length = \xs.null? xs 0 (+ 1 (length (cdr xs)))


lg = \lf.\xs.null? xs 0 (+ 1 (lf (cdr xs)))

Note that: lg length = length

Fixed-point operators

One startling idea

You can define a fixed-point operator fix

Algebraic Law: fix g = g (fix g)

Use fix g to define recursive functions!

{Y combinator can implement }:
TeX slide

Lambda calculus in context

Why is it called a "calculus"?

What's the role of calculi in computer science:

Why so many calculi? They have simple metatheory and proof technique.

29 Apr 2015: Comp 105 Conclusion

pdf slides

Announcements

Today

What have we done?

Type Systems

Type Systems: Big Ideas

Type Systems: Mechanics

Hindley-Milner Type Inference: Big Idea

Hindley-Milner Type Inference: Mechanics

Object-Oriented Programming: Big Ideas

Object-Oriented Programming: Mechanics

Module Systems a la SML: Big Ideas

Module Systems a la SML: Mechanics

Lambda Calculus: Big Ideas

Lambda Calculus: Mechanics

Programming Experience

Built substantial pieces of code

Where might you go from here?

Haskell

Prolog

Ruby

Additional Courses

Big-picture questions?

Studying for the Exam

Other Questions?

Congratulations!

end


Back to class home page