concepts of programming languages
play

Concepts of programming languages Lecture 6 Wouter Swierstra - PowerPoint PPT Presentation

Faculty of Science Information and Computing Sciences 1 Concepts of programming languages Lecture 6 Wouter Swierstra Faculty of Science Information and Computing Sciences 2 Announcements and will try to get back to you with feedback soon.


  1. Faculty of Science Information and Computing Sciences 1 Concepts of programming languages Lecture 6 Wouter Swierstra

  2. Faculty of Science Information and Computing Sciences 2 Announcements and will try to get back to you with feedback soon. modifications – be sure to check that you can make it. presentation. Ideally, I’d like you all to prepare a poster about your project. ▶ Thanks for all the proposals – I’ve read most of them ▶ The presentation schedule has some minor ▶ The lecture and lab session on Thursday is cancelled. ▶ I’m trying to book ‘de Vagant’ to host the final project

  3. Faculty of Science Information and Computing Sciences 3 Last time ▶ How can we define the semantics of trivial languages? ▶ What is the λ -calculus and what is its semantics?

  4. Faculty of Science Information and Computing Sciences 4 Micro programming languages We defined the semantics of a series of small programming languages. And started the study of the lambda calculus .

  5. Faculty of Science Information and Computing Sciences 5 The lambda calculus The lambda calculus is the ’smallest programming language imaginable’. It was originally introduced by Alonzo Church (1930’s) as a foundation for mathematics – but surprisingly, it perfectly captures computation! There are only three constructs: (variables) (application) (abstraction) e ::= x | e e | λ x . e

  6. Faculty of Science Information and Computing Sciences 6 binding ; e is called the abstraction’s body . by a λ -Calculus: β -Reduction A term of the form λ x . e is called an abstraction or lambda The central rewrite rule of the λ -calculus is β -reduction: ( λ x . e ) a → β e [ x �→ a ] [ x �→ a ] := substitution of all free occurrences of variable x ( λ f . λ x . λ y . f y x ) a b c → β ( λ x . λ y . a y x ) b c → β ( λ y . a y b ) c → β a c b An expression of the form ( λ x . e ) ( t ) is called a β -redex.

  7. Faculty of Science Information and Computing Sciences different results! What went wrong? Two equivalent expressions produced And now we can reduce the following equivalent expression: Problem: a is captured by the innermost lambda binding! Consider the following example: 7 λ -Calculus: Name Capturing and α -conversion ( λ y . ( λ b . y b )) a → β ( λ b . y b ) [ y �→ a ] = λ b . a b ( λ y . ( λ a . y a )) a → β ( λ a . y a ) [ y �→ a ] = λ a . a a

  8. Faculty of Science Information and Computing Sciences different results! What went wrong? Two equivalent expressions produced And now we can reduce the following equivalent expression: Problem: a is captured by the innermost lambda binding! Consider the following example: 7 λ -Calculus: Name Capturing and α -conversion ( λ y . ( λ b . y b )) a → β ( λ b . y b ) [ y �→ a ] = λ b . a b ( λ y . ( λ a . y a )) a → β ( λ a . y a ) [ y �→ a ] = λ a . a a

  9. Faculty of Science Information and Computing Sciences 8 Capture avoiding substitution substitution – that is, it should renames the abstraction variable if necessary: The substitution [ x �→ y ] must be a capture-avoiding ( λ y . ( λ a . y a )) a → β ( λ a . y a ) [ y �→ a ] → α ( λ a ′ . y a ′ ) [ y �→ a ] λ a ′ . a a ′ = Note that we introduce an explicit α -conversion step, renaming a to a ′ .

  10. Faculty of Science Information and Computing Sciences 9 Capture avoiding substitution We can define such a capture avoiding substitution as follows: y in order to proceed with the substitution. x [ x �→ t ] = t y [ x �→ t ] = y when x ̸≡ y ( t 1 t 2 ) [ x �→ t ] = ( t 1 [ x �→ t ]) ( t 2 [ x �→ t ]) ( λ y . s ) [ x �→ t ] = λ y . s [ x �→ t ] provided y ̸≡ x and y does not occur free in t . Note that this last rule may require α -renaming the variable

  11. Faculty of Science Information and Computing Sciences 10 Transitive, reflexive closure Beta-reduction allows us to define a single reduction step… …but what if we’re interested in evaluating a more complicated λ -term? We can define the relation t → ∗ β t ′ as follows: ▶ t → ∗ β t for any term t ▶ if t 1 → β t 2 and t 2 → ∗ β t 3 , then also t 1 → ∗ β t 3 If t → ∗ β t ′ , we can reach t ′ from t after zero or more β -reduction steps.

  12. Faculty of Science Information and Computing Sciences 11 This corresponds to saying that the two lambda terms, t and This is undecidable in general. λ -Calculus: β -equivalence β v or t ′ → ∗ When we can find a term v , such that t → ∗ β v we call t and t ′ β -equivalent. In that case, we sometimes write t = β t ′ . t ′ , correspond to the same program. For example ( λ y . a y ) b = β ( λ x . x b ) a because ( λ y . a y ) b → β a b ← β ( λ x . x b ) a

  13. Faculty of Science Information and Computing Sciences 12 Lambda terms and functional programming Despite all its simplicity, the lambda calculus really captures the heart of (functional) programming. A function like: Is easy to represent by the following lambda term: In fact, this is how GHC represents programs under the hood. flip f x y = f y x flip = λ f . λ x . λ y . f y x

  14. Faculty of Science inc x And after substituting function calls with their definition: inc flip These can be desugared as: Information and Computing Sciences map f main print x Given the following definitions: Extended example 13 = print ( flip map [ 1 . . ] inc ) = putStrLn ( show x ) flip f x y = f y x = x + 1 = ... main = print ( flip map [ 1 . . ] inc ) print = λ x . putStrLn ( show x ) = λ f . λ x . λ y . f y x = λ x . x + 1 map = λ f . ... ( λ x . putStrLn ( show x )) (( λ f . λ y . λ x . f y x ) ...

  15. Faculty of Science 14 Example as a Syntax-Tree Information and Computing Sciences @ λ x @ λ x @ @ [ 1 .. ] putStrLn @ @ @ λ f λ f show x @ 1 . . λ x + . x λ y @ @ x f y

  16. Faculty of Science 14 Example as a Syntax-Tree Information and Computing Sciences @ λ x @ λ x @ @ [ 1 .. ] putStrLn @ @ @ λ f λ f show x @ 1 . . λ x + . x λ y @ @ x f y

  17. Faculty of Science 14 Example as a Syntax-Tree Information and Computing Sciences @ λ x @ λ x @ @ λ x [ 1 .. ] putStrLn @ @ λ y show x @ 1 + @ x @ x λ f y . . .

  18. Faculty of Science 14 Example as a Syntax-Tree Information and Computing Sciences @ λ x @ λ x @ @ λ x [ 1 .. ] putStrLn @ @ λ y show x @ 1 + @ x @ x λ f y . . .

  19. Faculty of Science 14 Example as a Syntax-Tree Information and Computing Sciences @ λ x @ λ y λ x @ putStrLn @ @ @ [ 1 .. ] show x @ @ 1 λ f + y x . . .

  20. Faculty of Science 14 Example as a Syntax-Tree Information and Computing Sciences @ λ x @ λ y λ x @ putStrLn @ @ @ [ 1 .. ] show x @ @ 1 λ f + y x . . .

  21. Faculty of Science 14 Example as a Syntax-Tree Information and Computing Sciences @ λ x @ [ 1 .. ] @ @ λ f λ x putStrLn @ . . show x . @ @ 1 + x

  22. Faculty of Science 14 Example as a Syntax-Tree Information and Computing Sciences @ λ x @ [ 1 .. ] @ @ λ f λ x putStrLn @ . . show x . @ @ 1 + x

  23. Faculty of Science Information and Computing Sciences 14 Example as a Syntax-Tree @ putStrLn @ show @ [ 1 .. ] @ λ f λ x . . . @ @ 1 + x

  24. Faculty of Science Information and Computing Sciences 15 Evaluation order As we saw previously, we can choose different evaluation orders : beta reducing; redex on the spine; never reduce under lambdas. This reduces a term to weak head normal form – we will have a lambda or stuck application at the top level, but there may still be beta redexes. ▶ strict languages evaluate arguments to a value, before ▶ non-strict languages evaluate leftmost-outermost beta

  25. Faculty of Science Information and Computing Sciences 16 Example: Non-strict Evaluation @ @ @ λ y λ x b @ λ z @ a c λ x y z x

  26. Faculty of Science Information and Computing Sciences 16 Example: Non-strict Evaluation @ @ @ λ y λ x b @ λ z @ a c λ x y z x

  27. Faculty of Science Information and Computing Sciences 16 Example: Non-strict Evaluation @ @ @ λ x λ x b @ λ z x a c z

  28. Faculty of Science Information and Computing Sciences 16 Example: Non-strict Evaluation @ @ @ λ x λ x b @ λ z x a c z

  29. Faculty of Science Information and Computing Sciences 16 Example: Non-strict Evaluation @ @ @ λ z a λ x b z c

  30. Faculty of Science Information and Computing Sciences 16 Example: Non-strict Evaluation @ @ @ λ z a λ x b z c

  31. Faculty of Science Information and Computing Sciences 16 Example: Non-strict Evaluation Term is a WHNF but not a normal form. @ a @ λ x b c

  32. Faculty of Science Information and Computing Sciences 17 Semantics To complete our definition of the lambda calculus, we need to specify its semantics. To do so, we’ll define a handful of rules capturing how to perform a single evaluation step. they also need to fix the order of evaluation. These rules should – of course – include β -reduction – but

Recommend


More recommend