Typed Lambda Calculus

1 / 33

# Typed Lambda Calculus - PowerPoint PPT Presentation

Typed Lambda Calculus. Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley. Lecture Outline. Lambda Calculus Review and examples Simple types Type checking Comparison with OO type systems. Lambda Calculus Review. Grammar:

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Typed Lambda Calculus' - efuru

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Typed Lambda Calculus

Profs Aiken and Necula of

Univ. of California at Berkeley

Lecture Outline
• Lambda Calculus
• Review and examples
• Simple types
• Type checking
• Comparison with OO type systems

Lambda Calculus Review
• Grammar:

E ::= x variables

| E1 E2function application

| lx. E function creation

• Evaluation:

(lx. e) e’®[e’/x]e

Example 1

(lx.x) ly.y ®ly.y

(lx.x) ly.y ®

[ly.y/x] x =

ly.y

Example 2

(lx.ly.x) (lz.z) e ®*lz.z

((lx.ly.x) (lz.z)) e ®

([lz.z/x]ly.x) e =

(ly.lz.z) e ®

[e/y]lz.z =

lz.z

Example 3

(lx. x x) (lx. x x) ® (lx. x x) (lx. x x) ® . . .

(lx. x x) (lx. x x) ®

[(l x. x x)/x](x x) =

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

Question

What programming errors can occur in lambda calculus?

Notes
• We can encode anything we like in lambda calculus
• Booleans, integers, objects, if-then-else, recursion, . . .
• But don’t forget that these are encodings
• Akin to programming directly with 0’s and 1’s

Extension
• Grammar:

E ::= x variables

| E1 E2function application

| lx. E function creation

| 0,1,2,… integers

• Evaluation:

(lx. e) e’®[e’/x]e

+ i j ® k where k = i + j

Digression
• There is nothing magic about adding integers and + as constants to the lambda calculus
• We could add any data types and operations we like
• They can be encoded directly in lambda calculus anyway

Examples

(lx. + x 1)

(lx. + x x)

(lx. + x 1) 3 ® 4

(lx. + x 1) (ly.y) ® ?

What Happens?

(lx. + x 1) (ly.y) ® + (ly.y) 1 ® ?

A runtime error; we can’t add to a function

Or no error: If + and 1 are encoded as lambda terms, computation will continue!

Notes
• Assume 1, + are encoded as lambda terms
• Nothing guarantees the encoding of 1 is used as an integer
• E.g., (1 lx.x)
• Evaluation doesn’t know what the encodings are supposed to represent

Back to the Future
• We need to be able to restrict uses of values to appropriate operations
• We need a type system!
• Note that we’d like a type system whether or not + (ly.y) 1 causes a runtime error
• Catching these errors before program execution is better

Typed Lambda Calculus
• Grammar:

E ::= x variables

| E1 E2function application

| lx:t. E function creation

| 0,1,2,… integers

• Evaluation:

(lx:t. e) e’®[e’/x]e

+ i j ® k where k = i + j

The Types
• We have only two kinds of values
• Integers
• Functions
• Type grammar:

t := int | t®t

Examples of Types

int

int ® int

(int ® int) ® int

int ® (int ® int)

“has type”

“it is provable that”

Type environment

Type

Expression

Type Judgments

Examples

[Var]

Type Rule: Variables

A variable has the type assumed in the type environment.

Abstraction

A function has type t1®t2 if the function body has type t2 when we assume the argument has type t1.

[Abs]

[App]

Application

Applying a function of type t1®t2 to an argument of type t1gives a result of type t2

Integers

An integer has type int

[Int]

Adding two int’s produces an int

Example 1

Example 2

Type Checking
• One recursive descent traversal.
• Top-down: Add type declarations of formal parameters to environments

[Abs]

[App]

Type Checking (Cont.)
• Bottom-up: Check that types match in applications

Structural Equality
• “Types match” means “types are equal”
• Equality is recursively defined

int = int

a ® b = c ® d Û a = c Ù b = d

Notes
• In typed lambda calculus:
• Types of all variables are declared
• Types are structured (e.g., int ® int)
• Types are checked for equality
• Many typed languages in this class
• Nearly all typed languages before 1980
• FORTRAN, ALGOL, Pascal, C
• Captures C typing except for casts & coercions

Typed OO Languages
• In many typed object-oriented languages
• Types of all variables are declared
• Types are non-structural (just names)
• Declare all types and type relationships
• Types are checked for subtyping relationships
• What if type declarations are omitted?
• A language with type inference