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