objectives church numerals
play

Objectives Church Numerals Use lambda calculus to implement - PowerPoint PPT Presentation

Objectives Church Numerals Church Booleans Arbitrary Data Objectives Church Numerals Church Booleans Arbitrary Data Objectives Church Numerals Use lambda calculus to implement integers and booleans. Defjne some operations on Church


  1. Objectives Church Numerals Church Booleans Arbitrary Data Objectives Church Numerals Church Booleans Arbitrary Data Objectives Church Numerals ◮ Use lambda calculus to implement integers and booleans. ◮ Defjne some operations on Church numerals: Dr. Mattox Beckman inc, plus, times. ◮ Explain how to represent boolean operations: and, or, not, if. University of Illinois at Urbana-Champaign Department of Computer Science ◮ Use lambda calculus to implement arbitrary types. Objectives Church Numerals Church Booleans Arbitrary Data Objectives Church Numerals Church Booleans Arbitrary Data What Is a Number? Incrementing Church Numerals, 0 ◮ 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. ◮ To increment a Church numeral, what do we want to do? Some Church Numerals Running Example 1 f0 = \ f -> \ x -> x 2 f1 = \ f -> \ x -> f x 1 finc = undefined 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

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

  3. Objectives Church Numerals Church Booleans Arbitrary Data Objectives Church Numerals Church Booleans Arbitrary Data Adding Church Numerals Implementing Booleans ◮ Church numerals represented integers as a potential number of actions. ◮ Church Booleans represent true and false as a choice. ◮ Similar reasoning can yield addition and multiplication. ◮ Here is addition. Can you fjgure our multiplication? Hint: What does ( nf ) do? T ≡ λ ab . a ◮ Subtraction is a bit more tricky. F ≡ λ ab . b Running Example 1 true = \ a b -> a 2 false = \ a b -> b 3 showb f = f True False 1 fadd m n = \ f x -> m f (n f x) ◮ Type these into a REPL and try them out! ◮ Next slide: and and or . Try to fjgure it out before going ahead! Objectives Church Numerals Church Booleans Arbitrary Data Objectives Church Numerals Church Booleans Arbitrary Data And and Or Representing Arbitrary Types ◮ There are a couple of ways to do it. ◮ Suppose we have an algebraic data type with n constructors. ◮ Then the Church representation is an abstraction that takes n parameters. and ≡ λ xy . xyF ◮ Each parameter represents one of the constructors. or ≡ λ xy . xTy if ≡ λ cte . cte T ≡ λ ab . a 1 and = \ x y -> x y false F ≡ λ ab . b 2 or = \ x y -> x true y 3 cif = \ c t e -> c t e

  4. Try to fjgure out how to represent linked lists .... 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.) Objectives Church Numerals Church Booleans Arbitrary Data Objectives Church Numerals Church Booleans Arbitrary Data The Maybe Type The Maybe Type ◮ The Maybe type has two constructors: Just and Nothing . ◮ The Maybe type has two constructors: Just and Nothing . 1 data Maybe a = Just a 1 data Maybe a = Just a 2 2 | Nothing | Nothing ◮ Can you give the lambda-calculus representation for Just 3 ? ◮ Can you give the lambda-calculus representation for Just 3 ? Just a ≡ λ jn . ja Just a ≡ λ jn . ja Nothing ≡ λ jn . n Nothing ≡ λ jn . n Just 3 ≡ λ jn . j λ fx . f ( f ( fx )) ◮ Try to fjgure out how to represent linked lists .... Objectives Church Numerals Church Booleans Arbitrary Data Objectives Church Numerals Church Booleans Arbitrary Data Linked Lists Linked Lists ◮ A list has two constructors: Cons and Nil . ◮ A list has two constructors: Cons and Nil . 1 data List a = Cons a ( List a) 1 data List a = Cons a ( List a) 2 2 | Nil | Nil ◮ Can you give the lambda-calculus representation for ◮ Can you give the lambda-calculus representation for Cons True (Cons False Nil) ? Cons True (Cons False Nil) ? Cons x y ≡ λ cn . cxy Cons x y ≡ λ cn . cxy Nil ≡ λ cn . n 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.)

  5. Abstraction: x x Application: e e e e Objectives Church Numerals Church Booleans Arbitrary Data Objectives Church Numerals Church Booleans Arbitrary Data Length 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: Cons x y ≡ λ cn . cxy Nil ≡ λ cn . n ] f M = λ fa . [ [ M ] a ] f [ Var x ] x [ a = ] f ] f [ Abs x M ] f λ x . [ [ M ] Length x = x ( λ xy . inc ( Length y )) zero [ 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! 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

Recommend


More recommend