fix: lambdas capture their defining environment#883
Open
mwaddip wants to merge 1 commit into
Open
Conversation
The JVM's FuncValue.eval returns a closure over the env it was created in
(values.scala): each application evaluates the body in that captured env
extended with the argument binding. sigma-rust's Value::Lambda carried
only args + body, and every invocation site (Apply and the HOF evals)
bound arguments into the CALLER's env with a save/restore dance — correct
for lambdas applied where they were created, wrong for lambdas that
escape: the inner lambda of add = (a: Int) => (b: Int) => a + b lost a
the moment the outer Apply returned, so the curried add(3)(1) errored
where the JVM evaluates 4.
Lambda gains the captured bindings (snapshotted by FuncValue::eval), and
a shared LambdaInvoker materializes the captured env once per lambda
value and binds the argument slot per call — replacing the save/restore
dance in Apply, Coll.{map,filter,fold,exists,forAll,flatMap} and
Option.{map,filter}. The capture must hold uniformly: an escaped lambda
fed to a HOF (val f = mk(3); coll.map(f)) hits the same root cause.
Surfaced by SANTA's v6 HOF vectors (HOF_currying_Apply_of_Apply,
blessed Int 4).
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The JVM's
FuncValue.evalreturns a closure over its defining env (values.scala); each application evaluates the body in that captured env plus the argument binding. sigma-rust'sValue::Lambdacarried onlyargs+body, and every invocation site (Apply, theColl/OptionHOF evals) bound arguments into the CALLER's env with a save/restore dance — wrong for lambdas that escape: the inner lambda ofadd = (a: Int) => (b: Int) => a + blostawhen the outer Apply returned, so the curriedadd(3)(1)errored where the JVM evaluates 4.Lambdanow captures its defining bindings and a sharedLambdaInvokerevaluates bodies in the captured env — uniformly, since an escaped lambda fed to a HOF hits the same root cause.Surfaced by SANTA's v6 HOF vectors (
add(3)(1)→ Int 4).