Skip to content

to_additive unable to translate toOneHomClass #660

Closed

Description

@winstonyin

See PR #659, in Algebra.Hom.Group, around theorem MonoidHom.coe_coe. If the quickfix

attribute [to_additive AddMonoidHomClass.toZeroHomClass] MonoidHomClass.toOneHomClass

is removed, the to_additive tactic seems to be looking for AddMonoidHomClass.toOneHomClass, when it should look for AddMonoidHomClass.toZeroHomClass. It has no trouble translating OneHomClass to ZeroHomClass, but has trouble with toOneHomClass.

Activity

  1. self-assigned this
    on Nov 20, 2022
  2. joneugster commented on Nov 20, 2022

    @joneugster
    Member

    I'll look into that tonight :)

  3. joneugster commented on Nov 20, 2022

    @joneugster
    Member

    Looks like to_additive always has problems if a class extends 2+ other classes. The relevant TODO is this here:

    /-- Add the structure fields of `src` to the translations dictionary
    so that future uses of `to_additive` will map them to the corresponding `tgt` fields. -/
    def proceedFields (src tgt : Name) : CoreM Unit := do
    let env : Environment ← getEnv
    let aux := proceedFieldsAux src tgt
    aux (fun n => do
    let fields := if isStructure env n then getStructureFieldsFlattened env n else #[]
    return fields |> .map Name.toString |> Array.toList
    )
    -- [todo] run to_additive on the constructors of n:
    -- aux (fun n => (env.constructorsOf n).mmap $ ...

    MWE to reproduce error
    -- First, all the additive versions
    class ZeroA (A : Type _) where
      zero : ℕ
      zero_add : zero + zero = zero
    
    class ZeroB (A : Type _) extends ZeroA A where
      zero_sub : zero - zero = 0
    
    class ZeroC (A : Type _) extends ZeroA A where
      zero_refl : zero = zero
    
    class ZeroD (A : Type _) extends ZeroB A, ZeroC A
    
    -- Now all the multiplicative versions
    @[to_additive]
    class OneA (A : Type _) where
      one : ℕ
      one_mul : one * one = one
    
    @[to_additive]
    class OneB (A : Type _) extends OneA A where
      one_sub : one - one = 0
    
    @[to_additive]
    class OneC (A : Type _) extends OneA A where
      one_refl : one = one
    
    @[to_additive]
    class OneD (A : Type _) extends OneB A, OneC A
    
    -- check `to_additive` deals properly with all extensions.
    @[to_additive]
    lemma one_mul_test_one {A : Type _} [x : OneD A] : true := by
      have := x.toOneA
      have := x.toOneC
      have := x.toOneB
      trivial
    

    Error: (kernel) unknown constant 'Test.ZeroD.toOneC'

    (Because it fails to translate OneD.toOneC to ZeroD.toZeroC)

    Unfortunately I don't know how to fix this just now, maybe somebody else does.

  4. removed their assignment
    on Nov 20, 2022
  5. fpvandoorn commented on Nov 23, 2022

    @fpvandoorn
    Member

    That todo is not quite the right one. It is for translating constructors, like AddMonoidHomClass.mk. However, this is not actually necessary: the implementation will do fine without these translations.

    @digama0: Am I correct that the environment does not contain information about which structures a given structure extends? What is the best way forward? The @[ancestor] hack from Lean 3? Make a Core PR that records this information?

  6. digama0 commented on Nov 23, 2022

    @digama0
    Member

    When I removed the ancestor attribute from mathport, my understanding was that this information either already exists or would be stored by lean to avoid the need for duplicating the information. (That said I haven't looked at it in detail and a core PR may be needed.)

  7. fpvandoorn commented on Nov 24, 2022

    @fpvandoorn
    Member

    I created a RFC for a core PR at leanprover/lean4#1881

  8. eric-wieser commented on Apr 3, 2023

    @eric-wieser
    Member

    This has come up again in #3171; it would be great to revisit it.

  9. added a commit that references this issue on Dec 21, 2024
    c459ca0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions