leanprover-community / leanprover-community/mathlib4

`simps -fullyApplied` should generate `coe_` instead of `_apply`

Open
#37,331 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

t-meta
Dominant language
Lean
Stars
4.2k
Forks
1.7k
PR merge metrics
No merged PRs in 30d

Description

It is quite often the case that one wants to generate an unapplied simp lemma for a bundled morphism bar of type Foo (implementing FunLike). Unfortunately, one can only give one name to the coercion to function projection, and to switch between the applied and unapplied versions one has to use simps -fullyApplied (whose behavior is clear only on unary functions, btw).

Here is a MWE:

import Mathlib.Data.FunLike.Basic
import Mathlib.Tactic.Common

structure Foo where
  toFun : Nat → Nat

instance : FunLike Foo Nat Nat where
  coe f := f.toFun
  coe_injective' := by rintro ⟨⟩; congr!

initialize_simps_projections Foo (toFun → apply)

-- This is good
/-- trace: [simps.verbose] The projections for this structure have already been initialized by a previous invocation of `initialize_simps_projections` or `@[simps]`.
    Generated projections for Foo:
    Projection apply: DFunLike.coe
[simps.verbose] adding projection bar_apply:
      ∀ (a : ℕ), bar a = id a -/
#guard_msgs in
@[simps?]
def bar : Foo where
  toFun := id

-- This is bad
/-- trace: [simps.verbose] The projections for this structure have already been initialized by a previous invocation of `initialize_simps_projections` or `@[simps]`.
    Generated projections for Foo:
    Projection apply: DFunLike.coe
[simps.verbose] adding projection baz_apply:
      ⇑baz = id -/
#guard_msgs in
@[simps? -fullyApplied]
def baz : Foo where
  toFun := id

The most principled way I see of solving this is to allow users to register arbitrary projections (rather than just structure fields) and let them decide which projection to generate by doing simps name_of_projection. Eg the second bundled morphism above would instead be declared as

@[simps coe]
def baz : Foo where
  toFun := id

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 with the MWE using simps -fullyApplied, initialize_simps_projections, and the @[simps]/@[simps?] declarations. Trace how the applied and unapplied projections are selected, including the unary-function limitation. Done means the unapplied case generates the intended coe_ projection and the MWE's guarded messages reflect the corrected behavior.

Written by the indexing model from the issue text.

Assessment

Domain
tooling
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.