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

« back to all changes in this revision

Viewing changes to theories/Lists/ListSet.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___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
 
4
(*   \VV/  **************************************************************)
 
5
(*    //   *      This file is distributed under the terms of the       *)
 
6
(*         *       GNU Lesser General Public License Version 2.1        *)
 
7
(************************************************************************)
 
8
 
 
9
(*i $Id: ListSet.v 10616 2008-03-04 17:33:35Z letouzey $ i*)
 
10
 
 
11
(** A Library for finite sets, implemented as lists *)
 
12
 
 
13
(** List is loaded, but not exported.
 
14
    This allow to "hide" the definitions, functions and theorems of List
 
15
    and to see only the ones of ListSet *)
 
16
 
 
17
Require Import List.
 
18
 
 
19
Set Implicit Arguments.
 
20
 
 
21
Section first_definitions.
 
22
 
 
23
  Variable A : Type.
 
24
  Hypothesis Aeq_dec : forall x y:A, {x = y} + {x <> y}.
 
25
 
 
26
  Definition set := list A.
 
27
 
 
28
  Definition empty_set : set := nil.
 
29
 
 
30
  Fixpoint set_add (a:A) (x:set) {struct x} : set :=
 
31
    match x with
 
32
    | nil => a :: nil
 
33
    | a1 :: x1 =>
 
34
        match Aeq_dec a a1 with
 
35
        | left _ => a1 :: x1
 
36
        | right _ => a1 :: set_add a x1
 
37
        end
 
38
    end.
 
39
 
 
40
 
 
41
  Fixpoint set_mem (a:A) (x:set) {struct x} : bool :=
 
42
    match x with
 
43
    | nil => false
 
44
    | a1 :: x1 =>
 
45
        match Aeq_dec a a1 with
 
46
        | left _ => true
 
47
        | right _ => set_mem a x1
 
48
        end
 
49
    end.
 
50
 
 
51
  (** If [a] belongs to [x], removes [a] from [x]. If not, does nothing *)
 
52
  Fixpoint set_remove (a:A) (x:set) {struct x} : set :=
 
53
    match x with
 
54
    | nil => empty_set
 
55
    | a1 :: x1 =>
 
56
        match Aeq_dec a a1 with
 
57
        | left _ => x1
 
58
        | right _ => a1 :: set_remove a x1
 
59
        end
 
60
    end.
 
61
 
 
62
  Fixpoint set_inter (x:set) : set -> set :=
 
63
    match x with
 
64
    | nil => fun y => nil
 
65
    | a1 :: x1 =>
 
66
        fun y =>
 
67
          if set_mem a1 y then a1 :: set_inter x1 y else set_inter x1 y
 
68
    end.
 
69
 
 
70
  Fixpoint set_union (x y:set) {struct y} : set :=
 
71
    match y with
 
72
    | nil => x
 
73
    | a1 :: y1 => set_add a1 (set_union x y1)
 
74
    end.
 
75
        
 
76
  (** returns the set of all els of [x] that does not belong to [y] *)
 
77
  Fixpoint set_diff (x y:set) {struct x} : set :=
 
78
    match x with
 
79
    | nil => nil
 
80
    | a1 :: x1 =>
 
81
        if set_mem a1 y then set_diff x1 y else set_add a1 (set_diff x1 y)
 
82
    end.
 
83
   
 
84
 
 
85
  Definition set_In : A -> set -> Prop := In (A:=A).
 
86
 
 
87
  Lemma set_In_dec : forall (a:A) (x:set), {set_In a x} + {~ set_In a x}.
 
88
 
 
89
  Proof.
 
90
    unfold set_In in |- *.
 
91
    (*** Realizer set_mem. Program_all. ***)
 
92
    simple induction x.
 
93
    auto.
 
94
    intros a0 x0 Ha0. case (Aeq_dec a a0); intro eq.
 
95
    rewrite eq; simpl in |- *; auto with datatypes.
 
96
    elim Ha0.
 
97
    auto with datatypes.
 
98
    right; simpl in |- *; unfold not in |- *; intros [Hc1| Hc2];
 
99
     auto with datatypes.
 
100
  Qed.
 
101
 
 
102
  Lemma set_mem_ind :
 
103
   forall (B:Type) (P:B -> Prop) (y z:B) (a:A) (x:set),
 
104
     (set_In a x -> P y) -> P z -> P (if set_mem a x then y else z).
 
105
 
 
106
  Proof.
 
107
    simple induction x; simpl in |- *; intros.
 
108
    assumption.
 
109
    elim (Aeq_dec a a0); auto with datatypes.
 
110
  Qed.
 
111
 
 
112
  Lemma set_mem_ind2 :
 
113
   forall (B:Type) (P:B -> Prop) (y z:B) (a:A) (x:set),
 
114
     (set_In a x -> P y) ->
 
115
     (~ set_In a x -> P z) -> P (if set_mem a x then y else z).
 
116
 
 
117
  Proof.
 
118
    simple induction x; simpl in |- *; intros.
 
119
    apply H0; red in |- *; trivial.
 
120
    case (Aeq_dec a a0); auto with datatypes.
 
121
    intro; apply H; intros; auto.
 
122
    apply H1; red in |- *; intro.
 
123
    case H3; auto.
 
124
  Qed.
 
125
 
 
126
 
 
127
  Lemma set_mem_correct1 :
 
128
   forall (a:A) (x:set), set_mem a x = true -> set_In a x.
 
129
  Proof.
 
130
    simple induction x; simpl in |- *.
 
131
    discriminate.
 
132
    intros a0 l; elim (Aeq_dec a a0); auto with datatypes.
 
133
  Qed.
 
134
 
 
135
  Lemma set_mem_correct2 :
 
136
   forall (a:A) (x:set), set_In a x -> set_mem a x = true.
 
137
  Proof.
 
138
    simple induction x; simpl in |- *.
 
139
    intro Ha; elim Ha.
 
140
    intros a0 l; elim (Aeq_dec a a0); auto with datatypes.
 
141
    intros H1 H2 [H3| H4].
 
142
    absurd (a0 = a); auto with datatypes.
 
143
    auto with datatypes.
 
144
  Qed.
 
145
 
 
146
  Lemma set_mem_complete1 :
 
147
   forall (a:A) (x:set), set_mem a x = false -> ~ set_In a x.
 
148
  Proof.
 
149
    simple induction x; simpl in |- *.
 
150
    tauto.
 
151
    intros a0 l; elim (Aeq_dec a a0).
 
152
    intros; discriminate H0.
 
153
    unfold not in |- *; intros; elim H1; auto with datatypes.
 
154
  Qed.
 
155
 
 
156
  Lemma set_mem_complete2 :
 
157
   forall (a:A) (x:set), ~ set_In a x -> set_mem a x = false.
 
158
  Proof.
 
159
    simple induction x; simpl in |- *.
 
160
    tauto.
 
161
    intros a0 l; elim (Aeq_dec a a0).
 
162
    intros; elim H0; auto with datatypes.
 
163
    tauto.
 
164
  Qed.
 
165
 
 
166
  Lemma set_add_intro1 :
 
167
   forall (a b:A) (x:set), set_In a x -> set_In a (set_add b x).
 
168
 
 
169
  Proof.
 
170
    unfold set_In in |- *; simple induction x; simpl in |- *.
 
171
    auto with datatypes.
 
172
    intros a0 l H [Ha0a| Hal].
 
173
    elim (Aeq_dec b a0); left; assumption.
 
174
    elim (Aeq_dec b a0); right; [ assumption | auto with datatypes ].
 
175
  Qed.
 
176
 
 
177
  Lemma set_add_intro2 :
 
178
   forall (a b:A) (x:set), a = b -> set_In a (set_add b x).
 
179
 
 
180
  Proof.
 
181
    unfold set_In in |- *; simple induction x; simpl in |- *.
 
182
    auto with datatypes.
 
183
    intros a0 l H Hab.
 
184
    elim (Aeq_dec b a0);
 
185
     [ rewrite Hab; intro Hba0; rewrite Hba0; simpl in |- *;
 
186
        auto with datatypes
 
187
     | auto with datatypes ].
 
188
  Qed.
 
189
 
 
190
  Hint Resolve set_add_intro1 set_add_intro2.
 
191
 
 
192
  Lemma set_add_intro :
 
193
   forall (a b:A) (x:set), a = b \/ set_In a x -> set_In a (set_add b x).
 
194
  
 
195
  Proof.
 
196
    intros a b x [H1| H2]; auto with datatypes.
 
197
  Qed.
 
198
 
 
199
  Lemma set_add_elim :
 
200
   forall (a b:A) (x:set), set_In a (set_add b x) -> a = b \/ set_In a x.
 
201
 
 
202
  Proof.
 
203
    unfold set_In in |- *.
 
204
    simple induction x.
 
205
    simpl in |- *; intros [H1| H2]; auto with datatypes.
 
206
    simpl in |- *; do 3 intro.
 
207
    elim (Aeq_dec b a0).
 
208
    simpl in |- *; tauto.
 
209
    simpl in |- *; intros; elim H0.
 
210
    trivial with datatypes.
 
211
    tauto.
 
212
    tauto.
 
213
  Qed.
 
214
 
 
215
  Lemma set_add_elim2 :
 
216
   forall (a b:A) (x:set), set_In a (set_add b x) -> a <> b -> set_In a x.
 
217
   intros a b x H; case (set_add_elim _ _ _ H); intros; trivial.
 
218
   case H1; trivial.
 
219
   Qed.
 
220
 
 
221
  Hint Resolve set_add_intro set_add_elim set_add_elim2.
 
222
 
 
223
  Lemma set_add_not_empty : forall (a:A) (x:set), set_add a x <> empty_set.
 
224
  Proof.
 
225
    simple induction x; simpl in |- *.
 
226
    discriminate.
 
227
    intros; elim (Aeq_dec a a0); intros; discriminate.
 
228
  Qed.   
 
229
 
 
230
 
 
231
  Lemma set_union_intro1 :
 
232
   forall (a:A) (x y:set), set_In a x -> set_In a (set_union x y).
 
233
  Proof.
 
234
    simple induction y; simpl in |- *; auto with datatypes.
 
235
  Qed.
 
236
 
 
237
  Lemma set_union_intro2 :
 
238
   forall (a:A) (x y:set), set_In a y -> set_In a (set_union x y).
 
239
  Proof.
 
240
    simple induction y; simpl in |- *.
 
241
    tauto.
 
242
    intros; elim H0; auto with datatypes.
 
243
  Qed.
 
244
 
 
245
  Hint Resolve set_union_intro2 set_union_intro1.
 
246
 
 
247
  Lemma set_union_intro :
 
248
   forall (a:A) (x y:set),
 
249
     set_In a x \/ set_In a y -> set_In a (set_union x y).
 
250
  Proof.
 
251
    intros; elim H; auto with datatypes.
 
252
  Qed.
 
253
 
 
254
  Lemma set_union_elim :
 
255
   forall (a:A) (x y:set),
 
256
     set_In a (set_union x y) -> set_In a x \/ set_In a y.
 
257
  Proof.
 
258
    simple induction y; simpl in |- *.
 
259
    auto with datatypes.
 
260
    intros.
 
261
    generalize (set_add_elim _ _ _ H0).
 
262
    intros [H1| H1].
 
263
    auto with datatypes.
 
264
    tauto.
 
265
  Qed.
 
266
 
 
267
  Lemma set_union_emptyL :
 
268
   forall (a:A) (x:set), set_In a (set_union empty_set x) -> set_In a x.
 
269
    intros a x H; case (set_union_elim _ _ _ H); auto || contradiction.
 
270
  Qed.
 
271
 
 
272
 
 
273
  Lemma set_union_emptyR :
 
274
   forall (a:A) (x:set), set_In a (set_union x empty_set) -> set_In a x.
 
275
    intros a x H; case (set_union_elim _ _ _ H); auto || contradiction.
 
276
  Qed.
 
277
 
 
278
 
 
279
  Lemma set_inter_intro :
 
280
   forall (a:A) (x y:set),
 
281
     set_In a x -> set_In a y -> set_In a (set_inter x y).
 
282
  Proof.
 
283
    simple induction x.
 
284
    auto with datatypes.
 
285
    simpl in |- *; intros a0 l Hrec y [Ha0a| Hal] Hy.
 
286
    simpl in |- *; rewrite Ha0a.
 
287
    generalize (set_mem_correct1 a y).
 
288
    generalize (set_mem_complete1 a y).
 
289
    elim (set_mem a y); simpl in |- *; intros.
 
290
    auto with datatypes.
 
291
    absurd (set_In a y); auto with datatypes.
 
292
    elim (set_mem a0 y); [ right; auto with datatypes | auto with datatypes ].     
 
293
  Qed.
 
294
 
 
295
  Lemma set_inter_elim1 :
 
296
   forall (a:A) (x y:set), set_In a (set_inter x y) -> set_In a x.
 
297
  Proof.
 
298
    simple induction x.
 
299
    auto with datatypes.
 
300
    simpl in |- *; intros a0 l Hrec y.
 
301
    generalize (set_mem_correct1 a0 y).
 
302
    elim (set_mem a0 y); simpl in |- *; intros.
 
303
    elim H0; eauto with datatypes.
 
304
    eauto with datatypes.
 
305
  Qed.
 
306
 
 
307
  Lemma set_inter_elim2 :
 
308
   forall (a:A) (x y:set), set_In a (set_inter x y) -> set_In a y.
 
309
  Proof.
 
310
    simple induction x.
 
311
    simpl in |- *; tauto.
 
312
    simpl in |- *; intros a0 l Hrec y.
 
313
    generalize (set_mem_correct1 a0 y).
 
314
    elim (set_mem a0 y); simpl in |- *; intros.
 
315
    elim H0;
 
316
     [ intro Hr; rewrite <- Hr; eauto with datatypes | eauto with datatypes ].
 
317
    eauto with datatypes.
 
318
  Qed.
 
319
 
 
320
  Hint Resolve set_inter_elim1 set_inter_elim2.
 
321
 
 
322
  Lemma set_inter_elim :
 
323
   forall (a:A) (x y:set),
 
324
     set_In a (set_inter x y) -> set_In a x /\ set_In a y.
 
325
  Proof.
 
326
    eauto with datatypes.
 
327
  Qed. 
 
328
 
 
329
  Lemma set_diff_intro :
 
330
   forall (a:A) (x y:set),
 
331
     set_In a x -> ~ set_In a y -> set_In a (set_diff x y).
 
332
  Proof.
 
333
    simple induction x.
 
334
    simpl in |- *; tauto.
 
335
    simpl in |- *; intros a0 l Hrec y [Ha0a| Hal] Hay.
 
336
    rewrite Ha0a; generalize (set_mem_complete2 _ _ Hay).
 
337
    elim (set_mem a y);
 
338
     [ intro Habs; discriminate Habs | auto with datatypes ].
 
339
    elim (set_mem a0 y); auto with datatypes.
 
340
  Qed.
 
341
 
 
342
  Lemma set_diff_elim1 :
 
343
   forall (a:A) (x y:set), set_In a (set_diff x y) -> set_In a x.
 
344
  Proof.
 
345
    simple induction x.
 
346
    simpl in |- *; tauto.
 
347
    simpl in |- *; intros a0 l Hrec y; elim (set_mem a0 y).
 
348
    eauto with datatypes.
 
349
    intro; generalize (set_add_elim _ _ _ H).
 
350
    intros [H1| H2]; eauto with datatypes.
 
351
  Qed.
 
352
 
 
353
  Lemma set_diff_elim2 :
 
354
   forall (a:A) (x y:set), set_In a (set_diff x y) -> ~ set_In a y.
 
355
  intros a x y; elim x; simpl in |- *.
 
356
  intros; contradiction.
 
357
  intros a0 l Hrec. 
 
358
  apply set_mem_ind2; auto.
 
359
  intros H1 H2; case (set_add_elim _ _ _ H2); intros; auto.
 
360
  rewrite H; trivial.
 
361
  Qed.
 
362
 
 
363
  Lemma set_diff_trivial : forall (a:A) (x:set), ~ set_In a (set_diff x x).
 
364
  red in |- *; intros a x H.
 
365
  apply (set_diff_elim2 _ _ _ H).
 
366
  apply (set_diff_elim1 _ _ _ H).
 
367
  Qed.
 
368
 
 
369
Hint Resolve set_diff_intro set_diff_trivial.
 
370
 
 
371
 
 
372
End first_definitions.
 
373
 
 
374
Section other_definitions.
 
375
 
 
376
  Variables A B : Type.
 
377
 
 
378
  Definition set_prod : set A -> set B -> set (A * B) :=
 
379
    list_prod (A:=A) (B:=B).
 
380
 
 
381
  (** [B^A], set of applications from [A] to [B] *)
 
382
  Definition set_power : set A -> set B -> set (set (A * B)) :=
 
383
    list_power (A:=A) (B:=B).
 
384
 
 
385
  Definition set_map : (A -> B) -> set A -> set B := map (A:=A) (B:=B).
 
386
 
 
387
  Definition set_fold_left : (B -> A -> B) -> set A -> B -> B :=
 
388
    fold_left (A:=B) (B:=A).
 
389
 
 
390
  Definition set_fold_right (f:A -> B -> B) (x:set A) 
 
391
    (b:B) : B := fold_right f b x.
 
392
 
 
393
 
 
394
End other_definitions.
 
395
 
 
396
Unset Implicit Arguments.