-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCompounds.v
694 lines (609 loc) · 21.6 KB
/
Compounds.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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
(**********************************************************************)
(* This program is free software; you can redistribute it and/or *)
(* modify it under the terms of the GNU Lesser General Public License *)
(* as published by the Free Software Foundation; either version 2.1 *)
(* of the License, or (at your option) any later version. *)
(* *)
(* This program is distributed in the hope that it will be useful, *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)
(* GNU General Public License for more details. *)
(* *)
(* You should have received a copy of the GNU Lesser General Public *)
(* License along with this program; if not, write to the Free *)
(* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *)
(* 02110-1301 USA *)
(**********************************************************************)
(**********************************************************************)
(* Typed LambdaFactor Calculus *)
(* *)
(* is implemented in Coq by adapting the implementation of *)
(* Lambda Calculus from Project Coq *)
(* 2015 *)
(**********************************************************************)
(**********************************************************************)
(* Compounds.v *)
(* *)
(* Barry Jay *)
(* *)
(**********************************************************************)
Require Import Arith.
Require Import Test.
Require Import General.
Require Import LamSF_Terms.
Require Import LamSF_Tactics.
Require Import LamSF_Substitution_term.
Require Import Components.
(* status *)
(* If a term has a head reduction then it is Reducible.
If it is an abstraction with an active variable then it is a Lam.
If it is a variable application then it is Active.
Operators and compounds are characterised by the influence of additional arguments.
*)
Inductive status_val :=
| Reducible : status_val
| Lam : nat -> status_val (* the active variable *)
| Active : nat -> status_val (* the active variable *)
| Ternary_op (* S, A *)
| Binary_op0 (* K *)
| Binary_op2 (* E *)
| Binary_op1 (* G, U *)
| Ternary_op1 (* Q, case operators *)
| Unary_op (* Y *)
| Lazy2 (* need two args *)
| Lazy1 (* need one arg *)
| Eager2 (* QM, DOM *)
| Eager (* EO *)
.
(* If application of an operator to one argument produces a compound,
then it uses the same atom_status, e.g. status (KM = Atom 0 *)
Fixpoint status (M: lamSF) :=
match M with
| Ref i => Active i
| Op Sop | Op Aop => Ternary_op
| Op Kop => Binary_op0
| Op Eop => Binary_op2
| Op Gop | Op Uop => Binary_op1
| Op Yop => Unary_op
| Op _ => Ternary_op1
| Abs M1 => match status M1 with
| Reducible => Reducible
| Lam 0 => Lazy1
| Lam (S n) => Lam n
| Active 0 => Lazy1
| Active (S n) => Lam n
| _ => Lazy1
end
| App M1 M2 => match status M1 with
| Reducible => Reducible
| Lam _ => Reducible
| Active n => Active n
| Ternary_op => Lazy2
| Binary_op0 => Lazy1
| Binary_op2 =>
match status M2 with
| Reducible => Reducible
| Lam n => Active n
| Active n => Active n
| Ternary_op
| Binary_op0
| Binary_op2
| Binary_op1
| Ternary_op1
| Unary_op => Eager
| _ => Reducible
end
| Binary_op1 => Eager
| Ternary_op1 => Eager2
| Unary_op => Reducible
| Lazy2 => Lazy1
| Lazy1 => Reducible
| Eager2 => Eager
| Eager =>
match status M2 with
| Lam n => Active n
| Active n => Active n
| _ => Reducible
end
end
end.
Lemma star_compound :
forall M, status (star M) = Lazy1 \/ status(star M) = Lazy2.
Proof. induction M; split_all; try (case n); split_all. Qed.
Definition compound M :=
(status M = Lazy2) \/
(status M = Lazy1) \/
(status M = Eager2) \/
(status M = Eager).
Definition factorable M :=
(status M = Ternary_op) \/
(status M = Binary_op0) \/
(status M = Binary_op2) \/
(status M = Binary_op1) \/
(status M = Ternary_op1) \/
(status M = Unary_op) \/
(status M = Lazy2) \/
(status M = Lazy1) \/
(status M = Eager2) \/
(status M = Eager).
Lemma compounds_are_factorable: forall M, compound M -> factorable M.
Proof. split_all. unfold factorable; inversion H; split_all; auto 10. Qed.
Lemma factorable_applications_are_compounds:
forall M N, factorable (App M N) -> compound(App M N).
Proof.
unfold factorable, compound; split_all. gen_case H (status M);
try (inversion H; [ discriminate |
inversion H0; [ discriminate |
inversion H1; [ discriminate |
inversion H2; [ discriminate |
inversion H3; [ discriminate |
inversion H4; discriminate ]]]]]); or_tac.
(* 2 *)
gen_case H (status N);
try (inversion H; [ discriminate |
inversion H0; [ discriminate |
inversion H1; [ discriminate |
inversion H2; [ discriminate |
inversion H3; [ discriminate |
inversion H4; discriminate ]]]]]); or_tac.
(* 1 *)
gen_case H (status N);
try (inversion H; [ discriminate |
inversion H0; [ discriminate |
inversion H1; [ discriminate |
inversion H2; [ discriminate |
inversion H3; [ discriminate |
inversion H4; discriminate ]]]]]); or_tac.
Qed.
Lemma factorable_abstractions_are_compounds:
forall M, factorable (Abs M) -> compound (Abs M).
Proof.
unfold factorable, compound; split_all. gen_case H (status M); or_tac; gen_case H n; or_tac.
Qed.
Lemma factorable_implies_compound_or_operator:
forall M, factorable M -> (compound M \/ exists o, M = Op o).
Proof.
unfold factorable, compound; split_all. gen_case H M; or_tac.
right; eauto.
gen_case H (status l); or_tac; gen_case H n; or_tac.
gen_case H (status l); or_tac; gen_case H (status l0); or_tac.
Qed.
Definition preserves_components_l (red: termred) :=
forall M, factorable M -> forall N, red M N -> factorable N /\
multi_step red (left_component M) (left_component N).
Lemma preserves_components_l_multi_step :
forall red, preserves_components_l red ->
forall M, factorable M -> forall N, multi_step red M N -> factorable N /\
multi_step red (left_component M) (left_component N).
Proof.
intros red p M c N R; induction R; split_all.
eapply2 IHR. eapply2 p.
apply transitive_red with (left_component N); split_all.
eapply2 p. eapply2 IHR. eapply2 p.
Qed.
Definition preserves_components_r (red: termred) :=
forall M, factorable M -> forall N, red M N -> factorable N /\
multi_step red (right_component M) (right_component N).
Lemma preserves_components_r_multi_step :
forall red, preserves_components_r red ->
forall M, factorable M -> forall N, multi_step red M N -> factorable N /\
multi_step red (right_component M) (right_component N).
Proof.
intros red p M c N R; induction R; split_all.
eapply2 IHR. eapply2 p.
apply transitive_red with (right_component N); split_all.
eapply2 p. eapply2 IHR. eapply2 p.
Qed.
Definition relocate_status a n k :=
match a with
| Lam i => Lam (relocate i n k)
| Active i => Active (relocate i n k)
| _ => a
end.
Lemma rank_compound_l : forall M, compound M -> rank (left_component M) < rank M.
Proof.
induction M; split_all; inv1 compound; simpl in *; try discriminate; try (gen_case H0 o); or_tac.
(* 4 *)
assert(rank M >0) by eapply rank_positive; omega.
(* 3 *)
assert(rank M >0) by eapply rank_positive; omega.
(* 2 *)
gen2_case IHM1 H0 (status M1); omega.
(* 1 *)
gen2_case IHM1 H0 (status M1); omega.
Qed.
Lemma rank_compound_r : forall M, compound M -> rank (right_component M) < rank M.
Proof.
induction M; split_all; inv1 compound; simpl in *; try discriminate; try (gen_case H0 o); or_tac.
(* 4 *)
gen_case H0 (status M); gen_case H0 n.
(* 3 *)
gen2_case IHM H0 (status M);
assert(rank (star M) < rank (Abs M)) by eapply2 rank_star; simpl in *; omega.
(* 2 *)
gen2_case IHM1 H0 (status M1). omega.
gen2_case IHM2 H0 (status M2).
gen2_case IHM2 H0 (status M2). omega.
Qed.
Lemma lift_rec_preserves_status :
forall (M: lamSF) (n k: nat), status (lift_rec M n k) = relocate_status (status M) n k.
Proof.
rank_tac.
induction M; split_all; try (rewrite relocate_succ; auto);
try (eapply2 IHM1).
(* 3 *)
case o; split_all.
(* 2 *)
rewrite IHM; try omega.
unfold relocate_status.
case (status M); split_all.
unfold relocate.
elim(test (S n) n0); split_all; try noway.
replace (k+ n0) with (S (k+pred n0)) by omega.
replace n0 with (S (pred n0)) by omega.
elim(test n (pred n0)); split_all; try noway.
gen_case b n0.
elim(test n n1); split_all; try noway.
unfold relocate.
elim(test (S n) n0); split_all; try noway.
replace (k+ n0) with (S (k+pred n0)) by omega.
replace n0 with (S (pred n0)) by omega.
elim(test n (pred n0)); split_all; try noway.
gen_case b n0.
elim(test n n1); split_all; try noway.
(* 1 *)
rewrite IHM1; try omega.
unfold relocate_status.
case (status M1); split_all;
rewrite IHM2; try omega;
unfold relocate_status;
case (status M2); split_all.
Qed.
Hint Resolve lift_rec_preserves_status.
Lemma lam_is_abs : forall M i, status M = Lam i -> exists N, M = Abs N.
Proof.
induction M; split_all; eauto.
gen_case H o.
gen_case H (status M1); gen_case H (status M2).
Qed.
Lemma subst_rec_preserves_status:
forall (M: lamSF)(k : nat),
(forall i, status M = Active i -> i < k) ->
(forall i, status M = Lam i -> i < k) ->
forall N, status (subst_rec M N k) = status M.
Proof.
rank_tac.
induction M; intros.
(* 4 *)
invsub. insert_Ref_out; split_all.
(* 3 *)
split_all.
(* 2 *)
simpl in *.
gen4_case IHM H H0 H1 (status M); try (rewrite IHM; split_all; omega).
(* 3 *)
gen4_case IHM H H0 H1 n.
rewrite IHM; split_all; try omega. invsub; omega.
rewrite IHM; split_all; try omega. invsub. assert(n0<k) by eapply2 H1. omega.
(* 2 *)
gen4_case IHM H H0 H1 n.
rewrite IHM; split_all; try omega. invsub; omega.
rewrite IHM; split_all; try omega. invsub. assert(n0<k) by eapply2 H1. omega.
(* 1 Applications *)
clear H1.
assert(forall i, status M1 = Lam i -> exists N, M1 = Abs N) by eapply2 lam_is_abs.
simpl in *. gen3_case IHM1 H0 H1 (status M1);
try (rewrite IHM1; split_all; simpl in *; omega).
(* 3 *)
assert(exists N, M1 = Abs N) by eapply2 H1. split_all; subst.
simpl.
case(status (subst_rec x N (S k))); split_all.
case n0; split_all.
case n0; split_all.
(* 2 *)
rewrite IHM1; simpl in *; split_all; try omega.
rewrite IHM2; split_all; try omega.
rewrite H2 in H0. eapply2 H0.
rewrite H2 in H0. eapply2 H0.
(* 1 *)
rewrite IHM1; simpl in *; split_all; try omega.
rewrite IHM2; split_all; try omega.
rewrite H2 in H0. eapply2 H0.
rewrite H2 in H0. eapply2 H0.
Qed.
Lemma rank_component_app_l:
forall M N, rank (left_component (App M N)) < rank (App M N).
Proof. split_all; omega. Qed.
Lemma rank_component_app_r:
forall M N, rank (right_component (App M N)) < rank (App M N).
Proof. split_all; omega. Qed.
Lemma rank_component_abs_l:
forall M, rank (left_component (Abs M)) < rank (Abs M).
Proof.
induction M; intros.
(* 4 *)
split_all; try omega.
(* 3 *)
split_all; try omega.
(* 2 *)
assert(rank M >0) by eapply2 rank_positive.
unfold left_component. unfold_op; unfold rank; fold rank.
replace (abs_rank * (abs_rank * rank M)) with (abs_rank * abs_rank * rank M) by ring.
cut (S (S (1 + S (S (1 + S (1 + 1)) + 1)) + S (1 + S (S (1 + 1) + 1))) < abs_rank * abs_rank).
2: unfold abs_rank; split_all; omega. intro.
replace (rank M) with (1+ (pred (rank M))) by omega.
replace(abs_rank * abs_rank * (1+ (pred (rank M)))) with
(abs_rank * abs_rank + abs_rank * abs_rank * (pred (rank M))) by ring.
unfold abs_left, abs_rank; unfold_op. unfold rank at 1.
assert(1<6*6) by (simpl; omega). omega.
(* 1 *)
unfold left_component, abs_left, abs_rank; unfold_op. simpl. omega.
Qed.
Lemma rank_component_abs_r:
forall M, rank (right_component (Abs M)) < rank (Abs M).
Proof.
unfold right_component. intros. eapply2 rank_star.
Qed.
Lemma subst_rec_preserves_star_active :
forall (M : lamSF) N i k, status M = Active i -> i <= k ->
subst_rec(star M) N k = star (subst_rec M N (S k)).
Proof.
induction M; split_all.
gen_case H n.
unfold insert_Ref.
elim(compare k n0); elim(compare (S k) (S n0));split_all; try noway.
elim a; elim a0; split_all; try noway.
invsub; noway.
inversion H. noway.
elim a; split_all; try noway.
elim a; split_all; try noway.
gen2_case IHM H (status M).
replace (subst_rec (star M) N (S k)) with (star (subst_rec M N (S(S k)))).
auto.
eapply2 eq_sym.
eapply2 IHM.
gen_case H n. gen_case H n.
Qed.
Lemma subst_rec_preserves_star_lam :
forall (M : lamSF) N i k, status M = Lam i -> i <= k ->
subst_rec(star M) N k = star (subst_rec M N (S k)).
Proof.
induction M; split_all.
assert(status M = Lam (S i) \/ status M = Active (S i)).
gen2_case IHM H (status M); gen_case H n; inversion H; subst; auto.
inversion H1.
(* 2 *)
rewrite (IHM N (S i) (S k)); auto. omega.
(* 1 *)
rewrite (subst_rec_preserves_star_active M N (S i) (S k)); auto. omega.
Qed.
Lemma subst_rec_preserves_star_factorable :
forall (M : lamSF) N k, factorable M ->
subst_rec(star M) N k = star(subst_rec M N (S k)).
Proof.
induction M; split_all; inv1 factorable; simpl in *; try discriminate; or_tac.
(* 2 *)
gen_case H0 (status M); gen_case H0 n.
(* 1 *)
assert( match status M with
| Reducible => Reducible
| Lam 0 => Lazy1
| Lam (S n) => Lam n
| Active 0 => Lazy1
| Active (S n) => Lam n
| Ternary_op => Lazy1
| Binary_op0 => Lazy1
| Binary_op2 => Lazy1
| Binary_op1 => Lazy1
| Ternary_op1 => Lazy1
| Unary_op => Lazy1
| Lazy2 => Lazy1
| Lazy1 => Lazy1
| Eager2 => Lazy1
| Eager => Lazy1
end = Lazy1).
gen_case H0 (status M).
(* 4 *)
or_tac.
gen_case H0 n; or_tac.
gen_case H0 n; or_tac.
clear H0.
(* 1 *)
assert(status M = Lam 0 -> 0 <= (S k) ->
subst_rec(star M) N (S k) = star (subst_rec M N (S (S k)))) by eapply2 subst_rec_preserves_star_lam.
assert(status M = Active 0 -> 0 <= (S k) ->
subst_rec(star M) N (S k) = star (subst_rec M N (S (S k)))) by eapply2 subst_rec_preserves_star_active.
unfold factorable in *.
gen4_case IHM H H0 H1 (status M);
try (gen3_case H H0 H1 n); try (rewrite IHM; auto; right; right; auto; fail).
(* 5 *)
rewrite H0; auto; omega.
rewrite H1; auto; omega.
rewrite IHM; auto 10.
rewrite IHM; auto 10.
rewrite IHM; auto 10.
rewrite IHM; auto 10.
Qed.
Lemma subst_rec_preserves_components_l : forall (M : lamSF) n k, factorable M ->
subst_rec(left_component M) n k = left_component(subst_rec M n k).
Proof. induction M; split_all; inv1 factorable; simpl in *; or_tac. Qed.
Lemma subst_rec_preserves_components_active_r :
forall (M : lamSF) i, status M = Active i -> forall N k, i < k ->
subst_rec(right_component M) N k = right_component(subst_rec M N k).
Proof.
induction M; split_all.
(* 2 *)
invsub.
insert_Ref_out; split_all.
assert(forall i k, status M = Active i -> i <= k ->
subst_rec(star M) N k = star (subst_rec M N (S k)))
by eapply2 subst_rec_preserves_star_active.
gen3_case IHM H H1 (status M).
gen_case H n.
gen_case H n.
Qed.
Lemma subst_rec_preserves_components_lam_r :
forall (M : lamSF) i, status M = Lam i -> forall N k, i < k ->
subst_rec(right_component M) N k = right_component(subst_rec M N k).
Proof.
induction M; split_all.
assert(status M = Lam (S i) \/ status M = Active (S i)).
gen_case H (status M); gen_case H n; invsub.
inversion H1.
rewrite (subst_rec_preserves_star_lam M N (S i) k); auto.
rewrite (subst_rec_preserves_star_active M N (S i) k); auto.
Qed.
Lemma subst_rec_preserves_components_compound_r :
forall (M : lamSF), factorable M -> forall n k,
subst_rec(right_component M) n k = right_component(subst_rec M n k).
Proof.
induction M; split_all.
(* 2 *)
inv1 factorable; simpl in *; or_tac.
(* 1 *)
assert(status M = Lam 0 \/ status M = Active 0 \/ factorable M).
inv1 factorable; simpl in *; try discriminate.
gen_case H0 (status M); gen_case H0 n0.
assert(match status M with
| Reducible => Reducible
| Lam 0 => Lazy1
| Lam (S n) => Lam n
| Active 0 => Lazy1
| Active (S n) => Lam n
| Ternary_op => Lazy1
| Binary_op0 => Lazy1
| Binary_op2 => Lazy1
| Binary_op1 => Lazy1
| Ternary_op1 => Lazy1
| Unary_op => Lazy1
| Lazy2 => Lazy1
| Lazy1 => Lazy1
| Eager2 => Lazy1
| Eager => Lazy1
end = Lazy1).
gen_case H0 (status M); or_tac; gen_case H0 n0; or_tac.
(* 2 *)
clear H0.
unfold factorable.
gen_case H (status M); try (gen_case H n0); auto 20.
(* 1 *)
inversion H0. rewrite (subst_rec_preserves_star_lam M n 0); auto; omega.
inversion H1. rewrite (subst_rec_preserves_star_active M n 0); auto; omega.
rewrite (subst_rec_preserves_star_factorable M n k); auto; omega.
Qed.
Definition preserves_compound (red:termred) :=
forall M , compound M -> forall N, red M N ->
compound N /\ red (left_component M) (left_component N) /\ red(right_component M) (right_component N).
Lemma preserves_compound_seq :
forall (red1 red2:termred), preserves_compound red1 -> preserves_compound red2 ->
preserves_compound (sequential red1 red2).
Proof.
red; split_all.
inversion H2.
elim(H M H1 N0); split_all.
eapply2 H0.
inversion H2.
elim(H M H1 N0); split_all.
elim(H0 N0 H9 N); split_all.
eapply2 seq_red.
inversion H2.
elim(H M H1 N0); split_all.
elim(H0 N0 H9 N); split_all.
eapply2 seq_red.
Qed.
Lemma preserves_compound_multi_step :
forall (red:termred), preserves_compound red -> preserves_compound (multi_step red).
Proof.
red. intros red p M c N R; induction R; split_all.
eapply2 IHR. eapply2 p.
apply succ_red with (left_component N); auto.
eapply2 p. eapply2 IHR. eapply2 p.
apply succ_red with (right_component N); auto.
eapply2 p. eapply2 IHR. eapply2 p.
Qed.
Hint Resolve preserves_compound_multi_step.
Lemma lift_rec_preserves_factorable:
forall M n k, factorable M -> factorable (lift_rec M n k).
Proof.
intros.
assert(status (lift_rec M n k) = relocate_status (status M) n k)
by eapply2 lift_rec_preserves_status.
unfold factorable in *.
rewrite H0.
inversion H. rewrite H1; auto.
inversion H1. rewrite H2; auto.
inversion H2. rewrite H3; auto.
inversion H3. rewrite H4; auto.
inversion H4. rewrite H5; auto 20.
inversion H5. rewrite H6; auto 20.
inversion H6. rewrite H7; auto 20.
inversion H7. rewrite H8; auto 20.
inversion H8; rewrite H9; auto 20.
Qed.
Lemma subst_rec_preserves_factorable:
forall M, factorable M -> forall N k, factorable (subst_rec M N k).
Proof.
intros M f.
inversion f; split_all.
assert(status (subst_rec M N k) = status M) .
eapply2 subst_rec_preserves_status; split_all.
unfold factorable; rewrite H0; auto.
inversion H; split_all.
assert(status (subst_rec M N k) = status M) .
eapply2 subst_rec_preserves_status; split_all.
unfold factorable; rewrite H1; auto.
inversion H0; split_all.
assert(status (subst_rec M N k) = status M) .
eapply2 subst_rec_preserves_status; split_all.
unfold factorable; rewrite H2; auto.
inversion H1; split_all.
assert(status (subst_rec M N k) = status M) .
eapply2 subst_rec_preserves_status; split_all.
unfold factorable; rewrite H3; auto.
inversion H2; split_all.
assert(status (subst_rec M N k) = status M) .
eapply2 subst_rec_preserves_status; split_all.
unfold factorable; rewrite H4; auto.
inversion H3; split_all.
assert(status (subst_rec M N k) = status M) .
eapply2 subst_rec_preserves_status; split_all.
unfold factorable; rewrite H5; auto.
assert(status (subst_rec M N k) = status M) .
eapply2 subst_rec_preserves_status; split_all.
rewrite H5 in *. or_tac.
rewrite H5 in *. or_tac.
unfold factorable; rewrite H5; auto.
Qed.
Lemma status_app_active:
forall M N i, status (App M N) = Active i ->
status M = Active i \/ status N = Active i \/ status N = Lam i.
Proof.
induction M; split_all.
(* 3 *)
gen_case H o; gen_case H (status N); invsub.
(* 2 *)
gen_case H (status M); gen_case H n.
(* 1 *)
gen_case H (status M1); gen_case H (status M2); gen_case H (status N); invsub.
Qed.
Lemma lift_rec_preserves_compound:
forall M n k, compound M -> compound (lift_rec M n k).
Proof.
intros.
assert(status (lift_rec M n k) = relocate_status (status M) n k)
by eapply2 lift_rec_preserves_status.
unfold compound in *.
rewrite H0.
inversion H. rewrite H1; auto.
inversion H1. rewrite H2; auto.
inversion H2; rewrite H3; auto.
Qed.
Lemma subst_rec_preserves_compound:
forall M, compound M -> forall N k, compound (subst_rec M N k).
Proof.
split_all.
assert(factorable M) by eapply2 compounds_are_factorable.
assert(factorable (subst_rec M N k)) by eapply2 subst_rec_preserves_factorable.
elim(factorable_implies_compound_or_operator (subst_rec M N k)); split_all.
gen2_case H H3 M. unfold compound in H; simpl in *; or_tac.
Qed.