std:List

lib/List.omg

lib/List.omg357 lines
1{-  Lists.
2
3    `List` is a builtin type: literals (`[1, 2, 3]`, `[] : List Natural`) and
4    the append operator `#`.  Its induction principle is the builtin
5    `List/elim`; everything below is defined with it.
6-}
7
8-- Right fold: fold f z [x, y] = f x (f y z)
9let fold
10    : forall {la lb : Level} {a : Type la} {b : Type lb} -> (a -> b -> b) -> b -> List a -> b
11    = \{la} {lb} {a} {b} cons nil ->
12        List/elim (\(_ : List a) -> b) nil (\(x : a) (_ : List a) (rest : b) -> cons x rest)
13
14-- Left fold: foldLeft f z [x, y] = f (f z x) y
15let foldLeft
16    : forall {la lb : Level} {a : Type la} {b : Type lb} -> (b -> a -> b) -> b -> List a -> b
17    = \{la} {lb} {a} {b} f z xs ->
18        fold (\(x : a) (continue : b -> b) (acc : b) -> continue (f acc x)) (\(acc : b) -> acc) xs z
19
20-- The number of elements. A target reads it from the list itself, so it
21-- costs what reading one number of a list costs, not one step per element.
22let length
23    : forall {l : Level} {a : Type l} -> List a -> Natural
24    = \{l} {a} -> List/length @l @a
25
26-- Whether the list has no elements.  The step ignores the recursive
27-- result, so this reads the first cell and stops: it never walks the list
28-- to count it.
29let null
30    : forall {l : Level} {a : Type l} -> List a -> Bool
31    = \{l} {a} -> fold (\(_ : a) (_ : Bool) -> False) True
32
33let singleton
34    : forall {l : Level} {a : Type l} -> a -> List a
35    = \x -> [ x ]
36
37let cons
38    : forall {l : Level} {a : Type l} -> a -> List a -> List a
39    = \x xs -> [ x ] # xs
40
41let snoc
42    : forall {l : Level} {a : Type l} -> List a -> a -> List a
43    = \xs x -> xs # [ x ]
44
45let append
46    : forall {l : Level} {a : Type l} -> List a -> List a -> List a
47    = \xs ys -> xs # ys
48
49let map
50    : forall {la lb : Level} {a : Type la} {b : Type lb} -> (a -> b) -> List a -> List b
51    = \{la} {lb} {a} {b} f -> fold (\(x : a) (rest : List b) -> [ f x ] # rest) ([] : List b)
52
53let filter
54    : forall {l : Level} {a : Type l} -> (a -> Bool) -> List a -> List a
55    = \{l} {a} keep ->
56        fold (\(x : a) (rest : List a) -> if keep x then [ x ] # rest else rest) ([] : List a)
57
58let concat
59    : forall {l : Level} {a : Type l} -> List (List a) -> List a
60    = \{l} {a} -> fold (\(xs : List a) (rest : List a) -> xs # rest) ([] : List a)
61
62let concatMap
63    : forall {la lb : Level} {a : Type la} {b : Type lb} -> (a -> List b) -> List a -> List b
64    = \{la} {lb} {a} {b} f -> fold (\(x : a) (rest : List b) -> f x # rest) ([] : List b)
65
66let reverse
67    : forall {l : Level} {a : Type l} -> List a -> List a
68    = \{l} {a} -> foldLeft (\(acc : List a) (x : a) -> [ x ] # acc) ([] : List a)
69
70let head
71    : forall {l : Level} {a : Type l} -> List a -> Optional a
72    = \{l} {a} -> fold (\(x : a) (_ : Optional a) -> Some x) (None a)
73
74let last
75    : forall {l : Level} {a : Type l} -> List a -> Optional a
76    = \{l} {a} -> foldLeft (\(_ : Optional a) (x : a) -> Some x) (None a)
77
78-- The first element and the rest, if the list is not empty.
79let uncons
80    : forall {l : Level} {a : Type l} -> List a -> Optional (a * List a)
81    = \{l} {a} ->
82        List/elim
83          (\(_ : List a) -> Optional (a * List a))
84          (None (a * List a))
85          (\(x : a) (xs : List a) (_ : Optional (a * List a)) -> Some (x, xs))
86
87let any
88    : forall {l : Level} {a : Type l} -> (a -> Bool) -> List a -> Bool
89    = \{l} {a} p -> fold (\(x : a) (rest : Bool) -> p x || rest) False
90
91let all
92    : forall {l : Level} {a : Type l} -> (a -> Bool) -> List a -> Bool
93    = \{l} {a} p -> fold (\(x : a) (rest : Bool) -> p x && rest) True
94
95let find
96    : forall {l : Level} {a : Type l} -> (a -> Bool) -> List a -> Optional a
97    = \{l} {a} p -> fold (\(x : a) (rest : Optional a) -> if p x then Some x else rest) (None a)
98
99-- Is the element in the list?  The test of equality comes first, as in
100-- std:Map: elem Text/equal "b" ["a", "b"] = True
101let elem
102    : forall {l : Level} {a : Type l} -> (a -> a -> Bool) -> a -> List a -> Bool
103    = \{l} {a} equal x -> any (\(y : a) -> equal x y)
104
105-- Do the two lists have equal elements, in the same order?
106let equal
107    : forall {l : Level} {a : Type l} -> (a -> a -> Bool) -> List a -> List a -> Bool
108    = \{l} {a} same xs ys ->
109        fold
110          (\(x : a) (rest : List a -> Bool) (others : List a) ->
111             List/elim
112               (\(_ : List a) -> Bool)
113               False
114               (\(y : a) (tail : List a) (_ : Bool) -> same x y && rest tail)
115               others)
116          (\(others : List a) -> null others)
117          xs
118          ys
119
120-- The longest prefix whose elements satisfy a predicate, and the rest:
121-- span std:Natural.even [2, 4, 5, 6] = ([2, 4], [5, 6])
122let span
123    : forall {l : Level} {a : Type l} -> (a -> Bool) -> List a -> List a * List a
124    = \{l} {a} p ->
125        List/elim
126          (\(_ : List a) -> List a * List a)
127          ([] : List a, [] : List a)
128          (\(x : a) (xs : List a) (rest : List a * List a) ->
129             if p x then ([ x ] # rest.1, rest.2) else ([] : List a, [ x ] # xs))
130
131let takeWhile
132    : forall {l : Level} {a : Type l} -> (a -> Bool) -> List a -> List a
133    = \{l} {a} p xs -> (span p xs).1
134
135let dropWhile
136    : forall {l : Level} {a : Type l} -> (a -> Bool) -> List a -> List a
137    = \{l} {a} p xs -> (span p xs).2
138
139-- The results of a function that may fail, where it succeeds:
140-- filterMap Natural/parse ["1", "x", "2"] = [1, 2]
141let filterMap
142    : forall {la lb : Level} {a : Type la} {b : Type lb} -> (a -> Optional b) -> List a -> List b
143    = \{la} {lb} {a} {b} f ->
144        concatMap
145          (\(x : a) ->
146             Optional/elim (\(_ : Optional b) -> List b) ([] : List b) (\(y : b) -> [ y ]) (f x))
147
148-- The values of the options that have one.
149let unpackOptionals
150    : forall {l : Level} {a : Type l} -> List (Optional a) -> List a
151    = \{l} {a} -> filterMap (\(o : Optional a) -> o)
152
153-- replicate n x = [x, ..., x], with n elements.
154let replicate
155    : forall {l : Level} {a : Type l} -> Natural -> a -> List a
156    = \{l} {a} n x ->
157        Natural/elim
158          (\(_ : Natural) -> List a)
159          ([] : List a)
160          (\(_ : Natural) (rest : List a) -> [ x ] # rest)
161          n
162
163-- generate n f = [f 0, f 1, ..., f (n - 1)]
164let generate
165    : forall {l : Level} {a : Type l} -> Natural -> (Natural -> a) -> List a
166    = \{l} {a} n f ->
167        Natural/elim
168          (\(_ : Natural) -> List a)
169          ([] : List a)
170          (\(k : Natural) (below : List a) -> below # [ f k ])
171          n
172
173-- The first n elements.
174let take
175    : forall {l : Level} {a : Type l} -> Natural -> List a -> List a
176    = \{l} {a} n xs ->
177        fold
178          (\(x : a) (rest : Natural -> List a) (k : Natural) ->
179             if Natural/isZero k
180             then ([] : List a)
181             else [ x ] # rest (Natural/subtract 1 k))
182          (\(_ : Natural) -> ([] : List a))
183          xs
184          n
185
186-- Everything but the first n elements.
187let drop
188    : forall {l : Level} {a : Type l} -> Natural -> List a -> List a
189    = \{l} {a} n xs ->
190        fold
191          (\(x : a) (rest : Natural -> List a) (k : Natural) ->
192             if Natural/isZero k
193             then [ x ] # rest 0
194             else rest (Natural/subtract 1 k))
195          (\(_ : Natural) -> ([] : List a))
196          xs
197          n
198
199-- The element at the given position, counting from 0.
200let index
201    : forall {l : Level} {a : Type l} -> Natural -> List a -> Optional a
202    = \{l} {a} n xs -> head (drop n xs)
203
204-- Combine two lists element by element; the result is as long as the
205-- shorter of the two.
206let zipWith
207    : forall {la lb lc : Level} {a : Type la} {b : Type lb} {c : Type lc}
208      -> (a -> b -> c)
209      -> List a
210      -> List b
211      -> List c
212    = \{la} {lb} {lc} {a} {b} {c} f ->
213        fold
214          (\(x : a) (rest : List b -> List c) (ys : List b) ->
215             List/elim
216               (\(_ : List b) -> List c)
217               ([] : List c)
218               (\(y : b) (tail : List b) (_ : List c) -> [ f x y ] # rest tail)
219               ys)
220          (\(_ : List b) -> ([] : List c))
221
222let zip
223    : forall {la lb : Level} {a : Type la} {b : Type lb} -> List a -> List b -> List (a * b)
224    = \{la} {lb} {a} {b} -> zipWith (\(x : a) (y : b) -> (x, y))
225
226let unzip
227    : forall {la lb : Level} {a : Type la} {b : Type lb} -> List (a * b) -> List a * List b
228    = \{la} {lb} {a} {b} ->
229        fold
230          (\(p : a * b) (rest : List a * List b) -> ([ p.1 ] # rest.1, [ p.2 ] # rest.2))
231          ([] : List a, [] : List b)
232
233-- Split a list into the elements that satisfy a predicate and those that
234-- do not.
235let partition
236    : forall {l : Level} {a : Type l} -> (a -> Bool) -> List a -> { yes : List a, no : List a }
237    = \{l} {a} p ->
238        fold
239          (\(x : a) (rest : { yes : List a, no : List a }) ->
240             if p x
241             then { yes = [ x ] # rest.yes, no = rest.no }
242             else { yes = rest.yes, no = [ x ] # rest.no })
243          { yes = ([] : List a), no = ([] : List a) }
244
245-- Pair every element with its position.
246let indexed
247    : forall {l : Level} {a : Type l} -> List a -> List { index : Natural, value : a }
248    = \{l} {a} xs ->
249        zipWith
250          (\(i : Natural) (x : a) -> { index = i, value = x })
251          (generate (length xs) (\(k : Natural) -> k))
252          xs
253
254-- Insert an element in a list sorted by the given "less than or equal",
255-- after the elements equal to it.
256let insertBy
257    : forall {l : Level} {a : Type l} -> (a -> a -> Bool) -> a -> List a -> List a
258    = \{l} {a} lessEq x xs ->
259        let parts = span (\(y : a) -> lessEq y x) xs
260        in  parts.1 # [ x ] # parts.2
261
262-- Merge two lists sorted by the given "less than or equal" into one; of two
263-- equal elements, the one of the first list comes first.  Each element of
264-- the first list takes along the elements of the second that are smaller.
265let mergeBy
266    : forall {l : Level} {a : Type l} -> (a -> a -> Bool) -> List a -> List a -> List a
267    = \{l} {a} lessEq xs ys ->
268        fold
269          (\(x : a) (rest : List a -> List a) (others : List a) ->
270             let smaller = span (\(y : a) -> if lessEq x y then False else True) others
271             in  smaller.1 # [ x ] # rest smaller.2)
272          (\(others : List a) -> others)
273          xs
274          ys
275
276-- Sort a list by the given "less than or equal".  Elements that are equal
277-- keep their order: the sort is stable.
278--
279-- It is a merge sort: the list is cut into lists of one element, and each
280-- round merges them two by two, which takes a time proportional to the
281-- length.  After as many rounds as there are elements (most of which find a
282-- single sorted list and leave it as it is), the list is sorted.
283let sortBy
284    : forall {l : Level} {a : Type l} -> (a -> a -> Bool) -> List a -> List a
285    = \{l} {a} lessEq xs ->
286        -- One round: [r1, r2, r3, r4, r5] becomes [r1, r2 and r3 merged, r4
287        -- and r5 merged].  The lists are paired from the end: one may be
288        -- left over, waiting for the one before it.
289        let Paired = { pending : Optional (List a), merged : List (List a) }
290
291        let round =
292              \(sorted : List (List a)) ->
293                let paired =
294                      fold
295                        (\(run : List a) (rest : Paired) ->
296                           Optional/elim
297                             (\(_ : Optional (List a)) -> Paired)
298                             { pending = Some run, merged = rest.merged }
299                             (\(next : List a) ->
300                                { pending = None (List a)
301                                , merged = [ mergeBy lessEq run next ] # rest.merged
302                                })
303                             rest.pending)
304                        { pending = None (List a), merged = ([] : List (List a)) }
305                        sorted
306                in  Optional/elim
307                      (\(_ : Optional (List a)) -> List (List a))
308                      paired.merged
309                      (\(run : List a) -> [ run ] # paired.merged)
310                      paired.pending
311
312        in  concat
313              (Natural/elim
314                 (\(_ : Natural) -> List (List a))
315                 (map (\(x : a) -> [ x ]) xs)
316                 (\(_ : Natural) -> round)
317                 (length xs))
318
319in  { fold
320    , foldLeft
321    , length
322    , null
323    , singleton
324    , cons
325    , snoc
326    , append
327    , map
328    , filter
329    , concat
330    , concatMap
331    , reverse
332    , head
333    , last
334    , uncons
335    , any
336    , all
337    , find
338    , elem
339    , equal
340    , span
341    , takeWhile
342    , dropWhile
343    , filterMap
344    , unpackOptionals
345    , replicate
346    , generate
347    , take
348    , drop
349    , index
350    , zipWith
351    , zip
352    , unzip
353    , partition
354    , indexed
355    , insertBy
356    , sortBy
357    }
Your result
Complete file: lib/List.omg · with imports