[plt-scheme] about letrec and continuation : which behavior is correct ? and why ?

From: Sam TH (samth at ccs.neu.edu)
Date: Wed Aug 20 11:57:03 EDT 2008

On Wed, Aug 20, 2008 at 11:35 AM, Matthias Felleisen
<matthias at ccs.neu.edu> wrote:
> The reason letrec has an "expansive" semantics (let + set!) is so that we
> can use abstractions even for the definition of mutually recursive
> functions:
>  (letrec ((co1 (make-coroutine-for-game "Matthias"))
>          (co2 (make-coroutine-for-game "Abdulaziz")))
>   (coroutine-start co1 (pick-card)))
> is the kind of "poem" that we used (at Indiana) to justify the EXPRESSION
> rhs for letrec.
> Reality is, we do use this power on rare occasions but perhaps we should
> admit by now, these occasions are rare and the cost is high.
> How about:
> 1. LETREC RHS must be syntactic values. (Note: ML got away with this for
> POLY LET after Andrew showed that there were like fewer than 10 uses in 100
> Kloc of SML code.)
> 2. LETREC RHS must be "provably equal" to values without effects. That is,
> we allow beta_value and nothing else.
> 1b. LETREC RHS must be "expansionably equal" (macro expansion) to values.
> That's probably the proper compromise.
> Nice, easy spec and we won't miss much of the other opportunities. --
> Matthias

Note that this rules out the following code:

(define (f x)
  (define local-var (add1 x))
  (define (do-stuff . args) ...)
  (do-stuff x local-var))

which I think would be a big loss.  What precisely are we gaining by
restricting `letrec'?

sam th
samth at ccs.neu.edu

Posted on the users mailing list.