Announcements • HW5 grades posted tonight

Meeting 24, CSCI 5535, Spring 2010

2

Program Analysis in a Nutshell?

Define an Abstraction Lambda Calculus

Compute a Fixed Point in the Abstraction

3

Lambda Calculus Lambda Background

• Developed in 1930’s by Alonzo Church • Subsequently studied by many people Goal : Come up with a “core” language that’s as – Still studied today! small as possible and still Turing • Considered the “testbed” for procedural and functional languages complete – Simple – Powerful This will give a way of illustrating important – Easy to extend with new features of interest language features and algorithms – “Lambda:PL :: Turing Machine:Complexity”

“Whatever the next 700 languages turn out to be, they will surely be variants of lambda calculus.” (Landin ’66) 5 6

1 Lambda Syntax Why Should I Care?

• The λ-calculus has 3 kinds of expressions (terms) • A language with 3 expressions? Woof! e ::= x Variables • Li and Zdancewic. Downgrading policies and relaxed noninterference . POPL ’05 λ | x. e Functions ( abstractions ) – Just one example of a recent PL/security paper | e1 e2 Application • λx. e is a one-argument anonymous function with body e

• e1 e2 is a function application • Application associates to the left x y z === (x y) z • Abstraction extends as far to the right as possible λ λ λ λ x. x y. x y z === x. (x [ y. {(x y) z}]) 7

8

Examples of Lambda Expressions Examples of Lambda Expressions

• The identity function: • The identity function:

I = def λx. x I = def λx. x • A function that, given an argument y, • A function that, given an argument y, discards it and yields the identity discards it and yields the identity function: function: λy. ( λx. x) λy. ( λx. x) • A function that, given an function f, • A function that, given an function f, invokes it on the identity function: invokes it on the identity function: λf. f ( λx. x) λf. f ( λx. x)

9 10

Scope of Variables Free and Bound Variables

• As in all languages with variables, it is • A variable is said to be free in E if it has important to discuss the notion of occurrences that are not bound in E – The scope of an identifier is the portion of a • We can define the free variables of an program where the identifier is accessible expression E recursively as follows: • An abstraction λx. E binds variable x in E – Free(x) = {x}

– x is the newly introduced variable – Free(E 1 E2) = Free(E 1) ∪ Free(E 2) –E is the scope of x (unless x is shadowed) – Free( λx. E) = Free(E) – {x} – We say x is bound in λx. E • Example: Free( λx. x ( λy. x y z) ) = {z} – Just like formal function arguments are • Free variables are (implicitly or explicitly) bound in the function body declared outside the expression 11 12

2 = α-renaming or Free Your Mind! Renaming Bound Variables α-conversion • Just as in any language with statically- • λ-terms that can be obtained from one nested scoping we have to worry about another by renaming bound variables are variable shadowing considered identical • This is called α-equivalence – An occurrence of a variable might refer to different things in different contexts • Ex: λx. x is identical to λy. y and to λz. z • Example let-expressions (as in ML): • Intuition: – By changing the name of a formal argument and all let x = 5 in x + (let x = 9 in x) + x of its occurrences in the function body, the • In λ-calculus: behavior of the function does not change – In λ-calculus such functions are considered λx. x (λx. x) x identical 13 14

Make It Easy On Yourself Substitution

• Convention: we will always try to rename • The substitution of e’ for x in e bound variables so that they are all (written [ e’ /x]e) unique – Step 1. Rename bound variables in e and e’ – e.g., write λx. x ( λy.y) x so they are unique instead of λx. x ( λx.x) x – Step 2. Perform the textual substitution of e’ for x in e • This makes it easy to see the scope of bindings and also prevents confusion! • Called capture-avoiding substitution

15 16

Substitution Substitution

• Example: [y ( λx. x) / x] λy. ( λx. x) y x • Example: [y ( λx. x) / x] λy. ( λx. x) y x – After renaming: – After renaming: [y ( λx. x) / x] λz. ( λu. u) z x – After substitution: λz. ( λu. u) z (y ( λx. x))

– After substitution:

• If we are not careful with scopes we might get: λy. ( λx. x) y (y ( λx. x)) ← wrong!

17 18

3 Alternative: The de Bruijn Notation Combinators

• An alternative syntax that avoids naming of bound • A λ-term without free variables is closed or a variables (and the subsequent confusions) combinator • The de Bruijn index of a variable occurrence is that • Some interesting combinators: number of lambda that separate the occurrence I = λ x. x from its binding lambda in the abstract syntax tree Explain K = λ x. λ y. x these • The de Bruijn notation replaces names of λ λ λ informally occurrences with their De Bruijn indices S = f. g. x. f x (g x) D = λ x. x x • Examples: Identical terms Y = λ f. ( λ x. f (x x)) ( λ x. f (x x)) – λλλ x. x λλλ. 0 have identical – λλλ x. λλλ x. x λλλ. λλλ. 0 representations! • Theorem: any closed term is equivalent to one – λλλ x. λλλ y. y λλλ. λλλ. 0 written with just S, K and I – (λλλ x. x x) ( λλλ z. z z) (λλλ. 0 0) ( λλλ. 0 0) – Example: D = β S I I – λλλ x. ( λλλ x. λλλ y. x) x λλλ. ( λλλ. λλλ. 1) 0 – (we’ll discuss this form of equivalence later) 19 20

Informal Semantics Informal Semantics

• All we’ve got are functions, so all we can do is • All we’ve got are functions, so all we can do is call them! call them! • The evaluation of (λ x. e) e’ – Binds x to e’ – Evaluates e with the new binding – Yields the result of this evaluation • Like a function call, or like “ let x = e’ in e ” • Example: (λ f. f (f e)) g evaluates to g (g e)

21 22

Informal Semantics Operational Semantics

• All we’ve got are functions, so all we can do is • Many operational semantics for the λ-calculus call them! • All are based on the equation • The evaluation of (λλλ x. e1) e2 =βββ [e2/x]e 1 usually read from left to right (λ x. e) e’ • This is called the β-rule and the evaluation – Binds x to e’ step a β-reduction – Evaluates e with the new binding • The subterm (λ x. e 1) e 2 is a β-redex – Yields the result of this evaluation • We write e →β e’ to say that e β-reduces to e’ • Like a function call, or like “ let x = e’ in e ” in one step * • Example: • We write e →β e’ to say that e β-reduces to e’ in 0 or more steps (λ f. f (f e)) g evaluates to g (g e) – Remind you of the small-step opsem term rewriting? 23 24

4 Operational Semantics Examples of Evaluation

• The identity function: (λλλ x. x) E →→→ [E / x] x = E betabeta--reduction--reduction • Another example with the identity: (λλλ f. f ( λλλ x. x)) ( λλλ x. x) →→→ → (λx.e 1) e 2 β [e 2/x]e 1

25 26

Examples of Evaluation Examples of Evaluation

• The identity function: • What happens here? (λλλ x. x) E →→→ [E / x] x = E (λλλ x. xx) ( λλλ y. yy) →→→ • Another example with the identity: [λλλ y. yy / x] xx = ( λλλ y. yy) ( λλλ y. yy) (λλλ f. f ( λλλ x. x)) ( λλλ x. x) →→→ →→→ … [λλλ x. x / f] f ( λλλ x. x)) = • Try T T, where T = λx. x x x [λλλ x. x / f] f ( λλλ y. y)) = (λλλ x. x) ( λλλ y. y) →→→ [λλλ y. y / x] x = λλλ y. y

27 28

Evaluation and the Static Scope Another View of Reduction

• The definition of substitution • The application guarantees that evaluation respects λx. e static scoping:

(λ x. ( λ y. y x)) ( y (λ x. x)) →β λ z. z (y (λ v. v)) e x x x g (y remains free, i.e., defined externally) • Becomes:

• If we forget to rename the bound y: e * (λ x. ( λ y. y x)) ( y (λ x. x)) →β λ y. y (y (λ v. v)) (y was free before but is bound now ) g g g (terms can grow substantially through β-reduction!)

29 30

5 Normal Forms Structural Operational Semantics

• A term without redexes is in normal form • We define a small-step reduction relation • A reduction sequence stops at a normal form

(λx. e1) e2 → [e2/x]e 1 * • If e is in normal form and e →β e’ then e1 → e1’ e2 → e2’

e is identical to e’ e1 e2 → e1’ e2 e1 e2 → e1 e2’ e → e’ λx. e → λx. e’ • This is a non-deterministic semantics • Note that we evaluate under λ (where?)

31 32

Lambda Calculus Contexts Lambda Calculus Contexts

• Define contexts with one hole • Define contexts with one hole •H ::= • | •H ::= • | λ x. H | H e | e H • Write H[e] to denote the filling of the hole in H with the expression e • Example: H = λ x. x • H[ λ y. y] = λ x. x ( λ y. y) • Filling the hole allows variable capture! H = λ x. x • H[x] = λ x. x x

33 34

Contextual Operational Semantics Contextual Operational Semantics

e → e’

(λx. e1) e2 → [e2/x]e 1 H[e] → H[ e’ ] • Contexts allow concise formulations of congruence rules (application of local reduction rules on subterms) • Reduction occurs at a β-redex that can be anywhere inside the expression • The latter rule is called a congruence or structural rule • The above rules to not specify which redex must be reduced first 35 36

6 The Order of Evaluation The Diamond Property

• In a λ-term there could be more than • A relation R has the diamond property if whenever e R e 1 and e R e 2 then there exists e’ such that e1 R e’ one instance of (λ x. e 1) e 2, as in: and e2 R e’ (λ y. (λ x. x) y ) E e R R – Could reduce the inner or outer λ e e – Which one should we pick? 1 2 (λλλ y. ( λλλ x. x) y) E R R e’ inner outer • →β does not have the diamond property (λλλ y. [y/x] x) E = ( λλλ y. y) E [E/y] ( λλλ x. x) y = ( λλλ x. x) E * • →β has the diamond property

E 37 38

A Diamond In The Rough Beta Equality

• Languages defined by non-deterministic • Let = β be the reflexive, transitive and sets of rules are common symmetric closure of →β – Logic programming languages * =βββ is ( →→→βββ ∪∪∪←←← βββ) – Expert systems – Constraint satisfaction systems • And thus most pointer analyses … • That is, e = β e’ if e converts to e’ via a – Dataflow systems sequence of forward and backward →β – Makefiles •• • It is useful to know whether such e • e’ systems have the diamond property

39 40

The Church-Rosser Theorem Corollaries

• If e1 =β e2 then there exists e’ such that • If e1 =β e2 and e1 and e2 are normal → * → * e1 β e’ and e2 β e’ forms then e1 is identical to e2 •• * * – From CR we have ∃e’. e 1 →β e’ and e2 →β e’

e1 • e2 – Since e1 and e2 are normal forms they are •• identical to e’ e’ * * • If e →β e1 and e →β e2 and e1 and e2 are normal forms then e is identical to e • Proof (sketch): apply the diamond 1 2 property as many times as necessary – “All terms have a unique normal form.”

41 42

7 Evaluation Strategies Evaluation Strategies Summary

• Church-Rosser says that independent of the • Normal Order reduction strategy we will find ≤1 normal form – Evaluates the left-most redex not contained in another redex • But some reduction strategies might find 0 – If there is a normal form, this finds it –(λ x. z) (( λ y. y y) ( λ y. y y)) → – Not used in practice: requires partially evaluating function pointers and looking “inside” functions (λ x. z) (( λ y. y y) ( λ y. y y)) → … • Call-By-Name (“lazy”) λ λ λ → –( x. z) (( y. y y) ( y. y y)) z – Don’t reduce under λ, don’t evaluate a function • There are three traditional strategies argument (until you need to) – normal order (never used, always works) – Does not always evaluate to a normal form • Call-By-Value (“eager” or “strict”) – call-by-name (rarely used, cf. TeX) – Don’t reduce under λ, do evaluate a function’s – call-by-value (amazingly popular) argument right away

43 – Finds normal forms less often than the other two 44

Class and Reading

• Today – Lambda calculus: syntax, semantics, reductions, equality Bonus: Evaluation Strategies • Reading/Bonus Slides – Evaluation strategies – Encodings

45

Normal-Order Reduction Why Not Normal Order ?

• A redex is outermost if it is not contained • In most (all?) programming languages, inside another redex functions are considered values (fully • Example: evaluated) S (K x y) (K u v) • Example: • K x , K u and S (K x y) are all redexes λx. D D = ⊥ (with normal order) • Both K u and S (K x y) are outermost • Thus, no reduction is done under lambda • Normal order always reduces the leftmost outermost redex first • Theorem: If e has a normal form e’ then • No popular programming language uses normal order reduction will reduce e to e’ normal order 47 48

8 Call-by-Name Call by Name Example

• Don’t reduce under λ • Example: • Don’t evaluate the argument to a function call • A value is an abstraction (λy. ( λx. x) y) (( λu. u) ( λv. v)) →βn * * e1 →n λx. e 1’ [e 2/x]e 1’ →n e * * λx. e →n λx. e e1 e2 →n e (λx. x) (( λu. u) ( λv. v)) →βn • Call-by-name is demand-driven: an expression is not evaluated unless needed (λu. u) ( λv. v) →β • It is normalizing: converges whenever normal order n converges λv. v • Call-by-name does not necessarily evaluate to a normal form. Example: D D

49 50

Call-by-Value Evaluation Call by Value Example

• Don’t reduce under lambda • Example: • Do evaluate the arguments to a function call

• A value is an abstraction (λy. ( λx. x) y) (( λu. u) ( λv. v)) →βv

e → * λx. e ’ e → * e ’ [e’ /x]e ’ → * e 1 v 1 2 v 2 2 1 v (λy. ( λx. x) y) ( λv. v) →βv * * λx. e →v λx. e e1 e2 →v e

(λx. x) ( λv. v) →βv • Most languages are primarily call-by-value • But CBV is not normalizing: (λx. I) (D D) λv. v • CBV diverges more often than normal order or even CBN 51 52

Evaluation Strategy Considerations Which is Better?

• Call-by-value: • The debate about whether languages should – easy to implement be strict (CBV) or lazy (CBN) is nearly 20 – well-behaved (predictable) with respect to side-effects years old • Call-by-name: – More difficult to implement (must pass unevaluated expressions) • This debate is typically confined to the – The order of evaluation is harder to predict (e.g., difficulty functional programming community (where it is with side-effects) – Has a simpler theory than call-by-value sometimes intense) – Allows the natural expression of infinite data structures (e.g. streams) – Terminates more often than call-by-value • Outside the functional community CBN is rarely considered (but remember TeX)

53 54

9 Caveats

• The terms lazy and strict are not used consistently in the literature • Call-by-value and call-by-name are well defined • There are parameter passing mechanisms besides call- by-value and call-by-name that cannot be naturally expressed in the lambda calculus – by reference – value-result • These additional mechanisms deal with side-effects • Many languages have a mixture of parameter passing mechanisms

55

10