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

Open
#16,905 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
tooling

Research direction

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.

Written by the indexing model from the issue text.

Description

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).

Dominant language
Lean
Stars
10.5k
Forks
1.2k
Avg merge
19h 36m
Merged PRs (30d)
323

Getting set up

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

All issues in leanprover/lean4

Similar issues

More DevTools issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.