Principles of Programming Languages HW 2
Total Page:16
File Type:pdf, Size:1020Kb
CSE 230: Winter 2010 HW 2 ? Principles of Programming Languages Lecture 9: The λ-Calculus RjitRanjit JhlJhala UC San Diego Project Alarm: Midway Meeting 1 Plan • So far, ignored procedures and functions • Check that you’ve found papers – generally making progress • IdiIntroduction to the λ-calllculus • Schedule 10min meeting next Wed – Syntax and operational semantics – Between 1-3pm – Properties • Sign up sheet outside my office … • (Next time): Relationship to PL Bindings Binding: association of name to value IMP++ : Procedures Occurs at various times: – dfiitidefinition or ilimplemen ttitation time • defining the meaning of “+” –compile time or lin k time • constant inlining – run time • passing parameters Trade-off between efficiency and flexibility Static vs Dynamic Scoping Higher-Order Languages and Scoping declare Y; Q: Should this program print 0 or 1? Higher-order Language: procedureY = 100 P(X); Dynamic Scoping begin functions passed as arguments, returned as results def P(X): • use most recent binding on call-stack print(X * Y); end;print(X * Y);Free •based on dynamic execution Free Unpleasant scoping issues: • Concise but confusing procedure Q(Y); dfQ(defbib egQ(iYn) : What are free variables bound to Static Scoping P(P(Y)Y ); end; •in passed (as argument) function ? •use nearest syntactic enclosing binding Y := 0 • in returned function ? • based on static program structure beginQ(1) • Easier to understand Y := 0; Q(1); end; The “downward funarg” Problem The “upward funarg” Problem Ydeclare = 100 Y; procedure R(); procedure P(); Q should be passed declare Y; should return defbegin P(): procedure Q(); R print(print(Y) Y); a closure for P begin a closure for Q end; print(Y); end; proceduredef Q(R): Q(R); begin Closure: Y =declare 0 Y; Closure: Y:=0; R()begin Code + bindings for free return Q; Y := 0; R(); Code + Bindings for free end; variables (e. g. Y) Y :=end; 1 variables (e.g. Y) beginQ(P) begin Y := 1; Q(P); T:= R(); T(); end; end; Functions/Procedures are tricky … when they are first-class values λ−Calculus i.e. can be passed around/bound like other values and Much easier to study a core language… Functional Programming Background Syntax • Developed in 1930’ s by Alonzo Church • Three kinds of expp()ressions (terms): –studied in logic and computer science e ::= x Variables | λx.e Functions (λ-abstraction) | e e Application • Test bed for procedural and functional langs 1 2 • λx.e is a one-argument function with body e –Simpp,le, Powerful, Extensible •e1 e2 is a funct ion applicat ion “Whatever the next 700 langgguages turn out to be, they • Application associates to the left: will surely be variants of lambda calculus.” x y z means (x y) z (Landin ’66) • Abstraction extends to the right as far as possible: λx. x λy. x y z means λx.(x (λy. ((x y) z))) Examples of Lambda Expressions Scoping • The identity function: I =def λx. x Scope of an identifier (variable): • A function that given an argument y discards it part of program where and returns the identity function: λy. (λx. x) variable is accessible • A function that given a function f invokes it on the identity function: λf. f (λx. x) Scoping, Free and Bound Variables Scoping, Free and Bound Variables Abstraction (λx. E) binds variable x in E: y is free in E if it occurs not bound in E –x is the newly introduced variable – Free(x) = {x} –Eis the scope of x – Free( E1 E2) = Free(E1) ∪ Free(E2) –We sayx is bound in λx. E – Free( λx. E) = Free(E) - { x } y is free in E if it occurs not boun d in E – Free(x) = {x} eg:e.g: Free( λx. x (λy. x y z)) = { z } – Free( E1 E2) = Free(E1) ∪ Free(E2) – Free( λx. E) = Free(E) - { x } Free and Bound Variables Renaming Bound Variables • Variable “shadowing” α-renaming: –Different occurrences of var • λ-terms after renaming bound var occurrences may refer to different values • considered identical to original • E.g., in IMP with locals: let x ← E in x + (let x ← E’ in x) + x Example: λx. x is identical to λy. y and to λz. z • In lambda calculus: λx. x (λx. x) x Rename bound variables so names unique • e.g., write λx. x (λy.y) x instead of λx. x (λx.x) x • Easy to see the scope of bindings Substitution Informal Semantics [E’/x] E : Substitution of E’ for x in E The evaluation of (λx. e) e’ 1. Uniquely rename bound vars in E and E’ 1. binds x to e’ 2. evaluates e with the new binding 2. Do textual subibstitut ion of E’ for x in E 3. yields the result of this evaluation Example: [y (λx. x)/x] λy. (λx. x) y x Like let x = e’ in e 1. After renaming: [y (λv. v)/x] λz. (λu. u) z x 2. After substitution: λz. (λu. u) z (y (λv. v)) Example: (λf. f (f e)) g evaluates to g (g e) Another View of Reduction Operational Semantics The application APP • Formalize semantics as β-reduction rule: λx. (λx. e) e’ →β [e’/x]e ee’ x x x •A term (λx. e) e’ is a β-redex Terms can grow substantially by reduction becomes: e • e →β e’ : e β-reduces to e’ in single step * •e →β e’: e β-reduces to e’ in many steps e’ e’ e’ Examples of Evaluation Examples of Evaluation • The identity function: (λx. x) E → [E / x] x (λx. x x)(λy. y y) = E → [λy. y y / x] x x = (λy. y y)(λy. y y) • Another example with the identity: (λf. f (λx. x)) (λx. x) = (λx. x x)(λy. y y) → [λx. x / f] f (λx. x) → … = [(λx. x) / f] f (λy. y) A non-terminating evaluation ! = (λx. x) (λy. y) → [λyyy. y /x] x = λy. y Evaluation and Static Scoping Nondeterministic evaluation • Definition of substitution guarantees that Define a small-step reduction relation: evaluation respects static scoping: (λx. e) e’ → [e’/x]e (λ x. (λy. y x)) (y (λx. x)) →β λz. z (y (λv. v)) e → e ’ e1 → e1’ 2 2 e e → e e ’ y remains free, i.e., defined externally e1 e2 → e1’ e2 e → e’ 1 2 1 2 λx. e → λx. e’ • If we forget to rename the bound y: ∗ • This is a nondeterministic set of rules (λ x. (λy. y x)) (y (λx. x)) → β λy. y (y (λv. v)) • Three rules saying where to evaluate y was free before but is bound after – e. g. under λ The Order of Evaluation Normal Forms λ-term can have many β-redexes (λx. E) E’ • A term without redexes is in normal form (λy. (λx. x) y) E • A reduction sequence stops at a normal form * Reduce the inner or the outer application ? –If e in normal form and e → β e’ then e ≡ e’ (λ y. (λ x. x) y) E inner outer •(λx.λy. x) is in normal form (λ y. [y/x] x) E = (λ y. y) E [E/y] (λ x. x) y =(λ x. x) E •(λx.λy. x) (λz. z) is not in normal form E The Diamond Property The Diamond Property e • Relation R has diamond ppproperty if: Languages defined by nondeterministic rules: R R whenever e R e1 and e R e2 , – Logic programming languages there exists e’ such that e1 R e’ and e2 R e’ e1 e2 –Exppyert systems R R – Constraint satisfaction systems e’ –Make • →β does not have the diamond property. Useful to know if systems have diamond property! – For example, consider (λx. x x x)(λy. (λx. x) y) * • If so, a unique result is guaranteed • →β has the diamond property – The proof is quite technical Equality The Church-Rosser Theorem * * =β : symmetric, reflexive, transitive closure of →β If e1 =β e2 then exists e’ s.t. e1 →β e’ and e2 →β e’ * =β is (→β ∪←β) • • So e1 =β e2 if e1 converts to e2 • e1 e2 via a seq of forward and backward →β : •• • • e’ • e e1 2 • Proof (informal): apply the diamond property as many times as necessary. Corollaries Next time If e1 =β e2 and e1, e2 are normal forms then e1 ≡ e2 “Programming” with the λ-Calculus Proof: * * • From CR we have ∃e’. e1 → β e’ and e2 → β e’ •As e1, e2 are normal forms they are ≡ e’ * * If e → β e1 and e → β e2 and e1 , e2 are normal forms then e1 ≡ e2 Proof ? Significance: Each term reduces to one normal form.