Skip to content

Commit 963d701

Browse files
authored
[ refactor ] Add Data.Nat.ListAction (#2558)
* move code to new module * refactor: deprecate old definitions * refactor: rejig `Nat` imports to uncouple `sum`, `product` form `length` * refactor: `CHANGELOG` * refactor: move and deprecate * refactor: move+deprecate * refactor: cosmetic, tighten `import`s * refactor: fix deprecation problem * refactor: renamed new module * refactor: introduce `Properties` module * bug: add the new module! * fix: pasre error
1 parent b003e79 commit 963d701

File tree

13 files changed

+232
-76
lines changed

13 files changed

+232
-76
lines changed

CHANGELOG.md

+18
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,31 @@ Deprecated names
7070
or ↦ Data.Bool.ListAction.or
7171
any ↦ Data.Bool.ListAction.any
7272
all ↦ Data.Bool.ListAction.all
73+
sum ↦ Data.Nat.ListAction.sum
74+
product ↦ Data.Nat.ListAction.product
75+
```
76+
77+
* In `Data.List.Properties`:
78+
```agda
79+
sum-++ ↦ Data.Nat.ListAction.Properties.sum-++
80+
∈⇒∣product ↦ Data.Nat.ListAction.Properties.∈⇒∣product
81+
product≢0 ↦ Data.Nat.ListAction.Properties.product≢0
82+
∈⇒≤product ↦ Data.Nat.ListAction.Properties.∈⇒≤product
83+
```
84+
85+
* In `Data.List.Relation.Binary.Permutation.Propositional.Properties`:
86+
```agda
87+
sum-↭ ↦ Data.Nat.ListAction.Properties.sum-↭
88+
product-↭ ↦ Data.Nat.ListAction.Properties.product-↭
7389
```
7490

7591
New modules
7692
-----------
7793

7894
* `Data.List.Base.{and|or|any|all}` have been lifted out into `Data.Bool.ListAction`.
7995

96+
* `Data.List.Base.{sum|product}` and their properties have been lifted out into `Data.Nat.ListAction` and `Data.Nat.ListAction.Properties`.
97+
8098
Additions to existing modules
8199
-----------------------------
82100

doc/README/Data/List.agda

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
module README.Data.List where
88

99
open import Data.Nat.Base using (ℕ; _+_)
10+
open import Data.Nat.ListAction using (sum)
1011
open import Relation.Binary.PropositionalEquality using (_≡_; refl)
1112

1213
------------------------------------------------------------------------
@@ -18,7 +19,7 @@ open import Data.List
1819
using
1920
(List
2021
; []; _∷_
21-
; sum; map; take; reverse; _++_; drop
22+
; map; take; reverse; _++_; drop
2223
)
2324

2425
-- Lists are built using the "[]" and "_∷_" constructors.

src/Data/List/Base.agda

+15-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ open import Data.Bool.Base as Bool
1616
using (Bool; false; true; not; _∧_; _∨_; if_then_else_)
1717
open import Data.Fin.Base using (Fin; zero; suc)
1818
open import Data.Maybe.Base as Maybe using (Maybe; nothing; just; maybe′)
19-
open import Data.Nat.Base as ℕ using (ℕ; zero; suc; _+_; _*_)
19+
open import Data.Nat.Base as ℕ using (ℕ; zero; suc)
2020
open import Data.Product.Base as Product using (_×_; _,_; map₁; map₂′)
2121
open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂)
2222
open import Data.These.Base as These using (These; this; that; these)
@@ -150,12 +150,6 @@ null : List A → Bool
150150
null [] = true
151151
null (x ∷ xs) = false
152152

153-
sum : List ℕ
154-
sum = foldr _+_ 0
155-
156-
product : List ℕ
157-
product = foldr _*_ 1
158-
159153
length : List A
160154
length = foldr (const suc) 0
161155

@@ -598,3 +592,17 @@ Please use Data.Bool.ListAction.or instead."
598592
"Warning: any was deprecated in v2.3.
599593
Please use Data.Bool.ListAction.any instead."
600594
#-}
595+
596+
sum : List ℕ
597+
sum = foldr ℕ._+_ 0
598+
{-# WARNING_ON_USAGE sum
599+
"Warning: sum was deprecated in v2.3.
600+
Please use Data.Nat.ListAction.sum instead."
601+
#-}
602+
603+
product : List ℕ
604+
product = foldr ℕ._*_ 1
605+
{-# WARNING_ON_USAGE product
606+
"Warning: product was deprecated in v2.3.
607+
Please use Data.Nat.ListAction.product instead."
608+
#-}

src/Data/List/Properties.agda

+48-37
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ import Algebra.Structures as AlgebraicStructures
2121
open import Data.Bool.Base using (Bool; false; true; not; if_then_else_)
2222
open import Data.Fin.Base using (Fin; zero; suc; cast; toℕ)
2323
open import Data.List.Base as List
24-
open import Data.List.Membership.Propositional using (_∈_)
2524
open import Data.List.Relation.Unary.All using (All; []; _∷_)
2625
open import Data.List.Relation.Unary.Any using (Any; here; there)
2726
open import Data.Maybe.Base as Maybe using (Maybe; just; nothing)
2827
open import Data.Maybe.Relation.Unary.Any using (just) renaming (Any to MAny)
2928
open import Data.Nat.Base
30-
open import Data.Nat.Divisibility using (_∣_; divides; ∣n⇒∣m*n)
3129
open import Data.Nat.Properties
3230
open import Data.Product.Base as Product
3331
using (_×_; _,_; uncurry; uncurry′; proj₁; proj₂; <_,_>)
@@ -829,34 +827,6 @@ mapMaybeIsInj₂∘mapInj₂ = mapMaybe-map-retract λ _ → refl
829827
mapMaybeIsInj₂∘mapInj₁ : (xs : List A) mapMaybe (isInj₂ {B = B}) (map inj₁ xs) ≡ []
830828
mapMaybeIsInj₂∘mapInj₁ = mapMaybe-map-none λ _ refl
831829

832-
------------------------------------------------------------------------
833-
-- sum
834-
835-
sum-++ : xs ys sum (xs ++ ys) ≡ sum xs + sum ys
836-
sum-++ [] ys = refl
837-
sum-++ (x ∷ xs) ys = begin
838-
x + sum (xs ++ ys) ≡⟨ cong (x +_) (sum-++ xs ys) ⟩
839-
x + (sum xs + sum ys) ≡⟨ sym (+-assoc x _ _) ⟩
840-
(x + sum xs) + sum ys ∎
841-
842-
------------------------------------------------------------------------
843-
-- product
844-
845-
∈⇒∣product : {n ns} n ∈ ns n ∣ product ns
846-
∈⇒∣product {n} {n ∷ ns} (here refl) = divides (product ns) (*-comm n (product ns))
847-
∈⇒∣product {n} {m ∷ ns} (there n∈ns) = ∣n⇒∣m*n m (∈⇒∣product n∈ns)
848-
849-
product≢0 : {ns} All NonZero ns NonZero (product ns)
850-
product≢0 [] = _
851-
product≢0 {n ∷ ns} (n≢0 ∷ ns≢0) = m*n≢0 n (product ns) {{n≢0}} {{product≢0 ns≢0}}
852-
853-
∈⇒≤product : {n ns} All NonZero ns n ∈ ns n ≤ product ns
854-
∈⇒≤product {ns = n ∷ ns} (_ ∷ ns≢0) (here refl) =
855-
m≤m*n n (product ns) {{product≢0 ns≢0}}
856-
∈⇒≤product {ns = n ∷ _} (n≢0 ∷ ns≢0) (there n∈ns) =
857-
m≤n⇒m≤o*n n {{n≢0}} (∈⇒≤product ns≢0 n∈ns)
858-
859-
860830
------------------------------------------------------------------------
861831
-- applyUpTo
862832

@@ -1534,7 +1504,7 @@ module _ (f : A → B) where
15341504

15351505

15361506
------------------------------------------------------------------------
1537-
-- DEPRECATED
1507+
-- DEPRECATED NAMES
15381508
------------------------------------------------------------------------
15391509
-- Please use the new names as continuing support for the old names is
15401510
-- not guaranteed.
@@ -1565,12 +1535,6 @@ map-++-commute = map-++
15651535
Please use map-++ instead."
15661536
#-}
15671537

1568-
sum-++-commute = sum-++
1569-
{-# WARNING_ON_USAGE sum-++-commute
1570-
"Warning: map-++-commute was deprecated in v2.0.
1571-
Please use map-++ instead."
1572-
#-}
1573-
15741538
reverse-map-commute = reverse-map
15751539
{-# WARNING_ON_USAGE reverse-map-commute
15761540
"Warning: reverse-map-commute was deprecated in v2.0.
@@ -1658,3 +1622,50 @@ concat-[-] = concat-map-[_]
16581622
"Warning: concat-[-] was deprecated in v2.2.
16591623
Please use concat-map-[_] instead."
16601624
#-}
1625+
1626+
-- Version 2.3
1627+
1628+
sum-++ : xs ys sum (xs ++ ys) ≡ sum xs + sum ys
1629+
sum-++ [] ys = refl
1630+
sum-++ (x ∷ xs) ys = begin
1631+
x + sum (xs ++ ys) ≡⟨ cong (x +_) (sum-++ xs ys) ⟩
1632+
x + (sum xs + sum ys) ≡⟨ +-assoc x _ _ ⟨
1633+
(x + sum xs) + sum ys ∎
1634+
{-# WARNING_ON_USAGE sum-++
1635+
"Warning: sum-++ was deprecated in v2.3.
1636+
Please use Data.Nat.ListAction.Properties.sum-++ instead."
1637+
#-}
1638+
sum-++-commute = sum-++
1639+
{-# WARNING_ON_USAGE sum-++-commute
1640+
"Warning: sum-++-commute was deprecated in v2.0.
1641+
Please use Data.Nat.ListAction.Properties.sum-++ instead."
1642+
#-}
1643+
1644+
open import Data.List.Membership.Propositional using (_∈_)
1645+
open import Data.Nat.Divisibility using (_∣_; m∣m*n; ∣n⇒∣m*n)
1646+
1647+
∈⇒∣product : {n ns} n ∈ ns n ∣ product ns
1648+
∈⇒∣product {ns = n ∷ ns} (here refl) = m∣m*n (product ns)
1649+
∈⇒∣product {ns = m ∷ ns} (there n∈ns) = ∣n⇒∣m*n m (∈⇒∣product n∈ns)
1650+
{-# WARNING_ON_USAGE ∈⇒∣product
1651+
"Warning: ∈⇒∣product was deprecated in v2.3.
1652+
Please use Data.Nat.ListAction.Properties.∈⇒∣product instead."
1653+
#-}
1654+
1655+
product≢0 : {ns} All NonZero ns NonZero (product ns)
1656+
product≢0 [] = _
1657+
product≢0 {n ∷ ns} (n≢0 ∷ ns≢0) = m*n≢0 n (product ns) {{n≢0}} {{product≢0 ns≢0}}
1658+
{-# WARNING_ON_USAGE product≢0
1659+
"Warning: product≢0 was deprecated in v2.3.
1660+
Please use Data.Nat.ListAction.Properties.product≢0 instead."
1661+
#-}
1662+
1663+
∈⇒≤product : {n ns} All NonZero ns n ∈ ns n ≤ product ns
1664+
∈⇒≤product {ns = n ∷ ns} (_ ∷ ns≢0) (here refl) =
1665+
m≤m*n n (product ns) {{product≢0 ns≢0}}
1666+
∈⇒≤product {ns = n ∷ _} (n≢0 ∷ ns≢0) (there n∈ns) =
1667+
m≤n⇒m≤o*n n {{n≢0}} (∈⇒≤product ns≢0 n∈ns)
1668+
{-# WARNING_ON_USAGE ∈⇒≤product
1669+
"Warning: ∈⇒≤product was deprecated in v2.3.
1670+
Please use Data.Nat.ListAction.Properties.∈⇒≤product instead."
1671+
#-}

src/Data/List/Relation/Binary/Permutation/Propositional/Properties.agda

+23-19
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ open import Data.Bool.Base using (Bool; true; false)
1515
open import Data.Nat.Base using (ℕ; suc)
1616
import Data.Nat.Properties as ℕ
1717
open import Data.Product.Base using (-,_)
18-
open import Data.List.Base as List
18+
open import Data.List.Base as List hiding (sum; product)
1919
open import Data.List.Relation.Binary.Permutation.Propositional
2020
open import Data.List.Relation.Unary.Any using (Any; here; there)
2121
open import Data.List.Relation.Unary.All using (All; []; _∷_)
@@ -372,24 +372,6 @@ module _ {ℓ} {R : Rel A ℓ} (R? : Decidable R) where
372372
x ∷ xs ++ y ∷ ys ∎
373373
where open PermutationReasoning
374374

375-
------------------------------------------------------------------------
376-
-- sum
377-
378-
sum-↭ : sum Preserves _↭_ ⟶ _≡_
379-
sum-↭ p = foldr-commMonoid ℕ-+-0.isCommutativeMonoid (↭⇒↭ₛ p)
380-
where
381-
module ℕ-+-0 = CommutativeMonoid ℕ.+-0-commutativeMonoid
382-
open Permutation ℕ-+-0.setoid
383-
384-
------------------------------------------------------------------------
385-
-- product
386-
387-
product-↭ : product Preserves _↭_ ⟶ _≡_
388-
product-↭ p = foldr-commMonoid ℕ-*-1.isCommutativeMonoid (↭⇒↭ₛ p)
389-
where
390-
module ℕ-*-1 = CommutativeMonoid ℕ.*-1-commutativeMonoid
391-
open Permutation ℕ-*-1.setoid
392-
393375
------------------------------------------------------------------------
394376
-- catMaybes
395377

@@ -408,3 +390,25 @@ catMaybes-↭ (swap (just x) (just y) xs↭) = swap x y $ catMaybes-↭ xs↭
408390

409391
mapMaybe-↭ : (f : A Maybe B) xs ↭ ys mapMaybe f xs ↭ mapMaybe f ys
410392
mapMaybe-↭ f = catMaybes-↭ ∘ map⁺ f
393+
394+
395+
------------------------------------------------------------------------
396+
-- DEPRECATED NAMES
397+
------------------------------------------------------------------------
398+
-- Please use the new names as continuing support for the old names is
399+
-- not guaranteed.
400+
401+
-- Version 2.3
402+
403+
import Data.Nat.ListAction.Properties as ℕ
404+
405+
sum-↭ = ℕ.sum-↭
406+
{-# WARNING_ON_USAGE sum-↭
407+
"Warning: sum-↭ was deprecated in v2.3.
408+
Please use Data.Nat.ListAction.sum-↭ instead."
409+
#-}
410+
product-↭ = ℕ.product-↭
411+
{-# WARNING_ON_USAGE product-↭
412+
"Warning: product-↭ was deprecated in v2.3.
413+
Please use Data.Nat.ListAction.product-↭ instead."
414+
#-}

src/Data/Nat/ListAction.agda

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
------------------------------------------------------------------------
2+
-- The Agda standard library
3+
--
4+
-- Natural numbers: sum and product of lists
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 where
13+
14+
open import Data.List.Base using (List; []; _∷_; _++_; foldr)
15+
open import Data.Nat.Base using (ℕ; _+_; _*_)
16+
17+
18+
------------------------------------------------------------------------
19+
-- Definitions
20+
21+
sum : List ℕ
22+
sum = foldr _+_ 0
23+
24+
product : List ℕ
25+
product = foldr _*_ 1
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 using (_↭_; ↭⇒↭ₛ)
18+
open import Data.List.Relation.Binary.Permutation.Setoid.Properties
19+
using (foldr-commMonoid)
20+
open import Data.List.Relation.Unary.All using (All; []; _∷_)
21+
open import Data.List.Relation.Unary.Any using (here; there)
22+
open import Data.Nat.Base using (ℕ; _+_; _*_; NonZero; _≤_)
23+
open import Data.Nat.Divisibility using (_∣_; m∣m*n; ∣n⇒∣m*n)
24+
open import Data.Nat.ListAction
25+
open import Data.Nat.Properties
26+
using (+-assoc; *-assoc; *-identityˡ; m*n≢0; m≤m*n; m≤n⇒m≤o*n;
27+
+-0-commutativeMonoid; *-1-commutativeMonoid)
28+
open import Relation.Binary.Core using (_Preserves_⟶_)
29+
open import Relation.Binary.PropositionalEquality.Core
30+
using (_≡_; refl; sym; cong)
31+
open import Relation.Binary.PropositionalEquality.Properties
32+
using (module ≡-Reasoning)
33+
34+
private
35+
variable
36+
m n :
37+
ms ns : List ℕ
38+
39+
40+
------------------------------------------------------------------------
41+
-- Properties
42+
43+
-- sum
44+
45+
sum-++ : ms ns sum (ms ++ ns) ≡ sum ms + sum ns
46+
sum-++ [] ns = refl
47+
sum-++ (m ∷ ms) ns = begin
48+
m + sum (ms ++ ns) ≡⟨ cong (m +_) (sum-++ ms ns) ⟩
49+
m + (sum ms + sum ns) ≡⟨ +-assoc m _ _ ⟨
50+
(m + sum ms) + sum ns ∎
51+
where open ≡-Reasoning
52+
53+
sum-↭ : sum Preserves _↭_ ⟶ _≡_
54+
sum-↭ p = foldr-commMonoid ℕ-+-0.setoid ℕ-+-0.isCommutativeMonoid (↭⇒↭ₛ p)
55+
where module ℕ-+-0 = CommutativeMonoid +-0-commutativeMonoid
56+
57+
58+
-- product
59+
60+
product-++ : ms ns product (ms ++ ns) ≡ product ms * product ns
61+
product-++ [] ns = sym (*-identityˡ _)
62+
product-++ (m ∷ ms) ns = begin
63+
m * product (ms ++ ns) ≡⟨ cong (m *_) (product-++ ms ns) ⟩
64+
m * (product ms * product ns) ≡⟨ *-assoc m _ _ ⟨
65+
(m * product ms) * product ns ∎
66+
where open ≡-Reasoning
67+
68+
∈⇒∣product : n ∈ ns n ∣ product ns
69+
∈⇒∣product {ns = n ∷ ns} (here refl) = m∣m*n (product ns)
70+
∈⇒∣product {ns = m ∷ ns} (there n∈ns) = ∣n⇒∣m*n m (∈⇒∣product n∈ns)
71+
72+
product≢0 : All NonZero ns NonZero (product ns)
73+
product≢0 [] = _
74+
product≢0 (n≢0 ∷ ns≢0) = m*n≢0 _ _ {{n≢0}} {{product≢0 ns≢0}}
75+
76+
∈⇒≤product : All NonZero ns n ∈ ns n ≤ product ns
77+
∈⇒≤product (n≢0 ∷ ns≢0) (here refl) = m≤m*n _ _ {{product≢0 ns≢0}}
78+
∈⇒≤product (n≢0 ∷ ns≢0) (there n∈ns) = m≤n⇒m≤o*n _ {{n≢0}} (∈⇒≤product ns≢0 n∈ns)
79+
80+
product-↭ : product Preserves _↭_ ⟶ _≡_
81+
product-↭ p = foldr-commMonoid ℕ-*-1.setoid ℕ-*-1.isCommutativeMonoid (↭⇒↭ₛ p)
82+
where module ℕ-*-1 = CommutativeMonoid *-1-commutativeMonoid

src/Data/Nat/Primality.agda

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88

99
module Data.Nat.Primality where
1010

11-
open import Data.List.Base using ([]; _∷_; product)
12-
open import Data.List.Properties using (product≢0)
11+
open import Data.List.Base using ([]; _∷_)
1312
open import Data.List.Relation.Unary.All as All using (All; []; _∷_)
1413
open import Data.Nat.Base
1514
open import Data.Nat.Divisibility
1615
open import Data.Nat.GCD using (module GCD; module Bézout)
16+
open import Data.Nat.ListAction using (product)
17+
open import Data.Nat.ListAction.Properties using (product≢0)
1718
open import Data.Nat.Properties
1819
open import Data.Product.Base using (∃-syntax; _×_; map₂; _,_)
1920
open import Data.Sum.Base using (_⊎_; inj₁; inj₂; [_,_]′)

0 commit comments

Comments
 (0)