Skip to content

Commit ffeb1cb

Browse files
Re-organisation of algebra operation definitions (#1387)
1 parent f9a53e8 commit ffeb1cb

File tree

18 files changed

+410
-227
lines changed

18 files changed

+410
-227
lines changed

CHANGELOG.md

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -193,34 +193,31 @@ New modules
193193
`Data.Unit.Polymorphic.Instances`, `Data.Vec.Instances`,
194194
`Data.Word.Instances`, and `Reflection.Instances`.
195195

196-
* Generic divisibility over algebraic structures
196+
* Generic definitions over algebraic structures (divisibility, multiplication etc.):
197197
```
198-
Algebra.Divisibility
199-
Algebra.GCD
200-
Algebra.Primality
201-
Algebra.Properties.Magma.Divisibility
202-
Algebra.Properties.Semigroup.Divisibility
203-
Algebra.Properties.Monoid.Divisibility
204-
Algebra.Properties.CommutativeSemigroup.Divisibility
205-
Algebra.Properties.Semiring.Divisibility
206-
Algebra.Properties.Semiring.GCD
198+
Algebra.Definitions.RawMagma
199+
Algebra.Definitions.RawMonoid
200+
Algebra.Definitions.RawSemiring
207201
```
208202

209-
* Generic summation over algebraic structures
203+
* Properties of generic definitions over algebraic structures (divisibility, multiplication etc.):
210204
```
205+
Algebra.Properties.Magma.Divisibility
206+
207+
Algebra.Properties.Semigroup.Divisibility
208+
209+
Algebra.Properties.CommutativeSemigroup.Divisibility
210+
211211
Algebra.Properties.Monoid.Summation
212-
Algebra.Properties.CommutativeMonoid.Summation
213-
```
214-
215-
* Generic multiplication over algebraic structures
216-
```
217212
Algebra.Properties.Monoid.Multiplication
213+
Algebra.Properties.Monoid.Divisibility
214+
215+
Algebra.Properties.CommutativeMonoid.Summation
216+
218217
Algebra.Properties.Semiring.Multiplication
219-
```
220-
221-
* Generic exponentiation over algebraic structures
222-
```
223218
Algebra.Properties.Semiring.Exponentiation
219+
Algebra.Properties.Semiring.GCD
220+
Algebra.Properties.Semiring.Divisibility
224221
```
225222

226223
* Setoid equality over vectors:

src/Algebra/Bundles.agda

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ open import Algebra.Core
1515
open import Algebra.Structures
1616
open import Relation.Binary
1717
open import Function.Base
18+
import Relation.Nullary as N
1819
open import Level
1920

2021
------------------------------------------------------------------------
@@ -29,6 +30,10 @@ record RawMagma c ℓ : Set (suc (c ⊔ ℓ)) where
2930
_≈_ : Rel Carrier ℓ
3031
_∙_ : Op₂ Carrier
3132

33+
infix 4 _≉_
34+
_≉_ : Rel Carrier _
35+
x ≉ y = N.¬ (x ≈ y)
36+
3237

3338
record Magma c ℓ : Set (suc (c ⊔ ℓ)) where
3439
infixl 7 _∙_
@@ -44,7 +49,7 @@ record Magma c ℓ : Set (suc (c ⊔ ℓ)) where
4449
rawMagma : RawMagma _ _
4550
rawMagma = record { _≈_ = _≈_; _∙_ = _∙_ }
4651

47-
open Setoid setoid public
52+
open RawMagma rawMagma public
4853
using (_≉_)
4954

5055

@@ -178,6 +183,9 @@ record RawMonoid c ℓ : Set (suc (c ⊔ ℓ)) where
178183
; _∙_ = _∙_
179184
}
180185

186+
open RawMagma rawMagma public
187+
using (_≉_)
188+
181189

182190
record Monoid c ℓ : Set (suc (c ⊔ ℓ)) where
183191
infixl 7 _∙_
@@ -281,7 +289,7 @@ record RawGroup c ℓ : Set (suc (c ⊔ ℓ)) where
281289
}
282290

283291
open RawMonoid rawMonoid public
284-
using (rawMagma)
292+
using (_≉_; rawMagma)
285293

286294

287295
record Group c ℓ : Set (suc (c ⊔ ℓ)) where
@@ -354,6 +362,9 @@ record RawLattice c ℓ : Set (suc (c ⊔ ℓ)) where
354362
∧-rawMagma : RawMagma c ℓ
355363
∧-rawMagma = record { _≈_ = _≈_; _∙_ = _∧_ }
356364

365+
open RawMagma ∨-rawMagma public
366+
using (_≉_)
367+
357368

358369
record Lattice c ℓ : Set (suc (c ⊔ ℓ)) where
359370
infixr 7 _∧_
@@ -428,7 +439,7 @@ record RawNearSemiring c ℓ : Set (suc (c ⊔ ℓ)) where
428439
}
429440

430441
open RawMonoid +-rawMonoid public
431-
using () renaming (rawMagma to +-rawMagma)
442+
using (_≉_) renaming (rawMagma to +-rawMagma)
432443

433444
*-rawMagma : RawMagma c ℓ
434445
*-rawMagma = record
@@ -568,7 +579,7 @@ record RawSemiring c ℓ : Set (suc (c ⊔ ℓ)) where
568579
}
569580

570581
open RawNearSemiring rawNearSemiring public
571-
using (+-rawMonoid; +-rawMagma; *-rawMagma)
582+
using (_≉_; +-rawMonoid; +-rawMagma; *-rawMagma)
572583

573584
*-rawMonoid : RawMonoid c ℓ
574585
*-rawMonoid = record

src/Algebra/Divisibility.agda renamed to src/Algebra/Definitions/RawMagma.agda

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
------------------------------------------------------------------------
22
-- The Agda standard library
33
--
4-
-- Definition of divisibility
4+
-- Basic auxiliary definitions for magma-like structures
55
------------------------------------------------------------------------
66

77
-- You're unlikely to want to use this module directly. Instead you
@@ -10,16 +10,17 @@
1010

1111
{-# OPTIONS --without-K --safe #-}
1212

13-
open import Algebra.Core
13+
open import Algebra.Bundles using (RawMagma)
1414
open import Data.Product using (∃; _×_; _,_)
1515
open import Level using (_⊔_)
1616
open import Relation.Binary
1717
open import Relation.Nullary using (¬_)
1818

19-
module Algebra.Divisibility
20-
{a ℓ} {A : Set a} (_≈_ : Rel A ℓ) (_∙_ : Op₂ A)
19+
module Algebra.Definitions.RawMagma
20+
{a ℓ} (M : RawMagma a ℓ)
2121
where
2222

23+
open RawMagma M renaming (Carrier to A)
2324
open import Algebra.Definitions _≈_
2425

2526
------------------------------------------------------------------------
@@ -65,35 +66,3 @@ x ∣∣ y = x ∣ y × y ∣ x
6566

6667
_∤∤_ : Rel A (a ⊔ ℓ)
6768
x ∤∤ y = ¬ x ∣∣ y
68-
69-
------------------------------------------------------------------------------
70-
-- Greatest common divisor (GCD)
71-
72-
record IsGCD (x y gcd : A) : Set (a ⊔ ℓ) where
73-
constructor gcdᶜ
74-
field
75-
divides₁ : gcd ∣ x
76-
divides₂ : gcd ∣ y
77-
greatest : {z} z ∣ x z ∣ y z ∣ gcd
78-
79-
------------------------------------------------------------------------
80-
-- Properties
81-
82-
∣-refl : {ε} LeftIdentity ε _∙_ Reflexive _∣_
83-
∣-refl {ε} idˡ {x} = ε , idˡ x
84-
85-
∣-reflexive : Transitive _≈_ {ε} LeftIdentity ε _∙_ _≈_ ⇒ _∣_
86-
∣-reflexive trans {ε} idˡ x≈y = ε , trans (idˡ _) x≈y
87-
88-
∣-trans : Transitive _≈_ LeftCongruent _∙_ Associative _∙_ Transitive _∣_
89-
∣-trans trans congˡ assoc {x} {y} {z} (p , px≈y) (q , qy≈z) =
90-
q ∙ p , trans (assoc q p x) (trans (congˡ px≈y) qy≈z)
91-
92-
∣-respʳ : Transitive _≈_ _∣_ Respectsʳ _≈_
93-
∣-respʳ trans y≈z (q , qx≈y) = q , trans qx≈y y≈z
94-
95-
∣-respˡ : Symmetric _≈_ Transitive _≈_ LeftCongruent _∙_ _∣_ Respectsˡ _≈_
96-
∣-respˡ sym trans congˡ x≈z (q , qx≈y) = q , trans (congˡ (sym x≈z)) qx≈y
97-
98-
∣-resp : Symmetric _≈_ Transitive _≈_ LeftCongruent _∙_ _∣_ Respects₂ _≈_
99-
∣-resp sym trans cong = ∣-respʳ trans , ∣-respˡ sym trans cong
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Basic auxiliary definitions for monoid-like structures
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --without-K --safe #-}
8+
9+
open import Algebra.Bundles using (RawMonoid)
10+
open import Data.Nat.Base as ℕ using (ℕ; zero; suc)
11+
open import Data.Vec.Functional as Vector using (Vector)
12+
13+
module Algebra.Definitions.RawMonoid {a ℓ} (M : RawMonoid a ℓ) where
14+
15+
open RawMonoid M renaming ( _∙_ to _+_ ; ε to 0# )
16+
17+
------------------------------------------------------------------------
18+
-- Re-export definitions over a magma
19+
------------------------------------------------------------------------
20+
21+
open import Algebra.Definitions.RawMagma rawMagma public
22+
23+
------------------------------------------------------------------------
24+
-- Multiplication by natural number
25+
------------------------------------------------------------------------
26+
-- Standard definition
27+
28+
-- A simple definition, easy to use and prove properties about.
29+
30+
infixr 8 _×_
31+
32+
_×_ : Carrier Carrier
33+
0 × x = 0#
34+
suc n × x = x + (n × x)
35+
36+
------------------------------------------------------------------------
37+
-- Type-checking optimised definition
38+
39+
-- For use in code where high performance at type-checking time is
40+
-- important, e.g. solvers and tactics. Firstly it avoids unnecessarily
41+
-- multiplying by the unit if possible, speeding up type-checking and
42+
-- makes for much more readable proofs:
43+
--
44+
-- Standard definition: x * 2 = x + x + 0#
45+
-- Optimised definition: x * 2 = x + x
46+
--
47+
-- Secondly, associates to the left which, counterintuitive as it may
48+
-- seem, also speeds up typechecking.
49+
--
50+
-- Standard definition: x * 3 = x + (x + (x + 0#))
51+
-- Our definition: x * 3 = (x + x) + x
52+
53+
infixl 8 _×′_
54+
55+
_×′_ : Carrier Carrier
56+
0 ×′ x = 0#
57+
1 ×′ x = x
58+
suc n ×′ x = x + n ×′ x
59+
60+
------------------------------------------------------------------------
61+
-- Summation
62+
------------------------------------------------------------------------
63+
64+
sum : {n} Vector Carrier n Carrier
65+
sum = Vector.foldr _+_ 0#
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Basic auxiliary definitions for semiring-like structures
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --without-K --safe #-}
8+
9+
open import Algebra.Bundles using (RawSemiring)
10+
open import Data.Sum.Base using (_⊎_)
11+
open import Level using (_⊔_)
12+
open import Relation.Binary.Core using (Rel)
13+
14+
module Algebra.Definitions.RawSemiring {a ℓ} (M : RawSemiring a ℓ) where
15+
16+
open RawSemiring M renaming (Carrier to A)
17+
18+
------------------------------------------------------------------------
19+
-- Definitions over _+_
20+
21+
open import Algebra.Definitions.RawMonoid +-rawMonoid public
22+
using
23+
( _×_
24+
; _×′_
25+
; sum
26+
)
27+
28+
------------------------------------------------------------------------
29+
-- Definitions over _*_
30+
31+
open import Algebra.Definitions.RawMonoid *-rawMonoid public
32+
using
33+
( _∣_
34+
; _∤_
35+
)
36+
renaming
37+
( sum to product
38+
)
39+
40+
------------------------------------------------------------------------
41+
-- Coprimality
42+
43+
Coprime : Rel A (a ⊔ ℓ)
44+
Coprime x y = {z} z ∣ x z ∣ y z ∣ 1#
45+
46+
record Irreducible (p : A) : Set (a ⊔ ℓ) where
47+
constructor mkIrred
48+
field
49+
p∤1 : p ∤ 1#
50+
split-∣1 : {x y} p ≈ (x * y) x ∣ 1# ⊎ y ∣ 1#
51+
52+
record Prime (p : A) : Set (a ⊔ ℓ) where
53+
constructor mkPrime
54+
field
55+
p≉0 : p ≉ 0#
56+
split-∣ : {x y} p ∣ x * y p ∣ x ⊎ p ∣ y
57+
58+
------------------------------------------------------------------------
59+
-- Greatest common divisor
60+
61+
record IsGCD (x y gcd : A) : Set (a ⊔ ℓ) where
62+
constructor gcdᶜ
63+
field
64+
divides₁ : gcd ∣ x
65+
divides₂ : gcd ∣ y
66+
greatest : {z} z ∣ x z ∣ y z ∣ gcd
67+

0 commit comments

Comments
 (0)