programming language concepts lecture 15
play

Programming Language Concepts: Lecture 15 Madhavan Mukund Chennai - PowerPoint PPT Presentation

Programming Language Concepts: Lecture 15 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 15, 18 March 2009 -calculus A notation for computable functions


  1. Programming Language Concepts: Lecture 15 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 15, 18 March 2009

  2. λ -calculus ◮ A notation for computable functions ◮ Alonzo Church

  3. λ -calculus ◮ A notation for computable functions ◮ Alonzo Church ◮ How do we describe a function? ◮ By its graph — a binary relation between domain and codomain ◮ Single-valued ◮ Extensional — graph completely defines the function

  4. λ -calculus ◮ A notation for computable functions ◮ Alonzo Church ◮ How do we describe a function? ◮ By its graph — a binary relation between domain and codomain ◮ Single-valued ◮ Extensional — graph completely defines the function ◮ An extensional definition is not suitable for computation ◮ All sorting functions are the same!

  5. λ -calculus ◮ A notation for computable functions ◮ Alonzo Church ◮ How do we describe a function? ◮ By its graph — a binary relation between domain and codomain ◮ Single-valued ◮ Extensional — graph completely defines the function ◮ An extensional definition is not suitable for computation ◮ All sorting functions are the same! ◮ Need an intensional definition ◮ How are outputs computed from inputs?

  6. λ -calculus: syntax ◮ Assume a set Var of variables ◮ Set Λ of lambda expressions is given by Λ = x | λ x . M | MM ′ where x ∈ Var , M , M ′ ∈ Λ.

  7. λ -calculus: syntax ◮ Assume a set Var of variables ◮ Set Λ of lambda expressions is given by Λ = x | λ x . M | MM ′ where x ∈ Var , M , M ′ ∈ Λ. ◮ λ x . M : Abstraction ◮ A function of x with computation rule M . ◮ “Abstracts” the computation rule M over arbitrary input values x ◮ Like writing f ( x ) = e without assigning a name f

  8. λ -calculus: syntax ◮ Assume a set Var of variables ◮ Set Λ of lambda expressions is given by Λ = x | λ x . M | MM ′ where x ∈ Var , M , M ′ ∈ Λ. ◮ λ x . M : Abstraction ◮ A function of x with computation rule M . ◮ “Abstracts” the computation rule M over arbitrary input values x ◮ Like writing f ( x ) = e without assigning a name f ◮ MM ′ : Application ◮ Apply the function M to the argument M ′

  9. λ -calculus: syntax . . . ◮ Can write expressions such as xx — no types!

  10. λ -calculus: syntax . . . ◮ Can write expressions such as xx — no types! ◮ What can we do without types?

  11. λ -calculus: syntax . . . ◮ Can write expressions such as xx — no types! ◮ What can we do without types? ◮ Set theory as a basis for mathematics ◮ Bit strings in memory

  12. λ -calculus: syntax . . . ◮ Can write expressions such as xx — no types! ◮ What can we do without types? ◮ Set theory as a basis for mathematics ◮ Bit strings in memory ◮ In an untyped world, some data is meaningful

  13. λ -calculus: syntax . . . ◮ Can write expressions such as xx — no types! ◮ What can we do without types? ◮ Set theory as a basis for mathematics ◮ Bit strings in memory ◮ In an untyped world, some data is meaningful ◮ Functions manipulate meaningful data to yield meaningful data

  14. λ -calculus: syntax . . . ◮ Can write expressions such as xx — no types! ◮ What can we do without types? ◮ Set theory as a basis for mathematics ◮ Bit strings in memory ◮ In an untyped world, some data is meaningful ◮ Functions manipulate meaningful data to yield meaningful data ◮ Can also apply functions to non-meaningful data, but the result has no significance

  15. The computation rule β ◮ Basic rule for computing (rewriting) is called β ( λ x . M ) M ′ → β M { x ← M ′ } ◮ M { x ← M ′ } : substitute free occurrences of x in M by M ′

  16. The computation rule β ◮ Basic rule for computing (rewriting) is called β ( λ x . M ) M ′ → β M { x ← M ′ } ◮ M { x ← M ′ } : substitute free occurrences of x in M by M ′ ◮ This is the normal rule we use for functions:

  17. The computation rule β ◮ Basic rule for computing (rewriting) is called β ( λ x . M ) M ′ → β M { x ← M ′ } ◮ M { x ← M ′ } : substitute free occurrences of x in M by M ′ ◮ This is the normal rule we use for functions: f ( x ) = 2 x 2 + 3 x + 4

  18. The computation rule β ◮ Basic rule for computing (rewriting) is called β ( λ x . M ) M ′ → β M { x ← M ′ } ◮ M { x ← M ′ } : substitute free occurrences of x in M by M ′ ◮ This is the normal rule we use for functions: f ( x ) = 2 x 2 + 3 x + 4 f (7) = 2 · 7 2 + 3 · 7 + 4 = (2 x 2 + 3 x + 4) { x ← 7 } .

  19. The computation rule β ◮ Basic rule for computing (rewriting) is called β ( λ x . M ) M ′ → β M { x ← M ′ } ◮ M { x ← M ′ } : substitute free occurrences of x in M by M ′ ◮ This is the normal rule we use for functions: f ( x ) = 2 x 2 + 3 x + 4 f (7) = 2 · 7 2 + 3 · 7 + 4 = (2 x 2 + 3 x + 4) { x ← 7 } . ◮ β is the only rule we need!

  20. The computation rule β ◮ Basic rule for computing (rewriting) is called β ( λ x . M ) M ′ → β M { x ← M ′ } ◮ M { x ← M ′ } : substitute free occurrences of x in M by M ′ ◮ This is the normal rule we use for functions: f ( x ) = 2 x 2 + 3 x + 4 f (7) = 2 · 7 2 + 3 · 7 + 4 = (2 x 2 + 3 x + 4) { x ← 7 } . ◮ β is the only rule we need! ◮ MM ′ is meaningful only if M is of the form λ x . M ′′ ◮ Cannot do anything with expressions like xx

  21. Variable capture ◮ Consider ( λ x . ( λ y . xy )) y

  22. Variable capture ◮ Consider ( λ x . ( λ y . xy )) y ◮ β yields λ y . yy ◮ The y substituted for inner x has been “confused” with the y bound by λ y ◮ Rename bound variables to avoid capture ( λ x . ( λ y . xy )) y = ( λ x . ( λ z . xz )) y → β λ z . yz ◮ Renaming bound variables does not change the function ◮ f ( x ) = 2 x + 5 vs f ( z ) = 2 z + 5

  23. Variable capture Formally, bound and free variables are defined as ◮ FV ( x ) = { x } , for any variable x ◮ FV ( λ x . M ) = FV ( M ) − { x } ◮ FV ( MM ′ ) = FV ( M ) ∪ FV ( M ′ )

  24. Variable capture Formally, bound and free variables are defined as ◮ FV ( x ) = { x } , for any variable x ◮ FV ( λ x . M ) = FV ( M ) − { x } ◮ FV ( MM ′ ) = FV ( M ) ∪ FV ( M ′ ) ◮ BV ( x ) = ∅ , for any variable x ◮ BV ( λ x . M ) = BV ( M ) ∪ { x } ◮ BV ( MM ′ ) = BV ( M ) ∪ BV ( M ′ )

  25. Variable capture Formally, bound and free variables are defined as ◮ FV ( x ) = { x } , for any variable x ◮ FV ( λ x . M ) = FV ( M ) − { x } ◮ FV ( MM ′ ) = FV ( M ) ∪ FV ( M ′ ) ◮ BV ( x ) = ∅ , for any variable x ◮ BV ( λ x . M ) = BV ( M ) ∪ { x } ◮ BV ( MM ′ ) = BV ( M ) ∪ BV ( M ′ ) When we apply β to MM ′ , assume that we always rename the bound variables in M to avoid “capturing” free variables from M ′ .

  26. Encoding arithmetic In set theory, use nesting depth to encode numbers ◮ Encoding of n : � n � ◮ � n � = {� 0 � , � 1 � , . . . , � n − 1 �}

  27. Encoding arithmetic In set theory, use nesting depth to encode numbers ◮ Encoding of n : � n � ◮ � n � = {� 0 � , � 1 � , . . . , � n − 1 �} Thus 0 = ∅ 1 = {∅} {∅ , {∅}} 2 = 3 = {∅ , {∅} , {∅ , {∅}}} . . .

  28. Encoding arithmetic In set theory, use nesting depth to encode numbers ◮ Encoding of n : � n � ◮ � n � = {� 0 � , � 1 � , . . . , � n − 1 �} Thus 0 = ∅ 1 = {∅} {∅ , {∅}} 2 = 3 = {∅ , {∅} , {∅ , {∅}}} . . . In λ -calculus, encode n by number of times we apply a function

  29. Encoding arithmetic . . . Church numerals � 0 � = λ fx . x � n + 1 � = λ fx . f ( � n � fx )

  30. Encoding arithmetic . . . Church numerals � 0 � = λ fx . x � n + 1 � = λ fx . f ( � n � fx ) For instance � 1 � = λ fx . f ( � 0 � fx ) = λ fx . ( f (( λ fx . x ) fx ))

  31. Encoding arithmetic . . . Church numerals � 0 � = λ fx . x � n + 1 � = λ fx . f ( � n � fx ) For instance � 1 � = λ fx . f ( � 0 � fx ) = λ fx . ( f (( λ fx . x ) fx )) Note that � 0 � gy → β ( λ x . x ) y → β y . Hence � 1 � = . . . = λ fx . ( f (( λ fx . x ) fx )) → β λ fx . ( fx ) � �� � apply β So � 1 � gy → β ( λ x . ( gx )) y → β gy

  32. Church numerals . . . � 2 � = λ fx . f ( � 1 � fx ) = λ fx . ( f ( λ fx . ( fx ) fx ) ) → β λ fx . ( f ( fx )) � �� � apply β so, � 2 � gy → β λ x . ( g ( gx )) y = g ( gy )

  33. Church numerals . . . � 2 � = λ fx . f ( � 1 � fx ) = λ fx . ( f ( λ fx . ( fx ) fx ) ) → β λ fx . ( f ( fx )) � �� � apply β so, � 2 � gy → β λ x . ( g ( gx )) y = g ( gy ) ◮ Let g k y denote g ( g ( . . . ( gy ))) with k applications of g to y ◮ Show by induction that � n � = λ fx . f ( � n − 1 � fx ) → β . . . → β λ fx . ( f n x )

  34. Encoding arithmetic functions . . . Successor ◮ succ ( n ) = n + 1 ◮ Define as λ pfx . f ( pfx )

  35. Encoding arithmetic functions . . . Successor ◮ succ ( n ) = n + 1 ◮ Define as λ pfx . f ( pfx ) ( λ pfx . f ( pfx )) � n �

  36. Encoding arithmetic functions . . . Successor ◮ succ ( n ) = n + 1 ◮ Define as λ pfx . f ( pfx ) ( λ pfx . f ( pfx )) � n � → β λ fx . f ( � n � fx )

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend