[plt-scheme] 3D boxes and macros

From: Arjun Guha (garjun at cs.brown.edu)
Date: Wed Jul 20 10:56:03 EDT 2005

Well, the reason it doesn't work is:

(module box-and-macro mzscheme

   (define-for-syntax the-box (box 'initial))

   (define-syntax (my-macro stx)
     (syntax-case stx ()
       [(_)
        #`(eq? #,the-box #,the-box)])))

> (my-macro)
#f

It seems as though the entire set-box! operation needs to happen in the 
transformer environment.  Here's a more involved example, that shows you 
can use a 3D box as an accumulator:

(module box-and-macro mzscheme

   (define-for-syntax the-box (box 0))

   (define-syntax (my-macro stx)
     (syntax-case stx ()
       [(_)
        #`(begin
            #,(set-box! the-box (add1 (unbox the-box)))
            (unbox #,the-box))]))
)

Welcome to DrScheme, version 299.106-svn16jun2005.
Language: (module ...).
> (my-macro)
1
> (my-macro)
2
> (my-macro)
3
> (my-macro)
4
> (my-macro)
5

I think this is fine for my purposes, but I still don't know why my first 
attempt failed.  Incidentally, if we change the line with set-box! to

(#,set-box! #,the-box (add1 (#,unbox #,the-box)))

or any other creative variant, it fails.

-Arjun

On Wed, 20 Jul 2005, Gregory Cooper wrote:

>
> I was trying to do something similar a while ago, and for some reason boxes 
> didn't seem to work correctly as 3d values.  Have you tried using a procedure 
> or hash-table to simulate the box?  (That worked for me.)
>
> On Wed, 20 Jul 2005, Arjun Guha wrote:
>
>> That doesn't seem to work either.  It seems like a new box is created--or 
>> something.
>> 
>> On Wed, 20 Jul 2005, Jens Axel Søgaard wrote:
>> 
>>> For list-related administrative tasks:
>>> http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>> 
>>> Arjun Guha wrote:
>>>> I have a box defined in the transformer environment, that I insert into 
>>>> the syntax.  I'd expect that the set-box! would first mutate the contents 
>>>> to 'outer-value, followed by the call to unbox. 
>>> 
>>> 
>>> Is this what you want?
>>> 
>>> (module box-and-macro mzscheme
>>> 
>>>  (define-for-syntax the-box (box 'initial))
>>> 
>>>  (define-syntax (my-macro stx)
>>>    (syntax-case stx ()
>>>      [(_)
>>>       #`(begin
>>>           (begin-for-syntax
>>>             (set-box! the-box 'value))
>>>           (if (eq? (unbox #,the-box) 'value)
>>>               'okay
>>>               'fail))])))
>>> 
>>> -- 
>>> Jens Axel Søgaard
>

Posted on the users mailing list.