[plt-scheme] I'm not saying it's a bug, but I don't understand the behavior of this code

From: Kyle Smith (airfoil at bellsouth.net)
Date: Wed Nov 15 18:24:22 EST 2006

I was fooling around with a Help Desk illustration of the separation of 
transformer and run environments.  And it occurred to me that I could 
get around a require-for-syntax, in their example, by using defines 
internal to the definition of the syntax.  The code below is my  version 
of the let1 example, which works exactly as I would expect.  These 
examples were run on both 352.9 and now 359.200 with the same results.  
With the let behind the lambda providing a pseudo static variable, and 
the function inc! defined after the lambda, the counter runs up during 
expansion phase, then the output from the transformer syntax displays 
the same numbers after requiring the module.

(module let1 mzscheme
  (define-syntax let1
    (let ([count 1])
      (lambda (stx)
        (define (inc!) (set! count (add1 count)))
       
        (syntax-case stx ()
          [(_ x v b)
           (begin
             (printf "expanding ~a~n" count)
             (inc!)
             (syntax (let ([x v])
                       (let ([x b]) (printf "~a~n" x) x))))]))))
 
  (let1 x (let1 x (let1 x (let1 x 0 (add1 x)) (add1 x)) (add1 x)) (add1 x))
  )
(require let1)
=>
expanding 1
expanding 2
expanding 3
expanding 4
1
2
3
4

However, after some more playing with it I noticed that if the same code 
were put inside a let statement, or an equivalent lambda as in this 
example, the counter stutters  on the first number, repeating it twice.  
Which I suppose means that somehow it has gone through an expansion once 
without saving the state of count.  This is the part I don't 
understand.  See example below.

(module let1 mzscheme
  ((lambda ()
     (define-syntax let1
       (let ([count 1])
         (lambda (stx)
           (define (inc!) (set! count (add1 count)))
           
           (syntax-case stx ()
             [(_ x v b)
              (begin
                (printf "expanding ~a~n" count)
                (inc!)
                (syntax (let ([x v])
                          (let ([x b]) (printf "~a~n" x) x))))]))))
     
     (let1 x (let1 x (let1 x (let1 x 0 (add1 x)) (add1 x)) (add1 x)) 
(add1 x))))
  )
(require let1)
=>
expanding 1
expanding 1
expanding 2
expanding 3
1
2
3
4

I would appreciate very much if anyone has better insight into this than 
I do.

Thanks,

--kyle
airfoil at bellsouth.net
http://schemekeys.blogspot.com/


Posted on the users mailing list.