Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 15 additions & 5 deletions src/compute-engine/function-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
173 changes: 173 additions & 0 deletions test/compute-engine/apply-capture-rename.test.ts
Original file line number Diff line number Diff line change
@@ -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<integer>' }] : '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]);
});
});
Loading