Skip to content

Commit c0b6044

Browse files
Make Cancellative be more consistent in whole library (#1823)
1 parent 5c27f90 commit c0b6044

File tree

19 files changed

+111
-73
lines changed

19 files changed

+111
-73
lines changed

CHANGELOG.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,38 @@ Non-backwards compatible changes
366366
3. Finally, if the above approaches are not viable then you may be forced to explicitly
367367
use `cong` combined with a lemma that proves the old reduction behaviour.
368368
369+
### Change to the definition of `LeftCancellative` and `RightCancellative`
370+
371+
* The definitions of the types for cancellativity in `Algebra.Definitions` previously
372+
made some of their arguments implicit. This was under the assumption that the operators were
373+
defined by pattern matching on the left argument so that Agda could always infer the
374+
argument on the RHS.
375+
376+
* Although many of the operators defined in the library follow this convention, this is not
377+
always true and cannot be assumed in user's code.
378+
379+
* Therefore the definitions have been changed as follows to make all their arguments explicit:
380+
- `LeftCancellative _•_`
381+
- From: `∀ x {y z} → (x • y) ≈ (x • z) → y ≈ z`
382+
- To: `∀ x y z → (x • y) ≈ (x • z) → y ≈ z`
383+
384+
- `RightCancellative _•_`
385+
- From: `∀ {x} y z → (y • x) ≈ (z • x) → y ≈ z`
386+
- To: `∀ x y z → (y • x) ≈ (z • x) → y ≈ z`
387+
388+
- `AlmostLeftCancellative e _•_`
389+
- From: `∀ {x} y z → ¬ x ≈ e → (x • y) ≈ (x • z) → y ≈ z`
390+
- To: `∀ x y z → ¬ x ≈ e → (x • y) ≈ (x • z) → y ≈ z`
391+
392+
- `AlmostRightCancellative e _•_`
393+
- From: `∀ {x} y z → ¬ x ≈ e → (y • x) ≈ (z • x) → y ≈ z`
394+
- To: `∀ x y z → ¬ x ≈ e → (y • x) ≈ (z • x) → y ≈ z`
395+
396+
* Correspondingly some proofs of the above types will need additional arguments passed explicitly.
397+
Instances can easily be fixed by adding additional underscores, e.g.
398+
- `∙-cancelˡ x` to `∙-cancelˡ x _ _`
399+
- `∙-cancelʳ y z` to `∙-cancelʳ _ y z`
400+
369401
### Change in the definition of `Prime`
370402
371403
* The definition of `Prime` in `Data.Nat.Primality` was:

src/Algebra/Consequences/Setoid.agda

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ open import Algebra.Consequences.Base public
3434
module _ {_•_ : Op₂ A} (comm : Commutative _•_) where
3535

3636
comm+cancelˡ⇒cancelʳ : LeftCancellative _•_ RightCancellative _•_
37-
comm+cancelˡ⇒cancelʳ cancelˡ {x} y z eq = cancelˡ x $ begin
37+
comm+cancelˡ⇒cancelʳ cancelˡ x y z eq = cancelˡ x y z $ begin
3838
x • y ≈⟨ comm x y ⟩
3939
y • x ≈⟨ eq ⟩
4040
z • x ≈⟨ comm z x ⟩
4141
x • z ∎
4242

4343
comm+cancelʳ⇒cancelˡ : RightCancellative _•_ LeftCancellative _•_
44-
comm+cancelʳ⇒cancelˡ cancelʳ x {y} {z} eq = cancelʳ y z $ begin
44+
comm+cancelʳ⇒cancelˡ cancelʳ x y z eq = cancelʳ x y z $ begin
4545
y • x ≈⟨ comm y x ⟩
4646
x • y ≈⟨ eq ⟩
4747
x • z ≈⟨ comm x z ⟩
@@ -90,17 +90,17 @@ module _ {_•_ : Op₂ A} (comm : Commutative _•_) {e : A} where
9090

9191
comm+almostCancelˡ⇒almostCancelʳ : AlmostLeftCancellative e _•_
9292
AlmostRightCancellative e _•_
93-
comm+almostCancelˡ⇒almostCancelʳ cancelˡ-nonZero {x} y z x≉e yx≈zx =
94-
cancelˡ-nonZero y z x≉e $ begin
93+
comm+almostCancelˡ⇒almostCancelʳ cancelˡ-nonZero x y z x≉e yx≈zx =
94+
cancelˡ-nonZero x y z x≉e $ begin
9595
x • y ≈⟨ comm x y ⟩
9696
y • x ≈⟨ yx≈zx ⟩
9797
z • x ≈⟨ comm z x ⟩
9898
x • z ∎
9999

100100
comm+almostCancelʳ⇒almostCancelˡ : AlmostRightCancellative e _•_
101101
AlmostLeftCancellative e _•_
102-
comm+almostCancelʳ⇒almostCancelˡ cancelʳ-nonZero {x} y z x≉e xy≈xz =
103-
cancelʳ-nonZero y z x≉e $ begin
102+
comm+almostCancelʳ⇒almostCancelˡ cancelʳ-nonZero x y z x≉e xy≈xz =
103+
cancelʳ-nonZero x y z x≉e $ begin
104104
y • x ≈⟨ comm y x ⟩
105105
x • y ≈⟨ xy≈xz ⟩
106106
x • z ≈⟨ comm x z ⟩

src/Algebra/Definitions.agda

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
-- The contents of this module should be accessed via `Algebra`, unless
88
-- you want to parameterise it via the equality relation.
99

10+
-- Note that very few of the element arguments are made implicit here,
11+
-- as we do not assume that the Agda can infer either the right or left
12+
-- argument of the binary operators. This is despite the fact that the
13+
-- library defines most of its concrete operators (e.g. in
14+
-- `Data.Nat.Base`) as being left-biased.
15+
1016
{-# OPTIONS --without-K --safe #-}
1117

1218
open import Relation.Binary.Core
@@ -124,19 +130,19 @@ Involutive : Op₁ A → Set _
124130
Involutive f = x f (f x) ≈ x
125131

126132
LeftCancellative : Op₂ A Set _
127-
LeftCancellative _•_ = x {y z} (x • y) ≈ (x • z) y ≈ z
133+
LeftCancellative _•_ = x y z (x • y) ≈ (x • z) y ≈ z
128134

129135
RightCancellative : Op₂ A Set _
130-
RightCancellative _•_ = {x} y z (y • x) ≈ (z • x) y ≈ z
136+
RightCancellative _•_ = x y z (y • x) ≈ (z • x) y ≈ z
131137

132138
Cancellative : Op₂ A Set _
133139
Cancellative _•_ = (LeftCancellative _•_) × (RightCancellative _•_)
134140

135141
AlmostLeftCancellative : A Op₂ A Set _
136-
AlmostLeftCancellative e _•_ = {x} y z ¬ x ≈ e (x • y) ≈ (x • z) y ≈ z
142+
AlmostLeftCancellative e _•_ = x y z ¬ x ≈ e (x • y) ≈ (x • z) y ≈ z
137143

138144
AlmostRightCancellative : A Op₂ A Set _
139-
AlmostRightCancellative e _•_ = {x} y z ¬ x ≈ e (y • x) ≈ (z • x) y ≈ z
145+
AlmostRightCancellative e _•_ = x y z ¬ x ≈ e (y • x) ≈ (z • x) y ≈ z
140146

141147
AlmostCancellative : A Op₂ A Set _
142148
AlmostCancellative e _•_ = AlmostLeftCancellative e _•_ × AlmostRightCancellative e _•_

src/Algebra/Morphism/MagmaMonomorphism.agda

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,14 @@ module _ (◦-isMagma : IsMagma _≈₂_ _◦_) where
7979
⟦ y ⟧ ∎))
8080

8181
cancelˡ : LeftCancellative _≈₂_ _◦_ LeftCancellative _≈₁_ _∙_
82-
cancelˡ cancelˡ x {y} {z} x∙y≈x∙z = injective (cancelˡ ⟦ x ⟧ (begin
82+
cancelˡ cancelˡ x y z x∙y≈x∙z = injective (cancelˡ ⟦ x ⟧ ⟦ y ⟧ ⟦ z ⟧ (begin
8383
⟦ x ⟧ ◦ ⟦ y ⟧ ≈˘⟨ homo x y ⟩
8484
⟦ x ∙ y ⟧ ≈⟨ ⟦⟧-cong x∙y≈x∙z ⟩
8585
⟦ x ∙ z ⟧ ≈⟨ homo x z ⟩
8686
⟦ x ⟧ ◦ ⟦ z ⟧ ∎))
8787

8888
cancelʳ : RightCancellative _≈₂_ _◦_ RightCancellative _≈₁_ _∙_
89-
cancelʳ cancelʳ {x} y z y∙x≈z∙x = injective (cancelʳ ⟦ y ⟧ ⟦ z ⟧ (begin
89+
cancelʳ cancelʳ x y z y∙x≈z∙x = injective (cancelʳ ⟦ x ⟧ ⟦ y ⟧ ⟦ z ⟧ (begin
9090
⟦ y ⟧ ◦ ⟦ x ⟧ ≈˘⟨ homo y x ⟩
9191
⟦ y ∙ x ⟧ ≈⟨ ⟦⟧-cong y∙x≈z∙x ⟩
9292
⟦ z ∙ x ⟧ ≈⟨ homo z x ⟩

src/Algebra/Properties/CancellativeCommutativeSemiring.agda

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ xy≈0⇒x≈0∨y≈0 _≟_ {x} {y} xy≈0 with x ≟ 0# | y ≟ 0#
3131
... | no x≉0 | no y≉0 = contradiction y≈0 y≉0
3232
where
3333
xy≈x*0 = trans xy≈0 (sym (zeroʳ x))
34-
y≈0 = *-cancelˡ-nonZero y 0# x≉0 xy≈x*0
34+
y≈0 = *-cancelˡ-nonZero _ y 0# x≉0 xy≈x*0
3535

3636
x≉0∧y≉0⇒xy≉0 : Decidable _≈_ {x y} x ≉ 0# y ≉ 0# x * y ≉ 0#
3737
x≉0∧y≉0⇒xy≉0 _≟_ x≉0 y≉0 xy≈0 with xy≈0⇒x≈0∨y≈0 _≟_ xy≈0

src/Algebra/Properties/Group.agda

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ private
3939
x ⁻¹ ∙ (x ∙ y) ∎
4040

4141
∙-cancelˡ : LeftCancellative _∙_
42-
∙-cancelˡ x {y} {z} eq = begin
42+
∙-cancelˡ x y z eq = begin
4343
y ≈⟨ right-helper x y ⟩
4444
x ⁻¹ ∙ (x ∙ y) ≈⟨ ∙-congˡ eq ⟩
4545
x ⁻¹ ∙ (x ∙ z) ≈˘⟨ right-helper x z ⟩
4646
z ∎
4747

4848
∙-cancelʳ : RightCancellative _∙_
49-
∙-cancelʳ {x} y z eq = begin
49+
∙-cancelʳ x y z eq = begin
5050
y ≈⟨ left-helper y x ⟩
5151
y ∙ x ∙ x ⁻¹ ≈⟨ ∙-congʳ eq ⟩
5252
z ∙ x ∙ x ⁻¹ ≈˘⟨ left-helper z x ⟩
@@ -63,14 +63,14 @@ private
6363
x ∎
6464

6565
⁻¹-injective : {x y} x ⁻¹ ≈ y ⁻¹ x ≈ y
66-
⁻¹-injective {x} {y} eq = ∙-cancelʳ x y ( begin
66+
⁻¹-injective {x} {y} eq = ∙-cancelʳ _ _ _ ( begin
6767
x ∙ x ⁻¹ ≈⟨ inverseʳ x ⟩
6868
ε ≈˘⟨ inverseʳ y ⟩
6969
y ∙ y ⁻¹ ≈˘⟨ ∙-congˡ eq ⟩
7070
y ∙ x ⁻¹ ∎ )
7171

7272
⁻¹-anti-homo-∙ : x y (x ∙ y) ⁻¹ ≈ y ⁻¹ ∙ x ⁻¹
73-
⁻¹-anti-homo-∙ x y = ∙-cancelˡ _ ( begin
73+
⁻¹-anti-homo-∙ x y = ∙-cancelˡ _ _ _ ( begin
7474
x ∙ y ∙ (x ∙ y) ⁻¹ ≈⟨ inverseʳ _ ⟩
7575
ε ≈˘⟨ inverseʳ _ ⟩
7676
x ∙ x ⁻¹ ≈⟨ ∙-congʳ (left-helper x y) ⟩

src/Algebra/Properties/Quasigroup.agda

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ open import Relation.Binary.Reasoning.Setoid setoid
1616
open import Data.Product
1717

1818
cancelˡ : LeftCancellative _∙_
19-
cancelˡ x {y} {z} eq = begin
19+
cancelˡ x y z eq = begin
2020
y ≈⟨ sym( leftDividesʳ x y) ⟩
2121
x \\ (x ∙ y) ≈⟨ \\-congˡ eq ⟩
2222
x \\ (x ∙ z) ≈⟨ leftDividesʳ x z ⟩
2323
z ∎
2424

2525
cancelʳ : RightCancellative _∙_
26-
cancelʳ {x} y z eq = begin
26+
cancelʳ x y z eq = begin
2727
y ≈⟨ sym( rightDividesʳ x y) ⟩
2828
(y ∙ x) // x ≈⟨ //-congʳ eq ⟩
2929
(z ∙ x) // x ≈⟨ rightDividesʳ x z ⟩

src/Data/Fin/Properties.agda

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ combine-injectiveˡ i j k l cᵢⱼ≡cₖₗ with <-cmp i k
654654
combine-injectiveʳ : (i : Fin m) (j : Fin n) (k : Fin m) (l : Fin n)
655655
combine i j ≡ combine k l j ≡ l
656656
combine-injectiveʳ {m} {n} i j k l cᵢⱼ≡cₖₗ with combine-injectiveˡ i j k l cᵢⱼ≡cₖₗ
657-
... | refl = toℕ-injective (ℕₚ.+-cancelˡ-≡ (n ℕ.* toℕ i) (begin
657+
... | refl = toℕ-injective (ℕₚ.+-cancelˡ-≡ (n ℕ.* toℕ i) _ _ (begin
658658
n ℕ.* toℕ i ℕ.+ toℕ j ≡˘⟨ toℕ-combine i j ⟩
659659
toℕ (combine i j) ≡⟨ cong toℕ cᵢⱼ≡cₖₗ ⟩
660660
toℕ (combine i l) ≡⟨ toℕ-combine i l ⟩

src/Data/Integer/Properties.agda

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,8 +1599,8 @@ abs-*-commute i j = abs-◃ _ _
15991599
*-cancelʳ-≡ : i j k .{{_ : NonZero k}} i * k ≡ j * k i ≡ j
16001600
*-cancelʳ-≡ i j k eq with sign-cong′ eq
16011601
... | inj₁ s[ik]≡s[jk] = ◃-cong
1602-
(𝕊ₚ.*-cancelʳ-≡ {sign k} (sign i) (sign j) s[ik]≡s[jk])
1603-
(ℕ.*-cancelʳ-≡ ∣ i ∣ ∣ j ∣ (abs-cong eq))
1602+
(𝕊ₚ.*-cancelʳ-≡ (sign k) (sign i) (sign j) s[ik]≡s[jk])
1603+
(ℕ.*-cancelʳ-≡ ∣ i ∣ ∣ j ∣ _ (abs-cong eq))
16041604
... | inj₂ (∣ik∣≡0 , ∣jk∣≡0) = trans
16051605
(∣i∣≡0⇒i≡0 (ℕ.m*n≡0⇒m≡0 _ _ ∣ik∣≡0))
16061606
(sym (∣i∣≡0⇒i≡0 (ℕ.m*n≡0⇒m≡0 _ _ ∣jk∣≡0)))
@@ -1785,10 +1785,10 @@ neg-distribʳ-* i j = begin
17851785
*-monoʳ-<-pos i {j} {k} rewrite *-comm j i | *-comm k i = *-monoˡ-<-pos i
17861786

17871787
*-cancelˡ-<-nonNeg : k .{{_ : NonNegative k}} k * i < k * j i < j
1788-
*-cancelˡ-<-nonNeg {+ i} {+ j} (+ n) leq = +<+ (ℕ.*-cancelˡ-< n (+◃-cancel-< leq))
1788+
*-cancelˡ-<-nonNeg {+ i} {+ j} (+ n) leq = +<+ (ℕ.*-cancelˡ-< n _ _ (+◃-cancel-< leq))
17891789
*-cancelˡ-<-nonNeg {+ i} { -[1+ j ]} (+ n) leq = contradiction leq +◃≮-◃
17901790
*-cancelˡ-<-nonNeg { -[1+ i ]} {+ j} (+ n)leq = -<+
1791-
*-cancelˡ-<-nonNeg { -[1+ i ]} { -[1+ j ]} (+ n) leq = -<- (ℕ.≤-pred (ℕ.*-cancelˡ-< n (neg◃-cancel-< leq)))
1791+
*-cancelˡ-<-nonNeg { -[1+ i ]} { -[1+ j ]} (+ n) leq = -<- (ℕ.≤-pred (ℕ.*-cancelˡ-< n _ _ (neg◃-cancel-< leq)))
17921792

17931793
*-cancelʳ-<-nonNeg : k .{{_ : NonNegative k}} i * k < j * k i < j
17941794
*-cancelʳ-<-nonNeg {i} {j} k rewrite *-comm i k | *-comm j k = *-cancelˡ-<-nonNeg k

src/Data/List/Properties.agda

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -183,18 +183,18 @@ module _ {A : Set a} where
183183
++-identityˡ-unique {xs = x ∷ xs} (y ∷ [] ) eq | ()
184184
++-identityˡ-unique {xs = x ∷ xs} (y ∷ _ ∷ _) eq | ()
185185

186-
++-cancelˡ : xs {ys zs : List A} xs ++ ys ≡ xs ++ zs ys ≡ zs
187-
++-cancelˡ [] ys≡zs = ys≡zs
188-
++-cancelˡ (x ∷ xs) x∷xs++ys≡x∷xs++zs = ++-cancelˡ xs (∷-injectiveʳ x∷xs++ys≡x∷xs++zs)
186+
++-cancelˡ : LeftCancellative _++_
187+
++-cancelˡ [] _ _ ys≡zs = ys≡zs
188+
++-cancelˡ (x ∷ xs) _ _ x∷xs++ys≡x∷xs++zs = ++-cancelˡ xs _ _ (∷-injectiveʳ x∷xs++ys≡x∷xs++zs)
189189

190-
++-cancelʳ : {xs : List A} ys zs ys ++ xs ≡ zs ++ xs ys ≡ zs
191-
++-cancelʳ {_} [] [] _ = refl
192-
++-cancelʳ {xs} [] (z ∷ zs) eq =
190+
++-cancelʳ : RightCancellative _++_
191+
++-cancelʳ _ [] [] _ = refl
192+
++-cancelʳ xs [] (z ∷ zs) eq =
193193
contradiction (trans (cong length eq) (length-++ (z ∷ zs))) (m≢1+n+m (length xs))
194-
++-cancelʳ {xs} (y ∷ ys) [] eq =
194+
++-cancelʳ xs (y ∷ ys) [] eq =
195195
contradiction (trans (sym (length-++ (y ∷ ys))) (cong length eq)) (m≢1+n+m (length xs) ∘ sym)
196-
++-cancelʳ {_} (y ∷ ys) (z ∷ zs) eq =
197-
cong₂ _∷_ (∷-injectiveˡ eq) (++-cancelʳ ys zs (∷-injectiveʳ eq))
196+
++-cancelʳ _ (y ∷ ys) (z ∷ zs) eq =
197+
cong₂ _∷_ (∷-injectiveˡ eq) (++-cancelʳ _ ys zs (∷-injectiveʳ eq))
198198

199199
++-cancel : Cancellative _++_
200200
++-cancel = ++-cancelˡ , ++-cancelʳ

0 commit comments

Comments
 (0)