~ubuntu-branches/ubuntu/lucid/agda-stdlib/lucid

« back to all changes in this revision

Viewing changes to src/Data/List.agda

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2010-01-08 23:35:09 UTC
  • Revision ID: james.westby@ubuntu.com-20100108233509-l6ejt9xji64xysfb
Import upstream version 0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
------------------------------------------------------------------------
 
2
-- Lists
 
3
------------------------------------------------------------------------
 
4
{-# OPTIONS --universe-polymorphism #-}
 
5
 
 
6
module Data.List where
 
7
 
 
8
open import Data.Nat
 
9
open import Data.Sum as Sum using (_⊎_; inj₁; inj₂)
 
10
open import Data.Bool
 
11
open import Data.Maybe using (Maybe; nothing; just)
 
12
open import Data.Product as Prod using (_×_; _,_)
 
13
open import Data.Function
 
14
open import Algebra
 
15
import Relation.Binary.PropositionalEquality as PropEq
 
16
import Algebra.FunctionProperties as FunProp
 
17
 
 
18
infixr 5 _∷_ _++_
 
19
 
 
20
------------------------------------------------------------------------
 
21
-- Types
 
22
 
 
23
data List {a} (A : Set a) : Set a where
 
24
  []  : List A
 
25
  _∷_ : (x : A) (xs : List A) → List A
 
26
 
 
27
{-# BUILTIN LIST List #-}
 
28
{-# BUILTIN NIL  []   #-}
 
29
{-# BUILTIN CONS _∷_  #-}
 
30
 
 
31
------------------------------------------------------------------------
 
32
-- Some operations
 
33
 
 
34
-- * Basic functions
 
35
 
 
36
[_] : ∀ {a} {A : Set a} → A → List A
 
37
[ x ] = x ∷ []
 
38
 
 
39
_++_ : ∀ {a} {A : Set a} → List A → List A → List A
 
40
[]       ++ ys = ys
 
41
(x ∷ xs) ++ ys = x ∷ (xs ++ ys)
 
42
 
 
43
-- Snoc.
 
44
 
 
45
infixl 5 _∷ʳ_
 
46
 
 
47
_∷ʳ_ : ∀ {a} {A : Set a} → List A → A → List A
 
48
xs ∷ʳ x = xs ++ [ x ]
 
49
 
 
50
null : ∀ {a} {A : Set a} → List A → Bool
 
51
null []       = true
 
52
null (x ∷ xs) = false
 
53
 
 
54
-- * List transformations
 
55
 
 
56
map : ∀ {a b} {A : Set a} {B : Set b} → (A → B) → List A → List B
 
57
map f []       = []
 
58
map f (x ∷ xs) = f x ∷ map f xs
 
59
 
 
60
reverse : ∀ {a} {A : Set a} → List A → List A
 
61
reverse xs = rev xs []
 
62
  where
 
63
  rev : ∀ {a} {A : Set a} → List A → List A → List A
 
64
  rev []       ys = ys
 
65
  rev (x ∷ xs) ys = rev xs (x ∷ ys)
 
66
 
 
67
replicate : ∀ {a} {A : Set a} → (n : ℕ) → A → List A
 
68
replicate zero    x = []
 
69
replicate (suc n) x = x ∷ replicate n x
 
70
 
 
71
zipWith : ∀ {a b c} {A : Set a} {B : Set b} {C : Set c}
 
72
          → (A → B → C) → List A → List B → List C
 
73
zipWith f (x ∷ xs) (y ∷ ys) = f x y ∷ zipWith f xs ys
 
74
zipWith f _        _        = []
 
75
 
 
76
zip : ∀ {a b} {A : Set a} {B : Set b} → List A → List B → List (A × B)
 
77
zip = zipWith (_,_)
 
78
 
 
79
intersperse : ∀ {a} {A : Set a} → A → List A → List A
 
80
intersperse x []           = []
 
81
intersperse x (y ∷ [])     = [ y ]
 
82
intersperse x (y ∷ z ∷ zs) = y ∷ x ∷ intersperse x (z ∷ zs)
 
83
 
 
84
-- * Reducing lists (folds)
 
85
 
 
86
foldr : ∀ {a b} {A : Set a} {B : Set b} → (A → B → B) → B → List A → B
 
87
foldr c n []       = n
 
88
foldr c n (x ∷ xs) = c x (foldr c n xs)
 
89
 
 
90
foldl : ∀ {a b} {A : Set a} {B : Set b} → (A → B → A) → A → List B → A
 
91
foldl c n []       = n
 
92
foldl c n (x ∷ xs) = foldl c (c n x) xs
 
93
 
 
94
-- ** Special folds
 
95
 
 
96
concat : ∀ {a} {A : Set a} → List (List A) → List A
 
97
concat = foldr _++_ []
 
98
 
 
99
concatMap : ∀ {a b} {A : Set a} {B : Set b} →
 
100
            (A → List B) → List A → List B
 
101
concatMap f = concat ∘ map f
 
102
 
 
103
and : List Bool → Bool
 
104
and = foldr _∧_ true
 
105
 
 
106
or : List Bool → Bool
 
107
or = foldr _∨_ false
 
108
 
 
109
any : ∀ {a} {A : Set a} → (A → Bool) → List A → Bool
 
110
any p = or ∘ map p
 
111
 
 
112
all : ∀ {a} {A : Set a} → (A → Bool) → List A → Bool
 
113
all p = and ∘ map p
 
114
 
 
115
sum : List ℕ → ℕ
 
116
sum = foldr _+_ 0
 
117
 
 
118
product : List ℕ → ℕ
 
119
product = foldr _*_ 1
 
120
 
 
121
length : ∀ {a} {A : Set a} → List A → ℕ
 
122
length = foldr (λ _ → suc) 0
 
123
 
 
124
-- * Building lists
 
125
 
 
126
-- ** Scans
 
127
 
 
128
scanr : ∀ {a b} {A : Set a} {B : Set b} →
 
129
        (A → B → B) → B → List A → List B
 
130
scanr f e []       = e ∷ []
 
131
scanr f e (x ∷ xs) with scanr f e xs
 
132
... | []     = []                -- dead branch
 
133
... | y ∷ ys = f x y ∷ y ∷ ys
 
134
 
 
135
scanl : ∀ {a b} {A : Set a} {B : Set b} →
 
136
        (A → B → A) → A → List B → List A
 
137
scanl f e []       = e ∷ []
 
138
scanl f e (x ∷ xs) = e ∷ scanl f (f e x) xs
 
139
 
 
140
-- ** Unfolding
 
141
 
 
142
-- Unfold. Uses a measure (a natural number) to ensure termination.
 
143
 
 
144
unfold : ∀ {a b} {A : Set a} (B : ℕ → Set b)
 
145
         (f : ∀ {n} → B (suc n) → Maybe (A × B n)) →
 
146
         ∀ {n} → B n → List A
 
147
unfold B f {n = zero}  s = []
 
148
unfold B f {n = suc n} s with f s
 
149
... | nothing       = []
 
150
... | just (x , s') = x ∷ unfold B f s'
 
151
 
 
152
-- downFrom 3 = 2 ∷ 1 ∷ 0 ∷ [].
 
153
 
 
154
downFrom : ℕ → List ℕ
 
155
downFrom n = unfold Singleton f (wrap n)
 
156
  where
 
157
  data Singleton : ℕ → Set where
 
158
    wrap : (n : ℕ) → Singleton n
 
159
 
 
160
  f : ∀ {n} → Singleton (suc n) → Maybe (ℕ × Singleton n)
 
161
  f {n} (wrap .(suc n)) = just (n , wrap n)
 
162
 
 
163
-- ** Conversions
 
164
 
 
165
fromMaybe : ∀ {a} {A : Set a} → Maybe A → List A
 
166
fromMaybe (just x) = [ x ]
 
167
fromMaybe nothing  = []
 
168
 
 
169
-- * Sublists
 
170
 
 
171
-- ** Extracting sublists
 
172
 
 
173
take : ∀ {a} {A : Set a} → ℕ → List A → List A
 
174
take zero    xs       = []
 
175
take (suc n) []       = []
 
176
take (suc n) (x ∷ xs) = x ∷ take n xs
 
177
 
 
178
drop : ∀ {a} {A : Set a} → ℕ → List A → List A
 
179
drop zero    xs       = xs
 
180
drop (suc n) []       = []
 
181
drop (suc n) (x ∷ xs) = drop n xs
 
182
 
 
183
splitAt : ∀ {a} {A : Set a} → ℕ → List A → (List A × List A)
 
184
splitAt zero    xs       = ([] , xs)
 
185
splitAt (suc n) []       = ([] , [])
 
186
splitAt (suc n) (x ∷ xs) with splitAt n xs
 
187
... | (ys , zs) = (x ∷ ys , zs)
 
188
 
 
189
takeWhile : ∀ {a} {A : Set a} → (A → Bool) → List A → List A
 
190
takeWhile p []       = []
 
191
takeWhile p (x ∷ xs) with p x
 
192
... | true  = x ∷ takeWhile p xs
 
193
... | false = []
 
194
 
 
195
dropWhile : ∀ {a} {A : Set a} → (A → Bool) → List A → List A
 
196
dropWhile p []       = []
 
197
dropWhile p (x ∷ xs) with p x
 
198
... | true  = dropWhile p xs
 
199
... | false = x ∷ xs
 
200
 
 
201
span : ∀ {a} {A : Set a} → (A → Bool) → List A → (List A × List A)
 
202
span p []       = ([] , [])
 
203
span p (x ∷ xs) with p x
 
204
... | true  = Prod.map (_∷_ x) id (span p xs)
 
205
... | false = ([] , x ∷ xs)
 
206
 
 
207
break : ∀ {a} {A : Set a} → (A → Bool) → List A → (List A × List A)
 
208
break p = span (not ∘ p)
 
209
 
 
210
inits : ∀ {a} {A : Set a} →  List A → List (List A)
 
211
inits []       = [] ∷ []
 
212
inits (x ∷ xs) = [] ∷ map (_∷_ x) (inits xs)
 
213
 
 
214
tails : ∀ {a} {A : Set a} → List A → List (List A)
 
215
tails []       = [] ∷ []
 
216
tails (x ∷ xs) = (x ∷ xs) ∷ tails xs
 
217
 
 
218
infixl 5 _∷ʳ'_
 
219
 
 
220
data InitLast {a} {A : Set a} : List A → Set a where
 
221
  []    : InitLast []
 
222
  _∷ʳ'_ : (xs : List A) (x : A) → InitLast (xs ∷ʳ x)
 
223
 
 
224
initLast : ∀ {a} {A : Set a} (xs : List A) → InitLast xs
 
225
initLast []               = []
 
226
initLast (x ∷ xs)         with initLast xs
 
227
initLast (x ∷ .[])        | []       = [] ∷ʳ' x
 
228
initLast (x ∷ .(ys ∷ʳ y)) | ys ∷ʳ' y = (x ∷ ys) ∷ʳ' y
 
229
 
 
230
-- * Searching lists
 
231
 
 
232
-- ** Searching with a predicate
 
233
 
 
234
-- A generalised variant of filter.
 
235
 
 
236
gfilter : ∀ {a b} {A : Set a} {B : Set b} →
 
237
          (A → Maybe B) → List A → List B
 
238
gfilter p []       = []
 
239
gfilter p (x ∷ xs) with p x
 
240
... | just y  = y ∷ gfilter p xs
 
241
... | nothing =     gfilter p xs
 
242
 
 
243
filter : ∀ {a} {A : Set a} → (A → Bool) → List A → List A
 
244
filter p = gfilter (λ x → if p x then just x else nothing)
 
245
 
 
246
partition : ∀ {a} {A : Set a} → (A → Bool) → List A → (List A × List A)
 
247
partition p []       = ([] , [])
 
248
partition p (x ∷ xs) with p x | partition p xs
 
249
... | true  | (ys , zs) = (x ∷ ys , zs)
 
250
... | false | (ys , zs) = (ys , x ∷ zs)
 
251
 
 
252
------------------------------------------------------------------------
 
253
-- List monoid
 
254
 
 
255
monoid : Set → Monoid
 
256
monoid A = record
 
257
  { Carrier  = List A
 
258
  ; _≈_      = _≡_
 
259
  ; _∙_      = _++_
 
260
  ; ε        = []
 
261
  ; isMonoid = record
 
262
    { isSemigroup = record
 
263
      { isEquivalence = PropEq.isEquivalence
 
264
      ; assoc         = assoc
 
265
      ; ∙-cong        = cong₂ _++_
 
266
      }
 
267
    ; identity = ((λ _ → refl) , identity)
 
268
    }
 
269
  }
 
270
  where
 
271
  open PropEq
 
272
  open FunProp _≡_
 
273
 
 
274
  identity : RightIdentity [] _++_
 
275
  identity []       = refl
 
276
  identity (x ∷ xs) = cong (_∷_ x) (identity xs)
 
277
 
 
278
  assoc : Associative _++_
 
279
  assoc []       ys zs = refl
 
280
  assoc (x ∷ xs) ys zs = cong (_∷_ x) (assoc xs ys zs)
 
281
 
 
282
------------------------------------------------------------------------
 
283
-- List monad
 
284
 
 
285
open import Category.Monad
 
286
 
 
287
monad : RawMonad List
 
288
monad = record
 
289
  { return = λ x → x ∷ []
 
290
  ; _>>=_  = λ xs f → concat (map f xs)
 
291
  }
 
292
 
 
293
monadZero : RawMonadZero List
 
294
monadZero = record
 
295
  { monad = monad
 
296
  ; ∅     = []
 
297
  }
 
298
 
 
299
monadPlus : RawMonadPlus List
 
300
monadPlus = record
 
301
  { monadZero = monadZero
 
302
  ; _∣_       = _++_
 
303
  }
 
304
 
 
305
------------------------------------------------------------------------
 
306
-- Monadic functions
 
307
 
 
308
private
 
309
 module Monadic {M} (Mon : RawMonad M) where
 
310
 
 
311
  open RawMonad Mon
 
312
 
 
313
  sequence : ∀ {A : Set} → List (M A) → M (List A)
 
314
  sequence []       = return []
 
315
  sequence (x ∷ xs) = _∷_ <$> x ⊛ sequence xs
 
316
 
 
317
  mapM : ∀ {a} {A : Set a} {B} → (A → M B) → List A → M (List B)
 
318
  mapM f = sequence ∘ map f
 
319
 
 
320
open Monadic public