`vcgen`: spec lemmas never apply when the spec's value type is a dependent application `T r` of a rule premise

Aperta
#16,905 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
48/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Ambito
tooling

Direzione di ricerca

Start with the self-contained reproducer in the issue and the vcgen spec-application path, especially applySpec; compare how it unifies the spec value type with the goal against how it unifies the program. Run the reproducer and its non-dependent control to confirm the failure and expected behavior. Done means the dependent spec applies after the program determines its rule premise, with both result-type phrasings working.

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

Descrizione

bug
Description

A @[spec] lemma for a program whose result type is a type-family application T r, with r universally quantified in the lemma, is found as a candidate but never applies:

error: No spec applicable to program readReg R.a in monad EStateM PUnit St.
Candidates were [SpecProof.global readReg_spec].

Self-contained reproducer (also in Repro.lean, which contains a passing non-dependent control with the identical proof shape):

import Std.Tactic.Do
import Std.WP
import Std.Data.ExtDHashMap

open Std.WP Lean.Order
set_option experimental.vcgen true

inductive R where | a | b deriving DecidableEq, Hashable

abbrev T : R → Type | .a => Nat | .b => Bool

structure St where
  regs : Std.ExtDHashMap R T

abbrev M := EStateM Unit St

def resultPost {σ α ε : Type} (Q : α → σ → Prop) (E : ε → σ → Prop) :
    EStateM.Result ε σ α → Prop
  | .ok a s => Q a s
  | .error e s => E e s

def readReg (r : R) : M (T r) := do
  let .some v := (← get).regs.get? r
    | throw ()
  pure v

@[spec] theorem readReg_spec (r : R) (Q : T r → St → Prop) (E : Unit → St → Prop) :
    Triple (readReg r) (fun s => resultPost Q E (readReg r s)) Q E := by
  constructor
  intro s h
  cases hres : readReg r s <;>
    simp_all [wp, WP.trans, Lean.Order.PredTrans.apply_mk, resultPost]
  -- on v4.35.0: simp_all [wp, WP.wpTrans, resultPost]

example : ⦃fun s => s.regs.get? R.a = some (0 : Nat)⦄
    (readReg R.a)
    ⦃fun v _ => v = (0 : Nat)⦄ := by
  vcgen          -- No spec applicable to program readReg R.a
  all_goals simp_all [readReg, resultPost, bind, EStateM.bind, get, getThe,
    MonadState.get, MonadStateOf.get, EStateM.get, throw, throwThe,
    MonadExceptOf.throw, EStateM.throw, pure, EStateM.pure]

The same file with value type Nat instead of T r (the readNat control in Repro.lean) applies and closes.

Analysis

The goal's Triple/wp carries the reduced value type (Nat): with T an abbrev this always happens at elaboration; with a non-reducible def T it happens whenever the call site forces the reduced type, e.g. a postcondition fun v _ => v = (0 : Nat). The spec's value type is T ?r. Spec application appears to attempt T ?r =?= Nat before the program unification (readReg ?r =?= readReg R.a) that would determine ?r := R.a, and T ?r =?= Nat cannot be solved by inversion, so applySpec gives up.

Consistent with this, with def T the example applies when the postcondition is phrased type-preservingly (through val : (r : R) → T r, keeping the goal's value type T R.a), and fails with the (0 : Nat) phrasing. Unifying the program first would make both phrasings work.

The Std.Do/mvcgen machinery applies the analogous spec in all phrasings.

Context

The shape comes from Sail's Lean backend (https://github.com/rems-project/lean-sail): generated ISA semantics use a register file RegisterType : Register → Type with readReg r : SailM (RegisterType r) over EStateM (Error ue) (SequentialState RegisterType c), and generated call sites always use the reduced value type (BitVec 32). This makes per-primitive @[spec] lemmas unusable for Sail-generated code; we currently work around it with relational specs at whole-instruction granularity.

A likely-related symptom in the full Sail-generated setting (not yet minimized): binding the raw state, do return (← get).regs.get? X3, in the generated SailM also reports No spec applicable for the surrounding bind, although the same pattern works in small monads.

Versions

Reproduced identically on v4.35.0, nightly-2026-11-24
(4.38.0-nightly-2026-11-24, commit 59cbe2250650), and nightly-2026-11-30
(4.38.0-nightly-2026-11-30, commit fd149e00cbdb).

Lingua principale
Lean
Stelle
10.5k
Fork
1.2k
Merge medio
19h 36m
PR unite (30g)
323

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 leanprover/lean4

Tutte le issue di leanprover/lean4

Issue simili

Altre issue su DevTools

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.