In a second `for…of` loop with the same loop variable, specs that use `_x_idx2` get an unknown type

Open
#343 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
65/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
typescript
Domain
compilers

Research direction

Start with the forof case in tools/src/resolve.ts, where the loop index is bound for specs, and compare it with the transform code that numbers repeated loops per function as _x_idx, _x_idx2, and so on. The two counters must agree so that a //@ invariant naming _x_idx2 resolves to type nat. Done when the reproduction emits Dafny that verifies and a regression test checks the second loop's counter name and type.

Written by the indexing model from the issue text.

Description

Problem

SPEC.md §4.1 ("For-of loops") says that when several for…of loops use the same variable name, the generated index is disambiguated as _id_idx, _id_idx2, and so on. Resolve binds the index in the loop's environment so that specs such as //@ invariant can refer to it (tools/src/resolve.ts, the forof case). But resolve and transform name the counter separately: resolve gives every such loop's counter the name _x_idx, while transform numbers repeated loops per function and emits _x_idx2 for the second. A spec in the second loop that writes _x_idx2 therefore names a variable resolve never bound, and it gets an unknown type.

The unknown type matters wherever lowering depends on the type. In real arithmetic the counter is not converted with as real, and Dafny rejects the output. No false proof results, but a documented spec does not verify.

Reproduced on upstream main at add6ccd (0.6.10).

Reproduction

export function halfTwice(xs: number[]): number {
  let n = 0;
  for (const x of xs) {
    n = n + 1;
  }
  let m = 0;
  for (const x of xs) {
    //@ invariant m * 1.5 === _x_idx2 * 1.5
    m = m + 1;
  }
  return n + m;
}

emits, for the second loop:

  var i_x_idx2 := 0;
  while i_x_idx2 < |xs|
    invariant (i_x_idx2 <= |xs|)
    invariant (((m as real) * 1.5) == (i_x_idx2 * 1.5))

dafny verify reports arguments must have comparable types (got real and int) and type of right argument to * (real) must agree with the result type (int).

The same invariant in a function with one loop, //@ invariant m * 1.5 === _x_idx * 1.5, emits ((i_x_idx as real) * 1.5) and verifies.

Expected

Resolve binds the same counter name that transform emits for each loop, so _x_idx2 in the second loop has type nat and lowers like _x_idx in the first. Generating the name once in resolve, recording it on the forof statement, and having transform reuse it removes the disagreement. robertpenner/LemmaScript#37 does this; with it, the reproduction emits ((i_x_idx2 as real) * 1.5) and verifies.

Acceptance criteria

  • The reproduction generates Dafny that verifies.
  • A regression test checks that resolve gives the second loop's counter the name _x_idx2 with type nat, and that an invariant using it verifies.
Dominant language
TypeScript
Stars
113
Forks
13
Avg merge
15h 35m
Merged PRs (30d)
56

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from midspiral/LemmaScript

All issues in midspiral/LemmaScript

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.