| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Algebraic properties of products |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
module Data.Product.Algebra where |
| 10 |
|
| 11 |
open import Algebra.Bundles |
| 12 |
using (Magma; Semigroup; Monoid; CommutativeMonoid; CommutativeSemiring) |
| 13 |
open import Algebra.Definitions |
| 14 |
open import Algebra.Structures |
| 15 |
using (IsMagma; IsSemigroup; IsMonoid; IsCommutativeMonoid |
| 16 |
; IsSemiringWithoutAnnihilatingZero; IsSemiring; IsCommutativeSemiring) |
| 17 |
open import Data.Bool.Base using (true; false) |
| 18 |
open import Data.Empty.Polymorphic using (⊥; ⊥-elim) |
| 19 |
open import Data.Product.Base |
| 20 |
open import Data.Product.Properties |
| 21 |
open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂; [_,_]′) |
| 22 |
open import Data.Sum.Algebra using (⊎-isCommutativeMonoid) |
| 23 |
open import Data.Unit.Polymorphic using (⊤; tt) |
| 24 |
open import Function.Base using (_∘′_) |
| 25 |
open import Function.Bundles using (_↔_; Inverse; mk↔ₛ′) |
| 26 |
open import Function.Properties.Inverse using (↔-isEquivalence) |
| 27 |
open import Level using (Level; suc) |
| 28 |
open import Relation.Binary.PropositionalEquality.Core using (_≡_; cong′; cong₂) |
| 29 |
|
| 30 |
import Function.Definitions as FuncDef |
| 31 |
|
| 32 |
------------------------------------------------------------------------ |
| 33 |
|
| 34 |
private |
| 35 |
variable |
| 36 |
a b c d p : Level |
| 37 |
A B C D : Set a |
| 38 |
|
| 39 |
------------------------------------------------------------------------ |
| 40 |
-- Properties of Σ |
| 41 |
|
| 42 |
-- Σ is associative |
| 43 |
Σ-assoc : {B : A → Set b} {C : (a : A) → B a → Set c} → |
| 44 |
Σ (Σ A B) (uncurry C) ↔ Σ A (λ a → Σ (B a) (C a)) |
| 45 |
Σ-assoc = mk↔ₛ′ assocʳ assocˡ cong′ cong′ |
| 46 |
|
| 47 |
-- Σ is associative, alternate formulation |
| 48 |
Σ-assoc-alt : {B : A → Set b} {C : Σ A B → Set c} → |
| 49 |
Σ (Σ A B) C ↔ Σ A (λ a → Σ (B a) (curry C a)) |
| 50 |
Σ-assoc-alt = mk↔ₛ′ assocʳ-curried assocˡ-curried cong′ cong′ |
| 51 |
|
| 52 |
------------------------------------------------------------------------ |
| 53 |
-- Algebraic properties |
| 54 |
|
| 55 |
-- × is a congruence |
| 56 |
×-cong : A ↔ B → C ↔ D → (A × C) ↔ (B × D) |
| 57 |
×-cong i j = mk↔ₛ′ (map I.to J.to) (map I.from J.from) |
| 58 |
(λ {(a , b) → cong₂ _,_ (I.strictlyInverseˡ a) (J.strictlyInverseˡ b)}) |
| 59 |
(λ {(a , b) → cong₂ _,_ (I.strictlyInverseʳ a) (J.strictlyInverseʳ b)}) |
| 60 |
where module I = Inverse i; module J = Inverse j |
| 61 |
|
| 62 |
-- × is commutative. |
| 63 |
-- (we don't use Commutative because it isn't polymorphic enough) |
| 64 |
×-comm : (A : Set a) (B : Set b) → (A × B) ↔ (B × A) |
| 65 |
×-comm _ _ = mk↔ₛ′ swap swap swap-involutive swap-involutive |
| 66 |
|
| 67 |
module _ (ℓ : Level) where |
| 68 |
|
| 69 |
-- × is associative |
| 70 |
×-assoc : Associative {ℓ = ℓ} _↔_ _×_ |
| 71 |
×-assoc _ _ _ = mk↔ₛ′ assocʳ′ assocˡ′ cong′ cong′ |
| 72 |
|
| 73 |
-- ⊤ is the identity for × |
| 74 |
×-identityˡ : LeftIdentity {ℓ = ℓ} _↔_ ⊤ _×_ |
| 75 |
×-identityˡ _ = mk↔ₛ′ proj₂ (tt ,_) cong′ cong′ |
| 76 |
|
| 77 |
×-identityʳ : RightIdentity {ℓ = ℓ} _↔_ ⊤ _×_ |
| 78 |
×-identityʳ _ = mk↔ₛ′ proj₁ (_, tt) cong′ cong′ |
| 79 |
|
| 80 |
×-identity : Identity _↔_ ⊤ _×_ |
| 81 |
×-identity = ×-identityˡ , ×-identityʳ |
| 82 |
|
| 83 |
-- ⊥ is the zero for × |
| 84 |
×-zeroˡ : LeftZero {ℓ = ℓ} _↔_ ⊥ _×_ |
| 85 |
×-zeroˡ A = mk↔ₛ′ proj₁ ⊥-elim ⊥-elim λ () |
| 86 |
|
| 87 |
×-zeroʳ : RightZero {ℓ = ℓ} _↔_ ⊥ _×_ |
| 88 |
×-zeroʳ A = mk↔ₛ′ proj₂ ⊥-elim ⊥-elim λ () |
| 89 |
|
| 90 |
×-zero : Zero _↔_ ⊥ _×_ |
| 91 |
×-zero = ×-zeroˡ , ×-zeroʳ |
| 92 |
|
| 93 |
-- × distributes over ⊎ |
| 94 |
×-distribˡ-⊎ : _DistributesOverˡ_ {ℓ = ℓ} _↔_ _×_ _⊎_ |
| 95 |
×-distribˡ-⊎ _ _ _ = mk↔ₛ′ |
| 96 |
(uncurry λ x → [ inj₁ ∘′ (x ,_) , inj₂ ∘′ (x ,_) ]′) |
| 97 |
[ map₂ inj₁ , map₂ inj₂ ]′ |
| 98 |
Sum.[ cong′ , cong′ ] |
| 99 |
(uncurry λ _ → Sum.[ cong′ , cong′ ]) |
| 100 |
|
| 101 |
×-distribʳ-⊎ : _DistributesOverʳ_ {ℓ = ℓ} _↔_ _×_ _⊎_ |
| 102 |
×-distribʳ-⊎ _ _ _ = mk↔ₛ′ |
| 103 |
(uncurry [ curry inj₁ , curry inj₂ ]′) |
| 104 |
[ map₁ inj₁ , map₁ inj₂ ]′ |
| 105 |
Sum.[ cong′ , cong′ ] |
| 106 |
(uncurry Sum.[ (λ _ → cong′) , (λ _ → cong′) ]) |
| 107 |
|
| 108 |
×-distrib-⊎ : _DistributesOver_ {ℓ = ℓ} _↔_ _×_ _⊎_ |
| 109 |
×-distrib-⊎ = ×-distribˡ-⊎ , ×-distribʳ-⊎ |
| 110 |
|
| 111 |
------------------------------------------------------------------------ |
| 112 |
-- Algebraic structures |
| 113 |
|
| 114 |
×-isMagma : IsMagma {ℓ = ℓ} _↔_ _×_ |
| 115 |
×-isMagma = record |
| 116 |
{ isEquivalence = ↔-isEquivalence |
| 117 |
; ∙-cong = ×-cong |
| 118 |
} |
| 119 |
|
| 120 |
×-isSemigroup : IsSemigroup _↔_ _×_ |
| 121 |
×-isSemigroup = record |
| 122 |
{ isMagma = ×-isMagma |
| 123 |
; assoc = λ _ _ _ → Σ-assoc |
| 124 |
} |
| 125 |
|
| 126 |
×-isMonoid : IsMonoid _↔_ _×_ ⊤ |
| 127 |
×-isMonoid = record |
| 128 |
{ isSemigroup = ×-isSemigroup |
| 129 |
; identity = ×-identityˡ , ×-identityʳ |
| 130 |
} |
| 131 |
|
| 132 |
×-isCommutativeMonoid : IsCommutativeMonoid _↔_ _×_ ⊤ |
| 133 |
×-isCommutativeMonoid = record |
| 134 |
{ isMonoid = ×-isMonoid |
| 135 |
; comm = ×-comm |
| 136 |
} |
| 137 |
|
| 138 |
⊎-×-isSemiringWithoutAnnihilatingZero : IsSemiringWithoutAnnihilatingZero _↔_ _⊎_ _×_ ⊥ ⊤ |
| 139 |
⊎-×-isSemiringWithoutAnnihilatingZero = record |
| 140 |
{ +-isCommutativeMonoid = ⊎-isCommutativeMonoid ℓ |
| 141 |
; *-cong = ×-cong |
| 142 |
; *-assoc = ×-assoc |
| 143 |
; *-identity = ×-identity |
| 144 |
; distrib = ×-distrib-⊎ |
| 145 |
} |
| 146 |
|
| 147 |
⊎-×-isSemiring : IsSemiring _↔_ _⊎_ _×_ ⊥ ⊤ |
| 148 |
⊎-×-isSemiring = record |
| 149 |
{ isSemiringWithoutAnnihilatingZero = ⊎-×-isSemiringWithoutAnnihilatingZero |
| 150 |
; zero = ×-zero |
| 151 |
} |
| 152 |
|
| 153 |
⊎-×-isCommutativeSemiring : IsCommutativeSemiring _↔_ _⊎_ _×_ ⊥ ⊤ |
| 154 |
⊎-×-isCommutativeSemiring = record |
| 155 |
{ isSemiring = ⊎-×-isSemiring |
| 156 |
; *-comm = ×-comm |
| 157 |
} |
| 158 |
------------------------------------------------------------------------ |
| 159 |
-- Algebraic bundles |
| 160 |
|
| 161 |
×-magma : Magma (suc ℓ) ℓ |
| 162 |
×-magma = record |
| 163 |
{ isMagma = ×-isMagma |
| 164 |
} |
| 165 |
|
| 166 |
×-semigroup : Semigroup (suc ℓ) ℓ |
| 167 |
×-semigroup = record |
| 168 |
{ isSemigroup = ×-isSemigroup |
| 169 |
} |
| 170 |
|
| 171 |
×-monoid : Monoid (suc ℓ) ℓ |
| 172 |
×-monoid = record |
| 173 |
{ isMonoid = ×-isMonoid |
| 174 |
} |
| 175 |
|
| 176 |
×-commutativeMonoid : CommutativeMonoid (suc ℓ) ℓ |
| 177 |
×-commutativeMonoid = record |
| 178 |
{ isCommutativeMonoid = ×-isCommutativeMonoid |
| 179 |
} |
| 180 |
|
| 181 |
×-⊎-commutativeSemiring : CommutativeSemiring (suc ℓ) ℓ |
| 182 |
×-⊎-commutativeSemiring = record |
| 183 |
{ isCommutativeSemiring = ⊎-×-isCommutativeSemiring |
| 184 |
} |