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

[Merged by Bors] - refactor(algebra/module): change module into an abbreviation for semimodule #2848

Closed
wants to merge 33 commits into from

Conversation

sgouezel
Copy link
Collaborator

The file algebra/module.lean contains the lines

There were several attempts to make `module` an abbreviation of `semimodule` but this makes
  class instance search too hard for Lean 3.

It turns out that this was too hard for Lean 3 until recently, but this is not too hard for Lean 3.14. This PR removes these two lines, and changes the files accordingly.

This means that the basic objects of linear algebra are now semimodules over semiring, making it possible to talk about matrices with natural number coefficients or apply general results on multilinear maps to get the binomial or the multinomial formula.

A linear map is now defined over semirings, between semimodules which are add_comm_monoids. For some statements, you need to have an add_comm_group, and a ring or a comm_semiring or a comm_ring. I have not tried to lower the possible typeclass assumptions like this in all files, but I have done it carefully in the following files:

  • algebra/module
  • linear_algebra/basic
  • linear_algebra/multilinear
  • topology/algebra/module
  • topology/algebra/multilinear

Other files can be optimised later in the same way when needed, but this PR is already big enough.

I have also fixed the breakage in all the other files. It was sometimes completely mysterious (in tensor products, I had to replace several times linear_map.id.flip with linear_map.flip linear_map.id), but globally all right. I have tweaked a few instance priorities to make sure that things don't go too bad: there are often additional steps in typeclass inference, going from ring to semiring and from add_comm_group to add_comm_monoid, so I have increased their priority (putting it strictly between 100 and 1000), and adjusted some other priorities to get more canonical instance paths, fixing some preexisting weirdnesses (notably in multilinear maps). One place where I really had to help Lean is with normed spaces of continuous multilinear maps, where instance search is just too big.

I am aware that this will be a nightmare to refer, but as often with big refactor it's an "all or nothing" PR, impossible to split in tiny pieces.

@sgouezel sgouezel added the awaiting-review The author would like community review of the PR label May 28, 2020
Copy link
Member

@jcommelin jcommelin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very good to me. Thanks for doing this 🎉

I've flagged places where the instance priority is changed. This is something that we might want to discuss a bit.
I've also flagged places where proofs "downgraded". (Many proofs have moved around, so I probably didn't catch all of these cases.) We might want to discuss/investigate.

@@ -138,6 +138,9 @@ open_locale classical

universes u v w

local attribute [instance, priority 1001]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't comment on whether this is a good setting. I don't understand the priorities well enough, so I would like a second pair of eyes here.

(⇑((@finset.univ _ hs.fintype).sum (λ x, (linear_map.proj x : (s → ℝ) →ₗ[ℝ] ℝ).smul_right x.1))) ''
(@std_simplex _ hs.fintype) :=
(⇑((@finset.univ _ hs.fintype).sum
(λ x, (@linear_map.proj ℝ s _ (λ i, ℝ) _ _ x).smul_right x.1))) ''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unfortunate.

@@ -670,13 +670,15 @@ by rw [← rat.norm_cast_real, ← int.norm_cast_real]; congr' 1; norm_cast
section normed_space

section prio
set_option default_priority 100 -- see Note [default priority]
-- see Note[vector space definition] for why we extend `module`.
set_option default_priority 920 -- see Note [default priority]. Here, we set a rather high priority,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second pair of eyes please.

definitions are given in terms of semirings, but many applications use rings or fields. We increase
a little bit its priority above 100 to try it quickly, but remaining below the default 1000 so that
more specific instances are tried first. -/
attribute [instance, priority 200] ring.to_semiring
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. But flagging this so that other reviewers can easily find it.

@@ -57,6 +57,8 @@ noncomputable theory
open_locale classical
open finset

local attribute [instance, priority 1001]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second pair of eyes please.

begin
show is_linear_map R ((lsubtype_domain s : (α →₀ M) →ₗ[R] (s →₀ M)).comp
(submodule.subtype (supported M R s))),
let F : (supported M R s) ≃ (s →₀ M) := restrict_support_equiv s M,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unfortunate.

@@ -366,8 +369,10 @@ theorem ext_threefold {g h : (M ⊗[R] N) ⊗[R] P →ₗ[R] Q}
begin
let e := linear_equiv.to_equiv (lift.equiv R (M ⊗[R] N) P Q),
apply e.symm.injective,
ext x y z,
exact H x y z,
refine ext _,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to tag something with @[ext] to fix this?

@@ -171,6 +198,8 @@ theorem smul_eq_zero {R E : Type*} [division_ring R] [add_comm_group E] [module

end module

section
set_option default_priority 910
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Second pair of eyes please.

src/algebra/module.lean Outdated Show resolved Hide resolved
src/algebra/module.lean Outdated Show resolved Hide resolved
src/algebra/module.lean Outdated Show resolved Hide resolved
src/algebra/module.lean Outdated Show resolved Hide resolved
@sgouezel
Copy link
Collaborator Author

I had to fix some additional failures after merging master. One of them is the following, in lie_algebra: the proof was

lie_equiv_matrix'.to_morphism.comp (skew_adjoint_lie_subalgebra J.to_bilin_form).incl

and I had to change it to

lie_algebra.morphism.comp (lie_algebra.equiv.to_morphism lie_equiv_matrix')
  (skew_adjoint_lie_subalgebra J.to_bilin_form).incl

So, exactly the same proof, but without the dot notation. I already had to make several changes with the same flavor when fixing the library, for instance going from linear_map.id.flip to linear_map.flip linear_map.id several times. I don't know if this is worth investigating, or if it was to be expected.

@jcommelin
Copy link
Member

Thanks 🎉

bors merge

@github-actions github-actions bot added ready-to-merge All that is left is for bors to build and merge this PR. (Remember you need to say `bors r+`.) and removed awaiting-review The author would like community review of the PR labels May 30, 2020
@bors
Copy link

bors bot commented May 30, 2020

Canceled.

@jcommelin
Copy link
Member

bors merge

@bors
Copy link

bors bot commented May 30, 2020

Merge conflict.

@jcommelin
Copy link
Member

🥺

bors merge

@bors
Copy link

bors bot commented May 30, 2020

Canceled.

@rwbarton
Copy link
Collaborator

bors merge

bors bot pushed a commit that referenced this pull request May 30, 2020
…module (#2848)

The file `algebra/module.lean` contains the lines
```
There were several attempts to make `module` an abbreviation of `semimodule` but this makes
  class instance search too hard for Lean 3.
```
It turns out that this was too hard for Lean 3 until recently, but this is not too hard for Lean 3.14. This PR removes these two lines, and changes the files accordingly.

This means that the basic objects of linear algebra are now semimodules over semiring, making it possible to talk about matrices with natural number coefficients or apply general results on multilinear maps to get the binomial or the multinomial formula.

A linear map is now defined over semirings, between semimodules which are `add_comm_monoid`s. For some statements, you need to have an `add_comm_group`, and a `ring` or a `comm_semiring` or a `comm_ring`. I have not tried to lower the possible typeclass assumptions like this in all files, but I have done it carefully in the following files:
* `algebra/module`
* `linear_algebra/basic`
* `linear_algebra/multilinear`
* `topology/algebra/module`
* `topology/algebra/multilinear`

Other files can be optimised later in the same way when needed, but this PR is already big enough.

I have also fixed the breakage in all the other files. It was sometimes completely mysterious (in tensor products, I had to replace several times `linear_map.id.flip` with `linear_map.flip linear_map.id`), but globally all right. I have tweaked a few instance priorities to make sure that things don't go too bad: there are often additional steps in typeclass inference, going from `ring` to `semiring` and from `add_comm_group` to `add_comm_monoid`, so I have increased their priority (putting it strictly between 100 and 1000), and adjusted some other priorities to get more canonical instance paths, fixing some preexisting weirdnesses (notably in multilinear maps). One place where I really had to help Lean is with normed spaces of continuous multilinear maps, where instance search is just too big.

I am aware that this will be a nightmare to refer, but as often with big refactor it's an "all or nothing" PR, impossible to split in tiny pieces.

Co-authored-by: Scott Morrison <scott.morrison@anu.edu.au>
Co-authored-by: Bryan Gin-ge Chen <bryangingechen@gmail.com>
Co-authored-by: sgouezel <sebastien.gouezel@univ-rennes1.fr>
@bors
Copy link

bors bot commented May 30, 2020

Pull request successfully merged into master.

Build succeeded:

@bors bors bot changed the title refactor(algebra/module): change module into an abbreviation for semimodule [Merged by Bors] - refactor(algebra/module): change module into an abbreviation for semimodule May 30, 2020
@bors bors bot closed this May 30, 2020
@bors bors bot deleted the remove_module branch May 30, 2020 18:01
cipher1024 pushed a commit to cipher1024/mathlib that referenced this pull request Mar 15, 2022
…module (leanprover-community#2848)

The file `algebra/module.lean` contains the lines
```
There were several attempts to make `module` an abbreviation of `semimodule` but this makes
  class instance search too hard for Lean 3.
```
It turns out that this was too hard for Lean 3 until recently, but this is not too hard for Lean 3.14. This PR removes these two lines, and changes the files accordingly.

This means that the basic objects of linear algebra are now semimodules over semiring, making it possible to talk about matrices with natural number coefficients or apply general results on multilinear maps to get the binomial or the multinomial formula.

A linear map is now defined over semirings, between semimodules which are `add_comm_monoid`s. For some statements, you need to have an `add_comm_group`, and a `ring` or a `comm_semiring` or a `comm_ring`. I have not tried to lower the possible typeclass assumptions like this in all files, but I have done it carefully in the following files:
* `algebra/module`
* `linear_algebra/basic`
* `linear_algebra/multilinear`
* `topology/algebra/module`
* `topology/algebra/multilinear`

Other files can be optimised later in the same way when needed, but this PR is already big enough.

I have also fixed the breakage in all the other files. It was sometimes completely mysterious (in tensor products, I had to replace several times `linear_map.id.flip` with `linear_map.flip linear_map.id`), but globally all right. I have tweaked a few instance priorities to make sure that things don't go too bad: there are often additional steps in typeclass inference, going from `ring` to `semiring` and from `add_comm_group` to `add_comm_monoid`, so I have increased their priority (putting it strictly between 100 and 1000), and adjusted some other priorities to get more canonical instance paths, fixing some preexisting weirdnesses (notably in multilinear maps). One place where I really had to help Lean is with normed spaces of continuous multilinear maps, where instance search is just too big.

I am aware that this will be a nightmare to refer, but as often with big refactor it's an "all or nothing" PR, impossible to split in tiny pieces.

Co-authored-by: Scott Morrison <scott.morrison@anu.edu.au>
Co-authored-by: Bryan Gin-ge Chen <bryangingechen@gmail.com>
Co-authored-by: sgouezel <sebastien.gouezel@univ-rennes1.fr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready-to-merge All that is left is for bors to build and merge this PR. (Remember you need to say `bors r+`.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants