Skip to content

a better scanr, via Data.List.NonEmpty #2258

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

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ Deprecated modules
Deprecated names
----------------

* In `Data.List.Base`:
```agda
scanr : (A → B → B) → B → List A → List B
```

* In `Data.List.Properties`:
```agda
scanr-defn : scanr f e ≗ map (foldr f e) ∘ tails
```

* In `Data.Nat.Divisibility.Core`:
```agda
*-pres-∣ ↦ Data.Nat.Divisibility.*-pres-∣
Expand All @@ -37,6 +47,24 @@ Additions to existing modules
nonZeroIndex : Fin n → ℕ.NonZero n
```

* In `Data.List`:
```agda
scanr : (A → B → B) → B → List A → List B
```

* In `Data.List.NonEmpty.Base`:
```agda
tails⁺ : List A → List⁺ (List A)
scanr⁺ : (A → B → B) → B → List A → List⁺ B
```

* In `Data.List.NonEmpty.Properties`:
```agda
toList-tails⁺ : toList ∘ tails⁺ ≗ List.tails
scanr⁺-defn : scanr⁺ f e ≗ map (List.foldr f e) ∘ tails⁺
toList-scanr⁺ : toList ∘ scanr⁺ f e ≗ List.map (List.foldr f e) ∘ List.tails
```

* In `Data.List.Relation.Unary.All.Properties`:
```agda
All-catMaybes⁺ : All (Maybe.All P) xs → All P (catMaybes xs)
Expand Down
25 changes: 24 additions & 1 deletion src/Data/List.agda
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,30 @@

module Data.List where

import Data.List.NonEmpty.Base as List⁺
open import Function.Base using (_∘_)

------------------------------------------------------------------------
-- Types and basic operations

open import Data.List.Base public
open import Data.List.Base public hiding (scanr)

------------------------------------------------------------------------
-- scanr

module _ {a b} {A : Set a} {B : Set b} (f : A → B → B) (e : B) where

open List⁺ using (toList; scanr⁺)

-- definition

scanr : List A → List B
scanr = toList ∘ scanr⁺ f e
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is screaming to me that we're doing something wrong here with the dependencies. Can we discuss this evening at the stdlib implementor's meeting?

Copy link
Contributor Author

@jamesmckinna jamesmckinna Jan 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this is the motor for this PR: AFAICT, the Data.List.Base defn of scanr arises in the (historical) form it takes precisely to ensure that it can exist in Data.List.Base, without any of the other analysis of its typing.

But, as other commenters elsewhere note, the 'official' library definition has terrible properties (or better: terrible proofs of its properties...) because of its 'wrinkly' definition, arising from its combination of with and dead-code.

The other operations considered in #2267 / #2269 are OK, but scanr seems 'special'.

This PR represents my attempt at the least-invasive restructuring of Data.List.Base able to avoid the wrinkles...


{-
-- which satisfies the following property:

scanr-defn : scanr ≗ map (foldr f e) ∘ tails
scanr-defn = Data.List.NonEmpty.Properties.toList-scanr⁺
-}

19 changes: 13 additions & 6 deletions src/Data/List/Base.agda
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,6 @@ updateAt (x ∷ xs) (suc i) f = x ∷ updateAt xs i f

-- Scans

scanr : (A → B → B) → B → List A → List B
scanr f e [] = e ∷ []
scanr f e (x ∷ xs) with scanr f e xs
... | [] = [] -- dead branch
... | y ∷ ys = f x y ∷ y ∷ ys

scanl : (A → B → A) → A → List B → List A
scanl f e [] = e ∷ []
scanl f e (x ∷ xs) = e ∷ scanl f (f e x) xs
Expand Down Expand Up @@ -570,3 +564,16 @@ _─_ = removeAt
"Warning: _─_ was deprecated in v2.0.
Please use removeAt instead."
#-}

-- Version 2.1

scanr : (A → B → B) → B → List A → List B
scanr f e [] = e ∷ []
scanr f e (x ∷ xs) with scanr f e xs
... | [] = [] -- dead branch
... | ys@(y ∷ _) = f x y ∷ ys
{-# WARNING_ON_USAGE scanr
"Warning: scanr was deprecated in v2.1.
Please use List.scanr instead."
#-}

15 changes: 15 additions & 0 deletions src/Data/List/NonEmpty/Base.agda
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,21 @@ concatMap f = concat ∘′ map f
ap : List⁺ (A → B) → List⁺ A → List⁺ B
ap fs as = concatMap (λ f → map f as) fs

-- Tails

tails⁺ : List A → List⁺ (List A)
tails⁺ xs = xs ∷ go xs
where
go : List A → List (List A)
go [] = []
go (x ∷ xs) = xs ∷ go xs

-- Scanr

scanr⁺ : (f : A → B → B) (e : B) → List A → List⁺ B
scanr⁺ f e [] = e ∷ []
scanr⁺ f e (x ∷ xs) = let y ∷ ys = scanr⁺ f e xs in f x y ∷ y ∷ ys

-- Reverse

reverse : List⁺ A → List⁺ A
Expand Down
27 changes: 27 additions & 0 deletions src/Data/List/NonEmpty/Properties.agda
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ private
η : ∀ (xs : List⁺ A) → head xs ∷ tail xs ≡ toList xs
η _ = refl

toList-injective : {xs ys : List⁺ A} → toList xs ≡ toList ys → xs ≡ ys
toList-injective refl = refl

toList-fromList : ∀ x (xs : List A) → x ∷ xs ≡ toList (x ∷ xs)
toList-fromList _ _ = refl

Expand Down Expand Up @@ -113,6 +116,30 @@ map-cong f≗g (x ∷ xs) = cong₂ _∷_ (f≗g x) (List.map-cong f≗g xs)
map-∘ : {g : B → C} {f : A → B} → map (g ∘ f) ≗ map g ∘ map f
map-∘ (x ∷ xs) = cong (_ ∷_) (List.map-∘ xs)

------------------------------------------------------------------------
-- tails

toList-tails⁺ : toList ∘ tails⁺ ≗ List.tails {A = A}
toList-tails⁺ [] = refl
toList-tails⁺ ys@(_ ∷ xs) = cong (ys ∷_) (toList-tails⁺ xs)

------------------------------------------------------------------------
-- scanr

module _ (f : A → B → B) (e : B) where

scanr⁺-defn : scanr⁺ f e ≗ map (List.foldr f e) ∘ tails⁺
scanr⁺-defn [] = refl
scanr⁺-defn (x ∷ []) = refl
scanr⁺-defn (x ∷ xs@(_ ∷ _)) = let eq = scanr⁺-defn xs
in cong₂ (λ z → f x z ∷_) (cong head eq) (cong toList eq)

toList-scanr⁺ : toList ∘ scanr⁺ f e ≗ List.map (List.foldr f e) ∘ List.tails
toList-scanr⁺ [] = refl
toList-scanr⁺ (x ∷ []) = refl
toList-scanr⁺ (x ∷ xs@(_ ∷ _)) = let eq = toList-scanr⁺ xs
in cong₂ (λ z → f x z ∷_) (List.∷-injectiveˡ eq) eq

------------------------------------------------------------------------
-- groupSeqs

Expand Down
29 changes: 17 additions & 12 deletions src/Data/List/Properties.agda
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
-- equalities than _≡_.

{-# OPTIONS --cubical-compatible --safe #-}
{-# OPTIONS --warn=noUserWarning #-} -- for deprecated `scanr` (PR #2258)

module Data.List.Properties where

Expand Down Expand Up @@ -618,18 +619,6 @@ sum-++ (x ∷ xs) ys = begin
∈⇒∣product {n} {n ∷ ns} (here refl) = divides (product ns) (*-comm n (product ns))
∈⇒∣product {n} {m ∷ ns} (there n∈ns) = ∣n⇒∣m*n m (∈⇒∣product n∈ns)

------------------------------------------------------------------------
-- scanr

scanr-defn : ∀ (f : A → B → B) (e : B) →
scanr f e ≗ map (foldr f e) ∘ tails
scanr-defn f e [] = refl
scanr-defn f e (x ∷ []) = refl
scanr-defn f e (x ∷ y∷xs@(_ ∷ _))
with eq ← scanr-defn f e y∷xs
with z ∷ zs ← scanr f e y∷xs
= let z≡fy⦇f⦈xs , _ = ∷-injective eq in cong₂ (λ z → f x z ∷_) z≡fy⦇f⦈xs eq

------------------------------------------------------------------------
-- scanl

Expand Down Expand Up @@ -1292,3 +1281,19 @@ map-─ = map-removeAt
"Warning: map-─ was deprecated in v2.0.
Please use map-removeAt instead."
#-}

-- Version 2.1

scanr-defn : ∀ (f : A → B → B) (e : B) →
scanr f e ≗ map (foldr f e) ∘ tails
scanr-defn f e [] = refl
scanr-defn f e (x ∷ []) = refl
scanr-defn f e (x ∷ xs@(_ ∷ _))
with eq ← scanr-defn f e xs
with ys@(_ ∷ _) ← scanr f e xs
= cong₂ (λ z → f x z ∷_) (∷-injectiveˡ eq) eq
{-# WARNING_ON_USAGE scanr-defn
"Warning: scanr-defn was deprecated in v2.1.
Please use Data.List.NonEmpty.toList-scanr⁺ instead."
#-}