Debian Codegraph @

src/Algebra/Solver/Ring/AlmostCommutativeRing.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Commutative semirings with some additional structure ("almost"
5 -- commutative rings), used by the ring solver
6 ------------------------------------------------------------------------
7
8 {-# OPTIONS --cubical-compatible --safe #-}
9
10 module Algebra.Solver.Ring.AlmostCommutativeRing where
11
12 open import Algebra using
13 (Op₁; Op₂; CommutativeSemiring; RawRing; CommutativeRing)
14 open import Algebra.Structures using (IsCommutativeSemiring)
15 open import Algebra.Definitions using (Congruent₁; Congruent₂)
16 import Algebra.Morphism.Definitions as MorphismDefinitions using
17 (Homomorphic₀; Homomorphic₁; Homomorphic₂; Morphism)
18 open import Function.Base using (id)
19 open import Level using (suc; _⊔_)
20 open import Relation.Binary.Core using (Rel)
21
22
23 record IsAlmostCommutativeRing {a ℓ} {A : Set a} (_≈_ : Rel A ℓ)
24 (_+_ _*_ : Op₂ A) (-_ : Op₁ A)
25 (0# 1# : A) : Set (a ⊔ ℓ) where
26 field
27 isCommutativeSemiring : IsCommutativeSemiring _≈_ _+_ _*_ 0# 1#
28 -‿cong : Congruent₁ _≈_ -_
29 -‿*-distribˡ : ∀ x y → ((- x) * y) ≈ (- (x * y))
30 -‿+-comm : ∀ x y → ((- x) + (- y)) ≈ (- (x + y))
31
32 open IsCommutativeSemiring isCommutativeSemiring public
33
34
35 record AlmostCommutativeRing c ℓ : Set (suc (c ⊔ ℓ)) where
36 infix 8 -_
37 infixl 7 _*_
38 infixl 6 _+_
39 infix 4 _≈_
40 field
41 Carrier : Set c
42 _≈_ : Rel Carrier ℓ
43 _+_ : Op₂ Carrier
44 _*_ : Op₂ Carrier
45 -_ : Op₁ Carrier
46 0# : Carrier
47 1# : Carrier
48 isAlmostCommutativeRing : IsAlmostCommutativeRing _≈_ _+_ _*_ -_ 0# 1#
49
50 open IsAlmostCommutativeRing isAlmostCommutativeRing public
51
52 commutativeSemiring : CommutativeSemiring _ _
53 commutativeSemiring = record
54 { isCommutativeSemiring = isCommutativeSemiring
55 }
56
57 open CommutativeSemiring commutativeSemiring public
58 using
59 ( +-magma; +-semigroup
60 ; *-magma; *-semigroup; *-commutativeSemigroup
61 ; +-monoid; +-commutativeMonoid
62 ; *-monoid; *-commutativeMonoid
63 ; semiring
64 )
65
66 rawRing : RawRing _ _
67 rawRing = record
68 { _≈_ = _≈_
69 ; _+_ = _+_
70 ; _*_ = _*_
71 ; -_ = -_
72 ; 0# = 0#
73 ; 1# = 1#
74 }
75
76
77 ------------------------------------------------------------------------
78 -- Homomorphisms
79
80 infix 4 _-Raw-AlmostCommutative⟶_
81
82 record _-Raw-AlmostCommutative⟶_
83 {r₁ r₂ r₃ r₄}
84 (From : RawRing r₁ r₄)
85 (To : AlmostCommutativeRing r₂ r₃) : Set (r₁ ⊔ r₂ ⊔ r₃) where
86 private
87 module F = RawRing From
88 module T = AlmostCommutativeRing To
89 open MorphismDefinitions F.Carrier T.Carrier T._≈_
90 field
91 ⟦_⟧ : Morphism
92 +-homo : Homomorphic₂ ⟦_⟧ F._+_ T._+_
93 *-homo : Homomorphic₂ ⟦_⟧ F._*_ T._*_
94 -‿homo : Homomorphic₁ ⟦_⟧ F.-_ T.-_
95 0-homo : Homomorphic₀ ⟦_⟧ F.0# T.0#
96 1-homo : Homomorphic₀ ⟦_⟧ F.1# T.1#
97
98 -raw-almostCommutative⟶ :
99 ∀ {r₁ r₂} (R : AlmostCommutativeRing r₁ r₂) →
100 AlmostCommutativeRing.rawRing R -Raw-AlmostCommutative⟶ R
101 -raw-almostCommutative⟶ R = record
102 { ⟦_⟧ = id
103 ; +-homo = λ _ _ → refl
104 ; *-homo = λ _ _ → refl
105 ; -‿homo = λ _ → refl
106 ; 0-homo = refl
107 ; 1-homo = refl
108 }
109 where open AlmostCommutativeRing R
110
111 Induced-equivalence : ∀ {c₁ c₂ ℓ₁ ℓ₂} {Coeff : RawRing c₁ ℓ₁}
112 {R : AlmostCommutativeRing c₂ ℓ₂} →
113 Coeff -Raw-AlmostCommutative⟶ R →
114 Rel (RawRing.Carrier Coeff) ℓ₂
115 Induced-equivalence {R = R} morphism a b = ⟦ a ⟧ ≈ ⟦ b ⟧
116 where
117 open AlmostCommutativeRing R
118 open _-Raw-AlmostCommutative⟶_ morphism
119
120 ------------------------------------------------------------------------
121 -- Conversions
122
123 -- Commutative rings are almost commutative rings.
124
125 fromCommutativeRing : ∀ {r₁ r₂} → CommutativeRing r₁ r₂ → AlmostCommutativeRing r₁ r₂
126 fromCommutativeRing CR = record
127 { isAlmostCommutativeRing = record
128 { isCommutativeSemiring = isCommutativeSemiring
129 ; -‿cong = -‿cong
130 ; -‿*-distribˡ = λ x y → sym (-‿distribˡ-* x y)
131 ; -‿+-comm = ⁻¹-∙-comm
132 }
133 }
134 where
135 open CommutativeRing CR
136 open import Algebra.Properties.Ring ring
137 open import Algebra.Properties.AbelianGroup +-abelianGroup
138
139 -- Commutative semirings can be viewed as almost commutative rings by
140 -- using identity as the "almost negation".
141
142 fromCommutativeSemiring : ∀ {r₁ r₂} → CommutativeSemiring r₁ r₂ → AlmostCommutativeRing _ _
143 fromCommutativeSemiring CS = record
144 { -_ = id
145 ; isAlmostCommutativeRing = record
146 { isCommutativeSemiring = isCommutativeSemiring
147 ; -‿cong = id
148 ; -‿*-distribˡ = λ _ _ → refl
149 ; -‿+-comm = λ _ _ → refl
150 }
151 }
152 where open CommutativeSemiring CS