Debian Codegraph @

src/Data/List/NonEmpty/Base.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Non-empty lists: base type and operations
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.List.NonEmpty.Base where
10
11 open import Level using (Level)
12 open import Data.Bool.Base using (Bool; false; true)
13 open import Data.List.Base as List using (List; []; _∷_)
14 open import Data.Maybe.Base using (Maybe ; nothing; just)
15 open import Data.Nat.Base as ℕ using (ℕ; suc; zero; pred)
16 open import Data.Product.Base as Prod using (∃; _×_; proj₁; proj₂; _,_; -,_)
17 open import Data.Sum.Base as Sum using (_⊎_; inj₁; inj₂)
18 open import Data.These.Base as These using (These; this; that; these)
19 open import Data.Vec.Base as Vec using (Vec; []; _∷_)
20 open import Function.Base using (id; _∘′_; _∘_; const)
21 open import Relation.Binary.PropositionalEquality.Core
22 using (_≡_; _≢_; refl)
23 open import Relation.Unary using (Pred; Decidable; U; ∅)
24 open import Relation.Unary.Properties using (U?; ∅?)
25 open import Relation.Nullary.Decidable using (does)
26
27 private
28 variable
29 a p : Level
30 A B C : Set a
31
32 ------------------------------------------------------------------------
33 -- Definition
34
35 infixr 5 _∷_
36
37 record List⁺ (A : Set a) : Set a where
38 constructor _∷_
39 field
40 head : A
41 tail : List A
42
43 open List⁺ public
44
45 ------------------------------------------------------------------------
46 -- Basic combinators
47
48 uncons : List⁺ A → A × List A
49 uncons (hd ∷ tl) = hd , tl
50
51 [_] : A → List⁺ A
52 [ x ] = x ∷ []
53
54 infixr 5 _∷⁺_
55
56 _∷⁺_ : A → List⁺ A → List⁺ A
57 x ∷⁺ y ∷ xs = x ∷ y ∷ xs
58
59 length : List⁺ A → ℕ
60 length (x ∷ xs) = suc (List.length xs)
61
62 ------------------------------------------------------------------------
63 -- Conversion
64
65 toList : List⁺ A → List A
66 toList (x ∷ xs) = x ∷ xs
67
68 fromList : List A → Maybe (List⁺ A)
69 fromList [] = nothing
70 fromList (x ∷ xs) = just (x ∷ xs)
71
72 fromVec : ∀ {n} → Vec A (suc n) → List⁺ A
73 fromVec (x ∷ xs) = x ∷ Vec.toList xs
74
75 toVec : (xs : List⁺ A) → Vec A (length xs)
76 toVec (x ∷ xs) = x ∷ Vec.fromList xs
77
78 lift : (∀ {m} → Vec A (suc m) → ∃ λ n → Vec B (suc n)) →
79 List⁺ A → List⁺ B
80 lift f xs = fromVec (proj₂ (f (toVec xs)))
81
82 ------------------------------------------------------------------------
83 -- Other operations
84
85 map : (A → B) → List⁺ A → List⁺ B
86 map f (x ∷ xs) = (f x ∷ List.map f xs)
87
88 replicate : ∀ n → n ≢ 0 → A → List⁺ A
89 replicate n n≢0 a = a ∷ List.replicate (pred n) a
90
91 -- when dropping more than the size of the length of the list, the
92 -- last element remains
93 drop+ : ℕ → List⁺ A → List⁺ A
94 drop+ zero xs = xs
95 drop+ (suc n) (x ∷ []) = x ∷ []
96 drop+ (suc n) (x ∷ y ∷ xs) = drop+ n (y ∷ xs)
97
98 -- Right fold. Note that s is only applied to the last element (see
99 -- the examples below).
100
101 foldr : (A → B → B) → (A → B) → List⁺ A → B
102 foldr {A = A} {B = B} c s (x ∷ xs) = foldr′ x xs
103 where
104 foldr′ : A → List A → B
105 foldr′ x [] = s x
106 foldr′ x (y ∷ xs) = c x (foldr′ y xs)
107
108 -- Right fold.
109
110 foldr₁ : (A → A → A) → List⁺ A → A
111 foldr₁ f = foldr f id
112
113 -- Left fold. Note that s is only applied to the first element (see
114 -- the examples below).
115
116 foldl : (B → A → B) → (A → B) → List⁺ A → B
117 foldl c s (x ∷ xs) = List.foldl c (s x) xs
118
119 -- Left fold.
120
121 foldl₁ : (A → A → A) → List⁺ A → A
122 foldl₁ f = foldl f id
123
124 -- Append (several variants).
125
126 infixr 5 _⁺++⁺_ _++⁺_ _⁺++_
127
128 _⁺++⁺_ : List⁺ A → List⁺ A → List⁺ A
129 (x ∷ xs) ⁺++⁺ (y ∷ ys) = x ∷ (xs List.++ y ∷ ys)
130
131 _⁺++_ : List⁺ A → List A → List⁺ A
132 (x ∷ xs) ⁺++ ys = x ∷ (xs List.++ ys)
133
134 _++⁺_ : List A → List⁺ A → List⁺ A
135 xs ++⁺ ys = List.foldr _∷⁺_ ys xs
136
137 concat : List⁺ (List⁺ A) → List⁺ A
138 concat (xs ∷ xss) = xs ⁺++ List.concat (List.map toList xss)
139
140 concatMap : (A → List⁺ B) → List⁺ A → List⁺ B
141 concatMap f = concat ∘′ map f
142
143 ap : List⁺ (A → B) → List⁺ A → List⁺ B
144 ap fs as = concatMap (λ f → map f as) fs
145
146 -- Inits
147
148 inits : List A → List⁺ (List A)
149 inits xs = [] ∷ List.Inits.tail xs
150
151 -- Tails
152
153 tails : List A → List⁺ (List A)
154 tails xs = xs ∷ List.Tails.tail xs
155
156 -- Reverse
157
158 reverse : List⁺ A → List⁺ A
159 reverse = lift (-,_ ∘′ Vec.reverse)
160
161 -- Align and Zip
162
163 alignWith : (These A B → C) → List⁺ A → List⁺ B → List⁺ C
164 alignWith f (a ∷ as) (b ∷ bs) = f (these a b) ∷ List.alignWith f as bs
165
166 zipWith : (A → B → C) → List⁺ A → List⁺ B → List⁺ C
167 zipWith f (a ∷ as) (b ∷ bs) = f a b ∷ List.zipWith f as bs
168
169 unalignWith : (A → These B C) → List⁺ A → These (List⁺ B) (List⁺ C)
170 unalignWith f = foldr (These.alignWith mcons mcons ∘′ f)
171 (These.map [_] [_] ∘′ f)
172
173 where mcons : ∀ {e} {E : Set e} → These E (List⁺ E) → List⁺ E
174 mcons = These.fold [_] id _∷⁺_
175
176 unzipWith : (A → B × C) → List⁺ A → List⁺ B × List⁺ C
177 unzipWith f (a ∷ as) = Prod.zip _∷_ _∷_ (f a) (List.unzipWith f as)
178
179 align : List⁺ A → List⁺ B → List⁺ (These A B)
180 align = alignWith id
181
182 zip : List⁺ A → List⁺ B → List⁺ (A × B)
183 zip = zipWith _,_
184
185 unalign : List⁺ (These A B) → These (List⁺ A) (List⁺ B)
186 unalign = unalignWith id
187
188 unzip : List⁺ (A × B) → List⁺ A × List⁺ B
189 unzip = unzipWith id
190
191 -- Snoc.
192
193 infixl 5 _∷ʳ_ _⁺∷ʳ_
194
195 _∷ʳ_ : List A → A → List⁺ A
196 [] ∷ʳ y = [ y ]
197 (x ∷ xs) ∷ʳ y = x ∷ (xs List.∷ʳ y)
198
199 _⁺∷ʳ_ : List⁺ A → A → List⁺ A
200 xs ⁺∷ʳ x = toList xs ∷ʳ x
201
202 -- A snoc-view of non-empty lists.
203
204 infixl 5 _∷ʳ′_
205
206 data SnocView {A : Set a} : List⁺ A → Set a where
207 _∷ʳ′_ : (xs : List A) (x : A) → SnocView (xs ∷ʳ x)
208
209 snocView : (xs : List⁺ A) → SnocView xs
210 snocView (x ∷ xs) with List.initLast xs
211 snocView (x ∷ .[]) | [] = [] ∷ʳ′ x
212 snocView (x ∷ .(xs List.∷ʳ y)) | xs List.∷ʳ′ y = (x ∷ xs) ∷ʳ′ y
213
214 -- The last element in the list.
215
216 private
217 last′ : ∀ {l} → SnocView {A = A} l → A
218 last′ (_ ∷ʳ′ y) = y
219
220 last : List⁺ A → A
221 last = last′ ∘ snocView
222
223 -- Groups all contiguous elements for which the predicate returns the
224 -- same result into lists. The left sums are the ones for which the
225 -- predicate holds, the right ones are the ones for which it doesn't.
226 groupSeqsᵇ : (A → Bool) → List A → List (List⁺ A ⊎ List⁺ A)
227 groupSeqsᵇ p [] = []
228 groupSeqsᵇ p (x ∷ xs) with p x | groupSeqsᵇ p xs
229 ... | true | inj₁ xs′ ∷ xss = inj₁ (x ∷⁺ xs′) ∷ xss
230 ... | true | xss = inj₁ [ x ] ∷ xss
231 ... | false | inj₂ xs′ ∷ xss = inj₂ (x ∷⁺ xs′) ∷ xss
232 ... | false | xss = inj₂ [ x ] ∷ xss
233
234 -- Groups all contiguous elements /not/ satisfying the predicate into
235 -- lists. Elements satisfying the predicate are dropped.
236 wordsByᵇ : (A → Bool) → List A → List (List⁺ A)
237 wordsByᵇ p = List.mapMaybe Sum.[ const nothing , just ] ∘ groupSeqsᵇ p
238
239 groupSeqs : {P : Pred A p} → Decidable P → List A → List (List⁺ A ⊎ List⁺ A)
240 groupSeqs P? = groupSeqsᵇ (does ∘ P?)
241
242 wordsBy : {P : Pred A p} → Decidable P → List A → List (List⁺ A)
243 wordsBy P? = wordsByᵇ (does ∘ P?)
244
245 -- Inverse operation for groupSequences.
246 ungroupSeqs : List (List⁺ A ⊎ List⁺ A) → List A
247 ungroupSeqs = List.concat ∘ List.map Sum.[ toList , toList ]
248
249 ------------------------------------------------------------------------
250 -- Examples
251
252 -- Note that these examples are simple unit tests, because the type
253 -- checker verifies them.
254
255 private
256 module Examples {A B : Set}
257 (_⊕_ : A → B → B)
258 (_⊗_ : B → A → B)
259 (_⊙_ : A → A → A)
260 (f : A → B)
261 (a b c : A)
262 where
263
264 hd : head (a ∷⁺ b ∷⁺ [ c ]) ≡ a
265 hd = refl
266
267 tl : tail (a ∷⁺ b ∷⁺ [ c ]) ≡ b ∷ c ∷ []
268 tl = refl
269
270 mp : map f (a ∷⁺ b ∷⁺ [ c ]) ≡ f a ∷⁺ f b ∷⁺ [ f c ]
271 mp = refl
272
273 right : foldr _⊕_ f (a ∷⁺ b ∷⁺ [ c ]) ≡ (a ⊕ (b ⊕ f c))
274 right = refl
275
276 right₁ : foldr₁ _⊙_ (a ∷⁺ b ∷⁺ [ c ]) ≡ (a ⊙ (b ⊙ c))
277 right₁ = refl
278
279 left : foldl _⊗_ f (a ∷⁺ b ∷⁺ [ c ]) ≡ ((f a ⊗ b) ⊗ c)
280 left = refl
281
282 left₁ : foldl₁ _⊙_ (a ∷⁺ b ∷⁺ [ c ]) ≡ ((a ⊙ b) ⊙ c)
283 left₁ = refl
284
285 ⁺app⁺ : (a ∷⁺ b ∷⁺ [ c ]) ⁺++⁺ (b ∷⁺ [ c ]) ≡
286 a ∷⁺ b ∷⁺ c ∷⁺ b ∷⁺ [ c ]
287 ⁺app⁺ = refl
288
289 ⁺app : (a ∷⁺ b ∷⁺ [ c ]) ⁺++ (b ∷ c ∷ []) ≡
290 a ∷⁺ b ∷⁺ c ∷⁺ b ∷⁺ [ c ]
291 ⁺app = refl
292
293 app⁺ : (a ∷ b ∷ c ∷ []) ++⁺ (b ∷⁺ [ c ]) ≡
294 a ∷⁺ b ∷⁺ c ∷⁺ b ∷⁺ [ c ]
295 app⁺ = refl
296
297 conc : concat ((a ∷⁺ b ∷⁺ [ c ]) ∷⁺ [ b ∷⁺ [ c ] ]) ≡
298 a ∷⁺ b ∷⁺ c ∷⁺ b ∷⁺ [ c ]
299 conc = refl
300
301 rev : reverse (a ∷⁺ b ∷⁺ [ c ]) ≡ c ∷⁺ b ∷⁺ [ a ]
302 rev = refl
303
304 snoc : (a ∷ b ∷ c ∷ []) ∷ʳ a ≡ a ∷⁺ b ∷⁺ c ∷⁺ [ a ]
305 snoc = refl
306
307 snoc⁺ : (a ∷⁺ b ∷⁺ [ c ]) ⁺∷ʳ a ≡ a ∷⁺ b ∷⁺ c ∷⁺ [ a ]
308 snoc⁺ = refl
309
310 groupSeqs-true : groupSeqs U? (a ∷ b ∷ c ∷ []) ≡
311 inj₁ (a ∷⁺ b ∷⁺ [ c ]) ∷ []
312 groupSeqs-true = refl
313
314 groupSeqs-false : groupSeqs ∅? (a ∷ b ∷ c ∷ []) ≡
315 inj₂ (a ∷⁺ b ∷⁺ [ c ]) ∷ []
316 groupSeqs-false = refl
317
318 groupSeqs-≡1 : groupSeqsᵇ (ℕ._≡ᵇ 1) (1 ∷ 2 ∷ 3 ∷ 1 ∷ 1 ∷ 2 ∷ 1 ∷ []) ≡
319 inj₁ [ 1 ] ∷
320 inj₂ (2 ∷⁺ [ 3 ]) ∷
321 inj₁ (1 ∷⁺ [ 1 ]) ∷
322 inj₂ [ 2 ] ∷
323 inj₁ [ 1 ] ∷
324 []
325 groupSeqs-≡1 = refl
326
327 wordsBy-true : wordsByᵇ (const true) (a ∷ b ∷ c ∷ []) ≡ []
328 wordsBy-true = refl
329
330 wordsBy-false : wordsByᵇ (const false) (a ∷ b ∷ c ∷ []) ≡
331 (a ∷⁺ b ∷⁺ [ c ]) ∷ []
332 wordsBy-false = refl
333
334 wordsBy-≡1 : wordsByᵇ (ℕ._≡ᵇ 1) (1 ∷ 2 ∷ 3 ∷ 1 ∷ 1 ∷ 2 ∷ 1 ∷ []) ≡
335 (2 ∷⁺ [ 3 ]) ∷
336 [ 2 ] ∷
337 []
338 wordsBy-≡1 = refl