forked from fabriceleal/Software-Foundations-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoreLogic.v
595 lines (504 loc) · 21.6 KB
/
MoreLogic.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
(** * MoreLogic: More on Logic in Coq *)
Require Export "Prop".
(* ############################################################ *)
(** * Existential Quantification *)
(** Another critical logical connective is _existential
quantification_. We can express it with the following
definition: *)
Inductive ex (X:Type) (P : X->Prop) : Prop :=
ex_intro : forall (witness:X), P witness -> ex X P.
(** That is, [ex] is a family of propositions indexed by a type [X]
and a property [P] over [X]. In order to give evidence for the
assertion "there exists an [x] for which the property [P] holds"
we must actually name a _witness_ -- a specific value [x] -- and
then give evidence for [P x], i.e., evidence that [x] has the
property [P].
*)
(** *** *)
(** Coq's [Notation] facility can be used to introduce more
familiar notation for writing existentially quantified
propositions, exactly parallel to the built-in syntax for
universally quantified propositions. Instead of writing [ex nat
ev] to express the proposition that there exists some number that
is even, for example, we can write [exists x:nat, ev x]. (It is
not necessary to understand exactly how the [Notation] definition
works.) *)
Notation "'exists' x , p" := (ex _ (fun x => p))
(at level 200, x ident, right associativity) : type_scope.
Notation "'exists' x : X , p" := (ex _ (fun x:X => p))
(at level 200, x ident, right associativity) : type_scope.
(** *** *)
(** We can use the usual set of tactics for
manipulating existentials. For example, to prove an
existential, we can [apply] the constructor [ex_intro]. Since the
premise of [ex_intro] involves a variable ([witness]) that does
not appear in its conclusion, we need to explicitly give its value
when we use [apply]. *)
Example exists_example_1 : exists n, n + (n * n) = 6.
Proof.
apply ex_intro with (witness:=2).
reflexivity. Qed.
(** Note that we have to explicitly give the witness. *)
(** *** *)
(** Or, instead of writing [apply ex_intro with (witness:=e)] all the
time, we can use the convenient shorthand [exists e], which means
the same thing. *)
Example exists_example_1' : exists n, n + (n * n) = 6.
Proof.
exists 2.
reflexivity. Qed.
(** *** *)
(** Conversely, if we have an existential hypothesis in the
context, we can eliminate it with [inversion]. Note the use
of the [as...] pattern to name the variable that Coq
introduces to name the witness value and get evidence that
the hypothesis holds for the witness. (If we don't
explicitly choose one, Coq will just call it [witness], which
makes proofs confusing.) *)
Theorem exists_example_2 : forall n,
(exists m, n = 4 + m) ->
(exists o, n = 2 + o).
Proof.
intros n H.
inversion H as [m Hm].
exists (2 + m).
apply Hm. Qed.
(** Here is another example of how to work with existentials. *)
Lemma exists_example_3 :
exists (n:nat), even n /\ beautiful n.
Proof.
(* WORKED IN CLASS *)
exists 8.
split.
unfold even. simpl. reflexivity.
apply b_sum with (n:=3) (m:=5).
apply b_3. apply b_5.
Qed.
(** **** Exercise: 1 star, optional (english_exists) *)
(** In English, what does the proposition
ex nat (fun n => beautiful (S n))
]]
mean? *)
(*
there is a number n so that [n+1] is beautiful.
*)
(** **** Exercise: 1 star (dist_not_exists) *)
(** Prove that "[P] holds for all [x]" implies "there is no [x] for
which [P] does not hold." *)
Theorem dist_not_exists : forall (X:Type) (P : X -> Prop),
(forall x, P x) -> ~ (exists x, ~ P x).
Proof.
intros.
unfold not.
intros.
inversion H0. apply H1. apply H.
Qed.
(** [] *)
(** **** Exercise: 3 stars, optional (not_exists_dist) *)
(** (The other direction of this theorem requires the classical "law
of the excluded middle".) *)
Theorem not_exists_dist :
excluded_middle ->
forall (X:Type) (P : X -> Prop),
~ (exists x, ~ P x) -> (forall x, P x).
Proof.
unfold excluded_middle.
unfold not.
intros.
destruct (H (P x)). apply H1.
apply ex_falso_quodlibet.
apply H0. exists x. apply H1.
Qed.
(** [] *)
(** **** Exercise: 2 stars (dist_exists_or) *)
(** Prove that existential quantification distributes over
disjunction. *)
Theorem dist_exists_or : forall (X:Type) (P Q : X -> Prop),
(exists x, P x \/ Q x) <-> (exists x, P x) \/ (exists x, Q x).
Proof.
split.
intros.
destruct H. destruct H.
left. exists witness. apply H. right. exists witness. apply H.
intros.
destruct H. destruct H. exists witness. left. apply H.
destruct H. exists witness. right. apply H.
Qed.
(** [] *)
(* ###################################################### *)
(** * Evidence-Carrying Booleans *)
(** So far we've seen two different forms of equality predicates:
[eq], which produces a [Prop], and the type-specific forms, like
[beq_nat], that produce [boolean] values. The former are more
convenient to reason about, but we've relied on the latter to let
us use equality tests in _computations_. While it is
straightforward to write lemmas (e.g. [beq_nat_true] and
[beq_nat_false]) that connect the two forms, using these lemmas
quickly gets tedious. *)
(** *** *)
(** It turns out that we can get the benefits of both forms at once by
using a construct called [sumbool]. *)
Inductive sumbool (A B : Prop) : Set :=
| left : A -> sumbool A B
| right : B -> sumbool A B.
Notation "{ A } + { B }" := (sumbool A B) : type_scope.
(** Think of [sumbool] as being like the [boolean] type, but instead
of its values being just [true] and [false], they carry _evidence_
of truth or falsity. This means that when we [destruct] them, we
are left with the relevant evidence as a hypothesis -- just as
with [or]. (In fact, the definition of [sumbool] is almost the
same as for [or]. The only difference is that values of [sumbool]
are declared to be in [Set] rather than in [Prop]; this is a
technical distinction that allows us to compute with them.) *)
(** *** *)
(** Here's how we can define a [sumbool] for equality on [nat]s *)
Theorem eq_nat_dec : forall n m : nat, {n = m} + {n <> m}.
Proof.
(* WORKED IN CLASS *)
intros n.
induction n as [|n'].
Case "n = 0".
intros m.
destruct m as [|m'].
SCase "m = 0".
left. reflexivity.
SCase "m = S m'".
right. intros contra. inversion contra.
Case "n = S n'".
intros m.
destruct m as [|m'].
SCase "m = 0".
right. intros contra. inversion contra.
SCase "m = S m'".
destruct IHn' with (m := m') as [eq | neq].
left. apply f_equal. apply eq.
right. intros Heq. inversion Heq as [Heq']. apply neq. apply Heq'.
Defined.
(** Read as a theorem, this says that equality on [nat]s is decidable:
that is, given two [nat] values, we can always produce either
evidence that they are equal or evidence that they are not. Read
computationally, [eq_nat_dec] takes two [nat] values and returns a
[sumbool] constructed with [left] if they are equal and [right] if
they are not; this result can be tested with a [match] or, better,
with an [if-then-else], just like a regular [boolean]. (Notice
that we ended this proof with [Defined] rather than [Qed]. The
only difference this makes is that the proof becomes
_transparent_, meaning that its definition is available when Coq
tries to do reductions, which is important for the computational
interpretation.) *)
(** *** *)
(** Here's a simple example illustrating the advantages of the
[sumbool] form. *)
Definition override' {X: Type} (f: nat->X) (k:nat) (x:X) : nat->X:=
fun (k':nat) => if eq_nat_dec k k' then x else f k'.
Theorem override_same' : forall (X:Type) x1 k1 k2 (f : nat->X),
f k1 = x1 ->
(override' f k1 x1) k2 = f k2.
Proof.
intros X x1 k1 k2 f. intros Hx1.
unfold override'.
destruct (eq_nat_dec k1 k2). (* observe what appears as a hypothesis *)
Case "k1 = k2".
rewrite <- e.
symmetry. apply Hx1.
Case "k1 <> k2".
reflexivity. Qed.
(** Compare this to the more laborious proof (in MoreCoq.v) for the
version of [override] defined using [beq_nat], where we had to use
the auxiliary lemma [beq_nat_true] to convert a fact about
booleans to a Prop. *)
(** **** Exercise: 1 star (override_shadow') *)
Theorem override_shadow' : forall (X:Type) x1 x2 k1 k2 (f : nat->X),
(override' (override' f k1 x2) k1 x1) k2 = (override' f k1 x1) k2.
Proof.
intros.
unfold override'.
destruct (eq_nat_dec k1 k2). reflexivity. reflexivity.
Qed.
(** [] *)
(* ####################################################### *)
(** * Additional Exercises *)
(** **** Exercise: 3 stars (all_forallb) *)
(** Inductively define a property [all] of lists, parameterized by a
type [X] and a property [P : X -> Prop], such that [all X P l]
asserts that [P] is true for every element of the list [l]. *)
Inductive all (X : Type) (P : X -> Prop) : list X -> Prop :=
| all_nil : all X P []
| all_cons : forall (l:list X) (x:X), P x -> all X P l -> all X P (x :: l).
(** Recall the function [forallb], from the exercise
[forall_exists_challenge] in chapter [MoreCoq]: *)
Fixpoint forallb {X : Type} (test : X -> bool) (l : list X) : bool :=
match l with
| [] => true
| x :: l' => andb (test x) (forallb test l')
end.
(** Using the property [all], write down a specification for [forallb],
and prove that it satisfies the specification. Try to make your
specification as precise as possible.
Are there any important properties of the function [forallb] which
are not captured by your specification? *)
Theorem all_forallb : forall X (l:list X) (P:X->bool),
forallb P l = true <-> all X (fun x => P x = true) l.
Proof.
intros. split. induction l.
intros. apply all_nil.
simpl. intros. apply all_cons. apply andb_true_elim1 in H. apply H.
apply IHl. apply andb_true_elim2 in H. apply H.
intros. induction l. reflexivity.
simpl. inversion H. rewrite H2. simpl. apply IHl. apply H3.
Qed.
(** [] *)
(** **** Exercise: 4 stars, advanced (filter_challenge) *)
(** One of the main purposes of Coq is to prove that programs match
their specifications. To this end, let's prove that our
definition of [filter] matches a specification. Here is the
specification, written out informally in English.
Suppose we have a set [X], a function [test: X->bool], and a list
[l] of type [list X]. Suppose further that [l] is an "in-order
merge" of two lists, [l1] and [l2], such that every item in [l1]
satisfies [test] and no item in [l2] satisfies test. Then [filter
test l = l1].
A list [l] is an "in-order merge" of [l1] and [l2] if it contains
all the same elements as [l1] and [l2], in the same order as [l1]
and [l2], but possibly interleaved. For example,
[1,4,6,2,3]
is an in-order merge of
[1,6,2]
and
[4,3].
Your job is to translate this specification into a Coq theorem and
prove it. (Hint: You'll need to begin by defining what it means
for one list to be a merge of two others. Do this with an
inductive relation, not a [Fixpoint].) *)
Inductive in_order_merge {X:Type} : list X -> list X -> list X -> Prop :=
| merge_nil : in_order_merge [] [] []
| merge1 : forall x l1 l2 l3, in_order_merge l1 l2 l3 -> in_order_merge (x :: l1) l2 (x :: l3)
| merge2 : forall x l1 l2 l3, in_order_merge l1 l2 l3 -> in_order_merge l1 (x :: l2) (x :: l3).
Theorem filter_merge : forall (X:Type) (test:X->bool) (l1 l2 l3:list X),
in_order_merge l1 l2 l3 -> forallb test l1 = true ->
forallb (fun x => negb (test x)) l2 = true ->
filter test l3 = l1.
Proof.
intros X test l1 l2 l3. generalize dependent l1. generalize dependent l2.
induction l3.
intros. inversion H. reflexivity. intros. inversion H.
simpl. destruct (test x) eqn:testeq. apply f_equal. apply IHl3 with (l2:=l2).
apply H5. rewrite <- H3 in H0. simpl in H0. apply andb_true_elim2 in H0.
apply H0. apply H1. rewrite <- H3 in H0. simpl in H0. rewrite <- H2 in testeq.
rewrite testeq in H0. inversion H0.
simpl. rewrite <- H4 in H1. simpl in H1. destruct (test x) eqn:testeq.
rewrite <- H2 in testeq. rewrite testeq in H1. inversion H1.
apply IHl3 with (l2:=l4). apply H5. apply H0. apply andb_true_elim2 in H1.
apply H1.
Qed.
(** [] *)
(** **** Exercise: 5 stars, advanced, optional (filter_challenge_2) *)
(** A different way to formally characterize the behavior of [filter]
goes like this: Among all subsequences of [l] with the property
that [test] evaluates to [true] on all their members, [filter test
l] is the longest. Express this claim formally and prove it. *)
Theorem ble_nat_S : forall a b:nat, ble_nat a b = true -> ble_nat a (S b) = true.
Proof.
intros. apply ble_nat_true_trans with (m:=b).
apply H. apply NatList.ble_n_Sn.
Qed.
Theorem longest : forall (X:Type) (l1 l2:list X) (test:X->bool),
subseq l1 l2 -> forallb test l1 = true ->
ble_nat (length l1) (length (filter test l2)) = true.
Proof.
intros X l1 l2. generalize dependent l1. induction l2.
intros. inversion H. simpl. reflexivity.
intros. inversion H. reflexivity.
simpl. destruct (test x) eqn:testeq. simpl. apply ble_nat_S.
apply IHl2. apply H3. apply H0.
apply IHl2. apply H3. apply H0.
simpl. destruct (test x) eqn:testeq.
destruct (length (x :: filter test l2)) eqn:leneq.
simpl in leneq. inversion leneq. simpl in leneq. inversion leneq.
apply IHl2. apply H3. rewrite <- H2 in H0. simpl in H0.
apply andb_true_elim2 in H0. apply H0.
rewrite <- H2 in H0. simpl in H0. rewrite H1 in H0.
rewrite testeq in H0. inversion H0.
Qed.
(** [] *)
(** **** Exercise: 4 stars, advanced (no_repeats) *)
(** The following inductively defined proposition... *)
Inductive appears_in {X:Type} (a:X) : list X -> Prop :=
| ai_here : forall l, appears_in a (a::l)
| ai_later : forall b l, appears_in a l -> appears_in a (b::l).
(** ...gives us a precise way of saying that a value [a] appears at
least once as a member of a list [l].
Here's a pair of warm-ups about [appears_in].
*)
Lemma appears_in_app : forall (X:Type) (xs ys : list X) (x:X),
appears_in x (xs ++ ys) -> appears_in x xs \/ appears_in x ys.
Proof.
intros X xs. induction xs.
intros. right. apply H.
intros. inversion H. left. apply ai_here.
apply IHxs in H1. inversion H1. left. apply ai_later. apply H3.
right. apply H3.
Qed.
Lemma app_appears_in : forall (X:Type) (xs ys : list X) (x:X),
appears_in x xs \/ appears_in x ys -> appears_in x (xs ++ ys).
Proof.
intros X xs. induction xs. intros. inversion H.
inversion H0. apply H0. intros.
inversion H. inversion H0. rewrite cons_app. apply ai_here.
rewrite cons_app. apply ai_later. apply IHxs. left. apply H2.
rewrite cons_app. apply ai_later. apply IHxs. right. apply H0.
Qed.
(** Now use [appears_in] to define a proposition [disjoint X l1 l2],
which should be provable exactly when [l1] and [l2] are
lists (with elements of type X) that have no elements in common. *)
Definition disjoint {X:Type} (l1 l2:list X) : Prop :=
forall x, ~ ((appears_in x l1) /\ (appears_in x l2)).
(** Next, use [appears_in] to define an inductive proposition
[no_repeats X l], which should be provable exactly when [l] is a
list (with elements of type [X]) where every member is different
from every other. For example, [no_repeats nat [1,2,3,4]] and
[no_repeats bool []] should be provable, while [no_repeats nat
[1,2,1]] and [no_repeats bool [true,true]] should not be. *)
Inductive no_repeats {X:Type} : list X -> Prop :=
| nore1 : forall x, no_repeats [x]
| nore_cons : forall x l, ~ (appears_in x l) -> no_repeats l -> no_repeats (x :: l).
(** Finally, state and prove one or more interesting theorems relating
[disjoint], [no_repeats] and [++] (list append). *)
Theorem disjoin_nore_append : forall (X:Type) (l1 l2:list X),
no_repeats l1 -> no_repeats l2 -> disjoint l1 l2 -> no_repeats (l1 ++ l2).
Proof.
intros X l1. induction l1.
intros. apply H0.
intros. inversion H. apply nore_cons.
unfold disjoint in H1.
unfold not. intros. destruct (H1 x).
split. apply ai_here. apply H2. apply H0.
rewrite cons_app. apply nore_cons.
unfold not. intros.
unfold disjoint in H1. apply appears_in_app in H6.
destruct H6. apply contradiction_implies_anything with (P:=appears_in x l1).
split. apply H6. apply H4. destruct (H1 x).
split. apply ai_here. apply H6. apply IHl1.
apply H5. apply H0.
unfold disjoint in H1. unfold disjoint.
intros. unfold not. intros.
destruct H6. destruct (H1 x1). split.
apply ai_later. apply H6. apply H7.
Qed.
(** **** Exercise: 3 stars (nostutter) *)
(** Formulating inductive definitions of predicates is an important
skill you'll need in this course. Try to solve this exercise
without any help at all.
We say that a list of numbers "stutters" if it repeats the same
number consecutively. The predicate "[nostutter mylist]" means
that [mylist] does not stutter. Formulate an inductive definition
for [nostutter]. (This is different from the [no_repeats]
predicate in the exercise above; the sequence [1;4;1] repeats but
does not stutter.) *)
Inductive nostutter: list nat -> Prop :=
| nostu_nil : nostutter nil
| nostu1 : forall x, nostutter [x]
| nostu_cons : forall x y l, x <> y -> nostutter (y :: l) -> nostutter (x :: y :: l).
(** Make sure each of these tests succeeds, but you are free
to change the proof if the given one doesn't work for you.
Your definition might be different from mine and still correct,
in which case the examples might need a different proof.
The suggested proofs for the examples (in comments) use a number
of tactics we haven't talked about, to try to make them robust
with respect to different possible ways of defining [nostutter].
You should be able to just uncomment and use them as-is, but if
you prefer you can also prove each example with more basic
tactics. *)
Example test_nostutter_1: nostutter [3;1;4;1;5;6].
Proof. repeat constructor; apply beq_nat_false; auto. Qed.
Example test_nostutter_2: nostutter [].
Proof. apply nostu_nil. Qed.
(*
Proof. repeat constructor; apply beq_nat_false; auto. Qed.
*)
Example test_nostutter_3: nostutter [5].
Proof. apply nostu1. Qed.
(*
Proof. repeat constructor; apply beq_nat_false; auto. Qed.
*)
Example test_nostutter_4: not (nostutter [3;1;1;4]).
Proof.
unfold not. intros. inversion H.
inversion H4. unfold not in H7.
apply H7. reflexivity.
Qed.
(* Proof. intro. *)
(* repeat match goal with *)
(* h: nostutter _ |- _ => inversion h; clear h; subst *)
(* end. *)
(* contradiction H1; auto. Qed. *)
(** [] *)
(** **** Exercise: 4 stars, advanced (pigeonhole principle) *)
(** The "pigeonhole principle" states a basic fact about counting:
if you distribute more than [n] items into [n] pigeonholes, some
pigeonhole must contain at least two items. As is often the case,
this apparently trivial fact about numbers requires non-trivial
machinery to prove, but we now have enough... *)
(** First a pair of useful lemmas (we already proved these for lists
of naturals, but not for arbitrary lists). *)
Lemma app_length : forall (X:Type) (l1 l2 : list X),
length (l1 ++ l2) = length l1 + length l2.
Proof.
intros X l1. induction l1.
simpl. intros. reflexivity.
simpl. intros. apply f_equal. apply IHl1.
Qed.
Lemma appears_in_app_split : forall (X:Type) (x:X) (l:list X),
appears_in x l ->
exists l1, exists l2, l = l1 ++ (x::l2).
Proof.
intros. induction l.
inversion H. inversion H.
exists nil. exists l. reflexivity.
apply IHl in H1. destruct H1. destruct H1.
exists (x0 :: witness). exists witness0. simpl. rewrite <- H1.
reflexivity.
Qed.
(** Now define a predicate [repeats] (analogous to [no_repeats] in the
exercise above), such that [repeats X l] asserts that [l] contains
at least one repeated element (of type [X]). *)
Inductive repeats {X:Type} : list X -> Prop :=
| rep_here : forall x l, appears_in x l -> repeats (x :: l)
| rep_later : forall x l, repeats l -> repeats (x :: l).
(** Now here's a way to formalize the pigeonhole principle. List [l2]
represents a list of pigeonhole labels, and list [l1] represents
the labels assigned to a list of items: if there are more items
than labels, at least two items must have the same label. This
proof is much easier if you use the [excluded_middle] hypothesis
to show that [appears_in] is decidable, i.e. [forall x
l, (appears_in x l) \/ ~ (appears_in x l)]. However, it is also
possible to make the proof go through _without_ assuming that
[appears_in] is decidable; if you can manage to do this, you will
not need the [excluded_middle] hypothesis. *)
Theorem pigeonhole_principle: forall (X:Type) (l1 l2:list X),
excluded_middle ->
(forall x, appears_in x l1 -> appears_in x l2) ->
length l2 < length l1 ->
repeats l1.
Proof.
intros X l1. induction l1 as [|x l1']; intros.
inversion H1.
destruct (H (appears_in x l1')).
apply rep_here. apply H2. apply rep_later.
destruct (H (appears_in x l2)). apply appears_in_app_split in H3.
inversion H3. inversion H4.
apply IHl1' with (l2:=(witness ++ witness0)).
apply H. intros. destruct (H (x0 = x)). subst. contradiction.
apply ai_later with (b:=x) in H6. apply H0 in H6.
rewrite H5 in H6. apply appears_in_app in H6. apply app_appears_in.
destruct H6. left. apply H6. right.
inversion H6. contradiction. assumption.
rewrite H5 in H1. rewrite app_length in H1.
simpl in H1. rewrite <- plus_n_Sm in H1.
apply Sn_le_Sm__n_le_m in H1.
unfold lt. rewrite app_length. apply H1.
apply contradiction_implies_anything with (P:=(appears_in x l2)).
split. apply H0. apply ai_here. apply H3.
Qed.
(** [] *)
(** $Date: 2014-12-31 16:01:37 -0500 (Wed, 31 Dec 2014) $ *)