The Lambda Calculus

The Lambda Calculus

The Lambda Calculus Stephen A. Edwards Columbia University Fall 2012 Lambda Expressions Function application written in prefix form. “Add four and five” is ( 4 5) Å Evaluation: select a redex and evaluate it: ( ( 5 6) ( 8 3)) ( 30 ( 8 3)) Å ¤ ¤ ! Å ¤ ( 30 24) ! Å 54 ! Often more than one way to proceed: ( ( 5 6) ( 8 3)) ( ( 5 6) 24) Å ¤ ¤ ! Å ¤ ( 30 24) ! Å 54 ! Simon Peyton Jones, The Implementation of Functional Programming Languages, Prentice-Hall, 1987. Function Application and Currying Function application is written as juxtaposition: f x Every function has exactly one argument. Multiple-argument functions, e.g., , are represented by currying, named after Haskell Å Brooks Curry (1900–1982). So, ( x) Å is the function that adds x to its argument. Function application associates left-to-right: ( 3 4) (( 3) 4) Å Æ Å 7 ! Lambda Abstraction The only other thing in the lambda calculus is lambda abstraction: a notation for defining unnamed functions. (¸x . x 1) Å ( ¸ x . x 1 ) Å """""" That function of x that adds x to 1 The Syntax of the Lambda Calculus expr ::= expr expr ¸ variable . expr j constant j variable j (expr) j Constants are numbers and built-in functions; variables are identifiers. Function application binds more tightly than ¸: ¸x.f g x ¡¸x.(f g)x¢ Æ Beta-Reduction Evaluation of a lambda abstraction—beta-reduction—is just substitution: (¸x . x 1) 4 ( 4 1) Å ! Å 5 ! The argument may appear more than once (¸x . x x) 4 ( 4 4) Å ! Å 8 ! or not at all (¸x . 3) 5 3 ! Beta-Reduction Fussy, but mechanical. Extra parentheses may help. µ³ ³ ´´ ¶ (¸x . ¸y . x y) 3 4 ¸x . ¸y . ¡( x) y¢ 3 4 Å Æ Å µ ¶ ¸y . ¡( 3) y¢ 4 ! Å ¡( 3) 4¢ ! Å 7 ! Functions may be arguments (¸f . f 3) (¸x . x 1) (¸x . x 1) 3 Å ! Å ( 3 1) ! Å 4 ! Free and Bound Variables (¸x . x y) 4 Å Here, x is like a function argument but y is like a global variable. Technically, x occurs bound and y occurs free in (¸x . x y) Å However, both x and y occur free in ( x y) Å Beta-Reduction More Formally (¸x . E) F E 0 !¯ where E 0 is obtained from E by replacing every instance of x that appears free in E with F . The definition of free and bound mean variables have scopes. Only the rightmost x appears free in (¸x . ( x 1)) x 3 Å ¡ so (¸x .(¸x . ( x 1)) x 3) 9 (¸ x . ( x 1)) 9 3 Å ¡ ! Å ¡ ( 9 1) 3 !Å ¡ 8 3 !Å 11 ! Another Example ³ ´ ³ ´ ¸x . ¸y . x ¡(¸x . x 3) y¢ 5 6 ¸y . 5 ¡(¸x . x 3) y¢ 6 Å ¡ ! Å ¡ 5 ¡(¸x . x 3) 6¢ !Å ¡ 5 ( 6 3) !Å ¡ 5 3 !Å 8 ! Alpha-Conversion One way to confuse yourself less is to do ®-conversion: renaming a ¸ argument and its bound variables. Formal parameters are only names: they are correct if they are consistent. (¸x .(¸x . ( x 1)) x 3) 9 (¸x .(¸y . ( y 1)) x 3) 9 Å ¡ $ Å ¡ ((¸y . ( y 1)) 9 3) ! Å ¡ ( ( 9 1) 3) ! Å ¡ ( 8 3) ! Å 11 ! You’ve probably done this before in C or Java: int add(int x, int y) int add(int a, int b) { { return x + y; return a + b; } $ } Beta-Abstraction and Eta-Conversion Running ¯-reduction in reverse, leaving the “meaning” of a lambda expression unchanged, is called beta abstraction: 4 1 (¸x . x 1) 4 Å Ã Å Eta-conversion is another type of conversion that leaves “meaning” unchanged: (¸x . 1 x) ( 1) Å $´ Å Formally, if F is a function in which x does not occur free, (¸x . F x) F $´ int f(int y) { ... } int g(int x){ return f(x); } g(w); can be replaced with f(w) à Reduction Order The order in which you reduce things can matter. (¸x . ¸y . y) ¡(¸z . z z)(¸z . z z)¢ Two things can be reduced: (¸z . z z)(¸z . z z) (¸x . ¸y . y)( ) ¢¢¢ However, (¸z . z z)(¸z . z z) (¸z . z z)(¸z . z z) ! (¸x . ¸y . y)( ) (¸y . y) ¢¢¢ ! Normal Form A lambda expression that cannot be ¯-reduced is in normal form. Thus, ¸y . y is the normal form of (¸x . ¸y . y) ¡(¸z . z z)(¸z . z z)¢ Not everything has a normal form. E.g., (¸z . z z)(¸z . z z) can only be reduced to itself, so it never produces an non-reducible expression. Normal Form Can a lambda expression have more than one normal form? Church-Rosser Theorem I: If E E , then there ex- 1 $ 2 ists an expression E such that E E and E E. 1 ! 2 ! Corollary. No expression may have two distinct normal forms. Proof. Assume E and E are distinct normal forms for E: E E 1 2 $ 1 and E E . So E E and by the Church-Rosser Theorem I, there $ 2 1 $ 2 must exist an F such that E F and E F . However, since E 1 ! 2 ! 1 and E are in normal form, E F E , a contradiction. 2 1 Æ Æ 2 Normal-Order Reduction Not all expressions have normal forms, but is there a reliable way to find the normal form if it exists? Church-Rosser Theorem II: If E E and E is in normal form, 1 ! 2 2 then there exists a normal order reduction sequence from E1 to E2. Normal order reduction: reduce the leftmost outermost redex. Normal-Order Reduction µ³ ´ ¶ ¸x . ¡(¸w . ¸z . w z) 1¢ ¡(¸x . x x)(¸x . x x)¢ ¡(¸y . y 1) ( 2 3)¢ Å Å Å leftmost outermost ¸x ¸y leftmost innermost ¸x ¸x 3 ¸w 1 1 + 2 x x x x + y ¸z z + w Boolean Logic in the Lambda Calculus “Church Booleans” true ¸x . ¸y . x Æ false ¸x . ¸y . y Æ Each is a function of two arguments: true is “select first;” false is “select second.” If-then-else uses its predicate to select then or else: ifelse ¸p . ¸a . ¸b . p a b Æ E.g., ifelse true 42 58 true 42 58 Æ (¸x . ¸y . x) 42 58 ! (¸y . 42) 58 ! 42 ! Boolean Logic in the Lambda Calculus Logic operators can be expressed with if-then-else: and ¸p . ¸q . p q p Æ or ¸p . ¸q . p p q Æ not ¸p . ¸a . ¸b . p b a Æ and true false (¸p . ¸q . p q p) true false Æ true false true ! (¸x . ¸y . x) false true ! false ! not true (¸p . ¸a . ¸b . p b a) true Æ ¸a . ¸b . true b a !¯ ¸a . ¸b . b !¯ ¸x . ¸y . y !® false Æ Arithmetic: The Church Numerals 0 ¸f . ¸x . x Æ 1 ¸f . ¸x . f x Æ 2 ¸f . ¸x . f (f x) Æ 3 ¸f . ¸x . f ¡f (f x)¢ Æ I.e., for n 0,1,2,..., n f x f (n)(x). The successor function: Æ Æ succ ¸n . ¸f . ¸x . f (n f x) Æ succ 2 ¡¸n . ¸f . ¸x . f (n f x)¢ 2 Æ ¸f . ¸x . f (2 f x) ! ³ ´ ¸f . ¸x . f ¡¸f . ¸x . f (f x)¢ f x Æ ¸f . ¸x . f ¡ f (f x)¢ ! 3 Æ Adding Church Numerals Finally, we can add: plus ¸m.¸n.¸f .¸x. m f ( n f x) Æ plus 3 2 ¡¸m.¸n.¸f .¸x. m f ( n f x)¢ 3 2 Æ ¸f .¸x. 3 f ( 2 f x) ! ¸f .¸x. f (f (f (2 f x))) ! ¸f .¸x. f (f (f (f ( f x)))) ! 5 Æ (m) (n) (m n) Not surprising since f f f Å ± Æ Multiplying Church Numerals mult ¸m.¸n.¸f .m (n f ) Æ mult 2 3 ¡¸m.¸n.¸f .m (n f )¢ 2 3 Æ ¸f .2 (3 f ) ! ¸f . 2 (¸x. f (f (f x))) Æ ¸f . 2 (¸y. f (f (f y))) $® ¸f . ¸x.(¸y. f (f (f y))) ((¸y. f (f (f y))) x) ! ¸f . ¸x.(¸y. f (f (f y))) (f (f (f x))) ! ¸f . ¸x. f (f (f (f (f (f x))) )) ! 6 Æ The predecessor function is trickier since there aren’t negative numbers. Recursion Where is recursion in the lambda calculus? µ ³ ´¶ fac ¸n . if ( n 0) 1 n ¡fac ( n 1)¢ Æ Æ ¤ ¡ This does not work: functions are unnamed in the lambda calculus. But it is possible to express recursion as a function. fac (¸n . ... fac ...) Æ (¸f .(¸n .... f ...)) fac ï H fac Æ That is, the factorial function, fac, is a fixed point of the (non-recursive) function H: H ¸f . ¸n . if ( n 0) 1 ( n (f ( n 1))) Æ Æ ¤ ¡ Recursion Let’s invent a function Y that computes fac from H, i.e., fac YH: Æ fac H fac Æ YH H (YH) Æ fac 1 YH 1 Æ H (YH) 1 Æ (¸f . ¸n . if ( n 0) 1 ( n (f ( n 1)))) (YH) 1 Æ Æ ¤ ¡ (¸n . if ( n 0) 1 ( n ((YH)( n 1)))) 1 ! Æ ¤ ¡ if ( 1 0) 1 ( 1 ((YH)( 1 1))) ! Æ ¤ ¡ 1 (YH 0) ! ¤ 1 (H (YH) 0) Æ ¤ 1 ((¸f . ¸n . if ( n 0) 1 ( n (f ( n 1)))) (YH) 0) Æ ¤ Æ ¤ ¡ 1 ((¸n . if ( n 0) 1 ( n (YH ( n 1)))) 0) ! ¤ Æ ¤ ¡ 1 (if ( 0 0) 1 ( 0 (YH ( 0 1)))) ! ¤ Æ ¤ ¡ 1 1 ! ¤ 1 ! The Y Combinator Here’s the eye-popping part: Y can be a simple lambda expression. Y Æ ¸f . ¡¸x . f (x x)¢ ¡¸x . f (x x)¢ Æ ³ ´ YH ¸f .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    27 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us