Migrating mutable variables to the heap

How to migrate a single variable

If a local variable x is migrated to the heap, every mention needs to be changed:

I recommend defining a helper function for each of these cases.

Primitives ref, !, and := are equivalent to the primitives of the same names that are built into Standard ML; you will need to add them to the UFT and the SVM. Their implementations in the SVM will be almost identical to mkclosure, getclslot, and setclslot, except they will operate on a struct VMBlock, not a closure.

How to identify and migrate all necessary variables

A local variable x must be migrated if it meets both of the following criteria:

Mutated, captured variables can be migrated to the heap in a two-pass algorithm. I recommend that you define a monad ’a decorated, which should extend any value of type ’a with an environment that remembers a property of mutated variables:

The translation can be written using the Applicative operators <*>, <$>, and pure, which you will have to define.1

A definition is rewritten by translating its expressions, then discarding the decorations.

Testing

The ultimate test will be the examples from section 2.7.1 of my book. But for starters, here is a file you can test. First use run-hox-with-predef, then use uft hox-cl to confirm that y is not migrated.

(define must-migrate (x)
  (let* ([f (lambda () x)]
         [_ (set x 99)])
    (f)))

(check-expect (must-migrate 3) 99)


(define should-not-migrate (y)
  (let* ([f (lambda () y)])
    (f)))

(check-expect (should-not-migrate 3) 3)

  1. You know pure as succeed.