Using environment-bound? in macro definitions

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Using environment-bound? in macro definitions

Duncan Mak
Hello,

Here's a stripped down version of two define-form macros that I'm working on:

(require 'syntax-utils)
(define-for-syntax known (list))

(define-syntax define-foo
  (lambda (stx)
    (syntax-case stx ()
      ((define-foo a)
       (begin
         (set! known (cons #'a known))
         #'(define a (list)))))))

(define-syntax define-bar
  (lambda (stx)
    (syntax-case stx ()
      ((define-bar a n)
       (if (environment-bound? (apply environment known) #'a)
           #'(set! a (cons n a))
           #'(begin
               (define-foo a)
               (set! a (cons n a))))))))

;; (1) should expand to (define x (list)) and store x in known
(define-foo x)

;; (2) should expand to (set! x (cons 1 x)) since x was previously
;; defined (i.e. can be found in known)
(define-bar x 1)

;; (3) should expand to (begin (define-foo y) (set! y (cons 2 y)))
;; because y is not previous defined
(define-bar y 2)

When I try to run it, I get

test.scm:29:1: evaluating syntax transformer 'define-bar' threw
gnu.text.SyntaxException: <unknown>: unknown library (x)

I must be doing something wrong, is this the right approach?

Thanks!

--
Duncan.
Reply | Threaded
Open this post in threaded view
|

Re: Using environment-bound? in macro definitions

Per Bothner


On 01/02/2017 03:29 PM, Duncan Mak wrote:

> (require 'syntax-utils)
> (define-for-syntax known (list))
>
> (define-syntax define-foo
>   (lambda (stx)
>     (syntax-case stx ()
>       ((define-foo a)
>        (begin
>          (set! known (cons #'a known))
>          #'(define a (list)))))))
>
> (define-syntax define-bar
>   (lambda (stx)
>     (syntax-case stx ()
>       ((define-bar a n)
>        (if (environment-bound? (apply environment known) #'a)
>            #'(set! a (cons n a))
>            #'(begin
>                (define-foo a)
>                (set! a (cons n a))))))))
>
> ;; (1) should expand to (define x (list)) and store x in known
> (define-foo x)

In other words, at this point:

(equal? known '(x))

test.scm:29:1: evaluating syntax transformer 'define-bar' threw
gnu.text.SyntaxException: <unknown>: unknown library (x)

That means you're evaluating (environment 'x).
The 'x is interpreted as an "environment specifier" - i.e. a library name,
and there is no such library (or class).

I think you misunderstand the 'environment' procedure.  Check the Kawa and R7RS documentation.

What are you trying to do? Trying to dynamically build environment maps
is possible, but there isn't a Scheme API for it, and there are tricky interactions
between the dynamic top-level and the module-level lexical scope.

--
        --Per Bothner
[hidden email]   http://per.bothner.com/
Reply | Threaded
Open this post in threaded view
|

Re: Using environment-bound? in macro definitions

Duncan Mak
Hello Per,

Ah! You're totally right. I thought I'll need to use the environment
instance but if I just keep a data structure (known) at syntax time, I
won't need to deal with environments at all, because 'known' will be
the environment (for my purposes).

This is the working code:

(define-for-syntax known (list))

(define-syntax define-foo
  (lambda (stx)
    (syntax-case stx ()
      ((define-foo a)
       (begin
         (set! known (cons (list #'a 0) known))
         #'(define a (list)))))))

(define-syntax define-bar
  (lambda (stx)
    (syntax-case stx ()
      ((define-bar a n)
       (identifier? #'a)
       (if (assoc #'a known)
           #'(set! a (cons n a))
           #'(begin
               (define-foo a)
               (set! a (cons (list n 0) a))))))))

(define-foo x)
(display x) (newline) ;; prints ()
(define-bar x 1)
(display x) (newline) ;; prints (1)


Thanks!


Duncan.
Reply | Threaded
Open this post in threaded view
|

Re: Using environment-bound? in macro definitions

Duncan Mak
Hello Per,

I just noticed that if I put my macros inside a R7RS define-library,
then it get these warnings:

(define-library (foo)
  (export define-foo define-bar)
  (import (kawa base) (kawa lib syntax))
  (begin
    (define-for-syntax known (list))
    (define-syntax define-foo
      (lambda (stx)
        (syntax-case stx ()
          ((define-foo a)
           (begin
             (set! known (cons (list #'a 0) known))
             #'(define a (list)))))))

    (define-syntax define-bar
      (lambda (stx)
        (syntax-case stx ()
          ((define-bar a n)
           (identifier? #'a)
           (if (assoc #'a known)
               #'(set! a (cons n a))
               #'(begin
                   (define-foo a)
                   (set! a (cons (list n 0) a))))))))))


(import (foo) (srfi 64))

(test-begin "t")
(define-foo x)
(test-equal '() x)
(define-bar x 1)
(test-equal '(1) x)
(test-end)

$ kawa test.scm

test.scm:13:14: warning - no declaration seen for known
test.scm:13:45: warning - no declaration seen for known
test.scm:21:27: warning - no declaration seen for known

%%%% Starting test t  (Writing full log to "t.log")

# of expected passes      2

It'd be nice if there's a way to not have warnings show up.

Thanks again!


Duncan.
Reply | Threaded
Open this post in threaded view
|

Re: Using environment-bound? in macro definitions

Per Bothner


On 01/03/2017 06:40 PM, Duncan Mak wrote:

> $ kawa test.scm
>
> test.scm:13:14: warning - no declaration seen for known
> test.scm:13:45: warning - no declaration seen for known
> test.scm:21:27: warning - no declaration seen for known
>
> %%%% Starting test t  (Writing full log to "t.log")
>
> # of expected passes      2
>
> It'd be nice if there's a way to not have warnings show up.

You can try compiling compiling with --no-warn-undefined-variable
or add:
(module-compile-options warn-undefined-variable: #f)

with-compile-options provides more localized control.

Perhaps this could be fixed in a cleaner way,
but I can't think of anything off-hand.
--
        --Per Bothner
[hidden email]   http://per.bothner.com/