Datasets:

Modalities:
Text
Languages:
English
Libraries:
Datasets
License:
File size: 69,401 Bytes
4365a98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
theory BTree_ImpSet
  imports
    BTree_Imp
    BTree_Set
begin

section "Imperative Set operations"

subsection "Auxiliary operations"

definition "split_relation xs \<equiv>
   \<lambda>(as,bs) i. i \<le> length xs \<and> as = take i xs \<and> bs = drop i xs"

lemma split_relation_alt:
  "split_relation as (ls,rs) i = (as = ls@rs \<and> i = length ls)"
  by (auto simp add: split_relation_def)


lemma split_relation_length: "split_relation xs (ls,rs) (length xs) = (ls = xs \<and> rs = [])"
  by (simp add: split_relation_def)

(* auxiliary lemmas on assns *)
(* simp? not sure if it always makes things more easy *)
lemma list_assn_prod_map: "list_assn (A \<times>\<^sub>a B) xs ys = list_assn B (map snd xs) (map snd ys) * list_assn A (map fst xs) (map fst ys)"
  apply(induct "(A \<times>\<^sub>a B)" xs ys rule: list_assn.induct)
     apply(auto simp add: ab_semigroup_mult_class.mult.left_commute ent_star_mono star_aci(2) star_assoc)
  done

(* concrete *)
lemma id_assn_list: "h \<Turnstile> list_assn id_assn (xs::'a list) ys \<Longrightarrow> xs = ys"
  apply(induction "id_assn::('a \<Rightarrow> 'a \<Rightarrow> assn)" xs ys rule: list_assn.induct)
     apply(auto simp add: less_Suc_eq_0_disj pure_def)
  done


lemma snd_map_help:
  "x \<le> length tsi \<Longrightarrow>
       (\<forall>j<x. snd (tsi ! j) = ((map snd tsi)!j))"
  "x < length tsi \<Longrightarrow> snd (tsi!x) = ((map snd tsi)!x)"
  by auto


lemma split_ismeq: "((a::nat) \<le> b \<and> X) = ((a < b \<and> X) \<or> (a = b \<and> X))"
  by auto

lemma split_relation_map: "split_relation as (ls,rs) i \<Longrightarrow> split_relation (map f as) (map f ls, map f rs) i"
  apply(induction as arbitrary: ls rs i)
   apply(auto simp add: split_relation_def take_map drop_Cons')
  apply(metis list.simps(9) take_map)
  done

lemma split_relation_access: "\<lbrakk>split_relation as (ls,rs) i; rs = r#rrs\<rbrakk> \<Longrightarrow> as!i = r"
  by (simp add: split_relation_alt)



lemma index_to_elem_all: "(\<forall>j<length xs. P (xs!j)) = (\<forall>x \<in> set xs. P x)"
  by (simp add: all_set_conv_nth)

lemma index_to_elem: "n < length xs \<Longrightarrow> (\<forall>j<n. P (xs!j)) = (\<forall>x \<in> set (take n xs). P x)"
  by (simp add: all_set_conv_nth)
    (* ----------------- *)

definition split_half :: "('a::heap \<times> 'b::{heap}) pfarray \<Rightarrow> nat Heap"
  where
    "split_half a \<equiv> do {
  l \<leftarrow> pfa_length a;
  return (l div 2)
}"

lemma split_half_rule[sep_heap_rules]: "<
    is_pfa c tsi a
  * list_assn R ts tsi>
    split_half a
  <\<lambda>i.
      is_pfa c tsi a
    * list_assn R ts tsi
    * \<up>(i = length ts div 2 \<and>  split_relation ts (BTree_Set.split_half ts) i)>"
  unfolding split_half_def split_relation_def
  apply(rule hoare_triple_preI)
  apply(sep_auto dest!: list_assn_len mod_starD)
  done

subsection "The imperative split locale"

text "This locale extends the abstract split locale,
assuming that we are provided with an imperative program
that refines the abstract split function."


locale imp_split = abs_split: BTree_Set.split split
  for split::
    "('a btree \<times> 'a::{heap,default,linorder}) list \<Rightarrow> 'a
       \<Rightarrow> ('a btree \<times> 'a) list \<times> ('a btree \<times> 'a) list" +
  fixes imp_split:: "('a btnode ref option \<times> 'a::{heap,default,linorder}) pfarray \<Rightarrow> 'a \<Rightarrow> nat Heap"
  assumes imp_split_rule [sep_heap_rules]:"sorted_less (separators ts) \<Longrightarrow>
   <is_pfa c tsi (a,n)
  * blist_assn k ts tsi>
    imp_split (a,n) p
  <\<lambda>i.
    is_pfa c tsi (a,n)
    * blist_assn k ts tsi
    * \<up>(split_relation ts (split ts p) i)>\<^sub>t"
begin

subsection "Membership"

partial_function (heap) isin :: "'a btnode ref option \<Rightarrow> 'a \<Rightarrow>  bool Heap"
  where
    "isin p x =
  (case p of
     None \<Rightarrow> return False |
     (Some a) \<Rightarrow> do {
       node \<leftarrow> !a;
       i \<leftarrow> imp_split (kvs node) x;
       tsl \<leftarrow> pfa_length (kvs node);
       if i < tsl then do {
         s \<leftarrow> pfa_get (kvs node) i;
         let (sub,sep) = s in
         if x = sep then
           return True
         else
           isin sub x
       } else
           isin (last node) x
    }
)"

subsection "Insertion"


datatype 'b btupi =
  T\<^sub>i "'b btnode ref option" |
  Up\<^sub>i "'b btnode ref option" "'b" "'b btnode ref option"

fun btupi_assn where
  "btupi_assn k (abs_split.T\<^sub>i l) (T\<^sub>i li) =
   btree_assn k l li" |
  "btupi_assn k (abs_split.Up\<^sub>i l a r) (Up\<^sub>i li ai ri) =
   btree_assn k l li * id_assn a ai * btree_assn k r ri" |
  "btupi_assn _ _ _ = false"



definition node\<^sub>i :: "nat \<Rightarrow> ('a btnode ref option \<times> 'a) pfarray \<Rightarrow> 'a btnode ref option \<Rightarrow> 'a btupi Heap" where
  "node\<^sub>i k a ti \<equiv> do {
    n \<leftarrow> pfa_length a;
    if n \<le> 2*k then do {
      a' \<leftarrow> pfa_shrink_cap (2*k) a;
      l \<leftarrow> ref (Btnode a' ti);
      return (T\<^sub>i (Some l))
    }
    else do {
      b \<leftarrow> (pfa_empty (2*k) :: ('a btnode ref option \<times> 'a) pfarray Heap);
      i \<leftarrow> split_half a;
      m \<leftarrow> pfa_get a i;
      b' \<leftarrow> pfa_drop a (i+1) b;
      a' \<leftarrow> pfa_shrink i a;
      a'' \<leftarrow> pfa_shrink_cap (2*k) a';
      let (sub,sep) = m in do {
        l \<leftarrow> ref (Btnode a'' sub);
        r \<leftarrow> ref (Btnode b' ti);
        return (Up\<^sub>i (Some l) sep (Some r))
      }
    }
}"


partial_function (heap) ins :: "nat \<Rightarrow> 'a \<Rightarrow> 'a btnode ref option \<Rightarrow> 'a btupi Heap"
  where
    "ins k x apo = (case apo of
  None \<Rightarrow>
    return (Up\<^sub>i None x None) |
  (Some ap) \<Rightarrow> do {
    a \<leftarrow> !ap;
    i \<leftarrow> imp_split (kvs a) x;
    tsl \<leftarrow> pfa_length (kvs a);
    if i < tsl then do {
      s \<leftarrow> pfa_get (kvs a) i;
      let (sub,sep) = s in
      if sep = x then
        return (T\<^sub>i apo)
      else do {
        r \<leftarrow> ins k x sub;
        case r of
          (T\<^sub>i lp) \<Rightarrow> do {
            pfa_set (kvs a) i (lp,sep);
            return (T\<^sub>i apo)
          } |
          (Up\<^sub>i lp x' rp) \<Rightarrow> do {
            pfa_set (kvs a) i (rp,sep);
            if tsl < 2*k then do {
                kvs' \<leftarrow> pfa_insert (kvs a) i (lp,x');
                ap := (Btnode kvs' (last a));
                return (T\<^sub>i apo)
            } else do {
              kvs' \<leftarrow> pfa_insert_grow (kvs a) i (lp,x');
              node\<^sub>i k kvs' (last a)
            }
          }
        }
      }
    else do {
      r \<leftarrow> ins k x (last a);
      case r of
        (T\<^sub>i lp) \<Rightarrow> do {
          ap := (Btnode (kvs a) lp);
          return (T\<^sub>i apo)
        } |
        (Up\<^sub>i lp x' rp) \<Rightarrow>
          if tsl < 2*k then do {
            kvs' \<leftarrow> pfa_append (kvs a) (lp,x');
            ap := (Btnode kvs' rp);
            return (T\<^sub>i apo)
          } else do {
            kvs' \<leftarrow> pfa_append_grow' (kvs a) (lp,x');
            node\<^sub>i k kvs' rp
        }
    }
  }
)"


(*fun tree\<^sub>i::"'a up\<^sub>i \<Rightarrow> 'a btree" where
  "tree\<^sub>i (T\<^sub>i sub) = sub" |
  "tree\<^sub>i (Up\<^sub>i l a r) = (Node [(l,a)] r)"

fun insert::"nat \<Rightarrow> 'a \<Rightarrow> 'a btree \<Rightarrow> 'a btree" where
  "insert k x t = tree\<^sub>i (ins k x t)"
*)

definition insert :: "nat \<Rightarrow> ('a::{heap,default,linorder}) \<Rightarrow> 'a btnode ref option \<Rightarrow> 'a btnode ref option Heap" where
  "insert \<equiv> \<lambda>k x ti. do {
  ti' \<leftarrow> ins k x ti;
  case ti' of
     T\<^sub>i sub \<Rightarrow> return sub |
     Up\<^sub>i l a r \<Rightarrow> do {
        kvs \<leftarrow> pfa_init (2*k) (l,a) 1;
        t' \<leftarrow> ref (Btnode kvs r);
        return (Some t')
      }
}"

subsection "Deletion"

(* rebalance middle tree gets a list of trees, an index pointing to
the position of sub/sep and a last tree *)
definition rebalance_middle_tree:: "nat \<Rightarrow> (('a::{default,heap,linorder}) btnode ref option \<times> 'a) pfarray \<Rightarrow> nat \<Rightarrow> 'a btnode ref option \<Rightarrow> 'a btnode Heap"
  where
    "rebalance_middle_tree \<equiv> \<lambda> k tsi i r_ti. (
  case r_ti of
  None \<Rightarrow> do {
      (r_sub,sep) \<leftarrow> pfa_get tsi i;
      case r_sub of None \<Rightarrow>  return (Btnode tsi r_ti)
  } |
  Some p_t \<Rightarrow> do {
      (r_sub,sep) \<leftarrow> pfa_get tsi i;
      case r_sub of (Some p_sub) \<Rightarrow>  do {
      ti \<leftarrow> !p_t;
      sub \<leftarrow> !p_sub;
      l_sub \<leftarrow> pfa_length (kvs sub);
      l_tts \<leftarrow> pfa_length (kvs ti);
      if l_sub \<ge> k \<and> l_tts \<ge> k then do {
        return (Btnode tsi r_ti)
      } else do {
        l_tsi \<leftarrow> pfa_length tsi;
        if i+1 = l_tsi then do {
          mts' \<leftarrow> pfa_append_extend_grow (kvs sub) (last sub,sep) (kvs ti);
          res_node\<^sub>i \<leftarrow> node\<^sub>i k mts' (last ti);
          case res_node\<^sub>i of
            T\<^sub>i u \<Rightarrow> do {
              tsi' \<leftarrow> pfa_shrink i tsi;
              return (Btnode tsi' u)
            } |
            Up\<^sub>i l a r \<Rightarrow> do {
              tsi' \<leftarrow> pfa_set tsi i (l,a);
              return (Btnode tsi' r)
            }
        } else do {
          (r_rsub,rsep) \<leftarrow> pfa_get tsi (i+1);
          case r_rsub of Some p_rsub \<Rightarrow> do {
            rsub \<leftarrow> !p_rsub;
            mts' \<leftarrow> pfa_append_extend_grow (kvs sub) (last sub,sep) (kvs rsub);
            res_node\<^sub>i \<leftarrow> node\<^sub>i k mts' (last rsub);
            case res_node\<^sub>i of
             T\<^sub>i u \<Rightarrow> do {
              tsi' \<leftarrow> pfa_set tsi i (u,rsep);
              tsi'' \<leftarrow> pfa_delete tsi' (i+1);
              return (Btnode tsi'' r_ti)
            } |
             Up\<^sub>i l a r \<Rightarrow> do {
              tsi' \<leftarrow> pfa_set tsi i (l,a);
              tsi'' \<leftarrow> pfa_set tsi' (i+1) (r,rsep);
              return (Btnode tsi'' r_ti)
            }
          }
        }
      }
  }
})
"


definition rebalance_last_tree:: "nat \<Rightarrow> (('a::{default,heap,linorder}) btnode ref option \<times> 'a) pfarray \<Rightarrow> 'a btnode ref option \<Rightarrow> 'a btnode Heap"
  where
    "rebalance_last_tree \<equiv> \<lambda>k tsi ti. do {
   l_tsi \<leftarrow> pfa_length tsi;
   rebalance_middle_tree k tsi (l_tsi-1) ti
}"


subsection "Refinement of the abstract B-tree operations"

definition empty ::"('a::{default,heap,linorder}) btnode ref option Heap"
  where "empty = return None"


lemma P_imp_Q_implies_P: "P \<Longrightarrow> (Q \<longrightarrow> P)"
  by simp


lemma  "sorted_less (inorder t) \<Longrightarrow>
   <btree_assn k t ti>
     isin ti x
   <\<lambda>r. btree_assn k t ti * \<up>(abs_split.isin t x = r)>\<^sub>t"
proof(induction t x arbitrary: ti rule: abs_split.isin.induct)
  case (1 x)
  then show ?case
    apply(subst isin.simps)
    apply (cases ti)
     apply (auto simp add: return_cons_rule)
    done
next
  case (2 ts t x)
  then obtain ls rs where list_split[simp]: "split ts x = (ls,rs)"
    by (cases "split ts x")
  then show ?case
  proof (cases rs)
    (* NOTE: induction condition trivial here *)
    case [simp]: Nil
    show ?thesis
      apply(subst isin.simps)
      apply(sep_auto)
      using "2.prems" sorted_inorder_separators apply blast
      apply(auto simp add: split_relation_def dest!: sym[of "[]"] mod_starD list_assn_len)[]
      apply(rule hoare_triple_preI)
      apply(auto simp add: split_relation_def dest!: sym[of "[]"] mod_starD list_assn_len)[]
      using 2(3) apply(sep_auto heap: "2.IH"(1)[of ls "[]"] simp add: sorted_wrt_append)
      done
  next
    case [simp]: (Cons h rrs)
    obtain sub sep where h_split[simp]: "h = (sub,sep)"
      by (cases h)
    show ?thesis
    proof (cases "sep = x")
      (* NOTE: no induction required here, only vacuous counter cases generated *)
      case [simp]: True
      then show ?thesis
        apply(simp split: list.splits prod.splits)
        apply(subst isin.simps)
        using "2.prems" sorted_inorder_separators apply(sep_auto)
         apply(rule hoare_triple_preI)
         apply(auto simp add: split_relation_alt list_assn_append_Cons_left dest!: mod_starD list_assn_len)[]
        apply(rule hoare_triple_preI)
        apply(auto simp add: split_relation_def dest!: sym[of "[]"] mod_starD list_assn_len)[]
        done
    next
      case [simp]: False
      show ?thesis
        apply(simp split: list.splits prod.splits)
        apply safe
        using False apply simp
        apply(subst isin.simps)
        using "2.prems" sorted_inorder_separators
        apply(sep_auto)
          (*eliminate vacuous case*)
          apply(auto simp add: split_relation_alt list_assn_append_Cons_left dest!:  mod_starD list_assn_len)[]
          (* simplify towards induction step *)
         apply(auto simp add: split_relation_alt list_assn_append_Cons_left dest!: mod_starD list_assn_len)[]

(* NOTE show that z = (suba, sepa) *)
         apply(rule norm_pre_ex_rule)+
         apply(rule hoare_triple_preI)
        subgoal for p tsi n ti xsi suba sepa zs1 z zs2 _
          apply(subgoal_tac "z = (suba, sepa)", simp)
          using 2(3) apply(sep_auto
              heap:"2.IH"(2)[of ls rs h rrs sub sep]
              simp add: sorted_wrt_append)
          using list_split Cons h_split apply simp_all
            (* proof that previous assumptions hold later *)
           apply(rule P_imp_Q_implies_P)
           apply(rule ent_ex_postI[where x="(tsi,n)"])
           apply(rule ent_ex_postI[where x="ti"])
           apply(rule ent_ex_postI[where x="(zs1 @ (suba, sepa) # zs2)"])
           apply(rule ent_ex_postI[where x="zs1"])
           apply(rule ent_ex_postI[where x="z"])
           apply(rule ent_ex_postI[where x="zs2"])
           apply sep_auto
            (* prove subgoal_tac assumption *)
          apply (metis (no_types, lifting) list_assn_aux_ineq_len list_assn_len nth_append_length star_false_left star_false_right)
          done
            (* eliminate last vacuous case *)
        apply(rule hoare_triple_preI)
        apply(auto simp add: split_relation_def dest!: mod_starD list_assn_len)[]
        done
    qed
  qed
qed

declare abs_split.node\<^sub>i.simps [simp add]

lemma node\<^sub>i_rule: assumes c_cap: "2*k \<le> c" "c \<le> 4*k+1"
  shows "<is_pfa c tsi (a,n) * list_assn ((btree_assn k) \<times>\<^sub>a id_assn) ts tsi * btree_assn k t ti>
  node\<^sub>i k (a,n) ti
  <\<lambda>r. btupi_assn k (abs_split.node\<^sub>i k ts t) r >\<^sub>t"
proof (cases "length ts \<le> 2 * k")
  case [simp]: True
  then show ?thesis
    apply(subst node\<^sub>i_def)
    apply(rule hoare_triple_preI)
    apply(sep_auto dest!: mod_starD list_assn_len)
       apply(sep_auto simp add: is_pfa_def)[]
    using c_cap apply(sep_auto simp add: is_pfa_def)[]
     apply(sep_auto  dest!: mod_starD list_assn_len)[]
    using True apply(sep_auto dest!: mod_starD list_assn_len)
    done
next
  note max.absorb1 [simp del] max.absorb2 [simp del] max.absorb3 [simp del] max.absorb4 [simp del]
  note min.absorb1 [simp del] min.absorb2 [simp del] min.absorb3 [simp del] min.absorb4 [simp del]
  case [simp]: False
  then obtain ls sub sep rs where
    split_half_eq: "BTree_Set.split_half ts = (ls,(sub,sep)#rs)"
    using abs_split.node\<^sub>i_cases by blast
  then show ?thesis
    apply(subst node\<^sub>i_def)
    apply(rule hoare_triple_preI)
    apply(sep_auto dest!: mod_starD list_assn_len)
       apply(sep_auto simp add:  split_relation_alt split_relation_length is_pfa_def dest!: mod_starD list_assn_len)
    using False apply(sep_auto simp add: split_relation_alt )
    using False  apply(sep_auto simp add: is_pfa_def)[]
    apply(sep_auto)[]
      apply(sep_auto simp add: is_pfa_def split_relation_alt)[]
    using c_cap apply(sep_auto simp add: is_pfa_def)[]
     apply(sep_auto)[]
    using c_cap apply(sep_auto simp add: is_pfa_def)[]
    using c_cap apply(simp)
    apply(vcg)
    apply(simp)
    apply(rule impI)
    subgoal for  _ _ _ _ rsa subi ba rn lsi al ar _
      thm ent_ex_postI
      thm ent_ex_postI[where x="take (length tsi div 2) tsi"]
        (* instantiate right hand side *)
      apply(rule ent_ex_postI[where x="(rsa,rn)"])
      apply(rule ent_ex_postI[where x="ti"])
      apply(rule ent_ex_postI[where x="(drop (Suc (length tsi div 2)) tsi)"])
      apply(rule ent_ex_postI[where x="lsi"])
      apply(rule ent_ex_postI[where x="subi"])
      apply(rule ent_ex_postI[where x="take (length tsi div 2) tsi"])
        (* introduce equality between equality of split tsi/ts and original lists *)
      apply(simp add: split_relation_alt)
      apply(subgoal_tac "tsi =
            take (length tsi div 2) tsi @ (subi, ba) # drop (Suc (length tsi div 2)) tsi")
       apply(rule back_subst[where a="blist_assn k ts (take (length tsi div 2) tsi @ (subi, ba) # (drop (Suc (length tsi div 2)) tsi))" and b="blist_assn k ts tsi"])
        apply(rule back_subst[where a="blist_assn k (take (length tsi div 2) ts @ (sub, sep) # rs)" and b="blist_assn k ts"])
         apply(subst list_assn_aux_append_Cons)
          apply sep_auto
         apply sep_auto
        apply simp
       apply simp
      apply(rule back_subst[where a="tsi ! (length tsi div 2)" and b="(subi, ba)"])
       apply(rule id_take_nth_drop)
       apply simp
      apply simp
      done
    done
qed
declare abs_split.node\<^sub>i.simps [simp del]


lemma node\<^sub>i_no_split: "length ts \<le> 2*k \<Longrightarrow> abs_split.node\<^sub>i k ts t = abs_split.T\<^sub>i (Node ts t)"
  by (simp add: abs_split.node\<^sub>i.simps)


lemma node\<^sub>i_rule_app: "\<lbrakk>2*k \<le> c; c \<le> 4*k+1\<rbrakk> \<Longrightarrow>
<is_pfa c (tsi' @ [(li, ai)]) (aa, al) *
   blist_assn k ls tsi' *
   btree_assn k l li *
   id_assn a ai *
   btree_assn k r ri> node\<^sub>i k (aa, al) ri
 <btupi_assn k (abs_split.node\<^sub>i k (ls @ [(l, a)]) r)>\<^sub>t"
proof -
  note node\<^sub>i_rule[of k c "(tsi' @ [(li, ai)])" aa al "(ls @ [(l, a)])" r ri]
  moreover assume "2*k \<le> c" "c \<le> 4*k+1"
  ultimately show ?thesis
    by (simp add: mult.left_assoc)
qed

lemma node\<^sub>i_rule_ins2: "\<lbrakk>2*k \<le> c; c \<le> 4*k+1; length ls = length lsi\<rbrakk> \<Longrightarrow>
 <is_pfa c (lsi @ (li, ai) # (ri,a'i) # rsi) (aa, al) *
   blist_assn k ls lsi *
   btree_assn k l li *
   id_assn a ai *
   btree_assn k r ri *
   id_assn a' a'i *
   blist_assn k rs rsi *
   btree_assn k t ti> node\<^sub>i k (aa, al)
          ti <btupi_assn k (abs_split.node\<^sub>i k (ls @ (l, a) # (r,a') # rs) t)>\<^sub>t"
proof -
  assume [simp]: "2*k \<le> c" "c \<le> 4*k+1" "length ls = length lsi"
  moreover note node\<^sub>i_rule[of k c "(lsi @ (li, ai) # (ri,a'i) # rsi)" aa al "(ls @ (l, a) # (r,a') # rs)" t ti]
  ultimately show ?thesis
    by (simp add: mult.left_assoc list_assn_aux_append_Cons)
qed

lemma ins_rule:
  "sorted_less (inorder t) \<Longrightarrow> <btree_assn k t ti>
  ins k x ti
  <\<lambda>r. btupi_assn k (abs_split.ins k x t) r>\<^sub>t"
proof (induction k x t arbitrary: ti rule: abs_split.ins.induct)
  case (1 k x)
  then show ?case
    apply(subst ins.simps)
    apply (sep_auto simp add: pure_app_eq)
    done
next
  case (2 k x ts t)
  obtain ls rrs where list_split: "split ts x = (ls,rrs)"
    by (cases "split ts x")
  have [simp]: "sorted_less (separators ts)"
    using "2.prems" sorted_inorder_separators by simp
  have [simp]: "sorted_less (inorder t)"
    using "2.prems" sorted_inorder_induct_last by simp
  show ?case
  proof (cases rrs)
    case Nil
    then show ?thesis
    proof (cases "abs_split.ins k x t")
      case (T\<^sub>i a)
      then show ?thesis
        apply(subst ins.simps)
        apply(sep_auto)
        subgoal for p tsil tsin tti
          using Nil list_split
          by (simp add: list_assn_aux_ineq_len split_relation_alt)
        subgoal for p tsil tsin tti tsi' i tsin' _ sub sep
          apply(rule hoare_triple_preI)
          using Nil list_split
          by (simp add: list_assn_aux_ineq_len split_relation_alt)
        subgoal for p tsil tsin tti tsi'
          thm "2.IH"(1)[of ls rrs tti]
          using Nil list_split T\<^sub>i apply(sep_auto split!: list.splits simp add: split_relation_alt
              heap add: "2.IH"(1)[of ls rrs tti])
          subgoal for ai
            apply(cases ai)
             apply sep_auto
            apply sep_auto
            done
          done
        done
    next
      case (Up\<^sub>i l a r)
      then show ?thesis
        apply(subst ins.simps)
        apply(sep_auto)
        subgoal for p tsil tsin tti
          using Nil list_split
          by (simp add: list_assn_aux_ineq_len split_relation_alt)
        subgoal for p tsil tsin tti tsi' i tsin' _ sub sep
          using Nil list_split
          by (simp add: list_assn_aux_ineq_len split_relation_alt)
        subgoal for p tsil tsin tti tsi' i tsin'
          thm "2.IH"(1)[of ls rrs tti]
          using Nil list_split Up\<^sub>i apply(sep_auto split!: list.splits
              simp add: split_relation_alt
              heap add: "2.IH"(1)[of ls rrs tti])
          subgoal for ai
            apply(cases ai)
             apply sep_auto
            apply(rule hoare_triple_preI)
            apply(sep_auto)
              apply(auto dest!: mod_starD simp add: is_pfa_def)[]
             apply (sep_auto)
            subgoal for li ai ri (* no split case *)
              apply(subgoal_tac "length (ls @ [(l,a)]) \<le> 2*k")
               apply(simp add: node\<^sub>i_no_split)
               apply(rule ent_ex_postI[where x="(tsil,Suc tsin)"])
               apply(rule ent_ex_postI[where x="ri"])
               apply(rule ent_ex_postI[where x="tsi' @ [(li, ai)]"])
               apply(sep_auto)
              apply (sep_auto dest!: mod_starD list_assn_len simp add: is_pfa_def)
              done
                (* split case*)
            apply(sep_auto heap add: node\<^sub>i_rule_app)
            done
          done
        done
    qed
  next
    case (Cons a rs)
    obtain sub sep where a_split: "a = (sub,sep)"
      by (cases a)
    then have [simp]: "sorted_less (inorder sub)"
      using "2.prems" abs_split.split_axioms list_split Cons sorted_inorder_induct_subtree split_def
      by fastforce
    then show ?thesis
    proof(cases "x = sep")
      case True
      show ?thesis
        apply(subst ins.simps)
        apply(sep_auto)
        subgoal for p tsil tsin tti tsi j subi
          using Cons list_split a_split True
          by sep_auto
        subgoal for p tsil tsin tti tsi j _ _ subi sepi
          apply(rule hoare_triple_preI)
          using Cons list_split a_split True
          apply(subgoal_tac "sepi = sep")
           apply (sep_auto simp add: split_relation_alt)
          apply(sep_auto simp add: list_assn_prod_map dest!: mod_starD id_assn_list)
          by (metis length_map snd_conv snd_map_help(2) split_relation_access)
        subgoal for p tsil tsin tti tsi j
          apply(rule hoare_triple_preI)
          using Cons list_split
          by (sep_auto simp add: split_relation_alt dest!: mod_starD list_assn_len)
        done
    next
      case False
      then show ?thesis
      proof (cases "abs_split.ins k x sub")
        case (T\<^sub>i a')
        then show ?thesis
          apply(auto simp add: Cons list_split a_split False)
          using False apply simp
          apply(subst ins.simps)
          apply vcg
           apply auto
          apply(rule norm_pre_ex_rule)+
            (* at this point, we want to introduce the split, and after that tease the
  hoare triple assumptions out of the bracket, s.t. we don't split twice *)
          apply vcg
           apply sep_auto
          using list_split Cons
          apply(simp add: split_relation_alt list_assn_append_Cons_left)
          apply (rule impI)
          apply(rule norm_pre_ex_rule)+
          apply(rule hoare_triple_preI)
          apply sep_auto
            (* discard wrong branch *)
          subgoal for p tsil tsin ti zs1 subi sepi zs2 _ _ suba
            apply(subgoal_tac "sepi  = x")
            using list_split Cons a_split
             apply(auto  dest!:  mod_starD )[]
            apply(auto dest!:  mod_starD list_assn_len)[]
            done
              (* actual induction branch *)
          subgoal for p tsil tsin ti zs1 subi sepi zs2 _ _ n z suba sepa
            apply (cases a, simp)
            apply(subgoal_tac "subi = suba", simp)
            using list_split a_split T\<^sub>i False
             apply (vcg heap: 2)
               apply(auto split!: btupi.splits)
              (* careful progression for manual value insertion *)
             apply vcg
              apply simp
             apply vcg
             apply simp
            subgoal for a'i q r
              apply(rule impI)
              apply(simp add: list_assn_append_Cons_left)
              apply(rule ent_ex_postI[where x="(tsil,tsin)"])
              apply(rule ent_ex_postI[where x="ti"])
              apply(rule ent_ex_postI[where x="(zs1 @ (a'i, sepi) # zs2)"])
              apply(rule ent_ex_postI[where x="zs1"])
              apply(rule ent_ex_postI[where x="(a'i,sep)"])
              apply(rule ent_ex_postI[where x="zs2"])
              apply sep_auto
               apply (simp add: pure_app_eq)
              apply(sep_auto dest!:  mod_starD list_assn_len)[]
              done
            apply (metis list_assn_aux_ineq_len Pair_inject list_assn_len nth_append_length star_false_left star_false_right)
            done
          subgoal for p tsil tsin ti zs1 subi sepi zs2 _ _ suba
            apply(auto dest!:  mod_starD list_assn_len)[]
            done
          done
      next
        case (Up\<^sub>i l w r)
        then show ?thesis
          apply(auto simp add: Cons list_split a_split False)
          using False apply simp
          apply(subst ins.simps)
          apply vcg
           apply auto
          apply(rule norm_pre_ex_rule)+
            (* at this point, we want to introduce the split, and after that tease the
  hoare triple assumptions out of the bracket, s.t. we don't split twice *)
          apply vcg
           apply sep_auto
          using list_split Cons
          apply(simp add: split_relation_alt list_assn_append_Cons_left)
          apply (rule impI)
          apply(rule norm_pre_ex_rule)+
          apply(rule hoare_triple_preI)
          apply sep_auto
            (* discard wrong branch *)
          subgoal for p tsil tsin ti zs1 subi sepi zs2 _ _ suba
            apply(subgoal_tac "sepi  = x")
            using list_split Cons a_split
             apply(auto  dest!:  mod_starD )[]
            apply(auto dest!:  mod_starD list_assn_len)[]
            done
              (* actual induction branch *)
          subgoal for p tsil tsin ti zs1 subi sepi zs2 _ _ n z suba sepa
            apply(subgoal_tac "subi = suba", simp)
            thm 2(2)[of ls rrs a rs sub sep]
            using list_split a_split Cons Up\<^sub>i False
             apply (sep_auto heap: 2(2))
             apply(auto split!: btupi.splits)
              (* careful progression for manual value insertion *)
              apply vcg
               apply simp
            subgoal for li wi ri u (* no split case *)
              apply (cases u,simp)
              apply (sep_auto dest!: mod_starD list_assn_len heap: pfa_insert_grow_rule)
                apply (simp add: is_pfa_def)[]
                apply (metis le_less_linear length_append length_take less_not_refl min.absorb2 trans_less_add1)
               apply(simp add: is_pfa_def)
               apply (metis add_Suc_right length_Cons length_append length_take min.absorb2)
              apply(sep_auto split: prod.splits  dest!: mod_starD list_assn_len)[]
                (* no split case *)
              apply(subgoal_tac "length (ls @ [(l,w)]) \<le> 2*k")
               apply(simp add: node\<^sub>i_no_split)
               apply(rule ent_ex_postI[where x="(tsil,Suc tsin)"])
               apply(rule ent_ex_postI[where x="ti"])
               apply(rule ent_ex_postI[where x="(zs1 @ (li, wi) # (ri, sep) # zs2)"])
               apply(sep_auto dest!: mod_starD list_assn_len)
              apply (sep_auto dest!: mod_starD list_assn_len simp add: is_pfa_def)
              done
             apply vcg
              apply simp
            subgoal for x21 x22 x23 u (* split case *)
              apply (cases u,simp)
              thm pfa_insert_grow_rule[where ?l="((zs1 @ (suba, sepi) # zs2)[length ls := (x23, sepa)])"]
              apply (sep_auto dest!: mod_starD list_assn_len heap: pfa_insert_grow_rule)
               apply (simp add: is_pfa_def)[]
               apply (metis le_less_linear length_append length_take less_not_refl min.absorb2 trans_less_add1)
              apply(auto split: prod.splits  dest!: mod_starD list_assn_len)[]

              apply (vcg heap: node\<^sub>i_rule_ins2)
                 apply simp
                apply simp
               apply simp
              apply sep_auto
              done
            apply(auto dest!:  mod_starD list_assn_len)[]
            done
          subgoal for p tsil tsin ti zs1 subi sepi zs2 _ _ suba
            apply(auto dest!:  mod_starD list_assn_len)[]
            done
          done
      qed
    qed
  qed
qed

text "The imperative insert refines the abstract insert."

lemma insert_rule:
  assumes "k > 0" "sorted_less (inorder t)"
  shows "<btree_assn k t ti>
  insert k x ti
  <\<lambda>r. btree_assn k (abs_split.insert k x t) r>\<^sub>t"
  unfolding insert_def
  apply(cases "abs_split.ins k x t")
   apply(sep_auto split!: btupi.splits heap: ins_rule[OF assms(2)])
  using assms
  apply(vcg heap: ins_rule[OF assms(2)])
  apply(simp split!: btupi.splits)
  apply(vcg)
   apply auto[]
  apply vcg
  apply auto[]
  subgoal for l a r li ai ri tsa tsn ti
    apply(rule ent_ex_postI[where x="(tsa,tsn)"])
    apply(rule ent_ex_postI[where x="ri"])
    apply(rule ent_ex_postI[where x="[(li, ai)]"])
    apply sep_auto
    done
  done

text "The \"pure\" resulting rule follows automatically."
lemma insert_rule':
  shows "<btree_assn (Suc k) t ti * \<up>(abs_split.invar_inorder (Suc k) t \<and> sorted_less (inorder t))>
  insert (Suc k) x ti
  <\<lambda>ri.\<exists>\<^sub>Ar. btree_assn (Suc k) r ri * \<up>(abs_split.invar_inorder (Suc k) r \<and> sorted_less (inorder r) \<and> inorder r = (ins_list x (inorder t)))>\<^sub>t"
  using abs_split.insert_bal abs_split.insert_order abs_split.insert_inorder
  by (sep_auto heap: insert_rule simp add: sorted_ins_list)

lemma list_update_length2 [simp]:
  "(xs @ x # y # ys)[Suc (length xs) := z] = (xs @ x # z # ys)"
  by (induct xs, auto)


lemma node\<^sub>i_rule_ins: "\<lbrakk>2*k \<le> c; c \<le> 4*k+1; length ls = length lsi\<rbrakk> \<Longrightarrow>
 <is_pfa c (lsi @ (li, ai) # rsi) (aa, al) *
   blist_assn k ls lsi *
   btree_assn k l li *
   id_assn a ai *
   blist_assn k rs rsi *
   btree_assn k t ti>
     node\<^sub>i k (aa, al) ti
 <btupi_assn k (abs_split.node\<^sub>i k (ls @ (l, a) # rs) t)>\<^sub>t"
proof -
  assume [simp]: "2*k \<le> c" "c \<le> 4*k+1" "length ls = length lsi"
  moreover note node\<^sub>i_rule[of k c "(lsi @ (li, ai) # rsi)" aa al "(ls @ (l, a) # rs)" t ti]
  ultimately show ?thesis
    by (simp add: mult.left_assoc list_assn_aux_append_Cons)
qed

lemma btupi_assn_T: "h \<Turnstile> btupi_assn k (abs_split.node\<^sub>i k ts t) (T\<^sub>i x) \<Longrightarrow> abs_split.node\<^sub>i k ts t = abs_split.T\<^sub>i (Node ts t)"
  apply(auto simp add: abs_split.node\<^sub>i.simps dest!: mod_starD split!: list.splits)
  done

lemma btupi_assn_Up: "h \<Turnstile> btupi_assn k (abs_split.node\<^sub>i k ts t) (Up\<^sub>i l a r) \<Longrightarrow>
  abs_split.node\<^sub>i k ts t = (
    case BTree_Set.split_half ts of (ls, (sub,sep)#rs) \<Rightarrow>
      abs_split.Up\<^sub>i (Node ls sub) sep (Node rs t))"
  apply(auto simp add: abs_split.node\<^sub>i.simps dest!: mod_starD split!: list.splits)
  done

lemma second_last_access:"(xs@a#b#ys) ! Suc(length xs) = b"
  by (simp add: nth_via_drop)

lemma pfa_assn_len:"h \<Turnstile> is_pfa k ls (a,n) \<Longrightarrow> n \<le> k \<and> length ls = n"
  by (auto simp add: is_pfa_def)

(*declare "impCE"[rule del]*)
lemma rebalance_middle_tree_rule:
  assumes "height t = height sub"
    and "case rs of (rsub,rsep) # list \<Rightarrow> height rsub = height t | [] \<Rightarrow> True"
    and "i = length ls"
  shows "<is_pfa (2*k) tsi (a,n) * blist_assn k (ls@(sub,sep)#rs) tsi * btree_assn k t ti>
  rebalance_middle_tree k (a,n) i ti
  <\<lambda>r. btnode_assn k (abs_split.rebalance_middle_tree k ls sub sep rs t) r >\<^sub>t"
  apply(simp add: list_assn_append_Cons_left)
  apply(rule norm_pre_ex_rule)+
proof(goal_cases)
  case (1 lsi z rsi)
  then show ?case
  proof(cases z)
    case z_split: (Pair subi sepi)
    then show ?thesis
    proof(cases sub)
      case sub_leaf[simp]: Leaf
      then have t_leaf[simp]: "t = Leaf" using assms
        by (cases t) auto
      show ?thesis
        apply (subst rebalance_middle_tree_def)
        apply (rule hoare_triple_preI)
        apply (vcg)
        using assms apply (auto dest!: mod_starD list_assn_len split!: option.splits)
        apply (vcg)
        apply (auto dest!: mod_starD list_assn_len split!: option.splits)
        apply (rule ent_ex_postI[where x=tsi])
        apply sep_auto
        done
    next
      case sub_node[simp]: (Node mts mt)
      then obtain tts tt where t_node[simp]: "t = Node tts tt" using assms
        by (cases t) auto
      then show ?thesis
      proof(cases "length mts \<ge> k \<and> length tts \<ge> k")
        case True
        then show ?thesis
          apply(subst rebalance_middle_tree_def)
          apply(rule hoare_triple_preI)
          apply(sep_auto dest!: mod_starD)
          using assms apply (auto  dest!: list_assn_len)[]

          using assms apply(sep_auto  split!: prod.splits)
          using assms apply (auto simp del: height_btree.simps dest!: mod_starD list_assn_len)[]
          using z_split apply(auto)[]
          subgoal for _ _ _ _ _ _ _ _ tp tsia' tsin' _ _  _ _ _ _ _ _ _ _ tsia tsin tti ttsi sepi subp
            apply(auto dest!: mod_starD list_assn_len simp: prod_assn_def)[]
            apply(vcg)
             apply(auto)[]
             apply(rule ent_ex_postI[where x="lsi@(Some subp, sepi)#rsi"])
             apply(rule ent_ex_postI[where x="(tsia, tsin)"])
             apply(rule ent_ex_postI[where x="tti"])
             apply(rule ent_ex_postI[where x=ttsi])
             apply(sep_auto)[]
            apply(rule hoare_triple_preI)
            using True apply(auto dest!: mod_starD list_assn_len)
            done
          done
      next
        case False
        then show ?thesis
        proof(cases rs)
          case Nil
          then show ?thesis
            apply(subst rebalance_middle_tree_def)
            apply(rule hoare_triple_preI)
            apply(sep_auto dest!: mod_starD)
            using assms apply (auto  dest!: list_assn_len)[]

             apply(sep_auto  split!: prod.splits)
            using assms apply (auto simp del: height_btree.simps dest!: mod_starD list_assn_len)[]
            using z_split apply(auto)[]
            subgoal for _ _ _ _ _ _ _ _ tp tsia' tsin' _ _  _ _ _ _ _ _ _ _ tsia tsin tti ttsi
              apply(auto dest!: mod_starD list_assn_len simp: prod_assn_def)[]
              apply(vcg)
              using False apply(auto dest!: mod_starD list_assn_len)
              done
            apply(sep_auto dest!: mod_starD)
            using assms apply (auto dest!: list_assn_len)[]
            using assms apply (auto dest!: list_assn_len)[]
            apply(sep_auto)
            using assms apply (auto dest!: list_assn_len mod_starD)[]
            using assms apply (auto dest!: list_assn_len mod_starD)[]
              (* Issue: we do not know yet what  'subp is pointing at *)
            subgoal for _ _ _ _ _ _ tp tsia tsin tti ttsi _ _ _ _ _ _ _ _ tsia' tsin' tti' tsi' subi sepi subp
              apply(subgoal_tac "z = (subi, sepi)")
               prefer 2
               apply (metis assms(3) list_assn_len nth_append_length)
              apply simp
              apply(vcg)
              subgoal
                (* still the "IF" branch *)
                apply(rule entailsI)
                  (* solves impossible case*)
                using False apply (auto dest!: list_assn_len mod_starD)[]
                done
              apply (auto del: impCE)
              apply(thin_tac "_ \<Turnstile> _")+
              apply(rule hoare_triple_preI)
                (* for each possible combination of \<le> and \<not>\<le>, a subgoal is created *)
              apply(sep_auto heap add: node\<^sub>i_rule_ins dest!: mod_starD del: impCE)
                 apply (auto dest!: pfa_assn_len)[]
                apply (auto dest!: pfa_assn_len list_assn_len)[]
              subgoal
                apply(thin_tac "_ \<Turnstile> _")+
                apply(rule hoare_triple_preI)
                apply(sep_auto split!: btupi.splits del: impCE)
                 apply(auto dest!: btupi_assn_T mod_starD del: impCE)[]
                 apply(rule ent_ex_postI[where x="lsi"])
                 apply sep_auto
                apply (sep_auto del: impCE)
                apply(auto dest!: btupi_assn_Up mod_starD split!: list.splits del: impCE)[]
                subgoal for li ai ri
                  apply(rule ent_ex_postI[where x="lsi @ [(li, ai)]"])
                  apply sep_auto
                  done
                done
              apply (sep_auto del: impCE)
              using assms apply(auto dest!: pfa_assn_len list_assn_len mod_starD)[]
              using assms apply(auto dest!: pfa_assn_len list_assn_len mod_starD)[]
              done
            done
        next
          case (Cons rss rrs)
          then show ?thesis
            apply(subst rebalance_middle_tree_def)
            apply(rule hoare_triple_preI)
            apply(sep_auto dest!: mod_starD)
            using assms apply (auto  dest!: list_assn_len)[]

             apply(sep_auto  split!: prod.splits)
            using assms apply (auto simp del: height_btree.simps dest!: mod_starD list_assn_len)[]
             apply(auto)[]
            subgoal for _ _ _ _ _ _ _ _ tp tsia' tsin' _ _  _ _ _ _ _ _ _ _ tsia tsin tti ttsi
              apply(auto dest!: mod_starD list_assn_len simp: prod_assn_def)[]
              apply(vcg)
              using False apply(auto dest!: mod_starD list_assn_len)
              done
            apply(sep_auto dest!: mod_starD del: impCE)
            using assms apply (auto dest!: list_assn_len)[]
            apply(sep_auto del: impCE)
            using assms apply (auto dest!: list_assn_len mod_starD)[]
              (* Issue: we do not know yet what  'xa' is pointing at *)
            subgoal for list_heap1 list_heap2 _ _ _ _ _ _ tp ttsia' ttsin' tti' ttsi' _ _ _ _ _ _ _ _ ttsia ttsin tti ttsi subi sepi subp
              apply(subgoal_tac "z = (subi, sepi)")
               prefer 2
               apply (metis assms(3) list_assn_len nth_append_length)
              apply simp
              apply(vcg)
              subgoal
                (* still the "IF" branch *)
                apply(rule entailsI)
                  (* solves impossible case*)
                using False apply (auto dest!: list_assn_len mod_starD)[]
                done
              apply simp
              subgoal for subtsi subti subts ti subi subtsl ttsl
                (* TODO different nodei rule here *)
                supply R = node\<^sub>i_rule_ins[where k=k and c="(max (2 * k) (Suc (_ + ttsin)))" and lsi=subts]
                thm R
                apply(cases subtsi)
                apply(sep_auto heap add: R pfa_append_extend_grow_rule dest!: mod_starD del: impCE)
                  (* all of these cases are vacuous *)
                using assms apply (auto dest!: list_assn_len pfa_assn_len)[]
                using assms apply (auto dest!: list_assn_len pfa_assn_len)[]
                using assms apply (auto dest!: list_assn_len pfa_assn_len)[]
                apply(sep_auto split!: btupi.splits del: impCE)
                using assms apply (auto dest!: list_assn_len pfa_assn_len)[]
                apply(thin_tac "_ \<Turnstile> _")+
                apply(rule hoare_triple_preI)
                apply (cases rsi)
                 apply(auto dest!: list_assn_len mod_starD)[]
                  (* TODO avoid creating subgoals here but still split the heap? do we need to do that anyways *)
                subgoal for subtsa subtsn mtsa mtsn mtt mtsi _ _ _ _ _ _ _ _ rsubsep _ rrsi rssi
                  (* ensuring that the tree to the right is not none *)
                  apply (cases rsubsep)
                  apply(subgoal_tac "rsubsep = rrsi")
                   prefer 2
                  using assms apply(auto dest!: list_assn_len mod_starD del: impCE simp add: second_last_access)[]
                  apply (simp add: prod_assn_def)
                  apply(cases rss)
                  apply simp
                  subgoal for rsubi rsepi rsub rsep
                    apply(subgoal_tac "height rsub \<noteq> 0")
                     prefer 2
                    using assms apply(auto)[]
                    apply(cases rsubi; cases rsub)
                       apply simp+
                      (* now we may proceed *)
                    apply (vcg (ss))
                    apply (vcg (ss))
                    apply (vcg (ss))
                     apply (vcg (ss))
                    apply (vcg (ss))
                    subgoal for rsubi rsubts rsubt rsubtsi' rsubti rsubtsi subnode
                      apply(cases "kvs subnode")
                      apply (vcg (ss))
                      apply (vcg (ss))
                      apply (vcg (ss))
                      apply (vcg (ss))
                       apply (vcg (ss))
                      subgoal for _ rsubtsn subtsmergedi
                        apply (cases subtsmergedi)
                        apply simp
                        apply (vcg (ss))
                        subgoal for subtsmergeda _
                          supply R = node\<^sub>i_rule_ins[where
                              k=k and
                              c="max (2*k) (Suc (subtsn + rsubtsn))" and
                              ls="mts" and
                              al="Suc (subtsn+rsubtsn)" and
                              aa=subtsmergeda and
                              ti=rsubti and
                              rsi=rsubtsi and
                              li=subti and a=sep and ai=sep
                              ]
                          thm R
                          apply(rule P_imp_Q_implies_P)
                          apply(auto del: impCE dest!: mod_starD list_assn_len)[]
                          apply(rule hoare_triple_preI)
                          apply(subgoal_tac "subtsn \<le> 2*k \<and> rsubtsn \<le> 2*k")
                           prefer 2
                           apply (auto simp add: is_pfa_def)[]
                          apply (sep_auto heap add: R del: impCE)
                          apply(sep_auto split!: btupi.splits del: impCE)
                          using assms apply(auto  dest!: mod_starD list_assn_len)[]
                           apply(sep_auto del: impCE)
                          using assms apply(auto  dest!: mod_starD list_assn_len pfa_assn_len del: impCE)[]
                           apply(thin_tac "_ \<Turnstile> _")+
                           apply(rule hoare_triple_preI)
                           apply (drule btupi_assn_T mod_starD | erule conjE exE)+
                           apply vcg
                           apply simp
                          subgoal for rsubtsi ai tsian
                            apply(cases tsian)
                            apply simp
                            apply(rule P_imp_Q_implies_P)
                            apply(rule ent_ex_postI[where x="lsi @ (ai, rsep) # rssi"])
                            apply(rule ent_ex_postI[where x="(ttsia, ttsin)"])
                            apply(rule ent_ex_postI[where x="tti"])
                            apply(rule ent_ex_postI[where x="ttsi"])
                            using assms apply (sep_auto dest!: list_assn_len)
                            done
                          subgoal for _ _ rsubp rsubtsa _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _  li ai ri
                            apply(sep_auto del: impCE)
                            using assms apply(auto dest!: list_assn_len)[]
                            apply(sep_auto del: impCE)
                            using assms apply(auto dest!: list_assn_len)[]
                            apply(thin_tac "_ \<Turnstile> _")+
                            apply(rule hoare_triple_preI)
                            apply (drule btupi_assn_Up mod_starD | erule conjE exE)+
                            apply vcg
                              (* generates two identical subgoals ? *)
                            apply(simp split!: list.split)
                             apply(rule ent_ex_postI[where x="(lsi @ (li, ai) # (ri, rsepi) # rssi)"])
                             apply(rule ent_ex_postI[where x="(ttsia, ttsin)"])
                             apply(rule ent_ex_postI[where x="tti"])
                             apply(rule ent_ex_postI[where x="ttsi"])
                            using assms apply (sep_auto dest!: list_assn_len)
                            apply(rule ent_ex_postI[where x="(lsi @ (li, ai) # (ri, rsepi) # rssi)"])
                            apply(rule ent_ex_postI[where x="(ttsia, ttsin)"])
                            apply(rule ent_ex_postI[where x="tti"])
                            apply(rule ent_ex_postI[where x="ttsi"])
                            using assms apply (sep_auto dest!: list_assn_len)
                            done
                          done
                        done
                      done
                    done
                  done
                done
              done
            done
        qed
      qed
    qed
  qed
qed

lemma rebalance_last_tree_rule:
  assumes "height t = height sub"
    and "ts = list@[(sub,sep)]"
  shows "<is_pfa (2*k) tsi tsia * blist_assn k ts tsi * btree_assn k t ti>
  rebalance_last_tree k tsia ti
  <\<lambda>r. btnode_assn k (abs_split.rebalance_last_tree k ts  t) r >\<^sub>t"
  apply(subst rebalance_last_tree_def)
  apply(rule hoare_triple_preI)
  using assms apply(auto dest!: mod_starD)
  apply(subgoal_tac "length tsi - Suc 0 = length list")
   prefer 2
   apply(auto dest!: list_assn_len)[]
  using assms apply(sep_auto)
  supply R = rebalance_middle_tree_rule[where
      ls="list" and
      rs="[]" and
      i="length tsi - 1", simplified]
  apply(cases tsia)
  using R by blast

partial_function (heap) split_max ::"nat \<Rightarrow> ('a::{default,heap,linorder}) btnode ref option \<Rightarrow> ('a btnode ref option \<times> 'a) Heap"
  where
    "split_max k r_t = (case r_t of Some p_t \<Rightarrow> do {
   t \<leftarrow> !p_t;
   (case (last t) of None \<Rightarrow> do {
      (sub,sep) \<leftarrow> pfa_last (kvs t);
      tsi' \<leftarrow> pfa_butlast (kvs t);
      p_t := Btnode tsi' sub;
      return (Some p_t, sep)
  } |
    Some x \<Rightarrow> do {
      (sub,sep) \<leftarrow> split_max k (Some x);
      p_t' \<leftarrow> rebalance_last_tree k (kvs t) sub;
      p_t := p_t';
      return (Some p_t, sep)
  })
})
"


declare  abs_split.split_max.simps [simp del] abs_split.rebalance_last_tree.simps [simp del] height_btree.simps [simp del]

lemma split_max_rule:
  assumes "abs_split.nonempty_lasttreebal t"
    and "t \<noteq> Leaf"
  shows "<btree_assn k t ti>
  split_max k ti
  <((btree_assn k) \<times>\<^sub>a id_assn) (abs_split.split_max k t)>\<^sub>t"
  using assms
proof(induction k t arbitrary: ti rule: abs_split.split_max.induct)
  case (2 Leaf)
  then show ?case by auto
next
  case (1 k ts tt)
  then show ?case
  proof(cases tt)
    case Leaf
    then show ?thesis
      apply(subst split_max.simps)
      apply (vcg)
      using assms apply auto[]
      apply (vcg (ss))
      apply simp
      apply (vcg (ss))
      apply (vcg (ss))
       apply (vcg (ss))
      apply (vcg (ss))
      apply (vcg (ss))
      apply (vcg (ss))
      apply (vcg (ss))
       apply (vcg (ss))
       apply (vcg (ss))
        apply (vcg (ss))
        apply(rule hoare_triple_preI)
        apply (vcg (ss))
      using 1 apply(auto dest!: mod_starD)[]
       apply (vcg (ss))
       apply (vcg (ss))
       apply (vcg (ss))
       apply (vcg (ss))
       apply (vcg (ss))
      subgoal for tp tsi tti tsi' tnode subsep sub sep
        apply(cases tsi)
        apply(rule hoare_triple_preI)
        apply (vcg)
        apply(auto simp add: prod_assn_def abs_split.split_max.simps split!: prod.splits)
        subgoal for tsia tsin _ _ tsin' lastsep lastsub
          apply(rule ent_ex_postI[where x="(tsia, tsin')"])
          apply(rule ent_ex_postI[where x="sub"])
          apply(rule ent_ex_postI[where x="(butlast tsi')"])
          using 1 apply (auto dest!: mod_starD simp add: list_assn_append_Cons_left)
          apply sep_auto
          done
        done
      apply(sep_auto)
      done
  next
    case (Node tts ttt)
    have IH_help: "abs_split.nonempty_lasttreebal tt \<Longrightarrow>
tt \<noteq> Leaf \<Longrightarrow>
<btree_assn k (Node tts ttt) (Some ttp)> split_max k (Some ttp) <(btree_assn k \<times>\<^sub>a id_assn) (abs_split.split_max k tt)>\<^sub>t"
      for ttp
      using "1.IH" Node by blast
    obtain butlasttts l_sub l_sep where ts_split:"tts = butlasttts@[(l_sub, l_sep)]"
      using 1 Node by auto
    from Node show ?thesis
      apply(subst split_max.simps)
      apply (vcg)
      using 1 apply auto[]
      apply (vcg (ss))
      apply simp
      apply (vcg (ss))
      apply (vcg (ss))
       apply (vcg (ss))
      apply (vcg (ss))
      apply (vcg (ss))
      apply (vcg (ss))
      apply (vcg (ss))
       apply (vcg (ss))
      using 1 apply(auto dest!: mod_starD)[]
      apply (vcg (ss))
      subgoal for tp tsi tti tsi' tnode ttp
        using "1.prems" apply (vcg heap add: IH_help)
          apply simp
         apply simp
        apply(subst prod_assn_def)
        apply(cases "abs_split.split_max k tt")
        apply (auto simp del: abs_split.split_max.simps abs_split.rebalance_last_tree.simps height_btree.simps)[]
        subgoal for ttsubi ttmaxi ttsub ttmax butlasttsi' lasttssubi butlastts lasttssub lasttssepi lasttssep
          apply(rule hoare_triple_preI)
          supply R = rebalance_last_tree_rule[where k=k and tsia=tsi and ti=ttsubi and t=ttsub and tsi=tsi' and ts=" (butlasttsi' @ [(lasttssubi, lasttssepi)])"
              and list=butlasttsi' and sub=lasttssubi and sep=lasttssepi]
          thm R
          using ts_split
            (*TODO weird post conditions... *)
          apply (sep_auto heap add: R
              simp del: abs_split.split_max.simps abs_split.rebalance_last_tree.simps height_btree.simps
              dest!: mod_starD)
            apply (metis abs_split.nonempty_lasttreebal.simps(2) abs_split.split_max_height btree.distinct(1))
           apply simp
          apply(rule hoare_triple_preI)
          apply (simp add: prod_assn_def)
          apply vcg
          apply(subst abs_split.split_max.simps)
          using "1.prems" apply(auto dest!: mod_starD split!: prod.splits btree.splits)
          subgoal for _ _ _ _ _ _ _ _ _ _ tp'
            apply(cases "abs_split.rebalance_last_tree k (butlasttsi' @ [(lasttssubi, lasttssepi)]) ttsub"; cases tp')
             apply auto
            apply(rule ent_ex_preI)
            subgoal for _ _ tsia' tsin' tt' _ tsi'
              apply(rule ent_ex_postI[where x="(tsia', tsin')"])
              apply(rule ent_ex_postI[where x="tt'"])
              apply(rule ent_ex_postI[where x="tsi'"])
              apply sep_auto
              done
            done
          done
        done
      done
  qed
qed

partial_function (heap) del ::"nat \<Rightarrow> 'a \<Rightarrow> ('a::{default,heap,linorder}) btnode ref option \<Rightarrow> 'a btnode ref option Heap"
  where
    "del k x ti = (case ti of None \<Rightarrow> return None |
   Some p \<Rightarrow> do {
   node \<leftarrow> !p;
   i \<leftarrow> imp_split (kvs node) x;
   tsl \<leftarrow> pfa_length (kvs node);
   if i < tsl then do {
     (sub,sep) \<leftarrow> pfa_get (kvs node) i;
     if sep \<noteq> x then do {
       sub' \<leftarrow> del k x sub;
       kvs' \<leftarrow> pfa_set (kvs node) i (sub',sep);
       node' \<leftarrow> rebalance_middle_tree k kvs' i (last node);
       p := node';
       return (Some p)
      }
     else if sub = None then do{
       kvs' \<leftarrow> pfa_delete (kvs node) i;
       p := (Btnode kvs' (last node));
       return (Some p)
     }
     else do {
        sm \<leftarrow> split_max k sub;
        kvs' \<leftarrow> pfa_set (kvs node) i sm;
        node' \<leftarrow> rebalance_middle_tree k kvs' i (last node);
        p := node';
        return (Some p)
     }
   } else do {
       t' \<leftarrow> del k x (last node);
       node' \<leftarrow> rebalance_last_tree k (kvs node) t';
       p := node';
       return (Some p)
    }
})"

lemma rebalance_middle_tree_update_rule:
  assumes "height tt = height sub"
    and "case rs of (rsub,rsep) # list \<Rightarrow> height rsub = height tt | [] \<Rightarrow> True"
    and "i = length ls"
  shows "<is_pfa (2 * k) (zs1 @ (x', sep) # zs2) a * btree_assn k sub x' *
     blist_assn k ls zs1 *
     id_assn sep sep *
     blist_assn k rs zs2 *
     btree_assn k tt ti>
  rebalance_middle_tree k a i ti
   <btnode_assn k (abs_split.rebalance_middle_tree k ls sub sep rs tt)>\<^sub>t"
proof (cases a)
  case [simp]: (Pair a n)
  note R=rebalance_middle_tree_rule[of tt sub rs i ls k "zs1@(x', sep)#zs2" a n sep ti]
  show ?thesis
    apply(rule hoare_triple_preI)
    using R assms apply (sep_auto dest!: mod_starD list_assn_len simp add: prod_assn_def)
    using assn_times_assoc star_aci(3) by auto
qed

lemma del_rule:
  assumes "bal t" and "sorted (inorder t)" and "root_order k t" and "k > 0"
  shows "<btree_assn k t ti>
  del k x ti
  <btree_assn k (abs_split.del k x t)>\<^sub>t"
  using assms
proof (induction k x t arbitrary: ti rule: abs_split.del.induct)
  case (1 k x)
  then show ?case
    apply(subst del.simps)
    apply sep_auto
    done
next
  case (2 k x ts tt ti)
  obtain ls rs where split_ts[simp]: "split ts x = (ls, rs)"
    by (cases "split ts x")
  obtain tss lastts_sub lastts_sep where last_ts: "ts = tss@[(lastts_sub, lastts_sep)]"
    using "2.prems"  apply auto
    by (metis abs_split.isin.cases neq_Nil_rev_conv)
  show ?case
  proof(cases "rs")
    case Nil
    then show ?thesis
      apply(subst del.simps)
      apply sep_auto
      using "2.prems"(2) sorted_inorder_separators apply blast
      apply(rule hoare_triple_preI)
      apply (sep_auto)
      using Nil  apply (auto simp add: split_relation_alt dest!: mod_starD list_assn_len)[]
      using Nil  apply (auto simp add: split_relation_alt dest!: mod_starD list_assn_len)[]
      using Nil  apply (auto simp add: split_relation_alt dest!: mod_starD list_assn_len)[]
      apply (sep_auto heap add: "2.IH"(1))
      using "2.prems" apply (auto dest!: mod_starD)[]
      using "2.prems" apply (auto dest!: mod_starD simp add: sorted_wrt_append)[]
      using "2.prems" order_impl_root_order apply (auto dest!: mod_starD)[]
      using "2.prems" apply (auto)[]
      subgoal for tp tsia tsin tti tsi i _ _ tti'
        apply(rule hoare_triple_preI)
        supply R = rebalance_last_tree_rule[where t="(abs_split.del k x tt)" and ti=tti' and ts=ts and sub=lastts_sub
            and list=tss and sep=lastts_sep]
        thm R
        using last_ts apply(sep_auto heap add: R)
        using "2.prems" abs_split.del_height[of k tt x] order_impl_root_order[of k tt] apply (auto dest!: mod_starD)[]
         apply simp
        apply(rule hoare_triple_preI)
        apply (sep_auto)
        apply(cases "abs_split.rebalance_last_tree k ts (abs_split.del k x tt)")
         apply(auto simp add: split_relation_alt dest!: mod_starD list_assn_len)
        subgoal for tnode
          apply (cases tnode; sep_auto)
          done
        done
      done
  next
    case [simp]: (Cons rrs rss)
    then obtain sub sep where [simp]: "rrs = (sub, sep)"
      by (cases rrs)
    consider (sep_n_x) "sep \<noteq> x" |
      (sep_x_Leaf) "sep = x \<and> sub = Leaf" |
      (sep_x_Node) "sep = x \<and> (\<exists>ts t. sub = Node ts t)"
      using btree.exhaust by blast
    then show ?thesis
    proof(cases)
      case sep_n_x
      then show ?thesis
        apply(subst del.simps)
        apply sep_auto
        using "2.prems"(2) sorted_inorder_separators apply blast
        apply(vcg (ss))
        apply(vcg (ss))
        apply(vcg (ss))
         apply(vcg (ss))
         apply(vcg (ss))
        apply(vcg (ss))
        apply(vcg (ss))
        apply(vcg (ss))
        apply(vcg (ss))
         apply(vcg (ss))
          apply(vcg (ss))
          apply(vcg (ss))
          apply simp
         apply(vcg (ss))
         apply(vcg (ss))
         apply(vcg (ss))
        subgoal for tp tsi ti' tsi' tnode i tsi'l subsep subi sepi
          (* TODO this causes 4 subgoals *)
          apply(auto simp add: split_relation_alt list_assn_append_Cons_left;
              rule norm_pre_ex_rule; rule norm_pre_ex_rule; rule norm_pre_ex_rule;
              rule hoare_triple_preI;
              auto dest!: mod_starD)[]
             apply (auto simp add: split_relation_alt dest!: list_assn_len)[]
          subgoal for lsi subi rsi
            apply(subgoal_tac "subi = None")
             prefer 2
             apply(auto dest!: list_assn_len)[]
            supply R = "2.IH"(2)[of ls rs rrs rss sub sep]
            thm R
            using split_ts apply(sep_auto heap add: R)
            using "2.prems" apply auto[]
               apply (metis "2.prems"(2) sorted_inorder_induct_subtree)
            using "2.prems" apply auto[]
              apply (meson "2.prems"(4) order_impl_root_order)
            using "2.prems"(4) apply fastforce
            apply(vcg (ss))
            apply(vcg (ss))
             apply(vcg (ss))
             apply (auto simp add: split_relation_alt dest!: list_assn_len)[]
            apply(vcg (ss))
            apply(vcg (ss); simp)
            apply(cases tsi; simp)
            subgoal for subi' _ tsia' tsin'
              supply R = rebalance_middle_tree_update_rule
              thm R
                (* TODO create a new heap rule, in the node_i style *)
              apply(auto dest!: list_assn_len)[]
              apply(rule hoare_triple_preI)
              apply (sep_auto heap add: R dest!: mod_starD)
              using "2.prems" abs_split.del_height[of k sub x] order_impl_root_order[of k sub] apply (auto)[]
              using "2.prems" apply (auto split!: list.splits)[]
               apply auto[]
              apply sep_auto
              subgoal for _ _ _ _ _ _ _ _ _ _ _ _ _ _ tnode''
                apply (cases "(abs_split.rebalance_middle_tree k ls (abs_split.del k x sub) sepi rss tt)"; cases tnode'')
                 apply sep_auto
                apply sep_auto
                done
              done
            done
           apply (auto simp add: split_relation_alt dest!: mod_starD list_assn_len)[]
            (* copy pasta of "none" branch *)
          subgoal for subnode lsi subi rsi
            apply(subgoal_tac "subi = Some subnode")
             prefer 2
             apply(auto dest!: list_assn_len)[]
            supply R = "2.IH"(2)[of ls rs rrs rss sub sep]
            thm R
            using split_ts apply(sep_auto heap add: R)
            using "2.prems" apply auto[]
               apply (metis "2.prems"(2) sorted_inorder_induct_subtree)
            using "2.prems" apply auto[]
              apply (meson "2.prems"(4) order_impl_root_order)
            using "2.prems"(4) apply fastforce
            apply(vcg (ss))
            apply(vcg (ss))
             apply(vcg (ss))
             apply (auto simp add: split_relation_alt dest!: list_assn_len)[]
            apply(vcg (ss))
            apply(vcg (ss); simp)
            apply(cases tsi; simp)
            subgoal for x' xab a n
              supply R = rebalance_middle_tree_update_rule
              thm R
                (* TODO create a new heap rule, in the node_i style *)
              apply(auto dest!: list_assn_len)[]
              apply(rule hoare_triple_preI)
              apply (sep_auto heap add: R dest!: mod_starD)
              using "2.prems" abs_split.del_height[of k sub x] order_impl_root_order[of k sub] apply (auto)[]
              using "2.prems" apply (auto split!: list.splits)[]
               apply auto[]
              apply sep_auto
              subgoal for _ _ _ _ _ _ _ _ _ _ _ _ _  _  tnode'
                apply (cases "(abs_split.rebalance_middle_tree k ls (abs_split.del k x sub) sepi rss tt)"; cases tnode')
                 apply sep_auto
                apply sep_auto
                done
              done
            done
          done
        apply(rule hoare_triple_preI)
        using Cons  apply (auto simp add: split_relation_alt dest!: mod_starD list_assn_len)[]
        done
    next
      case sep_x_Leaf
      then show ?thesis
        apply(subst del.simps)
        apply sep_auto
        using "2.prems"(2) sorted_inorder_separators apply blast
        apply(vcg (ss))
        apply(vcg (ss))
        apply(vcg (ss))
         apply(vcg (ss))
         apply(vcg (ss))
        apply(vcg (ss))
        apply(vcg (ss))
        apply(vcg (ss))
        apply(vcg (ss))
         apply(vcg (ss))
          apply(vcg (ss))
          apply(vcg (ss))
          apply simp
         apply(vcg (ss))
         apply(vcg (ss))
         apply(vcg (ss))
        subgoal for tp tsi ti' tsi' tnode i tsi'l subsep subi sepi
          (* TODO this causes 4 subgoals *)
          apply(auto simp add: split_relation_alt list_assn_append_Cons_left;
              rule norm_pre_ex_rule; rule norm_pre_ex_rule; rule norm_pre_ex_rule;
              rule hoare_triple_preI;
              auto dest!: mod_starD)[]
            (* the correct subbranch *)
          subgoal for lsi subi rsi
            apply(cases tsi)
            apply (sep_auto)
             apply(auto simp add: is_pfa_def dest!: list_assn_len)[]
             apply (metis add_Suc_right le_imp_less_Suc length_append length_take less_add_Suc1 less_trans_Suc list.size(4) min.cobounded2 not_less_eq)
            apply vcg
            apply auto
            subgoal for tsin tsia
              apply(rule ent_ex_postI[where x="(tsia, tsin-1)"])
              apply(rule ent_ex_postI[where x="ti'"])
              apply(rule ent_ex_postI[where x="lsi@rsi"])
              apply (sep_auto dest!: list_assn_len)
              done
            done
            apply (auto simp add: split_relation_alt dest!: list_assn_len)[]
           apply (auto simp add: split_relation_alt dest!: list_assn_len)[]
          apply (auto simp add: split_relation_alt dest!: list_assn_len)[]
          done
        apply(rule hoare_triple_preI)
        using Cons  apply (auto simp add: split_relation_alt dest!: mod_starD list_assn_len)[]
        done
    next
      case sep_x_Node
      then show ?thesis
        apply(subst del.simps)
        apply sep_auto
        using "2.prems"(2) sorted_inorder_separators apply blast
        apply(vcg (ss))
        apply(vcg (ss))
        apply(vcg (ss))
         apply(vcg (ss))
         apply(vcg (ss))
        apply(vcg (ss))
        apply(vcg (ss))
        apply(vcg (ss))
        apply(vcg (ss))
         apply(vcg (ss))
          apply(vcg (ss))
          apply(vcg (ss))
          apply simp
         apply(vcg (ss))
         apply(vcg (ss))
         apply(vcg (ss))
        subgoal for subts subt tp tsi ti tsi' tnode i tsi'l subsep subi sepi
          (* TODO this causes 4 subgoals *)
          apply(auto simp add: split_relation_alt list_assn_append_Cons_left;
              rule norm_pre_ex_rule; rule norm_pre_ex_rule; rule norm_pre_ex_rule;
              rule hoare_triple_preI;
              auto dest!: mod_starD)[]
             apply (auto simp add: split_relation_alt dest!: list_assn_len)[]
            apply (auto simp add: split_relation_alt dest!: list_assn_len)[]
            (* the correct sub branch *)
          subgoal for subnode lsi subi rsi
            apply(subgoal_tac "subi = Some subnode")
             apply (simp del: btree_assn.simps)
             supply R = split_max_rule[of "Node subts subt" k "Some subnode"]
            thm R
             apply(sep_auto heap add: R simp del: btree_assn.simps)
            using "2.prems" apply(auto dest!: list_assn_len mod_starD simp del: bal.simps order.simps)[]
            subgoal
            proof(goal_cases)
              case 1
              then have "order k (Node subts subt)"
                by blast
              moreover have "k > 0"
                by (simp add: "2.prems"(4))
              ultimately obtain sub_ls lsub lsep where sub_ts_split: "subts = sub_ls@[(lsub,lsep)]"
                by (metis abs_split.isin.cases le_0_eq list.size(3) order.simps(2) rev_exhaust zero_less_iff_neq_zero)
              from 1 have "bal (Node subts subt)"
                by auto
              then have "height lsub = height subt"
                by (simp add: sub_ts_split)
              then show ?thesis using sub_ts_split by blast
            qed
            using "2.prems" abs_split.order_bal_nonempty_lasttreebal[of k subt] order_impl_root_order[of k subt]
               apply(auto)[]
              apply (auto simp add: split_relation_alt dest!: list_assn_len)[]
             apply vcg
              apply auto[]
             apply(cases "abs_split.split_max k (Node subts subt)"; simp)
            subgoal for split_res _ split_sub split_sep
              apply(cases split_res; simp)
              subgoal for split_subi split_sepi
                supply R = rebalance_middle_tree_update_rule[
                    of tt split_sub rss "length lsi" ls k lsi split_subi split_sep rsi tsi ti
                    ]
                thm R
                  (* id_assn split_sepi doesnt match yet... *)
                apply(auto simp add: prod_assn_def dest!: list_assn_len)
                apply (sep_auto)
                 apply(rule hoare_triple_preI)
                 apply(auto dest!: mod_starD)[]
                 apply (sep_auto heap add: R)
                using "2.prems" abs_split.split_max_height[of k sub] order_impl_root_order[of k sub]
                  abs_split.order_bal_nonempty_lasttreebal[of k sub] apply (auto)[]
                using "2.prems" abs_split.split_max_bal[of sub k] order_impl_root_order[of k sub]
                  apply (auto split!: list.splits)[]
                 apply auto[]
                apply(rule hoare_triple_preI)
                apply(auto dest!: mod_starD)[]
                subgoal for subtsi''a subtsi''n ti subtsi'' tnode'
                  apply(cases "(abs_split.rebalance_middle_tree k ls split_sub split_sep rss tt)"; cases "tnode'")
                   apply auto
                  apply sep_auto
                  done
                done
              done
            apply (auto simp add: split_relation_alt dest!: list_assn_len)[]
            done
          apply (auto simp add: split_relation_alt dest!: list_assn_len)[]
          done
        apply(rule hoare_triple_preI)
        using Cons  apply (auto simp add: split_relation_alt dest!: mod_starD list_assn_len)[]
        done
    qed
  qed
qed

definition reduce_root ::"('a::{default,heap,linorder}) btnode ref option \<Rightarrow> 'a btnode ref option Heap"
  where
    "reduce_root ti = (case ti of
  None \<Rightarrow> return None |
  Some p_t \<Rightarrow> do {
    node \<leftarrow> !p_t;
    tsl \<leftarrow> pfa_length (kvs node);
    case tsl of 0 \<Rightarrow> return (last node) |
    _ \<Rightarrow> return ti
})"

lemma reduce_root_rule:
  "<btree_assn k t ti> reduce_root ti <btree_assn k (abs_split.reduce_root t)>\<^sub>t"
  apply(subst reduce_root_def)
  apply(cases t; cases ti)
     apply (sep_auto split!: nat.splits list.splits)+
  done

definition delete ::"nat \<Rightarrow> 'a \<Rightarrow> ('a::{default,heap,linorder}) btnode ref option \<Rightarrow> 'a btnode ref option Heap"
  where
    "delete k x ti = do {
  ti' \<leftarrow> del k x ti;
  reduce_root ti'
}"

lemma delete_rule:
  assumes "bal t" and "root_order k t" and "k > 0" and "sorted (inorder t)"
  shows "<btree_assn k t ti> delete k x ti <btree_assn k (abs_split.delete k x t)>\<^sub>t"
  apply(subst delete_def)
  using assms apply (sep_auto heap add: del_rule reduce_root_rule)
  done

lemma empty_rule:
  shows "<emp>
  empty
  <\<lambda>r. btree_assn k (abs_split.empty_btree) r>"
  apply(subst empty_def)
  apply(sep_auto simp add: abs_split.empty_btree_def)
  done

end

end