[R6RS] Yet another question (to Matthew I guess)

R. Kent Dybvig dyb
Mon Oct 4 11:18:16 EDT 2004


I'm not sure I understand what you mean, since no amount of optimization
can even make foo terminate in a reasonable amount of time, in general,
but the call to foo in gee can be recognized as a call to foo and
optimized at the compiler's pleasure.

Here's what our letrec handling algorithm effectively produces for your
example, prior to optimization.

(define export-gee) ; actually a generated module export name
(let ([valid? #f])
  (letrec ([foo (lambda ()
                  (let loop ([n 1000000000000000000000000000000000000000000000])
                     (if (> n 0)   
                         (begin
                            (bar n)
                            (loop (- n 1))))))]
           [bar (lambda (n) <whatever you want>)]
           [gee (lambda () ...(foo)...)])
    (if (read)
        ((begin
           (if (not valid?) (undefined-variable-error 'foo))
          foo)))
    (set! valid? #t)
    (set! export-gee gee)))

Since foo is not assigned, the call to foo from gee can be optimized,
as well as, for that matter, the call to foo in the code produced for
(if (read) (foo)).  Chez Scheme will inline foo in both places if it
decides that the resulting code is small enough.

(The output above assumes letrec semantics; letrec* semantics produces
slightly different code, but the same optimizations are possible.)

Kent


More information about the R6RS mailing list