-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbst_spec.v
644 lines (588 loc) · 15.9 KB
/
bst_spec.v
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
Require Import VST.floyd.proofauto.
Require Import Coq.Program.Equality.
Require Import my_tactics.
Inductive bin_tree (A: Type) : Type :=
| leaf : bin_tree A
| node : A -> bin_tree A -> bin_tree A -> bin_tree A.
Arguments bin_tree _%type_scope.
Arguments leaf {A}%type_scope.
Arguments node {A}%type_scope.
Inductive bin_tree_forall {A} : bin_tree A -> (A -> Prop) -> Prop :=
| bin_tree_forall_leaf : forall p, bin_tree_forall leaf p
| bin_tree_forall_node : forall (p: A -> Prop) a l r,
p a -> bin_tree_forall l p -> bin_tree_forall r p -> bin_tree_forall (node a l r) p.
Theorem bin_tree_forall_strengthen {A} : forall (p q: A -> Prop) b,
(forall x, p x -> q x) -> bin_tree_forall b p -> bin_tree_forall b q.
Proof.
intros.
dependent induction H0; subst; constructor; simplify_assumps.
- apply H. assumption.
- assumption.
- assumption.
Qed.
Inductive well_ordered {A} : bin_tree (Z * A) -> Prop :=
| well_ordered_leaf : well_ordered leaf
| well_ordered_node : forall k v l r,
bin_tree_forall l (fun n => fst n < k) ->
bin_tree_forall r (fun n => fst n > k) ->
well_ordered l ->
well_ordered r ->
well_ordered (node (k, v) l r).
Definition bst A := bin_tree (Z * A).
(* Subtrees *)
Inductive is_subtree {A} : bin_tree A -> bin_tree A -> Prop :=
| is_subtree_refl : forall b, is_subtree b b
| is_subtree_left : forall b n l r, is_subtree b l -> is_subtree b (node n l r)
| is_subtree_right : forall b n l r, is_subtree b r -> is_subtree b (node n l r).
(* Definition proper_subtree {A} (b1 b2: bin_tree A) := b1 <> b2 /\ is_subtree b1 b2. *)
Lemma is_subtree_leaf : forall {A} (b: bin_tree A), is_subtree leaf b.
Proof.
intros A b.
induction b; constructor; assumption.
Qed.
Theorem subtree_forall : forall {A} sub b (p: A -> Prop),
is_subtree sub b -> bin_tree_forall b p -> bin_tree_forall sub p.
Proof.
intros.
induction H;
try (apply IHis_subtree; inv H0);
assumption.
Qed.
Theorem subtree_well_ordered : forall {A} (b sub: bst A),
is_subtree sub b -> well_ordered b -> well_ordered sub.
Proof.
intros.
induction b.
- destruct sub.
+ constructor.
+ inv H.
- inv H.
+ assumption.
+ inv H0; apply IHb1; assumption.
+ inv H0; apply IHb2; assumption.
Qed.
Theorem is_subtree_up_left {A}: forall (a: A) l r b,
is_subtree (node a l r) b -> is_subtree l b.
Proof.
intros.
dependent induction H.
- apply is_subtree_left. constructor.
- apply is_subtree_left.
eapply IHis_subtree.
reflexivity.
- apply is_subtree_right.
eapply IHis_subtree.
reflexivity.
Qed.
Theorem is_subtree_up_right {A}: forall (a: A) l r b,
is_subtree (node a l r) b -> is_subtree r b.
Proof.
intros.
dependent induction H.
- apply is_subtree_right. constructor.
- apply is_subtree_left.
eapply IHis_subtree.
reflexivity.
- apply is_subtree_right.
eapply IHis_subtree.
reflexivity.
Qed.
Lemma no_infinite_node_left {A} : forall (a: A) l r,
node a l r <> l.
Proof.
unfold not.
intros.
induction l.
- discriminate.
- apply IHl1.
inv H.
rewrite H2 at 1.
reflexivity.
Qed.
Lemma no_infinite_node_right {A} : forall (a: A) l r,
node a l r <> r.
Proof.
unfold not.
intros.
induction r.
- discriminate.
- apply IHr2.
inv H.
rewrite H3 at 1.
reflexivity.
Qed.
Fixpoint depth {A} (b: bin_tree A) : nat :=
match b with
| leaf => 0
| node a l r => S (max (depth l) (depth r))
end.
Theorem subtree_depth_monotonic {A} : forall (b1 b2: bin_tree A),
is_subtree b1 b2 -> (depth b1 <= depth b2)%nat.
Proof.
intros.
dependent induction H; simpl; lia.
Qed.
Theorem no_subtree_cycle_left {A} : forall (a: A) l r,
not (is_subtree (node a l r) l).
Proof.
unfold not.
intros.
apply subtree_depth_monotonic in H.
simpl in H.
lia.
Qed.
Theorem no_subtree_cycle_right {A} : forall (a: A) l r,
not (is_subtree (node a l r) r).
Proof.
unfold not.
intros.
apply subtree_depth_monotonic in H.
simpl in H.
lia.
Qed.
Lemma wo_node_eq {A} : forall k (v1 v2: A) l1 l2 r1 r2,
well_ordered (node (k,v1) l1 r1) ->
is_subtree (node (k,v2) l2 r2) (node (k,v1) l1 r1) ->
node (k,v1) l1 r1 = node (k,v2) l2 r2.
Proof.
intros.
inv H0.
- reflexivity.
- inv H.
eapply subtree_forall in H3; [|eassumption].
inv H3.
simpl in H5. lia.
- inv H.
eapply subtree_forall in H3; [|eassumption].
inv H3.
simpl in H5. lia.
Qed.
Lemma wo_subtree_left {A} : forall k1 k2 (v1 v2: A) l1 l2 r1 r2,
well_ordered (node (k1,v1) l1 r1) ->
is_subtree (node (k2,v2) l2 r2) (node (k1,v1) l1 r1) ->
k2 < k1 ->
is_subtree (node (k2,v2) l2 r2) l1.
Proof.
intros.
inv H0.
- lia.
- assumption.
- inv H.
eapply subtree_forall in H4; [|eassumption].
inv H4.
simpl in H6. lia.
Qed.
Lemma wo_subtree_right {A} : forall k1 k2 (v1 v2: A) l1 l2 r1 r2,
well_ordered (node (k1,v1) l1 r1) ->
is_subtree (node (k2,v2) l2 r2) (node (k1,v1) l1 r1) ->
k2 > k1 ->
is_subtree (node (k2,v2) l2 r2) r1.
Proof.
intros.
inv H0.
- lia.
- inv H.
eapply subtree_forall in H4; [|eassumption].
inv H4.
simpl in H6. lia.
- assumption.
Qed.
(* Search *)
Fixpoint search {A} (k: Z) (b: bst A) : bst A :=
match b with
| leaf => leaf
| node (k', v) l r =>
match k ?= k' with
| Eq => b
| Lt => search k l
| Gt => search k r
end
end.
Theorem search_spec {A} : forall (b: bst A) k v l r,
well_ordered b ->
is_subtree (node (k,v) l r) b ->
search k b = node (k,v) l r.
Proof.
intros.
induction b.
- inv H0.
- inv H.
simplify_assumps.
simpl.
find_Z_compare_destruct.
+ eapply wo_node_eq; assumption.
+ apply IHb1.
eapply wo_subtree_left; eassumption.
+ apply IHb2.
eapply wo_subtree_right; eassumption.
Qed.
Theorem search_is_subtree : forall {A} k (b sub: bst A),
search k b = sub -> is_subtree sub b.
Proof.
intros A k b sub.
induction b; try destruct_pair; simpl; intro H.
- subst. constructor.
- find_Z_compare_destruct.
+ constructor.
+ apply is_subtree_left.
apply IHb1.
assumption.
+ apply is_subtree_right.
apply IHb2.
assumption.
Qed.
Lemma search_well_ordered : forall {A} k (b sub: bst A),
search k b = sub -> well_ordered b -> well_ordered sub.
Proof.
intros.
eapply subtree_well_ordered.
eapply search_is_subtree.
eassumption.
assumption.
Qed.
(* Insert *)
Fixpoint insert {A} (k: Z) (v: A) (b: bst A) : bst A :=
match b with
| leaf => node (k, v) leaf leaf
| node (k', v') l r =>
match k ?= k' with
| Eq => node (k', v) l r
| Lt => node (k', v') (insert k v l) r
| Gt => node (k', v') l (insert k v r)
end
end.
Lemma insert_forall {A} : forall (p: (Z * A) -> Prop) k v bst,
p (k, v) -> bin_tree_forall bst p -> bin_tree_forall (insert k v bst) p.
Proof.
intros p k v b H.
induction b; intro H0; simpl.
- constructor; assumption.
- destruct_pair. find_Z_compare_destruct;
inv H0; constructor;
try (apply IHb1 + apply IHb2);
assumption.
Qed.
Theorem insert_well_ordered {A}: forall (b: bst A) k v,
well_ordered b -> well_ordered (insert k v b).
Proof.
intro b; induction b; intros k v H; simpl.
- repeat constructor.
- inv H.
find_Z_compare_destruct;
constructor; try (apply insert_forall + apply IHb1 + apply IHb2); assumption.
Qed.
Theorem search_insert {A}: forall k v (b: bst A),
exists l r, search k (insert k v b) = node (k, v) l r.
Proof.
intros k v b.
induction b.
- simpl.
rewrite Z.compare_refl.
unify_goal.
- simpl. destruct_pair.
find_Z_compare_destruct;
simpl; rewrite Heqc; assumption + unify_goal.
Qed.
(* Delete *)
Fixpoint get_min {A} (a: Z * A) (l r: bst A) : Z * A :=
match l with
| leaf => a
| node a' l' r' => get_min a' l' r'
end.
Fixpoint rm_min {A} (a: Z * A) (l r: bst A) : bst A :=
match l with
| leaf => r
| node a' l' r' => node a (rm_min a' l' r') r
end.
Lemma get_min_forall {A} : forall (p: (Z * A) -> Prop) a l r,
bin_tree_forall (node a l r) p -> p (get_min a l r).
Proof.
intros p a l; revert p a.
induction l; intros; simpl.
- inv H; assumption.
- apply IHl1.
inv H; assumption.
Qed.
Lemma rm_min_forall {A} : forall (p: (Z * A) -> Prop) a l r,
bin_tree_forall (node a l r) p -> bin_tree_forall (rm_min a l r) p.
Proof.
intros p a l; revert p a.
induction l; intros; simpl.
- inv H; assumption.
- constructor.
+ inv H; assumption.
+ apply IHl1. inv H; assumption.
+ inv H; assumption.
Qed.
Lemma get_min_is_min {A} : forall (a: Z * A) l r,
well_ordered (node a l r) ->
bin_tree_forall (rm_min a l r) (fun n => fst n > fst (get_min a l r)).
Proof.
intros a l; revert a.
induction l; intros; simpl.
- inv H; assumption.
- constructor.
+ inv H.
apply get_min_forall in H3.
simpl. lia.
+ apply IHl1. inv H; assumption.
+ inv H.
eapply bin_tree_forall_strengthen; [|eassumption].
intros [x].
simpl.
enough (k > fst (get_min a l1 l2)); [lia|].
apply get_min_forall in H3.
lia.
Qed.
Lemma get_min_is_min_2 {A} : forall (a: Z * A) l r,
well_ordered (node a l r) ->
bin_tree_forall (node a l r) (fun n => fst n >= fst (get_min a l r)).
Proof.
intros a l; revert a.
induction l; intros; simpl.
- inv H.
constructor.
+ lia.
+ constructor.
+ simpl.
eapply bin_tree_forall_strengthen; [|eassumption].
intros [x].
lia.
- inv H.
constructor.
+ specialize (IHl1 a l2 H5).
inv IHl1.
simpl.
inv H3.
lia.
+ apply IHl1.
assumption.
+ eapply bin_tree_forall_strengthen; [|eassumption].
intros [x].
simpl.
enough (k > fst (get_min a l1 l2)); [lia|].
apply get_min_forall in H3.
lia.
Qed.
Theorem rm_min_well_ordered {A} : forall (a: Z * A) l r,
well_ordered (node a l r) -> well_ordered (rm_min a l r).
Proof.
intros a l; revert a.
induction l; intros; simpl.
- inv H; assumption.
- destruct a0; constructor.
+ apply rm_min_forall. inv H; assumption.
+ inv H; assumption.
+ apply IHl1. inv H; assumption.
+ inv H; assumption.
Qed.
Fixpoint delete {A} (k: Z) (b: bst A) : bst A :=
match b with
| leaf => leaf
| node (k', v) l r =>
match k ?= k' with
| Eq => match l with
| leaf => r
| _ => match r with
| leaf => leaf
| node ra rl rr => node (get_min ra rl rr) l (rm_min ra rl rr)
end
end
| Lt => delete k l
| Gt => delete k r
end
end.
Lemma delete_forall {A} : forall (p: (Z * A) -> Prop) k bst,
bin_tree_forall bst p -> bin_tree_forall (delete k bst) p.
Proof.
intros p k b.
induction b as [|[k' v] l IH1 r IH2]; intro H; simpl; [assumption|].
find_Z_compare_destruct.
- destruct l, r; try (inv H; assumption).
constructor.
+ apply get_min_forall.
inv H; assumption.
+ inv H; assumption.
+ apply rm_min_forall.
inv H; assumption.
- apply IH1. inv H; assumption.
- apply IH2. inv H; assumption.
Qed.
Theorem delete_well_ordered {A}: forall (b: bst A) k,
well_ordered b -> well_ordered (delete k b).
Proof.
intros b; induction b as [|[k' v] l IH1 r IH2]; intros k H; simpl; [constructor|].
find_Z_compare_destruct.
- destruct l, r; try (inv H; assumption).
destruct (get_min p0 r1 r2) eqn:H_min.
constructor.
+ inv H; subst.
eapply bin_tree_forall_strengthen; [|eassumption].
intros [x].
simpl.
enough (k' < z); [lia|].
apply get_min_forall in H5.
rewrite H_min in H5; simpl in H5.
lia.
+ replace z with (fst (get_min p0 r1 r2)) by (rewrite H_min; reflexivity).
apply get_min_is_min.
inv H; assumption.
+ inv H; assumption.
+ apply rm_min_well_ordered.
inv H; assumption.
- apply IH1. inv H; assumption.
- apply IH2. inv H; assumption.
Qed.
(* search_path: This describes a point in a key-search traversal, commonly used to express
loop invariants *)
(* The direction of transitivity here is nice for induction, but doesn't correspond
to the intuitive direction in which a path grows. *)
Inductive search_path : Z -> bst Z -> bst Z -> Prop :=
| path_refl : forall k b, search_path k b b
| path_step_l : forall k k' v l r b,
search_path k l b -> k < k' -> search_path k (node (k',v) l r) b
| path_step_r : forall k k' v l r b,
search_path k r b -> k > k' -> search_path k (node (k',v) l r) b.
(* This is the direction a path will actually grow in *)
Lemma path_step_down_l : forall k head k' v l r,
search_path k head (node (k',v) l r) -> k < k' -> search_path k head l.
Proof.
intros. dependent induction H.
- apply path_step_l.
+ constructor.
+ assumption.
- apply path_step_l.
+ eapply IHsearch_path; trivial.
+ assumption.
- apply path_step_r.
+ eapply IHsearch_path; trivial.
+ assumption.
Qed.
Lemma path_step_down_r : forall k head k' v l r,
search_path k head (node (k',v) l r) -> k > k' -> search_path k head r.
Proof.
intros. dependent induction H.
- apply path_step_r.
+ constructor.
+ assumption.
- apply path_step_l.
+ eapply IHsearch_path; trivial.
+ assumption.
- apply path_step_r.
+ eapply IHsearch_path; trivial.
+ assumption.
Qed.
Theorem search_path_fail : forall k b, search_path k b leaf -> search k b = leaf.
Proof.
intros. dependent induction H.
- reflexivity.
- simpl.
rewrite Zaux.Zcompare_Lt by assumption.
apply IHsearch_path.
reflexivity.
- simpl.
rewrite Zaux.Zcompare_Gt by lia.
apply IHsearch_path.
reflexivity.
Qed.
Theorem search_path_success: forall k b v l r,
search_path k b (node (k,v) l r) -> search k b = (node (k,v) l r).
Proof.
intros. dependent induction H.
- simpl.
rewrite Z.compare_refl.
reflexivity.
- simpl.
rewrite Zaux.Zcompare_Lt by assumption.
apply IHsearch_path.
reflexivity.
- simpl.
rewrite Zaux.Zcompare_Gt by lia.
apply IHsearch_path.
reflexivity.
Qed.
Lemma search_path_constant : forall k b1 b2,
search_path k b1 b2 -> search k b1 = search k b2.
Proof.
intros k b1 b2 H.
dependent induction H; simpl.
- reflexivity.
- rewrite Zaux.Zcompare_Lt; assumption.
- rewrite Zaux.Zcompare_Gt by lia. assumption.
Qed.
Theorem search_path_is_subtree: forall k b sub, search_path k b sub -> is_subtree sub b.
Proof.
intros.
induction H.
- constructor.
- apply is_subtree_left. assumption.
- apply is_subtree_right. assumption.
Qed.
Theorem path_shrink_l : forall k_search k k' v v' l l' r r',
well_ordered (node (k,v) l r) ->
k' < k ->
search_path k_search (node (k,v) l r) (node (k',v') l' r') ->
search_path k_search l (node (k',v') l' r').
Proof.
intros.
inv H1.
- lia.
- assumption.
- apply search_path_is_subtree in H8.
inv H.
eapply subtree_forall in H8; [|eassumption].
inv H8.
simpl in *.
lia.
Qed.
Theorem path_shrink_r : forall k_search k k' v v' l l' r r',
well_ordered (node (k,v) l r) ->
k < k' ->
search_path k_search (node (k,v) l r) (node (k',v') l' r') ->
search_path k_search r (node (k',v') l' r').
Proof.
intros.
inv H1.
- lia.
- apply search_path_is_subtree in H8.
inv H.
eapply subtree_forall in H8; [|eassumption].
inv H8.
simpl in *.
lia.
- assumption.
Qed.
Theorem path_extract_ord_l : forall k_search k k' v v' l l' r r',
well_ordered (node (k,v) l r) ->
k' < k ->
search_path k_search (node (k,v) l r) (node (k',v') l' r') ->
k_search < k.
Proof.
intros.
inv H1.
- lia.
- assumption.
- apply search_path_is_subtree in H8.
inv H.
eapply subtree_forall in H8; [|eassumption].
inv H8.
simpl in *.
lia.
Qed.
Theorem path_extract_ord_r : forall k_search k k' v v' l l' r r',
well_ordered (node (k,v) l r) ->
k < k' ->
search_path k_search (node (k,v) l r) (node (k',v') l' r') ->
k < k_search.
Proof.
intros.
inv H1.
- lia.
- apply search_path_is_subtree in H8.
inv H.
eapply subtree_forall in H8; [|eassumption].
inv H8.
simpl in *.
lia.
- lia.
Qed.