


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
Lecture notes on the mathematical concept of function composition and currying in the context of programming languages. It covers the notation, traditional function composition, lambda calculus, function application, and currying. The document also explains the semantics of lambda calculus and the evaluation rule.
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



The mathematical notation for defining a function is with a statement such as f (x) = x^2 , f : Z → Z,
where Z is the set of all integers. The first Z is called the domain of the function, or the set of values x can take. The second Z is called the range of the function, or the set containing all possible values of f (x). Suppose f (x) = x^2 and g(x) = x + 1. Traditional function composition is defined as f ◦ g = f (g(x)).
With our functions f and g,
f ◦ g = f (g(x)) = f (x + 1) = x^2 + 2x + 1.
Similarly g ◦ f = g (f (x)) = g(x^2 ) = x^2 + 1. Therefore, function composition is not commutative. In lambda (λ) calculus, we can use a different notation to define these same concepts. To define a function f (x) = x^2 , we instead write
λx.x^2.
Similarly for g(x) = x + 1 we instead write
λx.x + 1.
To describe a function application such as f (2) = 4, we write
(λx.x^2 2) ⇒ 22 ⇒ 4.
The syntax for lambda calculus expressions is
e ::= v – variable | λv.e – lambda expression | (e e) – procedure call
The semantics of the lambda calculus, or the way of evaluating or simplifying expressions, is defined by the rule
(λx.E M ) ⇒ E{M/x}.
The new expression E{M/x} can be read as “replace ‘fresh’ x’s in E with M ”. Informally, a “fresh” x is an x that is not nested inside another lambda expres- sion. We will cover free and bound variable occurrences in detail in upcoming lectures. For example, in the expression
(λx.x^2 2),
E = x^2 and M = 2. To evaluate the expression, we replace x’s in E with M , to obtain (λx.x^2 2) ⇒ 22 ⇒ 4. In lambda calculus, all functions may only have one variable. Functions with more than one variable may be expressed as a function of one variable through currying. Suppose we have a function of two variables expressed in the normal way h(x, y) = x + y, h : (Z × Z) → Z.
With currying, we can input one variable at a time into separate functions. The first function will take the first argument, x, and return a function that will take the second variable, y, and will in turn provide the desired output. To create the same function with currying, let
f : Z → (Z → Z)
and g : Z → Z.
That is, f maps integers to a function, and g maps integers to integers. The function f (x) returns a function gx that provides the appropriate result when supplied with y. For example,
f (2) = g 2 , where g 2 (y) = 2 + y.
So f (2)(3) = g 2 (3) = 2 + 3 = 5.
In lambda calculus this function would be described with currying by
λx.λy.x + y.
For function application, we nest two application expressions
((λx.λy.x + y 2) 3).
defines a function f (x) = x^2. To perform a procedure call, use the code
(f x [y z ...])
where y, z, etc. are additional parameters that f may require. The code
(f 2)
evaluates f (2) = 4.