Loop invariant inference for int locals (Liquid Types / Houdini)

Aperta
#542 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
18/100
Tipo di issue
Funzionalità
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
java
Ambito
compilers

Direzione di ricerca

Read RefinementTypeChecker.visitLoop, havocChangedIn and forgetChanges first: today known is always true at the loop head, and this issue is about filling it with an inferred inductive invariant. The work is a multi-PR design (candidates, Houdini start/step checks plugged into checkUntilFixpoint, post-loop facts), with sub-issues #543-#548 and prerequisite #538; start by reading the Liquid Types and Houdini papers named here plus the test programs in #537 and #479. Done means for (int i = 0; i < 10; i++) use(i) verifies without the i == -1 counterexample and no unsound program is accepted.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

enhancement

Goal

Infer loop invariants for int local variables, so that ordinary loops verify without annotations:

for (int i = 0; i < 10; i++)
    use(i);              // use(@Refinement("_ >= 0") int x): today "found i < 10", counterexample i == -1

Today a loop havocs every variable it changes: at the loop head the variable gets a fresh instance that keeps only its declared refinement (RefinementTypeChecker.visitLoop → havocChangedIn → forgetChanges(name, known, loop), where known is always true). Inside the body only the loop condition is known about it, so i >= 0 is lost (#537, part of #479). The plan is to fill known at the loop head with an inferred invariant.

Approach: liquid inference / Houdini

This is how Liquid Types infers refinements (Rondon, Kawaguchi, Jhala, Liquid Types, PLDI 2008; LiquidHaskell): conjunctions of qualifiers from a finite set, kept only if they can be proved. The fixed-point step is the Houdini algorithm (Flanagan and Leino, Houdini, an Annotation Assistant for ESC/Java, FME 2001).

  1. Candidates for each int local x the loop changes:
    • x >= x_pre, x <= x_pre, where x_pre is x's instance just before the loop (works for symbolic start values too).
    • The comparisons of the loop condition, weakened to be inductive (i < n gives the candidate i <= n).
    • What the body requires of x (a refined parameter _ >= 0 that receives i gives i >= 0; _ < size(this) gives i < size(l)).
  2. Start check: drop each candidate the facts before the loop do not prove for x_pre.
  3. Step check: check the body with the remaining candidates assumed at the head (with the loop condition); at the back edges (end of the body, after a for's update, each continue) drop each candidate not proved for x's last instance. Repeat until nothing is dropped. This plugs into the existing checkUntilFixpoint loop, which already re-checks the body until typestates stop changing and discards the errors of trial checks.
  4. Use: what survives is the known fact of the loop-head instance. After a loop without break, the instance after the loop also knows the invariant and the negated condition.

Soundness: a candidate survives only if it is proved at the start and preserved by every iteration, so the result is inductive. Wrong guesses can only cost precision, never accept a wrong program. Integers are mathematical integers, as elsewhere in LiquidJava (overflow is not modelled).

Cost: each round checks the body once plus one SMT query per candidate. Rounds only drop candidates, so they terminate; candidates and rounds are capped for IDE response time and nested loops.

Scope (PM decision, 2026-11-23)

  • int local variables only.
  • No fields (a call can change them out of sight; they stay havocked).
  • Object ghosts (e.g. size(l)) appear only as bounds in candidates; inferring facts about ghosts is out of scope.

Plan: one small PR per sub-issue

Sub-issues are listed below, in order. Each PR builds on the previous one.

Test programs: #537 (counter, accumulator), #479 (i < l.size() and l.get(i)), #538 (guarded accumulation), and the buffer-filling loop from #537.

Order Issue Part
0 #538 Prerequisite: keep the version-based copy of a path condition when a variable is reassigned (independent of loops)
1 #543 Candidate invariants
2 #544 Houdini check at the loop head (fixes #537)
3 #545 Invariant and negated condition after a loop without break
4 #546 Candidates from what the body requires (#479)
5 #547 Cost caps and nested loops
6 #548 Error messages (needs a PM decision)
Lingua principale
Java
Stelle
73
Fork
38
Merge medio
12h 32m
PR unite (30g)
45

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di liquid-java/liquidjava

Tutte le issue di liquid-java/liquidjava

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.