Discussion:
[Jscheme-user] definitions available at compile time?
jeffbz
2008-01-07 18:58:30 UTC
Permalink
Hi,

I'm getting an error that seems to indicate the compiler can't see my
toplevel function definitions during macroexpansion. I have a function foo,
and a macro M that uses it. I get an error compiling function bar, which
uses M, saying undefined variable "foo". How can I make my definitions
visible during compilation?
Example code that exhibits this issue:
(define (foo x) (+ 1 x))
(define-macro (M y) (foo y))
(define (bar z) (M 3))

output:
first pass of compiler
(compiling foo)
(compiling bar)
(Error while compiling SchemeException: ERROR: undefined variable "foo")
SchemeException: ERROR: undefined variable "foo"
at jsint.E.error(E.java:14)

thank you, and thank you for JScheme, which I am greatly enjoying!!

-Jeff
--
View this message in context: http://www.nabble.com/definitions-available-at-compile-time--tp14673314p14673314.html
Sent from the JScheme - User mailing list archive at Nabble.com.
Kyle R. Burton
2008-01-07 22:16:19 UTC
Permalink
Post by jeffbz
I'm getting an error that seems to indicate the compiler can't see my
toplevel function definitions during macroexpansion. I have a function foo,
and a macro M that uses it. I get an error compiling function bar, which
uses M, saying undefined variable "foo". How can I make my definitions
visible during compilation?
(define (foo x) (+ 1 x))
(define-macro (M y) (foo y))
(define (bar z) (M 3))
first pass of compiler
(compiling foo)
(compiling bar)
(Error while compiling SchemeException: ERROR: undefined variable "foo")
SchemeException: ERROR: undefined variable "foo"
at jsint.E.error(E.java:14)
thank you, and thank you for JScheme, which I am greatly enjoying!!
Do you want foo to be part of the expansoin? In other words, do you
want M to expand to the call to foo, or do you want it to be 'compiled
away'?

eg, do you want bar to be equivalent to:

(define (bar z) (foo 3))

Or do you want it to be

(define (bar z) 4)

?

If it is the former, then M should probably look like:

(define-macro (M y) `(foo ,y))

Either way, when I type in your expressions, I receive no error with
jscheme 7.2:

$ java -jar jscheme-7.2.jar
JScheme 7.2 (2/2/06 9:47 PM) http://jscheme.sourceforge.net
Post by jeffbz
(define (foo x) (+ 1 x))
$1 = (lambda foo (x)...)
Post by jeffbz
(foo 3)
$2 = 4
Post by jeffbz
(define-macro (M y) (foo y))
$3 = (macro M (y)...)
Post by jeffbz
(define (bar z) (M 3))
$4 = (lambda bar (z)...)
Post by jeffbz
(bar 1)
$5 = 4
Post by jeffbz
(bar 2)
$6 = 4
If you're after constant math (making bar be the integer 4), actually
regardless, it looks like if you put the macro definition in a
separate file and load it, it succeeds:

$ cat mac.scm
(define-macro (M y) (foo y))

***@lap0029 ~/projects/sandbox/jscheme
$ cat Foo.scm
(load "mac.scm")
(define (foo x) (+ 1 x))
(define (bar z) (M 3))

(define (main args)
(write "bar ") (write (bar 10)) (newline))

I'm not very familiar with the compiler...I hope this helps.

Regards,

Kyle Burton
jeffbz
2008-01-08 00:43:27 UTC
Permalink
Thanks for the reply.
The code is written as intended. And indeed, this problem is confined to the
compiler; in the REPL everything is fine. I'll try moving some definitions
to another file to see if (load ...) helps. In general this wouldn't help
too much though, since I'd like to be able to run an arbitrary portion of my
code during macroexpansion.

-Jeff
Post by Kyle R. Burton
Post by jeffbz
I'm getting an error that seems to indicate the compiler can't see my
toplevel function definitions during macroexpansion. I have a function foo,
and a macro M that uses it. I get an error compiling function bar, which
uses M, saying undefined variable "foo". How can I make my definitions
visible during compilation?
(define (foo x) (+ 1 x))
(define-macro (M y) (foo y))
(define (bar z) (M 3))
first pass of compiler
(compiling foo)
(compiling bar)
(Error while compiling SchemeException: ERROR: undefined variable "foo")
SchemeException: ERROR: undefined variable "foo"
at jsint.E.error(E.java:14)
thank you, and thank you for JScheme, which I am greatly enjoying!!
Do you want foo to be part of the expansoin? In other words, do you
want M to expand to the call to foo, or do you want it to be 'compiled
away'?
(define (bar z) (foo 3))
Or do you want it to be
(define (bar z) 4)
?
(define-macro (M y) `(foo ,y))
Either way, when I type in your expressions, I receive no error with
$ java -jar jscheme-7.2.jar
JScheme 7.2 (2/2/06 9:47 PM) http://jscheme.sourceforge.net
Post by jeffbz
(define (foo x) (+ 1 x))
$1 = (lambda foo (x)...)
Post by jeffbz
(foo 3)
$2 = 4
Post by jeffbz
(define-macro (M y) (foo y))
$3 = (macro M (y)...)
Post by jeffbz
(define (bar z) (M 3))
$4 = (lambda bar (z)...)
Post by jeffbz
(bar 1)
$5 = 4
Post by jeffbz
(bar 2)
$6 = 4
If you're after constant math (making bar be the integer 4), actually
regardless, it looks like if you put the macro definition in a
$ cat mac.scm
(define-macro (M y) (foo y))
$ cat Foo.scm
(load "mac.scm")
(define (foo x) (+ 1 x))
(define (bar z) (M 3))
(define (main args)
(write "bar ") (write (bar 10)) (newline))
I'm not very familiar with the compiler...I hope this helps.
Regards,
Kyle Burton
--
View this message in context: http://www.nabble.com/definitions-available-at-compile-time--tp14673314p14679936.html
Sent from the JScheme - User mailing list archive at Nabble.com.
Loading...