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

Abierto
#343 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
65/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
typescript
Área
compilers

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.
Lenguaje dominante
TypeScript
Estrellas
113
Forks
13
Merge medio
15 h 35 min
PR fusionados (30 d)
56

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de midspiral/LemmaScript

Todos los issues de midspiral/LemmaScript

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.