# Elements of Lambda Calculus Functional Programming Academic Year 2005-2006 Alessandro Cimatti

## Presentation on theme: "Elements of Lambda Calculus Functional Programming Academic Year 2005-2006 Alessandro Cimatti"— Presentation transcript:

Elements of Lambda Calculus Functional Programming Academic Year 2005-2006 Alessandro Cimatti cimatti@itc.it

Foundations Lambda calculus –Invented by Church in 1932 –Model of computation –All computable functions –As powerful as Turing machine Main ideas –Extremely simple –Functions as objects –Computations as sequences of reductions Basis for many functional languages –LISP, SCHEME, ML

Syntax Expressions (also called λ-terms) have the following syntax e is either –x a variable –λx.e a lambda abstraction –e1 e2 application Notation –for variables we use x, y, z, w –for expressions we use e, e’, e1, e2,...

Conventions Parentheses used to avoid ambiguities –x y z can be interpreted either as (x y) z or as x (y z) Two conventions to avoid too many parentheses –applications associate to the left, i.e. e e1 e2... eN stands for (... ((e e1) e2)... eN) –bodies of lambdas extended as much as possible λx.λy.e1 e2 e3 stands for λx.(λy.((e1 e2) e3)) rather than λx.((λy. (e1 e2)) e3) Nested lambdas may be collapsed together –λx1.(λx2.(...(λxN.e)...)) can be written as λx1x2...xN.e

Scope In λx.e, e is the scope of λx An occurrence of a variable x is bound when it occurs in the body of an abstraction λx.e An occurrence is said to be free if it appears in a position where it is not bound by an enclosing abstraction of x Examples –x y –λy.x y –λx.x –(λx.x x)(λx.x x) –(λx.x)y –(λx.x)x

Free variables The set of free variables in e, denoted as FV(e), is defined as –FV(x) = { x } –FV(e1 e2) = FV(e1) U FV(e2) –FV(λx.e) = FV(e) – { x } e is a closed λ-term iff FV(e) is empty

Alpha Renaming λ-terms are equivalent up to renaming of bound variables –all occurrences of x are replaced by y The rule of α-conversion –λx.e ≡α λy.[y/x]e –provided that y is not free in e For instance, –λx.x ≡α λy.y –λx.y x ≡α λz.y z But not –λx.y x ≡α λy.y y

Substitution No problem when free variables are replaced by term that does not clash –[λz.z w / x] λy.x = λy.λz.z w However, there may be a problem if there is name capture/clash –[λz.z w / x] λx.x is not the same as λx.λz.z w –[λz.z w / x] λw.x is not the same as λw.λz.z w

Definition of Substitution [e’/x]x = e’ [e’/x]y = y [e’/x](e1 e2) = ([e’/x]e1) ([e’/x] e2) [e’/x](λx.e) = λx.e [e’/x](λy.e) = λy.e if x not in FV(e) [e’/x](λy.e) = λy.[e’/x]e if x in FV(e), y not in FV(e’) [e’/x](λy.e) = λz.[e’/x][z/y]e if x in FV(e), y in FV(e’)

Beta Reduction An application whose LHS is an abstraction –An expression of the form ((λx. e1) e2) –is called redex (reducible expression) A redex evaluates to the body of the abstraction with parameter substitution Examples –(λx.(x y)) z → β z y –(λx. y) z → β y –(λx.x x) y → β y y –(λx.x x) (λx.x x) → β (λx.x x) (λx.x x) Beta-reduction –(λx. e1) e2 → β [e2/x]e1

Properties of Beta Reduction If e1 → β e2, then –(e e1) → β (e e2) –(e1 e) → β (e2 e) –(λx.e1) → β (λx.e2) The transitive closure of → β –e1 → β e2 → β... → β en –e1 reduces to en The induced equivalence relation is denoted ≡β

β-Normal Form A λ-term is in β-normal form if it does not have any redex as subexpression –meaning: there is no way to apply beta reduction A λ-term e has a β-normal form if for some e’ –e ≡β e’ –e’ is a β-normal form Some λ-terms do not have any normal form: –(λx.xx)(λx.xx) → β [(λx.xx)/x]xx = (λx.xx)(λx.xx) Intuition? –loop for ever

Properties of Lambda Calculus If e has a normal form, then this is unique (up to renaming) The β-reduction satisfies the so-called Church-Rosser property –if e reduces to e1 and e reduces to e2 –then there exists e3 such that –e1 reduces to e3 and e2 reduces to e3 Confluence