Fwd: issue with internal define and define-values

23 views
Skip to first unread message

John Cowan

unread,
Oct 23, 2018, 5:45:32 PM10/23/18
to scheme-re...@googlegroups.com
I received this report with a request to forward it to WG1.  You who receive this message are WG1 for this purpose.  Please discuss.

---------- Forwarded message ---------
From: Marc Feeley <fee...@iro.umontreal.ca>
Date: Tue, Oct 23, 2018 at 5:21 PM
Subject: issue with internal define and define-values
To: John Cowan <co...@ccil.org>


Hi John.  I’m not sure where to send this issue report on R7RS small.  I hope you can forward it to the committee for discussion.

The specification of internal define and define-values in the R7RS is misleading because it says that the variable(s) is/are *bound* to the value(s).  But because internal definitions are transformed to a letrec* they are in fact assigned, similarly to top-level defines.  This is an important aspect which affects the semantics of closures that close over internally defined variables when continuations are used to “repeat” a definition.

Below is some code that shows the issue for define-values.  I tested it with Chicken, Guile and Chez and got two different results.  Which one is the semantics intended by the R7RS spec?

Marc



(define (p x)
  (write x)
  (newline))

(define (f x)
  (call-with-current-continuation
   (lambda (k)
     (k (* x x) k))))

(define closures '())

(define (go)
  (define-values (n again)
    (f 2))
  (set! closures (cons (lambda () n) closures))
  (p n)
  (let ((next (- n 1)))
    (set! n (+ n 1000))
    (if (>= next 0)
        (again next again))))

(go)

(p ((list-ref closures 0)))
(p ((list-ref closures 1)))
(p ((list-ref closures 2)))
(p ((list-ref closures 3)))
(p ((list-ref closures 4)))


;; Chicken and Guile:
;; 4
;; 3
;; 2
;; 1
;; 0
;; 1000
;; 1000
;; 1000
;; 1000
;; 1000
;;
;; Chez:
;; 4
;; 3
;; 2
;; 1
;; 0
;; 1000
;; 1001
;; 1002
;; 1003
;; 1004

Reply all
Reply to author
Forward
0 new messages