-
Notifications
You must be signed in to change notification settings - Fork 247
Refactor Data.List.Base.scan*
and their properties
#2395
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
acd4830
refactor `scanr` etc.
jamesmckinna 2426a96
restore `inits` etc. but lazier; plus knock-on consequences
jamesmckinna 833d9c1
more refactoring; plus knock-on consequences
jamesmckinna cfded46
tidy up
jamesmckinna 268fbb7
refactored into `Base`
jamesmckinna a74fba4
... and `Properties`
jamesmckinna 1c1e079
fix-up `inits` and `tails`
jamesmckinna f2b1d61
fix up `import`s
jamesmckinna c7af1ef
knock-ons
jamesmckinna 3cc0b3c
Andreas's suggestions
jamesmckinna 9fef63a
further, better, refactoring is possible
jamesmckinna b3d45a7
yet further, better, refactoring is possible: remove explicit equatio…
jamesmckinna 3f5ad53
Update CHANGELOG.md
jamesmckinna aa26560
Update Base.agda
jamesmckinna f70cf4d
Update Properties.agda
jamesmckinna 0efdcd0
Update CHANGELOG.md
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,106 @@ | ||
------------------------------------------------------------------------ | ||
-- The Agda standard library | ||
-- | ||
-- List scans: definition and properties | ||
------------------------------------------------------------------------ | ||
|
||
{-# OPTIONS --cubical-compatible --safe #-} | ||
|
||
module Data.List.Scans where | ||
|
||
open import Data.List.Base as List using (List; []; _∷_) | ||
open import Data.List.NonEmpty.Base as List⁺ using (List⁺; _∷_; toList) | ||
import Data.List.Properties as List | ||
import Data.List.NonEmpty.Properties as List⁺ | ||
open import Function.Base using (_∘_; _$_) | ||
open import Level using (Level) | ||
open import Relation.Binary.PropositionalEquality.Core | ||
using (_≡_; _≗_; refl; trans; cong; cong₂) | ||
open import Relation.Binary.PropositionalEquality.Properties using (module ≡-Reasoning) | ||
|
||
private | ||
variable | ||
a b : Level | ||
A : Set a | ||
B : Set b | ||
|
||
|
||
------------------------------------------------------------------------ | ||
-- Definitions | ||
|
||
-- Scanr | ||
|
||
module _ (f : A → B → B) where | ||
|
||
scanr⁺ : (e : B) → List A → List⁺ B | ||
scanr⁺ e [] = e ∷ [] | ||
scanr⁺ e (x ∷ xs) = let y ∷ ys = scanr⁺ e xs in f x y ∷ y ∷ ys | ||
|
||
scanr : (e : B) → List A → List B | ||
scanr e = toList ∘ scanr⁺ e | ||
|
||
-- Scanl | ||
|
||
module _ (f : A → B → A) where | ||
|
||
scanl⁺ : A → List B → List⁺ A | ||
scanl⁺ e xs = e ∷ go e xs | ||
where | ||
go : A → List B → List A | ||
go _ [] = [] | ||
go e (x ∷ xs) = let fex = f e x in fex ∷ go fex xs | ||
|
||
scanl : A → List B → List A | ||
scanl e = toList ∘ scanl⁺ e | ||
|
||
------------------------------------------------------------------------ | ||
-- Properties | ||
jamesmckinna marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
-- scanr⁺ and scanr | ||
|
||
module _ (f : A → B → B) (e : B) where | ||
|
||
private | ||
h = List.foldr f e | ||
|
||
scanr⁺-defn : scanr⁺ f e ≗ List⁺.map h ∘ List⁺.tails | ||
scanr⁺-defn [] = refl | ||
scanr⁺-defn (x ∷ xs) = let eq = scanr⁺-defn xs | ||
in cong₂ (λ z → f x z ∷_) (cong List⁺.head eq) (cong toList eq) | ||
|
||
scanr-defn : scanr f e ≗ List.map h ∘ List.tails | ||
scanr-defn xs = begin | ||
scanr f e xs | ||
≡⟨ cong toList (scanr⁺-defn xs) ⟩ | ||
toList (List⁺.map h (List⁺.tails xs)) | ||
≡⟨⟩ | ||
List.map h (toList (List⁺.tails xs)) | ||
≡⟨ cong (List.map h) (List⁺.toList-tails xs) ⟩ | ||
List.map h (List.tails xs) | ||
∎ | ||
where open ≡-Reasoning | ||
|
||
-- scanl⁺ and scanl | ||
|
||
module _ (f : A → B → A) where | ||
|
||
private | ||
h = List.foldl f | ||
|
||
scanl⁺-defn : ∀ e → scanl⁺ f e ≗ List⁺.map (h e) ∘ List⁺.inits | ||
scanl⁺-defn e [] = refl | ||
scanl⁺-defn e (x ∷ xs) = let eq = scanl⁺-defn (f e x) xs in | ||
cong (e ∷_) $ cong (f e x ∷_) $ trans (cong List⁺.tail eq) (List.map-∘ _) | ||
|
||
scanl-defn : ∀ e → scanl f e ≗ List.map (h e) ∘ List.inits | ||
scanl-defn e [] = refl | ||
scanl-defn e (x ∷ xs) = cong (e ∷_) $ begin | ||
scanl f (f e x) xs | ||
≡⟨ scanl-defn (f e x) xs ⟩ | ||
List.map (h (f e x)) (List.inits xs) | ||
≡⟨⟩ | ||
List.map (h e ∘ (x ∷_)) (List.inits xs) | ||
≡⟨ List.map-∘ (List.inits xs) ⟩ | ||
List.map (h e) (List.map (x ∷_) (List.inits xs)) | ||
∎ | ||
where open ≡-Reasoning |
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.
Uh oh!
There was an error while loading. Please reload this page.