Lambda Calculus and Types in Programming Languages - Prof. Atif M. Memon, Study notes of Programming Languages

Material Type: Notes; Professor: Memon; Class: ORGNZTN PROGM LANG; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-4jo
koofers-user-4jo 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
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-conv ert 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 suff iciently clever...
CMSC 330 5
Booleans
The lambda calculus was creat ed by logician Alonzo
Church in the 1930's to formulate a m athematical logical
system
true = x.y.x
false = x.y.y
if a then b else c is defined to be the expression: a b c
•Examples:
if true then b else c (x.y.x) b c (y.b) c b
if fal se then b else c (x.y.y) b c (y.y ) c c
CMSC 330 6
Booleans (continued)
Other Boolean operations:
not = x.((x false) true)
not true x.((x false) true) true
((true false) true) false
and = x.y.((xy) false)
•or = x.y.((x true) y)
•Show not, and and or have the desired properties, …
Given these operatio ns, can build up a logical inference
system
pf3
pf4
pf5

Partial preview of the text

Download Lambda Calculus and Types in Programming Languages - 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 5

Booleans

The lambda calculus was created by logician Alonzo Church in the 1930's to formulate a mathematical logical system true = x.y.x false = x.y.y

if a then b else c is defined to be the  expression: a b c

  • Examples:
    • if true then b else c  (x.y.x) b c  (y.b) c  b
    • if false then b else c  (x.y.y) b c  (y.y) c  c

CMSC 330 6

Booleans (continued)

Other Boolean operations:

  • not = x.((x false) true)
  • not true  x.((x false) true) true  ((true false) true)  false
  • and = x.y.((xy) false)
  • or = x.y.((x true) y)
  • Show not, and and or have the desired properties, …
  • Given these operations, can build up a logical inference system

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 11

Looping

  • Define D = x.x x
  • Then
    • D D = (x.x x) (x.x x)  (x.x x) (x.x x) = D D
  • So D D is an infinite loop
    • In general, self application is how we get looping

CMSC 330 12

The “Paradoxical” Combinator

Y = f.(x.f (x x)) (x.f (x x))

  • Then Y F = (f.(x.f (x x)) (x.f (x x))) F  (x.F (x x)) (x.F (x x))  F ((x.F (x x)) (x.F (x x))) = F (Y F)
  • Thus Y F = F (Y F) = F (F (Y F)) = ...

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 23

Discussion

  • The type rules are a kind of logic for reasoning

about types of programs

  • The tree of judgments we just saw is a kind of proof in this logic that the program has a valid type
  • So the type checking problem is like solving a

jigsaw puzzle

  • Can we apply the rules to a program in such a way as to produce a typing proof?
  • It turns out we can easily decide whether or not we can do this. CMSC 330 24

An Algorithm for Type Checking

(Write this in OCaml!) TypeCheck : type env ™ expression  type

TypeCheck(A, n) = int TypeCheck(A, x) = if x in A then A(x) else fail TypeCheck(A, x:t.e) = let t' = TypeCheck((A, x:t), e) in t  t' TypeCheck(A, e1 e2) = let t1 = TypeCheck(A, e1) in let t2 = TypeCheck(A, e2) in if dom(t1) = t2 then range(t1) else fail

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)