Title

SRFI 31: A special form rec for recursive evaluation

Author

Mirko Luedde <Mirko.Luedde@SAP.com>

Status

This SRFI is currently in ``final'' status. To see an explanation of each status that a SRFI can hold, see here. You can access the discussion via the archive of the mailing list.

Abstract

We propose the implementation of a special form called rec. This form is a generalization and combination of the forms rec and named-lambda of [Clinger1985]. It allows the simple and non-imperative construction of self-referential expressions. As an important special case, it extends the A. Church form lambda such that it allows the direct definition of recursive procedures without using further special forms like let or letrec, without using advanced constructions like the H. B. Curry combinator and, unlike define, without introducing variable bindings into the external environment.

Rationale

General

Among the prominent features of the Scheme programming language as defined in [KCR1998] are the following.
  1. It has simple syntax.
  2. It encourages recursive definitions, e.g. by ensuring memory efficient tail recursion.
  3. It supports non-imperative programming.
Nevertheless Scheme does not provide a syntax for recursive evaluations with the properties of
  1. being as simple, intuitive and close to the mathematical standard notation as possible,
  2. allowing general recursion,
  3. being non-imperative.

Example

Problem 1

Let us look at the factorial function. In mathematical notation this function is expressed as
 
  (F : N |--> 1,            if N = 0; 
              N * F(N - 1), otherwise).
This expression is a term and not a definition or proposition.

We investigate some approaches to express the factorial function in Scheme.

Solution 1

A solution to our problem was already provided in [Clinger1985] by the form named-lambda. An even earlier solution with a slightly different syntax was implemented in Kent Dybvig's Chez Scheme system. Using this special form, we can denote the factorial simply by
(named-lambda (F N) 
              (if (zero? N) 1 
                (* N (F (- N 1)))))
This expression is a function term that denotes the factorial in the appropriate brevity.

However, the form named-lambda has been dropped from later versions of the Scheme Report. Also it is missing in state-of-the-art implementations such as Chez Scheme (6.0a) and MIT Scheme (7.7.0). (The latter actually knows a form named-lambda with different semantics).

Problem 2

The constant stream of ones can be defined via
(define S (cons 1 (delay S)))
As in the case of the factorial, we are able to define the recursive object at the price of spending an externally bound name. Remedying this with let or letrec leads to similar objections as above.

Solution 2

This particular case of the self-referential problem was solved by the rec form in [Clinger1985]. This form allows writing
(rec S (cons 1 (delay S)))
This expression is non-imperative and does not introduce an external variable binding.

Also this form has been dropped from later versions of the Scheme Report. Moreover, from our point of view this form alone is not capable of solving Problem 1. The respective definition would look like

(rec F 
     (lambda (N) 
       (if (zero? N) 1 
	 (* N (F (- N 1))))))
This again does not seem quite as simple and intuitive as the mathematical notation.

Proposal

We therefore propose to implement the rec special form in a generalized way that combines the advantages of the named-lambda and rec forms. The factorial function could be written
(rec (F N) 
     (if (zero? N) 1 
       (* N (F (- N 1)))))

Specification

Syntax

The following production rules are to be added to those of [KCR1998] (we reuse names of non-terminals).
  1. <derived expression> --> <rec expression>
  2. <rec expression> --> (rec <variable> <expression>)
  3. <rec expression> --> (rec (<variable>+) <body>)

Semantics

Scheme versions such as [KCR1998] providing define-syntax, syntax-rules, letrec and lambda might implement rec as follows.
(define-syntax rec
  (syntax-rules ()
    ((rec (NAME . VARIABLES) . BODY)
     (letrec ( (NAME (lambda VARIABLES . BODY)) ) NAME))
    ((rec NAME EXPRESSION)
     (letrec ( (NAME EXPRESSION) ) NAME))))

Test

The following session shows in which way rec allows a tail-recursive implementation of the factorial function.
> (define F (rec (F N)
		((rec (G K L)
		   (if (zero? K) L
		     (G (- K 1) (* K L)))) N 1)))
> F
#<procedure>
> (F 0)
1
> (F 10)
3628800

Acknowledgements

The author thanks Al Petrofsky for the final solution and Hal Abelson, Chris Hanson and others for their input. The work of the maintainers of the SRFI forum is highly appreciated.

Bibliography

Copyright

Copyright (C) Dr. Mirko Luedde (2002). All Rights Reserved.

This document and translations of it may be copied and furnished to others, and derivative works that comment on or otherwise explain it or assist in its implementation may be prepared, copied, published and distributed, in whole or in part, without restriction of any kind, provided that the above copyright notice and this paragraph are included on all such copies and derivative works. However, this document itself may not be modified in any way, such as by removing the copyright notice or references to the Scheme Request For Implementation process or editors, except as needed for the purpose of developing SRFIs in which case the procedures for copyrights defined in the SRFI process must be followed, or as required to translate it into languages other than English.

The limited permissions granted above are perpetual and will not be revoked by the authors or their successors or assigns.

This document and the information contained herein is provided on an "AS IS" basis and THE AUTHOR AND THE SRFI EDITORS DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.


Author: Mirko Luedde
Editor: Francisco Solsona