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

Commit dd60035

Browse files
committed
chore(algebra/{covariant_and_contravariant + ordered_monoid_lemmas}): new file covariant_and_contravariant (#7890)
This PR creates a new file `algebra/covariant_and_contravariant` and moves the part of `algebra/ordered_monoid_lemmas` dealing exclusively with `covariant` and `contravariant` into it. It also rearranges the documentation, with a view to the later PRs, building up to #7645. The discrepancy between the added and removed lines is entirely due to longer documentation: no actual Lean code has changed, except, of course, for the `import` in `algebra/ordered_monoid_lemmas` that now uses `covariant_and_contravariant`.
1 parent 538f015 commit dd60035

File tree

2 files changed

+117
-82
lines changed

2 files changed

+117
-82
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/-
2+
Copyright (c) 2021 Damiano Testa. All rights reserved.
3+
Released under Apache 2.0 license as described in the file LICENSE.
4+
Authors: Damiano Testa
5+
-/
6+
7+
import algebra.group.defs
8+
9+
/-!
10+
# Covariants and contravariants
11+
12+
This file contains general lemmas and instances to work with the interactions between a relation and
13+
an action on a Type.
14+
15+
The intended application is the splitting of the ordering from the algebraic assumptions on the
16+
operations in the `ordered_[...]` hierarchy.
17+
18+
The strategy is to introduce two more flexible typeclasses, `covariant_class` and
19+
`contravariant_class`.
20+
21+
* `covariant_class` models the implication `a ≤ b → c * a ≤ c * b` (multiplication is monotone),
22+
* `contravariant_class` models the implication `a * b < a * c → b < c`.
23+
24+
Since `co(ntra)variant_class` takes as input the operation (typically `(+)` or `(*)`) and the order
25+
relation (typically `(≤)` or `(<)`), these are the only two typeclasses that I have used.
26+
27+
The general approach is to formulate the lemma that you are interested and prove it, with the
28+
`ordered_[...]` typeclass of your liking. After that, you convert the single typeclass,
29+
say `[ordered_cancel_monoid M]`, with three typeclasses, e.g.
30+
`[partial_order M] [left_cancel_semigroup M] [covariant_class M M (function.swap (*)) (≤)]`
31+
and have a go at seeing if the proof still works!
32+
33+
Note that it is possible to combine several co(ntra)variant_class assumptions together.
34+
Indeed, the usual ordered typeclasses arise from assuming the pair
35+
`[covariant_class M M (*) (≤)] [contravariant_class M M (*) (<)]`
36+
on top of order/algebraic assumptions.
37+
38+
A formal remark is that normally covariant_class uses the `(≤)`-relation, while contravariant_class
39+
uses the `(<)`-relation. This need not be the case in general, but seems to be the most common
40+
usage. In the opposite direction, the implication
41+
```lean
42+
[semigroup α] [partial_order α] [contravariant_class α α (*) (≤)] => left_cancel_semigroup α
43+
```
44+
holds (note the `co*ntra*` assumption and the `(≤)`-relation).
45+
46+
# Formalization notes
47+
We stick to the convention of using `function.swap (*)` (or `function.swap (+)`), for the
48+
typeclass assumptions, since `function.swap` is slightly better behaved than `flip`.
49+
However, sometimes as a **non-typeclass** assumption, we prefer `flip (*)` (or `flip (+)`),
50+
as it is easier to use. -/
51+
52+
-- TODO: convert `has_exists_mul_of_le`, `has_exists_add_of_le`?
53+
-- TODO: relationship with `add_con`
54+
-- include equivalence of `left_cancel_semigroup` with
55+
-- `semigroup partial_order contravariant_class α α (*) (≤)`?
56+
-- use ⇒, as per Eric's suggestion?
57+
58+
section variants
59+
60+
variables {M N : Type*} (μ : M → N → N) (r s : N → N → Prop) (m : M) {a b c : N}
61+
62+
63+
variables (M N)
64+
/-- `covariant` is useful to formulate succintly statements about the interactions between an
65+
action of a Type on another one and a relation on the acted-upon Type.
66+
67+
See the `covariant_class` doc-string for its meaning. -/
68+
def covariant : Prop := ∀ (m) {n₁ n₂}, r n₁ n₂ → r (μ m n₁) (μ m n₂)
69+
70+
/-- `contravariant` is useful to formulate succintly statements about the interactions between an
71+
action of a Type on another one and a relation on the acted-upon Type.
72+
73+
See the `contravariant_class` doc-string for its meaning. -/
74+
def contravariant : Prop := ∀ (m) {n₁ n₂}, s (μ m n₁) (μ m n₂) → s n₁ n₂
75+
76+
/-- Given an action `μ` of a Type `M` on a Type `N` and a relation `r` on `N`, informally, the
77+
`covariant_class` says that "the action `μ` preserves the relation `r`.
78+
79+
More precisely, the `covariant_class` is a class taking two Types `M N`, together with an "action"
80+
`μ : M → N → N` and a relation `r : N → N`. Its unique field `covc` is the assertion that
81+
for all `m ∈ M` and all elements `n₁, n₂ ∈ N`, if the relation `r` holds for the pair
82+
`(n₁, n₂)`, then, the relation `r` also holds for the pair `(μ m n₁, μ m n₂)`,
83+
obtained from `(n₁, n₂)` by "acting upon it by `m`".
84+
85+
If `m : M` and `h : r n₁ n₂`, then `covariant_class.covc m h : r (μ m n₁) (μ m n₂)`.
86+
-/
87+
class covariant_class :=
88+
(covc : covariant M N μ r)
89+
90+
/-- Given an action `μ` of a Type `M` on a Type `N` and a relation `r` on `N`, informally, the
91+
`contravariant_class` says that "if the result of the action `μ` on a pair satisfies the
92+
relation `r`, then the initial pair satisfied the relation `r`.
93+
94+
More precisely, the `contravariant_class` is a class taking two Types `M N`, together with an
95+
"action" `μ : M → N → N` and a relation `r : N → N`. Its unique field `covtc` is the assertion that
96+
for all `m ∈ M` and all elements `n₁, n₂ ∈ N`, if the relation `r` holds for the pair
97+
`(μ m n₁, μ m n₂)` obtained from `(n₁, n₂)` by "acting upon it by `m`"", then, the relation `r`
98+
also holds for the pair `(n₁, n₂)`.
99+
100+
If `m : M` and `h : r (μ m n₁) (μ m n₂)`, then `covariant_class.covc m h : r n₁ n₂`.
101+
-/
102+
class contravariant_class :=
103+
(covtc : contravariant M N μ s)
104+
105+
end variants

src/algebra/ordered_monoid_lemmas.lean

Lines changed: 12 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,93 +3,23 @@ Copyright (c) 2016 Jeremy Avigad. All rights reserved.
33
Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Jeremy Avigad, Leonardo de Moura, Mario Carneiro, Johannes Hölzl, Damiano Testa
55
-/
6-
import algebra.group.defs
7-
import order.basic
8-
/-!
9-
This file begins the splitting of the ordering assumptions from the algebraic assumptions on the
10-
operations in the `ordered_[...]` hierarchy.
11-
12-
The strategy is to introduce two more flexible typeclasses, covariant_class and contravariant_class.
13-
14-
* covariant_class models the implication a ≤ b → c * a ≤ c * b (multiplication is monotone),
15-
* contravariant_class models the implication a * b < a * c → b < c.
16-
17-
Since `co(ntra)variant_class` takes as input the operation (typically `(+)` or `(*)`) and the order
18-
relation (typically `(≤)` or `(<)`), these are the only two typeclasses that I have used.
19-
20-
The general approach is to formulate the lemma that you are interested and prove it, with the
21-
`ordered_[...]` typeclass of your liking. After that, you convert the single typeclass,
22-
say `[ordered_cancel_monoid M]`, with three typeclasses, e.g.
23-
`[partial_order M] [left_cancel_semigroup M] [covariant_class M M (function.swap (*)) (≤)]`
24-
and have a go at seeing if the proof still works!
25-
26-
Note that it is possible to combine several co(ntra)variant_class assumptions together.
27-
Indeed, the usual ordered typeclasses arise from assuming the pair
28-
`[covariant_class M M (*) (≤)] [contravariant_class M M (*) (<)]`
29-
on top of order/algebraic assumptions.
30-
31-
A formal remark is that normally covariant_class uses the `(≤)`-relation, while contravariant_class
32-
uses the `(<)`-relation. This need not be the case in general, but seems to be the most common
33-
usage. In the opposite direction, the implication
34-
35-
```lean
36-
[semigroup α] [partial_order α] [contravariant_class α α (*) (≤)] => left_cancel_semigroup α
37-
```
38-
holds (note the `co*ntra*` assumption and the `(≤)`-relation).
39-
-/
40-
-- TODO: convert `has_exists_mul_of_le`, `has_exists_add_of_le`?
41-
-- TODO: relationship with add_con
42-
-- include equivalence of `left_cancel_semigroup` with
43-
-- `semigroup partial_order contravariant_class α α (*) (≤)`?
44-
-- use ⇒, as per Eric's suggestion?
45-
section variants
46-
47-
variables {M N : Type*} (μ : M → N → N) (r s : N → N → Prop) (m : M) {a b c : N}
486

7+
import algebra.covariant_and_contravariant
8+
import order.basic
499

50-
variables (M N)
51-
/-- `covariant` is useful to formulate succintly statements about the interactions between an
52-
action of a Type on another one and a relation on the acted-upon Type.
53-
54-
See the `covariant_class` doc-string for its meaning. -/
55-
def covariant : Prop := ∀ (m) {n₁ n₂}, r n₁ n₂ → r (μ m n₁) (μ m n₂)
56-
57-
/-- `contravariant` is useful to formulate succintly statements about the interactions between an
58-
action of a Type on another one and a relation on the acted-upon Type.
59-
60-
See the `contravariant_class` doc-string for its meaning. -/
61-
def contravariant : Prop := ∀ (m) {n₁ n₂}, s (μ m n₁) (μ m n₂) → s n₁ n₂
62-
63-
/-- Given an action `μ` of a Type `M` on a Type `N` and a relation `r` on `N`, informally, the
64-
`covariant_class` says that "the action `μ` preserves the relation `r`.
65-
66-
More precisely, the `covariant_class` is a class taking two Types `M N`, together with an "action"
67-
`μ : M → N → N` and a relation `r : N → N`. Its unique field `covc` is the assertion that
68-
for all `m ∈ M` and all elements `n₁, n₂ ∈ N`, if the relation `r` holds for the pair
69-
`(n₁, n₂)`, then, the relation `r` also holds for the pair `(μ m n₁, μ m n₂)`,
70-
obtained from `(n₁, n₂)` by "acting upon it by `m`".
71-
72-
If `m : M` and `h : r n₁ n₂`, then `covariant_class.covc m h : r (μ m n₁) (μ m n₂)`.
73-
-/
74-
class covariant_class :=
75-
(covc : covariant M N μ r)
76-
77-
/-- Given an action `μ` of a Type `M` on a Type `N` and a relation `r` on `N`, informally, the
78-
`contravariant_class` says that "if the result of the action `μ` on a pair satisfies the
79-
relation `r`, then the initial pair satisfied the relation `r`.
10+
/-!
11+
# Ordered monoids
12+
This file develops the basics of ordered monoids.
8013
81-
More precisely, the `contravariant_class` is a class taking two Types `M N`, together with an
82-
"action" `μ : M → N → N` and a relation `r : N → N`. Its unique field `covtc` is the assertion that
83-
for all `m ∈ M` and all elements `n₁, n₂ ∈ N`, if the relation `r` holds for the pair
84-
`(μ m n₁, μ m n₂)` obtained from `(n₁, n₂)` by "acting upon it by `m`"", then, the relation `r`
85-
also holds for the pair `(n₁, n₂)`.
14+
## Implementation details
15+
Unfortunately, the number of `'` appended to lemmas in this file
16+
may differ between the multiplicative and the additive version of a lemma.
17+
The reason is that we did not want to change existing names in the library.
8618
87-
If `m : M` and `h : r (μ m n₁) (μ m n₂)`, then `covariant_class.covc m h : r n₁ n₂`.
19+
## Remark
20+
No monoid is actually present in this file: all assumptions have been generalized to `has_mul` or
21+
`mul_one_class`.
8822
-/
89-
class contravariant_class :=
90-
(covtc : contravariant M N μ s)
91-
92-
end variants
9323

9424
variables {α : Type*} {a b c d : α}
9525

0 commit comments

Comments
 (0)