Function Composition and Currying in Programming Languages, Study notes of Programming Languages

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

Pre 2010

Uploaded on 08/09/2009

koofers-user-tm2
koofers-user-tm2 🇺🇸

5

(1)

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCI.4430/6969 Programming Languages
Lecture Notes
August 26, 2003
The mathematical notation for defining a function is with a statement such
as
f(x) = x2, f :ZZ,
where Zis the set of all integers. The first Zis called the domain of the function,
or the set of values xcan take. The second Zis called the range of the function,
or the set containing all possible values of f(x).
Suppose f(x) = x2and g(x) = x+ 1. Traditional function composition is
defined as
fg=f(g(x)) .
With our functions fand g,
fg=f(g(x)) = f(x+ 1) = x2+ 2x+ 1.
Similarly
gf=g(f(x)) = g(x2) = x2+ 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) = x2, we instead write
λx.x2.
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.x22) 224.
The syntax for lambda calculus expressions is
e::= v variable
|λv.e lambda expression
|(e e) procedure call
1
pf3
pf4

Partial preview of the text

Download Function Composition and Currying in Programming Languages and more Study notes Programming Languages in PDF only on Docsity!

CSCI.4430/6969 Programming Languages

Lecture Notes

August 26, 2003

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.