











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to lambda calculus, a mathematical system for functions and computation. It covers the basics of lambda calculus, including variables, functions, and semantics. The document also includes exercises for practicing reading and translating lambda calculus terms into other programming languages.
Typology: Papers
1 / 19
This page cannot be seen from the preview
Don't miss anything!












Describing Semantics
Tutorials: learn by working examples Reference Manuals Natural language explanation for each syntax rule
Attribute grammars Associate attributes (values) with each grammar symbol Associate semantics rules with each grammar rule Operational semantics Interpret the language on an abstract machine or using another language Denotational semantics Define language constructs as mathematical functions Axiomatic semantics (proof rules) Define properties (invariance) of language constructs
Pure Lambda Calculus
x represents variable names Each expression in Lambda Calculus is called a lambda term or a lambda expression
( M M ) has higher precedence than λ x.M ; i.e, λ x.M N <=> λ x. (M N) M M is left associative; i.e. x y z <=> (x y) z Add parentheses to allow alternative grouping of operations
M ::= x | (lambda (x) M) | (M M)
The Applied Lambda Calculus
Yes, it is Turing complete. Other values/operations can be represented as function abstractions. For example, booleans can be expressed as True = λ t. (λ f. t) False = λ t. (λ f. f) But we are not going to be extreme.
M ::= e | x | λ x.M | M M e represents all regular arithmetic expressions
Expressions: x+y, x+2y+z Function abstraction/definition: λ x.(x+y), λz.(x+2y+z) Function application (invocation): (λ x.(x+y)) 3
Lambda Calculus In Real Languages
Many different dialects Lisp 1.5, Maclisp, …, Scheme, ...CommonLisp,… This class uses Scheme Function abstraction (allow multiple parameters) λ x. M => (lambda (x) M) λx. λ y. λ z. M => (lambda (x y z) M) Function application M1 M2 => (M1 M2) (M1 M2) M3 => (M1 M2 M3)
λ x. λ y. λ z. M => int f(int x,int y,int z) { return M; } (M1 M2) M3 => M1(M2, M3)
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 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)+
Each variable is a name (or memory store) that can be given different values When variables are used in expressions, need find the binding location/declaration and get the value
Variable Binding Bound and Free variables
x is bound (local) in λ x.M The binding scope of x is M => the occurrences of x in M refers to the λ x declaration 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 The binding scope of x is somewhere outside of M Example: λ x. λ y. (z1*x+z2 y) Bound variables: x, y; free variables: z1, z Binding scopes λ x => λ y. (z1x+z2 y) λ y => (z1x+z2 *y)
Equality of Lambda Terms Idempotence axiom M = M Commutativity rule M = N => N = M Transitivity rule M = N N = P M = P Congruence rule M=M’ => λ x. M = λ x. M’ M = M’ N = N’ M N = M’ N’ What does the above mean? Make sure you copy the un-modified terms
Equality of Lambda Terms
λ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)
(λ 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)
Example: Variable Substitution
Substitute variables “blindly”
Rename bound variables
Easy rule: always rename variables to be distinct
Additional Exercises
Recursion and Fixed Points
Factorial: f(n) = if n=0 then 1 else n*f(n-1)