Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

refactor(order/well_founded): typeclasses for well-founded < and >#15023

Closed
vihdzp wants to merge 143 commits intomasterfrom
wf_rel_class
Closed

refactor(order/well_founded): typeclasses for well-founded < and >#15023
vihdzp wants to merge 143 commits intomasterfrom
wf_rel_class

Conversation

@vihdzp
Copy link
Copy Markdown
Collaborator

@vihdzp vihdzp commented Jun 28, 2022

Well-founded refactor

We create a new unbundled typeclass is_well_founded α r and the corresponding well_founded_lt and well_founded_gt typeclasses. We rephrase the existing results on generic well-founded relations in terms of these new typeclasses.

This is a very large refactor, so we thoroughly describe its main effects on the library:

order/rel_classes.lean

Here's where the new typeclasses are defined. We provide basic instances, and transfer preexisting API on well_founded for convenience.

A minor change: we remove the redundant irrefl hypothesis from is_well_order.

order/well_founded.lean

We pretty much re-do this whole file. The basic theorems are stated in terms of all three new typeclasses for convenience.

order/well_founded_set.lean

We redefine set.well_founded_on and set.is_wf in terms of our new predicates. These definitions are equivalent to the previous. We rename set.well_founded_on to set.is_well_founded_on for consistency. We also add API to get the corresponding instances when needed.

(on second thought, this is definitely scope creep. I'll move this to a new refactor at a later point.)

Miscellaneous

Many theorems that previously had well_founded in the name are renamed using is_well_founded and stated in terms of this new typeclass. (Todo: evaluate potential scope creep here)

Various structures, such as category.noetherian_object, category.artinian_object, and wf_dvd_monoid, whose sole field was the well-foundedness of some relation, have been turned into reducible defs.

A few definitions for well-founded relations have been rewritten in terms of inv_image and measure. These are def-eq but their well-foundedness can now be inferred automatically.

Many theorems are turned into instances. Some former theorems that can now be inferred as instances are removed, such as polynomial.degree_lt_wf.

We golf a few theorems by using the new API, particularly the function.arg_max API, though we don't change their proofs considerably. Some proofs show up with a large diff, but this is only due to the indenting changing.


This will probably be a huge refactor once it's done. Be patient!

Open in Gitpod

@vihdzp vihdzp added WIP Work in progress awaiting-review The author would like community review of the PR awaiting-CI The author would like to see what CI has to say before doing more work. labels Jun 28, 2022
dec_tac := tactic.assumption }

@[priority 100] -- see Note [lower instance priority]
instance is_well_order.is_strict_total_order {α} (r : α → α → Prop) [is_well_order α r] :
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I don't know why these redundant typeclasses were here. I presume it's to short-circuit the search, but is this standard practice? If so, I'll add these again.

classical.some (H.has_min p h)
/-- A minimal element of a nonempty set with respect to a well-founded relation. See also
`well_founded_lt.min` and `well_founded_gt.max`. -/
noncomputable def min [is_well_founded α r] (s : set α) (hs : s.nonempty) : α :=
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I've decided to make s explicit in all of these theorems, since you can't really infer s from a proof of s.nonempty a lot of the time.

@vihdzp vihdzp marked this pull request as ready for review July 2, 2022 19:29
@vihdzp
Copy link
Copy Markdown
Collaborator Author

vihdzp commented Jul 2, 2022

The PR builds now! Right now the diff is huge, but
a) this number will go down when the helper PRs are merged
b) most files have under 50 lines of diff, it's mostly the big three files in the PR description taking all the space.

@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 Jul 2, 2022
@vihdzp vihdzp removed the WIP Work in progress label Jul 4, 2022
@vihdzp
Copy link
Copy Markdown
Collaborator Author

vihdzp commented Jul 15, 2022

On second thought, I definitely scope crept here. I'll introduce the typeclasses in a separate PR and slowly implement them throughout the library.

@vihdzp vihdzp closed this Jul 15, 2022
bors bot pushed a commit that referenced this pull request Jul 27, 2022
…15399)

# Well-founded typeclasses

We introduce a new unbundled typeclass `is_well_founded` for a general well-founded relation, and reducible defs `well_founded_lt` and `well_founded_gt` for well-founded `<` and `>` specifically, to be used as mixins. For now, all we do is specialize only the most basic API on well-founded relations to these new typeclasses. 

This is just an initial development. If this is merged, a subsequent PR will redefine `is_well_order` in terms of `is_well_founded` (and remove the redundant `irrefl` field). Further PRs will focus on providing all the possible instances for this new typeclass, and actually using it throughout mathlib.

Moved from #15023. 

## Why do we want this?

Here's the part where I justify why this is a good thing we want in mathlib.

### Well-ordered `<`

There is a need to talk about well-ordered `<` relations, as a quick search for `is_well_order α (<)` or `is_well_order ι (<)` reveals. The most obvious way to spell this condition out, namely `[has_lt α] [is_well_order α (<)]` is actually the most inconvenient, since none of the results for linear orders are available. You actually need to write `[linear_order α] [is_well_order α (<)]` instead.

With this new typeclass, the obvious spelling is now the good one: `[linear_order α] [well_founded_lt α]`

### Redundant typeclasses

We actually already at least four typeclasses for the well-foundedness of a concrete relation: `category.noetherian_object`, `category.artinian_object`, `topological_space.noetherian_space`, and `wf_dvd_monoid`. One currently needs to specialize the characterizing properties of a well-founded relation to these separate typeclasses in order to use them. Redefining these typeclasses in terms of these new ones, and making them `reducible def`s, would alleviate this problem.

### Typeclass inference

Well-founded relations are actually a nice candidate for typeclass inference, since there's many common constructions that automatically preserve well-foundedness. These include taking inverse images (including `measure` and `order.preimage`), maps through relation embeddings (including subtypes), lexicographic sums or products, and adding a top or bottom element. Many of these are currently provided as instances, but only for well-orders.

### Misleading theorem names 

Perhaps the most useful result on well-founded relations is that every nonempty set has a minimum - when you view the relation as `<`, that is. This leads to misleading theorem names when you're using them on a well-founded `>` relation. With this refactor, we could simply specialize `well_founded.min` into `well_founded_gt.max` and end up with clearer theorem statements.

On a related note, given a linear order and a well-founded `<` relation, we can rephrase `well_founded.not_lt_min` into the much more convenient `well_founded_lt.min_le`.
bottine pushed a commit to bottine/mathlib that referenced this pull request Jul 30, 2022
…eanprover-community#15399)

# Well-founded typeclasses

We introduce a new unbundled typeclass `is_well_founded` for a general well-founded relation, and reducible defs `well_founded_lt` and `well_founded_gt` for well-founded `<` and `>` specifically, to be used as mixins. For now, all we do is specialize only the most basic API on well-founded relations to these new typeclasses. 

This is just an initial development. If this is merged, a subsequent PR will redefine `is_well_order` in terms of `is_well_founded` (and remove the redundant `irrefl` field). Further PRs will focus on providing all the possible instances for this new typeclass, and actually using it throughout mathlib.

Moved from leanprover-community#15023. 

## Why do we want this?

Here's the part where I justify why this is a good thing we want in mathlib.

### Well-ordered `<`

There is a need to talk about well-ordered `<` relations, as a quick search for `is_well_order α (<)` or `is_well_order ι (<)` reveals. The most obvious way to spell this condition out, namely `[has_lt α] [is_well_order α (<)]` is actually the most inconvenient, since none of the results for linear orders are available. You actually need to write `[linear_order α] [is_well_order α (<)]` instead.

With this new typeclass, the obvious spelling is now the good one: `[linear_order α] [well_founded_lt α]`

### Redundant typeclasses

We actually already at least four typeclasses for the well-foundedness of a concrete relation: `category.noetherian_object`, `category.artinian_object`, `topological_space.noetherian_space`, and `wf_dvd_monoid`. One currently needs to specialize the characterizing properties of a well-founded relation to these separate typeclasses in order to use them. Redefining these typeclasses in terms of these new ones, and making them `reducible def`s, would alleviate this problem.

### Typeclass inference

Well-founded relations are actually a nice candidate for typeclass inference, since there's many common constructions that automatically preserve well-foundedness. These include taking inverse images (including `measure` and `order.preimage`), maps through relation embeddings (including subtypes), lexicographic sums or products, and adding a top or bottom element. Many of these are currently provided as instances, but only for well-orders.

### Misleading theorem names 

Perhaps the most useful result on well-founded relations is that every nonempty set has a minimum - when you view the relation as `<`, that is. This leads to misleading theorem names when you're using them on a well-founded `>` relation. With this refactor, we could simply specialize `well_founded.min` into `well_founded_gt.max` and end up with clearer theorem statements.

On a related note, given a linear order and a well-founded `<` relation, we can rephrase `well_founded.not_lt_min` into the much more convenient `well_founded_lt.min_le`.
robertylewis pushed a commit that referenced this pull request Aug 2, 2022
…15399)

# Well-founded typeclasses

We introduce a new unbundled typeclass `is_well_founded` for a general well-founded relation, and reducible defs `well_founded_lt` and `well_founded_gt` for well-founded `<` and `>` specifically, to be used as mixins. For now, all we do is specialize only the most basic API on well-founded relations to these new typeclasses. 

This is just an initial development. If this is merged, a subsequent PR will redefine `is_well_order` in terms of `is_well_founded` (and remove the redundant `irrefl` field). Further PRs will focus on providing all the possible instances for this new typeclass, and actually using it throughout mathlib.

Moved from #15023. 

## Why do we want this?

Here's the part where I justify why this is a good thing we want in mathlib.

### Well-ordered `<`

There is a need to talk about well-ordered `<` relations, as a quick search for `is_well_order α (<)` or `is_well_order ι (<)` reveals. The most obvious way to spell this condition out, namely `[has_lt α] [is_well_order α (<)]` is actually the most inconvenient, since none of the results for linear orders are available. You actually need to write `[linear_order α] [is_well_order α (<)]` instead.

With this new typeclass, the obvious spelling is now the good one: `[linear_order α] [well_founded_lt α]`

### Redundant typeclasses

We actually already at least four typeclasses for the well-foundedness of a concrete relation: `category.noetherian_object`, `category.artinian_object`, `topological_space.noetherian_space`, and `wf_dvd_monoid`. One currently needs to specialize the characterizing properties of a well-founded relation to these separate typeclasses in order to use them. Redefining these typeclasses in terms of these new ones, and making them `reducible def`s, would alleviate this problem.

### Typeclass inference

Well-founded relations are actually a nice candidate for typeclass inference, since there's many common constructions that automatically preserve well-foundedness. These include taking inverse images (including `measure` and `order.preimage`), maps through relation embeddings (including subtypes), lexicographic sums or products, and adding a top or bottom element. Many of these are currently provided as instances, but only for well-orders.

### Misleading theorem names 

Perhaps the most useful result on well-founded relations is that every nonempty set has a minimum - when you view the relation as `<`, that is. This leads to misleading theorem names when you're using them on a well-founded `>` relation. With this refactor, we could simply specialize `well_founded.min` into `well_founded_gt.max` and end up with clearer theorem statements.

On a related note, given a linear order and a well-founded `<` relation, we can rephrase `well_founded.not_lt_min` into the much more convenient `well_founded_lt.min_le`.
khwilson pushed a commit that referenced this pull request Aug 2, 2022
…15399)

# Well-founded typeclasses

We introduce a new unbundled typeclass `is_well_founded` for a general well-founded relation, and reducible defs `well_founded_lt` and `well_founded_gt` for well-founded `<` and `>` specifically, to be used as mixins. For now, all we do is specialize only the most basic API on well-founded relations to these new typeclasses. 

This is just an initial development. If this is merged, a subsequent PR will redefine `is_well_order` in terms of `is_well_founded` (and remove the redundant `irrefl` field). Further PRs will focus on providing all the possible instances for this new typeclass, and actually using it throughout mathlib.

Moved from #15023. 

## Why do we want this?

Here's the part where I justify why this is a good thing we want in mathlib.

### Well-ordered `<`

There is a need to talk about well-ordered `<` relations, as a quick search for `is_well_order α (<)` or `is_well_order ι (<)` reveals. The most obvious way to spell this condition out, namely `[has_lt α] [is_well_order α (<)]` is actually the most inconvenient, since none of the results for linear orders are available. You actually need to write `[linear_order α] [is_well_order α (<)]` instead.

With this new typeclass, the obvious spelling is now the good one: `[linear_order α] [well_founded_lt α]`

### Redundant typeclasses

We actually already at least four typeclasses for the well-foundedness of a concrete relation: `category.noetherian_object`, `category.artinian_object`, `topological_space.noetherian_space`, and `wf_dvd_monoid`. One currently needs to specialize the characterizing properties of a well-founded relation to these separate typeclasses in order to use them. Redefining these typeclasses in terms of these new ones, and making them `reducible def`s, would alleviate this problem.

### Typeclass inference

Well-founded relations are actually a nice candidate for typeclass inference, since there's many common constructions that automatically preserve well-foundedness. These include taking inverse images (including `measure` and `order.preimage`), maps through relation embeddings (including subtypes), lexicographic sums or products, and adding a top or bottom element. Many of these are currently provided as instances, but only for well-orders.

### Misleading theorem names 

Perhaps the most useful result on well-founded relations is that every nonempty set has a minimum - when you view the relation as `<`, that is. This leads to misleading theorem names when you're using them on a well-founded `>` relation. With this refactor, we could simply specialize `well_founded.min` into `well_founded_gt.max` and end up with clearer theorem statements.

On a related note, given a linear order and a well-founded `<` relation, we can rephrase `well_founded.not_lt_min` into the much more convenient `well_founded_lt.min_le`.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

awaiting-review The author would like community review of the PR blocked-by-other-PR This PR depends on another PR which is still in the queue. A bot manages this label via PR comment.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant