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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
|
(************************************************************************)
(* * 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) *)
(************************************************************************)
Require Import PeanoNat Bool.
Set Implicit Arguments.
(* Set Universe Polymorphism. *)
(******************************************************************)
(** * Basics: definition of polymorphic lists and some operations *)
(******************************************************************)
(** The definition of [list] is now in [Init/Datatypes],
as well as the definitions of [length] and [app] *)
Open Scope list_scope.
(** Standard notations for lists.
In a special module to avoid conflicts. *)
Module ListNotations.
Notation "[ ]" := nil (format "[ ]") : list_scope.
Notation "[ x ]" := (cons x nil) : list_scope.
Notation "[ x ; y ; .. ; z ]" := (cons x (cons y .. (cons z nil) ..)) : list_scope.
End ListNotations.
Import ListNotations.
Section Lists.
Variable A : Type.
(** Head and tail *)
Definition hd (default:A) (l:list A) :=
match l with
| [] => default
| x :: _ => x
end.
Definition hd_error (l:list A) :=
match l with
| [] => None
| x :: _ => Some x
end.
Definition tl (l:list A) :=
match l with
| [] => nil
| a :: m => m
end.
(** The [In] predicate *)
Fixpoint In (a:A) (l:list A) : Prop :=
match l with
| [] => False
| b :: m => b = a \/ In a m
end.
End Lists.
Section Facts.
Variable A : Type.
(** *** Generic facts *)
(** Discrimination *)
Theorem nil_cons (x:A) (l:list A) : [] <> x :: l.
Proof.
discriminate.
Qed.
(** Destruction *)
Theorem destruct_list (l : list A) : {x:A & {tl:list A | l = x::tl}}+{l = []}.
Proof.
induction l as [|a tail].
right; reflexivity.
left; exists a, tail; reflexivity.
Qed.
Lemma hd_error_tl_repr l (a:A) r :
hd_error l = Some a /\ tl l = r <-> l = a :: r.
Proof. destruct l as [|x xs].
- unfold hd_error, tl; split; firstorder discriminate.
- intros. simpl. split.
* intros (H1, H2). inversion H1. rewrite H2. reflexivity.
* inversion 1. subst. auto.
Qed.
Lemma hd_error_some_nil l (a:A) : hd_error l = Some a -> l <> nil.
Proof. unfold hd_error. destruct l; now discriminate. Qed.
Theorem length_zero_iff_nil (l : list A):
length l = 0 <-> l=[].
Proof.
split; [now destruct l | now intros ->].
Qed.
(** *** Head and tail *)
Theorem hd_error_nil : hd_error (@nil A) = None.
Proof.
simpl; reflexivity.
Qed.
Theorem hd_error_cons (l : list A) (x : A) : hd_error (x::l) = Some x.
Proof.
simpl; reflexivity.
Qed.
(**************************)
(** *** Facts about [app] *)
(**************************)
(** Discrimination *)
Theorem app_cons_not_nil (x y:list A) (a:A) : [] <> x ++ a :: y.
Proof.
unfold not.
destruct x; simpl; intros H.
discriminate H.
discriminate H.
Qed.
(** Concat with [nil] *)
Theorem app_nil_l (l:list A) : [] ++ l = l.
Proof.
reflexivity.
Qed.
Theorem app_nil_r (l:list A) : l ++ [] = l.
Proof.
induction l; simpl; f_equal; auto.
Qed.
(* begin hide *)
(* Deprecated *)
Theorem app_nil_end (l:list A) : l = l ++ [].
Proof. symmetry; apply app_nil_r. Qed.
(* end hide *)
(** [app] is associative *)
Theorem app_assoc (l m n:list A) : l ++ m ++ n = (l ++ m) ++ n.
Proof.
induction l; simpl; f_equal; auto.
Qed.
(* begin hide *)
(* Deprecated *)
Theorem app_assoc_reverse (l m n:list A) : (l ++ m) ++ n = l ++ m ++ n.
Proof.
auto using app_assoc.
Qed.
#[local]
Hint Resolve app_assoc_reverse : core.
(* end hide *)
(** [app] commutes with [cons] *)
Theorem app_comm_cons (x y:list A) (a:A) : a :: (x ++ y) = (a :: x) ++ y.
Proof.
auto.
Qed.
(** Facts deduced from the result of a concatenation *)
Theorem app_eq_nil (l l':list A) : l ++ l' = [] -> l = [] /\ l' = [].
Proof.
destruct l as [| x l]; destruct l' as [| y l']; simpl; auto.
intro; discriminate.
intros H; discriminate H.
Qed.
Theorem app_eq_unit (x y:list A) (a:A) :
x ++ y = [a] -> x = [] /\ y = [a] \/ x = [a] /\ y = [].
Proof.
destruct x as [|a' l]; [ destruct y as [|a' l] | destruct y as [| a0 l0] ];
simpl.
intros H; discriminate H.
left; split; auto.
intro H; right; split; auto.
generalize H.
generalize (app_nil_r l); intros E.
rewrite -> E; auto.
intros H.
injection H as [= H H0].
assert ([] = l ++ a0 :: l0) as H1 by auto.
apply app_cons_not_nil in H1 as [].
Qed.
Lemma elt_eq_unit l1 l2 (a b : A) :
l1 ++ a :: l2 = [b] -> a = b /\ l1 = [] /\ l2 = [].
Proof.
intros Heq.
apply app_eq_unit in Heq.
now destruct Heq as [[Heq1 Heq2]|[Heq1 Heq2]]; inversion_clear Heq2.
Qed.
Lemma app_inj_tail_iff :
forall (x y:list A) (a b:A), x ++ [a] = y ++ [b] <-> x = y /\ a = b.
Proof.
intro x; induction x as [| x l IHl]; intro y;
[ destruct y as [| a l] | destruct y as [| a l0] ];
simpl; auto.
- intros a b. split.
+ intros [= ]. auto.
+ intros [H0 H1]. subst. auto.
- intros a0 b. split.
+ intros [= H1 H0]. apply app_cons_not_nil in H0 as [].
+ intros [H0 H1]. inversion H0.
- intros a b. split.
+ intros [= H1 H0]. assert ([] = l ++ [a]) as H by auto. apply app_cons_not_nil in H as [].
+ intros [H0 H1]. inversion H0.
- intros a0 b. split.
+ intros [= <- H0]. specialize (IHl l0 a0 b). apply IHl in H0. destruct H0. subst. split; auto.
+ intros [H0 H1]. inversion H0. subst. auto.
Qed.
Lemma app_inj_tail :
forall (x y:list A) (a b:A), x ++ [a] = y ++ [b] -> x = y /\ a = b.
Proof.
apply app_inj_tail_iff.
Qed.
(** Compatibility with other operations *)
Lemma app_length : forall l l' : list A, length (l++l') = length l + length l'.
Proof.
intro l; induction l; simpl; auto.
Qed.
Lemma last_length : forall (l : list A) a, length (l ++ a :: nil) = S (length l).
Proof.
intros ; rewrite app_length ; simpl.
rewrite <- plus_n_Sm, plus_n_O; reflexivity.
Qed.
Lemma app_inv_head_iff:
forall l l1 l2 : list A, l ++ l1 = l ++ l2 <-> l1 = l2.
Proof.
intro l; induction l as [|? l IHl]; split; intros H; simpl; auto.
- apply IHl. inversion H. auto.
- subst. auto.
Qed.
Lemma app_inv_head:
forall l l1 l2 : list A, l ++ l1 = l ++ l2 -> l1 = l2.
Proof.
apply app_inv_head_iff.
Qed.
Lemma app_inv_tail:
forall l l1 l2 : list A, l1 ++ l = l2 ++ l -> l1 = l2.
Proof.
intros l l1 l2; revert l1 l2 l.
intro l1; induction l1 as [ | x1 l1]; intro l2; destruct l2 as [ | x2 l2].
- now intros.
- intros l Hl. apply (f_equal (@length A)) in Hl.
now rewrite ?app_length, Nat.add_cancel_r in Hl.
- intros l Hl. apply (f_equal (@length A)) in Hl.
now rewrite ?app_length, Nat.add_cancel_r in Hl.
- intros l [=H1 H2 %IHl1]. now subst.
Qed.
Lemma app_inv_tail_iff:
forall l l1 l2 : list A, l1 ++ l = l2 ++ l <-> l1 = l2.
Proof.
split; [apply app_inv_tail | now intros ->].
Qed.
(************************)
(** *** Facts about [In] *)
(************************)
(** Characterization of [In] *)
Theorem in_eq : forall (a:A) (l:list A), In a (a :: l).
Proof.
simpl; auto.
Qed.
Theorem in_cons : forall (a b:A) (l:list A), In b l -> In b (a :: l).
Proof.
simpl; auto.
Qed.
Theorem not_in_cons (x a : A) (l : list A):
~ In x (a::l) <-> x<>a /\ ~ In x l.
Proof.
simpl. intuition.
Qed.
Theorem in_nil : forall a:A, ~ In a [].
Proof.
unfold not; intros a H; inversion_clear H.
Qed.
Lemma in_app_or : forall (l m:list A) (a:A), In a (l ++ m) -> In a l \/ In a m.
Proof.
intros l m a.
elim l; simpl; auto.
intros a0 y H H0.
now_show ((a0 = a \/ In a y) \/ In a m).
elim H0; auto.
intro H1.
now_show ((a0 = a \/ In a y) \/ In a m).
elim (H H1); auto.
Qed.
Lemma in_or_app : forall (l m:list A) (a:A), In a l \/ In a m -> In a (l ++ m).
Proof.
intros l m a.
elim l; simpl; intro H.
now_show (In a m).
elim H; auto; intro H0.
now_show (In a m).
elim H0. (* subProof completed *)
intros y H0 H1.
now_show (H = a \/ In a (y ++ m)).
elim H1; auto 4.
intro H2.
now_show (H = a \/ In a (y ++ m)).
elim H2; auto.
Qed.
Lemma in_app_iff : forall l l' (a:A), In a (l++l') <-> In a l \/ In a l'.
Proof.
split; auto using in_app_or, in_or_app.
Qed.
Theorem in_split : forall x (l:list A), In x l -> exists l1 l2, l = l1++x::l2.
Proof.
intros x l; induction l as [|a l IHl]; simpl; [destruct 1|destruct 1 as [?|H]].
subst a; auto.
exists [], l; auto.
destruct (IHl H) as (l1,(l2,H0)).
exists (a::l1), l2; simpl. apply f_equal. auto.
Qed.
Lemma in_elt : forall (x:A) l1 l2, In x (l1 ++ x :: l2).
Proof.
intros.
apply in_or_app.
right; left; reflexivity.
Qed.
Lemma in_elt_inv : forall (x y : A) l1 l2,
In x (l1 ++ y :: l2) -> x = y \/ In x (l1 ++ l2).
Proof.
intros x y l1 l2 Hin.
apply in_app_or in Hin.
destruct Hin as [Hin|[Hin|Hin]]; [right|left|right]; try apply in_or_app; intuition.
Qed.
(** Inversion *)
Lemma in_inv : forall (a b:A) (l:list A), In b (a :: l) -> a = b \/ In b l.
Proof. easy. Qed.
(** Decidability of [In] *)
Theorem in_dec :
(forall x y:A, {x = y} + {x <> y}) ->
forall (a:A) (l:list A), {In a l} + {~ In a l}.
Proof.
intros H a l; induction l as [| a0 l IHl].
right; apply in_nil.
destruct (H a0 a); simpl; auto.
destruct IHl; simpl; auto.
right; unfold not; intros [Hc1| Hc2]; auto.
Defined.
End Facts.
#[global]
Hint Resolve app_assoc app_assoc_reverse: datatypes.
#[global]
Hint Resolve app_comm_cons app_cons_not_nil: datatypes.
#[global]
Hint Immediate app_eq_nil: datatypes.
#[global]
Hint Resolve app_eq_unit app_inj_tail: datatypes.
#[global]
Hint Resolve in_eq in_cons in_inv in_nil in_app_or in_or_app: datatypes.
(*******************************************)
(** * Operations on the elements of a list *)
(*******************************************)
Section Elts.
Variable A : Type.
(*****************************)
(** ** Nth element of a list *)
(*****************************)
Fixpoint nth (n:nat) (l:list A) (default:A) {struct l} : A :=
match n, l with
| O, x :: l' => x
| O, other => default
| S m, [] => default
| S m, x :: t => nth m t default
end.
Fixpoint nth_ok (n:nat) (l:list A) (default:A) {struct l} : bool :=
match n, l with
| O, x :: l' => true
| O, other => false
| S m, [] => false
| S m, x :: t => nth_ok m t default
end.
Lemma nth_in_or_default :
forall (n:nat) (l:list A) (d:A), {In (nth n l d) l} + {nth n l d = d}.
Proof.
intros n l d; revert n; induction l as [|? ? IHl].
- intro n; right; destruct n; trivial.
- intros [|n]; simpl.
* left; auto.
* destruct (IHl n); auto.
Qed.
Lemma nth_S_cons :
forall (n:nat) (l:list A) (d a:A),
In (nth n l d) l -> In (nth (S n) (a :: l) d) (a :: l).
Proof.
simpl; auto.
Qed.
Fixpoint nth_error (l:list A) (n:nat) {struct n} : option A :=
match n, l with
| O, x :: _ => Some x
| S n, _ :: l => nth_error l n
| _, _ => None
end.
Definition nth_default (default:A) (l:list A) (n:nat) : A :=
match nth_error l n with
| Some x => x
| None => default
end.
Lemma nth_default_eq :
forall n l (d:A), nth_default d l n = nth n l d.
Proof.
unfold nth_default; intro n; induction n; intros [ | ] ?; simpl; auto.
Qed.
(** Results about [nth] *)
Lemma nth_In :
forall (n:nat) (l:list A) (d:A), n < length l -> In (nth n l d) l.
Proof.
unfold lt; intro n; induction n as [| n hn]; simpl; intro l.
- destruct l; simpl; [ inversion 2 | auto ].
- destruct l; simpl.
* inversion 2.
* intros d ie; right; apply hn. now apply Nat.succ_le_mono.
Qed.
Lemma In_nth l x d : In x l ->
exists n, n < length l /\ nth n l d = x.
Proof.
induction l as [|a l IH].
- easy.
- intros [H|H].
* subst; exists 0; simpl; auto using Nat.lt_0_succ.
* destruct (IH H) as (n & Hn & Hn').
apply Nat.succ_lt_mono in Hn. now exists (S n).
Qed.
Lemma nth_overflow : forall l n d, length l <= n -> nth n l d = d.
Proof.
intro l; induction l as [|? ? IHl]; intro n; destruct n;
simpl; intros d H; auto.
- inversion H.
- apply IHl. now apply Nat.succ_le_mono.
Qed.
Lemma nth_indep :
forall l n d d', n < length l -> nth n l d = nth n l d'.
Proof.
intro l; induction l.
- inversion 1.
- intros [|n] d d'; [intros; reflexivity|].
intros H. apply IHl. now apply Nat.succ_lt_mono.
Qed.
Lemma app_nth1 :
forall l l' d n, n < length l -> nth n (l++l') d = nth n l d.
Proof.
intro l; induction l.
- inversion 1.
- intros l' d [|n]; simpl; [intros; reflexivity|].
intros H. apply IHl. now apply Nat.succ_lt_mono.
Qed.
Lemma app_nth2 :
forall l l' d n, n >= length l -> nth n (l++l') d = nth (n-length l) l' d.
Proof.
intro l; induction l as [|? ? IHl]; intros l' d [|n]; auto.
- inversion 1.
- intros; simpl; rewrite IHl; [reflexivity|now apply Nat.succ_le_mono].
Qed.
Lemma app_nth2_plus : forall l l' d n,
nth (length l + n) (l ++ l') d = nth n l' d.
Proof.
intros.
rewrite app_nth2, Nat.add_comm, Nat.add_sub; trivial.
now apply Nat.le_add_r.
Qed.
Lemma nth_middle : forall l l' a d,
nth (length l) (l ++ a :: l') d = a.
Proof.
intros.
rewrite plus_n_O at 1.
apply app_nth2_plus.
Qed.
Lemma nth_split n l d : n < length l ->
exists l1, exists l2, l = l1 ++ nth n l d :: l2 /\ length l1 = n.
Proof.
revert l.
induction n as [|n IH]; intros [|a l] H; try easy.
- exists nil; exists l; now simpl.
- destruct (IH l) as (l1 & l2 & Hl & Hl1); [now apply Nat.succ_lt_mono|].
exists (a::l1); exists l2; simpl; split; now f_equal.
Qed.
Lemma nth_ext : forall l l' d d', length l = length l' ->
(forall n, n < length l -> nth n l d = nth n l' d') -> l = l'.
Proof.
intro l; induction l as [|a l IHl];
intros l' d d' Hlen Hnth; destruct l' as [| b l'].
- reflexivity.
- inversion Hlen.
- inversion Hlen.
- change a with (nth 0 (a :: l) d).
change b with (nth 0 (b :: l') d').
rewrite Hnth; f_equal.
+ apply IHl with d d'; [ now inversion Hlen | ].
intros n Hlen'; apply (Hnth (S n)).
now apply (Nat.succ_lt_mono n (length l)).
+ simpl; apply Nat.lt_0_succ.
Qed.
(** Results about [nth_error] *)
Lemma nth_error_In l n x : nth_error l n = Some x -> In x l.
Proof.
revert n. induction l as [|a l IH]; intros [|n]; simpl; try easy.
- injection 1; auto.
- eauto.
Qed.
Lemma In_nth_error l x : In x l -> exists n, nth_error l n = Some x.
Proof.
induction l as [|a l IH].
- easy.
- intros [H|H].
* subst; now exists 0.
* destruct (IH H) as (n,Hn).
now exists (S n).
Qed.
Lemma nth_error_None l n : nth_error l n = None <-> length l <= n.
Proof.
revert n. induction l as [|? ? IHl]; intro n; destruct n; simpl.
- split; auto.
- now split; intros; [apply Nat.le_0_l|].
- now split; [|intros ? %Nat.nle_succ_0].
- now rewrite IHl, Nat.succ_le_mono.
Qed.
Lemma nth_error_Some l n : nth_error l n <> None <-> n < length l.
Proof.
revert n. induction l as [|? ? IHl]; intro n; destruct n; simpl.
- split; [now destruct 1 | inversion 1].
- split; [now destruct 1 | inversion 1].
- now split; intros; [apply Nat.lt_0_succ|].
- now rewrite IHl, Nat.succ_lt_mono.
Qed.
Lemma nth_error_split l n a : nth_error l n = Some a ->
exists l1, exists l2, l = l1 ++ a :: l2 /\ length l1 = n.
Proof.
revert l.
induction n as [|n IH]; intros [|x l] H; simpl in *; try easy.
- exists nil; exists l. now injection H as [= ->].
- destruct (IH _ H) as (l1 & l2 & H1 & H2).
exists (x::l1); exists l2; simpl; split; now f_equal.
Qed.
Lemma nth_error_app1 l l' n : n < length l ->
nth_error (l++l') n = nth_error l n.
Proof.
revert l.
induction n as [|n IHn]; intros [|a l] H; auto; try solve [inversion H].
simpl in *. apply IHn. now apply Nat.succ_lt_mono.
Qed.
Lemma nth_error_app2 l l' n : length l <= n ->
nth_error (l++l') n = nth_error l' (n-length l).
Proof.
revert l.
induction n as [|n IHn]; intros [|a l] H; auto; try solve [inversion H].
simpl in *. apply IHn. now apply Nat.succ_le_mono.
Qed.
(** Results directly relating [nth] and [nth_error] *)
Lemma nth_error_nth : forall (l : list A) (n : nat) (x d : A),
nth_error l n = Some x -> nth n l d = x.
Proof.
intros l n x d H.
apply nth_error_split in H. destruct H as [l1 [l2 [H H']]].
subst. rewrite app_nth2; [|auto].
rewrite Nat.sub_diag. reflexivity.
Qed.
Lemma nth_error_nth' : forall (l : list A) (n : nat) (d : A),
n < length l -> nth_error l n = Some (nth n l d).
Proof.
intros l n d H.
apply (nth_split _ d) in H. destruct H as [l1 [l2 [H H']]].
subst. rewrite H. rewrite nth_error_app2; [|auto].
rewrite app_nth2; [| auto]. repeat (rewrite Nat.sub_diag). reflexivity.
Qed.
(******************************)
(** ** Last element of a list *)
(******************************)
(** [last l d] returns the last element of the list [l],
or the default value [d] if [l] is empty. *)
Fixpoint last (l:list A) (d:A) : A :=
match l with
| [] => d
| [a] => a
| a :: l => last l d
end.
Lemma last_last : forall l a d, last (l ++ [a]) d = a.
Proof.
intro l; induction l as [|? l IHl]; intros; [ reflexivity | ].
simpl; rewrite IHl.
destruct l; reflexivity.
Qed.
(** [removelast l] remove the last element of [l] *)
Fixpoint removelast (l:list A) : list A :=
match l with
| [] => []
| [a] => []
| a :: l => a :: removelast l
end.
Lemma app_removelast_last :
forall l d, l <> [] -> l = removelast l ++ [last l d].
Proof.
intro l; induction l as [|? l IHl].
destruct 1; auto.
intros d _.
destruct l as [|a0 l]; auto.
pattern (a0::l) at 1; rewrite IHl with d; auto; discriminate.
Qed.
Lemma exists_last :
forall l, l <> [] -> { l' : (list A) & { a : A | l = l' ++ [a]}}.
Proof.
intro l; induction l as [|a l IHl].
destruct 1; auto.
intros _.
destruct l.
exists [], a; auto.
destruct IHl as [l' (a',H)]; try discriminate.
rewrite H.
exists (a::l'), a'; auto.
Qed.
Lemma removelast_app :
forall l l', l' <> [] -> removelast (l++l') = l ++ removelast l'.
Proof.
intro l; induction l as [|? l IHl].
simpl; auto.
simpl; intros l' H.
assert (l++l' <> []) as H0.
destruct l.
simpl; auto.
simpl; discriminate.
specialize (IHl l' H).
destruct (l++l'); [elim H0; auto|f_equal; auto].
Qed.
Lemma removelast_last : forall l a, removelast (l ++ [a]) = l.
Proof.
intros.
rewrite removelast_app.
- apply app_nil_r.
- intros Heq; inversion Heq.
Qed.
(*****************)
(** ** Remove *)
(*****************)
Hypothesis eq_dec : forall x y : A, {x = y}+{x <> y}.
Fixpoint remove (x : A) (l : list A) : list A :=
match l with
| [] => []
| y::tl => if (eq_dec x y) then remove x tl else y::(remove x tl)
end.
Lemma remove_cons : forall x l, remove x (x :: l) = remove x l.
Proof.
intros x l; simpl; destruct (eq_dec x x); [ reflexivity | now exfalso ].
Qed.
Lemma remove_app : forall x l1 l2,
remove x (l1 ++ l2) = remove x l1 ++ remove x l2.
Proof.
intros x l1; induction l1 as [|a l1 IHl1]; intros l2; simpl.
- reflexivity.
- destruct (eq_dec x a).
+ apply IHl1.
+ rewrite <- app_comm_cons; f_equal.
apply IHl1.
Qed.
Theorem remove_In : forall (l : list A) (x : A), ~ In x (remove x l).
Proof.
intro l; induction l as [|x l IHl]; auto.
intro y; simpl; destruct (eq_dec y x) as [yeqx | yneqx].
apply IHl.
unfold not; intro HF; simpl in HF; destruct HF; auto.
apply (IHl y); assumption.
Qed.
Lemma notin_remove: forall l x, ~ In x l -> remove x l = l.
Proof.
intros l x; induction l as [|y l]; simpl; intros Hnin.
- reflexivity.
- destruct (eq_dec x y); subst; intuition.
f_equal; assumption.
Qed.
Lemma in_remove: forall l x y, In x (remove y l) -> In x l /\ x <> y.
Proof.
intro l; induction l as [|z l IHl]; intros x y Hin.
- inversion Hin.
- simpl in Hin.
destruct (eq_dec y z) as [Heq|Hneq]; subst; split.
+ right; now apply IHl with z.
+ intros Heq; revert Hin; subst; apply remove_In.
+ inversion Hin; subst; [left; reflexivity|right].
now apply IHl with y.
+ destruct Hin as [Hin|Hin]; subst.
* now intros Heq; apply Hneq.
* intros Heq; revert Hin; subst; apply remove_In.
Qed.
Lemma in_in_remove : forall l x y, x <> y -> In x l -> In x (remove y l).
Proof.
intro l; induction l as [|z l IHl]; simpl; intros x y Hneq Hin.
- apply Hin.
- destruct (eq_dec y z); subst.
+ destruct Hin.
* exfalso; now apply Hneq.
* now apply IHl.
+ simpl; destruct Hin; [now left|right].
now apply IHl.
Qed.
Lemma remove_remove_comm : forall l x y,
remove x (remove y l) = remove y (remove x l).
Proof.
intro l; induction l as [| z l IHl]; simpl; intros x y.
- reflexivity.
- destruct (eq_dec y z); simpl; destruct (eq_dec x z); try rewrite IHl; auto.
+ subst; symmetry; apply remove_cons.
+ simpl; destruct (eq_dec y z); tauto.
Qed.
Lemma remove_remove_eq : forall l x, remove x (remove x l) = remove x l.
Proof. intros l x; now rewrite (notin_remove _ _ (remove_In l x)). Qed.
Lemma remove_length_le : forall l x, length (remove x l) <= length l.
Proof.
intro l; induction l as [|y l IHl]; simpl; intros x; trivial.
destruct (eq_dec x y); simpl.
- rewrite IHl; constructor; reflexivity.
- apply (proj1 (Nat.succ_le_mono _ _) (IHl x)).
Qed.
Lemma remove_length_lt : forall l x, In x l -> length (remove x l) < length l.
Proof.
intro l; induction l as [|y l IHl]; simpl; intros x Hin.
- contradiction Hin.
- destruct Hin as [-> | Hin].
+ destruct (eq_dec x x); intuition.
apply Nat.lt_succ_r, remove_length_le.
+ specialize (IHl _ Hin); destruct (eq_dec x y); simpl; auto.
now apply Nat.succ_lt_mono in IHl.
Qed.
(******************************************)
(** ** Counting occurrences of an element *)
(******************************************)
Fixpoint count_occ (l : list A) (x : A) : nat :=
match l with
| [] => 0
| y :: tl =>
let n := count_occ tl x in
if eq_dec y x then S n else n
end.
(** Compatibility of count_occ with operations on list *)
Theorem count_occ_In l x : In x l <-> count_occ l x > 0.
Proof.
induction l as [|y l IHl]; simpl.
- split; [destruct 1 | apply Nat.nlt_0_r].
- destruct eq_dec as [->|Hneq]; rewrite IHl; intuition.
now apply Nat.lt_0_succ.
Qed.
Theorem count_occ_not_In l x : ~ In x l <-> count_occ l x = 0.
Proof.
rewrite count_occ_In. unfold gt. now rewrite Nat.nlt_ge, Nat.le_0_r.
Qed.
Lemma count_occ_nil x : count_occ [] x = 0.
Proof.
reflexivity.
Qed.
Theorem count_occ_inv_nil l :
(forall x:A, count_occ l x = 0) <-> l = [].
Proof.
split.
- induction l as [|x l]; trivial.
intros H. specialize (H x). simpl in H.
destruct eq_dec as [_|NEQ]; [discriminate|now elim NEQ].
- now intros ->.
Qed.
Lemma count_occ_cons_eq l x y :
x = y -> count_occ (x::l) y = S (count_occ l y).
Proof.
intros H. simpl. now destruct (eq_dec x y).
Qed.
Lemma count_occ_cons_neq l x y :
x <> y -> count_occ (x::l) y = count_occ l y.
Proof.
intros H. simpl. now destruct (eq_dec x y).
Qed.
Lemma count_occ_app l1 l2 x :
count_occ (l1 ++ l2) x = count_occ l1 x + count_occ l2 x.
Proof.
induction l1 as [ | h l1 IHl1]; cbn; auto.
now destruct (eq_dec h x); [ rewrite IHl1 | ].
Qed.
Lemma count_occ_elt_eq l1 l2 x y : x = y ->
count_occ (l1 ++ x :: l2) y = S (count_occ (l1 ++ l2) y).
Proof.
intros ->.
rewrite ? count_occ_app; cbn.
destruct (eq_dec y y) as [Heq | Hneq];
[ apply Nat.add_succ_r | now contradiction Hneq ].
Qed.
Lemma count_occ_elt_neq l1 l2 x y : x <> y ->
count_occ (l1 ++ x :: l2) y = count_occ (l1 ++ l2) y.
Proof.
intros Hxy.
rewrite ? count_occ_app; cbn.
now destruct (eq_dec x y) as [Heq | Hneq]; [ contradiction Hxy | ].
Qed.
Lemma count_occ_bound x l : count_occ l x <= length l.
Proof.
induction l as [|h l]; cbn; auto.
destruct (eq_dec h x); [ apply (proj1 (Nat.succ_le_mono _ _)) | ]; intuition.
Qed.
End Elts.
(*******************************)
(** * Manipulating whole lists *)
(*******************************)
Section ListOps.
Variable A : Type.
(*************************)
(** ** Reverse *)
(*************************)
Fixpoint rev (l:list A) : list A :=
match l with
| [] => []
| x :: l' => rev l' ++ [x]
end.
Lemma rev_app_distr : forall x y:list A, rev (x ++ y) = rev y ++ rev x.
Proof.
intro x; induction x as [| a l IHl].
intro y; destruct y as [| a l].
simpl.
auto.
simpl.
rewrite app_nil_r; auto.
intro y.
simpl.
rewrite (IHl y).
rewrite app_assoc; trivial.
Qed.
Remark rev_unit : forall (l:list A) (a:A), rev (l ++ [a]) = a :: rev l.
Proof.
intros l a.
apply (rev_app_distr l [a]); simpl; auto.
Qed.
Lemma rev_involutive : forall l:list A, rev (rev l) = l.
Proof.
intro l; induction l as [| a l IHl].
simpl; auto.
simpl.
rewrite (rev_unit (rev l) a).
rewrite IHl; auto.
Qed.
Lemma rev_eq_app : forall l l1 l2, rev l = l1 ++ l2 -> l = rev l2 ++ rev l1.
Proof.
intros l l1 l2 Heq.
rewrite <- (rev_involutive l), Heq.
apply rev_app_distr.
Qed.
(** Compatibility with other operations *)
Lemma in_rev : forall l x, In x l <-> In x (rev l).
Proof.
intro l; induction l.
simpl; intuition.
intros.
simpl.
split; intro H; [destruct H|].
subst.
apply in_or_app; right; simpl; auto.
apply in_or_app; left; firstorder.
destruct (in_app_or _ _ _ H); firstorder.
Qed.
Lemma rev_length : forall l, length (rev l) = length l.
Proof.
intro l; induction l as [|? l IHl];simpl; auto.
rewrite app_length.
rewrite IHl.
simpl.
elim (length l); simpl; auto.
Qed.
Lemma rev_nth : forall l d n, n < length l ->
nth n (rev l) d = nth (length l - S n) l d.
Proof.
intro l; induction l as [|a l IHl].
- intros d n H; inversion H.
- intros ? n H. simpl in H.
inversion H.
+ rewrite Nat.sub_diag; simpl.
rewrite <- rev_length.
rewrite app_nth2; auto.
now rewrite Nat.sub_diag.
+ simpl. rewrite app_nth1; [|now rewrite rev_length].
rewrite IHl; [|eassumption].
destruct (length l); [exfalso; now apply (Nat.nlt_0_r n)|].
rewrite (Nat.sub_succ_l n); [reflexivity|].
now apply Nat.succ_le_mono.
Qed.
(** An alternative tail-recursive definition for reverse *)
Fixpoint rev_append (l l': list A) : list A :=
match l with
| [] => l'
| a::l => rev_append l (a::l')
end.
Definition rev' l : list A := rev_append l [].
Lemma rev_append_rev : forall l l', rev_append l l' = rev l ++ l'.
Proof.
intro l; induction l; simpl; auto; intros.
rewrite <- app_assoc; firstorder.
Qed.
Lemma rev_alt : forall l, rev l = rev_append l [].
Proof.
intros; rewrite rev_append_rev.
rewrite app_nil_r; trivial.
Qed.
(*********************************************)
(** Reverse Induction Principle on Lists *)
(*********************************************)
Section Reverse_Induction.
Lemma rev_list_ind : forall P:list A-> Prop,
P [] ->
(forall (a:A) (l:list A), P (rev l) -> P (rev (a :: l))) ->
forall l:list A, P (rev l).
Proof.
intros P ? ? l; induction l; auto.
Qed.
Theorem rev_ind : forall P:list A -> Prop,
P [] ->
(forall (x:A) (l:list A), P l -> P (l ++ [x])) -> forall l:list A, P l.
Proof.
intros P H H0 l.
generalize (rev_involutive l).
intros E; rewrite <- E.
apply (rev_list_ind P).
- auto.
- simpl.
intros a l0 ?.
apply (H0 a (rev l0)).
auto.
Qed.
End Reverse_Induction.
(*************************)
(** ** Concatenation *)
(*************************)
Fixpoint concat (l : list (list A)) : list A :=
match l with
| nil => nil
| cons x l => x ++ concat l
end.
Lemma concat_nil : concat nil = nil.
Proof.
reflexivity.
Qed.
Lemma concat_cons : forall x l, concat (cons x l) = x ++ concat l.
Proof.
reflexivity.
Qed.
Lemma concat_app : forall l1 l2, concat (l1 ++ l2) = concat l1 ++ concat l2.
Proof.
intros l1; induction l1 as [|x l1 IH]; intros l2; simpl.
- reflexivity.
- rewrite IH; apply app_assoc.
Qed.
Lemma in_concat : forall l y,
In y (concat l) <-> exists x, In x l /\ In y x.
Proof.
intro l; induction l as [|a l IHl]; simpl; intro y; split; intros H.
contradiction.
destruct H as (x,(H,_)); contradiction.
destruct (in_app_or _ _ _ H) as [H0|H0].
exists a; auto.
destruct (IHl y) as (H1,_); destruct (H1 H0) as (x,(H2,H3)).
exists x; auto.
apply in_or_app.
destruct H as (x,(H0,H1)); destruct H0.
subst; auto.
right; destruct (IHl y) as (_,H2); apply H2.
exists x; auto.
Qed.
(***********************************)
(** ** Decidable equality on lists *)
(***********************************)
Hypothesis eq_dec : forall (x y : A), {x = y}+{x <> y}.
Lemma list_eq_dec : forall l l':list A, {l = l'} + {l <> l'}.
Proof. decide equality. Defined.
End ListOps.
(***************************************************)
(** * Applying functions to the elements of a list *)
(***************************************************)
(************)
(** ** Map *)
(************)
Section Map.
Variables (A : Type) (B : Type).
Variable f : A -> B.
Fixpoint map (l:list A) : list B :=
match l with
| [] => []
| a :: t => (f a) :: (map t)
end.
Lemma map_cons (x:A)(l:list A) : map (x::l) = (f x) :: (map l).
Proof.
reflexivity.
Qed.
Lemma in_map :
forall (l:list A) (x:A), In x l -> In (f x) (map l).
Proof.
intro l; induction l; firstorder (subst; auto).
Qed.
Lemma in_map_iff : forall l y, In y (map l) <-> exists x, f x = y /\ In x l.
Proof.
intro l; induction l; firstorder (subst; auto).
Qed.
Lemma map_length : forall l, length (map l) = length l.
Proof.
intro l; induction l; simpl; auto.
Qed.
Lemma map_nth : forall l d n,
nth n (map l) (f d) = f (nth n l d).
Proof.
intro l; induction l; simpl map; intros d n; destruct n; firstorder.
Qed.
Lemma nth_error_map : forall n l,
nth_error (map l) n = option_map f (nth_error l n).
Proof.
intro n. induction n as [|n IHn]; intro l.
- now destruct l.
- destruct l as [|? l]; [reflexivity|exact (IHn l)].
Qed.
Lemma map_nth_error : forall n l d,
nth_error l n = Some d -> nth_error (map l) n = Some (f d).
Proof.
intros n l d H. now rewrite nth_error_map, H.
Qed.
Lemma map_app : forall l l',
map (l++l') = (map l)++(map l').
Proof.
intro l; induction l as [|a l IHl]; simpl; auto.
intros; rewrite IHl; auto.
Qed.
Lemma map_last : forall l a,
map (l ++ [a]) = (map l) ++ [f a].
Proof.
intro l; induction l as [|a l IHl]; intros; [ reflexivity | ].
simpl; rewrite IHl; reflexivity.
Qed.
Lemma map_rev : forall l, map (rev l) = rev (map l).
Proof.
intro l; induction l as [|a l IHl]; simpl; auto.
rewrite map_app.
rewrite IHl; auto.
Qed.
Lemma map_eq_nil : forall l, map l = [] -> l = [].
Proof.
intro l; destruct l; simpl; reflexivity || discriminate.
Qed.
Lemma map_eq_cons : forall l l' b,
map l = b :: l' -> exists a tl, l = a :: tl /\ f a = b /\ map tl = l'.
Proof.
intros l l' b Heq.
destruct l as [|a l]; inversion_clear Heq.
exists a, l; repeat split.
Qed.
Lemma map_eq_app : forall l l1 l2,
map l = l1 ++ l2 -> exists l1' l2', l = l1' ++ l2' /\ map l1' = l1 /\ map l2' = l2.
Proof.
intro l; induction l as [|a l IHl]; simpl; intros l1 l2 Heq.
- symmetry in Heq; apply app_eq_nil in Heq; destruct Heq; subst.
exists nil, nil; repeat split.
- destruct l1; simpl in Heq; inversion Heq as [[Heq2 Htl]].
+ exists nil, (a :: l); repeat split.
+ destruct (IHl _ _ Htl) as (l1' & l2' & ? & ? & ?); subst.
exists (a :: l1'), l2'; repeat split.
Qed.
(** [map] and count of occurrences *)
Hypothesis decA: forall x1 x2 : A, {x1 = x2} + {x1 <> x2}.
Hypothesis decB: forall y1 y2 : B, {y1 = y2} + {y1 <> y2}.
Hypothesis Hfinjective: forall x1 x2: A, (f x1) = (f x2) -> x1 = x2.
Theorem count_occ_map x l:
count_occ decA l x = count_occ decB (map l) (f x).
Proof.
revert x. induction l as [| a l' Hrec]; intro x; simpl.
- reflexivity.
- specialize (Hrec x).
destruct (decA a x) as [H1|H1], (decB (f a) (f x)) as [H2|H2].
+ rewrite Hrec. reflexivity.
+ contradiction H2. rewrite H1. reflexivity.
+ specialize (Hfinjective H2). contradiction H1.
+ assumption.
Qed.
(** [flat_map] *)
Definition flat_map (f:A -> list B) :=
fix flat_map (l:list A) : list B :=
match l with
| nil => nil
| cons x t => (f x)++(flat_map t)
end.
Lemma flat_map_app : forall f l1 l2,
flat_map f (l1 ++ l2) = flat_map f l1 ++ flat_map f l2.
Proof.
intros F l1 l2.
induction l1 as [|? ? IHl1]; [ reflexivity | simpl ].
rewrite IHl1, app_assoc; reflexivity.
Qed.
Lemma in_flat_map : forall (f:A->list B)(l:list A)(y:B),
In y (flat_map f l) <-> exists x, In x l /\ In y (f x).
Proof.
clear f Hfinjective.
intros f l; induction l as [|a l IHl]; simpl; intros y; split; intros H.
contradiction.
destruct H as (x,(H,_)); contradiction.
destruct (in_app_or _ _ _ H) as [H0|H0].
exists a; auto.
destruct (IHl y) as (H1,_); destruct (H1 H0) as (x,(H2,H3)).
exists x; auto.
apply in_or_app.
destruct H as (x,(H0,H1)); destruct H0.
subst; auto.
right; destruct (IHl y) as (_,H2); apply H2.
exists x; auto.
Qed.
End Map.
Lemma flat_map_concat_map : forall A B (f : A -> list B) l,
flat_map f l = concat (map f l).
Proof.
intros A B f l; induction l as [|x l IH]; simpl.
- reflexivity.
- rewrite IH; reflexivity.
Qed.
Lemma concat_map : forall A B (f : A -> B) l, map f (concat l) = concat (map (map f) l).
Proof.
intros A B f l; induction l as [|x l IH]; simpl.
- reflexivity.
- rewrite map_app, IH; reflexivity.
Qed.
Lemma remove_concat A (eq_dec : forall x y : A, {x = y}+{x <> y}) : forall l x,
remove eq_dec x (concat l) = flat_map (remove eq_dec x) l.
Proof.
intros l x; induction l as [|? ? IHl]; [ reflexivity | simpl ].
rewrite remove_app, IHl; reflexivity.
Qed.
Lemma map_id : forall (A :Type) (l : list A),
map (fun x => x) l = l.
Proof.
intros A l; induction l as [|? ? IHl]; simpl; auto; rewrite IHl; auto.
Qed.
Lemma map_map : forall (A B C:Type)(f:A->B)(g:B->C) l,
map g (map f l) = map (fun x => g (f x)) l.
Proof.
intros A B C f g l; induction l as [|? ? IHl]; simpl; auto.
rewrite IHl; auto.
Qed.
Lemma map_ext_in :
forall (A B : Type)(f g:A->B) l, (forall a, In a l -> f a = g a) -> map f l = map g l.
Proof.
intros A B f g l; induction l as [|? ? IHl]; simpl; auto.
intros H; rewrite H by intuition; rewrite IHl; auto.
Qed.
Lemma ext_in_map :
forall (A B : Type)(f g:A->B) l, map f l = map g l -> forall a, In a l -> f a = g a.
Proof. intros A B f g l; induction l; intros [=] ? []; subst; auto. Qed.
Arguments ext_in_map [A B f g l].
Lemma map_ext_in_iff :
forall (A B : Type)(f g:A->B) l, map f l = map g l <-> forall a, In a l -> f a = g a.
Proof. split; [apply ext_in_map | apply map_ext_in]. Qed.
Arguments map_ext_in_iff {A B f g l}.
Lemma map_ext :
forall (A B : Type)(f g:A->B), (forall a, f a = g a) -> forall l, map f l = map g l.
Proof.
intros; apply map_ext_in; auto.
Qed.
Lemma flat_map_ext : forall (A B : Type)(f g : A -> list B),
(forall a, f a = g a) -> forall l, flat_map f l = flat_map g l.
Proof.
intros A B f g Hext l.
rewrite 2 flat_map_concat_map.
now rewrite (map_ext _ g).
Qed.
Lemma nth_nth_nth_map A : forall (l : list A) n d ln dn, n < length ln \/ length l <= dn ->
nth (nth n ln dn) l d = nth n (map (fun x => nth x l d) ln) d.
Proof.
intros l n d ln dn; revert n; induction ln as [|? ? IHln]; intros n Hlen.
- destruct Hlen as [Hlen|Hlen].
+ inversion Hlen.
+ now rewrite nth_overflow; destruct n.
- destruct n; simpl; [ reflexivity | apply IHln ].
destruct Hlen; [ left; apply Nat.succ_lt_mono | right ]; assumption.
Qed.
(************************************)
(** Left-to-right iterator on lists *)
(************************************)
Section Fold_Left_Recursor.
Variables (A : Type) (B : Type).
Variable f : A -> B -> A.
Fixpoint fold_left (l:list B) (a0:A) : A :=
match l with
| nil => a0
| cons b t => fold_left t (f a0 b)
end.
Lemma fold_left_app : forall (l l':list B)(i:A),
fold_left (l++l') i = fold_left l' (fold_left l i).
Proof.
intro l; induction l.
simpl; auto.
intros.
simpl.
auto.
Qed.
End Fold_Left_Recursor.
Lemma fold_left_length :
forall (A:Type)(l:list A), fold_left (fun x _ => S x) l 0 = length l.
Proof.
intros A l.
enough (H : forall n, fold_left (fun x _ => S x) l n = n + length l) by exact (H 0).
induction l as [|? ? IHl]; simpl; auto.
now intros; rewrite IHl, Nat.add_succ_r.
Qed.
(************************************)
(** Right-to-left iterator on lists *)
(************************************)
Section Fold_Right_Recursor.
Variables (A : Type) (B : Type).
Variable f : B -> A -> A.
Variable a0 : A.
Fixpoint fold_right (l:list B) : A :=
match l with
| nil => a0
| cons b t => f b (fold_right t)
end.
End Fold_Right_Recursor.
Lemma fold_right_app : forall (A B:Type)(f:A->B->B) l l' i,
fold_right f i (l++l') = fold_right f (fold_right f i l') l.
Proof.
intros A B f l; induction l.
simpl; auto.
simpl; intros.
f_equal; auto.
Qed.
Lemma fold_left_rev_right : forall (A B:Type)(f:A->B->B) l i,
fold_right f i (rev l) = fold_left (fun x y => f y x) l i.
Proof.
intros A B f l; induction l.
simpl; auto.
intros.
simpl.
rewrite fold_right_app; simpl; auto.
Qed.
Theorem fold_symmetric :
forall (A : Type) (f : A -> A -> A),
(forall x y z : A, f x (f y z) = f (f x y) z) ->
forall (a0 : A), (forall y : A, f a0 y = f y a0) ->
forall (l : list A), fold_left f l a0 = fold_right f a0 l.
Proof.
intros A f assoc a0 comma0 l.
induction l as [ | a1 l IHl]; [ simpl; reflexivity | ].
simpl. rewrite <- IHl. clear IHl. revert a1.
induction l as [|? ? IHl]; [ auto | ].
simpl. intro. rewrite <- assoc. rewrite IHl. rewrite IHl. auto.
Qed.
(** [(list_power x y)] is [y^x], or the set of sequences of elts of [y]
indexed by elts of [x], sorted in lexicographic order. *)
Fixpoint list_power (A B:Type)(l:list A) (l':list B) :
list (list (A * B)) :=
match l with
| nil => cons nil nil
| cons x t =>
flat_map (fun f:list (A * B) => map (fun y:B => cons (x, y) f) l')
(list_power t l')
end.
(*************************************)
(** ** Boolean operations over lists *)
(*************************************)
Section Bool.
Variable A : Type.
Variable f : A -> bool.
(** find whether a boolean function can be satisfied by an
elements of the list. *)
Fixpoint existsb (l:list A) : bool :=
match l with
| nil => false
| a::l => f a || existsb l
end.
Lemma existsb_exists :
forall l, existsb l = true <-> exists x, In x l /\ f x = true.
Proof.
intro l; induction l as [ | a m IH ]; split; simpl.
- easy.
- intros [x [[]]].
- rewrite orb_true_iff; intros [ H | H ].
+ exists a; auto.
+ rewrite IH in H; destruct H as [ x [ Hxm Hfx ] ].
exists x; auto.
- intros [ x [ [ Hax | Hxm ] Hfx ] ].
+ now rewrite Hax, Hfx.
+ destruct IH as [ _ -> ]; eauto with bool.
Qed.
Lemma existsb_nth : forall l n d, n < length l ->
existsb l = false -> f (nth n l d) = false.
Proof.
intro l; induction l as [|? ? IHl].
inversion 1.
simpl; intros n ? ? H0.
destruct (orb_false_elim _ _ H0); clear H0; auto.
destruct n ; auto.
rewrite IHl; auto. now apply Nat.succ_lt_mono.
Qed.
Lemma existsb_app : forall l1 l2,
existsb (l1++l2) = existsb l1 || existsb l2.
Proof.
intro l1; induction l1 as [|a ? ?]; intros l2; simpl.
solve[auto].
case (f a); simpl; solve[auto].
Qed.
(** find whether a boolean function is satisfied by
all the elements of a list. *)
Fixpoint forallb (l:list A) : bool :=
match l with
| nil => true
| a::l => f a && forallb l
end.
Lemma forallb_forall :
forall l, forallb l = true <-> (forall x, In x l -> f x = true).
Proof.
intro l; induction l as [|a l IHl]; simpl; [ tauto | split; intro H ].
+ destruct (andb_prop _ _ H); intros a' [?|?].
- congruence.
- apply IHl; assumption.
+ apply andb_true_intro; split.
- apply H; left; reflexivity.
- apply IHl; intros; apply H; right; assumption.
Qed.
Lemma forallb_app :
forall l1 l2, forallb (l1++l2) = forallb l1 && forallb l2.
Proof.
intro l1; induction l1 as [|a ? ?]; simpl.
solve[auto].
case (f a); simpl; solve[auto].
Qed.
(** [filter] *)
Fixpoint filter (l:list A) : list A :=
match l with
| nil => nil
| x :: l => if f x then x::(filter l) else filter l
end.
Lemma filter_In : forall x l, In x (filter l) <-> In x l /\ f x = true.
Proof.
intros x l; induction l as [|a ? ?]; simpl.
intuition.
intros.
case_eq (f a); intros; simpl; intuition congruence.
Qed.
Lemma filter_app (l l':list A) :
filter (l ++ l') = filter l ++ filter l'.
Proof.
induction l as [|x l IH]; simpl; trivial.
destruct (f x); simpl; now rewrite IH.
Qed.
Lemma concat_filter_map : forall (l : list (list A)),
concat (map filter l) = filter (concat l).
Proof.
intro l; induction l as [| v l IHl]; [auto|].
simpl. rewrite IHl. rewrite filter_app. reflexivity.
Qed.
(** [find] *)
Fixpoint find (l:list A) : option A :=
match l with
| nil => None
| x :: tl => if f x then Some x else find tl
end.
Lemma find_some l x : find l = Some x -> In x l /\ f x = true.
Proof.
induction l as [|a l IH]; simpl; [easy| ].
case_eq (f a); intros Ha Eq.
* injection Eq as [= ->]; auto.
* destruct (IH Eq); auto.
Qed.
Lemma find_none l : find l = None -> forall x, In x l -> f x = false.
Proof.
induction l as [|a l IH]; simpl; [easy|].
case_eq (f a); intros Ha Eq x IN; [easy|].
destruct IN as [<-|IN]; auto.
Qed.
(** [partition] *)
Fixpoint partition (l:list A) : list A * list A :=
match l with
| nil => (nil, nil)
| x :: tl => let (g,d) := partition tl in
if f x then (x::g,d) else (g,x::d)
end.
Theorem partition_cons1 a l l1 l2:
partition l = (l1, l2) ->
f a = true ->
partition (a::l) = (a::l1, l2).
Proof.
simpl. now intros -> ->.
Qed.
Theorem partition_cons2 a l l1 l2:
partition l = (l1, l2) ->
f a=false ->
partition (a::l) = (l1, a::l2).
Proof.
simpl. now intros -> ->.
Qed.
Theorem partition_length l l1 l2:
partition l = (l1, l2) ->
length l = length l1 + length l2.
Proof.
revert l1 l2. induction l as [ | a l' Hrec]; intros l1 l2.
- now intros [= <- <- ].
- simpl. destruct (f a), (partition l') as (left, right);
intros [= <- <- ]; simpl; rewrite (Hrec left right); auto.
Qed.
Theorem partition_inv_nil (l : list A):
partition l = ([], []) <-> l = [].
Proof.
split.
- destruct l as [|a l'].
* intuition.
* simpl. destruct (f a), (partition l'); now intros [= -> ->].
- now intros ->.
Qed.
Theorem elements_in_partition l l1 l2:
partition l = (l1, l2) ->
forall x:A, In x l <-> In x l1 \/ In x l2.
Proof.
revert l1 l2. induction l as [| a l' Hrec]; simpl; intros l1 l2 Eq x.
- injection Eq as [= <- <-]. tauto.
- destruct (partition l') as (left, right).
specialize (Hrec left right eq_refl x).
destruct (f a); injection Eq as [= <- <-]; simpl; tauto.
Qed.
End Bool.
(*******************************)
(** ** Further filtering facts *)
(*******************************)
Section Filtering.
Variables (A : Type).
Lemma filter_map : forall (f g : A -> bool) (l : list A),
filter f l = filter g l <-> map f l = map g l.
Proof.
intros f g l; induction l as [| a l IHl]; [firstorder|].
simpl. destruct (f a) eqn:Hfa; destruct (g a) eqn:Hga; split; intros H.
- inversion H as [H1]. apply IHl in H1. rewrite H1. reflexivity.
- inversion H as [H1]. apply IHl in H1. rewrite H1. reflexivity.
- assert (Ha : In a (filter g l)). { rewrite <- H. apply in_eq. }
apply filter_In in Ha. destruct Ha as [_ Hga']. rewrite Hga in Hga'. inversion Hga'.
- inversion H.
- assert (Ha : In a (filter f l)). { rewrite H. apply in_eq. }
apply filter_In in Ha. destruct Ha as [_ Hfa']. rewrite Hfa in Hfa'. inversion Hfa'.
- inversion H.
- rewrite IHl in H. rewrite H. reflexivity.
- inversion H. apply IHl. assumption.
Qed.
Lemma filter_ext_in : forall (f g : A -> bool) (l : list A),
(forall a, In a l -> f a = g a) -> filter f l = filter g l.
Proof.
intros f g l H. rewrite filter_map. apply map_ext_in. auto.
Qed.
Lemma ext_in_filter : forall (f g : A -> bool) (l : list A),
filter f l = filter g l -> (forall a, In a l -> f a = g a).
Proof.
intros f g l H. rewrite filter_map in H. apply ext_in_map. assumption.
Qed.
Lemma filter_ext_in_iff : forall (f g : A -> bool) (l : list A),
filter f l = filter g l <-> (forall a, In a l -> f a = g a).
Proof.
split; [apply ext_in_filter | apply filter_ext_in].
Qed.
Lemma filter_ext : forall (f g : A -> bool),
(forall a, f a = g a) -> forall l, filter f l = filter g l.
Proof.
intros f g H l. rewrite filter_map. apply map_ext. assumption.
Qed.
(** Remove by filtering *)
Hypothesis eq_dec : forall x y : A, {x = y}+{x <> y}.
Definition remove' (x : A) : list A -> list A :=
filter (fun y => if eq_dec x y then false else true).
Lemma remove_alt (x : A) (l : list A) : remove' x l = remove eq_dec x l.
Proof with intuition.
induction l...
simpl. destruct eq_dec; f_equal...
Qed.
(** Counting occurrences by filtering *)
Definition count_occ' (l : list A) (x : A) : nat :=
length (filter (fun y => if eq_dec y x then true else false) l).
Lemma count_occ_alt (l : list A) (x : A) :
count_occ' l x = count_occ eq_dec l x.
Proof with intuition.
unfold count_occ'. induction l...
simpl; destruct eq_dec; simpl...
Qed.
End Filtering.
(******************************************************)
(** ** Operations on lists of pairs or lists of lists *)
(******************************************************)
Section ListPairs.
Variables (A : Type) (B : Type).
(** [split] derives two lists from a list of pairs *)
Fixpoint split (l:list (A*B)) : list A * list B :=
match l with
| [] => ([], [])
| (x,y) :: tl => let (left,right) := split tl in (x::left, y::right)
end.
Lemma in_split_l : forall (l:list (A*B))(p:A*B),
In p l -> In (fst p) (fst (split l)).
Proof.
intro l; induction l as [|a l IHl]; simpl; intros p H; auto.
destruct p as [a0 b]; destruct a; destruct (split l); simpl in *.
destruct H as [H|H].
injection H; auto.
right; apply (IHl (a0,b) H).
Qed.
Lemma in_split_r : forall (l:list (A*B))(p:A*B),
In p l -> In (snd p) (snd (split l)).
Proof.
intro l; induction l as [|a l IHl]; simpl; intros p H; auto.
destruct p as [a0 b]; destruct a; destruct (split l); simpl in *.
destruct H as [H|H].
injection H; auto.
right; apply (IHl (a0,b) H).
Qed.
Lemma split_nth : forall (l:list (A*B))(n:nat)(d:A*B),
nth n l d = (nth n (fst (split l)) (fst d), nth n (snd (split l)) (snd d)).
Proof.
intro l; induction l as [|a l IHl].
intros n d; destruct n; destruct d; simpl; auto.
intros n d; destruct n; destruct d; simpl; auto.
destruct a; destruct (split l); simpl; auto.
destruct a; destruct (split l); simpl in *; auto.
apply IHl.
Qed.
Lemma split_length_l : forall (l:list (A*B)),
length (fst (split l)) = length l.
Proof.
intro l; induction l as [|a l IHl]; simpl; auto.
destruct a; destruct (split l); simpl; auto.
Qed.
Lemma split_length_r : forall (l:list (A*B)),
length (snd (split l)) = length l.
Proof.
intro l; induction l as [|a l IHl]; simpl; auto.
destruct a; destruct (split l); simpl; auto.
Qed.
(** [combine] is the opposite of [split].
Lists given to [combine] are meant to be of same length.
If not, [combine] stops on the shorter list *)
Fixpoint combine (l : list A) (l' : list B) : list (A*B) :=
match l,l' with
| x::tl, y::tl' => (x,y)::(combine tl tl')
| _, _ => nil
end.
Lemma split_combine : forall (l: list (A*B)),
let (l1,l2) := split l in combine l1 l2 = l.
Proof.
intro l; induction l as [|a l IHl].
simpl; auto.
destruct a; simpl.
destruct (split l); simpl in *.
f_equal; auto.
Qed.
Lemma combine_split : forall (l:list A)(l':list B), length l = length l' ->
split (combine l l') = (l,l').
Proof.
intro l; induction l as [|a l IHl]; intro l'; destruct l';
simpl; trivial; try discriminate.
now intros [= ->%IHl].
Qed.
Lemma in_combine_l : forall (l:list A)(l':list B)(x:A)(y:B),
In (x,y) (combine l l') -> In x l.
Proof.
intro l; induction l as [|a l IHl].
simpl; auto.
intro l'; destruct l' as [|a0 l']; simpl; auto; intros x y H.
contradiction.
destruct H as [H|H].
injection H; auto.
right; apply IHl with l' y; auto.
Qed.
Lemma in_combine_r : forall (l:list A)(l':list B)(x:A)(y:B),
In (x,y) (combine l l') -> In y l'.
Proof.
intro l; induction l as [|? ? IHl].
simpl; intros; contradiction.
intro l'; destruct l'; simpl; auto; intros x y H.
destruct H as [H|H].
injection H; auto.
right; apply IHl with x; auto.
Qed.
Lemma combine_length : forall (l:list A)(l':list B),
length (combine l l') = min (length l) (length l').
Proof.
intro l; induction l.
simpl; auto.
intro l'; destruct l'; simpl; auto.
Qed.
Lemma combine_nth : forall (l:list A)(l':list B)(n:nat)(x:A)(y:B),
length l = length l' ->
nth n (combine l l') (x,y) = (nth n l x, nth n l' y).
Proof.
intro l; induction l; intro l'; destruct l'; intros n x y; try discriminate.
destruct n; simpl; auto.
destruct n; simpl in *; auto.
Qed.
(** [list_prod] has the same signature as [combine], but unlike
[combine], it adds every possible pairs, not only those at the
same position. *)
Fixpoint list_prod (l:list A) (l':list B) :
list (A * B) :=
match l with
| nil => nil
| cons x t => (map (fun y:B => (x, y)) l')++(list_prod t l')
end.
Lemma in_prod_aux :
forall (x:A) (y:B) (l:list B),
In y l -> In (x, y) (map (fun y0:B => (x, y0)) l).
Proof.
intros x y l; induction l;
[ simpl; auto
| simpl; destruct 1 as [H1| ];
[ left; rewrite H1; trivial | right; auto ] ].
Qed.
Lemma in_prod :
forall (l:list A) (l':list B) (x:A) (y:B),
In x l -> In y l' -> In (x, y) (list_prod l l').
Proof.
intro l; induction l;
[ simpl; tauto
| simpl; intros l' x y H H0; apply in_or_app; destruct H as [H|H];
[ left; rewrite H; apply in_prod_aux; assumption | right; auto ] ].
Qed.
Lemma in_prod_iff :
forall (l:list A)(l':list B)(x:A)(y:B),
In (x,y) (list_prod l l') <-> In x l /\ In y l'.
Proof.
intros l l' x y; split; [ | intros H; apply in_prod; intuition ].
induction l as [|a l IHl]; simpl; intros H.
intuition.
destruct (in_app_or _ _ _ H) as [H0|H0]; clear H.
destruct (in_map_iff (fun y : B => (a, y)) l' (x,y)) as (H1,_).
destruct (H1 H0) as (z,(H2,H3)); clear H0 H1.
injection H2 as [= -> ->]; intuition.
intuition.
Qed.
Lemma prod_length : forall (l:list A)(l':list B),
length (list_prod l l') = (length l) * (length l').
Proof.
intro l; induction l; simpl; auto.
intros.
rewrite app_length.
rewrite map_length.
auto.
Qed.
End ListPairs.
(*****************************************)
(** * Miscellaneous operations on lists *)
(*****************************************)
(******************************)
(** ** Length order of lists *)
(******************************)
Section length_order.
Variable A : Type.
Definition lel (l m:list A) := length l <= length m.
Variables a b : A.
Variables l m n : list A.
Lemma lel_refl : lel l l.
Proof.
now apply Nat.le_refl.
Qed.
Lemma lel_trans : lel l m -> lel m n -> lel l n.
Proof.
unfold lel; intros.
now_show (length l <= length n).
now apply Nat.le_trans with (length m).
Qed.
Lemma lel_cons_cons : lel l m -> lel (a :: l) (b :: m).
Proof.
now intros ? %Nat.succ_le_mono.
Qed.
Lemma lel_cons : lel l m -> lel l (b :: m).
Proof.
intros. now apply Nat.le_le_succ_r.
Qed.
Lemma lel_tail : lel (a :: l) (b :: m) -> lel l m.
Proof.
intros. now apply Nat.succ_le_mono.
Qed.
Lemma lel_nil : forall l':list A, lel l' nil -> nil = l'.
Proof.
intro l'; elim l'; [now intros|].
now intros a' y H H0 %Nat.nle_succ_0.
Qed.
End length_order.
#[global]
Hint Resolve lel_refl lel_cons_cons lel_cons lel_nil lel_nil nil_cons:
datatypes.
(******************************)
(** ** Set inclusion on list *)
(******************************)
Section SetIncl.
Variable A : Type.
Definition incl (l m:list A) := forall a:A, In a l -> In a m.
#[local]
Hint Unfold incl : core.
Lemma incl_nil_l : forall l, incl nil l.
Proof.
intros l a Hin; inversion Hin.
Qed.
Lemma incl_l_nil : forall l, incl l nil -> l = nil.
Proof.
intro l; destruct l as [|a l]; intros Hincl.
- reflexivity.
- exfalso; apply Hincl with a; simpl; auto.
Qed.
Lemma incl_refl : forall l:list A, incl l l.
Proof.
auto.
Qed.
#[local]
Hint Resolve incl_refl : core.
Lemma incl_tl : forall (a:A) (l m:list A), incl l m -> incl l (a :: m).
Proof.
auto with datatypes.
Qed.
#[local]
Hint Immediate incl_tl : core.
Lemma incl_tran : forall l m n:list A, incl l m -> incl m n -> incl l n.
Proof.
auto.
Qed.
Lemma incl_appl : forall l m n:list A, incl l n -> incl l (n ++ m).
Proof.
auto with datatypes.
Qed.
#[local]
Hint Immediate incl_appl : core.
Lemma incl_appr : forall l m n:list A, incl l n -> incl l (m ++ n).
Proof.
auto with datatypes.
Qed.
#[local]
Hint Immediate incl_appr : core.
Lemma incl_cons :
forall (a:A) (l m:list A), In a m -> incl l m -> incl (a :: l) m.
Proof.
unfold incl; simpl; intros a l m H H0 a0 H1.
now_show (In a0 m).
elim H1.
now_show (a = a0 -> In a0 m).
elim H1; auto; intro H2.
now_show (a = a0 -> In a0 m).
elim H2; auto. (* solves subgoal *)
now_show (In a0 l -> In a0 m).
auto.
Qed.
#[local]
Hint Resolve incl_cons : core.
Lemma incl_cons_inv : forall (a:A) (l m:list A),
incl (a :: l) m -> In a m /\ incl l m.
Proof.
intros a l m Hi.
split; [ | intros ? ? ]; apply Hi; simpl; auto.
Qed.
Lemma incl_app : forall l m n:list A, incl l n -> incl m n -> incl (l ++ m) n.
Proof.
unfold incl; simpl; intros l m n H H0 a H1.
now_show (In a n).
elim (in_app_or _ _ _ H1); auto.
Qed.
#[local]
Hint Resolve incl_app : core.
Lemma incl_app_app : forall l1 l2 m1 m2:list A,
incl l1 m1 -> incl l2 m2 -> incl (l1 ++ l2) (m1 ++ m2).
Proof.
intros.
apply incl_app; [ apply incl_appl | apply incl_appr]; assumption.
Qed.
Lemma incl_app_inv : forall l1 l2 m : list A,
incl (l1 ++ l2) m -> incl l1 m /\ incl l2 m.
Proof.
intro l1; induction l1 as [|a l1 IHl1]; intros l2 m Hin; split; auto.
- apply incl_nil_l.
- intros b Hb; inversion_clear Hb; subst; apply Hin.
+ now constructor.
+ simpl; apply in_cons.
apply incl_appl with l1; [ apply incl_refl | assumption ].
- apply IHl1.
now apply incl_cons_inv in Hin.
Qed.
Lemma incl_filter f l : incl (filter f l) l.
Proof. intros x Hin; now apply filter_In in Hin. Qed.
Lemma remove_incl (eq_dec : forall x y : A, {x = y} + {x <> y}) : forall l1 l2 x,
incl l1 l2 -> incl (remove eq_dec x l1) (remove eq_dec x l2).
Proof.
intros l1 l2 x Hincl y Hin.
apply in_remove in Hin; destruct Hin as [Hin Hneq].
apply in_in_remove; intuition.
Qed.
End SetIncl.
Lemma incl_map A B (f : A -> B) l1 l2 : incl l1 l2 -> incl (map f l1) (map f l2).
Proof.
intros Hincl x Hinx.
destruct (proj1 (in_map_iff _ _ _) Hinx) as [y [<- Hiny]].
apply in_map; intuition.
Qed.
#[global]
Hint Resolve incl_refl incl_tl incl_tran incl_appl incl_appr incl_cons
incl_app incl_map: datatypes.
(**************************************)
(** * Cutting a list at some position *)
(**************************************)
Section Cutting.
Variable A : Type.
Fixpoint firstn (n:nat)(l:list A) : list A :=
match n with
| 0 => nil
| S n => match l with
| nil => nil
| a::l => a::(firstn n l)
end
end.
Lemma firstn_nil n: firstn n [] = [].
Proof. induction n; now simpl. Qed.
Lemma firstn_cons n a l: firstn (S n) (a::l) = a :: (firstn n l).
Proof. now simpl. Qed.
Lemma firstn_all l: firstn (length l) l = l.
Proof. induction l as [| ? ? H]; simpl; [reflexivity | now rewrite H]. Qed.
Lemma firstn_all2 n: forall (l:list A), (length l) <= n -> firstn n l = l.
Proof. induction n as [|k iHk].
- intro l. inversion 1 as [H1|?].
rewrite (length_zero_iff_nil l) in H1. subst. now simpl.
- intro l; destruct l as [|x xs]; simpl.
* now reflexivity.
* simpl. intro H. f_equal. apply iHk. now apply Nat.succ_le_mono.
Qed.
Lemma firstn_O l: firstn 0 l = [].
Proof. now simpl. Qed.
Lemma firstn_le_length n: forall l:list A, length (firstn n l) <= n.
Proof.
induction n as [|k iHk]; simpl; [auto | intro l; destruct l as [|x xs]; simpl].
- now apply Nat.le_0_l.
- now rewrite <- Nat.succ_le_mono.
Qed.
Lemma firstn_length_le: forall l:list A, forall n:nat,
n <= length l -> length (firstn n l) = n.
Proof. intro l; induction l as [|x xs Hrec].
- simpl. intros n H. apply Nat.le_0_r in H. now subst.
- intro n; destruct n as [|n].
* now simpl.
* simpl. intro H. f_equal. apply Hrec. now apply Nat.succ_le_mono.
Qed.
Lemma firstn_app n:
forall l1 l2,
firstn n (l1 ++ l2) = (firstn n l1) ++ (firstn (n - length l1) l2).
Proof. induction n as [|k iHk]; intros l1 l2.
- now simpl.
- destruct l1 as [|x xs].
* reflexivity.
* rewrite <- app_comm_cons. simpl. f_equal. apply iHk.
Qed.
Lemma firstn_app_2 n:
forall l1 l2,
firstn ((length l1) + n) (l1 ++ l2) = l1 ++ firstn n l2.
Proof. induction n as [| k iHk];intros l1 l2.
- unfold firstn at 2. rewrite <- plus_n_O, app_nil_r.
rewrite firstn_app. rewrite Nat.sub_diag.
unfold firstn at 2. rewrite app_nil_r. apply firstn_all.
- destruct l2 as [|x xs].
* simpl. rewrite app_nil_r. apply firstn_all2. now apply Nat.le_add_r.
* rewrite firstn_app. assert (H0 : (length l1 + S k - length l1) = S k).
now rewrite Nat.add_comm, Nat.add_sub.
rewrite H0, firstn_all2; [reflexivity | now apply Nat.le_add_r].
Qed.
Lemma firstn_firstn:
forall l:list A,
forall i j : nat,
firstn i (firstn j l) = firstn (min i j) l.
Proof. intro l; induction l as [|x xs Hl].
- intros. simpl. now rewrite ?firstn_nil.
- intro i; destruct i.
* intro. now simpl.
* intro j; destruct j.
+ now simpl.
+ simpl. f_equal. apply Hl.
Qed.
Fixpoint skipn (n:nat)(l:list A) : list A :=
match n with
| 0 => l
| S n => match l with
| nil => nil
| a::l => skipn n l
end
end.
Lemma firstn_skipn_comm : forall m n l,
firstn m (skipn n l) = skipn n (firstn (n + m) l).
Proof. now intros m n; induction n; intros []; simpl; destruct m. Qed.
Lemma skipn_firstn_comm : forall m n l,
skipn m (firstn n l) = firstn (n - m) (skipn m l).
Proof. now intro m; induction m; intros [] []; simpl; rewrite ?firstn_nil. Qed.
Lemma skipn_O : forall l, skipn 0 l = l.
Proof. reflexivity. Qed.
Lemma skipn_nil : forall n, skipn n ([] : list A) = [].
Proof. now intros []. Qed.
Lemma skipn_cons n a l: skipn (S n) (a::l) = skipn n l.
Proof. reflexivity. Qed.
Lemma skipn_all : forall l, skipn (length l) l = nil.
Proof. now intro l; induction l. Qed.
#[deprecated(since="8.12",note="Use skipn_all instead.")] Notation skipn_none := skipn_all.
Lemma skipn_all2 n: forall l, length l <= n -> skipn n l = [].
Proof.
intros l L%Nat.sub_0_le; rewrite <-(firstn_all l) at 1.
now rewrite skipn_firstn_comm, L.
Qed.
Lemma firstn_skipn : forall n l, firstn n l ++ skipn n l = l.
Proof.
intro n; induction n.
simpl; auto.
intro l; destruct l; simpl; auto.
f_equal; auto.
Qed.
Lemma firstn_length : forall n l, length (firstn n l) = min n (length l).
Proof.
intro n; induction n; intro l; destruct l; simpl; auto.
Qed.
Lemma skipn_length n :
forall l, length (skipn n l) = length l - n.
Proof.
induction n.
- intros l; simpl; rewrite Nat.sub_0_r; reflexivity.
- intro l; destruct l; simpl; auto.
Qed.
Lemma skipn_app n : forall l1 l2,
skipn n (l1 ++ l2) = (skipn n l1) ++ (skipn (n - length l1) l2).
Proof. induction n; auto; intros [|]; simpl; auto. Qed.
Lemma firstn_skipn_rev: forall x l,
firstn x l = rev (skipn (length l - x) (rev l)).
Proof.
intros x l; rewrite <-(firstn_skipn x l) at 3.
rewrite rev_app_distr, skipn_app, rev_app_distr, rev_length,
skipn_length, Nat.sub_diag; simpl; rewrite rev_involutive.
rewrite <-app_nil_r at 1; f_equal; symmetry; apply length_zero_iff_nil.
repeat rewrite rev_length, skipn_length; apply Nat.sub_diag.
Qed.
Lemma firstn_rev: forall x l,
firstn x (rev l) = rev (skipn (length l - x) l).
Proof.
now intros x l; rewrite firstn_skipn_rev, rev_involutive, rev_length.
Qed.
Lemma skipn_rev: forall x l,
skipn x (rev l) = rev (firstn (length l - x) l).
Proof.
intros x l; rewrite firstn_skipn_rev, rev_involutive, <-rev_length.
destruct (Nat.le_ge_cases (length (rev l)) x) as [L | L].
- rewrite skipn_all2; [apply Nat.sub_0_le in L | trivial].
now rewrite L, Nat.sub_0_r, skipn_all.
- f_equal. now apply Nat.eq_sym, Nat.add_sub_eq_l, Nat.sub_add.
Qed.
Lemma removelast_firstn : forall n l, n < length l ->
removelast (firstn (S n) l) = firstn n l.
Proof.
intro n; induction n as [|n IHn]; intro l; destruct l as [|a l].
simpl; auto.
simpl; auto.
simpl; auto.
intros H.
simpl in H.
change (firstn (S (S n)) (a::l)) with ((a::nil)++firstn (S n) l).
change (firstn (S n) (a::l)) with (a::firstn n l).
rewrite removelast_app.
rewrite IHn; [reflexivity|now apply Nat.succ_le_mono].
clear IHn; destruct l; simpl in *; try discriminate.
inversion_clear H as [|? H1].
inversion_clear H1.
Qed.
Lemma removelast_firstn_len : forall l,
removelast l = firstn (pred (length l)) l.
Proof.
intro l; induction l as [|a l IHl]; [ reflexivity | simpl ].
destruct l; [ | rewrite IHl ]; reflexivity.
Qed.
Lemma firstn_removelast : forall n l, n < length l ->
firstn n (removelast l) = firstn n l.
Proof.
intro n; induction n; intro l; destruct l as [|a l].
simpl; auto.
simpl; auto.
simpl; auto.
intros H.
simpl in H.
change (removelast (a :: l)) with (removelast ((a::nil)++l)).
rewrite removelast_app.
simpl; f_equal. apply IHn. now apply Nat.succ_lt_mono.
intro H0; rewrite H0 in H; inversion_clear H as [|? H1]; inversion_clear H1.
Qed.
End Cutting.
(**************************************************************)
(** ** Combining pairs of lists of possibly-different lengths *)
(**************************************************************)
Section Combining.
Variables (A B : Type).
Lemma combine_nil : forall (l : list A),
combine l (@nil B) = @nil (A*B).
Proof.
intros l.
apply length_zero_iff_nil.
rewrite combine_length. simpl. rewrite Nat.min_0_r.
reflexivity.
Qed.
Lemma combine_firstn_l : forall (l : list A) (l' : list B),
combine l l' = combine l (firstn (length l) l').
Proof.
intro l; induction l as [| x l IHl]; intros l'; [reflexivity|].
destruct l' as [| x' l']; [reflexivity|].
simpl. specialize IHl with l'. rewrite <- IHl.
reflexivity.
Qed.
Lemma combine_firstn_r : forall (l : list A) (l' : list B),
combine l l' = combine (firstn (length l') l) l'.
Proof.
intros l l'. generalize dependent l.
induction l' as [| x' l' IHl']; intros l.
- simpl. apply combine_nil.
- destruct l as [| x l]; [reflexivity|].
simpl. specialize IHl' with l. rewrite <- IHl'.
reflexivity.
Qed.
Lemma combine_firstn : forall (l : list A) (l' : list B) (n : nat),
firstn n (combine l l') = combine (firstn n l) (firstn n l').
Proof.
intro l; induction l as [| x l IHl]; intros l' n.
- simpl. repeat (rewrite firstn_nil). reflexivity.
- destruct l' as [| x' l'].
+ simpl. repeat (rewrite firstn_nil). rewrite combine_nil. reflexivity.
+ simpl. destruct n as [| n]; [reflexivity|].
repeat (rewrite firstn_cons). simpl.
rewrite IHl. reflexivity.
Qed.
End Combining.
(**********************************************************************)
(** ** Predicate for List addition/removal (no need for decidability) *)
(**********************************************************************)
Section Add.
Variable A : Type.
(* [Add a l l'] means that [l'] is exactly [l], with [a] added
once somewhere *)
Inductive Add (a:A) : list A -> list A -> Prop :=
| Add_head l : Add a l (a::l)
| Add_cons x l l' : Add a l l' -> Add a (x::l) (x::l').
Lemma Add_app a l1 l2 : Add a (l1++l2) (l1++a::l2).
Proof.
induction l1; simpl; now constructor.
Qed.
Lemma Add_split a l l' :
Add a l l' -> exists l1 l2, l = l1++l2 /\ l' = l1++a::l2.
Proof.
induction 1 as [l|x ? ? ? IHAdd].
- exists nil; exists l; split; trivial.
- destruct IHAdd as (l1 & l2 & Hl & Hl').
exists (x::l1); exists l2; split; simpl; f_equal; trivial.
Qed.
Lemma Add_in a l l' : Add a l l' ->
forall x, In x l' <-> In x (a::l).
Proof.
induction 1 as [|? ? ? ? IHAdd]; intros; simpl in *; rewrite ?IHAdd; tauto.
Qed.
Lemma Add_length a l l' : Add a l l' -> length l' = S (length l).
Proof.
induction 1; simpl; now auto.
Qed.
Lemma Add_inv a l : In a l -> exists l', Add a l' l.
Proof.
intro Ha. destruct (in_split _ _ Ha) as (l1 & l2 & ->).
exists (l1 ++ l2). apply Add_app.
Qed.
Lemma incl_Add_inv a l u v :
~In a l -> incl (a::l) v -> Add a u v -> incl l u.
Proof.
intros Ha H AD y Hy.
assert (Hy' : In y (a::u)).
{ rewrite <- (Add_in AD). apply H; simpl; auto. }
destruct Hy'; [ subst; now elim Ha | trivial ].
Qed.
End Add.
(********************************)
(** ** Lists without redundancy *)
(********************************)
Section ReDun.
Variable A : Type.
Inductive NoDup : list A -> Prop :=
| NoDup_nil : NoDup nil
| NoDup_cons : forall x l, ~ In x l -> NoDup l -> NoDup (x::l).
Lemma NoDup_Add a l l' : Add a l l' -> (NoDup l' <-> NoDup l /\ ~In a l).
Proof.
induction 1 as [l|x l l' AD IH].
- split; [ inversion_clear 1; now split | now constructor ].
- split.
+ inversion_clear 1. rewrite IH in *. rewrite (Add_in AD) in *.
simpl in *; split; try constructor; intuition.
+ intros (N,IN). inversion_clear N. constructor.
* rewrite (Add_in AD); simpl in *; intuition.
* apply IH. split; trivial. simpl in *; intuition.
Qed.
Lemma NoDup_remove l l' a :
NoDup (l++a::l') -> NoDup (l++l') /\ ~In a (l++l').
Proof.
apply NoDup_Add. apply Add_app.
Qed.
Lemma NoDup_remove_1 l l' a : NoDup (l++a::l') -> NoDup (l++l').
Proof.
intros. now apply NoDup_remove with a.
Qed.
Lemma NoDup_remove_2 l l' a : NoDup (l++a::l') -> ~In a (l++l').
Proof.
intros. now apply NoDup_remove.
Qed.
Theorem NoDup_cons_iff a l:
NoDup (a::l) <-> ~ In a l /\ NoDup l.
Proof.
split.
+ inversion_clear 1. now split.
+ now constructor.
Qed.
Lemma NoDup_rev l : NoDup l -> NoDup (rev l).
Proof.
induction l as [|a l IHl]; simpl; intros Hnd; [ constructor | ].
inversion_clear Hnd as [ | ? ? Hnin Hndl ].
assert (Add a (rev l) (rev l ++ a :: nil)) as Hadd
by (rewrite <- (app_nil_r (rev l)) at 1; apply Add_app).
apply NoDup_Add in Hadd; apply Hadd; intuition.
now apply Hnin, in_rev.
Qed.
Lemma NoDup_filter f l : NoDup l -> NoDup (filter f l).
Proof.
induction l as [|a l IHl]; simpl; intros Hnd; auto.
apply NoDup_cons_iff in Hnd.
destruct (f a); [ | intuition ].
apply NoDup_cons_iff; split; [intro H|]; intuition.
apply filter_In in H; intuition.
Qed.
(** Effective computation of a list without duplicates *)
Hypothesis decA: forall x y : A, {x = y} + {x <> y}.
Fixpoint nodup (l : list A) : list A :=
match l with
| [] => []
| x::xs => if in_dec decA x xs then nodup xs else x::(nodup xs)
end.
Lemma nodup_fixed_point (l : list A) :
NoDup l -> nodup l = l.
Proof.
induction l as [| x l IHl]; [auto|]. intros H.
simpl. destruct (in_dec decA x l) as [Hx | Hx]; rewrite NoDup_cons_iff in H.
- destruct H as [H' _]. contradiction.
- destruct H as [_ H']. apply IHl in H'. rewrite -> H'. reflexivity.
Qed.
Lemma nodup_In l x : In x (nodup l) <-> In x l.
Proof.
induction l as [|a l' Hrec]; simpl.
- reflexivity.
- destruct (in_dec decA a l'); simpl; rewrite Hrec.
* intuition; now subst.
* reflexivity.
Qed.
Lemma nodup_incl l1 l2 : incl l1 (nodup l2) <-> incl l1 l2.
Proof.
split; intros Hincl a Ha; apply nodup_In; intuition.
Qed.
Lemma NoDup_nodup l: NoDup (nodup l).
Proof.
induction l as [|a l' Hrec]; simpl.
- constructor.
- destruct (in_dec decA a l'); simpl.
* assumption.
* constructor; [ now rewrite nodup_In | assumption].
Qed.
Lemma nodup_inv k l a : nodup k = a :: l -> ~ In a l.
Proof.
intros H.
assert (H' : NoDup (a::l)).
{ rewrite <- H. apply NoDup_nodup. }
now inversion_clear H'.
Qed.
Theorem NoDup_count_occ l:
NoDup l <-> (forall x:A, count_occ decA l x <= 1).
Proof.
induction l as [| a l' Hrec].
- simpl; split; auto. constructor.
- rewrite NoDup_cons_iff, Hrec, (count_occ_not_In decA). clear Hrec. split.
+ intros (Ha, H) x. simpl. destruct (decA a x); auto.
subst; now rewrite Ha.
+ intro H; split.
* specialize (H a). rewrite count_occ_cons_eq in H; trivial.
now inversion H.
* intros x. specialize (H x). simpl in *. destruct (decA a x); auto.
now apply Nat.lt_le_incl.
Qed.
Theorem NoDup_count_occ' l:
NoDup l <-> (forall x:A, In x l -> count_occ decA l x = 1).
Proof.
rewrite NoDup_count_occ.
setoid_rewrite (count_occ_In decA). unfold gt, lt in *.
split; intros H x; specialize (H x);
set (n := count_occ decA l x) in *; clearbody n.
(* the rest would be solved by omega if we had it here... *)
- now apply Nat.le_antisymm.
- destruct (Nat.le_gt_cases 1 n); trivial.
+ rewrite H; trivial.
+ now apply Nat.lt_le_incl.
Qed.
(** Alternative characterisations of being without duplicates,
thanks to [nth_error] and [nth] *)
Lemma NoDup_nth_error l :
NoDup l <->
(forall i j, i<length l -> nth_error l i = nth_error l j -> i = j).
Proof.
split.
{ intros H; induction H as [|a l Hal Hl IH]; intros i j Hi E.
- inversion Hi.
- destruct i, j; simpl in *; auto.
* elim Hal. eapply nth_error_In; eauto.
* elim Hal. eapply nth_error_In; eauto.
* f_equal. now apply IH;[apply Nat.succ_lt_mono|]. }
{ induction l as [|a l IHl]; intros H; constructor.
* intro Ha. apply In_nth_error in Ha. destruct Ha as (n,Hn).
assert (n < length l) by (now rewrite <- nth_error_Some, Hn).
specialize (H 0 (S n)). simpl in H. now discriminate H; [apply Nat.lt_0_succ|].
* apply IHl.
intros i j Hi %Nat.succ_lt_mono E. now apply eq_add_S, H. }
Qed.
Lemma NoDup_nth l d :
NoDup l <->
(forall i j, i<length l -> j<length l ->
nth i l d = nth j l d -> i = j).
Proof.
split.
{ intros H; induction H as [|a l Hal Hl IH]; intros i j Hi Hj E.
- inversion Hi.
- destruct i, j; simpl in *; auto.
* elim Hal. subst a. now apply nth_In, Nat.succ_lt_mono.
* elim Hal. subst a. now apply nth_In, Nat.succ_lt_mono.
* f_equal. apply IH; [| |assumption]; now apply Nat.succ_lt_mono. }
{ induction l as [|a l IHl]; intros H; constructor.
* intro Ha. eapply In_nth in Ha. destruct Ha as (n & Hn & Hn').
specialize (H 0 (S n)). simpl in H.
apply Nat.succ_lt_mono in Hn.
discriminate H; eauto using Nat.lt_0_succ.
* apply IHl.
intros i j Hi %Nat.succ_lt_mono Hj %Nat.succ_lt_mono E.
now apply eq_add_S, H. }
Qed.
(** Having [NoDup] hypotheses bring more precise facts about [incl]. *)
Lemma NoDup_incl_length l l' :
NoDup l -> incl l l' -> length l <= length l'.
Proof.
intros N. revert l'. induction N as [|a l Hal N IH]; simpl.
- intros. now apply Nat.le_0_l.
- intros l' H.
destruct (Add_inv a l') as (l'', AD). { apply H; simpl; auto. }
rewrite (Add_length AD). apply le_n_S. apply IH.
now apply incl_Add_inv with a l'.
Qed.
Lemma NoDup_length_incl l l' :
NoDup l -> length l' <= length l -> incl l l' -> incl l' l.
Proof.
intros N. revert l'. induction N as [|a l Hal N IH].
- intro l'; destruct l'; easy.
- intros l' E H x Hx.
destruct (Add_inv a l') as (l'', AD). { apply H; simpl; auto. }
rewrite (Add_in AD) in Hx. simpl in Hx.
destruct Hx as [Hx|Hx]; [left; trivial|right].
revert x Hx. apply (IH l''); trivial.
* apply Nat.succ_le_mono. now rewrite <- (Add_length AD).
* now apply incl_Add_inv with a l'.
Qed.
Lemma NoDup_incl_NoDup (l l' : list A) : NoDup l ->
length l' <= length l -> incl l l' -> NoDup l'.
Proof.
revert l'; induction l as [|a l IHl]; simpl; intros l' Hnd Hlen Hincl.
- now destruct l'; inversion Hlen.
- assert (In a l') as Ha by now apply Hincl; left.
apply in_split in Ha as [l1' [l2' ->]].
inversion_clear Hnd as [|? ? Hnin Hnd'].
apply (NoDup_Add (Add_app a l1' l2')); split.
+ apply IHl; auto.
* rewrite app_length.
rewrite app_length in Hlen; simpl in Hlen; rewrite Nat.add_succ_r in Hlen.
now apply Nat.succ_le_mono.
* apply (incl_Add_inv (u:= l1' ++ l2')) in Hincl; auto.
apply Add_app.
+ intros Hnin'.
assert (incl (a :: l) (l1' ++ l2')) as Hincl''.
{ apply incl_tran with (l1' ++ a :: l2'); auto.
intros x Hin.
apply in_app_or in Hin as [Hin|[->|Hin]]; intuition. }
apply NoDup_incl_length in Hincl''; [ | now constructor ].
apply (Nat.nle_succ_diag_l (length l1' + length l2')).
rewrite_all app_length.
simpl in Hlen; rewrite Nat.add_succ_r in Hlen.
now transitivity (S (length l)).
Qed.
End ReDun.
(** NoDup and map *)
(** NB: the reciprocal result holds only for injective functions,
see FinFun.v *)
Lemma NoDup_map_inv A B (f:A->B) l : NoDup (map f l) -> NoDup l.
Proof.
induction l; simpl; inversion_clear 1; subst; constructor; auto.
intro H. now apply (in_map f) in H.
Qed.
(***********************************)
(** ** Sequence of natural numbers *)
(***********************************)
Section NatSeq.
(** [seq] computes the sequence of [len] contiguous integers
that starts at [start]. For instance, [seq 2 3] is [2::3::4::nil]. *)
Fixpoint seq (start len:nat) : list nat :=
match len with
| 0 => nil
| S len => start :: seq (S start) len
end.
Lemma cons_seq : forall len start, start :: seq (S start) len = seq start (S len).
Proof.
reflexivity.
Qed.
Lemma seq_length : forall len start, length (seq start len) = len.
Proof.
intro len; induction len; simpl; auto.
Qed.
Lemma seq_nth : forall len start n d,
n < len -> nth n (seq start len) d = start+n.
Proof.
intro len; induction len as [|len IHlen]; intros start n d H.
inversion H.
simpl seq.
destruct n; simpl. now rewrite Nat.add_0_r.
now rewrite IHlen; [rewrite Nat.add_succ_r|apply Nat.succ_lt_mono].
Qed.
Lemma seq_shift : forall len start,
map S (seq start len) = seq (S start) len.
Proof.
intro len; induction len as [|len IHlen]; simpl; auto.
intros.
now rewrite IHlen.
Qed.
Lemma in_seq len start n :
In n (seq start len) <-> start <= n < start+len.
Proof.
revert start. induction len as [|len IHlen]; simpl; intros start.
- rewrite <- plus_n_O. split;[easy|].
intros (H,H'). apply (Nat.lt_irrefl start).
eapply Nat.le_lt_trans; eassumption.
- rewrite IHlen, <- plus_n_Sm; simpl; split.
+ intros [H|H]; subst; intuition.
* apply -> Nat.succ_le_mono. apply Nat.le_add_r.
* now apply Nat.lt_le_incl.
+ intros (H,H'). inversion H.
* now left.
* right. subst. now split; [apply -> Nat.succ_le_mono|].
Qed.
Lemma seq_NoDup len start : NoDup (seq start len).
Proof.
revert start; induction len; simpl; constructor; trivial.
rewrite in_seq. intros (H,_). now apply (Nat.lt_irrefl start).
Qed.
Lemma seq_app : forall len1 len2 start,
seq start (len1 + len2) = seq start len1 ++ seq (start + len1) len2.
Proof.
intro len1; induction len1 as [|len1' IHlen]; intros; simpl in *.
- now rewrite Nat.add_0_r.
- now rewrite Nat.add_succ_r, IHlen.
Qed.
Lemma seq_S : forall len start, seq start (S len) = seq start len ++ [start + len].
Proof.
intros len start.
change [start + len] with (seq (start + len) 1).
rewrite <- seq_app.
rewrite <- plus_n_Sm, <- plus_n_O; reflexivity.
Qed.
End NatSeq.
Section Exists_Forall.
(** * Existential and universal predicates over lists *)
Variable A:Type.
Section One_predicate.
Variable P:A->Prop.
Inductive Exists : list A -> Prop :=
| Exists_cons_hd : forall x l, P x -> Exists (x::l)
| Exists_cons_tl : forall x l, Exists l -> Exists (x::l).
#[local]
Hint Constructors Exists : core.
Lemma Exists_exists (l:list A) :
Exists l <-> (exists x, In x l /\ P x).
Proof.
split.
- induction 1; firstorder.
- induction l; firstorder; subst; auto.
Qed.
Lemma Exists_nth l :
Exists l <-> exists i d, i < length l /\ P (nth i l d).
Proof.
split.
- intros HE; apply Exists_exists in HE.
destruct HE as [a [Hin HP]].
apply (In_nth _ _ a) in Hin; destruct Hin as [i [Hl Heq]].
rewrite <- Heq in HP.
now exists i; exists a.
- intros [i [d [Hl HP]]].
apply Exists_exists; exists (nth i l d); split.
apply nth_In; assumption.
assumption.
Qed.
Lemma Exists_nil : Exists nil <-> False.
Proof. split; inversion 1. Qed.
Lemma Exists_cons x l:
Exists (x::l) <-> P x \/ Exists l.
Proof. split; inversion 1; auto. Qed.
Lemma Exists_app l1 l2 :
Exists (l1 ++ l2) <-> Exists l1 \/ Exists l2.
Proof.
induction l1; simpl; split; intros HE; try now intuition.
- inversion_clear HE; intuition.
- destruct HE as [HE|HE]; intuition.
inversion_clear HE; intuition.
Qed.
Lemma Exists_rev l : Exists l -> Exists (rev l).
Proof.
induction l; intros HE; intuition.
inversion_clear HE; simpl; apply Exists_app; intuition.
Qed.
Lemma Exists_dec l:
(forall x:A, {P x} + { ~ P x }) ->
{Exists l} + {~ Exists l}.
Proof.
intro Pdec. induction l as [|a l' Hrec].
- right. abstract now rewrite Exists_nil.
- destruct Hrec as [Hl'|Hl'].
+ left. now apply Exists_cons_tl.
+ destruct (Pdec a) as [Ha|Ha].
* left. now apply Exists_cons_hd.
* right. abstract now inversion 1.
Defined.
Lemma Exists_fold_right l :
Exists l <-> fold_right (fun x => or (P x)) False l.
Proof.
induction l; simpl; split; intros HE; try now inversion HE; intuition.
Qed.
Lemma incl_Exists l1 l2 : incl l1 l2 -> Exists l1 -> Exists l2.
Proof.
intros Hincl HE.
apply Exists_exists in HE; destruct HE as [a [Hin HP]].
apply Exists_exists; exists a; intuition.
Qed.
Inductive Forall : list A -> Prop :=
| Forall_nil : Forall nil
| Forall_cons : forall x l, P x -> Forall l -> Forall (x::l).
#[local]
Hint Constructors Forall : core.
Lemma Forall_forall (l:list A):
Forall l <-> (forall x, In x l -> P x).
Proof.
split.
- induction 1; firstorder; subst; auto.
- induction l; firstorder auto with datatypes.
Qed.
Lemma Forall_nth l :
Forall l <-> forall i d, i < length l -> P (nth i l d).
Proof.
split.
- intros HF i d Hl.
apply (Forall_forall l).
assumption.
apply nth_In; assumption.
- intros HF.
apply Forall_forall; intros a Hin.
apply (In_nth _ _ a) in Hin; destruct Hin as [i [Hl Heq]].
rewrite <- Heq; intuition.
Qed.
Lemma Forall_inv : forall (a:A) l, Forall (a :: l) -> P a.
Proof.
intros a l H; inversion H; trivial.
Qed.
Theorem Forall_inv_tail : forall (a:A) l, Forall (a :: l) -> Forall l.
Proof.
intros a l H; inversion H; trivial.
Qed.
Lemma Forall_app l1 l2 :
Forall (l1 ++ l2) <-> Forall l1 /\ Forall l2.
Proof.
induction l1; simpl; split; intros HF; try now intuition.
- inversion_clear HF; intuition.
- destruct HF as [HF1 HF2]; inversion HF1; intuition.
Qed.
Lemma Forall_elt a l1 l2 : Forall (l1 ++ a :: l2) -> P a.
Proof.
intros HF; apply Forall_app in HF; destruct HF as [HF1 HF2]; now inversion HF2.
Qed.
Lemma Forall_rev l : Forall l -> Forall (rev l).
Proof.
induction l; intros HF; intuition.
inversion_clear HF; simpl; apply Forall_app; intuition.
Qed.
Lemma Forall_rect : forall (Q : list A -> Type),
Q [] -> (forall b l, P b -> Q (b :: l)) -> forall l, Forall l -> Q l.
Proof.
intros Q H H' l; induction l; intro; [|eapply H', Forall_inv]; eassumption.
Qed.
Lemma Forall_dec :
(forall x:A, {P x} + { ~ P x }) ->
forall l:list A, {Forall l} + {~ Forall l}.
Proof.
intros Pdec l. induction l as [|a l' Hrec].
- left. apply Forall_nil.
- destruct Hrec as [Hl'|Hl'].
+ destruct (Pdec a) as [Ha|Ha].
* left. now apply Forall_cons.
* right. abstract now inversion 1.
+ right. abstract now inversion 1.
Defined.
Lemma Forall_fold_right l :
Forall l <-> fold_right (fun x => and (P x)) True l.
Proof.
induction l; simpl; split; intros HF; try now inversion HF; intuition.
Qed.
Lemma incl_Forall l1 l2 : incl l2 l1 -> Forall l1 -> Forall l2.
Proof.
intros Hincl HF.
apply Forall_forall; intros a Ha.
apply (Forall_forall l1); intuition.
Qed.
End One_predicate.
Lemma map_ext_Forall B : forall (f g : A -> B) l,
Forall (fun x => f x = g x) l -> map f l = map g l.
Proof.
intros; apply map_ext_in, Forall_forall; assumption.
Qed.
Theorem Exists_impl : forall (P Q : A -> Prop), (forall a : A, P a -> Q a) ->
forall l, Exists P l -> Exists Q l.
Proof.
intros P Q H l H0.
induction H0 as [x l H0|x l H0 IHExists].
apply (Exists_cons_hd Q x l (H x H0)).
apply (Exists_cons_tl x IHExists).
Qed.
Lemma Exists_or : forall (P Q : A -> Prop) l,
Exists P l \/ Exists Q l -> Exists (fun x => P x \/ Q x) l.
Proof.
intros P Q l; induction l as [|a l IHl]; intros [H | H]; inversion H; subst.
1,3: apply Exists_cons_hd; auto.
all: apply Exists_cons_tl, IHl; auto.
Qed.
Lemma Exists_or_inv : forall (P Q : A -> Prop) l,
Exists (fun x => P x \/ Q x) l -> Exists P l \/ Exists Q l.
Proof.
intros P Q l; induction l as [|a l IHl];
intro Hl; inversion Hl as [ ? ? H | ? ? H ]; subst.
- inversion H; now repeat constructor.
- destruct (IHl H); now repeat constructor.
Qed.
Lemma Forall_impl : forall (P Q : A -> Prop), (forall a, P a -> Q a) ->
forall l, Forall P l -> Forall Q l.
Proof.
intros P Q H l. rewrite !Forall_forall. firstorder.
Qed.
Lemma Forall_and : forall (P Q : A -> Prop) l,
Forall P l -> Forall Q l -> Forall (fun x => P x /\ Q x) l.
Proof.
intros P Q l; induction l; intros HP HQ; constructor; inversion HP; inversion HQ; auto.
Qed.
Lemma Forall_and_inv : forall (P Q : A -> Prop) l,
Forall (fun x => P x /\ Q x) l -> Forall P l /\ Forall Q l.
Proof.
intros P Q l; induction l; intro Hl; split; constructor; inversion Hl; firstorder.
Qed.
Lemma Forall_Exists_neg (P:A->Prop)(l:list A) :
Forall (fun x => ~ P x) l <-> ~(Exists P l).
Proof.
rewrite Forall_forall, Exists_exists. firstorder.
Qed.
Lemma Exists_Forall_neg (P:A->Prop)(l:list A) :
(forall x, P x \/ ~P x) ->
Exists (fun x => ~ P x) l <-> ~(Forall P l).
Proof.
intro Dec.
split.
- rewrite Forall_forall, Exists_exists; firstorder.
- intros NF.
induction l as [|a l IH].
+ destruct NF. constructor.
+ destruct (Dec a) as [Ha|Ha].
* apply Exists_cons_tl, IH. contradict NF. now constructor.
* now apply Exists_cons_hd.
Qed.
Lemma neg_Forall_Exists_neg (P:A->Prop) (l:list A) :
(forall x:A, {P x} + { ~ P x }) ->
~ Forall P l ->
Exists (fun x => ~ P x) l.
Proof.
intro Dec.
apply Exists_Forall_neg; intros x.
destruct (Dec x); auto.
Qed.
Lemma Forall_Exists_dec (P:A->Prop) :
(forall x:A, {P x} + { ~ P x }) ->
forall l:list A,
{Forall P l} + {Exists (fun x => ~ P x) l}.
Proof.
intros Pdec l.
destruct (Forall_dec P Pdec l); [left|right]; trivial.
now apply neg_Forall_Exists_neg.
Defined.
Lemma incl_Forall_in_iff l l' :
incl l l' <-> Forall (fun x => In x l') l.
Proof. now rewrite Forall_forall; split. Qed.
End Exists_Forall.
#[global]
Hint Constructors Exists : core.
#[global]
Hint Constructors Forall : core.
Lemma Exists_map A B (f : A -> B) P l :
Exists P (map f l) <-> Exists (fun x => P (f x)) l.
Proof.
induction l as [|a l IHl].
- cbn. now rewrite Exists_nil.
- cbn. now rewrite ?Exists_cons, IHl.
Qed.
Lemma Exists_concat A P (ls : list (list A)) :
Exists P (concat ls) <-> Exists (Exists P) ls.
Proof.
induction ls as [|l ls IHls].
- cbn. now rewrite Exists_nil.
- cbn. now rewrite Exists_app, Exists_cons, IHls.
Qed.
Lemma Exists_flat_map A B P ls (f : A -> list B) :
Exists P (flat_map f ls) <-> Exists (fun d => Exists P (f d)) ls.
Proof.
now rewrite flat_map_concat_map, Exists_concat, Exists_map.
Qed.
Lemma Forall_map A B (f : A -> B) P l :
Forall P (map f l) <-> Forall (fun x => P (f x)) l.
Proof.
induction l as [|a l IHl].
- constructor; intros; now constructor.
- constructor; intro H;
(constructor; [exact (Forall_inv H) | apply IHl; exact (Forall_inv_tail H)]).
Qed.
Lemma Forall_concat A P (ls : list (list A)) :
Forall P (concat ls) <-> Forall (Forall P) ls.
Proof.
induction ls as [|l ls IHls].
- constructor; intros; now constructor.
- cbn. rewrite Forall_app. constructor; intro H.
+ constructor; [exact (proj1 H) | apply IHls; exact (proj2 H)].
+ constructor; [exact (Forall_inv H) | apply IHls; exact (Forall_inv_tail H)].
Qed.
Lemma Forall_flat_map A B P ls (f : A -> list B) :
Forall P (flat_map f ls) <-> Forall (fun d => Forall P (f d)) ls.
Proof.
now rewrite flat_map_concat_map, Forall_concat, Forall_map.
Qed.
Lemma exists_Forall A B : forall (P : A -> B -> Prop) l,
(exists k, Forall (P k) l) -> Forall (fun x => exists k, P k x) l.
Proof.
intros P l; induction l as [|a l IHl]; intros [k HF]; constructor; inversion_clear HF.
- now exists k.
- now apply IHl; exists k.
Qed.
Lemma Forall_image A B : forall (f : A -> B) l,
Forall (fun y => exists x, y = f x) l <-> exists l', l = map f l'.
Proof.
intros f l; induction l as [|a l IHl]; split; intros HF.
- exists nil; reflexivity.
- constructor.
- inversion_clear HF as [| ? ? [x Hx] HFtl]; subst.
destruct (proj1 IHl HFtl) as [l' Heq]; subst.
now exists (x :: l').
- destruct HF as [l' Heq].
symmetry in Heq; apply map_eq_cons in Heq.
destruct Heq as (x & tl & ? & ? & ?); subst.
constructor.
+ now exists x.
+ now apply IHl; exists tl.
Qed.
Lemma concat_nil_Forall A : forall (l : list (list A)),
concat l = nil <-> Forall (fun x => x = nil) l.
Proof.
intro l; induction l as [|a l IHl]; simpl; split; intros Hc; auto.
- apply app_eq_nil in Hc.
constructor; firstorder.
- inversion Hc; subst; simpl.
now apply IHl.
Qed.
Lemma in_flat_map_Exists A B : forall (f : A -> list B) x l,
In x (flat_map f l) <-> Exists (fun y => In x (f y)) l.
Proof.
intros f x l; rewrite in_flat_map.
split; apply Exists_exists.
Qed.
Lemma notin_flat_map_Forall A B : forall (f : A -> list B) x l,
~ In x (flat_map f l) <-> Forall (fun y => ~ In x (f y)) l.
Proof.
intros f x l; rewrite Forall_Exists_neg.
apply not_iff_compat, in_flat_map_Exists.
Qed.
Section Forall2.
(** [Forall2]: stating that elements of two lists are pairwise related. *)
Variables A B : Type.
Variable R : A -> B -> Prop.
Inductive Forall2 : list A -> list B -> Prop :=
| Forall2_nil : Forall2 [] []
| Forall2_cons : forall x y l l',
R x y -> Forall2 l l' -> Forall2 (x::l) (y::l').
#[local]
Hint Constructors Forall2 : core.
Theorem Forall2_refl : Forall2 [] [].
Proof. intros; apply Forall2_nil. Qed.
Theorem Forall2_app_inv_l : forall l1 l2 l',
Forall2 (l1 ++ l2) l' ->
exists l1' l2', Forall2 l1 l1' /\ Forall2 l2 l2' /\ l' = l1' ++ l2'.
Proof.
intro l1; induction l1 as [|a l1 IHl1]; intros l2 l' H.
exists [], l'; auto.
simpl in H; inversion H as [|? y ? ? ? H4]; subst; clear H.
apply IHl1 in H4 as (l1' & l2' & Hl1 & Hl2 & ->).
exists (y::l1'), l2'; simpl; auto.
Qed.
Theorem Forall2_app_inv_r : forall l1' l2' l,
Forall2 l (l1' ++ l2') ->
exists l1 l2, Forall2 l1 l1' /\ Forall2 l2 l2' /\ l = l1 ++ l2.
Proof.
intro l1'; induction l1' as [|a l1' IHl1']; intros l2' l H.
exists [], l; auto.
simpl in H; inversion H as [|x ? ? ? ? H4]; subst; clear H.
apply IHl1' in H4 as (l1 & l2 & Hl1 & Hl2 & ->).
exists (x::l1), l2; simpl; auto.
Qed.
Theorem Forall2_app : forall l1 l2 l1' l2',
Forall2 l1 l1' -> Forall2 l2 l2' -> Forall2 (l1 ++ l2) (l1' ++ l2').
Proof.
intros l1 l2 l1' l2' H H0. induction l1 in l1', H, H0 |- *; inversion H; subst; simpl; auto.
Qed.
End Forall2.
#[global]
Hint Constructors Forall2 : core.
Section ForallPairs.
(** [ForallPairs] : specifies that a certain relation should
always hold when inspecting all possible pairs of elements of a list. *)
Variable A : Type.
Variable R : A -> A -> Prop.
Definition ForallPairs l :=
forall a b, In a l -> In b l -> R a b.
(** [ForallOrdPairs] : we still check a relation over all pairs
of elements of a list, but now the order of elements matters. *)
Inductive ForallOrdPairs : list A -> Prop :=
| FOP_nil : ForallOrdPairs nil
| FOP_cons : forall a l,
Forall (R a) l -> ForallOrdPairs l -> ForallOrdPairs (a::l).
#[local]
Hint Constructors ForallOrdPairs : core.
Lemma ForallOrdPairs_In : forall l,
ForallOrdPairs l ->
forall x y, In x l -> In y l -> x=y \/ R x y \/ R y x.
Proof.
induction 1.
inversion 1.
simpl; destruct 1; destruct 1; subst; auto.
right; left. apply -> Forall_forall; eauto.
right; right. apply -> Forall_forall; eauto.
Qed.
(** [ForallPairs] implies [ForallOrdPairs]. The reverse implication is true
only when [R] is symmetric and reflexive. *)
Lemma ForallPairs_ForallOrdPairs l: ForallPairs l -> ForallOrdPairs l.
Proof.
induction l as [|a l IHl]; auto. intros H.
constructor.
apply <- Forall_forall. intros; apply H; simpl; auto.
apply IHl. red; intros; apply H; simpl; auto.
Qed.
Lemma ForallOrdPairs_ForallPairs :
(forall x, R x x) ->
(forall x y, R x y -> R y x) ->
forall l, ForallOrdPairs l -> ForallPairs l.
Proof.
intros Refl Sym l Hl x y Hx Hy.
destruct (ForallOrdPairs_In Hl _ _ Hx Hy); subst; intuition.
Qed.
End ForallPairs.
Section Repeat.
Variable A : Type.
Fixpoint repeat (x : A) (n: nat ) :=
match n with
| O => []
| S k => x::(repeat x k)
end.
Theorem repeat_length x n:
length (repeat x n) = n.
Proof.
induction n as [| k Hrec]; simpl; rewrite ?Hrec; reflexivity.
Qed.
Theorem repeat_spec n x y:
In y (repeat x n) -> y=x.
Proof.
induction n as [|k Hrec]; simpl; destruct 1; auto.
Qed.
Lemma repeat_cons n a :
a :: repeat a n = repeat a n ++ (a :: nil).
Proof.
induction n as [|n IHn]; simpl.
- reflexivity.
- f_equal; apply IHn.
Qed.
Lemma repeat_app x n m :
repeat x (n + m) = repeat x n ++ repeat x m.
Proof.
induction n as [|n IHn]; simpl; auto.
now rewrite IHn.
Qed.
Lemma repeat_eq_app x n l1 l2 :
repeat x n = l1 ++ l2 -> repeat x (length l1) = l1 /\ repeat x (length l2) = l2.
Proof.
revert n; induction l1 as [|a l1 IHl1]; simpl; intros n Hr; subst.
- repeat split; now rewrite repeat_length.
- destruct n; inversion Hr as [ [Heq Hr0] ]; subst.
now apply IHl1 in Hr0 as [-> ->].
Qed.
Lemma repeat_eq_cons x y n l :
repeat x n = y :: l -> x = y /\ repeat x (pred n) = l.
Proof.
intros Hr.
destruct n; inversion_clear Hr; auto.
Qed.
Lemma repeat_eq_elt x y n l1 l2 :
repeat x n = l1 ++ y :: l2 -> x = y /\ repeat x (length l1) = l1 /\ repeat x (length l2) = l2.
Proof.
intros Hr; apply repeat_eq_app in Hr as [Hr1 Hr2]; subst.
apply repeat_eq_cons in Hr2; intuition.
Qed.
Lemma Forall_eq_repeat x l :
Forall (eq x) l -> l = repeat x (length l).
Proof.
induction l as [|a l IHl]; simpl; intros HF; auto.
inversion_clear HF as [ | ? ? ? HF']; subst.
now rewrite (IHl HF') at 1.
Qed.
Hypothesis decA : forall x y : A, {x = y}+{x <> y}.
Lemma count_occ_repeat_eq x y n : x = y -> count_occ decA (repeat y n) x = n.
Proof.
intros ->.
induction n; cbn; auto.
destruct (decA y y); auto.
exfalso; intuition.
Qed.
Lemma count_occ_repeat_neq x y n : x <> y -> count_occ decA (repeat y n) x = 0.
Proof.
intros Hneq.
induction n; cbn; auto.
destruct (decA y x); auto.
exfalso; intuition.
Qed.
Lemma count_occ_unique x l : count_occ decA l x = length l -> l = repeat x (length l).
Proof.
induction l as [|h l]; cbn; intros Hocc; auto.
destruct (decA h x).
- f_equal; intuition.
- assert (Hb := count_occ_bound decA x l).
rewrite Hocc in Hb.
exfalso; apply (Nat.nle_succ_diag_l _ Hb).
Qed.
Lemma count_occ_repeat_excl x l :
(forall y, y <> x -> count_occ decA l y = 0) -> l = repeat x (length l).
Proof.
intros Hocc.
apply Forall_eq_repeat, Forall_forall; intros z Hin.
destruct (decA z x) as [Heq|Hneq]; auto.
apply Hocc, count_occ_not_In in Hneq; intuition.
Qed.
Lemma count_occ_sgt l x : l = x :: nil <->
count_occ decA l x = 1 /\ forall y, y <> x -> count_occ decA l y = 0.
Proof.
split.
- intros ->; cbn; split; intros; destruct decA; subst; intuition.
- intros [Heq Hneq].
apply count_occ_repeat_excl in Hneq.
rewrite Hneq, count_occ_repeat_eq in Heq; trivial.
now rewrite Heq in Hneq.
Qed.
Lemma nth_repeat a m n :
nth n (repeat a m) a = a.
Proof.
revert n. induction m as [|m IHm].
- now intros [|n].
- intros [|n]; [reflexivity|exact (IHm n)].
Qed.
Lemma nth_error_repeat a m n :
n < m -> nth_error (repeat a m) n = Some a.
Proof.
intro Hnm. rewrite (nth_error_nth' _ a).
- now rewrite nth_repeat.
- now rewrite repeat_length.
Qed.
End Repeat.
Lemma repeat_to_concat A n (a:A) :
repeat a n = concat (repeat [a] n).
Proof.
induction n as [|n IHn]; simpl.
- reflexivity.
- f_equal; apply IHn.
Qed.
(** Sum of elements of a list of [nat]: [list_sum] *)
Definition list_sum l := fold_right plus 0 l.
Lemma list_sum_app : forall l1 l2,
list_sum (l1 ++ l2) = list_sum l1 + list_sum l2.
Proof.
intro l1; induction l1 as [|a l1 IHl1]; intros l2; [ reflexivity | ].
simpl; rewrite IHl1.
apply Nat.add_assoc.
Qed.
(** Max of elements of a list of [nat]: [list_max] *)
Definition list_max l := fold_right max 0 l.
Lemma list_max_app : forall l1 l2,
list_max (l1 ++ l2) = max (list_max l1) (list_max l2).
Proof.
intro l1; induction l1 as [|a l1 IHl1]; intros l2; [ reflexivity | ].
now simpl; rewrite IHl1, Nat.max_assoc.
Qed.
Lemma list_max_le : forall l n,
list_max l <= n <-> Forall (fun k => k <= n) l.
Proof.
intro l; induction l as [|a l IHl]; simpl; intros n; split.
- now intros.
- intros. now apply Nat.le_0_l.
- intros [? ?] %Nat.max_lub_iff. now constructor; [|apply IHl].
- intros H. apply Nat.max_lub_iff.
constructor; [exact (Forall_inv H)|apply IHl; exact (Forall_inv_tail H)].
Qed.
Lemma list_max_lt : forall l n, l <> nil ->
list_max l < n <-> Forall (fun k => k < n) l.
Proof.
intro l; induction l as [|a l IHl]; simpl; intros n Hnil; split; intros H; intuition.
- destruct l.
+ repeat constructor.
now simpl in H; rewrite Nat.max_0_r in H.
+ apply Nat.max_lub_lt_iff in H.
now constructor; [ | apply IHl ].
- destruct l; inversion_clear H as [ | ? ? Hlt HF ].
+ now simpl; rewrite Nat.max_0_r.
+ apply IHl in HF.
* now apply Nat.max_lub_lt_iff.
* intros Heq; inversion Heq.
Qed.
(** * Inversion of predicates over lists based on head symbol *)
Ltac is_list_constr c :=
match c with
| nil => idtac
| (_::_) => idtac
| _ => fail
end.
Ltac invlist f :=
match goal with
| H:f ?l |- _ => is_list_constr l; inversion_clear H; invlist f
| H:f _ ?l |- _ => is_list_constr l; inversion_clear H; invlist f
| H:f _ _ ?l |- _ => is_list_constr l; inversion_clear H; invlist f
| H:f _ _ _ ?l |- _ => is_list_constr l; inversion_clear H; invlist f
| H:f _ _ _ _ ?l |- _ => is_list_constr l; inversion_clear H; invlist f
| _ => idtac
end.
(** * Exporting hints and tactics *)
Global Hint Rewrite
rev_involutive (* rev (rev l) = l *)
rev_unit (* rev (l ++ a :: nil) = a :: rev l *)
map_nth (* nth n (map f l) (f d) = f (nth n l d) *)
map_length (* length (map f l) = length l *)
seq_length (* length (seq start len) = len *)
app_length (* length (l ++ l') = length l + length l' *)
rev_length (* length (rev l) = length l *)
app_nil_r (* l ++ nil = l *)
: list.
Ltac simpl_list := autorewrite with list.
Ltac ssimpl_list := autorewrite with list using simpl.
(* begin hide *)
(* Compatibility notations after the migration of [list] to [Datatypes] *)
Notation list := list (only parsing).
Notation list_rect := list_rect (only parsing).
Notation list_rec := list_rec (only parsing).
Notation list_ind := list_ind (only parsing).
Notation nil := nil (only parsing).
Notation cons := cons (only parsing).
Notation length := length (only parsing).
Notation app := app (only parsing).
(* Compatibility Names *)
Notation tail := tl (only parsing).
Notation head := hd_error (only parsing).
Notation head_nil := hd_error_nil (only parsing).
Notation head_cons := hd_error_cons (only parsing).
Notation ass_app := app_assoc (only parsing).
Notation app_ass := app_assoc_reverse (only parsing).
Notation In_split := in_split (only parsing).
Notation In_rev := in_rev (only parsing).
Notation In_dec := in_dec (only parsing).
Notation distr_rev := rev_app_distr (only parsing).
Notation rev_acc := rev_append (only parsing).
Notation rev_acc_rev := rev_append_rev (only parsing).
Notation AllS := Forall (only parsing). (* was formerly in TheoryList *)
#[global]
Hint Resolve app_nil_end : datatypes.
(* end hide *)
(* Unset Universe Polymorphism. *)
|