COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence...
Embed Size (px)
Transcript of COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence...
![Page 2: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/2.jpg)
Outline
Introduction
Lambda Calculus TermsAlpha EquivalenceSubstitution
DynamicsBeta ReductionEta ReductionNormal FormsEvaluation Strategies
Conclusion
![Page 3: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/3.jpg)
The Lambda Calculus
Alonzo Church(1903–1995)
The λ-calculus is a fundamentalcomputational model.
It also has a number of connectionsto logic.
The next five lectures (including this one) willconcentrate on the untyped calculus.
The typed calculus has the most elegant con-nections to logic, so we will focus on computa-tion.
But there is also an interesting equational logicto consider.
Introduction 3/30
![Page 4: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/4.jpg)
Course Plan
By me:
1. Basics (today)
2. Equational LogicI connection to −→∗
βI examplesI inconsistent assumptionsI soundness & completeness
3. Soundness via Church-Rosser
4. Standardisation
5. ComputationI Church NumeralsI Encoding Other TypesI Encoding the Recursive Functions
And then, five lectures on the typed calculus by Jeremy Dawson.
Introduction 4/30
![Page 5: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/5.jpg)
Some Sources
H. P. Barendregt, The Lambda Calculus:Its Syntax and Semantics. Elsevier, 1984.ISBN 0 444 87508 5.
Chris Hankin, Lambda Calculi: A Guidefor Computer Scientists. Oxford UniversityPress, 1994. ISBN 0 19 853840 3.
Introduction 5/30
![Page 6: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/6.jpg)
What are Lambda Terms?
Lambda terms make up the world’s simplest programminglanguage.
A lambda term is either
I a variable v; or
I the application of term M to term N, written M N; or
I the abstraction of variable v in term M, written (λv.M).
Examples:
I f x — f, a “function”, applied to an argument x
I (f x) y — a function applied to two arguments
I f (g x) — two function calls
I (λv. v) — the identity function
I (λu. (λv. u z)) x — abstraction and application
Lambda Calculus Terms 6/30
![Page 7: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/7.jpg)
Term Shorthands
Applications are left-associative.So, you can write M N P, and don’t have to write (M N) P
(You do have to write M (N P) with the parentheses.)
You can “chain” binders.
Instead of (λu. (λv.M)), just write (λu v.M)
Thus:S = (λab c. a c (b c))
instead ofS = (λa. (λb. (λc. (a c) (b c))))
Think of abstract syntax trees if necessary.
Lambda Calculus Terms 7/30
![Page 8: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/8.jpg)
Abstractions are (Anonymous) Functions
In secondary school mathematics, you learn to write things like
f(x) = x2 + 2x+ 1
meaning that f is a function that takes a parameter x and returnsa value derived from that x.
When you learnt to program, you might have learnt to write
int f(int x)return x*x + 2*x + 1;
instead.
Lambda Calculus Terms 8/30
![Page 9: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/9.jpg)
Abstractions are (Anonymous) Functions
In λ-calculus (with arithmetic added), you might write
f = (λx. x2 + 2x+ 1)
In other words,(λx. x2 + 2x+ 1)
is a function that takes a value x as a parameter, and calculates a“return value”.
This λ-term is a function without a name. We can decide to give itthe name f (or g, or nothing) later.
Lambda Calculus Terms 9/30
![Page 10: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/10.jpg)
Free and Bound Names
Consider: (λv. u v)
The variable v is bound. It’s the name of a parameter.
The variable u is free.It’s not the name of an enclosing parameter.
The same notions are apparent in other languages:
int f(int v)return u + v;
Lambda Calculus Terms Alpha Equivalence 10/30
![Page 11: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/11.jpg)
Bound Names Can be Renamed
These two functions are the same:
int f(int v)return u + v;
int f(int x)
return u + x;
But this one is different:
int f(int u)
return u + u;
Clearly, not all bound variable renaming is OK.
Lambda Calculus Terms Alpha Equivalence 11/30
![Page 12: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/12.jpg)
Alpha Equivalence
Two λ-terms M and N are alpha-equivalent (written M ≡ N) ifthey
“are the same up to renaming of bound variables”
Proof rules
v ≡ vM1 ≡M2 N1 ≡ N2M1 N1 ≡M2 N2
v not free in M N ≡ (uv) ·M(λu.M) ≡ (λv.N)
where (uv) ·M = “swap u and v everywhere they appear in M”
Lambda Calculus Terms Alpha Equivalence 12/30
![Page 13: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/13.jpg)
Alpha Equivalence—Examples
Rule for (λv.M) again:
v not free in M N ≡ (uv) ·M(λu.M) ≡ (λv.N)
where (uv) ·M = “swap u and v everywhere they appear in M”So,
(λv. v u) x ≡ (λw.w u) x 6≡ (λu. u u) x
(λu. (λv. u v) u) ≡ (λv. (λu. v u) v) 6≡ (λu. (λu. u v) u)
(λx. (λy. f y) x) ≡ (λy. (λy. f y) y) 6≡ (λf. (λy. f y) f)
Lambda Calculus Terms Alpha Equivalence 13/30
![Page 14: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/14.jpg)
Alpha Equivalence—Examples
Rule for (λv.M) again:
v not free in M N ≡ (uv) ·M(λu.M) ≡ (λv.N)
where (uv) ·M = “swap u and v everywhere they appear in M”So,
(λv. v u) x ≡ (λw.w u) x 6≡ (λu. u u) x
(λu. (λv. u v) u) ≡ (λv. (λu. v u) v) 6≡ (λu. (λu. u v) u)
(λx. (λy. f y) x) ≡ (λy. (λy. f y) y) 6≡ (λf. (λy. f y) f)
Lambda Calculus Terms Alpha Equivalence 13/30
![Page 15: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/15.jpg)
Alpha Equivalence—Examples
Rule for (λv.M) again:
v not free in M N ≡ (uv) ·M(λu.M) ≡ (λv.N)
where (uv) ·M = “swap u and v everywhere they appear in M”So,
(λv. v u) x ≡ (λw.w u) x 6≡ (λu. u u) x
(λu. (λv. u v) u) ≡ (λv. (λu. v u) v) 6≡ (λu. (λu. u v) u)
(λx. (λy. f y) x) ≡ (λy. (λy. f y) y) 6≡ (λf. (λy. f y) f)
Lambda Calculus Terms Alpha Equivalence 13/30
![Page 16: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/16.jpg)
SubstitutionSubstitution is a ternary operation:
M[v := N]
means “replace free occurrences of v in M with N”(making sure that free variables in N are not captured by binders in M)
(alpha-convert M as necessary)
Conditions:
I Freeness: (λv.M)[v := N] ≡ (λv.M)(as v is not free in (λv.M))
I Capture-avoiding:
(λv. u v)[u := v] 6≡ (λv. v v) (!!!)
(λv. u v)[u := v] ≡ (λw.u w)[u := v]≡ (λw. v w)
Lambda Calculus Terms Substitution 14/30
![Page 17: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/17.jpg)
SubstitutionSubstitution is a ternary operation:
M[v := N]
means “replace free occurrences of v in M with N”(making sure that free variables in N are not captured by binders in M)
(alpha-convert M as necessary)
Conditions:
I Freeness: (λv.M)[v := N] ≡ (λv.M)(as v is not free in (λv.M))
I Capture-avoiding:
(λv. u v)[u := v] 6≡ (λv. v v) (!!!)
(λv. u v)[u := v] ≡ (λw.u w)[u := v]≡ (λw. v w)
Lambda Calculus Terms Substitution 14/30
![Page 18: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/18.jpg)
Substitution: Please Take Care!
Substitution is tricky!
You have seen it before (in handling f.o.l. quantifiers)
Use the Barendregt Variable Convention:rename bound variables so they don’t overlap with free variables.
Lambda Calculus Terms Substitution 15/30
![Page 19: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/19.jpg)
Substitution Drives Computation
A General Programming Language Axiom:
When you apply a function to an argument, the result isas if you substituted the actual argument for the formalparameter and then performed the specified computation
For example, how do we figure out what f(3,5) will return?
int f(int x, int y)
return 2*x + y;
Dynamics Beta Reduction 16/30
![Page 20: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/20.jpg)
Beta Reduction: λ-Terms Doing ThingsSubstitution of “actuals for formals” is the essence ofbeta-reduction:
(λv.M) N −→β M[v := N]
Reduction can occur anywhere within a term.
Examples:
(λv. v) (λx. x v) −→β (λx. x v)
(λu. (λv. v z) (u z)) −→β (λu. (u z) z)
(λu. (λv. v u) (u z)) v z −→β (λw.w v) (v z) z−→β (v z) v z
(λx. x x) (λy. y y) −→β (λy. y y) (λy. y y)≡ (λx. x x) (λy. y y)
Dynamics Beta Reduction 17/30
![Page 21: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/21.jpg)
Beta Reduction: λ-Terms Doing ThingsSubstitution of “actuals for formals” is the essence ofbeta-reduction:
(λv.M) N −→β M[v := N]
Reduction can occur anywhere within a term.
Examples:
(λv. v) (λx. x v) −→β (λx. x v)
(λu. (λv. v z) (u z)) −→β (λu. (u z) z)
(λu. (λv. v u) (u z)) v z −→β (λw.w v) (v z) z−→β (v z) v z
(λx. x x) (λy. y y) −→β (λy. y y) (λy. y y)≡ (λx. x x) (λy. y y)
Dynamics Beta Reduction 17/30
![Page 22: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/22.jpg)
Beta Reduction: λ-Terms Doing ThingsSubstitution of “actuals for formals” is the essence ofbeta-reduction:
(λv.M) N −→β M[v := N]
Reduction can occur anywhere within a term.
Examples:
(λv. v) (λx. x v) −→β (λx. x v)
(λu. (λv. v z) (u z)) −→β (λu. (u z) z)
(λu. (λv. v u) (u z)) v z −→β (λw.w v) (v z) z−→β (v z) v z
(λx. x x) (λy. y y) −→β (λy. y y) (λy. y y)≡ (λx. x x) (λy. y y)
Dynamics Beta Reduction 17/30
![Page 23: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/23.jpg)
Beta Reduction: λ-Terms Doing ThingsSubstitution of “actuals for formals” is the essence ofbeta-reduction:
(λv.M) N −→β M[v := N]
Reduction can occur anywhere within a term.
Examples:
(λv. v) (λx. x v) −→β (λx. x v)
(λu. (λv. v z) (u z)) −→β (λu. (u z) z)
(λu. (λv. v u) (u z)) v z −→β (λw.w v) (v z) z−→β (v z) v z
(λx. x x) (λy. y y) −→β (λy. y y) (λy. y y)≡ (λx. x x) (λy. y y)
Dynamics Beta Reduction 17/30
![Page 24: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/24.jpg)
Beta Reduction Rules and Notation
(λv.M) N −→β M[v := N]
M −→β M′
M N −→β M′ N
N −→β N′
M N −→β M N ′
M −→β M′
(λv.M) −→β (λv.M ′)
Write M −→∗β N to mean M can take zero or more β-reductionsteps and evolve to N.
Write M −→+β N to mean M can take one or more β-reduction
steps and evolve to N.
Dynamics Beta Reduction 18/30
![Page 25: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/25.jpg)
Beta Reduction Does It All!
Lambda Terms + Beta Reduction = All Computation
Any computation can be encoded in what we have just seen.
We will look at computational aspects of the λ-calculus in detail inlater lectures.
In particular, it is easy to encode
I Numbers
I Recursion
And that’s all you need.
Dynamics Beta Reduction 19/30
![Page 26: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/26.jpg)
Eta Reduction
We can also add another computational rule, η-reduction:
v not free in M(λv.M v) −→η M
(Can perform η-reduction anywhere within a term, as with β.)
We will later see how η ties into extensionality.
Eta-reduction and eta-expansion (!) also play a role in thetyped λ-calculus.
Dynamics Eta Reduction 20/30
![Page 27: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/27.jpg)
Can Combine Beta and Eta
For example:
(λf. f x) (λy. g y)−→βη (λf. f x) g−→βη g x
Or:(λf. f x) (λy. g y)
−→βη (λy. g y) x−→βη g x (Eta and Beta coincide here)
Dynamics Eta Reduction 21/30
![Page 28: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/28.jpg)
Can Combine Beta and Eta
For example:
(λf. f x) (λy. g y)−→βη (λf. f x) g−→βη g x
Or:(λf. f x) (λy. g y)
−→βη (λy. g y) x−→βη g x (Eta and Beta coincide here)
Dynamics Eta Reduction 21/30
![Page 29: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/29.jpg)
Normal Forms
A normal form is a term that cannot be further reduced.
For example, (λx. g x) is a β-normal form,
but not an η-normal form.
It is reasonable to think of normal forms as values towards whichwe want evaluation to proceed.
Dynamics Normal Forms 22/30
![Page 30: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/30.jpg)
Normal Form Questions
It is natural to ask:
1. Does term M have a normal form?
2. Does M have more than one normal form?
And, we can ask these questions of all terms too.
Dynamics Normal Forms 23/30
![Page 31: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/31.jpg)
Lambda Term Evaluation is Non-Deterministic
Recall:
M −→β M′
M N −→β M′ N
N −→β N′
M N −→β M N ′
Remember also:
(λx. x x) (λy. y y) −→β (λy. y y) (λy. y y)
Call (λx. x x) (λy. y y), the self-looping term, Ω.
Dynamics Evaluation Strategies 24/30
![Page 32: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/32.jpg)
Infinite Loops (Depending on Evaluation Order)
Consider what (λx. y) Ω might do.
I If we keep evaluating the argument (Ω), we never stop:
(λx. y) Ω −→β (λx. y) Ω −→β (λx. y) Ω −→β · · ·
I If we apply the top-level function, substituting Ω for x we get:
(λx. y) Ω −→β y
Done!
This term ((λx. y) Ω) has one normal form (y), but not allevaluation choices reach it.
Dynamics Evaluation Strategies 25/30
![Page 33: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/33.jpg)
How Do Other Languages Do This?
int g l o b a l = 3;
unsigned f(void)unsigned x = 0;while (1) x++; return x;
int g(unsigned i) return g l o b a l;
int main(void) return g( f());
What happens in C?
Dynamics Evaluation Strategies 26/30
![Page 34: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/34.jpg)
Evaluation Strategy #1: Applicative Order
Evaluate everything from the bottom up.
I I.e., in (λv.M) N work will start with M, passing to N andperforming the top-level β-reduction last
A function’s arguments (and the function itself) will be evaluatedbefore the argument is passed to the function.
Also known as strict evaluation.(Used in Fortran, C, Pascal, Ada, SML, Java†. . . )
Causes (λx. y) Ω to go into an infinite loop.
Dynamics Evaluation Strategies 27/30
![Page 35: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/35.jpg)
Evaluation Strategy #2: Normal Order
Evaluate top-down, left-to-right.
I With (λv.M) N, start by performing the β-reduction,producing M[v := N]
I Find the top-most, left-most β-redex and reduce it.
I Keep going
This strategy is behind the lazy evaluation of languages likeHaskell.
Normal order evaluation will always terminate with a normal formif a term has one. (Proof in Lecture 4)
Dynamics Evaluation Strategies 28/30
![Page 36: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/36.jpg)
Strategy Rules
Write M −→n N for “M normal order reduces to N”.Then:
(λv.M) N −→n M[v := N]
M −→n M′
(λv.M) −→n (λv.M ′)
M −→n M′ M not an abstraction
M N −→n M′ N
N −→n N′ M not an abstraction M in β-nf
M N −→n M N ′
Rules for applicative order evaluation will be an assignmentquestion.
Dynamics Evaluation Strategies 29/30
![Page 37: COMP4630: [fg]structure-Calculus - 1. BasicsIntroduction Lambda Calculus Terms Alpha Equivalence Substitution Dynamics Beta Reduction Eta Reduction Normal Forms Evaluation Strategies](https://reader035.fdocument.org/reader035/viewer/2022062507/5fd846ab2233da093f0d9793/html5/thumbnails/37.jpg)
Summary
Lambda Terms:
I Variables, applications, abstractions
I Bound names, free names, alpha equivalence
I Substitution
Dynamic Behaviour:
I Beta reduction: computation through substitution
I Eta reduction
I Normal forms, and strategies for achieving them
Conclusion 30/30