typed lambda calculus n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Typed Lambda Calculus PowerPoint Presentation
Download Presentation
Typed Lambda Calculus

Loading in 2 Seconds...

play fullscreen
1 / 33

Typed Lambda Calculus - PowerPoint PPT Presentation


  • 139 Views
  • Uploaded on

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:

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

PowerPoint Slideshow about 'Typed Lambda Calculus' - efuru


An Image/Link below is provided (as is) to download presentation

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

Typed Lambda Calculus

Adapted from Lectures by

Profs Aiken and Necula of

Univ. of California at Berkeley

Prasad

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

Prasad

lambda calculus review
Lambda Calculus Review
  • Grammar:

E ::= x variables

| E1 E2function application

| lx. E function creation

  • Evaluation:

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

Prasad

example 1
Example 1

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

(lx.x) ly.y ®

[ly.y/x] x =

ly.y

Prasad

example 2
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

Prasad

example 3
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)

Prasad

question
Question

What programming errors can occur in lambda calculus?

Prasad

notes
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

Prasad

extension
Extension
  • Grammar:

E ::= x variables

| E1 E2function application

| lx. E function creation

| 0,1,2,… integers

| + addition

  • Evaluation:

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

+ i j ® k where k = i + j

Prasad

digression
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

Prasad

examples
Examples

(lx. + x 1)

(lx. + x x)

(lx. + x 1) 3 ® 4

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

Prasad

what happens
What Happens?

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

Answer: It depends.

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

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

Prasad

notes1
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

Prasad

back to the future
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

Prasad

typed lambda calculus1
Typed Lambda Calculus
  • Grammar:

E ::= x variables

| E1 E2function application

| lx:t. E function creation

| 0,1,2,… integers

| + addition

  • Evaluation:

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

+ i j ® k where k = i + j

Prasad

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

t := int | t®t

Prasad

examples of types
Examples of Types

int

int ® int

(int ® int) ® int

int ® (int ® int)

Prasad

type judgments

“has type”

“it is provable that”

Type environment

Type

Expression

Type Judgments

Prasad

examples1
Examples

Prasad

type rule variables

[Var]

Type Rule: Variables

A variable has the type assumed in the type environment.

Prasad

abstraction
Abstraction

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

[Abs]

Prasad

application

[App]

Application

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

Prasad

integers
Integers

An integer has type int

[Int]

Prasad

addition
Addition

Adding two int’s produces an int

[Add]

Prasad

example 11
Example 1

Prasad

example 21
Example 2

Prasad

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

[Abs]

Prasad

type checking cont

[App]

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

Prasad

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

int = int

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

Prasad

notes2
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

Prasad

typed oo languages
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

Prasad

discussion
Discussion

What about structural types + subtyping?

  • Area of current research
  • Currently no consensus on the right way to combine C-like type systems with typical OO-like type systems

Prasad