/
Announcements Quiz 6 HW5 due on Friday Announcements Quiz 6 HW5 due on Friday

Announcements Quiz 6 HW5 due on Friday - PowerPoint Presentation

jasmine
jasmine . @jasmine
Follow
66 views
Uploaded On 2023-06-22

Announcements Quiz 6 HW5 due on Friday - PPT Presentation

Exam 2 in one week Practice tests on Submitty Review and practice on Friday Programming Languages CSCI 4430 A Milanova 1 Lambda Calculus Lecture Outline Quiz 6 Lambda calculus Reduction strategies catchup ID: 1001548

languages type 4430 csci type languages csci 4430 milanova int order programming reduction fix lambda pred bool normal calculus

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Announcements Quiz 6 HW5 due on Friday" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

1. AnnouncementsQuiz 6HW5 due on FridayExam 2 in one weekPractice tests on SubmittyReview and practice on FridayProgramming Languages CSCI 4430, A. Milanova1

2. Lambda Calculus

3. Lecture OutlineQuiz 6Lambda calculusReduction strategies (catch-up)Applied lambda calculusIntroduction to types and type systemsSimply typed lambda calculus (System F1)If we have timeProgramming Languages CSCI 4430, A. Milanova3

4. 4Reduction StrategyLook again at (x.y.z. x z (y z)) (u. u) (v. v) Actually, there are (at least) two “reduction paths”:Path 1: (x.y.z. x z (y z)) (u. u) (v. v) β (y.z. (u. u) z (y z)) (v. v) β (z. (u. u) z ((v. v) z)) β (z. z ((v. v) z)) β z. z zPath 2: (x.y.z. x z (y z)) (u. u) (v. v) β (y.z. (u. u) z (y z)) (v. v) β (y.z. z (y z)) (v. v) β (z. z ((v. v) z)) β z. z z

5. Programming Languages CSCI 4430, A. Milanova5Reduction StrategyA reduction strategy (also called evaluation order) is a strategy for choosing redexesHow do we arrive at a normal form (answer)?Applicative order reduction chooses the leftmost-innermost redex in an expressionAlso referred to as call-by-value reduction

6. Programming Languages CSCI 4430, A. Milanova6Reduction StrategyA reduction strategy (also called evaluation order) is a strategy for choosing redexesHow do we arrive at a normal form (answer)?Normal order reduction chooses the leftmost-outermost redex in an expressionAlso referred to as call-by-name reduction

7. 7Reduction Strategy: Examples Evaluate (x. x x) ( (y. y) (z. z) )Using applicative order reduction:Using normal order reduction

8. Programming Languages CSCI 4430, A. Milanova8Reduction StrategyIn our examples, both strategies produced the same result. This is not always the caseFirst, look at expression (x. x x) (x. x x). What happens when we apply β-reduction to this expression?Then look at (x.y. y) ((x. x x) (x. x x)) zApplicative order reduction – what happens?Normal order reduction – what happens?

9. 9Church-Rosser TheoremNormal form implies that there are no more reductions possible Church-Rosser Theorem, informallyIf normal form exists, then it is unique (i.e., result of computation does not depend on the order that reductions are applied; i.e., no expression can have two distinct normal forms)If normal form exists, then normal order will find itChurch-Rosser Theorem, more formally: For all pure -expressions M, P and Q, if M * P and M * Q, then there must exist an expression R such that P * R and Q * R

10. 10Reduction StrategyIntuitively:Applicative order (call-by-value) is an eager evaluation strategy. Also known as strictNormal order (call-by-name) is a lazy evaluation strategyWhat order of evaluation do most programming languages use?

11. ExercisesEvaluate (x.y. x y) ((z. z) w) Using applicative order reductionUsing normal order reduction11Programming Languages CSCI 4430, A. Milanova

12. ExerciseLet S = xyz. x z (y z) and let I = x. xEvaluate S I I I using applicative order12Programming Languages CSCI 4430, A. Milanova

13. ExerciseLet S = xyz. x z (y z) and let I = x. xEvaluate S I I I using normal order13Programming Languages CSCI 4430, A. Milanova

14. Lecture OutlineQuiz 6Lambda calculusReduction strategies (catch-up)Applied lambda calculusIntroduction to types and type systemsSimply typed lambda calculus (System F1)If we have timeProgramming Languages CSCI 4430, A. Milanova14

15. 15Applied Lambda Calculus (from Sethi)E ::= c | x | ( x.E1 ) | ( E1 E2 ) An applied lambda calculus augments the pure lambda calculus with constants. It defines its set of constants and reduction rules. For example: Constants: Reduction rules: if, true, false (all these are  terms, e.g., true=x.y. x) 0, iszero, pred, succif true M N δ Mif false M N δ Niszero 0 δ trueiszero (succk 0) δ false, k>0iszero (predk 0) δ false, k>0succ (pred M) δ Mpred (succ M) δ MProgramming Languages CSCI 4430, A. Milanova

16. 16From an Applied Lambda Calculus to a Functional LanguageConstruct Applied -Calculus A Language (ML)Variable x xConstant c cApplication M N M NAbstraction x.M fun x => MInteger succk 0, k>0 k predk 0, k>0 -kConditional if P M N if P then M else NLet (x.M) N let val x = N in M end Programming Languages CSCI 4430, A. Milanova

17. 17The Fixed-Point OperatorOne more constant and one more rule: fix fix M δ M (fix M)Needed to define recursive functions:Therefore, we need:plus = x.y. if (iszero x) y (plus (pred x) (succ y))y if x = 0plus (pred x) (succ y) otherwiseplus x y = x-1y+1M(M(M… ))Programming Languages CSCI 4430, A. Milanova

18. 18The Fixed-Point OperatorBut how do we define plus?Define plus = fix M, whereM = f. x.y. if (iszero x) y (f (pred x) (succ y))We must show that fix M =δβ x.y. if (iszero x) y ((fix M) (pred x) (succ y))Programming Languages CSCI 4430, A. Milanova?

19. plus = fix M19)?otherwise

20. The Fixed-Point OperatorWe have to showfix M =δβ x.y. if (iszero x) y ((fix M) (pred x) (succ y))fix M =δ M ( fix M ) =(f. x.y. if (iszero x) y (f (pred x) (succ y))) ( fix M ) =βx.y. if (iszero x) y ((fix M) (pred x) (succ y))Programming Languages CSCI 4430, A. Milanova20

21. 21Define times =fix (f.x.y. if (iszero (pred x)) y (plus y (f (pred x) y))) Exercise: define factorial = ?The Fixed-Point OperatorProgramming Languages CSCI 4430, A. Milanova

22. The Y Combinatorfix is, of course, a lambda expression!One possibility, the famous Y combinator:Y = f. (x. f (x x)) (x. f (x x))Show that Y M indeed reduces to M (Y M)22

23. 23Types!Constants are convenientBut they raise problems because they permit “bad” terms such asif 0 y z (0 doesn’t make sense as first argument; true/false values do)(0 x) (0 does not apply as a function)succ true (undefined in our language)plus true 0 etc.Programming Languages CSCI 4430, A. Milanova

24. Types!Why types?Safety. Catch semantic errors earlyData abstraction. Simple types and ADTsDocumentation (statically-typed languages only)Type signature is a form of specification!Statically typed vs. dynamically typed languagesType annotations vs. type inferenceType safe vs. type unsafe24Programming Languages CSCI 4430, A. Milanova

25. Type SystemInformally, it is a set of rules that we apply on syntactic constructs in the languageIn type theory, it is defined in terms ofType environmentTyping rules, also called type judgmentsThis is typically referred to as the type system25Programming Languages CSCI 4430, A. Milanova

26. 26Example, More On This LaterΓ |- E1 : στ Γ |- E2 : σΓ |- (E1 E2) : τ Γ |- x : τΓ,x:σ |- E1 : τΓ |- (x:σ. E1) : σ  τ (Variable)(Application)(Abstraction)binding: augments environment Γwith binding of x to type σlooks up the type of x in environment Γ x:τ ΓProgramming Languages CSCI 4430, A. Milanova

27. Type SystemA type system either accepts a term (i.e., term is “well-typed”), or rejects it Type soundness, also called type safetyWell-typed terms never “go wrong”A sound type system never accepts a term that can “go wrong”A complete type system never rejects a term that cannot “go wrong”Whether a term can “go wrong” is undecidableType systems choose type soundness (i.e., safety)27

28. Putting It All Together, FormallySimply typed lambda calculus (System F1)Syntax of the simply typed lambda calculusThe type system: type expressions, environment, and type judgmentsThe dynamic semanticsStuck statesType soundness theorem: progress and preservation theorem28

29. 29Type ExpressionsIntroducing type expressionsτ ::= b | τ  τA type is a basic type b (we will only consider int and bool, for simplicity), or a function typeExamplesintbool  (int  int) //  is right-associative, thus can write just bool  int  intSyntax of terms:E ::= x | ( x:τ. E1 ) | ( E1 E2 ) Programming Languages CSCI 4430, A. Milanova

30. Type Environment and Type JudgmentsA term in the simply typed lambda calculus isType correct i.e., well-typed, orType incorrect The rules that judge type correctness are given in the form of type judgments in an environmentEnvironment Γ |- E : τ (|- is the turnstile)Read: environment Γ entails that E has type τType judgment Γ |- E1 : στ Γ |- E2 : σ Γ |- (E1 E2) : τPremisesConclusion

31. 31SemanticsΓ |- E1 : στ Γ |- E2 : σΓ |- (E1 E2) : τ Γ |- x : τΓ,x:σ |- E1 : τΓ |- (x:σ. E1) : σ  τ (Variable)(Application)(Abstraction)binding: augments environment Γwith binding of x to type σlooks up the type of x in environment Γ x:τ ΓProgramming Languages CSCI 4430, A. Milanova

32. 32ExamplesDeduce the type for x: int.y: bool. x in the nil environmentProgramming Languages CSCI 4430, A. Milanova

33. Extensions33Γ |- c : int Γ |- E1 : int Γ |- E2 : intΓ |- E1+E2 : int Γ |- E1 : int Γ |- E2 : intΓ |- E1=E2 : bool Γ |- b : bool Γ |- E1 : τ Γ |- E2 : τΓ |- if b then E1 else E2 : τ (Comparison)Programming Languages CSCI 4430, A. Milanova

34. ExamplesIs this a valid type?Nil |- x: int.y: bool. x+y : int  bool  intNo. It gets rightfully rejected. Term reaches a state that goes wrong as it applies + on a value of the wrong type (y is bool, + is defined on ints)Is this a valid type?Nil |- x: bool.y: int. if x then y else y+1 : bool  int  int34Programming Languages CSCI 4430, A. Milanova

35. ExamplesCan we deduce the type of this term?f. x. if x=1 then x else (f (f (x-1))) : ? Programming Languages CSCI 4430, A. Milanova (example from MIT 2015 Program Analysis OCW) 35Γ |- E1 : int Γ |- E2 : intΓ |- E1+E2 : intΓ |- E1 : int Γ |- E2 : intΓ |- E1=E2 : boolΓ |- b : bool Γ |- E1 : τ Γ |- E2 : τΓ |- if b then E1 else E2 : τ

36. ExamplesCan we deduce the type of this term?foldl = f.x.y.if x=() then y else (foldl f (cdr x) (f y (car x))): 36Γ |- E : list τΓ |- (car E) : τ Γ |- E : list τΓ |- (cdr E) : list τ Programming Languages CSCI 4430, A. Milanova

37. ExamplesHow about this (x. x (y. y) (x 1)) (z. z) : ?x cannot have two “different” types(x 1) demands int  ?(x (y. y)) demands ( τ  τ )  ?Program does not reach a “stuck state” but is nevertheless rejected. A sound type system typically rejects some correct programs37

38. The EndProgramming Languages CSCI 4430, A. Milanova38