the untyped lambda calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y combinator...

66
x = 1 let x = 1 in ... x(1). !x(1) x.set(1) The Untyped Lambda Calculus Ralf Lämmel Programming Language Theory Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & paste or by selection, annotation, or rewording. [Järvi] is in turn based on [Pierce] as the underlying textbook. [Järvi] Slides by J. Järvi: “Programming Languages”, CPSC 604 @ TAMU (2009) [Pierce] B.C. Pierce: Types and Programming Languages, MIT Press, 2002

Upload: others

Post on 24-Jul-2020

14 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

x = 1 let x = 1 in ...

x(1).

!x(1) x.set(1)

The Untyped Lambda CalculusRalf Lämmel

Programming Language Theory

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & paste or by selection, annotation, or rewording. [Järvi] is in turn based on [Pierce] as the underlying textbook.

[Järvi] Slides by J. Järvi: “Programming Languages”, CPSC 604 @ TAMU (2009) [Pierce] B.C. Pierce: Types and Programming Languages, MIT Press, 2002

Page 2: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

What’s the lambda calculus?

• It is the core of functional languages.

• ...

���2

Introduction

Examples

Some lambda terms

x�x .x�f .�g .�x .f (g x)

Compare lambda and, say, Haskell constructs:

�x .M (\x � M)

Aside: where does the � (lambda) come from ?

7 / 36

The identity function

Function composition

Page 3: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

What’s the lambda calculus?

• It is the core of functional languages.

• It is a mathematical system for studying programming languages. ✦ design, specification, implementation, type systems, et al.

• It comes in variations of typing: implicit/explicit/none.

• Formal systems built on top of simply typed lambda calculus: ✦ System F — for studying polymorphism ✦ System F<: — for studying subtyping ✦ ...

���3

Page 4: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Language constructs

���4

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

• Abstract syntax: M ::= x | M M | λx.M

• M is a lambda term. • An infinite set of variables x, y, z, . . . is assumed. • M N is an application.

Function M is applied to the argument N. • λx.M is an abstraction.

The resulting function maps x to M. Lambda functions are anonymous.

Page 5: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise ���5

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

• Church’s thesis:

All intuitively computable functions are λ-definable.

• An established equivalence of notions of computability:

Set of Lambda-definable functions

= Set of Turing-computable functions

Computability theory

Page 6: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Syntax and semantics

• Syntax

!

!

• Evaluation

���6

Summarizing

Summarizing the system

The grammar and (small-step) operational semantics oflambda-calculus

Syntax

t ::=x�x .tt t

v ::=�x .t

Evaluation rules (call-by-name)

t1 � t10

t1 t2 � t10 t2

t � t 0

v t � v t 0

(�x .t) v � [v/x ]t

� is the smallest binary relation onterms satisfying the rules

34 / 36

Summarizing

Summarizing the system

The grammar and (small-step) operational semantics oflambda-calculus

Syntax

t ::=x�x .tt t

v ::=�x .t

Evaluation rules (call-by-name)

t1 � t10

t1 t2 � t10 t2

t � t 0

v t � v t 0

(�x .t) v � [v/x ]t

� is the smallest binary relation onterms satisfying the rules

34 / 36

Summarizing

Summarizing the system

The grammar and (small-step) operational semantics oflambda-calculus

Syntax

t ::=x�x .tt t

v ::=�x .t

Evaluation rules (call-by-name)

t1 � t10

t1 t2 � t10 t2

t � t 0

v t � v t 0

(�x .t) v � [v/x ]t

� is the smallest binary relation onterms satisfying the rules

34 / 36

Terms

Values (normal forms)

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Reduce function position, then reduce argument position,

then apply.

A term that cannot be

reduced further.

Page 7: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Syntactic sugar and conventions

���7

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

• M N1 ...Nk means (...((M N1) N2) ... Nk).

Function application groups from left to right.

• λx.x y means (λx.(x y)).

Function application has higher precedence.

• λx1x2 . . . xk. M means λx1.(λx2.(...(λxk .(M)) . . .))).

Page 8: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Variable binding

���8

• λ is a binding operator: It binds a variable in the scope of the lambda abstraction.

• Examples: ✦ λx.M x is bound (in the lambda abstraction) ✦ λx.x y y is not bound (in the lambda abstraction).

• If a variable occurs in an expression without being bound, then it is called a free occurrence, or a free variable. Other occurrences of variables are called bound.

• A closed term is one without free variable occurrences.

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 9: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Variable binding — precise definition

���9

Introduction

Variable binding — precise definition

FV(M) defines the set of free variables in the term M

FV(x) = {x}FV(MN) = FV(M) � FV(N)

FV(�x .M) = FV(M) \ {x}

Spot free and bound occurrences here:

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

�x .(�y .x y)y

Combinator — a term with no free variables (also closed term)9 / 36

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 10: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Exercise: what are the free and bound variable occurrences in these terms?

���10

Introduction

Variable binding — precise definition

FV(M) defines the set of free variables in the term M

FV(x) = {x}FV(MN) = FV(M) � FV(N)

FV(�x .M) = FV(M) \ {x}

Spot free and bound occurrences here:

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

�x .(�y .x y)y

Combinator — a term with no free variables (also closed term)9 / 36

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Introduction

Variable binding — precise definition

FV(M) defines the set of free variables in the term M

FV(x) = {x}FV(MN) = FV(M) � FV(N)

FV(�x .M) = FV(M) \ {x}

Spot free and bound occurrences here:

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

�x .(�y .x y)y

Combinator — a term with no free variables (also closed term)9 / 36

Page 11: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Substitution and β-equivalence• Computation for the λ-calculus is based on substitution.

• Substitution is defined by the equational axiom:

(λx.M)N = [N/x]M

!

!

• Think of substitution as invoking a function:

★ (λx.M) is the function, ★ N is the argument, ★ Substitution takes care of parameter passing.

���11

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

The terms on both sides are also called β-equivalent.

Redex “→” direction = β-reduction

Page 12: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

α-equivalence and conversion

���12

• Names of bound variable are insignificant.

λx.x defines the same function as λy.y

• Suppose two terms differ only on the names of bound variables.

Then, they are said to be α-equivalent ( =α ).

• Equational axiom:

λx.M = λy.[y/x]M

where y does not appear in M

and substitution applies to free occurrences only.

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Performing such renaming is also

called α-conversion.

Page 13: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Reduction M→N

���13

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

• Computation (→) with the lambda calculus is then a series of

✦ β-reductions, and

✦ (“implicit”) α-conversions.

• Reflexive, transitive closure

• M →* N means M reduces to N in zero or more steps.

Summarizing

Summarizing the system

The grammar and (small-step) operational semantics oflambda-calculus

Syntax

t ::=x�x .tt t

v ::=�x .t

Evaluation rules (call-by-name)

t1 � t10

t1 t2 � t10 t2

t � t 0

v t � v t 0

(�x .t) v � [v/x ]t

� is the smallest binary relation onterms satisfying the rules

34 / 36

Page 14: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Inductive definition of substitution

���14

Introduction

Inductive definition of substitution in � calculus

[N/x ]x = N[N/x ]y = y , y any variable di�erent from x[N/x ](M1 M2) = ([N/x ]M1) ([N/x ]M2)

[N/x ](�x .M) = �x .M[N/x ](�y .M) = �y .([N/x ]M), y not free in N

Alpha renaming can be applied freely, variables drawn from the infinitepool of variable namesExamples:

[z/x ]x[z/x ](�x .x x)

[z/x ](�y .y x)

[z/x ](�z .x z)

12 / 36

Examples

Introduction

Inductive definition of substitution in � calculus

[N/x ]x = N[N/x ]y = y , y any variable di�erent from x[N/x ](M1 M2) = ([N/x ]M1) ([N/x ]M2)

[N/x ](�x .M) = �x .M[N/x ](�y .M) = �y .([N/x ]M), y not free in N

Alpha renaming can be applied freely, variables drawn from the infinitepool of variable namesExamples:

[z/x ]x[z/x ](�x .x x)

[z/x ](�y .y x)

[z/x ](�z .x z)

z�x .x x�y .y z�a.z a

12 / 36

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

If this condition is not met, then

alpha conversion is needed.

Page 15: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Properties of reduction (i.e., semantics)

���15

• How do we select redexes for reduction steps?

• Does the result depend on such a choice?

• Does reduction ultimately terminate with a normal form?

Page 16: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Illustration of different reductions (We assume natural numbers with “+”.)

Option 1

(λf .λx.f (f x)) (λy.y + 1) 2

→ (λx.(λy.y +1) ((λy.y +1) x)) 2

→ (λx.(λy.y +1) (x +1)) 2

→ (λx.(x+1+1)) 2

→ (2+1+1)

→ 4

���16

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Option 2

(λf .λx.f (f x)) (λy.y + 1) 2

→ (λx.(λy.y +1) ((λy.y +1) x)) 2

→ (λy.y +1) ((λy.y +1) 2)

→ ...

→ ...

→ 4

Page 17: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Confluence

• Confluence: evaluation strategy is not significant for final value.

• That is: there is (at most) one normal form of a given expression.

���17

[http://en.wikipedia.org/wiki/Church–Rosser_theorem]

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 18: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Confluence

• M →* N means M reduces to N in zero or more steps.

• Confluence

If M →* N and M →* N’,

then there exists some P

such that N →* P and N’ →* P.

���18

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 19: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Strong normalization property of a calculus with reduction

• Definition:

For every term M there is a normal form N such that M →* N.

• Strong normalization properties for lambda calculi:

✦ Untyped lambda calculus: no

✦ Simply-typed lambda calculus: yes

���19

Introduction

Example

No

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

We do have strong normalization, however, in simply-typed lambdacalculusNote, di�erent evaluation strategies can have di�erent terminationbehavior

call-by-value vs. call-by-nameReduction strategies:

Full beta reduction—reduce anywhereNormal order—reduce the leftmost outermost redexCall by name—normal order, but never reduce inside abstractionsCall by value—reduce outermost redexes only, and reduce the argumentfirst

18 / 36

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 20: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Evaluation/reduction strategies

✦ Full beta reduction

Reduce anywhere.

✦ Applicative order (“reduce the leftmost innermost redex”)

Reduce argument before applying function.

✦ Normal order (“reduce the leftmost outermost redex”)

Apply function before reducing argument.

���20

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Choice of strategy may impact

termination behavior

http://en.wikipedia.org/wiki/Evaluation_strategy

lazy (non-strict)

eager (strict)

Page 21: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Extension vs. encoding

• Typical extensions (giving rise to so-called applied lambda calculi) ✦ Primitive types (numbers, Booleans, ...) ✦ Type constructors (tuples, records, ...) ✦ Recursive functions ✦ Effects (cell, exceptions, ...) ✦ ...

• Many extensions can be encoded in theory in terms of pure lambda calculus, except that such encoding is somewhat tedious.

���21

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 22: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Church Booleans• Encodings of literals

✦ true = λt.λf.t

✦ false = λt.λf.f

• Conditional expression (if) ✦ Expectations

★ test b v w →* v, if b = true

★ test b v w →* w, if b = false ✦ Encoding

★ test = λl.λm.λn.l m n = λl.(λm.(λn.((l m) n)))���22

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 23: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Example reduction

(λl.λm.λn.l m n) true v w

→ (λm.λn.true m n) v w

→ (λn.true v n) w

→ true v w

→ (λt.λf.t) v w

→ (λf.v)w

→ v���23

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 24: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Church pairs

• A Boolean value picks either the 1st or the 2nd value of the pair.

• Construction and projections

✦ pair = λf.λs.λb.b f s

✦ first = λp.p true

✦ second = λp.p false

���24

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 25: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Church numerals• Encodings of numbers

✦ c0 = λs.λz.z ✦ c1 = λs.λz.s z ✦ c2 = λs.λz.s (s z) ✦ c3 = λs.λz.s (s (s z)) ✦ ...

• Encodings of functions on numbers ✦ succ = λn.λs.λz.s (n s z) ✦ plus = λm.λn.λs.λz.m s (n s z) ✦ times = λm.λn.m (plus n) c0 ✦ ...

���25

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

A numeral n is a lambda abstraction that is parameterized by a case for zero, and a

case for succ. In the body, the latter is applied n times to the former. This caters

for primitive recursion.

Page 26: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Recursive functions

• Let us define the factorial function.

• Suppose we had “recursive function definitions”.

f ≡ λn. if n == 0 then 1 else n*f(n - 1)

• Let us do such recursion with anonymous functions.

• Fixed point combinators to the rescue!

���26

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 27: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Fixed points• Consider a function f : X → X.

• A fixed point of f is a value z such that z = f(z).

• A function may have none, one, or multiple fixed points.

• Examples (functions and sets of fixed points):

✦ f(x) = 2x

✦ f(x) = x

✦ f(x) = x + 1

���27

{0}{0,1,...}∅

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 28: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Fixed point combinators

We call Y a fixed-point combinator

if it satisfies the following definitional property:

For all f : X → X it holds that Y f = f (Y f)

���28

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

100(M)$ Question: does such a Y exist?

Page 29: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Defining factorial as a fixed point

• Start from a recursive definition. f ≡ λn. if n == 0 then 1 else n*f(n - 1)

• Eliminate self-reference; receive function as argument. g ≡ λh.λn.if n == 0 then 1 else n*h(n - 1)

★ g takes a function (h) and returns a function. • Define f as a fixed point.

f ≡ Y g

���29

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 30: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Fixed points cont’d

• For example, apply definitional property to factorial g: (Y g) 2 =[Y def. prop.] g (Y g) 2

=[unfold g] (λ h.λ n.if n == 0 then 1 else n*h(n - 1)) (Y g) 2 =[beta reduce] (λ n.if n == 0 then 1 else n*((Y g)(n - 1))) 2 =[beta reduce] if 2 == 0 then 1 else 2*((Y g)(2 - 1)) =[“-” reduce] if 2 == 0 then 1 else 2*((Y g)(1)) =[“if” reduce] 2*((Y g)(1)) = ... = 2

���30

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Apply these steps one more time.

This is as if we had extended evaluation.

Page 31: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

A lambda term for Y• One option: Y = λf .(λx.f (x x))(λx.f (x x))

• Verification of the definitional property: Y g = g (Y g)

• Proof: Y g =[unfold Y] (λf .(λx.f (x x))(λx.f (x x))) g

=[beta reduce] (λx.g(x x))(λx.g(x x)))

=[beta reduce] g ((λx.g(x x)) (λx.g(x x)))

=[fold Y] g (Y g)���31

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Not suitable for applicative order.

Page 32: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

CBV fixed point combinator

���32

lambda(f,apply(

lambda(x,apply(var(f),lambda(y,apply(apply(var(x),var(x)),var(y))))),

lambda(x,apply(var(f),lambda(y,apply(apply(var(x),var(x)),var(y)))))))

Page 33: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

• Summary: The untyped lambda calculus ✦ A concise core of functional programming. ✦ A foundation of computability. ✦ A Prolog model is straightforward.

• Prepping: “Types and Programming Languages” ✦ Chapter 5

• Outlook: ✦ The simply-typed lambda calculus ✦ Polymorphism

���33

Page 34: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

x = 1 let x = 1 in ...

x(1).

!x(1) x.set(1)

The Simply Typed Lambda CalculusRalf Lämmel

Programming Language Theory

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & paste or by selection, annotation, or rewording. [Järvi] is in turn based on [Pierce] as the underlying textbook.

[Järvi] Slides by J. Järvi: “Programming Languages”, CPSC 604 @ TAMU (2009) [Pierce] B.C. Pierce: Types and Programming Languages, MIT Press, 2002

Page 35: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

• Now suppose you want to distinguish values of different types: ✦ Booleans ✦ Numbers ✦ Functions on Booleans ✦ Functions on functions on Booleans ✦ Products, sums of ... ✦ ...

• These types need to be ... ✦ specified in the program, and ✦ checked to be correct.

���35

Towards typed lambda calculus

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 36: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Setting up the simply typed lambda calculus

• Define syntax for types including function types.

• Extend lambda abstractions for explicit types.

• Define typing rules.

• Revise reduction semantics.

• Establish type safety.

• Consider extensions.

���36

There is also a type system for lambda terms

without explicit types, which we do not discuss in this

Page 37: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Revised lambda abstraction

• Lambda abstractions are annotated with types:

λx : T.t

• Grammar of types:

T ::= bool

nat

T→T

���37

We only consider these simple types here for simplicity.

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 38: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Examples

• What are the types of these terms?

✦ λx : bool.x

✦ λf : bool → bool.f x

• Here are the same terms for the untyped calculus:

✦ λx .x

✦ λf.f x

���38

Note that lambda variables are typed explicitly.

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 39: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Meaningless terms

• Some terms diverge.

• Some applications are ill-typed, e.g.:

(λf : bool → bool.f x) true

• Goal: a type system to reject ill-typed terms.

���39

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

... unless we end up showing strong

normalization.

Page 40: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Typing relation with context

���40

Adding types to lambda-calculus

Typing relation with context

Now our typing relation (with context/environment information) is athree-place relation (�, t, T ), written as:

� ⇥ t : T

and read as:Term t has type T in the typing context �

If � = �, it is usually omitted

⇥ t : T

9 / 42

Adding types to lambda-calculus

Typing relation with context

Now our typing relation (with context/environment information) is athree-place relation (�, t, T ), written as:

� ⇥ t : T

and read as:Term t has type T in the typing context �

If � = �, it is usually omitted

⇥ t : T

9 / 42

• A typing context is a sequence of bindings.

• Each binding is a variable-type pair, e.g.: x : T.

• Contexts are composed as in Γ, x : T.

• All variable names are distinct for a given Γ.

• Γ can be omitted if it is empty.

• Γ can be empty for closed terms.

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 41: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Typing rules for simply-typed lambda calculus

���41

Adding types to lambda-calculus

Typing rules for simply-typed lambda calculus

x , y , z , f , g range over variabless, t, u range over termsS , T , U range over types

T-Variablex : T ⇥ �

� ⇤ x : T

T-Abstraction�, x : T ⇤ u : U

� ⇤ �x : T .u : T � U

T-Application� ⇤ t : U � T � ⇤ u : U

� ⇤ t u : T

11 / 42

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 42: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Rules for bool

���42

Adding types to lambda-calculus

Rules for bool

T-True� true : bool

T-False� false : bool

12 / 42

These typing rules illustrate one option to add specific types and their operations to a basic lambda calculus. Basically, we need

to add one rule per operation.

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 43: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Evaluation rules

���43

• Syntax (terms, values, types)

!

• Evaluation rules

!

!

Evaluation rules do not bother with types.

Adding types to lambda-calculus

Evaluation rules

Syntax

t ::=x | v | t tv ::=�x :T .t | true | falseT ::=bool|T �T

t = terms, v = value, T = type

Evaluation rules

t1 � t1�

t1 t2 � t1� t2

t � t �

v t � v t �

(�x :T .t) v � [v/x ]t

� is the smallest binary relationon terms satisfying the rules

Note again that evaluation rules do not bother with types!Type checker’s task to reject programs that try to apply rules formeaningless typesThis corresponds (to some extent) to language implementations

14 / 42

Adding types to lambda-calculus

Evaluation rules

Syntax

t ::=x | v | t tv ::=�x :T .t | true | falseT ::=bool|T �T

t = terms, v = value, T = type

Evaluation rules

t1 � t1�

t1 t2 � t1� t2

t � t �

v t � v t �

(�x :T .t) v � [v/x ]t

� is the smallest binary relationon terms satisfying the rules

Note again that evaluation rules do not bother with types!Type checker’s task to reject programs that try to apply rules formeaningless typesThis corresponds (to some extent) to language implementations

14 / 42

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 44: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Typing derivations

���44

Construct derivations as proofs of terms having a certain type.

Adding types to lambda-calculus

Typing derivations

We can construct derivations (proofs) that explain why a given termhas a certain typeExample: (�f : bool� bool.f false)�g : bool.g has type bool(under empty environment)

f :bool�bool 2 f :bool�bool

f :bool�bool ` f : bool�bool f :bool�bool ` false : bool

f :bool�bool ` f false : bool

` (�f :bool�bool.f false) : (bool�bool)�bool

g :bool 2 g :bool

g :bool ` g : bool

` �g :bool.g : bool�bool

` (�f :bool�bool.f false) �g :bool.g : bool

13 / 42

Adding types to lambda-calculus

Typing rules for simply-typed lambda calculus

x , y , z , f , g range over variabless, t, u range over termsS , T , U range over types

T-Variablex : T ⇥ �

� ⇤ x : T

T-Abstraction�, x : T ⇤ u : U

� ⇤ �x : T .u : T � U

T-Application� ⇤ t : U � T � ⇤ u : U

� ⇤ t u : T

11 / 42

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 45: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Type safety = progress + preservation

���45

Type safety

Type safety

We again define type safety of simply typed lambda-calculus via theprogress and preservation theoremsReminder:

Type safety = progress + preservation

Progress: If t is a closed, well-typed term, then either t is a value, orthere exists some u, such that t � u.Preservation: If � ⇥ t : T and t � u, then � ⇥ u : T

16 / 42

Requires several trivial lemmas (properties) that are omitted here.

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 46: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

A few extensions

���46

• Recursion (fixed point combinator)

• Type annotation (for documentation, abstraction)

• Pairs (as a simple form of type construction)

• Lists (another example of type construction)

• Records (as a first step towards objects)

• Mutable variables (not discussed in this lecture)

• ...

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 47: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Recursion

• A fixed point combinator is definable in the untyped calculus.

• It is not definable in the simply typed version.

✦ A special combinator fix is added to the formal system.

✦ Alternatively, a more powerful type system is needed.

���47

Page 48: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Why is Y not typeable?• Consider again the fixed-point combinator: Y = λf .(λx.f (x x))(λx.f (x x))

• Let us simplify. Y contains self-application: λx. x x

• How could self-application be typeable? λx : ?. x x

���48

The type of x would need to be obviously a function type and the argument type of the same type.

Page 49: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Illustration of fix

���49

iseven : nat → bool

iseven = fix iseven’

!

iseven’: (nat→ bool)→ nat→ bool

iseven’ = λ f:nat → bool. λ x:nat.

if iszero x then true

else if iszero (pred x) then false

else f (pred (pred x))

iseven’ is a generator for the iseven function. Given a function f that equates

with iseven for numbers up to n, iseven’ f equates with iseven an approximation up to n + 2. fix iseven’ extends this to all n.

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 50: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Intuition

• iseven’ ? 0 ⇒ true

• iseven’ ? 1 ⇒ false

• iseven’ ? 2 ⇒ ?

• iseven’ (iseven’ ?) 2 ⇒ true

• iseven’ (iseven’ ?) 3 ⇒ false

• iseven’ (iseven’ ?) 4 ⇒ ?

���50

We think of unfolding the recursive definition.

• fix iseven’ 0 ⇒ true

• fix iseven’ 1 ⇒ false

• fix iseven’ 2 ⇒ true

• fix iseven’ 3 ⇒ false

• fix iseven’ 4 ⇒ true

Page 51: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Recursion in the presence of types

���51

!

Add a primitive fix. t ::= ... | fix t

Aside: general recursion

The fix operator

We discussed the fix point operator (Y-combinator, fix), and showedits definition in untyped lambda calculusJust like self-application, fix cannot be typed in simply-typed lambdacalculusSimple fix: add fix as a primitive

fix (�x : T .t)� [(fix (�x : T .t))/x ] t

t � t �

fix t � fix t �

� ⇥ t : T � T� ⇥ fix t : T

16 / 50

Typing rule

Aside: general recursion

The fix operator

We discussed the fix point operator (Y-combinator, fix), and showedits definition in untyped lambda calculusJust like self-application, fix cannot be typed in simply-typed lambdacalculusSimple fix: add fix as a primitive

fix (�x : T .t)� [(fix (�x : T .t))/x ] t

t � t �

fix t � fix t �

� ⇥ t : T � T� ⇥ fix t : T

16 / 50

Aside: general recursion

The fix operator

We discussed the fix point operator (Y-combinator, fix), and showedits definition in untyped lambda calculusJust like self-application, fix cannot be typed in simply-typed lambdacalculusSimple fix: add fix as a primitive

fix (�x : T .t)� [(fix (�x : T .t))/x ] t

t � t �

fix t � fix t �

� ⇥ t : T � T� ⇥ fix t : T

16 / 50

Evaluation rules

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 52: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Type annotation (ascription)

���52

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

• Syntax:

t ::= ... | t as T

• Typing rule:

!

• Evaluation rules:

Subtyping and other extensions

Ascription

Reminder. Type rules:

� ⇥ t : T� ⇥ t as T : T

Evaluation rules:

t � ut as T � u as T

v as T � v

18 / 40

Subtyping and other extensions

Ascription

Reminder. Type rules:

� ⇥ t : T� ⇥ t as T : T

Evaluation rules:

t � ut as T � u as T

v as T � v

18 / 40

Subtyping and other extensions

Ascription

Reminder. Type rules:

� ⇥ t : T� ⇥ t as T : T

Evaluation rules:

t � ut as T � u as T

v as T � v

18 / 40

Page 53: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

• Summary: The typed lambda calculus ✦ Typing relation carries argument for context. ✦ Recursion requires built-in Y combinator.

• Prepping: “Types and Programming Languages” ✦ Chapters 7 and 11

• Outlook: ✦ Lambda calculi with polymorphism ✦ Object calculi ✦ Process calculi

���53

Page 54: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

x = 1 let x = 1 in ...

x(1).

!x(1) x.set(1)

System FRalf Lämmel

Programming Language Theory

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & paste or by selection, annotation, or rewording. [Järvi] is in turn based on [Pierce] as the underlying textbook.

[Järvi] Slides by J. Järvi: “Programming Languages”, CPSC 604 @ TAMU (2009) [Pierce] B.C. Pierce: Types and Programming Languages, MIT Press, 2002

Page 55: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Polymorphism -- Why?

• What’s the identity function?

• In the simply typed lambda calculus, we need many!

• Examples

✦ λx:bool. x ✦ λx:nat. x ✦ λx:bool→bool. x ✦ λx:bool→nat. x ✦ ...

���55

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

In need of polymorphic functions, i.e., functions that accept many types of arguments.

Page 56: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Kinds of polymorphism

✦ Parametric polymorphism (“all types”)

✦ Bounded polymorphism (“subtypes”)

✦ Ad-hoc polymorphism (“some types”)

✦ Existential types (“exists as opposed to for all”)

���56

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 57: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

System F -- Syntax

���57

Introduction

System-F on one page

Syntaxt ::=x | v | t t | t[T ]

v ::=�x :T .t| ⇥X .tT ::=X |T �T |⇤X .T

Evaluation rules

E-AppFunt1 � t1�

t1 t2 � t1� t2

E-AppArgt � t �

v t � v t �

E-AppAbs(�x :T .t) v � [v/x ]t

E-TypeAppt1 � t1�

t1[T ] � t1�[T ]

E-TypeAppAbs(⇥X .t)[T ] � [T/X ]t

Typing rules

T-Variablex : T ⇥ �

� ⌅ x : T

T-Abstraction�, x : T ⌅ u : U

� ⌅ �x : T .u : T � U

T-Application� ⌅ t : U � T � ⌅ u : U

� ⌅ t u : T

T-TypeAbstraction�,X ⌅ t : T

� ⌅ ⇥X .t : ⇤X .T

T-TypeApplication� ⌅ t : ⇤X .T

� ⌅ t[T1] : [T1/X ]T

7 / 50

Type application

Type abstraction

Polymorphic type

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

System F [Girard72,Reynolds74] = (simply-typed) lambda calculus + type abstraction & application

Type variable Example:

id : ∀X.X →X id =ΛX.λx :X.x

Page 58: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Examples

���58

Term

id =ΛX.λx :X.xid[bool]

id[bool] true

id true

Type

: ∀X.X →X

: bool → bool: bool

type error

In actual programming languages, type application may be implicit.

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 59: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

System F -- Typing rules

���59

Introduction

System-F on one page

Syntaxt ::=x | v | t t | t[T ]

v ::=�x :T .t| ⇥X .tT ::=X |T �T |⇤X .T

Evaluation rules

E-AppFunt1 � t1�

t1 t2 � t1� t2

E-AppArgt � t �

v t � v t �

E-AppAbs(�x :T .t) v � [v/x ]t

E-TypeAppt1 � t1�

t1[T ] � t1�[T ]

E-TypeAppAbs(⇥X .t)[T ] � [T/X ]t

Typing rules

T-Variablex : T ⇥ �

� ⌅ x : T

T-Abstraction�, x : T ⌅ u : U

� ⌅ �x : T .u : T � U

T-Application� ⌅ t : U � T � ⌅ u : U

� ⌅ t u : T

T-TypeAbstraction�,X ⌅ t : T

� ⌅ ⇥X .t : ⇤X .T

T-TypeApplication� ⌅ t : ⇤X .T

� ⌅ t[T1] : [T1/X ]T

7 / 50

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Type variables are held the

context.

Type variables are subject to

alpha conversion.

Example: id : ∀X.X →X id =ΛX.λx :X.x

Page 60: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

System F -- Evaluation rules

���60

Introduction

System-F on one page

Syntaxt ::=x | v | t t | t[T ]

v ::=�x :T .t| ⇥X .tT ::=X |T �T |⇤X .T

Evaluation rules

E-AppFunt1 � t1�

t1 t2 � t1� t2

E-AppArgt � t �

v t � v t �

E-AppAbs(�x :T .t) v � [v/x ]t

E-TypeAppt1 � t1�

t1[T ] � t1�[T ]

E-TypeAppAbs(⇥X .t)[T ] � [T/X ]t

Typing rules

T-Variablex : T ⇥ �

� ⌅ x : T

T-Abstraction�, x : T ⌅ u : U

� ⌅ �x : T .u : T � U

T-Application� ⌅ t : U � T � ⌅ u : U

� ⌅ t u : T

T-TypeAbstraction�,X ⌅ t : T

� ⌅ ⇥X .t : ⇤X .T

T-TypeApplication� ⌅ t : ⇤X .T

� ⌅ t[T1] : [T1/X ]T

7 / 50

Introduction

System-F on one page

Syntaxt ::=x | v | t t | t[T ]

v ::=�x :T .t| ⇥X .tT ::=X |T �T |⇤X .T

Evaluation rules

E-AppFunt1 � t1�

t1 t2 � t1� t2

E-AppArgt � t �

v t � v t �

E-AppAbs(�x :T .t) v � [v/x ]t

E-TypeAppt1 � t1�

t1[T ] � t1�[T ]

E-TypeAppAbs(⇥X .t)[T ] � [T/X ]t

Typing rules

T-Variablex : T ⇥ �

� ⌅ x : T

T-Abstraction�, x : T ⌅ u : U

� ⌅ �x : T .u : T � U

T-Application� ⌅ t : U � T � ⌅ u : U

� ⌅ t u : T

T-TypeAbstraction�,X ⌅ t : T

� ⌅ ⇥X .t : ⇤X .T

T-TypeApplication� ⌅ t : ⇤X .T

� ⌅ t[T1] : [T1/X ]T

7 / 50

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Example: id : ∀X.X →X id =ΛX.λx :X.x

Page 61: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

The doubling function double:∀X.(X →X) → X → X

double=ΛX.λf :X →X.λx :X.f (f x)

���61

• Instantiated with nat double_nat = double [nat] : (nat → nat) → nat → nat

• Instantiated with nat → nat double_nat_arrow_nat = double [nat → nat] : ((nat → nat) → nat → nat) → (nat → nat) → nat → nat

• Invoking double double [nat] (λx : nat.succ (succ x)) 5 →* 9

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 62: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Functions on polymorphic functions• Consider the polymorphic identity function:

id : ∀X. X→X id = ΛX.λx : X.x

• Use id to construct a pair of Boolean and String: pairid : (Bool, String) pairid = (id true, id “true”)

• Abstract over id: pairapply : (∀X. X→X) → (Bool, String) pairapply = λf : ∀X. X→X. (f true, f “true”)

���62

Type application left implicit.

Argument must be polymorphic!

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 63: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Self application

���63

• Not typeable in the simply-typed lambda calculus

λx : ? . x x

• Typeable in System F

selfapp : (∀X.X → X) → (∀X.X → X)

selfapp = λx : ∀X.X → X.x [∀X.X → X] x

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Page 64: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

The fix operator (Y)

���64

• Not typeable in the simply-typed lambda calculus

✦ Extension required

• Typeable in System F.

fix : ∀X.(X → X) → X

• Encodeable in System F with recursive types.

fix = ?

See [TAPL]

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

Aside: general recursion

The fix operator

We discussed the fix point operator (Y-combinator, fix), and showedits definition in untyped lambda calculusJust like self-application, fix cannot be typed in simply-typed lambdacalculusSimple fix: add fix as a primitive

fix (�x : T .t)� [(fix (�x : T .t))/x ] t

t � t �

fix t � fix t �

� ⇥ t : T � T� ⇥ fix t : T

16 / 50

Page 65: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise

Meaning of “all types”

���65

• Predicative polymorphism ✦ X ranges over simple types. ✦ Polymorphic types are “type schemes”. ✦ Type inference is decidable.

• Impredicative polymorphism ✦ X also ranges over polymorphic types. ✦ Type inference is undecidable.

• type:type polymorphism ✦ X ranges over all types, including itself. ✦ Computations on types are expressible. ✦ Type checking is undecidable.

This slide is derived from Jaakko Järvi’s slides for his course ”Programming Languages”, CPSC 604 @ TAMU.

In the type ∀X. ..., we quantify over “all types”.

Not covered by this lecture

Generality is used for selfapp.

Page 66: The Untyped Lambda Calculuslaemmel/plt/slides/lambda.pdf · (x .y )(y .y ) x .(y .xy)y Combinator —atermwithnofreevariables(alsoclosed term) 9/36 ... Apply function before reducing

© Ralf Lämmel, 2009-2012 unless noted otherwise ���66

End of Lambda slides