Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING Syllabus: Lambda notation for functions – syntax – curried functions – parametric polymorphism – lambda reduction – alpha reduction – beta reduction – beta abstraction – extensionality theorem – delta reduction – reduction strategies – normal forms Church-Rosser Theorems – pure lambda calculus – constants – arithmetic – conditionals Iteration – recursion – introduction to functional programming
x . x — is a function taking an argument x, and returning x The Lambda Calculus was invented by Alonzo Church [1932] as a mathematical formalism for expressing computation by functions. Syntax: e ::= x a variable | x . e an abstraction (function) | e1 e2 a (function) application Examples: x . x — is a function taking an argument x, and returning x f x — is a function f applied to an argument x
Lambda notation for functions A lambda abstraction is just an anonymous function. Consider the Haskell function: The value of compose is the anonymous lambda abstraction: f g x . f (g x) NB: This is the same as: f . g . x . f (g x)
The lambda calculus can be viewed as the simplest possible pure functional programming language.
Lambda extends as far as possible to the right f.x y f.(x y) Application is left-associative x y z (x y) z Multiple lambdas may be suppressed f g.x f . g.x
Syntax Only three kinds of expressions E ::= x variables | E1 E2 function application | lx. E function creation The form lx. E is also called lambda abstraction, or simply abstraction E are called l-terms or l-expressions
(x.x) y (x.f x) x y (x.x) (x.x) (x.x y) z (x y.x) t f (x y z.z x y) a b (x y.x) (f g.f g) (x.x) (x.x) z (x y.x y) y (x y.x y) (x.x) (x.x) (x y.x y) ((x.x) (x.x))
Curried Functions Since a lambda abstraction only binds a single variable, functions with multiple parameters must be modelled as Curried higher-order functions. As we have seen, to improve readability, multiple lambdas are suppressed, so: x y . x = x . y . x b x y . b x y = b . x . y . ( b x ) y
Parametric Polymorphism
Lambda Reduction λ-reduction The key notion of the λ-calculus is that it is possible to arrive at a logically equivalent expression by means of a process called λ-reduction. In the usual case, λ-reduction is actually a combination of three distinct reduction operations, each of which is discussed below. The key operation, the one that does the heavy lifting, is called β-reduction, and that is operation we will discuss first. By the way, some people say "λ-conversion" instead of λ-reduction; others reserve "λ-conversion" to refer specifically to a single step in a series of reductions. This tutorial is trying not to be overly pedantic, so for present purposes I don't care.
(λ x (λ y (x (+ y x)))) (λ z (λ y (z (+ y z)))) alpha reduction The solution is to make use of alphabetic variants. Roughly, two expressions are "alphabetic variants" if they are identical except perhaps for the choice of variable symbols. For instance, the following expressions are alphabetic variants of one another: (λ x (λ y (x (+ y x)))) (λ z (λ y (z (+ y z)))) To create an alphabetic variant for an expression of the form "(λ var body)", simply replace each free occurrence of "var" in the expression with "new", where "new" is a variable symbol not occurring anywhere in "body". (Since expressions have finite length, as long as there is an infinite supply of variable symbols, it will always be possible to find a suitable variable to serve the role of "new".) This transformation is called α-reduction. The crucial property of the reduced form is that each λ operator binds the same number of variables in the same positions within its body
beta reduction
beta abstraction The conversion of an expression to an application of a lambda abstraction to anargument expression. Some subterm of the original expression becomes the argument of the abstractionand the rest becomes its body. E.g. 4+1 --> (\ x . x+1) 4 The opposite of beta abstraction is beta reduction. These are the two kinds of beta conversion.
Extensionality theorem In logic, extensionality, or extensional equality refers to principles that judge objects to be equal if they have the same external properties. It stands in contrast to the concept of intensionality, which is concerned with whether the internal definitions of objects are the same. Consider the two functions f and g mapping from and to natural numbers, defined as follows: To find f(n), first add 5 to n, then multiply by 2. To find g(n), first multiply n by 2, then add 10. These functions are extensionally equal; given the same input, both functions always produce the same value. But the definitions of the functions are not equal, and in that intensional sense the functions are not the same.
Delta Reduction A delta redex is matched against the left hand side of alldelta rules and is replaced by the right hand side of the (first) matching rule. There is notionally one deltarule for each possible combination of function and arguments. Where this implies an infinite number of rules,the result is usually defined by reference to some external system such as mathematical addition or thehardware operations of some computer.
Reduction Strategies
Normal Forms A lambda expression is in normal form if it can no longer be reduced by beta or eta reduction rules. Not all lambda expressions have normal forms! = ( x . x x) ( x . x x) [ ( x . x x) / x ] ( x x ) = ( x . x x) ( x . x x) reduction ( x . x x) ( x . x x) reduction ... Reduction of a lambda expression to a normal form is analogous to a Turing machine halting or a program terminating.
Church-Rosser Theorems
Pure Lambda Calculus Lambda-calculus with no constants, only functions expressed as lambda abstractions.
constants
arithmetic
Iteration conditionals Every procedural programming language has conditional statements (if-statements) and iterative statements (loops). The block. if-statement. if-else statement. while-loop. for-loop.
Recursion
Introduction to functional programming