unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* Inconsistency with expressions between definitions
@ 2023-09-24  7:09 Dr. Arne Babenhauserheide
  2023-09-24 11:58 ` Linus Björnstam
  0 siblings, 1 reply; 5+ messages in thread
From: Dr. Arne Babenhauserheide @ 2023-09-24  7:09 UTC (permalink / raw)
  To: guile-devel

[-- Attachment #1: Type: text/plain, Size: 660 bytes --]

Hi,

while writing a comment to SRFI-245 I think I found an inconsistency in
the Implementation in Guile.

This works:

(define (using-later-variable)
  (define x y)
  (define y #t)
  x)
(using-later-variable)
;; => #t

This still works:

(define (using-later-variable)
  (define x y)
  (newline)
  (define y #t)
  x)
(using-later-variable)
;; => (newline output)
;; => #t

This fails:

(define (using-later-variable)
  (define x y)
  (display x)
  (newline)
  (define y #t)
  x)
(using-later-variable)
;; => #<unspecified>

Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein,
ohne es zu merken.
draketo.de

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 1125 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Inconsistency with expressions between definitions
  2023-09-24  7:09 Inconsistency with expressions between definitions Dr. Arne Babenhauserheide
@ 2023-09-24 11:58 ` Linus Björnstam
  2023-09-24 16:02   ` Dr. Arne Babenhauserheide
  0 siblings, 1 reply; 5+ messages in thread
From: Linus Björnstam @ 2023-09-24 11:58 UTC (permalink / raw)
  To: guile-devel

Hey!

When you are not referencing x before defining y everything works as you 
want. There is no, so to say, temporal dependency on how the things are 
bound. When you introduce (display x) before actually defining y you 
force letrec* to bind x to the unspecified value, because display has 
side-effects and you don't move around side-effecting code.

If you do (display "heippa!") instead it works as you want.

I believe racket (which does the same optimization) has the same behaviour.

-- Linus Björnstam

Den 2023-09-24 kl. 09:09, skrev Dr. Arne Babenhauserheide:
> Hi,
> 
> while writing a comment to SRFI-245 I think I found an inconsistency in
> the Implementation in Guile.
> 
> This works:
> 
> (define (using-later-variable)
>    (define x y)
>    (define y #t)
>    x)
> (using-later-variable)
> ;; => #t
> 
> This still works:
> 
> (define (using-later-variable)
>    (define x y)
>    (newline)
>    (define y #t)
>    x)
> (using-later-variable)
> ;; => (newline output)
> ;; => #t
> 
> This fails:
> 
> (define (using-later-variable)
>    (define x y)
>    (display x)
>    (newline)
>    (define y #t)
>    x)
> (using-later-variable)
> ;; => #<unspecified>
> 
> Best wishes,
> Arne



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Inconsistency with expressions between definitions
  2023-09-24 11:58 ` Linus Björnstam
@ 2023-09-24 16:02   ` Dr. Arne Babenhauserheide
  2023-09-25 12:48     ` Linus Björnstam
  0 siblings, 1 reply; 5+ messages in thread
From: Dr. Arne Babenhauserheide @ 2023-09-24 16:02 UTC (permalink / raw)
  To: Linus Björnstam; +Cc: guile-devel

[-- Attachment #1: Type: text/plain, Size: 1292 bytes --]

Hi,

Linus Björnstam <linus.bjornstam@fastmail.se> writes:

> When you are not referencing x before defining y everything works as
> you want. There is no, so to say, temporal dependency on how the
> things are bound. When you introduce (display x) before actually
> defining y you force letrec* to bind x to the unspecified value,
> because display has side-effects and you don't move around
> side-effecting code.

This is a technical explanation. It answers "how does this happen?"
(thank you for that!), but not "why is this the correct behavior?".

The core problem I see: if you inject some logging code between the
defines, the behavior changes.

I would expect that referencing a variable that can’t yet be used in an
intermediate expression (between defines) would not cause a (potentially
subtle) behavior change, but would throw an error: variable used in
expression that depends on later define.

Racket does not support defines using later defines at all:

$ racket
> (define (using-later-variable)
>      (define x y)
>      (define y #t)
>      x)
> (using-later-variable)
y: undefined;
 cannot use before initialization
 [,bt for context]

Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein,
ohne es zu merken.
draketo.de

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 1125 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Inconsistency with expressions between definitions
  2023-09-24 16:02   ` Dr. Arne Babenhauserheide
@ 2023-09-25 12:48     ` Linus Björnstam
  2023-09-25 13:53       ` Dr. Arne Babenhauserheide
  0 siblings, 1 reply; 5+ messages in thread
From: Linus Björnstam @ 2023-09-25 12:48 UTC (permalink / raw)
  To: Dr. Arne Babenhauserheide; +Cc: guile-devel

Hi again!

I am not sure about letrec*, but this should really raise an error in r6rs. You cannot reference a variable before it is assigned. 

I said it "would work in racket" because I believe it uses the same algorithm for deciding when and how and where and how and when things are bound in letrec*. This should also be the case in chez, but chez displays an error. Given I have found the chez is never wrong with regards to R6RS we can say that guiles behaviour is not conformant with r6rs. 

It is also inconsistent with regards to guiles manual, at least if the part on internal definitions is to be believed. 

What I am saying is: congrats, you found a bug :)

Sorry about confusing you. I was never really sure about letrec* behaviour, but you nerdsniped me and I spent some time reading the "fixing letrec (reloaded)" paper, and now things are more clear.

-- 
  Linus Björnstam

On Sun, 24 Sep 2023, at 18:02, Dr. Arne Babenhauserheide wrote:
> Hi,
>
> Linus Björnstam <linus.bjornstam@fastmail.se> writes:
>
>> When you are not referencing x before defining y everything works as
>> you want. There is no, so to say, temporal dependency on how the
>> things are bound. When you introduce (display x) before actually
>> defining y you force letrec* to bind x to the unspecified value,
>> because display has side-effects and you don't move around
>> side-effecting code.
>
> This is a technical explanation. It answers "how does this happen?"
> (thank you for that!), but not "why is this the correct behavior?".
>
> The core problem I see: if you inject some logging code between the
> defines, the behavior changes.
>
> I would expect that referencing a variable that can’t yet be used in an
> intermediate expression (between defines) would not cause a (potentially
> subtle) behavior change, but would throw an error: variable used in
> expression that depends on later define.
>
> Racket does not support defines using later defines at all:
>
> $ racket
>> (define (using-later-variable)
>>      (define x y)
>>      (define y #t)
>>      x)
>> (using-later-variable)
> y: undefined;
>  cannot use before initialization
>  [,bt for context]
>
> Best wishes,
> Arne
> -- 
> Unpolitisch sein
> heißt politisch sein,
> ohne es zu merken.
> draketo.de
>
> Attachments:
> * signature.asc



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Inconsistency with expressions between definitions
  2023-09-25 12:48     ` Linus Björnstam
@ 2023-09-25 13:53       ` Dr. Arne Babenhauserheide
  0 siblings, 0 replies; 5+ messages in thread
From: Dr. Arne Babenhauserheide @ 2023-09-25 13:53 UTC (permalink / raw)
  To: Linus Björnstam; +Cc: guile-devel

[-- Attachment #1: Type: text/plain, Size: 768 bytes --]


Linus Björnstam <linus.bjornstam@fastmail.se> writes:

> are bound in letrec*. This should also be the case in chez, but chez
> displays an error. Given I have found the chez is never wrong with
> regards to R6RS we can say that guiles behaviour is not conformant
> with r6rs.
> What I am saying is: congrats, you found a bug :)
> Sorry about confusing you. I was never really sure about letrec*
> behaviour, but you nerdsniped me and I spent some time reading the
> "fixing letrec (reloaded)" paper, and now things are more clear.

I guess "sorry"? Or rather "you’re welcome"? :-)

I guess the next step would be to report a bug …

Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein,
ohne es zu merken.
draketo.de

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 1125 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-09-25 13:53 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-24  7:09 Inconsistency with expressions between definitions Dr. Arne Babenhauserheide
2023-09-24 11:58 ` Linus Björnstam
2023-09-24 16:02   ` Dr. Arne Babenhauserheide
2023-09-25 12:48     ` Linus Björnstam
2023-09-25 13:53       ` Dr. Arne Babenhauserheide

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).