[racket] is there any way to check whether two syntax objects have the same ... ?

From: Alexander D. Knauth (alexander at knauth.org)
Date: Sun Aug 17 15:58:08 EDT 2014

On Aug 17, 2014, at 2:29 PM, Carl Eastlund <carl.eastlund at gmail.com> wrote:

> Comparing lexical context is a very involved process.  Remember that every syntax object potentially carries context for an unbounded number of symbol names at an unbounded number of phases, since context information can be transferred freely.  Comparing all of them for equivalence would be quite expensive.  You probably want to either decide what subset of that context is relevant to you, or reformulate your solution in a way that does not involve comparing syntax at all.  It's a problematic technique to employ, to say the very least.
> 

Well, if what I want to do is a sort of syntax-object memoizing, where I do something like this:

#lang racket/base (require (for-syntax racket/base))

(module+ test
  (require rackunit))

(begin-for-syntax
  (define (syntax=? s1 s2)
    ....)
  (define (make-stx-dict)
    (stx-dict '()))
  (struct stx-dict ([alist #:mutable]) #:transparent)
  (define (stx-dict-ref! stx-dict key to-set)
    (define alist (stx-dict-alist stx-dict))
    (define maybe-pair (assoc key alist syntax=?))
    (if maybe-pair
        (cdr maybe-pair)
        (let ([new-val (to-set)])
          (set-stx-dict-alist! stx-dict (cons (cons key new-val) alist))
          new-val)))
  )

(define-syntax m
  (let ([stx-dict (make-stx-dict)])
    (lambda (stx)
      (define sym (stx-dict-ref! stx-dict (syntax-local-introduce stx) gensym))
      (with-syntax ([sym (datum->syntax stx sym)])
        (syntax 'sym)))))

(module+ test
  (define-syntax chk
    (lambda (stx)
      (with-syntax ([expr #'(m)])
        #`(begin
            (check-not-equal? (m) (m))
            (check-equal? expr expr)
            (check-equal? #,(syntax-local-introduce #'expr) #,(syntax-local-introduce #'expr))
            (check-not-equal? expr #,(syntax-local-introduce #'expr))))))
  (chk)
  )

> On Aug 17, 2014 12:02 PM, "Alexander D. Knauth" <alexander at knauth.org> wrote:
> But those only work on identifiers, right?
> 
> But would something like this work?
> 
> (define (lexical-context=? s1 s2)
>   (bound-identifier=? (datum->syntax s1 ‘x) (datum->syntax s2 ‘x)))
> 
> Or would that miss something?  
> 
> 
> 
> On Aug 17, 2014, at 1:31 PM, Roman Klochkov <kalimehtar at mail.ru> wrote:
> 
>> Maybe something of
>> 
>> http://docs.racket-lang.org/reference/stxcmp.html
>> 
>> ?
>> 
>> 
>> Sat, 16 Aug 2014 23:32:10 -0400 от "Alexander D. Knauth" <alexander at knauth.org>:
>> Is there any way to check whether two syntax objects have the same lexical information, syntax-e (recursively), source location, properties, and whatever else they have, even if they aren’t eq?
>> 
>> For the syntax-e and source-location this is straightforward, but if there’s no accessor for the lexical information then how do I check if the lexical information is the same if the syntax objects aren’t eq?
>> 
>> And for the properties, can you only check the properties with keys that are interned symbols? 
>> 
>> 
>> 
>> ____________________
>>   Racket Users list:
>>   http://lists.racket-lang.org/users
>> 
>> 
>> -- 
>> Roman Klochkov
> 
> 
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140817/efb743c9/attachment-0001.html>

Posted on the users mailing list.