guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Define works different when Compile and Eval


From: Andrew Tropin
Subject: Re: Define works different when Compile and Eval
Date: Fri, 21 Jun 2024 16:33:42 +0400

On 2024-06-19 09:53, Attila Lendvai wrote:

>> (define (test-eval teval)
>> (teval '(define a 1))
>
> if you want this^ to be actually defined while compilation is
> happening (i.e. in the compilation stage; see staged computing), then
> you need to use an (eval-when (expand) ...) wrapper around it.

Thank you for the reply!  Not sure what you mean.  The following code
defines `a`:

--8<---------------cut here---------------start------------->8---
(define (test-eval teval)
  (teval '(define a 1))
  (format #t "a value with ~a is ~a\n" teval (teval 'a)))

(test-eval peval)
(test-eval primitive-eval)

(exit 0)
;; a value with #<procedure peval (e)> is 1
;; a value with #<procedure primitive-eval (exp)> is 1
--8<---------------cut here---------------end--------------->8---

The problem and the difference is in how `(define a a)` is evaluated by
compile.

>
> https://www.gnu.org/software/guile/manual/guile.html#Eval-When
>
> compile does not evaluate (aka load) the definitions, it only compiles them.

compile returns value by default, which is probably means that
everything is loaded as well.

> alternatively, you yourself can explicitly call the lambda returned by 
> compile.

--8<---------------cut here---------------start------------->8---
(use-modules (system base compile)
             (system vm loader))

(define (peval e)
  ((load-thunk-from-memory
    (compile
     e
     #:to 'bytecode
     #:env (resolve-module
            '(2024-06-18-define-bug))))))
--8<---------------cut here---------------end--------------->8---

It works the same way as the original code :(

>
> --
> • attila lendvai
> • PGP: 963F 5D5F 45C7 DFCD 0A39
> --
> “A politician is someone asking you to trust them more with power than they 
> trust you with freedom...”
>       — Kelly Diamond
>
>
>

-- 
Best regards,
Andrew Tropin

Attachment: signature.asc
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]