[Top][All Lists]

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

Re: Self-evaluating function and closure

From: Mark H Weaver
Subject: Re: Self-evaluating function and closure
Date: Sat, 15 Jun 2019 20:36:34 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (gnu/linux)

Hi Vladimir,

Vladimir Zhbanov <address@hidden> writes:

> Greetings,
> I have tried almost a textbook example with Guile 2.2.4:
> scheme@(guile-user)> (define (function-generator)
>                        (let ((func #f))                         
>                          (lambda () (set! func (let a () a)) func)))
> scheme@(guile-user)> (define x (function-generator))
> scheme@(guile-user)> (define y (function-generator))
> scheme@(guile-user)> x
> $20 = #<procedure f9f9d0 at <unknown port>:562:25 ()>
> scheme@(guile-user)> y
> $21 = #<procedure bf2660 at <unknown port>:562:25 ()>
> scheme@(guile-user)> (x)
> $22 = #<procedure a ()>
> scheme@(guile-user)> (y)
> $23 = #<procedure a ()>
> scheme@(guile-user)> (eq? (x) (y))
> $24 = #t
> The result is unexpected for me, I expected a new self-evaluating
> procedure every time I run the function-generator procedure (and
> it works differently with Guile 2.0, IIUC, cannot check just now).

Why would you expect 'eq?' to return #false here?  Do you know of any
text in Guile's manual, or in any of the relevant Scheme standards, that
would lead you to expect this?

Since (let a () a) contains no free variable references, every procedure
returned by (let a () a) is operationally equivalent to every other
procedure returned by it.  Therefore, as I understand it, a conforming
Scheme implementation is permitted (but not required) to return the same
procedure object every time.

I just refreshed my memory of the requirements of the R5RS, R6RS, and
R7RS on 'eq?' when applied to procedures.  Conforming implementations
are required to return #true if the procedures have the same "location
tags", and are required to return #false if the procedures would behave
differently (return different value(s) or have different side effects)
for some arguments.

> AFAICS, Guile creates a toplevel procedure "a" while it should not do
> scheme@(guile-user)> a
> $25 = #<procedure 109aa90 at <unknown port>:422:25 ()>

If this were the case, it would certainly be a bug.  However, I cannot
reproduce it, and I strongly suspect that you had defined 'a' as a
toplevel variable earlier in your Guile session and forgot about it.


reply via email to

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