-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.v
656 lines (573 loc) · 16.9 KB
/
List.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
(************************************************************************)
(* * The Coq Proof Assistant / The Coq Development Team *)
(* v * Copyright INRIA, CNRS and contributors *)
(* <O___,, * (see version control and CREDITS file for authors & dates) *)
(* \VV/ **************************************************************)
(* // * This file is distributed under the terms of the *)
(* * GNU Lesser General Public License Version 2.1 *)
(* * (see LICENSE file for the text of the license) *)
(************************************************************************)
(* The interface is an extended version of OCaml stdlib/list.ml. *)
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
(* *)
(* Copyright 1996 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
Require Import Ltac2.Init.
Require Ltac2.Int.
Require Ltac2.Control.
Require Ltac2.Bool.
Require Ltac2.Message.
Ltac2 rec length (ls : 'a list) :=
match ls with
| [] => 0
| _ :: xs => Int.add 1 (length xs)
end.
Ltac2 rec compare_lengths (ls1 : 'a list) (ls2 : 'b list) :=
match ls1 with
| []
=> match ls2 with
| [] => 0
| _ :: _ => -1
end
| _ :: ls1
=> match ls2 with
| [] => 1
| _ :: ls2 => compare_lengths ls1 ls2
end
end.
Ltac2 rec compare_length_with (ls : 'a list) (n : int) :=
match Int.lt n 0 with
| true => 1
| false
=> match ls with
| [] => Int.compare 0 n
| _ :: ls => compare_length_with ls (Int.sub n 1)
end
end.
Ltac2 cons (x : 'a) (xs : 'a list) :=
x :: xs.
(* Since Ltac-2 distinguishes between backtracking and fatal exceptions,
we provide option and default variants of functions which throw in the
OCaml stdlib. *)
Ltac2 hd_opt (ls : 'a list) :=
match ls with
| [] => None
| x :: _ => Some x
end.
Ltac2 hd (ls : 'a list) :=
match ls with
| [] => Control.throw_invalid_argument "List.hd"
| x :: _ => x
end.
Ltac2 tl (ls : 'a list) :=
match ls with
| [] => []
| _ :: xs => xs
end.
Ltac2 dest (xs : 'a list) : 'a * 'a list :=
match xs with
| x :: xs => (x, xs)
| [] => Control.throw_invalid_argument "List.dest: list empty"
end.
Ltac2 is_empty (xs : 'a list) : bool :=
match xs with
| _ :: _ => false
| _ => true
end.
Ltac2 rec last_opt (ls : 'a list) :=
match ls with
| [] => None
| x :: xs
=> match xs with
| [] => Some x
| _ :: _ => last_opt xs
end
end.
Ltac2 last (ls : 'a list) :=
match last_opt ls with
| None => Control.throw_invalid_argument "List.last"
| Some v => v
end.
Ltac2 rec removelast (ls : 'a list) :=
match ls with
| [] => []
| x :: xs
=> match xs with
| [] => []
| _ :: _ => x :: removelast xs
end
end.
Ltac2 rec nth_opt_aux (ls : 'a list) (n : int) :=
match ls with
| [] => None
| x :: xs
=> match Int.equal n 0 with
| true => Some x
| false => nth_opt_aux xs (Int.sub n 1)
end
end.
Ltac2 nth_opt (ls : 'a list) (n : int) :=
Control.assert_valid_argument "List.nth" (Int.ge n 0);
nth_opt_aux ls n.
Ltac2 nth (ls : 'a list) (n : int) :=
match nth_opt ls n with
| Some v => v
| None => Control.throw_out_of_bounds "List.nth"
end.
Ltac2 rec rev_append (l1 : 'a list) (l2 : 'a list) :=
match l1 with
| [] => l2
| a :: l => rev_append l (a :: l2)
end.
Ltac2 rev l := rev_append l [].
Ltac2 rec append ls1 ls2 :=
match ls1 with
| [] => ls2
| x :: xs => x :: append xs ls2
end.
Ltac2 rec concat (ls : 'a list list) :=
match ls with
| [] => []
| x :: xs => append x (concat xs)
end.
Ltac2 flatten (ls : 'a list list) := concat ls.
Ltac2 rec iter (f : 'a -> unit) (ls : 'a list) :=
match ls with
| [] => ()
| l :: ls => f l; iter f ls
end.
Ltac2 rec iteri_aux (i : int) (f : int -> 'a -> unit) (ls : 'a list) :=
match ls with
| [] => ()
| l :: ls => f i l; iteri_aux (Int.add i 1) f ls
end.
Ltac2 iteri (f : int -> 'a -> unit) (ls : 'a list) :=
iteri_aux 0 f ls.
Ltac2 rec map (f : 'a -> 'b) (ls : 'a list) :=
match ls with
| [] => []
| l :: ls => f l :: map f ls
end.
Ltac2 rec mapi_aux (i : int) (f : int -> 'a -> 'b) (ls : 'a list) :=
match ls with
| [] => []
| l :: ls => f i l :: mapi_aux (Int.add i 1) f ls
end.
Ltac2 mapi (f : int -> 'a -> 'b) (ls : 'a list) :=
mapi_aux 0 f ls.
Ltac2 rec flat_map (f : 'a -> 'b list) (xs : 'a list) :=
match xs with
| [] => []
| x :: xs => append (f x) (flat_map f xs)
end.
(* from the OCaml std lib *)
Ltac2 rev_map (f : 'a -> 'b) (ls : 'a list) :=
let rec rmap_f accu ls :=
match ls with
| [] => accu
| a::l => rmap_f (f a :: accu) l
end in
rmap_f [] ls.
Ltac2 rec fold_right (f : 'a -> 'b -> 'b) (ls : 'a list) (a : 'b) : 'b :=
match ls with
| [] => a
| l :: ls => f l (fold_right f ls a)
end.
Ltac2 rec fold_left (f : 'a -> 'b -> 'a) (a : 'a) (xs : 'b list) : 'a :=
match xs with
| [] => a
| x :: xs => fold_left f (f a x) xs
end.
Ltac2 fold_lefti (f : int -> 'a -> 'b -> 'a) (a : 'a) (xs : 'b list) : 'a :=
let rec go i a xs :=
match xs with
| [] => a
| x :: xs => go (Int.add i 1) (f i a x) xs
end
in go 0 a xs.
Ltac2 rec iter2 (f : 'a -> 'b -> unit) (ls1 : 'a list) (ls2 : 'b list) :=
match ls1 with
| []
=> match ls2 with
| [] => ()
| _ :: _ => Control.throw_invalid_argument "List.iter2"
end
| l1 :: ls1
=> match ls2 with
| [] => Control.throw_invalid_argument "List.iter2"
| l2 :: ls2
=> f l1 l2; iter2 f ls1 ls2
end
end.
Ltac2 rec map2 (f : 'a -> 'b -> 'c) (ls1 : 'a list) (ls2 : 'b list) :=
match ls1 with
| []
=> match ls2 with
| [] => []
| _ :: _ => Control.throw_invalid_argument "List.map2"
end
| l1 :: ls1
=> match ls2 with
| [] => Control.throw_invalid_argument "List.map2"
| l2 :: ls2
=> f l1 l2 :: map2 f ls1 ls2
end
end.
(* from the OCaml std lib *)
Ltac2 rev_map2 (f : 'a -> 'b -> 'c) (ls1 : 'a list) (ls2 : 'b list) :=
let rec rmap2_f accu ls1 ls2 :=
match ls1 with
| []
=> match ls2 with
| [] => accu
| _ :: _ => Control.throw_invalid_argument "List.rev_map2"
end
| l1 :: ls1
=> match ls2 with
| [] => Control.throw_invalid_argument "List.rev_map2"
| l2 :: ls2
=> rmap2_f (f l1 l2 :: accu) ls1 ls2
end
end in
rmap2_f [] ls1 ls2.
Ltac2 rec fold_right2 (f : 'a -> 'b -> 'c -> 'c) (a : 'c) (ls1 : 'a list) (ls2 : 'b list) :=
match ls1 with
| []
=> match ls2 with
| [] => a
| _ :: _ => Control.throw_invalid_argument "List.fold_right2"
end
| l1 :: ls1
=> match ls2 with
| [] => Control.throw_invalid_argument "List.fold_right2"
| l2 :: ls2
=> f l1 l2 (fold_right2 f a ls1 ls2)
end
end.
Ltac2 rec fold_left2 (f : 'a -> 'b -> 'c -> 'a) (ls1 : 'b list) (ls2 : 'c list) (a : 'a) :=
match ls1 with
| []
=> match ls2 with
| [] => a
| _ :: _ => Control.throw_invalid_argument "List.fold_left2"
end
| l1 :: ls1
=> match ls2 with
| [] => Control.throw_invalid_argument "List.fold_left2"
| l2 :: ls2
=> fold_left2 f ls1 ls2 (f a l1 l2)
end
end.
Ltac2 rec for_all f ls :=
match ls with
| [] => true
| x :: xs => match f x with
| true => for_all f xs
| false => false
end
end.
(* we would call this [exists] a la OCaml's [List.exists], but that's a syntax error, so instead we name it exist *)
Ltac2 rec exist f ls :=
match ls with
| [] => false
| x :: xs => match f x with
| true => true
| false => exist f xs
end
end.
Ltac2 rec for_all2_aux (on_length_mismatch : 'a list -> 'b list -> bool) f xs ys :=
match xs with
| [] => match ys with
| [] => true
| _ :: _ => on_length_mismatch xs ys
end
| x :: xs'
=> match ys with
| [] => on_length_mismatch xs ys
| y :: ys'
=> match f x y with
| true => for_all2_aux on_length_mismatch f xs' ys'
| false => false
end
end
end.
Ltac2 for_all2 f xs ys := for_all2_aux (fun _ _ => Control.throw_invalid_argument "List.for_all2") f xs ys.
Ltac2 equal f xs ys := for_all2_aux (fun _ _ => false) f xs ys.
Ltac2 rec exist2 f xs ys :=
match xs with
| [] => match ys with
| [] => false
| _ :: _ => Control.throw_invalid_argument "List.exist2"
end
| x :: xs'
=> match ys with
| [] => Control.throw_invalid_argument "List.exist2"
| y :: ys'
=> match f x y with
| true => true
| false => exist2 f xs' ys'
end
end
end.
Ltac2 rec find_opt f xs :=
match xs with
| [] => None
| x :: xs => match f x with
| true => Some x
| false => find_opt f xs
end
end.
Ltac2 find f xs :=
match find_opt f xs with
| Some v => v
| None => Control.throw Not_found
end.
Ltac2 rec find_rev_opt f xs :=
match xs with
| [] => None
| x :: xs => match find_rev_opt f xs with
| Some v => Some v
| None => match f x with
| true => Some x
| false => None
end
end
end.
Ltac2 find_rev f xs :=
match find_rev_opt f xs with
| Some v => v
| None => Control.throw Not_found
end.
Ltac2 mem (eq : 'a -> 'a -> bool) (a : 'a) (ls : 'a list) :=
exist (eq a) ls.
Ltac2 rec filter f xs :=
match xs with
| [] => []
| x :: xs
=> match f x with
| true => x :: filter f xs
| false => filter f xs
end
end.
Ltac2 rec filter_out f xs :=
filter (fun x => Bool.neg (f x)) xs.
Ltac2 find_all (f : 'a -> bool) (ls : 'a list) := filter f ls.
Ltac2 remove (eqb : 'a -> 'a -> bool) (x : 'a) (ls : 'a list) :=
filter_out (eqb x) ls.
Ltac2 count_occ (eqb : 'a -> 'a -> bool) (x : 'a) (ls : 'a list) :=
length (filter (eqb x) ls).
(* from the Coq stdlib *)
Ltac2 rec list_power (ls1 : 'a list) (ls2 : 'b list) :=
match ls1 with
| [] => [] :: []
| x :: t
=> flat_map (fun f => map (fun y => (x, y) :: f) ls2)
(list_power t ls2)
end.
Ltac2 rec partition (f : 'a -> bool) (l : 'a list) :=
match l with
| [] => ([], [])
| x :: tl
=> let (g, d) := partition f tl in
match f x with
| true => ((x::g), d)
| false => (g, (x::d))
end
end.
(* from the Coq stdlib *)
(** [list_prod] has the same signature as [combine], but unlike
[combine], it adds every possible pairs, not only those at the
same position. *)
Ltac2 rec list_prod (ls1 : 'a list) (ls2 : 'b list) :=
match ls1 with
| [] => []
| x :: t
=> append (map (fun y => (x, y)) ls2) (list_prod t ls2)
end.
Ltac2 rec firstn (n : int) (ls : 'a list) :=
Control.assert_valid_argument "List.firstn" (Int.ge n 0);
match Int.equal n 0 with
| true => []
| false
=> match ls with
| [] => Control.throw_out_of_bounds "List.firstn"
| x :: xs
=> x :: firstn (Int.sub n 1) xs
end
end.
Ltac2 rec skipn (n : int) (ls : 'a list) :=
Control.assert_valid_argument "List.skipn" (Int.ge n 0);
match Int.equal n 0 with
| true => ls
| false
=> match ls with
| [] => Control.throw_out_of_bounds "List.skipn"
| _ :: xs
=> skipn (Int.sub n 1) xs
end
end.
Ltac2 lastn (n : int) (ls : 'a list) :=
let l := length ls in
Control.assert_valid_argument "List.lastn" (Int.ge n 0);
Control.assert_bounds "List.lastn" (Int.le n l);
skipn (Int.sub l n) ls.
Ltac2 rec nodup (eqb : 'a -> 'a -> bool) (ls : 'a list) :=
match ls with
| [] => []
| x :: xs
=> match mem eqb x xs with
| true => nodup eqb xs
| false => x :: nodup eqb xs
end
end.
(* seq start 1 last = start :: start + 1 :: ... :: (last - 1) *)
Ltac2 rec seq (start : int) (step : int) (last : int) :=
match Int.lt (Int.sub last start) step with
| true
=> []
| false
=> start :: seq (Int.add start step) step last
end.
Ltac2 init (len : int) (f : int -> 'a) :=
Control.assert_valid_argument "List.init" (Int.ge len 0);
map f (seq 0 1 len).
Ltac2 repeat (x : 'a) (n : 'int) :=
init n (fun _ => x).
Ltac2 assoc (eqk : 'k -> 'k -> bool) (k : 'k) (l : ('k * 'v) list) :=
let eq_key kv := let (k', _) := kv in eqk k k' in
let (_, v) := find eq_key l in
v.
Ltac2 assoc_opt (eqk : 'k -> 'k -> bool) (k : 'k) (l : ('k * 'v) list) :=
let eq_key kv := let (k', _) := kv in eqk k k' in
match find_opt eq_key l with
| Some kv => let (_, v) := kv in Some v
| None => None
end.
Ltac2 mem_assoc (eqk : 'k -> 'k -> bool) (k : 'k) (l : ('k * 'v) list) :=
let eq_key kv := let (k', _) := kv in eqk k k' in
exist eq_key l.
Ltac2 remove_assoc (eqk : 'k -> 'k -> bool) (k : 'k) (l : ('k * 'v) list) :=
let eq_key kv := let (k', _) := kv in eqk k k' in
filter_out eq_key l.
Ltac2 rec split (ls : ('a * 'b) list) :=
match ls with
| [] => ([], [])
| xy :: tl
=> let (x, y) := xy in
let (left, right) := split tl in
((x::left), (y::right))
end.
Ltac2 rec combine (ls1 : 'a list) (ls2 : 'b list) :=
match ls1 with
| []
=> match ls2 with
| [] => []
| _ :: _ => Control.throw_invalid_argument "List.combine"
end
| x :: xs
=> match ls2 with
| y :: ys
=> (x, y) :: combine xs ys
| [] => Control.throw_invalid_argument "List.combine"
end
end.
Ltac2 enumerate (ls : 'a list) :=
combine (seq 0 1 (length ls)) ls.
(* from Coq stdlib *)
Ltac2 rec merge (cmp : 'a -> 'a -> int) (l1 : 'a list) (l2 : 'b list) :=
let rec merge_aux l2 :=
match l1 with
| [] => l2
| a1 :: l1'
=> match l2 with
| [] => l1
| a2 :: l2'
=> match Int.le (cmp a1 a2) 0 with
| true => a1 :: merge cmp l1' l2
| false => a2 :: merge_aux l2'
end
end
end in
merge_aux l2.
Ltac2 rec merge_list_to_stack cmp stack l :=
match stack with
| [] => [Some l]
| l' :: stack'
=> match l' with
| None => Some l :: stack'
| Some l'
=> None :: merge_list_to_stack cmp stack' (merge cmp l' l)
end
end.
Ltac2 rec merge_stack cmp stack :=
match stack with
| [] => []
| l :: stack'
=> match l with
| None => merge_stack cmp stack'
| Some l => merge cmp l (merge_stack cmp stack')
end
end.
Ltac2 rec iter_merge cmp stack l :=
match l with
| [] => merge_stack cmp stack
| a::l' => iter_merge cmp (merge_list_to_stack cmp stack [a]) l'
end.
Ltac2 sort cmp l := iter_merge cmp [] l.
(* TODO: maybe replace this with a faster implementation *)
Ltac2 sort_uniq (cmp : 'a -> 'a -> int) (l : 'a list) :=
let rec uniq l :=
match l with
| [] => []
| x1 :: xs
=> match xs with
| [] => x1 :: xs
| x2 :: _
=> match Int.equal (cmp x1 x2) 0 with
| true => uniq xs
| false => x1 :: uniq xs
end
end
end in
uniq (sort cmp l).
Ltac2 inclusive_range (lb : int) (ub : int) : int list :=
let rec go lb ub :=
if Int.gt lb ub then [] else lb :: go (Int.add lb 1) ub
in
go lb ub.
Ltac2 range (lb : int) (ub : int) : int list :=
inclusive_range lb (Int.sub ub 1).
(** [concat_rev [x1; ..; xN-1; xN]] computes [rev xN ++ rev xN-1 ++ .. x1].
Note that [x1] is not reversed and appears in its original order.
[concat_rev] is faster than [concat] and should be preferred over [concat]
when the order of items does not matter. *)
Ltac2 concat_rev (ls : 'a list list) : 'a list :=
let rec go ls acc :=
match ls with
| [] => acc
| l :: ls => go ls (rev_append l acc)
end
in
match ls with
| [] => []
| l :: ls => go ls l
end.
Ltac2 rec map_filter (f : 'a -> 'b option) (l : 'a list) : 'b list :=
match l with
| [] => []
| x :: l =>
match f x with
| Some y => y :: map_filter f l
| None => map_filter f l
end
end.