bugKawa - Bugs: bug #37051, define-syntax does not work when...

 
 

bug #37051: define-syntax does not work when included

Submitter:  None
Submitted:  Wed 08 Aug 2012 12:34:36 AM UTC
   
 
Category:  None Severity:  3 - Normal
Item Group:  Unexpected result Status:  Invalid
Privacy:  Public Assigned to:  None
Open/Closed:  Closed
* Mandatory Fields

Add a New Comment Rich Markup
   

Fri 17 Aug 2012 02:06:12 AM UTC, comment #4: 

Thanks for the test-case.  I believe the compiler is correct.
The call to bar should not match the define, because of macros hygiene:

http://en.wikipedia.org/wiki/Hygienic_macro
http://www.randomhacks.net/articles/2002/09/13/hygienic-macros

The reason it "works" when you remove the begin is because then things happen at the "top-level" which has special rules.

One solution is to explicitly pass bar as a macro argument:

(begin
(define-syntax foo
  (syntax-rules ()
    ((_ e bname) (define (bname) (display e)(newline)))))
(foo 123 bar)
(bar)
)

Another is to use define-syntax-case, and a "literal" 'bar is the expansion:

(begin
(define-syntax-case foo ()
  ((_ e) #`(define (#,'bar) (display e)(newline))))
(foo 123)
(bar)
)

Even better would be to use datum->syntax:

(begin
(define-syntax-case foo ()
  ((foo e) #`(define (#,(datum->syntax #'foo 'bar)) (display e)(newline))))
(foo 123)
(bar)
)

The datum->syntax means to take the symbol 'bar and convert it to a syntax object in the same lexical scope as foo.  (For this to work you need to use (foo e) in the pattern.)



Per Bothner <bothner>
Group administrator
Fri 17 Aug 2012 01:30:15 AM UTC, comment #3: 

I have made the test script far simpler.  Please see the attached foo2.scm.

In this case, too, removing the enclosing (begin ...) makes a good result.




(file #26375)

Anonymous
Fri 17 Aug 2012 12:12:43 AM UTC, comment #2: 

I have made a simpler script to show this bug.  Attached foo.scm shows this result:

$ java -cp kawa-1.12.jar kawa.repl --script foo.scm
foo.scm:16:1: warning - no declaration seen for baz
foo.scm:16:1: unbound location baz
        at gnu.mapping.Location.get(Location.java:67)
        at atInteractiveLevel$1.run(foo.scm:16)
        at gnu.expr.ModuleExp.evalModule2(ModuleExp.java:299)
        at gnu.expr.ModuleExp.evalModule(ModuleExp.java:200)
        at kawa.Shell.run(Shell.java:279)
        at kawa.Shell.runFile(Shell.java:492)
        at kawa.Shell.runFileOrClass(Shell.java:426)
        at kawa.repl.processArgs(repl.java:274)
        at kawa.repl.main(repl.java:870)

Removing the enclosing (begin ...) makes the script show a good result.



(file #26374)

Anonymous
Wed 15 Aug 2012 02:20:52 AM UTC, comment #1: 

(include ...) is not essential to this problem.

Just enclosing the contents of define-syntax.scm with (begin ...) makes the same result.

Anonymous
Wed 08 Aug 2012 12:34:36 AM UTC, original submission:  

I have two files define-syntax.scm and define-syntax-main.scm. The latter just includes the former.  The former works well but the latter does not.

$ cat define-syntax.scm
(define-syntax define-rec
   (syntax-rules ()
      ((_ rec e ...) (define-rec0 rec (e ...)))))

(define-syntax define-rec0
  (lambda (x)
    (syntax-case x ()
      ((k rec flds)
        (datum->syntax #'k
          (let* ((recnm (syntax->datum #'rec))
                 (recnmstr (symbol->string recnm))
                 (fldlist (syntax->datum #'flds)))
            (append
              `(define-record-type
                 ,recnm
                 ,(cons (string->symbol (string-append "make-" recnmstr))
                        fldlist)
                 ,(string->symbol (string-append recnmstr "?")))
              (let loop ((src fldlist) (out '()))
                (cond
                  ((null? src) (reverse out))
                  (#t
                    (let* ((fldnm (car src))
                           (fldnmstr (symbol->string fldnm))
                           (getter
                             (string->symbol
                               (string-append
                                  recnmstr "." fldnmstr "-ref")))
                           (setter
                             (string->symbol
                               (string-append
                                  recnmstr "." fldnmstr "-set!"))))
                         (loop (cdr src) (cons (list fldnm getter setter) out)))))))))))))


(define-rec rec1 fld1 fld2 fld3)
(define a (make-rec1 0 0 0))
(display a)(newline)
(rec1.fld2-set! a 999)
(display (rec1.fld2-ref a))(newline)
$ cat define-syntax-main.scm
(include "define-syntax.scm")
$ java kawa.repl --script define-syntax.scm
atInteractiveLevel$3$rec1@11fd245
999
$ java kawa.repl --script define-syntax-main.scm
define-syntax.scm:37:11: warning - no declaration seen for make-rec1
define-syntax.scm:39:1: warning - no declaration seen for rec1.fld2-set!
define-syntax.scm:40:10: warning - no declaration seen for rec1.fld2-ref
define-syntax.scm:37:11: unbound location make-rec1
        at gnu.mapping.SharedLocation.get(SharedLocation.java:22)
        at gnu.mapping.ThreadLocation.get(ThreadLocation.java:105)
        at atInteractiveLevel$1.run(define-syntax-main.scm:10002)
        at gnu.expr.ModuleExp.evalModule2(ModuleExp.java:309)
        at gnu.expr.ModuleExp.evalModule(ModuleExp.java:210)
        at kawa.Shell.run(Shell.java:279)
        at kawa.Shell.runFile(Shell.java:492)
        at kawa.Shell.runFileOrClass(Shell.java:426)
        at kawa.repl.processArgs(repl.java:274)
        at kawa.repl.main(repl.java:870)


Anonymous

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #26375:  foo2.scm added by None (117B - application/octet-stream)
file #26374:  foo.scm added by None (281B - application/octet-stream)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by bothner (Posted a comment)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follow 4 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2012-09-11 bothner Open/ClosedOpen Closed
    2012-08-17 bothner StatusNone Invalid
    2012-08-17 None Attached File- Added foo2.scm, #26375
    2012-08-17 None Attached File- Added foo.scm, #26374

    Back to the top

    Powered by Savane 3.13-aa77.
    Corresponding source code