[racket] Lazy syntax class attributes

From: J. Ian Johnson (ianj at ccs.neu.edu)
Date: Fri May 31 11:25:22 EDT 2013

You could also make a compile-time macro that will introduce several identifier macros to your syntax-class pattern right-hand-sides like 

(define-lazy-syntax-class blah
  (pattern pat #:lazy-wrap [complicated (e.v ...)] rhs ...) ...)

=>
;; generate ((force-e.v ...) ...)
(define-syntax-class blah
  (pattern pat #:do [(define-syntax (force-e.v stx)
                      (syntax-case stx (e.v)
                        [e.v #`(complicated #,(force (attribute e.v))])) ...]
    rhs ...) ...)

This way you can make the attributes promises (at compile time) to later force when using them in rhs ...

Too much?
-Ian
----- Original Message -----
From: "Eric Dobson" <eric.n.dobson at gmail.com>
To: "Matthias Felleisen" <matthias at ccs.neu.edu>
Cc: users at racket-lang.org
Sent: Friday, May 31, 2013 11:00:13 AM GMT -05:00 US/Canada Eastern
Subject: Re: [racket] Lazy syntax class attributes

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
>
____________________
  Racket Users list:
  http://lists.racket-lang.org/users

Posted on the users mailing list.