aboutsummaryrefslogtreecommitdiffhomepage
path: root/contrib/graphs/cgraph.v
blob: 3f7203339d14eaea3aead2fcb0f604cf52d2b16c (plain)
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

(*s Decision procedure for arithmetic formulas by looking 
    for negative cycles in  a graph *)

Require Allmaps.
Require Arith.
Require ZArith.
Require Bool.
Require Sumbool.
Require PolyList.
Require Wf_nat.

Section ConstraintGraphs.

(*s Axiomatisation of the domain of interpretation [D] *)

  Variable D : Set.  

  Variable Dz : D.
  Variable Dplus : D -> D -> D.
  Variable Dneg : D -> D.
  Variable Dle : D -> D -> bool.

  Variable Dplus_d_z : (d:D) (Dplus d Dz)=d.
  Variable Dplus_z_d : (d:D) (Dplus Dz d)=d.
  Variable Dplus_assoc : 
   (d,d',d'':D) (Dplus (Dplus d d') d'')=(Dplus d (Dplus d' d'')).

  Variable Dplus_neg : (d:D) (Dplus d (Dneg d))=Dz.

  Variable Dle_refl : (d:D) (Dle d d)=true.
  Variable Dle_antisym : (d,d':D) (Dle d d')=true -> (Dle d' d)=true -> d=d'.
  Variable Dle_trans : 
   (d,d',d'':D) (Dle d d')=true -> (Dle d' d'')=true -> (Dle d d'')=true.
  Variable Dle_total : (d,d':D) {(Dle d d')=true}+{(Dle d' d)=true}.

  Variable Dle_plus_mono : 
   (d,d',d'',d''':D) (Dle d d')=true -> (Dle d'' d''')=true ->
      (Dle (Dplus d d'') (Dplus d' d'''))=true.

(*s Properties of [Dle] *)

Lemma Dle_true_permut : 
       (d,d':D) (Dle d d')=true -> {(Dle d' d)=false}+{d=d'}.
  Intros; Case (sumbool_of_bool (Dle d' d)); Intro; Auto.
  Save.

(*s Definition of the minimum function *)

Definition Dmin := [d,d':D] if (Dle d d') then d else d'.

Lemma Dle_true_Dmin : (d,d':D)(Dle d d')=true -> (Dmin d d')=d.
  Unfold Dmin; Intros d d' H; Rewrite H; Trivial.
  Save.

Lemma Dle_inv_Dmin : (d,d':D)(Dle d' d)=true -> (Dmin d d')=d'.
  Unfold Dmin; Intros d d' H.
  Case (sumbool_of_bool (Dle d d')); Intro H'; Rewrite H'; Auto.
  Save.

Hints Resolve Dle_true_Dmin Dle_inv_Dmin.

Lemma Dmin_idempotent : (d:D) (Dmin d d)=d.
  Proof.
    Auto.
  Qed.

Lemma Dmin_comm : (d,d':D) (Dmin d d')=(Dmin d' d).
  Proof.
    Intros d d'; Case (Dle_total d d'); Intro H. 
    Rewrite Dle_true_Dmin with 1:= H.
    Rewrite Dle_inv_Dmin with 1:= H; Trivial.
    Rewrite Dle_true_Dmin with 1:= H.
    Rewrite Dle_inv_Dmin with 1:= H; Trivial.
  Save.

Lemma Dmin_assoc 
    : (d,d',d'':D) (Dmin (Dmin d d') d'')=(Dmin d (Dmin d' d'')).
  Proof.
    Intros d d' d''; Case (Dle_total d d'); Intro H.
    Rewrite Dle_true_Dmin with 1:= H.
    Case (Dle_total d' d''); Intro H'.
    Rewrite Dle_true_Dmin with 1:= H'.
    Rewrite Dle_true_Dmin with 1:= H.
    Rewrite (Dle_true_Dmin d d''); EAuto.
    Rewrite Dle_inv_Dmin with 1:= H'; Trivial.
    Rewrite Dle_inv_Dmin with 1:= H.
    Case (Dle_total d' d''); Intro H'.
    Rewrite Dle_true_Dmin with 1:= H'.
    Rewrite Dle_inv_Dmin with 1:= H; Trivial.
    Rewrite Dle_inv_Dmin with 1:= H'.
    Rewrite (Dle_inv_Dmin d d''); EAuto.
Save.

Lemma Dmin_le_1 : (d,d':D) (Dle (Dmin d d') d)=true.
  Proof.
    Intros. Case (Dle_total d d'); Intro H.
    Rewrite Dle_true_Dmin with 1:= H; Auto.
    Rewrite Dle_inv_Dmin with 1:= H; Auto.
  Qed.

Lemma Dmin_le_2 : (d,d':D) (Dle (Dmin d d') d')=true.
  Proof.
    Intros. Rewrite (Dmin_comm d d'). Apply Dmin_le_1.
  Qed.

Lemma Dmin_le_3 : (d,d',d'':D) (Dle d (Dmin d' d''))=true -> (Dle d d')=true.
  Proof.
    Intros d d' d''; Elim (Dle_total d' d''); Intro H0.
    Rewrite Dle_true_Dmin with 1:=H0; Trivial.
    Rewrite Dle_inv_Dmin with 1:=H0; EAuto.
  Qed.

Lemma Dmin_le_4 : (d,d',d'':D) (Dle d (Dmin d' d''))=true -> (Dle d d'')=true.
  Proof.
    Intros. Rewrite (Dmin_comm d' d'') in H. Exact (Dmin_le_3 ? ? ? H).
  Qed.

Lemma Dmin_le_5 : (d,d',d'':D) (Dle d d')=true -> (Dle d d'')=true ->
      (Dle d (Dmin d' d''))=true.
  Proof.
    Intros. Unfold Dmin. Case (Dle d' d''); Assumption.
  Qed.

(*s Properties of [Dneg] *)

Lemma Dneg_Dz : (Dneg Dz)=Dz.
  Proof.
    Rewrite <- (Dplus_z_d (Dneg Dz)). Apply Dplus_neg.
  Qed.

Lemma Dneg_neg : (d:D) (Dneg (Dneg d))=d.
  Proof.
    Intro. Rewrite <- (Dplus_z_d (Dneg (Dneg d))). Rewrite <- (Dplus_neg d).
    Rewrite Dplus_assoc. Rewrite Dplus_neg. Apply Dplus_d_z.
  Qed.

Lemma Dplus_neg_2 : (d:D) (Dplus (Dneg d) d)=Dz.
  Proof.
    Intro. Cut (Dplus (Dneg d) (Dneg (Dneg d)))=Dz. Rewrite Dneg_neg. Trivial.
    Apply Dplus_neg.
  Qed.

(*s Properties of [Dplus] *)

Lemma Dplus_reg_l 
   : (d,d',d'':D) (Dle (Dplus d'' d) (Dplus d'' d'))=true 
               -> (Dle d d')=true.
  Proof.
    Intros. Rewrite <- (Dplus_z_d d). Rewrite <- (Dplus_z_d d'). Rewrite <- (Dplus_neg_2 d'').
    Rewrite (Dplus_assoc (Dneg d'') d'' d). Rewrite (Dplus_assoc (Dneg d'') d'' d').
    Apply Dle_plus_mono. Apply Dle_refl.
    Assumption.
  Qed.

Lemma Dplus_reg_r 
   : (d,d',d'':D) (Dle (Dplus d d'') (Dplus d' d''))=true -> (Dle d d')=true.
  Proof.
    Intros. Rewrite <- (Dplus_d_z d). Rewrite <- (Dplus_d_z d'). Rewrite <- (Dplus_neg d'').
    Rewrite <- (Dplus_assoc d d'' (Dneg d'')). Rewrite <- (Dplus_assoc d' d'' (Dneg d'')).
    Apply Dle_plus_mono. Assumption.
    Apply Dle_refl.
  Qed.

Lemma Dmin_plus_l 
   : (d,d',d'':D) (Dplus (Dmin d d') d'')=(Dmin (Dplus d d'') (Dplus d' d'')).
  Proof.
    Intros d d' d''; Case (Dle_total d d'); Intro H.
    Rewrite Dle_true_Dmin with 1:=H.
    Rewrite (Dle_true_Dmin (Dplus d d'')); Auto.
    Rewrite Dle_inv_Dmin with 1:=H.
    Rewrite (Dle_inv_Dmin (Dplus d d'')); Auto.
  Qed.

Lemma Dmin_plus_r 
   : (d,d',d'':D) (Dplus d'' (Dmin d d'))=(Dmin (Dplus d'' d) (Dplus d'' d')).
  Proof.
   Intros d d' d''; Case (Dle_total d d'); Intro H.
    Rewrite Dle_true_Dmin with 1:=H.
    Rewrite (Dle_true_Dmin (Dplus d'' d)); Auto.
    Rewrite Dle_inv_Dmin with 1:=H.
    Rewrite (Dle_inv_Dmin (Dplus d'' d)); Auto.
  Qed.

Lemma Dle_neg : (d:D) (Dle Dz d)=true -> (Dle (Dneg d) Dz)=true.
  Proof.
    Intros. Cut (Dle (Dplus Dz (Dneg d)) (Dplus d (Dneg d)))=true. Intro.
    Rewrite (Dplus_z_d (Dneg d)) in H0. Rewrite (Dplus_neg d) in H0. Assumption.
    Exact (Dle_plus_mono ? ? ? ? H (Dle_refl ?)).
  Qed.

Lemma Dle_neg_2 : (d:D) (Dle d Dz)=true -> (Dle Dz (Dneg d))=true.
  Proof.
    Intros. Cut (Dle (Dplus d (Dneg d)) (Dplus Dz (Dneg d)))=true. Intro.
    Rewrite (Dplus_neg d) in H0. Rewrite (Dplus_z_d (Dneg d)) in H0. Assumption.
    Exact (Dle_plus_mono ? ? ? ? H (Dle_refl ?)).
  Qed.
 
Lemma Dnotle_not_eq : (d,d':D)(Dle d d')=false ->~d=d'.
  Red; Intros.
  Apply diff_true_false.
  Rewrite H0 in H.  
  Rewrite (Dle_refl d') in H; Trivial.
  Save.

Hints Immediate Dnotle_not_eq.

Lemma Dnotle_not_eq_sym : (d,d':D) (Dle d d')=false -> ~d'=d.
  Proof.
    Intros; Apply sym_not_eq; Auto.
  Qed.

Hints Immediate Dnotle_not_eq_sym.

(*s Equality on [D] is decidable *)

Lemma D_dec : (d,d':D) {d=d'}+{~d=d'}.
  Proof.
    Intros d d'; Case (sumbool_of_bool (Dle d d')); Intro H; Auto. 
    Case (sumbool_of_bool (Dle d' d)); Intro H0; Auto.
  Qed.

Lemma Dnotle_3_cases 
  : (d,d':D) {(Dle d d')=false}+{d=d'}+{(Dle d' d)=false}.
  Proof.
   Intros d d'; Case (sumbool_of_bool (Dle d d')); Intro H; Auto. 
    Case (sumbool_of_bool (Dle d' d)); Intro H0; Auto.
  Qed.

Lemma Dle_noteq_notle 
   : (d,d':D) (Dle d' d)=true -> ~d=d' -> (Dle d d')=false.
  Proof.
    Intros d d'; Case (sumbool_of_bool (Dle d d')); Intro H; Auto. 
    Intros; Absurd d=d'; Auto.
  Qed.

Lemma Dnotle_not_refl 
   : (d:D) ~(Dle d d)=false.
  Proof.
    Red; Intro;  Rewrite (Dle_refl d).
    Exact diff_true_false.
  Qed.


Lemma Dnotle_elim : 
  (d,d':D) (Dle d' d)=false -> ((Dle d d')=true /\ ~d=d').
  Intros.
  Case (Dle_total d d'); Intro H'; Auto.
  Case diff_true_false.
  Rewrite <- H'; Trivial.
Save.  

Lemma Dnotle_trans : (d,d',d'':D) (Dle d d')=false -> (Dle d' d'')=false -> (Dle d d'')=false.
  Proof.
   Intros.
   Case Dnotle_elim with 1:= H; Intros.
   Case Dnotle_elim with 1:= H0; Intros.
   Apply Dle_noteq_notle; EAuto.
   Red; Intros.
   Apply diff_false_true.
   Rewrite <- H0.
   Rewrite <- H5; Trivial.
   Save.

Lemma Dnotle_le_1 : (d,d':D) (Dle d d')=false -> (Dle d' d)=true.
  Proof.
    Intros; Case Dnotle_elim with 1:= H; Trivial.
  Qed.

Lemma Dmin_le_distr_l : (d,d',d'':D) (Dle (Dmin d d') d'')=(orb (Dle d d'') (Dle d' d'')).
  Proof.
    Intros; Case (Dle_total d d'); Intro H.
    Rewrite Dle_true_Dmin with 1:= H.
    Pattern (Dle d d''); Apply bool_eq_ind; Intro H'; Simpl; Trivial.
    Pattern (Dle d' d''); Apply bool_eq_ind; Intro H''; Simpl; Trivial.
    Rewrite <- H'; EAuto.
    Rewrite Dle_inv_Dmin with 1:= H.
    Pattern (Dle d d''); Apply bool_eq_ind; Intro H'; Simpl; EAuto.
    Save.

Lemma Dmin_choice : (d,d':D) {(Dmin d d')=d}+{(Dmin d d')=d'}.
  Proof.
    Unfold Dmin. Intros. Elim (sumbool_of_bool (Dle d d')). Intro H. Left . Rewrite H. Reflexivity.
    Intro H. Right . Rewrite H. Reflexivity.
  Qed.

Lemma Dnotle_noteq : (d,d':D) (Dle d d')=false -> ~d=d'.
  Proof.
    Unfold not. Intros. Rewrite H0 in H. Rewrite (Dle_refl d') in H. Discriminate H.
  Qed.

Lemma Dneg_plus : (d,d':D) (Dneg (Dplus d d'))=(Dplus (Dneg d') (Dneg d)).
  Proof.
    Intros. Cut (Dplus (Dplus d d') (Dplus (Dneg d') (Dneg d)))=Dz. Intro.
    Rewrite <- (Dplus_d_z (Dneg (Dplus d d'))). Rewrite <- H. Rewrite <- Dplus_assoc.
    Rewrite Dplus_neg_2. Rewrite Dplus_z_d. Reflexivity.
    Rewrite Dplus_assoc. Rewrite <- (Dplus_assoc d' (Dneg d') (Dneg d)). Rewrite Dplus_neg.
    Rewrite Dplus_z_d. Apply Dplus_neg.
  Qed.

Lemma Dneg_le : (d,d':D) (Dle d d')=true -> (Dle (Dneg d') (Dneg d))=true.
  Proof.
    Intros. Apply Dplus_reg_l with d'':=d'. Rewrite Dplus_neg. Rewrite <- (Dneg_neg d').
    Rewrite <- Dneg_plus. Apply Dle_neg_2. Rewrite <- (Dplus_neg d').
    Exact (Dle_plus_mono ? ? ? ? H (Dle_refl ?)).
  Qed.

Lemma Dnotle_plus_mono_1 : (d,d',d'',d''':D) (Dle d' d)=true -> (Dle d'' d''')=false
      	  -> (Dle (Dplus d d'') (Dplus d' d'''))=false.
  Proof.
    Intros. Apply Dle_noteq_notle. Apply Dle_plus_mono. Assumption.
    Apply Dnotle_le_1. Assumption.
    Unfold not. Intro H1.
    Cut (Dle (Dplus (Dneg d) (Dplus d d'')) (Dplus (Dneg d') (Dplus d' d''')))=true.
    Rewrite <- (Dplus_assoc (Dneg d) d d''). Rewrite Dplus_neg_2. Rewrite Dplus_z_d.
    Rewrite <- Dplus_assoc. Rewrite Dplus_neg_2. Rewrite Dplus_z_d. Rewrite H0.
    Intro H2. Discriminate H2.
    Apply Dle_plus_mono. Apply Dneg_le. Assumption.
    Rewrite H1. Apply Dle_refl.
  Qed.

Lemma Dnotle_plus_mono : (d,d',d'',d''':D) (Dle d d')=false -> (Dle d'' d''')=false
      	  -> (Dle (Dplus d d'') (Dplus d' d'''))=false.
  Proof.
    Intros. (Apply Dnotle_plus_mono_1; Try Assumption). (Apply Dnotle_le_1; Assumption).
  Qed.

(*s Extending D with an element NONE representing infinity *)

Definition Ddmin := [dd,dd':(option D)] Cases dd dd' of
                                              NONE _ => dd'
					    | _ NONE => dd
					    | (SOME d) (SOME d') => (SOME D (Dmin d d'))
					  end.

Lemma Ddmin_idempotent : (dd:(option D)) (Ddmin dd dd)=dd.
  Proof.
    Induction dd. Trivial.
    Intro. Simpl. Rewrite Dmin_idempotent. Reflexivity.
  Qed.

Lemma Ddmin_comm : (dd,dd':(option D)) (Ddmin dd dd')=(Ddmin dd' dd).
  Proof.
    Induction dd. Intro. (Case dd'; Reflexivity).
    Intro d. Induction dd'. Reflexivity.
    Intro d'. Simpl. Rewrite Dmin_comm. Reflexivity.
  Qed.

Lemma Ddmin_assoc : (dd,dd',dd'':(option D))
      (Ddmin (Ddmin dd dd') dd'')=(Ddmin dd (Ddmin dd' dd'')).
  Proof.
    Intros. Case dd. Simpl. Case dd'. Simpl. (Case dd''; Reflexivity).
    Intro. Simpl. (Case dd''; Reflexivity).
    Intro. Simpl. Case dd'. Simpl. (Case dd''; Reflexivity).
    Intro d'. Simpl. Case dd''. Reflexivity.
    Intro d''. Rewrite Dmin_assoc. Reflexivity.
  Qed.

(*s The order [Dle] extended to $D_{\infty}$ *)
Definition Ddle 
  := [dd,dd':(option D)] Cases dd dd' of
                               _ NONE => true
			  | NONE _    => false
                          | (SOME d) (SOME d') => (Dle d d')
		         end.

Lemma Ddle_refl : (dd:(option D)) (Ddle dd dd)=true.
  Proof.
    Intro. Case dd. Reflexivity.
    Intro. Exact (Dle_refl d).
  Qed.

Lemma Ddle_antisym 
  : (dd,dd':(option D)) (Ddle dd dd')=true -> (Ddle dd' dd)=true -> dd=dd'.
  Proof.
    Intros dd dd'. Case dd. Case dd'. Trivial.
    Intros. Discriminate H.
    Case dd'. Intros. Discriminate H0.
    Intros. Rewrite (Dle_antisym ? ? H0 H). Reflexivity.
  Qed.

Lemma Ddle_trans 
  : (dd,dd',dd'':(option D)) 
    (Ddle dd dd')=true -> (Ddle dd' dd'')=true -> (Ddle dd dd'')=true.
  Proof.
    Intros dd dd' dd''. Case dd''. (Case dd; Trivial).
    Intro d''. Case dd'. Intros. Discriminate H0.
    Intro d'. Case dd. Intro. Discriminate H.
    Intros d H H0. Exact (Dle_trans ? ? ? H H0).
  Qed.

Lemma Ddle_d_none : (dd:(option D)) (Ddle dd (NONE D))=true.
  Proof.
    Induction dd; Trivial.
  Qed.

Lemma Ddmin_le_1 : (dd,dd':(option D)) (Ddle (Ddmin dd dd') dd)=true.
  Proof.
    Intros. Case dd. (Case dd'; Reflexivity).
    Intro d. Case dd'. Apply Ddle_refl.
    Exact (Dmin_le_1 d).
  Qed.

Lemma Ddmin_le_2 : (dd,dd':(option D)) (Ddle (Ddmin dd dd') dd')=true.
  Proof.
    Intros. Rewrite (Ddmin_comm dd dd'). Apply Ddmin_le_1.
  Qed.

Lemma Ddmin_le_3 
  : (dd,dd',dd'':(option D)) 
    (Ddle dd (Ddmin dd' dd''))=true -> (Ddle dd dd')=true.
  Proof.
    Intros dd dd' dd''. Case dd'. (Case dd; Trivial).
    Intro d'. Case dd. Case dd''. Intro. Discriminate H.
    Intros d'' H. Discriminate H.
    Intro d. Case dd''. Trivial.
    Exact (Dmin_le_3 d d').
  Qed.

Lemma Ddmin_le_4 : (dd,dd',dd'':(option D)) (Ddle dd (Ddmin dd' dd''))=true ->
      (Ddle dd dd'')=true.
  Proof.
    Intros. Rewrite (Ddmin_comm dd' dd'') in H. Exact (Ddmin_le_3 ? ? ? H).
  Qed.

Lemma Ddmin_le_distr_l 
  : (dd,dd',dd'':(option D))
    (Ddle (Ddmin dd dd') dd'')=(orb (Ddle dd dd'') (Ddle dd' dd'')).
  Proof.
    Induction dd. Induction dd'. (Induction dd''; Trivial).
    Intro d. (Induction dd''; Trivial).
    Intro d. Induction dd'. (Induction dd''; Trivial).
    Simpl. Intro d'. Rewrite orb_b_false. Reflexivity.
    Intro d'. (Induction dd''; Trivial). Simpl. Exact (Dmin_le_distr_l d d').
  Qed.

Lemma Ddmin_choice 
  : (dd,dd':(option D)) {(Ddmin dd dd')=dd}+{(Ddmin dd dd')=dd'}.
  Proof.
    Induction dd. Intro. Right . Simpl. (Case dd'; Reflexivity).
    Intro d. Induction dd'. Left . Reflexivity.
    Intro d'. Simpl. Elim (Dmin_choice d d'). Intro H. Left . Rewrite H. Reflexivity.
    Intro H. Right . Rewrite H. Reflexivity.
  Qed.

(*s Operation [Ddplus] on $D_{\infty}$ *)

Definition Ddplus := [dd:(option D)][d':D]
      Cases dd of
          (SOME d) => (SOME ? (Dplus d d'))
	| _ => dd
      end.

Lemma Ddmin_plus_l 
  : (dd,dd':(option D)) (d'':D)
    (Ddplus (Ddmin dd dd') d'')=(Ddmin (Ddplus dd d'') (Ddplus dd' d'')).
  Proof.
    Induction dd. Induction dd'. Trivial.
    Trivial.
    Intro d. Induction dd'. Trivial.
    Intros d' d''. Simpl. Rewrite Dmin_plus_l. Reflexivity.
  Qed.

Lemma Ddle_plus_mono 
  : (dd,dd':(option D)) (d,d':D)
    (Ddle dd dd')=true -> (Dle d d')=true ->
        (Ddle (Ddplus dd d) (Ddplus dd' d'))=true.
  Proof.
    Induction dd. Induction dd'. Intros. Trivial.
    Simpl. Intros. Assumption.
    Intro d0. Induction dd'. Trivial.
    Simpl. Exact (Dle_plus_mono d0).
  Qed.

Lemma Ddplus_reg_r 
  : (dd,dd':(option D)) (d'':D)
        (Ddle (Ddplus dd d'') (Ddplus dd' d''))=true->(Ddle dd dd')=true.
  Proof.
    Induction dd. Induction dd'. Trivial.
    Simpl. Trivial.
    Intro d. Induction dd'. Trivial.
    Intros d' d'' H. Exact (Dplus_reg_r d d' d'' H).
  Qed.

(*s Introducing graphs of objects in [D] *)

Definition CGraph1 := (Map (Map D)).

Definition CGraph := (option CGraph1).

Section CGDist.

    Variable cg : CGraph1.

Definition CG_edge := [x,y:ad]
      	Cases (MapGet ? cg x) of
	    (SOME edges) => Cases (MapGet ? edges y) of
                                (SOME d) => (SOME D d)
		              | _ => (NONE D)
			    end
          | _ => (NONE D)
        end.

(*s Let $\rho$ be an interpretation of adresses as elements in [D], 
    the graph [cg] is satisfied by $\rho$ if for any edge 
    from $x$ to $y$ indexed by $d$, we have $\rho(x) \leq \rho(y)+d$
   A graph is consistent if there exists an interpretation which satisfies it.
*)

Definition CGsat 
  := [rho:ad->D] 
     (x,y:ad) (d:D)
     (CG_edge x y)=(SOME D d) -> (Dle (rho x) (Dplus (rho y) d))=true.


Definition CGconsistent := (sig ? CGsat). 

(* [CG_path last d l] if there exists a path starting from [last] with successive 
   vertexes $l=[x0;...;xn]$ $(xn=last)$ and [d] is the sum of the weights on the 
   edges *)

Inductive CG_path [last : ad] : D -> (list ad) -> Set :=
        CG_p1 : (x:ad) x=last -> (CG_path last Dz (cons x (nil ?)))
      | CG_p2 : (x,y:ad) (l:(list ad)) (d:D) (CG_path last d (cons y l)) ->
      	          (d':D) (CG_edge x y)=(SOME D d') ->
                    (CG_path last (Dplus d d') (cons x (cons y l))).

(* If $\rho$ satisfies the graph and there is a path from [last] to [x] of 
   weight d then $\rho(x) \leq \rho(last)+d$
*)

Definition first : (list ad) -> ad := [l]Cases l of nil => ad_z | (cons x _) => x end.

Lemma CG_path_head 
  : (l:(list ad)) (last:ad) (d:D) (CG_path last d l) ->
      	(rho:ad->D) (CGsat rho) -> (Dle (rho (first l)) (Dplus (rho last) d))=true.
    Proof.
    Intros; Elim H; Simpl; Intros.
    Rewrite e; Rewrite Dplus_d_z; Auto.
    Apply Dle_trans with (Dplus (rho y) d'); Auto.
    Rewrite <- Dplus_assoc; Auto.
    Save.

Lemma CG_path_correct 
  : (l:(list ad)) (x,last:ad) (d:D) (CG_path last d (cons x l)) ->
      	(rho:ad->D) (CGsat rho) -> (Dle (rho x) (Dplus (rho last) d))=true.
    Proof.
    Intros; Apply (CG_path_head (cons x l) last d); Trivial.
    Save.

(*s If there is a circuit [(cons x l)] with negative weight [d], then [cg] is inconsistent: *)

Theorem CG_circuit_correct 
  : (x:ad) (d:D) (l:(list ad))
    (CG_path x d (cons x l)) -> (Dle Dz d)=false -> CGconsistent -> False.
    Proof.
      Intros. Unfold CGconsistent in H1. Elim H1. Intros rho H2.
      Cut (Dle (Dplus (rho x) Dz) (Dplus (rho x) d))=true. Intro H3.
      Rewrite (Dplus_reg_l ? ? ? H3) in H0. Discriminate H0.
      Rewrite Dplus_d_z. Exact (CG_path_correct l x x d H rho H2).
    Qed.

Section CGConsistent.

      Variable P : CGconsistent.

(*s Assuming that [cg] is consistent, we can build a distance d(x,y) as follows:
      	 d(x,y) is the length of the shortest path from x to y (or +infty if none). *)

Lemma CG_circuits_non_negative_weight 
  : (x:ad) (d:D) (l:(list ad)) (CG_path x d (cons x l)) -> (Dle Dz d)=true.
      Proof.
      	Intros. Elim (sumbool_of_bool (Dle Dz d)). Trivial.
	Intro H0. Elim (CG_circuit_correct x d l H H0 P).
      Qed.

End CGConsistent.

(*s We assume that any cycle has a positive weight *)

Section CGNoBadCycles.

      Variable no_bad_cycles : (x:ad) (d:D) (l:(list ad))
      	 (CG_path x d (cons x l)) -> (Dle Dz d)=true.

(*s The edges are in the domain of the graph *)

Lemma CG_edge_in_cg_1 
  : (x,y:ad) (d:D) (CG_edge x y)=(SOME D d) -> (in_FSet x (MapDom ? cg))=true.
      Proof.
      	Unfold CG_edge. Intros x y d. 
	Elim (option_sum ? (MapGet (Map D) cg x)). Intro H.
      	Elim H. Intros edges H0. Rewrite H0. Intros. 
	Exact (MapDom_semantics_1 ? cg x edges H0).
	Intros H H0. Rewrite H in H0. Discriminate H0.
      Qed.


(*s The elements of a path are in the domain of the graph extended with 
    the last element *)
Lemma CG_path_in_cg_1 
  : (l:(list ad)) (last:ad) (d:D)
    (CG_path last d l) -> (MapSubset ? ? (Elems l)
                                 (MapPut ? (MapDom ? cg) last tt)).
      Proof.
      
      	Induction 1.
        Unfold MapSubset; Unfold Elems; Simpl; Intros.
        Rewrite in_dom_put.
        Rewrite in_dom_M1 in H0.
        Rewrite <- e.
        Rewrite (ad_eq_comm a x).
        Rewrite H0; Simpl; Trivial.
        Unfold MapSubset.
        Intros; Rewrite in_dom_put.
        Change (in_dom unit a 
                 (MapPut unit (Elems (cons y l0)) x tt))=true in H1.
        Rewrite in_dom_put in H1.
        Case orb_prop with 1:=H1; Intro.
      	Rewrite (ad_eq_complete ? ? H2).
        Change 
         (orb (ad_eq x last) (in_FSet x (MapDom (Map D) cg)))=true.
	Rewrite CG_edge_in_cg_1 with 1:=e; Auto with bool.
        LetTac H3:=(H0 a H2).
	Rewrite in_dom_put in H3.
	Trivial.
Save.

Lemma CG_path_last 
  : (l:(list ad)) (last:ad) (d:D)
    (CG_path last d l) -> {l':(list ad) | l=(app l' (cons last (nil ad)))}.
      Proof.
      	Induction 1; Intros. 
        Exists (nil ad); Simpl; Rewrite e; Trivial.
	Case H0; Intros l' H1.
        Exists (cons x l'); Simpl.
        Rewrite H1; Auto.
      Qed.

(*s The length of a path without repetition is less than the
   cardinal of the map representing the graph *)

Lemma ad_simple_path_bounded_card 
  : (l:(list ad)) (last,x:ad) (d:D)
    (CG_path last d (cons x l)) -> (ad_list_stutters (cons x l))=false
	    -> (le (length (cons x l)) (S (MapCard ? cg))).
      Proof.
      	Intros. Apply le_trans with m:=(MapCard ? (MapPut ? (MapDom ? cg) last tt)).
      	Rewrite (ad_list_not_stutters_card (cons x l) H0). Apply MapSubset_Card_le.
      	Apply CG_path_in_cg_1 with 1:=H; Trivial.
	Rewrite (MapCard_Dom ? cg). Apply MapCard_Put_ub.
      Qed.

Lemma CG_path_app_1 : (l1,l2:(list ad)) (last,x:ad) (d1,d2:D)
      	  (CG_path last d2 (cons x l2)) -> (CG_path x d1 l1) ->
	    (CG_path last (Dplus d2 d1) (app l1 l2)).
      Proof.
        Intros.
        Elim H0; Intros.
        Simpl; Rewrite Dplus_d_z; Rewrite e; Trivial.
        Rewrite <- Dplus_assoc.
        Simpl; Constructor; Auto.
      Save.

Lemma CG_path_app_2 : (l1,l2:(list ad)) (last,x:ad) (d:D)
      	  (CG_path last d (app l1 (cons x l2))) ->
	    {d2 : D & (CG_path last d2 (cons x l2))}.
      Proof.
      	Induction l1. Simpl. Intros. Split with d. Assumption.
	Simpl. Induction l. Intros. Simpl in H0. Inversion H0. Split with d0. Assumption.
	Intros. Simpl in H1. Inversion H1. Exact (H0 l2 last x d0 H5).
      Qed.

Lemma CG_path_app_3 : (l1,l2:(list ad)) (last,x:ad) (d:D)
      	  (CG_path last d (app l1 (cons x l2))) ->
	    {d1 : D & (CG_path x d1 (app l1 (cons x (nil ad))))}.
      Proof.
      	Induction l1. Simpl. Intros. Split with Dz. Apply CG_p1. Reflexivity.
	Induction l. Simpl. Intros. Inversion H0. Split with d'. Rewrite <- (Dplus_z_d d').
      	Apply CG_p2. Apply CG_p1. Reflexivity.
	Assumption.
	Simpl. Intros. Inversion H1. Elim (H0 ? ? ? ? H5). Intros d1 H9. Split with (Dplus d1 d').
      	Apply CG_p2. Assumption.
	Assumption.
      Qed.

Lemma CG_path_weight_and_last_unique 
  : (l:(list ad)) (last,last':ad) (d,d':D)
    (CG_path last d l) -> (CG_path last' d' l) -> d=d' /\ last=last'.
      Proof.
        Intros l last last' d d' H; Generalize d'.
      	Elim H; Intros. 
	Inversion H0. 
	Split; Trivial. Transitivity x; Auto.
        Inversion H1.
        Case H0 with 1:=H5; Split; Intros; Trivial.
        Rewrite H8.
        Replace d'0 with d'2; Auto.
        Cut (SOME D d'2)=(SOME D d'0).
        Intro E; Injection E; Trivial.
        Transitivity (CG_edge x y); Auto.
      Qed.

    Inductive and_sp [A:Set; B:Prop] : Set := conj_sp : A -> B -> (and_sp A B).

(*s Given a path, one may find a shortest path withour repetition *)

Lemma ad_path_then_simple_path : 
          (l:(list ad)) (last:ad) (d:D)
      	  (CG_path last d l) ->
	    {sl:(list ad) & {d0:D & 
                   (and_sp (CG_path last d0 (cons (first l) sl))
                           ((ad_list_stutters (cons (first l) sl))=false /\
				              (Dle d0 d)=true))}}.
      Proof.
      	Induction 1; Unfold first; Intros.
        Exists (nil ad); Exists Dz; Split; Auto.
        Constructor; Auto.
        Case H0; Clear H0; Intros sl (d1,(H1,(H2,H3))).
        Elim (sumbool_of_bool (ad_in_list x (cons y sl))); Intro.
        Case (ad_in_list_forms_circuit x (cons y sl) a); Intros.
        Case s; Clear a s; Intros l2 H4.
        Case (CG_path_app_2 x0 l2 last x d1).
        Rewrite <- H4; Trivial.
        Intros d2 H5; Exists l2; Exists d2.
        Split; Trivial.
        Split.
        Apply (ad_list_stutters_app_conv_r x0).
	Rewrite <- H4; Trivial.
        Apply Dle_trans with (Dplus d1 d').
	Case (CG_path_app_3 (cons x x0) l2 last x (Dplus d1 d')).
        Simpl; Rewrite <- H4; Constructor; Trivial.
        Intros d3 H6.
        Replace (Dplus d1 d') with (Dplus d2 d3).
        Apply Dle_trans with (Dplus d2 Dz).
        Rewrite Dplus_d_z; Auto.
        Apply Dle_plus_mono; Trivial.
        Apply (no_bad_cycles x d3 (app x0 (cons x (nil ad)))); Auto.
        Case (CG_path_weight_and_last_unique 
                (app (app (cons x x0) (cons x (nil ad))) l2) 
                last last (Dplus d2 d3) (Dplus d1 d')); Auto.
        Apply CG_path_app_1 with x; Trivial.
        Replace (app (app (cons x x0) (cons x (nil ad))) l2)
        with (cons x (cons y sl)); Auto.
        Constructor; Auto.
        Rewrite H4; Rewrite <- ass_app; Simpl; Trivial.
        Apply Dle_plus_mono; Trivial.
	Exists (cons y sl); Exists (Dplus d1 d'); Repeat Split; Auto.
        Constructor; Auto.
        Change (orb (ad_in_list x (cons y sl)) (ad_list_stutters (cons y sl))) = false.
        Apply orb_false_intro; Trivial.
      Qed.

Lemma CG_path_app_4 : (l1,l2:(list ad)) (last,x:ad) (d:D)
      	  (CG_path last d (app l1 (cons x l2))) ->
	    {d1 : D & {d2 : D &
	          (and_sp (CG_path x d1 (app l1 (cons x (nil ad)))) *
		          (CG_path last d2 (cons x l2))
			  d=(Dplus d2 d1))}}.
      Proof.
      	Intros. Elim (CG_path_app_2 l1 l2 last x d). Intros d1 H0.
      	Elim (CG_path_app_3 l1 l2 last x d). Intros d2 H1. Split with d2. Split with d1.
      	Split. Exact (H1,H0).
	Cut (app l1 (cons x l2))=(app (app l1 (cons x (nil ad))) l2). 
	Intro. Rewrite H2 in H.
      	Elim (CG_path_weight_and_last_unique ? ? ? ? ? H
               (CG_path_app_1 (app l1 (cons x (nil ad))) l2 last x d2 d1 H0 H1)).
      	Trivial.
	Exact (ass_app l1 (cons x (nil ad)) l2).
	Assumption.
	Assumption.
      Qed.

(*s [(ad_simple_path_naive_search x y prefix n)] is true when 
    there esists a path [x::l] from [y] to [x] of length less than 
    [n] with edges not in prefix
*)

Fixpoint ad_simple_path_naive_search [x,y:ad; l:(list ad); n:nat] : bool :=
        (orb (ad_eq x y)
      	Cases n of
	    O => false
	  | (S n') => Cases (MapGet ? cg x) of
	                  NONE => false
			| (SOME edges) =>
                          let l'=(cons x l) in (* builds reverse path *)
                          Cases (MapSweep D
			           [z:ad][d:D] if (ad_in_list z l')
                                               then false
				               else (ad_simple_path_naive_search z y l' n')
				   edges) of
			      NONE => false
			    | (SOME _) => true
			  end
                     end
        end).

Lemma ad_simple_path_naive_search_correct_1 
  : (n:nat) (x,y:ad) (l:(list ad)) (d:D)
    (le (length l) n) -> (CG_path y d (cons x l)) ->
    (prefix:(list ad))
      	    (ad_list_stutters (app (rev prefix) (cons x l)))=false ->
	    (ad_simple_path_naive_search x y prefix n)=true.
      Proof.
      	Induction n. Intros x y l. Case l. Intros. Inversion H0. 
        Rewrite H4. Simpl.
      	Rewrite (ad_eq_correct y). Reflexivity.
	Intros. Elim (le_Sn_O ? H).
	Intros n0 H x y l. Case l. Intros. Inversion H1. Rewrite H5. Simpl.
      	Rewrite (ad_eq_correct y). Reflexivity.
	Intros. Simpl. Elim (sumbool_of_bool (ad_eq x y)). Intro H3. Rewrite H3. Reflexivity.
	Intro H3. Rewrite H3. Simpl. Elim (option_sum ? (MapGet ? cg x)). Intro H4. Elim H4.
      	Clear H4. Intros edges H4. Rewrite H4. Inversion_clear H1. Unfold CG_edge in H6.
      	Rewrite H4 in H6. Elim (option_sum ? (MapGet D edges a)). Intro H7. Elim H7. Clear H7.
      	Intros d'' H7. Rewrite H7 in H6.
      	Cut (if (ad_in_list a (cons x prefix))
             then false
             else (ad_simple_path_naive_search a y (cons x prefix) n0))=true.
      	Intro. Elim (MapSweep_semantics_4 D [z:ad] [_:D]
         (if (orb (ad_eq z x) (ad_in_list z prefix))
          then false
          else (ad_simple_path_naive_search z y (cons x prefix) n0))
             edges a d'' H7 H1).
      	Intros a' H8. Elim H8. Intros d1 H9. Rewrite H9. Reflexivity.
	Rewrite (ad_list_app_rev prefix (cons a l0) x) in H2.
      	Rewrite <- (ad_in_list_rev (cons x prefix) a).
      	Rewrite (ad_list_stutters_prev_conv_l ? ? ? H2).
      	Exact (H a y l0 d0 (le_S_n ? ? H0) H5 (cons x prefix) H2).
	Intro H7. Rewrite H7 in H6. Discriminate H6.
	Intro H4. Inversion_clear H1. Unfold CG_edge in H6. Rewrite H4 in H6. Discriminate H6.
      Qed.

Lemma ad_simple_path_naive_search_correct 
  : (n:nat) (x,y:ad) (l:(list ad)) (d:D)
    (le (length l) n) -> (CG_path y d (cons x l)) 
    -> (ad_list_stutters (cons x l))=false 
    -> (ad_simple_path_naive_search x y (nil ad) n)=true.
      Proof.
      	Intros. Exact (ad_simple_path_naive_search_correct_1 n x y l d H H0 (nil ad) H1).
      Qed.

Lemma ad_simple_path_naive_search_complete_1 
  : (n:nat) (x,y:ad) (prefix:(list ad))
    (d':D) (CG_path x d' (rev (cons x prefix))) ->
      	   (ad_list_stutters (cons x prefix))=false ->
      	   (ad_simple_path_naive_search x y prefix n)=true ->
	     {d:D & {l:(list ad) & (and_sp (CG_path y d (app (rev (cons x prefix)) l))
	                                   (ad_list_stutters (app (rev (cons x prefix)) l))
                                                =false)}}.
      Proof.
      	Induction n. Intros. Split with d'. Split with (nil ad). Simpl in H1.
      	Rewrite (orb_b_false (ad_eq x y)) in H1. Rewrite <- (ad_eq_complete ? ? H1).
      	Rewrite <- app_nil_end. Split. Assumption.
	Rewrite ad_list_stutters_rev. Assumption.
	Intros. Simpl in H2. Elim (orb_true_elim ? ? H2). Intro H3.
      	Rewrite <- (ad_eq_complete ? ? H3). Split with d'. Split with (nil ad).
      	Rewrite <- app_nil_end. Split. Assumption.
	Rewrite ad_list_stutters_rev. Assumption.
	Intro H3. Clear H2. Elim (option_sum ? (MapGet ? cg x)). Intro H4. Elim H4.
      	Intros edges H5. Rewrite H5 in H3.
      	Elim (option_sum ? (MapSweep D [z:ad] [_:D]
                            (if (orb (ad_eq z x) (ad_in_list z prefix))
                             then false
                             else (ad_simple_path_naive_search z y (cons x prefix) n0)) edges)).
      	Intro H2. Elim H2. Intro r. Elim r. Intros x0 d0 H6.
      	Cut (if (ad_in_list x0 (cons x prefix))
             then false
             else (ad_simple_path_naive_search x0 y (cons x prefix) n0))=true.
      	Intro. Elim (sumbool_of_bool (ad_in_list x0 (cons x prefix))). Intro H8.
      	Rewrite H8 in H7. Discriminate H7.
	Intro H8. Rewrite H8 in H7. Clear H2 H3. Elim (H x0 y (cons x prefix) (Dplus d0 d')).
      	Intros d1 H9. Elim H9. Intros l H10. Elim H10. Intros H11 H12.
      	Rewrite <- (ad_list_app_rev (cons x prefix) l x0) in H11.
      	Rewrite <- (ad_list_app_rev (cons x prefix) l x0) in H12. Split with d1.
      	Split with (cons x0 l). Split. Assumption.
	Assumption.
	Change (CG_path x0 (Dplus d0 d') (app (rev (cons x prefix)) (cons x0 (nil ad)))).
      	Apply CG_path_app_1 with x:=x. Rewrite <- (Dplus_z_d d0). Apply CG_p2. Apply CG_p1.
      	Reflexivity.
	Unfold CG_edge. Rewrite H5. Rewrite (MapSweep_semantics_2 ? ? edges x0 d0 H6).
      	Reflexivity.
	Assumption.
	Simpl. Simpl in H1. Rewrite H1. Simpl in H8. Rewrite H8. Reflexivity.
	Assumption.
	Exact (MapSweep_semantics_1 ? ? ? x0 d0 H6).
	Intro H2. Rewrite H2 in H3. Discriminate H3.
	Intro H4. Rewrite H4 in H3. Discriminate H3.
      Qed.

Lemma ad_simple_path_naive_search_complete : (n:nat) (x,y:ad)
      	   (ad_simple_path_naive_search x y (nil ad) n)=true ->
	     {d:D & {l:(list ad) & (and_sp (CG_path y d (cons x l))
	                                   (ad_list_stutters (cons x l))=false)}}.
      Proof.
      	Intros. Exact (ad_simple_path_naive_search_complete_1 n x y (nil ad) Dz
                       (CG_p1 x x (refl_equal ? ?)) (refl_equal ? false) H).
      Qed.

(*s Definition of simple paths : paths without repetition *)

Definition CG_simple_path := [last:ad] [d:D] [l:(list ad)]
                                   (and_sp (CG_path last d l) (ad_list_stutters l)=false).

(*s Between two vertexes, there exists a simple path or there exists no path *)
Lemma ad_simple_path_dec : (x,y:ad)
      	  {l:(list ad) & {d:D & (CG_simple_path y d (cons x l))}}+
	  {(l:(list ad)) (d:D) (CG_simple_path y d (cons x l)) -> False}.
      Proof.
      	Intros. Elim (sumbool_of_bool (ad_simple_path_naive_search x y (nil ad) (MapCard ? cg))).
      	Intro H. Left . Elim (ad_simple_path_naive_search_complete ? ? ? H). Intros d H0.
      	Elim H0. Intros l H1. Split with l. Split with d. Exact H1.
	Intro H. Right . Intros l d H0. Unfold CG_simple_path in H0. Elim H0. Intros H1 H2.
      	Rewrite (ad_simple_path_naive_search_correct ? x y l d
                  (le_S_n ? ? (ad_simple_path_bounded_card ? ? ? ? H1 H2)) H1 H2) in H.
      	Discriminate H.
      Qed.

(*s Computing the minimum of edges in a Map *)

Definition all_min := [f:ad->D->(option D)] (MapFold ? ? (NONE D) Ddmin f).

Lemma all_min_le_1 : (f:ad->D->(option D)) (m:(Map D))
      	  (a:ad) (d:D) (MapGet ? m a)=(SOME ? d) ->
            (Ddle (all_min f m) (f a d))=true.
      Proof.
      	Intros. Elim (option_sum ? (f a d)). Intro H0. Elim H0. Intros d' H1. Rewrite H1.
      	Unfold all_min. Cut ([a:(option D)][b:D](Ddle a (SOME D b))
                              (MapFold D (option D) (NONE D) Ddmin f m) d')
                            =(MapFold D bool false orb
                              [a:ad] [y:D]([a0:(option D)][b:D](Ddle a0 (SOME D b)) (f a y) d') m).
      	Intro. Rewrite H2. Rewrite MapFold_orb.
      	Elim (MapSweep_semantics_4 D [a:ad][y:D](Ddle (f a y) (SOME D d')) m a d H).
      	Intros a' H3. Elim H3. Intros d'' H4. Rewrite H4. Reflexivity.
	Rewrite H1. Simpl. Apply Dle_refl.
	Exact (MapFold_distr_r D ? (NONE D) Ddmin bool false orb D
               [a:(option D)][b:D](Ddle a (SOME D b))
               [c:D](refl_equal ? false)
               [a,b:(option D)][c:D](Ddmin_le_distr_l a b (SOME ? c)) f m d').
	Intro H0. Rewrite H0. (Case (all_min f m); Reflexivity).
      Qed.

Lemma all_min_le_2_1 : (f:ad->D->(option D)) (m:(Map D)) (pf:ad->ad)
      	  (d:D) (MapFold1 ? ? (NONE D) Ddmin f pf m)=(SOME ? d) ->
            {a:ad & {d':D | (MapGet ? m a)=(SOME ? d') /\ (f (pf a) d')=(SOME ? d)}}.
      Proof.
        Induction m. Intros. Discriminate H.
	Intros a y pf d H. Simpl in H. Split with a. Split with y. Split. Apply M1_semantics_1.
	Assumption.
	Intros. Simpl in H1.
      	Elim (Ddmin_choice
           (MapFold1 D (option D) (NONE D) Ddmin f [a0:ad](pf (ad_double a0)) m0)
           (MapFold1 D (option D) (NONE D) Ddmin f [a0:ad](pf (ad_double_plus_un a0)) m1)).
      	Intro H2. Rewrite H2 in H1. Elim (H [a0:ad](pf (ad_double a0)) d H1). Intros a0 H3.
      	Elim H3. Intros d' H4. Split with (ad_double a0). Split with d'. Split.
      	Rewrite MapGet_M2_bit_0_0. Rewrite ad_double_div_2. (Elim H4; Trivial).
	Apply ad_double_bit_0.
	(Elim H4; Trivial).
	Intro H2. Rewrite H2 in H1. Elim (H0 [a0:ad](pf (ad_double_plus_un a0)) d H1).
      	Intros a0 H3. Elim H3. Intros d' H4. Split with (ad_double_plus_un a0). Split with d'.
      	Split. Rewrite MapGet_M2_bit_0_1. Rewrite ad_double_plus_un_div_2. (Elim H4; Trivial).
	Apply ad_double_plus_un_bit_0.
	(Elim H4; Trivial).
      Qed.

Lemma all_min_le_2 : (f:ad->D->(option D)) (m:(Map D))
      	  (d:D) (all_min f m)=(SOME ? d) ->
            {a:ad & {d':D | (MapGet ? m a)=(SOME ? d') /\ (f a d')=(SOME ? d)}}.
      Proof.
      	Intros. Exact (all_min_le_2_1 f m [a:ad]a d H).
      Qed.

Lemma all_min_le_3 : (f,g:ad->D->(option D)) (m:(Map D))
      	  ((a:ad) (d:D) (MapGet ? m a)=(SOME ? d) -> (Ddle (f a d) (g a d))=true) ->
	  (Ddle (all_min f m) (all_min g m))=true.
      Proof.
      	Intros. Elim (option_sum ? (all_min g m)). Intro H0. Elim H0. Intros d H1.
      	Elim (all_min_le_2 g m d H1). Intros a H2. Elim H2. Intros d' H3. Elim H3. Intros H4 H5.
      	Apply Ddle_trans with (f a d'). Apply all_min_le_1. Assumption.
	Rewrite H1. Rewrite <- H5. Apply H. Assumption.
	Intro H0. Rewrite H0. Apply Ddle_d_none.
      Qed.

(*s [(ad_simple_path_dist_1 x y l n)] computes the minimum weight of a path 
    from [x] to [y] of maximal length [n] which does not contain vertexes 
    from [l] *)

Fixpoint ad_simple_path_dist_1 [x,y:ad; l:(list ad); n:nat] : (option D) :=
        if (ad_eq x y)
	then (SOME ? Dz)
	else
      	Cases n of
	    O => (NONE ?)
	  | (S n') => Cases (MapGet ? cg x) of
	                  NONE => (NONE ?)
			| (SOME edges) =>
                          let l'=(cons x l) in (* builds reverse path *)
			  (all_min [z:ad][d:D]
			           if (ad_in_list z l')
                                   then (NONE D)
				   else (Ddplus (ad_simple_path_dist_1 z y l' n') d)
				   edges)
                      end
        end.

Lemma ad_simple_path_dist_1_correct_1 : (n:nat) (x,y:ad) (l:(list ad)) (d:D)
      	  (le (length l) n) -> (CG_path y d (cons x l)) ->
      	  (prefix:(list ad))
      	    (ad_list_stutters (app (rev prefix) (cons x l)))=false ->
	    (Ddle (ad_simple_path_dist_1 x y prefix n) (SOME ? d))=true.
      Proof.
      	Induction n. Intros x y l. Case l. Intros. Inversion H0. 
        Rewrite H4. Simpl.
      	Rewrite (ad_eq_correct y). Rewrite H3. Apply Ddle_refl.
	Intros. Elim (le_Sn_O ? H).
	Intros n0 H x y l. Case l. Intros. Inversion H1. 
        Rewrite H5. Simpl.
      	Rewrite (ad_eq_correct y). Rewrite H4. Apply Ddle_refl.
	Intros. Simpl. Elim (sumbool_of_bool (ad_eq x y)). Intro H3. Rewrite H3.
      	Rewrite (ad_eq_complete ? ? H3) in H1. Exact (no_bad_cycles ? ? ? H1).
	Intro H3. Rewrite H3. Elim (option_sum ? (MapGet ? cg x)). Intro H4. Elim H4.
      	Intros edges H5. Rewrite H5. Inversion_clear H1.
      	Apply Ddle_trans with dd':=Case (ad_in_list a (cons x prefix)) of
              (NONE D)
              (Ddplus (ad_simple_path_dist_1 a y (cons x prefix) n0) d')
              end.
      	Cut (MapGet ? edges a)=(SOME ? d'). Intro.
      	Exact (all_min_le_1 [z:ad] [d:D]
          Case (orb (ad_eq z x) (ad_in_list z prefix)) of
             (NONE D)
             (Ddplus (ad_simple_path_dist_1 z y (cons x prefix) n0) d)
             end edges a d' H1).
	Unfold CG_edge in H7. Rewrite H5 in H7. Elim (option_sum ? (MapGet D edges a)). Intro H8.
      	Elim H8. Intros d1 H9. Rewrite H9 in H7. Rewrite H9. Inversion_clear H7. Reflexivity.
	Intro H8. Rewrite H8 in H7. Discriminate H7.
	Rewrite (ad_list_app_rev prefix (cons a l0) x) in H2.
      	Rewrite <- (ad_in_list_rev (cons x prefix) a).
      	Rewrite (ad_list_stutters_prev_conv_l ? ? ? H2).
      	Apply (Ddle_plus_mono (ad_simple_path_dist_1 a y (cons x prefix) n0) (SOME D d0) d' d').
      	Exact (H a y l0 d0 (le_S_n ? ? H0) H6 (cons x prefix) H2).
	Apply Dle_refl.
	Intro H4. Inversion H1. Unfold CG_edge in H10. 
        Rewrite H4 in H10. Discriminate H10.
      Qed.

Lemma ad_simple_path_dist_1_correct_2 : (n:nat) (x,y:ad) (l:(list ad)) (d:D)
      	  (le (length l) n) -> (CG_path y d (cons x l)) ->
      	    (ad_list_stutters (cons x l))=false ->
	    (Ddle (ad_simple_path_dist_1 x y (nil ad) n) (SOME ? d))=true.
      Proof.
      	Intros. Exact (ad_simple_path_dist_1_correct_1 n x y l d H H0 (nil ad) H1).
      Qed.

(*s [(ad_simple_path_dist x y)] computes the minimum path from x to y *)
Definition ad_simple_path_dist := [x,y:ad]
          (ad_simple_path_dist_1 x y (nil ad) (MapCard ? cg)).

Lemma ad_simple_path_dist_correct_1 : (x,y:ad) (l:(list ad)) (d:D)
      	  (CG_path y d (cons x l)) ->
      	  (ad_list_stutters (cons x l))=false ->
	    (Ddle (ad_simple_path_dist x y) (SOME ? d))=true.
      Proof.
      	Intros. Unfold ad_simple_path_dist.
      	(Apply ad_simple_path_dist_1_correct_2 with l:=l; Try Assumption).
      	Exact (le_S_n ? ? (ad_simple_path_bounded_card l y x d H H0)).
      Qed.

Lemma ad_simple_path_dist_correct : (x,y:ad) (l:(list ad)) (d:D)
      	  (CG_path y d (cons x l)) ->
	    (Ddle (ad_simple_path_dist x y) (SOME ? d))=true.
      Proof.
      	Intros. Elim ad_path_then_simple_path with 1:= H. 
        Intros l0 H0.
      	Elim H0. Intros d0 H1. Elim H1. Intros H2 H3. Elim H3. Intros H4 H5.
      	Apply Ddle_trans with dd':=(SOME D d0).
      	Exact (ad_simple_path_dist_correct_1 x y l0 d0 H2 H4).
	Exact H5.
      Qed.

Lemma ad_simple_path_dist_1_complete_1 : (n:nat) (x,y:ad) (prefix:(list ad))
      	   (d':D) (CG_path x d' (rev (cons x prefix))) ->
      	   (ad_list_stutters (cons x prefix))=false ->
	   (d0:D)
      	   (ad_simple_path_dist_1 x y prefix n)=(SOME D d0) ->
	     {l:(list ad) & (and_sp (CG_path y (Dplus d0 d') (app (rev (cons x prefix)) l))
	                            (ad_list_stutters (app (rev (cons x prefix)) l))=false /\
                                     (le (length l) n) )}.
      Proof.
      	Induction n. Intros. Split with (nil ad). Split. Unfold ad_simple_path_dist_1 in H1.
      	Elim (sumbool_of_bool (ad_eq x y)). Intro H2. Rewrite H2 in H1. Inversion H1.
      	Rewrite <- H4. Rewrite Dplus_z_d. Rewrite <- app_nil_end.
      	Rewrite <- (ad_eq_complete ? ? H2). Assumption.
	Intro H2. Rewrite H2 in H1. Discriminate H1.
	Split. Rewrite <- app_nil_end. Rewrite ad_list_stutters_rev. Assumption.
	Apply le_n.
	Intros. Simpl in H2. Elim (sumbool_of_bool (ad_eq x y)). Intro H3. Rewrite H3 in H2.
      	Inversion H2. Split with (nil ad). Split. Rewrite <- H5. Rewrite Dplus_z_d.
      	Rewrite <- app_nil_end. Rewrite <- (ad_eq_complete ? ? H3). Assumption.
	Split. Rewrite <- app_nil_end. Rewrite ad_list_stutters_rev. Assumption.
	Apply le_O_n.
	Intro H3. Rewrite H3 in H2. Elim (option_sum ? (MapGet ? cg x)). Intro H4. Elim H4.
      	Intros edges H5. Rewrite H5 in H2. Elim (all_min_le_2 ? edges d0 H2). Intros a H6.
      	Elim H6. Intros d1 H7. Elim H7. Intros H8 H9. Clear H2 H6 H7.
      	Cut (ad_in_list a (cons x prefix))=false. Intro.
      	Elim (option_sum ? (ad_simple_path_dist_1 a y (cons x prefix) n0)). Intro H6. Elim H6.
      	Intros d2 H7. Clear H6. Rewrite H7 in H9. Cut (Dplus d2 d1)=d0. Intro.
      	Cut (CG_path a (Dplus d1 d') (rev (cons a (cons x prefix)))). Intro.
      	Cut (ad_list_stutters (cons a (cons x prefix)))=false. Intro.
      	Elim (H a y (cons x prefix) ? H10 H11 d2 H7). Intros l0 H12. Elim H12. Intros H13 H14.
      	Elim H14. Intros H15 H16. Split with (cons a l0). Split. Rewrite ad_list_app_rev.
      	Rewrite <- H6. Rewrite Dplus_assoc. Assumption.
	Split. Rewrite ad_list_app_rev. Assumption.
	Exact (le_n_S ? ? H16).
	Simpl. Simpl in H2. Rewrite H2. Simpl in H1. Rewrite H1. Reflexivity.
	Apply (CG_path_app_1 (rev (cons x prefix)) (cons a (nil ad)) a x).
      	Rewrite <- (Dplus_z_d d1). Apply CG_p2. Apply CG_p1. Reflexivity.
	Unfold CG_edge. Rewrite H5. Rewrite H8. Reflexivity.
	Assumption.
	Elim (sumbool_of_bool (orb (ad_eq a x) (ad_in_list a prefix))). Intro H10.
      	Rewrite H10 in H9. Discriminate H9.
	Intro H10. Rewrite H10 in H9. Simpl in H9. Inversion H9. Reflexivity.
	Intro H6. Rewrite H6 in H9. Generalize H9.
      	Case (orb (ad_eq a x) (ad_in_list a prefix)); Intro H10; Discriminate H10.
	Elim (sumbool_of_bool (ad_in_list a (cons x prefix))). Intro H10. Simpl in H10.
      	Rewrite H10 in H9. Discriminate H9.
	Trivial.
	Intro H4. Rewrite H4 in H2. Discriminate H2.
      Qed.

Lemma ad_simple_path_dist_1_complete : (n:nat) (x,y:ad) (d:D)
      	   (ad_simple_path_dist_1 x y (nil ad) n)=(SOME D d) ->
	     {l:(list ad) & (and_sp (CG_path y d (cons x  l))
	                            (ad_list_stutters (cons x l))=false)}.
      Proof.
      	Intros. Elim (ad_simple_path_dist_1_complete_1 n x y (nil ad) Dz
                        (CG_p1 x x (refl_equal ? ?)) (refl_equal ? ?) d H).
      	Intros l H0. Split with l. Elim H0. Intros H1 H2. Elim H2. Intros H3 H4. Split.
      	Rewrite (Dplus_d_z d) in H1. Assumption.
	Exact H3.
      Qed.

Lemma ad_simple_path_dist_complete : (x,y:ad) (d:D)
      	   (ad_simple_path_dist x y)=(SOME D d) ->
	     {l:(list ad) & (and_sp (CG_path y d (cons x  l))
	                            (ad_list_stutters (cons x l))=false)}.
      Proof.
      	Intros. Exact (ad_simple_path_dist_1_complete (MapCard ? cg) x y d H).
      Qed.

Lemma ad_simple_path_dist_complete_2 : (x,y:ad) (d:D)
      	   (ad_simple_path_dist x y)=(SOME D d) ->
	     {l:(list ad) & (CG_path y d (cons x  l))}.
      Proof.
      	Intros. Elim (ad_simple_path_dist_complete x y d H). Intros l H0. Split with l.
      	(Elim H0; Trivial).
      Qed.

Lemma ad_simple_path_dist_complete_3 : (x,y:ad) (dd:(option D))
      	  ((l:(list ad)) (d:D) (CG_path y d (cons x l)) -> (Ddle dd (SOME ? d))=true) ->
	     (Ddle dd (ad_simple_path_dist x y))=true.
      Proof.
      	Intros x y dd. Case dd. Intros. Elim (option_sum ? (ad_simple_path_dist x y)). Intro H0.
      	Elim H0. Intros d H1. Elim (ad_simple_path_dist_complete_2 x y d H1). Intros l H2.
      	Simpl in H. Rewrite <- (H l d H2). Rewrite H1. Reflexivity.
	Intro H0. Rewrite H0. Reflexivity.
	Intros. Elim (option_sum ? (ad_simple_path_dist x y)). Intro H0. Elim H0. Intros d0 H1.
      	Rewrite H1. Elim (ad_simple_path_dist_complete_2 x y d0 H1). Intros l H2.
      	Exact (H l d0 H2).
	Intro H0. Rewrite H0. Reflexivity.
      Qed.

Lemma ad_simple_path_dist_d_1 : (x:ad) (ad_simple_path_dist x x)=(SOME ? Dz).
      Proof.
      	Unfold ad_simple_path_dist. (Elim (MapCard ? cg); Simpl). Intro.
      	Rewrite (ad_eq_correct x). Reflexivity.
	Intros. Rewrite (ad_eq_correct x). Reflexivity.
      Qed.

Lemma ad_simple_path_dist_d_2 : (x,y,z:ad) (d,d':D)
      	  (ad_simple_path_dist x y)=(SOME ? d) -> (ad_simple_path_dist y z)=(SOME ? d') ->
	    (Ddle (ad_simple_path_dist x z) (SOME ? (Dplus d' d)))=true.
      Proof.
      	Intros. Elim (ad_simple_path_dist_complete_2 x y d H).
      	Elim (ad_simple_path_dist_complete_2 y z d' H0). Intros l1 H1 l2 H2.
      	Apply (ad_simple_path_dist_correct x z (app l2 l1)).
      	Exact (CG_path_app_1 (cons x l2) l1 z y d d' H1 H2).
      Qed.

Lemma ad_simple_path_dist_d_3 : (x,y,z:ad) (d,d':D)
      	  (Ddle (ad_simple_path_dist x y) (SOME ? d))=true ->
          (Ddle (ad_simple_path_dist y z) (SOME ? d'))=true ->
	    (Ddle (ad_simple_path_dist x z) (SOME ? (Dplus d' d)))=true.
      Proof.
      	Intros. Elim (option_sum ? (ad_simple_path_dist x y)). Intro H1. Elim H1. Intros d0 H2.
      	Elim (option_sum ? (ad_simple_path_dist y z)). Intro H3. Elim H3. Intros d'0 H4.
      	Apply Ddle_trans with dd':=(SOME D (Dplus d'0 d0)).
      	Exact (ad_simple_path_dist_d_2 x y z d0 d'0 H2 H4).
	Rewrite H2 in H. Simpl in H. Rewrite H4 in H0. Simpl in H0. Simpl.
      	Exact (Dle_plus_mono ? ? ? ? H0 H).
	Intro H3. Rewrite H3 in H0. Discriminate H0.
	Intro H1. Rewrite H1 in H. Discriminate H.
      Qed.

(*s [(CG_leq x y)] is true when there is a path from [x] to [y] *)

Definition CG_leq := [x,y:ad] Cases (ad_simple_path_dist x y) of
                                         (SOME _) => true
				       | _ => false
				     end.

Lemma CG_leq_refl : (x:ad) (CG_leq x x)=true.
      Proof.
      	Unfold CG_leq. Intros. Rewrite (ad_simple_path_dist_d_1 x). Reflexivity.
      Qed.

Lemma CG_leq_trans : (x,y,z:ad) (CG_leq x y)=true -> (CG_leq y z)=true -> (CG_leq x z)=true.
      Proof.
      	Unfold CG_leq. Intros. Elim (option_sum ? (ad_simple_path_dist x y)). Intro H1.
      	Elim H1. Intros d1 H2. Elim (option_sum ? (ad_simple_path_dist y z)). Intro H3.
      	Elim H3. Intros d2 H4. Cut (Ddle (ad_simple_path_dist x z) (SOME D (Dplus d2 d1)))=true.
      	Intro. Elim (option_sum ? (ad_simple_path_dist x z)). Intro H6. Elim H6. Intros d3 H7.
      	Rewrite H7. Reflexivity.
	Intro H6. Rewrite H6 in H5. Discriminate H5.
	Exact (ad_simple_path_dist_d_2 ? ? ? ? ? H2 H4).
	Intro H3. Rewrite H3 in H0. Discriminate H0.
	Intro H1. Rewrite H1 in H. Discriminate H.
      Qed.

(*s [(CG_standard_rho root d0 others x)] computes [d0-d] when
     [root] and [x] are connected by a path of minimal 
     length [d] and [(others x)] when there are not connected 
*)

Definition CG_standard_rho 
  := [root:ad] [d0:D] [others:ad->D] [x:ad]
                 Cases (ad_simple_path_dist root x) of
                     (SOME d) => (Dplus d0 (Dneg d))
		   | NONE => (others x) (* dummy *)
		 end.

Lemma CG_standard_rho_root : (root:ad) (d0:D) (others:ad->D)
          (CG_standard_rho root d0 others root)=d0.
      Proof.
      	Unfold CG_standard_rho. Intros. Rewrite (ad_simple_path_dist_d_1 root).
      	Rewrite Dneg_Dz. Apply Dplus_d_z.
      Qed.

(*s If there is a root such that all the nodes ae connected to 
    this root, then [CG_standard_rho] gives a correct valuation 
*)
Lemma CG_rooted_sat_1 : (root:ad) (d0:D) (others:ad->D)
      	  ((x:ad) (in_dom ? x cg)=true -> (CG_leq root x)=true) ->
	    (CGsat (CG_standard_rho root d0 others)).
      Proof.
      	Unfold CG_standard_rho. Intros. Unfold CGsat. Intros.
      	Elim (option_sum ? (ad_simple_path_dist root x)). Intro H1. Elim H1. Intros d1 H2.
      	Rewrite H2. Cut (Ddle (ad_simple_path_dist root y) (SOME D (Dplus d d1)))=true. Intro.
      	Elim (option_sum ? (ad_simple_path_dist root y)). Intro H4. Elim H4. Intros d2 H5.
      	Rewrite H5. Rewrite Dplus_assoc. Apply Dle_plus_mono. Apply Dle_refl.
	Apply Dplus_reg_l with d'':=d2. Rewrite <- Dplus_assoc. Rewrite Dplus_neg.
      	Rewrite Dplus_z_d. Apply Dplus_reg_r with d'':=d1. Rewrite Dplus_assoc.
      	Rewrite Dplus_neg_2. Rewrite Dplus_d_z. Rewrite H5 in H3. Exact H3.
	Intro H4. Rewrite H4 in H3. Discriminate H3.
	Elim (ad_simple_path_dist_complete_2 root x d1 H2). Intros l H3.
      	Apply (ad_simple_path_dist_correct root y (app l (cons y (nil ad))) (Dplus d d1)).
      	Change (CG_path y (Dplus d d1) (app (cons root l) (cons y (nil ad)))).
      	Apply CG_path_app_1 with last:=y x:=x. Rewrite <- (Dplus_z_d d). Apply CG_p2.
      	Apply CG_p1. Reflexivity.
	Assumption.
	Assumption.
	Intro H1. Cut (CG_leq root x)=true. 
	Unfold CG_leq. Rewrite H1. Intro. Discriminate H2.
	Apply H. Rewrite MapDom_Dom. Exact (CG_edge_in_cg_1 ? ? ? H0).
      Qed.

Lemma CG_rooted_sat : (root:ad) (d0:D)
      	  ((x:ad) (in_dom ? x cg)=true -> (CG_leq root x)=true) ->
	    {rho:ad->D | (CGsat rho) /\ (rho root)=d0}.
      Proof.
      	Intros. Split with (CG_standard_rho root d0 [x:ad]d0). Split.
      	Exact (CG_rooted_sat_1 root d0 [x:ad]d0 H).
	Apply CG_standard_rho_root.
      Qed.

(*s The [CG_standard_rho] valuation is the minimal one *)
Lemma CG_standard_rho_minimal : (root:ad) (d0:D) (others:ad->D)
	    (rho:ad->D) (CGsat rho) -> (Dle d0 (rho root))=true ->
	      (x:ad) (CG_leq root x)=true ->
                (Dle (CG_standard_rho root d0 others x) (rho x))=true.
      Proof.
      	Unfold CG_leq CG_standard_rho. Intros. Elim (option_sum ? (ad_simple_path_dist root x)).
      	Intro H2. Elim H2. Intros d H3. Rewrite H3.
      	Elim (ad_simple_path_dist_complete_2 root x d H3). Intros l H4.
      	Apply Dplus_reg_r with d'':=d. Rewrite Dplus_assoc. Rewrite Dplus_neg_2.
      	Rewrite Dplus_d_z. Apply Dle_trans with d':=(rho root). Exact H0.
	Exact (CG_path_correct l root x d H4 rho H).
	Intro H2. Rewrite H2 in H1. Discriminate H1.
      Qed.

Lemma CG_sat_add_1 : (x,y:ad) (d:D)
          (rho:ad->D) (CGsat rho) -> (Dle (rho x) (Dplus (rho y) d))=true ->
            (Ddle (SOME ? (Dneg d)) (ad_simple_path_dist y x))=true.
      Proof.
      	Intros. Apply ad_simple_path_dist_complete_3. Intros. Simpl.
      	Apply Dplus_reg_r with d'':=d. Rewrite Dplus_neg_2. Apply Dplus_reg_l with d'':=(rho x).
      	Rewrite Dplus_d_z. Apply Dle_trans with d':=(Dplus (rho y) d); Try Assumption.
      	Apply Dle_trans with d':=(Dplus (Dplus (rho x) d0) d). Apply Dle_plus_mono.
      	Exact (CG_path_correct l y x d0 H1 rho H).
	Apply Dle_refl.
	Rewrite Dplus_assoc. Apply Dle_refl.
      Qed.

    End CGNoBadCycles.

(*s [(CG_add x y d)] adds the edge (x,y) of weight d to G, 
   in case an edge already exists, only the minimal one is kept
*)

Definition CG_add := [x,y:ad; d:D]
      Cases (MapGet ? cg x) of
          NONE => (MapPut ? cg x (M1 ? y d))
        | (SOME edges) => Cases (MapGet ? edges y) of
                              NONE => (MapPut ? cg x (MapPut ? edges y d))
  			    | (SOME d0) => (MapPut ? cg x (MapPut ? edges y (Dmin d d0)))
  	                end
      end.

End CGDist.

(*s Properties of [CG_add] *)
Section CGAdd.

    Variable cg1 : CGraph1.

    Variable x,y : ad.
    Variable d:D.

Definition cg2 := (CG_add cg1 x y d).

Lemma CG_add_edge_1 : (x0,y0:ad) (CG_edge cg2 x0 y0)=
              (if (andb (ad_eq x x0) (ad_eq y y0))
		 then (Ddmin (SOME ? d) (CG_edge cg1 x0 y0))
		 else (CG_edge cg1 x0 y0)).
    Proof.
    	Unfold cg2 CG_add. Intros. Elim (sumbool_of_bool (andb (ad_eq x x0) (ad_eq y y0))).
    	Intro H. Elim (andb_prop ? ? H). Intros H0 H1. Rewrite H.
    	Rewrite <- (ad_eq_complete ? ? H0). Rewrite <- (ad_eq_complete ? ? H1).
    	Elim (option_sum ? (MapGet ? cg1 x)). Intro H2. Elim H2. Intros edges H3. Rewrite H3.
    	Elim (option_sum ? (MapGet D edges y)). Intro H4. Elim H4. Intros d0 H5. Rewrite H5.
    	Unfold CG_edge. Rewrite (MapPut_semantics ? cg1 x (MapPut D edges y (Dmin d d0)) x).
    	Rewrite (ad_eq_correct x). Rewrite (MapPut_semantics ? edges y (Dmin d d0) y).
    	Rewrite (ad_eq_correct y). Rewrite H3. Rewrite H5. Reflexivity.
	Intro H4. Rewrite H4. Unfold CG_edge.
    	Rewrite (MapPut_semantics ? cg1 x (MapPut D edges y d) x). Rewrite (ad_eq_correct x).
    	Rewrite H3. Rewrite H4. Rewrite (MapPut_semantics ? edges y d y).
    	Rewrite (ad_eq_correct y). Reflexivity.
	Intro H2. Rewrite H2. Unfold CG_edge. Rewrite (MapPut_semantics ? cg1 x (M1 D y d) x).
    	Rewrite (ad_eq_correct x). Rewrite H2. Rewrite (M1_semantics_1 ? y d). Reflexivity.
	Intro H. Rewrite H. Elim (andb_false_elim ? ? H). Intro H0.
    	Elim (option_sum ? (MapGet ? cg1 x)). Intro H1. Elim H1. Intros edges H2. Rewrite H2.
    	Elim (option_sum ? (MapGet ? edges y)). Intro H3. Elim H3. Intros d0 H4. Rewrite H4.
    	Unfold CG_edge. Rewrite (MapPut_semantics ? cg1 x (MapPut D edges y (Dmin d d0)) x0).
    	Rewrite H0. Reflexivity.
	Intro H3. Rewrite H3. Unfold CG_edge.
    	Rewrite (MapPut_semantics ? cg1 x (MapPut D edges y d) x0). Rewrite H0. Reflexivity.
	Intro H1. Rewrite H1. Unfold CG_edge. Rewrite (MapPut_semantics ? cg1 x (M1 D y d) x0).
    	Rewrite H0. Reflexivity.
	Intro H0. Elim (option_sum ? (MapGet ? cg1 x)). Intro H1. Elim H1. Intros edges H2.
    	Rewrite H2. Elim (option_sum ? (MapGet ? edges y)). Intro H3. Elim H3. Intros d0 H4.
    	Rewrite H4. Unfold CG_edge.
    	Rewrite (MapPut_semantics ? cg1 x (MapPut D edges y (Dmin d d0)) x0).
    	Elim (sumbool_of_bool (ad_eq x x0)). Intro H5. Rewrite H5.
    	Rewrite (MapPut_semantics ? edges y (Dmin d d0) y0). Rewrite H0.
    	Rewrite <- (ad_eq_complete ? ? H5). Rewrite H2. Reflexivity.
	Intro H5. Rewrite H5. Reflexivity.
	Intro H3. Rewrite H3. Unfold CG_edge.
    	Rewrite (MapPut_semantics ? cg1 x (MapPut D edges y d) x0).
    	Elim (sumbool_of_bool (ad_eq x x0)). Intro H4. Rewrite H4.
    	Rewrite <- (ad_eq_complete ? ? H4). Rewrite H2.
    	Rewrite (MapPut_semantics ? edges y d y0). Rewrite H0. Reflexivity.
	Intro H4. Rewrite H4. Reflexivity.
	Intro H1. Rewrite H1. Unfold CG_edge. Rewrite (MapPut_semantics ? cg1 x (M1 D y d) x0).
    	Elim (sumbool_of_bool (ad_eq x x0)). Intro H2. Rewrite H2.
    	Rewrite (M1_semantics_2 ? y y0 d H0). Rewrite <- (ad_eq_complete ? ? H2).
    	Rewrite H1. Reflexivity.
	Intro H2. Rewrite H2. Reflexivity.
    Qed.

Lemma CG_add_edge_2 : (x0,y0:ad) (Ddle (CG_edge cg2 x0 y0) (CG_edge cg1 x0 y0))=true.
    Proof.
    	Intros. Rewrite (CG_add_edge_1 x0 y0).
    	Elim (sumbool_of_bool (andb (ad_eq x x0) (ad_eq y y0))). Intro H. Rewrite H.
    	Apply Ddmin_le_2.
	Intro H. Rewrite H. Apply Ddle_refl.
    Qed.

Lemma CG_add_1 : (rho:ad->D)
        (CGsat cg1 rho) -> (Dle (rho x) (Dplus (rho y) d))=true
          -> (CGsat cg2 rho).
    Proof.
    	Unfold CGsat. Intros. Rewrite (CG_add_edge_1 x0 y0) in H1.
    	Elim (sumbool_of_bool (andb (ad_eq x x0) (ad_eq y y0))). Intro H2.
    	Elim (andb_prop ? ? H2). Intros H3 H4. Rewrite H2 in H1.
    	Rewrite <- (ad_eq_complete ? ? H3). Rewrite <- (ad_eq_complete ? ? H4).
    	Elim (option_sum ? (CG_edge cg1 x0 y0)). Intro H5. Elim H5. Intros d1 H6.
    	Rewrite H6 in H1. Simpl in H1. Inversion H1. Rewrite Dmin_plus_r. Apply Dmin_le_5.
    	Assumption.
	Apply H. Rewrite (ad_eq_complete ? ? H3). Rewrite (ad_eq_complete ? ? H4). Assumption.
	Intro H5. Rewrite H5 in H1. Simpl in H1. Inversion H1. Rewrite <- H7. Assumption.
	Intro H2. Rewrite H2 in H1. Apply H. Assumption.
    Qed.

Lemma CG_add_2 : (rho:ad->D) (CGsat cg2 rho) -> (CGsat cg1 rho).
    Proof.
    	Unfold CGsat. Intros. Elim (option_sum ? (CG_edge cg2 x0 y0)). Intro H1. Elim H1.
    	Intros d1 H2. Cut (Dle d1 d0)=true. Intro H3.
    	Apply Dle_trans with d':=(Dplus (rho y0) d1). Apply H. Assumption.
	Apply Dle_plus_mono. Apply Dle_refl.
	Assumption.
	Change (Ddle (SOME ? d1) (SOME ? d0))=true. Rewrite <- H2. Rewrite <- H0.
    	Apply CG_add_edge_2.
	Intro H1. Cut (Ddle (NONE ?) (SOME ? d0))=true. Intro H2. Discriminate H2.
	Rewrite <- H1. Rewrite <- H0. Apply CG_add_edge_2.
    Qed.

Lemma CG_add_3 : (rho:ad->D) (CGsat cg2 rho) -> (Dle (rho x) (Dplus (rho y) d))=true.
    Proof.
    	Unfold CGsat. Intros. Elim (option_sum ? (CG_edge cg2 x y)). Intro H0. Elim H0.
    	Intros d0 H1. Apply Dle_trans with d':=(Dplus (rho y) d0). Apply H. Assumption.
	Apply Dle_plus_mono. Apply Dle_refl.
	Rewrite (CG_add_edge_1 x y) in H1. Rewrite (ad_eq_correct x) in H1.
    	Rewrite (ad_eq_correct y) in H1. Simpl in H1. Elim (option_sum ? (CG_edge cg1 x y)).
    	Intro H2. Elim H2. Intros d1 H3. Rewrite H3 in H1. Inversion H1. Apply Dmin_le_1.
	Intro H2. Rewrite H2 in H1. Inversion H1. Apply Dle_refl.
	Intro H0. Rewrite (CG_add_edge_1 x y) in H0. Rewrite (ad_eq_correct x) in H0.
    	Rewrite (ad_eq_correct y) in H0. Simpl in H0. Generalize H0.
    	(Case (CG_edge cg1 x y); Intro H1). Discriminate H1.
	Intro H2. Discriminate H2.
    Qed.

(*s Every path in the new graph [cg2] either goes through the new edge from [x] to [y],
       or is a path in the old graph cg1: 
*)
Lemma CG_add_4_1 : (l:(list ad)) (x0,y0:ad) (d0:D) (CG_path cg2 y0 d0 (cons x0 l)) ->
    	  {l0:(list ad) & {l1:(list ad) | (cons x0 l)=(app l0 (cons x (cons y l1)))}}+
	  (CG_path cg1 y0 d0 (cons x0 l)).
    Proof.
    	Induction l. Intros. Inversion H. Right . Rewrite H2.
        Rewrite <- H1. Apply CG_p1.
    	Reflexivity.
	Intros. Inversion H0. Rewrite (CG_add_edge_1 x0 a) in H6.
    	Elim (sumbool_of_bool (andb (ad_eq x x0) (ad_eq y a))). Intro H8. Left .
    	Split with (nil ad). Split with l0. Elim (andb_prop ? ? H8). Intros H9 H10.
    	Rewrite (ad_eq_complete ? ? H9). Rewrite (ad_eq_complete ? ? H10). Reflexivity.
	Intro H8. Elim (H ? ? ? H4). Intro H9. Elim H9. Intros l2 H10. Elim H10. Intros l3 H11.
    	Left . Split with (cons x0 l2). Split with l3. Rewrite H11. Reflexivity.
	Intro H9. Right . Apply CG_p2. Assumption.
	Rewrite H8 in H6. Assumption.
    Qed.

Lemma CG_add_4_2 : (l:(list ad)) (last:ad) (d0:D)
    	  (CG_path cg2 last d0 l) -> (ad_in_list y l)=false -> (CG_path cg1 last d0 l).
    Proof.
      Induction l. Intros. Inversion H.
	Intros a l0. Case l0. Intros. Inversion H0. Rewrite <- H3. Apply CG_p1. Assumption.
	Intros. Inversion H0. Rewrite (CG_add_edge_1 a a0) in H7. Elim (orb_false_elim ? ? H1).
    	Intros H9 H10. Elim (orb_false_elim ? ? H10). 
        Intros H11 H12. Rewrite H11 in H7.
    	Rewrite (andb_b_false (ad_eq x a)) in H7. Apply CG_p2. Apply H. Assumption.
	Exact H10.
	Assumption.
    Qed.

Lemma CG_add_4_3 : (x0:ad) (d0:D) (l:(list ad))
        (ad_list_stutters (cons y l))=false ->
	  (CG_path cg2 x0 d0 (cons y l)) -> (CG_path cg1 x0 d0 (cons y l)).
    Proof.
    	Intros x0 d0 l. Elim l. Intros. Inversion H0. 
        Rewrite <- H2. Apply CG_p1. Assumption.
	Intros. Inversion H1. Apply CG_p2. Apply CG_add_4_2. 
        Assumption.
	Elim (orb_false_elim ? ? H0). Trivial.
	Rewrite (CG_add_edge_1 y a) in H7. 
        Elim (orb_false_elim ? ? H0). Intros.
    	Elim (orb_false_elim ? ? H8). Intros. Rewrite H10 in H7.
    	Rewrite (andb_b_false (ad_eq x y)) in H7. Assumption.
    Qed.

Lemma CG_add_4_4 : (l:(list ad)) (last:ad) (d0:D)
    	  (CG_path cg2 last d0 l) -> (ad_in_list x l)=false -> (CG_path cg1 last d0 l).
    Proof.
    	Induction l. Intros. Inversion H.
	Intros a l0. Case l0. Intros. Inversion H0. Rewrite <- H3. Apply CG_p1. Assumption.
	Intros. Inversion H0. Apply CG_p2. Apply H. Assumption.
	Elim (orb_false_elim ? ? H1). Trivial.
	Rewrite (CG_add_edge_1 a a0) in H7. Elim (orb_false_elim ? ? H1). Intros.
    	Rewrite H8 in H7. Exact H7.
    Qed.

Lemma CG_add_4_5 : (d0:D) (l:(list ad))
        (ad_list_stutters (app l (cons x (nil ad))))=false ->
	  (CG_path cg2 x d0 (app l (cons x (nil ad)))) ->
          (CG_path cg1 x d0 (app l (cons x (nil ad)))).
    Proof.
    	Intros d0 l. Cut {l0:(list ad) & {y0:ad | l=(app l0 (cons y0 (nil ad)))}}+{l=(nil ad)}.
    	Intro. Elim H. Intro H0. Elim H0. Intros l0 H1. Elim H1. Intros y0 H2 H3 H4.
    	Rewrite H2 in H4. Rewrite (app_ass l0 (cons y0 (nil ad)) (cons x (nil ad))) in H4.
    	Simpl in H4. Elim (CG_path_app_3 cg2 l0 (cons x (nil ad)) x y0 d0 H4). Intros d1 H5.
    	Rewrite H2. Rewrite (app_ass l0 (cons y0 (nil ad)) (cons x (nil ad))). Simpl.
    	Elim (CG_path_app_2 cg2 l0 (cons x (nil ad)) x y0 d0 H4). Intros d2 H6.
    	Cut d0=(Dplus d2 d1). Intro. Rewrite H7.
    	Change (CG_path cg1 x (Dplus d2 d1) (app l0 (app (cons y0 (nil ad)) (cons x (nil ad))))).
    	Rewrite <- (app_ass l0 (cons y0 (nil ad)) (cons x (nil ad))).
    	Apply CG_path_app_1 with last:=x x:=y0. Inversion H6. 
        Inversion H11. Apply CG_p2.
    	Rewrite <- H15. Apply CG_p1. Assumption.
	Rewrite (CG_add_edge_1 y0 x) in H13. Rewrite H2 in H3.
    	Rewrite (app_ass l0 (cons y0 (nil ad)) (cons x (nil ad))) in H3. Simpl in H3.
    	Elim (orb_false_elim ? ? (ad_list_stutters_app_conv_r ? ? H3)). Intros.
    	Elim (orb_false_elim ? ? H17). Intros. 
        Rewrite (ad_eq_comm y0 x) in H19.
    	Rewrite H19 in H13. 
        Rewrite (andb_false_b (ad_eq y x)) in H13. Assumption.
	Apply CG_add_4_4. Assumption.
	Rewrite H2 in H3. Apply ad_list_stutters_prev_conv_l with l':=(nil ad). Assumption.
	Elim (CG_path_weight_and_last_unique cg2
             (app l0 (cons y0 (cons x (nil ad)))) x x d0 (Dplus d2 d1)).
    	Trivial.
	Assumption.
	Change (CG_path cg2 x (Dplus d2 d1) (app l0 (app (cons y0 (nil ad)) (cons x (nil ad))))).
    	Rewrite <- (app_ass l0 (cons y0 (nil ad)) (cons x (nil ad))).
    	Apply CG_path_app_1 with x:=y0. Assumption.
	Assumption.
	Intro H0. Rewrite H0. Simpl. Intros. Inversion H2. 
        Rewrite <- H4. Apply CG_p1.
    	Assumption.
	Elim l. Right . Reflexivity.
	Intros. Elim H. Intro H0. Elim H0. Intros l1 H1. Elim H1. Intros a0 H2. Rewrite H2.
    	Left . Split with (cons a l1). Split with a0. Reflexivity.
	Intro H0. Rewrite H0. Left . Split with (nil ad). Split with a. Reflexivity.
    Qed.

(*s If there is no cycle of negative weight in the old graph [cg1], and the distance
   from [y] to [x] is $\geq -d$, then there is no simple cycle of negative weight in 
   the new graph  [cg2] either: *)

Lemma CG_add_4 :
        ((x0:ad) (d0:D) (l:(list ad)) (CG_path cg1 x0 d0 (cons x0 l)) -> (Dle Dz d0)=true) ->
	  (Ddle (SOME ? (Dneg d)) (ad_simple_path_dist cg1 y x))=true ->
	    (x0:ad) (d0:D) (l:(list ad)) (CG_path cg2 x0 d0 (cons x0 l)) ->
                  (ad_list_stutters l)=false -> (Dle Dz d0)=true.
    Proof.
    	Intros. Elim (CG_add_4_1 ? ? ? ? H1). Intro H3. Elim H3. Intros l0 H4. Elim H4.
    	Intros l1 H5. Rewrite H5 in H1.
    	Change (CG_path cg2 x0 d0 (app l0 (app (cons x (nil ad)) (cons y l1)))) in H1.
    	Elim (CG_path_app_4 cg2 ? ? ? ? ? H1). Intros d1 H6. Elim H6. Intros d2 H7.
    	Elim H7. Intros H8 H9. Elim H8. Intros H10 H11. Clear H3 H6 H7 H8. Rewrite H9.
    	Inversion_clear H11. Rewrite (CG_add_edge_1 x y) in H6. Rewrite (ad_eq_correct x) in H6.
    	Rewrite (ad_eq_correct y) in H6. Simpl in H6. Apply Dplus_reg_r with d'':=(Dneg d1).
    	Rewrite Dplus_assoc. Rewrite Dplus_neg. Rewrite Dplus_d_z. Rewrite Dplus_z_d.
    	Apply Dplus_reg_l with d'':=d1. Rewrite Dplus_neg. Rewrite <- Dplus_assoc.
    	Change (cons x0 l)=(app l0 (app (cons x (nil ad)) (cons y l1))) in H5.
    	Rewrite (ass_app l0 (cons x (nil ad)) (cons y l1)) in H5.
    	Cut {l'0:(list ad) | (app l0 (cons x (nil ad)))=(cons x0 l'0)}. Intro H7. Elim H7.
    	Intros l'0 H8. Clear H4 H7. Rewrite H8 in H5. Simpl in H5. Inversion H5. Rewrite H8 in H10.
    	Cut (CG_path cg1 x (Dplus d1 d3) (cons y (app l1 l'0))). Intro.
    	Elim (option_sum ?  (CG_edge cg1 x y)). Intro H11. Elim H11. Intros d'0 H12.
    	Rewrite H12 in H6. Inversion_clear H6. Elim (Dmin_choice d d'0). Intro H13. Rewrite H13.
    	Apply Dplus_reg_r with d'':=(Dneg d). Rewrite Dplus_assoc. Rewrite Dplus_neg.
    	Rewrite Dplus_d_z. Rewrite Dplus_z_d.
    	Change (Ddle (SOME D (Dneg d)) (SOME D (Dplus d1 d3)))=true.
    	Apply Ddle_trans with dd':=(ad_simple_path_dist cg1 y x). Assumption.
	Exact (ad_simple_path_dist_correct cg1 H y x ? ? H4).
	Intro H13. Rewrite H13. Apply (H x (Dplus (Dplus d1 d3) d'0) (cons y (app l1 l'0))).
    	Apply (CG_path_app_1 cg1 (cons x (cons y (nil ad))) (app l1 l'0) x y). Assumption.
	Rewrite <- (Dplus_z_d d'0). Apply CG_p2. Apply CG_p1. Reflexivity.
	Assumption.
	Intro H11. Rewrite H11 in H6. Inversion H6. Rewrite <- H13.
    	Apply Dplus_reg_r with d'':=(Dneg d). Rewrite Dplus_assoc. Rewrite Dplus_neg.
    	Rewrite Dplus_d_z. Rewrite Dplus_z_d.
    	Change (Ddle (SOME D (Dneg d)) (SOME D (Dplus d1 d3)))=true.
    	Apply Ddle_trans with dd':=(ad_simple_path_dist cg1 y x). Assumption.
	Exact (ad_simple_path_dist_correct cg1 H y x ? ? H4).
	Apply (CG_path_app_1 cg1 (cons y l1) l'0 x x0). Rewrite <- H8. Apply CG_add_4_5.
    	Rewrite H8. Elim (CG_path_last cg2 ? ? ? H3). Intros l'1 H11. Rewrite H7 in H2.
    	Simpl. Rewrite (ad_list_stutters_app_conv_l ? ? H2). Rewrite H11 in H2.
    	Rewrite (ass_app l'0 l'1 (cons x0 (nil ad))) in H2.
    	Elim (sumbool_of_bool (ad_in_list x0 l'0)). Intro H12.
    	Rewrite (ad_list_stutters_prev_l ? (nil ad) ? (ad_in_list_l l'0 l'1 x0 H12)) in H2.
    	Discriminate H2.
	Intro H12. Rewrite H12. Reflexivity.
	Rewrite H8. Assumption.
	Apply CG_add_4_3. Rewrite H7 in H2. Exact (ad_list_stutters_app_conv_r l'0 (cons y l1) H2).
	Assumption.
	Generalize H5. Elim l0. Simpl. Intro H7. Exists (nil ad). Inversion_clear H7. Reflexivity.
	Intros. Rewrite (app_ass (cons a l2) (cons x (nil ad)) (cons y l1)) in H8. Simpl in H8.
    	Exists (app l2 (cons x (nil ad))). Inversion_clear H8. Reflexivity.
	Intro H3. Exact (H x0 d0 l H3).
    Qed.

Lemma CG_add_5_1 : (n:nat)
        ((x0:ad) (d0:D) (l:(list ad)) (CG_path cg1 x0 d0 (cons x0 l)) -> (Dle Dz d0)=true) ->
	  (Ddle (SOME ? (Dneg d)) (ad_simple_path_dist cg1 y x))=true ->
	    (x0:ad) (d0:D) (l:(list ad)) (CG_path cg2 x0 d0 (cons x0 l)) ->
            (le (length l) n) -> (Dle Dz d0)=true.
    Proof.
    	Induction n. Induction l. Intros. Inversion H1. Apply Dle_refl.
	Intros. Elim (le_Sn_O ? H3).
	Intros. Elim (sumbool_of_bool (ad_list_stutters l)). Intros H4.
    	Elim (ad_list_stutters_has_circuit l H4). Intros y0 H5. Elim H5. Intros l0 H6. Elim H6.
    	Intros l1 H7. Elim H7. Intros l2 H8. Rewrite H8 in H2.
    	Elim (CG_path_app_4 cg2 (cons x0 l0) (app l1 (cons y0 l2)) x0 y0 d0 H2). Intros d1 H9.
    	Elim H9. Intros d2 H10. Elim H10. Intros H11 H12. Elim H11. Intros H13 H14.
    	Clear H5 H6 H7 H9 H10 H11. Elim (CG_path_app_4 cg2 (cons y0 l1) l2 x0 y0 d2 H14).
    	Intros d3 H5. Elim H5. Intros d4 H6. Elim H6. Intros H7 H9. Elim H7. Intros H10 H11.
    	Clear H5 H6 H7 H14. Rewrite H12. Rewrite H9. Apply Dle_trans with d':=(Dplus d4 d1).
    	Apply (H H0 H1 x0 (Dplus d4 d1) (app l0 (cons y0 l2))).
    	Cut (cons x0 (app l0 (cons y0 l2)))=(app (app (cons x0 l0) (cons y0 (nil ad))) l2).
    	Intro. Rewrite H5. Exact (CG_path_app_1 cg2 ? ? ? ? ? ? H11 H13).
	Exact (ass_app (cons x0 l0) (cons y0 (nil ad)) l2).
	Rewrite ad_list_app_length. Apply le_S_n. Apply le_trans with m:=(length l).
    	Rewrite H8. Rewrite ad_list_app_length. Simpl. Rewrite ad_list_app_length. Simpl.
    	Rewrite <- plus_Snm_nSm. Rewrite <- plus_Snm_nSm. Rewrite <- plus_Snm_nSm. Simpl.
    	Rewrite <- plus_Snm_nSm. Simpl. Apply le_n_S. Apply le_n_S. Apply le_reg_l.
    	Apply le_plus_r. Assumption.
	Apply Dle_plus_mono. Apply Dle_trans with (Dplus d4 Dz). Rewrite Dplus_d_z. Apply Dle_refl.
    	Apply Dle_plus_mono. Apply Dle_refl.
    	Apply (H H0 H1 y0 d3 (app l1 (cons y0 (nil ad))) H10). Rewrite ad_list_app_length. Simpl.
    	Apply le_S_n. Apply le_trans with m:=(length l). Rewrite H8. Rewrite ad_list_app_length.
    	Simpl. Rewrite ad_list_app_length. Simpl. Rewrite <- plus_Snm_nSm. Rewrite <- plus_Snm_nSm.
    	Rewrite <- plus_Snm_nSm. Simpl. Rewrite <- plus_Snm_nSm. Simpl. Apply le_n_S. Apply le_n_S.
    	Apply le_trans with m:=(plus (length l1) (length l2)). Apply le_plus_plus. Apply le_n.
    	Apply le_O_n. Apply le_plus_r. Assumption.
	Apply Dle_refl.
	Intro H4. Exact (CG_add_4 H0 H1 x0 d0 l H2 H4).
    Qed.

Lemma CG_add_5 :
        ((x0:ad) (d0:D) (l:(list ad)) (CG_path cg1 x0 d0 (cons x0 l)) -> (Dle Dz d0)=true) ->
	  (Ddle (SOME ? (Dneg d)) (ad_simple_path_dist cg1 y x))=true ->
	    (x0:ad) (d0:D) (l:(list ad)) (CG_path cg2 x0 d0 (cons x0 l)) -> (Dle Dz d0)=true.
    Proof.
    	Intros. Exact (CG_add_5_1 (length l) H H0 x0 d0 l H1 (le_n ?)).
    Qed.

  End CGAdd.

(*s Properties of the range of the graph *)

Definition cg_range := (DMerge D).

Lemma cg_range_1 
  : (cg:CGraph1) (x,y:ad) (d:D) 
    (CG_edge cg x y)=(SOME ? d) -> (in_dom D y (cg_range cg))=true.
  Proof.
    Unfold cg_range CG_edge. Intros. Elim (option_sum ? (MapGet ? cg x)). Intro H0. Elim H0.
    Intros edges H1. Rewrite H1 in H. Apply in_dom_DMerge_3 with a:=x m0:=edges. Assumption.
    Unfold in_dom. Generalize H. Case (MapGet D edges y). Intro H2. Discriminate H2.
    Trivial.
    Intro H0. Rewrite H0 in H. Discriminate H.
  Qed.

Lemma cg_range_2 
  : (cg:CGraph1) (y:ad) (in_dom D y (cg_range cg))=true ->
      {x:ad & {d:D | (CG_edge cg x y)=(SOME ? d)}}.
  Proof.
    Intros. Elim (in_dom_DMerge_2 ? cg y H). Intros x H0. Elim H0. Intros edges H1. Elim H1.
    Intros H2 H3. Split with x. Elim (in_dom_some ? edges y H3). Intros d H4. Split with d.
    Unfold CG_edge. Rewrite H2. Rewrite H4. Reflexivity.
  Qed.

Lemma cg_range_4 : (cg:CGraph1) (x,y:ad) (d:D) (a:ad)
    	(in_dom D a (cg_range (CG_add cg x y d)))=(orb (ad_eq a y) (in_dom D a (cg_range cg))).
  Proof.
    Intros. Elim (sumbool_of_bool (in_dom D a (cg_range (CG_add cg x y d)))). Intro H.
    Elim (cg_range_2 ? ? H). Intros a0 H0. Elim H0. Intros d0 H1. Clear H0.
    Change (CG_edge (cg2 cg x y d) a0 a)=(SOME D d0) in H1.
    Rewrite (CG_add_edge_1 cg x y d a0 a) in H1. Rewrite H.
    Elim (sumbool_of_bool (andb (ad_eq x a0) (ad_eq y a))). Intro H2. Elim (andb_prop ? ? H2).
    Intros H3 H4. Rewrite (ad_eq_comm y a) in H4. Rewrite H4. Reflexivity.
    Intro H2. Rewrite H2 in H1. Rewrite (cg_range_1 ? ? ? ? H1). Apply sym_eq. Apply orb_b_true.
    Intro H. Rewrite H. Elim (sumbool_of_bool (ad_eq a y)). Intro H0.
    Rewrite (ad_eq_complete ? ? H0) in H. Cut {d0:D | (CG_edge (cg2 cg x y d) x y)=(SOME ? d0)}.
    Intro H1. Elim H1. Intros d0 H2. Unfold cg2 in H2. Rewrite (cg_range_1 ? ? ? ? H2) in H.
    Discriminate H.
    Rewrite (CG_add_edge_1 cg x y d x y). Rewrite (ad_eq_correct x). Rewrite (ad_eq_correct y).
    Case (CG_edge cg x y). Split with d. Reflexivity.
    Intro d0. Split with (Dmin d d0). Reflexivity.
    Intro H0. Rewrite H0. Elim (sumbool_of_bool (in_dom D a (cg_range cg))). Intro H1.
    Elim (cg_range_2 ? ? H1). Intros a0 H2. Elim H2. Intros d0 H3.
    Cut {d1:D | (CG_edge (cg2 cg x y d) a0 a)=(SOME ? d1)}. Intro H4. Elim H4. Intros d1 H5.
    Unfold cg2 in H5. Rewrite (cg_range_1 ? ? ? ? H5) in H. Discriminate H.
    Rewrite (CG_add_edge_1 cg x y d a0 a). Rewrite (ad_eq_comm a y) in H0. Rewrite H0.
    Rewrite (andb_b_false (ad_eq x a0)). Split with d0. Assumption.
    Intro H1. Rewrite H1. Reflexivity.
  Qed.

Lemma cg_out_of_range_1 : (cg:CGraph1) (y:ad) (in_dom D y (cg_range cg))=false ->
    	(x:ad) (CG_edge cg x y)=(NONE D).
  Proof.
    Intros. Elim (option_sum ? (CG_edge cg x y)). Intro H0. Elim H0. Intros d H1.
    Rewrite (cg_range_1 cg x y d H1) in H. Discriminate H.
    Trivial.
  Qed.

Lemma cg_out_of_range_2 : (cg:CGraph1) (y:ad) (in_dom D y (cg_range cg))=false ->
    	(x:ad) (ad_eq x y)=false -> (ad_simple_path_dist cg x y)=(NONE D).
  Proof.
    Intros. Elim (option_sum ? (ad_simple_path_dist cg x y)). Intro H1. Elim H1. Intros d H2.
    Elim (ad_simple_path_dist_complete_2 cg x y d H2). Intros l H3.
    Cut {a:ad & {d':D | (CG_edge cg a y)=(SOME ? d')}}. Intro H4. Elim H4. Intros a H5. Elim H5.
    Intros d' H6. Rewrite (cg_out_of_range_1 cg y H a) in H6. Discriminate H6.
    Generalize x H0 d H3. Elim l. Intros. Inversion H5. 
    Rewrite H8 in H4.
    Rewrite (ad_eq_correct y) in H4. Discriminate H4.
    Intros. Inversion H6. Elim (sumbool_of_bool (ad_eq a y)). 
    Intro H14. Split with x0.
    Split with d'. Rewrite <- (ad_eq_complete ? ? H14). Assumption.
    Intro H14. Exact (H4 a H14 ? H10).
    Trivial.
  Qed.

Lemma cg_out_of_range_3 : (cg:CGraph1) (y:ad) (in_dom D y (cg_range cg))=false ->
    	(x:ad) (ad_eq x y)=false -> (CG_leq cg x y)=false.
  Proof.
    Intros. Unfold CG_leq. Rewrite (cg_out_of_range_2 cg y H x H0). Reflexivity.
  Qed.

Lemma cg_add_out_of_range_1 : (cg:(CGraph1)) (x,y:ad) (d:D)
    	((x0:ad) (d0:D) (l:(list ad)) (CG_path cg x0 d0 (cons x0 l)) -> (Dle Dz d0)=true) ->
	(in_dom D x (cg_range cg))=false ->
	(ad_eq y x)=false ->
	  (x0:ad) (d0:D) (l:(list ad)) (CG_path (CG_add cg x y d) x0 d0 (cons x0 l)) ->
           (Dle Dz d0)=true.
  Proof.
    Intros. Apply (CG_add_5 cg x y d H) with x0:=x0 l:=l.
    Rewrite (cg_out_of_range_2 cg x H0 y H1). Reflexivity.
    Assumption.
  Qed.


Lemma cg_add_dom_subset : (cg:CGraph1) (x,y:ad) (d:D)
      (MapSubset ? ? (MapDom ? cg) (MapDom ? (CG_add cg x y d))).
  Proof.
    Unfold CG_add. Intros. Elim (option_sum ? (MapGet ? cg x)). Intro H. Elim H. Intros edges H0.
    Rewrite H0. Elim (option_sum ? (MapGet D edges y)). Intro H1. Elim H1. Intros d0 H2.
    Rewrite H2. Apply MapSubset_Dom_1. Apply MapSubset_Put.
    Intro H1. Rewrite H1. Apply MapSubset_Dom_1. Apply MapSubset_Put.
    Intro H. Rewrite H. Apply MapSubset_Dom_1. Apply MapSubset_Put.
  Qed.

(*s Adding a lis of adresses connected to [root] with weight $0$ *)

Fixpoint CG_add_root [root:ad; cg:CGraph1; l:(list ad)] : CGraph1 :=
    Cases l of
        nil => cg
	| (cons x l') => (CG_add_root root (CG_add cg root x Dz) l')
    end.

Lemma CG_add_root_out_of_range : (l:(list ad)) (cg:CGraph1) (root:ad)
    	((x0:ad) (d0:D) (l:(list ad)) (CG_path cg x0 d0 (cons x0 l)) -> (Dle Dz d0)=true) ->
    	(MapSubset ? ? (Elems l) (MapDom ? cg)) ->
	(ad_in_list root l)=false ->
	(in_dom ? root (cg_range cg))=false ->
	(x0:ad) (d0:D) (l0:(list ad)) (CG_path (CG_add_root root cg l) x0 d0 (cons x0 l0)) ->
	      (Dle Dz d0)=true.
    Proof.
    	Induction l. Trivial.
	Simpl. Intros. Apply (H (CG_add cg root a Dz) root) with x0:=x0 d0:=d0 l1:=l1. Intros.
    	Apply (cg_add_out_of_range_1 cg root a Dz) with x0:=x1 d0:=d1 l:=l2. Intros.
    	Exact (H0 ? ? ? H6).
	Assumption.
	Elim (orb_false_elim ? ? H2). Intros. Rewrite ad_eq_comm. Assumption.
	Assumption.
	Apply MapSubset_trans with m':=(MapPut unit (Elems l0) a tt). Apply MapSubset_Put.
	Apply MapSubset_trans with m':=(MapDom ? cg). Assumption.
	Apply cg_add_dom_subset.
	Elim (orb_false_elim ? ? H2). Trivial.
	Rewrite cg_range_4. Elim (orb_false_elim ? ? H2). Intros H5 H6. Rewrite H5. Rewrite H3.
    	Reflexivity.
	Assumption.
    Qed.

Lemma CG_add_rooted_1 : (cg:CGraph1) (root,a:ad) (d:D)
    	(CG_edge cg root a)=(NONE D) ->
    	  (CG_edge (CG_add cg root a d) root a)=(SOME D d).
  Proof.
    Intros. Change (CG_edge (cg2 cg root a d) root a)=(SOME D d). Rewrite CG_add_edge_1.
    Rewrite (ad_eq_correct root). Rewrite (ad_eq_correct a). Rewrite H. Reflexivity.
  Qed.

Lemma CG_add_root_rooted_1 : (l:(list ad)) (cg:CGraph1) (root,a:ad)
    	(ad_list_stutters l)=false ->
      (ad_in_list a l)=false -> (d:D) (CG_edge cg root a)=(SOME D d) ->
        (CG_edge (CG_add_root root cg l) root a)=(SOME D d).
  Proof.
    Induction l. Trivial.
    Simpl. Intros. Elim (orb_false_elim ? ? H0). Intros. Elim (orb_false_elim ? ? H1).
    Intros. Apply H. Assumption.
    Assumption.
    Change (CG_edge (cg2 cg root a Dz) root a0)=(SOME D d). Rewrite CG_add_edge_1.
    Rewrite (ad_eq_comm a a0). Rewrite H5. Rewrite (andb_b_false (ad_eq root root)). Assumption.
  Qed.

Lemma CG_add_root_rooted_2 : (l:(list ad)) (cg:CGraph1) (root:ad)
    	(ad_list_stutters l)=false ->
	((a0:ad) (ad_in_list a0 l)=true -> (CG_edge cg root a0)=(NONE D)) ->
    	(a:ad) (ad_in_list a l)=true ->
             (CG_edge (CG_add_root root cg l) root a)=(SOME ? Dz).
  Proof.
    Induction l. Intros. Discriminate H1.
    Simpl. Intros. Elim (orb_false_elim ? ? H0). Intros. Elim (sumbool_of_bool (ad_eq a0 a)).
    Intro H5. Rewrite (ad_eq_complete ? ? H5). Apply CG_add_root_rooted_1. Assumption.
    Assumption.
    Apply CG_add_rooted_1. Apply H1. Rewrite (ad_eq_correct a). Reflexivity.
    Intro H5. Rewrite H5 in H2. Simpl in H2. Apply H. Assumption.
    Intros. Change (CG_edge (cg2 cg root a Dz) root a1)=(NONE D). Rewrite CG_add_edge_1.
    Elim (sumbool_of_bool (ad_eq a a1)). Intro H7. Rewrite (ad_eq_complete ? ? H7) in H3.
    Rewrite H6 in H3. Discriminate H3.
    Intro H7. Rewrite H7. Rewrite (andb_b_false (ad_eq root root)).
    Apply H1. Rewrite H6. Apply orb_b_true.
    Assumption.
  Qed.
 
Lemma CG_add_root_rooted_3 : (cg:CGraph1) (root:ad)
	(in_dom ? root cg)=false ->
    	(a:ad) (in_dom ? a cg)=true ->
             (CG_edge (CG_add_root root cg (ad_list_of_dom ? cg)) root a)=(SOME ? Dz).
  Proof.
    Intros. Apply CG_add_root_rooted_2. Apply ad_list_of_dom_not_stutters.
    Intros. Unfold CG_edge. Rewrite (in_dom_none ? ? ? H). Reflexivity.
    Rewrite ad_in_list_of_dom_in_dom. Assumption.
  Qed.

Lemma CG_add_root_rooted_4 : (l:(list ad)) (cg:CGraph1) (root:ad) (a:ad)
    	(in_dom ? a (CG_add_root root cg l))=true -> {a=root}+{(in_dom ? a cg)=true}.
  Proof.
    Induction l. Intros. Right . Exact H.
    Simpl. Intros. Elim (H (CG_add cg root a Dz) root a0 H0). Intro H1. Left . Assumption.
    Intro H1. Unfold in_dom CG_add in H1. Elim (option_sum ? (MapGet ? cg root)). Intro H2.
    Elim H2. Intros edges H3. Rewrite H3 in H1. Elim (option_sum ? (MapGet D edges a)). Intro H4.
    Elim H4. Intros d H5. Rewrite H5 in H1.
    Rewrite (MapPut_semantics ? cg root (MapPut D edges a (Dmin Dz d)) a0) in H1.
    Elim (sumbool_of_bool (ad_eq root a0)). Intro H6. Left . Rewrite (ad_eq_complete ? ? H6).
    Reflexivity.
    Intro H6. Rewrite H6 in H1. Right . Exact H1.
    Intro H4. Rewrite H4 in H1.
    Rewrite (MapPut_semantics ? cg root (MapPut D edges a Dz) a0) in H1.
    Elim (sumbool_of_bool (ad_eq root a0)). Intro H5. Left . Rewrite (ad_eq_complete ? ? H5).
    Reflexivity.
    Intro H5. Rewrite H5 in H1. Right . Exact H1.
    Intro H2. Rewrite H2 in H1. Rewrite (MapPut_semantics ? cg root (M1 D a Dz) a0) in H1.
    Elim (sumbool_of_bool (ad_eq root a0)). Intro H3. Left . Rewrite (ad_eq_complete ? ? H3).
    Reflexivity.
    Intro H3. Rewrite H3 in H1. Right . Exact H1.
  Qed.

Lemma CG_edge_dist_some_1 : (n:nat) (cg:CGraph1) (x,y:ad) (d:D) (prefix:(list ad))
    	(CG_edge cg x y)=(SOME D d) -> (ad_in_list y prefix)=false ->
        {d':D | (Ddle (ad_simple_path_dist_1 cg x y prefix (S n)) (SOME ? d'))=true}.
  Proof.
    Unfold CG_edge. Intros. Simpl. Elim (sumbool_of_bool (ad_eq x y)). Intro H1. Rewrite H1.
    Split with Dz. Simpl. Apply Dle_refl.
    Intro H1. Rewrite H1. Elim (option_sum ? (MapGet ? cg x)). Intro H2. Elim H2.
    Intros edges H3. Rewrite H3 in H. Rewrite H3. Elim (option_sum ? (MapGet D edges y)).
    Intro H4. Elim H4. Intros d0 H5. Rewrite H5 in H. Inversion H. Split with (Dplus Dz d).
    Rewrite H7 in H5. Apply Ddle_trans with dd':=([z:ad] [d0:D]
            Case (orb (ad_eq z x) (ad_in_list z prefix)) of
               (NONE D)
               (Ddplus
                 (ad_simple_path_dist_1 cg z y (cons x prefix) n) d0)
               end y d).
    Apply all_min_le_1 with f:=[z:ad] [d0:D]
         Case (orb (ad_eq z x) (ad_in_list z prefix)) of
            (NONE D)
            (Ddplus (ad_simple_path_dist_1 cg z y (cons x prefix) n)
              d0)
            end.
    Assumption.
    Rewrite (ad_eq_comm y x). Rewrite H1. Rewrite H0. Simpl. Case n. Simpl.
    Rewrite (ad_eq_correct y). Apply Ddle_refl.
    Intro. Simpl. Rewrite (ad_eq_correct y). Apply Ddle_refl.
    Intro H4. Rewrite H4 in H. Discriminate H.
    Intro H2. Rewrite H2 in H. Discriminate H.
  Qed.

Lemma CG_edge_dist_some : (cg:CGraph1) (x,y:ad) (d:D)
    	(CG_edge cg x y)=(SOME D d) -> {d':D | (ad_simple_path_dist cg x y)=(SOME ? d')}.
  Proof.
    Intros. Elim (option_sum ? (ad_simple_path_dist cg x y)). Trivial.
    Unfold ad_simple_path_dist. Intro H0. Elim (option_sum ? (MapGet ? cg x)). Intro H1. Elim H1.
    Intros edges H2. Elim (MapCard_is_not_O ? cg x edges H2). Intros n H3.
    Elim (CG_edge_dist_some_1 n cg x y d (nil ad) H (refl_equal ? ?)). Intros d0 H4.
    Rewrite H3 in H0. Rewrite H0 in H4. Discriminate H4.
    Intro H1. Unfold CG_edge in H. Rewrite H1 in H. Discriminate H.
  Qed.

Lemma CG_add_root_rooted : (cg:CGraph1) (root:ad)
      ((x:ad) (d:D) (l:(list ad)) (CG_path cg x d (cons x l)) -> (Dle Dz d)=true) ->
 	(in_dom ? root cg)=false ->
    	(a:ad) (in_dom ? a (CG_add_root root cg (ad_list_of_dom ? cg)))=true ->
             (CG_leq (CG_add_root root cg (ad_list_of_dom ? cg)) root a)=true.
  Proof.
    Intros. Elim (CG_add_root_rooted_4 ? ? ? ? H1). Intro H2. Rewrite H2. Apply CG_leq_refl.
    Intro H2. Elim (CG_edge_dist_some ? root a Dz (CG_add_root_rooted_3 cg root H0 a H2)).
    Intros d H3. Unfold CG_leq. Rewrite H3. Reflexivity.
  Qed.

Lemma CG_add_sat : (cg:CGraph1) (root,a:ad) (d:D) (rho:ad->D)
    	(CGsat (cg2 cg root a d) rho) -> (CGsat cg rho).
  Proof.
    Unfold CGsat. Intros. Elim (sumbool_of_bool (andb (ad_eq root x) (ad_eq a y))). Intro H1.
    Apply Dle_trans with d':=(Dplus (rho y) (Dmin d d0)). Apply H. Rewrite CG_add_edge_1.
    Rewrite H1. Rewrite H0. Reflexivity.
    Apply Dle_plus_mono. Apply Dle_refl.
    Apply Dmin_le_2.
    Intro H1. Apply H. Rewrite CG_add_edge_1. Rewrite H1. Assumption.
  Qed.

Lemma CG_add_root_sat : (l:(list ad)) (cg:CGraph1) (root:ad) (rho:ad->D)
    	(CGsat (CG_add_root root cg l) rho) -> (CGsat cg rho).
  Proof.
    Induction l. Trivial.
    Simpl. Intros. Apply (CG_add_sat cg root a Dz rho). Unfold cg2. Exact (H ? ? ? H0).
  Qed.

Lemma CG_add_root_consistent : (l:(list ad)) (cg:CGraph1) (root:ad)
    	(CGconsistent (CG_add_root root cg l)) -> (CGconsistent cg).
  Proof.
    Unfold CGconsistent. Intros. Elim H. Intros rho H0. Split with rho.
    Exact (CG_add_root_sat l cg root rho H0).
  Qed.

Lemma CG_circuit_complete_1 : (cg:CGraph1)
    ((x:ad)(d:D)(l:(list ad))
     (CG_path cg x d (cons x l)) -> (Dle Dz d)=true) ->
     (root:ad) 
     root=(ad_alloc_opt unit (FSetUnion (MapDom ? cg) (MapDom ? (cg_range cg)))) ->
     (cg':CGraph1) cg'=(CG_add_root root cg (ad_list_of_dom ? cg)) 
  ->  (CGconsistent cg).
  Proof.
    Intros. Apply (CG_add_root_consistent (ad_list_of_dom (Map D) cg) cg root). Rewrite <- H1.
    Cut (orb (in_dom ? root cg) (in_dom ? root (cg_range cg)))=false. Intro H2.
    Elim (orb_false_elim ? ? H2). Intros. Elim CG_rooted_sat with cg:=cg' root:=root d0:=Dz.
    Intros rho H5. Elim H5. Unfold CGconsistent. Intros H6 H7. Split with rho. Assumption.
    Rewrite H1. Intros.
    Apply CG_add_root_out_of_range with l:=(ad_list_of_dom (Map D) cg) cg:=cg root:=root
                                        x0:=x d0:=d l0:=l.
    Assumption.
    Apply MapSubset_2_imp. Unfold MapSubset_2.
    Apply eqmap_trans with m':=(MapDomRestrBy unit unit (MapDom (Map D) cg) (MapDom (Map D) cg)).
    Apply MapDomRestrBy_ext. Apply Elems_of_list_of_dom.
    Apply eqmap_refl.
    Apply MapDomRestrBy_m_m_1.
    Rewrite ad_in_list_of_dom_in_dom. Assumption.
    Assumption.
    Assumption.
    Intros. Rewrite H1. Apply CG_add_root_rooted. Assumption.
    Assumption.
    Rewrite <- H1. Assumption.
    Rewrite MapDom_Dom. Rewrite MapDom_Dom. Rewrite <- in_FSet_union. Rewrite H0.
    Unfold in_FSet. Apply ad_alloc_opt_allocates.
  Qed.

(*s If there is no circuit [(cons x l)] with negative weight [d], then [cg] is consistent: *)

Theorem CG_circuit_complete : (cg:CGraph1)
      ((x:ad) (d:D) (l:(list ad)) (CG_path cg x d (cons x l)) -> (Dle Dz d)=true) ->
			          (CGconsistent cg).
  Proof.
    Intros. EApply CG_circuit_complete_1. Assumption.
    Reflexivity.
    Reflexivity.
  Qed.

Lemma CG_translate_l : (cg:CGraph1) (rho:ad->D) (d:D) (CGsat cg rho) ->
      (CGsat cg [a:ad] (Dplus d (rho a))).
  Proof.
    Unfold CGsat. Intros. Rewrite Dplus_assoc. Apply Dle_plus_mono. Apply Dle_refl.
    Apply H. Assumption.
  Qed.

(*s [(CGconsistent_anchored cg a d)] if there exists a valuation $\rho$ which 
     satisfies [cg] and such that $\rho(a)=d$
*)

Definition CGconsistent_anchored :=
      [cg:CGraph1][a:ad][d0:D] {rho:ad->D | (CGsat cg rho) /\ (rho a)=d0}.

Lemma CGconsistent_then_anchored 
  : (cg:CGraph1) (CGconsistent cg) ->
      (a:ad) (d0:D) (CGconsistent_anchored cg a d0).
  Proof.
    Unfold CGconsistent CGconsistent_anchored. Intros. Elim H. Intros rho H0.
    Split with [a0:ad](Dplus (Dplus d0 (Dneg (rho a))) (rho a0)). Split.
    Apply CG_translate_l. Assumption.
    Rewrite Dplus_assoc. Rewrite Dplus_neg_2. Apply Dplus_d_z.
  Qed.

Lemma CGanchored_then_consistent : (cg:CGraph1) (a:ad) (d0:D) (CGconsistent_anchored cg a d0) ->
      (CGconsistent cg).
  Proof.
    Unfold CGconsistent CGconsistent_anchored. Intros. Elim H. Intros rho H0. Elim H0. Intros.
    Split with rho. Assumption.
  Qed.

(*s Definition of [ad_0_path_dist_1] 
    a more efficient version of [ad_simple_path_dist]: *)

Section CGDist1.

    Variable cg : CGraph1.

Fixpoint ad_0_path_dist_1 [x,y:ad; l:(list ad); n:nat] : (option D) :=
      if (ad_eq x y)
      then (SOME ? Dz)
      else
      	Cases n of
	    O => (NONE ?)
	  | (S n') => Cases (MapGet ? cg x) of
	                  NONE => (NONE ?)
			| (SOME edges) =>
			  if (ad_in_list x l)
			  then (NONE D)
                          else let l'=(cons x l) in (* builds reverse path *)
			       (all_min [z:ad][d:D]
			           if (ad_in_list z l')
                                   then (NONE D)
				   else (Ddplus (ad_0_path_dist_1 z y l' n') d)
				   edges)
                      end
        end.

Definition ad_0_path_dist := [x,y:ad]
          (ad_0_path_dist_1 x y (nil ad) (MapCard ? cg)).

Lemma ad_0_path_dist_1_ge : (n:nat) (l:(list ad)) (x,y:ad)
      	(Ddle (ad_simple_path_dist_1 cg x y l n) (ad_0_path_dist_1 x y l n))=true.
    Proof.
      Induction n. Simpl. Intros. Apply Ddle_refl.
      Simpl. Intros. Case (ad_eq x y). Apply Ddle_refl.
      Case (MapGet ? cg x). Apply Ddle_refl.
      Intro. Case (ad_in_list x l). Apply Ddle_d_none.
      Apply all_min_le_3. Intros. Case (orb (ad_eq a x) (ad_in_list a l)). Apply Ddle_refl.
      Apply Ddle_plus_mono. Apply H.
      Apply Dle_refl.
    Qed.

Lemma ad_0_path_dist_1_correct_1 :
      	((x:ad) (d:D) (l:(list ad)) (CG_path cg x d (cons x l)) -> (Dle Dz d)=true) ->
        (n:nat) (x,y:ad) (l:(list ad)) (d:D)
    	  (le (length l) n) -> (CG_path cg y d (cons x l)) ->
      	  (prefix:(list ad))
      	    (ad_list_stutters (app (rev prefix) (cons x l)))=false ->
	      (Ddle (ad_0_path_dist_1 x y prefix n) (SOME ? d))=true.
    Proof.
      Induction n. Intros x y l. Case l. Intros. Simpl. 
      Inversion H1. Rewrite H5.
      Rewrite (ad_eq_correct y). Apply Ddle_refl.
      Intros. Elim (le_Sn_O ? H0).
      Intros n0 H0 x y l. Case l. Intros. Inversion H2. 
      Rewrite H6. Simpl.
      Rewrite (ad_eq_correct y). Rewrite H5. Apply Ddle_refl.
      Intros. Simpl. Elim (sumbool_of_bool (ad_eq x y)). Intro H4. Rewrite H4.
      Rewrite (ad_eq_complete ? ? H4) in H2. Exact (H ? ? ? H2).

      Intro H4. Rewrite H4. Elim (option_sum ? (MapGet ? cg x)). Intro H5. Elim H5.
      Intros edges H6. Rewrite H6. Inversion_clear H2. Apply Ddle_trans
             with dd':=Case (ad_in_list a (cons x prefix)) of
              (NONE D)
              (Ddplus (ad_0_path_dist_1 a y (cons x prefix) n0) d')
              end.
      Cut (MapGet ? edges a)=(SOME ? d'). Intro. Cut (ad_in_list x prefix)=false. Intro H9.
      Rewrite H9. Exact (all_min_le_1 [z:ad] [d:D]
          Case (orb (ad_eq z x) (ad_in_list z prefix)) of
             (NONE D)
             (Ddplus (ad_0_path_dist_1 z y (cons x prefix) n0) d)
             end edges a d' H2).
      Rewrite <- ad_in_list_rev. Exact (ad_list_stutters_prev_conv_l ? ? ? H3).
      Unfold CG_edge in H8. Rewrite H6 in H8. Elim (option_sum ? (MapGet D edges a)).
      Intro H9. Elim H9. Intros d1 H10. Rewrite H10 in H8. Rewrite H10. Assumption.
      Intro H9. Rewrite H9 in H8. Discriminate H8.
      Rewrite (ad_list_app_rev prefix (cons a l0) x) in H3.
      Rewrite <- (ad_in_list_rev (cons x prefix) a).
      Rewrite (ad_list_stutters_prev_conv_l ? ? ? H3).
      Apply (Ddle_plus_mono (ad_0_path_dist_1 a y (cons x prefix) n0) (SOME D d0) d' d').
      Exact (H0 a y l0 d0 (le_S_n ? ? H1) H7 (cons x prefix) H3).
      Apply Dle_refl.
      Intro H5. Inversion H2.
      Unfold CG_edge in H11. Rewrite H5 in H11. Discriminate H11.
    Qed.

Lemma ad_0_path_dist_1_le :
      	((x:ad) (d:D) (l:(list ad)) (CG_path cg x d (cons x l)) -> (Dle Dz d)=true) ->
	(n:nat) (x,y:ad)
      	  (Ddle (ad_0_path_dist_1 x y (nil ad) n) (ad_simple_path_dist_1 cg x y (nil ad) n))=true.
    Proof.
      Intros. Elim (option_sum ? (ad_simple_path_dist_1 cg x y (nil ad) n)). Intro H0. Elim H0.
      Intros d H1. Rewrite H1.
      Elim (ad_simple_path_dist_1_complete_1 cg n x y (nil ad) Dz) with d0:=d. Intros l H2.
      Elim H2. Intros H3 H4. Apply (ad_0_path_dist_1_correct_1 H n x y l). Exact (proj2 ? ? H4).
      Rewrite (Dplus_d_z d) in H3. Exact H3.
      Exact (proj1 ? ? H4).
      Simpl. Apply CG_p1. Reflexivity.
      Reflexivity.
      Assumption.
      Intro H0. Rewrite H0. Apply Ddle_d_none.
    Qed.

Lemma ad_0_path_dist_correct_1 :
      	(CGconsistent cg) ->
	(x,y:ad) (n:nat)
	  (ad_0_path_dist_1 x y (nil ad) n)=(ad_simple_path_dist_1 cg x y (nil ad) n).
    Proof.
      Intros. Apply Ddle_antisym. Apply ad_0_path_dist_1_le.
      Exact (CG_circuits_non_negative_weight cg H).
      Apply ad_0_path_dist_1_ge.
    Qed.

Lemma ad_0_path_dist_correct : (CGconsistent cg) ->
      	(x,y:ad) (ad_0_path_dist x y)=(ad_simple_path_dist cg x y).
    Proof.
      Intros. Exact (ad_0_path_dist_correct_1 H x y (MapCard ? cg)).
    Qed.

(*s Uses a set [s] of already visited nodes *)

Fixpoint ad_1_path_dist_1 [x,y:ad; s:FSet; n:nat] : (option D) :=
      if (ad_eq x y)
      then (SOME ? Dz)
      else
      	Cases n of
	    O => (NONE ?)
	  | (S n') => Cases (MapGet ? cg x) of
	                  NONE => (NONE ?)
			| (SOME edges) =>
			  Cases (MapGet ? s x) of
			      (SOME _) => (NONE ?)
			    | NONE => let s'=(MapPut unit s x tt) in
			              (all_min [z:ad][d:D]
			                  Cases (MapGet ? s' z) of
				              (SOME _) => (NONE D)
				            | NONE => (Ddplus (ad_1_path_dist_1 z y s' n') d)
				          end
				          edges)
                          end
	              end
        end.

Definition ad_1_path_dist := [x,y:ad]
          (ad_1_path_dist_1 x y (M0 unit) (MapCard ? cg)).

Lemma ad_1_path_dist_correct_1 : (n:nat) (x,y:ad) (l:(list ad))
      	  (ad_1_path_dist_1 x y (Elems l) n)=(ad_0_path_dist_1 x y l n).
    Proof.
      Induction n. Trivial.
      Simpl. Intros. Case (ad_eq x y). Reflexivity.
      Case (MapGet ? cg x). Reflexivity.
      Intro. Unfold all_min. Elim (sumbool_of_bool (ad_in_list x l)). Intro H0. Rewrite H0.
      Rewrite <- (ad_in_elems_in_list l x) in H0. Elim (in_dom_some ? ? ? H0). Intros t H1.
      Rewrite H1. Reflexivity.
      Intro H0. Rewrite H0. Rewrite <- (ad_in_elems_in_list l x) in H0.
      Rewrite (in_dom_none ? ? ? H0). Apply MapFold_ext_f. Intros.
      Elim (sumbool_of_bool (ad_in_list a (cons x l))). Intro H2.
      Rewrite <- (ad_in_elems_in_list (cons x l) a) in H2. Elim (in_dom_some ? ? ? H2). Simpl.
      Intro t. Elim t. Intro H3. Rewrite H3. Rewrite (ad_in_elems_in_list (cons x l) a) in H2.
      Simpl in H2. Rewrite H2. Reflexivity.
      Intro H2. Cut (orb (ad_eq a x) (ad_in_list a l))=false. Intro H3. Rewrite H3.
      Rewrite <- (ad_in_elems_in_list (cons x l) a) in H2. Simpl in H2.
      Rewrite (in_dom_none ? ? ? H2). Rewrite <- (H a y (cons x l)). Reflexivity.
      Assumption.
    Qed.

Lemma ad_1_path_dist_correct_2 : (x,y:ad) (ad_1_path_dist x y)=(ad_0_path_dist x y).
    Proof.
      Intros. Exact (ad_1_path_dist_correct_1 (MapCard ? cg) x y (nil ad)).
    Qed.

Lemma ad_1_path_dist_correct_3 : (CGconsistent cg) ->
      	(n:nat) (x,y:ad)
          (ad_1_path_dist_1 x y (M0 unit) n)=(ad_simple_path_dist_1 cg x y (nil ad) n).
    Proof.
      Intros. Rewrite <- (ad_0_path_dist_correct_1 H x y n).
      Exact (ad_1_path_dist_correct_1 n x y (nil ad)).
    Qed.

Lemma ad_1_path_dist_correct : (CGconsistent cg) ->
      	(x,y:ad) (ad_1_path_dist x y)=(ad_simple_path_dist cg x y).
    Proof.
      Intros. Rewrite ad_1_path_dist_correct_2. Apply ad_0_path_dist_correct. Assumption.
    Qed.

Lemma ad_1_path_dist_big_enough_1 : (n:nat) (s:FSet)
      	(MapSubset ? ? s (MapDom ? cg)) ->
      	(le (MapCard ? cg) (plus n (MapCard ? s))) ->
	  (x,y:ad) (ad_1_path_dist_1 x y s n)=(ad_1_path_dist_1 x y s (S n)).
    Proof.
      Induction n. Intros. Simpl. Case (ad_eq x y). Reflexivity.
      Elim (sumbool_of_bool (in_dom ? x cg)). Intro H1. Elim (in_dom_some ? ? ? H1).
      Intros edges H2. Rewrite H2. Elim (sumbool_of_bool (in_dom ? x s)). Intro H3.
      Elim (in_dom_some ? ? ? H3). Intros t H4. Rewrite H4. Reflexivity.
      Intro H3. Simpl in H0. Rewrite (MapCard_Dom ? cg) in H0. Rewrite (MapDom_Dom ? s x) in H3.
      Cut (MapGet ? (MapDom unit s) x)=(NONE ?). Rewrite (MapSubset_card_eq ? ? ? ? H H0 x).
      Rewrite (FSet_Dom (MapDom ? cg)).
      Elim (in_dom_some ? ? ? (MapDom_semantics_1 ? cg x edges H2)). Intros t H4 H5.
      Rewrite H4 in H5. Discriminate H5.
      Exact (in_dom_none ? ? ? H3).
      Intro H1. Rewrite (in_dom_none ? ? ? H1). Reflexivity.
      Intros. Cut (m:nat) m=(S n0)->(ad_1_path_dist_1 x y s (S n0))=(ad_1_path_dist_1 x y s (S m)).
      Intro. Exact (H2 (S n0) (refl_equal ? ?)).
      Intros. Simpl. Case (ad_eq x y). Reflexivity.
      Elim (option_sum ? (MapGet ? cg x)). Intro H'. Elim H'. Intros edges H'0. Rewrite H'0.
      Elim (option_sum ? (MapGet ? s x)). Intro H3. Elim H3. Intros t H4. Rewrite H4. Reflexivity.
      Intro H3. Rewrite H3. Unfold all_min. Apply MapFold_ext_f. Intros.
      Rewrite (MapPut_semantics unit s x tt a). Elim (sumbool_of_bool (ad_eq x a)). Intro H5.
      Rewrite H5. Reflexivity.
      Intro H5. Rewrite H5. Case (MapGet ? s a). Rewrite H2. Rewrite H. Reflexivity.
      Unfold MapSubset. Intros. Elim (in_dom_some ? ? ? H6). Intro.
      Rewrite (MapPut_semantics unit s x tt a0). Elim (sumbool_of_bool (ad_eq x a0)). Intro H7.
      Rewrite H7. Intro H8. Rewrite <- (ad_eq_complete ? ? H7). Fold (in_FSet x (MapDom ? cg)).
      Rewrite <- (MapDom_Dom ? cg x). Unfold in_dom. Rewrite H'0. Reflexivity.
      Intro H7. Rewrite H7. Intro H8. Apply (H0 a0). Unfold in_dom. Rewrite H8. Reflexivity.
      Rewrite MapCard_Put_2_conv. Rewrite <- plus_Snm_nSm. Exact H1.
      Assumption.
      Trivial.
      Intro H3. Rewrite H3. Reflexivity.
    Qed.

Lemma ad_1_path_dist_big_enough_2 : (n:nat)
	  (x,y:ad) (ad_1_path_dist x y)=(ad_1_path_dist_1 x y (M0 unit) (plus n (MapCard ? cg))).
    Proof.
      Induction n. Trivial.
      Intros. Unfold plus. Fold (plus n0 (MapCard ? cg)). Rewrite <- ad_1_path_dist_big_enough_1.
      Apply H.
      Unfold MapSubset. Intros. Discriminate H0.
      Simpl. Rewrite <- plus_n_O. Apply le_plus_r.
    Qed.

Lemma ad_1_path_dist_big_enough : (n:nat) (le (MapCard ? cg) n) ->
	  (x,y:ad) (ad_1_path_dist x y)=(ad_1_path_dist_1 x y (M0 unit) n).
    Proof.
      Intros. Cut (EX m:nat | n=(plus m (MapCard ? cg))). Intro H0. Elim H0. Intros m H1.
      Rewrite H1. Apply ad_1_path_dist_big_enough_2.
      Elim H. Split with O. Reflexivity.
      Intros. Elim H1. Intros m' H2. Split with (S m'). Rewrite H2. Reflexivity.
    Qed.

End CGDist1.

(*s Definition of concrete formulas : 
    [(CGleq x y d)] means $x\leq y+d$, [(CGeq x y d)] means $x=y+d$
 *)

Inductive CGForm : Set :=
      CGleq : ad -> ad -> D -> CGForm 
    | CGeq : ad -> ad -> D -> CGForm  
    | CGand : CGForm -> CGForm -> CGForm
    | CGor : CGForm -> CGForm -> CGForm
    | CGimp : CGForm -> CGForm -> CGForm
    | CGnot : CGForm -> CGForm.

(*s Interpretation of concrete formulas as proposition *)

Fixpoint CGeval [rho:ad->D; f:CGForm] : Prop :=
    Cases f of
        (CGleq x y d) => (Dle (rho x) (Dplus (rho y) d))=true
      | (CGeq x y d) => (rho x)=(Dplus (rho y) d)
      | (CGand f0 f1) => (CGeval rho f0) /\ (CGeval rho f1)
      | (CGor f0 f1) => (CGeval rho f0) \/ (CGeval rho f1)
      | (CGimp f0 f1) => (CGeval rho f0) -> (CGeval rho f1)
      | (CGnot f0) => ~(CGeval rho f0)
    end.

(*s Decidability of satisfaction *)

Lemma CGeval_dec : (f:CGForm) (rho:ad->D) {(CGeval rho f)}+{~(CGeval rho f)}.
  Proof.
    Induction f. 
    Intros. Simpl. Elim (sumbool_of_bool (Dle (rho a) (Dplus (rho a0) d))).
    Intro H. Left . Assumption.
    Intro H. Right . Unfold not. Rewrite H. Intro H0. Discriminate H0.
    Simpl. Intros. Apply D_dec.
    Simpl. Intros. Elim (H rho). Intro H1. Elim (H0 rho). Intro H2. Left . (Split; Assumption).
    Unfold not. Intro H2. Right . Intro. Elim H3. Intro. Assumption.
    Unfold not. Intro H1. Right . Intro H2. Elim H2. Intros. Apply (H1 H3).
    Simpl. Intros. Elim (H rho). Intro H1. Left . Left . Assumption.
    Elim (H0 rho). Intros H1 H2. Left . Right . Assumption.
    Unfold not. Intros H1 H2. Right . Intro H3. (Elim H3; Assumption).
    Simpl. Intros. Elim (H0 rho). Intro H1. Left . Intro H2. Assumption.
    Elim (H rho). Unfold not. Intros H1 H2. Right . Intro H3. Apply H2. Apply H3. Assumption.
    Unfold not. Intros H1 H2. Left . Intro H3. Elim (H1 H3).
    Intros. Simpl. Elim (H rho). Intro H0. Right . Unfold not. Intros. Exact (H1 H0).
    Intro H0. Left . Assumption.
  Qed.

(*s Simplified formulae: *)
Inductive CGSForm : Set :=
      CGSleq : ad -> ad -> D -> CGSForm
    | CGSand : CGSForm -> CGSForm -> CGSForm
    | CGSor : CGSForm -> CGSForm -> CGSForm.

Fixpoint CG_of_CGS [fs:CGSForm] : CGForm :=
    Cases fs of
        (CGSleq x y d) => (CGleq x y d)
      | (CGSand fs0 fs1) => (CGand (CG_of_CGS fs0) (CG_of_CGS fs1))
      | (CGSor fs0 fs1) => (CGor (CG_of_CGS fs0) (CG_of_CGS fs1))
    end.

Definition CGSeval := [rho:ad->D; fs:CGSForm] (CGeval rho (CG_of_CGS fs)).

(*s Decision procedure for simplified formulae *)

Section CGSSolve.

    Variable anchor : ad.
    Variable anchor_value : D.

(*s Is $x\leq y+d$ consistent with [cg]? *)

Definition CG_test_ineq := [cg:CGraph1; n:nat; x,y:ad; d:D]
      	(Ddle (SOME ? (Dneg d)) (ad_1_path_dist_1 cg y x (M0 unit) n)).

    Variable def_answer : bool.

(*s Invariants: [cg] is consistent, [|cg|<=n].
       [(CGS_solve_1 cg n fsl gas)]
       returns [true] iff [cg /\ fsl /\ anchor=anchor_value] is consistent,
       	 where [fsl] is understood as the conjunction of all the [fs] in [fsl];
         i.e. iff [cg /\ fsl] alone is consistent 
          (lemmas [CG_anchored_then_consistent] and [CG_consistent_then_anchored)].
       [gas] is intended to be [>=] the sum of sizes of all formulas in [fsl]. *)

Fixpoint CGS_solve_1 [cg:CGraph1; n:nat; fsl:(list CGSForm); gas:nat] : bool :=
      Cases gas of
          O => def_answer
	| (S gas') =>
          Cases fsl of
              nil => true
	    | (cons fs fsl') =>
              Cases fs of
                  (CGSleq x y d) => if (CG_test_ineq cg n x y d)
                                    then let cg' = (CG_add cg x y d) in 
                                             (CGS_solve_1 cg' (S n) fsl' gas')
		                    else false
                | (CGSand fs0 fs1) => (CGS_solve_1 cg n (cons fs0 (cons fs1 fsl')) gas')
	        | (CGSor fs0 fs1) => if (CGS_solve_1 cg n (cons fs0 fsl') gas')
	                             then true
			             else (CGS_solve_1 cg n (cons fs1 fsl') gas')
              end
          end
      end.

(*s Size of a set of formula and of a list of formula *)

Fixpoint FSize [f:CGSForm] : nat :=
      Cases f of
          (CGSand f0 f1) => (S (plus (FSize f0) (FSize f1)))
	| (CGSor f0 f1) => (S (plus (FSize f0) (FSize f1)))
	| _ => (1)
      end.

Fixpoint FlSize [fsl:(list CGSForm)] : nat :=
      Cases fsl of
          nil => O
	| (cons fs fsl') => (plus (FSize fs) (FlSize fsl'))
      end.


Definition CGS_solve := [fs:CGSForm] (CGS_solve_1 (M0 ?) O (cons fs (nil ?)) (S (FSize fs))).

(*s Interpretation of a list of formula as a conjonction *)

Fixpoint CGSeval_l [rho:ad->D; fsl:(list CGSForm)] : Prop :=
      Cases fsl of
          nil => True
	| (cons fs fsl') => (CGSeval rho fs) /\ (CGSeval_l rho fsl')
      end.

Lemma FSize_geq_1 : (fs:CGSForm) {n:nat | (FSize fs)=(S n)}.
    Proof.
      Induction fs. Simpl. Intros. Split with O. Reflexivity.
      Intros. Simpl. Split with (plus (FSize c) (FSize c0)). Reflexivity.
      Intros. Simpl. Split with (plus (FSize c) (FSize c0)). Reflexivity.
    Qed.

Lemma FlSize_is_O : (fsl:(list CGSForm)) (FlSize fsl)=O -> fsl=(nil ?).
    Proof.
      Induction fsl. Trivial.
      Intros. Simpl in H0. Elim (FSize_geq_1 a). Intros n H1. Rewrite H1 in H0. Discriminate H0.
    Qed.

Lemma CG_add_card_le : (cg:CGraph1) (x,y:ad) (d:D) (n:nat)
      	(le (MapCard ? cg) n) -> (le (MapCard ? (CG_add cg x y d)) (S n)).
    Proof.
      Intros. Unfold CG_add. Case (MapGet ? cg x).
      Apply (le_trans ? ? (S n) (MapCard_Put_ub ? cg x (M1 D y d))). Apply le_n_S. Assumption.
      Intro. Case (MapGet ? m y).
      Apply (le_trans ? ? (S n) (MapCard_Put_ub ? cg x (MapPut D m y d))). Apply le_n_S.
      Assumption.
      Intro. Apply (le_trans ? ? (S n) (MapCard_Put_ub ? cg x (MapPut D m y (Dmin d d0)))).
      Apply le_n_S. Assumption.
    Qed.

Lemma CGS_solve_1_correct : (gas:nat) (fsl:(list CGSForm)) (cg:CGraph1) (n:nat)
      	(CGconsistent cg) ->
      	(le (MapCard ? cg) n) ->
	(lt (FlSize fsl) gas) ->
	(CGS_solve_1 cg n fsl gas)=true ->
	  {rho:ad->D | (CGSeval_l rho fsl) /\ (CGsat cg rho)}.
    Proof.
      Induction gas. Intros. Elim (lt_n_O ? H1).
      Simpl. Intros n H fsl. Case fsl. Intros. Elim H0. Intros rho H4. Split with rho.
      Split. Exact I.
      Assumption.
      Intro fs. Case fs. Intros. Elim (sumbool_of_bool (CG_test_ineq cg n0 a a0 d)). Intro H4.
      Rewrite H4 in H3. Unfold CG_test_ineq in H4. Elim (H l (CG_add cg a a0 d) (S n0)).
      Intros rho H5. Split with rho. Elim H5. Intros. Split. Simpl. Split. Unfold CGSeval.
      Simpl. Exact (CG_add_3 cg a a0 d rho H7).
      Assumption.
      Exact (CG_add_2 cg a a0 d rho H7).
      Apply CG_circuit_complete. 
      Rewrite <- (ad_1_path_dist_big_enough cg n0 H1 a0 a) in H4.
      Rewrite (ad_1_path_dist_correct cg H0 a0 a) in H4.
      Exact (CG_add_5 cg a a0 d (CG_circuits_non_negative_weight cg H0) H4).
      Apply CG_add_card_le. Assumption.
      Exact (lt_S_n ? ? H2).
      Assumption.
      Intro H4. Rewrite H4 in H3. Discriminate H3.
      Intros. Elim (H (cons c (cons c0 l)) cg n0 H0). Simpl. Intros rho H4. Split with rho.
      Elim H4. Intros. Split; Try Assumption. Elim H5. Intros. Elim H8. Intros.
      Split; Try Assumption. Exact (conj ? ? H7 H9).
      Assumption.
      Simpl. Simpl in H2. Rewrite plus_assoc_l. Apply lt_S_n. Assumption.
      Assumption.
      Intros. Elim (sumbool_of_bool (CGS_solve_1 cg n0 (cons c l) n)). Intro H4.
      Elim (H (cons c l) cg n0 H0 H1). Intros rho H5. Split with rho. Elim H5. Intros. Simpl.
      Split; Try Assumption. Split; Try Assumption. Unfold CGSeval. Simpl. Left . Simpl in H6.
      Exact (proj1 ? ? H6).
      Exact (proj2 ? ? H6).
      Simpl. Simpl in H2. Apply le_lt_trans with m:=(plus (plus (FSize c) (FSize c0)) (FlSize l)).
      Apply le_reg_r. Apply le_plus_l.
      Apply lt_S_n. Assumption.
      Assumption.
      Intro H4. Rewrite H4 in H3. Elim (H (cons c0 l) cg n0 H0). Intros rho H5. Elim H5.
      Intros. Split with rho. Split; Try Assumption. Simpl. Simpl in H6. Elim H6. Intros.
      Split; Try Assumption. Unfold CGSeval. Simpl. Right . Assumption.
      Assumption.
      Simpl. Simpl in H2. Apply le_lt_trans with m:=(plus (plus (FSize c) (FSize c0)) (FlSize l)).
      Apply le_reg_r. Apply le_plus_r.
      Apply lt_S_n. Assumption.
      Assumption.
    Qed.

Lemma CGS_solve_correct : (fs:CGSForm) (CGS_solve fs)=true ->
          {rho:ad->D | (CGSeval rho fs)}.
    Proof.
      Intros. Elim (CGS_solve_1_correct (S (FSize fs)) (cons fs (nil ?)) (M0 ?) O). Intros rho H0.
      Simpl in H0. Elim H0. Intros. Elim H1. Intros. Split with rho. Assumption.
      Split with [x:ad]Dz. Unfold CGsat. Unfold CG_edge. Simpl. Intros. Discriminate H0.
      Apply le_n.
      Simpl. Rewrite <- plus_n_O. Unfold lt. Apply le_n.
      Exact H.
    Qed.

Lemma CGS_translate_l : (fs:CGSForm) (rho:ad->D) (d:D) (CGSeval rho fs) ->
      	(CGSeval [a:ad] (Dplus d (rho a)) fs).
    Proof.
      Induction fs. Unfold CGSeval. Simpl. Intros. Rewrite Dplus_assoc. Apply Dle_plus_mono.
      Apply Dle_refl.
      Assumption.
      Unfold CGSeval. Simpl. Intros. Elim H1. Intros. Split. Apply H. Assumption.
      Apply H0. Assumption.
      Unfold CGSeval. Simpl. Intros. Elim H1. Intro. Left . Apply H. Assumption.
      Intro. Right . Apply H0. Assumption.
    Qed.

Lemma CGS_solve_correct_anchored : (fs:CGSForm) (CGS_solve fs)=true ->
          {rho:ad->D | (CGSeval rho fs) /\ (rho anchor)=anchor_value}.
    Proof.
      Intros. Elim (CGS_solve_correct fs H). Intros rho H0.
      Split with [a:ad](Dplus (Dplus anchor_value (Dneg (rho anchor))) (rho a)). Split.
      Apply CGS_translate_l. Assumption.
      Rewrite Dplus_assoc. Rewrite Dplus_neg_2. Apply Dplus_d_z.
    Qed.

Lemma CGS_solve_complete_1 : (gas:nat) (fsl:(list CGSForm)) (cg:CGraph1) (n:nat)
        (lt (FlSize fsl) gas) -> (le (MapCard ? cg) n) ->
      	  (rho:ad->D) (CGsat cg rho) -> (CGSeval_l rho fsl) -> (CGS_solve_1 cg n fsl gas)=true.
    Proof.
      Induction gas. Intros. Elim (lt_n_O ? H).
      Intros n H fsl. Case fsl. Trivial.
      Intro fs. Case fs. Simpl. Intros. Unfold CG_test_ineq.
      Rewrite <- (ad_1_path_dist_big_enough cg n0 H1 a0 a). Cut (CGconsistent cg). Intro H4.
      Rewrite (ad_1_path_dist_correct cg H4 a0 a). Elim H3. Intros.
      Rewrite (CG_sat_add_1 cg a a0 d rho H2 H5). Apply H with rho:=rho. Apply lt_S_n. Assumption.
      Apply CG_add_card_le. Assumption.
      Fold (cg2 cg a a0 d). Apply CG_add_1. Assumption.
      Exact H5.
      Assumption.
      Split with rho. Assumption.
      Simpl. Intros. Apply H with rho:=rho. Simpl. Rewrite plus_assoc_l. Apply lt_S_n. Assumption.
      Assumption.
      Assumption.
      Elim H3. Intros. Simpl. Elim H4. Intros. Split; Try Assumption. Split; Assumption.
      Simpl. Intros. Elim H3. Intros. Elim H4. Intro. Cut (CGS_solve_1 cg n0 (cons c l) n)=true.
      Intro H7. Rewrite H7. Reflexivity.
      Apply H with rho:=rho. Simpl.
      Apply le_lt_trans with m:=(plus (plus (FSize c) (FSize c0)) (FlSize l)). Apply le_reg_r.
      Apply le_plus_l.
      Apply lt_S_n. Assumption.
      Assumption.
      Assumption.
      Split; Assumption.
      Intro H6. Cut (CGS_solve_1 cg n0 (cons c0 l) n)=true. Intro H7.
      Case (CGS_solve_1 cg n0 (cons c l) n); Trivial.
      Apply H with rho:=rho. Simpl.
      Apply le_lt_trans with m:=(plus (plus (FSize c) (FSize c0)) (FlSize l)). Apply le_reg_r.
      Apply le_plus_r.
      Apply lt_S_n. Assumption.
      Assumption.
      Assumption.
      Split; Assumption.
    Qed.

Lemma CGS_solve_complete : (fs:CGSForm) (rho:ad->D)
        (CGSeval rho fs) -> (CGS_solve fs)=true.
    Proof.
      Intros. Apply (CGS_solve_complete_1 (S (FSize fs)) (cons fs (nil ?)) (M0 ?) O) with rho:=rho.
      Simpl. Rewrite <- plus_n_O. Unfold lt. Apply le_n.
      Apply le_n.
      Unfold CGsat. Intros. Unfold CG_edge in H0. Discriminate H0.
      Simpl. Split; Trivial.
    Qed.

Definition CGSeq := [x,y:ad] [d:D] (CGSand (CGSleq x y d) (CGSleq y x (Dneg d))).

Lemma CGSeq_correct : (x,y:ad) (d:D) (rho:ad->D)
      	(CGSeval rho (CGSeq x y d)) -> (rho x)=(Dplus (rho y) d).
    Proof.
      Intros. Unfold CGSeq CGSeval in H. Simpl in H. Elim H. Intros. Apply Dle_antisym. Assumption.
      Apply Dplus_reg_r with d'':=(Dneg d). Rewrite Dplus_assoc. Rewrite Dplus_neg.
      Rewrite Dplus_d_z. Assumption.
    Qed.

Lemma CGSeq_complete : (x,y:ad) (d:D) (rho:ad->D)
      	(rho x)=(Dplus (rho y) d) -> (CGSeval rho (CGSeq x y d)).
    Proof.
      Intros. Unfold CGSeq CGSeval. Simpl. Rewrite H. Split. Apply Dle_refl.
      Rewrite Dplus_assoc. Rewrite Dplus_neg. Rewrite Dplus_d_z. Apply Dle_refl.
    Qed.

End CGSSolve.

Section CGWithOne.

    Variable Done : D.
    Variable Done_pos : (Dle Done Dz)=false.
    Variable Done_min_pos : (d:D) (Dle d Dz)=false -> (Dle Done d)=true.

(*s Defining the negation of a formula :
	  $\neg x \leq y+d \Leftrightarrow x>y+d \Leftrightarrow x \geq y+d+1$
 *)
Fixpoint CGSnot [fs:CGSForm] : CGSForm :=
      Cases fs of
      	  (CGSleq x y d) => (CGSleq y x (Dneg (Dplus d Done)))

	| (CGSand f0 f1) => (CGSor (CGSnot f0) (CGSnot f1))
	| (CGSor f0 f1) => (CGSand (CGSnot f0) (CGSnot f1))
      end.

Lemma Dmone_neg : (Dle Dz (Dneg Done))=false.
    Proof.
      Elim (sumbool_of_bool (Dle Dz (Dneg Done))). Intro H. 
      Rewrite <- (Dneg_neg Done) in Done_pos.
      Rewrite (Dle_neg ? H) in Done_pos. Discriminate Done_pos.
      Trivial.
    Qed.

Lemma Dminus_one_1 : (d:D) (Dle d (Dplus d (Dneg Done)))=false.
    Proof.
      Intro. Elim (sumbool_of_bool (Dle d (Dplus d (Dneg Done)))). Intro H.
      Cut (Dle Dz (Dneg Done))=true. Intro. Rewrite Dmone_neg in H0. Discriminate H0.
      Apply Dplus_reg_l with d'':=d. Rewrite Dplus_d_z. Assumption.
      Trivial.
    Qed.

Lemma Dle_lt_1 : (d,d':D) (Dle d' d)=false -> (Dle d (Dplus d' (Dneg Done)))=true.
    Proof.
      Intros. Apply Dplus_reg_r with d'':=Done. Rewrite Dplus_assoc. Rewrite Dplus_neg_2.
      Rewrite Dplus_d_z. Apply Dplus_reg_l with d'':=(Dneg d). Rewrite <- Dplus_assoc.
      Rewrite Dplus_neg_2. Rewrite Dplus_z_d. Apply Done_min_pos.
      Elim (sumbool_of_bool (Dle (Dplus (Dneg d) d') Dz)). Intro H0. Cut (Dle d' d)=true.
      Rewrite H. Intro. Discriminate H1.
      Apply Dplus_reg_l with d'':=(Dneg d). Rewrite Dplus_neg_2. Assumption.
      Trivial.
    Qed.

Lemma Dle_lt_2 : (d,d':D) (Dle d (Dplus d' (Dneg Done)))=true -> (Dle d' d)=false.
    Proof.
      Intros. Elim (sumbool_of_bool (Dle d' d)). Intro H0. Cut (Dle d (Dplus d (Dneg Done)))=true.
      Rewrite Dminus_one_1. Intro. Discriminate H1.
      Apply Dle_trans with d':=(Dplus d' (Dneg Done)). Assumption.
      Apply Dle_plus_mono. Assumption.
      Apply Dle_refl.
      Trivial.
    Qed.

Lemma CGSnot_correct : (fs:CGSForm) (rho:ad->D) (CGSeval rho fs) -> ~(CGSeval rho (CGSnot fs)).
    Proof.
      Unfold not. Induction fs. Simpl. Unfold CGSeval. Simpl. Intros.
      Cut (Dle (rho a) (Dplus (rho a) (Dneg Done)))=true. Rewrite Dminus_one_1. Intro.
      Discriminate H1.
      Rewrite <- (Dplus_d_z (Dneg Done)). Rewrite <- (Dplus_neg_2 d).
      Rewrite <- (Dplus_assoc (Dneg Done)). Rewrite <- Dneg_plus.
      Apply Dle_trans with d':=(Dplus (rho a0) d). Assumption.
      Rewrite <- Dplus_assoc. Apply Dle_plus_mono. Assumption.
      Apply Dle_refl.
      Unfold CGSeval. Simpl. Intros. Elim H1. Intros. Elim H2. Intro. Exact (H rho H3 H5).
      Intro. Exact (H0 rho H4 H5).
      Unfold CGSeval. Simpl. Intros. Elim H2. Intros. Elim H1. Intro. Exact (H rho H5 H3).
      Intro. Exact (H0 rho H5 H4).
    Qed.

Lemma CGSnot_complete : (fs:CGSForm) (rho:ad->D)
        ~(CGSeval rho (CGSnot fs)) -> (CGSeval rho fs).
    Proof.
      Unfold not CGSeval. Induction fs. Simpl. Intros.
      Elim (sumbool_of_bool (Dle (rho a) (Dplus (rho a0) d))). Trivial.
      Intro H0. Cut False. Intro. Elim H1.
      Apply H. Rewrite Dneg_plus. Rewrite <- Dplus_assoc. Apply Dplus_reg_r with d'':=d.
      Rewrite Dplus_assoc. Rewrite Dplus_neg_2. Rewrite Dplus_d_z. Apply Dle_lt_1. Assumption.
      Simpl. Intros. Split. Apply H. Intro. Apply H1. Left . Assumption.
      Apply H0. Intro. Apply H1. Right . Assumption.
      Simpl. Intros.
      Cut ~(CGeval rho (CG_of_CGS (CGSnot c))) \/ ~(CGeval rho (CG_of_CGS (CGSnot c0))). Intro.
      Elim H2. Intro. Left . Apply H. Assumption.
      Intro. Right . Apply H0. Assumption.
      Elim (CGeval_dec (CG_of_CGS (CGSnot c)) rho). Intro H2.
      Elim (CGeval_dec (CG_of_CGS (CGSnot c0)) rho). Intro H3. Elim (H1 (conj ? ? H2 H3)).
      Intro H3. Right . Assumption.
      Intro H2. Left . Assumption.
    Qed.

(*s Interpreting formula as simplified formula *)

Fixpoint CGFormSimplify [f:CGForm] : CGSForm :=
      Cases f of
          (CGleq x y d) => (CGSleq x y d)
        | (CGeq x y d) => (CGSeq x y d)
        | (CGand f0 f1) => (CGSand (CGFormSimplify f0) (CGFormSimplify f1))
        | (CGor f0 f1) => (CGSor (CGFormSimplify f0) (CGFormSimplify f1))
        | (CGimp f0 f1) => (CGSor (CGSnot (CGFormSimplify f0)) (CGFormSimplify f1))
        | (CGnot f0) => (CGSnot (CGFormSimplify f0))
      end.

Lemma  CGFormSimplify_correct : (f:CGForm) (rho:ad->D)
        (CGeval rho f) <-> (CGSeval rho (CGFormSimplify f)).
    Proof.
      Induction f. Intros. Split; Trivial.
      Intros. Unfold CGSeval. Simpl. Split. Intro H. Exact (CGSeq_complete a a0 d rho H).
      Exact (CGSeq_correct a a0 d rho).
      Simpl. Intros. Unfold CGSeval. Simpl. Elim (H rho). Intros. Elim (H0 rho). Intros.
      Split. Intro. Elim H5. Intros. Split. Apply H1. Assumption.
      Apply H3. Assumption.
      Intro. Elim H5. Intros. Split. Apply H2. Assumption.
      Apply H4. Assumption.
      Simpl. Intros. Unfold CGSeval. Elim (H rho). Intros. Elim (H0 rho). Intros. Simpl. Split.
      Intro. Elim H5. Intro. Left . Apply H1. Assumption.
      Intro. Right . Apply H3. Assumption.
      Intro. Elim H5. Intro. Left . Apply H2. Assumption.
      Intro. Right . Apply H4. Assumption.
      Simpl. Intros. Unfold CGSeval. Simpl. Elim (H rho). Intros. Elim (H0 rho). Intros. Split.
      Intro. Elim (CGeval_dec c rho). Intro H6. Right . Apply H3. Apply H5. Assumption.
      Intro H6. Left . Elim (CGeval_dec (CG_of_CGS (CGSnot (CGFormSimplify c))) rho). Trivial.
      Intro H7. Elim (H6 (H2 (CGSnot_complete ? ? H7))).
      Intro. Elim H5. Intros. Elim (CGeval_dec (CG_of_CGS (CGFormSimplify c)) rho). Intro H8.
      Elim (CGSnot_correct ? ? H8 H6).
      Intro H8. Elim (H8 (H1 H7)).
      Intros. Apply H4. Assumption. Simpl. Intros. Unfold CGSeval. Simpl. Elim (H rho). Intros.
      Split. Intro. Elim (CGeval_dec (CG_of_CGS (CGSnot (CGFormSimplify c))) rho). Trivial.
      Intro H3. Elim (H2 (H1 (CGSnot_complete ? ? H3))).
      Unfold not. Intros. Elim (CGSnot_correct ? ? (H0 H3) H2).
    Qed.

(*s Formula are solved by looking at their simplified form *)

Definition CG_solve := [f:CGForm] (CGS_solve false (CGFormSimplify f)).

Theorem CG_solve_correct : (f:CGForm) (CG_solve f)=true -> {rho:ad->D | (CGeval rho f)}.
    Proof.
      Intros. Elim (CGS_solve_correct ? ? H). Intros rho H0. Split with rho.
      Apply (proj2 ? ? (CGFormSimplify_correct f rho)). Assumption.
    Qed.

Theorem CG_solve_correct_anchored : (anchor:ad) (anchor_value:D)
      	(f:CGForm) (CG_solve f)=true ->
          {rho:ad->D | (CGeval rho f) /\ (rho anchor)=anchor_value}.
    Proof.
      Intros. Elim (CGS_solve_correct_anchored anchor anchor_value ? ? H). Intros rho H0.
      Split with rho. Elim H0. Intros. Split. Apply (proj2 ? ? (CGFormSimplify_correct f rho)).
      Assumption.
      Assumption.
    Qed.

Theorem CG_solve_complete : (f:CGForm) (rho:ad->D)
        (CGeval rho f) -> (CG_solve f)=true.
    Proof.
      Intros. Unfold CG_solve. Apply (CGS_solve_complete false) with rho:=rho.
      Apply (proj1 ? ? (CGFormSimplify_correct f rho)). Assumption.
    Qed.

(*s A formula is proved when its negation cannot be satisfied *)

Definition CG_prove := [f:CGForm] (negb (CG_solve (CGnot f))).

Theorem CG_prove_correct : (f:CGForm) (CG_prove f)=true -> (rho:ad->D) (CGeval rho f).
    Proof.
      Unfold CG_prove CG_solve. Simpl. Intros. Apply (proj2 ? ? (CGFormSimplify_correct f rho)).
      Apply CGSnot_complete. Unfold not. Intro. Rewrite (CGS_solve_complete false ? ? H0) in H.
      Discriminate H.
    Qed.

Theorem CG_prove_complete : (f:CGForm) ((rho:ad->D) (CGeval rho f)) -> (CG_prove f)=true.
    Proof.
      Unfold CG_prove CG_solve. Simpl. Intros.
      Elim (sumbool_of_bool (CGS_solve false (CGSnot (CGFormSimplify f)))). Intro H0.
      Elim (CGS_solve_correct false ? H0). Intros rho H1.
      Elim (CGSnot_correct ? ? (proj1 ? ? (CGFormSimplify_correct ? ?) (H rho)) H1).
      Intro H0. Rewrite H0. Reflexivity.
    Qed.

Theorem CG_prove_complete_anchored : (f:CGForm) (anchor:ad) (anchor_value:D)
        ((rho:ad->D) (rho anchor)=anchor_value -> (CGeval rho f)) -> (CG_prove f)=true.
    Proof.
      Unfold CG_prove CG_solve. Simpl. Intros.
      Elim (sumbool_of_bool (CGS_solve false (CGSnot (CGFormSimplify f)))). Intro H0.
      Elim (CGS_solve_correct_anchored anchor anchor_value false ? H0). Intros rho H1.
      Elim H1. Intros.
      Elim (CGSnot_correct ? ? (proj1 ? ? (CGFormSimplify_correct f rho) (H rho H3)) H2).
      Intro H0. Rewrite H0. Reflexivity.
    Qed.

  End CGWithOne.

End ConstraintGraphs.