ChezScheme
ChezScheme copied to clipboard
Reducing to single instance the hard-coded Scheme objects
The following program:
#!r6rs
(import (rnrs (6)))
(display (eq? "ciao" "ciao"))
(newline)
(display (eq? '(1 2 3) '(1 2 3)))
(newline)
(display (eq? '#(1 2 3) '#(1 2 3)))
(newline)
(display (eq? '#vu8(1 2 3) '#vu8(1 2 3)))
(newline)
(flush-output-port (current-output-port))
outputs four #f both when the program is run from source and when the program is run after compiling it. I understand why this happens, its implications, and I understand that implementations can choose how to handle hard-coded objects.
I would like a command line option and/or a Scheme parameter that causes the fasl writer to reduce equal? hard-coded objects to a single instance (simply using an internal hash table and appropriate fields in the fasl format).
It's pretty widely accepted that hard-coded objects are an anti-pattern in every language I've ever heard of. If you want them to be the same object, define a constant like you should have in the first place.
Also, there's no guarantee that doing the thing you ask will result in any different behavior from the example code you supplied. The result of the eq? could be determined by the optimizer, which means those hard-coded objects don't necessarily have to exist in the compiled code at all.
there's no guarantee that doing the thing you ask will result in any different behavior from the example code you supplied.
True. I just do not want duplicate objects in compiled code.
Whether pairs are mutable or not is a syntactic property, not a value property. Procedures can refer directly to pairs for which mutation is defined, and sharing those would be incorrect. This is not a problem for immutable values, and perhaps also not for values where mutation is undefined (e.g., strings).