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

Structural recursion: wrong motive generated #6015

Closed
3 tasks done
arthur-adjedj opened this issue Nov 8, 2024 · 2 comments · Fixed by #6116
Closed
3 tasks done

Structural recursion: wrong motive generated #6015

arthur-adjedj opened this issue Nov 8, 2024 · 2 comments · Fixed by #6116
Labels
bug Something isn't working P-medium We may work on this issue if we find the time

Comments

@arthur-adjedj
Copy link
Contributor

Prerequisites

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

Description

Structural recursion may fail due to a badly formed motive in a brecOn call

Context

I encountered this issue when trying to formalise a theory of signatures for inductive types.

Steps to Reproduce

MWE:

inductive Tyₛ (l : List Unit): Type
| U : Tyₛ l
open Tyₛ

inductive Varₚ (d : Unit): List Unit → Type
| vz : Varₚ d [d']
| vs : Varₚ d l → Varₚ d (Bₛ :: l)

inductive Tmₛ {l : List Unit}: Tyₛ l → Unit → Type 1
| arr   : (T : Type) → Tmₛ A d → Tmₛ A d
| param : Varₚ d l → Tmₛ A d → Tmₛ (@U l) d

def TmₛA {l : List Unit} {d : Unit} {Aₛ : Tyₛ l} (t : Tmₛ Aₛ d): Type := 
match t with
  | .arr dom cd => 
    let cd := TmₛA cd
    dom → cd
  | _ => sorry
termination_by structural t

Expected behavior: The function gets accepted.

Actual behavior: The following error pops up:

failed to infer structural recursion:
Cannot use parameter t:
  application type mismatch
    @Tmₛ.brecOn l fun {d} {Aₛ} t => Type
  argument
    fun {d} {Aₛ} t => Type
  has type
    {d : Unit} → {Aₛ : Tyₛ l} → Tmₛ Aₛ d → Type 1 : Type 2
  but is expected to have type
    (a : Tyₛ l) → (a_1 : Unit) → Tmₛ a a_1 → Type 1 : Type 2

Versions

Lean 4.15.0-nightly-2024-11-08 Target: x86_64-unknown-linux-gnu

Additional Information

The issue seems to be that the motive generated for the recursive call puts arguments in the wrong order. Changing the order of arguments in the signature of Tmₛ fixes things:

def TmₛA {l : List Unit} {Aₛ : Tyₛ l} {d : Unit} (t : Tmₛ Aₛ d): Type := 
match t with
  | .arr dom cd => 
    let cd := TmₛA cd
    dom → cd
  | _ => sorry
termination_by structural t 
-- works

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 Nov 8, 2024
@nomeata
Copy link
Collaborator

nomeata commented Nov 8, 2024

Ok, in general there is code there for this kind of reshuffling of arguments, but it seems to be not tested enough. Maybe the tests only had one level of indices or so. I can have a look next week.

@leanprover-bot leanprover-bot added the P-medium We may work on this issue if we find the time label Nov 15, 2024
nomeata added a commit that referenced this issue Nov 18, 2024
This PR fixes a bug where structural recursion did not work when indices
of the recursive argument appeared as function parameters in a different
order than in the argument's type's definition.

Fixes #6015.
@nomeata
Copy link
Collaborator

nomeata commented Nov 18, 2024

Mostly a straight-forward fix, it seems: #6116

github-merge-queue bot pushed a commit that referenced this issue Nov 18, 2024
This PR fixes a bug where structural recursion did not work when indices
of the recursive argument appeared as function parameters in a different
order than in the argument's type's definition.

Fixes #6015.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working P-medium We may work on this issue if we find the time
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants