Download Lambda Calculus and Types: CMSC 330 Lecture Notes - Prof. Atif M. Memon and more Study notes Programming Languages in PDF only on Docsity!
CMSC 330: Organization of
Programming Languages
Lambda Calculus and Types
CMSC 330 2
Lambda Calculus
• A lambda calculus expression is defined as
e ::= x variable
| x.e function
| e e function application
• x.e is like (fun x -> e) in OCaml
• That’s it! Only higher-order functions
CMSC 330 3
Beta-Reduction, Again
• Whenever we do a step of beta reduction...
- (x.e1) e2 e1[x/e2]
- ...alpha-convert variables as necessary
• Examples:
- (x.x (x.x)) z = (x.x (y.y)) z z (y.y)
- (x.y.x y) y = (x.z.x z) y z.y z
CMSC 330 4
Encodings
• It turns out that this language is Turing complete
• That means we can encode any computation
we want in it
- ...if we’re sufficiently clever...
CMSC 330 7
Pairs
(a,b) = x.if x then a else b
fst = f.f true
snd = f.f false
• Examples:
- fst (a,b) = (f.f true) (x.if x then a else b) (x.if x then a else b) true if true then a else b a
- snd (a,b) = (f.f false) (x.if x then a else b) (x.if x then a else b) false if false then a else b b
CMSC 330 8
Natural Numbers (Church*)
0 = f.y.y
1 = f.y.f y
2 = f.y.f (f y)
3 = f.y.f (f (f y))
i.e., n = f.y.
succ = z.f.y.f (z f y)
iszero = g.g (y.false) true
- Recall that this is equivalent to g.((g (y.false)) true)
*(Named after Alonzo Church, developer of lambda calculus)
CMSC 330 9
Natural Numbers (cont’d)
• Examples:
succ 0 = (z.f.y.f (z f y)) (f.y.y) f.y.f ((f.y.y) f y) f.y.f y = 1
iszero 0 = (z.z (y.false) true) (f.y.y) (f.y.y) (y.false) true (y.y) true true
CMSC 330 10
Arithmetic defined
- Addition, if M and N are integers (as expressions): M + N = x.y.(M x)((N x) y) Equivalently: + = M.N.x.y.(M x)((N x) y)
- Multiplication: M * N = x.(M (N x))
- Prove 1+1 = 2. 1+1 = x.y.(1 x)((1 x) y) x.y.((x.y.x y) x)(((x.y.x y) x) y) x.y.(y.x y)(((x.y.x y) x) y) x.y.(y.x y)((y.x y) y) x.y.x ((y.x y) y) x.y.x (x y) = 2
- With these definitions, can build a theory of integer arithmetic.
CMSC 330 13
Example
fact = f. n.if n = 0 then 1 else n * (f (n-1))
- The second argument to fact is the integer
- The first argument is the function to call in the body
- We’ll use Y to make this recursively call fact
(Y fact) 1 = (fact (Y fact)) 1
if 1 = 0 then 1 else 1 * ((Y fact) 0)
1 * ((Y fact) 0)
1 * (fact (Y fact) 0)
1 * (if 0 = 0 then 1 else 0 * ((Y fact) (-1))
CMSC 330 14
Discussion
• Using encodings we can represent pretty much
anything we have in a “real” language
- But programs would be pretty slow if we really implemented things this way
- In practice, we use richer languages that include built- in primitives
• Lambda calculus shows all the issues with
scoping and higher-order functions
• It's useful for understanding how languages work
CMSC 330 15
The Need for Types
• Consider the untyped lambda calculus
• Since everything is encoded as a function...
- We can easily misuse terms
- false 0 y.y
- if 0 then ...
- Everything evaluates to some function
• The same thing happens in assembly language
- Everything is a machine word (a bunch of bits)
- All operations take machine words to machine words
CMSC 330 16
What is a Type System?
• A type system is some mechanism for
distinguishing good programs from bad
- Good = well typed
- Bad = ill typed or not typable; has a type error
• Examples
- 0 + 1 // well typed
- false 0 // ill-typed; can’t apply a boolean
CMSC 330 19
Type Judgments
• We will construct a type system that proves
judgments of the form
A e : t
- “In type environment A, expression e has type t”
• If for a program e we can prove that it has some
type, then the program type checks
- Otherwise the program has a type error, and we’ll reject the program as bad
CMSC 330 20
Type Environments
• A type environment is a map from variables
names to their types
- Just like in our operational semantics for Scheme
• • is the empty type environment
• A, x:t is just like A, except x now has type t
• When we see a variable in the program, we’ll
look up its type in the environment
CMSC 330 21
Type Rules
A n : int
e ::= n | x | x:t.e | e e
A x : A(x)
x A
A x:t.e : t t'
A, x : t e : t' A e e' : t'
A e : t t' A e' : t
CMSC 330 22
Example
A (x:int.+ x 3) 4 : int
A = + : int int int
A (x:int.+ x 3) : int int A 4 : int
B + x 3 : int
B 3 : int
B + : iii
B + x : int int
B x : int
B = A, x : int
CMSC 330 25
Type Inference
• We could extend the rules to show how a
language could figure out, even if types aren't
specified, what the types of everything are in a
program
- Can you believe there are languages which can actually do this?
• We could do these things, but we actually won't.
CMSC 330 26
Summary
• Lambda calculus shows all the issues with
scoping and higher-order functions
• It's useful for understanding how languages
work
CMSC 330 27
Practice
• Reduce the following:
- (x.y.x y y) (a.a) b
- (or true) (and true false)
- (* 1 2) (* m n = M.N.x.(M (N x)) )
• Derive and prove the type of:
- (f:int->int.n:int.f n) (x:int. 3 + x) 6
- x:int->int->int. y:int->int. z:int.x z (y z)