-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathList.lp
1610 lines (1320 loc) Β· 49.5 KB
/
List.lp
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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Library on polymorphic lists
by Quentin Buzet (July 2022)
following https://github.com/math-comp/math-comp/blob/master/mathcomp/ssreflect/seq.v
seq.v documentation
-------------------
The library provides many operations. The operations are geared towards
reflection: they generally expect and provide boolean predicates.
As there is no true subtyping, we don't use a type for non-empty
sequences; rather, we pass explicitly the head and tail of the sequence.
The empty sequence is especially bothersome for subscripting, since it
forces us to pass a default value. This default value can often be hidden
by a notation.
Here is the list of seq operations:
** Constructors:
seq T == the type of sequences of items of type T.
bitseq == seq bool.
β‘ == the empty sequence (of type T).
x βΈ¬ s == the sequence x followed by s (of type T).
rcons s x == the sequence s, followed by x.
All of the above, except rcons, can be used in patterns.
** Factories:
nseq n x == a sequence of n x's.
ncons n x s == a sequence of n x's, followed by s.
seqn n x_0 ... x_n-1 == the sequence of the x_i; can be partially applied.
iota m n == the sequence m, m + 1, ..., m + n - 1.
mkseq f n == the sequence f 0, f 1, ..., f (n - 1).
** Sequential access:
head x0 s == the head (zero'th item) of s if s is non-empty, else x0.
behead s == s minus its head, i.e., s' if s = x :: s', else β‘.
last x s == the last element of x :: s (which is non-empty).
belast x s == x :: s minus its last item.
** Dimensions:
size s == the number of items (length) in s.
** Random access:
nth x0 s i == the item i of s (numbered from 0), or x0 if s does
not have at least i+1 items (i.e., size x <= i)
s`_i == standard notation for nth x0 s i for a default x0,
e.g., 0 for rings.
set_nth x0 s i y == s where item i has been changed to y; if s does not
have an item i, it is first padded with copies of x0
to size i+1.
incr_nth s i == the nat sequence s with item i incremented (s is
first padded with 0's to size i+1, if needed).
** Predicates:
nilp s <=> s is β‘.
:= (size s == 0).
x β s == x appears in s (this requires an eqType for T).
index x s == the first index at which x appears in s, or size s if
x \notin s.
has a s <=> a holds for some item in s, where a is an applicative
bool predicate.
all a s <=> a holds for all items in s.
all2 r s t <=> the (bool) relation r holds for all _respective_ items
in s and t, which must also have the same size, i.e.,
for s := x1 βΈ¬ ... βΈ¬ x_m βΈ¬ β‘ and t := y1 βΈ¬ ... βΈ¬ y_n βΈ¬ β‘,
the condition [&& r x_1 y_1, ..., r x_n y_n & m == n].
find p s == the index of the first item in s for which p holds,
or size s if no such item is found.
count p s == the number of items of s for which p holds.
count_mem x s == the multiplicity of x in s, i.e., count (pred1 x) s.
constant s <=> all items in s are identical (trivial if s = β‘).
uniq s <=> all the items in s are pairwise different.
subseq s1 s2 <=> s1 is a subsequence of s2, i.e., s1 = mask m s2 for
some m : bitseq (see below).
infix s1 s2 <=> s1 is a contiguous subsequence of s2, i.e.,
s ++ s1 ++ s' = s2 for some sequences s, s'.
prefix s1 s2 <=> s1 is a subchain of s2 appearing at the beginning
of s2.
suffix s1 s2 <=> s1 is a subchain of s2 appearing at the end of s2.
infix_index s1 s2 <=> the first index at which s1 appears in s2,
or (size s2).+1 if infix s1 s2 is false.
perm_eq s1 s2 <=> s2 is a permutation of s1, i.e., s1 and s2 have the
items (with the same repetitions), but possibly in a
different order.
** Filtering:
filter p s == the subsequence of s consisting of all the items
for which the (boolean) predicate p holds.
rem x s == the subsequence of s, where the first occurrence
of x has been removed (compare filter (predC1 x) s
where ALL occurrences of x are removed).
undup s == the subsequence of s containing only the first
occurrence of each item in s, i.e., s with all
duplicates removed.
mask m s == the subsequence of s selected by m : bitseq, with
item i of s selected by bit i in m (extra items or
bits are ignored.
** Surgery:
s1 ++ s2 == the concatenation of s1 and s2.
take n s == the sequence containing only the first n items of s
(or all of s if size s <= n).
drop n s == s minus its first n items (β‘ if size s <= n)
rot n s == s rotated left n times (or s if size s <= n).
:= drop n s ++ take n s
rotr n s == s rotated right n times (or s if size s <= n).
rev s == the (linear time) reversal of s.
** Iterators: for s == x_1 βΈ¬ ... βΈ¬ x_n βΈ¬ β‘, t == y_1 βΈ¬ ... βΈ¬ y_m βΈ¬ β‘,
map f s == the sequence f x_1 βΈ¬ ... βΈ¬ f x_n βΈ¬ β‘.
zip s t == itemwise pairing of s and t (dropping any extra items).
:= x_1 & y_1 βΈ¬ ... βΈ¬ x_mn & y_mn βΈ¬ β‘ with mn = minn n m.
unzip1 s == x_1 .1 βΈ¬ ... βΈ¬ x_n .1 βΈ¬ β‘ when s : seq (S * T).
unzip2 s == x_1 .2 βΈ¬ ... βΈ¬ x_n .2 βΈ¬ β‘ when s : seq (S * T).
Not available
-------------
ohead s == None if s is empty, else Some x when the head of s is x.
shape ss == the sequence of sizes of the items of the sequence of
sequences ss.
'has_aP <-> the view reflect (exists2 x, x \in s & A x) (has a s),
where aP x : reflect (A x) (a x).
'all_aP <=> the view for reflect {in s, forall x, A x} (all a s).
tally s == a tally of s, i.e., a sequence of (item, multiplicity)
pairs for all items in sequence s (without duplicates).
incr_tally bs x == increment the multiplicity of x in the tally bs, or add
x with multiplicity 1 at then end if x is not in bs.
bs \is a wf_tally <=> bs is well-formed tally, with no duplicate items or
null multiplicities.
tally_seq bs == the expansion of a tally bs into a sequence where each
(x, n) pair expands into a sequence of n x's.
perm_eql s1 s2 <-> s1 and s2 behave identically on the left of perm_eq.
perm_eqr s1 s2 <-> s1 and s2 behave identically on the right of perm_eq.
--> These left/right transitive versions of perm_eq make it easier to
chain a sequence of equivalences.
permutations s == a duplicate-free list of all permutations of s.
catrev s1 s2 == the reversal of s1 followed by s2 (this is the
recursive form of rev).
** Dependent iterator: for s : seq S and t : S -> seq T
[seq E | x <- s, y <- t] := flatten [seq [seq E | x <- t] | y <- s]
== the sequence of all the f x y, with x and y drawn from
s and t, respectively, in row-major order,
and where t is possibly dependent in elements of s
allpairs_dep f s t := self expanding definition for
[seq f x y | x <- s, y <- t y]
** Iterators: for s == x_1 βΈ¬ ... βΈ¬ x_n βΈ¬ β‘, t == y_1 βΈ¬ ... βΈ¬ y_m βΈ¬ β‘,
allpairs f s t := same as allpairs_dep but where t is non dependent,
i.e. self expanding definition for
[seq f x y | x <- s, y <- t]
:= f x_1 y_1 βΈ¬ ... βΈ¬ f x_1 y_m βΈ¬ f x_2 y_1 βΈ¬ ... βΈ¬ f x_n y_m βΈ¬ β‘
allrel r xs ys := all [pred x | all (r x) ys] xs
<=> r x y holds whenever x is in xs and y is in ys
all2rel r xs := allrel r xs xs
<=> the proposition r x y holds for all possible x, y in xs.
pairwise r xs <=> the relation r holds for any i-th and j-th element of
xs such that i < j.
pmap pf s == the sequence y_i1 βΈ¬ ... βΈ¬ y_ik βΈ¬ β‘ where i1 < ... < ik,
pf x_i = Some y_i, and pf x_j = None iff j is not in
{i1, ..., ik}.
foldr f a s == the right fold of s by f (i.e., the natural iterator).
:= f x_1 (f x_2 ... (f x_n a))
sumn s == x_1 + (x_2 + ... + (x_n + 0)) (when s : seq nat).
foldl f a s == the left fold of s by f.
:= f (f ... (f a x_1) ... x_n-1) x_n
scanl f a s == the sequence of partial accumulators of foldl f a s.
:= f a x_1 βΈ¬ ... βΈ¬ foldl f a s
pairmap f a s == the sequence of f applied to consecutive items in a :: s.
:= f a x_1 βΈ¬ f x_1 x_2 βΈ¬ ... βΈ¬ f x_n-1 x_n βΈ¬ β‘
flatten s == x_1 ++ ... ++ x_n when s : seq (seq T).
reshape r s == s reshaped into a sequence of sequences whose sizes are
given by r (truncating if s is too long or too short).
:= (x_1 βΈ¬ ... βΈ¬ x_r1 βΈ¬ β‘)
βΈ¬ (x_(r1 + 1) βΈ¬ ... βΈ¬ x_(r0 + r1) βΈ¬ β‘)
βΈ¬ ...
βΈ¬ (x_(r1 + ... + r(k-1) + 1) βΈ¬ ... βΈ¬ x_(r0 + ... rk) βΈ¬ β‘)
flatten_index sh r c == the index, in flatten ss, of the item of indexes
(r, c) in any sequence of sequences ss of shape sh
:= sh_1 + sh_2 + ... + sh_r + c
reshape_index sh i == the index, in reshape sh s, of the sequence
containing the i-th item of s.
reshape_offset sh i == the offset, in the (reshape_index sh i)-th
sequence of reshape sh s of the i-th item of s
** Notation for manifest comprehensions:
[seq x <- s | C] := filter (fun x => C) s.
[seq E | x <- s] := map (fun x => E) s.
[seq x <- s | C1 & C2] := [seq x <- s | C1 && C2].
[seq E | x <- s & C] := [seq E | x <- [seq x | C]].
--> The above allow optional type casts on the eigenvariables, as in
[seq x : T <- s | C] or [seq E | x : T <- s, y : U <- t]. The cast may be
needed as type inference considers E or C before s.
We are quite systematic in providing lemmas to rewrite any composition
of two operations. "rev", whose simplifications are not natural, is
protected with nosimpl.
** The following are equivalent:
[<-> P0; P1; ..; Pn] <-> P0, P1, ..., Pn are all equivalent.
:= P0 -> P1 -> ... -> Pn -> P0
if T : [<-> P0; P1; ..; Pn] is such an equivalence, and i, j are in nat
then T i j is a proof of the equivalence Pi <-> Pj between Pi and Pj;
when i (resp. j) is out of bounds, Pi (resp. Pj) defaults to P0.
The tactic tfae splits the goal into n+1 implications to prove.
An example of use can be found in fingraph theorem orbitPcycle.
*/
require open Stdlib.Set Stdlib.Prop Stdlib.FOL Stdlib.Eq
Stdlib.Nat Stdlib.Bool;
(a:Set) inductive π:TYPE β
| β‘ : π a // \Box
| βΈ¬ : Ο a β π a β π a; // ::
notation βΈ¬ infix right 20;
// set code for π
constant symbol list : Set β Set;
rule Ο (list $a) βͺ π $a;
// isβ‘
symbol isβ‘ [a]: π a β πΉ;
rule isβ‘ β‘ βͺ true
with isβ‘ (_ βΈ¬ _) βͺ false;
// non confusion of constructors
opaque symbol βΈ¬β β‘ [a] [x:Ο a] [l] : Ο (x βΈ¬ l β β‘) β
begin
assume a x l h; refine ind_eq h (Ξ» l, istrue(isβ‘ l)) β€α΅’
end;
opaque symbol β‘β βΈ¬ [a] [x:Ο a] [l] : Ο (β‘ β x βΈ¬ l) β
begin
assume a x l h; refine @βΈ¬β β‘ a x l _; symmetry; apply h
end;
// head
symbol head [a] : Ο a β π a β Ο a;
rule head $x β‘ βͺ $x
with head _ ($x βΈ¬ _) βͺ $x;
// tail
symbol behead [a] : π a β π a;
rule behead β‘ βͺ β‘
with behead (_ βΈ¬ $l) βͺ $l;
// injectivity of constructors
opaque symbol βΈ¬_inj [a] [x:Ο a] [l y m] : Ο(x βΈ¬ l = y βΈ¬ m) β Ο(x = y β§ l = m) β
begin
assume a x l y m e; apply β§α΅’ { apply feq (head x) e } { apply feq behead e }
end;
// boolean equality on lists
symbol eql [a] : (Ο a β Ο a β πΉ) β π a β π a β πΉ;
rule eql _ β‘ β‘ βͺ true
with eql _ (_ βΈ¬ _) β‘ βͺ false
with eql _ β‘ (_ βΈ¬ _) βͺ false
with eql $beq ($x βΈ¬ $l) ($y βΈ¬ $m) βͺ ($beq $x $y) and (eql $beq $l $m);
opaque symbol eql_correct a (beq:Ο a β Ο a β πΉ) :
Ο(`β x, `β y, beq x y β x = y) β Ο(`β l, `β m, eql beq l m β l = m) β
begin
assume a beq beq_correct; induction
{ induction
{ reflexivity }
{ simplify; assume y m i c; refine β₯β c }
}
{ assume x l h; induction
{ simplify; assume c; refine β₯β c; }
{ simplify; assume y m i c;
apply feq2 (βΈ¬) _ _
{ apply beq_correct; apply @andββ _ (eql beq l m) c}
{ apply h; refine @andββ (beq x y) _ c
}
}
}
end;
opaque symbol eql_complete a (beq : Ο a β Ο a β πΉ) :
Ο(`β x, `β y, x = y β beq x y) β Ο(`β l, `β m, l = m β eql beq l m) β
begin
assume a beq beq_complete; induction
{ assume m i; rewrite left i; apply β€α΅’; }
{ assume x l h; induction
{ assume j; apply βΈ¬β β‘ j; }
{ assume y m i j; simplify;
have j': Ο(x = y β§ l = m) { apply βΈ¬_inj j };
apply @istrue_and (beq x y) (eql beq l m); apply β§α΅’
{ apply beq_complete x y; apply β§ββ j' }
{ apply h m; apply β§ββ j' }
}
}
end;
// size
symbol size [a] : π a β β;
rule size β‘ βͺ 0
with size (_ βΈ¬ $l) βͺ size $l +1;
opaque symbol size0nil [a] (l:π a) : Ο (size l = 0) β Ο (l = β‘) β
begin
assume a; induction
{ reflexivity; }
{ assume e l h i; apply β₯β; apply sβ 0 i; }
end;
symbol nilp [a] l β is0 (@size a l);
opaque symbol size_behead [a] (l:π a) : Ο (size (behead l) = size l βΈ1) β
begin
assume a; induction
{ reflexivity; }
{ assume e l h; reflexivity; }
end;
// concatenation
symbol ++ [a] : π a β π a β π a; notation ++ infix right 30; // \cdot
assert x y z β’ x ++ y ++ z β‘ x ++ (y ++ z);
assert x l m β’ x βΈ¬ l ++ m β‘ x βΈ¬ (l ++ m);
rule β‘ ++ $m βͺ $m
with ($x βΈ¬ $l) ++ $m βͺ $x βΈ¬ ($l ++ $m);
opaque symbol cat0s [a] (l:π a) : Ο (β‘ ++ l = l) β
begin
reflexivity;
end;
opaque symbol cat1s [a] (x:Ο a) l : Ο ((x βΈ¬ β‘) ++ l = (x βΈ¬ l)) β
begin
reflexivity;
end;
opaque symbol cat_cons [a] (x:Ο a) l1 l2 : Ο ((x βΈ¬ l1) ++ l2 = x βΈ¬ (l1 ++ l2)) β
begin
reflexivity;
end;
// nseq
symbol nseq [a] : β β Ο a β π a;
rule nseq 0 _ βͺ β‘
with nseq ($n +1) $x βͺ $x βΈ¬ (nseq $n $x);
// ncons
symbol ncons [a] : β β Ο a β π a β π a;
rule ncons 0 _ $l βͺ $l
with ncons ($n +1) $x $l βͺ $x βΈ¬ ncons $n $x $l;
opaque symbol size_ncons [a] n (x:Ο a) l : Ο (size (ncons n x l) = n + size l) β
begin
assume a; induction
{ reflexivity; }
{ assume n h x l; simplify; apply feq (+1) (h x l); }
end;
opaque symbol size_nseq [a] n (x:Ο a) : Ο (size (nseq n x) = n) β
begin
assume a; induction
{ reflexivity; }
{ assume n h x; simplify; apply feq (+1) (h x); }
end;
opaque symbol cat_nseq [a] n (x:Ο a) l : Ο (nseq n x ++ l = ncons n x l) β
begin
assume a; induction
{ reflexivity; }
{ assume n h x l; simplify; rewrite h x l; reflexivity; }
end;
opaque symbol nseqD [a] n1 n2 (x:Ο a) :
Ο (nseq (n1 + n2) x = nseq n1 x ++ nseq n2 x) β
begin
assume a; induction
{ reflexivity; }
{ assume n1 h n2 x; simplify; rewrite h n2; reflexivity; }
end;
opaque symbol cats0 [a] (l:π a) : Ο(l ++ β‘ = l) β
begin
assume a;
induction
// case l = β‘
{ reflexivity; }
// case l = x βΈ¬ l'
{ assume x l' h; simplify; rewrite h; reflexivity; }
end;
rule $m ++ β‘ βͺ $m;
opaque symbol size_cat [a] (l m : π a) : Ο(size (l ++ m) = size l + size m) β
begin
assume a;
induction
// case l = β‘
{ reflexivity; }
// case l = xβΈ¬l'
{ assume x l' h m; simplify; rewrite h; reflexivity; }
end;
rule size ($l ++ $m) βͺ size $l + size $m;
opaque symbol catA [a] (l m n : π a) : Ο((l ++ m) ++ n = l ++ (m ++ n)) β
begin
assume a;
induction
// case l = β‘
{ reflexivity; }
// case l = xβΈ¬l'
{ assume x l' h m n; simplify; rewrite h; reflexivity; }
end;
rule ($l ++ $m) ++ $n βͺ $l ++ ($m ++ $n);
opaque symbol cat_nilp [a] (l1 l2 : π a) :
Ο (nilp (l1 ++ l2) = (nilp l1 and nilp l2)) β
begin
assume a; induction
{ reflexivity; }
{ assume e l h l2; simplify; reflexivity; }
end;
// list reversal
symbol rev [a] : π a β π a;
rule rev β‘ βͺ β‘
with rev ($x βΈ¬ $l) βͺ rev $l ++ ($x βΈ¬ β‘);
opaque symbol rev_concat [a] (l m : π a) : Ο(rev (l ++ m) = rev m ++ rev l) β
begin
assume a;
induction
// case l = β‘
{ simplify; reflexivity; }
// case l = βΈ¬
{ assume x l h m; simplify; rewrite h; reflexivity; }
end;
rule rev ($l ++ $m) βͺ rev $m ++ rev $l;
opaque symbol rev_idem [a] (l :π a) : Ο(rev (rev l) = l) β
begin
assume a; induction
{ reflexivity }
{ assume x l h; simplify; rewrite h; reflexivity }
end;
opaque symbol size_rev [a] (l : π a) : Ο(size (rev l) = size l) β
begin
assume a;
induction
// case l = β‘
{ simplify; reflexivity; }
// case l = βΈ¬
{ assume x l h; simplify; rewrite h; reflexivity; }
end;
// rcons
symbol rcons [a] : π a β Ο a β π a;
rule rcons β‘ $x βͺ $x βΈ¬ β‘
with rcons ($e βΈ¬ $l) $x βͺ $e βΈ¬ (rcons $l $x);
opaque symbol cats1 [a] (l:π a) (z:Ο a) : Ο (l ++ (z βΈ¬ β‘) = rcons l z) β
begin
assume a; induction
{ reflexivity; }
{ assume e l h z; simplify; rewrite h z; reflexivity; }
end;
opaque symbol rcons_cons [a] (x:Ο a) (s:π a) (z:Ο a) :
Ο (rcons (x βΈ¬ s) z = x βΈ¬ rcons s z) β
begin
reflexivity;
end;
// Arr
symbol Arr : β β Set β Set β TYPE;
rule Arr 0 _ $b βͺ Ο $b
with Arr ($n +1) $a $b βͺ Ο $a β Arr $n $a $b;
// seqn
symbol seqn_acc [a] n : π a β Arr n a (list a);
rule seqn_acc 0 $l βͺ rev $l
with seqn_acc ($n +1) $l $x βͺ seqn_acc $n ($x βΈ¬ $l);
symbol seqn [a] n β @seqn_acc a n β‘;
assert a (x y : Ο a) β’ seqn 2 x y β‘ x βΈ¬ y βΈ¬ β‘;
// iota
symbol iota : β β β β π nat;
rule iota _ 0 βͺ β‘
with iota $n ($k +1) βͺ $n βΈ¬ iota ($n +1) $k;
assert β’ iota 1 2 β‘ 1 βΈ¬ 2 βΈ¬ β‘;
// indexes
symbol indexes [a] : π a β π nat;
rule indexes $l βͺ iota 0 (size $l);
assert x β’ indexes (x βΈ¬ x βΈ¬ x βΈ¬ x βΈ¬ β‘) β‘ 0 βΈ¬ 1 βΈ¬ 2 βΈ¬ 3 βΈ¬ β‘;
// last
symbol last [a] : Ο a β π a β Ο a;
rule last $x β‘ βͺ $x
with last _ ($e βΈ¬ $l) βͺ last $e $l;
assert β’ last 4 (3 βΈ¬ 2 βΈ¬ 1 βΈ¬ β‘) β‘ 1;
assert β’ last 4 β‘ β‘ 4;
// belast
symbol belast [a] : Ο a β π a β π a;
rule belast _ β‘ βͺ β‘
with belast $x ($e βΈ¬ $l) βͺ $x βΈ¬ belast $e $l;
assert β’ belast 4 (3 βΈ¬ 2 βΈ¬ 1 βΈ¬ β‘) β‘ 4 βΈ¬ 3 βΈ¬ 2 βΈ¬ β‘;
// nth
symbol nth [a] : Ο a β π a β β β Ο a;
rule nth $x β‘ _ βͺ $x
with nth _ ($e βΈ¬ _) 0 βͺ $e
with nth $x (_ βΈ¬ $l) ($n +1) βͺ nth $x $l $n;
assert β’ nth 4 (3 βΈ¬ 2 βΈ¬ 1 βΈ¬ β‘) 0 β‘ 3;
assert β’ nth 4 (3 βΈ¬ 2 βΈ¬ 1 βΈ¬ β‘) 2 β‘ 1;
assert β’ nth 4 (3 βΈ¬ 2 βΈ¬ 1 βΈ¬ β‘) 3 β‘ 4;
assert β’ nth 4 (3 βΈ¬ 2 βΈ¬ 1 βΈ¬ β‘) 42 β‘ 4;
// set_nth
symbol set_nth [a] : Ο a β π a β β β Ο a β π a;
rule set_nth _ β‘ 0 $y βͺ $y βΈ¬ β‘
with set_nth _ (_ βΈ¬ $l) 0 $y βͺ $y βΈ¬ $l
with set_nth $x β‘ ($i +1) $y βͺ $x βΈ¬ set_nth $x β‘ $i $y
with set_nth $x ($e βΈ¬ $l) ($i +1) $y βͺ $e βΈ¬ set_nth $x $l $i $y;
assert β’ set_nth 42 (3 βΈ¬ 2 βΈ¬ 1 βΈ¬ β‘) 1 6 β‘ 3 βΈ¬ 6 βΈ¬ 1 βΈ¬ β‘;
assert β’ set_nth 42 (3 βΈ¬ 2 βΈ¬ 1 βΈ¬ β‘) 2 6 β‘ 3 βΈ¬ 2 βΈ¬ 6 βΈ¬ β‘;
assert β’ set_nth 42 (3 βΈ¬ 2 βΈ¬ 1 βΈ¬ β‘) 5 6 β‘ 3 βΈ¬ 2 βΈ¬ 1 βΈ¬ 42 βΈ¬ 42 βΈ¬ 6 βΈ¬ β‘;
// incr_nth
symbol incr_nth : π nat β Ο nat β π nat;
rule incr_nth β‘ 0 βͺ 1 βΈ¬ β‘
with incr_nth β‘ ($i +1) βͺ 0 βΈ¬ incr_nth β‘ $i
with incr_nth ($n βΈ¬ $l) 0 βͺ $n +1 βΈ¬ $l
with incr_nth ($n βΈ¬ $l) ($i +1) βͺ $n βΈ¬ incr_nth $l $i;
assert β’ incr_nth (3 βΈ¬ 2 βΈ¬ 1 βΈ¬ β‘) 1 β‘ 3 βΈ¬ 3 βΈ¬ 1 βΈ¬ β‘;
assert β’ incr_nth (3 βΈ¬ 2 βΈ¬ 1 βΈ¬ β‘) 2 β‘ 3 βΈ¬ 2 βΈ¬ 2 βΈ¬ β‘;
assert β’ incr_nth (3 βΈ¬ 2 βΈ¬ 1 βΈ¬ β‘) 5 β‘ 3 βΈ¬ 2 βΈ¬ 1 βΈ¬ 0 βΈ¬ 0 βΈ¬ 1 βΈ¬ β‘;
// zip
symbol zip [a b] : π a β π b β π (a Γ b);
rule zip β‘ β‘ βͺ β‘
with zip β‘ _ βͺ β‘
with zip _ β‘ βͺ β‘
with zip ($x βΈ¬ $l) ($y βΈ¬ $m) βͺ $x & $y βΈ¬ zip $l $m;
symbol unzip1 [a b] : π (a Γ b) β π a;
rule unzip1 β‘ βͺ β‘
with unzip1 ($x & _ βΈ¬ $l) βͺ $x βΈ¬ unzip1 $l;
symbol unzip2 [a b] : π (a Γ b) β π b;
rule unzip2 β‘ βͺ β‘
with unzip2 (_ & $y βΈ¬ $l) βͺ $y βΈ¬ unzip2 $l;
assert β’ unzip1 ((3 & 5) βΈ¬ (6 & 4) βΈ¬ (7 & 2) βΈ¬ (8 & 1) βΈ¬ β‘) β‘ 3 βΈ¬ 6 βΈ¬ 7 βΈ¬ 8 βΈ¬ β‘;
assert β’ unzip2 ((3 & 5) βΈ¬ (6 & 4) βΈ¬ (7 & 2) βΈ¬ (8 & 1) βΈ¬ β‘) β‘ 5 βΈ¬ 4 βΈ¬ 2 βΈ¬ 1 βΈ¬ β‘;
symbol all2 [a b] : (Ο a β Ο b β πΉ) β π a β π b β πΉ;
rule all2 _ β‘ β‘ βͺ true
with all2 $p ($x βΈ¬ $l) ($y βΈ¬ $m) βͺ ($p $x $y) and (all2 $p $l $m);
opaque symbol unzip1_zip [a b] (la:π a) (lb:π b) :
Ο (size la β€ size lb) β Ο (unzip1 (zip la lb) = la) β
begin
assume a b; induction
{ reflexivity; }
{ assume ea la h; induction
{ assume i; apply β₯β i; }
{ assume eb lb i j; apply feq (Ξ» l, ea βΈ¬ l) (h lb j); }
}
end;
opaque symbol unzip2_zip [a b] (la:π a) (lb:π b) :
Ο (size lb β€ size la) β Ο (unzip2 (zip la lb) = lb) β
begin
assume a b; induction
{ assume lb h;
have t:Ο (size lb = 0) { apply β€0 (size lb) h; };
symmetry; apply size0nil lb t; }
{ assume ea la h; induction
{ reflexivity; }
{ assume eb lb i j; apply feq (Ξ» l, eb βΈ¬ l) (h lb j); }
}
end;
opaque symbol size1_zip [a b] (la:π a) (lb:π b) :
Ο (size la β€ size lb) β Ο (size (zip la lb) = size la) β
begin
assume a b; induction
{ reflexivity; }
{ assume ea la h; induction
{ assume i; apply β₯β i; }
{ assume eb lb i j; apply feq (+1) (h lb j); }
}
end;
opaque symbol size2_zip [a b] (la:π a) (lb:π b) :
Ο (size lb β€ size la) β Ο (size (zip la lb) = size lb) β
begin
assume a b; induction
{ assume lb h; symmetry; apply β€0 (size lb) h; }
{ assume ea la h; induction
{ reflexivity; }
{ assume eb lb i j; apply feq (+1) (h lb j); }
}
end;
opaque symbol size_zip [a b] (la:π a) (lb:π b) :
Ο (size (zip la lb) = min (size la) (size lb)) β
begin
assume a b; induction
{ reflexivity }
{ assume ea la h; induction
{ reflexivity; }
{ assume eb lb i; simplify; apply feq (+1) (h lb); }
}
end;
// double induction
opaque symbol seq_ind2 [a b] (p:π a β π b β Prop) :
Ο (p β‘ β‘) β
(Ξ la lb ea eb, Ο(size la = size lb) β Ο(p la lb) β Ο(p (ea βΈ¬ la) (eb βΈ¬ lb)))
β Ξ la lb, Ο(size la = size lb) β Ο(p la lb) β
begin
assume a b p p0 pH; induction
{ induction
{ assume h; apply p0; }
{ assume eb lb h1 h2; apply β₯β; apply sβ 0 [size lb]; symmetry; apply h2 }
}
{ assume ea la h; induction
{ assume i; apply β₯β (sβ 0 i); }
{ assume eb lb i j;
have t:Ο (size la = size lb) { apply +1_inj j; };
apply pH la lb ea eb t (h lb t); }
}
end;
opaque symbol zip_cat [a b] (la sa:π a) (lb sb:π b) :
Ο (size la = size lb) β Ο (zip (la ++ sa) (lb ++ sb) = zip la lb ++ zip sa sb) β
begin
assume a b la sa lb sb h;
apply @seq_ind2 a b (Ξ» l1 l2, (zip (l1 ++ sa) (l2 ++ sb) = zip l1 l2 ++ zip sa sb)) _ _ la lb h {
reflexivity;
} {
assume l1 l2 e1 e2 h1 h2; simplify; apply feq (Ξ» l, e1 & e2 βΈ¬ l) h2;
};
end;
opaque symbol nth_zip [a b] (x:Ο a) (y:Ο b) la lb i: Ο(size la = size lb) β
Ο(nth (x & y) (zip la lb) i = nth x la i & nth y lb i) β
begin
assume a b x y; induction
{ assume lb i h;
have t: Ο (lb = β‘) { apply size0nil lb; symmetry; apply h; };
rewrite t; reflexivity; }
{ assume ea la h; induction
{ assume i j; apply β₯β (sβ 0 j); }
{ assume eb lb k; induction
{ assume m; reflexivity; }
{ assume i m n; apply h lb i _; apply +1_inj n; }
}
}
end;
opaque symbol rev_zip [a b] (la:π a) (lb:π b) :
Ο (size la = size lb) β Ο (rev (zip la lb) = zip (rev la) (rev lb)) β
begin
assume a b la lb h;
apply seq_ind2 (Ξ» l1 l2, rev (zip l1 l2) = zip (rev l1) (rev l2)) _ _ la lb h {
reflexivity;
} {
assume l1 l2 e1 e2 i j; simplify;
have i': Ο(size(rev l1) = size(rev l2))
{ rewrite @size_rev; rewrite @size_rev; apply i};
rewrite zip_cat (rev l1) (e1 βΈ¬ β‘) (rev l2) (e2 βΈ¬ β‘) i';
rewrite left j; reflexivity;
};
end;
// drop
symbol drop [a] : β β π a β π a;
rule drop 0 $l βͺ $l
with drop _ β‘ βͺ β‘
with drop ($n +1) (_ βΈ¬ $l) βͺ drop $n $l;
assert β’ drop 3 (7 βΈ¬ 2 βΈ¬ 3 βΈ¬ 1 βΈ¬ 41 βΈ¬ β‘) β‘ 1 βΈ¬ 41 βΈ¬ β‘;
assert β’ drop 10 (7 βΈ¬ 2 βΈ¬ 3 βΈ¬ 1 βΈ¬ 41 βΈ¬ β‘) β‘ β‘;
opaque symbol drop0 [a] (l:π a) : Ο (drop 0 l = l) β
begin
reflexivity;
end;
opaque symbol drop_oversize [a] n (l:π a) : Ο (size l β€ n) β Ο (drop n l = β‘) β
begin
assume a; induction
{ assume l h;
have t:Ο (size l = 0) { apply β€0 (size l) h;};
simplify; apply size0nil l t;
}
{
assume n h; induction
{ reflexivity; }
{ assume e l i; simplify; refine h l; }
}
end;
opaque symbol drop_size [a] (l:π a) : Ο (drop (size l) l = β‘) β
begin
assume a; induction
{ reflexivity; }
{ assume e l h; simplify; apply h; }
end;
opaque symbol drop_cons [a] (e:Ο a) l n : Ο (drop (n +1) (e βΈ¬ l) = drop n l) β
begin
assume a e l n; reflexivity;
end;
opaque symbol size_drop [a] (l:π a) n : Ο (size (drop n l) = size l - n) β
begin
assume a; induction
{ reflexivity; }
{ assume e l h; simplify; induction
{ reflexivity; }
{ assume n i; simplify; apply h n; }
}
end;
opaque symbol size_cons [a] (e:Ο a) n l : Ο (size l = n β size (e βΈ¬ l) = n +1) β
begin
assume a e n l; apply β§α΅’ {
generalize n; induction
{ assume l h; simplify; rewrite h; reflexivity; }
{ assume n h l i; simplify; apply feq (+1) i;}
} {
generalize n; induction
{ assume l i; apply +1_inj i;}
{ assume n h l i; apply +1_inj i; }
};
end;
opaque symbol drop_size_cat [a] n (l1 l2:π a) :
Ο (size l1 = n) β Ο (drop n (l1 ++ l2) = l2) β
begin
assume a; induction
{ assume l1 l2 h; simplify;
have t:Ο (l1 = β‘) { apply size0nil l1 h }; rewrite t; reflexivity; }
{ assume n h; induction
{ assume l2 i; apply β₯β; apply sβ 0 [n]; symmetry; apply i; }
{ assume e l1 i l2 j; apply h l1 l2; apply +1_inj j; }
}
end;
opaque symbol drop_drop [a] (l:π a) n1 n2 :
Ο (drop n1 (drop n2 l) = drop (n1 + n2) l) β
begin
assume a; induction
{ reflexivity; }
{ assume e l h n1; induction
{ reflexivity; }
{ assume n2 i; simplify; apply h n1 n2; }
}
end;
// take
symbol take [a] : β β π a β π a;
rule take 0 _ βͺ β‘
with take _ β‘ βͺ β‘
with take ($n +1) ($x βΈ¬ $l) βͺ $x βΈ¬ (take $n $l);
assert β’ take 3 (7 βΈ¬ 2 βΈ¬ 3 βΈ¬ 1 βΈ¬ 41 βΈ¬ β‘) β‘ 7 βΈ¬ 2 βΈ¬ 3 βΈ¬ β‘;
assert β’ take 10 (7 βΈ¬ 2 βΈ¬ 3 βΈ¬ 1 βΈ¬ 41 βΈ¬ β‘) β‘ 7 βΈ¬ 2 βΈ¬ 3 βΈ¬ 1 βΈ¬ 41 βΈ¬ β‘;
opaque symbol take0 [a] (l: π a) : Ο (take 0 l = β‘) β
begin
reflexivity;
end;
opaque symbol take_cons [a] n (x:Ο a) l :
Ο (take (n +1) (x βΈ¬ l) = (x βΈ¬ take n l)) β
begin
assume a l; reflexivity;
end;
opaque symbol take_size [a] (l: π a) : Ο (take (size l) l = l) β
begin
assume a; induction
{ reflexivity; }
{ assume e l h; simplify; rewrite h; reflexivity; }
end;
opaque symbol take_oversize [a] n (l:π a) : Ο (size l β€ n) β Ο (take n l = l) β
begin
assume a; induction
{ assume l h; simplify; symmetry; apply size0nil l; apply β€0 (size l) h; }
{ assume n h; induction
{ reflexivity; }
{ assume e l i; simplify; assume j; rewrite h l j; reflexivity; }
}
end;
opaque symbol cat_take_drop [a] n (l:π a) : Ο (take n l ++ drop n l = l) β
begin
assume a; induction
{ reflexivity; }
{ assume n h; induction
{ reflexivity; }
{ assume e l i; simplify; rewrite h l; reflexivity; }
}
end;
opaque symbol size_takel [a] n (l:π a) :
Ο (n β€ size l) β Ο (size (take n l) = n) β
begin
assume a; induction
{ reflexivity; }
{ assume n h; induction
{ simplify; assume i; apply β₯β i; }
{ assume e l i; simplify; assume j; apply feq (+1); apply h l j; }
}
end;
opaque symbol size_take [a] n (l:π a) :
Ο (size (take n l) = if (n < size l) n (size l)) β
begin
abort;
opaque symbol size_take_min [a] n (l:π a) :
Ο (size (take n l) = min n (size l)) β
begin
assume a; induction
{ reflexivity; }
{ assume n h; induction
{ reflexivity; }
{ assume e l i; simplify; apply feq (+1) (h l); }
}
end;
opaque symbol take_size_cat [a] n (l1 l2:π a) :
Ο (size l1 = n) β Ο (take n (l1 ++ l2) = l1) β
begin
assume a; induction
{ assume l1 l2 h; simplify; symmetry; apply size0nil l1 h; }
{ assume n h; induction
{ assume l2 i; apply β₯β; apply sβ 0 [n]; symmetry; apply i; }
{ assume e1 l1 i l2 j; simplify; apply feq (Ξ» l, e1 βΈ¬ l);
apply h l1 l2; apply β§ββ (size_cons e1 n l1) j;
}
}
end;
opaque symbol takel_cat [a] (l1 l2:π a) n :
Ο (n β€ size l1) β Ο (take n (l1 ++ l2) = take n l1) β
begin
assume a; induction
{ assume l2 n h; have t:Ο (n = 0) { apply β€0 n h }; rewrite t; reflexivity; }
{ assume e1 l1 h l2; induction
{ reflexivity; }
{ assume n i j; apply feq (Ξ» l:π a, e1 βΈ¬ l); apply h l2 n j; }
}
end;
opaque symbol take_drop [a] m n (l:π a):
Ο (take m (drop n l) = drop n (take (m + n) l)) β
begin
assume a; induction
{ induction
{ reflexivity; }
{ assume m h; induction
{ reflexivity; }
{ assume e l i; simplify; rewrite left .[m in take m l] add0n m;
rewrite left h l; reflexivity; }
}
}
{ assume n h; induction
{ reflexivity; }
{ assume m i; induction
{ reflexivity; }
{ assume e l j; simplify; apply i l; }
}
}
end;
opaque symbol takeD [a] m n (l:π a) :
Ο (take (m + n) l = take m l ++ take n (drop m l)) β
begin
assume a; induction
{ reflexivity; }
{ assume m h; induction
{ reflexivity; }
{ assume n i; induction
{ reflexivity; }
{ assume e l j; simplify; apply feq (Ξ» l:π a, e βΈ¬ l);
rewrite left addnS; apply h (n +1) l; }
}
}
end;
opaque symbol takeC [a] (l:π a) i j:
Ο (take i (take j l) = take j (take i l)) β
begin
assume a; induction
{ reflexivity; }
{ assume e l h; induction
{ reflexivity; }
{ assume i h2; induction
{ reflexivity; }
{ assume j h3; simplify; rewrite h i j; reflexivity; }
}
}
end;
// rot
symbol rot [a] n (l:π a) β drop n l ++ take n l;
assert β’ rot 2 (7 βΈ¬ 2 βΈ¬ 3 βΈ¬ 1 βΈ¬ 41 βΈ¬ β‘) β‘ 3 βΈ¬ 1 βΈ¬ 41 βΈ¬ 7 βΈ¬ 2 βΈ¬ β‘;
opaque symbol Ο_rot [a] n (l:π a) : Ο (drop n l ++ take n l = rot n l) β
begin
reflexivity;
end;
opaque symbol rot0 [a] (l:π a) : Ο (rot 0 l = l) β
begin
reflexivity;
end;
opaque symbol size_rot [a] (l:π a) n0 : Ο (size (rot n0 l) = size l) β
begin
assume a l n0; simplify; rewrite addnC;
rewrite left @size_cat a (take n0 l) (drop n0 l);
rewrite cat_take_drop n0 l; reflexivity;
end;
opaque symbol rot_oversize [a] n (l:π a) : Ο (size l β€ n) β Ο (rot n l = l) β
begin
assume a n l h; simplify; rewrite drop_oversize n l h;
rewrite take_oversize n l h; reflexivity;
end;
opaque symbol rot_size [a] (l:π a) : Ο (rot (size l) l = l) β
begin
assume a l; simplify; rewrite take_size l; rewrite @drop_size a; reflexivity;
end;
opaque symbol rot_size_cat [a] (l1 l2:π a) :
Ο (rot (size l1) (l1 ++ l2) = l2 ++ l1) β
begin
assume a l1 l2; simplify;