CORNELL CS 611 - Lecture 4 Encodings and Recursion

Unformatted text preview:

CS611 Lecture 4 λ Encodings and Recursion 6 September 2006Lecturer: Dexter Kozen1 Encoding Booleans, Natural Numbers, and Data StructuresEven though the pure λ-calculus consists only of λ-terms, we can represent and manipulate common dataobjects like integers, Boolean values, lists, and trees. All these things can be encoded as λ-terms.1.1 Encoding BooleansThe Booleans are the easiest to encode, so let us start with them. We would like to define the Booleanconstants TRUE and FALSE and the Boolean operators IF, AND, OR, NOT, etc. so that they behave inthe expected way. There are many reasonable encodings. One good one is to define TRUE and FALSE by:TRUE4= λxy. xFALSE4= λxy. y.Now we would like to define the conditional test IF. We would like IF to take three arguments b, t, f,where b is a Boolean value and t, f are arbitrary λ-terms. The function should return t if b = TRUE and fif b = FALSE.IF = λb tf.t, if b = TRUE,f, if b = FALSE.Now the reason for defining TRUE and FALSE the way we did becomes clear. Since TRUE t f → t andFALSE t f → f, all IF has to do is apply its Boolean argument to the other two arguments:IF4= λb tf. b t fThe other Bo ole an operators can be defined from IF:AND4= λb1b2. IF b1b2FALSEOR4= λb1b2. IF b1TRUE b2NOT4= λb1. IF b1FALSE TRUEWhereas these operators work correctly when given Boolean values as we have defined them, all betsare off if they are applied to any other λ-term. There is no guarantee of any kind of reasonable behavior.Basically, with the untyped λ-calculus, it is garbage in, garbage out.1.2 Encoding IntegersWe will encode natural numbers N using Church numerals. This is the same enco ding that Alonzo Churchused, although there are other reasonable encodings. The Church numeral for the number n ∈ N is denotedn. It is the λ-term λfx. fnx, where fndenotes the n-fold comp osition of f with itself:04= λfx. f0x = λfx. x14= λfx. f1x = λfx. f x24= λfx. f2x = λfx. f(f x)34= λfx. f3x = λfx. f(f(f x))...n4= λfx. fnx = λfx. f(f(. . . (f| {z }nx) . . . )1We can define the successor function s ass4= λnfx. f(n f x).That is, s on input n returns a function that takes a function f as input, applies n to it to get the n-foldcomposition of f with itself, then composes that with one more f to get the (n + 1)-fold composition of fwith itself. Thens n = (λnfx. f(n f x)) n→ λfx. f (n f x)→ λfx. f (fnx)= λfx. fn+1x= n + 1.We can perform basic arithmetic with Church numerals. For addition, we might defineADD4= λm n f x. m f (n f x).On input m and n, this function returns(λm n f x. m f (n f x)) m n → λf x. m f (n f x)→ λf x. fm(fnx)= λf x. fm+nx= m + n.Here we are composing fmwith fnto get fm+n.Alternatively, recall that Church numerals act on a function to apply that function repeatedly, andaddition can be viewed as repeated application of the successor function, so we could defineADD4= λm n. m s n.Similarly, multiplication is just iterated addition, and exponentiation is iterated multiplication:MUL4= λm n. m (ADD n) 0 EXP4= λm n. m (MU L n) 1.1.3 Pairing and ProjectionsLogic and arithmetic are good places to start, but we still are lacking any useful data structures. Forexample, consider ordered pairs. It would be nice to have a pairing function PAIR with projections FIRSTand SECOND that obeyed the following equational specifications:FIRST (PAIR e1e2) = e1SECOND (PAIR e1e2) = e2PAIR (FIRST p) (SECOND p) = p,provided p is a pair. We can take a hint from IF. Recall that IF selects one of its two branch optionsdepending on its Boolean argument. PAIR can do something similar, wrapping its two arguments for laterextraction by some function f:PAIR4= λabf. f a b.Thus PAIR e1e2→ λf. f e1e2. To get e1back out, we can just apply this to TRUE: (λf. f e1e2) TRUE →TRUE e1e2→ e1, and similarly applying it to FALSE extracts e2. Thus we can defineFIRST4= λp. p TRUE SECOND4= λp. p FALSE.Again, if p isn’t a term of the form PAIR a b, expect the unexpected.22 Recursion and the Y CombinatorWith an encoding for IF, we have some control over the flow of a program. We can also write simple forloops using the Church numerals n. However, we do not yet have the ability to write an unbounded whileloop or a recursive function.In ML, we can write the factorial function recursively asfun fact(n) = if n ≤ 1 then 1 else n*fact(n-1)But how can we write this in the λ-calculus, where all the functions are anonymous? We must somehowconstruct a λ-term FACT that satisfies the equationFACT = λn. IF (n ≤ 1) 1 (MUL n (FACT (SUB n 1))) (1)Equivalently, we must construct a fixpoint of the map F defined byF4= λf. λn. IF (n ≤ 1) 1 (MUL n (f (SUB n 1)));that is, a λ-term FACT such that F (FACT) = FACT. Any solution of (1) will do; different solutions maydisagree on non-integers, but one can show inductively that any solution of (1) will yield n! on input n. Thusit is only a question of existence.Now consider the λ-term(λx. F (x x)) (λx. F (x x)).This is a fixpoint of F , since(λx. F (x x)) (λx. F (x x)) → F ((λx. F (x x)) (λx. F (x x))).Moreover, this construction does not depend on the nature of F , so we can defineY4= λf. (λx. f (x x)) (λx. f (x x)).Then for any f, we have that Y f is a fixpoint of f; that is, Y f = f (Y f ).This Y is the infamous Y combinator, a closed λ-term that constructs solutions to recursive equations ina uniform way.Curiously, although every λ-term is a fixpoint of the identity map λx. x, the Y combinator produces aparticularly unfortunate one, namely the divergent λ-term Ω introduced in Lecture


View Full Document

CORNELL CS 611 - Lecture 4 Encodings and Recursion

Download Lecture 4 Encodings and Recursion
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 4 Encodings and Recursion and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 4 Encodings and Recursion 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?