Debian Codegraph @

src/Tactic/RingSolver/Core/AlmostCommutativeRing.agda

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