Slides – Evaluation Strategies – Encodings
Total Page:16
File Type:pdf, Size:1020Kb
Announcements Lambda Calculus • 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 scope 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.