Lambda Calculuscs-gw.utcluj.ro/~adrian/pf/pf_13.pdf · 2012-01-10 · λ-calculus Operational...
Transcript of Lambda Calculuscs-gw.utcluj.ro/~adrian/pf/pf_13.pdf · 2012-01-10 · λ-calculus Operational...
Lambda Calculus
Adrian Groza
Department of Computer ScienceTechnical University of Cluj-Napoca
January 3, 2011
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Outline
1 λ-calculus
2 Operational SemanticsSyntaxConversionsNormal Form
3 Lambda Calculus as a Functional LanguageAritmethic OperationsBoolean ExpressionsTuples
4 Food for Tought
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
What is Computable?
Computation is usually modelled as a mapping from inputs tooutputs, carried out by a formal ”machine” or program, whichprocesses its input in a sequence of steps.
An ”effectively computable” function is one that can becomputed in a finite amount of time using finite resources.
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Computation Models
Turing Machine (Turing, 1936)modifies the content of the memory and of aninput tape whose movement is controllablecorresponds to imperative languages where thestate of the computation process is representedand explicitly processed
Lambda Calculus (Church, 1932)works on unary functionsrelies on function application ⇔ the substitutionin the function body of the formal parameter withthe effective parameterprocessing without state
(1912-1954)
(1903-1995)
Church-Turing thesis
If an algorithm (a procedure that terminates) exists then there is anequivalent Turing Machine or applicable λ-function for that algorithm.
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Lambda Calculus
1 universal: any computable function can be expressed andevaluated using this formalism
2 to directly express higher-order functions3 to study evaluation orders, termination, uniqueness of
answers4 to serve as a kernel language for functional languages5 the smallest universal programming language of the world
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Outline
1 λ-calculus
2 Operational SemanticsSyntaxConversionsNormal Form
3 Lambda Calculus as a Functional LanguageAritmethic OperationsBoolean ExpressionsTuples
4 Food for Tought
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Syntax
Syntax
E = x | λx . E | E1 E2
x - variableλx.E - functional abstraction (x - bound variable, E - body)E1 E2 - function application (E1 function, E2 argument)
<expression> := <name> | <function> | <application><function> := λ <name>.<expression><application> := <expression><expression>
Examples:x λ-expression reduced to a variableλx.x is a function taking an argument x, and returning x (identity fun)λx. λy.x function which selects the first value from a pair of valuesf x is a function f applied to an argument x (same as f(x))(λx.x) y applying the function λx.x on the argument y
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Syntax
Examples
(λx. x x) (λy. y) ≡ = (λy. y)(λx.(x + 5)) 3 = 3+5 = 8((λx.(λy.x + y)) 3) 5 = (λy.3 + y)) 5 = 3+5=8(λ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+1
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Syntax
Parsing Lambda Expressions
1 Lambda extends as far as possible to the rightλf.x y≡ λ f.(x y)λx.E1 E2 E3≡ λ x.(E1 E2 E3)λx.x λz.x z x ≡λx.(x λz.(x z x))
2 Application is left-associativex y z ≡ (x y) z
3 Parantheses are needed for (λx.E1 E2) E3 where E3 is anargument to the function λx.E1 E2, not part of the body ofthe function
4 Multiple lambdas may be suppressedλ f g . x ≡ λ f . λ g . x
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Syntax
Functions with Many Arguments
We can’t yet write functions with many arguments - forexample, two arguments: λ(x,y).eSolution: take the arguments, one at a time
λx.λy.eA function that takes x and returns another function thattakes y and returns e(λx.λy.e) a b → (λy.e[a/x]) b → e[a/x][b/y]This is called CurryingCan represent any number of arguments
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Syntax
Free and Bound Variables
In function λx.t , the variable x is bound; t is the body of thefunctionA variable which is not bound is free.
Examples:(λx . x y) x is bound, y is free(λx . x)(λy . y x ) x is bound in the first expression, x is independent of x(λz . (λx . (y x))) x is bound, y is free
Observations:Name of bound var does not matter: λx.(3+x) = λy.(3+y)Name of free var does matter: λx.(x+y) 6= λx.(x+z)Same identifier can occur free and bound in the sameexpression: (λx.xy)(λy.y)
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Conversions
α-conversion
Conversion = rule for transforming a λ-expression into aanother one with the same meaning.
α-conversion: renaming bound variables(λx .t) ≡α (λy .t [y/x ])
Example(λx .ax) ≡α (λy .ay)(λx .(x(λy .(yx)))) ≡α (λz.(z(λy .(yz))))[z/x ](λx .x(λx .ax))b ≡α (λy .y(λx .ax))b[y/x ]
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Conversions
β-conversion
β-conversion: applying abstractions to arguments((λx .t)u) ≡β (t [u/x ])
Example((λx .((fx)x)(ga) ≡β ((f (ga))(ga))((λz.(za)(λx .x) ≡β (λx .x)a ≡β a((λx .λy .y)a)b) ≡β ((λy .y))b
≡ (λy .y)b≡β b
β-conversion represents the computationalengine of the lambda calculus.
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Conversions
η-conversion
η-conversion: eliminates redundant lambda-expressions(λx .fx)y ≡η fy
Every time when x is not free in f, we can write:
(λ x . f x ) = f
Example(λ b . y b) ≡ y(λ z . (λ x . x y) z) ≡ λ x . x y
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Conversions
Applying Conversions
Conversions1 (λx .t) ≡α (λy .t [y/x ])
2 ((λx .t)u) ≡β (t [u/x ])
3 (λx .fx)y ≡η fy
(λ x y . x y) (λ x . x y) (λ a b . a b) NB: left assoc≡α (λ x z . x z) (λ x . x y) (λ a b . a b) α-conversion [z/x]≡β (λ z . (λ x . x y) z) (λ a b . a b) β-reduction [(λ x . x y)/x]≡η (λ x . x y) (λ a b . a b) η-reduction (λ z . f z )= f≡β (λ a b . a b) y β-reduction [(λ a b . a b)/x]≡β (λ b . y b) β-reduction [(y/a]≡η y η-reduction(λ z . f z)= f
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Conversions
Nested expressions
Syntax simplification:
(λx1 . (λx2 . ...(λxn . t)...)) ≡ (λx1x2...xn. t)
(λx y.y x) y b ≡ (λx . (λy. y x)) y b≡α (λx . (λz. z x)) y b≡β (λz . z y) b≡β b y
(λx y.y x) y b ≡ (λx . (λy. y x)) y b≡β(λy . y y) b≡β b b
Don’t accidentally bind a free variable!
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Normal Form
Normal Form
A λ-expression is said to be in the normal form if no furtherreduction steps could be applied to it.
Example
(λ x. a x)((λ y. b y) c) ≡α (λ z. a z)((λ y. b y) c)≡β a ((λ y. b y) c)≡ a (b c)
Not all expressions have a normal form(λ x . x x) (λ x . x x) ≡β (x x)[(λ x . x x)/x]
≡ (λ x . x x) (λ x . x x)≡ ... ⇔ the program never ends.
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Normal Form
Church-Rosser Thesis
ThesisTwo sequences of reductions applied to a λ-expressions willalways result in equivalent normal forms.
Thus the normal form does not depend on the order thereductions are performed.(λ x. a x)((λ y. b y) c) ≡α (λ z. a z)((λ y. b y) c)
≡β a ((λ y. b y) c)≡ a (b c)
(λ x. a x)((λ y. b y) c) ≡β (λx. a x)(b c)≡βa (b c)
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Normal Form
Call by name and call by value in λ-calculus
Call by name(λ y . a) ((λ x . x x) (λ x . x x))≡β aargument is not evaluated and it is substitued in the body of thefunction.
Call by value(λ y . a) ((λ x . x x) (λ x . x x)) ≡β (λ y . a) ((λ x . x x) (λ x . x x))
This type of substitution always finds the normal form, providedthat such a form actually exists.
(\x -> \y -> x) 1 (5/0) = 1
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Outline
1 λ-calculus
2 Operational SemanticsSyntaxConversionsNormal Form
3 Lambda Calculus as a Functional LanguageAritmethic OperationsBoolean ExpressionsTuples
4 Food for Tought
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Aritmethic Operations
λ-Calculus Expressivity
In the lambda calculus we can only define new functions.How can we program with only functions?
IdeaEncode the behavior of values and not their structure.
Introducing variableslet x = e1 in e2 ⇔ (λx.e1) e2
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Aritmethic Operations
Aritmethic
Can we represent numbers in λ-calculus?Numbers will be defined as ... functions:0 := λsz.z1 := λsz.s(z)2 := λsz.s(s(z))...
DefinitionA natural number is a function that given an operation s and astarting value z, applies s a number of times to z.
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Aritmethic Operations
Successor Function
Our first interesting function...
S ≡ λwyx.y(wyx)
S 0 ≡ λw y x . y(wyx) (λsz . z)≡β λy x . y ((λsz . z) y x)≡β λy x . y ((λz . z) x)≡β λy x . y (x)≡α λs x . s(x)≡1
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Aritmethic Operations
Summing numbers
Our second interesting function...
PLUS ≡ λ mnfx . nf (mfx)
PLUS 1 2 ≡ (λ mnfx.nf (mfx))1 2≡ββ λ fx.2f (1fx)≡ λ fx.2f ((λ fx.f (x)) fx)≡η λ fx.2f (f (x))≡ λ fx.(λ fx.f (f (x)))f (f (x))≡ λ fx.f (f (f (x)))≡α 3
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Boolean Expressions
Boolean Expressions
What is ”true”?True is something that used as the first parameter of if makesthe result of if to be its second parameter
if T M N ⇒ M
if ≡ λ p. λthen.λelse. p then elseif ≡ λp . (λ c . (λ a . pca))) ≡ λpca . pca
A boolean is a function that given two choices selects one of themT ≡ λx . (λy. x) ≡ λx y . xF ≡ λx . (λy. y) ≡ λx y . y
if T M N ≡ λp c a . p c a (λx y . x) M N≡β (λc a . (λx y . x) c a) M N≡β (λa . (λx y . x) a) M N≡β (λx y . x) M N≡β (λy . M) N ≡βM
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Boolean Expressions
Logical Operators
And, Or, Notand ≡ λx (λy if x y F)≡ λxy. x y For ≡ λx (λy if x T y) ≡ λxy. x T ynot ≡ λx . x F T
1 (λx .t) ≡α (λy .t [y/x ])
2 ((λx .t)u) ≡β (t [u/x ])
3 (λx .fx)y ≡η fy
and T F ≡ (λx y. x y F) T F≡β (λy. T y F) F≡β T F F≡β (λx y . x) F F≡β (λy . F) F≡β F
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Boolean Expressions
Logical Operators
And, Or, Notand ≡ λx (λy if x y F)≡... λxy. x y For ≡ λx (λy if x T y) ≡ λxy. x T ynot ≡ λx . x F T
1 (λx .t) ≡α (λy .t [y/x ])
2 ((λx .t)u) ≡β (t [u/x ])
3 (λx .fx)y ≡η fy
and T F ≡ (λx y. x y F) T F≡β (λy. T y F) F≡β T F F≡β (λx y . x) F F≡β (λy . F) F≡β F
not T ≡ (λ b . b F T ) (λ x y . x )≡β (λ x y . x) F T≡β F
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Boolean Expressions
Testing if a number is zero
Another interesting function ...
Z ≡ λx.xF¬F
Z 0 ≡ λx.xF¬F 0≡ 0F¬F≡ (λs z . z) F¬F≡ ¬F≡ T
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Boolean Expressions
Testing if a number is zero
Z ≡ λx.xF¬F
Z 0 ≡ λx.xF¬F 0≡ 0F¬F≡ (λs z . z) F¬F≡ ¬F≡ T
Z 1 ≡ λx.xF¬F 1≡ 1F¬F≡ (λsz.s(z))F¬F≡ F(¬)F≡ (λxy.y)(¬)F≡ F
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Tuples
Defining pairs
PAIR ≡ λxyf.fxyFIRST ≡ λ p. p TSECOND ≡ λ p. p F
First (Pair (a b)) ≡ (λp.p T) (PAIR a b)≡ (PAIR a b) T≡ (λxyf.fxy a b) T≡ (λf. f a b) T≡ T a b≡ a
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Tuples
Recursion
A self-regenerating function...
Y ≡ λy.(λx.y(xx))(λx.y(xx))
YR ≡ λy.(λx.y(xx))(λx.y(xx))R≡ (λx.R(xx))(λx.R(xx))≡ R((λx.R(xx))(λx.R(xx)))≡ R(YR)
When Y is applied on an arbitrary function R, R will call itself on(YR). Subsequent equivalent forms appear when R getsexpanded according to its definition. It is R’s responsibility tobreak the recursive loop at some moment by providing acondition for stopping.
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Tuples
Recursion - Example
fact = λ f. λ n.if n = 0 then 1 else n * (f (n-1))(Y fact) 1 = (fact (Y fact)) 1
−→ if 1 = 0 then 1 else 1 * ((Y fact) 0)−→1 * ((Y fact) 0)−→1 * (fact (Y fact) 0)−→1 * (if 0 = 0 then 1 else 0 * ((Y fact) (-1))−→1 * 1−→1
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Outline
1 λ-calculus
2 Operational SemanticsSyntaxConversionsNormal Form
3 Lambda Calculus as a Functional LanguageAritmethic OperationsBoolean ExpressionsTuples
4 Food for Tought
λ-calculus Operational Semantics Lambda Calculus as a Functional Language Food for Tought
Readings
1 Mandatory reading: Chapter 8: pages107-119
2 Optional reading: A Tutorial Introduction to theLambda Calculus, Raul Rojas, FU Berlin,Germany, WS-97/98