330 likes | 503 Views
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:
E N D
Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley Prasad
Lecture Outline • Lambda Calculus • Review and examples • Simple types • Type checking • Comparison with OO type systems Prasad
Lambda Calculus Review • Grammar: E ::= x variables | E1 E2function application | lx. E function creation • Evaluation: (lx. e) e’®[e’/x]e Prasad
Example 1 (lx.x) ly.y ®ly.y (lx.x) ly.y ® [ly.y/x] x = ly.y Prasad
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 (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 What programming errors can occur in lambda calculus? Prasad
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 • 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 • 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 (lx. + x 1) (lx. + x x) (lx. + x 1) 3 ® 4 (lx. + x 1) (ly.y) ® ? Prasad
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
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 • 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 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 • We have only two kinds of values • Integers • Functions • Type grammar: t := int | t®t Prasad
Examples of Types int int ® int (int ® int) ® int int ® (int ® int) Prasad
“has type” “it is provable that” Type environment Type Expression Type Judgments Prasad
Examples Prasad
More Examples Prasad
[Var] Type Rule: Variables A variable has the type assumed in the type environment. Prasad
Abstraction A function has type t1®t2 if the function body has type t2 when we assume the argument has type t1. [Abs] Prasad
[App] Application Applying a function of type t1®t2 to an argument of type t1gives a result of type t2 Prasad
Integers An integer has type int [Int] Prasad
Addition Adding two int’s produces an int [Add] Prasad
Example 1 Prasad
Example 2 Prasad
Type Checking • One recursive descent traversal. • Top-down: Add type declarations of formal parameters to environments [Abs] Prasad
[App] Type Checking (Cont.) • Bottom-up: Check that types match in applications Prasad
Structural Equality • “Types match” means “types are equal” • Equality is recursively defined int = int a ® b = c ® d Û a = c Ù b = d Prasad
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 • 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 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