From a7bcb3c94df323098ccb846b7f384678c53f4bb0 Mon Sep 17 00:00:00 2001 From: enumeratio Date: Sat, 21 Nov 2026 16:00:43 -0500 Subject: [PATCH] Applying a function does not rename the enclosing function's parameters inside a nested block --- CHANGELOG.md | 11 ++ src/compute-engine/function-utils.ts | 20 +- .../apply-capture-rename.test.ts | 173 ++++++++++++++++++ 3 files changed, 199 insertions(+), 5 deletions(-) create mode 100644 test/compute-engine/apply-capture-rename.test.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index e786ed0d7..fea79583b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,17 @@ get the protocols, and a case pattern of `match` gets no keyword other than `true` and `false`. +### Resolved Issues + +- **Applying a typed let no longer renames the parameters of the enclosing + function** (#655). In `acc, k ↦ Apply(v ↦ Map(j ↦ …acc[j]…, …), Map(q ↦ + acc[q], …))`, the nested `Block` holds the binding of `acc` because its body + reads it. The capture check counted that entry as a binder of the `Block`, + and as the value of `v` mentions `acc`, it renamed `acc` to `acc_1` there, + and nothing substituted the renamed occurrence: a `Fold` with such a step + gave `[5, acc_1[2], acc_1_1[3]]`, not `[5, 6, 7]`. An entry that holds the + binding of a parameter of an enclosing literal is a read, not a binder. + ## 0.195.1 _2026-11-20_ ### Resolved Issues diff --git a/src/compute-engine/function-utils.ts b/src/compute-engine/function-utils.ts index 96b96f8c5..a03aea134 100644 --- a/src/compute-engine/function-utils.ts +++ b/src/compute-engine/function-utils.ts @@ -5323,11 +5323,21 @@ function parameterSubBinds( if (isFunction(node, 'Function')) return names; const held = !node.isCanonical && node.localScope === undefined; const locals = held ? heldBoundNames(node) : blockLocalNames(node); - const kept = names.filter( - (name) => - locals.includes(name) || - !isParameterEntry(subs, node.localScope?.bindings.get(name), name) - ); + // An entry that holds the binding of a parameter of an enclosing literal + // is a read of that parameter, not a binder: a nested `Block` has it when + // its body reads the parameter (`acc` in `(acc) ↦ (v ↦ Map(j ↦ acc[j], …))`). + // Counting it as a binder renamed `acc` to `acc_1` when the value of `v` + // mentioned `acc`, and nothing substituted the renamed occurrences. + const kept = names.filter((name) => { + if (locals.includes(name)) return true; + const entry = node.localScope?.bindings.get(name); + if (isParameterEntry(subs, entry, name)) return false; + return !( + entry !== undefined && + 'value' in entry && + isFunctionLiteralParameter(entry.value) + ); + }); const added = locals.filter((name) => !names.includes(name)); return kept.length === names.length && added.length === 0 ? names diff --git a/test/compute-engine/apply-capture-rename.test.ts b/test/compute-engine/apply-capture-rename.test.ts new file mode 100644 index 000000000..a6fa882dc --- /dev/null +++ b/test/compute-engine/apply-capture-rename.test.ts @@ -0,0 +1,173 @@ +import { engine as ce } from '../utils'; +import { compile } from '../../src/compute-engine/compilation/compile-expression'; + +// Applying a typed let (`Apply(Function(body, v), value)`) whose value mentions +// a parameter of the enclosing function must not rename that parameter inside +// the nested function bodies of `body`. + +type J = any; +const apply = (f: J, ...args: J[]) => ['Apply', f, ...args]; +const range = (to: J) => ['Range', 1, to, 1]; + +const letV = (body: J, typed: boolean) => [ + 'Function', + body, + typed ? ['Typed', 'v', { str: 'list' }] : 'v', +]; + +const stepBody = (nested: 'Map' | 'Filter') => + nested === 'Map' + ? [ + 'Map', + [ + 'Function', + ['If', ['Less', 'j', 'k'], ['At', 'v', 'j'], ['At', 'acc', 'j']], + 'j', + ], + range(3), + ] + : [ + 'Filter', + range(3), + ['Function', ['Less', ['At', 'v', 'j'], ['At', 'acc', 'j']], 'j'], + ]; + +const value = [ + 'Map', + ['Function', ['At', 'acc', 'q'], 'q'], + range(['Subtract', 'k', 1]), +]; + +const fold = (step: J) => ['Fold', step, ['List', 5, 6, 7], ['Range', 2, 3, 1]]; + +const leaked = (json: J) => /acc_1|k_1/.test(JSON.stringify(json)); + +describe('APPLY TYPED LET MENTIONING OUTER PARAMETERS', () => { + for (const typed of [true, false]) { + const tag = typed ? 'typed' : 'untyped'; + + test(`Fold step with Map (${tag})`, () => { + const step = [ + 'Function', + apply(letV(stepBody('Map'), typed), value), + 'acc', + 'k', + ]; + const expr = ce.expr(fold(step) as J); + expect(expr.evaluate().json).toEqual(['List', 5, 6, 7]); + expect(expr.N().json).toEqual(['List', 5, 6, 7]); + }); + + test(`Apply of the step body (${tag})`, () => { + const step = [ + 'Function', + apply(letV(stepBody('Map'), typed), value), + 'acc', + 'k', + ]; + const expr = ce.expr(apply(step, ['List', 5, 6, 7], 2) as J); + expect(leaked(expr.evaluate().json)).toBe(false); + expect(leaked(expr.N().json)).toBe(false); + }); + + test(`Filter instead of Map (${tag})`, () => { + const step = [ + 'Function', + apply(letV(stepBody('Filter'), typed), value), + 'acc', + 'k', + ]; + const r = ce.expr(apply(step, ['List', 5, 6, 7], 2) as J); + expect(leaked(r.evaluate().json)).toBe(false); + expect(leaked(r.N().json)).toBe(false); + }); + } + + test('two levels of nesting', () => { + const inner = letV( + [ + 'Map', + ['Function', ['Add', ['At', 'v', 'j'], ['At', 'acc', 'j']], 'j'], + range(3), + ], + false + ); + const outer = [ + 'Function', + apply( + [ + 'Function', + apply(inner, [ + 'Map', + ['Function', ['Add', ['At', 'w', 'q'], ['At', 'acc', 'q']], 'q'], + range(3), + ]), + 'w', + ], + ['Map', ['Function', ['At', 'acc', 'r'], 'r'], range(3)] + ), + 'acc', + ]; + const r = ce.expr(apply(outer, ['List', 5, 6, 7]) as J); + expect(leaked(r.evaluate().json)).toBe(false); + expect(leaked(r.N().json)).toBe(false); + }); + + test('a user symbol named like the fresh name is not captured', () => { + ce.declare('acc_1', { type: 'integer', value: 100 }); + try { + const step = [ + 'Function', + apply( + letV( + [ + 'Map', + ['Function', ['Add', ['At', 'v', 'j'], 'acc_1'], 'j'], + range(2), + ], + false + ), + [ + 'Map', + ['Function', ['Add', ['At', 'acc', 'q'], 'acc_1'], 'q'], + range(2), + ] + ), + 'acc', + ]; + const r = ce.expr(apply(step, ['List', 5, 6, 7]) as J); + const out = JSON.stringify(r.evaluate().json); + expect(out).not.toMatch(/"acc"/); + expect(out).not.toMatch(/acc_1_/); + expect(out.match(/"acc_1"/g)).toHaveLength(2); + } finally { + ce.forget('acc_1'); + } + }); + + test('a real binder named like a value free name is still renamed', () => { + // `x` is free in the value and bound by the nested literal: the nested + // literal is renamed, so the value keeps meaning the outer `x`. + const f = ['Function', ['Function', ['Power', 'y', 'x'], 'x'], 'y']; + const r = ce.expr(apply(f, 'x') as J).evaluate(); + const g = apply(r.json, 2); + expect(ce.expr(g as J).evaluate().json).toEqual(['Power', 'x', 2]); + }); + + test('the compiled form, where it compiles, agrees', () => { + const step = [ + 'Function', + apply(letV(stepBody('Map'), true), value), + 'acc', + 'k', + ]; + const expr = ce.expr(fold(step) as J); + let result: unknown; + try { + result = (compile(expr, { fallback: false }) as any).run?.(); + } catch { + return; // declines on this branch + } + if (result !== undefined) expect(result).toEqual([5, 6, 7]); + }); +});