Skip to content

Some lemmata for non-empty lists #2730

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

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
c6da092
[add]: map-id for Data.List.NonEmpty
pmbittner Jun 8, 2025
e5ac9d9
[changelog]: map-id for Data.List.NonEmpty
pmbittner Jun 8, 2025
882ba44
[add]: some lemmata for ⁺++⁺ of Data.List.NonEmpty
pmbittner Jun 8, 2025
1e7d355
[changelog]: some lemmata for ⁺++⁺
pmbittner Jun 8, 2025
873400c
[add]: ∷→∷⁺ and ∷⁺→∷
pmbittner Jun 8, 2025
357e661
[changelog]: ∷→∷⁺ and ∷⁺→∷
pmbittner Jun 8, 2025
c30fcec
[add]: ⁺++⁺-assoc for Data.List.NonEmpty
pmbittner Jun 8, 2025
be0a8da
[changelog]: ⁺++⁺-assoc for Data.List.NonEmpty
pmbittner Jun 8, 2025
f73e379
[add]: ⁺++⁺-cancelˡ for Data.List.Empty
pmbittner Jun 8, 2025
4ecdc8b
[changelog]: ⁺++⁺-cancelˡ for Data.List.Empty
pmbittner Jun 8, 2025
5aa3de0
[add]: ⁺++⁺-cancelʳ for Data.List.Empty
pmbittner Jun 8, 2025
8587e4b
[changelog]: ⁺++⁺-cancelʳ for Data.List.Empty
pmbittner Jun 8, 2025
f14822a
[add]: ⁺++⁺-cancel for Data.List.NonEmpty
pmbittner Jun 8, 2025
c4ae852
[changelog]: ⁺++⁺-cancel for Data.List.NonEmpty
pmbittner Jun 8, 2025
805671a
[add]: map-⁺++⁺
pmbittner Jun 8, 2025
73462b7
[changelog]: map-⁺++⁺
pmbittner Jun 8, 2025
7e4d589
[changelog] remove implicit args in List.NonEmpty
pmbittner Jun 10, 2025
cc5e482
[changelog]: style adaptions for List.NonEmpty
pmbittner Jun 10, 2025
1887e1e
[add]: length-++-≤ in Data.List.Properties
pmbittner Jun 10, 2025
238b81f
[changelog]: length-++-≤ in Data.List.Properties
pmbittner Jun 10, 2025
658a9b0
[refactor] avoid rewrite in length-⁺++⁺-≤
pmbittner Jun 10, 2025
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
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ Additions to existing modules

* In `Data.List.Properties`:
```agda
length-++-≤ : ∀ (xs : List A) → length xs ≤ length (xs ++ ys)
Copy link
Member

Choose a reason for hiding this comment

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

Feels like this should have a ˡ suffix and we should have the ʳ variant too.

map-applyUpTo : ∀ (f : ℕ → A) (g : A → B) n → map g (applyUpTo f n) ≡ applyUpTo (g ∘ f) n
map-applyDownFrom : ∀ (f : ℕ → A) (g : A → B) n → map g (applyDownFrom f n) ≡ applyDownFrom (g ∘ f) n
map-upTo : ∀ (f : ℕ → A) n → map f (upTo n) ≡ applyUpTo f n
Expand All @@ -276,6 +277,22 @@ Additions to existing modules
filter-↭ : ∀ (P? : Pred.Decidable P) → xs ↭ ys → filter P? xs ↭ filter P? ys
```

* In `Data.List.NonEmpty.Properties`:
```agda
∷→∷⁺ : (x List.∷ xs) ≡ (y List.∷ ys) →
(x List⁺.∷ xs) ≡ (y List⁺.∷ ys)
∷⁺→∷ : (x List⁺.∷ xs) ≡ (y List⁺.∷ ys) →
(x List.∷ xs) ≡ (y List.∷ ys)
length-⁺++⁺ : (xs ys : List⁺ A) → length (xs ⁺++⁺ ys) ≡ length xs + length ys
length-⁺++⁺-≤ : (xs ys : List⁺ A) → length xs ≤ length (xs ⁺++⁺ ys)
Copy link
Member

Choose a reason for hiding this comment

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

Same remark about suffixes.

map-⁺++⁺ : ∀ (f : A → B) xs ys → map f (xs ⁺++⁺ ys) ≡ map f xs ⁺++⁺ map f ys
⁺++⁺-assoc : Associative _⁺++⁺_
⁺++⁺-cancelˡ : LeftCancellative _⁺++⁺_
⁺++⁺-cancelʳ : RightCancellative _⁺++⁺_
⁺++⁺-cancel : Cancellative _⁺++⁺_
map-id : map id ≗ id {A = List⁺ A}
```

* In `Data.Product.Function.Dependent.Propositional`:
```agda
Σ-↪ : (I↪J : I ↪ J) → (∀ {j} → A (from I↪J j) ↪ B j) → Σ I A ↪ Σ J B
Expand Down
53 changes: 50 additions & 3 deletions src/Data/List/NonEmpty/Properties.agda
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,28 @@

module Data.List.NonEmpty.Properties where

import Algebra.Definitions as AlgebraicDefinitions
open import Effect.Monad using (RawMonad)
open import Data.Nat.Base using (suc; _+_)
open import Data.Nat.Base using (suc; _+_; _≤_; s≤s)
open import Data.Nat.Properties using (suc-injective)
open import Data.Maybe.Properties using (just-injective)
open import Data.Bool.Base using (Bool; true; false)
open import Data.List.Base as List using (List; []; _∷_; _++_)
open import Data.List.Effectful using () renaming (monad to listMonad)
open import Data.List.Properties using (length-++; length-++-≤; ++-assoc; map-++)
open import Data.List.NonEmpty.Effectful using () renaming (monad to list⁺Monad)
open import Data.List.NonEmpty
open import Data.List.NonEmpty as List⁺
using (List⁺; _∷_; tail; head; toList; _⁺++_; _⁺++⁺_; _++⁺_; length; fromList;
drop+; map; inits; tails; groupSeqs; ungroupSeqs)
open import Data.List.NonEmpty.Relation.Unary.All using (All; toList⁺; _∷_)
open import Data.List.Relation.Unary.All using ([]; _∷_) renaming (All to ListAll)
import Data.List.Properties as List
open import Data.Product.Base using (_,_)
open import Data.Sum.Base using (inj₁; inj₂)
open import Data.Sum.Relation.Unary.All using (inj₁; inj₂)
import Data.Sum.Relation.Unary.All as Sum using (All; inj₁; inj₂)
open import Level using (Level)
open import Function.Base using (_∘_; _$_)
open import Function.Base using (id; _∘_; _$_)
open import Relation.Binary.PropositionalEquality.Core
using (_≡_; refl; cong; cong₂; _≗_)
open import Relation.Binary.PropositionalEquality.Properties
Expand Down Expand Up @@ -69,6 +72,47 @@ toList->>= f (x ∷ xs) = begin
List.concat (List.map toList (List.map f (x ∷ xs)))

-- turning equalities of lists that are not empty to equalities on non-empty lists ...
∷→∷⁺ : ∀ {x y : A} {xs ys : List A} →
(x List.∷ xs) ≡ (y List.∷ ys) →
(x List⁺.∷ xs) ≡ (y List⁺.∷ ys)
∷→∷⁺ refl = refl

-- ... and vice versa
∷⁺→∷ : ∀ {x y : A} {xs ys : List A} →
(x List⁺.∷ xs) ≡ (y List⁺.∷ ys) →
(x List.∷ xs) ≡ (y List.∷ ys)
∷⁺→∷ refl = refl

------------------------------------------------------------------------
-- _⁺++⁺_
length-⁺++⁺ : (xs ys : List⁺ A) →
length (xs ⁺++⁺ ys) ≡ length xs + length ys
length-⁺++⁺ (x ∷ xs) (y ∷ ys) = length-++ (x ∷ xs)

length-⁺++⁺-≤ : (xs ys : List⁺ A) →
length xs ≤ length (xs ⁺++⁺ ys)
length-⁺++⁺-≤ (x ∷ xs) (y ∷ ys) = s≤s (length-++-≤ xs)

map-⁺++⁺ : ∀ (f : A → B) xs ys →
map f (xs ⁺++⁺ ys) ≡ map f xs ⁺++⁺ map f ys
map-⁺++⁺ f (x ∷ xs) (y ∷ ys) = ∷→∷⁺ (map-++ f (x ∷ xs) (y ∷ ys))

module _ {A : Set a} where
open AlgebraicDefinitions {A = List⁺ A} _≡_

⁺++⁺-assoc : Associative _⁺++⁺_
⁺++⁺-assoc (x ∷ xs) (y ∷ ys) (z ∷ zs) = cong (x ∷_) (++-assoc xs (y ∷ ys) (z ∷ zs))

⁺++⁺-cancelˡ : LeftCancellative _⁺++⁺_
⁺++⁺-cancelˡ (x ∷ xs) (y ∷ ys) (z ∷ zs) eq = ∷→∷⁺ (List.++-cancelˡ (x ∷ xs) (y ∷ ys) (z ∷ zs) (∷⁺→∷ eq))

⁺++⁺-cancelʳ : RightCancellative _⁺++⁺_
⁺++⁺-cancelʳ (x ∷ xs) (y ∷ ys) (z ∷ zs) eq = ∷→∷⁺ (List.++-cancelʳ (x ∷ xs) (y ∷ ys) (z ∷ zs) (∷⁺→∷ eq))

⁺++⁺-cancel : Cancellative _⁺++⁺_
⁺++⁺-cancel = ⁺++⁺-cancelˡ , ⁺++⁺-cancelʳ

------------------------------------------------------------------------
-- _++⁺_

Expand Down Expand Up @@ -118,6 +162,9 @@ 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)

map-id : map id ≗ id {A = List⁺ A}
map-id (x ∷ xs) = cong (x ∷_) (List.map-id xs)

------------------------------------------------------------------------
-- inits

Expand Down
5 changes: 5 additions & 0 deletions src/Data/List/Properties.agda
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ length-++ : ∀ (xs : List A) {ys} →
length-++ [] = refl
length-++ (x ∷ xs) = cong suc (length-++ xs)

length-++-≤ : ∀ (xs : List A) {ys} →
length xs ≤ length (xs ++ ys)
length-++-≤ [] = z≤n
length-++-≤ (x ∷ xs) = s≤s (length-++-≤ xs)

module _ {A : Set a} where

open AlgebraicDefinitions {A = List A} _≡_
Expand Down