Skip to content

Commit f9a53e8

Browse files
authored
Definitions of GCD and Prime over semirings (#1345)
1 parent 651a6b6 commit f9a53e8

File tree

11 files changed

+213
-19
lines changed

11 files changed

+213
-19
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,14 @@ New modules
196196
* Generic divisibility over algebraic structures
197197
```
198198
Algebra.Divisibility
199+
Algebra.GCD
200+
Algebra.Primality
199201
Algebra.Properties.Magma.Divisibility
200202
Algebra.Properties.Semigroup.Divisibility
201203
Algebra.Properties.Monoid.Divisibility
202204
Algebra.Properties.CommutativeSemigroup.Divisibility
205+
Algebra.Properties.Semiring.Divisibility
206+
Algebra.Properties.Semiring.GCD
203207
```
204208

205209
* Generic summation over algebraic structures
@@ -554,6 +558,11 @@ Other minor additions
554558
*-distrib-⊓ : _*_ DistributesOver _⊓_
555559
```
556560

561+
* Added new operation in `Data.Sum`:
562+
```agda
563+
reduce : A ⊎ A → A
564+
```
565+
557566
* Added new proofs in `Data.Sign.Properties`:
558567
```agda
559568
s*opposite[s]≡- : ∀ s → s * opposite s ≡ -

src/Algebra/Bundles.agda

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,8 @@ record CancellativeCommutativeSemiring c ℓ : Set (suc (c ⊔ ℓ)) where
748748
; nearSemiring; semiringWithoutOne
749749
; semiringWithoutAnnihilatingZero
750750
; rawSemiring
751+
; semiring
752+
; _≉_
751753
)
752754

753755
------------------------------------------------------------------------

src/Algebra/Divisibility.agda

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,58 @@ module Algebra.Divisibility
2323
open import Algebra.Definitions _≈_
2424

2525
------------------------------------------------------------------------
26-
-- Definitions
27-
2826
-- Divisibility
2927

30-
infix 5 _∣_ _∤_ _∣∣_ _¬∣∣_
28+
infix 5 _∣ˡ_ _∤ˡ_ _∣ʳ_ _∤ʳ_ _∣_ _∤_
29+
30+
-- Divisibility from the left
31+
32+
_∣ˡ_ : Rel A (a ⊔ ℓ)
33+
x ∣ˡ y =λ q (x ∙ q) ≈ y
34+
35+
_∤ˡ_ : Rel A (a ⊔ ℓ)
36+
x ∤ˡ y = ¬ x ∣ˡ y
37+
38+
-- Divisibility from the right
39+
40+
_∣ʳ_ : Rel A (a ⊔ ℓ)
41+
x ∣ʳ y =λ q (q ∙ x) ≈ y
42+
43+
_∤ʳ_ : Rel A (a ⊔ ℓ)
44+
x ∤ʳ y = ¬ x ∣ʳ y
45+
46+
-- _∣ˡ_ and _∣ʳ_ are only equivalent in the commutative case. In this
47+
-- case we take the right definition to be the primary one.
3148

3249
_∣_ : Rel A (a ⊔ ℓ)
33-
x ∣ y =λ q (q ∙ x) ≈ y
50+
_∣_ = _∣ʳ_
3451

3552
_∤_ : Rel A (a ⊔ ℓ)
3653
x ∤ y = ¬ x ∣ y
3754

38-
-- Mutual divisibility.
55+
------------------------------------------------------------------------
56+
-- Mutual divisibility
3957

4058
-- When in a cancellative monoid, elements related by _∣∣_ are called
4159
-- associated and if x ∣∣ y then x and y differ by an invertible factor.
4260

61+
infix 5 _∣∣_ _∤∤_
62+
4363
_∣∣_ : Rel A (a ⊔ ℓ)
4464
x ∣∣ y = x ∣ y × y ∣ x
4565

46-
_¬∣∣_ : Rel A (a ⊔ ℓ)
47-
x ¬∣∣ y = ¬ x ∣∣ y
66+
_∤∤_ : Rel A (a ⊔ ℓ)
67+
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
4878

4979
------------------------------------------------------------------------
5080
-- Properties
@@ -67,9 +97,3 @@ x ¬∣∣ y = ¬ x ∣∣ y
6797

6898
∣-resp : Symmetric _≈_ Transitive _≈_ LeftCongruent _∙_ _∣_ Respects₂ _≈_
6999
∣-resp sym trans cong = ∣-respʳ trans , ∣-respˡ sym trans cong
70-
71-
∣-min : {ε} RightIdentity ε _∙_ Minimum _∣_ ε
72-
∣-min {ε} idʳ x = x , idʳ x
73-
74-
∣-max : {ε} LeftZero ε _∙_ Maximum _∣_ ε
75-
∣-max {ε} zeˡ x = ε , zeˡ x

src/Algebra/Primality.agda

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Definitions of irreducibility, primality, coprimality.
5+
------------------------------------------------------------------------
6+
7+
-- You're unlikely to want to use this module directly. Instead you
8+
-- probably want to be importing the appropriate module from e.g.
9+
-- `Algebra.Properties.Semiring.Primality`.
10+
11+
{-# OPTIONS --without-K --safe #-}
12+
13+
open import Algebra.Core using (Op₂)
14+
open import Data.Sum using (_⊎_)
15+
open import Function using (flip)
16+
open import Level using (_⊔_)
17+
open import Relation.Binary using (Rel; Symmetric)
18+
open import Relation.Nullary using (¬_)
19+
20+
module Algebra.Primality
21+
{a ℓ} {A : Set a} (_≈_ : Rel A ℓ) (_*_ : Op₂ A) (0# 1# : A)
22+
where
23+
24+
open import Algebra.Divisibility _≈_ _*_
25+
26+
------------------------------------------------------------------------------
27+
-- Definitions
28+
29+
Coprime : Rel A (a ⊔ ℓ)
30+
Coprime x y = {z} z ∣ x z ∣ y z ∣ 1#
31+
32+
record Irreducible (p : A) : Set (a ⊔ ℓ) where
33+
constructor mkIrred
34+
field
35+
p∤1 : p ∤ 1#
36+
split-∣1 : {x y} p ≈ (x * y) x ∣ 1# ⊎ y ∣ 1#
37+
38+
record Prime (p : A) : Set (a ⊔ ℓ) where
39+
constructor mkPrime
40+
field
41+
p≉0 : ¬ (p ≈ 0#)
42+
split-∣ : {x y} p ∣ x * y p ∣ x ⊎ p ∣ y

src/Algebra/Properties/CommutativeMagma/Divisibility.agda

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ open CommutativeMagma CM
1818
open import Relation.Binary.Reasoning.Setoid setoid
1919

2020
------------------------------------------------------------------------------
21-
-- Re-export the contents of semigroup
21+
-- Re-export the contents of magmas
2222

2323
open import Algebra.Properties.Magma.Divisibility magma public
2424

src/Algebra/Properties/Magma/Divisibility.agda

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ module Algebra.Properties.Magma.Divisibility {a ℓ} (M : Magma a ℓ) where
1414

1515
open Magma M
1616

17-
------------------------------------------------------------------------------
17+
------------------------------------------------------------------------
1818
-- Re-export divisibility relations publicly
1919

2020
open import Algebra.Divisibility _≈_ _∙_ as Div public
21-
using (_∣_; _∤_; _∣∣_; _¬∣∣_)
21+
using (_∣_; _∤_; _∣∣_; _∤∤_)
2222

23-
------------------------------------------------------------------------------
23+
------------------------------------------------------------------------
2424
-- Properties of divisibility
2525

2626
∣-respʳ : _∣_ Respectsʳ _≈_

src/Algebra/Properties/Monoid/Divisibility.agda

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ open Monoid M
1818
import Algebra.Divisibility _≈_ _∙_ as Div
1919

2020
------------------------------------------------------------------------
21-
-- Re-export magma divisibility
21+
-- Re-export semigroup divisibility
2222

2323
open import Algebra.Properties.Semigroup.Divisibility semigroup public
2424

2525
------------------------------------------------------------------------
2626
-- Additional properties
2727

2828
ε∣_ : x ε ∣ x
29-
ε∣_ = Div.∣-min identityʳ
29+
ε∣ x = x , identityʳ x
3030

3131
∣-refl : Reflexive _∣_
3232
∣-refl = Div.∣-refl identityˡ
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Properties of divisibility over semirings
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --without-K --safe #-}
8+
9+
open import Algebra using (Semiring)
10+
import Algebra.Properties.Monoid.Divisibility as MonoidDivisibility
11+
open import Data.Product using (_,_)
12+
open import Data.Sum.Base using (_⊎_; inj₁; inj₂)
13+
14+
module Algebra.Properties.Semiring.Divisibility
15+
{a ℓ} (R : Semiring a ℓ) where
16+
17+
open Semiring R
18+
19+
------------------------------------------------------------------------
20+
-- Re-exporting divisibility over monoids
21+
22+
open MonoidDivisibility *-monoid public
23+
renaming (ε∣_ to 1∣_)
24+
25+
------------------------------------------------------------------------
26+
-- Divisibility properties specific to semirings.
27+
28+
_∣0 : x x ∣ 0#
29+
x ∣0 = 0# , zeroˡ x
30+
31+
0∣x⇒x≈0 : {x} 0# ∣ x x ≈ 0#
32+
0∣x⇒x≈0 (q , q*0≈x) = trans (sym q*0≈x) (zeroʳ q)
33+
34+
x∣y∧y≉0⇒x≉0 : {x y} x ∣ y y ≉ 0# x ≉ 0#
35+
x∣y∧y≉0⇒x≉0 x∣y y≉0 x≈0 = y≉0 (0∣x⇒x≈0 (∣-respˡ x≈0 x∣y))
36+
37+
0∤1 : 0# ≉ 1# 0# ∤ 1#
38+
0∤1 0≉1 (q , q*0≈1) = 0≉1 (trans (sym (zeroʳ q)) q*0≈1)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Properties of the Greatest Common Divisor over semirings.
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --without-K --safe #-}
8+
9+
open import Algebra using (Semiring)
10+
import Algebra.Properties.Monoid.Divisibility as MonoidDiv
11+
open import Data.Product using (_,_)
12+
open import Data.Sum.Base using (_⊎_; inj₁; inj₂)
13+
14+
module Algebra.Properties.Semiring.GCD {a ℓ} (R : Semiring a ℓ) where
15+
16+
open Semiring R
17+
open import Algebra.Properties.Semiring.Divisibility R
18+
19+
------------------------------------------------------------------------
20+
-- Re-exporting definition of GCD
21+
22+
open import Algebra.Divisibility _≈_ _*_ public
23+
using (IsGCD; gcdᶜ)
24+
25+
------------------------------------------------------------------------
26+
-- Properties of GCD
27+
28+
x≉0⊎y≉0⇒gcd≉0 : {x y d} IsGCD x y d x ≉ 0# ⊎ y ≉ 0# d ≉ 0#
29+
x≉0⊎y≉0⇒gcd≉0 (gcdᶜ d∣x _ _) (inj₁ x≉0) = x∣y∧y≉0⇒x≉0 d∣x x≉0
30+
x≉0⊎y≉0⇒gcd≉0 (gcdᶜ _ d∣y _) (inj₂ y≉0) = x∣y∧y≉0⇒x≉0 d∣y y≉0
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Some theory for CancellativeCommutativeSemiring.
5+
------------------------------------------------------------------------
6+
7+
{-# OPTIONS --without-K --safe #-}
8+
9+
open import Algebra using (Semiring)
10+
import Algebra.Primality
11+
import Algebra.Properties.Semigroup.Divisibility as SemigroupDiv
12+
import Algebra.Properties.Semiring.Divisibility as SemiringDiv
13+
open import Data.Product using (_×_; _,_; proj₁; proj₂)
14+
open import Data.Sum using (_⊎_; inj₁; inj₂; reduce)
15+
open import Function using (case_of_; flip)
16+
open import Level using (_⊔_)
17+
open import Relation.Binary using (Rel; Symmetric; Setoid)
18+
open import Relation.Unary using (Pred)
19+
20+
module Algebra.Properties.Semiring.Primality
21+
{a ℓ} (R : Semiring a ℓ)
22+
where
23+
24+
open Semiring R renaming (Carrier to A)
25+
open SemiringDiv R
26+
27+
------------------------------------------------------------------------------
28+
-- Re-export primality definitions
29+
30+
open Algebra.Primality _≈_ _*_ 0# 1# public
31+
32+
------------------------------------------------------------------------------
33+
-- Properties of Irreducible
34+
35+
Irreducible⇒≉0 : 0# ≉ 1# {p} Irreducible p p ≉ 0#
36+
Irreducible⇒≉0 0≉1 (mkIrred _ chooseInvertible) p≈0 =
37+
0∤1 0≉1 (reduce (chooseInvertible (trans p≈0 (sym (zeroˡ 0#)))))
38+
39+
------------------------------------------------------------------------------
40+
-- Properties of Coprime
41+
42+
Coprime-sym : Symmetric Coprime
43+
Coprime-sym coprime = flip coprime
44+
45+
∣1⇒Coprime : {x} y x ∣ 1# Coprime x y
46+
∣1⇒Coprime {x} y x∣1 z∣x _ = ∣-trans z∣x x∣1

0 commit comments

Comments
 (0)