What is λcalculus Lambda calculus is a formal system in mathematical logic and computer science for expressing computation based on function abstraction and application using variable ID: 487614
Download Presentation The PPT/PDF document "Lambda calculus" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Lambda calculus
Slide2
What is λ-calculus ?
“Lambda calculus is a formal
system
in
mathematical logic and computer science for expressing computation based on function abstraction and application using variable binding and substitution” --Wikipedia
Slide3
Lambda calculus in history of mathematics
The lambda calculus was introduced by mathematician Alonzo Church in the 1930s . The original system was shown to be logically inconsistent in 1935 when Stephen Kleene and J. B. Rosser developed the Kleene–Rosser paradox.Subsequently, in 1936 Church isolated and published just the portion relevant to computation, what is now called the untyped lambda
calculus.
The λ-calculus was re-discovered as a versatile tool in Computer Science by people like McCarthy, Landin, and Scott in the 1960s. Mc-Carthy came out with his list processing language Lisp in 1960. McCarthy knew of the λ-calculus, and his language closely resembles it. Now used as • Tool for investigating computability • Basis of functional programming languages Lisp, Scheme, Haskell,ML…Slide4
Expressions in the λ-calculus
Expressions in the λ-calculus are written in strict prefix form
Function and argument are simply written next to each other. If a function takes more than one argument, then these are simply lined up after the function.Thus:
Brackets are employed only to enforce a special
grouping
*The
-
calculus is a purely syntactic
device,
it does not make
any distinctions between simple entities
Slide5
Functions in the λ-calculus
Function formation is sometimes written as an equation, , sometimes as a mapping
.
In
the λ-calculus a special notation is available - The Greek letter λ.The λ alerts that the variable is not part of an expression but the formal parameter of the function . The dot after the formal parameter introduces the function body. A function which has been written in λ-notation can itself be used in an expression.
why the brackets around the function?
They
are there to make clear where the definition of
the function
ends
.
(assuming that is interpreted as a 3-ary function) Slide6
Functions in the λ-calculus
Although it is not strictly necessary, it will be convenient to introduce abbreviations for-terms.
If body
of a function consists of
another function, as here we could also consider it as a function of two arguments.If we want to see it as a function of two arguments then we can leave out the brackets:
Slide7
The official definitionFunction formation and function application are all that there is.
λ-terms are constructed according to the grammar:
The placeholder
c represents any
constant, such as numbers 1, 2, 3,... or arithmetic operators etc. the letter x represents any of infinitely many possible variables.The given grammar is ambiguous; the term could be parsed as*(we use “app” to indicate use of the clause in the derivation)
Slide8
- Reduction There is only one rule of computation, called reduction
, it concerns the replacement of a formal
parameter by
an actual one.
5
* 3 5
When no
further reductions are possible, we say that the term has been reduced to
normal form
.
Is every
term has a normal form ? NO !
The
term Ω always reduces to
itself!
Slide9
ConfluenceIt may be that a λ-term offers many opportunities for reduction at the same time.
it is necessary that the result of a computation is independent from the order of reductionTheorem 1 (Church-Rosser) If a term M can be reduced (in several steps) to terms N and P, then there exists a term Q to which both N and P can be reduced (in several steps).As a picture: Slide10
Confluence
Theorem 1 (Church-Rosser) If a term M can be reduced (in several steps) to terms N and P, then there exists a term Q to which both N and P can be reduced (in several steps).Intuition:Lets look at a specific case where every variable can appear 0/1 time in a term.Base case - divided into 3 cases:
2.
Slide11
Confluence
3.
And if you believed me up until now the rest is very simple…
.
.
.
.
.
.
.
.
.Slide12
ConfluenceCorollary 2 Every λ-term has at most one normal form.
Proof. For the sake of contradiction, assume that there are normal forms N and P to whicha certain term M reduces:By the theorem of Church and Rosser there is a term Q to which both N and P can be reduced. However, N and P are assumed to be in normal form, so they don’t allow for any further reductions. The only possible interpretation is that N = P = Q. Slide13
Free and bound variables
The operator is a binding operator. Variables that fall within the scope of an abstraction are said to be bound. All other variables are called free.
Note:
variable is bound by its "nearest" abstraction (the single occurrence of x in the expression is bound by the second lambda)Formal Definition:The set of free variables of a lambda expression, M, is denoted as FV(M) and is defined by recursion on the structure of the terms, as follows:FV(x) = {x}, where x is a variableFV(λx.M) = FV(M) \ {x}FV(M N) = FV(M) ∪ FV(N) Slide14
conversion
Alpha - conversion, allows bound variable names to be
changed
The only variable occurrences that are renamed are those that are bound to the same abstraction Alpha-conversion is not possible if it would result in a variable getting captured by a different abstraction
(
(
(
Slide15
conversion
Two functions are the same
if and only if they give the same result for all arguments.Eta - conversion converts between and whenever does not appear free in
Slide16
Higher-order functions
Let us look at an example: A -term for squaring integers is given by
If we want to compute
In
-calculus notation, we would write for the “power-8”-function
:
It is now a simple step to write out a
-
term which applies
any function
three times
: Operators such as T are called higher order because they operate on functions rather than numbers.
Slide17
Iteration and recursion
How can we generalize what we saw, in the high order function, to get the behavior of a for-loop?First of all let us define some helpful new “constants”: 1
-
Its
behavior is like an if-then-else clause: (In , we would write this as 2.
“pred”
for predecessor function on natural
numbers
.
Slide18
Iteration and recursion
Now we construct a term (for “Iteration”) which takes as arguments a number, a function , and a value
, and computes the
application of to : (If then should simply return )Example:
Slide19
Well-typed -
terms there is nothing in the grammar which stops us from forming awful terms, such as “sin log”.Such terms do not make any sense at
all,
and any sensible programming
language compiler would reject them.What is missing in the calculus is a notion of type. For example, the type of the sin function should be “accepts real numbers and produces real numbers”.A language for expressing these properties (i.e., types) is easily defined- We start with some base types such as “int” and “real”, and then form function types on top of them.The grammar: c represents all the base types Slide20
Well-typed -
terms On the basis of a type system, we can formulate restrictions on what kind of terms are valid (or
well-typed
).
We do so by employing an inductive definition:Definition (Well-typed -terms):Base case. For every type and every variable , the term is well-typed and has type.Function formation. For every term of type , every variable , and every type , the termis well-typed and has type
.
Application.
If
is well-typed of
type
and
is well-typed of type then is well-typed and has type
O
Slide21
Well-typed -
terms Calculating simple types:It is quite easy to find out whether a term can be typed or not
by following the steps in which the term was
constructed.
What we do is to annotate subterms with type expressions which still contain type variables A,B,C, . . . and which we refine as we go along.Consider for example, the term
)
).
*
At this stage the type variables can be instantiated with something more concrete (such as “
int
” or
“real”) but we only wanted to establish typability and so we can stop here.
Slide22
Well-typed -
terms Calculating simple types:
Further refinement is required if we extend the term
to
Finally, if we spell out the types in the
term we get:
Theorem
Every well-typed
-
term has a normal form
.
Slide23
Slide24
The -
calculus as a model of computation Turing-completeA computational system that can compute every Turing-computable function is called Turing complete
We call a calculus Turing-Complete if it allows one to define all
computable
function from N to N .The -calculus is Turing-complete !Church encoding:Terms that are usually considered primitive in other notations (such as integers, Boolean) are mapped to higher-order functions under Church encoding.Church numerals- a representation of the natural numbers using lambda notation Slide25
The -
calculus as a model of computation
…
Computation with Church
numerals:
For example:
Addition – uses the identity
But
programs would be :
Pretty slow
Pretty large
Pretty hard to understand.
Slide26
From Theory to Programming LanguageAlthough the lambda-calculus is powerful enough to express any program, this doesn't mean that you'd actually
want to do so. After all, the Turing Machine offers an equally powerful computational basis.Which lead us to Functional Programming…Functional programming has its roots in lambda calculus – lambda calculus provides a theoretical framework for describing functions and their
evaluation. Although it is a mathematical abstraction rather than a programming language, it forms the basis of almost all functional programming languages today.
Many
functional programming languages can be viewed as elaborations on the lambda calculus. Slide27
From Theory to Programming Language
Functional Programming: “ functional programming is a programming paradigm, a style of building the structure and elements of computer programs, that treats computation as the evaluation of mathematical functions and avoids state and mutable data. “ Functional programming emphasizes functions that produce results that depend only on their inputs and not on the program state - i.e. pure mathematical functions.In functional code, the output value of a function depends only on the arguments that are input to the
function.
So calling
a function twice with the same value for an argument x will produce the same result both times. --Wikipedia Slide28
Similarity to Functional Programming
Pascal:
…<statements >… end;
ML:
Scheme:
Slide29