[racket] Lazy syntax class attributes

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Fri May 31 11:37:21 EDT 2013

You would have to implement laziness for attributes manually -- I think. 
But I don't have time to play with code and get an example to work right
now. -- Matthias

On May 31, 2013, at 11:00 AM, Eric Dobson wrote:

> That seems like a a workable amount of overhead, but I'm not sure what
> you are suggesting the v attribute to be bound as. It might have not
> been obvious from my my example that its value depends on the slow
> computation, so unless pattern variables can be mutated I'm not sure
> how force-all can change what e.v resolves to.
> On Fri, May 31, 2013 at 5:18 AM, Matthias Felleisen
> <matthias at ccs.neu.edu> wrote:
>> Can you prefix the #'(complicated ..) expression with a form
>> that forces all lazy computations so that complicated itself
>> remains the same?
>>  (let () (force-all) #'(complicated (form e.v)))
>> where force-all is defined via a begin in the slow syntax class?
>> -- Matthias
>> On May 31, 2013, at 2:47 AM, Eric Dobson wrote:
>>> I'm working on code (TR optimizer) that needs to match some
>>> expressions and then compute an attribute based on the expression. I
>>> would like to abstract this out as a syntax class. Example is below:
>>> #lang racket
>>> (require syntax/parse)
>>> (define-syntax-class slow
>>> (pattern e:expr
>>>          #:with v (begin (displayln 'slow) #''e)))
>>> (syntax-parse #'(x 2)
>>> ((e:slow 1) #'(complicated (form e.v)))
>>> ((e:slow 2) #'(complicated (form e.v))))
>>> The issue is that computing the attribute is slow/expensive/effectful
>>> and so I only want to do the calculation in the case that I use the
>>> result. One solution is to attach a thunk as the attribute value, but
>>> this requires changing #'(complicated (form e.v)) to #`(complicated
>>> (form #,((attribute e.v)))) which quickly becomes less readable with
>>> many such attributes in the final syntax object. Is there a way to do
>>> this in a lazy manner that does not complicate the use of the syntax
>>> class? I'm fine adding complexity to the creation of the syntax class
>>> because it is a one time cost versus the many places where the syntax
>>> class would be used.
>>> ____________________
>>> Racket Users list:
>>> http://lists.racket-lang.org/users

Posted on the users mailing list.