CORNELL CS 611 - Lecture 8 Lambda calculus encodings

Unformatted text preview:

CS611 Lecture 08 Lambda calculus encodings 17th Septemb er, 2001Scribe: Lin Guo, Feng Shao Lecturer: Andrew MyersThis note provides the following:• Boolean and IF• Arithmetic and integers• Data structures (lists, trees, arrays, cons cells(pairs))• Recursive functionsLambda calculus terms can become long. For compactness we will use certain names, as well as multiplearguments, as abbreviation. We will write NAME ≡ e to indicate that NAME is an abbreviation for e. Hereare some definitions for names we will use:APPLYTO FIVE ≡ (λf( f 5))COMPOSE ≡ λ (fg)(λx(f (gx)))TWICE ≡ (λf(λx(f (fx))))Here, COMPOSE composes two functions, and TWICE returns a function that calls the given functiontwice. For example :(TWICE INC)2 →∗4On the other hand, we can use COMPOSE to define the TWICE:TWICE ≡ (λf(COMPOSE ff))1BooleanLambda Calculus is universal. This means that no primitive boolean type or ’if’ statement is needed. Wecan form them as follows:TRUE ≡ (λx(λy x)) ∼ (λ(xy) x)FALSE ≡ (λx(λy y)) ∼ (λ(xy) y)IF ≡ λ(btf)(btf)So, TRUE is a function which takes two arguments and returns the first one , FA LS E returns the second oneand if e0then e1else e2⇒ IF e0e1e2. Note that call-by-name is important. e1and e2are not evaluatedeagerly by IF. So it doesn’t necessarily diverge if e1or e2does.2 ArithmeticAnother data type which we need is natural numbers.We can mo del the number n as a function that composesan arbitrary function n times, like n = f → fn.This representation is called Church numerals.Here is thedefinition:0 ≡ (λ(fx) x)(=FALSE )1 ≡ (λ(fx)(fx))2 ≡ (λ(fx)(f (fx)))3 ≡ (λ(fx)(f(f (fx))))n ≡ (λ(fx)(f (···(fx) ···)))1We can now define operations on integers. INC adds one to a number. It’s a function fn → fn+1.Sowe haveINC ≡ λn (λf (λx (f (nf) x)))+ ≡ λ(n1n2)((n1INC) n2)3 Data structureWe can construct pairs and lists. The pair/list operations are:(CONS xy): construct a list with head x and tail y(LEFT xy): return first item in list ( or first item in pair)(RIGHT xy): return remainder of list ( or second item in pair)So we have the following equations that a ny implementation must satisfy:LEFT(CONS xy) = xRIGHT(CONS xy) = yCONS((LEFT p)(RIGHT p)) = pHere is one way to implement these operations:CONS ≡ (λ(xy)(λf (f(xy)) p)LEFT ≡ λp(p TRUE)RIGHT ≡ λp(p FA LS E)If we use these operations in ways that the equations above do not handle, we get garbage. ConsiderLEFT 0 and it happens to evaluate to identity.Progr amming using these encodings is error-prone. This isa defect of this style .4 Define a Recursive FunctionsConsider a recursive function which computes the factorial of an integer. By intuitio n, we will describeFAC T as:FAC T =(λn IF (ISZERO n)1(× n (FACT (− n 1)))But this is just a description, not a definition. We need to somehow remove the recursion within thedefinition. We will do this by defining a new function of FACT’, which will be passed a function f such that((ff) n) to compute the factorial of n.FAC T≡ (λf (λn IF (ISZERO n)1(× n (ff(− n 1))))And the actual factorial function we are to define is FAC Tapplied to itself.FAC T ≡ (FACTFAC T)Now the function FACT actually works! As an example, let’s see what happens when we evaluate(FAC T n):FAC T n =(FAC TFAC Tn)= λn IF ( ISZE RO n)1(×n (FACTFAC T(n − 1) FAC T(n−1))))25 Recursion Removal TricksNow, let’s see what we just did to the FACT function to remove recursion. In general,suppose F = e ,where e mentions F , we use a 3-step process to remove the recursion in F :1. Define a new term Fwith a parameter f;2. Substitute ( ff) for all F to get F:- F≡ (λfe) {(ff)/F }3. Replace any external reference to the recursive function F with an application of our new functionapplied to itself, i.e. F ≡ FF6 Abstracting with the Fixed Point OperatorRecall our original recursive description of the factorial function:FAC T =(λn IF (ISZERO n )1(× n ( FAC T(− n 1)))This description’s solution is the factorial function. Note that we can simplify this equation by introducinga new function, say FACTEQN:FACTEQN ≡ λf(λn IF (ISZERO n)1(× n (f (− n 1)))and as a result:FAC T ≡ (FACTEQN FACT)Thus, FAC T is a fixed point of FACTEQN. Suppose we have an operator FIX that found the fixed point offunctions. In other words, for any function f,(FIX f)=f ( FIX f )So we can define FIX as:FIX =(λf(f (FIX f)))Now we can apply the removal technique we used above to FIX,FIX’ ≡ (λy(λf(f (yy f))))FIX ≡ (FIXFIX)The traditional form of FIX, which requires call-by-name, is the Y combinator:Y ≡ (λf((λx(f (xx)) (λx(f (xx))))))Both of these definitions have the defect that they diverge when used in a CBV language. We can addressthis by noting that we only expect ( FIX f) to be extensionally equal to f (FIX f ):(FIX f) x = f (FIX f ) xFIX = λf(λx(f (FIX f ) x))FIX≡ λyλf(λx(f (yyf) x))FIX ≡ FIX’ FIX’The Y combinator can be similarly repaired:YCBV≡ λf((λx(λy(f (xx) y))) (λx(λy(f (xx)


View Full Document

CORNELL CS 611 - Lecture 8 Lambda calculus encodings

Download Lecture 8 Lambda calculus encodings
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 8 Lambda calculus encodings 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 8 Lambda calculus encodings 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?