[racket] Lazy syntax class attributes

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Fri May 31 08:18:39 EDT 2013

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.