Dafny: array every and map callbacks that read the index emit ill-typed code
This was generated by AI during triage.
Problem
Array every and map callbacks that read their second argument (the element index) generate ill-typed Dafny. Valid TypeScript is rejected by dafny resolve.
Reproduced on current upstream main at 6b18f23 (LemmaScript 0.6.8), with Node 24.17.0 and Dafny 4.11.0. Both examples pass strict TypeScript checking. An element-only every/map control resolves successfully.
Reproduction
Save these as separate files:
// indexedEvery.ts
export function pairwiseAtMost(a: number[], b: number[]): boolean {
return a.length === b.length && a.every((x, i) => x <= b[i]);
}
// indexedMap.ts
export function plusIndex(a: number[]): number[] {
return a.map((x, i) => x + i);
}
From the repository root, after installing the development dependencies:
npm run build
node tools/dist/lsc.js gen --backend=dafny indexedEvery.ts
node tools/dist/lsc.js gen --backend=dafny indexedMap.ts
dafny resolve --standard-libraries indexedEvery.dfy
dafny resolve --standard-libraries indexedMap.dfy
Generation succeeds for each; both resolver invocations exit 2 with two errors.
every
Generated code:
type Unknown(==, 0)
function pairwiseAtMost(a: seq<int>, b: seq<int>): bool
{
((|a| == |b|) && Std.Collections.Seq.All(a, (x: int, i: Unknown) => (x <= b[i])))
}
Resolver errors:
incorrect type for selection into seq<int> (got Unknown)
incorrect argument type at index 1 for predicate parameter 'p'
(expected int -> bool, found (int, Unknown) -> bool)
The index has type Unknown, and Seq.All receives a two-parameter callback although it expects an element-only predicate.
map
Generated code:
type Unknown(==, 0)
function plusIndex(a: seq<int>): seq<int>
{
seq(|a|, i_map requires 0 <= i_map < |a| => ((x: int, i: Unknown) => (x + i))(a[i_map]))
}
Resolver errors:
wrong number of arguments (got 1, but function type '(int, Unknown) ~> int'
expects 2: (_#p0: int, _#p1: Unknown))
type of right argument to + (Unknown) must agree with the result type (int)
The comprehension applies the callback to the element only, leaving the index unbound and incorrectly typed.
Expected
The callback's index is an integer, and each callback invocation binds both the element and its zero-based index. Element-only callbacks continue to work.
For example, pairwiseAtMost([1, 2], [1, 3]) is true, pairwiseAtMost([1, 4], [1, 3]) is false, and plusIndex([10, 10, 10]) returns [10, 11, 12].
Related work
This was reported in a comment on midspiral/LemmaScript#233. That still-open PR fixes indexed some callbacks. This issue tracks the separate every and map cases; their observed symptom is compiler rejection. It does not claim that these two reproductions verify an incorrect result.
The fork's upstream/indexed-callbacks branch, commit 87d2d72, contains a candidate fix covering every, some, and map. Its some work overlaps midspiral/LemmaScript#233; an upstream follow-up would need to coordinate with that PR. The candidate branch was not rebased or revalidated as part of this current-main reproduction.
Acceptance criteria
- Both reproductions generate Dafny that resolves with standard libraries enabled.
- Regression tests cover the index type and both element/index bindings for
everyandmap. - Generated behavior agrees with JavaScript for empty arrays and representative non-empty inputs, including an
everyresult that depends on the index. - Element-only callbacks retain their existing behavior.