leanprover / leanprover/lean4

`obtain` in `decreasing_by` block leads to kernel type error

Open
#10,287 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug P-medium
Dominant language
Lean
Stars
9.2k
Forks
990
Avg merge
1d 17h
Merged PRs (30d)
175

Description

Prerequisites

Please put an X between the brackets as you perform the following steps:

Description

In certain cases, using the obtain tactic in a decreasing_by tactic block can lead to an ill-typed state that later leads to a "(kernel) application type mismatch" error.

Context

This bug occurred again while I was working on a project related to type theory formalization involving many declarations with delicate termination behavior.

Steps to Reproduce

Run this:

inductive X : Nat → Type where

/--
error: (kernel) application type mismatch
  @Eq.mp (X j) (X j) ⋯
argument has type
  (@fun _a => X _a) j = (@fun _a => X _a) x.val
but function has type
  X j = X j → X j → X j
-/
#guard_msgs(error) in
def f (h : X j) : X j := by
    have : { j' // j = j' } := ⟨j, rfl⟩
    rw [this.2] at h
    have _ := f h
    sorry
termination_by sizeOf h
decreasing_by
  obtain ⟨val, hty⟩ := this
  sorry

Expected behavior:

Either calling the obtain tactic should fail (although I think this one should work) or it should produce a well-typed tactic state.

Actual behavior:

The obtain tactic makes the goal ill-typed, leading to a kernel type error.

The tactic state at the end is:

j : Nat
h : X j
val : Nat
hty : j = val
⊢ sizeOf (@Eq.mp (X j) (X j) ⋯.mp h) < sizeOf h

The omitted proof is congrArg (fun _a => X _a) ⟨val, hty⟩.property : X j = X ⟨val, hty⟩.val, but @Eq.mp (X j) (X j) expects X j = X j. Pre-obtain, the proof had type X j = X this.val and this.val was defeq to j.

Moving the obtain out of the decreasing_by block into the declaration's body, the error disappears.

Versions
Lean 4.24.0-nightly-2025-09-06
Target: x86_64-unknown-linux-gnu
Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Contributor guide

Open the contributing guide

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.

Research direction

Start by running the minimal reproducer involving obtain inside the decreasing_by block and inspect the reported tactic state. Compare it with the behavior when obtain is moved into the declaration body; done means the tactic either rejects the case cleanly or leaves a well-typed state without the kernel application type mismatch.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.