-
Notifications
You must be signed in to change notification settings - Fork 242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ refactor ] Add Data.Nat.ListAction
#2558
Open
jamesmckinna
wants to merge
13
commits into
agda:master
Choose a base branch
from
jamesmckinna:issue2553
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
350eebf
move code to new module
jamesmckinna 11b41ea
refactor: deprecate old definitions
jamesmckinna 22873c6
refactor: rejig `Nat` imports to uncouple `sum`, `product` form `length`
jamesmckinna 3da9f74
refactor: `CHANGELOG`
jamesmckinna d98b257
refactor: move and deprecate
jamesmckinna 56d0a74
refactor: move+deprecate
jamesmckinna e37fe01
refactor: cosmetic, tighten `import`s
jamesmckinna c1423d2
refactor: fix deprecation problem
jamesmckinna a3585d3
Merge branch 'master' into issue2553
jamesmckinna e871651
refactor: renamed new module
jamesmckinna a2e7f54
refactor: introduce `Properties` module
jamesmckinna ed3936d
bug: add the new module!
jamesmckinna 0db6b09
Merge branch 'master' into issue2553
jamesmckinna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
------------------------------------------------------------------------ | ||
-- The Agda standard library | ||
-- | ||
-- Natural numbers: sum and product of lists | ||
-- | ||
-- Issue #2553: this is a compatibility stub module, | ||
-- ahead of a more thorough breaking set of changes. | ||
------------------------------------------------------------------------ | ||
|
||
{-# OPTIONS --cubical-compatible --safe #-} | ||
|
||
module Data.Nat.ListAction where | ||
|
||
open import Algebra.Bundles using (CommutativeMonoid) | ||
open import Data.List.Base using (List; []; _∷_; _++_; foldr) | ||
open import Data.List.Membership.Propositional using (_∈_) | ||
open import Data.List.Relation.Binary.Permutation.Propositional using (_↭_; ↭⇒↭ₛ) | ||
open import Data.List.Relation.Binary.Permutation.Setoid.Properties | ||
using (foldr-commMonoid) | ||
open import Data.List.Relation.Unary.All using (All; []; _∷_) | ||
open import Data.List.Relation.Unary.Any using (here; there) | ||
open import Data.Nat.Base using (ℕ; _+_; _*_; NonZero; _≤_) | ||
open import Data.Nat.Divisibility using (_∣_; m∣m*n; ∣n⇒∣m*n) | ||
open import Data.Nat.Properties | ||
using (+-assoc; *-assoc; *-identityˡ; m*n≢0; m≤m*n; m≤n⇒m≤o*n; | ||
+-0-commutativeMonoid; *-1-commutativeMonoid) | ||
open import Relation.Binary.Core using (_Preserves_⟶_) | ||
open import Relation.Binary.PropositionalEquality.Core | ||
using (_≡_; refl; sym; cong) | ||
open import Relation.Binary.PropositionalEquality.Properties | ||
using (module ≡-Reasoning) | ||
|
||
private | ||
variable | ||
m n : ℕ | ||
ms ns : List ℕ | ||
|
||
|
||
------------------------------------------------------------------------ | ||
-- Definitions | ||
|
||
sum : List ℕ → ℕ | ||
sum = foldr _+_ 0 | ||
|
||
product : List ℕ → ℕ | ||
product = foldr _*_ 1 | ||
|
||
------------------------------------------------------------------------ | ||
-- Properties | ||
|
||
-- sum | ||
|
||
sum-++ : ∀ ms ns → sum (ms ++ ns) ≡ sum ms + sum ns | ||
sum-++ [] ns = refl | ||
sum-++ (m ∷ ms) ns = begin | ||
m + sum (ms ++ ns) ≡⟨ cong (m +_) (sum-++ ms ns) ⟩ | ||
m + (sum ms + sum ns) ≡⟨ +-assoc m _ _ ⟨ | ||
(m + sum ms) + sum ns ∎ | ||
where open ≡-Reasoning | ||
|
||
sum-↭ : sum Preserves _↭_ ⟶ _≡_ | ||
sum-↭ p = foldr-commMonoid ℕ-+-0.setoid ℕ-+-0.isCommutativeMonoid (↭⇒↭ₛ p) | ||
where module ℕ-+-0 = CommutativeMonoid +-0-commutativeMonoid | ||
|
||
|
||
-- product | ||
|
||
product-++ : ∀ ms ns → product (ms ++ ns) ≡ product ms * product ns | ||
product-++ [] ns = sym (*-identityˡ _) | ||
product-++ (m ∷ ms) ns = begin | ||
m * product (ms ++ ns) ≡⟨ cong (m *_) (product-++ ms ns) ⟩ | ||
m * (product ms * product ns) ≡⟨ *-assoc m _ _ ⟨ | ||
(m * product ms) * product ns ∎ | ||
where open ≡-Reasoning | ||
|
||
∈⇒∣product : n ∈ ns → n ∣ product ns | ||
∈⇒∣product {ns = n ∷ ns} (here refl) = m∣m*n (product ns) | ||
∈⇒∣product {ns = m ∷ ns} (there n∈ns) = ∣n⇒∣m*n m (∈⇒∣product n∈ns) | ||
|
||
product≢0 : All NonZero ns → NonZero (product ns) | ||
product≢0 [] = _ | ||
product≢0 (n≢0 ∷ ns≢0) = m*n≢0 _ _ {{n≢0}} {{product≢0 ns≢0}} | ||
|
||
∈⇒≤product : All NonZero ns → n ∈ ns → n ≤ product ns | ||
∈⇒≤product (n≢0 ∷ ns≢0) (here refl) = m≤m*n _ _ {{product≢0 ns≢0}} | ||
∈⇒≤product (n≢0 ∷ ns≢0) (there n∈ns) = m≤n⇒m≤o*n _ {{n≢0}} (∈⇒≤product ns≢0 n∈ns) | ||
|
||
product-↭ : product Preserves _↭_ ⟶ _≡_ | ||
product-↭ p = foldr-commMonoid ℕ-*-1.setoid ℕ-*-1.isCommutativeMonoid (↭⇒↭ₛ p) | ||
where module ℕ-*-1 = CommutativeMonoid *-1-commutativeMonoid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I think these ought to be in
Data.Nat.ListAction.Properties
. I still will want to havesum
andproduct
available with a decently small footprint (dependency-wise) and bundling properties with definitions defeats that.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And a corresponding
Base
module for the definitions?Actually that tends not to be the case for eg
All
andAny
, so perhaps not needed (certainly at this stage)?