<div dir="ltr">I'm using Racket to implement a language for which I need to track state updates---in particular, variable mutation using set!. For example, consider this module definition:<div><br></div><div><div><font face="courier new, monospace">#lang racket</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">(require (only-in racket [set! #%set!]))</font></div><div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">(define global (make-hash))</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">(define-syntax-rule (location-of id)</font></div><div><font face="courier new, monospace"> (#%variable-reference id)) ; doesn't quite do the right thing</font></div>
<div><font face="courier new, monospace"><br></font></div><div><font face="courier new, monospace">(define-syntax-rule (set! id expr)</font></div><div><font face="courier new, monospace"> (let ([val expr])</font></div><div>
<font face="courier new, monospace"> (hash-set! global (location-of id) val)</font></div><div><font face="courier new, monospace"> (#%set! id val)))</font></div></div><div><br></div><div>When I evaluate the following sequence of forms against the above definition, I would like the <font face="courier new, monospace">global</font> hash map to contain just one binding that maps the location for 'x' to the value 2. With the above implementation I get two map entries, since variable-reference doesn't quite do what I hoped it did:</div>
<div><br></div><div><div><font face="courier new, monospace">> (define x 0)</font></div><div><font face="courier new, monospace">> (set! x 1)</font></div><div><font face="courier new, monospace">> (set! x 2)</font></div>
<div><font face="courier new, monospace">> x</font></div><div><font face="courier new, monospace">2</font></div><div><font face="courier new, monospace">> global</font></div><div><font face="courier new, monospace">'#hash((#<variable-reference> . 1) (#<variable-reference> . 2))</font></div>
</div><div><br></div><div>Is there another construct in Racket that I could use for this purpose? If not, can something like this be implemented and how much work would it entail?</div><div><br></div><div>I have a purely macro-based solution that works for the most part, but it's fragile and there are corner cases for which it is just wrong. So, before trying to fix that, I was wondering if there is a nicer way to solve it by somehow getting handles for variable locations that are comparable using eq? or equal?</div>
<div><br></div><div>Thanks!</div><div><br></div><div>Emina</div><div><br></div></div>