File: lagrange.v

package info (click to toggle)
coq-corn 8.20.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,216 kB
  • sloc: python: 112; haskell: 69; makefile: 39; sh: 4
file content (4834 lines) | stat: -rw-r--r-- 122,436 bytes parent folder | download | duplicates (4)
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
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
Require Import ssreflect CSetoids CSetoidFun 
  CFields CPolynomials Program  
  Omega Equivalence Morphisms 
  Wf Morphisms CRings  
  CRing_Homomorphisms Rational Setoid 
  CPoly_NthCoeff CPoly_Degree CReals Intervals
  CauchySeq IntervalFunct MoreIntervals
  MoreFunctions Composition.
Require Import seq.

Open Scope program_scope.

(**
 * The sq(uash) and unsq(uash) tricks are just
 * hacks because we are dealing with a flaw in
 * the Program Fixpoint construct. Bas and other
 * people are working on this. It is assumed that
 * this construct will not be required in the
 * final version of this proof.
 *)
Inductive sq (A : Type) : Prop := 
  insq : A -> (sq A).

Axiom unsq : forall A : Type, (sq A) -> A.

(**
 * A 'fresh' sequence is a sequence where no two 
 * elements are the same. Therefore, we know that 
 * for elements a, b in such a sequence it holds 
 * that a - b =/= 0. The following section deals 
 * with fresh sequences. 
 *)
Section FreshSeq.

Variable A : CSetoid.

(**
 * Freshness of a sequence relative to one
 * element. 
 *)
Lemma fresh (s : seq A) : A -> Type.
intro s. 
induction s as [a|b s fresh_s].
exact (fun _ => True).
exact (fun a => (b [#] a) and (fresh_s a)).
Defined.

(**
 * 'Squashed' version of the freshness property.
 * For more information, read the Type vs. Prop
 * discussion above.
 *)
Definition sqfresh (s : seq A) (a : A) := 
  (sq (fresh s a)).

(**
 * A fresh sequence is a sequence where every
 * two elements lie apart. It is similar to the
 * normal sequence type. 
 *)
Inductive fresh_seq : seq A -> Prop :=
  | fresh_nil : fresh_seq nil
  | fresh_cons : forall x s, 
      sqfresh s x -> fresh_seq s -> 
      fresh_seq (x :: s).

(**
 * If we an element a is fresh (relative to a 
 * sequence s), then a is also fresh relative
 * to any subsequence of s.
 *)
Lemma take_fresh : forall (n : nat) 
  (s : seq A) (a : A), fresh s a -> 
  fresh (take n s) a.
Proof.
  intro n; induction n.  
  intros s a H; rewrite take0; simpl; auto.
  intros s a H; destruct s; simpl; auto.
  simpl; split.
  by inversion H.
  apply IHn; by inversion H.
Defined.

(**
 * Any subsequence we take from the beginning of a fresh
 * sequence is still fresh.
 *)
Lemma take_fresh_seq : forall (n : nat) 
  (s : seq A), fresh_seq s -> 
  fresh_seq (take n s).
Proof.
  intro n; induction n.
  intros s H; simpl; rewrite take0; exact fresh_nil.
  intros s H; destruct s; simpl; auto.
  simpl; apply fresh_cons.
  unfold sqfresh; apply insq; apply take_fresh.
  apply unsq; by inversion H.
  apply IHn; by inversion H.
Defined.

(**
 * If an element is fresh with respect to a certain
 * sequence, it results in a fresh sequence if we add
 * this element to the rear of this sequence.
 *)
Lemma rcons_fresh : forall (s : seq A) 
  (a t : A), fresh (t :: s) a -> fresh (rcons s t) a.
Proof.
  intros s a t H; induction s.
  simpl; by inversion H.
  simpl; inversion H; inversion X0; split.
  auto.
  apply IHs; simpl; by split.
Defined.

(**
 * The inverse of the previous theorem. If an element
 * added to the rear of a sequence results in a fresh
 * sequence, we might instead add this element in the
 * front and still come up with a fresh sequence.
 *)
Lemma fresh_rcons : forall (s : seq A)
  (a t : A), fresh (rcons s t) a -> fresh (t :: s) a.
Proof.
  intros s a t H; induction s.
  simpl; by inversion H.
  simpl; simpl in H; split.
  apply IHs; by inversion H.
  split.
  by inversion H.
  apply IHs; by inversion H.
Defined.

(**
 * Freshness remains if we reverse a sequence.
 *)
Lemma rev_fresh : forall (s : seq A) (a : A),
  fresh s a -> fresh (rev s) a.
Proof.
  intros s a H; induction s.
  auto. 
  rewrite rev_cons; apply rcons_fresh; simpl; split.
  by inversion H.
  apply IHs; by inversion H.
Defined.

(**
 * If we have a fresh sequence with one element on front,
 * the sequence remains fresh if we add this element to
 * the rear.
 *)
Lemma rcons_fresh_seq : forall (s : seq A) 
  (a : A), fresh_seq (a :: s) -> 
  fresh_seq (rcons s a).
Proof.
  intros s a H; induction s.
  auto.
  simpl; apply fresh_cons.
  unfold sqfresh; inversion H; apply insq.
  apply rcons_fresh; simpl; unfold sqfresh in H2.
  assert (fresh (t :: s) a).
  by apply unsq.
  inversion X.
  split.
  algebra.
  assert (sq (fresh s t)).
  inversion H3; auto.
  by apply unsq in H4.
  apply IHs; inversion H; apply fresh_cons.
  inversion H2; inversion X; unfold sqfresh; by apply insq.
  by inversion H3.
Defined.

(**
 * If a sequence with a specific element on its rear is
 * fresh, the sequence is still fresh if we would have
 * added this element in the front.
 *)
Lemma fresh_seq_rcons : forall (s : seq A) 
  (a : A), fresh_seq (rcons s a) -> 
  fresh_seq (a :: s).
Proof.
  intros; induction s. auto.
  simpl in H; inversion H; apply fresh_cons.
  unfold sqfresh; apply insq.
  assert (fresh (a :: s) t); apply fresh_rcons.
  unfold sqfresh in H2; by apply unsq in H2.
  simpl; simpl in X; inversion X; apply unsq; inversion H.
  assert (fresh_seq (a :: s)). 
  by apply IHs.
  apply insq; apply rcons_fresh; simpl; split.
  algebra. 
  apply unsq; inversion H8. 
  unfold sqfresh in H11; apply unsq in H11; by apply insq.
  apply fresh_cons; unfold sqfresh in H2; apply unsq in H2.
  apply fresh_rcons in H2; inversion H2; unfold sqfresh.
  by apply insq.
  assert (fresh_seq (a :: s)).
  by apply IHs.
  by inversion H4.
Defined.

(**
 * If a sequence is fresh, it remains fresh if we completely
 * reverse it.
 *)
Lemma rev_fresh_seq : forall (s : seq A),
  fresh_seq s -> fresh_seq (rev s).
Proof.
  intros; induction s. auto.
  rewrite rev_cons; apply rcons_fresh_seq; apply fresh_cons.
  inversion H; unfold sqfresh; apply insq; apply rev_fresh.
  unfold sqfresh in H2; by apply unsq.
  apply IHs; by inversion H.
Defined.

Hint Constructors fresh_seq.
  
End FreshSeq.

(**
 * The definitions and lemmas for Newton 
 * polynomials hold for polynomials over an
 * arbitrary field K. We will later confine
 * this K to the real numbers R.
 *)

Section NewtonPolynomials.

Variable K : CField.
Variable f : K -> K.

(**
 * The definition of the divided differences
 * follows here. This is the function f [..]
 * as described in the paper written by Bas.
 * To avoid any confusion, the notation f ()
 * is used for the polynomial subject to
 * interpolation.
 *)
Program Fixpoint dd (s : seq K) (P : fresh_seq K s) 
  {measure (size s)} : K :=
match s with
| nil => Zero
| (a :: nil) => (f a)
| (a :: b :: s') =>
    ((dd (a :: s') _) [-] (dd (b :: s') _) [/]
    (a [-] b) [//] _)
end.

Next Obligation.
apply fresh_cons; inversion P; inversion H1.
inversion X; unfold sqfresh; by apply insq.
inversion P; by inversion H2.
Qed.

Next Obligation.
by inversion P.
Qed.

Next Obligation.
apply minus_ap_zero; apply unsq; inversion P; inversion H1.
inversion X; apply insq; algebra.
Qed.


(**
 * Now that we have solved all obligations for
 * the definition of divided differences, we  
 * continue with our definitions of the functions
 * N, alpha and eta.
 *)
Variable s : seq K.
Variable k : nat.
Hypothesis fresh_s : fresh_seq K s.

(**
 * This definition still uses the bigopsClass, but it will be
 * replaced by a fold until the problems in bigopsClass are
 * resolved. This definition of eta corresponds to the 
 * definition of n_j(x) in the paper written by Bas.
 *
 * TODO: Replace bigopsClass definitions by folds
 *)
Require Export bigopsClass.
Definition eta (j : nat) : cpoly_cring K :=
  \big[(cpoly_mult_cs K)/(cpoly_one K)]_(x_i <- take j s)
    (cpoly_linear _ ([--] x_i) (cpoly_one _)).

(**
 * This definition corresponds to the definition of a_j in
 * the paper. It is basically a direct call to the definition
 * of divided differences using the vector x_j, ..., x_0.
 *)
Definition alpha (j : nat) : K :=
  dd (rev (take (j + 1) s))
    ((rev_fresh_seq K (take (j + 1) s))
      (take_fresh_seq K (j + 1) s fresh_s)).

(**
 * This is the definition of N from the paper. The mkseq
 * construct creates an increasing sequence. 
 * 
 * TODO: Replace bigopsClass definitions by folds.
 *)
Definition N : cpoly_cring K :=
  \big[cpoly_plus_cs K/(cpoly_zero K)]_(j <- 
    (mkseq (fun x => x) (k + 1)) | (fun x => true) j)
     (_C_ (alpha j) [*] (eta j)).

End NewtonPolynomials.

Section BigopsTheory.

Variable K : CField.
Variable f : K -> K.

(**
 * This is proof independence of divided differences with
 * respect to freshness. 
 *)
Lemma dd_indep : forall (l1 l2 : seq K) (P1 : fresh_seq K l1)
  (P2 : fresh_seq K l2), l1 = l2 -> 
    dd K f l1 P1 [=] dd K f l2 P2.
Proof.
  intros.
  unfold dd.
  replace (existT (fun s : seq K => fresh_seq K s) l2 P2) with
    (existT (fun s : seq K => fresh_seq K s) l1 P1).
  reflexivity.
  by apply subsetT_eq_compat.
Qed.

(**
 * Equality of CPolynomials over a field K is reflexive,
 * symmetric and transitive according to the following 
 * three type class instances.
 *)
Instance cpoly_eq_refl : Reflexive (cpoly_eq K).
unfold Reflexive; by reflexivity. Qed.

Instance cpoly_eq_symm : Symmetric (cpoly_eq K).
unfold Symmetric; by symmetry. Qed.

Instance cpoly_eq_trans : Transitive (cpoly_eq K).
unfold Transitive; intros x y z; by transitivity y. Qed.

Instance eqv_cpoly : Equivalence (cpoly_eq K).

Instance eqv_K : Equivalence (@st_eq K).

(**
 * TODO: This definition is not required because we have
 * an equivalent construct in the type class system.
 *)
Add Parametric Relation : (cpoly_cring K) (cpoly_eq K)
  reflexivity proved by cpoly_eq_refl
  symmetry proved by cpoly_eq_symm
  transitivity proved by cpoly_eq_trans
  as cpeq.

(**
 * This is meant to prove that addition of polynomials 
 * is a morphism with respect to equality. However, I
 * cannot complete the proof because it somehow seems
 * that this exact morphism is required to finish the
 * proof.
 *
 * TODO: Fix this proof.
 *)
Instance morph_cpoly : Proper ((cpoly_eq K) ==> (cpoly_eq K) ==>
  (cpoly_eq K)) (cpoly_plus_cs K).
Admitted.

(**
 * Multiplication of polynomials is also a morphism with
 * respect to equality. However, the same problem as in
 * the previous proof arises here.
 * 
 * TODO: Complete this proof.
 *)
Instance morph_cpoly_mult : Proper ((cpoly_eq K) ==> (cpoly_eq K) ==> 
  (cpoly_eq K)) (cpoly_mult_cs K).
Admitted.

(**
 * Multiplication in an arbitrary field K is a morphism with
 * respect to the standard equality defined in K.
 * 
 * TODO: Are definitions like these really required? Aren't
 * they already defined in the CoRN libraries?
 *)
Instance morph_K_mult : Proper ((@st_eq K) ==> (@st_eq K) ==> (@st_eq K))
  cr_mult.
unfold Proper; unfold respectful.
intros x y H x0 y0 H0; rewrite H H0; reflexivity.
Qed.

(**
 * Addition in a field K is a morphism with respect to the
 * standard equality defined on K.
 *)
Instance morph_K_plus : Proper ((@st_eq K) ==> (@st_eq K) ==> (@st_eq K))
  csg_op.
unfold Proper; unfold respectful.
intros x y H x0 y0 H0; rewrite H H0; reflexivity.
Qed.

(**
 * Addition of polynomials is associative.
 *
 * TODO: As OperationClasses is no longer compiling, this
 * has to be replaced with another construct.
 *)
Instance assoc_cpoly : @OperationClasses.associative (cpoly_cring K) 
  (cpoly_eq K) eqv_cpoly (cpoly_plus_cs K).
unfold OperationClasses.associative.
intros x y z; red.
set cpoly_plus_associative.
unfold associative in a; simpl in a; apply a.
Qed.

(**
 * Multiplication of polynomials is associatve.
 * 
 * TODO: Fix usage of OperationClasses.
 *)
Instance assoc_cpoly_mult : @OperationClasses.associative (cpoly_cring K) 
  (cpoly_eq K) eqv_cpoly (cpoly_mult_cs K).
unfold OperationClasses.associative.
intros x y z; red.
set cpoly_mult_assoc.
unfold associative in a; simpl in a; apply a.
Qed.

(**
 * Multiplication is associative in any field K
 *
 * TODO: This should be replaced by a standard lemma from
 * the CoRN libraries.
 *)
Instance assoc_K_mult : @OperationClasses.associative K
  (@st_eq K) eqv_K cr_mult.
unfold OperationClasses.associative.
intros x y z; red; algebra.
Qed.

(**
 * Addition is associative in any field K
 *
 * TODO: This is very probably already somewhere in the
 * libraries.
 *)
Instance assoc_K_plus : @OperationClasses.associative K
  (@st_eq K) eqv_K csg_op.
unfold OperationClasses.associative.
intros x y z; red; algebra.
Qed.

(**
 * The zero-polynomial is a left-unit element with respect
 * to addition.
 *
 * TODO: Fix OperationClasses usage.
 *)
Instance left_unit_cpoly : @OperationClasses.left_unit (cpoly_cring K) 
  (cpoly_eq K) eqv_cpoly (cpoly_plus_cs K) (cpoly_zero K).
intro x; red.
simpl; reflexivity.
Qed.

(**
 * The one-polynomial is a left-unit element with respect
 * to multiplication.
 *
 * TODO: Fix OperationClasses usage.
 *)
Instance left_unit_cpoly_mult : @OperationClasses.left_unit (cpoly_cring K) 
  (cpoly_eq K) eqv_cpoly (cpoly_mult_cs K) (cpoly_one K).
intro x; red.
rewrite cpoly_mult_commutative.
rewrite cpoly_mult_one.
reflexivity.
Qed.

(**
 * TODO: Deprecated, do not use OperationClasses.
 *)
Instance left_unit_K_mult : @OperationClasses.left_unit K 
  (@st_eq K) eqv_K cr_mult (One:K).
intro x; red; algebra.
Qed.

(**
 * TODO: Deprecated, do not use Operationclasses.
 *)
Instance left_unit_K_plus : @OperationClasses.left_unit K
  (@st_eq K) eqv_K csg_op (Zero:K).
intro x; red; algebra.
Qed.

(**
 * The zero-polynomial is a right-unit element with respect
 * to to addition of polynomials.
 * 
 * TODO: Get rid of OperationClasses code.
 *)
Instance right_unit_cpoly : @OperationClasses.right_unit (cpoly_cring K)
  (cpoly_eq K) eqv_cpoly (cpoly_plus_cs K) (cpoly_zero K).
intro x; red.
rewrite cpoly_plus_commutative; simpl; reflexivity.
Qed.

(**
 * The one-polynomial is a right-unit element with respect
 * to multiplication of polynomials.
 *
 * TODO: Get rid of OperationClasses code.
 *)
Instance right_unit_cpoly_mult : @OperationClasses.right_unit (cpoly_cring K)
  (cpoly_eq K) eqv_cpoly (cpoly_mult_cs K) (cpoly_one K).
intro x; red.
rewrite cpoly_mult_one; reflexivity.
Qed.

(**
 * TODO: Deprecated, do not use OperationClasses code.
 *)
Instance right_unit_K_mult : @OperationClasses.right_unit K
  (@st_eq K) eqv_K cr_mult (One:K).
intro x; red; algebra.
Qed.

(**
 * TODO: Deprecated, do not use OperationClasses code.
 *)
Instance right_unit_K_plus : @OperationClasses.right_unit K
  (@st_eq K) eqv_K csg_op (Zero:K).
intro x; red; algebra.
Qed.

(**
 * Application of polynomials (over K) is a morphism with 
 * respect to the standard equality defined on K.
 *
 * TODO: Fix this proof. To be honest, I have no clue why
 * this cannot be done. Will take a look at it later.
 *)
Add Parametric Morphism : (@cpoly_apply K) with
 signature (@cpoly_eq K) ==> (@st_eq K) ==> (@st_eq K) as cpoly_apply_mor.
Admitted.

(**
 * Multiplication of polynomials is a morphism with respect
 * to the equality defined on polynomials.
 *
 * TODO: Replace with type class instance.
 *)
Add Parametric Morphism : (@cpoly_mult_cs K) with
 signature (@cpoly_eq K) ==> (@cpoly_eq K) ==> (@cpoly_eq K) as cpoly_mult_mor.
intros x y H x0 y0 H0.
rewrite H; rewrite H0; reflexivity.
Qed.

(**
 * TODO: Replace with type class instance.
 *)
Add Parametric Morphism : (@polyconst K) with
  signature (@st_eq K) ==> (@cpoly_eq K) as cpoly_const_mor.
intros x y H; rewrite H; reflexivity.
Qed.

(**
 * Multiplication of a polynomial (over any field K) and an
 * element from K is invariant under both equality over poly(K)
 * and K.
 *
 * TODO: Fix this proof. Seems to mutually depend on a previous
 * morphism for polynomial-multiplication.
 *)
Add Parametric Morphism : (@cpoly_mult_cr_cs K) with
  signature (@cpoly_eq K) ==> (@st_eq K) ==> (cpoly_eq K) as cpoly_mult_cr_mor.
intros x y H x0 y0 H0.
Admitted.

(**
 * TODO: Replace with corresponding type class instance.
 *)
Add Parametric Morphism : (@cg_minus K) with
  signature (@st_eq K) ==> (@st_eq K) ==> (@st_eq K) as cg_minus_mor.
intros x y H x0 y0 H0; rewrite H; rewrite H0; reflexivity.
Qed.

(**
 * Getting the nth coefficient of a polynomial is a morphism
 * with respect to equality over nat.
 *
 * TODO: Fix this proof. Should not be very difficult. Will
 * take a look at it later.
 *)
Add Parametric Morphism : (@nth_coeff K) with
  signature (@eq nat) ==> (@cpoly_eq K) ==> (@st_eq K) as nth_coeff_mor.
intros y x y0 H.
Admitted.

(**
 * TODO: Replace with corresponding type class instance
 *)
Add Parametric Morphism : (@csg_op K) with
  signature (@st_eq K) ==> (@st_eq K) ==> (@st_eq K) as csg_op_mor.
intros x y H x0 y0 H0; rewrite H H0; reflexivity.
Qed.

(**
 * TODO: Replace with corresponding type class instance
 *)
Add Parametric Morphism : (@cg_inv K) with
  signature (@st_eq K) ==> (@st_eq K) as cg_inv_mor.
intros x y H; rewrite H; reflexivity.
Qed.

(**
 * The equality on K can be continued to an equality on
 * polynomials. However, it was not immediately clear how
 * to prove this. 
 *
 * TODO: Replace with corresponding definition from the
 * CoRN libraries.
 *)
Add Parametric Morphism : (@cpoly_linear K) with
  signature (@st_eq K) ==> (@cpoly_eq K) ==> (@cpoly_eq K) as cpoly_lin_mor.
intros x y H x0 y0 H0.
Admitted.

(**
 * I have not been able to get rings to work for polynomials.
 * 
 * TODO: Fix this because it makes many proofs easier to
 * understand.
 * 
 * Add Ring cpolyk_th : (CRing_Ring (cpoly_cring K)).
 * Add Ring cring_K : (CRing_Ring K).
 *
 *)

(**
 * If a bigops-expression results in a polynomial, and if
 * this expression is therefore applied to a particular 
 * value, the application results in the same value as if
 * the application was done inside the bigops-expression.
 *
 * TODO: This should be more general.
 * TODO: Replace the bigops expression with a corresponding
 * fold.
 *)
Lemma apply_bigops : forall (r : seq K) F x,
  (\big[cpoly_mult_cs K/cpoly_one K]_(i <- r) F i) ! x [=]
  \big[cr_mult/(One:K)]_(i <- r) ((F i) ! x).
Proof.
  intros r F x; destruct r; simpl.
  rewrite cring_mult_zero; algebra.
  induction r.
  simpl; rewrite mult_one; rewrite cpoly_mult_one. 
  reflexivity.
  repeat rewrite big_cons; simpl in IHr; rewrite mult_assoc.
  rewrite (mult_commutes K (cpoly_apply K (F s) x)
    (cpoly_apply K (F t) x)).
  rewrite <- mult_assoc; rewrite <- IHr.
  set (@mult_apply); simpl in s0; rewrite <- s0.
  rewrite cpoly_mult_fast_equiv.
  set (@cpoly_mult_assoc); unfold CSetoids.associative in a.
  simpl in a; repeat rewrite a.
  rewrite (cpoly_mult_commutative K (F s) (F t)).
  reflexivity.
Qed.
  
(**
 * If we take a subsequence from (the start of) 
 * another sequence, it does not matter if this
 * sequence was already the result of a 'take' operation.
 *)
Lemma take_nest_redun : forall (n m : nat)
  (s : seq K), m <= n -> m <= size s ->
  take m (take n s) = take m s.
Proof.
  intro n; induction n.
  intros m s H H0; rewrite take0; inversion H.
  repeat rewrite take0; reflexivity.
  intros m s H H0. destruct s. simpl; reflexivity.
  assert (take (S n) (s :: s0) = s :: take n s0) by auto.
  rewrite H1; destruct m. simpl; reflexivity.
  simpl; rewrite IHn. reflexivity. omega.
  inversion H0. auto. omega.
Qed.

(**
 * This lemma effectively says that:
 *
 * x_0, x_1, ..., x_(k+1) = x_0, x_1, ..., x_i, x_(i+1), 
 * ..., x_(k+1)
 *
 * TODO: Perhaps all these takes, nths and drops can be
 * set up a bit more clearer. 
 *)
Lemma take_nth_drop : forall (i k : nat) (s : seq K),
  i <= k -> k < size s -> 
  take (S k) s = take i s ++ (nth Zero s i) ::
    (take (k - i) (drop (i + 1) s)).
Proof.
  intro i; induction i.
  intros k s H H0; destruct s.
  inversion H0; simpl.
  replace (k - 0) with k by omega; simpl.
  rewrite drop0; reflexivity.
  intros k s H H0; destruct s.
  inversion H0; simpl.
  replace (k - S i) with ((k - 1) - i) by omega.
  simpl; rewrite <- IHi.
  replace (S (k - 1)) with k by omega; reflexivity.
  omega. inversion H0.
  assert (1 <= k) by omega; omega.
  assert (1 <= k) by omega; omega.
Qed.

(**
 * This lemma asserts that the repeated multiplication 
 * of an expression (-x_i) + x_i is equal to zero.
 *)
Lemma lem11a : forall i s k,
  size s > 1 -> k < size s -> i <= k ->
  \big[cr_mult/(One:K)]_(x_i <- take (S k) s)
    (cpoly_linear K [--]x_i (cpoly_one K)) ! (nth Zero s i) [=] Zero.
Proof.
  intros.
  rewrite (@eq_big_idx_seq K (@st_eq K)
    eqv_K (cr_mult) (One:K) morph_K_mult
    right_unit_K_mult (One:K) K 
    (take (S k) s) _ 
    (fun x : K => (cpoly_linear K [--]x (cpoly_one K)) !
    (nth Zero s i)) right_unit_K_mult).
  assert (take (S k) s = (take i s) ++ ((nth Zero s i) :: 
    (take (k - i) (drop (i + 1) s)))).
  apply take_nth_drop.
  omega.
  exact H0.
  rewrite H2.
  rewrite (@big_cat K (@st_eq K) eqv_K
    cr_mult (One:K) morph_K_mult
    assoc_K_mult left_unit_K_mult 
    K (take i s) (nth Zero s i :: take (k - i) (drop (i + 1) s))  
    (fun x : K => true)).
  rewrite big_cons.
  assert ((cpoly_linear K [--](nth Zero s i) (cpoly_one K)) !
    (nth Zero s i) [=] Zero).
  simpl.
  rewrite cring_mult_zero.
  rewrite cm_rht_unit_unfolded.
  rewrite mult_one.
  rewrite cg_lft_inv_unfolded.
  reflexivity.
  rewrite H3.
  rewrite mult_assoc.
  rewrite cring_mult_zero.
  rewrite mult_commutes.
  rewrite cring_mult_zero.
  reflexivity.
  destruct s.
  inversion H.
  simpl; auto.
Qed.

(**
 * If we have a fresh sequence s, it is clear that the kth
 * element is fresh with respect to a subsequence of s.
 *)
Lemma nth_fresh : forall (s : seq K) (k c : nat),
  k < size s -> fresh_seq K s ->
  fresh K (take (k - c) s) (nth Zero s k).
Proof.
  intros.
  induction c.
  assert (k - 0 = k) by omega.
  rewrite H1.
  assert (fresh_seq K (take (k + 1) s)).
  apply take_fresh_seq.
  exact H0.
  cut (take (k + 1) s = take k s ++ [:: nth Zero s k]).
  intro.
  rewrite H3 in H2.
  assert (fresh_seq K ((nth Zero s k) :: 
    take k s)).
  apply fresh_seq_rcons.
  rewrite <- cat_rcons in H2.
  rewrite cats0 in H2.
  exact H2.
  apply unsq.
  inversion H4.
  unfold sqfresh in H7.
  exact H7.
  rewrite <- cat_rcons.
  rewrite cats0.
  rewrite <- take_nth.
  replace (S k) with (k + 1) by omega.
  reflexivity.
  apply (ssrbool.introT (P := S k <= size s)).
  apply ssrnat.leP.
  omega.
  cut (take (k - S c) s = take (k - S c)
    (take (k - c) s)).
  intro.
  rewrite H1.
  apply take_fresh.
  exact IHc.
  rewrite take_nest_redun.
  reflexivity.
  omega.
  omega.
Qed.  

(**
 * If a sequence of elements is fresh, than any two
 * elements from this sequence lie apart. This is not
 * immediately clear from the definition (although it 
 * seems so) because of difficulty with the definition
 * of nth. 
 *
 * TODO: This proof can be made a bit shorter. 
 *)
Lemma ap_fresh_nth : forall (s : seq K)
  (k c : nat), 0 < k -> c < k -> k < size s -> 
  fresh_seq K s ->
  (nth Zero s k) [-] (nth Zero s (k - S c)) [#] Zero.
Proof.
  intros.
  apply minus_ap_zero.
  assert (fresh K (take (k - c) s) (nth Zero s k)).
  apply nth_fresh.
  exact H1.
  exact H2.
  assert (take (k - c) s = take (k - S c) s ++
    [:: nth Zero s (k - S c)]).
  rewrite <- cat_rcons.
  rewrite <- take_nth.
  replace (S (k - S c)) with (k - c) by omega.
  rewrite cats0.
  reflexivity.
  apply (ssrbool.introT (P := S (k - S c) <= size s)).
  apply ssrnat.leP.
  omega.
  rewrite H3 in X.
  rewrite <- cat_rcons in X.
  rewrite cats0 in X.
  apply fresh_rcons in X.
  inversion X.
  algebra.
Qed.

(**
 * This lemma is essentially a one-step reduction in the
 * definition of divided differences. However, it is not
 * as straightforwardly provable as it might seem. 
 *
 * TODO: Fix this proof. 
 *)
Lemma dd_reduce : forall (s : seq K) (a b : K)
  (P1 : fresh_seq K (a :: b :: s)) 
  (P2 : fresh_seq K (a :: s))
  (P3 : fresh_seq K (b :: s))
  (P4 : a [-] b [#] Zero),
  (dd K f (a :: b :: s) P1) = 
  (((dd K f (a :: s) P2) [-] (dd K f (b :: s) P3)) 
  [/] (a [-] b) [//] P4).
Proof.
  intros.
  Admitted.
    
(**
 * This rather complicated lemma states that if we have a
 * one-step reduction for divided differences, we also have
 * an n-step reduction according to a specific pattern.
 *
 * TODO: This lemma is very unreadable. Perhaps this can be
 * made more clear using appropriate syntax elements.
 *)
Lemma dd_reduce_nth : forall (s : seq K) (k c : nat)
  (P : fresh_seq K 
    (nth Zero s k :: rev (take (k - c) s)))
  (Q : fresh_seq K
    (nth Zero s k :: rev (take (k - S c) s)))
  (R : fresh_seq K (rev (take (k - c) s)))
  (X : (nth Zero s k) [-] (nth Zero s (k - S c)) 
    [#] Zero),
  0 < k -> c < k -> k < size s -> fresh_seq K s ->
  (dd K f (nth Zero s k :: rev (take (k - c) s)) P) [=]
  ((dd K f (nth Zero s k :: rev (take (k - S c) s)) Q)[-]
   (dd K f (rev (take (k - c) s)) R)[/]
  (nth Zero s k[-]nth Zero s (k - S c))[//]X).
Proof.
  intros. 
  assert (fresh_seq K  
    (nth Zero s k :: nth Zero s (k - S c) ::
      rev (take (k - S c) s))).
  apply fresh_cons.
  unfold sqfresh.
  apply insq.
  simpl.
  split.
  apply zero_minus_apart.
  algebra.
  apply rev_fresh.
  apply nth_fresh.
  exact H1. 
  exact H2.
  apply fresh_cons.
  unfold sqfresh.
  apply insq.
  assert (forall k, k < size s -> 
     fresh K (rev (take k s))
    (nth Zero s k)).  
  intros.
  apply rev_fresh.
  assert (take k0 s = take (k0 - 0) s).
  replace (k0 - 0) with k0 by omega; reflexivity.
  rewrite H4.
  apply nth_fresh.
  exact H3.
  exact H2.
  apply X0.
  omega.
  apply rev_fresh_seq.
  apply take_fresh_seq.
  exact H2.
  assert (rev (take (k - c) s) = 
    nth Zero s (k - S c) :: rev (take (k - S c) s)).
  assert (k - c = (S (k - S c))) by omega.
  rewrite H4.
  set (@take_nth).
  rewrite (@take_nth K Zero (k - S c) s).
  rewrite rev_rcons; reflexivity.
  apply (ssrbool.introT (P := S (k - S c) <= size s)).
  apply ssrnat.leP.
  omega.
  assert ((dd K f (nth Zero s k :: rev (take (k - c) s)) P) [=] 
    (dd K f [:: nth Zero s k, nth Zero s (k - S c) &
       rev (take (k - S c) s)] H3)).
  apply dd_indep.
  rewrite H4; reflexivity.
  rewrite H5.
  assert (fresh_seq K ((nth Zero s (k - S c)) ::
    rev (take (k - S c) s))).
  inversion H3.
  exact H9.
  assert ((dd K f (rev (take (k - c) s)) R) [=]
    (dd K f (nth Zero s (k - S c) :: rev (take (k - S c) s)) H6)).
  apply dd_indep.
  rewrite H4; reflexivity.
  unfold cf_div.
  rewrite H7.
  set (dd_reduce (rev (take (k - S c) s)) 
    (nth Zero s k) (nth Zero s (k - S c))
    H3 Q H6 X).
  unfold cf_div in e.
  rewrite e.
  reflexivity.
Qed.
    
(**
 * The Newton polynomial coincides with the Lagrange 
 * polynomial. This lemma essentially proves this. 
 *
 * TODO: Replace bigopsClass operators with corresponding
 * folds. 
 *)
Lemma lem11b : forall (k c : nat) (s : seq K) 
  (Q : fresh_seq K s)
  (R : fresh_seq K ((nth Zero s k) :: 
    (rev (take (k - c) s)))),
  0 < k -> c <= k -> k < size s -> 
  (N K f s k Q) ! (nth Zero s k) [=]
  (\big[(cpoly_plus_cs K)/(cpoly_zero K)]_(j <- (mkseq (fun x => x) (k - c)) | (fun x => true) j)
    (_C_ (alpha K f s Q j) [*] (eta K s j)))
  ! (nth Zero s k) [+] 
  (dd K f ((nth Zero s k) :: 
    (rev (take (k - c) s))) R) [*] 
  (eta K s (k - c)) ! (nth Zero s k).
Proof.
  intros.
  induction c.
  assert (k - 0 = k) by omega.
  assert (mkseq ssrfun.id (k - 0) =
    mkseq ssrfun.id k).
  rewrite H2; reflexivity.
  rewrite H3.
  clear H2 H3.
  assert (fresh_seq K (rev (take k s))).
  apply rev_fresh_seq.
  apply take_fresh_seq.
  exact Q.
  assert (dd K f (nth Zero s k ::
    rev (take (k - 0) s)) R [=]
    alpha K f s Q k).
  unfold alpha.
  apply dd_indep.
  rewrite <- cat1s.
  assert (rev [:: nth Zero s k] = [:: nth Zero s k]).
  auto.
  rewrite <- H3.
  rewrite <- rev_cat.
  assert (take (k - 0) s ++ [:: nth Zero s k] =
    take (k + 1) s).
  rewrite cats1.
  assert (k - 0 = k) by omega.
  rewrite H4.
  rewrite <- take_nth.
  assert (S k = k + 1) by omega.
  rewrite H5.
  reflexivity.
  apply (ssrbool.introT (P := S k <= size s)).
  apply ssrnat.leP.
  auto.
  rewrite H4.
  reflexivity.
  rewrite H3.
  rewrite <- c_mult_apply.
  rewrite <- plus_apply.
  assert (k - 0 = k) by omega.
  rewrite H4.
  rewrite <- (@big_seq1 (cpoly K) (cpoly_eq K)
    eqv_cpoly (cpoly_plus_cs K) (cpoly_zero K)
    right_unit_cpoly nat k  
    (fun x => cr_mult (_C_ (alpha K f s Q x)) (eta K s x))).
  rewrite <- (@big_cat (cpoly K) (cpoly_eq K)
    (eqv_cpoly) (cpoly_plus_cs K) (cpoly_zero K)
    morph_cpoly assoc_cpoly left_unit_cpoly nat
    (mkseq ssrfun.id k) ([:: k])
    (fun x => true) (fun x => cr_mult (_C_ (alpha K f s Q x)) (eta K s x))
    ).
  simpl.
  assert (mkseq ssrfun.id k ++ [:: k] =
    mkseq ssrfun.id (k + 1)).
  unfold mkseq.
  assert ([:: k] = map ssrfun.id [:: k]).
  auto.
  rewrite H5.
  rewrite <- map_cat.
  assert ([:: k] = iota k 1).
  auto.
  rewrite H6.
  rewrite <- iota_add.
  auto.  
  rewrite H5.
  reflexivity. 
  assert (fresh_seq K (nth Zero s k ::
    rev (take (k - c) s))).
  apply fresh_cons.
  unfold sqfresh.
  apply insq.
  apply rev_fresh.
  apply nth_fresh.
  exact H1.
  exact Q.
  apply rev_fresh_seq.
  apply take_fresh_seq.
  exact Q.
  rewrite (IHc H2).
  assert (mkseq ssrfun.id (k - c) =
    mkseq ssrfun.id (k - S c) ++
    [:: (k - S c)]).
  unfold mkseq.
  assert ([:: k - S c] =
    map ssrfun.id (iota (k - S c) 1))
    by auto.
  rewrite H3.
  rewrite <- map_cat.
  assert (iota (k - S c) 1 = 
    iota (0 + (k - S c)) 1) by auto.
  rewrite H4.
  rewrite <- iota_add.
  assert (ssrnat.addn (k - S c) 1 = k - c).
  rewrite ssrnat.addn1.
  omega.
  rewrite H5; reflexivity.
  rewrite H3.
  rewrite (@big_cat (cpoly K) (cpoly_eq K)
    eqv_cpoly (cpoly_plus_cs K) (cpoly_zero K)
    morph_cpoly assoc_cpoly left_unit_cpoly nat
    (mkseq ssrfun.id (k - S c)) [:: k - S c]
    ).
  rewrite plus_apply.
  set (CSemiGroups.plus_assoc K).
  unfold associative in a.
  rewrite <- a.
  rewrite (@big_seq1 (cpoly K) (cpoly_eq K)
    eqv_cpoly (cpoly_plus_cs K) (cpoly_zero K)
    right_unit_cpoly nat (k - S c)).
  assert (fresh_seq K (rev (take (k - c) s))).
  apply rev_fresh_seq.
  apply take_fresh_seq.
  exact Q.
  assert ((nth Zero s k) [-] (nth Zero s (k - S c)) [#]
    Zero).
  apply ap_fresh_nth.
  omega.
  omega.
  exact H1.
  exact Q.
  assert ((dd K f (nth Zero s k :: 
    rev (take (k - c) s)) H2) [=]
    (((dd K f (nth Zero s k :: 
      rev (take (k - S c) s)) R) [-] 
    (dd K f (rev (take (k - c) s)) H4)) [/]
    ((nth Zero s k) [-] (nth Zero s (k - S c))) [//]
    X)).
  apply dd_reduce_nth.
  exact H.
  omega.
  exact H1.
  exact Q.
  rewrite H5.
  unfold eta at 3.
  assert (\big[cpoly_mult_cs K/cpoly_one K]_(x_i <- take (k - c) s)
    (cpoly_linear K (cg_inv x_i) (cpoly_one K)) [=]
    \big[cpoly_mult_cs K/cpoly_one K]_(x_i <- (take (k - S c) s) ++ [:: nth Zero s (k - S c)])
      (cpoly_linear K (cg_inv x_i) (cpoly_one K))).    
  assert (take (k - c) s = take (k - S c) s ++
    [:: nth Zero s (k - S c)]).
  rewrite <- cat_rcons.
  rewrite <- take_nth.
  assert (k - c = S (k - S c)) by omega.
  rewrite H6.
  rewrite cats0; reflexivity.
  apply (ssrbool.introT (P := S (k - S c) <= size s)).
  apply ssrnat.leP.
  omega.
  rewrite H6.
  reflexivity.
  rewrite H6.
  rewrite (@big_cat (cpoly K) (cpoly_eq K)
    eqv_cpoly (cpoly_mult_cs K) (cpoly_one K)
    morph_cpoly_mult assoc_cpoly_mult
    left_unit_cpoly_mult K (take (k - S c) s)
    [:: nth Zero s (k - S c)]). 
  rewrite (@big_seq1 (cpoly K) (cpoly_eq K)
    eqv_cpoly (cpoly_mult_cs K) (cpoly_one K)
    right_unit_cpoly_mult K (nth Zero s (k - S c))).
  rewrite (mult_apply K _ 
    (cpoly_linear K (cg_inv (nth Zero s (k - S c))) 
    (cpoly_one K))).
  assert (cpoly_apply_fun (cpoly_linear K
    (cg_inv (nth Zero s (k - S c))) (cpoly_one K))
    (nth Zero s k) [=] nth Zero s k [-]
    nth Zero s (k - S c)).
  simpl.
  rewrite cring_mult_zero.
  rewrite cm_rht_unit_unfolded.
  rewrite mult_one.
  rewrite cg_minus_unfolded.
  algebra.
  rewrite H7.
  rewrite (mult_commutes K _ 
    (nth Zero s k [-] nth Zero s (k - S c))).
  rewrite mult_assoc.
  rewrite div_1.
  rewrite (mult_commutes K (_ [-] _) _).
  rewrite ring_dist_minus.
  unfold alpha at 2.
  assert (dd K f (rev (take (k - S c + 1) s))
    (rev_fresh_seq K (take (k - S c + 1) s)
      (take_fresh_seq K (k - S c + 1) s Q)) [=]
    dd K f (rev (take (k - c) s)) H4).
  apply dd_indep.
  assert (k - S c + 1 = k - c) by omega.
  rewrite H8.
  reflexivity.
  rewrite H8.
  rewrite cg_minus_unfolded.
  rewrite (cag_commutes K _ (cg_inv _)).
  rewrite (a _ (cg_inv _)).
  unfold eta at 2.
  rewrite c_mult_apply.
  rewrite (mult_commutes K _ (dd _ _ _ H4)).
  rewrite cg_rht_inv_unfolded.
  rewrite cm_lft_unit_unfolded.
  rewrite (mult_commutes K (dd _ _ _ _) _).
  unfold eta at 3.
  reflexivity.
  omega.
Qed.

(**
 * This lemma corresponds with lemma 13 in the paper as 
 * sent to me on january 11th. It is called 'lem11' here
 * because it was based on a previous version of the paper.
 * It relies on lemmas 11a and 11b (see before). 
 *)
Lemma lem11 : forall (k i : nat) (s : seq K) (P : i <= k) 
  (Q : fresh_seq K s), k < size s ->
  f (nth Zero s i) [=] (N K f s k Q) ! (nth Zero s i).
Proof.
  intros.
  induction k.
  inversion P.
  destruct s.
  inversion H.
  simpl.
  unfold alpha.
  assert (fresh_seq K [:: s]).
  apply fresh_cons.
  unfold sqfresh.
  apply insq.
  simpl; auto.
  apply fresh_nil.
  assert (dd K f (rev (take (0 + 1) (s :: s0)))
    (rev_fresh_seq K (take (0 + 1) (s :: s0))
      (take_fresh_seq K (0 + 1) (s :: s0) Q)) [=] 
    dd K f [:: s] H1).
  apply dd_indep.
  simpl.
  rewrite take0.
  reflexivity.  
  rewrite H2.
  assert (f s [=] dd K f [:: s] H1).
  algebra.
  rewrite <- H3.
  rewrite cring_mult_zero.
  rewrite cm_rht_unit_unfolded.
  rewrite cm_rht_unit_unfolded.
  algebra.
  inversion P.
  assert (fresh_seq K (nth Zero s (S k) :: 
    rev (take (S k - S k) s))).
  assert (S k - S k = 0) by omega.
  rewrite H1.
  rewrite take0.
  apply fresh_cons.
  unfold sqfresh.
  apply insq.
  simpl; auto.
  apply rev_fresh_seq.
  apply fresh_nil.
  rewrite (lem11b (S k) (S k) _ _ H1).
  assert (mkseq ssrfun.id (S k - S k) =
    Nil nat).
  assert (S k - S k = 0) by omega.
  rewrite H2.
  auto.
  rewrite H2.
  rewrite big_nil.
  assert (fresh_seq K [:: nth Zero s (S k)]).
  apply fresh_cons.
  unfold sqfresh.
  apply insq.
  simpl; auto.
  apply fresh_nil.
  setoid_replace (dd K f (nth Zero s (S k) ::
    (rev (take (S k - S k) s))) H1) with
    (dd K f [:: nth Zero s (S k)] H3).
  assert (cpoly_apply_fun (cpoly_zero K) 
    (nth Zero s (S k)) [=] Zero).
  algebra.
  rewrite H4.
  rewrite cm_lft_unit_unfolded.
  unfold eta.
  assert (take (S k - S k) s = Nil K).
  assert (S k - S k = 0) by omega.
  rewrite H5.
  rewrite take0; reflexivity.
  rewrite H5.
  rewrite apply_bigops.
  rewrite big_nil.
  rewrite mult_one.
  algebra.
  apply dd_indep.
  assert (S k - S k = 0) by omega.
  rewrite H4.
  rewrite take0.
  auto.
  omega.
  auto.
  omega.
  unfold N.
  rewrite (@eq_big_idx_seq (cpoly K) (cpoly_eq K)
    eqv_cpoly (cpoly_plus_cs K) (cpoly_zero K)
    morph_cpoly 
    right_unit_cpoly (cpoly_zero K) nat
    (mkseq ssrfun.id (S k + 1)) (fun x : nat => true)
    (fun x : nat => cr_mult (_C_ (alpha K f s Q x)) (eta K s x))
    right_unit_cpoly).
  assert (mkseq ssrfun.id (S (k + 1)) = 
    (mkseq ssrfun.id (S k)) ++ [:: S k]).
  unfold mkseq.
  replace (S (S k)) with (S k + 1).
  rewrite (iota_add 0 (S k) 1).
  rewrite map_cat; auto.
  omega.
  rewrite H2.
  clear H2.
  rewrite (@big_cat (cpoly K) (cpoly_eq K) eqv_cpoly
    (cpoly_plus_cs K) (cpoly_zero K) morph_cpoly assoc_cpoly
    left_unit_cpoly nat 
    (mkseq ssrfun.id (S k)) ([:: S k]) (fun x : nat => true)
    (fun x : nat => _C_ (alpha K f s Q x) [*] (eta K s x))).
  unfold N in IHk.
  rewrite (@big_seq1 (cpoly K) (cpoly_eq K) eqv_cpoly
    (cpoly_plus_cs K) (cpoly_zero K) right_unit_cpoly nat (S k)
    (fun x : nat => _C_ (alpha K f s Q x) [*] (eta K s x))).
  rewrite (@plus_apply K
    ((\big[cpoly_plus_cs K/cpoly_zero K]_(i0 <- mkseq ssrfun.id (S k))
         (_C_ (alpha K f s Q i0) [*] (eta K s i0))))
    (_C_ (alpha K f s Q (S k)) [*] (eta K s (S k)))
    (nth Zero s i)).
  assert ((_C_ (alpha K f s Q (S k)) [*] (eta K s (S k))) !
    (nth Zero s i) [=] Zero).
  rewrite c_mult_apply.
  unfold eta.
  rewrite (@apply_bigops (take (S k) s)).
  rewrite lem11a.
  rewrite cring_mult_zero.
  reflexivity.
  omega.
  omega.
  exact H1.
  rewrite H2.
  rewrite IHk.
  assert (S k = k + 1) by omega.
  rewrite H3.
  rewrite cm_rht_unit_unfolded.
  reflexivity.
  exact H1.
  omega.
  auto.
Qed.

(**
 * Getting the nth coefficient of a polynomial can be done
 * outside a bigop, as well as inside, resulting in the 
 * same value (with the bigop applied in the latter case,
 * of course).
 *
 * TODO: Generalise to arbitrary bigop.
 * TODO: Replace bigopsClass construct with corresponding
 * folds.
 *)
Lemma nth_coeff_bigops : forall (k : nat) (r : seq nat) F,
  nth_coeff k (\big[cpoly_plus_cs K/cpoly_zero K]_(i <- r) F i) [=]
  \big[csg_op/(Zero:K)]_(i <- r) (nth_coeff k (F i)).
Proof.
  intros. 
  induction r.
  rewrite big_nil.
  simpl; reflexivity.
  rewrite big_cons.
  rewrite nth_coeff_plus.
  rewrite IHr.
  rewrite big_cons.
  reflexivity.
Qed.  

(**
 * A small lemma to ascertain compatibility between two
 * types of multiplication between polynomials and constant
 * values. 
 *)
Lemma cpoly_mult_cr_c_ : forall p c,
  (cpoly_mult_cr K p c [=] p [*] _C_ c).
Proof.
  intros.
  simpl.
  induction p.
  simpl; auto.
  simpl.
  split.
  rewrite cm_rht_unit_unfolded.
  rewrite mult_commutes.
  reflexivity.
  rewrite IHp.
  reflexivity.
Qed.

(**
 * This corrolary corresponds to corrolary 14 in the PDF as
 * e-mailed to me on january 11th. It states that the divided
 * difference f[a_1, ..., a_n] is the coefficient of x^n in
 * the (Newton) polynomial that interpolates f at a_1, ..., a_n.
 *
 * TODO: Fix usage of bigopsClass constructs.
 * TODO: Shorten proof.
 *)
Lemma cor12 : forall (k : nat) (s : seq K) 
  (Q : fresh_seq K s), k < size s -> 
  nth_coeff k (N K f s k Q) [=] alpha K f s Q k.
Proof.
  intros.
  unfold N.
  cut (mkseq ssrfun.id (k + 1) =
    mkseq ssrfun.id k ++ [:: k]).
  intro.
  rewrite H0.
  rewrite (@big_cat (cpoly K) (cpoly_eq K)
    eqv_cpoly (cpoly_plus_cs K) (cpoly_zero K) 
    morph_cpoly assoc_cpoly left_unit_cpoly).
  rewrite (@big_seq1 (cpoly K) (cpoly_eq K)
    eqv_cpoly (cpoly_plus_cs K) (cpoly_zero K)
    right_unit_cpoly).
  rewrite nth_coeff_plus.
  rewrite nth_coeff_c_mult_p.
  assert (nth_coeff k (eta K s k) [=] One).
  unfold eta.
  clear H0.
  induction k.
  rewrite take0.
  simpl; reflexivity.
  cut (take (S k) s = take k s ++ 
    [:: nth Zero s k]).
  intro.
  rewrite H0.
  rewrite (@big_cat (cpoly K) (cpoly_eq K)
    eqv_cpoly (cpoly_mult_cs K) (cpoly_one K) 
    morph_cpoly_mult assoc_cpoly_mult left_unit_cpoly_mult).
  rewrite (@big_seq1 (cpoly K) (cpoly_eq K)
    eqv_cpoly (cpoly_mult_cs K) (cpoly_one K)
    right_unit_cpoly_mult).
  rewrite cpoly_mult_lin.
  rewrite nth_coeff_plus.
  rewrite coeff_Sm_lin.
  rewrite cpoly_mult_one.
  rewrite IHk.
  unfold cpoly_mult_cr_cs.
  rewrite cpoly_mult_cr_c_.
  rewrite nth_coeff_p_mult_c_.
  assert (degree_le k
    (\big[cpoly_mult_cs K/cpoly_one K]_(i <- take k s)
      cpoly_linear K (cg_inv i) (cpoly_one K))).
  clear IHk H0.
  induction k.
  rewrite take0.
  unfold degree_le.
  intros.
  rewrite big_nil.
  destruct m.
  inversion H0.
  simpl; reflexivity.
  cut (take (S k) s = take k s ++
    [:: nth Zero s k]).
  intro.
  rewrite H0.
  unfold degree_le.
  intros.
  rewrite (@big_cat (cpoly K) (cpoly_eq K)
    eqv_cpoly (cpoly_mult_cs K) (cpoly_one K) 
    morph_cpoly_mult assoc_cpoly_mult left_unit_cpoly_mult).
  set degree_mult_aux.
  simpl in s0.
  destruct m.
  inversion H1.
  replace (S m) with (m + 1) by omega.
  rewrite <- cpoly_mult_fast_equiv.
  rewrite s0.
  simpl.
  rewrite mult_one.
  rewrite cm_rht_unit_unfolded.
  rewrite mult_one.
  unfold degree_le in IHk.
  rewrite IHk.
  reflexivity.
  omega.
  omega.
  unfold degree_le in IHk.
  unfold degree_le.
  intros.
  rewrite IHk.
  reflexivity.
  omega.
  omega.
  unfold degree_le.
  intros.
  destruct m0.
  inversion H2.
  destruct m0.
  inversion H2.
  inversion H4.
  simpl.
  reflexivity.
  rewrite cats1.
  rewrite <- take_nth.
  reflexivity.
  apply (ssrbool.introT (P := S k <= size s)).
  apply ssrnat.leP.
  omega.
  unfold degree_le in H1.
  rewrite H1.
  rewrite mult_commutes.
  rewrite cring_mult_zero.
  algebra.
  omega.
  omega.
  rewrite cats1.
  rewrite <- take_nth.
  reflexivity.
  apply (ssrbool.introT (P := S k <= size s)).
  apply ssrnat.leP.
  omega.
  rewrite H1.
  rewrite mult_one.
  assert (degree_le (k - 1)
     (\big[cpoly_plus_cs K/cpoly_zero K]_(i <- mkseq ssrfun.id k)
         cr_mult (_C_ (alpha K f s Q i)) (eta K s i))).
  unfold degree_le.
  clear H0 H1.
  induction k.
  simpl.
  intros.
  reflexivity.
  intros.
  cut (mkseq ssrfun.id (S k) = 
    mkseq ssrfun.id k ++ [:: k]).
  intro.
  rewrite H1.  
  rewrite (@big_cat (cpoly K) (cpoly_eq K)
    eqv_cpoly (cpoly_plus_cs K) (cpoly_zero K) 
    morph_cpoly assoc_cpoly left_unit_cpoly).
  rewrite (@big_seq1 (cpoly K) (cpoly_eq K)
    eqv_cpoly (cpoly_plus_cs K) (cpoly_zero K)
    right_unit_cpoly).
  rewrite nth_coeff_plus.
  unfold degree_le in IHk.
  rewrite IHk.
  rewrite cm_lft_unit_unfolded.
  rewrite nth_coeff_c_mult_p.
  clear IHk.
  assert (degree_le k (eta K s k)).
  unfold degree_le.
  induction k.
  unfold degree_le, eta.
  intros.
  rewrite take0.
  destruct m0.
  assert (1 < 0) by omega.
  inversion H2.
  simpl; reflexivity.
  unfold degree_le, eta.
  intros.
  cut (take (S k) s = take k s ++
    [:: nth Zero s k]).
  intro.
  rewrite H3.
  rewrite (@big_cat (cpoly K) (cpoly_eq K)
    eqv_cpoly (cpoly_mult_cs K) (cpoly_one K) 
    morph_cpoly_mult assoc_cpoly_mult left_unit_cpoly_mult).
  set degree_mult_aux.
  simpl in s0.
  rewrite <- cpoly_mult_fast_equiv.
  destruct m0.
  inversion H2.
  replace (S m0) with (m0 + 1) by omega.
  rewrite s0.
  unfold degree_le in IHk.
  rewrite IHk.
  rewrite mult_commutes.
  rewrite cring_mult_zero.
  reflexivity.
  omega.
  omega.
  unfold mkseq.
  assert ([:: k] = map ssrfun.id (iota k 1)).
  simpl; reflexivity.
  rewrite H4.
  rewrite <- map_cat.
  rewrite <- iota_add.
  rewrite ssrnat.addn1.
  reflexivity.
  omega.
  unfold degree_le.
  intros.
  rewrite IHk.
  reflexivity.
  omega.
  omega.
  unfold mkseq.
  assert ([:: k] = map ssrfun.id (iota k 1)).
  simpl; reflexivity.
  rewrite H5.
  rewrite <- map_cat.
  rewrite <- iota_add.
  rewrite ssrnat.addn1.
  reflexivity.
  omega.
  unfold degree_le.
  intros.
  simpl.
  destruct m1.
  inversion H4.
  destruct m1.
  inversion H4.
  inversion H6.
  reflexivity.
  rewrite cats1.
  rewrite <- take_nth.
  reflexivity.
  apply (ssrbool.introT (P := S k <= size s)).
  apply ssrnat.leP.
  omega.
  unfold degree_le in H2.
  rewrite H2.
  rewrite cring_mult_zero.
  reflexivity.
  omega.
  omega.
  omega.
  unfold mkseq.
  assert ([:: k] = map ssrfun.id (iota k 1)).
  simpl; reflexivity.
  rewrite H1.
  rewrite <- map_cat.
  rewrite <- iota_add.
  rewrite ssrnat.addn1.
  reflexivity.
  destruct k.
  simpl.
  rewrite cm_lft_unit_unfolded.
  reflexivity.
  unfold degree_le in H2.
  rewrite H2.
  rewrite cm_lft_unit_unfolded.
  reflexivity.
  omega.
  unfold mkseq.
  assert ([:: k] = map ssrfun.id (iota k 1)).
  simpl; reflexivity.
  rewrite H0.
  rewrite <- map_cat.
  rewrite <- iota_add.
  rewrite ssrnat.addn1.
  replace (S k) with (k + 1) by omega.
  reflexivity.
Qed.

End BigopsTheory.

(**
 * Since we don't have decidable equality for elements
 * in a Field K, we have to define our own permutation
 * for sequences. 
 *)

Section Permutations.

Variable A : CSetoid.

(**
 * Alternative definition for permutations. This one
 * matches the definition of dd a bit better.
 *)
Inductive permutation : seq A -> seq A -> Prop :=
  | permutation_nil : permutation nil nil
  | permutation_singleton : 
      forall (a : A), permutation [:: a] [:: a]
  | permutation_skip :
      forall (a b : A) (s1 s2 : seq A),
      permutation s1 s2 ->
      permutation (a :: s1) (a :: s2) -> 
      permutation (b :: s1) (b :: s2) ->
      permutation (a :: b :: s1) (a :: b :: s2)
  | permutation_swap :
      forall (a b : A) (s : seq A), 
      permutation (a :: b :: s) (b :: a :: s)
  | permutation_trans :
      forall s1 s2 s3 : seq A,
      permutation s1 s2 -> permutation s2 s3 -> 
      permutation s1 s3.

Hint Constructors permutation.

(**
 * A type of induction on sequences that suits the 
 * definition of divided differences a bit better. 
 *
 * TODO: Fix this proof, which should not be too hard. 
 * A solution might be to distinguish between sequences
 * of even and odd length.
 *)
Lemma dd_type_ind : forall P, P [::] ->
  (forall (a : A), P [:: a]) ->
  (forall (a b : A) (s : seq A), 
    P s -> P (a :: s) ->
    P (b :: s) -> P (a :: b :: s)) ->
  (forall (s : seq A), P s).
Proof.
  intros.
  Admitted.

(**
 * Permutations are reflexive.
 *)
Lemma permutation_refl : forall s : seq A, 
  permutation s s.
Proof.
  intros.
  induction s using dd_type_ind.
  apply permutation_nil.
  apply permutation_singleton.
  apply permutation_skip.
  exact IHs.
  exact IHs0.
  exact IHs1.
Qed.

Hint Resolve permutation_refl.

(**
 * Permutations are symmetric.
 *)
Lemma permutation_sym :
 forall l m : seq A, permutation l m -> 
 permutation m l.
Proof.
intros l1 l2 H'; elim H'.
apply permutation_nil.
intro a.
apply permutation_singleton.
intros.
apply permutation_skip.
exact H0.
exact H2.
exact H4.
intros.
apply permutation_swap.
intros l1' l2' l3' H1 H2 H3 H4.
apply permutation_trans with (1 := H4) (2 := H2).
Qed.

(**
 * The length of a sequence does not change under
 * permutation.
 *)
Lemma permutation_size :
 forall l m : seq A, permutation l m -> 
 size l = size m.
Proof.
intros l m H'; elim H'; simpl in |- *; auto.
intros l1 l2 l3 H'0 H'1 H'2 H'3.
rewrite <- H'3; auto.
Qed.

(**
 * The permutation nil originates from nil.
 *)
Lemma permutation_nil_inv : forall l : seq A, 
  permutation l nil -> l = nil.
Proof.
intros l H; 
generalize (permutation_size _ _ H); case l; 
simpl in |- *; auto.
intros; discriminate.
Qed.

(**
 * Permutation on sequences of length 1 is an identity
 * operation.
 *)
Lemma permutation_one_inv_aux :
  forall l1 l2 : seq A,
  permutation l1 l2 -> 
  forall a : A, l1 = a :: nil -> l2 = a :: nil.
Proof.
intros l1 l2 H; elim H; clear H l1 l2; auto.
intros.
inversion H5.
intros.
inversion H.
Qed.

(**
 * TODO: Do we really require this lemma?
 *)
Lemma permutation_one_inv :
 forall (a : A) (l : seq A), 
 permutation (a :: nil) l -> l = a :: nil.
intros a l H; 
apply permutation_one_inv_aux with (l1 := a :: nil); 
auto.
Qed.

End Permutations.

Section PermProperties.

Variable K : CField.
Variable f : K -> K.

(**
 * The property of freshness is preserved under permutation.
 *)
Lemma perm_fresh : forall (s1 s2 : seq K),
  fresh_seq K s1 -> permutation K s1 s2 ->
  fresh_seq K s2.
Proof.
  intros.
  induction H0.
  apply fresh_nil.
  apply fresh_cons.
  unfold sqfresh; simpl.
  apply insq; auto.
  apply fresh_nil.
  apply fresh_cons.
  assert (fresh_seq K (a :: s2)).
  apply IHpermutation2.
  apply fresh_cons.
  inversion H.
  inversion H2.
  inversion X.
  unfold sqfresh.
  apply insq.
  exact X1.
  inversion H.
  inversion H3.
  exact H7.
  inversion H.
  inversion H3.
  inversion X.
  unfold sqfresh.
  apply insq.
  simpl.
  split.
  exact X0.
  apply unsq.
  inversion H0.
  unfold sqfresh in H7.
  exact H7.
  apply IHpermutation3.
  inversion H.
  exact H3.
  apply fresh_cons.
  inversion H.
  inversion H2.
  inversion X.
  unfold sqfresh.
  apply insq.
  simpl. 
  split.
  apply ap_symmetric.
  exact X0.
  apply unsq.
  inversion H3.
  unfold sqfresh in H6.
  exact H6.
  apply fresh_cons.
  inversion H.
  inversion H2.
  inversion X.
  unfold sqfresh.
  apply insq.
  exact X1.
  inversion H.
  inversion H3.
  exact H7.
  apply IHpermutation2.
  apply IHpermutation1.
  exact H.
Qed.
 
(** 
 * Lemma 14 - The result of dd is invariant under 
 * permutation of its sequence of arguments.
 * 
 * In the paper as sent to me on january 11th, this is
 * referred to as lemma 16.
 * 
 * TODO: Shorten proof.
 *)
Lemma lem14 : forall (s1 s2 : seq K)
  (P1 : fresh_seq K s1) (P2 : fresh_seq K s2),
  (permutation K s1 s2) -> dd K f s1 P1 [=] dd K f s2 P2.
Proof.
  intros s1 s2 P1 P2 perm.
  induction perm.
  algebra.
  algebra.
  assert (fresh_seq K (a :: s1)).
  apply fresh_cons.
  inversion P1.
  inversion H1.
  inversion X.
  unfold sqfresh.
  apply insq.
  exact X1.
  inversion P1.
  inversion H2.
  exact H6.
  assert (fresh_seq K (b :: s1)).
  inversion P1.
  exact H3.
  inversion P1.
  inversion H3.
  inversion X.
  assert (a [-] b [#] Zero).
  apply minus_ap_zero.
  apply ap_symmetric.
  exact X0.
  rewrite (dd_reduce K f s1 a b P1 H H0 X2).
  assert (fresh_seq K (a :: s2)).
  apply fresh_cons.
  inversion P2.
  inversion H7.
  inversion X3.
  apply insq.
  exact X5.
  inversion P2.
  inversion H8.
  exact H12.
  assert (fresh_seq K (b :: s2)).
  inversion P2.
  exact H9.
  rewrite (dd_reduce K f s2 a b P2 H5 H6 X2).
  apply div_wd.
  rewrite (IHperm2 H H5).
  rewrite (IHperm3 H0 H6).
  reflexivity.
  reflexivity.
  assert (fresh_seq K (a :: s)).
  inversion P2.
  exact H2.
  assert (fresh_seq K (b :: s)).
  inversion P1.
  exact H3.
  inversion P1.
  inversion H3.
  inversion X.
  assert (a [-] b [#] Zero).
  apply minus_ap_zero.
  apply ap_symmetric.
  exact X0.
  rewrite (dd_reduce K f s a b P1 H H0 X2).
  assert (b [-] a [#] Zero).
  apply minus_ap_zero.
  exact X0.
  rewrite (dd_reduce K f s b a P2 H0 H X3).
  apply eq_div.
  rewrite ring_dist_minus.
  rewrite ring_dist_minus.
  rewrite dist_2b.
  rewrite dist_2b.
  rewrite dist_2b.
  rewrite dist_2b.
  set (cr_mult (dd K f (a :: s) H) b).
  set (cr_mult (dd K f (b :: s) H0) b).
  set (cr_mult (dd K f (a :: s) H) a).
  set (cr_mult (dd K f (b :: s) H0) a).
  rewrite assoc_1.
  rewrite assoc_1.
  rewrite <- minus_plus.
  rewrite <- minus_plus.
  rewrite (cag_commutes_unfolded K s3 s2).
  rewrite (cag_commutes_unfolded K _ s1).
  rewrite assoc_2.
  rewrite (cag_commutes_unfolded K _ s4).
  rewrite assoc_2.
  rewrite cg_minus_unfolded.
  rewrite cg_minus_unfolded.
  rewrite (cag_commutes_unfolded K s1 s4).
  reflexivity.
  assert (fresh_seq K s2).
  apply (perm_fresh s1).
  exact P1.
  exact perm1.
  rewrite (IHperm1 P1 H).
  rewrite (IHperm2 H P2).
  reflexivity.
Qed.

End PermProperties.

(**
 * This is the end of the section about permutations. As I
 * indicated before in our conversations, from this point on
 * I have much concern about the proper heading of this 
 * research. For instance, many lemmas below are only 
 * required to get NAH-material to work (while it is still
 * a bit uncertain if we require this at all). Another 
 * important mistake (in my opinion) is the usage of two
 * different definitions of continuity, differentiability
 * etc. 
 *
 * I therefore propose the following: 
 *
 * - Keep the file from line 0 up until here intact 
 *   (but fix the proofs, of course). 
 * - Start over again to work on multi-variable integration
 *   as a separate branch (but only what is really required
 *   for this project). 
 * - Bridge the gap between these two, and complete the
 *   proof.
 *)

Section Derivations.

(**
 * Lemma 15 - If a < b then C[a,b] is an algebra over
 * the ring R. 
 *)
Require Import Structures.

Variable a b : IR.
Hypothesis Hab : a [<] b.

(**
 * These are the partial functions on IR such that for
 * each n, they are n-times differentiable.
 *)
Record C_inf_ab := {
  f_crr : PartFunct IR ;
  f_pdf : forall n, Diffble_I_n Hab n f_crr}.

(**
 * These are type class instances and not further explained.
 *)
Instance IR_plus : RingPlus IR := csg_op.

Instance IR_mult : RingMult IR := cr_mult.

Instance IR_inv : GroupInv IR := cg_inv.

Instance IR_zero : RingZero IR := Zero.

Instance IR_one : RingOne IR := One.

Instance IR_equiv : Equiv IR := (@st_eq IR).

Instance IR_equivalence : Equivalence (@st_eq IR).

(**
 * Addition is associative for IR
 *)
Instance IR_associative : @Associative IR (@st_eq IR) IR_plus.
unfold Associative.
intros.
unfold IR_plus.
unfold equiv.
algebra.
Qed.

(**
 * Addition is a morphism with respect to the standard
 * equality on IR.
 *)
Instance IR_proper : Proper ((@st_eq IR) ==> (@st_eq IR) ==>
  (@st_eq IR)) IR_plus.
unfold Proper.
unfold respectful.
intros.
rewrite H.
rewrite H0.
reflexivity.
Qed.

(**
 * These two facts allow us to state IR as a semigroup for
 * addition.
 *)
Instance IR_semigroup : @SemiGroup IR (@st_eq IR) IR_plus.

(**
 * IR is a monoid for addition over IR.
 *)
Instance IR_monoid : @Monoid IR (@st_eq IR) IR_plus Zero.
assert (forall x : IR, Zero [+] x == x).
intros.
rewrite cm_lft_unit_unfolded.
reflexivity.
assert (forall x : IR, x [+] Zero == x).
intros.
rewrite cm_rht_unit_unfolded.
reflexivity.
apply (Build_Monoid IR (@st_eq IR) IR_plus Zero
  IR_semigroup H H0).
Qed.

(**
 * Type instance stating that inversion for IR is a
 * morphism with respect to the standard equality on IR.
 *)
Instance IR_proper_inv : Proper ((@st_eq IR) ==> 
  (@st_eq IR)) IR_inv.
unfold Proper.
unfold respectful.
intros.
rewrite H.
reflexivity.
Qed.

(**
 * This makes IR a group with addition, inversion and zero.
 *)
Instance IR_group : @Group IR (@st_eq IR) IR_plus
  Zero IR_inv.
assert (forall x : IR, IR_plus (IR_inv x) x == Zero).
intros.
unfold IR_plus, IR_inv.
assert (csg_op (cg_inv x) x [=] Zero).
algebra.
rewrite H.
reflexivity.
assert (forall x : IR, IR_plus x (IR_inv x) == Zero).
intros.
unfold IR_plus, IR_inv.
assert (csg_op x (cg_inv x) [=] Zero).
algebra.
rewrite H0.
reflexivity.
apply (Build_Group IR (@st_eq IR) IR_plus Zero IR_inv
  IR_monoid IR_proper_inv H H0).
Qed.

(**
 * IR is also an Abelian group because of the commutativity
 * of addition.
 *)
Instance IR_abgroup : @AbGroup IR (@st_eq IR) IR_plus
  Zero IR_inv.
assert (Commutative IR_plus).
unfold Commutative.
intros.
assert (IR_plus x y [=] IR_plus y x).
unfold IR_plus.
algebra.
rewrite H.
reflexivity.
apply (Build_AbGroup IR (@st_eq IR) IR_plus Zero 
  IR_inv IR_group H).
Qed.

(**
 * Multiplication on IR is clearly associative.
 *)
Instance IR_associative_mult : @Associative IR (@st_eq IR) IR_mult.
unfold Associative.
intros.
unfold IR_mult.
unfold equiv.
algebra.
Qed.

(**
 * Multiplication on IR is a morphism with respect to the
 * standard equality on IR.
 *)
Instance IR_proper_mult : Proper ((@st_eq IR) ==> (@st_eq IR) ==>
  (@st_eq IR)) IR_mult.
unfold Proper.
unfold respectful.
intros.
rewrite H.
rewrite H0.
reflexivity.
Qed.

(**
 * This makes IR a semigroup with respect to multiplication.
 *)
Instance IR_semigroup_mult : @SemiGroup IR (@st_eq IR) IR_mult.

(**
 * We may now conclude that IR is a monoid with one as the
 * neutral element in multiplication.
 *)
Instance IR_monoid_mult : @Monoid IR (@st_eq IR) IR_mult One.
assert (forall x : IR, One [*] x == x).
intros.
rewrite mult_commutes.
rewrite mult_one.
reflexivity.
assert (forall x : IR, x [*] One == x).
intros.
rewrite mult_one.
reflexivity.
apply (Build_Monoid IR (@st_eq IR) IR_mult One
  IR_semigroup_mult H H0).
Qed.

(**
 * The reals form a ring with addition and multiplication.
 *
 * TODO: All these lemmas (this one and the ones before) are
 * probably defined in the CoRN libraries as well and are 
 * therefore not required to be stated here (but I am not
 * sure if they are already present as type class instances).
 *)
Instance IR_ring : @Ring IR (@st_eq IR) IR_plus
  IR_mult IR_inv Zero One.
assert (Commutative IR_mult).
unfold Commutative.
intros.
assert (IR_mult x y [=] IR_mult y x).
unfold IR_mult.
algebra.
rewrite H.
reflexivity.
assert (Distribute IR_mult IR_plus).
assert (forall a b c, IR_mult a (IR_plus b c) ==
  IR_plus (IR_mult a b) (IR_mult a c)).
intros.
unfold IR_mult, IR_plus.
assert (cr_mult a0 (csg_op b0 c) [=]
  csg_op (cr_mult a0 b0) (cr_mult a0 c)).
algebra.
rewrite H0.
reflexivity.
assert (forall a b c, IR_mult (IR_plus a b) c ==
  IR_plus (IR_mult a c) (IR_mult b c)).
intros.
unfold IR_mult, IR_plus.
assert (cr_mult (csg_op a0 b0) c [=]
  csg_op (cr_mult a0 c) (cr_mult b0 c)).
algebra.
rewrite H1.
reflexivity.
apply (Build_Distribute IR (@st_eq IR) IR_mult IR_plus
  H0 H1).
apply (Build_Ring IR (@st_eq IR) IR_plus IR_mult 
  IR_inv Zero One IR_abgroup IR_monoid_mult H H0).
Qed.

(**
 * If f' and g' are the nth derivatives of respectively
 * f and g, then f'+g' is the nth derivative of f+g.
 *
 * TODO: Is it possible to make these proofs a bit shorter?
 *)
Lemma Derivative_I_n_add : forall (n : nat)
  (f g f' g' : PartFunct IR) (Pfderiv : Derivative_I_n Hab n f f')
  (Pgderiv : Derivative_I_n Hab n g g'), 
  Derivative_I_n Hab n (f{+}g) (f'{+}g').
Proof.
  intro n.
  induction n.
  intros.
  apply Feq_plus.
  exact Pfderiv.
  exact Pgderiv.
  intros.
  simpl.
  elim Pfderiv.
  intros.
  elim Pgderiv.
  intros.
  exists (IPlus x x0).
  apply Derivative_I_wdr with (PartInt x{+}PartInt x0).
  apply part_int_plus.
  apply Feq_reflexive.
  apply derivative_imp_inc' with f.
  exact p.
  apply Feq_reflexive.
  apply derivative_imp_inc' with g.
  exact p0.
  apply Derivative_I_plus.
  exact p.
  exact p0.
  apply Derivative_I_n_wdl with (PartInt x{+}PartInt x0).
  apply part_int_plus.
  apply Feq_reflexive.
  apply derivative_imp_inc' with f.
  exact p.
  apply Feq_reflexive.
  apply derivative_imp_inc' with g.
  exact p0.
  apply IHn.
  exact q.
  exact q0.
Qed.

(**
 * Now that we have an exact representation of the nth
 * derivative under addition, we can more easily prove
 * n-times differentiability for the addition of functions.
 *)
Lemma Diffble_I_n_plus : forall (n : nat)
  (f g : PartFunct IR) (Pfdf : Diffble_I_n Hab n f)
  (Pgdf : Diffble_I_n Hab n g), 
  Diffble_I_n Hab n (Fplus f g).
Proof.
  intros.
  assert ({f0' : CSetoid_fun (subset (Compact (less_leEq _ _ _ Hab))) IR |
    Derivative_I_n Hab n f (PartInt f0')}).
  apply Diffble_I_n_imp_deriv_n.
  exact Pfdf.
  assert ({g' : CSetoid_fun (subset (Compact (less_leEq _ _ _ Hab))) IR |
    Derivative_I_n Hab n g (PartInt g')}).
  apply Diffble_I_n_imp_deriv_n.
  exact Pgdf.
  inversion_clear X.
  inversion_clear X0.
  apply deriv_n_imp_Diffble_I_n with (PartInt x{+}PartInt x0).
  apply Derivative_I_n_add.
  exact X1.
  exact X.
Qed.

(**
 * If f' and g' are the nth derivatives of respectively
 * f and g, then f'-g' is the nth derivative of f-g.
 *)
Lemma Derivative_I_n_minus : forall (n : nat)
  (f g f' g' : PartFunct IR) (Pfderiv : Derivative_I_n Hab n f f')
  (Pgderiv : Derivative_I_n Hab n g g'), 
  Derivative_I_n Hab n (f{-}g) (f'{-}g').
Proof.
  intro n.
  induction n.
  intros.
  apply Feq_minus.
  exact Pfderiv.
  exact Pgderiv.
  intros.
  simpl.
  elim Pfderiv.
  intros.
  elim Pgderiv.
  intros.
  exists (IMinus x x0).
  apply Derivative_I_wdr with (PartInt x{-}PartInt x0).
  apply part_int_minus.
  apply Feq_reflexive.
  apply derivative_imp_inc' with f.
  exact p.
  apply Feq_reflexive.
  apply derivative_imp_inc' with g.
  exact p0.
  apply Derivative_I_minus.
  exact p.
  exact p0.
  apply Derivative_I_n_wdl with (PartInt x{-}PartInt x0).
  apply part_int_minus.
  apply Feq_reflexive.
  apply derivative_imp_inc' with f.
  exact p.
  apply Feq_reflexive.
  apply derivative_imp_inc' with g.
  exact p0.
  apply IHn.
  exact q.
  exact q0.
Qed.

(**
 * The n-times differentiability of two functions f and g
 * continues to the n-times differentiability of f-g.
 *
 * TODO: Prove this lemma using only backward reasoning.
 *)
Lemma Diffble_I_n_minus : forall (n : nat)
  (f g : PartFunct IR) (Pfdf : Diffble_I_n Hab n f)
  (Pgdf : Diffble_I_n Hab n g), 
  Diffble_I_n Hab n (f{-}g).
Proof.
  intros.
  assert ({f0' : CSetoid_fun (subset (Compact (less_leEq _ _ _ Hab))) IR |
    Derivative_I_n Hab n f (PartInt f0')}).
  apply Diffble_I_n_imp_deriv_n.
  exact Pfdf.
  assert ({g' : CSetoid_fun (subset (Compact (less_leEq _ _ _ Hab))) IR |
    Derivative_I_n Hab n g (PartInt g')}).
  apply Diffble_I_n_imp_deriv_n.
  exact Pgdf.
  inversion_clear X.
  inversion_clear X0.
  apply deriv_n_imp_Diffble_I_n with (PartInt x{-}PartInt x0).
  apply Derivative_I_n_minus.
  exact X1.
  exact X.
Qed.

(**
 * Use addition of functions to create a semigroup-operation
 * for infinitely-differentiable functions.
 *)
Program Instance C_inf_ab_plus : SemiGroupOp C_inf_ab := 
  (fun f g => Build_C_inf_ab 
    (@Fplus IR (f_crr f) (f_crr g)) _ ).

Next Obligation.
apply Diffble_I_n_plus.
destruct f.
assert (f_crr (Build_C_inf_ab f_crr0 f_pdf0) = f_crr0).
auto.
rewrite H.
apply f_pdf0.
destruct g.
assert (f_crr (Build_C_inf_ab f_crr0 f_pdf0) = f_crr0).
auto.
rewrite H.
apply f_pdf0.
Qed.

(**
 * The standard equality between functions can be continued
 * to an equality on C_inf_ab (the functions that are
 * infinitely-times differentiable).
 *)
Program Instance C_inf_ab_eq : Equiv C_inf_ab :=
  (fun f g => sq (Feq (Compact (less_leEq _ _ _ Hab)) 
    (f_crr f) (f_crr g))).

(**
 * The addition in C_inf_ab is associative. This is 
 * represented here using a type class instance.
 *)
Instance C_inf_ab_associative : @Associative C_inf_ab
  (C_inf_ab_eq) C_inf_ab_plus.
unfold Associative.
intros.
unfold C_inf_ab_plus.
red.
unfold C_inf_ab_eq.
apply insq.
simpl.
FEQ.
apply included_FPlus.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply included_FPlus.
destruct y.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct z.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply included_FPlus.
apply included_FPlus.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct y.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct z.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
Qed.

(**
 * The addition we defined on C_inf_ab is a morphism 
 * with respect to our equality.
 *)
Instance C_inf_ab_proper : Proper (C_inf_ab_eq ==>
  C_inf_ab_eq ==> C_inf_ab_eq) C_inf_ab_plus.
unfold Proper.
unfold respectful.
intros.
red in H.
apply unsq in H.
red in H0.
apply unsq in H0.
unfold C_inf_ab_eq.
apply insq.
unfold C_inf_ab_plus.
simpl.
apply Feq_plus.
exact H.
exact H0.
Qed.

(**
 * Short proof that the equality we defined is also an
 * equivalence.
 *)
Instance C_inf_ab_equivalence : Equivalence C_inf_ab_eq.
assert (Reflexive C_inf_ab_eq).
unfold Reflexive.
intros.
unfold C_inf_ab_eq.
apply insq.
apply Feq_reflexive.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) with
  f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
assert (Symmetric C_inf_ab_eq).
unfold Symmetric.
intros.
unfold C_inf_ab_eq in H0.
apply unsq in H0.
unfold C_inf_ab_eq.
apply insq.
apply Feq_symmetric.
exact H0.
assert (Transitive C_inf_ab_eq).
unfold Transitive.
intros.
unfold C_inf_ab_eq in H1, H2.
apply unsq in H1; apply unsq in H2.
unfold C_inf_ab_eq; apply insq.
apply Feq_transitive with (f_crr y).
exact H1.
exact H2.
apply (Build_Equivalence C_inf_ab (C_inf_ab_eq) 
  H H0 H1).
Qed.

(**
 * This makes the infinitely differentiable functions on
 * [a,b] a semigroup with respect to functional addition.
 *)
Instance C_inf_ab_semigroup : @SemiGroup C_inf_ab 
  C_inf_ab_eq C_inf_ab_plus.

(** 
 * An n-times differentiable function can be multiplied with
 * an arbitrary constant.
 *)
Lemma Diffble_I_n_const : forall (n : nat) (c : IR),
  Diffble_I_n Hab n (Fconst c).
Proof.
  intro n.
  induction n.
  intro c.
  simpl.
  unfold included. 
  auto.
  intro c. 
  simpl.
  assert (Diffble_I Hab [-C-] c).
  apply Diffble_I_const.
  exists X.
  destruct X.
  simpl.
  assert (Derivative_I Hab [-C-]c [-C-]Zero).
  apply Derivative_I_const.
  apply Diffble_I_n_wd with ([-C-]Zero).
  apply Derivative_I_unique with ([-C-]c).
  exact X.
  exact d.
  apply IHn.
Qed.

(**
 * The constant zero function is a neutral element in
 * the definition of C_inf_ab as a monoid under addition.
 *)
Instance C_inf_ab_mon_unit : @MonoidUnit C_inf_ab :=
  (Build_C_inf_ab (Fconst Zero) _).
intros.
apply Diffble_I_n_const.
Defined.

(**
 * The infinitely-differentiable functions on [a,b] are
 * a monoid under addition.
 *)
Instance C_inf_ab_monoid : @Monoid C_inf_ab
  C_inf_ab_eq C_inf_ab_plus C_inf_ab_mon_unit.
assert (forall x, C_inf_ab_plus C_inf_ab_mon_unit
  x == x).
intros.
unfold C_inf_ab_mon_unit.
red.
unfold C_inf_ab_eq.
apply insq.
unfold C_inf_ab_plus.
simpl.
FEQ.
apply included_FPlus.
simpl.
unfold included.
auto.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) 
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) 
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
assert (forall x, 
  C_inf_ab_plus x C_inf_ab_mon_unit == x).
intros.
unfold C_inf_ab_mon_unit.
red.
unfold C_inf_ab_eq.
apply insq.
unfold C_inf_ab_plus.
simpl.
FEQ.
apply included_FPlus.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) 
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
simpl.
unfold included.
auto.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) 
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply (Build_Monoid C_inf_ab C_inf_ab_eq
  C_inf_ab_plus C_inf_ab_mon_unit
  C_inf_ab_semigroup H H0).
Qed.

(**
 * If a function is n-times differentiable, its functional
 * inverse also has this property. To prove this, we first
 * need an exact representation of the nth derivative.
 *)
Lemma Derivative_I_n_inv : forall (n : nat) (f f' : PartFunct IR),
  Derivative_I_n Hab n f f' -> Derivative_I_n Hab n (Finv f) (Finv f').
Proof.
  intro.
  induction n.
  intros.
  simpl.
  simpl in X.
  apply Feq_inv.
  exact X.
  intros.
  simpl.
  destruct X.
  exists (IInv x).
  apply Derivative_I_wdr with (Finv (PartInt x)).  
  apply part_int_inv.
  apply Feq_reflexive.
  apply derivative_imp_inc' with f.
  exact d.
  apply Derivative_I_inv.
  exact d.
  apply Derivative_I_n_wdl with (Finv (PartInt x)).
  apply part_int_inv.
  apply Feq_reflexive.
  apply derivative_imp_inc' with f.
  exact d.
  apply IHn.
  exact d0.
Qed.
  

(**
 * This is a small lemma to get from the nth derivative to
 * the n-times differentiability of the functional inverse.
 *)
Lemma Diffble_I_n_inv : forall (n : nat) (f : PartFunct IR),
  Diffble_I_n Hab n f -> Diffble_I_n Hab n (Finv f).
Proof.
  intros.
  assert ({f0' : CSetoid_fun (subset (Compact (less_leEq _ _ _ Hab))) IR |
    Derivative_I_n Hab n f (PartInt f0')}).   
  apply Diffble_I_n_imp_deriv_n.
  exact X.
  elim X0.
  intros.
  apply deriv_n_imp_Diffble_I_n with (Finv (PartInt x)).
  apply Derivative_I_n_inv.
  exact p.
Qed.

(**
 * Type class instance to define the inverse on C_inf_ab
 * as a group inversion operation.
 *)
Program Instance C_inf_ab_inv : GroupInv C_inf_ab := 
  (fun f => Build_C_inf_ab (Finv (f_crr f)) _).

Next Obligation.
apply Diffble_I_n_inv.
destruct f.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) with
  f_crr0 by auto. 
apply f_pdf0.
Defined.

(**
 * This inversion operation on C_inf_ab is a morphism with
 * respect to the equality as previously defined on C_inf_ab.
 *)
Instance C_inf_ab_proper_inv : Proper (C_inf_ab_eq ==>
  C_inf_ab_eq) C_inf_ab_inv.
intros.
unfold Proper.
unfold respectful.
intros.
red.
apply insq.
unfold C_inf_ab_inv.
simpl.
apply Feq_inv.
apply unsq.
inversion H.
apply insq.
exact X.
Qed.

(**
 * The infinitely-differentiable functions on [a,b] form
 * a group with respect to addition, inversion and the 
 * constant zero function.
 *)
Instance C_inf_ab_group : @Group C_inf_ab C_inf_ab_eq
  C_inf_ab_plus C_inf_ab_mon_unit C_inf_ab_inv.
assert (forall x, C_inf_ab_plus (C_inf_ab_inv x) x 
  == C_inf_ab_mon_unit).
intros.
red.
unfold C_inf_ab_eq.
apply insq.
unfold C_inf_ab_plus.
unfold C_inf_ab_inv.
simpl.
FEQ.
apply included_FPlus.
apply included_FInv.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
assert (forall x, C_inf_ab_plus x (C_inf_ab_inv x) 
  == C_inf_ab_mon_unit).
intros.
red.
unfold C_inf_ab_eq.
apply insq.
unfold C_inf_ab_plus.
unfold C_inf_ab_inv.
simpl.
FEQ.
apply included_FPlus.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply included_FInv.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply (Build_Group C_inf_ab C_inf_ab_eq C_inf_ab_plus
  C_inf_ab_mon_unit C_inf_ab_inv C_inf_ab_monoid 
  C_inf_ab_proper_inv H H0).
Qed.

(**
 * The addition operator we defined is commutative, 
 * resulting in an Abelian group for C_inf_ab.
 *)
Instance C_inf_ab_abgroup : @AbGroup C_inf_ab C_inf_ab_eq
  C_inf_ab_plus C_inf_ab_mon_unit C_inf_ab_inv.
assert (Commutative C_inf_ab_plus).
unfold Commutative.
intros.
unfold C_inf_ab_plus.
red.
unfold C_inf_ab_eq.
apply insq.
simpl.
FEQ.
apply included_FPlus.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) with
  f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct y.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) with
  f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply included_FPlus.
destruct y.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) with
  f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) with
  f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply (Build_AbGroup C_inf_ab C_inf_ab_eq C_inf_ab_plus
  C_inf_ab_mon_unit C_inf_ab_inv C_inf_ab_group H).
Qed.

(**
 * If f' is the nth derivative if f, it is immediate that
 * c*f' is the nth derivative of c*f.
 *)
Lemma Derivative_I_n_cmul : forall (n : nat) (f f' : PartFunct IR)
  (c : IR), Derivative_I_n Hab n f f' -> 
  Derivative_I_n Hab n (c{**}f) (c{**}f').
Proof.
  intro n.
  induction n.
  intros.
  simpl.
  simpl in X.
  unfold Fscalmult.
  apply Feq_mult.
  apply Feq_reflexive.
  unfold included.
  simpl.
  auto.
  exact X.
  intros.
  simpl.
  destruct X.
  exists (IMult (IConst c) x).
  apply Derivative_I_wdr with ((Fconst c) {*} PartInt x).
  apply part_int_mult.
  apply part_int_const.
  apply Feq_reflexive.
  apply derivative_imp_inc' with f.
  exact d.
  apply Derivative_I_scal.
  exact d.
  apply Derivative_I_n_wdl with ((Fconst c) {*} PartInt x).
  apply part_int_mult.
  apply part_int_const.
  apply Feq_reflexive.
  apply derivative_imp_inc' with f.
  exact d.
  apply IHn.
  exact d0.
Qed.

(**
 * Because of the previous lemma, it is only a small step to
 * the n-times differentiability of constant-multiplication.
 *)
Lemma Diffble_I_n_cmul : forall (n : nat) (f : PartFunct IR)
  (c : IR), Diffble_I_n Hab n f -> Diffble_I_n Hab n (c{**}f).
Proof.
  intros.
  assert ({f0' : CSetoid_fun (subset (Compact (less_leEq _ _ _ Hab))) IR |
    Derivative_I_n Hab n f (PartInt f0')}).   
  apply Diffble_I_n_imp_deriv_n.
  exact X.
  inversion_clear X0.
  apply deriv_n_imp_Diffble_I_n with (c{**}PartInt x).
  apply Derivative_I_n_cmul.
  exact X1.
Qed.

(**
 * Define the algebra-action (terminology from NAH) as an
 * operation on C_inf_ab using type class instances.
 *)
Program Instance C_inf_ab_ralg_act : RalgebraAction IR C_inf_ab :=
  (fun c f => Build_C_inf_ab (c {**} (f_crr f)) _).

Next Obligation.
apply Diffble_I_n_cmul.
destruct f.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) with
  f_crr0 by auto.
apply f_pdf0.
Defined.

(**
 * Addition and multiplication on IR, as well as addition
 * on C_inf_ab can be used to prove that the infinitely-
 * differentiable functions on [a,b] are an R-module.
 *)
Instance C_inf_ab_rmodule : @Rmodule IR (@st_eq IR) 
  C_inf_ab C_inf_ab_eq C_inf_ab_ralg_act IR_plus IR_mult
  IR_inv IR_zero IR_one C_inf_ab_plus C_inf_ab_mon_unit
  C_inf_ab_inv.
assert (forall (a b : C_inf_ab) (x : IR), 
  C_inf_ab_ralg_act x (C_inf_ab_plus a b) == 
  C_inf_ab_plus (C_inf_ab_ralg_act x a) (C_inf_ab_ralg_act x b)).
intros.
unfold C_inf_ab_ralg_act.
unfold C_inf_ab_plus.
simpl.
red.
unfold C_inf_ab_eq.
apply insq.
simpl.
FEQ.
apply included_FMult.
unfold included; simpl.
auto.
apply included_FPlus.
destruct a0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) with
  f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct b0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) with
  f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply included_FPlus.
apply included_FMult.
unfold included; simpl; auto.
destruct a0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) with
  f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply included_FMult.
unfold included; simpl; auto.
destruct b0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) with
  f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
assert (forall (a : C_inf_ab) (x y : IR), 
  C_inf_ab_ralg_act (x [+] y) a == 
  C_inf_ab_plus (C_inf_ab_ralg_act x a) 
    (C_inf_ab_ralg_act y a)).
intros.
unfold C_inf_ab_ralg_act, C_inf_ab_plus.
simpl.
red.
unfold C_inf_ab_eq.
apply insq.
simpl.
FEQ.
apply included_FMult.
unfold included; simpl; auto.
destruct a0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) with
  f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply included_FPlus.
apply included_FMult.
unfold included; simpl; auto.
destruct a0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) with
  f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply included_FMult.
unfold included; simpl; auto.
destruct a0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) with
  f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
assert (forall (a : C_inf_ab) (x y : IR), 
  C_inf_ab_ralg_act (x [*] y) a == 
  C_inf_ab_ralg_act x (C_inf_ab_ralg_act y a)).
intros.
unfold C_inf_ab_ralg_act; simpl.
red; unfold C_inf_ab_eq; simpl.
apply insq; FEQ.
apply included_FMult.
unfold included; simpl; auto.
destruct a0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) with
  f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply included_FMult.
unfold included; simpl; auto.
apply included_FMult.
unfold included; simpl; auto.
destruct a0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) with
  f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply (Build_Rmodule IR (@st_eq IR) C_inf_ab C_inf_ab_eq
  C_inf_ab_ralg_act IR_plus IR_mult IR_inv IR_zero IR_one
  C_inf_ab_plus C_inf_ab_mon_unit C_inf_ab_inv IR_ring
  C_inf_ab_abgroup H H0 H1).
Qed.

(**
 * The property of n-times differentiability is preserved
 * under multiplication. 
 *)
Lemma Diffble_I_n_mult : forall (n : nat)
  (f g : PartFunct IR) (Pfdf : Diffble_I_n Hab n f)
  (Pgdf : Diffble_I_n Hab n g), 
  Diffble_I_n Hab n (Fmult f g).
Proof.  
  intro n.
  induction n.
  intros.
  simpl.
  apply included_conj.
  simpl in Pfdf.
  exact Pfdf.
  simpl in Pgdf.
  exact Pgdf.
  intros.
  simpl.
  elim Pfdf.
  intros.
  inversion x.
  elim Pgdf.
  intros.
  inversion x1.
  assert (included (Compact (less_leEq _ _ _ Hab)) (Dom f)).
  apply derivative_imp_inc with (PartInt x0).
  exact X.
  assert (included (Compact (less_leEq _ _ _ Hab)) (Dom g)).
  apply derivative_imp_inc with (PartInt x2).
  exact X0.
  assert (Diffble_I Hab (f{*}g)).
  apply Diffble_I_mult.
  exact x.
  exact x1.
  exists X3.
  destruct X3.
  simpl.
  assert (Derivative_I Hab (f{*}g) 
    ((f{*}PartInt x2){+}(PartInt x0{*}g))).
  apply Derivative_I_mult.
  exact X.
  exact X0.
  apply Diffble_I_n_wd with (f{*}PartInt x2{+}PartInt x0{*}g).
  apply Derivative_I_unique with (f{*}g).
  exact X3.
  exact d.
  apply Diffble_I_n_plus.
  apply IHn.
  apply le_imp_Diffble_I with (S n).
  omega.
  exact Pfdf.
  destruct x1.
  simpl in p0.
  apply Diffble_I_n_wd with (PartInt x1).
  apply Derivative_I_unique with g.
  exact d0.
  exact X0.
  exact p0.
  apply IHn.
  destruct x.
  simpl in p.
  apply Diffble_I_n_wd with (PartInt x).
  apply Derivative_I_unique with f.
  exact d0.
  exact X.
  exact p.
  apply le_imp_Diffble_I with (S n).
  omega.
  exact Pgdf.
Qed.

(** 
 * Using the previous lemma, we can come up with a 
 * straightforward (but lengthy) proof about the 
 * n-times differentiability of functional division.
 *)
Lemma Diffble_I_n_div : forall (n : nat)
  (f g : PartFunct IR) (Pfdf : Diffble_I_n Hab n f)
  (Pgdf : Diffble_I_n Hab n g) 
  (Pbnd : bnd_away_zero (Compact (less_leEq _ _ _ Hab)) g), 
  Diffble_I_n Hab n (Fdiv f g).
Proof.
  intro n.
  induction n.
  intros f0 g Hf0 Hg Hbnd.
  simpl.
  unfold included.
  intros.
  red.
  split.
  simpl in Hf0.
  unfold included in Hf0.
  apply Hf0.
  exact H.
  red.
  split.
  simpl in Hg.
  unfold included in Hg.
  apply Hg.
  exact H.  
  intros.
  elim Hbnd.
  intros.
  elim b0.
  intros.
  apply AbsIR_cancel_ap_zero.
  apply pos_ap_zero.
  apply less_leEq_trans with x0.
  exact p.
  apply q.
  exact H.
  intros.
  simpl.
  simpl in Pfdf.
  inversion Pfdf.
  inversion x.
  simpl in Pgdf.
  inversion Pgdf.
  inversion x1.
  assert (Derivative_I Hab (f{/}g)
    (((PartInt x0){*}g{-}f{*}(PartInt x2)){/}
    (g{*}g))).
  apply Derivative_I_div.
  exact X0.
  exact X2.
  exact Pbnd.
  assert (Diffble_I Hab (f{/}g)).
  apply deriv_imp_Diffble_I with
    ((PartInt x0{*}g{-}f{*}PartInt x2){/}g{*}g).
  exact X3.
  exists X4.
  destruct X4.
  simpl.
  apply Diffble_I_n_wd with
    ((PartInt x0{*}g{-}f{*}PartInt x2){/}g{*}g).
  apply Derivative_I_unique with (f{/}g).
  exact X3.
  exact d.
  apply IHn.
  apply Diffble_I_n_minus.
  apply Diffble_I_n_mult.
  replace n with (pred (S n)) by omega.
  apply Diffble_I_imp_le with f.
  omega.
  simpl.
  exists x.
  exact X.
  exact X0.
  apply le_imp_Diffble_I with (S n).
  omega.
  simpl.
  exact Pgdf.
  apply Diffble_I_n_mult.
  apply le_imp_Diffble_I with (S n).
  omega.
  simpl.
  exact Pfdf.
  replace n with (pred (S n)) by omega.
  apply Diffble_I_imp_le with g.
  omega.
  simpl.
  exact Pgdf.
  exact X2.
  apply Diffble_I_n_mult.
  apply le_imp_Diffble_I with (S n).
  omega.
  simpl.
  exact Pgdf.
  apply le_imp_Diffble_I with (S n).
  omega.
  simpl.
  exact Pgdf.
  unfold bnd_away_zero.
  split. 
  apply included_FMult.
  apply Diffble_I_n_imp_inc with (S n).
  simpl.
  exact Pgdf.
  apply Diffble_I_n_imp_inc with (S n).
  simpl.
  exact Pgdf.
  unfold bnd_away_zero in Pbnd.
  inversion Pbnd.
  inversion X5.
  exists (x4[*]x4).
  apply mult_resp_pos.
  exact X6.
  exact X6. 
  intros.
  rewrite AbsIR_resp_mult.
  apply mult_resp_leEq_both.
  algebra.
  algebra.
  apply H.
  exact H0.
  apply H.
  exact H0.
Qed.

(**
 * We are now able to create a type class instance of
 * multiplication on C_inf_ab as a semi-group operation.
 *)
Program Instance C_inf_ab_mult : SemiGroupOp C_inf_ab := 
  (fun f g => Build_C_inf_ab 
    (@Fmult IR (f_crr f) (f_crr g)) _ ).

Next Obligation.
apply Diffble_I_n_mult.
destruct f.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
  with f_crr0 by auto.
apply f_pdf0.
destruct g.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
  with f_crr0 by auto.
apply f_pdf0.
Qed.

(**
 * Multiplication on C_inf_ab is associative.
 *)
Instance C_inf_ab_associative_mult : @Associative C_inf_ab
  (C_inf_ab_eq) C_inf_ab_mult.
unfold Associative.
intros.
unfold C_inf_ab_mult; simpl.
red; unfold C_inf_ab_eq.
apply insq; simpl.
FEQ.
apply included_FMult.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply included_FMult.
destruct y.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct z.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply included_FMult.
apply included_FMult.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct y.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct z.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
Qed.

(**
 * Multiplication on C_inf_ab is a morphism with respect
 * to the equality we previously defined. 
 *)
Instance C_inf_ab_proper_mult : Proper (C_inf_ab_eq ==>
  C_inf_ab_eq ==> C_inf_ab_eq) C_inf_ab_mult.
unfold Proper.
unfold respectful.
intros.
unfold C_inf_ab_eq, C_inf_ab_mult.
apply insq; simpl.
apply Feq_mult.
apply unsq; inversion H.
apply insq.
exact X.
apply unsq; inversion H0.
apply insq.
exact X.
Qed.

(**
 * The type class instance of C_inf_ab as a semigroup
 * under multiplication.
 *)
Instance C_inf_ab_semigroup_mult : @SemiGroup C_inf_ab 
  C_inf_ab_eq C_inf_ab_mult.

(**
 * The constant function one is a neutral element in the
 * multiplication of C_inf_ab.
 *)
Instance C_inf_ab_mon_unit_mult : @MonoidUnit C_inf_ab :=
  (Build_C_inf_ab (Fconst One) _).
intros.
apply Diffble_I_n_const.
Defined.

(**
 * C_inf_ab is a monoid under multiplication with 
 * the constant function one as unit element.
 *)
Instance C_inf_ab_monoid_mult : @Monoid C_inf_ab
  C_inf_ab_eq C_inf_ab_mult C_inf_ab_mon_unit_mult.
assert (forall x, C_inf_ab_mult C_inf_ab_mon_unit_mult
  x == x).
intros.
unfold C_inf_ab_mon_unit_mult.
red.
unfold C_inf_ab_eq.
apply insq.
unfold C_inf_ab_mult.
simpl.
FEQ.
apply included_FMult.
simpl.
unfold included.
auto.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) 
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) 
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
assert (forall x, 
  C_inf_ab_mult x C_inf_ab_mon_unit_mult == x).
intros.
unfold C_inf_ab_mon_unit_mult.
red.
unfold C_inf_ab_eq.
apply insq.
unfold C_inf_ab_mult.
simpl.
FEQ.
apply included_FMult.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) 
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
simpl.
unfold included.
auto.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0)) 
  with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply (Build_Monoid C_inf_ab C_inf_ab_eq
  C_inf_ab_mult C_inf_ab_mon_unit_mult
  C_inf_ab_semigroup_mult H H0).
Qed.

(**
 * C_inf_ab is a ring with respect to its addition and
 * multiplication operators.
 *)
Instance C_inf_ab_ring : @Ring C_inf_ab C_inf_ab_eq
  C_inf_ab_plus C_inf_ab_mult C_inf_ab_inv
  C_inf_ab_mon_unit C_inf_ab_mon_unit_mult.
assert (Commutative C_inf_ab_mult).
unfold Commutative.
intros; red.
unfold C_inf_ab_mult, C_inf_ab_eq.
apply insq; simpl.
FEQ.
apply included_FMult.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct y.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply included_FMult.
destruct y.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct x.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
assert (Distribute C_inf_ab_mult C_inf_ab_plus).
assert (forall a b c, C_inf_ab_mult a (C_inf_ab_plus b c) ==
  C_inf_ab_plus (C_inf_ab_mult a b) (C_inf_ab_mult a c)).
intros.
unfold C_inf_ab_mult, C_inf_ab_plus; red; simpl.
unfold C_inf_ab_eq; simpl; apply insq; FEQ.
apply included_FMult.
destruct a0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply included_FPlus.
destruct b0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct c.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply included_FPlus.
apply included_FMult.
destruct a0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct b0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply included_FMult.
destruct a0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct c.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
assert (forall a b c, C_inf_ab_mult (C_inf_ab_plus a b) c ==
  C_inf_ab_plus (C_inf_ab_mult a c) (C_inf_ab_mult b c)).
intros.
unfold C_inf_ab_mult, C_inf_ab_plus; simpl; red.
unfold C_inf_ab_eq; simpl; apply insq; FEQ.
apply included_FMult.
apply included_FPlus.
destruct a0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct b0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct c.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply included_FPlus.
apply included_FMult.
destruct a0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct c.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply included_FMult.
destruct b0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct c.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply (Build_Distribute C_inf_ab C_inf_ab_eq
  C_inf_ab_mult C_inf_ab_plus H0 H1).
apply (Build_Ring C_inf_ab C_inf_ab_eq C_inf_ab_plus
  C_inf_ab_mult C_inf_ab_inv C_inf_ab_mon_unit C_inf_ab_mon_unit_mult
  C_inf_ab_abgroup C_inf_ab_monoid_mult H H0).
Qed.

(**
 * Together with the operations in IR, we may use the 
 * addition and multiplication in C_inf_ab to define
 * and R-algebra for the infinitely-differentiable functions.
 *)
Instance C_inf_ab_ralgebra : @Ralgebra IR (@st_eq IR) 
  C_inf_ab C_inf_ab_eq C_inf_ab_ralg_act IR_plus IR_mult
  IR_inv IR_zero IR_one C_inf_ab_plus C_inf_ab_mult 
  C_inf_ab_mon_unit C_inf_ab_mon_unit_mult C_inf_ab_inv.
assert (forall (a b : C_inf_ab) (x : IR), 
  C_inf_ab_ralg_act x (C_inf_ab_mult a b) == 
  C_inf_ab_mult (C_inf_ab_ralg_act x a) b).
intros.
unfold C_inf_ab_ralg_act, C_inf_ab_mult; red.
unfold C_inf_ab_eq; simpl.
apply insq; FEQ.
apply included_FMult.
unfold included; simpl; auto.
apply included_FMult.
destruct a0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct b0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply included_FMult.
apply included_FMult.
unfold included; simpl; auto.
destruct a0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
destruct b0.
replace (f_crr (Build_C_inf_ab f_crr0 f_pdf0))
 with f_crr0 by auto.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply (Build_Ralgebra IR (@st_eq IR) C_inf_ab C_inf_ab_eq
  C_inf_ab_ralg_act IR_plus IR_mult IR_inv IR_zero IR_one
  C_inf_ab_plus C_inf_ab_mult C_inf_ab_mon_unit C_inf_ab_mon_unit_mult
  C_inf_ab_inv C_inf_ab_rmodule C_inf_ab_ring H).
Qed.

(**
 * This is the definition of the abstract property of 
 * derivation on an R-algebra.
 *)
Definition is_derivation `(Ralgebra Scalar Elem) 
  (D : Elem -> Elem) : Prop.
repeat intro.
exact (
  (forall (a b : Elem), e' (elem_plus (D a) (D b)) (D (elem_plus a b))) and
  (forall (a : Elem) (c : Scalar), e' (H c (D a)) (D (H c a))) and
  (forall (a b : Elem), e' (D (elem_mult a b)) 
    (elem_plus (elem_mult a (D b)) (elem_mult (D a) b)))).
Defined.

(**
 * Define the derivative of a function in C_inf_ab as a
 * directly-callable operation. That is, the result of 
 * the expression is the required derivative.
 *)
Definition deriv_I_C_inf (f : C_inf_ab) : C_inf_ab.
intro.
destruct f.
assert (Diffble_I_n Hab 1 f_crr0).
apply f_pdf0.
set (n_deriv_I a b Hab 1 f_crr0 X).
assert (forall n, Diffble_I_n Hab n p).
intro n.
replace n with (pred (S n)) by omega.
apply Diffble_I_imp_le with f_crr0.
omega.
apply f_pdf0.
assert (Diffble_I_n Hab 0 f_crr0).
simpl.
apply Diffble_I_n_imp_inc with 42.
apply f_pdf0.
apply Derivative_I_wdl with (n_deriv_I a b Hab 0 f_crr0 X0).
simpl.
apply Feq_symmetric.
apply FRestr_wd.
apply n_Sn_deriv.
exact (Build_C_inf_ab p X0).
Defined.

(**
 * This corresponds to proposition 25 in the PDF as sent to 
 * me on january 11th. We prove that the derivation operator
 * deriv_I_C_inf on C_inf_ab is a derivation with respect
 * to the R_algebra we defined on C_inf_ab.
 *
 * TODO: This proof can be made shorter
 *)
Lemma lem18 : is_derivation C_inf_ab_ralgebra deriv_I_C_inf.
Proof.
  unfold is_derivation.
  split.
  intros.
  unfold C_inf_ab_eq.
  apply insq.
  unfold C_inf_ab_plus.
  destruct a0.
  destruct b0.
  assert (forall x y, f_crr
    (Build_C_inf_ab x y) = x).
  intros.
  auto.
  rewrite H.
  assert (f_crr (deriv_I_C_inf (Build_C_inf_ab f_crr0 f_pdf0)) = 
    n_deriv_I a b Hab 1 f_crr0 (f_pdf0 1)).
  auto.
  rewrite H0.
  assert (f_crr (deriv_I_C_inf (Build_C_inf_ab f_crr1 f_pdf1)) = 
    n_deriv_I a b Hab 1 f_crr1 (f_pdf1 1)).
  auto.
  rewrite H1.
  assert (f_crr
    (deriv_I_C_inf
      (Build_C_inf_ab
        (f_crr (Build_C_inf_ab f_crr0 f_pdf0){+}
         f_crr (Build_C_inf_ab f_crr1 f_pdf1))
           (C_inf_ab_plus_obligation_1 
              (Build_C_inf_ab f_crr0 f_pdf0)
              (Build_C_inf_ab f_crr1 f_pdf1)))) = 
    n_deriv_I a b Hab 1 
      (f_crr0{+}f_crr1)
      (C_inf_ab_plus_obligation_1 
        (Build_C_inf_ab f_crr0 f_pdf0) 
        (Build_C_inf_ab f_crr1 f_pdf1) 1)).
  auto.
  rewrite H2.
  clear H H0 H1 H2. 
  apply Feq_transitive with 
    (n_deriv_I a b Hab 1 (f_crr0{+}f_crr1) 
      (Diffble_I_n_plus 1 f_crr0 f_crr1 
        (f_pdf0 1) (f_pdf1 1))).
  assert ({f_crr0' : CSetoid_fun (subset (Compact (less_leEq _ _ _ Hab))) IR | 
   Derivative_I_n Hab 1 f_crr0 (PartInt f_crr0')}).
  apply Diffble_I_n_imp_deriv_n.
  apply f_pdf0.
  inversion_clear X.
  assert (Derivative_I_n Hab 1 f_crr0
    (n_deriv_I a b Hab 1 f_crr0 (f_pdf0 1))).  
  apply n_deriv_lemma.
  assert (Feq (Compact (less_leEq IR a b Hab))
    (PartInt x) (n_deriv_I a b Hab 1 f_crr0 (f_pdf0 1))).
  apply (Derivative_I_n_unique a b Hab 1 f_crr0).
  exact X0.
  exact X.
  assert ({f_crr1' : CSetoid_fun (subset (Compact (less_leEq _ _ _ Hab))) IR | 
   Derivative_I_n Hab 1 f_crr1 (PartInt f_crr1')}).
  apply Diffble_I_n_imp_deriv_n.
  apply f_pdf1.
  inversion_clear X2.
  assert (Derivative_I_n Hab 1 f_crr1
    (n_deriv_I a b Hab 1 f_crr1 (f_pdf1 1))).  
  apply n_deriv_lemma.
  assert (Feq (Compact (less_leEq IR a b Hab))
    (PartInt x0) (n_deriv_I a b Hab 1 f_crr1 (f_pdf1 1))).
  apply (Derivative_I_n_unique a b Hab 1 f_crr1).
  exact X3.
  exact X2.
  assert (Feq (Compact (less_leEq IR a b Hab)) 
    (PartInt x{+}PartInt x0)
    ((n_deriv_I a b Hab 1 f_crr0 (f_pdf0 1)){+}
     (n_deriv_I a b Hab 1 f_crr1 (f_pdf1 1)))).
  apply Feq_plus.
  exact X1.
  exact X4.
  apply Feq_transitive with (PartInt x{+}PartInt x0).
  apply Feq_plus.
  apply Feq_symmetric; exact X1.
  apply Feq_symmetric; exact X4.
  assert (Derivative_I_n Hab 1 (f_crr0{+}f_crr1)
    (PartInt x{+}PartInt x0)).
  apply Derivative_I_n_add.
  exact X0.
  exact X3.
  assert (Derivative_I_n Hab 1 (f_crr0{+}f_crr1)
    ((n_deriv_I a b Hab 1 (f_crr0{+}f_crr1)
     (Diffble_I_n_plus 1 f_crr0 f_crr1 (f_pdf0 1) (f_pdf1 1))))).
  apply n_deriv_lemma.
  apply (Derivative_I_n_unique a b Hab 1 
    (f_crr0{+}f_crr1)).
  exact X6.
  exact X7.
  apply n_deriv_I_wd.
  apply Feq_plus.
  apply Feq_reflexive.
  apply Diffble_I_n_imp_inc with 42.
  apply f_pdf0.
  apply Feq_reflexive.
  apply Diffble_I_n_imp_inc with 42.
  apply f_pdf1.
  split.
  intros.
  unfold C_inf_ab_eq.
  apply insq.
  unfold C_inf_ab_ralg_act.
  assert (forall x y, 
    f_crr (Build_C_inf_ab x y) = x) by auto.
  rewrite H.
  destruct a0.
  assert (f_crr (deriv_I_C_inf
    (Build_C_inf_ab f_crr0 f_pdf0)) =
    n_deriv_I a b Hab 1 f_crr0 (f_pdf0 1)).
  auto.
  rewrite H0.
  assert ( 
    f_crr
     (deriv_I_C_inf
       (Build_C_inf_ab (c{**}f_crr (Build_C_inf_ab f_crr0 f_pdf0)) 
       (C_inf_ab_ralg_act_obligation_1 c 
         (Build_C_inf_ab f_crr0 f_pdf0)))) =
    n_deriv_I a b Hab 1 (c{**}f_crr0)
      (C_inf_ab_ralg_act_obligation_1 c 
        (Build_C_inf_ab f_crr0 f_pdf0) 1)).
  auto.
  rewrite H1.
  clear H H0 H1.
  apply Feq_transitive with
    (n_deriv_I a b Hab 1 (c{**}f_crr0)
      (Diffble_I_n_cmul 1 f_crr0 c (f_pdf0 1))).
  assert ({f_crr0' : CSetoid_fun (subset (Compact (less_leEq _ _ _ Hab))) IR | 
    Derivative_I_n Hab 1 f_crr0 (PartInt f_crr0')}).
  apply Diffble_I_n_imp_deriv_n.
  apply f_pdf0.
  inversion_clear X.
  assert (Derivative_I_n Hab 1 f_crr0
    (n_deriv_I a b Hab 1 f_crr0 (f_pdf0 1))).
  apply n_deriv_lemma.
  assert (Feq (Compact (less_leEq IR a b Hab))
    (PartInt x) (n_deriv_I a b Hab 1 f_crr0 (f_pdf0 1))).
  apply (Derivative_I_n_unique a b Hab 1 f_crr0).
  exact X0.
  exact X.
  assert (Derivative_I_n Hab 1 (c{**}f_crr0)
    (c{**}PartInt x)).
  apply Derivative_I_n_cmul.
  exact X0.
  assert (Derivative_I_n Hab 1 (c{**}f_crr0) 
    ((n_deriv_I a b Hab 1 (c{**}f_crr0) 
      (Diffble_I_n_cmul 1 f_crr0 c (f_pdf0 1))))).
  apply n_deriv_lemma.
  apply Feq_transitive with (c{**}PartInt x).
  unfold Fscalmult.
  apply Feq_mult.
  apply Feq_reflexive.
  unfold included; simpl; auto.
  apply Feq_symmetric.
  exact X1.
  assert (Derivative_I_n Hab 1 (c{**}f_crr0)
    (c{**}(n_deriv_I a b Hab 1 f_crr0 (f_pdf0 1)))).
  apply Derivative_I_n_cmul.
  apply n_deriv_lemma.
  assert (Derivative_I_n Hab 1 (c{**}f_crr0)
    (c{**}PartInt x)).
  apply Derivative_I_n_cmul.
  exact X0.
  apply Feq_transitive with 
    (c{**}n_deriv_I a b Hab 1 f_crr0 (f_pdf0 1)).
  unfold Fscalmult.
  apply Feq_mult.
  apply Feq_reflexive.
  unfold included; simpl; auto.
  exact X1.
  apply Feq_transitive with (c{**}PartInt x).
  apply (Derivative_I_n_unique a b Hab 1 
    (c{**}f_crr0)). 
  exact X4.
  exact X2.
  apply Feq_transitive with
    (c{**}n_deriv_I a b Hab 1 f_crr0 (f_pdf0 1)).
  unfold Fscalmult.
  apply Feq_mult.
  apply Feq_reflexive.
  unfold included; simpl; auto.
  exact X1.
  apply (Derivative_I_n_unique a b Hab 1
    (c{**}f_crr0)).
  exact X4.
  exact X3.
  apply n_deriv_I_wd.
  unfold Fscalmult.
  apply Feq_mult.
  apply Feq_reflexive.
  unfold included; simpl; auto.
  apply Feq_reflexive.
  apply Diffble_I_n_imp_inc with 42.
  apply f_pdf0.
  intros.
  unfold C_inf_ab_eq. 
  apply insq.
  destruct a0.
  destruct b0.
  unfold C_inf_ab_mult, C_inf_ab_plus.
  set (f_crr (Build_C_inf_ab f_crr0 f_pdf0)).
  set (f_crr (Build_C_inf_ab f_crr1 f_pdf1)).
  set (deriv_I_C_inf (Build_C_inf_ab f_crr0 f_pdf0)).
  set (deriv_I_C_inf (Build_C_inf_ab f_crr1 f_pdf1)).
  set (Build_C_inf_ab f_crr0 f_pdf0).
  set (Build_C_inf_ab f_crr1 f_pdf1).
  replace 
    (f_crr
      (deriv_I_C_inf
        (Build_C_inf_ab (p{*}p0) 
           (C_inf_ab_mult_obligation_1 c1 c2))))
  with
    (n_deriv_I a b Hab 1 (p{*}p0)
      (C_inf_ab_mult_obligation_1 c1 c2 1)) by auto.
  replace
    (f_crr
      (Build_C_inf_ab
        (f_crr
           (Build_C_inf_ab (p{*}f_crr c0) 
           (C_inf_ab_mult_obligation_1 c1 c0)){+}
         f_crr
           (Build_C_inf_ab (f_crr c{*}p0) 
           (C_inf_ab_mult_obligation_1 c c2)))
        (C_inf_ab_plus_obligation_1
           (Build_C_inf_ab (p{*}f_crr c0) 
           (C_inf_ab_mult_obligation_1 c1 c0))
           (Build_C_inf_ab (f_crr c{*}p0) 
           (C_inf_ab_mult_obligation_1 c c2)))))
  with
    (f_crr
      (Build_C_inf_ab (p{*}f_crr c0) 
      (C_inf_ab_mult_obligation_1 c1 c0)){+}
     f_crr
      (Build_C_inf_ab (f_crr c{*}p0) 
      (C_inf_ab_mult_obligation_1 c c2))) by auto.
  replace (f_crr ((Build_C_inf_ab 
    (f_crr c{*}p0) 
    (C_inf_ab_mult_obligation_1 c c2))))
  with
    (f_crr c{*}p0) by auto.
  replace (f_crr (Build_C_inf_ab 
    (p{*}f_crr c0) 
    (C_inf_ab_mult_obligation_1 c1 c0)))
  with
    (p{*}f_crr c0) by auto.
  assert (Derivative_I_n Hab 1 (p{*}p0)
    (n_deriv_I a b Hab 1 (p{*}p0) 
      (C_inf_ab_mult_obligation_1 c1 c2 1))).
  apply n_deriv_lemma.
  assert ({f_crr0' : CSetoid_fun (subset (Compact (less_leEq _ _ _ Hab))) IR | 
    Derivative_I_n Hab 1 f_crr0 (PartInt f_crr0')}).
  apply Diffble_I_n_imp_deriv_n.
  apply f_pdf0.
  inversion_clear X0.
  assert ({f_crr1' : CSetoid_fun (subset (Compact (less_leEq _ _ _ Hab))) IR |
    Derivative_I_n Hab 1 f_crr1 (PartInt f_crr1')}).
  apply Diffble_I_n_imp_deriv_n.
  apply f_pdf1.
  inversion_clear X0.
  assert (Derivative_I_n Hab 1 (p{*}p0)
    (f_crr0{*}PartInt x0{+}PartInt x{*}f_crr1)).
  simpl.
  assert (included (Compact (less_leEq _ _ _ Hab)) (Dom 
    (f_crr0{*}PartInt x0{+}PartInt x{*}f_crr1))).
  apply included_FPlus.
  apply included_FMult.
  apply (Derivative_I_n_imp_inc a b Hab 1 f_crr0
    (PartInt x)).
  exact X1.
  apply (Derivative_I_n_imp_inc' a b Hab 1 
    f_crr1 (PartInt x0)).
  exact X2.
  apply included_FMult.
  apply (Derivative_I_n_imp_inc' a b Hab 1 
    f_crr0 (PartInt x)).
  exact X1.
  apply (Derivative_I_n_imp_inc a b Hab 1 f_crr1
    (PartInt x0)).
  exact X2.
  exists (@IntPartIR 
    (f_crr0{*}PartInt x0{+}PartInt x{*}f_crr1)
    a b (less_leEq _ _ _ Hab) X0).
  apply Derivative_I_wdr with 
    (f_crr0{*}PartInt x0{+}PartInt x{*}f_crr1).
  apply int_part_int.
  replace p with f_crr0 by auto.
  replace p0 with f_crr1 by auto.
  apply Derivative_I_mult.
  simpl in X1.
  inversion_clear X1.
  apply Derivative_I_wdr with (PartInt x1).
  exact X4.
  exact X3.
  simpl in X2.
  inversion_clear X2.
  apply Derivative_I_wdr with (PartInt x1).
  exact X4.
  exact X3.
  apply Feq_symmetric.
  apply int_part_int.
  apply Feq_transitive with
    (p{*}f_crr c0{+}f_crr c{*}p0).
  apply (Derivative_I_n_unique a b Hab 1 (p{*}p0)).
  exact X.
  replace p with f_crr0 by auto.
  replace p0 with f_crr1 by auto.
  replace (f_crr c0) with
    (n_deriv_I a b Hab 1 f_crr1 (f_pdf1 1)) by auto.
  replace (f_crr c) with
    (n_deriv_I a b Hab 1 f_crr0 (f_pdf0 1)) by auto.
  replace p with f_crr0 in X0 by auto.
  replace p0 with f_crr1 in X0 by auto.
  apply Derivative_I_n_wdr with
    (f_crr0{*}PartInt x0{+}PartInt x{*}f_crr1).
  apply Feq_plus.
  apply Feq_mult.
  apply Feq_reflexive.
  apply (Derivative_I_n_imp_inc a b Hab 1 f_crr0
    (PartInt x)).
  exact X1.
  assert (Derivative_I_n Hab 1 f_crr1
    (n_deriv_I a b Hab 1 f_crr1 (f_pdf1 1))).
  apply n_deriv_lemma.
  apply (Derivative_I_n_unique a b Hab 1 f_crr1).
  exact X2.
  apply n_deriv_lemma.
  apply Feq_mult.
  apply (Derivative_I_n_unique a b Hab 1 f_crr0).
  exact X1.
  apply n_deriv_lemma.
  apply Feq_reflexive.
  apply (Derivative_I_n_imp_inc a b Hab 1 f_crr1
    (PartInt x0)).
  exact X2.
  exact X0.
  apply Feq_reflexive.
  apply included_FPlus.
  apply included_FMult.
  replace p with f_crr0 by auto.
  apply (Derivative_I_n_imp_inc a b Hab 1 f_crr0
    (PartInt x)).
  exact X1.
  replace (f_crr c0) with
    (n_deriv_I a b Hab 1 f_crr1 (f_pdf1 1)) by auto.
  apply n_deriv_inc.
  apply included_FMult.
  replace (f_crr c) with
    (n_deriv_I a b Hab 1 f_crr0 (f_pdf0 1)) by auto.
  apply n_deriv_inc.
  replace p0 with f_crr1 by auto.
  apply (Derivative_I_n_imp_inc a b Hab 1 f_crr1
    (PartInt x0)).
  exact X2.
Qed.

End Derivations.

(**
 * This section contains lemmas and definitions about the 
 * function L. Note that the order in which things appear
 * in the proof differs from the order in the paper because
 * the proof is still based on a previous version of the
 * paper.
 *)

Section Lfunct.

Variables a b c d : IR.
Hypothesis Hab : a [<] b.
Hypothesis Hcd : c [<] d.

Hypothesis Hac : a [<=] c.
Hypothesis Hdb : d [<=] b.

(**
 * TODO: This lemma is unneccesary.
 *)
Lemma ab_diff : b [-] a [#] Zero.
Proof.
  apply minus_ap_zero.
  apply Greater_imp_ap.
  exact Hab.
Qed.

(**
 * We don't define L_inner as a direct lambda-expression
 * but instead as a composition of functions because this
 * enhances the usage of this definition in the proof.
 *)
Definition L_inner :=
  Fplus (Fconst c)
    (Fmult (Fminus (Fconst d) (Fconst c))
      (Fdiv (Fminus (Fid IR) (Fconst a))
        (Fminus (Fconst b) (Fconst a)))).

Definition L_func (f : C_inf_ab c d Hcd) :=
  Fcomp (f_crr c d Hcd f) L_inner.

(**
 * Prove that the function L is n-times differentiable for
 * each n. This effectively proves that the function L 
 * belongs to the class C_inf_ab.
 *)
Lemma L_diffble : forall (n : nat) (f : C_inf_ab c d Hcd), 
  Diffble_I_n Hab n  (L_func f).
Proof.
  intro n.
  induction n.
  intro f0.
  unfold L_func.
  simpl.
  unfold included.
  intros.
  assert (Conj (fun _ : IR => True) (Conj (Conj (fun _ : IR => True) 
    (fun _ : IR => True)) (Conj (Conj (fun _ : IR => True) 
    (fun _ : IR => True)) (extend (Conj (fun _ : IR => True) 
    (fun _ : IR => True)) (fun (x0 : IR) (_ : Conj (fun _ : IR => True) 
    (fun _ : IR => True) x0) => b[-]a[#]Zero)))) x).
  unfold extend.
  red.
  split.
  auto.
  red.
  split.
  red.
  auto.
  red.
  split.
  red.
  auto.
  split.
  red.
  auto.
  intro.
  apply ab_diff.
  exists X.
  destruct f0.
  replace (f_crr c d Hcd (Build_C_inf_ab c d Hcd f_crr0 f_pdf0))
    with f_crr0 by auto.
  destruct f_crr0.
  simpl.
  assert (included (Compact (less_leEq _ _ _ Hcd)) pfdom).
  assert (Diffble_I_n Hcd 0
    (Build_PartFunct IR pfdom dom_wd pfpfun pfstrx)).
  apply f_pdf0.
  simpl in X0.
  exact X0.
  unfold included in X0.
  apply X0.
  red.
  split.
  red in H.
  rewrite <- cm_rht_unit_unfolded at 1.
  apply plus_resp_leEq_lft.
  rewrite <- (cring_mult_zero_op IR Zero).
  apply mult_resp_leEq_both.
  apply leEq_reflexive.  
  apply leEq_reflexive.
  apply shift_leEq_lft.
  algebra.
  apply shift_leEq_div.
  apply shift_zero_less_minus.
  exact Hab.
  rewrite cring_mult_zero_op.
  apply shift_leEq_lft.
  inversion H.
  exact H0.
  apply shift_plus_leEq'.
  rewrite <- (mult_one IR (d[-]c)) at 2.
  apply mult_resp_leEq_both.
  apply shift_leEq_lft.
  algebra.
  apply shift_leEq_div.
  apply shift_zero_less_minus.
  exact Hab.
  rewrite cring_mult_zero_op.
  red in H.
  inversion H.
  apply shift_leEq_lft.
  exact H0.
  apply leEq_reflexive.
  apply shift_div_leEq.
  apply shift_zero_less_minus.
  exact Hab.
  rewrite mult_commutes.
  rewrite mult_one.
  apply minus_resp_leEq_both.
  red in H.
  inversion H.
  exact H1.
  apply leEq_reflexive.
  intro f0.
  simpl.
  assert (Derivative_I Hab L_inner
    (
      ([-C-] Zero) {+}
      (
        ([-C-] d {-} [-C-] c) {*}
          (
            Fdiv 
              ((([-C-] One) {*} ([-C-] b {-} [-C-] a)) {-}
               ((Fid IR {-} [-C-] a) {*} ([-C-] Zero)))
              (([-C-] b {-} [-C-] a) {*} ([-C-] b {-} [-C-] a))
          )
        {+}
        ([-C-] Zero) {*}
          (
            Fdiv (Fid IR {-} [-C-] a)
                 ([-C-] b {-} [-C-] a)
          )
      )
    )).
  unfold L_inner.
  apply Derivative_I_plus.
  apply Derivative_I_const.
  apply Derivative_I_mult.
  apply Derivative_I_wdr with ([-C-]Zero{-}[-C-]Zero).
  FEQ.
  apply Derivative_I_minus.
  apply Derivative_I_const.
  apply Derivative_I_const.
  apply Derivative_I_div.
  apply Derivative_I_wdr with ([-C-]One{-}[-C-]Zero).
  FEQ.
  apply Derivative_I_minus.
  apply Derivative_I_id.
  apply Derivative_I_const.
  apply Derivative_I_wdr with ([-C-]Zero{-}[-C-]Zero).
  FEQ.
  apply Derivative_I_minus.
  apply Derivative_I_const.
  apply Derivative_I_const.
  red.
  split.
  apply included_FMinus.
  unfold included; simpl; auto.
  unfold included; simpl; auto.
  exists (AbsIR (b[-]a)).
  apply AbsIR_pos.  
  apply ab_diff.
  intros.
  simpl.
  apply leEq_reflexive.
  assert (Diffble_I Hcd (f_crr c d Hcd f0)).  
  destruct f0; simpl.
  apply Diffble_I_n_imp_diffble with 42.
  omega.
  apply f_pdf0.
  destruct X0.
  assert (forall n, Diffble_I_n Hcd n (PartInt x)).
  intro n0.
  replace n0 with (pred (S n0)) by omega.
  apply Diffble_I_imp_le with (f_crr c d Hcd f0).
  omega.
  destruct f0.
  replace (f_crr c d Hcd (Build_C_inf_ab c d Hcd f_crr0 f_pdf0))
    with f_crr0 by auto.
  apply f_pdf0.
  exact d0.
  assert (Derivative_I Hab (L_func f0)
    ((L_func (Build_C_inf_ab c d Hcd (PartInt x) X0)) {*} 
    (([-C-]Zero{+}
       (([-C-]d{-}[-C-]c){*}
        (([-C-]One{*}([-C-]b{-}[-C-]a){-}(FId{-}[-C-]a){*}[-C-]Zero){/}
         ([-C-]b{-}[-C-]a){*}([-C-]b{-}[-C-]a)){+}
        [-C-]Zero{*}((FId{-}[-C-]a){/}([-C-]b{-}[-C-]a))))))
  ).
  unfold L_func.
  apply (Derivative_I_comp _ _ _ _ _ _ _ c d Hcd).
  exact X.
  simpl.
  exact d0.
  unfold maps_into_compacts.
  split.
  destruct f0.
  simpl.
  apply Diffble_I_n_imp_inc with 42.
  apply f_pdf0.
  intros.
  red.
  split.
  unfold L_inner.
  simpl.
  rewrite <- cm_rht_unit_unfolded at 1.
  apply plus_resp_leEq_lft.
  rewrite <- (cring_mult_zero_op IR Zero).
  apply mult_resp_leEq_both.
  apply leEq_reflexive.  
  apply leEq_reflexive.
  apply shift_leEq_lft.
  algebra.
  apply shift_leEq_div.
  apply shift_zero_less_minus.
  exact Hab.
  rewrite cring_mult_zero_op.
  apply shift_leEq_lft.
  inversion H.
  exact H0.
  unfold L_inner.
  simpl.
  apply shift_plus_leEq'.
  rewrite <- (mult_one IR (d[-]c)) at 2.
  apply mult_resp_leEq_both.
  apply shift_leEq_lft.
  algebra.
  apply shift_leEq_div.
  apply shift_zero_less_minus.
  exact Hab.
  rewrite cring_mult_zero_op.
  inversion H.
  apply shift_leEq_lft.
  exact H0.
  apply leEq_reflexive.
  apply shift_div_leEq.
  apply shift_zero_less_minus.
  exact Hab.
  rewrite mult_commutes.
  rewrite mult_one.
  apply minus_resp_leEq_both.
  inversion H.
  exact H1.
  apply leEq_reflexive.
  assert (Diffble_I Hab (L_func f0)).
  apply (deriv_imp_Diffble_I _ _ _ _ _ X1).
  exists X2.
  destruct X2.
  simpl.
  apply Diffble_I_n_wd with
    (L_func (Build_C_inf_ab c d Hcd (PartInt x) X0){*}
      ([-C-]Zero{+}(([-C-]d{-}[-C-]c){*}
       (([-C-]One{*}([-C-]b{-}[-C-]a){-}(FId{-}[-C-]a){*}[-C-]Zero){/}
       ([-C-]b{-}[-C-]a){*}([-C-]b{-}[-C-]a)){+}
       [-C-]Zero{*}((FId{-}[-C-]a){/}([-C-]b{-}[-C-]a))))).
  apply Derivative_I_unique with (L_func f0).
  exact X1.
  exact d1.
  apply Diffble_I_n_mult.
  apply IHn.
  apply Diffble_I_n_plus.
  apply Diffble_I_n_const.
  apply Diffble_I_n_plus.
  apply Diffble_I_n_mult.
  apply Diffble_I_n_minus.
  apply Diffble_I_n_const.
  apply Diffble_I_n_const.
  apply Diffble_I_n_div.
  apply Diffble_I_n_minus.
  apply Diffble_I_n_mult.
  apply Diffble_I_n_const.
  apply Diffble_I_n_minus.
  apply Diffble_I_n_const.
  apply Diffble_I_n_const.
  apply Diffble_I_n_wd with ([-C-] Zero).
  FEQ.
  apply Diffble_I_n_const.
  apply Diffble_I_n_mult.
  apply Diffble_I_n_minus.
  apply Diffble_I_n_const.
  apply Diffble_I_n_const.
  apply Diffble_I_n_minus.
  apply Diffble_I_n_const.
  apply Diffble_I_n_const.
  unfold bnd_away_zero.
  split.
  apply included_FMult.
  apply included_FMinus.
  unfold included; simpl; auto.
  unfold included; simpl; auto.
  apply included_FMinus.  
  unfold included; simpl; auto.
  unfold included; simpl; auto.
  exists ((b[-]a)[*](b[-]a)).
  apply mult_resp_pos.
  apply shift_zero_less_minus.
  exact Hab.
  apply shift_zero_less_minus.
  exact Hab.
  intros.
  rewrite AbsIR_resp_mult.
  apply mult_resp_leEq_both.
  apply shift_zero_leEq_minus.
  algebra.
  apply shift_zero_leEq_minus.
  algebra.
  apply eq_imp_leEq.
  symmetry.
  apply AbsIR_eq_x.
  apply shift_zero_leEq_minus.
  algebra.
  apply eq_imp_leEq.
  symmetry.
  apply AbsIR_eq_x.
  algebra.
  apply Diffble_I_n_wd with ([-C-] Zero).
  apply eq_imp_Feq.
  unfold included; simpl; auto.
  apply included_FMult.
  unfold included; simpl; auto.
  apply included_FDiv.
  apply included_FMinus.
  unfold included; simpl; auto.
  unfold included; simpl; auto.
  apply included_FMinus.
  unfold included; simpl; auto.
  unfold included; simpl; auto.
  intros.
  simpl.
  apply pos_ap_zero.
  apply shift_zero_less_minus.
  exact Hab.
  intros.
  simpl.
  rewrite mult_commutes.
  rewrite cring_mult_zero.
  reflexivity.
  apply Diffble_I_n_const.
Qed.

Lemma L_diffble_all_n : forall (f : C_inf_ab c d Hcd) 
  (n : nat), Diffble_I_n Hab n (L_func f).
Proof.
  intros.
  apply L_diffble.
Qed.

(**
 * Define L as a member of the class C_inf_ab. 
 *)
Definition L (f : C_inf_ab c d Hcd) : (C_inf_ab a b Hab) := 
  Build_C_inf_ab a b Hab (L_func f) 
    (L_diffble_all_n f).

End Lfunct.

(**
 * The following section is vague at best. Various versions
 * of lemmas (e.g. continuity) are used. One of the main
 * improvements should be to make things more consistent.
 *)

Section Integration.

Require Import ProductMetric.
Require Import CRIR.
Require Import Integration.
Require Import Qmetric.
Require Import QposMinMax.

Variable a b : CR.
Hypothesis Hab : a [<] b.

(**
 * Define equality on a sub-metricspace (a sub-metricspace
 * is a collection of elements from a metric space restricted
 * to certain property).
 *)
Definition eqSubMS (X : MetricSpace) 
  (P : X -> Prop) (a : {X | P X}) 
  (b : {X | P X}) := (projT1 a) [=] (projT1 b).

(**
 * Because a metric space is a setoid, it follows 
 * automatically that a sub-metricspace is also a setoid.
 *)
Definition setoidSubMS (X : MetricSpace) 
  (P : X -> Prop) : Setoid.
intros X P.
assert (Reflexive (eqSubMS X P)).
unfold Reflexive, eqSubMS.
intro x.
reflexivity.
assert (Symmetric (eqSubMS X P)).
unfold Symmetric, eqSubMS.
intros x y H42.
symmetry.
exact H42.
assert (Transitive (eqSubMS X P)).
unfold Transitive, eqSubMS.
intros x y z H37 H42.
transitivity (projT1 y).
exact H37.
exact H42.
apply (@Build_Setoid {X | P X} (eqSubMS X P)
  (Build_Equivalence {X | P X} (eqSubMS X P) H H0 H1)).
Defined.

(**
 * The usual definition of a ball can be used in a 
 * sub-metricspace as well. 
 *)
Program Definition ballSubMS (X : MetricSpace)
  (P : X -> Prop) (x : Qpos)
  (a : {X | P X}) (b : {X | P X}) := 
  (@ball X x a b).

(**
 * The actual definition of sub-metricspace. 
 *
 * TODO: Perhaps P should be directly dicidable (although
 * we axiomatized anything in Prop to be decidable using
 * sq and unsq).
 *)
Definition SubMS (X : MetricSpace) (P : X -> Prop) : 
  MetricSpace.
intros X P.
apply (@Build_MetricSpace (setoidSubMS X P) 
  (ballSubMS X P)).
intros e1 e2 H x1 x2 H0 y1 y2 H1.
split.
intro H2.
unfold ballSubMS.
unfold ballSubMS in H2.
assert ((`x1) [=] (`x2)).
destruct x1.
destruct x2.
auto.
assert ((`y1) [=] (`y2)).
destruct y1.
destruct y2.
auto.
apply (ball_wd X H (`x1) (`x2) H3 (`y1) (`y2) H4).
exact H2.
intro H2.
unfold ballSubMS.
unfold ballSubMS in H2.
assert ((`x1) [=] (`x2)).
destruct x1.
destruct x2.
auto.
assert ((`y1) [=] (`y2)).
destruct y1.
destruct y2.
auto.
apply (ball_wd X H (`x1) (`x2) H3 (`y1) (`y2) H4).
exact H2.
assert (forall e : Qpos, Reflexive (ballSubMS X P e)).
intro e; unfold Reflexive.
intro x; unfold ballSubMS.
apply (msp_refl (msp X) e (`x)).
assert (forall e : Qpos, Symmetric (ballSubMS X P e)).
intro e; unfold Symmetric.
intros x y H0; unfold ballSubMS; unfold ballSubMS in H0.
apply (msp_sym (msp X) e (`x) (`y)).
exact H0.
assert (forall (e1 e2 : Qpos) (a b c : (setoidSubMS X P)),
  (ballSubMS X P e1 a b) -> (ballSubMS X P e2 b c) ->
  (ballSubMS X P ((e1 + e2)%Qpos) a c)).
intros e1 e2 a0 b0 c H1 H2.
unfold ballSubMS; unfold ballSubMS in H1; unfold ballSubMS in H2.
apply (msp_triangle (msp X) _ _ _ (`b0)).
exact H1.
exact H2.
assert (forall (e : Qpos) (a b : (setoidSubMS X P)),
  (forall d : Qpos, ballSubMS X P ((e + d)%Qpos) a b) ->
  ballSubMS X P e a b).
intros e a0 b0 H2.
unfold ballSubMS.
apply (msp_closed (msp X)).
exact H2.
assert (forall a b : (setoidSubMS X P), (forall e : Qpos,
  ballSubMS X P e a b) -> a [=] b).
intros a0 b0 H3.
destruct a0.
destruct b0.
simpl.
unfold eqSubMS.
simpl.
apply (msp_eq (msp X)).
unfold ballSubMS in H3.
simpl in H3.
exact H3.
apply (Build_is_MetricSpace (setoidSubMS X P)
  (ballSubMS X P) H H0 H1 H2 H3).
Defined.

(**
 * The abstract concept of nultiplication of a 
 * MetricSpace (as in IR x IR etc). This results in
 * a product space (which is again a metric space).
 *)
Fixpoint XpowM (X : MetricSpace) (m : nat)
  {struct m} : MetricSpace :=
match m with
| 0 => X    (* Should not be used *)
| 1 => X
| S n => ProductMS X (XpowM X n)
end.

(**
 * If an element is known to be inside a sub-metricspace, it
 * can also be typed as an element from the original metric
 * space.
 *)
Definition unres (m : nat) (P : CR -> Prop) 
  (X : XpowM (SubMS CR P) m) : XpowM CR m.
intros m P x.
induction m.
simpl; simpl in x.
inversion x.
exact x0.
destruct m.
simpl.
simpl in x.
inversion x.
exact x0.
simpl.
split.
simpl in x.
inversion x.
inversion X.
exact x0.
apply IHm.
simpl in x.
inversion x.
exact X0.
Defined.

(**
 * The definition of continuity according to the book of
 * Bishop. Please note that this definition does not use
 * the continuity-definitions from CoRN/ftc. 
 *
 * TODO: Find a way to make continuity and differentiability
 * as consistent as possible throughout the proof.
 *)
Definition Bcont (m : nat) (X : MetricSpace) 
  (f : (XpowM CR m) -> X) := forall (a b : CR)
  (Hab : a [<] b) (g : (XpowM (SubMS CR 
  (fun x => sq (a [<] x and x [<] b))) m) -> X),
  (forall x : (XpowM (SubMS CR 
    (fun x => sq (a [<] x and x [<] b))) m), 
    (f (unres m (fun x => 
      sq (a [<] x and x [<] b)) x) [=] g x)) ->
  {m | is_UniformlyContinuousFunction g m}.

(**
 * Application of a pair of functions to an element from a
 * metric space resulting in an element of a product space.
 *)
Definition pairFG (m : nat) (X : MetricSpace) (Y : MetricSpace)
  (f : (XpowM CR m) -> X) (g : (XpowM CR m) -> Y) :  
  (XpowM CR m) -> ProductMS X Y.
intros m X Y f g x.
unfold ProductMS.
simpl; split.
apply f.
exact x.
apply g.
exact x.
Defined.

(**
 * This lemma corresponds to lemma 20 in the PDF as sent to
 * me on january 11th. It states that Bishop-continuity of
 * individual functions can be continued to pairs of functions.
 *)
Lemma lem20 : forall (m : nat) (X : MetricSpace) (Y : MetricSpace)
  (f : (XpowM CR m) -> X) (g : (XpowM CR m) -> Y)
  (Hf : Bcont m X f) (Hg : Bcont m Y g),
  Bcont m (ProductMS X Y) (pairFG m X Y f g).
Proof.
  intros m X Y f g Hf Hg.
  unfold Bcont.
  intros a0 b0 Ha0b0 g0 Hg0.
  unfold Bcont in Hf.
  assert ({f' : XpowM (SubMS CR
    (fun x : CR => sq (cof_less a0 x and cof_less x b0))) m -> X |
    (forall x : XpowM (SubMS CR (fun x : CR => sq (cof_less a0 x and cof_less x b0))) m,
      f (unres m (fun x0 : CR => sq (cof_less a0 x0 and cof_less x0 b0)) x) [=] f' x)}).
  exists (fun x => fst (g0 x)).
  intro x.
  unfold pairFG in Hg0.
  destruct (Hg0 x).
  rewrite <- H.
  simpl; reflexivity.
  inversion X0.
  elim (Hf a0 b0 Ha0b0 x H).
  intros mu1 H0.
  unfold Bcont in Hg.
  assert ({g' : XpowM (SubMS CR
    (fun x : CR => sq (cof_less a0 x and cof_less x b0))) m -> Y |
    (forall x : XpowM (SubMS CR (fun x : CR => sq (cof_less a0 x and cof_less x b0))) m,
      g (unres m (fun x0 : CR => sq (cof_less a0 x0 and cof_less x0 b0)) x) [=] g' x)}).
  exists (fun x => snd (g0 x)).
  intro x0.
  unfold pairFG in Hg0.
  destruct (Hg0 x0).
  rewrite <- H2.
  simpl; reflexivity.
  inversion X1.
  elim (Hg a0 b0 Ha0b0 x0 H1).
  intros mu2 H2.
  exists (fun x : Qpos => 
    QposInf_min (mu1 x) (mu2 x)).
  unfold is_UniformlyContinuousFunction.
  intros e a1 b1 H3.
  unfold is_UniformlyContinuousFunction in H0, H2.
  assert (ball_ex (mu1 e) a1 b1).
  apply ball_ex_weak_le with 
    (QposInf_min (mu1 e) (mu2 e)).
  apply QposInf_min_lb_l.
  exact H3.
  assert (ball_ex (mu2 e) a1 b1).
  apply ball_ex_weak_le with
    (QposInf_min (mu1 e) (mu2 e)).
  apply QposInf_min_lb_r.
  exact H3.
  assert (ball e (x a1) (x b1)).
  apply H0.
  exact H4.
  assert (ball e (x0 a1) (x0 b1)).
  apply H2.
  exact H5.
  simpl; unfold prod_ball; split.
  destruct (Hg0 a1).
  rewrite <- H8.
  destruct (Hg0 b1).
  rewrite <- H10.
  simpl.
  rewrite H.
  rewrite H.
  exact H6.
  simpl.
  destruct (Hg0 a1).
  rewrite <- H9.
  destruct (Hg0 b1).
  rewrite <- H11.
  simpl.
  rewrite H1.
  rewrite H1.
  exact H7.
Qed.

(**
 * TODO: This should be formulated either directly in the
 * proof or in a more general way.
 *)
Definition betw01 (x : CR) : Prop :=
  Zero [<=] x and x [<=] One.

Require Import QMinMax.
Require Import iso_CReals.

(**
 * A quick definition to have the computational reals (CR)
 * represented as CR^1.
 *)
Definition CRasCRpow1 (c : CR) : XpowM CR 1.
intro c.
simpl.
simpl in c.
exact c.
Defined.

(**
 * A somewhat general definition of a subspace of 
 * the rationals. 
 *)
Definition QsubMS (a b : Q) := SubMS Q_as_MetricSpace
  (fun x : Q_as_MetricSpace => (a <= x)%Q and (x <= b)%Q).

(**
 * Any element in a subset of Q is - of course - an element
 * in Q. 
 *)
Definition QsubMSasQ (a b : Q) (m : QsubMS a b) : Q.
intros a0 b0 m.
inversion m.
exact x.
Defined.

(**
 * A 'flatten' function because apparently total functions
 * can be handled better by CoRN compared tot partial 
 * funtions. 
 *
 * flat_raw takes a function f from ([a,b] <intersect> Q) to
 * R and returns a function f':Q -> R according to the 
 * following diagram:
 *                              
 *                       ---(b)--------------   
 *                      /
 *                     /
 *                  -- 
 *                 /
 *   --------(a)--/
 *
 * That is: the result of f'(x) in x<a and x>b is equal to
 * f(a) and f(b) respectively. 
 *)
Definition flat_raw (a b : Q) (Hab : (a < b)%Q) 
  (f : (QsubMS a b) -> CR) (Hf : Bcont 1 CR f) := 
  (fun x : Q_as_MetricSpace => 
    match (Qlt_le_dec_fast a x) with 
    | left _ => match (Qlt_le_dec_fast x b) with
                | left _ => f (CRasCRpow1 (IRasCR (inj_Q IR x)))
                | right _ => f (CRasCRpow1 (IRasCR (inj_Q IR b)))
                end
    | right _ => f (CRasCRpow1 (IRasCR (inj_Q IR a)))
    end
  ).

(**
 * A proof that the 'flatten' operation indeed preserves
 * uniform continuity.
 *)
Lemma flat_prf : forall (a b : Qpos) (Hab : (a < b)%Q)
  (f : (XpowM CR 1) -> CR) (Hf : Bcont 1 CR f), 
  {m | is_UniformlyContinuousFunction 
   (flat_raw a b Hab f Hf) m}.
Proof.
  intros a0 b0 Ha0b0 f Hf.
  unfold Bcont in Hf.
  set (IRasCR (inj_Q IR a0)).
  set (IRasCR (inj_Q IR b0)).
  assert ({g : XpowM (SubMS CR
    (fun x : CR => sq (cof_less s x and cof_less x s0))) 1 -> CR |
    (forall x : XpowM (SubMS CR (fun x : CR => sq (cof_less s x and cof_less x s0))) 1,
      f (unres 1 (fun x0 : CR => sq (cof_less s x0 and cof_less x0 s0)) x) [=] g x)}).
  exists (fun x : XpowM (SubMS CR 
    (fun x : CR => sq (cof_less s x and cof_less x s0))) 1 =>
      f (unres 1 (fun x0 : CR => sq (cof_less s x0 and cof_less x0 s0)) x)).
  intro x; reflexivity.
  inversion X.
  assert (cof_less s s0).
  unfold s.
  unfold s0.
  Admitted.

(**
 * A 'flattened' function typed as a uniformly continuous 
 * space.
 *)
Definition flat (a b : Qpos) (Hab : (a < b)%Q)
  (f : (XpowM CR 1) -> CR) (Hf : Bcont 1 CR f) : 
  UniformlyContinuousSpace Q_as_MetricSpace CR.
intros a0 b0 Ha0b0 f Hf.
set (flat_prf a0 b0 Ha0b0 f Hf).
inversion s.
exact (Build_UniformlyContinuousFunction H).
Defined.


(**
 * The is a repeated attempt at the definition of the
 * function L (see above). The function L0 is again a
 * translation function. It transforms a function 
 * f : ([a,b] <intersect> Q) -> R to a function 
 * f' : ([c,d] <intersect> Q) -> R. 
 *
 * TODO: Finish or reconsider this proof.
 *)
Definition L0_def (a b c d : Q) (Hab : (a < b)%Q) (Hcd : (c < d)%Q)
  (f : (QsubMS a b) -> CR) : (QsubMS c d) -> CR.
intros a0 b0 c d Ha0b0 Hcd f x.
inversion x.
set (Qplus c (Qmult (Qminus d c) (Qdiv 
  (Qminus (QsubMSasQ c d x) a0) (Qminus b0 a0)))).
cut ((a0 <= q)%Q and (q <= b0)%Q).
intro Hq.
apply f.
simpl.
exists q.
exact Hq.
Admitted.

(** 
 * A new definition of L0 in terms of Q as a metric space.
 *
 * TODO: This proof (and the whole L0 construct, for that
 * matter) should be set up in a different way.
 *)
Definition L0_ext_def (a b c d : Q) (Hab : (a < b)%Q)
  (Hcd : (c < d)%Q) (f : (QsubMS a b) -> CR) :
  Q_as_MetricSpace -> CR.
intros a0 b0 c d Ha0b0 Hcd f x.
case (Qlt_le_dec_fast c x).
intro H.
case (Qlt_le_dec_fast x d).
intro H0.
apply (L0_def a0 b0 c d Ha0b0 Hcd f).
simpl.
exists x.
split.
auto with *.
auto with *.
intro H0.
apply (L0_def a0 b0 c d Ha0b0 Hcd f).
simpl.
exists d.
split.
auto with *.
auto with *.
intro H.
apply (L0_def a0 b0 c d Ha0b0 Hcd f).
simpl.
exists c.
split.
auto with *.
auto with *.
Defined.

(**
 * TODO: Lemma should be replaced/removed.
 *)
Lemma L0_ext_uc : forall (a b c d : Q) (f : (QsubMS a b) -> CR)
  (Hab : (a < b)%Q) (Hcd : (c < d)%Q),
  {m : Qpos -> QposInf | is_UniformlyContinuousFunction 
    (L0_ext_def a b c d Hab Hcd f) m}.
Admitted.

(**
 * TODO: Definition should be replaced/removed.
 *)
Definition L0 (a b c d : Q) (f : (QsubMS a b) -> CR)
  (Hab : (a < b)%Q) (Hcd : (c < d)%Q) : 
  UniformlyContinuousSpace Q_as_MetricSpace CR.
intros a0 b0 c d f Ha0b0 Hcd.
set (L0_ext_uc a0 b0 c d f Ha0b0 Hcd).
inversion s.
exact (Build_UniformlyContinuousFunction H).
Defined.

(**
 * Integration as a total function from Q to R. However, this
 * part of the proof probably has to be set up in a different
 * way (as I indicated before during our discussions).
 *)
Definition F (a b : Qpos) (Hab : (a < b)%Q)
  (f : (XpowM CR 1) -> CR) (Hf : Bcont 1 CR f) :=
  (fun x : Q_as_MetricSpace =>
    match (Qlt_le_dec_fast x (1%Q)) with 
    | left _ => IRasCR Zero
    | right _ => (Integrate01 (flat a b Hab f Hf))
    end
  ).