Lambda Calculus - Syntax
Lambda Calculus - Syntax
Partha Pratim
Das
Relations
Principles of Programming Languages
Functions Module M02: λ-Calculus: Syntax
Composition
Currying
λ-Calculus
Concept of λ
Syntax
Partha Pratim Das
λ-expressions
Notation
Examples
Department of Computer Science and Engineering
Simple
Indian Institute of Technology, Kharagpur
Composition
Boolean [email protected]
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11 January 05, 10, 12 & 17, 2022
Principles of Programming Languages Partha Pratim Das M02.1
Table of Contents
Functions
Currying
Composition 3 λ-Calculus
Currying
λ-Calculus
Concept of λ
Concept of λ 4 Syntax
Syntax
λ-expressions
λ-expressions
Notation Notation
Examples
Simple
Examples
Composition Simple
Boolean Composition
Numerals
Boolean
Recursion
Multi-variable
Numerals
Functions Recursion
Higher Order
Functions Multi-variable Functions
C++11 Higher Order Functions
C++11
Principles of Programming Languages Partha Pratim Das M02.2
Relations
Module M02
Partha Pratim
Das
Relations
Functions
Composition
Currying
λ-Calculus
Concept of λ
Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Relations
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Functions
• Set of relations between A and B is 2A×B , where 2X is the power set of a set X
Composition
Currying
• If A = B, r is said to be a relation over A
λ-Calculus • r is
Concept of λ
Module M02
Partha Pratim
Das
Relations
Functions
Composition
Currying
λ-Calculus
Concept of λ
Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Functions
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
• f : A → B is a function from A to B if
Partha Pratim
Das ◦ f is a relation between A and B (that is, f ∈ A × B), and
Relations
◦ ∀(s1 , s2 ), (t1 , t2 ) ∈ f , s1 = t1 ⇒ s2 = t2
Functions • f is total if ∀u ∈ A, ∃(u, v ) ∈ f
Composition
Currying • f is partial, otherwise
λ-Calculus
Concept of λ
• Set of functions from A to B is B A ⊂ 2A×B
Syntax • A is the domain, B is the codomain or range
λ-expressions
Notation • Image f (A) of f is {v : ∀u ∈ A, f (u) = v }
Examples
Simple • A total function f is
Composition
Boolean ◦ Injective (one-to-one): ∀u, v ∈ A, f (u) = f (v ) ⇒ u = v
Numerals
Recursion ◦ Surjective (onto): f (A) = B
Multi-variable
Functions
◦ Bijective (one-to-one and onto): Injective and Surjective
• f −1 = {(v , u) : (u, v ) ∈ f } is the inverse of f .
Higher Order
Functions
C++11
Module M02
Module M02
Partha Pratim
Das
Relations
Functions
Composition
Currying
λ-Calculus
Concept of λ
Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
• Using currying1 , one-variable functions can represent multiple-variable functions
Partha Pratim
Das • Consider:
Relations h(x, y ) = x + y of type h : Z × Z → Z
Functions
Composition
• Represent h as h of type2 c
Currying
hc : Z → Z → Z or hc : Z → (Z → Z ) or hc : Z → Z Z
λ-Calculus
Concept of λ such that
Syntax h(x, y ) = hc (x)(y ) = x + y
λ-expressions
Module M02
Partha Pratim
Das
Relations
Functions
Composition
Currying
λ-Calculus
Concept of λ
Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
λ-Calculus
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Partha Pratim • Developed by Alonzo Church and his doctoral student Stephen Cole Kleene in the 1930
Das
• Can represents all computable functions
Relations
Functions
• Has equal power as of Turing Machine
Composition
Currying
λ-Calculus
Concept of λ
Syntax
Source: λ- Calculus Overview
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Partha Pratim • Uncomplicated syntax and semantics provide an excellent vehicle for studying the
Das
meaning of programming language concepts
Relations
• All functional programming languages can be viewed as syntactic variations of the
Functions
Composition λ-calculus
Currying
λ-Calculus
• Denotational semantics is based on the λ-calculus and expresses its definitions using the
Concept of λ higher-order functions of the λ-calculus
Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Partha Pratim • A function is a mapping from the elements of a domain set to the elements of a
Das
codomain set given by a rule
Relations
• Example,
Functions
Composition
Currying
cube : Integer → Integer
λ-Calculus
Concept of λ
where
Syntax
λ-expressions
cube(n) = n3
Notation
Examples • Questions:
Simple
Composition ◦ What is the value of the identifier cube?
Boolean
Numerals
◦ How can we represent the object bound to cube?
Recursion ◦ Can this function be defined without giving it a name?
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Relations λn. n3
Functions
Composition defines the function that maps each n in the domain to n3
Currying
Module M02
• Function Abstraction
Partha Pratim
Das ◦ Mathematical Notation: cube : Integer → Integer
Relations
cube(n) = n3
Functions
n ⊢ n3
Composition ◦ λ-notation:
λn. n3 = λn. (n ∗ n ∗ n)
Currying
λ-Calculus
Concept of λ ◦ C Function:
Syntax int cube(int n) { return n * n * n; }
λ-expressions
Notation
Examples
Simple • Function Application
Composition
Boolean ◦ Mathematical Notation: cube(2) = 8
Numerals
Recursion
◦ λ-notation:
Multi-variable
Functions
(λn. n3 ) 2 ≡ 2 ∗ 2 ∗ 2 = 8
Higher Order
Functions
◦ C Function:
C++11 int n_cube = cube(2);
Principles of Programming Languages Partha Pratim Das M02.15
Concept of λ
Module M02
Partha Pratim • The number and order of the parameters to the function are specified between the λ
Das
symbol and an expression
Relations
• Example: Expression
Functions
Composition n2 + m
Currying
Syntax
(3, 4) ⊢ 32 + 4 = 13
λ-expressions
Notation
Examples
or
Simple
Composition (3, 4) ⊢ 42 + 3 = 19
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Partha Pratim • λ-notation resolves the ambiguity by specifying the order of the parameters:
Das
λn. λm. n2 + m, that is, (3, 4) ⊢ 32 + 4 = 13
Relations
Functions
λm. λn. n2 + m, that is, (3, 4) ⊢ 42 + 3 = 19
Composition
Currying
Notationally (by left-to-right order 3 binds to n and 4 binds to m):
λ-Calculus (λn. λm. (n2 + m) 3 4) = (λm. (32 + m) 4) = (λm. (9 + m) 4) = (9 + 4) = 13
Concept of λ
Syntax
• Most functional programming languages allow anonymous functions as values
λ-expressions
Notation
• Example: The function λn.n3 is represented as
Examples
◦ Scheme: (lambda (n)(∗ n n n))
Simple
Composition ◦ Standard ML: fn n ⇒ n ∗ n ∗ n
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
• Function Abstraction
Partha Pratim
Das ◦ Mathematical2 Notation: f , g : Integer × Integer → Integer
f (n, m) = n + m
Relations g (m, n) = n2 + m
Functions
◦ λ-notation:2
Composition
λn. λm. n + m
Currying λm. λn. n2 + m
λ-Calculus
◦ C Function:
Concept of λ
int f(int n, int m) { return n * n + m; }
int g(int m, int n) { return n * n + m; }
Syntax
λ-expressions
Notation • Function Application
Examples ◦ Mathematical Notation:
Simple f (3, 4) = 13
Composition g (3, 4) = 19
Boolean
◦ λ-notation: 2
Numerals
(λn. λm. n + m) 3 4 = 32 + 4 = 13
Recursion
Multi-variable
(λm. λn. n2 + m) 3 4 = 42 + 3 = 19
Functions ◦ C Function:
Higher Order
Functions
int r_f = f(3, 4); // 13
C++11 int r_g = g(3, 4); // 19
Module M02
Partha Pratim
Das
Relations
Functions
Composition
Currying
λ-Calculus
Concept of λ
Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Syntax of λ-Calculus
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Partha Pratim
λ-expressions come in four varieties:
Das
• Variables
Relations
◦ Usually, lowercase letters
Functions
Composition • Predefined Constants
Currying
λ-Calculus
◦ Act as values and operations
Concept of λ ◦ Allowed in an impure or applied λ-calculus
Syntax
λ-expressions • Function Applications
Notation
Examples
◦ Combinations
Simple
Composition
• λ-Abstractions
Boolean
Numerals
◦ Function definitions
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Partha Pratim
BNF Syntax of λ-Calculus:
Das
Relations < expression > ::= < variable > ; lowercase identifiers
Functions | < constant > ; predefined objects
Composition
Currying
| (< expression >< expression >) ; combinations
λ-Calculus | (λ < variable > . < expression >) ; abstractions
Concept of λ In short:
Syntax
λ-expressions
Notation
e ::= v ; variables / constants
Examples
Simple
| (e e) ; function application
Composition | (λv .e) ; function abstractions
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Partha Pratim • Identifiers of more than one letter may stand as variables and constants
Das
• Pure λ-calculus
Relations
Functions
◦ has no predefined constants, but
Composition ◦ it still allows the definition of all of the common constants and functions of
Currying
arithmetic and list manipulation
λ-Calculus
Concept of λ • Predefined constants
Syntax
λ-expressions ◦ Numerals (for example, 34),
Notation
Examples
◦ add (addition), mul (multiplication), succ (successor function), and sqr (squaring
Simple function)
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Syntax
isEmpty returns true if the list it is called on is the empty
λ-expressions list, returns false otherwise
Notation
Examples • (cons y nil) = (y )
Simple
Composition • (cons x (y )) = (x y )
Boolean
Numerals • (car (cons x y )) = x
Recursion
Multi-variable
Functions
• (cdr (cons x y )) = (y )
Higher Order
Functions 3 Contents
C++11
of the Address part of Register number
4 Contents of the Decrement part of Register number
Principles of Programming Languages Partha Pratim Das M02.23
Free and Bound Variable
Module M02
Partha Pratim • In an abstraction, the variable named is referred to as the bound variable and the
Das
associated λ-expression is the body of the abstraction
Relations
• In an expression of the form:
Functions
Composition λv . e
Currying
Syntax
• All occurrences of other variables are free
λ-expressions
Notation
• Example:
Examples
Simple
Composition
((λx. λy . (xy ))(yw ))
Boolean
Numerals ◦ x, and y are bound in first part
Recursion
Multi-variable ◦ y , and w are free in second part
Functions
Higher Order
Functions
C++11
Module M02
Partha Pratim • With a function application (E1 E2 ), it is expected that E1 evaluates to a predefined
Das
function (a constant) or an abstraction, say (λx. E3 ), in which case the result of the
Relations application will be the evaluation of E3 after every free occurrence of x in E3 has been
Functions
Composition
replaced by E2
Currying (λn. n3 2) ⇒ 23 ⇒ 8
λ-Calculus
Concept of λ (λn. (∗ (∗ n n) n) 2) ⇒ 23 ⇒ 8
Syntax
λ-expressions • In a combination (E1 E2 ), the function or operator E1 is called the rator and its
Notation
Examples
argument or operand E2 is called the rand
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Partha Pratim • Uppercase letters and identifiers beginning with capital letters will be used as
Das
meta-variables ranging over λ-expressions
Relations
Functions
Composition
Currying
λ-Calculus
Concept of λ
Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Relations E1 E2 E3
Functions
Composition means
Currying
λ-Calculus
Concept of λ ((E1 E2 ) E3 )
Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Partha Pratim • The scope of λ < variable > in an abstraction extends as far to the right as possible:
Das
Relations λx. E1 E2 E3
Functions
Composition
Currying
means
λ-Calculus (λx. (E1 E2 E3 )) and not ((λx. E1 E2 ) E3 )
Concept of λ
Module M02
Partha Pratim • An abstraction allows a list of variables that abbreviates a series of λ abstractions
Das
Relations λx y z. E
Functions
Composition
Currying
means
λ-Calculus (λx. (λy . (λz. E )))
Concept of λ
Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Partha Pratim • Functions defined as λ-expression abstractions are anonymous, so the λ-expression itself
Das
denotes the function
Relations
• As a notational convention, λ-expressions may be named using the syntax
Functions
Composition
Currying define < name > = < expression >
λ-Calculus
Concept of λ
Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Functions
it follows that
Composition (Twice (λn. (add n 1)) 5) = 7
Currying
λ-Calculus
Concept of λ
Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Partha Pratim
• Variables: n
Das • Constants: ∗ : Integer → Integer (binary multiplication), 2
Relations
• Function Abstraction
Functions
◦ Mathematical3 Notation: cube : Integer → Integer
cube(n) = n
Composition
Currying
◦ λ-notation: 3
cube ≡ λn. n = λn. (n ∗ n ∗ n) // Untyped λ
λ-Calculus
cube ≡ λ(n : int). n3 // Typed λ, return type inferred
Concept of λ
◦ C Function:
Syntax int cube(int n) { return n * n * n; } // return type explicit
λ-expressions ◦ C++ λ Function:
Notation auto cube = [](int n) { return n * n * n; }; // return type inferred
Examples
auto cube = [](int n) -> int { return n * n * n; }; // return type explicit
Simple
Composition • Function Application
Boolean ◦ Mathematical Notation: cube(2) = 8
Numerals ◦ λ-notation:
Recursion
(λn. n3 ) 2 ≡ 2 ∗ 2 ∗ 2 = 8
Multi-variable
Functions ◦ C / C++ λ Function:
Higher Order int n cube = cube(2);
Functions
C++11
Module M02
λ-Calculus
Concept of λ (λx. f (n f x)) (λy . g y )
Syntax (λf . (λx. f (n f x))) (λg . (λy . g y ))
λ-expressions
Notation
(λn. (λf . (λx. f (n f x))))
Examples
Simple
Composition • Completely parenthesized expression:
Boolean
Numerals
Recursion ((λn. (λf . (λx. (f ((n f ) x))))) (λg . (λy . (g y ))))
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
• Elementary
Partha Pratim
◦ Identity Function
Das ◦ Successor Function
Relations
◦ Constant Function
Functions • Composition
Composition
◦ Application
Currying
λ-Calculus
▷ twice
Concept of λ ▷ thrice
Syntax ◦ Composition
λ-expressions
Notation
• Church Boolean
Examples
Simple
◦ Selector Function (TRUE , FALSE )
Composition ◦ Conditional Test IF
Boolean
Numerals
◦ Boolean Algebra
Recursion • Church Numerals
Multi-variable
Functions • Recursion
Higher Order
Functions
◦ Self Application
C++11 ◦ Y Combinator
▷ factorial
Principles of Programming Languages Partha Pratim Das M02.35
λ-expressions: Identity Function
Module M02
Functions
denotes the identity function in the sense that
Composition
Currying
((λx. x) E ) = E
λ-Calculus
Concept of λ
Module M02
Functions
denotes the successor function on the integers so that
Composition
Currying
(λn. (add n 1)) 5 = 6
λ-Calculus
Concept of λ
Syntax • add and 1 need to be predefined constants to define this function, and 5 must be
λ-expressions predefined to apply the function
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Functions
builds a constant function (generator)
Composition
Currying
• (K 0) = (λx. λy . x) 0 = λy . 0 = 0, is a constant function returning 0
λ-Calculus • (K 1) = (λx. λy . x) 1 = λy . 1 = 1, is a constant function returning 1
Concept of λ
Syntax • ···
λ-expressions
Notation • (K n) = (λx. λy . x) n = λy . n = n, is a constant function returning n
Examples
Simple
◦ (λy . n) 0 = 0
Composition
Boolean
◦ (λy . n) 12 = 12
Numerals ◦ (λy . n) 935 = 935
Recursion
Multi-variable
◦ (λy . n) m = m, m is a constant
Functions
Higher Order
Functions
C++11
Module M02
Functions
takes a function and a value as argument and applies the function to the argument
Composition
Currying
• Since f is a function and it takes x as an argument, say of type A, then f must be of
λ-Calculus type A → B for some B
Concept of λ
Syntax
• Type of apply then is: (A → B) → A → B
λ-expressions
Notation
• A → B is a possible type of f , A is the possible type of x, and B is the result type of
Examples apply which is the same as result type of f
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Functions
is similar to apply but applies the function f twice
Composition
Currying
• It applies f to x obtaining a result, and applies f to this result once more
λ-Calculus • Unlike apply , since f is applied again to the result of f , the argument and result types
Concept of λ
Syntax
of f should be the same, say A
λ-expressions
Notation
• So, the type of twice is (A → A) → A → A
Examples
Simple
• If sqr is the (predefined) integer function, then
Composition
Boolean
Numerals
((twice sqr ) 3) ⇒ (((λf . (λx. (f (f x)))) sqr ) 3) ⇒
Recursion
Multi-variable
Functions ((λx. (sqr (sqr x))) 3) ⇒ (sqr (sqr 3)) ⇒ (sqr 9) ⇒ 81
Higher Order
Functions
C++11
• Similarly, (twice (λn. (add n 1)) 5) = 7
Principles of Programming Languages Partha Pratim Das M02.40
λ-expressions: thrice
Module M02
Functions
applies f thrice
Composition
Currying
• The type of thrice is (A → A) → A → A
λ-Calculus • If sqr is the (predefined) integer function, then
Concept of λ
Syntax
λ-expressions ((thrice sqr ) 3) ⇒ (((λf . (λx. f (f (f x)))) sqr ) 3) ⇒
Notation
Examples
Simple
((λx. (sqr (sqr (sqr x)))) 3) ⇒ (sqr (sqr (sqr 3))) ⇒
Composition
Boolean (sqr (sqr 9)) ⇒ (sqr 81) ⇒ 6561
Numerals
Recursion
Multi-variable
• Similarly, (thrice (λn. (add n 1)) 5) = 8
Functions
Higher Order
Functions
C++11
Module M02
Functions
is the mathematical composition: (comp g f ) ≡ g ◦ f
Composition
Currying
• If f is of type A → B and g is of type B → C , then type of g ◦ f is A → C
λ-Calculus • Given an argument, g ◦ f first applies f to the argument and then applies g to the
Concept of λ
Syntax
result of this application
λ-expressions
Notation
• The type of comp is (B → C ) → (A → B) → (A → C )
Examples
Simple
• twice f ≡ (comp f f )
Composition
Boolean
• thrice f ≡ (comp f (comp f f )) ≡ (comp (comp f f ) f )
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Functions
denotes the fst selector function
Composition
Currying
• It takes two arguments and returns the first argument as the result (ignoring the second
λ-Calculus argument)
Concept of λ
Syntax
• Note: (λx. λy . x) M N ≡ (λy . M) N ≡ M
λ-expressions ◦ The fst function is first given an argument, say of type A (of M), and it returns a
Notation
Examples function
Simple
Composition
◦ This (returned) function takes another argument, say of type B (of N), and returns
Boolean the original first argument (of type A)
Numerals
Recursion ◦ Hence, the type of fst is A → (B → A)
Multi-variable
Functions • The token TRUE is not part of the lambda-calculus – just an abbreviation for the term
Higher Order
Functions (λx. λy . x)
C++11
Module M02
Functions
denotes the snd selector function
Composition
Currying
• It takes two arguments and returns the second argument as the result (ignoring the first
λ-Calculus argument)
Concept of λ
Syntax
• Note: (λx. λy . y ) M N ≡ (λy . y ) N ≡ N
λ-expressions ◦ The snd function is first given an argument, say of type A (of M), and it returns a
Notation
Examples function
Simple
Composition
◦ This (returned) function takes another argument, say of type B (of N), and returns
Boolean the same argument (of type B)
Numerals
Recursion ◦ Hence, it has a type A → (B → B)
Multi-variable
Functions • The token FALSE is not part of the lambda-calculus – just an abbreviation for the term
Higher Order
Functions (λx. λy . y )
C++11
Module M02
Partha Pratim • IF should take three arguments b, t, f , where b is a Boolean value and t, f are
Das
arbitrary terms
Relations
• The function should return t if b = TRUE and f if b = FALSE
Functions
Composition • Now (TRUE t f ) ≡ t and (FALSE t f ) ≡ f
Currying
λ-Calculus • IF has to apply its Boolean argument to the other two arguments:
Concept of λ
Syntax
IF = λb. λt. λf . b t f
λ-expressions
Notation
Examples
Simple
Composition
Boolean
• If b is not of Boolean type, the result is undefined
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Partha Pratim • Boolean operators can be defined using IF , TRUE , and FALSE :
Das
Syntax
λ-expressions
Notation
Examples
Simple
• Using the above definitions prove the De Morgan’s Laws of Boolean Algebra
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Functions
• (λx. λy . x y y )(λa. a) b
Composition
Currying
• ((λx. λy . x y y )(λy . y ) y
λ-Calculus • (λx. x x)(λy . y x) z
Concept of λ
Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Functions
• http://www.shlomifish.org/lecture/Lambda-Calculus/slides/lc_church_
Composition ops.scm.html
Currying
λ-Calculus • http://okmij.org/ftp/Computation/lambda-calc.html
Concept of λ
Syntax
• https://en.wikipedia.org/wiki/Church_encoding
λ-expressions
Notation
Examples
Simple
Source: http://www.wikibooks.org; Wikibooks home
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Module M02
Partha Pratim
• A number n will be that number of successors of zero
Das
• If f and x are λ-terms, and n > 0 a natural number, write f n x for the term
Relations f (f (...(f x)...)), where f occurs n times
Functions
Composition
• For each natural number n, we define a λ-term n, called the nth Church Numeral, as
n = λf . λx. f n x
Currying
λ-Calculus
Concept of λ
Syntax
λ-expressions • First few Church numerals are:
Notation
Examples
Simple
C0 = 0 = λf . λx. x
Composition
Boolean
C1 = 1 = λf . λx. (f x)
Numerals
Recursion
C2 = 2 = λf . λx. (f (f x))
Multi-variable
Functions C3 = 3 = λf . λx. (f (f (f x)))
Higher Order
Functions
C++11
Cn = n = λf . λx. f n x
Module M02
Functions
• Apply f on n applications of f (that is, n)
Composition
Currying
• Hence it leads to n + 1 applications of f (that is, n + 1):
λ-Calculus
Concept of λ succ 0 = (λn. λf . λx. (f ((n f ) x))) 0
Syntax
λ-expressions = (λn. λf . λx. (f ((n f ) x)))(λf . λx. x)
Notation
Examples = λf . λx. (f (((λf . λx. x) f ) x))
Simple
Composition = λf . λx. (f (((λg . λy . y ) f ) x))
Boolean
Numerals = λf . λx. (f ((λy . y ) x))
Recursion
Multi-variable = λf . λx. (f x)
Functions
Higher Order
Functions
= 1
C++11
Module M02
Partha Pratim
Das
Relations
succ 1 = (λn. λf . λx. (f ((n f ) x))) 1
Functions = (λn. λf . λx. (f ((n f ) x)))(λf . λx. (f x))
Composition
Currying = λf . λx. (f (((λf . λx. (f x)) f ) x))
λ-Calculus
Concept of λ = λf . λx. (f (((λg . λy . (g y )) f ) x))
Syntax
= λf . λx. (f ((λy . (f y )) x))
λ-expressions
Notation
Examples
= λf . λx. (f (f x))
Simple
Composition
= 2
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Module M02
Partha Pratim • succ = λn. λf . λx. (f ((n f ) x)) goes one step from n
Das
• For addition of m with n, we need to go n steps from m
Relations
Functions
• The addition is defined as:
Composition
Currying
add = λm. λn. λf . λx. ((((m succ) n) f ) x)
λ-Calculus • Compute n successor of m. Apply n applications of f on m
Concept of λ
Module M02
Module M02
Functions
• Apply n applications of f (n) m times
Composition
Currying
λ-Calculus
Concept of λ
Syntax
λ-expressions
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Functions
pred = λn. λf . λx. (((n (prefn f )) (pair x x)) second)
Composition
Currying
Syntax
• Note:
λ-expressions ◦ Kleene discovered how to express the operation of subtraction within Church’s scheme (yes, Church
Notation
Examples
was unable to implement subtraction and subsequently division, within that calculus)!
Simple ◦ Other landmarks then followed, such as the recursive function Y.
Composition ◦ In 1937 Church and Turing, independently, showed that every computable operation (algorithm) can
Boolean
be achieved in a Turing machine and in the Lambda Calculus, and therefore the two are equivalent.
Numerals
Recursion
◦ Similarly Godel introduced his description of computability, again independently, in 1929, using a
Multi-variable third approach which was again shown to be equivalent to the other 2 schemes.
Functions
Higher Order
◦ It appears that there is a ”platonic reality” about computability. That is, it was ”discovered” (3
Functions times independently) rather than ”invented”. It appears to be natural in some sense.
C++11
Source: Natural Numbers as Church Numerals
Principles of Programming Languages Partha Pratim Das M02.59
Church Numerals
Practice Problems
Module M02
Functions
• Show: exp 3 2 = 8
Composition
Currying
• Show: add n 0 = n
λ-Calculus • Show: mul n 1 = n
Concept of λ
Module M02
Functions
takes an argument x, which is apparently a function and applies the function to itself
Composition and returns whatever is the result
Currying
Syntax
• (sa id) = id id = (λx. x) id = id
λ-expressions
Notation
• (sa fst) = fst fst = (λx. λy . x) fst = λy . fst
Examples
Simple
• (sa snd) = snd snd = (λx. λy . y ) snd = id
Composition
Boolean
• (sa twice) = twice twice = (λf . λx. f (f x)) twice = (λx. twice (twice x)) =
Numerals comp twice twice
Recursion
Multi-variable
Functions
• Finally! (sa sa) = sa sa = (λx. x x) sa = sa sa
Higher Order
Functions
◦ Infinite Loop in λ-Calculus, denoted by Ω
C++11
Functions
• Consider:
Composition
Currying
Y t = (λx. t (x x)) (λx. t (x x))
λ-Calculus
Concept of λ = (λy . t (y y )) (λx. t (x x))
Syntax
λ-expressions
= t ( (λx. t (x x)) (λx. t (x x)) )
Notation
Examples
= t (Y t)
Simple
Composition
Boolean
• (Y t) is function t applied to itself! Repeatedly unfolding:
Numerals
Recursion
Multi-variable
Y t = t (Y t) = t (t (Y t)) = t (t (t (Y t))) = · · ·
Functions
Higher Order
Functions • Another form of an infinite loop? No – it is quite useful
C++11
• Used to encode recursive functions in λ-calculus
Principles of Programming Languages Partha Pratim Das M02.62
λ-expressions: Fixed Point
Module M02
• The fixed point of a function f : A → A is a value x ∈ A such that f (x) = x
Partha Pratim
Das • Examples:
Relations
◦ f (x) = x 2 − 3x + 4 has a fixed point f (2) = 2
Functions
◦ f (x) = x 3 has 3 fixed points f (−1) = −1, f (0) = 0, and f (+1) = +1
Composition
◦ cos x = x has a fixed point cos 0.739085133 = 0.739085133
Currying
λ-Calculus
Concept of λ
Syntax
λ-expressions ◦
Notation
Examples
Simple
Composition
Boolean √ √
Numerals ◦ f (x) = x
+ x1 has a fixed point f ( 2) = 2. Starting with x0 = 1, we have: x0 = 1,
2
Recursion
x1 = 1/2 + 1/1 = 3/2, x2 = 3/4 + 2/3 = 17/12, x3 = 17/24 + 12/17 ≈ 1.41421569, · · ·
Multi-variable
Functions ◦ Not all functions have fixed points:
Higher Order
Functions ▷ f (x) = x + 1
C++11 ▷ Collatz Sequence f (n) = (n mod 2)? 3n + 1 : n/2 cycles between 4, 2, 1.
Principles of Programming Languages Partha Pratim Das M02.63
λ-expressions: Fixed Point
Module M02
Relations f x =x
Functions
Composition
Currying
λ-Calculus • Since y f = f (y f )
Concept of λ
Syntax
◦ (y f ) is a fixed point of the function f
λ-expressions ◦ Hence, y is called the fixed point combinator
Notation
Examples ▷ When y is applied to a function, it answers a value x in that function’s domain
Simple
Composition
▷ When we apply the function to x, we get x
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Functions
Composition
define factorial = T factorial
Currying
define T = λf . λn. if (= n 1) 1 (∗n (f (−n 1)))
λ-Calculus
Concept of λ
Syntax
λ-expressions
Notation
• Y T = T (Y T ), is then the factorial
Examples
Simple
Composition factorial = (Y T )
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
• The Fibonacci function in the λ-calculus
Partha Pratim
Das
fibo(n)= fibo(n − 1) + fibo(n − 2), if n > 1
Relations
= 1, if n = 1
Functions
Composition
= 0, if n = 0
Currying • Using the Y combinator, we can define Fibonacci function in the λ-calculus
λ-Calculus • Define function F , whose fixed-point will be Fibonacci:
Concept of λ
Syntax
λ-expressions F = λf . λn. (if (= 0 n) 0 (if (= 1 n) 1 (+ (f (− n 1) f (− n 2)))))
Notation
Examples
Simple
Composition
Boolean
• Then take the fixed point of F :
Numerals fibo = (Y F )
Recursion
Multi-variable
Functions
Higher Order
Functions • Show: fibo(5) = 5
C++11
• Use induction to prove that (Y F ) n = fibo(n)
Principles of Programming Languages Partha Pratim Das M02.67
λ-expressions: Ackermann Function
Module M02 • The Ackermann function A(x, y ) is defined for integers x and y by:
Partha Pratim
Das
A(x, y ) = y + 1, if x = 0
= A(x − 1, 1), if y = 0
Relations = A(x − 1, A(x, y − 1)), otherwise
Functions Special values for x include the following:
Composition
Currying
A(0, y ) = y +1
λ-Calculus A(1, y ) = y +2
Concept of λ A(2, y ) = 2∗y +3
Syntax y +3
A(3, y ) = 2 −3
λ-expressions
2
Notation ..
2.
Examples A(4, y ) = 2 −3
Simple
Composition
Boolean A(0, 2) = 2+1=3
Numerals
A(1, 2) = 2+2=4
Recursion
Multi-variable A(2, 2) = 2∗2+3=7
Functions
2+3
Higher Order A(3, 2) = 2 − 3 = 29
Functions
22
C++11
22 65536
A(4, 2) = 2 −3=2 − 3 = an integer of 19, 729 decimal digits
Principles of Programming Languages Partha Pratim Das M02.68
λ-expressions: Ackermann Function
Module M02
Partha Pratim • The Ackermann function grows faster than any primitive recursive function, that is: for
Das
any primitive recursive function f , there is an n such that
Relations
λ-Calculus
Concept of λ
• So A cannot be primitive recursive
Syntax
λ-expressions • Can we define A in the λ-calculus?
Notation
Examples
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Relations A(x, y ) = y + 1, if x = 0
Functions = A(x − 1, 1), if y = 0
Composition
Currying
= A(x − 1, A(x, y − 1)), otherwise
λ-Calculus
Concept of λ
• Using the Y combinator, we can define Ackermann function in the λ-calculus, even
Syntax
λ-expressions
though it is not primitive recursive!
Notation
• Define function aG , whose fixed-point will be ackermann:
Examples
Simple
Composition aG = (if (= 0 x) (succ y ) (if (= 0 y ) (f (pred x) 1) (f (pred x) (f x (pred y )))))
Boolean
Numerals
Recursion
Multi-variable
Functions
• Then take the fixed point of aG :
Higher Order
Functions
C++11
ackermann = (y aG )
Module M02
Functions
• Many useful functions, such as binary arithmetic operations, require more than one
Composition parameter; for example,
Currying
λ-Calculus
sum(a, b) = a + b
Concept of λ
Syntax
matches the syntactic specification
λ-expressions
Notation
Examples
sum : N × N → N
Simple
Composition
Boolean
where N denotes the natural numbers
Numerals
Recursion
• λ-calculus admits two solutions for this
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Functions
Composition
sum < a, b > = a + b
Currying
λ-Calculus
Concept of λ
Syntax
◦ Pairs can be provided by using a predefined cons operation as in Lisp, or
λ-expressions ◦ Pairing operation can be defined in terms of primitive λ-expressions in the pure
Notation
Examples λ-calculus
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Partha Pratim • Use the curried version of the function with the property that arguments are supplied
Das
one at a time:
Relations add : N → N → N
Functions
Composition where add a b = a + b
Currying
λ-Calculus • Now
Concept of λ
(add a) : N → N
Syntax
λ-expressions
Notation
is a function with the property that
Examples
Simple
Composition
((add a) b) = a + b
Boolean
Numerals
Recursion
Thus, the successor function can be defined as (add 1)
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Partha Pratim • The operations of currying and uncurrying a function can be expressed in the λ-calculus
Das
as
Relations
define Curry = λf . λx. λy . f < x, y >
Functions
Composition define Uncurry = λf . λp. f (head p)(tail p)
Currying
λ-Calculus
provided the pairing operation < x, y >= (cons x y ) and the functions (head p) and
Concept of λ (tail p) are available, either as predefined functions or as functions defined in the pure
Syntax λ-calculus
λ-expressions
Notation • The two versions of the addition operation are related as:
Examples
Simple Curry sum = add and Uncurry add = sum
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Functions
Composition
define Twice = λf . λx. f (f x)
Currying
λ-Calculus
Concept of λ
Syntax • Twice is a polymorphic function as it may be applied to any function and element as
λ-expressions long as that element is in the domain of the function and its image under the function
Notation
Examples is also in that domain
Simple
Composition • The mechanism that allows functions to be defined to work on a number of types of
Boolean
Numerals
data is also known as parametric polymorphism
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Partha Pratim • If D is any domain, the syntax (or signature) for Twice can be described as
Das
Relations Twice : (D → D) → D → D
Functions
Composition
Currying
Given the square function, sqr : N → N where N stands for the natural numbers, it
λ-Calculus follows that
Concept of λ (Twice sqr ) : N → N
Syntax
λ-expressions
Notation
Examples is itself a function. This new function can be named
Simple
Composition
Boolean define FourthPower = Twice sqr
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Functions
• Power of a functional programming language lies in its ability to define and apply
Composition higher-order functions
Currying
λ-Calculus ◦ functions that take functions as arguments and/or return a function as their result
Concept of λ ◦ Twice is higher-order since it maps one function to another
Syntax
λ-expressions
Notation
Examples Source: Higher-order functions in Multiple Languages
Simple
Composition
Boolean
Numerals
Recursion
Multi-variable
Functions
Higher Order
Functions
C++11
Module M02
Partha Pratim • Function objects (Functors) are objects specifically designed to be used with a syntax
Das
similar to that of functions. In C++, this is achieved by defining member function
Relations operator() in their class, like for example:
Functions
Composition
Currying
// Function Objects
λ-Calculus
Concept of λ
struct myclass {
Syntax int operator()(int a) { return a; }
λ-expressions } myobject;
Notation
Examples
Simple
Composition
int x = myobject(0); // function-like syntax with object myobject
Boolean
Numerals
Recursion • They are typically used as arguments to functions, such as predicates or comparison
Multi-variable
Functions functions passed to standard algorithms.
Higher Order
Functions
C++11
Source: <functional> in STL
Principles of Programming Languages Partha Pratim Das M02.78
C++11: λ
Module M02
#include <iostream>
Partha Pratim
Das
#include <functional> // Provides template <class Ret, class... Args> class function<Ret(Args...)>;
using namespace std;
Relations
// lambda expressions
Functions
auto f = [](int i) { return i + 3; };
Composition
auto sqr = [](int i) { return i * i; };
Currying
auto twice = [](const function<int(int)>& g, int v) { return g(g(v)); };
λ-Calculus auto comp = [](const function<int(int)>& g, const function<int(int)>& h, int v) { return g(h(v)); };
Concept of λ