Guarantees by Construction (Mechanization)

Jules Jacobs

   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
From iris.algebra Require Import gmap.
From iris.proofmode Require Export proofmode.
From iris.si_logic Require Export bi.
From dlfactris.algebra Require Export multiset.
From dlfactris.base_logic Require Import uforests.

Ltac sdec := repeat case_decide; simplify_eq/=; eauto; try done.

(* Definition uforest V := gset (V * V). *)
Notation cgraph V L := (gmap V (gmap V L)).

Section cgraph.
  Context `{Countable V} {L : ofe}.

  Definition out_edges (g : cgraph V L) (v : V) : gmap V L :=
    default  (g !! v).

  Definition ms_insert (v v' : V) (ev' : gmap V L) (ins : multiset L) : multiset L :=
    from_option  x, {[+ x +]})  (ev' !! v)  ins.

  Definition in_labels (g : cgraph V L) (v : V) : multiset L :=
    map_fold (ms_insert v)  g.

  Definition edge (g : cgraph V L) (ν1 ν2 : V) := is_Some (out_edges g ν1 !! ν2).

  Definition make_undirected (g : gset (V * V)) : gset (V * V) :=
    g  set_map prod_swap g.

  Definition dedges (g : cgraph V L) : gset (V * V) :=
    dom (gmap_uncurry g).
  Definition to_uforest (g : cgraph V L) : uforest V :=
    make_undirected $ dedges g.

  Definition no_short_loops (g : cgraph V L) :=
     ν1 ν2, ¬(edge g ν1 ν2  edge g ν2 ν1).

  Definition cgraph_wf (g : cgraph V L) :=
    no_short_loops g  is_uforest (to_uforest g).

  Definition uconn (g : cgraph V L) := rtsc (edge g).

  Section general.
    Lemma in_labels_insert g i x v :
      g !! i = None 
      in_labels (<[i:=x]> g) v  from_option  x, {[+ x +]})  (x !! v)  in_labels g v.
    Proof.
      intros Hi.
      unfold in_labels at 1.
      erewrite map_fold_insert with (R := ());
      eauto; first apply _; first solve_proper.
      intros. unfold ms_insert.
      destruct (z1 !! v) eqn:E;
      destruct (z2 !! v) eqn:F; simpl; eauto.
      rewrite assoc.
      rewrite (comm () {[+ o +]}).
      rewrite assoc. done.
    Qed.

    Lemma in_labels_delete g i v y :
      g !! i = Some y 
      from_option  x, {[+ x +]})  (y !! v)  in_labels (delete i g) v  in_labels g v.
    Proof.
      intro.
      pose proof (in_labels_insert (delete i g) i y v) as HH.
      rewrite insert_delete in HH; last done.
      rewrite HH; last by apply lookup_delete.
      done.
    Qed.

    Lemma in_labels_update g i x y v :
      g !! i = Some y 
      from_option  x, {[+ x +]})  (y !! v)  in_labels (<[i:=x]> g) v 
      from_option  x, {[+ x +]})  (x !! v)  in_labels g v.
    Proof.
      intro.
      assert (<[i:=x]> g = <[i:=x]> $ delete i g) as ->.
      { by rewrite insert_delete_insert. }
      rewrite in_labels_insert; last by apply lookup_delete.
      rewrite comm. rewrite -assoc.
      rewrite (comm () (in_labels _ _)).
      rewrite in_labels_delete; eauto.
    Qed.

    Lemma out_edges_in_labels_L g ν1 ν2 l :
      out_edges g ν1 !! ν2 = Some l 
       x, in_labels g ν2  {[+ l +]}  x.
    Proof.
      revert ν1 ν2 l.
      induction g using map_ind; intros.
      - rewrite lookup_empty in H0. inversion H0.
      - unfold out_edges in H1.
        rewrite lookup_insert_spec in H1.
        case_decide; simplify_eq.
        + exists (in_labels m ν2).
          rewrite in_labels_insert; eauto.
          rewrite H1. done.
        + destruct (m !! ν1) eqn:E. 2: { rewrite lookup_empty in H1. inversion H1. }
          destruct (IHg ν1 ν2 l).
          { unfold out_edges. rewrite E. done. }
          setoid_rewrite in_labels_insert; eauto.
          setoid_rewrite H3.
          exists (from_option  x, {[+ x +]})  (x !! ν2)  x0).
          rewrite !assoc.
          rewrite (comm () _ {[+ l +]}).
          done.
    Qed.

    Lemma no_in_labels_no_out_edge g ν1 ν2 :
      in_labels g ν2 =  
      out_edges g ν1 !! ν2 = None.
    Proof.
      intros Hin%multiset_empty_equiv_eq.
      destruct (out_edges g ν1 !! ν2) eqn:E; eauto.
      eapply out_edges_in_labels_L in E as [X Hin'].
      rewrite Hin' multiset_empty_equiv_eq multiset_disj_union_eq_empty in Hin.
      by destruct Hin.
    Qed.

    Lemma out_edges_insert (g : cgraph V L) (ν1 ν2 : V) e :
      out_edges (<[ ν1 := e ]> g) ν2 =
        if decide (ν1 = ν2) then e
        else out_edges g ν2.
    Proof.
      rewrite /out_edges. rewrite lookup_insert_spec.
      repeat case_decide; simplify_eq; done.
    Qed.

    Lemma in_labels_empty v :
      in_labels  v = .
    Proof.
      rewrite /in_labels map_fold_empty //.
    Qed.

    Lemma no_edges_no_uconn g v v' :
      out_edges g v =  
      in_labels g v =  
      uconn g v' v  v = v'.
    Proof.
      intros Hout Hin Hconn.
      eapply not_rtsc; last done.
      intros y. unfold edge. split; intros [].
      - rewrite Hout in H0. rewrite lookup_empty in H0. simplify_eq.
      - eapply no_in_labels_no_out_edge in Hin. erewrite H0 in Hin. simplify_eq.
    Qed.

    Lemma some_edge_L (g : cgraph V L) (ν1 ν2 : V) (l : L) :
      out_edges g ν1 !! ν2 = Some l  edge g ν1 ν2.
    Proof.
      unfold edge. intros ->. eauto.
    Qed.

    Lemma some_edge (g : cgraph V L) (ν1 ν2 : V) (l : L) :
      out_edges g ν1 !! ν2  Some l  edge g ν1 ν2.
    Proof.
      intro. inversion H0.
      unfold edge. rewrite -H1. eauto.
    Qed.

    Lemma not_uconn_out_disjoint g ν1 ν2 :
      ¬ uconn g ν1 ν2  out_edges g ν1 ## out_edges g ν2.
    Proof.
      intros HH v.
      destruct (out_edges g ν1 !! v) eqn:E;
      destruct (out_edges g ν2 !! v) eqn:F; simpl; eauto.
      assert (edge g ν1 v); eauto using some_edge_L.
      assert (edge g ν2 v); eauto using some_edge_L.
      apply HH.
      eapply rtc_transitive; eapply rtc_once; [left|right]; eauto.
    Qed.
  End general.


  Section acyclicity.
    Lemma make_undirected_sc (R : relation V) (e : gset (V * V)) :
      (∀ x y, (x,y)  e  R x y) 
       x y, (x,y)  make_undirected e  sc R x y.
    Proof.
      intros. unfold make_undirected.
      rewrite elem_of_union.
      rewrite elem_of_set_map_prod_swap.
      split; intros []; [left|right|..]; naive_solver.
    Qed.

    Lemma gmap_uncurry_out_edges g x y :
      gmap_uncurry g !! (x, y) = out_edges g x !! y.
    Proof.
      rewrite lookup_gmap_uncurry.
      unfold out_edges.
      destruct (g !! x); simpl; eauto.
    Qed.

    Lemma elem_of_dedges x y g :
      (x, y)  dedges g  edge g x y.
    Proof.
      unfold dedges.
      rewrite elem_of_dom.
      unfold edge.
      rewrite gmap_uncurry_out_edges. done.
    Qed.

    Lemma elem_of_to_uforest g x y :
      (x,y)  to_uforest g  sc (edge g) x y.
    Proof.
      unfold to_uforest.
      eapply make_undirected_sc. intros.
      eapply elem_of_dedges.
    Qed.

    Lemma out_edges_empty v :
      out_edges  v = .
    Proof.
      unfold out_edges. rewrite lookup_empty //.
    Qed.

    Lemma edge_empty ν1 ν2 :
      edge  ν1 ν2  False.
    Proof.
      split; intros [].
      rewrite out_edges_empty lookup_empty in H0. simplify_eq.
    Qed.

    Lemma to_uforest_empty :
      to_uforest  = .
    Proof.
      eapply set_eq. intros [x y].
      rewrite elem_of_to_uforest.
      unfold edge, sc.
      rewrite !out_edges_empty !lookup_empty //.
    Qed.

  End acyclicity.

  Section empty_cgraph.
    Lemma empty_wf :
      cgraph_wf .
    Proof.
      unfold cgraph_wf.
      split.
      - intros ??. rewrite !edge_empty. naive_solver.
      - rewrite to_uforest_empty. eapply uforest_empty.
    Qed.
  End empty_cgraph.

  Section insert_edge.
    (* This function is only supposed to be called if there is not already an edge
       between ν1 and ν2. In fact, it's only supposed to be called if ν1 and ν2
       are complete disconnected. *)
    Definition insert_edge (g : cgraph V L) (ν1 ν2 : V) (l : L) :=
      <[ ν1 := <[ ν2 := l ]> $ out_edges g ν1 ]> g.

    Lemma out_edges_insert_edge (g : cgraph V L) (ν1 ν2 v3 : V) (l : L) :
      out_edges (insert_edge g ν1 ν2 l) v3 =
        if decide (ν1 = v3) then <[ ν2 := l ]> $ out_edges g v3
        else out_edges g v3.
    Proof.
      unfold insert_edge. rewrite out_edges_insert.
      repeat case_decide; simplify_eq; done.
    Qed.

    Lemma in_labels_insert_edge (g : cgraph V L) (ν1 ν2 v3 : V) (l : L) :
      ¬ edge g ν1 ν2 
      in_labels (insert_edge g ν1 ν2 l) v3 
        if decide (ν2 = v3) then {[+ l +]}  in_labels g v3
        else in_labels g v3.
    Proof.
      intros Hnotedge.
      unfold insert_edge.
      destruct (g !! ν1) eqn:E.
      - assert (g !! ν1 = Some g0) as HH; eauto.
        pose proof (in_labels_update g ν1 (<[ν2:=l]> (out_edges g ν1)) g0 v3 HH) as H0.
        rewrite lookup_insert_spec in H0.
        destruct (g0 !! v3) eqn:F; simpl in *.
        + case_decide; simplify_eq/=.
          * exfalso. apply Hnotedge.
            unfold edge. unfold out_edges.
            rewrite E. rewrite F. eauto.
          * unfold out_edges in H0 at 2.
            rewrite E in H0.
            rewrite F in H0. simpl in *. by apply (inj _) in H0.
        + rewrite ->left_id in H0. 2: { intro. simpl. rewrite left_id. done. }
          rewrite H0. case_decide; simpl; eauto. unfold out_edges.
          rewrite HH. rewrite F. simpl. rewrite left_id. done.
      - rewrite in_labels_insert; eauto.
        rewrite lookup_insert_spec.
        case_decide; simplify_eq; try done.
        unfold out_edges.
        rewrite E. rewrite lookup_empty.
        simpl. rewrite left_id. done.
    Qed.

    Lemma edge_insert_edge g ν1 ν2 x y l :
      edge (insert_edge g ν1 ν2 l) x y  edge g x y  (x = ν1  y = ν2).
    Proof.
      unfold edge.
      rewrite out_edges_insert_edge.
      case_decide; subst.
      - rewrite lookup_insert_spec. case_decide; subst.
        + naive_solver.
        + naive_solver.
      - naive_solver.
    Qed.

    Lemma sc_edge_insert_edge g ν1 ν2 x y l :
      sc (edge (insert_edge g ν1 ν2 l)) x y 
        sc (edge g) x y  (x = ν1  y = ν2)  (x = ν2  y = ν1).
    Proof.
      split.
      - intros []; rewrite ->edge_insert_edge in H0;
        destruct H0; try naive_solver.
        + left. left. done.
        + left. right. done.
      - intros [].
        + destruct H0;[left|right]; rewrite edge_insert_edge; eauto.
        + destruct H0;[left|right]; rewrite edge_insert_edge; eauto.
          naive_solver.
    Qed.

    Lemma to_uforest_insert_edge g ν1 ν2 l :
      to_uforest (insert_edge g ν1 ν2 l) = to_uforest g  uedge ν1 ν2.
    Proof.
      eapply set_eq. intros [x y].
      rewrite elem_of_union.
      rewrite !elem_of_to_uforest.
      rewrite !sc_edge_insert_edge.
      unfold uedge. set_solver.
    Qed.

    Lemma uconn_connected0 g ν1 ν2 :
      cgraph_wf g 
      uconn g ν1 ν2  connected0 (to_uforest g) ν1 ν2.
    Proof.
      unfold uconn. intros.
      rewrite connected0_elem_of. 2: { by destruct H0. }
      assert (∀ x y, sc (edge g) x y  (x,y)  to_uforest g).
      { intros. rewrite elem_of_to_uforest. done. }
      unfold rtsc.
      erewrite rtc_iff; eauto.
      intros.
      split.
      - intros. left. rewrite elem_of_to_uforest. done.
      - intros []; rewrite ->elem_of_to_uforest in H2; eauto.
        destruct H2; [right|left]; done.
    Qed.

    Lemma insert_edge_wf g ν1 ν2 l :
      ¬ uconn g ν1 ν2 
      cgraph_wf g 
      cgraph_wf (insert_edge g ν1 ν2 l).
    Proof.
      intros H0 [].
      split.
      - intros x y. rewrite !edge_insert_edge.
        intros [].
        destruct H3; destruct H4.
        + eapply H1; eauto.
        + destruct H4; subst.
          eapply H0. eapply rtc_once.
          right. done.
        + destruct H3; subst.
          eapply H0. eapply rtc_once.
          right. done.
        + destruct H3. destruct H4. subst.
          eapply H0. constructor.
      - rewrite to_uforest_insert_edge.
        eapply uforest_connect; eauto.
        rewrite -uconn_connected0; done.
    Qed.
  End insert_edge.


  Section delete_edge.
    Definition delete_edge (g : cgraph V L) (ν1 ν2 : V) :=
      <[ ν1 := delete ν2 (out_edges g ν1) ]> g.

    Lemma out_edges_delete_edge (g : cgraph V L) (ν1 ν2 v3 : V) :
      out_edges (delete_edge g ν1 ν2) v3 =
        if decide (ν1 = v3) then delete ν2 (out_edges g v3)
        else out_edges g v3.
    Proof.
      unfold delete_edge. rewrite out_edges_insert.
      repeat case_decide; simplify_eq; done.
    Qed.

    Lemma in_labels_delete_edge_eq (g : cgraph V L) (ν1 ν2 : V) (l : L) (x : multiset L) :
      out_edges g ν1 !! ν2 = Some l 
      in_labels g ν2  {[+ l +]}  x 
      in_labels (delete_edge g ν1 ν2) ν2  x.
    Proof.
      intros H1 H2.
      unfold delete_edge.
      destruct (g !! ν1) eqn:E; last first.
      { unfold out_edges in H1. rewrite E lookup_empty in H1. simplify_eq. }
      pose proof (in_labels_update g ν1 (delete ν2 (out_edges g ν1)) g0 ν2 E) as H0.
      destruct (g0 !! ν2) eqn:F; simpl in *.
      - rewrite lookup_delete in H0.
        simpl in *.
        rewrite ->H2 in H0.
        unfold out_edges in H1.
        rewrite E in H1. rewrite F in H1. simplify_eq.
        revert H0. rewrite left_id. by intros H0%(inj _).
      - unfold out_edges in H1. rewrite E F in H1. simplify_eq.
    Qed.

    Lemma in_labels_delete_edge_neq (g : cgraph V L) (ν1 ν2 v3 : V) :
      ν2  v3 
      in_labels (delete_edge g ν1 ν2) v3  in_labels g v3.
    Proof.
      intros Hneq.
      unfold delete_edge.
      destruct (g !! ν1) eqn:E.
      - pose proof (in_labels_update g ν1 (delete ν2 (out_edges g ν1)) g0 v3 E) as H0.
        destruct (g0 !! v3) eqn:F.
        + simpl in *.
          rewrite lookup_delete_spec in H0.
          unfold out_edges in H0 at 2.
          rewrite E in H0.
          rewrite F in H0.
          case_decide; simplify_eq.
          simpl in *.
          by apply (inj _) in H0.
        + simpl in *. rewrite ->left_id in H0.
          2: { intro. simpl. rewrite left_id. done. }
          rewrite H0.
          rewrite lookup_delete_spec.
          unfold out_edges. rewrite E.
          rewrite F. case_decide; simplify_eq; simpl.
          rewrite left_id. done.
      - rewrite in_labels_insert; eauto.
        rewrite lookup_delete_spec.
        unfold out_edges.
        rewrite E. rewrite lookup_empty.
        case_decide; simpl; rewrite left_id //.
    Qed.

    Lemma edge_delete_edge g ν1 ν2 w1 w2 :
      ν1  w1  ν2  w2 
      edge g w1 w2 
      edge (delete_edge g ν1 ν2) w1 w2.
    Proof.
      intros.
      unfold edge.
      rewrite out_edges_delete_edge.
      case_decide; simplify_eq; eauto.
      rewrite lookup_delete_spec.
      case_decide; simplify_eq; eauto.
      naive_solver.
    Qed.

    Lemma edge_delete_edge' g ν1 ν2 w1 w2 :
      edge g w1 w2 
      edge (delete_edge g ν1 ν2) w1 w2  (ν1 = w1  ν2 = w2).
    Proof.
      intros.
      pose proof (edge_delete_edge g ν1 ν2 w1 w2).
      destruct (decide (ν1 = w1));
      destruct (decide (ν2 = w2));
      naive_solver.
    Qed.

    Lemma edge_delete_edge'' g ν1 ν2 x y :
      edge (delete_edge g ν1 ν2) x y  edge g x y  ¬ (x = ν1  y = ν2).
    Proof.
      split.
      - intros.
        move: H0.
        unfold edge. rewrite !out_edges_delete_edge.
        repeat case_decide; subst.
        + rewrite lookup_delete_spec.
          case_decide; subst; first by intros []. naive_solver.
        +  naive_solver.
      - intros []. eapply edge_delete_edge; eauto.
        destruct (decide (x = ν1)); destruct (decide (y = ν2)); naive_solver.
    Qed.

    Lemma elem_of_uedge (x y a b : V) :
      (x, y)  uedge a b  (x = a  y = b)  (x = b  y = a).
    Proof.
      unfold uedge.
      set_solver.
    Qed.

    Lemma edge_dec g x y : Decision (edge g x y).
    Proof.
      unfold edge.
      destruct (out_edges g x !! y).
      - left. eauto.
      - right. eauto.
    Qed.

    Lemma to_uforest_delete_edge g ν1 ν2 :
      no_short_loops g 
      edge g ν1 ν2 
      to_uforest (delete_edge g ν1 ν2) = to_uforest g  uedge ν1 ν2.
    Proof.
      intros.
      eapply set_eq. intros [x y].
      rewrite elem_of_difference.
      rewrite !elem_of_to_uforest.
      rewrite !sc_or.
      specialize (H0 ν1 ν2).
      rewrite !edge_delete_edge''.
      rewrite elem_of_uedge.
      naive_solver.
    Qed.

    Definition cgraph_equiv g1 g2 :=
       v, out_edges g1 v = out_edges g2 v.

    Lemma cgraph_equiv_edge g1 g2 ν1 ν2 :
      cgraph_equiv g1 g2 
      edge g1 ν1 ν2 
      edge g2 ν1 ν2.
    Proof.
      unfold edge. intros ->. done.
    Qed.

    Lemma uconn_equiv g1 g2 ν1 ν2 :
      cgraph_equiv g1 g2 
      uconn g1 ν1 ν2  uconn g2 ν1 ν2.
    Proof.
      intros He Hc.
      induction Hc; try reflexivity.
      eapply rtc_transitive; eauto.
      eapply rtc_once.
      destruct H0;[left|right]; eauto using cgraph_equiv_edge.
    Qed.

    Lemma delete_edge_not_edge g ν1 ν2 :
      ¬ edge g ν1 ν2 
      cgraph_equiv (delete_edge g ν1 ν2) g.
    Proof.
      intros ??.
      rewrite out_edges_delete_edge.
      case_decide; subst; eauto.
      rewrite delete_notin; eauto.
      destruct (out_edges g v !! ν2) eqn:E; eauto.
      exfalso. eapply H0. unfold edge. rewrite E. eauto.
    Qed.

    Lemma cgraph_equiv_sym g1 g2 :
      cgraph_equiv g1 g2  cgraph_equiv g2 g1.
    Proof.
      intros ??.
      symmetry. eapply H0.
    Qed.

    Lemma to_uforest_proper : Proper (cgraph_equiv ==> (=)) to_uforest.
    Proof.
      solve_proper_prepare.
      eapply set_eq.
      intros [a b].
      rewrite !elem_of_to_uforest.
      rewrite !sc_or.
      split; intros []; eauto using cgraph_equiv_edge,cgraph_equiv_sym.
    Qed.

    Lemma delete_edge_wf g ν1 ν2 :
      cgraph_wf g 
      cgraph_wf (delete_edge g ν1 ν2).
    Proof.
      intros [].
      unfold cgraph_wf.
      split.
      - unfold no_short_loops. intros x y [].
        apply edge_delete_edge'' in H2 as [].
        apply edge_delete_edge'' in H3 as [].
        unfold no_short_loops in *.
        eapply H0. eauto.
      - destruct (decide (edge g ν1 ν2)).
        + rewrite to_uforest_delete_edge; eauto.
          eapply uforest_delete; done.
        + assert (to_uforest (delete_edge g ν1 ν2) = to_uforest g).
          {
            eapply to_uforest_proper.
            eapply delete_edge_not_edge. done.
          }
          rewrite H2. done.
    Qed.

    Lemma delete_edge_uconn g ν1 ν2 :
      cgraph_wf g 
      edge g ν1 ν2 
      ¬ uconn (delete_edge g ν1 ν2) ν1 ν2.
    Proof.
      intros [] ?.
      rewrite uconn_connected0; last by eapply delete_edge_wf.
      rewrite to_uforest_delete_edge; eauto.
      eapply uforest_disconnect; eauto.
      rewrite elem_of_to_uforest; eauto.
      by left.
    Qed.

    Lemma no_self_edge g ν1 ν2 :
      cgraph_wf g 
      edge g ν1 ν2  ν1  ν2.
    Proof.
      intros H1 H2 ->.
      eapply delete_edge_uconn; eauto. reflexivity.
    Qed.

    Lemma no_self_edge' g ν1 ν2 :
      cgraph_wf g 
      sc (edge g) ν1 ν2  ν1  ν2.
    Proof.
      intros H1 [] ->; eapply no_self_edge; eauto.
    Qed.

    Lemma no_self_edge'' g v :
      cgraph_wf g 
      out_edges g v !! v = None.
    Proof.
      intros.
      destruct (_!!_) eqn:E; eauto.
      exfalso.
      eapply no_self_edge; eauto using some_edge_L.
    Qed.

    Lemma no_triangle g ν1 ν2 v3 :
      cgraph_wf g 
      sc (edge g) ν1 ν2 
      sc (edge g) ν2 v3 
      sc (edge g) v3 ν1 
      False.
    Proof.
      intros Hwf H1 H2 H3.
      assert (ν1  ν2); eauto using no_self_edge'.
      assert (ν2  v3); eauto using no_self_edge'.
      assert (v3  ν1); eauto using no_self_edge'.
      destruct H1,H2,H3; eapply delete_edge_uconn; eauto;
      eapply rtc_transitive; eapply rtc_once;
      try (solve [left; eapply edge_delete_edge; eauto] ||
           solve [right; eapply edge_delete_edge; eauto]).
      - left. eapply edge_delete_edge; eauto.
      - left. eapply edge_delete_edge.
        + right. intro. eapply H4. symmetry. done.
        + eauto.
      - right. eapply edge_delete_edge; eauto.
    Qed.

    Lemma edge_out_disjoint g ν1 ν2 :
      cgraph_wf g  edge g ν1 ν2  out_edges g ν1 ## out_edges g ν2.
    Proof.
      intros Hwf Hν12 v.
      destruct (out_edges g ν1 !! v) eqn:E;
      destruct (out_edges g ν2 !! v) eqn:F; simpl; eauto.
      eapply no_triangle; eauto.
      - left. eauto.
      - left. unfold edge. erewrite F. eauto.
      - right. unfold edge. rewrite E. eauto.
    Qed.
  End delete_edge.


  Section update_edge.
    Definition update_edge g ν1 ν2 l' :=
      insert_edge (delete_edge g ν1 ν2) ν1 ν2 l'.

    Lemma update_edge_out_edges g ν1 ν2 l' v :
      out_edges (update_edge g ν1 ν2 l') v =
        if decide (v = ν1) then <[ ν2 := l' ]> (out_edges g v)
        else out_edges g v.
    Proof.
      unfold update_edge.
      rewrite out_edges_insert_edge.
      rewrite out_edges_delete_edge.
      repeat case_decide; simplify_eq; eauto.
      apply map_eq. intros v.
      rewrite insert_delete_insert //.
    Qed.

    Lemma update_in_labels_neq g v ν1 ν2 l' :
      v  ν2 
      in_labels (update_edge g ν1 ν2 l') v  in_labels g v.
    Proof.
      intros H1.
      rewrite /update_edge in_labels_insert_edge.
      - case_decide; simplify_eq. rewrite in_labels_delete_edge_neq; eauto.
      - unfold edge. rewrite out_edges_delete_edge.
        case_decide; simplify_eq. rewrite lookup_delete. done.
    Qed.

    Lemma update_edge_wf g ν1 ν2 l' :
      cgraph_wf g 
      edge g ν1 ν2 
      cgraph_wf (update_edge g ν1 ν2 l').
    Proof.
      intros H1 H2.
      unfold update_edge.
      apply insert_edge_wf.
      - apply delete_edge_uconn; eauto.
      - eapply delete_edge_wf; eauto.
    Qed.
  End update_edge.

  Section move_edge.
    (* Move an edge ν1 --[l]--> v3 to be ν2 --[l]--> v *)
    (* This is only allowed if there is also an edge between ν1 and ν2. *)
    Definition move_edge g ν1 ν2 v3 :=
      match out_edges g ν1 !! v3 with
      | Some l => insert_edge (delete_edge g ν1 v3) ν2 v3 l
      | None => g
      end.

    Lemma move_edge_out_edges g ν1 ν2 v3 v l :
      out_edges g ν1 !! v3 = Some l 
      out_edges (move_edge g ν1 ν2 v3) v =
        if decide (v = ν2) then <[ v3 := l ]> $ out_edges g v
        else if decide (v = ν1) then delete v3 $ out_edges g v
        else out_edges g v.
    Proof.
      intros H1.
      unfold move_edge. rewrite H1.
      rewrite out_edges_insert_edge.
      rewrite out_edges_delete_edge.
      repeat case_decide; simplify_eq; eauto; apply map_eq; intro;
      rewrite ?lookup_union ?lookup_insert_spec ?lookup_delete_spec ?lookup_empty;
      repeat case_decide; simplify_eq; eauto.
    Qed.

    Lemma move_edge_in_labels g ν1 ν2 v3 v :
      cgraph_wf g 
      sc (edge g) ν1 ν2 
      in_labels (move_edge g ν1 ν2 v3) v  in_labels g v.
    Proof.
      intros Hwf Hν12.
      unfold move_edge.
      destruct (_!!_) eqn:E; eauto.
      rewrite in_labels_insert_edge.
      - case_decide; simplify_eq.
        + destruct (out_edges_in_labels_L _ _ _ _ E).
          rewrite in_labels_delete_edge_eq; eauto.
          by symmetry.
        + rewrite in_labels_delete_edge_neq; eauto.
      - unfold edge.
        assert (ν1  ν2); eauto using no_self_edge'.
        rewrite out_edges_delete_edge.
        case_decide; simplify_eq.
        intro HH.
        eapply no_triangle; eauto.
        + left; eauto.
        + right. unfold edge. erewrite E. eauto.
    Qed.

    Lemma move_edge_in_labels' g ν1 ν2 v3 v :
      cgraph_wf g 
      ¬ uconn g ν1 ν2 
      in_labels (move_edge g ν1 ν2 v3) v  in_labels g v.
    Proof.
      intros Hwf Hν12.
      unfold move_edge.
      destruct (_!!_) eqn:E; eauto.
      rewrite in_labels_insert_edge.
      - case_decide; simplify_eq.
        + destruct (out_edges_in_labels_L _ _ _ _ E).
          rewrite in_labels_delete_edge_eq; eauto.
          by symmetry.
        + rewrite in_labels_delete_edge_neq; eauto.
      - unfold edge.
        assert (ν1  ν2). { intros ->. apply Hν12. reflexivity. }
        rewrite out_edges_delete_edge.
        case_decide; simplify_eq.
        intros [].
        assert (edge g ν1 v3); eauto using some_edge_L.
        assert (edge g ν2 v3); eauto using some_edge_L.
        apply Hν12.
        eapply rtc_transitive; eapply rtc_once;[left|right]; eauto.
    Qed.

    Lemma move_edge_wf g ν1 ν2 v3 :
      ν2  v3 
      cgraph_wf g 
      sc (edge g) ν1 ν2 
      cgraph_wf (move_edge g ν1 ν2 v3).
    Proof.
      intros Hneq Hwf Hν12.
      unfold move_edge.
      destruct (_!!_) eqn:E; eauto.
      apply insert_edge_wf; eauto using delete_edge_wf.
      intro Hconn.
      eapply (delete_edge_uconn g ν1 v3); eauto.
      - unfold edge. rewrite E. eauto.
      - unfold uconn in *.
        assert (sc (edge (delete_edge g ν1 v3)) ν1 ν2). {
          destruct Hν12.
          - left. unfold edge. rewrite out_edges_delete_edge.
            case_decide; simplify_eq.
            rewrite lookup_delete_ne; eauto.
          - right. unfold edge. rewrite out_edges_delete_edge.
            case_decide; simplify_eq; eauto.
            exfalso. eapply no_self_edge; eauto.
        }
        eapply transitivity; last done.
        econstructor; first done.
        econstructor.
    Qed.

    Lemma edge_delete_edge''' g ν1 ν2 w1 w2 :
      edge (delete_edge g w1 w2) ν1 ν2  edge g ν1 ν2.
    Proof.
      rewrite /delete_edge /edge /out_edges.
      destruct (g !! w1) eqn:F; simpl; rewrite lookup_insert_spec; repeat case_decide;
      rewrite ?lookup_delete_spec; repeat case_decide; simplify_eq;
      try destruct (g !! ν1) eqn:E; simpl; rewrite ?lookup_empty; eauto; try intros [];
      simplify_eq. rewrite H0. eauto.
    Qed.

    Lemma delete_edge_preserves_not_uconn g ν1 ν2 w1 w2 :
      uconn (delete_edge g w1 w2) ν1 ν2  uconn g ν1 ν2.
    Proof.
      intros HH. induction HH; try reflexivity.
      eapply rtc_transitive; eauto. eapply rtc_once.
      destruct H0; [left|right]; eauto using edge_delete_edge'''.
    Qed.

    Lemma move_edge_wf' g ν1 ν2 v3 :
      ν2  v3 
      cgraph_wf g 
      ¬ uconn g ν1 ν2 
      cgraph_wf (move_edge g ν1 ν2 v3).
    Proof.
      intros Hneq Hwf Hν12.
      unfold move_edge.
      destruct (_!!_) eqn:E; eauto.
      apply insert_edge_wf; eauto using delete_edge_wf.
      assert  uconn g v3 ν2).
      { intro. apply Hν12.
        eapply rtc_transitive; eauto.
        eapply rtc_once. left. eauto using some_edge_L. }
      intro. apply delete_edge_preserves_not_uconn in H1.
      apply H0. symmetry. done.
    Qed.

    Lemma move_edge_uconn g ν1 ν2 v3 :
      cgraph_wf g 
      ¬ uconn g ν1 ν2 
      ¬ uconn (move_edge g ν1 ν2 v3) ν1 ν2.
    Proof.
      intros Hwf H1.
      destruct (out_edges g ν1 !! v3) eqn:E; last first.
      { unfold move_edge. rewrite E. done. }
      assert  uconn (delete_edge (move_edge (insert_edge g ν1 ν2 o) ν1 ν2 v3) ν1 ν2) ν1 ν2).
      {
        eapply delete_edge_uconn.
        - eapply move_edge_wf.
          + intros ->. eapply H1. eapply rtc_once. left. eauto using some_edge_L.
          + eapply insert_edge_wf; eauto.
          + left. unfold edge.
            rewrite out_edges_insert_edge.
            case_decide; simplify_eq. rewrite lookup_insert. eauto.
        - unfold edge.
          erewrite move_edge_out_edges; last first.
          + rewrite out_edges_insert_edge.
            case_decide; simplify_eq.
            rewrite lookup_insert_spec.
            case_decide; simplify_eq; eauto.
          + rewrite !out_edges_insert_edge.
            repeat case_decide; simplify_eq.
            * rewrite !lookup_insert_spec.
              case_decide; simplify_eq. eauto.
            * rewrite lookup_delete_spec lookup_insert_spec.
              case_decide; simplify_eq.
              -- exfalso. apply H1.
                 eapply rtc_once. left. eauto using some_edge_L.
              -- case_decide; simplify_eq. eauto.
      }
      intro. apply H0.
      eapply uconn_equiv; eauto.
      intro.
      rewrite out_edges_delete_edge.
      erewrite move_edge_out_edges; last done.
      erewrite move_edge_out_edges; last first.
      { rewrite out_edges_insert_edge. case_decide; eauto.
        rewrite lookup_insert_spec. case_decide; simplify_eq; eauto. }
      rewrite !out_edges_insert_edge.
      repeat case_decide; simplify_eq; eauto.
      - assert (ν2  v3). { intros ->. eapply no_self_edge; eauto using some_edge_L. }
        rewrite delete_insert_ne; eauto.
        rewrite delete_insert; eauto.
        destruct (out_edges g ν2 !! ν2) eqn:F; eauto.
        exfalso. eapply no_self_edge; eauto using some_edge_L.
      - rewrite delete_commute.
        rewrite delete_insert; eauto.
        destruct (out_edges g v !! ν2) eqn:F; eauto.
        exfalso. apply H1. apply rtc_once.
        left. eapply some_edge_L; eauto.
    Qed.
  End move_edge.

  Section move_edges.
    Lemma move_edges g ν1 ν2 e1 e2 :
      cgraph_wf g 
      ¬ uconn g ν1 ν2 
      e1 ## e2 
      out_edges g ν1 = e1  e2 
       g', cgraph_wf g' 
        ¬ uconn g' ν1 ν2 
        (∀ v, out_edges g' v =
          if decide (v = ν1) then e1
          else if decide (v = ν2) then out_edges g v  e2
          else out_edges g v) 
        (∀ v, in_labels g' v  in_labels g v).
    Proof.
      revert g e1.
      induction e2 using map_ind; intros g e1;
      intros Hwf Hnuconn Hdisj Hout.
      - exists g. rewrite right_id_L in Hout.
        split_and!; eauto. intro.
        repeat case_decide; simplify_eq; eauto.
        rewrite right_id_L //.
      - specialize (IHe2 (move_edge g ν1 ν2 i) e1).
        destruct IHe2.
        + apply move_edge_wf'; eauto. intros ->. apply Hnuconn.
          apply rtc_once.
          left.
          unfold edge.
          rewrite Hout.
          rewrite lookup_union lookup_insert.
          destruct (e1 !! i); eauto.
        + apply move_edge_uconn; eauto.
        + solve_map_disjoint.
        + destruct (out_edges g ν1 !! i) eqn:E.
          2: { rewrite Hout in E. rewrite lookup_union lookup_insert in E.
            destruct (e1 !! i); simpl in *; simplify_eq.  }
          erewrite move_edge_out_edges; eauto.
          repeat case_decide; simplify_eq.
          -- exfalso. apply Hnuconn. reflexivity.
          -- rewrite Hout. rewrite delete_union.
             rewrite delete_insert; eauto.
             rewrite delete_notin; eauto.
             solve_map_disjoint.
        + destruct H1 as (H1 & H2 & H3 & H4).
          exists x0. split_and!; eauto.
          -- intros v. rewrite H3.
             assert (out_edges g ν1 !! i = Some x).
             { rewrite Hout. rewrite lookup_union lookup_insert.
                destruct (e1 !! i) eqn:E; simpl; eauto.
                specialize (Hdisj i). rewrite E in Hdisj. rewrite lookup_insert in Hdisj.
                simpl in *. done. }
             repeat case_decide; simplify_eq; eauto.
             ++ erewrite move_edge_out_edges; eauto.
                case_decide; simplify_eq.
                apply map_eq; intro.
                rewrite !lookup_union !lookup_insert_spec;
                repeat case_decide; simplify_eq; eauto.
                rewrite H0. simpl.
                destruct (out_edges g ν2 !! i0) eqn:E; eauto; simpl.
                exfalso.
                apply Hnuconn.
                eapply rtc_transitive; eapply rtc_once; [left|right];
                eauto using some_edge_L.
             ++ erewrite move_edge_out_edges; eauto.
                repeat case_decide; simplify_eq. done.
          -- intros v. rewrite H4. rewrite move_edge_in_labels'; eauto.
    Qed.

    Lemma move_edges' g ν1 ν2 e1 e2 :
      cgraph_wf g 
      ¬ uconn g ν1 ν2 
      e1 ## e2 
      out_edges g ν1 = e1  e2 
       g', cgraph_wf g' 
        ¬ uconn g' ν1 ν2 
        out_edges g' ν1 = e1 
        out_edges g' ν2 = out_edges g ν2  e2 
        (∀ v, v  ν1  v  ν2  out_edges g' v = out_edges g v) 
        (∀ v, in_labels g' v  in_labels g v).
    Proof.
      intros.
      edestruct move_edges as (?&?&?&?&?); eauto.
      eexists. split_and!; eauto.
      - specialize (H6 ν1). case_decide; simplify_eq; done.
      - specialize (H6 ν2). repeat case_decide; simplify_eq; done.
      - intros. specialize (H6 v); repeat case_decide; simplify_eq; eauto.
    Qed.
  End move_edges.

  Section exchange.
    Lemma exchange g ν1 ν2 e1 e2 :
      cgraph_wf g 
      ¬ uconn g ν1 ν2 
      e1 ## e2 
      out_edges g ν1  out_edges g ν2 = e1  e2 
       g', cgraph_wf g' 
        ¬ uconn g' ν1 ν2 
        out_edges g' ν1 = e1 
        out_edges g' ν2 = e2 
        (∀ v, v  ν1  v  ν2  out_edges g' v = out_edges g v) 
        (∀ v, in_labels g' v  in_labels g v).
    Proof.
      intros Hwf Hnuconn Hdisj Hsplit.
      assert (out_edges g ν1 ## out_edges g ν2) as Hdisj'.
      { apply not_uconn_out_disjoint. done. }
      destruct (map_cross_split (e1  e2) _ _ _ Hdisj' Hdisj)
        as (e11 & e12 & e21 & e22 & Hdisj1 & Hdisj2 & Hdisj3 & Hdisj4 &
            ? & ? & HH1 & HH2); eauto; subst.

      destruct (move_edges' g ν1 ν2 e11 e12)
        as (g' & Hwf' & Hnuconn' & Hν1 & Hν2 & Hv & Hin); eauto.
      rewrite <-H1 in Hν2.
      rewrite <-assoc_L in Hν2; last apply _.
      rewrite map_union_comm in Hν2; last solve_map_disjoint.

      destruct (move_edges' g' ν2 ν1 (e22  e12) e21)
        as (g'' & Hwf'' & Hnuconn'' & Hν1' & Hν2' & Hv' & Hin'); eauto.
      { intro. apply Hnuconn'. symmetry. done. }
      { solve_map_disjoint. }

      eexists. split_and!; eauto.
      - intro. symmetry in H2. eauto.
      - rewrite Hν2' Hν1 //.
      - rewrite Hν1'. rewrite map_union_comm; eauto.
      - intros. rewrite Hv'; eauto.
      - intros. rewrite Hin'. eauto.
    Qed.
  End exchange.

  Lemma cgraph_ind (R : relation V) (g : cgraph V L) (P : V  Prop) :
    cgraph_wf g 
    asym R 
    (∀ x, (∀ y, R x y  sc (edge g) x y  P y)  P x)  (∀ x, P x).
  Proof.
    intros [] Hasy Hind.
    eapply uforest_ind; eauto.
    intros. eapply Hind.
    intros. eapply H2; eauto.
    eapply elem_of_to_uforest.
    done.
  Qed.

  (*
     The relation R x y l tells us for each pair of objects (x,y), what
     the waiting direction is for an edge x--[l]-->y labeled with l.
     If R x y l, then the waiting direction is in the same direction as the edge.
     If ¬R x y l, then waiting direction is in the opposite direction as the edge.
     So R tells us the waiting direction *relative to* the edge direction.
  *)
  Lemma cgraph_ind' (R : V  V  L  Prop)
      {Hproper :  x y, Proper (() ==> iff) (R x y)}
      (g : cgraph V L)
      (Hwf : cgraph_wf g)
      (P : V  Prop) :
    (∀ x,
      (∀ y l, out_edges g x !! y  Some l  R x y l  P y) 
      (∀ y l, out_edges g y !! x  Some l  ¬ R y x l  P y) 
      P x) 
    (∀ x, P x).
  Proof.
    intros Hind.
    eapply (cgraph_ind  x y,
       l, (out_edges g x !! y  Some l  R x y l) 
           (out_edges g y !! x  Some l  ¬ R y x l))); eauto.
    - destruct Hwf.
      intros ?? (l1 & [[]|[]]) (l2 & [[]|[]]).
      + exfalso. eapply H0; eauto using some_edge.
      + exfalso. rewrite ->H2 in H4. inversion H4. subst.
        eapply H5. rewrite <-H8. done.
      + exfalso. rewrite ->H2 in H4. inversion H4. subst.
        eapply H3. rewrite H8. done.
      + exfalso. eapply H0; eauto using some_edge.
    - intros. eapply Hind; intros.
      + eapply H0; eauto.
        left. eauto using some_edge.
      + eapply H0; eauto.
        right. eauto using some_edge.
  Qed.
End cgraph.

Section cgraph_si.
  Context `{!Sbi PROP, Countable V} {L : ofe}.
  Implicit Types g : cgraph V L.

  Lemma in_labels_out_edges g ν2 l X :
    in_labels g ν2  {[+ l +]}  X @{PROP}  ν1, out_edges g ν1 !! ν2  Some l.
  Proof.
    iIntros "H".
    iInduction g as [|v l1 g] "IH" using map_ind forall (X).
    - rewrite in_labels_empty.
      by iDestruct "H" as %?%symmetry%multiset_empty_equiv_eq.
    - rewrite in_labels_insert //.
      destruct (l1 !! ν2) as [l2|] eqn:E; simpl.
      + iDestruct (multiset_singleton_disj_unionI with "H") as "[[H1 H2]|H]".
        * iExists v. rewrite out_edges_insert decide_True // E.
          iRewrite "H1". done.
        * iDestruct "H" as (Z) "[H1 H2]".
          setoid_rewrite out_edges_insert.
          iDestruct ("IH" with "H1") as (v3) "H3".
          iExists v3. unfold out_edges.
          destruct (g !! v3) eqn:E2; last first.
          { rewrite lookup_empty. iDestruct "H3" as %H3. inversion H3. }
          rewrite decide_False //. intros ->. simplify_eq.
      + rewrite left_id.
        iDestruct ("IH" with "H") as (ν1) "H".
        setoid_rewrite out_edges_insert.
        iExists ν1. case_decide; eauto. subst.
        by rewrite /out_edges H0 lookup_empty option_equivI.
  Qed.

  Lemma in_labels_out_edges1 g ν2 l :
    in_labels g ν2  {[+ l +]} @{PROP}
     ν1, out_edges g ν1 !! ν2  Some l.
  Proof.
    iIntros "H". iApply (in_labels_out_edges _ _ _ ).
    rewrite right_id //.
  Qed.

  Lemma in_labels_out_edges2 g v l1 l2 :
    in_labels g v  {[+ l1; l2 +]} @{PROP}
     ν1 ν2,  ν1  ν2  
      out_edges g ν1 !! v  Some l1 
      out_edges g ν2 !! v  Some l2.
  Proof.
    iIntros "H".
    iInduction g as [|v3 l3 g] "IH" using map_ind.
    { rewrite in_labels_empty.
      by iDestruct "H" as %?%symmetry%multiset_empty_equiv_eq. }
    rewrite in_labels_insert //.
    destruct (l3 !! v) as [l4|] eqn:E; simpl.
    - iDestruct (multiset_singleton_disj_union_singletonsI with "H") as "[[H1 H2]|[H1 H2]]".
      + iDestruct (in_labels_out_edges1 with "H2") as (ν1) "H2".
        iExists v3,ν1.
        rewrite !out_edges_insert.
        repeat case_decide; simplify_eq; eauto.
        { rewrite /out_edges H0 lookup_empty.
          iDestruct "H2" as %H2. inversion H2. }
        iSplit; first done.
        iSplit; eauto.
        rewrite E. iRewrite "H1". done.
      + iDestruct (in_labels_out_edges1 with "H2") as (ν1) "H2".
        iExists ν1,v3.
        rewrite !out_edges_insert.
        repeat case_decide; simplify_eq; eauto.
        { rewrite /out_edges H0 lookup_empty.
          iDestruct "H2" as %H2. inversion H2. }
        iSplit; first done.
        iSplit; eauto.
        rewrite E. iRewrite "H1". done.
    - rewrite left_id.
      iDestruct ("IH" with "H") as (ν1 ν2) "(% & H1 & H2)".
      iExists ν1,ν2. iSplit; first done.
      rewrite !out_edges_insert.
      case_decide; case_decide; simplify_eq; eauto.
      + rewrite /out_edges H0 lookup_empty.
        iDestruct "H1" as %Q. inversion Q.
      + rewrite /out_edges H0 lookup_empty.
        iDestruct "H2" as %Q. inversion Q.
  Qed.

  Lemma in_labels_delete_edge_eqI g ν1 ν2 l X :
    out_edges g ν1 !! ν2 = Some l 
    in_labels g ν2  {[+ l +]}  X @{PROP} in_labels (delete_edge g ν1 ν2) ν2  X.
  Proof.
    iIntros (Hout) "Hin". rewrite /delete_edge. unfold out_edges in Hout.
    destruct (g !! ν1) as [Σ|] eqn:Hgν1; last done.
    pose proof (in_labels_update g ν1 (delete ν2 (out_edges g ν1)) Σ ν2 Hgν1) as .
    destruct (Σ !! ν2) eqn:F; simplify_eq/=.
    rewrite lookup_delete /= left_id in . by rewrite - multiset_disj_union_injI.
  Qed.

  Lemma exchange_alloc g ν1 ν2 Σ1 Σ2 l :
    cgraph_wf g 
    ¬uconn g ν1 ν2 
    Σ1 ## Σ2 
    out_edges g ν1  out_edges g ν2  Σ1  Σ2 @{PROP}
     g',
       cgraph_wf g'  
      out_edges g' ν1  {[ ν2 := l ]}  Σ1 
      out_edges g' ν2  Σ2 
      (∀ ν,  ν  ν1  ν  ν2   out_edges g' ν  out_edges g ν) 
      in_labels g' ν2  {[+ l +]}  in_labels g ν2 
      (∀ ν,  ν  ν2   in_labels g' ν  in_labels g ν).
  Proof.
    iIntros (Hwf Hnuconn Hdisj) "Hsplit".
    iDestruct (gmap_union_dist_eqI with "Hsplit") as (Σ1' Σ2' ?) "#[HΣ1 HΣ2]".
    iAssert  Σ1' ## Σ2' %I as %?.
    { iRewrite "HΣ1". by iRewrite "HΣ2". }
    destruct (exchange g ν1 ν2 Σ1' Σ2')
      as (g' & Hwf' & Hnuconn' & Hout1 & Hout2 & Hrest & Hin); [by eauto..|].
    assert edge g' ν1 ν2).
    { intros ?. apply Hnuconn', rtc_once. by left. }
    iExists (insert_edge g' ν1 ν2 l).
    iSplit; [by eauto using insert_edge_wf|]. repeat iSplit.
    - rewrite out_edges_insert_edge decide_True // Hout1.
      rewrite insert_union_singleton_l. by iRewrite "HΣ1".
    - assert (ν1  ν2).
      { intros ->. by apply Hnuconn'. }
      by rewrite out_edges_insert_edge decide_False // Hout2.
    - iIntros (ν [??]). rewrite out_edges_insert_edge decide_False // Hrest //.
    - by rewrite in_labels_insert_edge // decide_True // Hin.
    - iIntros (ν ?). rewrite in_labels_insert_edge // decide_False //.
  Qed.

  Lemma exchange_dealloc g ν1 ν2 Σ1 Σ2 l :
    cgraph_wf g 
    Σ1 ## Σ2 
    out_edges g ν1 !! ν2  Some l 
    delete ν2 (out_edges g ν1)  out_edges g ν2  Σ1  Σ2 @{PROP}
     g',
       cgraph_wf g'  
       ¬ uconn g' ν1 ν2  
      out_edges g' ν1  Σ1 
      out_edges g' ν2  Σ2 
      (∀ ν,  ν  ν1  ν  ν2   out_edges g' ν  out_edges g ν) 
      (∀ X, in_labels g ν2  {[+ l +]}  X  in_labels g' ν2  X) 
      (∀ v,  v  ν2   in_labels g' v  in_labels g v).
  Proof.
    iIntros (Hg Hdisj) "#[Hout Hsplit]". rewrite option_equivI.
    destruct (out_edges g ν1 !! ν2) as [l'|] eqn:Hl; [|done].
    iDestruct (gmap_union_dist_eqI with "Hsplit") as (Σ1' Σ2' ?) "[HΣ1 HΣ2]".
    iAssert  Σ1' ## Σ2' %I as %?.
    { iRewrite "HΣ1". by iRewrite "HΣ2". }
    destruct (exchange (delete_edge g ν1 ν2) ν1 ν2 Σ1' Σ2')
      as (g' & Hwf' & Hnuconn' & Hout1 & Hout2 & Hrest & Hin).
    - eapply delete_edge_wf; eauto.
    - eapply delete_edge_uconn; eauto using some_edge_L.
    - done.
    - rewrite !out_edges_delete_edge. sdec; eauto.
      exfalso. eapply no_self_edge; eauto using some_edge_L.
    - iExists g'. iSplit; [done|]. repeat iSplit; auto.
      + by rewrite Hout1.
      + by rewrite Hout2.
      + iIntros (ν [??]). rewrite Hrest // out_edges_delete_edge. by case_decide.
      + iIntros (X) "H". rewrite Hin.
        iApply in_labels_delete_edge_eqI; first done. by iRewrite "Hout".
      + iIntros (ν ?). rewrite Hin. by rewrite in_labels_delete_edge_neq.
  Qed.

  Lemma out_edges_in_labels g ν1 ν2 l :
    out_edges g ν1 !! ν2  Some l @{PROP}  X, in_labels g ν2  {[+ l +]}  X.
  Proof.
    iIntros "Hl". rewrite option_equivI.
    destruct (out_edges g ν1 !! ν2) as [l'|] eqn:Hl; [|done].
    destruct (out_edges_in_labels_L g ν1 ν2 l') as [X ?]; first by rewrite Hl.
    iExists X. by iRewrite -"Hl".
  Qed.

  Lemma move_fork g ν1 ν2 ν3 Σ1 Σ2 l1 l2 :
    cgraph_wf g 
    ν1  ν2  ν1  ν3  ν2  ν3 
    out_edges g ν2 =   out_edges g ν3 =  
    in_labels g ν2 =   in_labels g ν3 =  
    Σ1 ## Σ2 
    out_edges g ν1  Σ1  Σ2 @{PROP}
     g',
       cgraph_wf g'  
      out_edges g' ν1  {[ ν2 := l1 ]}  Σ2 
      out_edges g' ν2   
      out_edges g' ν3  {[ ν2 := l2 ]}  Σ1 
      in_labels g' ν2  {[+ l1; l2 +]} 
      (∀ v,  v  ν1  v  ν2  v  ν3  out_edges g' v  out_edges g v) 
      (∀ v,  v  ν2   in_labels g' v  in_labels g v).
  Proof.
    iIntros (Hwf (Hneq12 & Hneq13 & Hneq23) Hout2 Hout3 Hin2 Hin3 Hdisj) "#Hout".
    iDestruct (exchange_alloc g ν1 ν2 Σ2 Σ1 l1 with "[]")
      as (g' Hwf') "(Hout1' & Hout2' & Hout' & Hin2' & Hin')".
    { done. }
    { intros Hu. apply Hneq12, symmetry.
      eapply no_edges_no_uconn; [done| |done]. rewrite Hin2 //. }
    { done. }
    { rewrite Hout2 right_id_L map_union_comm //. }
    iAssert (out_edges g' ν3  )%I as %Hout3'.
    { iRewrite ("Hout'" $! ν3 with "[//]"). by rewrite Hout3. }
    rewrite map_empty_equiv_eq in Hout3'.
    iAssert (in_labels g' ν3  )%I as %?%multiset_empty_equiv_eq.
    { iRewrite ("Hin'" $! ν3 with "[//]"). by rewrite Hin3. }
    iDestruct (exchange_alloc g' ν3 ν2 Σ1  l2 with "[]")
      as (g'' Hwf'') "(Hout3'' & Hout2'' & Hout'' & Hin2'' & Hin'')".
    { done. }
    { intros ?. by apply Hneq23, symmetry, (no_edges_no_uconn g'). }
    { solve_map_disjoint. }
    { by rewrite Hout3' left_id right_id. }
    iExists g''. iSplit; first done. repeat iSplit.
    - by iRewrite ("Hout''" $! ν1 with "[//]").
    - done.
    - done.
    - iRewrite "Hin2''". iRewrite "Hin2'". rewrite Hin2 right_id.
      by rewrite -(comm _ {[+ l2 +]}).
    - iIntros (ν (?&?&?)). iRewrite ("Hout''" $! ν with "[//]").
      by iRewrite ("Hout'" $! ν with "[//]").
    - iIntros (ν ?). iRewrite ("Hin''" $! ν with "[//]").
      by iRewrite ("Hin'" $! ν with "[//]").
  Qed.

  Lemma exchange_relabel g ν1 ν2 Σ1 Σ2 l l' :
    cgraph_wf g 
    Σ1 ## Σ2 
    out_edges g ν1 !! ν2  Some l 
    delete ν2 (out_edges g ν1)  out_edges g ν2  Σ1  Σ2 @{PROP}
     g',
       cgraph_wf g'  
      out_edges g' ν1  {[ ν2 := l' ]}  Σ1 
      out_edges g' ν2  Σ2 
      (∀ ν,  ν  ν1  ν  ν2   out_edges g' ν  out_edges g ν) 
      (∀ X, in_labels g ν2  {[+ l +]}  X  in_labels g' ν2  {[+ l' +]}  X) 
      (∀ ν,  ν  ν2   in_labels g' ν  in_labels g ν).
  Proof.
    iIntros (Hwf Hdisj) "#[Hout Hsplit]".
    iDestruct (exchange_dealloc g ν1 ν2 Σ1 Σ2 l with "[]")
      as (g' Hwf' Hnuconn') "(Hout1 & Hout2 & Hrest & Hin2 & Hin)"; [by auto..|].
    assert edge g' ν1 ν2).
    { intros ?. apply Hnuconn', rtc_once. by left. }
    iExists (insert_edge g' ν1 ν2 l').
    iSplit; first by eauto using insert_edge_wf.
    repeat iSplit.
    - rewrite out_edges_insert_edge decide_True //.
      rewrite insert_union_singleton_l. by iRewrite "Hout1".
    - assert (ν1  ν2) by (intros ->; by apply Hnuconn').
      by rewrite out_edges_insert_edge decide_False.
    - iIntros (ν [??]). rewrite out_edges_insert_edge decide_False //.
      by iApply "Hrest".
    - iIntros (X) "#HX".
      rewrite in_labels_insert_edge // decide_True //.
      by iRewrite ("Hin2" $! X with "[//]").
    - iIntros (ν ?). rewrite in_labels_insert_edge // decide_False //.
      by iApply "Hin".
  Qed.

  Lemma ref_alloc g ν1 ν2 l :
    cgraph_wf g 
    ν1  ν2 
    out_edges g ν2 =  
    in_labels g ν2 =  
    @{PROP}
     g',
       cgraph_wf g'  
      out_edges g' ν1  {[ ν2:=l ]}  out_edges g ν1 
      (∀ v,  v  ν1   out_edges g' v  out_edges g v) 
      in_labels g' ν2  {[+ l +]} 
      (∀ v,  v  ν2   in_labels g' v  in_labels g v).
  Proof.
    iIntros (Hwf Hneq12 Hout Hin). assert  uconn g ν1 ν2).
    { intros ?. by eapply Hneq12, symmetry, no_edges_no_uconn. }
    iDestruct (exchange_alloc g ν1 ν2 (out_edges g ν1) (out_edges g ν2) l with "[]")
      as (g' Hwf') "(Hout1 & Hout2 & Hout & Hin1 & Hin2)";
      [by eauto using not_uconn_out_disjoint..|].
    iExists g'. iSplit; first done. repeat iSplit.
    - done.
    - iIntros (ν ?). destruct (decide (ν = ν2)) as [->|]; [done|].
      iApply "Hout". auto.
    - iRewrite "Hin1". by rewrite Hin !right_id.
    - done.
  Qed.

  Lemma ref_free g ν1 ν2 l :
    cgraph_wf g 
    out_edges g ν1 !! ν2  Some l  in_labels g ν2  {[+ l +]} @{PROP}
     g',
       cgraph_wf g'  
      out_edges g' ν1  delete ν2 (out_edges g ν1) 
      in_labels g' ν2   
      (∀ ν,  ν  ν1   out_edges g' ν  out_edges g ν) 
      (∀ v,  v  ν2   in_labels g' v  in_labels g v).
  Proof.
    iIntros (Hwf) "#[Hout Hin]".
    iAssert  delete ν2 (out_edges g ν1) ## out_edges g ν2 %I as %Hdisj.
    { rewrite option_equivI. destruct (_ !! _) as [l'|] eqn:Hν1; last done.
      iPureIntro.
      by eapply map_disjoint_delete_l, edge_out_disjoint, some_edge_L. }
    iDestruct (exchange_dealloc g ν1 ν2
                 (delete ν2 (out_edges g ν1)) (out_edges g ν2) l with "[]")
      as (g' Hwf' Huconn) "(Hout1 & Hout2 & Hout' & Hin1 & Hin2)"; eauto.
    iExists g'. iSplit; first done. repeat iSplit.
    - done.
    - iApply "Hin1". by rewrite right_id.
    - iIntros (ν ?). destruct (decide (ν = ν2)) as [->|]; [done|].
      iApply "Hout'". auto.
    - done.
  Qed.
End cgraph_si.