Nils M Holm wrote:
> As far as I can tell, that optimization would be perfectly
> fine.
Thanks.
> If there was any way in which DO-THAT could use A, BTW,
> even the first transformation indicated at the top would
> be invalid.
All of this happens on fully expanded code. The real thing
(as I understand it so far) is this: after full expansion
the code in the core language is:
(letrec ((a (display "ciao"))
(b (lambda (x) a (list x))))
(set! a 123)
123)
in which A is referenced and assigned, so an internal
structure reports that it is "referenced before
optimization"; the LETREC optimizer transforms the code into
(not quite but close enough):
(let ((a (display '"ciao")))
(let ((b (lambda (x)
(begin
a
(list x)))))
(begin
(set! a '123)
'123)))
and now comes the source optimizer:
* First it processes the inner body, and since A is marked
as referenced before optimization, it does not remove the
assignment; rather it marks A as "still assigned after
optimization".
* Then it processes the inner LET, recognising the function
B as not referenced; so it removes it:
(let ((a (display '"ciao")))
(begin
(set! a '123)
'123))
A is left marked as "not referenced after optimization".
* Finally it processes the outer LET, now A is actually no
more referenced, but still assigned after optimization and
so the final transformation is:
(begin
(display '"ciao")
(let ((a #<unspecified>))
(begin
(set! a '123)
'123)))
because it is too late to remove the assignment.
An additional pass would clean up things, but it is not
performed by Vicare (whose logic is the same as the Ikarus
Scheme one). Maybe because such suboptimization is rare.
--
Marco Maggi