[plt-scheme] attempt to discern applications from syntactic formsfails

From: Jos Koot (jos.koot at telefonica.net)
Date: Sat Jan 17 10:57:35 EST 2009

Thanks Matthew, with your help I concocted the following:

 ; (macro-id? id-stx) (identifier? . -> . boolean?)
 ; For use in a transformer only.
 ; Returns #t if the id-stx has a syntactic binding in the syntax local context of the run-time environment.
 ; If not, returns #f including when id-stx is not bound at all.

 (define (macro-id? id)
  (let/ec ec
   (call-with-exception-handler
    (lambda (exn) (ec #t))
    (lambda ()
     (syntax-case*
      (local-expand #`(#,id) (syntax-local-context) '())
      (#%plain-app) free-transformer-identifier=?
      ((#%plain-app . z) #f)
      (_ #t))))))

Jos


----- Original Message ----- 
From: "Matthew Flatt" <mflatt at cs.utah.edu>
To: "Jos Koot" <jos.koot at telefonica.net>
Cc: <plt-scheme at list.cs.brown.edu>
Sent: Thursday, January 15, 2009 6:57 PM
Subject: Re: [plt-scheme] attempt to discern applications from syntactic formsfails


> At Thu, 15 Jan 2009 18:40:36 +0100, "Jos Koot" wrote:
>> My question is at the <===== in the following code:
>> 
>> (define-syntax (x stx)
>>  (syntax-case stx ()
>>   ((_ x)
>>    (let ((y (expand-once #'x)))
>>     (printf "~s~n" (syntax->datum y))
>>     (syntax-case y (#%app)
>>      ((#%app . z) #''app)
>>      (_ #''no-app))))))
>> 
>> [...]
>> (x (list 1 2 3))
>> ; printed (#%app list 1 2 3)
>> ; value: no-app, why??? <===== I expected app
> 
> The `expand-once' is working at phase 1, while `syntax-case' is
> comparing identifiers at phase 0.
> 
> Furthermore, the `#%app' of `scheme/base' is being expanded to
> `#%plain-app'. Confusingly, in the scope of the `#%app' definition,
> `#%plain-app' is called `#%app', so the expansion prints as `#%app'
> 
> So, you would have gotten the expected results with
> 
>    (syntax-case* y (#%plain-app) free-transformer-identifier=?
>     ((#%plain-app . z) #''app)
>     (_ #''no-app))
> 
> 
> I doubt that this is what you really intended, though. I think it's
> more likely that you wanted to use `local-expand' (which will expand
> the given expression at phase 0) and use `#%app' as one of the stop
> forms, in which case the `syntax-case' match would work as you expect.
> 
> Actually, depending on the context, it's probably better to use
> `#%plain-app' as a stop form, in which case you'll match on
> `#%plain-app'. That works with macros defined in a language other than
> `scheme/base', since `#%plain-app' is the primitive application form.
> 
> 
> Matthew
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090117/59e58d24/attachment.html>

Posted on the users mailing list.