| 1 |
------------------------------------------------------------------------ |
| 2 |
-- The Agda standard library |
| 3 |
-- |
| 4 |
-- Natural numbers: properties of sum and product |
| 5 |
-- |
| 6 |
-- Issue #2553: this is a compatibility stub module, |
| 7 |
-- ahead of a more thorough breaking set of changes. |
| 8 |
------------------------------------------------------------------------ |
| 9 |
|
| 10 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 11 |
|
| 12 |
module Data.Nat.ListAction.Properties where |
| 13 |
|
| 14 |
open import Algebra.Bundles using (CommutativeMonoid) |
| 15 |
open import Data.List.Base using (List; []; _∷_; _++_) |
| 16 |
open import Data.List.Membership.Propositional using (_∈_) |
| 17 |
open import Data.List.Relation.Binary.Permutation.Propositional |
| 18 |
using (_↭_; ↭⇒↭ₛ) |
| 19 |
open import Data.List.Relation.Binary.Permutation.Setoid.Properties |
| 20 |
using (foldr-commMonoid) |
| 21 |
open import Data.List.Relation.Unary.All using (All; []; _∷_) |
| 22 |
open import Data.List.Relation.Unary.Any using (here; there) |
| 23 |
open import Data.Nat.Base using (ℕ; _+_; _*_; NonZero; _≤_) |
| 24 |
open import Data.Nat.Divisibility using (_∣_; m∣m*n; ∣n⇒∣m*n) |
| 25 |
open import Data.Nat.ListAction using (sum; product) |
| 26 |
open import Data.Nat.Properties |
| 27 |
using (+-assoc; *-assoc; *-identityˡ; m*n≢0; m≤m*n; m≤n⇒m≤o*n |
| 28 |
; +-0-commutativeMonoid; *-1-commutativeMonoid) |
| 29 |
open import Relation.Binary.Core using (_Preserves_⟶_) |
| 30 |
open import Relation.Binary.PropositionalEquality.Core |
| 31 |
using (_≡_; refl; sym; cong) |
| 32 |
open import Relation.Binary.PropositionalEquality.Properties |
| 33 |
using (module ≡-Reasoning) |
| 34 |
|
| 35 |
private |
| 36 |
variable |
| 37 |
m n : ℕ |
| 38 |
ms ns : List ℕ |
| 39 |
|
| 40 |
|
| 41 |
------------------------------------------------------------------------ |
| 42 |
-- Properties |
| 43 |
|
| 44 |
-- sum |
| 45 |
|
| 46 |
sum-++ : ∀ ms ns → sum (ms ++ ns) ≡ sum ms + sum ns |
| 47 |
sum-++ [] ns = refl |
| 48 |
sum-++ (m ∷ ms) ns = begin |
| 49 |
m + sum (ms ++ ns) ≡⟨ cong (m +_) (sum-++ ms ns) ⟩ |
| 50 |
m + (sum ms + sum ns) ≡⟨ +-assoc m _ _ ⟨ |
| 51 |
(m + sum ms) + sum ns ∎ |
| 52 |
where open ≡-Reasoning |
| 53 |
|
| 54 |
sum-↭ : sum Preserves _↭_ ⟶ _≡_ |
| 55 |
sum-↭ p = foldr-commMonoid ℕ-+-0.setoid ℕ-+-0.isCommutativeMonoid (↭⇒↭ₛ p) |
| 56 |
where module ℕ-+-0 = CommutativeMonoid +-0-commutativeMonoid |
| 57 |
|
| 58 |
|
| 59 |
-- product |
| 60 |
|
| 61 |
product-++ : ∀ ms ns → product (ms ++ ns) ≡ product ms * product ns |
| 62 |
product-++ [] ns = sym (*-identityˡ _) |
| 63 |
product-++ (m ∷ ms) ns = begin |
| 64 |
m * product (ms ++ ns) ≡⟨ cong (m *_) (product-++ ms ns) ⟩ |
| 65 |
m * (product ms * product ns) ≡⟨ *-assoc m _ _ ⟨ |
| 66 |
(m * product ms) * product ns ∎ |
| 67 |
where open ≡-Reasoning |
| 68 |
|
| 69 |
∈⇒∣product : n ∈ ns → n ∣ product ns |
| 70 |
∈⇒∣product {ns = n ∷ ns} (here refl) = m∣m*n (product ns) |
| 71 |
∈⇒∣product {ns = m ∷ ns} (there n∈ns) = ∣n⇒∣m*n m (∈⇒∣product n∈ns) |
| 72 |
|
| 73 |
product≢0 : All NonZero ns → NonZero (product ns) |
| 74 |
product≢0 [] = _ |
| 75 |
product≢0 (n≢0 ∷ ns≢0) = m*n≢0 _ _ {{n≢0}} {{product≢0 ns≢0}} |
| 76 |
|
| 77 |
∈⇒≤product : All NonZero ns → n ∈ ns → n ≤ product ns |
| 78 |
∈⇒≤product (n≢0 ∷ ns≢0) (here refl) = m≤m*n _ _ {{product≢0 ns≢0}} |
| 79 |
∈⇒≤product (n≢0 ∷ ns≢0) (there n∈ns) = m≤n⇒m≤o*n _ {{n≢0}} (∈⇒≤product ns≢0 n∈ns) |
| 80 |
|
| 81 |
product-↭ : product Preserves _↭_ ⟶ _≡_ |
| 82 |
product-↭ p = foldr-commMonoid ℕ-*-1.setoid ℕ-*-1.isCommutativeMonoid (↭⇒↭ₛ p) |
| 83 |
where module ℕ-*-1 = CommutativeMonoid *-1-commutativeMonoid |