cs7120 (prasad)l13-1-lambda-opt1 typed lambda calculus adapted from lectures by profs aiken and...

33
CS7120 (Prasad) L13-1-Lambda-Opt 1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

Upload: charles-higgins

Post on 19-Jan-2018

225 views

Category:

Documents


0 download

DESCRIPTION

CS7120 (Prasad)L13-1-Lambda-Opt3 Lambda Calculus Review Grammar: E ::= x variables | E 1 E 2 function application | x. E function creation Evaluation: ( x. e) e’  [e’/x]e

TRANSCRIPT

Page 1: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 1

Typed Lambda Calculus

Adapted from Lectures byProfs Aiken and Necula of

Univ. of California at Berkeley

Page 2: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 2

Lecture Outline

• Lambda Calculus – Review and examples

• Simple types

• Type checking

• Comparison with OO type systems

Page 3: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 3

Lambda Calculus Review

• Grammar:

E ::= x variables | E1 E2 function application

| x. E function creation

• Evaluation:(x. e) e’ [e’/x]e

Page 4: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 4

Example 1

(x.x) y.y y.y

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

Page 5: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 5

Example 2

(x.y.x) (z.z) e * z.z

((x.y.x) (z.z)) e ([z.z/x]y.x) e (y.z.z) e [e/y]z.z =z.z

Page 6: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 6

Example 3

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

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

Page 7: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 7

Question

What programming errors can occur in lambda calculus?

Page 8: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 8

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

Page 9: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 9

Extension

• Grammar: E ::= x variables | E1 E2 function application

| x. E function creation | 0,1,2,… integers | + addition

• Evaluation:(x. e) e’ [e’/x]e+ i j k where k = i + j

Page 10: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 10

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

Page 11: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 11

Examples

(x. + x 1)

(x. + x x)

(x. + x 1) 3 4

(x. + x 1) (y.y) ?

Page 12: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 12

What Happens?

(x. + x 1) (y.y) + (y.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!

Page 13: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 13

Notes

• Assume 1, + are encoded as lambda terms

• Nothing guarantees the encoding of 1 is used as an integer– E.g., (1 x.x)– Evaluation doesn’t know what the encodings

are supposed to represent

Page 14: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 14

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 + (y.y) 1 causes a runtime error– Catching these errors before program

execution is better

Page 15: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 15

Typed Lambda Calculus

• Grammar: E ::= x variables | E1 E2 function application

| x:. E function creation | 0,1,2,… integers | + addition

• Evaluation:(x:. e) e’ [e’/x]e+ i j k where k = i + j

Page 16: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 16

The Types

• We have only two kinds of values– Integers– Functions

• Type grammar: := int |

Page 17: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 17

Examples of Types

int

int int

(int int) int

int (int int)

Page 18: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 18

Type Judgments

A e : x Type environment Type

Expression

“it is provable that” “has type”

Page 19: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 19

Examples

x:int.x : int int

1 2 1 1 2 1 2x: . y: .x y : ( )

Page 20: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 20

More Examples

x:int. y:int.x : int int int

t:int. e:int. b:int int int.b t e :int int (int int int) int

x:int. y:int.y : int int int

Page 21: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 21

Type Rule: Variables

A variable has the type assumed in the type environment.

A, x: x: x [Var]

Page 22: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 22

Abstraction

A function has type 1 2 if the function body has type 2 when we assume the argument has type 1.

1 2

1 1 2

A, x: e :A x: .e :

x [Abs]

Page 23: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 23

Application

Applying a function of type 1 2 to an argument of type 1 gives a result of type 2

1 1 2

2 1

1 2 2

A e : A e : A e e :

[App]

Page 24: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 24

Integers

An integer has type int

A i:intx [Int]

Page 25: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 25

Addition

Adding two int’s produces an int

1

2

1 2

A e :intA e :int

A + e e :int

xx

x[Add]

Page 26: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 26

Example 1

x:int x:int x:int.x : int int

Page 27: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 27

Example 2

x:int x:int x:int 1: intx:int + x 1: int 3:int x:int.+ x 1: int int

( x:int.+ x 1) 3: int

x

Page 28: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 28

Type Checking

• One recursive descent traversal.

• Top-down: Add type declarations of formal parameters to environments

1 2

1 1 2

A, x: e :A x: .e :

x [Abs]

Page 29: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 29

Type Checking (Cont.)

• Bottom-up: Check that types match in applications

1 1 2

2 1

1 2 2

A e : A e : A e e :

[App]

Page 30: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 30

Structural Equality

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

int = inta b = c d a = c b = d

Page 31: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 31

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

Page 32: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 32

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

Page 33: CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley

CS7120 (Prasad) L13-1-Lambda-Opt 33

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