|
8 | 8 |
|
9 | 9 | module Data.List.NonEmpty.Properties where
|
10 | 10 |
|
| 11 | +import Algebra.Definitions as AlgebraicDefinitions |
11 | 12 | open import Effect.Monad using (RawMonad)
|
12 |
| -open import Data.Nat.Base using (suc; _+_) |
| 13 | +open import Data.Nat.Base using (suc; _+_; _≤_; s≤s) |
13 | 14 | open import Data.Nat.Properties using (suc-injective)
|
14 | 15 | open import Data.Maybe.Properties using (just-injective)
|
15 | 16 | open import Data.Bool.Base using (Bool; true; false)
|
16 | 17 | open import Data.List.Base as List using (List; []; _∷_; _++_)
|
17 | 18 | open import Data.List.Effectful using () renaming (monad to listMonad)
|
| 19 | +open import Data.List.Properties using (length-++; length-++-comm; length-++-≤ˡ; length-++-≤ʳ; ++-assoc; map-++) |
18 | 20 | open import Data.List.NonEmpty.Effectful using () renaming (monad to list⁺Monad)
|
19 |
| -open import Data.List.NonEmpty |
| 21 | +open import Data.List.NonEmpty as List⁺ |
20 | 22 | using (List⁺; _∷_; tail; head; toList; _⁺++_; _⁺++⁺_; _++⁺_; length; fromList;
|
21 | 23 | drop+; map; inits; tails; groupSeqs; ungroupSeqs)
|
22 | 24 | open import Data.List.NonEmpty.Relation.Unary.All using (All; toList⁺; _∷_)
|
23 | 25 | open import Data.List.Relation.Unary.All using ([]; _∷_) renaming (All to ListAll)
|
24 | 26 | import Data.List.Properties as List
|
| 27 | +open import Data.Product.Base using (_,_) |
25 | 28 | open import Data.Sum.Base using (inj₁; inj₂)
|
26 | 29 | open import Data.Sum.Relation.Unary.All using (inj₁; inj₂)
|
27 | 30 | import Data.Sum.Relation.Unary.All as Sum using (All; inj₁; inj₂)
|
28 | 31 | open import Level using (Level)
|
29 |
| -open import Function.Base using (_∘_; _$_) |
| 32 | +open import Function.Base using (id; _∘_; _$_) |
30 | 33 | open import Relation.Binary.PropositionalEquality.Core
|
31 | 34 | using (_≡_; refl; cong; cong₂; _≗_)
|
32 | 35 | open import Relation.Binary.PropositionalEquality.Properties
|
@@ -69,6 +72,56 @@ toList->>= f (x ∷ xs) = begin
|
69 | 72 | List.concat (List.map toList (List.map f (x ∷ xs)))
|
70 | 73 | ∎
|
71 | 74 |
|
| 75 | +-- turning equalities of lists that are not empty to equalities on non-empty lists ... |
| 76 | +∷→∷⁺ : ∀ {x y : A} {xs ys : List A} → |
| 77 | + (x List.∷ xs) ≡ (y List.∷ ys) → |
| 78 | + (x List⁺.∷ xs) ≡ (y List⁺.∷ ys) |
| 79 | +∷→∷⁺ refl = refl |
| 80 | + |
| 81 | +-- ... and vice versa |
| 82 | +∷⁺→∷ : ∀ {x y : A} {xs ys : List A} → |
| 83 | + (x List⁺.∷ xs) ≡ (y List⁺.∷ ys) → |
| 84 | + (x List.∷ xs) ≡ (y List.∷ ys) |
| 85 | +∷⁺→∷ refl = refl |
| 86 | + |
| 87 | +------------------------------------------------------------------------ |
| 88 | +-- _⁺++⁺_ |
| 89 | + |
| 90 | +length-⁺++⁺ : (xs ys : List⁺ A) → |
| 91 | + length (xs ⁺++⁺ ys) ≡ length xs + length ys |
| 92 | +length-⁺++⁺ (x ∷ xs) (y ∷ ys) = length-++ (x ∷ xs) |
| 93 | + |
| 94 | +length-⁺++⁺-comm : ∀ (xs ys : List⁺ A) → |
| 95 | + length (xs ⁺++⁺ ys) ≡ length (ys ⁺++⁺ xs) |
| 96 | +length-⁺++⁺-comm (x ∷ xs) (y ∷ ys) = length-++-comm (x ∷ xs) (y ∷ ys) |
| 97 | + |
| 98 | +length-⁺++⁺-≤ˡ : (xs ys : List⁺ A) → |
| 99 | + length xs ≤ length (xs ⁺++⁺ ys) |
| 100 | +length-⁺++⁺-≤ˡ (x ∷ xs) (y ∷ ys) = s≤s (length-++-≤ˡ xs) |
| 101 | + |
| 102 | +length-⁺++⁺-≤ʳ : (xs ys : List⁺ A) → |
| 103 | + length ys ≤ length (xs ⁺++⁺ ys) |
| 104 | +length-⁺++⁺-≤ʳ (x ∷ xs) (y ∷ ys) = length-++-≤ʳ (y ∷ ys) {x ∷ xs} |
| 105 | + |
| 106 | +map-⁺++⁺ : ∀ (f : A → B) xs ys → |
| 107 | + map f (xs ⁺++⁺ ys) ≡ map f xs ⁺++⁺ map f ys |
| 108 | +map-⁺++⁺ f (x ∷ xs) (y ∷ ys) = ∷→∷⁺ (map-++ f (x ∷ xs) (y ∷ ys)) |
| 109 | + |
| 110 | +module _ {A : Set a} where |
| 111 | + open AlgebraicDefinitions {A = List⁺ A} _≡_ |
| 112 | + |
| 113 | + ⁺++⁺-assoc : Associative _⁺++⁺_ |
| 114 | + ⁺++⁺-assoc (x ∷ xs) (y ∷ ys) (z ∷ zs) = cong (x ∷_) (++-assoc xs (y ∷ ys) (z ∷ zs)) |
| 115 | + |
| 116 | + ⁺++⁺-cancelˡ : LeftCancellative _⁺++⁺_ |
| 117 | + ⁺++⁺-cancelˡ (x ∷ xs) (y ∷ ys) (z ∷ zs) eq = ∷→∷⁺ (List.++-cancelˡ (x ∷ xs) (y ∷ ys) (z ∷ zs) (∷⁺→∷ eq)) |
| 118 | + |
| 119 | + ⁺++⁺-cancelʳ : RightCancellative _⁺++⁺_ |
| 120 | + ⁺++⁺-cancelʳ (x ∷ xs) (y ∷ ys) (z ∷ zs) eq = ∷→∷⁺ (List.++-cancelʳ (x ∷ xs) (y ∷ ys) (z ∷ zs) (∷⁺→∷ eq)) |
| 121 | + |
| 122 | + ⁺++⁺-cancel : Cancellative _⁺++⁺_ |
| 123 | + ⁺++⁺-cancel = ⁺++⁺-cancelˡ , ⁺++⁺-cancelʳ |
| 124 | + |
72 | 125 | ------------------------------------------------------------------------
|
73 | 126 | -- _++⁺_
|
74 | 127 |
|
@@ -118,6 +171,9 @@ map-cong f≗g (x ∷ xs) = cong₂ _∷_ (f≗g x) (List.map-cong f≗g xs)
|
118 | 171 | map-∘ : {g : B → C} {f : A → B} → map (g ∘ f) ≗ map g ∘ map f
|
119 | 172 | map-∘ (x ∷ xs) = cong (_ ∷_) (List.map-∘ xs)
|
120 | 173 |
|
| 174 | +map-id : map id ≗ id {A = List⁺ A} |
| 175 | +map-id (x ∷ xs) = cong (x ∷_) (List.map-id xs) |
| 176 | + |
121 | 177 | ------------------------------------------------------------------------
|
122 | 178 | -- inits
|
123 | 179 |
|
|
0 commit comments