algol and haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1...

75
Lambda Calculus Oded Padon & Mooly Sagiv (original slides by Kathleen Fisher, John Mitchell, Shachar Itzhaky, S. Tanimoto , Stephen A. Edwards) Benjamin Pierce Types and Programming Languages http://www.cs.cornell.edu/courses/cs3110/2008fa/recitations/rec26.html

Upload: others

Post on 12-Aug-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Lambda Calculus

Oded Padon & Mooly Sagiv

(original slides by Kathleen Fisher, John Mitchell, Shachar Itzhaky, S. Tanimoto , Stephen A. Edwards)

Benjamin Pierce

Types and Programming Languages

http://www.cs.cornell.edu/courses/cs3110/2008fa/recitations/rec26.html

Page 2: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Computation Models

• Turing Machines

• Wang Machines

• Counter Programs

• Lambda Calculus

Page 3: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Alonzo Church

1903–1995

Professor at Princeton (1929–1967)

and UCLA (1967–1990)

Invented the Lambda Calculus

Notable students:

• Alan Turing

• Stephen Cole Kleene

• Martin Davis

• Barkley Roser

• Dana Scott

• Leon Hankin

• Michael Rabin

• John Kemeny

Page 4: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Simple Example factorial

int fact(int x) {

if (x==0) return 1; else return n * fact(n-1);

}

C

No state mutations

fact x : int : int =

if x=0 then 1 else n * fact n-1

MLNo state mutations

Expression only

Page 5: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Another Example Compose

comp f: int int g: int int: int int =

fun x g (f x)

comp fun x x + 1 fun x x + 1

comp fun x x + 1 fun x x + 1 2

comp fun x 2 *x fun y 4 * y

comp fun x 2 *x fun y 4 * y 3

Page 6: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

What is calculus

• A complete computational model

• An assembly language for functional programming

– Powerful

– Concise

– Counterintuitive

• Can explain many interesting PL features

Page 7: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Basics

• Repetitive expressions can be compactly represented using functional abstraction

• Example:

– (5 * 4* 3 * 2 * 1) + (7 * 6 * 5 * 4 * 3 * 2 *1) =

– factorial(5) + factorial(7)

– factorial(n) = if n = 0 then 1 else n * factorial(n-1)

– factorial= n. if n = 0 then 1 else n * factorial(n-1)

– factorial= n. if n = 0 then 1 else n * apply (factorial(n-1))

Page 8: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Untyped Lambda Calculus

t ::= terms

x variable

x. t abstraction

t t application

Syntactic Conventions

• Applications associates to left

e1 e2 e3 (e1 e2) e3

• The body of abstraction extends as far as possible

• x. y. x y x x. (y. (x y) x)

Terms can be represented as abstract syntax trees

apply

apply

e1 e2 e3

Page 9: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Untyped Lambda Calculus

t ::= terms

x variable

x. t abstraction

t t application

Syntactic Conventions

• Applications associates to left

e1 e2 e3 (e1 e2) e3

• The body of abstraction extends as far

as possible

• x. y. x y x x. (y. (x y) x)

Terms can be represented as abstract syntax trees

x

y

apply

apply

x y x

Page 10: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Untyped Lambda Calculus++

t ::= terms

x variable

x. t abstraction

t t application

number number

op t1 t2 … tk built-in operator op t1, t2, …, tk

true true

false false

Page 11: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Lambda Calculus in Python

( x. x) y (lambda x: x) (y)

Page 12: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Lambda Calculus in ML

( x. x) y (fun x x) (y)

Page 13: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Functions on int

x. succ x

x

apply

succ x

(x. succ x) 0x

apply

succ x

apply

0

1

Define a function which computes 2?

Page 14: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Lambda Expressions

• Function application are written in prefix form

– “Add 4 to 5”

• Evaluation: Select a “redex” and apply function

• Different evaluation orders

Simon Peyton Jones, The Implementation of Functional

Programming Languages, Prentice-Hall, 1987.

(+ 4 5)

(+(* 5 6) (* 8 3)) (+ 30 (* 8 3))

(+( 30 24)

54

(+(* 5 6) (* 8 3)) (+(* 5 6 ) 24)

(+ 30 24)

54

Page 15: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Function Application and Currying• Function application written as juxtaposition

f x

• Every function has exactly one argument

– Multiple arguments can be simulated by Curring

– (+ x) is a function which adds x to its argument

– What is +4?

– What is +

(+ 4 5) = (+4) 5 9

Page 16: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Lambda Abstraction

• The only other thing in the lambda calculus is lambda abstraction

– a notation for defining unnamed functions

(x. + x 1)

The function of x that adds 1 to x

Page 17: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Boolean Expressions

• (if true x y) x

• (if false x y) y

• Boolean negation?

• Boolean conjunction

• Boolean disjunction

Page 18: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Beta Reduction

• Substitute formal by actual arguments

• x can occur multiple times

• Or not at all

(x. + x 1) 4 ( + 4 1)

5

(x. + x x) 4 ( + 4 4)

(x. + 1 6) 4 ( + 1 6)

8

7

Page 19: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

• Fuzzy but mechanical

• Extra parenthesis and tree-notations can help

Beta-Reduction(Cont)

=(x.y. + x y) 3 4

((x.(y. ((+ x) y))) 3) 4

(y. ((+ 3) y)) 4

((+ 3) 4)

7

Page 20: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

• Arbitrary functions as arguments/return

Functions can be arguments

(f. f 3) (x. + x 1)

4

(x. + x 1) 3

(x. + x 1) 3

(+ 3 1)

Page 21: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Free and Bound Variables

• x is bound

• y is free

– Determined in the enclosed context

– Like global variables

• Both x and y are free in (+ x y)

x. (+ x y) 2

Page 22: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Ill-Formed Beta Reductions

• Sometimes beta-reductions cannot be applied

• Like runtime error/undefined semantics in imperative programs– (1 5)

– (x 5 y. y)

– (+ true 2)

– (test 5 1 2)

• Later will refine lambda calculus to avoid such terms by the compiler

Page 23: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Beta Reduction Formally

Page 24: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Substitution

• Replace a term by a term

– x + ((x + 2) * y)[x ↦ 3, y ↦ 7] = ?

– x + ((x + 2) * y)[x ↦ z + 2] = ?

– x + ((x + 2) * y)[t ↦ z + 2] = ?x + ((x + 2) * y)[x ↦ y]

– More tricky in programming languages

– Why?

Page 25: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Free vs. Bound Variables

• An occurrence of x is bound in t if it occurs in x. t – otherwise it is free– x is a binder

• Examples– Id= x. x – y. x (y z)– z. x. y. x (y z)– (x. x) x

FV: t 2Var is the set free variables of t

FV(x) = {x}

FV( x. t) = FV(t) – {x}

FV (t1 t2) = FV(t1) FV(t2)

Page 26: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Beta-Reduction

[x↦s] x = s

[x↦s] y = y if y x

[x↦s] (x. t1) = x. t1

[x↦s] (y. t1) = y. [x ↦s] t1 if y x and yFV(s)

[x↦s] (t1 t2) = ([x↦s] t1) ([x↦s] t2)

( x. t1) t2 [x ↦t2] t1 (-reduction)

y

t1

[x↦s]y

[x↦s] t1

Page 27: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Beta-Reduction

[x↦s] x = s

[x↦s] y = y if y x

[x↦s] (y. t1) = y. [x ↦s] t1 if y x and yFV(s)

[x↦s] (t1 t2) = ([x↦s] t1) ([x↦s] t2)

( x. t1) t2 [x ↦t2] t1 (-reduction)

apply

t1

[x↦s]

t2 [x↦s] t1

apply

[x↦s] t2

Page 28: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Example Beta-Reduction

( x. t1) t2 [x ↦ t2] t1 (-reduction)

( x. x) y y

redex

apply

x

x y x

[x ↦ y]

y

Page 29: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Example Beta-Reduction (ex 2)

( x. t1) t2 [x ↦ t2] t1 (-reduction)

( x. x ( x. x) ) (u r)

redex

x

apply

x

x

apply

apply

u r

x

apply

u r

x ↦

apply

x

x

x

apply

x

x

x

apply

u r

x ↦

apply

u r

x ↦

Page 30: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Example Beta-Reduction (ex 2) (cont)

( x. t1) t2 [x ↦ t2] t1 (-reduction)

( x. x ( x. x) ) (u r)

redex

apply

x

x

apply

u r

x ↦

apply

u r

u r ( x. x)

apply

x

x

apply

u r

Page 31: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

31

Alpha- Renaming

Alpha conversion:

Renaming of a bound variable and its bound occurrences

x.y.y x.z.z

Simplifies terms

Page 32: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Example Beta-Reduction (ex 2)with renaming

( x. t1) t2 [x ↦ t2] t1 (-reduction)

( x. x ( x. x) ) (u r)

redex

Page 33: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Example Beta-Reduction (ex 3)

( x. t1) t2 [x ↦ t2] t1 (-reduction)

( x. (w. x w)) (y z) w. y z w

redex

x

apply

w

apply

apply

y z

x

w

apply

y z

x ↦

apply

wx

w

apply

wx

wapply

y z

x ↦

Page 34: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Simple Exercise

• Adding scopes to expressions

• Proposed syntax “let x = t1 in t2”

• Informal meaning:

– all the occurrences of x in t2 are replaced by t1

• Example: let a = x. (w. x w) in a a =

• How can we simulate let?

Page 35: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Divergence

( x. t1) t2 [x ↦t2] t1 (-reduction)

( x.y) (( x.(x x)) ( x.(x x)))

Apply

x

y

Apply

x

Apply

x x

x

Apply

x x

Page 36: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Divergence

( x. t1) t2 [x ↦t2] t1 (-reduction)

Apply

x

Apply

x x

x

Apply

x x

Apply

x

Apply

x x

x

Apply

x x

( x.(x x)) ( x.(x x))

Page 37: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Different Evaluation Orders

( x. t1) t2 [x ↦t2] t1 (-reduction)

( x.y) (( x.(x x)) ( x.(x x)))

Apply

x

y

Apply

x

Apply

x x

x

Apply

x x

Apply

x

y

Apply

x

Apply

x x

x

Apply

x x

Page 38: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Different Evaluation Orders

( x. t1) t2 [x ↦t2] t1 (-reduction)

( x.y) (( x.(x x)) ( x.(x x)))

Apply

x

y

Apply

x

Apply

x x

x

Apply

x x

y

Page 39: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Different Evaluation Orders

( x. t1) t2 [x ↦t2] t1 (-reduction)

( x.y) (( x.(x x)) ( x.(x x)))

Apply

x

y

Apply

x

Apply

x x

x

Apply

x x

def f():

while True: pass

def g(x):

return 2

print g(f())

Page 40: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Different Evaluation Orders

( x. t1) t2 [x ↦t2] t1 (-reduction)

( x. x) ((x. x) (z. (x. x) z)) id (id (z. id z))

Apply

x

x

Apply

z

Apply

z

x

x

x

x

Page 41: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Order of Evaluation

• Full-beta-reduction– All possible orders

• Applicative order call by value (Eager)– Left to right– Fully evaluate arguments before function

• Normal order– The leftmost, outermost redex is always reduced first

• Call by name– Evaluate arguments as needed

• Call by need– Evaluate arguments as needed and store for subsequent usages– Implemented in Haskel

Page 42: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Different Evaluation Orders( x. y. ( z. z) y) (( u. u) ( w. w))

Apply

x Apply

u

u

w

w

y

Apply

z

z

y

Page 43: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Call By Value( x. y. ( z. z) y) (( u. u) ( w. w))

Apply

x Apply

u

u

w

w

y

Apply

z

z

y

Apply

x w

wy

Apply

z

z

y

y

Apply

z

z

y ⇏

Page 44: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Call By Name (Lazy)( x. y. ( z. z) y) (( u. u) ( w. w))

Apply

x Apply

u

u

w

w

y

Apply

z

z

y

y

Apply

z

z

y ⇏

Page 45: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Normal Order( x. y. ( z. z) y) (( u. u) ( w. w))

Apply

x Apply

u

u

w

w

y

Apply

z

z

y

y

Apply

z

z

y

y

y⇏

Page 46: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

v ::= values

x. t abstraction values

other values

Call-by-value Small-stepOperational Semantics

t ::= terms

x variable

x. t abstraction

t t application

( x. t1) v2 [x ↦v2] t1 (E-AppAbs)

t1 t’1

t1 t2 t’1 t2(E-APPL1)

t2 t’2v1 t2 v1 t’2

(E-APPL2)

Page 47: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Call By Value( x. y. ( z. z) y) (( u. u) ( w. w))

Apply

x Apply

u

u

w

w

y

Apply

z

z

y

Apply

x w

wy

Apply

z

z

y

y

Apply

z

z

y ⇏

Page 48: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Call By Value OS( x. y. ( z. z) y) (( u. u) ( w. w))

Apply

u

u

w

w

w

w

E-AppAbs

t1

v2

Page 49: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Call By Value OS (2)( x. y. ( z. z) y) (( u. u) ( w. w))

Apply

u

u

w

w

w

w

v1 t2

Apply

x Apply

u

u

w

w

y

Apply

z

z

y

Apply

x

y

Apply

z

z

y

w

w

E-Appl2

Page 50: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Call By Value OS (3)( x. y. ( z. z) y) (( u. u) ( w. w))

t1v2

Apply

x

y

Apply

z

z

y

w

w

y

Apply

z

z

y

EAppAbs

Page 51: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

v ::= values

x. t abstraction values

other values

Call-by-value big-stepOperational Semantics

t ::= terms

x variable

x. t abstraction

t t application

x. t x. t (V-Value)

t1 x. t3 t2 v1 [x ↦v1] t3 v2

t1 t2 v2

(V-App)

Page 52: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

A Pseudocode for call-by value interpreter

Lambda eval(Lambda t) {

switch(t) {

case t= x. t1: // A value

return t

case t = t1 t2:

Lambda temp = eval(t1);

assert temp = x. t3;

Lambda v1 = eval(t2) ; // v1 must be a valuereturn eval([x v1] t3) ;

default: assert false;

}

}

Page 53: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Programming in Calculus

• Functions with multiple arguments

• Simulating values

– Tuples

– Booleans

– Numerics

• Recursion

Page 54: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Programming in the Multiple arguments

f= (x, y). s

-> Currying

f= x. y. s

f v w = ((x. y.s) v) w (y.[x ↦v]s) w [x ↦v] [y ↦w] s(f v) w =

((x. y. x*x+y*y) 3) 4 (y.[x ↦3] x*x+y*y) 4 =

3*3+4*4

(y.3*3+y*y) 4

[y ↦4] 3*3+y*y =

Page 55: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Simulating Booleans

• tru = t. f. t

• fls = t. f. f

• test = l. m. n. l m n

Page 56: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Showing that test is correct

• tru = t. f. t fls = t. f. f

• test = l. m. n. l m n

• test tru then else = (l. m. n. l m n) (t. f. t) then else

t

m

n

Apply

Apply

m

n

Apply Apply

f

t

then else

m

n

Apply

Apply

m

n

l

l

Apply

Apply Apply

f

t

tthen else

value value

Page 57: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Showing that test is correct(2)

• tru = t. f. t fls = t. f. f

• test = l. m. n. l m n

• test tru then else = (l. m. n. l m n) (t. f. t) then else

t

m

n

Apply

Apply

m

n

Apply Apply

f

t

then else

t

n

Apply

Apply

then

n

Apply

f

t

else

Page 58: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Showing that test is correct(3)

• tru = t. f. t fls = t. f. f

• test = l. m. n. l m n

• test tru then else = (l. m. n. l m n) (t. f. t) then else

t

n

Apply

Apply

then

n

Apply

f

t

else

t

Apply

Apply

then

else

f

t

Page 59: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Showing that test is correct(4)

• tru = t. f. t fls = t. f. f

• test = l. m. n. l m n

• test tru then else = (l. m. n. l m n) (t. f. t) then else

t

Apply

Apply

then

else

f

t

Apply

elsethen

f

Page 60: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Showing that test is correct(5)

• tru = t. f. t fls = t. f. f

• test = l. m. n. l m n

• test tru then else = (l. m. n. l m n) (t. f. t) then else

Apply

elsethen

f

then

Page 61: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Showing that test is correct

• tru = t. f. t

• fls = t. f. f

• test = l. m. n. l m n

test tru then else = (l. m. n. l m n) (t. f. t) then else

(m. n. (t. f. t) m n) then else

(n. (t. f. t) then n) else

(t. f. t) then else

(f. then) else

then

Page 62: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Simulating Tests

• tru = t. f. t

• fls = t. f. f

• test = l. m. n. l m n

• test tru then else = (l. m. n. l m n) (t. f. t)

• test fls then else = (l. m. n. l m n) (t. f. f)

Page 63: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Programming in Booleans

• tru = t. f. t

• fls = t. f. f

• test = l. m. n. l m n

• test tru then else = (l. m. n. l m n) (t. f. t)

• test fls then else = (l. m. n. l m n) (t. f. f)

• and = b. b’. test b b’ fls

• or = ?

• not = ?

Page 64: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Church Numerals

• c0 = s. z. z• c1 = s. z. s z• c2 = s. z. s (s z)• c3 = s. z. s (s (s z))• cn = s. z. s (s …(s z)))

Page 65: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

The Successor Function

• succ = n. s. z. s (n s z)

succ c0 = (n. s. z. s (n s z)) (s1. z1. z1)

s. z. s ((s1. z1. z1) s z)

s. z. s ((z1. z1) z)

s. z. s z

Page 66: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

The Successor Function (Cont)

• succ = n. s. z. s (n s z)

(c2 succ) c0

Page 67: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Addition

• plus = m. n. s. z. m s (n s z)

Page 68: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Multiplication

• times = m. n. m (plus n) c0

Page 69: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Combinators

• A combinator is a function in the Lambda Calculus having no free variables

• Examples– x. x is a combinator– x. y. (x y) is a combinator– x. y. (x z) is not a combinator

• Combinators can serve nicely as modular building blocks for more complex expressions

• The Church numerals and simulated Booleans are examples of useful combinators

Page 70: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Recursion

• Where is recursion in the lambda calculus?

• Cannot defineFAC = (test (= n 0) 1 (* n (FAC (- n 1))))

• Even let does not help

Page 71: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Fixpoints

• We say X is a fixpoint of a function F if F(X)= X

• Examples

– F = x.x

– F = x.2

– F = x.(+ 1 x)

• FAC is a fixpoint of the non-recursive equation

– H=f.n. (test (= n 0) 1 (* n (f (- n 1))))

• Compute FAC(n) from H using beta reductions

Page 72: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

The Y-combinator

• FAC is a fixpoint of the non-recursive equation– H=f.n. (test (= n 0) 1 (* n (f (- n 1))))

• A function Y which computes FAC from H– FAC = Y H

– FAC = H FAC

– Y H = H (Y H)

– Y is the function which that takes a function f and applies f (f (… (f …)))

Page 73: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

The Y-combinator• FAC is a fixpoint of the non-recursive equation

– H=f.n. (test (= n 0) 1 (* n (f (- n 1))))

• A function Y which computes FAC from H– FAC = Y H– FAC = H FAC– Y H = H (Y H)

FAC 1 =

Y H 1 = H (Y H) 1

= (f.n. test (= n 0) 1 (* n (f (- n 1)))) (Y H) 1

(n. test (= n 0) 1 (* n (Y H)(- n 1)))) 1

test (= 1 0) 1 (* 1 (Y H) (- 1 1))) * 1 (Y H) 0

= * 1 (H (Y H)) 0 * 1 ( (f.n. test (= n 0) 1 (* n (f (- n 1))) (Y H)) 0 =

* 1 (n. test (= n 0) 1 (* n ((Y H) (- n 1)))) 0

*

* 1 (test (= 0 0) 1 (* 0 ((Y H) (- 0 1)))) * * 1 1 1

Page 74: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

The Y-combinator

• omega= (x. x x) (x. x x)

• Recursion can be simulated

–Y = x . (y. x (y y)) (y. x (y y))–Y f * “f (Y f)”

Page 75: Algol and Haskellmsagiv/courses/pl18/lambda1.pdf · 2018-04-23 · fun x g (f x) comp fun x x + 1 fun x x + 1 comp fun x x + 1 fun x x + 1 2 comp fun x 2 *x fun y 4 * y comp fun x

Summary: Lambda Calculus

• Powerful• The ultimate assembly language• Useful to illustrate ideas• But can be counterintuitive• Usually extended with useful syntactic sugars• Other calculi exist

– pi-calculus– object calculus– mobile ambients– …