church numerals
play

Church Numerals Dr. Mattox Beckman University of Illinois at - PowerPoint PPT Presentation

Objectives Church Numerals Church Booleans Arbitrary Data Church Numerals Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science Objectives Church Numerals Church Booleans Arbitrary Data Objectives


  1. Objectives Church Numerals Church Booleans Arbitrary Data Church Numerals Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science

  2. Objectives Church Numerals Church Booleans Arbitrary Data Objectives ◮ Use lambda calculus to implement integers and booleans. ◮ Defjne some operations on Church numerals: inc, plus, times. ◮ Explain how to represent boolean operations: and, or, not, if. ◮ Use lambda calculus to implement arbitrary types.

  3. Objectives Church Numerals Church Booleans Arbitrary Data What Is a Number? ◮ The lambda calculus doesn’t have numbers. ◮ A number n can be thought of as a potential: someday we are going to do something n times. Some Church Numerals 1 f0 = \ f -> \ x -> x 2 f1 = \ f -> \ x -> f x 3 f2 = \ f -> \ x -> f (f x) 4 f3 = \ f -> \ x -> f (f (f x)) 1 Prelude> let show m = m ( + 1) 0 2 Prelude> show ( \ f x -> f (f x)) 3 2

  4. Objectives Church Numerals Church Booleans Arbitrary Data Incrementing Church Numerals, 0 ◮ To increment a Church numeral, what do we want to do? Running Example 1 finc = undefined

  5. Objectives Church Numerals Church Booleans Arbitrary Data Incrementing Church Numerals, 1 ◮ To increment a Church numeral, what do we want to do? ◮ First step, take the Church numeral you want to increment. Running Example 1 finc = \ m -> undefined

  6. Objectives Church Numerals Church Booleans Arbitrary Data Incrementing Church Numerals, 2 ◮ To increment a Church numeral, what do we want to do? ◮ First step, take the Church numeral you want to increment. ◮ Second step, return a Church numeral representing your result. Running Example 1 finc = \ m -> \ f x -> undefined

  7. Objectives Church Numerals Church Booleans Arbitrary Data Incrementing Church Numerals, 3 ◮ To increment a Church numeral, what do we want to do? ◮ First step, take the Church numeral you want to increment. ◮ Second step, return a Church numeral representing your result. ◮ Third step, apply f to x , m times. Running Example 1 finc = \ m -> \ f x -> m f x

  8. Objectives Church Numerals Church Booleans Arbitrary Data Incrementing Church Numerals, 4 ◮ To increment a Church numeral, what do we want to do? ◮ First step, take the Church numeral you want to increment. ◮ Second step, return a Church numeral representing your result. ◮ Third step, apply f to x , m times. ◮ Finally, apply f once more to the result. Running Example 1 finc = \ m -> \ f x -> f (m f x)

  9. Objectives Church Numerals Church Booleans Arbitrary Data Adding Church Numerals ◮ Similar reasoning can yield addition and multiplication. ◮ Here is addition. Can you fjgure our multiplication? Hint: What does ( nf ) do? ◮ Subtraction is a bit more tricky. Running Example 1 fadd m n = \ f x -> m f (n f x)

  10. Objectives Church Numerals Church Booleans Arbitrary Data Implementing Booleans ◮ Church numerals represented integers as a potential number of actions. ◮ Church Booleans represent true and false as a choice. T ≡ λ ab . a F ≡ λ ab . b 1 true = \ a b -> a 2 false = \ a b -> b 3 showb f = f True False ◮ Type these into a REPL and try them out! ◮ Next slide: and and or . Try to fjgure it out before going ahead!

  11. Objectives Church Numerals Church Booleans Arbitrary Data And and Or ◮ There are a couple of ways to do it. and ≡ λ xy . xyF or ≡ λ xy . xTy if ≡ λ cte . cte 1 and = \ x y -> x y false 2 or = \ x y -> x true y 3 cif = \ c t e -> c t e

  12. Objectives Church Numerals Church Booleans Arbitrary Data Representing Arbitrary Types ◮ Suppose we have an algebraic data type with n constructors. ◮ Then the Church representation is an abstraction that takes n parameters. ◮ Each parameter represents one of the constructors. T ≡ λ ab . a F ≡ λ ab . b

  13. Try to fjgure out how to represent linked lists .... | Nothing Objectives Church Numerals Church Booleans Arbitrary Data The Maybe Type ◮ The Maybe type has two constructors: Just and Nothing . 1 data Maybe a = Just a 2 ◮ Can you give the lambda-calculus representation for Just 3 ? Just a ≡ λ jn . ja Nothing ≡ λ jn . n

  14. | Nothing Objectives Church Numerals Church Booleans Arbitrary Data The Maybe Type ◮ The Maybe type has two constructors: Just and Nothing . 1 data Maybe a = Just a 2 ◮ Can you give the lambda-calculus representation for Just 3 ? Just a ≡ λ jn . ja Nothing ≡ λ jn . n Just 3 ≡ λ jn . j λ fx . f ( f ( fx )) ◮ Try to fjgure out how to represent linked lists ....

  15. Write a function length that determines the length of one of these lists. Assume you are allowed to use recursion. (Note, Haskell ’s type system will not let you write this.) | Nil Objectives Church Numerals Church Booleans Arbitrary Data Linked Lists ◮ A list has two constructors: Cons and Nil . 1 data List a = Cons a ( List a) 2 ◮ Can you give the lambda-calculus representation for Cons True (Cons False Nil) ? Cons x y ≡ λ cn . cxy Nil ≡ λ cn . n

  16. Objectives Church Numerals Church Booleans Arbitrary Data Linked Lists ◮ A list has two constructors: Cons and Nil . 1 data List a = Cons a ( List a) 2 | Nil ◮ Can you give the lambda-calculus representation for Cons True (Cons False Nil) ? Cons x y ≡ λ cn . cxy Nil ≡ λ cn . n λ c 1 n 1 . c 1 ( λ ab . a )( λ c 2 n 2 . c 2 ( λ ab . b )( λ c 3 n 3 . n 3 )) or ... λ cn . c ( λ ab . a )( c ( λ ab . b ) n ) ◮ Write a function length that determines the length of one of these lists. Assume you are allowed to use recursion. (Note, Haskell ’s type system will not let you write this.)

  17. Objectives Church Numerals Church Booleans Arbitrary Data Length Cons x y ≡ λ cn . cxy Nil ≡ λ cn . n Length x = x ( λ xy . inc ( Length y )) zero

  18. Abstraction: x x Application: e e e e Objectives Church Numerals Church Booleans Arbitrary Data Higher Order Abstract Syntax ◮ It is possible to represent lambda-calculus in lambda calculus! ◮ We can let variables represent themselves. ◮ This is a non-recursive version: ] f M = λ fa . [ [ M ] a ] f [ Var x ] x [ a = ] f ] f [ Abs x M ] f λ x . [ [ M ] [ a ≡ a ] f ] f ] f [ App e 1 e 2 ] a [ [ e 1 ] [ e 2 ] [ a [ a ≡ a ◮ You can then write an interpreter for this!

  19. Objectives Church Numerals Church Booleans Arbitrary Data Higher Order Abstract Syntax ◮ It is possible to represent lambda-calculus in lambda calculus! ◮ We can let variables represent themselves. ◮ This is a non-recursive version: ] f M = λ fa . [ [ M ] a ] f [ Var x ] x [ a = ] f ] f [ Abs x M ] f λ x . [ [ M ] [ a ≡ a ] f ] f ] f [ App e 1 e 2 ] a [ [ e 1 ] [ e 2 ] [ a [ a ≡ a ◮ You can then write an interpreter for this! ◮ Abstraction: λ x . x ◮ Application: λ e 1 e 2 . e 1 e 2

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