Lambda Calculus: Understanding Variables, Functions, and Evaluation, Papers of Programming Languages

Explore the mathematical system of lambda calculus, focusing on variables, functions, function parameters, and substitution. Learn about pure lambda calculus, its completeness, and its application in real programming languages.

Typology: Papers

Pre 2010

Uploaded on 07/30/2009

koofers-user-8zm
koofers-user-8zm 🇺🇸

10 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
cs3723 1
Lambda Calculus
Variables and Functions
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Lambda Calculus: Understanding Variables, Functions, and Evaluation and more Papers Programming Languages in PDF only on Docsity!

Lambda Calculus

Variables and Functions

Lambda Calculus

 Mathematical system for functions

 Computation with functions

 Captures essence of variable binding

 Function parameters and substitution

 Can be extended with types, expressions,

stores and side-effects

 Introduced by Church in 1930s

 Notation for function expressions

 Proof system for equality of expressions

 Calculation rules for function application

(invocation)

Is Pure Lambda Calculus complete?

 Can pure lambda calculi express all computable

functions?

 Yes, it is Turing complete

 Other constructs can be represented as function

abstractions. For example

 Booleans can be expressed as

 True = λ t. (λ f. t)  False = λ t. (λ f. f)

 Natural numbers can be expressed as

 c0 = λ s. (λ z. z)  c1 = λ s. (λ z. (s (s z)))  c2 = λ s. (λ z. (s (s (s z))))  …

 But we are not going to be extreme …

The Applied Lambda Calculus

 Syntax of lambda terms

M ::= e | x | λ x.M | M M

e represents regular arithmetic expressions

x represents variables

 Now we have expressions and functions

Expressions

 x + y, x + 2*y + z

Function abstraction/definition

 λ x. (x+y), λz. (x + 2*y + z)

Function application (invocation)

 (λ x. (x+y)) 3 = 3 + y  Symbolic evaluation of mathematical expressions

Exercises: translate Lambda Calculus

 From Lambda calculus ==> C language

 λ x. x <=> (lambda (x) x)

int dummy(int x) { return x; }What if x is a float or string?

 λ f. λ g. λ x. f (g x) <=>

(lambda (f) (lambda (g) (lambda (x) (f (g x))))) int dummy(int (f)(...), int (g)(...), int x) { return f(g(x)); }**  C: does not allow functions to return functions

 (λ f. λ x. (f x)) y z ==>?

 From C ==> Lambda calculus

int f1(int (g)(...)) { return g(g); } int f2(int x) { return x + 1; }*  int main() { return f2(3); } ==>?int main() { return f1(f2); } ==>?int main() { return f1(f1); } ==>?

Programming With Lambda Calculus

 The identity function

 λ x. x

 Composition of function parameters

 plus(x,y,z) = x + y + z  λ x. λ y. λ z. x + y + z

 Composition of multiple functions

 Given functions f(x) and g(x), return function f ° g (x)  λ f. λ g. λ x. f (g x)

 What happens in evaluation

 (λ y. y + 1) x = x + 1

 (λ f. λ x. f (f x)) g = λ x. g (g x)

 (λ f. λ x. f (f x)) (λ y. y+1) = λ x. (λ y. y+1) ((λ y. y+1) x)

= λ x. (λ y. y+1) (x+1) = λ x. (x+1)+

Higher-Order Functions  Functions that either take a function as parameter or return

a function as result
 First-order functions: parameters and results are not
functions
 Second-order functions: parameters or results are first-
order functions
 Third-order functions: parameters or results are second-
order functions

 Functions as first-class objects

 Functions treated same as primitive values
 True in Lisp. What about C/C++?

 Lambda calculus examples

 Higher-order function: λ f. λ g. λ x. f (g x)

 First-class functions (functions as primitive values)

 (λ f. λ g. λ x. f (g x)) ( λ y. y+1)z. zz)*

Higher-Order functions and Functions as first-class objects

(λ f. λ g. λ x. f (g x)) (λ y. y+1) (λ z. z*z)

 Lisp implementation (((lambda (f) (lambda (g) (lambda (x) (f (g x))))) (lambda (y) (+ y 1))) (lambda (z) (* z z)))

 C implementation (functions are not first-class)

int compose (int (f)(int), int (g)(int), int x) { return f(g(x)); } int plus1 (int y) { return y + 1; } int square (int z) { return z * z; } int main() { int x = 50; printf(“%d”, compose(plus1,square,x)); }

Variable Binding  Bound and Free variables  Each λ x.M declares a new local variable x

 x is bound (local) in λ x.M
 x is free (global) in M if there is no declarations of x in M
 The binding scope of x is M => the occurrences of x in M
refers to the declaration of x by λ x
 Each variable x in a expression M is free (global) if

 there is no λx in the expression M, or x appears outside all λx declarations in M

 Example: λ x. λ y. (z1*x+z2 *y)

 Bound variables: x, y; free variables: z1, z
 Binding scopes

λ x => λ y. (z1*x+z2 y) λ y => (z1x+z2 *y)

Variable Binding

 Do variable names in an expression matter?

 λ x. (x+y) = λ z. (z+y)

 Bound (local) variables: no; Free (global) variables: yes

 y is both free and bound in λ x. ((λ y. y+2) x) + y

 Rename bound variables to avoid conflict  Functions with and without names

 (λ x. (x+y)) 3 f(x) = x+y; return f(3)

 (λ f. λ x. f (f x)) (λ y. y+1) => λ x. (λ y. y+1) ((λ y. y+1) x)

=> λ x. (λ y. y+1) (x+1) => λ x. (x+1)+

 int h(int (*f)(…))
{ int dummy(x) { return f(f(x));}
return dummy; }
int g(int y) { return y+1; }
int main() { return h(g) }

Equality of Lambda Terms

 α-axiom

 λx. M = λy. [y/x]M

 [y/x]M: substitutes y for free occurrences of x in M

 y cannot already appear in M

 Example

 λ x. (x + y) = λ z. (z + y)  But λ x. (x + y) ≠ λ y. (y + y)

 β-axiom

 (λ x. M) N = [N/x] M

 [N/x]M is substitutes N for free occurrences of x in M

 Free variables in N cannot be bound in M

 Example

 (λ x. λ y. (x + y)) z1 = λ y. (z1+y)  But (λ x. λ y. (x + y)) y ≠ λ y. (y + y)

Evaluation of Lambda-terms

 β-reduction

(λ x. t 1 ) t 2 => [t 2 /x]t 1

 where [t 2 /x]t 1 involves renaming as needed  Rename bound variables in t 1 if they appear free in t 2  α-conversion: λ x. M => λ y. [y/x]M (y is not free in M)  Replaces all free occurrences of x in t 1 with t 2

 Reduction

 Repeatedly apply β-reduction to each subexpression

 Each reducible expression is called a redex

 The order of applying β-reductions does not matter

 Normal form

 An expression that cannot be further reduced

 Confluence:

 Final result (if there is one) is uniquely determined

Additional Exercises

 Reduce lambda terms

 λ x. (λ y. y x) (λ z. x z)

 (λ x. (λ y. y x) (λ z. x z) ) y

 (λ x. (λ y. y x) (λ z. x z) ) (λ y. y z)

Non-terminating Reductions

 Can all lambda terms be reduced to normal

form?

 Non-terminating reductions

 (λ x. x x) (λ x. x x)  =>(λ y. y y) (λ x. x x) =>(λ x. x x) (λ x. x x) …

 Combinators

 Pure lambda terms without free variables

 Fixed-point combinator

 A combinator Y such that given a function f

 Yf => f(Yf)

 Example: Y = λ f. (λ x. f (x x)) (λ x. f (x x))

Yf = ( λ f. ( λ x. f (x x)) ( λ x. f (x x))) f => ( λ x. f (x x)) ( λ x. f (x x)) => f (( λ x. f (x x)) ( λ x. f (x x))) => f (Yf)