TweetFollow Us on Twitter

Lambda
Volume Number:9
Issue Number:9
Column Tag:Lisp Listener

“The Lambda Lambada: Y Dance?”

Mutual Recursion

By André van Meulebrouck, Chatsworth, California

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

“Mathematics is thought moving in the sphere of complete abstraction from any particular instance of what it is talking about.” - Alfred North Whitehead

Welcome once again to Mutual of Omo Oz Y Old Kingdom (with apologies to the similar named TV series of yesteryears).

In this installment, Lambda, the forbidden (in conventional languages) function, does the lambada-the forbidden (in l-calculus) dance. Film at 11.

In [vanMeule Jun 91] the question was raised as to whether everything needed to create a metacircular interpreter (using combinators) has been given to the reader.

One of the last (if not the last) remaining items not yet presented is mutual recursion, which allows an interpreter’s eval and apply functions to do their curious tango (the “lambda lambada”?!?).

In this article, the derivation of a Y2 function will be shown. Y2 herein will be the sister combinator of Y, to be used for handling mutual recursion (of two functions) in the applicative order. The derivation of Y2 will be done in a similar manner as was done for deriving Y from pass-fact in [vanMeule May 92].

This exercise will hopefully give novel insights into Computer Science and the art of programming. (This is the stuff of Überprogrammers!) This exercise should also give the reader a much deeper understanding of Scheme while developing programming muscles in ways that conventional programming won’t.

Backdrop and motivation

[vanMeule Jun 91] described the minimalist game. The minimalist game is an attempt to program in Scheme using only those features of Scheme that have more or less direct counterparts in l-calculus. The aim of the minimalist game is (among other things):

1) To understand l-calculus and what it has to say about Computer Science.

2) To develop expressive skills. Part of the theory behind the minimalist game is that one’s expressive ability is not so much posited in how many programming constructs one knows, but in how cleverly one wields them. Hence, by deliberately limiting oneself to a restricted set of constructs, one is forced to exercise one’s expressive muscles in ways they would not normally get exercised when one has a large repertoire of constructs to choose from. The maxim here is: “learn few constructs, but learn them well”.

In l-calculus (and hence the minimalist game) there is no recursion. It turns out that recursion is a rather impure contortion in many ways! However, recursion can be simulated by making use of the higher order nature of l-calculus. A higher order function is a function which is either passed as an argument (to another function) or returned as a value. As thrifty as l-calculus is, it does have higher order functions, which is no small thing as very few conventional languages have such a capability, and those that do have it have only a very weak version of it. (This is one of the programming lessons to be learned from playing the minimalist game: The enormous power of higher order functions and the losses conventional languages suffer from not having them.)

Different kinds of recursion

As soon as a language has global functions or procedures and parameter passing provided via a stack discipline, you’ve got recursion! In fact, there is essentially no difference between a procedure calling itself or calling a different function-the same stack machinery that handles the one case will automatically handle the other. (There’s no need for the stack machinery to know nor care whether the user is calling other procedures or the same procedure.)

However, as soon as a language has local procedures, it makes a very big difference if a procedure calls itself! The problem is that when a local procedure sees a call to itself from within itself, by the rules of lexical scoping, it must look for its own definition outside of its own scope! This is because the symbol naming the recursive function is a free variable with respect to the context it occurs in.

; 1
>>> (let ((local-fact 
           (lambda (n)
             (if (zero? n)
                 1
                 (* n (local-fact (1- n)))))))
      (local-fact 5))
ERROR:  Undefined global variable
local-fact

Entering debugger.  Enter ? for help.
debug:> 

This is where letrec comes in.

; 2

>>> (letrec ((local-fact 
              (lambda (n)
                (if (zero? n)
                    1
                    (* n (local-fact (1- n)))))))
      (local-fact 5))
120

To understand what letrec is doing let’s translate it to its semantic equivalent. letrec can be simulated using let and set! [CR 91].

; 3
>>> (let ((local-fact ‘undefined))
      (begin
       (set! local-fact 
             (lambda (n)
               (if (zero? n)
                   1
                   (* n (local-fact (1- n))))))
       (local-fact 5)))
120

Mutual recursion is slightly different from “regular” recursion: instead of a function calling itself, it calls a different function that then calls the original function. For instance, “foo” and “fido” would be mutually recursive if foo called fido, and fido called foo. The letrec trick will work fine for mutual recursion.

; 4 

>>> (let ((my-even? ‘undefined)
          (my-odd? ‘undefined))
      (begin
       (set! my-even? 
             (lambda (n)
               (if (zero? n)
                   #t
                   (my-odd? (1- n)))))
       (set! my-odd? 
             (lambda (n)
               (if (zero? n)
                   #f
                   (my-even? (1- n)))))
       (my-even? 80)))
#t

The reason this works is because both functions that had to have mutual knowledge of each other were defined as symbols in a lexical context outside of the context in which the definitions were evaluated.

However, all the above letrec examples rely on being able to modify state. l-calculus doesn’t allow state to be modified. (An aside: since parallel machines have similar problems and restrictions in dealing with state, there is ample motivation for finding non-state oriented solutions to such problems in l-calculus.)

The recursion in local-fact can be ridded by using the Y combinator. However, in the my-even? and my-odd? example the Y trick doesn’t work because in trying to eliminate recursion using Y, the mutual nature of the functions causes us to get into a chicken-before-the-egg dilemma.

It’s clear we need a special kind of Y for this situation. Let’s call it Y2.

The pass-fact trick

[vanMeule May 92] derived the Y combinator in the style of [Gabriel 88] by starting with pass-fact (a version of the factorial function which avoids recursion by passing its own definition as an argument) and massaging it into two parts: a recursionless recursion mechanism and an abstracted version of the factorial function.

Let’s try the same trick for Y2, using my-even? and my-odd? as our starting point.

First, we want to massage my-even? and my-odd? into something that looks like pass-fact. Here’s what our “template” looks like:

; 5 

>>> (define pass-fact 
      (lambda (f n)
        (if (zero? n)
            1 
            (* n (f f (1- n))))))
pass-fact
>>> (pass-fact pass-fact 5)
120

Here’s a version of my-even? and my-odd? modeled after the pass-fact “template”.

; 6 
>>> (define even-odd
      (cons 
       (lambda (function-list)
         (lambda (n)
           (if (zero? n)
               #t
               (((cdr function-list) function-list)
                (1- n)))))
       (lambda (function-list)
         (lambda (n)
           (if (zero? n)
               #f
               (((car function-list) function-list) 
                (1- n)))))))
even-odd
>>> (define pass-even?
      ((car even-odd) even-odd))
pass-even?
>>> (define pass-odd?
      ((cdr even-odd) even-odd))
pass-odd?
>>> (pass-even? 8)
#t

This could derive one crazy!

Now that we know we can use higher order functions to get rid of the mutual recursion in my-even? and my-odd? the next step is to massage out the recursionless mutual recursion mechanism from the definitional parts that came from my-even? and my-odd?. The following is the code of such a derivation, including test cases and comments.

; 7
(define my-even?
  (lambda (n)
    (if (zero? n)
        #t
        (my-odd? (1- n)))))
;
(define my-odd?
  (lambda (n)
    (if (zero? n)
        #f
        (my-even? (1- n)))))
;
(my-even? 5)
;
; Get out of global environment-use local environment.
;
(define mutual-even?
  (letrec 
    ((my-even? (lambda (n)
                 (if (zero? n)
                     #t
                     (my-odd? (1- n)))))
     (my-odd? (lambda (n)
                (if (zero? n)
                    #f
                    (my-even? (1- n))))))
    my-even?))
;
(mutual-even? 5)
;
; Get rid of destructive letrec.  Use let instead.
; Make a list of the mutually recursive functions.
;
(define mutual-even?
  (lambda (n)
    (let 
      ((function-list 
        (cons (lambda (functions n) ; even?
                (if (zero? n)
                    #t
                    ((cdr functions) functions 
                                     (1- n))))
              (lambda (functions n) ; odd?
                (if (zero? n)
                    #f
                    ((car functions) functions 
                                     (1- n)))))))
      ((car function-list) function-list n))))
;
(mutual-even? 5)
;
; Curry, and get rid of initial (lambda (n) ...) .
;
(define mutual-even?
  (let 
    ((function-list 
      (cons (lambda (functions) ; even?
              (lambda (n) 
                (if (zero? n)
                    #t
                    (((cdr functions) functions) 
                     (1- n)))))
            (lambda (functions) ; odd?
              (lambda (n) 
                (if (zero? n)
                    #f
                    (((car functions) functions) 
                     (1- n))))))))
    ((car function-list) function-list)))
;
(mutual-even? 5)
;
; Abstract ((cdr functions) functions) out of if, etc..
;
(define mutual-even?
  (let 
    ((function-list 
      (cons (lambda (functions) 
              (lambda (n) 
                ((lambda (f)
                   (if (zero? n)
                       #t
                       (f (1- n))))
                 ((cdr functions) functions))))
            (lambda (functions) 
              (lambda (n) 
                ((lambda (f)
                   (if (zero? n)
                       #f
                       (f (1- n))))
                 ((car functions) functions)))))))
    ((car function-list) function-list)))
;
(mutual-even? 5)
;
; Massage functions into abstracted versions of 
; originals.
;
(define mutual-even?
  (let 
    ((function-list 
      (cons (lambda (functions) 
              (lambda (n) 
                (((lambda (f)
                    (lambda (n)
                      (if (zero? n)
                          #t
                          (f (1- n)))))
                  ((cdr functions) functions))
                 n)))
            (lambda (functions) 
              (lambda (n) 
                (((lambda (f)
                    (lambda (n)
                      (if (zero? n)
                          #f
                          (f (1- n)))))
                  ((car functions) functions))
                 n))))))
    ((car function-list) function-list)))
;
(mutual-even? 5)
;
; Separate abstracted functions out from recursive 
; mechanism.
;
(define mutual-even?
  (let 
    ((abstracted-functions
      (cons (lambda (f)
              (lambda (n)
                (if (zero? n)
                    #t
                    (f (1- n)))))
            (lambda (f)
              (lambda (n)
                (if (zero? n)
                    #f
                    (f (1- n))))))))
    (let 
      ((function-list 
        (cons (lambda (functions) 
                (lambda (n) 
                  (((car abstracted-functions)
                    ((cdr functions) functions))
                   n)))
              (lambda (functions) 
                (lambda (n) 
                  (((cdr abstracted-functions)
                    ((car functions) functions))
                   n))))))
      ((car function-list) function-list))))
;
(mutual-even? 5)
;
; Abstract out variable abstracted-functions in 2nd let.
;
(define mutual-even?
  (let 
    ((abstracted-functions
      (cons (lambda (f)
              (lambda (n)
                (if (zero? n)
                    #t
                    (f (1- n)))))
            (lambda (f)
              (lambda (n)
                (if (zero? n)
                    #f
                    (f (1- n))))))))
    ((lambda (abstracted-functions)
       (let 
         ((function-list 
           (cons (lambda (functions) 
                   (lambda (n) 
                     (((car abstracted-functions)
                       ((cdr functions) functions))
                      n)))
                 (lambda (functions) 
                   (lambda (n) 
                     (((cdr abstracted-functions)
                       ((car functions) functions))
                      n))))))
         ((car function-list) function-list)))
     abstracted-functions)))
;
(mutual-even? 5)
;
; Separate recursion mechanism into separate function.
;
(define y2
  (lambda (abstracted-functions)
    (let 
      ((function-list 
        (cons (lambda (functions) 
                (lambda (n) 
                  (((car abstracted-functions)
                    ((cdr functions) functions))
                   n)))
              (lambda (functions)
                (lambda (n) 
                  (((cdr abstracted-functions)
                    ((car functions) functions))
                   n))))))
      ((car function-list) function-list))))
;
(define mutual-even? 
  (y2
   (cons (lambda (f)
           (lambda (n)
             (if (zero? n)
                 #t
                 (f (1- n)))))
         (lambda (f)
           (lambda (n)
             (if (zero? n)
                 #f
                 (f (1- n))))))))
;
(mutual-even? 5)
;
; y2 has selector built into it-generalize it!
;
(define y2-choose
  (lambda (abstracted-functions)
    (lambda (selector)
      (let 
        ((function-list 
          (cons (lambda (functions) 
                  (lambda (n) 
                    (((car abstracted-functions)
                      ((cdr functions) functions))
                     n)))
                (lambda (functions)
                  (lambda (n) 
                    (((cdr abstracted-functions)
                      ((car functions) functions))
                     n))))))
        ((selector function-list) function-list)))))
;
; Now we can achieve the desired result-defining 
; both mutual-even? and mutual-odd? without recursion.
;
(define mutual-even-odd?
  (y2-choose
   (cons (lambda (f)
           (lambda (n)
             (if (zero? n)
                 #t
                 (f (1- n)))))
         (lambda (f)
           (lambda (n)
             (if (zero? n)
                 #f
                 (f (1- n))))))))
;
(define mutual-even? 
  (mutual-even-odd? car))
;
(define mutual-odd?
  (mutual-even-odd? cdr))  
;
(mutual-even? 5)
(mutual-odd? 5)
(mutual-even? 4)
(mutual-odd? 4)

Deriving Mutual Satisfaction

Notice that mutual-even? and mutual-odd? could have been defined using y2 instead of y2-choose, however, the definitional bodies of my-even? and my-odd? would have been repeated in defining mutual-even? and mutual-odd?.

Exercises for the Reader

• Herein Y2 was derived from mutual-even?. Try deriving it instead from pass-even?.

• Question for the Überprogrammer: if evaluation were normal order rather than applicative order, could we use the same version of Y for mutually recursive functions that we used for “regular” recursive functions (thus making a Y2 function unnecessary)?

• Another question: Let’s say we have 3 or more functions which are mutually recursive. What do we need to handle this situation when evaluation is applicative order? What about in normal order? (Note: evaluation in l-calculus is normal order.)

Looking Ahead

Creating a “minimalist” (i.e., combinator based) metacircular interpreter might now be possible if we can tackle the problem of manipulating state!

Thanks to:

The local great horned owls that watch over everything from on high; regularly letting fellow “night owls” know that all is well by bellowing their calming, reassuring “Who-w-h-o-o” sounds.

Bugs/infelicities due to: burning too much midnite oil!

Bibliography and References

[CR 91] William Clinger and Jonathan Rees (editors). “Revised4 Report on the Algorithmic Language Scheme”, LISP Pointers, SIGPLAN Special Interest Publication on LISP, Volume IV, Number 3, July-September, 1991. ACM Press.

[Gabriel 88] Richard P. Gabriel. “The Why of Y”, LISP Pointers, Vol. II, Number 2, October-November-December, 1988.

[vanMeule May 91] André van Meulebrouck. “A Calculus for the Algebraic-like Manipulation of Computer Code” (Lambda Calculus), MacTutor, Anaheim, CA, May 1991.

[vanMeule Jun 91] André van Meulebrouck. “Going Back to Church” (Church numerals.), MacTutor, Anaheim, CA, June 1991.

[vanMeule May 92] André van Meulebrouck. “Deriving Miss Daze Y”, (Deriving Y), MacTutor, Los Angeles, CA, April/May 1992.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

The secrets of Penacony might soon come...
Version 2.2 of Honkai: Star Rail is on the horizon and brings the culmination of the Penacony adventure after quite the escalation in the latest story quests. To help you through this new expansion is the introduction of two powerful new... | Read more »
The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »

Price Scanner via MacPrices.net

Apple Magic Keyboards for iPads are on sale f...
Amazon has Apple Magic Keyboards for iPads on sale today for up to $70 off MSRP, shipping included: – Magic Keyboard for 10th-generation Apple iPad: $199, save $50 – Magic Keyboard for 11″ iPad Pro/... Read more
Apple’s 13-inch M2 MacBook Airs return to rec...
Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices currently... Read more
Best Buy is clearing out iPad Airs for up to...
In advance of next week’s probably release of new and updated iPad Airs, Best Buy has 10.9″ M1 WiFi iPad Airs on record-low sale prices for up to $200 off Apple’s MSRP, starting at $399. Sale prices... Read more
Every version of Apple Pencil is on sale toda...
Best Buy has all Apple Pencils on sale today for $79, ranging up to 39% off MSRP for some models. Sale prices for online orders only, in-store prices may vary. Order online and choose free shipping... Read more
Sunday Sale: Apple Studio Display with Standa...
Amazon has the standard-glass Apple Studio Display on sale for $300 off MSRP for a limited time. Shipping is free: – Studio Display (Standard glass): $1299.97 $300 off MSRP For the latest prices and... Read more
Apple is offering significant discounts on 16...
Apple has a full line of 16″ M3 Pro and M3 Max MacBook Pros available, Certified Refurbished, starting at $2119 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free... Read more
Apple HomePods on sale for $30-$50 off MSRP t...
Best Buy is offering a $30-$50 discount on Apple HomePods this weekend on their online store. The HomePod mini is on sale for $69.99, $30 off MSRP, while Best Buy has the full-size HomePod on sale... Read more
Limited-time sale: 13-inch M3 MacBook Airs fo...
Amazon has the base 13″ M3 MacBook Air (8GB/256GB) in stock and on sale for a limited time for $989 shipped. That’s $110 off MSRP, and it’s the lowest price we’ve seen so far for an M3-powered... Read more
13-inch M2 MacBook Airs in stock today at App...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more

Jobs Board

Licensed Practical Nurse - Womens Imaging *A...
Licensed Practical Nurse - Womens Imaging Apple Hill - PRN Location: York Hospital, York, PA Schedule: PRN/Per Diem Sign-On Bonus Eligible Remote/Hybrid Regular Read more
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Operating Room Assistant - *Apple* Hill Sur...
Operating Room Assistant - Apple Hill Surgical Center - Day Location: WellSpan Health, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.