Skip to content
This repository was archived by the owner on Nov 6, 2026. It is now read-only.

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

Closed
sgouezel wants to merge 33 commits into
masterfrom
remove_module
Closed

sgouezel wants to merge 33 commits into
masterfrom
remove_module

Conversation

@sgouezel

Copy link
Copy Markdown
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

@jcommelin jcommelin left a comment

Copy link
Copy Markdown
Member

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.


universes u v w

local attribute [instance, priority 1001]

Copy link
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems unfortunate.

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
Copy Markdown
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.

Comment thread src/algebra/ring.lean
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
Copy Markdown
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.

open_locale classical
open finset

local attribute [instance, priority 1001]

Copy link
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems unfortunate.

apply e.symm.injective,
ext x y z,
exact H x y z,
refine ext _,

Copy link
Copy Markdown
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?

Comment thread src/algebra/module.lean
end module

section
set_option default_priority 910

Copy link
Copy Markdown
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.

Comment thread src/algebra/module.lean Resolved
Comment thread src/algebra/module.lean Resolved
Comment thread src/algebra/module.lean Resolved
Comment thread src/algebra/module.lean Resolved
Comment thread src/algebra/module.lean Resolved
@sgouezel

Copy link
Copy Markdown
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
Copy Markdown
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

bors Bot commented May 30, 2020

Copy link
Copy Markdown

Canceled.

@jcommelin

Copy link
Copy Markdown
Member

bors merge

@bors

bors Bot commented May 30, 2020

Copy link
Copy Markdown

Merge conflict.

@jcommelin

Copy link
Copy Markdown
Member

🥺

bors merge

@bors

bors Bot commented May 30, 2020

Copy link
Copy Markdown

Canceled.

@rwbarton

Copy link
Copy Markdown
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 <[email protected]>
Co-authored-by: Bryan Gin-ge Chen <[email protected]>
Co-authored-by: sgouezel <[email protected]>
@bors

bors Bot commented May 30, 2020

Copy link
Copy Markdown

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 <[email protected]>
Co-authored-by: Bryan Gin-ge Chen <[email protected]>
Co-authored-by: sgouezel <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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.

6 participants