Debian Codegraph @

src/Data/List/NonEmpty/Properties.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Properties of non-empty lists
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.List.NonEmpty.Properties where
10
11 import Algebra.Definitions as AlgebraicDefinitions
12 open import Effect.Monad using (RawMonad)
13 open import Data.Nat.Base using (suc; _+_; _≤_; s≤s)
14 open import Data.Nat.Properties using (suc-injective)
15 open import Data.Maybe.Properties using (just-injective)
16 open import Data.Bool.Base using (Bool; true; false)
17 open import Data.List.Base as List using (List; []; _∷_; _++_)
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-++)
20 open import Data.List.NonEmpty.Effectful using () renaming (monad to list⁺Monad)
21 open import Data.List.NonEmpty as List⁺
22 using (List⁺; _∷_; tail; head; toList; _⁺++_; _⁺++⁺_; _++⁺_; length; fromList;
23 drop+; map; inits; tails; groupSeqs; ungroupSeqs)
24 open import Data.List.NonEmpty.Relation.Unary.All using (All; toList⁺; _∷_)
25 open import Data.List.Relation.Unary.All using ([]; _∷_) renaming (All to ListAll)
26 import Data.List.Properties as List
27 open import Data.Product.Base using (_,_)
28 open import Data.Sum.Base using (inj₁; inj₂)
29 open import Data.Sum.Relation.Unary.All using (inj₁; inj₂)
30 import Data.Sum.Relation.Unary.All as Sum using (All; inj₁; inj₂)
31 open import Level using (Level)
32 open import Function.Base using (id; _∘_; _$_)
33 open import Relation.Binary.PropositionalEquality.Core
34 using (_≡_; refl; cong; cong₂; _≗_)
35 open import Relation.Binary.PropositionalEquality.Properties
36 using (module ≡-Reasoning)
37 open import Relation.Unary using (Pred; Decidable; ∁)
38 open import Relation.Nullary using (¬_; does; yes; no)
39
40 open ≡-Reasoning
41
42 private
43 variable
44 a p : Level
45 A B C : Set a
46
47 open module LMo {a} = RawMonad {f = a} listMonad
48 using () renaming (_>>=_ to _⋆>>=_)
49 open module L⁺Mo {a} = RawMonad {f = a} list⁺Monad
50
51 ------------------------------------------------------------------------
52 -- toList
53
54 η : ∀ (xs : List⁺ A) → head xs ∷ tail xs ≡ toList xs
55 η _ = refl
56
57 toList-fromList : ∀ x (xs : List A) → x ∷ xs ≡ toList (x ∷ xs)
58 toList-fromList _ _ = refl
59
60 toList-⁺++ : ∀ (xs : List⁺ A) ys → toList xs ++ ys ≡ toList (xs ⁺++ ys)
61 toList-⁺++ _ _ = refl
62
63 toList-⁺++⁺ : ∀ (xs ys : List⁺ A) →
64 toList xs ++ toList ys ≡ toList (xs ⁺++⁺ ys)
65 toList-⁺++⁺ _ _ = refl
66
67 toList->>= : ∀ (f : A → List⁺ B) (xs : List⁺ A) →
68 (toList xs ⋆>>= toList ∘ f) ≡ toList (xs >>= f)
69 toList->>= f (x ∷ xs) = begin
70 List.concat (List.map (toList ∘ f) (x ∷ xs))
71 ≡⟨ cong List.concat $ List.map-∘ {g = toList} (x ∷ xs) ⟩
72 List.concat (List.map toList (List.map f (x ∷ xs)))
73 ∎
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
125 ------------------------------------------------------------------------
126 -- _++⁺_
127
128 length-++⁺ : (xs : List A) (ys : List⁺ A) →
129 length (xs ++⁺ ys) ≡ List.length xs + length ys
130 length-++⁺ [] ys = refl
131 length-++⁺ (x ∷ xs) ys rewrite length-++⁺ xs ys = refl
132
133 length-++⁺-tail : (xs : List A) (ys : List⁺ A) →
134 length (xs ++⁺ ys) ≡ suc (List.length xs + List.length (List⁺.tail ys))
135 length-++⁺-tail [] ys = refl
136 length-++⁺-tail (x ∷ xs) ys rewrite length-++⁺-tail xs ys = refl
137
138 ++-++⁺ : (xs : List A) → ∀ {ys zs} → (xs ++ ys) ++⁺ zs ≡ xs ++⁺ ys ++⁺ zs
139 ++-++⁺ [] = refl
140 ++-++⁺ (x ∷ l) = cong (x ∷_) (cong toList (++-++⁺ l))
141
142 ++⁺-cancelˡ′ : ∀ xs ys {zs zs′ : List⁺ A} →
143 xs ++⁺ zs ≡ ys ++⁺ zs′ →
144 List.length xs ≡ List.length ys → zs ≡ zs′
145 ++⁺-cancelˡ′ [] [] eq eqxs = eq
146 ++⁺-cancelˡ′ (x ∷ xs) (y ∷ ys) eq eql = ++⁺-cancelˡ′ xs ys
147 (just-injective (cong fromList (cong List⁺.tail eq)))
148 (suc-injective eql)
149
150 ++⁺-cancelˡ : ∀ xs {ys zs : List⁺ A} → xs ++⁺ ys ≡ xs ++⁺ zs → ys ≡ zs
151 ++⁺-cancelˡ xs eq = ++⁺-cancelˡ′ xs xs eq refl
152
153 drop-+-++⁺ : ∀ (xs : List A) ys → drop+ (List.length xs) (xs ++⁺ ys) ≡ ys
154 drop-+-++⁺ [] ys = refl
155 drop-+-++⁺ (x ∷ xs) ys = drop-+-++⁺ xs ys
156
157 map-++⁺ : ∀ (f : A → B) xs ys →
158 map f (xs ++⁺ ys) ≡ List.map f xs ++⁺ map f ys
159 map-++⁺ f [] ys = refl
160 map-++⁺ f (x ∷ xs) ys = cong (λ zs → f x ∷ toList zs) (map-++⁺ f xs ys)
161
162 ------------------------------------------------------------------------
163 -- map
164
165 length-map : ∀ (f : A → B) xs → length (map f xs) ≡ length xs
166 length-map f (_ ∷ xs) = cong suc (List.length-map f xs)
167
168 map-cong : ∀ {f g : A → B} → f ≗ g → map f ≗ map g
169 map-cong f≗g (x ∷ xs) = cong₂ _∷_ (f≗g x) (List.map-cong f≗g xs)
170
171 map-∘ : {g : B → C} {f : A → B} → map (g ∘ f) ≗ map g ∘ map f
172 map-∘ (x ∷ xs) = cong (_ ∷_) (List.map-∘ xs)
173
174 map-id : map id ≗ id {A = List⁺ A}
175 map-id (x ∷ xs) = cong (x ∷_) (List.map-id xs)
176
177 ------------------------------------------------------------------------
178 -- inits
179
180 toList-inits : toList ∘ inits ≗ List.inits {A = A}
181 toList-inits _ = refl
182
183 ------------------------------------------------------------------------
184 -- tails
185
186 toList-tails : toList ∘ tails ≗ List.tails {A = A}
187 toList-tails _ = refl
188
189 ------------------------------------------------------------------------
190 -- groupSeqs
191
192 -- Groups all contiguous elements for which the predicate returns the
193 -- same result into lists.
194
195 module _ {P : Pred A p} (P? : Decidable P) where
196
197 groupSeqs-groups : ∀ xs → ListAll (Sum.All (All P) (All (∁ P))) (groupSeqs P? xs)
198 groupSeqs-groups [] = []
199 groupSeqs-groups (x ∷ xs) with P? x | groupSeqs P? xs | groupSeqs-groups xs
200 ... | yes px | [] | hyp = inj₁ (px ∷ []) ∷ hyp
201 ... | yes px | inj₁ xs′ ∷ xss | inj₁ pxs ∷ pxss = inj₁ (px ∷ toList⁺ pxs) ∷ pxss
202 ... | yes px | inj₂ xs′ ∷ xss | inj₂ pxs ∷ pxss = inj₁ (px ∷ []) ∷ inj₂ pxs ∷ pxss
203 ... | no ¬px | [] | hyp = inj₂ (¬px ∷ []) ∷ hyp
204 ... | no ¬px | inj₂ xs′ ∷ xss | inj₂ pxs ∷ pxss = inj₂ (¬px ∷ toList⁺ pxs) ∷ pxss
205 ... | no ¬px | inj₁ xs′ ∷ xss | inj₁ pxs ∷ pxss = inj₂ (¬px ∷ []) ∷ inj₁ pxs ∷ pxss
206
207 ungroupSeqs-groupSeqs : ∀ xs → ungroupSeqs (groupSeqs P? xs) ≡ xs
208 ungroupSeqs-groupSeqs [] = refl
209 ungroupSeqs-groupSeqs (x ∷ xs)
210 with does (P? x) | groupSeqs P? xs | ungroupSeqs-groupSeqs xs
211 ... | true | [] | hyp = cong (x ∷_) hyp
212 ... | true | inj₁ _ ∷ _ | hyp = cong (x ∷_) hyp
213 ... | true | inj₂ _ ∷ _ | hyp = cong (x ∷_) hyp
214 ... | false | [] | hyp = cong (x ∷_) hyp
215 ... | false | inj₁ _ ∷ _ | hyp = cong (x ∷_) hyp
216 ... | false | inj₂ _ ∷ _ | hyp = cong (x ∷_) hyp
217
218 ------------------------------------------------------------------------
219 -- DEPRECATED
220 ------------------------------------------------------------------------
221 -- Please use the new names as continuing support for the old names is
222 -- not guaranteed.
223
224 -- Version 2.0
225
226 map-compose = map-∘
227 {-# WARNING_ON_USAGE map-compose
228 "Warning: map-compose was deprecated in v2.0.
229 Please use map-∘ instead."
230 #-}
231
232 map-++⁺-commute = map-++⁺
233 {-# WARNING_ON_USAGE map-++⁺-commute
234 "Warning: map-++⁺-commute was deprecated in v2.0.
235 Please use map-++⁺ instead."
236 #-}