std:List/Law

lib/List/Law.omg

lib/List/Law.omg745 lines
1{-  Laws of the list eliminators, checked by the kernel.
2
3    Each definition's type is an equation between two list computations and
4    its body is the proof. The general laws are about `List/elim` itself
5    (`elimAppend`, `elimIf`, `elimCong`) and about consumers of producers
6    (`foldBuildBy`, `foldBuildState`, `elimSnoc`, `accumulateReverse`); the
7    laws a functional programmer expects (`mapMap`, `foldMap`, `mapAppend`,
8    `foldAppend`, `lengthMap`, `anyMap`, `foldFilter`, `foldLeftMap`) are
9    proved from them.
10
11    No law takes a hypothesis except `foldConstant`, which takes
12    `std:Arith.NatFacts`. A law about `fold c n` holds for every `c`; a law
13    stated with `List/elim` has a constant motive. `build` is a definition,
14    not a polymorphic argument, so `foldBuild` needs no parametricity.
15
16    The compiler rewrites a fold over a producer or an append by these laws
17    ("Omegyu.Contract.Stage.Eliminate.Fuse"); its tests check the instance of
18    the law behind each rewrite they make, not the rewriting in general.
19-}
20let A = std:Arith
21let E = std:Equal
22let L = std:List
23
24-- A list eliminator over an append is one over the first list that starts
25-- from the eliminator of the second; each step sees its tail with the second
26-- list appended.
27let elimAppend
28    : forall {la lr : Level}
29             {a : Type la}
30             {r : Type lr}
31             (n : r)
32             (c : a -> List a -> r -> r)
33             (xs : List a)
34             (ys : List a)
35      -> List/elim (\(_ : List a) -> r) n c (xs # ys)
36         === List/elim
37               (\(_ : List a) -> r)
38               (List/elim (\(_ : List a) -> r) n c ys)
39               (\(x : a) (t : List a) (acc : r) -> c x (t # ys) acc)
40               xs
41    = \{la} {lr} {a} {r} n c xs ys ->
42        List/elim
43          (\(zs : List a) ->
44             List/elim (\(_ : List a) -> r) n c (zs # ys)
45             === List/elim
46                   (\(_ : List a) -> r)
47                   (List/elim (\(_ : List a) -> r) n c ys)
48                   (\(x : a) (t : List a) (acc : r) -> c x (t # ys) acc)
49                   zs)
50          refl
51          (\(x : a)
52            (zs : List a)
53            (ih :
54              List/elim (\(_ : List a) -> r) n c (zs # ys)
55              === List/elim
56                    (\(_ : List a) -> r)
57                    (List/elim (\(_ : List a) -> r) n c ys)
58                    (\(y : a) (t : List a) (acc : r) -> c y (t # ys) acc)
59                    zs) ->
60             E.cong (\(acc : r) -> c x (zs # ys) acc) ih)
61          xs
62
63-- A fold over an append: the fold of the second list is the start of the
64-- fold of the first.
65let foldAppend
66    : forall {la lr : Level}
67             {a : Type la}
68             {r : Type lr}
69             (c : a -> r -> r)
70             (n : r)
71             (xs : List a)
72             (ys : List a)
73      -> L.fold c n (xs # ys) === L.fold c (L.fold c n ys) xs
74    = \{la} {lr} {a} {r} c n xs ys ->
75        elimAppend n (\(x : a) (_ : List a) (acc : r) -> c x acc) xs ys
76
77-- A list eliminator over a choice is the choice of the eliminators.
78let elimIf
79    : forall {la lr : Level}
80             {a : Type la}
81             {r : Type lr}
82             (n : r)
83             (c : a -> List a -> r -> r)
84             (q : Bool)
85             (u : List a)
86             (v : List a)
87      -> List/elim (\(_ : List a) -> r) n c (if q then u else v)
88         === (if q
89              then List/elim (\(_ : List a) -> r) n c u
90              else List/elim (\(_ : List a) -> r) n c v)
91    = \{la} {lr} {a} {r} n c q u v ->
92        Bool/elim
93          (\(k : Bool) ->
94             List/elim (\(_ : List a) -> r) n c (if k then u else v)
95             === (if k
96                  then List/elim (\(_ : List a) -> r) n c u
97                  else List/elim (\(_ : List a) -> r) n c v))
98          refl
99          refl
100          q
101
102-- Eliminators with pointwise equal steps are equal: no function
103-- extensionality is needed.
104let elimCong
105    : forall {la lr : Level}
106             {a : Type la}
107             {r : Type lr}
108             (n : r)
109             (s1 : a -> List a -> r -> r)
110             (s2 : a -> List a -> r -> r)
111             (same : forall (x : a) (t : List a) (acc : r) -> s1 x t acc === s2 x t acc)
112             (xs : List a)
113      -> List/elim (\(_ : List a) -> r) n s1 xs === List/elim (\(_ : List a) -> r) n s2 xs
114    = \{la} {lr} {a} {r} n s1 s2 same xs ->
115        List/elim
116          (\(zs : List a) ->
117             List/elim (\(_ : List a) -> r) n s1 zs === List/elim (\(_ : List a) -> r) n s2 zs)
118          refl
119          (\(x : a)
120            (zs : List a)
121            (ih :
122              List/elim (\(_ : List a) -> r) n s1 zs === List/elim (\(_ : List a) -> r) n s2 zs) ->
123             E.trans
124               (E.cong (\(acc : r) -> s1 x zs acc) ih)
125               (same x zs (List/elim (\(_ : List a) -> r) n s2 zs)))
126          xs
127
128-- A producer: each element puts a prefix, which may read the element and
129-- the rest of the input but not the output, before the output of the rest.
130let build
131    : forall {la lb : Level} {a : Type la} {b : Type lb}
132      -> (a -> List a -> List b)
133      -> List b
134      -> List a
135      -> List b
136    = \{la} {lb} {a} {b} p z ->
137        List/elim
138          (\(_ : List a) -> List b)
139          z
140          (\(x : a) (t : List a) (rest : List b) -> p x t # rest)
141
142-- The end of a producer's output is its start value.
143let buildSuffix
144    : forall {la lb : Level}
145             {a : Type la}
146             {b : Type lb}
147             (p : a -> List a -> List b)
148             (z : List b)
149             (xs : List a)
150      -> build p z xs === build p ([] : List b) xs # z
151    = \{la} {lb} {a} {b} p z xs ->
152        List/elim
153          (\(zs : List a) -> build p z zs === build p ([] : List b) zs # z)
154          refl
155          (\(x : a) (zs : List a) (ih : build p z zs === build p ([] : List b) zs # z) ->
156             E.cong (\(rest : List b) -> p x zs # rest) ih)
157          xs
158
159-- A fold over an eliminator whose step is propositionally a producer's: the
160-- consumer runs on each prefix as it is produced. The hypothesis `law`
161-- states that the step is a prefix append; a conditional prefix needs it,
162-- since the kernel has no commuting conversion.
163let foldBuildBy
164    : forall {la lb lr : Level}
165             {a : Type la}
166             {b : Type lb}
167             {r : Type lr}
168             (c : b -> r -> r)
169             (n : r)
170             (step : a -> List a -> List b -> List b)
171             (p : a -> List a -> List b)
172             (law : forall (x : a) (t : List a) (rest : List b) -> step x t rest === p x t # rest)
173             (z : List b)
174             (xs : List a)
175      -> L.fold c n (List/elim (\(_ : List a) -> List b) z step xs)
176         === List/elim
177               (\(_ : List a) -> r)
178               (L.fold c n z)
179               (\(x : a) (t : List a) (acc : r) -> L.fold c acc (p x t))
180               xs
181    = \{la} {lb} {lr} {a} {b} {r} c n step p law z xs ->
182        let produced = \(zs : List a) -> List/elim (\(_ : List a) -> List b) z step zs
183        let fused =
184              \(zs : List a) ->
185                List/elim
186                  (\(_ : List a) -> r)
187                  (L.fold c n z)
188                  (\(x : a) (t : List a) (acc : r) -> L.fold c acc (p x t))
189                  zs
190        in  List/elim
191              (\(zs : List a) -> L.fold c n (produced zs) === fused zs)
192              refl
193              (\(x : a) (zs : List a) (ih : L.fold c n (produced zs) === fused zs) ->
194                 E.trans
195                   (E.cong (\(ys : List b) -> L.fold c n ys) (law x zs (produced zs)))
196                   (E.trans
197                      (foldAppend c n (p x zs) (produced zs))
198                      (E.cong (\(acc : r) -> L.fold c acc (p x zs)) ih)))
199              xs
200
201-- A fold over a producer is one traversal of the producer's input.
202let foldBuild
203    : forall {la lb lr : Level}
204             {a : Type la}
205             {b : Type lb}
206             {r : Type lr}
207             (c : b -> r -> r)
208             (n : r)
209             (p : a -> List a -> List b)
210             (z : List b)
211             (xs : List a)
212      -> L.fold c n (build p z xs)
213         === List/elim
214               (\(_ : List a) -> r)
215               (L.fold c n z)
216               (\(x : a) (t : List a) (acc : r) -> L.fold c acc (p x t))
217               xs
218    = \{la} {lb} {lr} {a} {b} {r} c n p z xs ->
219        foldBuildBy
220          c
221          n
222          (\(x : a) (t : List a) (rest : List b) -> p x t # rest)
223          p
224          (\(x : a) (t : List a) (rest : List b) -> refl)
225          z
226          xs
227
228-- What a state-passing producer does at one element: return a list, go on
229-- with a state, or put a prefix and go on with a state.
230let Leaf
231    : forall {ls lb : Level} -> Type ls -> Type lb -> Type (Level/max ls lb)
232    = \{ls} {lb} s b -> < Done : List b | Next : s | Emit : { prefix : List b, next : s } >
233
234-- A leaf read by the producer, given the producer of the rest.
235let interpretProducer
236    : forall {ls lb : Level} {s : Type ls} {b : Type lb} -> (s -> List b) -> Leaf s b -> List b
237    = \{ls} {lb} {s} {b} rest leaf ->
238        merge
239          { Done = \(v : List b) -> v
240          , Next = \(next : s) -> rest next
241          , Emit = \(e : { prefix : List b, next : s }) -> e.prefix # rest e.next
242          }
243          leaf
244
245-- A leaf read by a fold of the producer's output, given the fold of the
246-- rest.
247let interpretConsumer
248    : forall {ls lb lr : Level} {s : Type ls} {b : Type lb} {r : Type lr}
249      -> (b -> r -> r)
250      -> r
251      -> (s -> r)
252      -> Leaf s b
253      -> r
254    = \{ls} {lb} {lr} {s} {b} {r} c n rest leaf ->
255        merge
256          { Done = \(v : List b) -> L.fold c n v
257          , Next = \(next : s) -> rest next
258          , Emit = \(e : { prefix : List b, next : s }) -> L.fold c (rest e.next) e.prefix
259          }
260          leaf
261
262-- A fold over a state-passing producer is one traversal of the producer's
263-- input with the same states. A producer without state is the instance at
264-- `s = unit`.
265let foldBuildState
266    : forall {la ls lb lr : Level}
267             {a : Type la}
268             {s : Type ls}
269             {b : Type lb}
270             {r : Type lr}
271             (c : b -> r -> r)
272             (n : r)
273             (z : s -> List b)
274             (d : a -> List a -> s -> Leaf s b)
275             (xs : List a)
276             (s0 : s)
277      -> L.fold
278           c
279           n
280           (List/elim
281              (\(_ : List a) -> s -> List b)
282              z
283              (\(x : a) (t : List a) (rest : s -> List b) (state : s) ->
284                 interpretProducer rest (d x t state))
285              xs
286              s0)
287         === List/elim
288               (\(_ : List a) -> s -> r)
289               (\(state : s) -> L.fold c n (z state))
290               (\(x : a) (t : List a) (rest : s -> r) (state : s) ->
291                  interpretConsumer c n rest (d x t state))
292               xs
293               s0
294    = \{la} {ls} {lb} {lr} {a} {s} {b} {r} c n z d xs s0 ->
295        let produced =
296              \(zs : List a) ->
297                List/elim
298                  (\(_ : List a) -> s -> List b)
299                  z
300                  (\(x : a) (t : List a) (rest : s -> List b) (state : s) ->
301                     interpretProducer rest (d x t state))
302                  zs
303        let consumed =
304              \(zs : List a) ->
305                List/elim
306                  (\(_ : List a) -> s -> r)
307                  (\(state : s) -> L.fold c n (z state))
308                  (\(x : a) (t : List a) (rest : s -> r) (state : s) ->
309                     interpretConsumer c n rest (d x t state))
310                  zs
311        let Same =
312              \(zs : List a) ->
313                forall (state : s) -> L.fold c n (produced zs state) === consumed zs state
314        in  List/elim
315              Same
316              (\(state : s) -> refl)
317              (\(x : a) (zs : List a) (ih : Same zs) (state : s) ->
318                 let byLeaf
319                     : forall (leaf : Leaf s b)
320                       -> L.fold c n (interpretProducer (produced zs) leaf)
321                          === interpretConsumer c n (consumed zs) leaf
322                     = \leaf ->
323                         merge
324                           { Done = \(v : List b) -> refl
325                           , Next = \(next : s) -> ih next
326                           , Emit =
327                               \(e : { prefix : List b, next : s }) ->
328                                 E.trans
329                                   (foldAppend c n e.prefix (produced zs e.next))
330                                   (E.cong (\(acc : r) -> L.fold c acc e.prefix) (ih e.next))
331                           }
332                           leaf
333                 in  byLeaf (d x zs state))
334              xs
335              s0
336
337-- A count-down of a natural as a list: down 3 = [2, 1, 0].
338let down
339    : Natural -> List Natural
340    = \n ->
341        Natural/elim
342          (\(_ : Natural) -> List Natural)
343          ([] : List Natural)
344          (\(k : Natural) (rest : List Natural) -> [ k ] # rest)
345          n
346
347-- `Natural/elim` is a list eliminator over the count-down.
348let naturalDown
349    : forall {lr : Level} {r : Type lr} (z : r) (s : Natural -> r -> r) (n : Natural)
350      -> Natural/elim (\(_ : Natural) -> r) z s n
351         === List/elim
352               (\(_ : List Natural) -> r)
353               z
354               (\(k : Natural) (_ : List Natural) (acc : r) -> s k acc)
355               (down n)
356    = \{lr} {r} z s n ->
357        let fold = \(m : Natural) -> Natural/elim (\(_ : Natural) -> r) z s m
358        let consumed =
359              \(m : Natural) ->
360                List/elim
361                  (\(_ : List Natural) -> r)
362                  z
363                  (\(k : Natural) (_ : List Natural) (acc : r) -> s k acc)
364                  (down m)
365        in  Natural/elim
366              (\(m : Natural) -> fold m === consumed m)
367              refl
368              (\(k : Natural) (ih : fold k === consumed k) -> E.cong (\(acc : r) -> s k acc) ih)
369              n
370
371-- Reverses a list onto another: reverseOnto [1, 2] [3] = [2, 1, 3].
372-- `std:List.reverse xs` is `reverseOnto xs []`.
373let reverseOnto
374    : forall {l : Level} {a : Type l} -> List a -> List a -> List a
375    = \{l} {a} xs ->
376        L.fold
377          (\(x : a) (continue : List a -> List a) (acc : List a) -> continue ([ x ] # acc))
378          (\(acc : List a) -> acc)
379          xs
380
381-- A producer that ignores the tail, over a list reversed onto another.
382let buildReverseOnto
383    : forall {la lb : Level}
384             {a : Type la}
385             {b : Type lb}
386             (q : a -> List b)
387             (xs : List a)
388             (acc : List a)
389             (z : List b)
390      -> build (\(x : a) (_ : List a) -> q x) z (reverseOnto xs acc)
391         === build
392               (\(x : a) (_ : List a) -> q x)
393               (build (\(x : a) (_ : List a) -> q x) z acc)
394               (reverseOnto xs ([] : List a))
395    = \{la} {lb} {a} {b} q xs acc z ->
396        let p = \(x : a) (_ : List a) -> q x
397        let Same =
398              \(zs : List a) ->
399                forall (before : List a) (end : List b)
400                -> build p end (reverseOnto zs before)
401                   === build p (build p end before) (reverseOnto zs ([] : List a))
402        in  List/elim
403              Same
404              (\(before : List a) (end : List b) -> refl)
405              (\(x : a) (zs : List a) (ih : Same zs) (before : List a) (end : List b) ->
406                 E.trans (ih ([ x ] # before) end) (E.sym (ih [ x ] (build p end before))))
407              xs
408              acc
409              z
410
411-- An eliminator that appends after the recursive result produces the
412-- reversed list.
413let elimSnoc
414    : forall {la lb : Level}
415             {a : Type la}
416             {b : Type lb}
417             (q : a -> List b)
418             (n : List b)
419             (xs : List a)
420      -> List/elim
421           (\(_ : List a) -> List b)
422           n
423           (\(x : a) (_ : List a) (rest : List b) -> rest # q x)
424           xs
425         === n # build (\(x : a) (_ : List a) -> q x) ([] : List b) (L.reverse xs)
426    = \{la} {lb} {a} {b} q n xs ->
427        let p = \(x : a) (_ : List a) -> q x
428        let produced =
429              \(zs : List a) ->
430                List/elim
431                  (\(_ : List a) -> List b)
432                  n
433                  (\(x : a) (_ : List a) (rest : List b) -> rest # q x)
434                  zs
435        in  List/elim
436              (\(zs : List a) -> produced zs === n # build p ([] : List b) (L.reverse zs))
437              refl
438              (\(x : a)
439                (zs : List a)
440                (ih : produced zs === n # build p ([] : List b) (L.reverse zs)) ->
441                 E.trans
442                   (E.cong (\(rest : List b) -> rest # q x) ih)
443                   (E.sym
444                      (E.cong
445                         (\(rest : List b) -> n # rest)
446                         (E.trans
447                            (buildReverseOnto q zs [ x ] ([] : List b))
448                            (buildSuffix p (q x) (L.reverse zs))))))
449              xs
450
451-- An eliminator that accumulates prefixes in a state produces the reversed
452-- list onto the first state.
453let accumulateReverse
454    : forall {la lb lr : Level}
455             {a : Type la}
456             {b : Type lb}
457             {r : Type lr}
458             (p : a -> List b)
459             (finish : List b -> r)
460             (xs : List a)
461             (s0 : List b)
462      -> List/elim
463           (\(_ : List a) -> List b -> r)
464           finish
465           (\(x : a) (_ : List a) (rest : List b -> r) (state : List b) -> rest (p x # state))
466           xs
467           s0
468         === finish (build (\(x : a) (_ : List a) -> p x) s0 (L.reverse xs))
469    = \{la} {lb} {lr} {a} {b} {r} p finish xs s0 ->
470        let q = \(x : a) (_ : List a) -> p x
471        let consumed =
472              \(zs : List a) ->
473                List/elim
474                  (\(_ : List a) -> List b -> r)
475                  finish
476                  (\(x : a) (_ : List a) (rest : List b -> r) (state : List b) ->
477                     rest (p x # state))
478                  zs
479        let Same =
480              \(zs : List a) ->
481                forall (state : List b)
482                -> consumed zs state === finish (build q state (L.reverse zs))
483        in  List/elim
484              Same
485              (\(state : List b) -> refl)
486              (\(x : a) (zs : List a) (ih : Same zs) (state : List b) ->
487                 E.trans
488                   (ih (p x # state))
489                   (E.sym (E.cong finish (buildReverseOnto p zs [ x ] state))))
490              xs
491              s0
492
493-- `||` and `&&` are choices on their first operand, and commute.
494let orSplit
495    : forall (a : Bool) (b : Bool) -> a || b === (if a then True else b)
496    = \a b -> Bool/elim (\(k : Bool) -> k || b === (if k then True else b)) refl refl a
497
498let andSplit
499    : forall (a : Bool) (b : Bool) -> a && b === (if a then b else False)
500    = \a b -> Bool/elim (\(k : Bool) -> k && b === (if k then b else False)) refl refl a
501
502let orSwap
503    : forall (a : Bool) (b : Bool) -> a || b === b || a
504    = \a b -> Bool/elim (\(k : Bool) -> k || b === b || k) refl refl a
505
506let andSwap
507    : forall (a : Bool) (b : Bool) -> a && b === b && a
508    = \a b -> Bool/elim (\(k : Bool) -> k && b === b && k) refl refl a
509
510-- The length of a list is the fold that counts its elements: a law that
511-- counts them one at a time is a law about its length.
512let lengthEqFold
513    : forall {l : Level} {a : Type l} (xs : List a)
514      -> L.length xs === L.fold (\(_ : a) (acc : Natural) -> acc + 1) 0 xs
515    = \{l} {a} xs ->
516        List/elim
517          (\(zs : List a) -> L.length zs === L.fold (\(_ : a) (acc : Natural) -> acc + 1) 0 zs)
518          refl
519          (\(x : a)
520            (zs : List a)
521            (ih : L.length zs === L.fold (\(_ : a) (acc : Natural) -> acc + 1) 0 zs) ->
522             E.cong (\(m : Natural) -> m + 1) ih)
523          xs
524
525-- A fold that counts is an addition of the length.
526let foldCount
527    : forall {l : Level} {a : Type l} (z : Natural) (xs : List a)
528      -> L.fold (\(_ : a) (acc : Natural) -> acc + 1) z xs === z + L.length xs
529    = \{l} {a} z xs ->
530        let counting
531            : forall (zs : List a)
532              -> L.fold (\(_ : a) (acc : Natural) -> acc + 1) z zs
533                 === z + L.fold (\(_ : a) (acc : Natural) -> acc + 1) 0 zs
534            = List/elim
535                (\(zs : List a) ->
536                   L.fold (\(_ : a) (acc : Natural) -> acc + 1) z zs
537                   === z + L.fold (\(_ : a) (acc : Natural) -> acc + 1) 0 zs)
538                refl
539                (\(x : a)
540                  (zs : List a)
541                  (ih :
542                    L.fold (\(_ : a) (acc : Natural) -> acc + 1) z zs
543                    === z + L.fold (\(_ : a) (acc : Natural) -> acc + 1) 0 zs) ->
544                   E.cong (\(m : Natural) -> m + 1) ih)
545        in  E.trans (counting xs) (E.sym (E.cong (\(m : Natural) -> z + m) (lengthEqFold xs)))
546
547-- A fold that adds a constant is an addition of a multiple of the length.
548-- Assumes `std:Arith.NatFacts`.
549let foldConstant
550    : A.NatFacts
551      -> forall {l : Level} {a : Type l} (h : Natural) (z : Natural) (xs : List a)
552      -> L.fold (\(_ : a) (acc : Natural) -> acc + h) z xs === z + h * L.length xs
553    = \facts {l} {a} h z xs ->
554        let adding
555            : forall (zs : List a)
556              -> L.fold (\(_ : a) (acc : Natural) -> acc + h) z zs
557                 === z + h * L.fold (\(_ : a) (acc : Natural) -> acc + 1) 0 zs
558            = List/elim
559                (\(zs : List a) ->
560                   L.fold (\(_ : a) (acc : Natural) -> acc + h) z zs
561                   === z + h * L.fold (\(_ : a) (acc : Natural) -> acc + 1) 0 zs)
562                refl
563                (\(x : a)
564                  (zs : List a)
565                  (ih :
566                    L.fold (\(_ : a) (acc : Natural) -> acc + h) z zs
567                    === z + h * L.fold (\(_ : a) (acc : Natural) -> acc + 1) 0 zs) ->
568                   E.trans
569                     (E.cong (\(m : Natural) -> m + h) ih)
570                     (E.sym
571                        (E.trans
572                           (E.cong
573                              (\(m : Natural) -> z + m)
574                              (A.timesSuccRight
575                                 facts
576                                 h
577                                 (L.fold (\(_ : a) (acc : Natural) -> acc + 1) 0 zs)))
578                           (E.sym
579                              (A.plusAssoc
580                                 z
581                                 (h * L.fold (\(_ : a) (acc : Natural) -> acc + 1) 0 zs)
582                                 h)))))
583        in  E.trans (adding xs) (E.sym (E.cong (\(m : Natural) -> z + h * m) (lengthEqFold xs)))
584
585-- A fold over a mapped list is one fold.
586let foldMap
587    : forall {la lb lr : Level}
588             {a : Type la}
589             {b : Type lb}
590             {r : Type lr}
591             (c : b -> r -> r)
592             (n : r)
593             (g : a -> b)
594             (xs : List a)
595      -> L.fold c n (L.map g xs) === L.fold (\(x : a) (acc : r) -> c (g x) acc) n xs
596    = \{la} {lb} {lr} {a} {b} {r} c n g xs ->
597        foldBuild c n (\(x : a) (_ : List a) -> [ g x ]) ([] : List b) xs
598
599let mapMap
600    : forall {la lb lc : Level}
601             {a : Type la}
602             {b : Type lb}
603             {c : Type lc}
604             (f : b -> c)
605             (g : a -> b)
606             (xs : List a)
607      -> L.map f (L.map g xs) === L.map (\(x : a) -> f (g x)) xs
608    = \{la} {lb} {lc} {a} {b} {c} f g xs ->
609        foldMap (\(y : b) (rest : List c) -> [ f y ] # rest) ([] : List c) g xs
610
611let lengthMap
612    : forall {la lb : Level} {a : Type la} {b : Type lb} (g : a -> b) (xs : List a)
613      -> L.length (L.map g xs) === L.length xs
614    = \{la} {lb} {a} {b} g xs ->
615        List/elim
616          (\(zs : List a) -> L.length (L.map g zs) === L.length zs)
617          refl
618          (\(x : a) (zs : List a) (ih : L.length (L.map g zs) === L.length zs) ->
619             E.cong (\(m : Natural) -> m + 1) ih)
620          xs
621
622let anyMap
623    : forall {la lb : Level}
624             {a : Type la}
625             {b : Type lb}
626             (keep : b -> Bool)
627             (g : a -> b)
628             (xs : List a)
629      -> L.any keep (L.map g xs) === L.any (\(x : a) -> keep (g x)) xs
630    = \{la} {lb} {a} {b} keep g xs -> foldMap (\(y : b) (rest : Bool) -> keep y || rest) False g xs
631
632let mapAppend
633    : forall {la lb : Level} {a : Type la} {b : Type lb} (g : a -> b) (xs : List a) (ys : List a)
634      -> L.map g (xs # ys) === L.map g xs # L.map g ys
635    = \{la} {lb} {a} {b} g xs ys ->
636        E.trans
637          (foldAppend (\(x : a) (rest : List b) -> [ g x ] # rest) ([] : List b) xs ys)
638          (buildSuffix (\(x : a) (_ : List a) -> [ g x ]) (L.map g ys) xs)
639
640-- A fold over a filtered list is one fold that skips the rejected elements.
641let foldFilter
642    : forall {la lr : Level}
643             {a : Type la}
644             {r : Type lr}
645             (c : a -> r -> r)
646             (n : r)
647             (keep : a -> Bool)
648             (xs : List a)
649      -> L.fold c n (L.filter keep xs)
650         === L.fold (\(x : a) (acc : r) -> if keep x then c x acc else acc) n xs
651    = \{la} {lr} {a} {r} c n keep xs ->
652        let p = \(x : a) (_ : List a) -> if keep x then [ x ] else ([] : List a)
653        let split
654            : forall (x : a) (rest : List a) (flag : Bool)
655              -> (if flag then [ x ] # rest else rest)
656                 === (if flag then [ x ] else ([] : List a)) # rest
657            = \x rest flag ->
658                Bool/elim
659                  (\(k : Bool) ->
660                     (if k then [ x ] # rest else rest)
661                     === (if k then [ x ] else ([] : List a)) # rest)
662                  refl
663                  refl
664                  flag
665        let consume
666            : forall (x : a) (acc : r) (flag : Bool)
667              -> L.fold c acc (if flag then [ x ] else ([] : List a))
668                 === (if flag then c x acc else acc)
669            = \x acc flag ->
670                Bool/elim
671                  (\(k : Bool) ->
672                     L.fold c acc (if k then [ x ] else ([] : List a))
673                     === (if k then c x acc else acc))
674                  refl
675                  refl
676                  flag
677        in  E.trans
678              (foldBuildBy
679                 c
680                 n
681                 (\(x : a) (_ : List a) (rest : List a) -> if keep x then [ x ] # rest else rest)
682                 p
683                 (\(x : a) (_ : List a) (rest : List a) -> split x rest (keep x))
684                 ([] : List a)
685                 xs)
686              (elimCong
687                 n
688                 (\(x : a) (t : List a) (acc : r) -> L.fold c acc (p x t))
689                 (\(x : a) (_ : List a) (acc : r) -> if keep x then c x acc else acc)
690                 (\(x : a) (_ : List a) (acc : r) -> consume x acc (keep x))
691                 xs)
692
693-- A left fold over a mapped list is one left fold.
694let foldLeftMap
695    : forall {la lb ls : Level}
696             {a : Type la}
697             {b : Type lb}
698             {s : Type ls}
699             (f : s -> b -> s)
700             (g : a -> b)
701             (z : s)
702             (xs : List a)
703      -> L.foldLeft f z (L.map g xs) === L.foldLeft (\(acc : s) (x : a) -> f acc (g x)) z xs
704    = \{la} {lb} {ls} {a} {b} {s} f g z xs ->
705        E.cong
706          (\(run : s -> s) -> run z)
707          (foldMap
708             (\(y : b) (continue : s -> s) (acc : s) -> continue (f acc y))
709             (\(acc : s) -> acc)
710             g
711             xs)
712
713in  { elimAppend
714    , foldAppend
715    , elimIf
716    , elimCong
717    , build
718    , buildSuffix
719    , foldBuildBy
720    , foldBuild
721    , Leaf
722    , interpretProducer
723    , interpretConsumer
724    , foldBuildState
725    , down
726    , naturalDown
727    , reverseOnto
728    , buildReverseOnto
729    , elimSnoc
730    , accumulateReverse
731    , orSplit
732    , andSplit
733    , orSwap
734    , andSwap
735    , lengthEqFold
736    , foldCount
737    , foldConstant
738    , foldMap
739    , mapMap
740    , lengthMap
741    , anyMap
742    , mapAppend
743    , foldFilter
744    , foldLeftMap
745    }
Your result
Complete file: lib/List/Law.omg · with imports