Download presentation
Presentation is loading. Please wait.
Published byCharles Higgins Modified over 8 years ago
1
CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley
2
CS7120 (Prasad)L13-1-Lambda-Opt2 Lecture Outline Lambda Calculus –Review and examples Simple types Type checking Comparison with OO type systems
3
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
4
CS7120 (Prasad)L13-1-Lambda-Opt4 Example 1 ( x.x) y.y y.y ( x.x) y.y [ y.y/x] x = y.y
5
CS7120 (Prasad)L13-1-Lambda-Opt5 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
6
CS7120 (Prasad)L13-1-Lambda-Opt6 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)
7
CS7120 (Prasad)L13-1-Lambda-Opt7 Question What programming errors can occur in lambda calculus?
8
CS7120 (Prasad)L13-1-Lambda-Opt8 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
9
CS7120 (Prasad)L13-1-Lambda-Opt9 Extension Grammar: E ::= x variables | E 1 E 2 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
10
CS7120 (Prasad)L13-1-Lambda-Opt10 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
11
CS7120 (Prasad)L13-1-Lambda-Opt11 Examples ( x. + x 1) ( x. + x x) ( x. + x 1) 3 4 ( x. + x 1) ( y.y) ?
12
CS7120 (Prasad)L13-1-Lambda-Opt12 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!
13
CS7120 (Prasad)L13-1-Lambda-Opt13 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
14
CS7120 (Prasad)L13-1-Lambda-Opt14 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
15
CS7120 (Prasad)L13-1-Lambda-Opt15 Typed Lambda Calculus Grammar: E ::= x variables | E 1 E 2 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
16
CS7120 (Prasad)L13-1-Lambda-Opt16 The Types We have only two kinds of values –Integers –Functions Type grammar: := int |
17
CS7120 (Prasad)L13-1-Lambda-Opt17 Examples of Types int int int (int int) int int (int int)
18
CS7120 (Prasad)L13-1-Lambda-Opt18 Type Judgments Type environment Type Expression “it is provable that” “has type”
19
CS7120 (Prasad)L13-1-Lambda-Opt19 Examples
20
CS7120 (Prasad)L13-1-Lambda-Opt20 More Examples
21
CS7120 (Prasad)L13-1-Lambda-Opt21 Type Rule: Variables A variable has the type assumed in the type environment. [Var]
22
CS7120 (Prasad)L13-1-Lambda-Opt22 Abstraction A function has type 1 2 if the function body has type 2 when we assume the argument has type 1. [Abs]
23
CS7120 (Prasad)L13-1-Lambda-Opt23 Application Applying a function of type 1 2 to an argument of type 1 gives a result of type 2 [App]
24
CS7120 (Prasad)L13-1-Lambda-Opt24 Integers An integer has type int [Int]
25
CS7120 (Prasad)L13-1-Lambda-Opt25 Addition Adding two int’s produces an int [Add]
26
CS7120 (Prasad)L13-1-Lambda-Opt26 Example 1
27
CS7120 (Prasad)L13-1-Lambda-Opt27 Example 2
28
CS7120 (Prasad)L13-1-Lambda-Opt28 Type Checking One recursive descent traversal. Top-down: Add type declarations of formal parameters to environments [Abs]
29
CS7120 (Prasad)L13-1-Lambda-Opt29 Type Checking (Cont.) Bottom-up: Check that types match in applications [App]
30
CS7120 (Prasad)L13-1-Lambda-Opt30 Structural Equality “Types match” means “types are equal” Equality is recursively defined int = int a b = c d a = c b = d
31
CS7120 (Prasad)L13-1-Lambda-Opt31 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
32
CS7120 (Prasad)L13-1-Lambda-Opt32 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
33
CS7120 (Prasad)L13-1-Lambda-Opt33 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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.