-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRewriting_theorems.v
1783 lines (1481 loc) · 76.2 KB
/
Rewriting_theorems.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
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(**********************************************************************)
(* Copyright 2020 Barry Jay *)
(* *)
(* Permission is hereby granted, free of charge, to any person *)
(* obtaining a copy of this software and associated documentation *)
(* files (the "Software"), to deal in the Software without *)
(* restriction, including without limitation the rights to use, copy, *)
(* modify, merge, publish, distribute, sublicense, and/or sell copies *)
(* of the Software, and to permit persons to whom the Software is *)
(* furnished to do so, subject to the following conditions: *)
(* *)
(* The above copyright notice and this permission notice shall be *)
(* included in all copies or substantial portions of the Software. *)
(* *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *)
(* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *)
(* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *)
(* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *)
(* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, *)
(* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *)
(* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)
(* DEALINGS IN THE SOFTWARE. *)
(**********************************************************************)
(**********************************************************************)
(* Reflective Programs in Tree Calculus *)
(* Chapter 7: Rewriting (theorems) *)
(* *)
(* Barry Jay *)
(* *)
(**********************************************************************)
Require Import Lia Bool List String.
Require Import Reflective.Rewriting_partI.
Set Default Proof Using "Type".
Open Scope string_scope.
(* new theorems that require rewriting *)
(* Confluence *)
(* Simultaneous Reduction *)
Inductive s_red1 : Tree -> Tree -> Prop :=
| ref_sred : forall i, s_red1 (Ref i) (Ref i)
| node_sred : s_red1 △ △
| k_sred : forall M M' N, s_red1 M M' -> s_red1 (△@△@M@N) M'
| s_sred: forall (M M' N N' P P': Tree), s_red1 M M' -> s_red1 N N' -> s_red1 P P' ->
s_red1 (△@(△@P)@M@N) (M'@N'@(P'@N'))
| f_sred : forall (P P' Q Q' M N N': Tree), s_red1 P P' -> s_red1 Q Q' -> s_red1 N N' ->
s_red1 (△@(△@P@Q)@M@N) (N'@P'@Q')
| app_sred : forall M M' N N', s_red1 M M' -> s_red1 N N' -> s_red1 (M@N) (M'@N')
.
Hint Constructors s_red1 : TreeHintDb.
Lemma s_red_refl: forall M, s_red1 M M.
Proof. induction M; intros; auto with TreeHintDb. Qed.
Hint Resolve s_red_refl: TreeHintDb.
Definition s_red := multi_step s_red1.
Lemma preserves_appl_s_red : preserves_appl s_red.
Proof. apply preserves_appl_multi_step. red; auto with *. Qed.
Lemma preserves_appr_s_red : preserves_appr s_red.
Proof. apply preserves_appr_multi_step. red; auto with *. Qed.
Lemma preserves_app_s_red : preserves_app s_red.
Proof. apply preserves_app_multi_step; red; auto with *. Qed.
Definition implies_red (red1 red2: Tree-> Tree-> Prop) := forall M N, red1 M N -> red2 M N.
Lemma implies_red_multi_step: forall red1 red2, implies_red red1 (multi_step red2) ->
implies_red (multi_step red1) (multi_step red2).
Proof. red. intros red1 red2 IR M N R; induction R; split_all; apply transitive_red with N; auto. Qed.
Definition diamond (red1 red2 : Tree -> Tree -> Prop) :=
forall M N, red1 M N -> forall P, red2 M P -> exists Q, red2 N Q /\ red1 P Q.
Lemma diamond_flip: forall red1 red2, diamond red1 red2 -> diamond red2 red1.
Proof. unfold diamond; intros red1 red2 d M N r1 P r2; elim (d M P r2 N r1); intros x ?; exist x. Qed.
Lemma diamond_strip :
forall red1 red2, diamond red1 red2 -> diamond red1 (multi_step red2).
Proof.
intros red1 red2 d; apply diamond_flip;
intros M N r; induction r; intros P0 r0; auto_t;
elim (d M P0 r0 N); auto; intros x (?&?); elim(IHr d x); split_all.
Qed.
Definition diamond_star (red1 red2: Tree -> Tree -> Prop) := forall M N, red1 M N -> forall P, red2 M P ->
exists Q, red1 P Q /\ multi_step red2 N Q.
Lemma diamond_star_strip: forall red1 red2, diamond_star red1 red2 -> diamond (multi_step red2) red1 .
Proof.
intros red1 red2 d M N r; induction r; intros P0 r0; auto_t;
elim(d M P0 r0 N); auto; intros x (?&?);
elim(IHr d x); auto; intros x0 (?&?); exist x0; eapply2 transitive_red.
Qed.
Theorem plus_0_r : forall n:nat, n + 0 = n.
Proof. induction n; split_all. Qed.
Lemma diamond_tiling :
forall red1 red2, diamond red1 red2 -> diamond (multi_step red1) (multi_step red2).
Proof.
intros red1 red2 d M N r; induction r as [ | red1 P P1 Q H]; intros P0 ?; auto_t;
elim(diamond_strip red1 red2 d P P1 H P0); auto; intros x (?&?);
elim(IHr d x); auto; intros x0 (?&?); exist x0.
Qed.
Hint Resolve diamond_tiling: TreeHintDb.
Theorem diamond_s_red1_s_red1 : diamond s_red1 s_red1.
Proof.
red; intros M N OR; induction OR; intros R s0; inv s_red1; auto_t.
(* 9 *)
match goal with | sM: s_red1 M ?M0 |- _ =>
elim(IHOR M0); auto; intros M1 (r1&r2); exist M1 end.
match goal with | sM: s_red1 M ?M0 , sN: s_red1 N ?N0, sP: s_red1 P ?P0 |- _ =>
elim(IHOR1 M0); auto; intros M1 (?&?);
elim(IHOR2 N0); auto; intros N1 (?&?);
elim(IHOR3 P0); auto; intros P1 (?&?); exist (M1 @ N1 @ (P1 @ N1)) end.
match goal with | sM: s_red1 M ?M0 , sN: s_red1 N ?N0, sP: s_red1 P ?P0 |- _ =>
elim(IHOR1 M0); auto; intros M1 (?&?);
elim(IHOR2 N0); auto; intros N1 (?&?);
elim(IHOR3 P0); auto; intros P1 (?&?); exist (M1 @ N1 @ (P1 @ N1)) end.
match goal with | sP: s_red1 P ?P0 , sQ: s_red1 Q ?Q0, sN: s_red1 N ?N0 |- _ =>
elim(IHOR1 P0); auto; intros P1 (?&?);
elim(IHOR2 Q0); auto; intros Q1 (?&?);
elim(IHOR3 N0); auto; intros N1 (?&?); exist (N1 @ P1 @ Q1) end.
match goal with | sP: s_red1 P ?P0 , sQ: s_red1 Q ?Q0 , sN: s_red1 N ?N0 |- _ =>
elim(IHOR1 P0); auto; intros P1 (?&?);
elim(IHOR2 Q0); auto; intros Q1 (?&?);
elim(IHOR3 N0); auto; intros N1 (?&?); exist (N1 @ P1 @ Q1) end.
inv s_red1; elim(IHOR1 (△ @ △ @ R)); auto with TreeHintDb; intros R1 (?&?); inv s_red1; inv s_red1;
repeat eexists; [| eassumption]; eapply k_sred; congruence.
match goal with
| s0 : s_red1 (△ @ (△ @ ?P) @ ?M2) ?M', sP: s_red1 P ?P0, sM: s_red1 ?M2 ?M0, sN: s_red1 N ?N0 |- _ =>
elim(IHOR1 (△ @ (△ @ P0) @ M0)); auto_t; intros R1 (?&?);
elim(IHOR2 N0); auto_t; intros N1 (?&?); inv s_red1; inv s_red1;
repeat eexists; [| repeat apply app_sred; eauto]; auto_t end.
match goal with
| s0 : s_red1 (△ @ (△ @ ?P @ ?Q) @ ?M2) ?M', sP: s_red1 P ?P0, sQ: s_red1 Q ?Q0, sN: s_red1 N ?N0 |- _ =>
elim(IHOR1 (△ @ (△ @ P0 @ Q0) @ M0)); auto_t; intros R1 (?&?);
elim(IHOR2 N0); auto_t; intros N1 (?&?); inv s_red1; inv s_red1;
repeat eexists; [| repeat apply app_sred; eauto]; auto_t end.
match goal with | sM: s_red1 M ?M0 , sN: s_red1 N ?N0 |- _ =>
elim(IHOR1 M0); auto; intros M1 (?&?); elim(IHOR2 N0); auto; intros N1 (?&?); intros;
exist(M1 @ N1) end.
Qed.
Hint Resolve diamond_s_red1_s_red1: TreeHintDb.
Lemma diamond_s_red1_s_red : diamond s_red1 s_red.
Proof. apply diamond_strip; auto_t. Qed.
Lemma diamond_s_red : diamond s_red s_red.
Proof. apply diamond_tiling; auto_t. Qed.
Hint Resolve diamond_s_red: TreeHintDb.
Lemma t_red1_to_s_red1 : implies_red t_red1 s_red1.
Proof. intros M N R; induction R; split_all. Qed.
Lemma t_red_to_s_red: implies_red t_red s_red.
Proof.
apply implies_red_multi_step; red; intros; eapply succ_red; [eapply2 t_red1_to_s_red1 | zerotac].
Qed.
Lemma to_t_red_multi_step: forall red, implies_red red t_red -> implies_red (multi_step red) t_red.
Proof.
intros red B M N R; induction R; intros; [ zerotac |];
assert(t_red M N) by (apply B; auto); apply transitive_red with N; [ | apply IHR] ; auto.
Qed.
Lemma s_red1_to_t_red: implies_red s_red1 t_red .
Proof.
intros M N OR; induction OR; intros; zerotac;
try (apply preserves_app_t_red; auto; fail);
eapply2 succ_red;
repeat apply preserves_app_t_red; auto.
Qed.
Hint Resolve s_red1_to_t_red: TreeHintDb.
Lemma s_red_to_t_red: implies_red s_red t_red.
Proof. eapply2 to_t_red_multi_step. Qed.
Lemma diamond_t_red: diamond t_red t_red.
Proof.
red; intros.
assert(rMN: s_red M N) by (apply t_red_to_s_red; auto_t).
assert(s_red M P) by (apply t_red_to_s_red; auto_t).
elim(diamond_s_red M N rMN P); auto; intros x (?&?); exist x; eapply2 s_red_to_t_red.
Qed.
Hint Resolve diamond_t_red: TreeHintDb.
(* Confluence *)
Definition confluence (A : Set) (R : A -> A -> Prop) :=
forall x y : A,
R x y -> forall z : A, R x z -> exists u : A, R y u /\ R z u.
Theorem confluence_tree_calculus: confluence Tree t_red.
Proof. apply diamond_t_red. Qed.
Lemma programs_are_stable: forall M, program M -> forall N, s_red1 M N -> N = M.
Proof. intros M n; induction n; intros; inv s_red1; repeat f_equal; auto. Qed.
Lemma programs_are_stable2: forall M N, s_red M N -> program M -> N = M.
Proof.
cut(forall red M N, multi_step red M N -> red = s_red1 -> program M -> N = M).
intro c; intros; eapply c; eauto.
intros red M N r; induction r; intros; subst; zerotac.
assert(N = M) by (eapply programs_are_stable; eauto); subst; auto.
Qed.
Lemma t_red_preserves_stems:
forall M N, t_red M N -> forall M0, M = △ @ M0 -> exists N0, N = △ @ N0 /\ t_red M0 N0.
Proof.
cut (forall red M N, multi_step red M N -> red = t_red1 ->
forall M0, M = △ @ M0 -> exists N0, N = △ @ N0 /\ t_red M0 N0).
intro H; intros; eapply H; eauto.
intros red M N r; induction r; intros e M0 eM; subst.
exist M0; zerotac.
inv t_red1. assert(H: exists N0, P = △ @ N0 /\ t_red N' N0) by (eapply IHr; eauto).
elim H; clear H; intros ? (?&?); subst; eexists; split; [eauto | eapply2 succ_red].
Qed.
Lemma t_red_preserves_forks:
forall M N, t_red M N -> forall M0 M1, M = △ @ M0 @ M1 ->
exists N0 N1, N = △ @ N0 @ N1 /\ t_red M0 N0 /\ t_red M1 N1.
Proof.
cut (forall red M N, multi_step red M N -> red = t_red1 ->
forall M0 M1, M = △ @ M0 @ M1 ->
exists N0 N1, N = △ @ N0 @ N1 /\ t_red M0 N0 /\ t_red M1 N1).
intro H; intros; eapply H; eauto.
intros red M N r; induction r; intros e M0 M1 eM; subst.
exists M0, M1; split_all; zerotac.
{ assert (e: t_red1 = t_red1) by reflexivity; inv t_red1.
match goal with | r1: t_red1 M0 ?M2 |- _ => elim (IHr e M2 M1); auto end;
intros Q ex; elim ex; intros Q1 ([=] & r1 & r2); subst;
exists Q, Q1; repeat split; zerotac; succtac; zerotac.
match goal with | r1: t_red1 M1 ?M2 |- _ => elim (IHr e M0 M2); auto end;
intros Q ex; elim ex; intros Q1 ([=] & r1 & r2); subst;
exists Q, Q1; repeat split; zerotac; succtac; zerotac.
}
Qed.
Lemma t_red_preserves_factorable: forall M N, t_red M N -> factorable M -> factorable N.
Proof.
intros M N r fac; inversion fac; subst.
{
assert(N = △) by (apply programs_are_stable2; [ apply t_red_to_s_red | apply pr_leaf]; auto);
subst; auto.
}{
assert(ex: exists Q, N = △ @ Q /\ t_red M0 Q) by (eapply t_red_preserves_stems; eauto);
elim ex; intros Q (?&?); subst; auto_t.
}{
assert(ex: exists Q1 Q2, N = △ @ Q1 @ Q2 /\ t_red M0 Q1 /\ t_red N0 Q2)
by (eapply t_red_preserves_forks; eauto);
elim ex; split_all; subst; split_all.
}
Qed.
(* Counting reduction steps *)
Inductive multi_ranked : (Tree -> Tree -> Prop) -> (nat -> Tree -> Tree -> Prop) :=
| zero_ranked : forall red M, multi_ranked red 0 M M
| succ_ranked : forall (red: Tree -> Tree -> Prop) M N n P,
red M N -> multi_ranked red n N P -> multi_ranked red (S n) M P
.
Hint Constructors multi_ranked : TreeHintDb.
Lemma multi_ranked_trans:
forall red n M N, multi_ranked red n M N -> forall p P, multi_ranked red p N P -> multi_ranked red (n+p) M P.
Proof. induction n; intros M N rN p P rP; intros; inversion rN; subst; eauto; eapply2 succ_ranked. Qed.
Lemma multi_ranked_iff_multi_red :
forall (red : Tree -> Tree -> Prop) M N, multi_step red M N <-> exists n, multi_ranked red n M N.
Proof.
intros red M N; split; intro r.
induction r. exist 0.
elim IHr; intro k; exists (S k); eapply2 succ_ranked.
elim r; intros k r1. generalize M N r1; clear.
induction k ; intros; inversion r1; subst; zerotac; eapply2 succ_red.
Qed.
Lemma diamond_multi_ranked:
forall red1 red2, diamond red1 red2 -> forall n, diamond (multi_ranked red1 n) red2.
Proof.
intros red1 red2 d; induction n as [| n']; red; intros M N r P rP; inversion r; subst.
exists P; auto_t.
match goal with | Hr : red1 M ?N0 |- _ => elim (d M N0 Hr P rP); auto; intros x (Hx & ?); intros end.
match goal with
| Hr : multi_ranked red1 n' ?N0 N, Hx : red2 ?N0 _ |- _ =>
elim(IHn' _ _ Hr _ Hx); auto; intro x1; intros (?&?)
end.
exist x1; eapply2 succ_ranked.
Qed.
Lemma diamond_multi_ranked2:
forall red1 red2, diamond red1 red2 -> forall m n, diamond (multi_ranked red1 m) (multi_ranked red2 n).
Proof. intros; repeat (apply diamond_multi_ranked; apply diamond_flip); auto_t. Qed.
(* Halting Problem *)
Definition valuable M := exists P, t_red M P /\ program P.
Definition omega := △ @ (△ @ I) @ I.
Ltac stable_tac := inv1 program; match goal with | H : s_red1 ?Q ?R |- _ => assert(R=Q) by
(apply programs_are_stable; cbv; program_tac); clear H end.
Lemma omega_omega_doesn't_halt:
forall n h M, h < n -> multi_ranked s_red1 h (omega@ omega) M -> factorable M -> False.
Proof.
induction n as [| n0]; intros h M hn r fac. lia.
inversion r; clear r; subst.
unfold omega at 1 in fac; inv1 factorable.
match goal with | H : s_red1 (omega@omega) _ |- _ => unfold omega at 1 in H; inv s_red1 end.
repeat stable_tac; subst. auto.
assert(r1: exists Q, s_red M Q /\ multi_ranked s_red1 n (omega@omega) Q).
eapply2 diamond_multi_ranked. eapply2 diamond_strip.
apply t_red_to_s_red; trtac. elim r1; intros Q (r2 & r3).
eapply2 (IHn0 n Q); eapply t_red_preserves_factorable; [ eapply2 s_red_to_t_red | auto].
{
repeat stable_tac; subst.
assert(r1: exists Q, s_red M Q /\ multi_ranked s_red1 n (omega@omega) Q).
eapply2 diamond_multi_ranked. eapply2 diamond_strip. eauto.
eapply t_red_to_s_red; trtac.
elim r1; intros Q (r2 & r3).
eapply2 (IHn0 n Q); eapply t_red_preserves_factorable; [eapply2 s_red_to_t_red | auto].
}
Qed.
Lemma omega_omega_has_no_value: ~(valuable (omega @ omega)).
Proof.
unfold valuable; intro v; elim v; clear v; intro P; intros (?&?).
assert(ex:exists n, multi_ranked s_red1 n (omega @ omega) P).
apply multi_ranked_iff_multi_red; auto. apply t_red_to_s_red; auto.
elim ex; intros n r. eapply omega_omega_doesn't_halt; eauto. apply programs_are_factorable; auto.
Qed.
Definition halting h :=
forall M, (t_red (h @ M) K /\ valuable M) \/ (t_red (h @ M) KI /\ ~ (valuable M)).
Definition self_negation := \"h" (\"f" ((Ref "h")@ ((Ref "f") @ (Ref "f")) @ (omega@omega) @ K)).
Theorem halting_problem_insoluble : forall h, ~(halting h).
Proof.
unfold halting; intro h; intro halt.
assert(h1: t_red (h @ K) K /\ valuable K \/ t_red (h @ K) KI /\ ~ valuable K) by apply halt.
inversion h1 as [ (r&v) | (r &nv) ]; clear h1.
2: apply nv; unfold valuable; exists K; split; [zerotac | program_tac].
assert(h2: t_red (h @ (omega @ omega)) K
/\ valuable (omega @ omega) \/ t_red (h @ (omega @ omega)) KI
/\ ~ valuable (omega @ omega)) by apply halt.
inversion h2 as [ (r2&v2) | (r2 &nv2) ]; clear h2; intros.
apply omega_omega_has_no_value; auto.
assert (h3: (t_red (h @ (self_negation @ h @ (self_negation @ h))) K /\ valuable (self_negation @ h @ (self_negation @ h))) \/ (t_red (h @ (self_negation @ h @ (self_negation @ h))) KI /\ ~ (valuable (self_negation @ h @ (self_negation @ h))))) by
apply halt.
inversion h3 as [ (r3&v3) | (r3 &nv3) ]; clear h3; intros.
assert(h4: t_red ((self_negation @ h @ (self_negation @ h))) (omega@omega)).
unfold self_negation at 1. starstac ("f" :: "h" :: nil).
ap2tac; zerotac; trtac.
assert(t_red (h @ (self_negation @ h @ (self_negation @ h))) KI).
ap2tac; zerotac; trtac. auto.
assert(h5: exists Q, t_red K Q /\ t_red KI Q) by eapply2 diamond_t_red.
elim h5; clear h5; intros Q (?&?).
assert(Q = K). apply programs_are_stable2. apply t_red_to_s_red; auto. program_tac.
assert(Q = KI). apply programs_are_stable2. apply t_red_to_s_red; auto. program_tac.
subst; discriminate.
assert( t_red ((self_negation @ h @ (self_negation @ h))) K).
unfold self_negation at 1. starstac ("f" :: "h" :: nil).
ap2tac; zerotac; trtac.
assert(t_red (h @ (self_negation @ h @ (self_negation @ h))) K).
ap2tac; zerotac; trtac.
assert(h5: exists Q, t_red K Q /\ t_red KI Q) by eapply2 diamond_t_red.
elim h5; intros Q (?&?).
assert(Q = K) . apply programs_are_stable2. eapply2 t_red_to_s_red. program_tac.
assert(Q = KI) . apply programs_are_stable2. eapply2 t_red_to_s_red. program_tac.
subst; discriminate.
Qed.
(* Standard Reduction *)
Fixpoint redexes M :=
(* counts the number of redexes currently available *)
match M with
| Ref _ => 0
| △ => 0
| △ @ △ @ M @ N => S (redexes M + redexes N)
| △ @ (△ @ P) @ M @ N => S (redexes P + redexes M + redexes N)
| △ @ (△ @ P @ Q) @ M @ N => S (redexes P + redexes Q + redexes M + redexes N)
| M @ N => redexes M + redexes N
end.
Lemma applications_add_redexes: forall M N, redexes (M@N) >= redexes M + redexes N.
Proof.
induction M as [ ? | | M1 ]; intro N; simpl; auto;
caseEq M1; [split_all | split_all | intros t t0; split_all];
caseEq t; split_all;
caseEq t0; [split_all | split_all | intros t1 ?; split_all];
caseEq t1; [split_all | split_all | intros t3 ?; split_all];
caseEq t3; split_all.
Qed.
Definition irreducible M := forall N, t_red1 M N -> False.
Lemma programs_are_irreducible: forall M, program M -> irreducible M.
Proof. intros M prM; induction prM; intro; intro; auto; inv t_red1. Qed.
Lemma irreducible_no_redexes: forall N, irreducible N -> redexes N = 0.
Proof.
induction N; intro irr; subst; simpl; auto; caseEq N1.
{ intros; subst; apply IHN2; intro; intro; eapply2 irr. }
{ intros [=]; simpl; apply IHN2; intro; intro; eapply2 irr. }
{ intros t t0 [=]; subst; caseEq t.
1,2: intros; subst; auto; simpl in *;
rewrite IHN1; auto; [rewrite IHN2; auto |]; intro; intro; eapply2 irr.
intros t1 t2 e; subst; rewrite IHN1; [ | intro; intro; eapply2 irr].
assert(d: t1 = △ \/ t1 <> △) by repeat decide equality.
inversion d; subst;
[ | replace (match t1 with
| △ =>
match t2 with
| △ => S (redexes t0 + redexes N2)
| △ @ Q => S (redexes Q + redexes t0 + redexes N2)
| △ @ P @ Q => S (redexes P + redexes Q + redexes t0 + redexes N2)
| _ => 0 + redexes N2
end
| _ => 0 + redexes N2
end)
with (0 + redexes N2)
by (caseEq t1; split_all);
rewrite IHN2; auto; intro; intro; eapply2 irr].
Ltac aux H irr t := caseEq t; intros; subst;
[ apply H; intro; intro; eapply2 irr | assert False by eapply2 irr; lia | ].
aux IHN2 irr t2; aux IHN2 irr t; aux IHN2 irr t2; rewrite IHN2; auto_t; intro; split_all.
}Qed.
Lemma program_no_redexes: forall N, program N -> redexes N = 0.
Proof. intros; apply irreducible_no_redexes; apply programs_are_irreducible; auto. Qed.
Lemma quaternary_redexes:
forall M, combination M -> forall N P Q R, M = N @ P @ Q @ R -> redexes M >0.
Proof.
cut (forall p M, term_size M < p -> combination M ->
forall N P Q R, M = N@P@Q@R -> redexes M >0).
intro H; intros; eapply H; eauto.
induction p; intros M s c N P Q R e; subst. lia.
assert(cN:combination N) by (inv1 combination).
inversion cN as [ | P1 Q1 d1 d2]; subst.
(* 2 *)
assert(cP: combination P) by (inv1 combination).
inversion cP as [| M0 N0 c1 c2]; subst; auto. simpl; lia.
inversion c1 as [ | M1 N1 c3 c4]; subst; auto. simpl; lia.
inversion c3 as [ | M2 N2]; subst; auto. simpl; lia.
assert(redexes (M2 @ N2 @ N1 @ N0) >0) by ( eapply IHp; eauto; simpl in *; lia).
simpl in *; lia.
(* 1 *)
assert(redexes (P1 @ Q1 @ P @ Q @ R) >= redexes (P1 @ Q1 @ P @ Q) + redexes R)
by (eapply applications_add_redexes; eauto).
assert(term_size R >0) by apply size_positive.
assert(redexes (P1 @ Q1 @ P @ Q) > 0) . eapply IHp; auto_t. simpl in *; lia.
combination_tac; inv1 combination.
lia.
Qed.
Lemma no_redexes_program: forall N, redexes N = 0 -> combination N -> program N.
Proof.
induction N; intros; subst; auto; subst.
(* 3 *)
inv1 combination.
apply pr_leaf.
(* 1 *)
assert(cN1: combination N1) by inv1 combination.
inversion cN1 as [| M0 N0 c1 c2]; subst. apply pr_stem. apply IHN2. simpl in *; lia. inv1 combination.
(* 1 *)
inversion c1 as [| M1 N1 c3 c4]; subst. apply pr_fork.
assert(program (△ @ N0)). apply IHN1. simpl in *; lia. eauto. inv1 program.
apply IHN2. simpl in *; lia. inv1 combination.
assert(redexes (M1 @ N1 @ N0 @ N2) >0) . eapply quaternary_redexes; eauto. lia.
Qed.
(* ready combinations *)
(* The proof of standardisation is adapted from the work of Ryo Kashima on lambda-calculus. *)
Inductive ready : Tree -> Prop :=
| kernelready: forall M, ready (△ @ △ @ M)
| stem_ready : forall M N, ready (△ @ (△ @ M) @ N)
| fork_ready : forall M N P, ready (△ @ (△ @ M @ N) @ P)
.
Hint Constructors ready :TreeHintDb.
Lemma ready_or_not: forall M, ready M \/ ~(ready M).
Proof.
induction M; intros; subst; auto; try (right; intro H; inversion H; fail); subst.
inversion IHM1. right; intro. inv1 ready; subst; inv1 ready.
(* 1 *)
case M1; intros; try (right; intro; inv1 ready; fail).
case t; intros; try (right; intro; inv1 ready; fail).
case t0; intros; try (right; intro; inv1 ready; fail). left; auto_t.
case t1; intros; try (right; intro; inv1 ready; fail). left; auto_t.
case t3; intros; try (right; intro; inv1 ready; fail). left; auto_t.
Qed.
(* t_nred1 tags reductions with the number of overlooked reductions on the left *)
Inductive t_nred1 : nat -> Tree -> Tree -> Prop :=
| kernel_nred : forall M N, t_nred1 0 (△ @ △ @ M @ N) M
| stem_nred : forall M N P, t_nred1 0 (△ @ (△ @ P) @ M @ N) (M@N @ (P @ N))
| fork_nred : forall M N P Q, t_nred1 0 (△ @ (△ @ P @ Q)@ M@ N) (N@P@ Q)
| not_factorable_nred:forall M M' N P, ~(factorable M) -> t_nred1 0 M M' ->
t_nred1 0 (△ @ M @ N@ P) (△ @ M' @ N@ P)
| appl_unready : forall n M M1 N, t_nred1 n M M1 -> ~(ready M) -> t_nred1 n (M@N) (M1 @ N)
| appl_ready : forall n M M1 N, t_nred1 n M M1 -> ready M -> t_nred1 (S n) (M@N) (M1 @ N)
| appr_unready : forall n M N N1, t_nred1 n N N1 -> ~(ready M) -> t_nred1 (n + redexes M) (M@N) (M@N1)
| appr_ready : forall n M N N1, t_nred1 n N N1 -> ready M -> t_nred1 (S (n+redexes M)) (M@N) (M@N1)
.
Lemma t_nred1_to_t_red1: forall n M N, t_nred1 n M N -> t_red1 M N.
Proof. intros n M N r; induction r; split_all. Qed.
(* l_red is leftmost reduction (since nothing has been overlooked) *)
Inductive seq_red : nat -> Tree -> Tree -> Prop :=
| zero_seq_red : forall m M, m<= redexes M -> seq_red m M M
| succ_seq_red: forall n n1 M N P, n <= n1 -> t_nred1 n M N -> seq_red n1 N P -> seq_red n1 M P
.
Definition l_red := seq_red 0.
Hint Constructors ready t_nred1 seq_red :TreeHintDb.
Lemma seq_red_to_t_red : forall n M N, seq_red n M N -> t_red M N.
Proof.
intros n M N r; induction r; intros; zerotac.
eapply succ_red; eauto. eapply t_nred1_to_t_red1; eauto.
Qed.
Theorem l_red_to_t_red: forall M N, l_red M N -> t_red M N.
Proof. apply seq_red_to_t_red. Qed.
Lemma l_red_transitive: forall M N, l_red M N -> forall P, l_red N P -> l_red M P.
Proof.
cut (forall n M N, seq_red n M N -> n = 0 -> forall P, l_red N P -> l_red M P).
intro H; intros; auto. eapply H; eauto.
intros n M N r; induction r; intros; subst; auto.
eapply2 succ_seq_red; eapply2 IHr.
Qed.
(* basic properties *)
Lemma seq_red_redexes: forall n M N, seq_red n M N -> n <= redexes N.
Proof. intros n M N r; induction r; auto. Qed.
Lemma seq_red_preserves_ready: forall n M N, seq_red n M N -> ready M -> ready N.
Proof.
intros n M N r; induction r as [r1 | ? ? ? ? ? ? tr]; intro rdy; auto. apply IHr. clear - rdy tr.
(* now the result for t_nred1 *)
inversion rdy; subst; inv (t_nred1 n); inv1 ready; inv (t_nred1 n1); inv1 ready.
Qed.
Lemma seq_red_app:
forall M M1, seq_red (redexes M1) M M1 ->
forall N N1, seq_red (redexes N1) N N1 ->
seq_red (redexes (M1 @ N1)) (M@N) (M1 @ N1).
Proof.
intros M M1 s; induction s as [| n1 n2 M N P lt r1 s0]; intros N0 N1 s1; subst; auto.
(* 2 *)
induction s1 as [| n0 n1 N P Q]; subst; auto_t.
assert(rM: ready M \/ ~(ready M)) by apply ready_or_not.
assert(rQ: n1 <= redexes Q). eapply seq_red_redexes. eauto.
inversion rM as [rM1 | rm2]; subst.
(* 3 *)
eapply succ_seq_red; [| |eauto]; [| eapply2 appr_ready]; inversion rM1; subst; simpl; lia.
(* 2 *)
eapply succ_seq_red; [| | eauto]; [| eapply2 appr_unready].
caseEq M; intros t; intros; subst; simpl; auto; try lia.
caseEq t; intros t1; subst; simpl; auto; try lia. intros r0 e.
caseEq t1; intros t2; subst; simpl; auto; try lia.
caseEq r0; intros t; subst; simpl; auto; try lia. intros t1 [=].
caseEq t; intros t2; subst; simpl; auto; try lia. intros t3 [=].
caseEq t2; intros; subst; simpl; auto; try lia.
(* 1 *)
assert(rM: ready M \/ ~(ready M)) by eapply ready_or_not.
inversion rM.
all: cycle 1.
(* 2 *)
eapply succ_seq_red; [ | eapply appl_unready; eassumption | auto].
assert(n2 <= redexes P) by (eapply seq_red_redexes; eauto);
assert(redexes (P @ N1) >= redexes P + redexes N1) by apply applications_add_redexes; lia.
(* 1 *)
eapply succ_seq_red; [| | eapply IHs0; eassumption]; [ | eapply appl_ready; eauto].
assert(rP: ready P) . eapply seq_red_preserves_ready; auto.
eapply succ_seq_red; eauto. auto.
assert(n2 <= redexes P) by (eapply seq_red_redexes; eauto).
inversion rP; subst; simpl in *; lia.
Qed.
(* hap performs head reduction *)
Inductive hap1 : Tree -> Tree -> Prop :=
| hap_kernel : forall M N, hap1 (△ @ △ @ M @ N) M
| hap_stem : forall P M N, hap1 (△ @ (△ @ P) @ M @ N) (M@N @ (P@ N))
| hap_fork : forall P Q M N, hap1 (△ @ (△ @ P @ Q) @ M @ N) (N@P@ Q)
| hap_app : forall M N P, hap1 M N -> hap1 (M@ P) (N@ P)
| hap_ready: forall M M' N P, hap1 M M' -> hap1 (△ @ M @ N@ P) (△ @ M' @ N@ P)
.
Definition hap := multi_step hap1.
Lemma hap1_functional: forall M N, hap1 M N -> forall P, hap1 M P -> N=P.
Proof.
intros M N r; induction r; intros P0 rP; inv hap1; subst; inv hap1;
repeat f_equal; apply IHr; auto.
Qed.
(* st_red allows nested head reductions, but always moving to the right *)
Inductive st_red : Tree -> Tree -> Prop :=
| st_ref : forall M i, hap M (Ref i) -> st_red M (Ref i)
| st_node : forall M, hap M △ -> st_red M △
| st_app : forall M N N1 P P1, hap M (N@ P) -> st_red N N1 -> st_red P P1 -> st_red M (N1 @P1)
.
Hint Constructors hap1 st_red :TreeHintDb.
Lemma hap_preserves_appr: forall M N P, hap M N -> hap (M@ P) (N@ P).
Proof.
cut(forall red M N P, multi_step red M N -> red = hap1 -> hap (M@ P) (N@ P));
[split_all |
intros red M N P r; induction r; split_all; subst;
[ zerotac | eapply succ_red; [ eapply2 hap_app | eapply2 IHr]]].
Qed.
Lemma hap_ready_multi:
forall M M' N P, hap M M' -> hap (△ @ M @ N @ P) (△ @ M' @ N @ P).
Proof.
cut (forall red M M',
multi_step red M M' -> red = hap1 ->
forall N P, hap (△ @ M @ N @ P) (△ @ M' @ N @ P));
[ split_all |
intros red M M' r; induction r; intros; subst;
[ zerotac | eapply succ_red; [ eapply2 hap_ready | eapply2 IHr]]].
Qed.
Lemma st_red_refl: forall M, st_red M M.
Proof. induction M; split_all; [ apply st_ref | apply st_node | eapply2 st_app]; zerotac. Qed.
Lemma hap_then_st_red: forall N M, hap M N -> forall P, st_red N P -> st_red M P.
Proof.
intros M N H P st; inversion st; subst; [eapply2 st_ref | eapply2 st_node | eapply2 st_app];
(eapply transitive_red; [ eexact H |]; auto_t).
Qed.
Lemma st_kernel_red :
forall M N P, st_red M (△ @ △ @ N @ P) -> st_red M N.
Proof.
intros M N P st; inv st_red.
eapply hap_then_st_red. 2: eassumption.
eapply transitive_red. eassumption.
eapply transitive_red. eapply hap_preserves_appr. eassumption.
eapply transitive_red. eapply hap_preserves_appr. eapply hap_preserves_appr. eassumption.
eapply transitive_red. eapply hap_preserves_appr. eapply hap_preserves_appr.
eapply hap_preserves_appr. eassumption.
eapply transitive_red. apply hap_ready_multi; eauto. eapply2 succ_red.
Qed.
Lemma st_stem_red :
forall M N P Q, st_red M (△ @ (△ @ N) @ P @ Q) -> st_red M (P@Q @(N@Q)).
Proof.
intros M N P Q H. inv st_red. eapply hap_then_st_red.
(* 2 *)
eapply transitive_red. eassumption.
eapply transitive_red. eapply hap_preserves_appr. eassumption.
eapply transitive_red. eapply hap_preserves_appr. eapply hap_preserves_appr. eassumption.
eapply transitive_red. eapply hap_preserves_appr. eapply hap_preserves_appr.
eapply hap_preserves_appr. eassumption.
eapply transitive_red. eapply hap_ready_multi. eassumption.
eapply transitive_red. eapply hap_ready_multi. eapply hap_preserves_appr. eassumption.
eapply succ_red; auto_t.
(* 1 *)
eapply st_app. zerotac. eapply st_app. zerotac. auto. auto. eapply st_app. zerotac. auto. auto.
Qed.
Lemma st_fork_red :
forall M N R P Q, st_red M (App (App (△ @ (△ @ N @ R)) P) Q) -> st_red M (Q@N@ R).
Proof.
intros M N R P Q H; inv st_red. eapply hap_then_st_red.
(* 2 *)
eapply transitive_red. eassumption.
eapply transitive_red. eapply hap_preserves_appr. eassumption.
eapply transitive_red. eapply hap_preserves_appr. eapply hap_preserves_appr. eassumption.
eapply transitive_red. eapply hap_preserves_appr. eapply hap_preserves_appr.
eapply hap_preserves_appr. eassumption.
eapply transitive_red. eapply hap_ready_multi. eassumption.
eapply transitive_red. eapply hap_ready_multi. eapply hap_preserves_appr. eassumption.
eapply transitive_red. eapply hap_ready_multi. eapply hap_preserves_appr. eapply hap_preserves_appr.
eassumption.
eapply2 succ_red.
(* 1 *)
eapply st_app. zerotac. eapply st_app. zerotac. all: auto.
Qed.
(* comparing and combining the relations *)
Lemma hap1_implies_t_nred1: forall M N, hap1 M N -> t_nred1 0 M N.
Proof.
intros M N h; induction h; simpl; split_all.
apply appl_unready; auto. intro r; inversion r; subst; inversion h; subst; inv hap1.
apply appl_unready; auto. apply appl_unready; auto.
replace 0 with (0+ (redexes △)) by auto.
apply appr_unready; auto.
all: intro r; inversion r; subst; inv hap1.
Qed.
Lemma hap_implies_l_red: forall M N, hap M N -> l_red M N.
Proof.
cut(forall red M N, multi_step red M N -> red = hap1 -> l_red M N).
intro H; intros; eapply H; eauto.
intros red M N r; induction r; simpl; subst; intros; auto; subst.
(* 2 *)
apply zero_seq_red; lia.
(* 1 *)
eapply succ_seq_red; [| eapply hap1_implies_t_nred1; eauto | eauto]; [lia | eapply2 IHr].
Qed.
Lemma hap_then_seq_red: forall M N, hap M N -> forall n P, seq_red n N P -> seq_red n M P.
Proof.
cut(forall red M N, multi_step red M N -> red = hap1 ->
forall n P, seq_red n N P -> seq_red n M P).
intro H; intros; eapply H; eauto.
intros red M N r; induction r; intros; subst; auto.
eapply succ_seq_red; [| | eapply2 IHr]; [| eapply2 hap1_implies_t_nred1]; lia.
Qed.
Lemma st_red_implies_seq_red: forall M N, st_red M N -> seq_red (redexes N) M N.
Proof.
intros M N st; induction st; intros; auto;
try (apply hap_implies_l_red; auto; fail);
eapply2 hap_then_seq_red; eapply2 seq_red_app.
Qed.
Ltac inv_st_red :=
match goal with
| H : st_red _ Node |- _ => inversion H; clear H; subst; inv_st_red
| H : st_red _ (Node @ _) |- _ => inversion H; clear H; subst; inv_st_red
| H : st_red _ (Node @ _ @ _) |- _ => inversion H; clear H; subst; inv_st_red
| H : st_red _ (_@_ @ _ @ _) |- _ => inversion H; clear H; subst; inv_st_red
| _ => split_all
end.
Lemma st_red_then_t_nred1: forall n N P, t_nred1 n N P -> forall M, st_red M N -> st_red M P.
Proof.
intros n N P r; induction r; intros ? s;
try (inv_st_red; try inversion s; subst; eapply2 st_app; auto_t; fail);
[eapply st_kernel_red | eapply st_stem_red | eapply st_fork_red]; auto_t.
Qed.
(* proofs below work from the other end of the reduction sequence *)
Inductive t_red_rev : Tree -> Tree -> Prop :=
| zero_t_red : forall M, t_red_rev M M
| succ_t_red : forall M N n P, t_red_rev M N -> t_nred1 n N P -> t_red_rev M P.
Hint Constructors t_red_rev :TreeHintDb.
Lemma transitive_t_red_rev :
forall N P, t_red_rev N P -> forall M, t_red_rev M N -> t_red_rev M P.
Proof.
intros N P r; induction r; intros; auto.
eapply succ_t_red. eapply IHr; auto. eauto.
Qed.
Lemma t_red1_implies_t_nred1:
forall M N, t_red1 M N -> exists n, t_nred1 n M N.
Proof.
induction M; intros N r; auto; inversion r; subst; auto_t.
(* 2 *)
assert(ex: exists n, t_nred1 n M1 M') by (eapply IHM1; eauto).
inversion ex; subst.
assert(rM: ready M1 \/ ~(ready M1)) by apply ready_or_not.
inversion rM; auto_t.
(* 1 *)
assert(ex: exists n, t_nred1 n M2 N') by (eapply IHM2; eauto).
inversion ex; subst.
match goal with | H : t_red1 (?M1 @ _) _ |- _ =>
assert(rM1: ready M1 \/ ~(ready M1)) by apply ready_or_not;
inversion rM1; auto_t
end.
Qed.
Lemma t_red_implies_t_red_rev:
forall M N, t_red M N -> t_red_rev M N.
Proof.
cut (forall red M N, multi_step red M N -> red = t_red1 -> t_red_rev M N).
intro H; intros; eapply H; auto_t.
intros red M N r; induction r; intros; subst; auto_t. eapply transitive_t_red_rev.
eapply IHr. auto.
assert(ex: exists n, t_nred1 n M N) by (eapply t_red1_implies_t_nred1; eauto).
inversion ex; auto_t.
Qed.
Lemma t_red_rev_st_red : forall M N, t_red_rev M N -> st_red M N.
Proof.
intros M N r; induction r; intros; subst; auto.
(* 2 *)
apply st_red_refl.
(* 1 *)
eapply st_red_then_t_nred1; eauto.
Qed.
Lemma t_red_st_red : forall M N, t_red M N -> st_red M N.
Proof. intros; apply t_red_rev_st_red; apply t_red_implies_t_red_rev; auto. Qed.
(* the standardization theorem *)
Theorem standardization : forall M N, t_red M N -> seq_red (redexes N) M N.
Proof. intros; apply st_red_implies_seq_red; apply t_red_st_red; auto. Qed.
Corollary leftmost_reduction:
forall M N, t_red M N -> program N -> l_red M N.
Proof.
intros M N r pr.
assert(seq_red (redexes N) M N) by (eapply standardization; eauto).
assert(r0: redexes N = 0) by (eapply program_no_redexes; eauto).
rewrite r0 in *; auto.
Qed.
Theorem head_reduction_to_factorable_form:
forall M N, t_red M N -> factorable N -> exists Q, hap M Q /\ factorable Q /\ t_red Q N.
Proof.
intros M N r fac.
assert(st: st_red M N) by (eapply t_red_rev_st_red; eapply t_red_implies_t_red_rev; eauto).
inversion st; subst; inv1 factorable; subst.
(* 2 *)
match goal with | st: st_red _ Node |- _ => inversion st; subst end.
exists (△ @P); repeat split_all.
eapply transitive_red. eassumption. apply hap_preserves_appr; auto.
apply preserves_app_t_red. zerotac.
eapply seq_red_to_t_red. apply st_red_implies_seq_red; auto_t.
(* 1 *)
match goal with | st: st_red _ (Node @ _) |- _ => inversion st; subst end.
match goal with | st: st_red _ Node |- _ => inversion st; subst end.
match goal with | hap1: hap M (?N0 @ ?P), hap2: hap ?N0 (N@ ?P0) |- _ => exists (△ @P0@P) end.
repeat split_all.
eapply transitive_red. eassumption. apply hap_preserves_appr.
eapply transitive_red. eassumption. apply hap_preserves_appr; auto.
repeat apply preserves_app_t_red; eapply seq_red_to_t_red;
apply st_red_implies_seq_red; auto_t. apply st_node; zerotac.
Qed.
(* Lemmas for self-evaluators *)
Ltac irrtac :=
match goal with
| pr: program ?M , r: t_red1 ?M _ |- _ =>
assert False by (eapply (programs_are_irreducible M); eauto); lia
| _ => idtac
end.
Ltac s_red_program_tac :=
match goal with
| H : program ?P, H1: s_red1 ?P ?Q |- _ => assert(Q=P) by apply programs_are_stable; clear H1; s_red_program_tac
| H : program ?P, H1: s_red ?P ?Q |- _ => assert(Q=P) by apply programs_are_stable2; clear H1; s_red_program_tac
| _ => idtac
end.
Ltac invsub :=
match goal with
| H : _ = _ |- _ => inversion H; subst; clear H; invsub
| _ => split_all
end.
Lemma program_application_t_red1 :
forall M N, t_red1 M N ->
forall M1 M2, M = M1 @ M2 -> program M1 -> program M2 -> hap1 M N.
Proof.
intros M N r; induction r; intros M1 M2 e pr1 pr2; invsub; auto_t; inv t_red1; irrtac.
Qed.
Lemma program_application2_t_red1 :
forall M N, t_red1 M N ->
forall M1 M2 M3, M = M1 @ M2 @ M3 -> program M1 -> program M2 -> program M3 -> hap1 M N.
Proof.
intros M N r; induction r; intros M1 M2 M3 e pr1 pr2 pr3; invsub; inv t_red1; auto_t; irrtac.
Qed.
Lemma program_application3_t_red1 :
forall M N, t_red1 M N ->
forall M1 M2 M3 M4, M = M1 @ M2 @ M3 @ M4 ->
program M1 -> program M2 -> program M3 -> program M4 -> hap1 M N.
Proof.
intros M N r; induction r; intros M1 M2 M3 M4 e pr1 pr2 pr3; invsub; inv t_red1; auto_t;
irrtac.
Qed.
Lemma program_application_s_red1 :
forall M N, s_red1 M N ->
forall M1 M2, M = M1 @ M2 -> program M1 -> program M2 -> M= N \/ hap1 M N.
Proof.
intros M N r; induction r; intros M1 M2 e pr1 pr2; invsub; auto_t; inv s_red1; inv1 program;
subst; s_red_program_tac; subst; auto_t; repeat stable_tac; subst; auto_t.
Qed.
Lemma program_application2_s_red1 :
forall M N, s_red1 M N ->
forall M1 M2 M3, M = M1 @ M2 @ M3 -> program M1 -> program M2 -> program M3 -> M= N \/ hap1 M N.
Proof.
intros M N r; induction r; intros M1 M2 M3 e pr1 pr2 pr3; invsub; auto_t; inv s_red1; inv1 program; subst; s_red_program_tac; subst; auto_t; repeat stable_tac; subst; auto_t.
Qed.