-
Notifications
You must be signed in to change notification settings - Fork 0
/
BPlusTree_Iter.thy
1345 lines (1253 loc) · 49.8 KB
/
BPlusTree_Iter.thy
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
theory BPlusTree_Iter
imports
BPlusTree_Imp
"HOL-Real_Asymp.Inst_Existentials"
"Separation_Logic_Imperative_HOL.Imp_List_Spec"
Flatten_Iter_Spec
Partially_Filled_Array_Iter
Subst_Mod_Mult_AC
begin
(* TODO use list_zip? \<rightarrow> not well defined return type *)
fun bplustree_assn_leafs :: "nat \<Rightarrow> ('a::heap) bplustree \<Rightarrow> 'a btnode ref \<Rightarrow> 'a btnode ref option \<Rightarrow> 'a btnode ref option \<Rightarrow> 'a btnode ref list \<Rightarrow> assn" where
"bplustree_assn_leafs k (Leaf xs) a r z leafptrs =
(\<exists>\<^sub>A xsi fwd.
a \<mapsto>\<^sub>r Btleaf xsi fwd
* is_pfa (2*k) xs xsi
* \<up>(fwd = z)
* \<up>(r = Some a)
* \<up>(leafptrs = [a])
)" |
"bplustree_assn_leafs k (Node ts t) a r z leafptrs =
(\<exists>\<^sub>A tsi ti tsi' tsi'' rs split.
a \<mapsto>\<^sub>r Btnode tsi ti
* bplustree_assn_leafs k t ti (last (r#rs)) (last (rs@[z])) (last split)
* is_pfa (2*k) tsi' tsi
* \<up>(concat split = leafptrs)
* \<up>(length tsi' = length rs)
* \<up>(length split = length rs + 1)
* \<up>(tsi'' = zip (zip (map fst tsi') (zip (butlast (r#rs)) (zip (butlast (rs@[z])) (butlast split)))) (map snd tsi'))
* list_assn ((\<lambda> t (ti,r',z',lptrs). bplustree_assn_leafs k t (the ti) r' z' lptrs) \<times>\<^sub>a id_assn) ts tsi''
)"
(*fun make_list_list where "make_list_list xs = [xs]"
lemma make_list_list_concat: "concat (make_list_list ys) = ys"
by auto
lemma ex_concat: "\<exists>xs. concat xs = ys"
using make_list_list_concat by blast*)
lemma inst_same: "(\<And>x. P x = Q x) \<Longrightarrow> (\<exists>\<^sub>A x. P x) = (\<exists>\<^sub>A x. Q x)"
by simp
lemma reorder_ex:
"\<And>z. (\<exists>\<^sub>Aa b c d e f g. z a b c d e f g) = (\<exists>\<^sub>Ab c d e f a g. z a b c d e f g)"
"\<And>z. (\<exists>\<^sub>Aa b . z a b) = (\<exists>\<^sub>Ab a. z a b)"
"\<And>z. (\<exists>\<^sub>Aa b c d. z a b c d) = (\<exists>\<^sub>Ab c a d. z a b c d)"
apply(intro ent_iffI; sep_auto)+
done
lemma inst_same2: "(\<And>x. P = Q x) \<Longrightarrow> P = (\<exists>\<^sub>A x. Q x)"
by simp
lemma pure_eq_pre:
"(P \<Longrightarrow> Q = R) \<Longrightarrow> (Q * \<up>P = R * \<up>P)"
by fastforce
lemma otf_lem_comm_ex:
"\<And>a b c d e f g. (\<exists>\<^sub>A x. a * b x * c * d x * e x * f x * g x) = a * c * (\<exists>\<^sub>A x. b x * d x * e x * f x * g x)"
"\<And>a b c d e. (\<exists>\<^sub>Aaa x. a * b x * c * d aa * e aa) = (a * c * (\<exists>\<^sub>A aa x. b x * d aa * e aa))"
"\<And>b d e. (\<exists>\<^sub>A aa x. b x * d aa * e aa) = (\<exists>\<^sub>A x. b x) * (\<exists>\<^sub>A aa. d aa * e aa)"
by (auto simp add: algebra_simps)
declare last.simps[simp del] butlast.simps[simp del]
lemma bplustree_extract_leafs:
"bplustree_assn k t ti r z = (\<exists>\<^sub>Aleafptrs. bplustree_assn_leafs k t ti r z leafptrs)"
proof(induction arbitrary: r rule: bplustree_assn.induct )
case (1 k xs a r z)
then show ?case
(*apply auto*)
apply (rule ent_iffI)
subgoal
apply(inst_ex_assn "[a]")
apply sep_auto
done
subgoal
apply(rule ent_ex_preI)
apply clarsimp
apply(rule ent_ex_preI)+
subgoal for x xsi fwd
apply(inst_ex_assn xsi fwd)
apply simp
done
done
done
next
case Istep: (2 k ts t a r z)
show ?case
apply(simp (no_asm))
thm bplustree_assn_leafs.simps(2)
apply(subst reorder_ex(1))
apply(intro inst_same)
thm reorder_ex(2)
apply(subst reorder_ex(2))
apply(subst reorder_ex(3))
apply(rule inst_same)
(* pre-massage term for an explicit treatment. ignore inductive assumptions in simp s.t.
bplustree of the last tree does not get simplified away immediately *)
proof(goal_cases)
case (1 tsi ti tsi' rs)
have *: "
length tsi's = length tss \<Longrightarrow>
length tss = length rss \<Longrightarrow>
set tsi's \<subseteq> set tsi' \<Longrightarrow>
set rss \<subseteq> set rs \<Longrightarrow>
set tss \<subseteq> set ts \<Longrightarrow>
blist_assn k tss
(zip (zip (subtrees tsi's) (zip (butlast (ra # rss)) rss)) (separators tsi's)) =
(\<exists>\<^sub>Asplit. list_assn ((\<lambda> t (ti,r',z',lptrs). bplustree_assn_leafs k t (the ti) r' z' lptrs) \<times>\<^sub>a id_assn) tss
(zip (zip (subtrees tsi's) (zip (butlast (ra # rss)) (zip rss split))) (separators tsi's)) *
\<up>(length split = length rss))"
for rss tsi's tss ra
proof (induct arbitrary: ra rule: list_induct3)
case Nil
then show ?case
apply sep_auto
apply(subst ex_one_point_gen[where v="[]"])
apply simp_all
done
next
case (Cons subsepi tsi's subsep tss subleaf rss r)
then show ?case
apply (auto simp add: butlast_double_Cons last_double_Cons)
apply(auto simp add: prod_assn_def split: prod.splits)
proof(goal_cases)
case (1 sub sep)
then have *: "bplustree_assn k sub (the (fst subsepi)) r subleaf = (\<exists>\<^sub>As. bplustree_assn_leafs k sub (the (fst subsepi)) r subleaf s)"
proof -
have "subsep \<in> set ts"
by (simp add: "1"(10) "1"(8))
moreover obtain temp1 temp2 where "((fst subsepi, (temp1:: 'a btnode ref option), subleaf), (temp2::'a)) \<in> set [((fst subsepi, temp1, subleaf), temp2)]"
by auto
ultimately show ?thesis
using Istep(2)[of "(sub,sep)" "((fst subsepi, temp1, subleaf), temp2)" "[((fst subsepi, temp1, subleaf), temp2)]" "fst subsepi" "(temp1, subleaf)" temp1 subleaf r]
using 1
by simp
qed
show ?case
apply (simp add: * 1(3)[of subleaf])
apply(intro ent_iffI)
subgoal
apply(intro ent_ex_preI)
subgoal for split x
apply(inst_ex_assn "x#split")
apply simp
done
done
subgoal
apply(intro ent_ex_preI)
subgoal for split
apply(cases split)
apply simp
subgoal for hdsplit tlsplit
apply(inst_ex_assn "tlsplit" "hdsplit")
apply (auto)
done
done
done
done
qed
qed
have **: "bplustree_assn k t ti (last (r # rs)) z = (\<exists>\<^sub>Alsplit. bplustree_assn_leafs k t ti (last (r # rs)) z lsplit)"
using Istep(1)[of ti "last(r #rs)" "[]"]
by (auto simp add: last.simps)
show ?case
(* apply IH to last tree *)
apply(subst **)
apply(simp add: inst_same[OF bplustree_assn_leafs.simps(2)])
proof(intro ent_iffI, goal_cases)
case _: (1)
show ?case
(* apply IH to list via rule just shown *)
apply(rule entails_preI)
apply(intro ent_ex_preI)
apply(clarsimp dest!: mod_starD list_assn_len)
apply (subst *[of tsi' ts rs])
apply simp_all
(* show that the remainder is equivalent *)
apply(intro ent_ex_preI)
apply(rule entails_preI)
apply(clarsimp dest!: mod_starD list_assn_len)
subgoal for lsplit _ _ _ _ _ _ _ split
find_theorems "\<exists>\<^sub>A_._"
apply(inst_ex_assn "concat (split@[lsplit])" "zip (zip (subtrees tsi') (zip (butlast (r # rs)) (zip rs (butlast (split@[lsplit])))))
(separators tsi')" "split@[lsplit]")
apply (sep_auto simp add: last.simps butlast.simps)
done
done
next
case _: (2)
show ?case
(* apply IH to list via rule just shown (other direction) *)
apply(rule entails_preI)
apply(rule ent_ex_preI)
apply(rule ent_ex_preI)
apply(clarsimp dest!: mod_starD list_assn_len)
apply(subst merge_pure_star[symmetric] mult.left_assoc)+
apply(subst otf_lem_comm_ex)+
apply(rule ent_star_mono)
subgoal by sep_auto
proof(goal_cases)
case (1 c aa d ae bd af be ag bf)
have **: "(\<exists>\<^sub>Ax. bplustree_assn_leafs k t ti (last (r # rs)) z (last x) *
list_assn
((\<lambda>t (ti, r', x, y). bplustree_assn_leafs k t (the ti) r' x y) \<times>\<^sub>a id_assn)
ts aa *
\<up> (concat x = c) *
\<up> (length x = Suc (length rs)) *
\<up> (aa =
zip (zip (subtrees tsi') (zip (butlast (r # rs)) (zip rs (butlast x))))
(separators tsi'))) \<Longrightarrow>\<^sub>A ((\<exists>\<^sub>Asplit. list_assn ((\<lambda>t (ti, r', x, y). bplustree_assn_leafs k t (the ti) r' x y) \<times>\<^sub>a id_assn) ts (zip (zip (subtrees tsi') (zip (butlast (r # rs)) (zip rs split))) (separators tsi')) *
\<up> (length split = length rs)) * (\<exists>\<^sub>Alsplit. bplustree_assn_leafs k t ti (last (r # rs)) z lsplit)
)"
using 1 by sep_auto
from ** show ?case
apply(rule ent_trans)
apply(subst mult.commute[of "ex_assn (bplustree_assn_leafs k t ti (last (r # rs)) z)"])
apply(rule ent_star_mono)
prefer 2
subgoal by sep_auto
subgoal
apply(subst *[of tsi' ts rs r, symmetric])
apply(simp_all add: 1)
apply sep_auto
done
done
qed
qed
qed
qed
declare last.simps[simp add] butlast.simps[simp add]
(* even without the existential quantifier, we get our general assertion, used in insertion etc back*)
lemma bplustree_discard_leafs:
"bplustree_assn_leafs k t ti r z leafptrs \<Longrightarrow>\<^sub>A bplustree_assn k t ti r z"
by (simp add: bplustree_extract_leafs)
fun leaf_nodes_assn :: "nat \<Rightarrow> ('a::heap) bplustree list \<Rightarrow> 'a btnode ref option \<Rightarrow> 'a btnode ref option \<Rightarrow> 'a btnode ref list \<Rightarrow> assn" where
"leaf_nodes_assn k ((Leaf xs)#lns) (Some r) z (r'#lptrs) =
(\<exists>\<^sub>A xsi fwd.
r \<mapsto>\<^sub>r Btleaf xsi fwd
* is_pfa (2*k) xs xsi
* leaf_nodes_assn k lns fwd z lptrs
* \<up>(r = r')
)" |
"leaf_nodes_assn k [] r z [] = \<up>(r = z)" |
"leaf_nodes_assn _ _ _ _ _ = false"
fun trunk_assn :: "nat \<Rightarrow> ('a::heap) bplustree \<Rightarrow> 'a btnode ref \<Rightarrow> 'a btnode ref option \<Rightarrow> 'a btnode ref option \<Rightarrow> 'a btnode ref list \<Rightarrow> assn" where
"trunk_assn k (Leaf xs) a r z lptrs = \<up>(r = Some a \<and> lptrs = [a])" |
"trunk_assn k (Node ts t) a r z lptrs =
(\<exists>\<^sub>A tsi ti tsi' tsi'' rs split.
a \<mapsto>\<^sub>r Btnode tsi ti
* trunk_assn k t ti (last (r#rs)) (last (rs@[z])) (last split)
* is_pfa (2*k) tsi' tsi
* \<up>(concat split = lptrs)
* \<up>(length tsi' = length rs)
* \<up>(length split = length rs + 1)
* \<up>(tsi'' = zip (zip (map fst tsi') (zip (butlast (r#rs)) (zip (butlast (rs@[z])) (butlast split)))) (map snd tsi'))
* list_assn ((\<lambda> t (ti,r',z',lptrs). trunk_assn k t (the ti) r' z' lptrs) \<times>\<^sub>a id_assn) ts tsi''
)"
lemma leaf_nodes_assn_split:
"length xs = length xsi \<Longrightarrow> ysi = (yi#ysr) \<Longrightarrow>
leaf_nodes_assn k (xs @ ys) r z (xsi @ ysi) = leaf_nodes_assn k xs r (Some yi) xsi * leaf_nodes_assn k ys (Some yi) z ysi"
proof(induction arbitrary: r rule: list_induct2)
case (Nil r)
then show ?case
apply(cases r; cases ys)
apply clarsimp_all
subgoal for _ t _
apply(cases t)
apply clarsimp
apply(intro inst_same)
apply(rule pure_eq_pre)
apply clarsimp_all
done
done
next
case (Cons x xs xi xsi r)
show ?case
apply(cases r; cases x)
apply clarsimp_all
apply(intro inst_same)
apply(rule pure_eq_pre)
subgoal for a x1 xsi' fwd
using Cons.IH[of fwd, OF Cons.prems]
apply (clarsimp simp add: mult.assoc)
done
done
qed
lemma "length xs \<noteq> length xsi \<Longrightarrow> leaf_nodes_assn k xs r z xsi = false"
by (induction rule: leaf_nodes_assn.induct) auto
lemma imp_eq_pure: "(\<forall>h. h \<Turnstile> P \<longrightarrow> Q) = (P = P * \<up>(Q))"
apply(intro iffI)
subgoal using ent_iffI by force
subgoal by (metis mod_pure_star_dist)
done
lemma imp_imp_pure: "(\<And>h. h \<Turnstile> P \<Longrightarrow> Q) \<Longrightarrow> (P = P * \<up>(Q))"
using imp_eq_pure by blast
thm concat_append
lemma concat_append_butlast: "xs \<noteq> [] \<Longrightarrow> concat (butlast xs) @ last xs = concat xs"
apply(induction xs)
apply auto
done
declare last.simps[simp del] butlast.simps[simp del]
lemma bplustree_assn_leafs_len_imp: "h \<Turnstile> bplustree_assn_leafs k t a r z leafptrs \<Longrightarrow> length leafptrs = length (leaf_nodes t)"
proof(induction k t a r z leafptrs arbitrary: h rule: bplustree_assn_leafs.induct)
case (1 k xs a r z leafptrs)
then show ?case
by(clarsimp)
next
case (2 k ts t a r z leafptrs h)
from "2.prems" show ?case
apply(sep_auto)
proof(goal_cases)
case (1 tsia tsin ti tsi' rs split)
have *: "
length tss = length splits \<Longrightarrow>
length splits = length tsi's \<Longrightarrow>
length tsi's = length rss \<Longrightarrow>
set tss \<subseteq> set ts \<Longrightarrow>
set tsi's \<subseteq> set tsi' \<Longrightarrow>
set rss \<subseteq> set rs \<Longrightarrow>
h \<Turnstile> list_assn
((\<lambda>t (ti, r', x, y). bplustree_assn_leafs k t (the ti) r' x y) \<times>\<^sub>a id_assn) tss
(zip (zip (subtrees tsi's) (zip (butlast (ra # rss)) (zip rss splits))) (separators tsi's))
\<Longrightarrow> (length (concat splits)) = length (concat (map (leaf_nodes \<circ> fst) tss))" for h tss tsi's splits rss ra
proof(induction arbitrary: ra h rule: list_induct4)
case Nil
then show ?case
by sep_auto
next
case (Cons x xs y ys z zs w ws)
from Cons.prems show ?case
apply (auto simp add: butlast_double_Cons last_double_Cons)
apply(auto simp add: prod_assn_def split: prod.splits)
apply(auto dest!: mod_starD)
using Cons.IH
apply(auto)
using "2.IH"(2)
apply sep_auto
by (meson list.set_intros(1))
qed
have **: "length ts = length rs" "split \<noteq> []"
using 1 by (auto dest!: mod_starD list_assn_len)
from 1 show ?case
apply(auto dest!: mod_starD)
apply(subst concat_append_butlast[symmetric])
subgoal using ** by sep_auto
subgoal for h1 h2 h3 h4 h5 h6 h7 h8
using *[of ts "butlast split" tsi' rs r "(h1,h2)"] "2.IH"(1)[of ti rs split "(h7,h8)"]
using **
by sep_auto
done
qed
qed
declare last.simps[simp add] butlast.simps[simp add]
lemma bplustree_assn_leafs_len_aux: "bplustree_assn_leafs k t a r z leafptrs = bplustree_assn_leafs k t a r z leafptrs * \<up>(length leafptrs = length (leaf_nodes t))"
by (meson bplustree_assn_leafs_len_imp imp_imp_pure)
declare last.simps[simp del] butlast.simps[simp del]
lemma trunk_assn_leafs_len_imp: "h \<Turnstile> trunk_assn k t a r z leafptrs \<Longrightarrow> length leafptrs = length (leaf_nodes t)"
(* same procedure as for bplustree_nodes_assn_leaf *)
proof(induction k t a r z leafptrs arbitrary: h rule: trunk_assn.induct)
case (1 k xs a r z leafptrs)
then show ?case
by(clarsimp)
next
case (2 k ts t a r z leafptrs h)
from "2.prems" show ?case
apply(sep_auto)
proof(goal_cases)
case (1 tsia tsin ti tsi' rs split)
have *: "
length tss = length splits \<Longrightarrow>
length splits = length tsi's \<Longrightarrow>
length tsi's = length rss \<Longrightarrow>
set tss \<subseteq> set ts \<Longrightarrow>
set tsi's \<subseteq> set tsi' \<Longrightarrow>
set rss \<subseteq> set rs \<Longrightarrow>
h \<Turnstile> list_assn
((\<lambda>t (ti, r', x, y). trunk_assn k t (the ti) r' x y) \<times>\<^sub>a id_assn) tss
(zip (zip (subtrees tsi's) (zip (butlast (ra # rss)) (zip rss splits))) (separators tsi's))
\<Longrightarrow> (length (concat splits)) = length (concat (map (leaf_nodes \<circ> fst) tss))" for h tss tsi's splits rss ra
proof(induction arbitrary: ra h rule: list_induct4)
case Nil
then show ?case
by sep_auto
next
case (Cons x xs y ys z zs w ws)
from Cons.prems show ?case
apply (auto simp add: butlast_double_Cons last_double_Cons)
apply(auto simp add: prod_assn_def split: prod.splits)
apply(auto dest!: mod_starD)
using Cons.IH
apply(auto)
using "2.IH"(2)
apply sep_auto
by (meson list.set_intros(1))
qed
have **: "length ts = length rs" "split \<noteq> []"
using 1 by (auto dest!: mod_starD list_assn_len)
from 1 show ?case
apply(auto dest!: mod_starD)
apply(subst concat_append_butlast[symmetric])
subgoal using ** by sep_auto
subgoal for h1 h2 h3 h4 h5 h6 h7 h8
using *[of ts "butlast split" tsi' rs r "(h1,h2)"] "2.IH"(1)[of ti rs split "(h7,h8)"]
using **
by sep_auto
done
qed
qed
declare last.simps[simp add] butlast.simps[simp add]
lemma trunk_assn_leafs_len_aux: "trunk_assn k t a r z leafptrs = trunk_assn k t a r z leafptrs * \<up>(length leafptrs = length (leaf_nodes t))"
by (meson trunk_assn_leafs_len_imp imp_imp_pure)
declare last.simps[simp del] butlast.simps[simp del]
lemma bplustree_assn_leafs_not_empty_aux: "bplustree_assn_leafs k t a r z leafptrs = bplustree_assn_leafs k t a r z leafptrs * \<up>(leafptrs \<noteq> [])"
apply(intro ent_iffI)
subgoal
apply(subst bplustree_assn_leafs_len_aux)
using leaf_nodes_not_empty
apply sep_auto
done
subgoal by sep_auto
done
lemma trunk_assn_not_empty_aux: "trunk_assn k t a r z leafptrs = trunk_assn k t a r z leafptrs * \<up>(leafptrs \<noteq> [])"
apply(intro ent_iffI)
subgoal
apply(subst trunk_assn_leafs_len_aux)
using leaf_nodes_not_empty
apply sep_auto
done
subgoal by sep_auto
done
declare last.simps[simp add] butlast.simps[simp add]
declare last.simps[simp del] butlast.simps[simp del]
lemma bplustree_assn_leafs_hd:
"h \<Turnstile> bplustree_assn_leafs k t a r z leafptrs \<Longrightarrow> r = Some (hd leafptrs)"
proof(induction k t a r z leafptrs arbitrary: h rule: bplustree_assn_leafs.induct)
case (1 k xs a r z leafptrs)
then show ?case
by(clarsimp)
next
case (2 k ts t a r z leafptrs h)
from "2.prems" show ?case
apply(sep_auto dest!: mod_starD)
proof(goal_cases)
case (1 a b ti tsi' rs split ab bb ad bd ae be af bf)
have "length ts = length rs"
using 1 by (auto dest!: list_assn_len)
then show ?case
proof(cases ts)
case Nil
then have "length split = 1" "rs = []"
using "1"(4) \<open>length ts = length rs\<close> by auto
then have *: "split = [last split]"
by (metis append_butlast_last_id list.distinct(1) list_decomp_1 list_se_match(4))
then have "concat split = last split"
apply(subst *)
unfolding concat.simps
by simp
then show ?thesis
using 1
using "2.IH"(1)[of ti rs split "(af,bf)"]
using \<open>rs = []\<close>
by (auto simp add: last.simps)
next
case (Cons a list)
then obtain ra rss ss1 ss2 splits tss tsi's where *:
"rs = ra#rss"
"split = ss1 # ss2 # splits"
"tsi' = tss # tsi's"
by (metis (no_types, lifting) "1"(3) "1"(4) Suc_length_conv \<open>length ts = length rs\<close>)
obtain h1 h2 where first_subtree: "(h1, h2) \<Turnstile> bplustree_assn_leafs k (fst a) (the (fst tss)) r ra ss1"
using 1
apply (auto simp add: butlast_double_Cons last_double_Cons * Cons)
apply (auto simp add: prod_assn_def split: prod.splits )
apply(auto dest!: mod_starD)
done
then have "ss1 \<noteq> []"
using bplustree_assn_leafs_not_empty_aux[of k "(fst a)" "(the (fst tss))" r ra ss1]
by auto
then have "hd (concat split) = hd ss1"
by (simp add: "*"(2))
then show ?thesis
using first_subtree
apply auto
by (metis "2.IH"(2) fst_conv list.set_intros(1) local.Cons)
qed
qed
qed
declare last.simps[simp add] butlast.simps[simp add]
lemma bplustree_assn_leafs_hd_aux:
"bplustree_assn_leafs k t a r z leafptrs = bplustree_assn_leafs k t a r z leafptrs * \<up>(r = Some (hd leafptrs))"
by (meson bplustree_assn_leafs_hd imp_imp_pure)
declare last.simps[simp del] butlast.simps[simp del]
lemma trunk_assn_hd:
"h \<Turnstile> trunk_assn k t a r z leafptrs \<Longrightarrow> r = Some (hd leafptrs)"
proof(induction k t a r z leafptrs arbitrary: h rule: trunk_assn.induct)
case (1 k xs a r z leafptrs)
then show ?case
by(clarsimp)
next
case (2 k ts t a r z leafptrs h)
from "2.prems" show ?case
apply(sep_auto dest!: mod_starD)
proof(goal_cases)
case (1 a b ti tsi' rs split ab bb ad bd ae be af bf)
have "length ts = length rs"
using 1 by (auto dest!: list_assn_len)
then show ?case
proof(cases ts)
case Nil
then have "length split = 1" "rs = []"
using "1"(4) \<open>length ts = length rs\<close> by auto
then have *: "split = [last split]"
by (metis append_butlast_last_id list.distinct(1) list_decomp_1 list_se_match(4))
then have "concat split = last split"
apply(subst *)
unfolding concat.simps
by simp
then show ?thesis
using 1
using "2.IH"(1)[of ti rs split "(af,bf)"]
using \<open>rs = []\<close>
by (auto simp add: last.simps)
next
case (Cons a list)
then obtain ra rss ss1 ss2 splits tss tsi's where *:
"rs = ra#rss"
"split = ss1 # ss2 # splits"
"tsi' = tss # tsi's"
by (metis (no_types, lifting) "1"(3) "1"(4) Suc_length_conv \<open>length ts = length rs\<close>)
obtain h1 h2 where first_subtree: "(h1, h2) \<Turnstile> trunk_assn k (fst a) (the (fst tss)) r ra ss1"
using 1
apply (auto simp add: butlast_double_Cons last_double_Cons * Cons)
apply (auto simp add: prod_assn_def split: prod.splits )
apply(auto dest!: mod_starD)
done
then have "ss1 \<noteq> []"
using trunk_assn_not_empty_aux[of k "(fst a)" "(the (fst tss))" r ra ss1]
by auto
then have "hd (concat split) = hd ss1"
by (simp add: "*"(2))
then show ?thesis
using first_subtree
apply auto
by (metis "2.IH"(2) fst_conv list.set_intros(1) local.Cons)
qed
qed
qed
declare last.simps[simp add] butlast.simps[simp add]
lemma trunk_assn_hd_aux:
"trunk_assn k t a r z leafptrs = trunk_assn k t a r z leafptrs * \<up>(r = Some (hd leafptrs))"
by (simp add: imp_imp_pure trunk_assn_hd)
declare last.simps[simp del] butlast.simps[simp del]
lemma subleaf_at_head_of_concat_inner: "length tsi's = length rss \<Longrightarrow>
length rss = length tss \<Longrightarrow>
length tss = length splits \<Longrightarrow>
list_assn ((\<lambda>t (ti, x, xa, y). trunk_assn k t (the ti) x xa y) \<times>\<^sub>a R) tss
(zip (zip (subtrees tsi's) (zip (butlast (subleaf # rss)) (zip rss splits)))
(separators tsi's)) *
trunk_assn k t ti (last (subleaf # rss)) z ss
=
list_assn ((\<lambda>t (ti, x, xa, y). trunk_assn k t (the ti) x xa y) \<times>\<^sub>a R) tss
(zip (zip (subtrees tsi's) (zip (butlast (subleaf # rss)) (zip rss splits)))
(separators tsi's)) *
trunk_assn k t ti (last (subleaf # rss)) z ss * \<up>(Some (hd (concat splits@ss)) = subleaf)"
apply(cases splits)
subgoal
apply (sep_auto simp add: last.simps)
apply (metis (mono_tags, opaque_lifting) trunk_assn_hd_aux pure_assn_eq_conv)
done
subgoal
apply(cases tss; cases rss; cases tsi's)
apply simp_all
apply (sep_auto
simp add: butlast_double_Cons last_double_Cons)
apply(intro ent_iffI)
subgoal
apply(subst trunk_assn_hd_aux)
apply(subst trunk_assn_not_empty_aux)
apply sep_auto
done
subgoal by sep_auto
done
done
lemma subleaf_at_head_of_concat_bplustree: "length tsi's = length rss \<Longrightarrow>
length rss = length tss \<Longrightarrow>
length tss = length splits \<Longrightarrow>
list_assn ((\<lambda>t (ti, x, xa, y). bplustree_assn_leafs k t (the ti) x xa y) \<times>\<^sub>a R) tss
(zip (zip (subtrees tsi's) (zip (butlast (subleaf # rss)) (zip rss splits)))
(separators tsi's)) *
bplustree_assn_leafs k t ti (last (subleaf # rss)) z ss
=
list_assn ((\<lambda>t (ti, x, xa, y). bplustree_assn_leafs k t (the ti) x xa y) \<times>\<^sub>a R) tss
(zip (zip (subtrees tsi's) (zip (butlast (subleaf # rss)) (zip rss splits)))
(separators tsi's)) *
bplustree_assn_leafs k t ti (last (subleaf # rss)) z ss * \<up>(Some (hd (concat splits@ss)) = subleaf)"
apply(cases splits)
subgoal
apply (sep_auto simp add: last.simps)
apply (metis (mono_tags, opaque_lifting) bplustree_assn_leafs_hd_aux pure_assn_eq_conv)
done
subgoal
apply(cases tss; cases rss; cases tsi's)
apply simp_all
apply (sep_auto
simp add: butlast_double_Cons last_double_Cons)
apply(intro ent_iffI)
subgoal
apply(subst bplustree_assn_leafs_hd_aux)
apply(subst bplustree_assn_leafs_not_empty_aux)
apply sep_auto
done
subgoal by sep_auto
done
done
declare last.simps[simp add] butlast.simps[simp add]
declare last.simps[simp del] butlast.simps[simp del]
lemma bplustree_leaf_nodes_sep:
"bplustree_assn_leafs k t ti r z lptrs = leaf_nodes_assn k (leaf_nodes t) r z lptrs * trunk_assn k t ti r z lptrs"
proof(induction arbitrary: r rule: bplustree_assn_leafs.induct)
case (1 k xs a r z)
then show ?case
apply(intro ent_iffI)
apply sep_auto+
done
next
case (2 k ts t a r z lptrs ra)
show ?case
apply simp
apply(intro inst_same)
apply (clarsimp simp add: mult.left_assoc)
apply(intro pure_eq_pre)
apply(clarsimp)
proof(goal_cases)
case (1 tsia tsin ti tsi' rs split)
have *: "
length tsi's = length rss \<Longrightarrow>
length rss = length tss \<Longrightarrow>
length tss = length splits \<Longrightarrow>
set tsi's \<subseteq> set tsi' \<Longrightarrow>
set rss \<subseteq> set rs \<Longrightarrow>
set tss \<subseteq> set ts \<Longrightarrow>
set splits \<subseteq> set split \<Longrightarrow>
bplustree_assn_leafs k t ti (last (ra # rss)) z (last split)*
list_assn ((\<lambda>t (ti, x, y, s). bplustree_assn_leafs k t (the ti) x y s) \<times>\<^sub>a id_assn) tss
(zip (zip (subtrees tsi's) (zip (butlast (ra # rss)) (zip rss splits))) (separators tsi's)) =
leaf_nodes_assn k (concat (map (leaf_nodes \<circ> fst) tss) @ leaf_nodes t) ra z (concat splits @ last split) *
list_assn ((\<lambda>t (ti, x, y, s). trunk_assn k t (the ti) x y s) \<times>\<^sub>a id_assn) tss
(zip (zip (subtrees tsi's) (zip (butlast (ra # rss)) (zip rss splits))) (separators tsi's)) *
trunk_assn k t ti (last (ra#rss)) z (last split)"
for rss tsi's tss splits
proof (induct arbitrary: ra rule: list_induct4)
case (Nil r)
then show ?case
apply(clarsimp)
using 2(1)[of ti r "[]" "split"]
apply (simp add: last.simps)
done
next
case (Cons subsepi tsi's subleaf rss subsep tss fsplit splits r)
show ?case
apply (sep_auto
simp add: butlast_double_Cons last_double_Cons)
apply(subst prod_assn_def)+
apply(simp split!: prod.splits add: mult.left_assoc)
subgoal for sub sep
(* extract fact that length of leaf nodes of subleaf matches leaf_nodes_assn_split req *)
apply(subst bplustree_assn_leafs_len_aux[of k sub])
apply(subst trunk_assn_leafs_len_aux[of k sub])
apply sep_auto
apply(intro pure_eq_pre)
(* extract fact that the remaining list is not empty *)
apply(subst bplustree_assn_leafs_not_empty_aux[of k t])
apply(subst trunk_assn_not_empty_aux[of k t])
apply sep_auto
apply(intro pure_eq_pre)
supply R = leaf_nodes_assn_split[of "leaf_nodes sub" fsplit
"concat splits @ last split" "hd (concat splits @ last split)" "tl (concat splits @ last split)"]
thm R
apply(subst R)
subgoal by simp
subgoal by simp
(* show that r = hd fsplit *)
apply(subst bplustree_assn_leafs_hd_aux[of k sub])
apply(subst trunk_assn_hd_aux[of k sub])
apply sep_auto
apply(intro pure_eq_pre)
(* refactor multiplication s.t. we can apply the lemma about two mult. factors with an OTF lemma *)
supply R = subleaf_at_head_of_concat_inner[of tsi's rss tss splits k id_assn subleaf t ti z "last split"]
thm R
apply (subst_mod_mult_ac R)
subgoal using Cons by simp
subgoal using Cons by simp
subgoal using Cons by simp
apply(simp add: mult.left_assoc)?
(* refactor multiplication s.t. we can apply the lemma about two mult. factors with an OTF lemma *)
supply R=subleaf_at_head_of_concat_bplustree[of tsi's rss tss splits k id_assn subleaf t ti z "last split"]
thm R
apply (subst_mod_mult_ac R)
subgoal using Cons by simp
subgoal using Cons by simp
subgoal using Cons by simp
apply(simp add: mult.left_assoc)?
apply(intro pure_eq_pre)
proof(goal_cases)
case 1
moreover have p: "set tsi's \<subseteq> set tsi'"
"set rss \<subseteq> set rs"
"set tss \<subseteq> set ts"
"set splits \<subseteq> set split"
using Cons.prems by auto
moreover have "(sub,sep) \<in> set ts"
using "1" Cons.prems(3) by force
moreover obtain temp1 temp2 where "((fst subsepi, (temp1:: 'a btnode ref option), subleaf, fsplit), (temp2::'a)) \<in> set [((fst subsepi, temp1, subleaf, fsplit), temp2)]"
by auto
ultimately show ?case
apply(inst_ex_assn subleaf)
using "Cons.hyps"(4)[of subleaf, OF p, simplified]
apply (auto simp add: algebra_simps)
using "2.IH"(2)[of subsep "((fst subsepi, temp1, subleaf, fsplit),temp2)" "[((fst subsepi, temp1, subleaf, fsplit),temp2)]"
"fst subsepi" "(temp1, subleaf, fsplit)" temp1 "(subleaf, fsplit)" subleaf fsplit r, simplified]
apply auto
using assn_times_assoc ent_refl by presburger
qed
done
qed
show ?case
apply(intro ent_iffI)
subgoal
apply(rule entails_preI)
using 1
apply(auto dest!: mod_starD list_assn_len)
apply(subst_mod_mult_ac *[of tsi' rs ts "butlast split", simplified])
subgoal by auto
subgoal by auto
subgoal by auto
subgoal by (meson in_set_butlastD subset_code(1))
subgoal
apply(subgoal_tac "concat (butlast split) @ (last split) = concat split")
prefer 2
subgoal
apply(subst concat_append_butlast)
apply auto
done
subgoal by sep_auto
done
done
subgoal
apply(rule entails_preI)
using 1
apply(auto dest!: mod_starD list_assn_len)
apply(subgoal_tac "concat split = concat (butlast split) @ (last split)")
prefer 2
subgoal
apply(subst concat_append_butlast)
apply auto
done
apply simp
apply(subst_mod_mult_ac *[of tsi' rs ts "butlast split", simplified, symmetric])
subgoal by auto
subgoal by auto
subgoal by auto
subgoal by (meson in_set_butlastD subset_code(1))
subgoal by sep_auto
done
done
qed
qed
declare last.simps[simp add] butlast.simps[simp add]
fun leaf_node:: "('a::heap) bplustree \<Rightarrow> 'a list \<Rightarrow> assn" where
"leaf_node (Leaf xs) xsi = \<up>(xs = xsi)" |
"leaf_node _ _ = false"
fun leafs_assn :: "('a::heap) pfarray list \<Rightarrow> 'a btnode ref list \<Rightarrow> 'a btnode ref option \<Rightarrow> 'a btnode ref option \<Rightarrow> assn" where
"leafs_assn (ln#lns) (r'#lptrs) (Some r) z =
(\<exists>\<^sub>A fwd.
r \<mapsto>\<^sub>r Btleaf ln fwd
* leafs_assn lns lptrs fwd z
* \<up>(r' = r)
)" |
"leafs_assn [] [] r z = \<up>(r = z)" |
"leafs_assn _ _ _ _ = false"
lemma leafs_assn_aux_append:
"length xs = length xsi \<Longrightarrow> leafs_assn (xs@ys) (xsi@ysi) r z = (\<exists>\<^sub>Al. leafs_assn xs xsi r l * leafs_assn ys ysi l z)"
apply(induction xs xsi r z rule: leafs_assn.induct)
apply(sep_auto intro!: ent_iffI)+
done
abbreviation "leaf_lists \<equiv> \<lambda>t. map leaves (leaf_nodes t)"
lemma leaf_nodes_assn_flatten_help:
"length ts = length lptrs \<Longrightarrow> leaf_nodes_assn k ts r z lptrs = (\<exists>\<^sub>Aps. list_assn leaf_node ts (map leaves ts) * list_assn (is_pfa (2*k)) (map leaves ts) ps * leafs_assn ps lptrs r z)"
proof (induction ts lptrs arbitrary: r rule: list_induct2)
case Nil
then show ?case
apply(intro ent_iffI)
subgoal by sep_auto
subgoal by sep_auto
done
next
case (Cons a xs r' lptrs r)
then show ?case
proof(intro ent_iffI, goal_cases)
case 1
show ?case
apply(cases r; cases a)
apply simp_all
find_theorems "\<exists>\<^sub>A_._ \<Longrightarrow>\<^sub>A_"
apply(rule ent_ex_preI)+
subgoal for aa x1 xsi fwd
apply (subst "Cons.IH"[of fwd])
apply simp
apply(rule ent_ex_preI)+
subgoal for ps
apply(inst_ex_assn "xsi#ps")
apply simp_all
apply(inst_ex_assn fwd)
apply (sep_auto)
done
done
done
next
case 2
have *: "list_assn leaf_node xs (map leaves xs) * list_assn (is_pfa (2 * k)) (map leaves xs) ps' * leafs_assn ps' lptrs r'' z
\<Longrightarrow>\<^sub>A leaf_nodes_assn k xs r'' z lptrs"
for ps' r''
using assn_eq_split(1)[OF sym[OF "Cons.IH"[of r'']]]
ent_ex_inst[where Q="leaf_nodes_assn k xs r'' z lptrs" and y=ps']
by blast
show ?case
apply(rule ent_ex_preI)+
subgoal for ps
apply(cases ps; cases r; cases a)
apply simp_all
apply(rule ent_ex_preI)+
subgoal for aa list aaa x1 fwd
apply(inst_ex_assn aa fwd)
apply sep_auto
using *[of list fwd]
by (smt (z3) assn_aci(9) assn_times_comm fr_refl)
done
done
qed
qed
lemma leaf_nodes_assn_impl_length: "h \<Turnstile> leaf_nodes_assn k xs r z lptrs \<Longrightarrow> length xs = length lptrs"
apply(induction xs arbitrary: h r lptrs)
subgoal for h r lptrs
apply(cases r; cases lptrs)
apply sep_auto+
done
subgoal for a xs h r lptrs
apply(cases r; cases lptrs; cases a)
apply (sep_auto dest: mod_starD)+
done
done
lemma leafs_assn_impl_length: "h \<Turnstile> leafs_assn xs lptrs r z \<Longrightarrow> length xs = length lptrs"
apply(induction xs arbitrary: h r lptrs)
subgoal for h r lptrs
apply(cases r; cases lptrs)
apply sep_auto+
done
subgoal for a xs h r lptrs
apply(cases r; cases lptrs)
apply (sep_auto dest: mod_starD)+
done
done
lemma leaf_nodes_assn_flatten:
"leaf_nodes_assn k ts r z lptrs = (\<exists>\<^sub>Aps. list_assn leaf_node ts (map leaves ts) * list_assn (is_pfa (2*k)) (map leaves ts) ps * leafs_assn ps lptrs r z)"
proof(intro ent_iffI, goal_cases)
case 1
then show ?case
apply(rule entails_preI)
apply (subst leaf_nodes_assn_flatten_help)
subgoal by (sep_auto dest!: mod_starD leaf_nodes_assn_impl_length)
subgoal by sep_auto
done
next
case 2
then show ?case
apply(rule entails_preI)
apply (subst leaf_nodes_assn_flatten_help)
subgoal by (sep_auto dest!: mod_starD leafs_assn_impl_length list_assn_len)
subgoal by sep_auto
done
qed
subsection "Iterator"
partial_function (heap) first_leaf :: "('a::heap) btnode ref \<Rightarrow> 'a btnode ref option Heap"
where
"first_leaf p = do {
node \<leftarrow> !p;
(case node of
Btleaf _ _ \<Rightarrow> do { return (Some p) } |
Btnode tsi ti \<Rightarrow> do {
s \<leftarrow> pfa_get tsi 0;
let (sub,sep) = s in do {
first_leaf (the sub)
}
}
)}"
partial_function (heap) last_leaf :: "('a::heap) btnode ref \<Rightarrow> 'a btnode ref option Heap"
where
"last_leaf p = do {
node \<leftarrow> !p;
(case node of
Btleaf _ z \<Rightarrow> do { return z } |
Btnode tsi ti \<Rightarrow> do {
last_leaf ti
}
)}"
declare last.simps[simp del] butlast.simps[simp del]
lemma first_leaf_rule[sep_heap_rules]:
assumes "k > 0" "root_order k t"
shows "<bplustree_assn k t ti r z>
first_leaf ti
<\<lambda>u. bplustree_assn k t ti r z * \<up>(u = r)>\<^sub>t"
using assms
proof(induction t arbitrary: ti z)
case (Leaf x)
then show ?case
apply(subst first_leaf.simps)
apply (sep_auto dest!: mod_starD)
done
next
case (Node ts t)
then obtain sub sep tts where Cons: "ts = (sub,sep)#tts"
apply(cases ts) by auto
then show ?case
apply(subst first_leaf.simps)
apply (sep_auto simp add: butlast.simps)
subgoal for tsia tsil ti tsi' rs subi sepi
apply(cases rs; cases tsi')
apply simp_all
subgoal for subleaf rrs _ ttsi'
supply R = "Node.IH"(1)[of "(sub,sep)" sub "(the subi)" subleaf]
thm R
using "Node.prems"(1)
apply (sep_auto heap add: R)
subgoal by (metis Node.prems(2) assms(1) bplustree.inject(2) bplustree.simps(4) Cons list.set_intros(1) order_impl_root_order root_order.elims(2) some_child_sub(1))
apply (sep_auto eintros del: exI)
apply(inst_existentials tsia tsil ti "(subi, sepi) # ttsi'" "((subi, (r, subleaf)),sepi)#(zip (zip (subtrees ttsi') (zip (butlast (subleaf # rrs)) rrs)) (separators ttsi'))" "subleaf # rrs")
apply (sep_auto simp add: last.simps butlast.simps)+
done
done
done
qed
declare last.simps[simp add] butlast.simps[simp add]
declare last.simps[simp del] butlast.simps[simp del]
lemma last_leaf_rule[sep_heap_rules]:
assumes "k > 0" "root_order k t"
shows "<bplustree_assn k t ti r z>
last_leaf ti
<\<lambda>u. bplustree_assn k t ti r z * \<up>(u = z)>\<^sub>t"
using assms
proof(induction t arbitrary: ti r)
case (Leaf x)
then show ?case
apply(subst last_leaf.simps)
apply (sep_auto dest!: mod_starD)
done