This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: local-eval


Daniel Lakeland wrote:
> 
> The only r5rs way to manipulate environments is the following:
> 
> (eval <expression> <environment>)
> 
> (scheme-report-environment <version>)
> (null-environment <version>)
> (interaction-environment) :optional
> 
> so perhaps you could do something like this:
> 
> (define new-environment
>  (lambda ()
>   (null-environment 5)))
> 
> (define make-eval-closure
>  (lambda ()
>   (lambda (ls)
>    (let ((local-environment (new-environment)))
>      (eval `(apply ,(car ls) ,(cdr ls)) local-environment)))))
> 
> (define my-eval-closure (make-eval-closure))
> 
> now you can do...
> 
> (my-eval-closure '(define somename 123))
> (my-eval-closure '(+ 1 somename ))
> 
> I don't have an r5rs scheme so I can't test it out... anyone want to
> give it a go?? the only issue is that you need to be able to "apply" the
> first thing...
> 
> someone try it out and see if you can make it do what you want.
> 
> ----Original Message Follows----
> 
> Alexander Asteroth writes:
>  >
>  > I wonder if there is a way of writing a local-eval - i.e. a procedure
>  > evaluating its argument in its own environmental frame - in a
>  > poratble way i.e. r5rs-scheme.
> 
> The SLIB provides something similar.
> --
> Klaus Schilling
> 
> ______________________________________________________
> Get Your Private, Free Email at http://www.hotmail.com

Using guile and slib I (require 'eval)d and tried

guile> (use-modules (ice-9 slib))
guile> (require 'eval)
guile> (define e (scheme-report-environment 5))
guile> (eval '(define a 7) e)
guile> (eval '(display a) e)

Backtrace:
0* [eval (display a) ((#<procedure acos #> #<primitive-procedure angle>
#<procedure asin #> ...) (acos angle asin ...) #<procedure
(expression)>)]
1  (if (null? environment) (eval-1 expression) ...)
   ...
2  [display ...

standard input:7:8: While evaluating arguments to display in expression
(display a):
standard input:7:8: Unbound variable: a
ABORT: (misc-error)
guile> 

Evaluation in fact takes place in the environment (null-environment
doesn't have display while r5rs enviroment has) which can be seen by

guile> (define e (scheme-report-environment 5))
guile> (eval '(display "bla\n") e)
bla
guile> (define e (null-environment))
guile> (eval '(display "bla\n") e)

Backtrace:
0* [eval (display "bla
") ((#f #f #f ...) (call-with-values dynamic-wind eval ...) #<procedure
(expression)>)]
1  (if (null? environment) (eval-1 expression) ...)
   ...
2  [#<procedure (call-with-values dynamic-wind eval ...)> #f #f ...]
3  (display "bla
")

standard input:41:8: In expression (display "bla
"):
standard input:41:8: Wrong type to apply: #f
ABORT: (misc-error)
guile>

So evalution is carried out in e but is not to be fed back into e. Wich
becomes obvious by looking at the slib-definition of eval. I don't know
if that was intended by r5rs definition of eval. If so it's a bug in
slib.

Alex