lean4 icon indicating copy to clipboard operation
lean4 copied to clipboard

Cannot derive functional induction principle: failed to transform matcher, type error when constructing new pre-splitter motive

Open TwoFX opened this issue 3 months ago • 3 comments

Prerequisites

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

  • [x] Check that your issue is not already filed: https://github.com/leanprover/lean4/issues
  • [x] Reduce the issue to a minimal, self-contained, reproducible test case. Avoid dependencies to Mathlib or Batteries.
  • [x] Test your test case against the latest nightly release, for example on https://live.lean-lang.org/#project=lean-nightly (You can also use the settings there to switch to “Lean nightly”)

Description

This code:

def bla (a : Nat) (h : 0 < a) : Nat :=
  if h : a ≤ 1 then
    0
  else
    have : 0 ≠ a := by omega
    match a with
    | 0 => False.elim (by contradiction)
    | a + 1 => bla a (by omega)
termination_by structural a

theorem blub {a : Nat} {h : 0 < a} : bla a h ≤ 1 := by
  fun_induction bla

generates this error message

Failed to realize constant bla.induct_unfolding:
  Cannot derive functional induction principle (please report this issue)
    failed to transform matcher, type error when constructing new pre-splitter motive:
      bla.match_1
        (fun a h h_1 this_1 =>
          t = a →
            ∀ (x : Nat.below (motive := fun a => ∀ (h : 0 < a), motive a h (bla a h)) a),
              motive a h
                (match a, h, h_1, this with
                | 0, h, h_2, this => (bla._proof_1 h).elim
                | a.succ, h, h_2, this => bla a (bla._proof_2 a h_2)))
        t h✝ h this
    failed with
      Application type mismatch: The argument
        this✝
      has type
        0 ≠ t
      but is expected to have type
        0 ≠ a✝
      in the application
        bla.match_1 (fun a h h_1 this => Nat) a✝ h✝ h this✝

Context

Minimized from a similar function in the String API.

Steps to Reproduce

  1. Copy the above code into live.lean-lang.org

Expected behavior: No error

Actual behavior: See above

Versions

Lean 4.25.0-nightly-2025-09-22

Impact

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

TwoFX avatar Sep 23 '25 08:09 TwoFX

Thanks for the report. match statements with dependent targets are notorious tricky.

A work-around in your case is to use the non-unfolding induction principle:

theorem blub {a : Nat} {h : 0 < a} : bla a h ≤ 1 := by
  induction a, h using bla.induct
  · unfold bla
    simp [*]
  · unfold bla
    simp [*]  

nomeata avatar Sep 23 '25 10:09 nomeata

Interesting, this works:

def bla (a : Nat) (h : 0 < a) : Nat :=
  if h : a ≤ 1 then
    0
  else
    (fun (this : 0 ≠ a) => match a with
    | 0 => False.elim (by contradiction)
    | a + 1 => bla a (by omega)) (by omega)
termination_by structural a

This is more have-like than have it seems…

nomeata avatar Sep 23 '25 11:09 nomeata

Some attempt at a fix in #10519, but I’m not sure if this is conceptually the right thing to do. (If not then maybe handling prop-valued let/have more gracefully would work in this case.)

nomeata avatar Sep 23 '25 12:09 nomeata