﻿id	summary	reporter	owner	description	type	status	priority	milestone	component	version	resolution	keywords	cc	difficulty
516	letrec fails when used in re-assigning top-level bindings	Moritz Heidkamp	felix winkelmann	"The following program works as expected when interpreted, i.e. it prints ""hey"". However, when compiled it prints {{{#<unbound value>}}} or, when compiled as a shared object, it signals {{{unbound variable: foo#bar}}}. The comments in the program point out two ways of making it work when compiled. It seems that the optimizer removes the original binding of {{{bar}}} when {{{letrec}}} is used for its redefinition. Putting some top-level code between definition and re-definition prevents that as well as replacing {{{letrec}}} with {{{let*}}}. Note that the module form can be left out to reproduce the problem, too. 

{{{

(module foo

(bar)
(import chicken scheme)

(define bar 'hey)

;; uncommenting the next line makes it work when compiled
;; (print 'some-top-level-expression)

(set! bar
      ;; changing the letrec to a let* also makes it work when compiled
      (letrec ((baz bar)
               (qux (lambda () baz)))
        qux))

(print (bar))

)

}}}"	defect	closed	critical	4.9.0	compiler	4.6.x	fixed	compiler letrec top-level		
