dcreager.net

Closing over values

Swanson will need some form of encapsulation, where a value can be “moved inside” of a quotation. (Or alternatively, that a quotation can be a closure.)

A core problem to solve is that Swanson quotations have multiple branches, and every branch of a quotation must have the responsibility to dispose of any values moved inside the quotation. It cannot be possible to move a value into a proper subset of the quotation's branches. If you did, and invoked one of the other branches, nothing would ever consume the value, violating linearity.

This also comes up when considering whether to model stack values as a quotation, since that's just one specific example of moving values (the contents of the stack value) into a quotation.

Stack values can be quotations

Decision

Constructing a quotation will always consume and close over precisely one value. That value must be a stack value. Whenever any of the branches of the quotation are invoked, the closed over value will be unpacked onto the stack before the branch's instructions are executed. If you don't want to close over a value, push an empty stack value onto the stack before creating the quotation. (Unpacking the empty stack value has no effect on the stack.)

There will be no composition instructions; you'll compose quotations manually, and have to decide how the branches of each side will merge together, and how they will consume all of the values that were closed over in the quotations being composed.

..