* primitive eval with module => Unbound variable?
@ 2017-05-16 21:59 Jan Nieuwenhuizen
2017-05-17 17:58 ` Vladimir Zhbanov
2017-05-17 19:44 ` Andy Wingo
0 siblings, 2 replies; 7+ messages in thread
From: Jan Nieuwenhuizen @ 2017-05-16 21:59 UTC (permalink / raw)
To: guile-user
Hi!
My Mescc C compiler produces from C
int main () {return 0;}
a list of functions (lambdas), that are called with text-address,
data-address, globals, functions to produce lists of x86 instructions:
#<procedure 2a61a80 at ice-9/eval.scm:345:13 (a b c d e)>
=> (85 137 229 131 236 64)
#<procedure 2a859c0 at ice-9/eval.scm:345:13 (a b c d e)>
=> (184 0 0 0 0)
#<procedure 2aada20 at ice-9/eval.scm:345:13 (a b c d e)>
=> (80)
#<procedure 2ace560 at ice-9/eval.scm:345:13 (a b c d e)>
...
I hoped to instead produce intermediate results in a sexp that can be
written to file (like .o object files). So instead of evaluating
the lambdas, I tried quoting them to now produce
==>
(lambda (f g ta t d) (list 85 137 229 131 236 64))
(lambda (f g ta t d) (list 184 0 0 0 0))
(lambda (f g ta t d) (list 80))
(lambda (f g ta t d) (i386:push-global-address (+ (data-offset "s:scaffold/mesmes" g) d)))
...
and primitive-eval and run them later.... However, when I do this,
i386:push-global-address is an Unbound variable. Here's a small test
that demonstrates my problem
--8<---------------cut here---------------start------------->8---
;; this works without define-module:
;; guile -e 'main' -s foo.scm
;; uncomment define-module below and run:
;; guile -e '(@ (foo) main)' -s foo.scm
;; => ERROR: Unbound variable: bar
;; (define-module (foo) #:export (main))
(define (bar) (display "bar!!!\n"))
(define foo '(lambda (x) (bar)))
(define (main . rest)
(let ()
(format (current-error-port) "eval: foo=~s\n" foo)
(let ((x (primitive-eval foo)))
(format (current-error-port) " =>~s\n" x)
(x 0))))
--8<---------------cut here---------------end--------------->8---
This code works when not put in a module; make it a module and I get
=> ERROR: Unbound variable: bar
What should I be doing differently?
Greetings,
janneke
--
Jan Nieuwenhuizen <janneke@gnu.org> | GNU LilyPond http://lilypond.org
Freelance IT http://JoyofSource.com | Avatar® http://AvatarAcademy.com
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: primitive eval with module => Unbound variable?
2017-05-16 21:59 primitive eval with module => Unbound variable? Jan Nieuwenhuizen
@ 2017-05-17 17:58 ` Vladimir Zhbanov
2017-05-17 19:44 ` Andy Wingo
1 sibling, 0 replies; 7+ messages in thread
From: Vladimir Zhbanov @ 2017-05-17 17:58 UTC (permalink / raw)
To: guile-user
On Tue, May 16, 2017 at 11:59:01PM +0200, Jan Nieuwenhuizen wrote:
> Hi!
>
> My Mescc C compiler produces from C
>
> int main () {return 0;}
>
> a list of functions (lambdas), that are called with text-address,
> data-address, globals, functions to produce lists of x86 instructions:
>
> #<procedure 2a61a80 at ice-9/eval.scm:345:13 (a b c d e)>
> => (85 137 229 131 236 64)
> #<procedure 2a859c0 at ice-9/eval.scm:345:13 (a b c d e)>
> => (184 0 0 0 0)
> #<procedure 2aada20 at ice-9/eval.scm:345:13 (a b c d e)>
> => (80)
> #<procedure 2ace560 at ice-9/eval.scm:345:13 (a b c d e)>
> ...
>
> I hoped to instead produce intermediate results in a sexp that can be
> written to file (like .o object files). So instead of evaluating
> the lambdas, I tried quoting them to now produce
>
> ==>
>
> (lambda (f g ta t d) (list 85 137 229 131 236 64))
> (lambda (f g ta t d) (list 184 0 0 0 0))
> (lambda (f g ta t d) (list 80))
> (lambda (f g ta t d) (i386:push-global-address (+ (data-offset "s:scaffold/mesmes" g) d)))
> ...
>
> and primitive-eval and run them later.... However, when I do this,
> i386:push-global-address is an Unbound variable. Here's a small test
> that demonstrates my problem
>
> --8<---------------cut here---------------start------------->8---
> ;; this works without define-module:
> ;; guile -e 'main' -s foo.scm
>
> ;; uncomment define-module below and run:
> ;; guile -e '(@ (foo) main)' -s foo.scm
> ;; => ERROR: Unbound variable: bar
> ;; (define-module (foo) #:export (main))
>
> (define (bar) (display "bar!!!\n"))
>
> (define foo '(lambda (x) (bar)))
>
> (define (main . rest)
> (let ()
> (format (current-error-port) "eval: foo=~s\n" foo)
> (let ((x (primitive-eval foo)))
> (format (current-error-port) " =>~s\n" x)
> (x 0))))
> --8<---------------cut here---------------end--------------->8---
>
> This code works when not put in a module; make it a module and I get
>
> => ERROR: Unbound variable: bar
>
> What should I be doing differently?
>
I've found two working variants:
First:
--------------------------------8<--------------------------------
(define-module (foo) #:export (main)) ; this is as is
...
(define foo '(lambda (x) ((@@ (foo) bar))))
...
-------------------------------->8--------------------------------
Second:
--------------------------------8<--------------------------------
(define-module (foo) #:export (main bar)) ; export bar
...
(define foo '(lambda (x) ((@ (foo) bar))))
...
-------------------------------->8--------------------------------
It seems bar is not exported when guile tries to eval (compile?)
the code. Should it compile it here?
--
Vladimir
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: primitive eval with module => Unbound variable?
2017-05-16 21:59 primitive eval with module => Unbound variable? Jan Nieuwenhuizen
2017-05-17 17:58 ` Vladimir Zhbanov
@ 2017-05-17 19:44 ` Andy Wingo
2017-05-18 4:54 ` Jan Nieuwenhuizen
1 sibling, 1 reply; 7+ messages in thread
From: Andy Wingo @ 2017-05-17 19:44 UTC (permalink / raw)
To: Jan Nieuwenhuizen; +Cc: guile-user
On Tue 16 May 2017 23:59, Jan Nieuwenhuizen <janneke@gnu.org> writes:
> This code works when not put in a module; make it a module and I get
>
> => ERROR: Unbound variable: bar
I assume you mean that this doesn't work:
(define-module (foo) #:export (baz))
(define bar 42)
(define (baz) (primitive-eval 'bar))
Then from another module you do (use-modules (foo)) and try to (baz).
The thing is that primitive-eval evaluates its expression with respect
to the current module. The current module when you do (use-modules
(foo)) isn't (foo) -- it's the importing module.
This is the right thing:
(define-module (foo) #:export (baz))
(define eval-module (current-module)
(define bar 42)
(define (baz) (eval 'bar eval-module))
Cheers,
Andy
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: primitive eval with module => Unbound variable?
2017-05-17 19:44 ` Andy Wingo
@ 2017-05-18 4:54 ` Jan Nieuwenhuizen
2017-05-22 19:45 ` Andy Wingo
2017-05-22 19:48 ` Andy Wingo
0 siblings, 2 replies; 7+ messages in thread
From: Jan Nieuwenhuizen @ 2017-05-18 4:54 UTC (permalink / raw)
To: Andy Wingo; +Cc: guile-user
Andy Wingo writes:
> On Tue 16 May 2017 23:59, Jan Nieuwenhuizen <janneke@gnu.org> writes:
>
>> This code works when not put in a module; make it a module and I get
>>
>> => ERROR: Unbound variable: bar
>
> I assume you mean that this doesn't work:
>
> (define-module (foo) #:export (baz))
> (define bar 42)
> (define (baz) (primitive-eval 'bar))
>
>
> Then from another module you do (use-modules (foo)) and try to (baz).
Yes.
> The thing is that primitive-eval evaluates its expression with respect
> to the current module. The current module when you do (use-modules
> (foo)) isn't (foo) -- it's the importing module.
Okay...
> This is the right thing:
>
> (define-module (foo) #:export (baz))
> (define eval-module (current-module))
> (define bar 42)
> (define (baz) (eval 'bar eval-module))
Thanks!...this works. I'm very happy with this, it means that I can
create an sexp-object format, yay!
Earlier I tried several things eval, but did not see this `define
eval-module' coming. The above works, this does not work
(define-module (foo) #:export (baz))
(define bar 42)
(define (baz) (eval 'bar (current-module)))
and neither does this work
(define-module (foo) #:export (baz))
(define bar 42)
(define (baz . args) (let ((eval-module (current-module))) (eval 'bar eval-module)))
So (current-module) is not the defining model (foo) in these cases.
Greetings,
janneke
--
Jan Nieuwenhuizen <janneke@gnu.org> | GNU LilyPond http://lilypond.org
Freelance IT http://JoyofSource.com | Avatar® http://AvatarAcademy.com
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: primitive eval with module => Unbound variable?
2017-05-18 4:54 ` Jan Nieuwenhuizen
@ 2017-05-22 19:45 ` Andy Wingo
2017-05-22 20:19 ` Jan Nieuwenhuizen
2017-05-22 19:48 ` Andy Wingo
1 sibling, 1 reply; 7+ messages in thread
From: Andy Wingo @ 2017-05-22 19:45 UTC (permalink / raw)
To: Jan Nieuwenhuizen; +Cc: guile-user
On Thu 18 May 2017 06:54, Jan Nieuwenhuizen <janneke@gnu.org> writes:
>> This is the right thing:
>>
>> (define-module (foo) #:export (baz))
>> (define eval-module (current-module))
>> (define bar 42)
>> (define (baz) (eval 'bar eval-module))
>
> Thanks!...this works. I'm very happy with this, it means that I can
> create an sexp-object format, yay!
>
> Earlier I tried several things eval, but did not see this `define
> eval-module' coming. The above works, this does not work
>
> (define-module (foo) #:export (baz))
> (define bar 42)
> (define (baz) (eval 'bar (current-module)))
current-module is dynamically scoped. Consider:
(define-module (qux)
#:use-module (foo))
(baz)
In this case the current module for the baz invocation is (qux).
Andy
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: primitive eval with module => Unbound variable?
2017-05-18 4:54 ` Jan Nieuwenhuizen
2017-05-22 19:45 ` Andy Wingo
@ 2017-05-22 19:48 ` Andy Wingo
1 sibling, 0 replies; 7+ messages in thread
From: Andy Wingo @ 2017-05-22 19:48 UTC (permalink / raw)
To: Jan Nieuwenhuizen; +Cc: guile-user
More concretely... you think in your examples that because you see
(current-module) in the (foo) module that (current-module) will be
(foo). That is a lexical assumption. It's usually what you want. But
current-module isn't lexically scoped -- it's dynamically scoped. Its
value comes from the continuation that invokes the call, not the
environment in which the expression appears.
Andy
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: primitive eval with module => Unbound variable?
2017-05-22 19:45 ` Andy Wingo
@ 2017-05-22 20:19 ` Jan Nieuwenhuizen
0 siblings, 0 replies; 7+ messages in thread
From: Jan Nieuwenhuizen @ 2017-05-22 20:19 UTC (permalink / raw)
To: Andy Wingo; +Cc: guile-user
Andy Wingo writes:
> current-module is dynamically scoped. Consider:
>
> (define-module (qux)
> #:use-module (foo))
>
> (baz)
>
> In this case the current module for the baz invocation is (qux).
Ah, yes! Thanks, now that helps making sense of it. Why didn't I
consider this option :-)
Greetings,
janneke
--
Jan Nieuwenhuizen <janneke@gnu.org> | GNU LilyPond http://lilypond.org
Freelance IT http://JoyofSource.com | Avatar® http://AvatarAcademy.com
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2017-05-22 20:19 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-16 21:59 primitive eval with module => Unbound variable? Jan Nieuwenhuizen
2017-05-17 17:58 ` Vladimir Zhbanov
2017-05-17 19:44 ` Andy Wingo
2017-05-18 4:54 ` Jan Nieuwenhuizen
2017-05-22 19:45 ` Andy Wingo
2017-05-22 20:19 ` Jan Nieuwenhuizen
2017-05-22 19:48 ` Andy Wingo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).