Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

genInjectivity failure #3386

Closed
1 task done
arthur-adjedj opened this issue Feb 18, 2024 · 0 comments · Fixed by #3398
Closed
1 task done

genInjectivity failure #3386

arthur-adjedj opened this issue Feb 18, 2024 · 0 comments · Fixed by #3398
Labels
bug Something isn't working

Comments

@arthur-adjedj
Copy link
Contributor

Prerequisites

  • Put an X between the brackets on this line if you have done all of the following:
    • Check that your issue is not already filed.
    • Reduce the issue to a minimal, self-contained, reproducible test case. Avoid dependencies to mathlib4 or std4.

Description

In some cases, Lean fails to generate injectivity lemmas for inductive types

Context

The issue was first discovered in lean-reflection

Steps to Reproduce

MWE:

inductive Tyₛ : Type (u+1)
| SPi : (T : Type u) -> (T -> Tyₛ) -> Tyₛ

inductive Tmₛ.{u} :  Tyₛ.{u} -> Type (u+1)
| app : Tmₛ (.SPi T A) -> (arg : T) -> Tmₛ (A arg)

Expected behavior: No error

Actual behavior: Error:

application type mismatch
  Tyₛ.SPi T A
argument has type
  T✝ → Tyₛ
but function has type
  (T → Tyₛ) → Tyₛ

Versions

4.6.0-rc1
Web

Additional Information

The generated injectivity lemma looks like this:

theorem Tmₛ.app.inj {T : Type u} {A : T → Tyₛ} {a : Tmₛ (Tyₛ.SPi T A)} {arg : T} {T_1 : Type u} {a_1 : Tmₛ (Tyₛ.SPi T_1 A)} :
  Tmₛ.app a arg = Tmₛ.app a_1 arg →
    T = T_1 ∧ HEq a a_1 := fun x => Tmₛ.noConfusion x fun T_eq A_eq a_eq arg_eq => eq_of_heq a_eq 

The error happens because T is duplicated where it shouldn't be. In order for the injectivity lemma to be as general as possible, Lean duplicates any argument which doesn't occur in the end type of the constructor (here, in Tmₛ (A arg)). However, this is not precise enough, as some terms of that type may still depend on the variable: here, T doesn't appear in that type, but appears in the type of A. A solution would be to also check the occurence of variables inside the types of free variables appearing in the end type.

Impact

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

@arthur-adjedj arthur-adjedj added the bug Something isn't working label Feb 18, 2024
github-merge-queue bot pushed a commit that referenced this issue May 6, 2024
Closes #3386

Currently, when generating the signature of an injectivity lemma for a
certain constructor `c : forall xs, Foo a_1 ... a_n`,
`mkInjectiveTheoremTypeCore?` will differentiate between variables which
are bound to stay the same between the two equal values (i.e inductive
indices), and non-fixed ones. To do that, the function currently checks
whether a variable `x ∈ xs` appears in the final co-domain `Foo a_1 ...
a_n` of the constructor. This condition isn't enough however. As shown
in the linked issue, the codomain may also depend on variables which
appears in the type of free vars contained in `Foo a_1 ... a_n`, but not
in the term itself. This PR fixes the issue by also checking the types
of any free variable occuring in the final codomain, so as to ensure
injectivity lemmas are well-typed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
1 participant