~ubuntu-branches/ubuntu/wily/coq-doc/wily

« back to all changes in this revision

Viewing changes to theories/FSets/FSetFullAVL.v

  • Committer: Bazaar Package Importer
  • Author(s): Stéphane Glondu, Stéphane Glondu, Samuel Mimram
  • Date: 2010-01-07 22:50:39 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100107225039-n3cq82589u0qt0s2
Tags: 8.2pl1-1
[ Stéphane Glondu ]
* New upstream release (Closes: #563669)
  - remove patches
* Packaging overhaul:
  - use git, advertise it in Vcs-* fields of debian/control
  - use debhelper 7 and dh with override
  - use source format 3.0 (quilt)
* debian/control:
  - set Maintainer to d-o-m, set Uploaders to Sam and myself
  - add Homepage field
  - bump Standards-Version to 3.8.3
* Register PDF documentation into doc-base
* Add debian/watch
* Update debian/copyright

[ Samuel Mimram ]
* Change coq-doc's description to mention that it provides documentation in
  pdf format, not postscript, closes: #543545.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(***********************************************************************)
 
2
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team    *)
 
3
(* <O___,, *        INRIA-Rocquencourt  &  LRI-CNRS-Orsay              *)
 
4
(*   \VV/  *************************************************************)
 
5
(*    //   *      This file is distributed under the terms of the      *)
 
6
(*         *       GNU Lesser General Public License Version 2.1       *)
 
7
(***********************************************************************)
 
8
 
 
9
(* Finite sets library.  
 
10
 * Authors: Pierre Letouzey and Jean-Christophe Filliâtre 
 
11
 * Institution: LRI, CNRS UMR 8623 - Université Paris Sud
 
12
 *              91405 Orsay, France *)
 
13
 
 
14
(* $Id: FSetFullAVL.v 11699 2008-12-18 11:49:08Z letouzey $ *)
 
15
 
 
16
(** * FSetFullAVL
 
17
   
 
18
   This file contains some complements to [FSetAVL].
 
19
 
 
20
   - Functor [AvlProofs] proves that trees of [FSetAVL] are not only 
 
21
   binary search trees, but moreover well-balanced ones. This is done
 
22
   by proving that all operations preserve the balancing.
 
23
 
 
24
   - Functor [OcamlOps] contains variants of [union], [subset], 
 
25
   [compare] and [equal] that are faithful to the original ocaml codes,
 
26
   while the versions in FSetAVL have been adapted to perform only
 
27
   structural recursive code. 
 
28
  
 
29
   - Finally, we pack the previous elements in a [Make] functor 
 
30
   similar to the one of [FSetAVL], but richer.
 
31
*)
 
32
 
 
33
Require Import Recdef FSetInterface FSetList ZArith Int FSetAVL.
 
34
 
 
35
Set Implicit Arguments.
 
36
Unset Strict Implicit.
 
37
 
 
38
Module AvlProofs (Import I:Int)(X:OrderedType).
 
39
Module Import Raw := Raw I X.
 
40
Import Raw.Proofs.
 
41
Module Import II := MoreInt I.
 
42
Open Local Scope pair_scope.
 
43
Open Local Scope Int_scope.
 
44
 
 
45
(** * AVL trees *)
 
46
 
 
47
(** [avl s] : [s] is a properly balanced AVL tree,
 
48
    i.e. for any node the heights of the two children
 
49
    differ by at most 2 *)
 
50
 
 
51
Inductive avl : tree -> Prop :=
 
52
  | RBLeaf : avl Leaf
 
53
  | RBNode : forall x l r h, avl l -> avl r ->
 
54
      -(2) <= height l - height r <= 2 ->
 
55
      h = max (height l) (height r) + 1 -> 
 
56
      avl (Node l x r h).
 
57
 
 
58
(** * Automation and dedicated tactics *)
 
59
 
 
60
Hint Constructors avl.
 
61
 
 
62
(** A tactic for cleaning hypothesis after use of functional induction. *)
 
63
 
 
64
Ltac clearf :=
 
65
 match goal with 
 
66
  | H : (@Logic.eq (Compare _ _ _ _) _ _) |- _ => clear H; clearf
 
67
  | H : (@Logic.eq (sumbool _ _) _ _) |- _ => clear H; clearf
 
68
  | _ => idtac
 
69
 end.
 
70
 
 
71
(** Tactics about [avl] *)
 
72
 
 
73
Lemma height_non_negative : forall s : tree, avl s -> height s >= 0.
 
74
Proof.
 
75
 induction s; simpl; intros; auto with zarith.
 
76
 inv avl; intuition; omega_max.
 
77
Qed.
 
78
Implicit Arguments height_non_negative. 
 
79
 
 
80
(** When [H:avl r], typing [avl_nn H] or [avl_nn r] adds [height r>=0] *)
 
81
 
 
82
Ltac avl_nn_hyp H := 
 
83
     let nz := fresh "nz" in assert (nz := height_non_negative H).
 
84
 
 
85
Ltac avl_nn h := 
 
86
  let t := type of h in 
 
87
  match type of t with 
 
88
   | Prop => avl_nn_hyp h
 
89
   | _ => match goal with H : avl h |- _ => avl_nn_hyp H end
 
90
  end.
 
91
 
 
92
(* Repeat the previous tactic. 
 
93
   Drawback: need to clear the [avl _] hyps ... Thank you Ltac *)
 
94
 
 
95
Ltac avl_nns :=
 
96
  match goal with 
 
97
     | H:avl _ |- _ => avl_nn_hyp H; clear H; avl_nns
 
98
     | _ => idtac
 
99
  end.
 
100
 
 
101
(** Results about [height] *)
 
102
 
 
103
Lemma height_0 : forall s, avl s -> height s = 0 -> s = Leaf.
 
104
Proof.
 
105
 destruct 1; intuition; simpl in *.
 
106
 avl_nns; simpl in *; elimtype False; omega_max.
 
107
Qed.
 
108
 
 
109
(** * Results about [avl] *)
 
110
 
 
111
Lemma avl_node : 
 
112
 forall x l r, avl l -> avl r ->
 
113
 -(2) <= height l - height r <= 2 ->
 
114
 avl (Node l x r (max (height l) (height r) + 1)).
 
115
Proof.
 
116
  intros; auto.
 
117
Qed.
 
118
Hint Resolve avl_node.
 
119
 
 
120
 
 
121
(** empty *)
 
122
 
 
123
Lemma empty_avl : avl empty.
 
124
Proof. 
 
125
 auto.
 
126
Qed.
 
127
 
 
128
(** singleton *)
 
129
 
 
130
Lemma singleton_avl : forall x : elt, avl (singleton x).
 
131
Proof.
 
132
 unfold singleton; intro.
 
133
 constructor; auto; try red; simpl; omega_max.
 
134
Qed.
 
135
 
 
136
(** create *)
 
137
 
 
138
Lemma create_avl : 
 
139
 forall l x r, avl l -> avl r ->  -(2) <= height l - height r <= 2 -> 
 
140
 avl (create l x r).
 
141
Proof.
 
142
 unfold create; auto.
 
143
Qed.
 
144
 
 
145
Lemma create_height : 
 
146
 forall l x r, avl l -> avl r ->  -(2) <= height l - height r <= 2 -> 
 
147
 height (create l x r) = max (height l) (height r) + 1.
 
148
Proof.
 
149
 unfold create; auto.
 
150
Qed.
 
151
 
 
152
(** bal *)
 
153
 
 
154
Lemma bal_avl : forall l x r, avl l -> avl r -> 
 
155
 -(3) <= height l - height r <= 3 -> avl (bal l x r).
 
156
Proof.
 
157
 intros l x r; functional induction bal l x r; intros; clearf;
 
158
 inv avl; simpl in *; 
 
159
 match goal with |- avl (assert_false _ _ _) => avl_nns
 
160
  | _ => repeat apply create_avl; simpl in *; auto
 
161
 end; omega_max.
 
162
Qed.
 
163
 
 
164
Lemma bal_height_1 : forall l x r, avl l -> avl r -> 
 
165
 -(3) <= height l - height r <= 3 ->
 
166
 0 <= height (bal l x r) - max (height l) (height r) <= 1.
 
167
Proof.
 
168
 intros l x r; functional induction bal l x r; intros; clearf;
 
169
 inv avl; avl_nns; simpl in *; omega_max.
 
170
Qed.
 
171
 
 
172
Lemma bal_height_2 : 
 
173
 forall l x r, avl l -> avl r -> -(2) <= height l - height r <= 2 -> 
 
174
 height (bal l x r) == max (height l) (height r) +1.
 
175
Proof.
 
176
 intros l x r; functional induction bal l x r; intros; clearf;
 
177
 inv avl; simpl in *; omega_max.
 
178
Qed.
 
179
 
 
180
Ltac omega_bal := match goal with 
 
181
  | H:avl ?l, H':avl ?r |- context [ bal ?l ?x ?r ] => 
 
182
     generalize (bal_height_1 x H H') (bal_height_2 x H H'); 
 
183
     omega_max
 
184
  end.
 
185
 
 
186
(** add *)
 
187
 
 
188
Lemma add_avl_1 :  forall s x, avl s -> 
 
189
 avl (add x s) /\ 0 <= height (add x s) - height s <= 1.
 
190
Proof. 
 
191
 intros s x; functional induction (add x s); subst;intros; inv avl; simpl in *.
 
192
 intuition; try constructor; simpl; auto; try omega_max.
 
193
 (* LT *)
 
194
 destruct IHt; auto.
 
195
 split.
 
196
 apply bal_avl; auto; omega_max.
 
197
 omega_bal.
 
198
 (* EQ *)
 
199
 intuition; omega_max.
 
200
 (* GT *)
 
201
 destruct IHt; auto.
 
202
 split.
 
203
 apply bal_avl; auto; omega_max.
 
204
 omega_bal.
 
205
Qed.
 
206
 
 
207
Lemma add_avl : forall s x, avl s -> avl (add x s).
 
208
Proof.
 
209
 intros; destruct (add_avl_1 x H); auto.
 
210
Qed.
 
211
Hint Resolve add_avl.
 
212
 
 
213
(** join *)
 
214
 
 
215
Lemma join_avl_1 : forall l x r, avl l -> avl r -> avl (join l x r) /\
 
216
 0<= height (join l x r) - max (height l) (height r) <= 1.
 
217
Proof. 
 
218
 join_tac.
 
219
 
 
220
 split; simpl; auto. 
 
221
 destruct (add_avl_1 x H0).
 
222
 avl_nns; omega_max.
 
223
 set (l:=Node ll lx lr lh) in *.
 
224
 split; auto.
 
225
 destruct (add_avl_1 x H).
 
226
 simpl (height Leaf).
 
227
 avl_nns; omega_max.
 
228
 
 
229
 inversion_clear H.
 
230
 assert (height (Node rl rx rr rh) = rh); auto.
 
231
 set (r := Node rl rx rr rh) in *; clearbody r.
 
232
 destruct (Hlr x r H2 H0); clear Hrl Hlr.
 
233
 set (j := join lr x r) in *; clearbody j.
 
234
 simpl.
 
235
 assert (-(3) <= height ll - height j <= 3) by omega_max.
 
236
 split.
 
237
 apply bal_avl; auto.
 
238
 omega_bal.
 
239
 
 
240
 inversion_clear H0.
 
241
 assert (height (Node ll lx lr lh) = lh); auto.
 
242
 set (l := Node ll lx lr lh) in *; clearbody l.
 
243
 destruct (Hrl H H1); clear Hrl Hlr.
 
244
 set (j := join l x rl) in *; clearbody j.
 
245
 simpl.
 
246
 assert (-(3) <= height j - height rr <= 3) by omega_max.
 
247
 split.
 
248
 apply bal_avl; auto.
 
249
 omega_bal.
 
250
 
 
251
 clear Hrl Hlr.
 
252
 assert (height (Node ll lx lr lh) = lh); auto.
 
253
 assert (height (Node rl rx rr rh) = rh); auto.
 
254
 set (l := Node ll lx lr lh) in *; clearbody l.
 
255
 set (r := Node rl rx rr rh) in *; clearbody r.
 
256
 assert (-(2) <= height l - height r <= 2) by omega_max.
 
257
 split.
 
258
 apply create_avl; auto.
 
259
 rewrite create_height; auto; omega_max.
 
260
Qed.
 
261
 
 
262
Lemma join_avl : forall l x r, avl l -> avl r -> avl (join l x r).
 
263
Proof.
 
264
 intros; destruct (join_avl_1 x H H0); auto.
 
265
Qed.
 
266
Hint Resolve join_avl.
 
267
 
 
268
(** remove_min *)
 
269
 
 
270
Lemma remove_min_avl_1 : forall l x r h, avl (Node l x r h) -> 
 
271
 avl (remove_min l x r)#1 /\ 
 
272
 0 <= height (Node l x r h) - height (remove_min l x r)#1 <= 1.
 
273
Proof.
 
274
 intros l x r; functional induction (remove_min l x r); subst;simpl in *; intros.
 
275
 inv avl; simpl in *; split; auto.
 
276
 avl_nns; omega_max.
 
277
 inversion_clear H.
 
278
 rewrite e0 in IHp;simpl in IHp;destruct (IHp _x); auto.
 
279
 split; simpl in *. 
 
280
 apply bal_avl; auto; omega_max.
 
281
 omega_bal.
 
282
Qed.
 
283
 
 
284
Lemma remove_min_avl : forall l x r h, avl (Node l x r h) -> 
 
285
    avl (remove_min l x r)#1. 
 
286
Proof.
 
287
 intros; destruct (remove_min_avl_1 H); auto.
 
288
Qed.
 
289
 
 
290
(** merge *)
 
291
 
 
292
Lemma merge_avl_1 : forall s1 s2, avl s1 -> avl s2 -> 
 
293
 -(2) <= height s1 - height s2 <= 2 -> 
 
294
 avl (merge s1 s2) /\ 
 
295
 0<= height (merge s1 s2) - max (height s1) (height s2) <=1.
 
296
Proof.
 
297
 intros s1 s2; functional induction (merge s1 s2); intros; 
 
298
 try factornode _x _x0 _x1 _x2 as s1.
 
299
 simpl; split; auto; avl_nns; omega_max.
 
300
 simpl; split; auto; avl_nns; simpl in *; omega_max.
 
301
 generalize (remove_min_avl_1 H0).
 
302
 rewrite e1; destruct 1.
 
303
 split.
 
304
 apply bal_avl; auto.
 
305
 simpl; omega_max.
 
306
 simpl in *; omega_bal.
 
307
Qed.
 
308
 
 
309
Lemma merge_avl : forall s1 s2, avl s1 -> avl s2 -> 
 
310
  -(2) <= height s1 - height s2 <= 2 -> avl (merge s1 s2).
 
311
Proof. 
 
312
 intros; destruct (merge_avl_1 H H0 H1); auto.
 
313
Qed.
 
314
 
 
315
 
 
316
(** remove *)
 
317
 
 
318
Lemma remove_avl_1 : forall s x, avl s -> 
 
319
 avl (remove x s) /\ 0 <= height s - height (remove x s) <= 1.
 
320
Proof.
 
321
 intros s x; functional induction (remove x s); intros.
 
322
 intuition; omega_max.
 
323
 (* LT *)
 
324
 inv avl.
 
325
 destruct (IHt H0).
 
326
 split. 
 
327
 apply bal_avl; auto.
 
328
 omega_max.
 
329
 omega_bal.
 
330
 (* EQ *)
 
331
 inv avl. 
 
332
 generalize (merge_avl_1 H0 H1 H2).
 
333
 intuition omega_max.
 
334
 (* GT *)
 
335
 inv avl.
 
336
 destruct (IHt H1).
 
337
 split. 
 
338
 apply bal_avl; auto.
 
339
 omega_max.
 
340
 omega_bal.
 
341
Qed.
 
342
 
 
343
Lemma remove_avl : forall s x, avl s -> avl (remove x s).
 
344
Proof. 
 
345
 intros; destruct (remove_avl_1 x H); auto.
 
346
Qed.
 
347
Hint Resolve remove_avl.
 
348
 
 
349
(** concat *)
 
350
 
 
351
Lemma concat_avl : forall s1 s2, avl s1 -> avl s2 -> avl (concat s1 s2).
 
352
Proof.
 
353
 intros s1 s2; functional induction (concat s1 s2); auto.
 
354
 intros; apply join_avl; auto.
 
355
 generalize (remove_min_avl H0); rewrite e1; simpl; auto.
 
356
Qed.
 
357
Hint Resolve concat_avl.
 
358
 
 
359
(** split *)
 
360
 
 
361
Lemma split_avl : forall s x, avl s -> 
 
362
  avl (split x s)#l /\ avl (split x s)#r.
 
363
Proof. 
 
364
 intros s x; functional induction (split x s); simpl; auto.
 
365
 rewrite e1 in IHt;simpl in IHt;inversion_clear 1; intuition.
 
366
 simpl; inversion_clear 1; auto.
 
367
 rewrite e1 in IHt;simpl in IHt;inversion_clear 1; intuition.
 
368
Qed.
 
369
 
 
370
(** inter *)
 
371
 
 
372
Lemma inter_avl : forall s1 s2, avl s1 -> avl s2 -> avl (inter s1 s2). 
 
373
Proof.
 
374
 intros s1 s2; functional induction inter s1 s2; auto; intros A1 A2;
 
375
 generalize (split_avl x1 A2); rewrite e1; simpl; destruct 1; 
 
376
 inv avl; auto.
 
377
Qed.
 
378
 
 
379
(** diff *)
 
380
 
 
381
Lemma diff_avl : forall s1 s2, avl s1 -> avl s2 -> avl (diff s1 s2). 
 
382
Proof. 
 
383
 intros s1 s2; functional induction diff s1 s2; auto; intros A1 A2;
 
384
 generalize (split_avl x1 A2); rewrite e1; simpl; destruct 1; 
 
385
 inv avl; auto.
 
386
Qed.
 
387
 
 
388
(** union *)
 
389
 
 
390
Lemma union_avl : forall s1 s2, avl s1 -> avl s2 -> avl (union s1 s2).
 
391
Proof.
 
392
 intros s1 s2; functional induction union s1 s2; auto; intros A1 A2;
 
393
 generalize (split_avl x1 A2); rewrite e1; simpl; destruct 1; 
 
394
 inv avl; auto.
 
395
Qed.
 
396
 
 
397
(** filter *)
 
398
 
 
399
Lemma filter_acc_avl : forall f s acc, avl s -> avl acc -> 
 
400
 avl (filter_acc f acc s).
 
401
Proof.
 
402
 induction s; simpl; auto.
 
403
 intros.
 
404
 inv avl.
 
405
 destruct (f t); auto.
 
406
Qed. 
 
407
Hint Resolve filter_acc_avl.
 
408
 
 
409
Lemma filter_avl : forall f s, avl s -> avl (filter f s). 
 
410
Proof.
 
411
 unfold filter; intros; apply filter_acc_avl; auto.
 
412
Qed.
 
413
 
 
414
(** partition *)
 
415
 
 
416
Lemma partition_acc_avl_1 : forall f s acc, avl s -> 
 
417
 avl acc#1 -> avl (partition_acc f acc s)#1.
 
418
Proof.
 
419
 induction s; simpl; auto.
 
420
 destruct acc as [acct accf]; simpl in *.
 
421
 intros.
 
422
 inv avl.
 
423
 apply IHs2; auto.
 
424
 apply IHs1; auto.
 
425
 destruct (f t); simpl; auto.
 
426
Qed.
 
427
 
 
428
Lemma partition_acc_avl_2 : forall f s acc, avl s -> 
 
429
 avl acc#2 -> avl (partition_acc f acc s)#2.
 
430
Proof.
 
431
 induction s; simpl; auto.
 
432
 destruct acc as [acct accf]; simpl in *.
 
433
 intros.
 
434
 inv avl.
 
435
 apply IHs2; auto.
 
436
 apply IHs1; auto.
 
437
 destruct (f t); simpl; auto.
 
438
Qed. 
 
439
 
 
440
Lemma partition_avl_1 : forall f s, avl s -> avl (partition f s)#1. 
 
441
Proof.
 
442
 unfold partition; intros; apply partition_acc_avl_1; auto.
 
443
Qed.
 
444
 
 
445
Lemma partition_avl_2 : forall f s, avl s -> avl (partition f s)#2. 
 
446
Proof.
 
447
 unfold partition; intros; apply partition_acc_avl_2; auto.
 
448
Qed.
 
449
 
 
450
End AvlProofs.
 
451
 
 
452
 
 
453
Module OcamlOps (Import I:Int)(X:OrderedType).
 
454
Module Import AvlProofs := AvlProofs I X.
 
455
Import Raw.
 
456
Import Raw.Proofs.
 
457
Import II.
 
458
Open Local Scope pair_scope.
 
459
Open Local Scope nat_scope.
 
460
 
 
461
(** Properties of cardinal *)
 
462
 
 
463
Lemma bal_cardinal : forall l x r, 
 
464
 cardinal (bal l x r) = S (cardinal l + cardinal r).
 
465
Proof.
 
466
 intros l x r; functional induction bal l x r; intros; clearf;
 
467
 simpl; auto with arith; romega with *.
 
468
Qed.
 
469
 
 
470
Lemma add_cardinal : forall x s, 
 
471
 cardinal (add x s) <= S (cardinal s).
 
472
Proof.
 
473
 intros; functional induction add x s; simpl; auto with arith; 
 
474
 rewrite bal_cardinal; romega with *.
 
475
Qed.
 
476
 
 
477
Lemma join_cardinal : forall l x r, 
 
478
 cardinal (join l x r) <= S (cardinal l + cardinal r).
 
479
Proof.
 
480
 join_tac; auto with arith.
 
481
 simpl; apply add_cardinal.
 
482
 simpl; destruct X.compare; simpl; auto with arith.
 
483
 generalize (bal_cardinal (add x ll) lx lr) (add_cardinal x ll); 
 
484
  romega with *.
 
485
 generalize (bal_cardinal ll lx (add x lr)) (add_cardinal x lr); 
 
486
  romega with *.
 
487
 generalize (bal_cardinal ll lx (join lr x (Node rl rx rr rh)))
 
488
  (Hlr x (Node rl rx rr rh)); simpl; romega with *.
 
489
 simpl S in *; generalize (bal_cardinal (join (Node ll lx lr lh) x rl) rx rr).
 
490
 romega with *.
 
491
Qed.
 
492
 
 
493
Lemma split_cardinal_1 : forall x s, 
 
494
 (cardinal (split x s)#l <= cardinal s)%nat.
 
495
Proof.
 
496
 intros x s; functional induction split x s; simpl; auto.
 
497
 rewrite e1 in IHt; simpl in *.
 
498
 romega with *.
 
499
 romega with *.
 
500
 rewrite e1 in IHt; simpl in *.
 
501
 generalize (@join_cardinal l y rl); romega with *.
 
502
Qed.
 
503
 
 
504
Lemma split_cardinal_2 : forall x s, 
 
505
 (cardinal (split x s)#r <= cardinal s)%nat.
 
506
Proof.
 
507
 intros x s; functional induction split x s; simpl; auto.
 
508
 rewrite e1 in IHt; simpl in *.
 
509
 generalize (@join_cardinal rl y r); romega with *.
 
510
 romega with *.
 
511
 rewrite e1 in IHt; simpl in *; romega with *.
 
512
Qed.
 
513
 
 
514
(** * [ocaml_union], an union faithful to the original ocaml code *)
 
515
 
 
516
Definition cardinal2 (s:t*t) := (cardinal s#1 + cardinal s#2)%nat.
 
517
 
 
518
Ltac ocaml_union_tac := 
 
519
 intros; unfold cardinal2; simpl fst in *; simpl snd in *;
 
520
 match goal with H: split ?x ?s = _ |- _ => 
 
521
  generalize (split_cardinal_1 x s) (split_cardinal_2 x s); 
 
522
  rewrite H; simpl; romega with *
 
523
 end.
 
524
 
 
525
Import Logic. (* Unhide eq, otherwise Function complains. *)
 
526
 
 
527
Function ocaml_union (s : t * t) { measure cardinal2 s } : t  :=
 
528
 match s with 
 
529
  | (Leaf, Leaf) => s#2
 
530
  | (Leaf, Node _ _ _ _) => s#2
 
531
  | (Node _ _ _ _, Leaf) => s#1
 
532
  | (Node l1 x1 r1 h1, Node l2 x2 r2 h2) => 
 
533
        if ge_lt_dec h1 h2 then
 
534
          if eq_dec h2 1%I then add x2 s#1 else
 
535
          let (l2',_,r2') := split x1 s#2 in 
 
536
             join (ocaml_union (l1,l2')) x1 (ocaml_union (r1,r2'))
 
537
        else
 
538
          if eq_dec h1 1%I then add x1 s#2 else
 
539
          let (l1',_,r1') := split x2 s#1 in 
 
540
             join (ocaml_union (l1',l2)) x2 (ocaml_union (r1',r2))
 
541
 end.
 
542
Proof.
 
543
abstract ocaml_union_tac.
 
544
abstract ocaml_union_tac.
 
545
abstract ocaml_union_tac.
 
546
abstract ocaml_union_tac.
 
547
Defined.
 
548
 
 
549
Lemma ocaml_union_in : forall s y, 
 
550
 bst s#1 -> avl s#1 -> bst s#2 -> avl s#2 ->
 
551
 (In y (ocaml_union s) <-> In y s#1 \/ In y s#2).
 
552
Proof.
 
553
 intros s; functional induction ocaml_union s; intros y B1 A1 B2 A2; 
 
554
  simpl fst in *; simpl snd in *; try clear e0 e1.
 
555
 intuition_in.
 
556
 intuition_in.
 
557
 intuition_in.
 
558
 (* add x2 s#1 *)
 
559
 inv avl.
 
560
 rewrite (height_0 H); [ | avl_nn l2; omega_max].
 
561
 rewrite (height_0 H0); [ | avl_nn r2; omega_max].
 
562
 rewrite add_in; intuition_in.
 
563
 (* join (union (l1,l2')) x1 (union (r1,r2')) *)
 
564
 generalize
 
565
  (split_avl x1 A2) (split_bst x1 B2)
 
566
  (split_in_1 x1 y B2) (split_in_2 x1 y B2).
 
567
 rewrite e2; simpl.
 
568
 destruct 1; destruct 1; inv avl; inv bst.
 
569
 rewrite join_in, IHt, IHt0; auto.
 
570
 do 2 (intro Eq; rewrite Eq; clear Eq).
 
571
 case (X.compare y x1); intuition_in.
 
572
 (* add x1 s#2 *)
 
573
 inv avl.
 
574
 rewrite (height_0 H3); [ | avl_nn l1; omega_max].
 
575
 rewrite (height_0 H4); [ | avl_nn r1; omega_max].
 
576
 rewrite add_in; auto; intuition_in.
 
577
 (* join (union (l1',l2)) x1 (union (r1',r2)) *)
 
578
 generalize 
 
579
  (split_avl x2 A1) (split_bst x2 B1)
 
580
  (split_in_1 x2 y B1) (split_in_2 x2 y B1).
 
581
 rewrite e2; simpl.
 
582
 destruct 1; destruct 1; inv avl; inv bst.
 
583
 rewrite join_in, IHt, IHt0; auto.
 
584
 do 2 (intro Eq; rewrite Eq; clear Eq).
 
585
 case (X.compare y x2); intuition_in.
 
586
Qed.
 
587
 
 
588
Lemma ocaml_union_bst : forall s,
 
589
 bst s#1 -> avl s#1 -> bst s#2 -> avl s#2 -> bst (ocaml_union s).
 
590
Proof.
 
591
 intros s; functional induction ocaml_union s; intros B1 A1 B2 A2;
 
592
  simpl fst in *; simpl snd in *; try clear e0 e1; 
 
593
  try apply add_bst; auto.
 
594
 (* join (union (l1,l2')) x1 (union (r1,r2')) *)
 
595
 clear _x _x0; factornode l2 x2 r2 h2 as s2.
 
596
 generalize (split_avl x1 A2) (split_bst x1 B2)
 
597
  (@split_in_1 s2 x1)(@split_in_2 s2 x1).
 
598
 rewrite e2; simpl.
 
599
 destruct 1; destruct 1; intros.
 
600
 inv bst; inv avl.
 
601
 apply join_bst; auto.
 
602
 intro y; rewrite ocaml_union_in, H3; intuition_in.
 
603
 intro y; rewrite ocaml_union_in, H4; intuition_in.
 
604
 (* join (union (l1',l2)) x1 (union (r1',r2)) *)
 
605
 clear _x _x0; factornode l1 x1 r1 h1 as s1.
 
606
 generalize (split_avl x2 A1) (split_bst x2 B1)
 
607
  (@split_in_1 s1 x2)(@split_in_2 s1 x2).
 
608
 rewrite e2; simpl.
 
609
 destruct 1; destruct 1; intros.
 
610
 inv bst; inv avl.
 
611
 apply join_bst; auto.
 
612
 intro y; rewrite ocaml_union_in, H3; intuition_in.
 
613
 intro y; rewrite ocaml_union_in, H4; intuition_in.
 
614
Qed.
 
615
 
 
616
Lemma ocaml_union_avl : forall s, 
 
617
 avl s#1 -> avl s#2 -> avl (ocaml_union s).
 
618
Proof.
 
619
 intros s; functional induction ocaml_union s; 
 
620
  simpl fst in *; simpl snd in *; auto.
 
621
 intros A1 A2; generalize (split_avl x1 A2); rewrite e2; simpl.
 
622
 inv avl; destruct 1; auto.
 
623
 intros A1 A2; generalize (split_avl x2 A1); rewrite e2; simpl.
 
624
 inv avl; destruct 1; auto.
 
625
Qed.
 
626
 
 
627
Lemma ocaml_union_alt : forall s, bst s#1 -> avl s#1 -> bst s#2 -> avl s#2 ->
 
628
 Equal (ocaml_union s) (union s#1 s#2).
 
629
Proof.
 
630
 red; intros; rewrite ocaml_union_in, union_in; simpl; intuition.
 
631
Qed.
 
632
 
 
633
 
 
634
(** * [ocaml_subset], a subset faithful to the original ocaml code *)
 
635
 
 
636
Function ocaml_subset (s:t*t) { measure cardinal2 s } : bool :=
 
637
 match s with
 
638
  | (Leaf, _) => true
 
639
  | (Node _ _ _ _, Leaf) => false
 
640
  | (Node l1 x1 r1 h1, Node l2 x2 r2 h2) =>
 
641
     match X.compare x1 x2 with
 
642
      | EQ _ => ocaml_subset (l1,l2) && ocaml_subset (r1,r2)
 
643
      | LT _ => ocaml_subset (Node l1 x1 Leaf 0%I, l2) && ocaml_subset (r1,s#2)
 
644
      | GT _ => ocaml_subset (Node Leaf x1 r1 0%I, r2) && ocaml_subset (l1,s#2)
 
645
     end
 
646
 end.
 
647
 
 
648
Proof.
 
649
 intros; unfold cardinal2; simpl; abstract romega with *.
 
650
 intros; unfold cardinal2; simpl; abstract romega with *.
 
651
 intros; unfold cardinal2; simpl; abstract romega with *.
 
652
 intros; unfold cardinal2; simpl; abstract romega with *.
 
653
 intros; unfold cardinal2; simpl; abstract romega with *.
 
654
 intros; unfold cardinal2; simpl; abstract romega with *.
 
655
Defined.
 
656
 
 
657
Lemma ocaml_subset_12 : forall s, 
 
658
 bst s#1 -> bst s#2 ->
 
659
 (ocaml_subset s = true <-> Subset s#1 s#2).
 
660
Proof.
 
661
 intros s; functional induction ocaml_subset s; simpl;
 
662
  intros B1 B2; try clear e0.
 
663
 intuition.
 
664
 red; auto; inversion 1.
 
665
 split; intros; try discriminate.
 
666
 assert (H': In _x0 Leaf) by auto; inversion H'.
 
667
 (**)
 
668
 simpl in *; inv bst.
 
669
 rewrite andb_true_iff, IHb, IHb0; auto; clear IHb IHb0.
 
670
 unfold Subset; intuition_in.
 
671
 assert (X.eq a x2) by order; intuition_in.
 
672
 assert (In a (Node l2 x2 r2 h2)) by auto; intuition_in; order.
 
673
 assert (In a (Node l2 x2 r2 h2)) by auto; intuition_in; order.
 
674
 (**)
 
675
 simpl in *; inv bst.
 
676
 rewrite andb_true_iff, IHb, IHb0; auto; clear IHb IHb0.
 
677
 unfold Subset; intuition_in.
 
678
 assert (In a (Node l2 x2 r2 h2)) by auto; intuition_in; order.
 
679
 assert (In a (Node l2 x2 r2 h2)) by auto; intuition_in; order.
 
680
 (**)
 
681
 simpl in *; inv bst.
 
682
 rewrite andb_true_iff, IHb, IHb0; auto; clear IHb IHb0.
 
683
 unfold Subset; intuition_in.
 
684
 assert (In a (Node l2 x2 r2 h2)) by auto; intuition_in; order.
 
685
 assert (In a (Node l2 x2 r2 h2)) by auto; intuition_in; order.
 
686
Qed.
 
687
 
 
688
Lemma ocaml_subset_alt : forall s, bst s#1 -> bst s#2 -> 
 
689
 ocaml_subset s = subset s#1 s#2.
 
690
Proof.
 
691
 intros.
 
692
 generalize (ocaml_subset_12 H H0); rewrite <-subset_12 by auto.
 
693
 destruct ocaml_subset; destruct subset; intuition.
 
694
Qed.
 
695
 
 
696
 
 
697
 
 
698
(** [ocaml_compare], a compare faithful to the original ocaml code *)
 
699
 
 
700
(** termination of [compare_aux] *)
 
701
 
 
702
Fixpoint cardinal_e e := match e with
 
703
  | End => 0
 
704
  | More _ s r => S (cardinal s + cardinal_e r)
 
705
 end.
 
706
 
 
707
Lemma cons_cardinal_e : forall s e, 
 
708
 cardinal_e (cons s e) = cardinal s + cardinal_e e.
 
709
Proof.
 
710
 induction s; simpl; intros; auto.
 
711
 rewrite IHs1; simpl; rewrite <- plus_n_Sm; auto with arith.
 
712
Qed.
 
713
 
 
714
Definition cardinal_e_2 e := cardinal_e e#1 + cardinal_e e#2.
 
715
 
 
716
Function ocaml_compare_aux 
 
717
 (e:enumeration*enumeration) { measure cardinal_e_2 e } : comparison := 
 
718
 match e with 
 
719
 | (End,End) => Eq
 
720
 | (End,More _ _ _) => Lt 
 
721
 | (More _ _ _, End) => Gt 
 
722
 | (More x1 r1 e1, More x2 r2 e2) => 
 
723
       match X.compare x1 x2 with 
 
724
        | EQ _ => ocaml_compare_aux (cons r1 e1, cons r2 e2)
 
725
        | LT _ => Lt 
 
726
        | GT _ => Gt 
 
727
       end
 
728
 end.
 
729
 
 
730
Proof.
 
731
intros; unfold cardinal_e_2; simpl; 
 
732
abstract (do 2 rewrite cons_cardinal_e; romega with *).
 
733
Defined.
 
734
 
 
735
Definition ocaml_compare s1 s2 := 
 
736
 ocaml_compare_aux (cons s1 End, cons s2 End).
 
737
 
 
738
Lemma ocaml_compare_aux_Cmp : forall e, 
 
739
 Cmp (ocaml_compare_aux e) (flatten_e e#1) (flatten_e e#2).
 
740
Proof.
 
741
 intros e; functional induction ocaml_compare_aux e; simpl; intros; 
 
742
  auto; try discriminate.
 
743
 apply L.eq_refl.
 
744
 simpl in *.
 
745
 apply cons_Cmp; auto.
 
746
 rewrite <- 2 cons_1; auto.
 
747
Qed.
 
748
 
 
749
Lemma ocaml_compare_Cmp : forall s1 s2,
 
750
 Cmp (ocaml_compare s1 s2) (elements s1) (elements s2).
 
751
Proof.
 
752
 unfold ocaml_compare; intros.
 
753
 assert (H1:=cons_1 s1 End).
 
754
 assert (H2:=cons_1 s2 End).
 
755
 simpl in *; rewrite <- app_nil_end in *; rewrite <-H1,<-H2.
 
756
 apply (@ocaml_compare_aux_Cmp (cons s1 End, cons s2 End)).
 
757
Qed.
 
758
 
 
759
Lemma ocaml_compare_alt : forall s1 s2, bst s1 -> bst s2 -> 
 
760
 ocaml_compare s1 s2 = compare s1 s2.
 
761
Proof.
 
762
 intros s1 s2 B1 B2.
 
763
 generalize (ocaml_compare_Cmp s1 s2)(compare_Cmp s1 s2). 
 
764
 unfold Cmp.
 
765
 destruct ocaml_compare; destruct compare; auto; intros; elimtype False.
 
766
 elim (lt_not_eq B1 B2 H0); auto.
 
767
 elim (lt_not_eq B2 B1 H0); auto.
 
768
  apply eq_sym; auto.
 
769
 elim (lt_not_eq B1 B2 H); auto.
 
770
  elim (lt_not_eq B1 B1).
 
771
  red; eapply L.lt_trans; eauto.
 
772
  apply eq_refl.
 
773
 elim (lt_not_eq B2 B1 H); auto.
 
774
  apply eq_sym; auto.
 
775
 elim (lt_not_eq B1 B2 H0); auto.
 
776
  elim (lt_not_eq B1 B1).
 
777
  red; eapply L.lt_trans; eauto.
 
778
  apply eq_refl.
 
779
Qed.
 
780
 
 
781
 
 
782
(** * Equality test *)
 
783
 
 
784
Definition ocaml_equal s1 s2 : bool := 
 
785
 match ocaml_compare s1 s2 with 
 
786
  | Eq => true
 
787
  | _ => false 
 
788
 end.
 
789
 
 
790
Lemma ocaml_equal_1 : forall s1 s2, bst s1 -> bst s2 ->  
 
791
 Equal s1 s2 -> ocaml_equal s1 s2 = true.
 
792
Proof.
 
793
unfold ocaml_equal; intros s1 s2 B1 B2 E.
 
794
generalize (ocaml_compare_Cmp s1 s2).
 
795
destruct (ocaml_compare s1 s2); auto; intros.
 
796
elim (lt_not_eq B1 B2 H E); auto.
 
797
elim (lt_not_eq B2 B1 H (eq_sym E)); auto.
 
798
Qed.
 
799
 
 
800
Lemma ocaml_equal_2 : forall s1 s2,
 
801
 ocaml_equal s1 s2 = true -> Equal s1 s2.
 
802
Proof.
 
803
unfold ocaml_equal; intros s1 s2 E.
 
804
generalize (ocaml_compare_Cmp s1 s2); 
 
805
 destruct ocaml_compare; auto; discriminate.
 
806
Qed.
 
807
 
 
808
Lemma ocaml_equal_alt : forall s1 s2, bst s1 -> bst s2 -> 
 
809
 ocaml_equal s1 s2 = equal s1 s2.
 
810
Proof.
 
811
intros; unfold ocaml_equal, equal; rewrite ocaml_compare_alt; auto.
 
812
Qed.
 
813
 
 
814
End OcamlOps.
 
815
 
 
816
 
 
817
 
 
818
(** * Encapsulation
 
819
 
 
820
   We can implement [S] with balanced binary search trees. 
 
821
   When compared to [FSetAVL], we maintain here two invariants
 
822
   (bst and avl) instead of only bst, which is enough for fulfilling
 
823
   the FSet interface.
 
824
 
 
825
   This encapsulation propose the non-structural variants 
 
826
   [ocaml_union], [ocaml_subset], [ocaml_compare], [ocaml_equal].
 
827
*) 
 
828
 
 
829
Module IntMake (I:Int)(X: OrderedType) <: S with Module E := X.
 
830
 
 
831
 Module E := X.
 
832
 Module Import OcamlOps := OcamlOps I X.
 
833
 Import AvlProofs.
 
834
 Import Raw.
 
835
 Import Raw.Proofs.
 
836
 
 
837
 Record bbst := Bbst {this :> Raw.t; is_bst : bst this; is_avl : avl this}.
 
838
 Definition t := bbst.
 
839
 Definition elt := E.t.
 
840
 
 
841
 Definition In (x : elt) (s : t) : Prop := In x s.
 
842
 Definition Equal (s s':t) : Prop := forall a : elt, In a s <-> In a s'.
 
843
 Definition Subset (s s':t) : Prop := forall a : elt, In a s -> In a s'.
 
844
 Definition Empty (s:t) : Prop := forall a : elt, ~ In a s.
 
845
 Definition For_all (P : elt -> Prop) (s:t) : Prop := forall x, In x s -> P x.
 
846
 Definition Exists (P : elt -> Prop) (s:t) : Prop := exists x, In x s /\ P x.
 
847
  
 
848
 Lemma In_1 : forall (s:t)(x y:elt), E.eq x y -> In x s -> In y s. 
 
849
 Proof. intro s; exact (@In_1 s). Qed.
 
850
 
 
851
 Definition mem (x:elt)(s:t) : bool := mem x s.
 
852
 
 
853
 Definition empty : t := Bbst empty_bst empty_avl.
 
854
 Definition is_empty (s:t) : bool := is_empty s.
 
855
 Definition singleton (x:elt) : t := 
 
856
   Bbst (singleton_bst x) (singleton_avl x).
 
857
 Definition add (x:elt)(s:t) : t := 
 
858
   Bbst (add_bst x (is_bst s)) (add_avl x (is_avl s)). 
 
859
 Definition remove (x:elt)(s:t) : t := 
 
860
   Bbst (remove_bst x (is_bst s)) (remove_avl x (is_avl s)).
 
861
 Definition inter (s s':t) : t := 
 
862
   Bbst (inter_bst (is_bst s) (is_bst s'))
 
863
        (inter_avl (is_avl s) (is_avl s')).
 
864
 Definition union (s s':t) : t :=
 
865
   Bbst (union_bst (is_bst s) (is_bst s'))
 
866
        (union_avl (is_avl s) (is_avl s')).
 
867
 Definition ocaml_union (s s':t) : t :=
 
868
   Bbst (@ocaml_union_bst (s.(this),s'.(this)) 
 
869
          (is_bst s) (is_avl s) (is_bst s') (is_avl s'))
 
870
        (@ocaml_union_avl (s.(this),s'.(this)) (is_avl s) (is_avl s')).
 
871
 Definition diff (s s':t) : t := 
 
872
   Bbst (diff_bst (is_bst s) (is_bst s'))
 
873
        (diff_avl (is_avl s) (is_avl s')).
 
874
 Definition elements (s:t) : list elt := elements s.
 
875
 Definition min_elt (s:t) : option elt := min_elt s.
 
876
 Definition max_elt (s:t) : option elt := max_elt s.
 
877
 Definition choose (s:t) : option elt := choose s.
 
878
 Definition fold (B : Type) (f : elt -> B -> B) (s:t) : B -> B := fold f s. 
 
879
 Definition cardinal (s:t) : nat := cardinal s.
 
880
 Definition filter (f : elt -> bool) (s:t) : t := 
 
881
   Bbst (filter_bst f (is_bst s)) (filter_avl f (is_avl s)).
 
882
 Definition for_all (f : elt -> bool) (s:t) : bool := for_all f s.
 
883
 Definition exists_ (f : elt -> bool) (s:t) : bool := exists_ f s.
 
884
 Definition partition (f : elt -> bool) (s:t) : t * t :=
 
885
   let p := partition f s in
 
886
   (@Bbst (fst p) (partition_bst_1 f (is_bst s)) 
 
887
                 (partition_avl_1 f (is_avl s)), 
 
888
    @Bbst (snd p) (partition_bst_2 f (is_bst s))
 
889
                 (partition_avl_2 f (is_avl s))).
 
890
 
 
891
 Definition equal (s s':t) : bool := equal s s'.
 
892
 Definition ocaml_equal (s s':t) : bool := ocaml_equal s s'.
 
893
 Definition subset (s s':t) : bool := subset s s'.
 
894
 Definition ocaml_subset (s s':t) : bool := 
 
895
  ocaml_subset (s.(this),s'.(this)).
 
896
 
 
897
 Definition eq (s s':t) : Prop := Equal s s'.
 
898
 Definition lt (s s':t) : Prop := lt s s'.
 
899
 
 
900
 Definition compare (s s':t) : Compare lt eq s s'.
 
901
 Proof.
 
902
  intros (s,b,a) (s',b',a').
 
903
  generalize (compare_Cmp s s').
 
904
  destruct Raw.compare; intros; [apply EQ|apply LT|apply GT]; red; auto.
 
905
  change (Raw.Equal s s'); auto.
 
906
 Defined.
 
907
 
 
908
 Definition ocaml_compare (s s':t) : Compare lt eq s s'.
 
909
 Proof.
 
910
  intros (s,b,a) (s',b',a').
 
911
  generalize (ocaml_compare_Cmp s s').
 
912
  destruct ocaml_compare; intros; [apply EQ|apply LT|apply GT]; red; auto.
 
913
  change (Raw.Equal s s'); auto.
 
914
 Defined.
 
915
 
 
916
 Definition eq_dec (s s':t) : { eq s s' } + { ~ eq s s' }.
 
917
 Proof.
 
918
  intros (s,b,a) (s',b',a'); unfold eq; simpl.
 
919
  case_eq (Raw.equal s s'); intro H; [left|right].
 
920
  apply equal_2; auto.
 
921
  intro H'; rewrite equal_1 in H; auto; discriminate.
 
922
 Defined.
 
923
 
 
924
 (* specs *)
 
925
 Section Specs. 
 
926
 Variable s s' s'': t. 
 
927
 Variable x y : elt.
 
928
 
 
929
 Hint Resolve is_bst is_avl.
 
930
 
 
931
 Lemma mem_1 : In x s -> mem x s = true. 
 
932
 Proof. exact (mem_1 (is_bst s)). Qed.
 
933
 Lemma mem_2 : mem x s = true -> In x s.
 
934
 Proof. exact (@mem_2 s x). Qed.
 
935
 
 
936
 Lemma equal_1 : Equal s s' -> equal s s' = true.
 
937
 Proof. exact (equal_1 (is_bst s) (is_bst s')). Qed.
 
938
 Lemma equal_2 : equal s s' = true -> Equal s s'.
 
939
 Proof. exact (@equal_2 s s'). Qed.
 
940
 
 
941
 Lemma ocaml_equal_alt : ocaml_equal s s' = equal s s'.
 
942
 Proof. 
 
943
  destruct s; destruct s'; unfold ocaml_equal, equal; simpl.
 
944
  apply ocaml_equal_alt; auto.
 
945
 Qed.
 
946
 
 
947
 Lemma ocaml_equal_1 : Equal s s' -> ocaml_equal s s' = true.
 
948
 Proof. exact (ocaml_equal_1 (is_bst s) (is_bst s')). Qed.
 
949
 Lemma ocaml_equal_2 : ocaml_equal s s' = true -> Equal s s'.
 
950
 Proof. exact (@ocaml_equal_2 s s'). Qed. 
 
951
 
 
952
 Ltac wrap t H := unfold t, In; simpl; rewrite H; auto; intuition.
 
953
 
 
954
 Lemma subset_1 : Subset s s' -> subset s s' = true.
 
955
 Proof. wrap subset subset_12. Qed.
 
956
 Lemma subset_2 : subset s s' = true -> Subset s s'.
 
957
 Proof. wrap subset subset_12. Qed.
 
958
 
 
959
 Lemma ocaml_subset_alt : ocaml_subset s s' = subset s s'.
 
960
 Proof. 
 
961
  destruct s; destruct s'; unfold ocaml_subset, subset; simpl.
 
962
  rewrite ocaml_subset_alt; auto.
 
963
 Qed.
 
964
 
 
965
 Lemma ocaml_subset_1 : Subset s s' -> ocaml_subset s s' = true.
 
966
 Proof. wrap ocaml_subset ocaml_subset_12; simpl; auto. Qed.
 
967
 Lemma ocaml_subset_2 : ocaml_subset s s' = true -> Subset s s'.
 
968
 Proof. wrap ocaml_subset ocaml_subset_12; simpl; auto. Qed.
 
969
 
 
970
 Lemma empty_1 : Empty empty.
 
971
 Proof. exact empty_1. Qed.
 
972
 
 
973
 Lemma is_empty_1 : Empty s -> is_empty s = true.
 
974
 Proof. exact (@is_empty_1 s). Qed.
 
975
 Lemma is_empty_2 : is_empty s = true -> Empty s. 
 
976
 Proof. exact (@is_empty_2 s). Qed.
 
977
 
 
978
 Lemma add_1 : E.eq x y -> In y (add x s).
 
979
 Proof. wrap add add_in. Qed.
 
980
 Lemma add_2 : In y s -> In y (add x s).
 
981
 Proof. wrap add add_in. Qed.
 
982
 Lemma add_3 : ~ E.eq x y -> In y (add x s) -> In y s. 
 
983
 Proof. wrap add add_in. elim H; auto. Qed.
 
984
 
 
985
 Lemma remove_1 : E.eq x y -> ~ In y (remove x s).
 
986
 Proof. wrap remove remove_in. Qed.
 
987
 Lemma remove_2 : ~ E.eq x y -> In y s -> In y (remove x s).
 
988
 Proof. wrap remove remove_in. Qed.
 
989
 Lemma remove_3 : In y (remove x s) -> In y s.
 
990
 Proof. wrap remove remove_in. Qed.
 
991
 
 
992
 Lemma singleton_1 : In y (singleton x) -> E.eq x y. 
 
993
 Proof. exact (@singleton_1 x y). Qed.
 
994
 Lemma singleton_2 : E.eq x y -> In y (singleton x). 
 
995
 Proof. exact (@singleton_2 x y). Qed.
 
996
 
 
997
 Lemma union_1 : In x (union s s') -> In x s \/ In x s'.
 
998
 Proof. wrap union union_in. Qed.
 
999
 Lemma union_2 : In x s -> In x (union s s'). 
 
1000
 Proof. wrap union union_in. Qed.
 
1001
 Lemma union_3 : In x s' -> In x (union s s').
 
1002
 Proof. wrap union union_in. Qed.
 
1003
 
 
1004
 Lemma ocaml_union_alt : Equal (ocaml_union s s') (union s s').
 
1005
 Proof. 
 
1006
  unfold ocaml_union, union, Equal, In.
 
1007
  destruct s as (s0,b,a); destruct s' as (s0',b',a'); simpl.
 
1008
  exact (@ocaml_union_alt (s0,s0') b a b' a').
 
1009
 Qed.
 
1010
 
 
1011
 Lemma ocaml_union_1 : In x (ocaml_union s s') -> In x s \/ In x s'.
 
1012
 Proof. wrap ocaml_union ocaml_union_in; simpl; auto. Qed.
 
1013
 Lemma ocaml_union_2 : In x s -> In x (ocaml_union s s').
 
1014
 Proof. wrap ocaml_union ocaml_union_in; simpl; auto. Qed.
 
1015
 Lemma ocaml_union_3 : In x s' -> In x (ocaml_union s s').
 
1016
 Proof. wrap ocaml_union ocaml_union_in; simpl; auto. Qed.
 
1017
 
 
1018
 Lemma inter_1 : In x (inter s s') -> In x s.
 
1019
 Proof. wrap inter inter_in. Qed.
 
1020
 Lemma inter_2 : In x (inter s s') -> In x s'.
 
1021
 Proof. wrap inter inter_in. Qed.
 
1022
 Lemma inter_3 : In x s -> In x s' -> In x (inter s s').
 
1023
 Proof. wrap inter inter_in. Qed.
 
1024
 
 
1025
 Lemma diff_1 : In x (diff s s') -> In x s. 
 
1026
 Proof. wrap diff diff_in. Qed.
 
1027
 Lemma diff_2 : In x (diff s s') -> ~ In x s'.
 
1028
 Proof. wrap diff diff_in. Qed.
 
1029
 Lemma diff_3 : In x s -> ~ In x s' -> In x (diff s s').
 
1030
 Proof. wrap diff diff_in. Qed.
 
1031
 
 
1032
 Lemma fold_1 : forall (A : Type) (i : A) (f : elt -> A -> A),
 
1033
      fold f s i = fold_left (fun a e => f e a) (elements s) i.
 
1034
 Proof. 
 
1035
 unfold fold, elements; intros; apply fold_1; auto.
 
1036
 Qed.
 
1037
 
 
1038
 Lemma cardinal_1 : cardinal s = length (elements s).
 
1039
 Proof. 
 
1040
 unfold cardinal, elements; intros; apply elements_cardinal; auto.
 
1041
 Qed.
 
1042
 
 
1043
 Section Filter.
 
1044
 Variable f : elt -> bool.
 
1045
 
 
1046
 Lemma filter_1 : compat_bool E.eq f -> In x (filter f s) -> In x s. 
 
1047
 Proof. intro. wrap filter filter_in. Qed.
 
1048
 Lemma filter_2 : compat_bool E.eq f -> In x (filter f s) -> f x = true. 
 
1049
 Proof. intro. wrap filter filter_in. Qed. 
 
1050
 Lemma filter_3 : compat_bool E.eq f -> In x s -> f x = true -> In x (filter f s).
 
1051
 Proof. intro. wrap filter filter_in. Qed.
 
1052
 
 
1053
 Lemma for_all_1 : compat_bool E.eq f -> For_all (fun x => f x = true) s -> for_all f s = true.
 
1054
 Proof. exact (@for_all_1 f s). Qed.
 
1055
 Lemma for_all_2 : compat_bool E.eq f -> for_all f s = true -> For_all (fun x => f x = true) s.
 
1056
 Proof. exact (@for_all_2 f s). Qed.
 
1057
 
 
1058
 Lemma exists_1 : compat_bool E.eq f -> Exists (fun x => f x = true) s -> exists_ f s = true.
 
1059
 Proof. exact (@exists_1 f s). Qed.
 
1060
 Lemma exists_2 : compat_bool E.eq f -> exists_ f s = true -> Exists (fun x => f x = true) s.
 
1061
 Proof. exact (@exists_2 f s). Qed.
 
1062
 
 
1063
 Lemma partition_1 : compat_bool E.eq f -> 
 
1064
  Equal (fst (partition f s)) (filter f s).
 
1065
 Proof.
 
1066
 unfold partition, filter, Equal, In; simpl ;intros H a.
 
1067
 rewrite partition_in_1, filter_in; intuition.
 
1068
 Qed.
 
1069
 
 
1070
 Lemma partition_2 : compat_bool E.eq f -> 
 
1071
  Equal (snd (partition f s)) (filter (fun x => negb (f x)) s).
 
1072
 Proof.
 
1073
 unfold partition, filter, Equal, In; simpl ;intros H a.
 
1074
 rewrite partition_in_2, filter_in; intuition.
 
1075
 rewrite H2; auto.
 
1076
 destruct (f a); auto.
 
1077
 red; intros; f_equal.
 
1078
 rewrite (H _ _ H0); auto.
 
1079
 Qed.
 
1080
 
 
1081
 End Filter.
 
1082
 
 
1083
 Lemma elements_1 : In x s -> InA E.eq x (elements s).
 
1084
 Proof. wrap elements elements_in. Qed.
 
1085
 Lemma elements_2 : InA E.eq x (elements s) -> In x s.
 
1086
 Proof. wrap elements elements_in. Qed.
 
1087
 Lemma elements_3 : sort E.lt (elements s).
 
1088
 Proof. exact (elements_sort (is_bst s)). Qed.
 
1089
 Lemma elements_3w : NoDupA E.eq (elements s).
 
1090
 Proof. exact (elements_nodup (is_bst s)). Qed.
 
1091
 
 
1092
 Lemma min_elt_1 : min_elt s = Some x -> In x s. 
 
1093
 Proof. exact (@min_elt_1 s x). Qed.
 
1094
 Lemma min_elt_2 : min_elt s = Some x -> In y s -> ~ E.lt y x.
 
1095
 Proof. exact (@min_elt_2 s x y (is_bst s)). Qed.
 
1096
 Lemma min_elt_3 : min_elt s = None -> Empty s.
 
1097
 Proof. exact (@min_elt_3 s). Qed.
 
1098
 
 
1099
 Lemma max_elt_1 : max_elt s = Some x -> In x s. 
 
1100
 Proof. exact (@max_elt_1 s x). Qed.
 
1101
 Lemma max_elt_2 : max_elt s = Some x -> In y s -> ~ E.lt x y.
 
1102
 Proof. exact (@max_elt_2 s x y (is_bst s)). Qed.
 
1103
 Lemma max_elt_3 : max_elt s = None -> Empty s.
 
1104
 Proof. exact (@max_elt_3 s). Qed.
 
1105
 
 
1106
 Lemma choose_1 : choose s = Some x -> In x s.
 
1107
 Proof. exact (@choose_1 s x). Qed.
 
1108
 Lemma choose_2 : choose s = None -> Empty s.
 
1109
 Proof. exact (@choose_2 s). Qed.
 
1110
 Lemma choose_3 : choose s = Some x -> choose s' = Some y -> 
 
1111
  Equal s s' -> E.eq x y.
 
1112
 Proof. exact (@choose_3 _ _ (is_bst s) (is_bst s') x y). Qed.
 
1113
 
 
1114
 Lemma eq_refl : eq s s. 
 
1115
 Proof. exact (eq_refl s). Qed.
 
1116
 Lemma eq_sym : eq s s' -> eq s' s.
 
1117
 Proof. exact (@eq_sym s s'). Qed.
 
1118
 Lemma eq_trans : eq s s' -> eq s' s'' -> eq s s''.
 
1119
 Proof. exact (@eq_trans s s' s''). Qed.
 
1120
  
 
1121
 Lemma lt_trans : lt s s' -> lt s' s'' -> lt s s''.
 
1122
 Proof. exact (@lt_trans s s' s''). Qed.
 
1123
 Lemma lt_not_eq : lt s s' -> ~eq s s'.
 
1124
 Proof. exact (@lt_not_eq _ _ (is_bst s) (is_bst s')). Qed.
 
1125
 
 
1126
 End Specs.
 
1127
End IntMake.
 
1128
 
 
1129
(* For concrete use inside Coq, we propose an instantiation of [Int] by [Z]. *)
 
1130
 
 
1131
Module Make (X: OrderedType) <: S with Module E := X
 
1132
 :=IntMake(Z_as_Int)(X).
 
1133