1 / 33

Typed Lambda Calculus

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:

efuru
Download Presentation

Typed Lambda Calculus

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley Prasad

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

  3. Lambda Calculus Review • Grammar: E ::= x variables | E1 E2function application | lx. E function creation • Evaluation: (lx. e) e’®[e’/x]e Prasad

  4. Example 1 (lx.x) ly.y ®ly.y (lx.x) ly.y ® [ly.y/x] x = ly.y Prasad

  5. 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

  6. 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

  7. Question What programming errors can occur in lambda calculus? Prasad

  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 Prasad

  9. 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

  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 Prasad

  11. Examples (lx. + x 1) (lx. + x x) (lx. + x 1) 3 ® 4 (lx. + x 1) (ly.y) ® ? Prasad

  12. 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

  13. 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

  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 + (ly.y) 1 causes a runtime error • Catching these errors before program execution is better Prasad

  15. 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

  16. The Types • We have only two kinds of values • Integers • Functions • Type grammar: t := int | t®t Prasad

  17. Examples of Types int int ® int (int ® int) ® int int ® (int ® int) Prasad

  18. “has type” “it is provable that” Type environment Type Expression Type Judgments Prasad

  19. Examples Prasad

  20. More Examples Prasad

  21. [Var] Type Rule: Variables A variable has the type assumed in the type environment. Prasad

  22. Abstraction A function has type t1®t2 if the function body has type t2 when we assume the argument has type t1. [Abs] Prasad

  23. [App] Application Applying a function of type t1®t2 to an argument of type t1gives a result of type t2 Prasad

  24. Integers An integer has type int [Int] Prasad

  25. Addition Adding two int’s produces an int [Add] Prasad

  26. Example 1 Prasad

  27. Example 2 Prasad

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

  29. [App] Type Checking (Cont.) • Bottom-up: Check that types match in applications Prasad

  30. Structural Equality • “Types match” means “types are equal” • Equality is recursively defined int = int a ® b = c ® d Û a = c Ù b = d Prasad

  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 Prasad

  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 Prasad

  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 Prasad

More Related