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

refactor(*): backport coe_fn from Lean 4 #7033

Closed
wants to merge 100 commits into from
Closed

refactor(*): backport coe_fn from Lean 4 #7033

wants to merge 100 commits into from

Conversation

urkud
Copy link
Member

@urkud urkud commented Apr 4, 2021

See leanprover-community/lean#557 for Lean core part. There are a few remaining bugs:

  • changes in the elaboration order made some code fail;
  • @[derive has_coe_to_fun] is no longer possible;
  • Lean fails to use coe_fn_coe_base as a simp lemma;
  • some tests are not fixed yet.

Open in Gitpod

@urkud urkud added the WIP Work in progress label Apr 4, 2021
@github-actions github-actions bot added the merge-conflict Please `git merge origin/master` then a bot will remove this label. label Apr 4, 2021
@dselsam
Copy link
Collaborator

dselsam commented Aug 18, 2021

FYI I am now (a) expanding coercions during mathport and (b) automatically creating the desired Lean4 Coe, CoeSort, and CoeFun instances instead of their lean3-style counterparts. So, this PR is not necessary to backport.

@Vierkantor
Copy link
Collaborator

src builds succesfully (again) on my machine. Still some errors in test/norm_swap.lean...

@github-actions github-actions bot added the merge-conflict Please `git merge origin/master` then a bot will remove this label. label Oct 13, 2021
@github-actions github-actions bot added merge-conflict Please `git merge origin/master` then a bot will remove this label. and removed merge-conflict Please `git merge origin/master` then a bot will remove this label. labels Oct 15, 2021
@github-actions github-actions bot removed the merge-conflict Please `git merge origin/master` then a bot will remove this label. label Oct 19, 2021
@Vierkantor Vierkantor added awaiting-CI The author would like to see what CI has to say before doing more work. awaiting-review The author would like community review of the PR and removed WIP Work in progress labels Oct 19, 2021
@github-actions github-actions bot removed the awaiting-CI The author would like to see what CI has to say before doing more work. label Oct 19, 2021
@github-actions github-actions bot added the merge-conflict Please `git merge origin/master` then a bot will remove this label. label Oct 21, 2021
@semorrison
Copy link
Collaborator

Replaced by #9824.

@semorrison semorrison closed this Oct 21, 2021
@urkud urkud deleted the coe-fn-backport branch October 24, 2021 17:08
Vierkantor added a commit that referenced this pull request Nov 27, 2021
@YaelDillies pointed out I never updated the `instance : has_coe_to_fun` line in the example to match the `coe_fn` backport #7033.
bors bot pushed a commit that referenced this pull request Dec 10, 2021
This PR is the main proof-of-concept in my plan to use typeclasses to reduce duplication surrounding `hom` classes. Essentially, I want to take each type of bundled homs, such as `monoid_hom`, and add a class `monoid_hom_class` which has an instance for each *type* extending `monoid_hom`. Declarations that now take a parameter of the specific type `monoid_hom M N` can instead take a more general `{F : Type*} [monoid_hom_class F M N] (f : F)`; this means we don't need to duplicate e.g. `monoid_hom.map_prod` to `ring_hom.map_prod`, `mul_equiv.map_prod`, `ring_equiv.map_prod`, or `monoid_hom.map_div` to `ring_hom.map_div`, `mul_equiv.map_div`, `ring_equiv.map_div`, ...

Basically, instead of having `O(n * k)` declarations for `n` types of homs and `k` lemmas, following the plan we only need `O(n + k)`.

## Overview

 * Change `has_coe_to_fun` to include the type of the function as a parameter (rather than a field of the structure) (**done** as part of #7033)
 * Define a class `fun_like`, analogous to `set_like`, for types of bundled function + proof (**done** in #10286)
 * Extend `fun_like` for each `foo_hom` to create a `foo_hom_class` (**done** in this PR for `ring_hom` and its ancestors, **todo** in follow-up for the rest)
 * Change parameters of type `foo_hom A B` to take `{F : Type*} [foo_hom_class F A B] (f : F)` instead (**done** in this PR for `map_{add,zero,mul,one,sub,div}`, **todo** in follow-up for remaining declarations)

## API changes

Lemmas matching `*_hom.map_{add,zero,mul,one,sub,div}` are deprecated. Use the new `simp` lemmas called simply `map_add`, `map_zero`, ...

Namespaced lemmas of the form `map_{add,zero,mul,one,sub,div}` are now protected. This includes e.g. `polynomial.map_add` and `multiset.map_add`, which involve `polynomial.map` and `multiset.map` respectively. In fact, it should be possible to turn those `map` definitions into bundled maps, so we don't even need to worry about the name change.

## New classes

 * `zero_hom_class`, `one_hom_class` defines `map_zero`, `map_one`
 * `add_hom_class`, `mul_hom_class` defines `map_add`, `map_mul`
 * `add_monoid_hom_class`, `monoid_hom_class` extends `{zero,one}_hom_class`, `{add,mul}_hom_class`
 * `monoid_with_zero_hom_class` extends `monoid_hom_class` and `zero_hom_class`
 * `ring_hom_class` extends `monoid_hom_class`, `add_monoid_hom_class` and `monoid_with_zero_hom_class`

## Classes still to be implemented

Some of the core algebraic homomorphisms are still missing their corresponding classes:

 * `mul_action_hom_class` defines `map_smul`
 * `distrib_mul_action_hom_class`, `mul_semiring_action_hom_class` extends the above
 * `linear_map_class` extends `add_hom_class` and defines `map_smulₛₗ`
 * `alg_hom_class` extends `ring_hom_class` and defines `commutes`

We could also add an `equiv_like` and its descendants `add_equiv_class`, `mul_equiv_class`, `ring_equiv_class`, `linear_equiv_class`, ...

## Other changes

`coe_fn_coe_base` now has an appropriately low priority, so it doesn't take precedence over `fun_like.has_coe_to_fun`.

## Why are you unbundling the morphisms again?

It's not quite the same thing as unbundling. When using unbundled morphisms, parameters have the form `(f : A → B) (hf : is_foo_hom f)`; bundled morphisms look like `(f : foo_hom A B)` (where `foo_hom A B` is equivalent to `{ f : A → B // is_foo_hom f }`; typically you would use a custom structure instead of `subtype`). This plan puts a predicate on the *type* `foo_hom` rather than the *elements* of the type as you would with unbundled morphisms. I believe this will preserve the advantages of the bundled approach (being able to talk about the identity map, making it work with `simp`), while addressing one of its disadvantages (needing to duplicate all the lemmas whenever extending the type of morphisms).

## Discussion

Main Zulip thread: https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/Typeclasses.20for.20morphisms

Some other threads referencing this plan:
 * https://leanprover.zulipchat.com/#narrow/stream/113488-general/topic/Morphism.20refactor
 * https://leanprover.zulipchat.com/#narrow/stream/263328-triage/topic/issue.20.231044.3A.20bundling.20morphisms
 * #1044
 * #4985



Co-authored-by: Anne Baanen <Vierkantor@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-review The author would like community review of the PR merge-conflict Please `git merge origin/master` then a bot will remove this label.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants