| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Finite summations over a commutative monoid |
| 5 |
------------------------------------------------------------------------ |
| 6 |
|
| 7 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 8 |
|
| 9 |
open import Algebra.Bundles using (CommutativeMonoid) |
| 10 |
open import Data.Bool.Base using (Bool; true; false) |
| 11 |
open import Data.Nat.Base as ℕ using (ℕ; zero; suc; NonZero) |
| 12 |
open import Data.Fin.Base using (Fin; zero; suc) |
| 13 |
open import Data.Fin.Permutation as Perm using (Permutation; _⟨$⟩ˡ_; _⟨$⟩ʳ_) |
| 14 |
open import Data.Fin.Patterns using (0F) |
| 15 |
open import Data.Vec.Functional using (Vector; rearrange; removeAt; tail; head) |
| 16 |
open import Function.Base using (_∘_) |
| 17 |
open import Relation.Binary.PropositionalEquality.Core as ≡ using (_≡_) |
| 18 |
open import Relation.Nullary.Negation using (contradiction) |
| 19 |
|
| 20 |
module Algebra.Properties.CommutativeMonoid.Sum |
| 21 |
{a ℓ} (M : CommutativeMonoid a ℓ) where |
| 22 |
|
| 23 |
open CommutativeMonoid M |
| 24 |
renaming |
| 25 |
( _∙_ to _+_ |
| 26 |
; ∙-cong to +-cong |
| 27 |
; ∙-congˡ to +-congˡ |
| 28 |
; identityˡ to +-identityˡ |
| 29 |
; identityʳ to +-identityʳ |
| 30 |
; assoc to +-assoc |
| 31 |
; ε to 0# |
| 32 |
) |
| 33 |
|
| 34 |
open import Algebra.Definitions _≈_ |
| 35 |
open import Algebra.Solver.CommutativeMonoid M |
| 36 |
open import Data.Vec.Functional.Relation.Binary.Equality.Setoid setoid |
| 37 |
open import Relation.Binary.Reasoning.Setoid setoid |
| 38 |
|
| 39 |
------------------------------------------------------------------------ |
| 40 |
-- Re-export summation over monoids |
| 41 |
|
| 42 |
open import Algebra.Properties.Monoid.Sum monoid public |
| 43 |
|
| 44 |
------------------------------------------------------------------------ |
| 45 |
-- Properties |
| 46 |
|
| 47 |
-- When summing over a function from a finite set, we can pull out any |
| 48 |
-- value and move it to the front. |
| 49 |
sum-remove : ∀ {n} {i : Fin (suc n)} t → sum t ≈ t i + sum (removeAt t i) |
| 50 |
sum-remove {_} {zero} xs = refl |
| 51 |
sum-remove {suc n} {suc i} xs = begin |
| 52 |
t₀ + ∑t ≈⟨ +-congˡ (sum-remove t) ⟩ |
| 53 |
t₀ + (tᵢ + ∑t′) ≈⟨ solve 3 (λ x y z → x ⊕ (y ⊕ z) ⊜ y ⊕ (x ⊕ z)) refl t₀ tᵢ ∑t′ ⟩ |
| 54 |
tᵢ + (t₀ + ∑t′) ∎ |
| 55 |
where |
| 56 |
t = tail xs |
| 57 |
t₀ = head xs |
| 58 |
tᵢ = t i |
| 59 |
∑t = sum t |
| 60 |
∑t′ = sum (removeAt t i) |
| 61 |
|
| 62 |
-- The '∑' operator distributes over addition. |
| 63 |
∑-distrib-+ : ∀ {n} (f g : Vector Carrier n) → ∑[ i < n ] (f i + g i) ≈ ∑[ i < n ] f i + ∑[ i < n ] g i |
| 64 |
∑-distrib-+ {zero} f g = sym (+-identityˡ _) |
| 65 |
∑-distrib-+ {suc n} f g = begin |
| 66 |
f₀ + g₀ + ∑fg ≈⟨ +-assoc _ _ _ ⟩ |
| 67 |
f₀ + (g₀ + ∑fg) ≈⟨ +-congˡ (+-congˡ (∑-distrib-+ (f ∘ suc) (g ∘ suc))) ⟩ |
| 68 |
f₀ + (g₀ + (∑f + ∑g)) ≈⟨ solve 4 (λ a b c d → a ⊕ (c ⊕ (b ⊕ d)) ⊜ (a ⊕ b) ⊕ (c ⊕ d)) refl f₀ ∑f g₀ ∑g ⟩ |
| 69 |
(f₀ + ∑f) + (g₀ + ∑g) ∎ |
| 70 |
where |
| 71 |
f₀ = f 0F |
| 72 |
g₀ = g 0F |
| 73 |
∑f = ∑[ i < n ] f (suc i) |
| 74 |
∑g = ∑[ i < n ] g (suc i) |
| 75 |
∑fg = ∑[ i < n ] (f (suc i) + g (suc i)) |
| 76 |
|
| 77 |
-- The '∑' operator commutes with itself. |
| 78 |
∑-comm : ∀ {m n} (f : Fin m → Fin n → Carrier) → |
| 79 |
∑[ i < m ] ∑[ j < n ] f i j ≈ ∑[ j < n ] ∑[ i < m ] f i j |
| 80 |
∑-comm {zero} {n} f = sym (sum-replicate-zero n) |
| 81 |
∑-comm {suc m} {n} f = begin |
| 82 |
∑[ j < n ] f zero j + ∑[ i < m ] ∑[ j < n ] f (suc i) j ≈⟨ +-congˡ (∑-comm (f ∘ suc)) ⟩ |
| 83 |
∑[ j < n ] f zero j + ∑[ j < n ] ∑[ i < m ] f (suc i) j ≈⟨ sym (∑-distrib-+ (f zero) _) ⟩ |
| 84 |
∑[ j < n ] (f zero j + ∑[ i < m ] f (suc i) j) ∎ |
| 85 |
|
| 86 |
-- Summation is insensitive to permutations of the input |
| 87 |
sum-permute : ∀ {m n} f (π : Permutation m n) → sum f ≈ sum (rearrange (π ⟨$⟩ʳ_) f) |
| 88 |
sum-permute {zero} {zero} f π = refl |
| 89 |
sum-permute {zero} {suc n} f π = contradiction π (Perm.refute λ()) |
| 90 |
sum-permute {suc m} {zero} f π = contradiction π (Perm.refute λ()) |
| 91 |
sum-permute {suc m} {suc n} f π = begin |
| 92 |
sum f ≡⟨⟩ |
| 93 |
f 0F + sum f/0 ≡⟨ ≡.cong (_+ sum f/0) (≡.cong f (Perm.inverseʳ π)) ⟨ |
| 94 |
πf π₀ + sum f/0 ≈⟨ +-congˡ (sum-permute f/0 (Perm.remove π₀ π)) ⟩ |
| 95 |
πf π₀ + sum (rearrange (π/0 ⟨$⟩ʳ_) f/0) ≡⟨ ≡.cong (πf π₀ +_) (sum-cong-≗ (≡.cong f ∘ Perm.punchIn-permute′ π 0F)) ⟨ |
| 96 |
πf π₀ + sum (removeAt πf π₀) ≈⟨ sym (sum-remove πf) ⟩ |
| 97 |
sum πf ∎ |
| 98 |
where |
| 99 |
f/0 = removeAt f 0F |
| 100 |
π₀ = π ⟨$⟩ˡ 0F |
| 101 |
π/0 = Perm.remove π₀ π |
| 102 |
πf = rearrange (π ⟨$⟩ʳ_) f |
| 103 |
|
| 104 |
∑-permute : ∀ {m n} (f : Vector Carrier n) (π : Permutation m n) → |
| 105 |
∑[ i < n ] f i ≈ ∑[ i < m ] f (π ⟨$⟩ʳ i) |
| 106 |
∑-permute f π = sum-permute f π |