-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab3_list.v
357 lines (292 loc) · 7.38 KB
/
lab3_list.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
(**************************************************************)
(* Copyright Dominique Larchey-Wendling [*] *)
(* *)
(* [*] Affiliation LORIA -- CNRS *)
(**************************************************************)
(* This file is distributed under the terms of the *)
(* CeCILL v2 FREE SOFTWARE LICENSE AGREEMENT *)
(**************************************************************)
Require Import Arith Setoid.
Set Implicit Arguments.
(*
Inductive bt :=
| leaf : bt
| node : bt -> bt -> bt.
Check bt_rect.
Check bt_ind.
*)
Section list.
Variable X : Type.
Implicit Type l : list X.
Infix "::" := cons.
Infix "++" := (@app _).
Notation "⌊ l ⌋" := (length l) (at level 1, format "⌊ l ⌋").
Print list.
Print Implicit nil.
Check nil.
Check @nil _.
Print app.
Arguments app {A}.
Check app.
Check @app.
Arguments app [A].
Check app.
Print app.
(* cf 0 + n = n *)
Fact app_nil_head l : nil++l = l.
Proof.
simpl.
trivial.
Qed.
(* cf S n + m = S (n+m) *)
Fact app_cons_head x l m : (x::l) ++ m = x::(l++m).
Proof.
simpl.
trivial.
Qed.
(* cf n+m+p = n+(m+p) *)
Fact app_assoc l m p : (l++m)++p = l++m++p.
Proof.
induction l as [ | x l IHl ].
+ simpl. trivial.
+ simpl.
f_equal.
trivial.
(* induction l; simpl; f_equal; trivial. *)
Qed.
(* cf n+0 = n *)
Fact app_nil_end l : l++nil = l.
Proof.
(* induction l; simpl; f_equal; trivial. *)
induction l as [ | x l IHl ].
+ simpl. trivial.
+ simpl.
f_equal.
trivial.
Qed.
Print length.
Fact app_length l m : ⌊l++m⌋ = ⌊l⌋+⌊m⌋.
Proof.
(* induction l; simpl; f_equal; trivial. *)
induction l as [ | x l IHl ].
+ simpl.
trivial.
+ simpl.
f_equal.
trivial.
Qed.
Section map.
Variable (Y : Type) (f : X -> Y).
Fixpoint map l :=
match l with
| nil => nil
| x::l => f x::map l
end.
Fact map_length l : ⌊map l⌋ = ⌊l⌋.
Proof.
induction l; simpl; f_equal; trivial.
Qed.
Fact map_app l m : map (l++m) = map l ++ map m.
Proof.
induction l; simpl; f_equal; trivial.
Qed.
End map.
Check map.
Fixpoint rev l :=
match l with
| nil => nil
| x::l => rev l ++ x :: nil
end.
Fixpoint rev_app a l :=
match l with
| nil => a
| x::l => rev_app (x::a) l
end.
Print rev_app.
Fact rev_rev_app_eq a l : rev_app a l = rev l ++ a.
Proof.
revert a.
induction l as [ | x l IHl ]; intros a.
+ simpl. trivial.
+ simpl.
rewrite IHl.
rewrite app_assoc.
simpl.
trivial.
Qed.
Fact rev_app_equiv l : rev_app nil l = rev l.
Proof.
rewrite rev_rev_app_eq.
rewrite app_nil_end.
trivial.
Qed.
Reserved Notation "x ∈ l" (at level 70, no associativity).
Fixpoint In x l :=
match l with
| nil => False
| y::l => x = y \/ x ∈ l
end
where "x ∈ l" := (In x l).
Fact in_app_iff x l m : x ∈ l++m <-> x ∈ l \/ x ∈ m.
Proof.
(* Print list.
Check list_rect.
Check list_ind. *)
induction l as [ | y l IHl ]; simpl.
+ tauto.
+ rewrite IHl.
tauto.
(* induction l as [ | ? ? IHl ]; simpl; [ | rewrite IHl ]; tauto. *)
Qed.
(*
End list.
Eval compute in In 2 (2::3::4::nil). *)
Definition incl l m := forall x, x ∈ l -> x ∈ m.
Infix "⊆" := incl (at level 70, no associativity).
Fact incl_refl l : l ⊆ l.
Proof.
unfold incl. auto.
Qed.
Fact incl_trans l m p : l ⊆ m -> m ⊆ p -> l ⊆ p.
Proof.
unfold incl.
(* intros H1 H2 x H3.
apply H2, H1, H3. *)
firstorder.
Qed.
Hint Resolve incl_refl : core.
Fact incl_app_l l m : l ⊆ l++m.
Proof.
(* red.
intros x Hx.
apply in_app_iff.
left; trivial. *)
intro.
Check in_app_iff. (* rewrite with <-> instead of = *)
rewrite in_app_iff.
tauto.
Qed.
Fact incl_app_r l m : m ⊆ l++m.
Proof.
intro; rewrite in_app_iff; tauto.
Qed.
Fact sg_incl x m : x::nil ⊆ m <-> x ∈ m.
Proof.
unfold incl; split.
+ intros H.
apply H.
simpl.
auto.
+ intros H y; simpl.
intros D.
destruct D as [ E | A ].
* rewrite E.
trivial.
* destruct A.
(* intros ? ? [ -> | [] ]; trivial. *)
Qed.
Hint Resolve incl_app_l incl_app_r : core.
Fact app_incl_left l r m : l++r ⊆ m <-> l ⊆ m /\ r ⊆ m.
Proof.
split.
+ intros H.
split.
* Check incl_trans.
(* apply incl_trans with (l++r). *)
apply incl_trans with (2 := H).
trivial.
* apply incl_trans with (2 := H); trivial.
+ intros [ H1 H2 ] x.
rewrite in_app_iff.
intros [ H | H ]; revert H.
* auto.
* auto.
Qed.
Fact cons_incl_left x l m : x::l ⊆ m <-> x ∈ m /\ l ⊆ m.
Proof.
rewrite <- sg_incl, <- app_incl_left.
simpl; tauto.
Qed.
Fact incl_cons_r x l : l ⊆ x::l.
Proof.
unfold incl; simpl; auto.
Qed.
Fact incl_nil_l l : nil ⊆ l.
Proof.
(* unfold incl.
simpl. *)
intros _ [].
Qed.
Hint Resolve incl_nil_l incl_cons_r : core.
Fact incl_nil_r l : l ⊆ nil <-> l = nil.
Proof.
split.
+ unfold incl.
simpl.
intros H.
destruct l as [ | x l ].
* trivial.
* destruct H with x.
simpl; auto.
+ intros ->. (* equiv intros E; rewrite -> E *)
auto. (* apply incl_refl. *)
Qed.
Fact incl_app_comm l m : l++m ⊆ m++l.
Proof.
intro.
rewrite !in_app_iff.
tauto.
Qed.
(* Alternative inductive definitions of In/∈ and incl/⊆ *)
Reserved Notation "x ∈' y" (at level 70, no associativity).
Inductive ind_In : X -> list X -> Prop :=
| in_ind_In0 : forall x l, x ∈' x::l
| in_ind_In1 : forall x y l, x ∈' l -> x ∈' y::l
where "x ∈' l" := (ind_In x l).
(** x ∈' l
------------- ---------------
x ∈' x::l x ∈' y::l *)
Fact ind_In_equiv x l : x ∈ l <-> x ∈' l.
Proof.
split.
+ induction l as [ | y l IHl ]; simpl.
* intros [].
* (* intros [ -> | ]; constructor; auto. *)
intros [ E | H ].
- rewrite E.
constructor 1. (* apply in_ind_In0. *)
- constructor 2.
apply IHl, H.
+ intros H.
induction H as [ x l | x y l H IH ].
* simpl. auto.
* simpl. auto.
Qed.
Reserved Notation "x ⊆' y" (at level 70, no associativity).
Inductive ind_incl : list X -> list X -> Prop :=
| in_ii0 : forall m, nil ⊆' m
| in_ii1 : forall x l m, x ∈' m -> l ⊆' m -> x::l ⊆' m
where "l ⊆' m" := (ind_incl l m).
(** x ∈' m l ⊆' m
------------- ------------------------
nil ⊆' m x::l ⊆' m *)
Fact ind_incl_equiv l m : l ⊆ m <-> l ⊆' m.
Proof.
split.
+ intros H.
induction l as [ | x l IHl ].
* constructor.
* constructor.
- apply ind_In_equiv.
apply H.
simpl; auto.
- apply IHl.
rewrite cons_incl_left in H.
tauto.
+ intros H.
induction H as [ m | x l m H1 H2 IH2 ].
* auto.
* rewrite cons_incl_left, ind_In_equiv.
auto.
Qed.
End list.