forked from barry-jay-personal/tree-calculus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLambda_Abstraction_in_VA_Calculus.v
1713 lines (1343 loc) · 60.4 KB
/
Lambda_Abstraction_in_VA_Calculus.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 Programming in Tree Calculus *)
(* Chapter 9: Lambda-Abstraction in VA-Calculus *)
(* *)
(* Barry Jay *)
(* *)
(**********************************************************************)
Require Import Arith Lia Bool List String.
Require Import Reflective.Rewriting_partI.
Require Import Incompleteness_of_Combinatory_Logic.
Set Default Proof Using "Type".
Ltac auto_t := eauto with TreeHintDb.
Ltac eapply2 H := eapply H; auto_t.
Ltac split_all := simpl; intros;
match goal with
| H : _ /\ _ |- _ => inversion_clear H; split_all
| H : exists _, _ |- _ => inversion H; clear H; split_all
| _ => try (split; split_all); try contradiction
end; try congruence; auto_t.
Ltac exist x := exists x; split_all.
(* Section 9.2: VA-Calculus *)
Inductive VA: Set :=
Ref : string -> VA
| Vop
| Lam
| App : VA -> VA -> VA
.
Declare Scope va_scope.
Open Scope va_scope.
Notation "x @ y" := (App x y) (at level 65, left associativity) : va_scope.
Definition v x := App Vop x.
Fixpoint size M :=
match M with
| Ref _ => 1
| Vop => 1
| Lam => 1
| M1 @ M2 => S (size M1 + size M2)
end.
Lemma size_positive: forall M, size M >0.
Proof. induction M; split_all; lia. Qed.
(* Multiple reduction steps *)
Inductive multi_step : (VA -> VA -> Prop) -> VA -> VA -> Prop :=
| zero_red : forall red M, multi_step red M M
| succ_red : forall (red: VA-> VA -> Prop) M N P,
red M N -> multi_step red N P -> multi_step red M P
.
Hint Constructors multi_step: TreeHintDb.
Definition transitive red := forall (M N P: VA), red M N -> red N P -> red M P.
Ltac one_step :=
try red;
match goal with
| |- multi_step _ _ ?N => apply succ_red with N; auto_t
end.
Lemma transitive_red : forall red, transitive (multi_step red).
Proof. red. intros red M N P r1; induction r1; split_all. Qed.
Definition preserves_appl (red : VA -> VA -> Prop) :=
forall M N M', red M M' -> red (M@ N) (M' @ N).
Definition preserves_appr (red : VA -> VA -> Prop) :=
forall M N N', red N N' -> red (M@ N) (M@ N').
Definition preserves_app (red : VA -> VA -> Prop) :=
forall M M', red M M' -> forall N N', red N N' -> red (M@ N) (M' @ N').
Lemma preserves_appl_multi_step :
forall (red: VA -> VA -> Prop), preserves_appl red -> preserves_appl (multi_step red).
Proof. red. intros red al M N M' r; induction r; auto_t. Qed.
Lemma preserves_appr_multi_step :
forall (red: VA -> VA -> Prop), preserves_appr red -> preserves_appr (multi_step red).
Proof. red. intros red ar M N N' r; induction r; split_all; eapply2 succ_red. Qed.
Lemma preserves_app_multi_step :
forall (red: VA -> VA -> Prop),
preserves_appl red -> preserves_appr red ->
preserves_app (multi_step red).
Proof.
red. intros red al ar M M' r N N' r2.
apply transitive_red with (M' @ N).
eapply2 preserves_appl_multi_step.
eapply2 preserves_appr_multi_step.
Qed.
Ltac inv1 prop :=
match goal with
| H : prop (Ref _) |- _ => inversion H; clear H; subst; inv1 prop
| H : prop Vop |- _ => inversion H; clear H; subst; inv1 prop
| H : prop Lam |- _ => inversion H; clear H; subst; inv1 prop
| H : prop (_ @ _) |- _ => inversion H; clear H; subst; inv1 prop
| _ => split_all
end.
Ltac inv red :=
match goal with
| H : red (Ref _) _ |- _ => inversion H; clear H; subst; inv red
| H : red Vop _ |- _ => inversion H; clear H; subst; inv red
| H : red Lam _ |- _ => inversion H; clear H; subst; inv red
| H : red (_ @ _) _ |- _ => inversion H; clear H; subst; inv red
| H : multi_step red (Ref _) _ |- _ => inversion H; clear H; subst; inv red
| H : multi_step red Vop _ |- _ => inversion H; clear H; subst; inv red
| H : multi_step red Lam _ |- _ => inversion H; clear H; subst; inv red
| H : multi_step red (_ @ _) _ |- _ => inversion H; clear H; subst; inv red
| _ => split_all
end.
(* lambda reduction *)
Inductive va_red1 : VA -> VA -> Prop :=
(* variables and abstractions *)
| v_red : forall M N P, va_red1 (Vop @ M @ N @ P) (Vop @ (Vop @ M @ N) @ P)
(* substitution *)
| r_v_red: forall P Q, va_red1 (Lam @ Vop @ P @ Q) Q (* the zero index *)
| r_v1_red : forall M P Q, va_red1 (Lam @ (Vop @ M) @ P @ Q) (P @ M) (* successor indices *)
| r_v2_red : forall M N P Q, va_red1 (Lam @ (Vop @ M @ N) @ P @ Q) (Lam @ M @ P @ Q @ (Lam @ N @ P @ Q))
| r_l_red : forall P Q, va_red1 (Lam @ Lam @ P @ Q) Lam
| r_l1_red : forall M P Q, va_red1 (Lam @ (Lam @ M) @ P @ Q) (Lam @ Q @ M @ P) (* for environments *)
| r_l2_red : forall M N P Q, va_red1 (Lam @ (Lam @ M @ N) @ P @ Q) (Lam @ M @ (Lam @ N @ P @ Q))
(* applications *)
| appl_va_red : forall M M' N, va_red1 M M' -> va_red1 (M @ N) (M' @ N)
| appr_va_red : forall M N N', va_red1 N N' -> va_red1 (M @ N) (M @ N')
.
Hint Constructors va_red1: TreeHintDb.
(* va_red *)
Definition va_red := multi_step va_red1.
Lemma preserves_appl_va_red : preserves_appl va_red.
Proof. apply preserves_appl_multi_step. red; auto_t. Qed.
Lemma preserves_appr_va_red : preserves_appr va_red.
Proof. apply preserves_appr_multi_step. red; auto_t. Qed.
Lemma preserves_app_va_red : preserves_app va_red.
Proof. apply preserves_app_multi_step; red; auto_t. Qed.
Ltac eval_pc :=
intros; repeat (eapply succ_red ; [ auto 10 with TreeHintDb; fail|]); try eapply zero_red.
Definition reflective red := forall (M: VA), red M M.
Lemma reflective_va_red: reflective va_red.
Proof. red; red; auto_t. Qed.
Hint Resolve reflective_va_red: TreeHintDb.
(* simultaneous reduction *)
Inductive s_red1 : VA -> VA -> Prop :=
| ref_pred : forall i, s_red1 (Ref i) (Ref i)
| v0_pred : s_red1 Vop Vop
| l0_pred : s_red1 Lam Lam
(* variables and abstractions *)
| v_pred : forall M M' N N' P P', s_red1 M M' -> s_red1 N N' -> s_red1 P P' ->
s_red1 (Vop @ M @ N @ P) (Vop @ (Vop @ M' @ N') @ P')
(* substitution *)
| r_v_pred: forall P Q Q', s_red1 Q Q' -> s_red1 (Lam @ Vop @ P @ Q) Q'
| r_v1_pred : forall M M' P P' Q, s_red1 M M' -> s_red1 P P' -> s_red1 (Lam @ (Vop @ M) @ P @ Q) (P' @ M')
| r_v2_pred : forall M M' N N' P P' Q Q',
s_red1 M M' -> s_red1 N N' -> s_red1 P P' -> s_red1 Q Q' ->
s_red1 (Lam @ (Vop @ M @ N) @ P @ Q) (Lam @ M' @ P' @ Q' @ (Lam @ N' @ P' @ Q'))
| r_l_pred : forall P Q, s_red1 (Lam @ Lam @ P @ Q) Lam
| r_l1_pred : forall M M' P P' Q Q', s_red1 M M' -> s_red1 P P' -> s_red1 Q Q' -> s_red1 (Lam @ (Lam @ M) @ P @ Q) (Lam @ Q' @ M' @ P')
| r_l2_pred : forall M M' N N' P P' Q Q',
s_red1 M M' -> s_red1 N N' -> s_red1 P P' -> s_red1 Q Q' ->
s_red1 (Lam @ (Lam @ M @ N) @ P @ Q) (Lam @ M' @ (Lam @ N' @ P' @ Q'))
(* applications *)
| app_s_red : forall M M' N N', s_red1 M M' -> s_red1 N N' -> s_red1 (M @ N) (M' @ N')
.
Hint Constructors s_red1: TreeHintDb.
Definition implies_red (red1 red2: VA-> VA-> 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 : VA -> VA -> 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 (?&?) ;exists x; split_all. Qed.
Lemma diamond_strip :
forall red1 red2, diamond red1 red2 -> diamond red1 (multi_step red2).
Proof.
red; intros red1 red2 d; eapply2 diamond_flip;
red; intros M N r; induction r; intros P0 r0; auto_t;
elim (d M P0 r0 N); auto; intro x; split_all;
elim(IHr d x); auto; intros x0 (?&?); exists x0; split; auto_t; eapply2 succ_red.
Qed.
Definition diamond_star (red1 red2: VA -> VA -> 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.
red; intros red1 red2 d M N r; induction r; intros P0 r0; auto_t;
elim(d M P0 r0 N); auto; intro x; split_all;
elim(IHr d x); auto; intros x0 (?&?); exists x0; split; auto_t; eapply2 transitive_red.
Qed.
Lemma diamond_tiling :
forall red1 red2, diamond red1 red2 -> diamond (multi_step red1) (multi_step red2).
Proof.
red; intros red1 red2 d M N r; induction r as [| red3 Q R T r1 ]; intros P0 r0; auto_t;
elim(diamond_strip red3 red2 d _ _ r1 P0); auto; intros x; split_all;
elim(IHr d x); auto; intros x0 (?&?); exists x0; auto_t.
Qed.
Hint Resolve diamond_tiling: TreeHintDb.
Lemma s_red_refl: forall M, s_red1 M M.
Proof. induction M; split_all. 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_t. Qed.
Lemma preserves_appr_s_red : preserves_appr s_red.
Proof. apply preserves_appr_multi_step. red; auto_t. Qed.
Lemma preserves_app_s_red : preserves_app s_red.
Proof. apply preserves_app_multi_step; red; auto_t. Qed.
Lemma diamond_s_red1_s_red1 : diamond s_red1 s_red1.
Proof.
red; intros M N OR; induction OR; split_all; inv s_red1; auto_t;
[
elim(IHOR1 M'0); elim(IHOR2 N'0); elim(IHOR3 P'0); split_all; exist(Vop @ (Vop @ x1 @ x0) @ x) |
elim(IHOR1 N'2); elim(IHOR2 N'1); elim(IHOR3 N'0); split_all; exist(Vop @ (Vop @ x1 @ x0) @ x) |
elim(IHOR N'); split_all; exist x |
elim(IHOR1 M'0); elim(IHOR2 P'0); split_all; exist(x @ x0) |
elim(IHOR1 N'2); elim(IHOR2 N'0); split_all; exist (x @ x0) |
elim(IHOR1 M'0); elim(IHOR2 N'0); elim(IHOR3 P'0); elim(IHOR4 Q'0); split_all;
exist(Lam @ x2 @ x0 @ x @ (Lam @ x1 @ x0 @ x)); auto 20 with TreeHintDb |
elim(IHOR1 N'2); elim(IHOR2 N'3); elim(IHOR3 N'1); elim(IHOR4 N'0); split_all;
exist(Lam @ x2 @ x0 @ x @ (Lam @ x1 @ x0 @ x)); auto 20 with TreeHintDb |
elim(IHOR1 M'0); elim(IHOR2 P'0); elim(IHOR3 Q'0); split_all; exist (Lam @ x @ x1 @ x0) |
elim(IHOR1 N'2); elim(IHOR2 N'0); elim(IHOR3 N'); split_all; exist (Lam @ x @ x1 @ x0) |
elim(IHOR1 M'0); elim(IHOR2 N'0); elim(IHOR3 P'0); elim(IHOR4 Q'0); split_all;
exist(Lam @ x2 @ (Lam @ x1 @ x0 @ x)); auto 20 with TreeHintDb |
elim(IHOR1 N'2); elim(IHOR2 N'3); elim(IHOR3 N'1); elim(IHOR4 N'0); split_all;
exist(Lam @ x2 @ (Lam @ x1 @ x0 @ x)); auto 20 with TreeHintDb |
elim(IHOR1 (Vop @ M'0 @ N'0)); elim(IHOR2 P'); split_all; inv s_red1;
exist(Vop @ (Vop @ N'4 @ N'3) @ x) |
elim(IHOR2 P); split_all; exist x |
elim(IHOR1 (Lam @ (Vop @ M'0) @ P')); split_all; inv s_red1; exist (N'1 @ N'4) |
elim(IHOR1 (Lam @ (Vop @ M'0 @ N'0) @ P')); elim(IHOR2 Q'); split_all; inv s_red1;
exist (Lam @ N'5 @ N'4 @ x @ (Lam @ N'6 @ N'4 @ x)); auto 20 with TreeHintDb |
elim(IHOR1 (Lam @ (Lam @ M'0) @ P')); elim(IHOR2 Q'); split_all; inv s_red1;
exist (Lam @ x @ N'4 @ N'1) |
elim(IHOR1 (Lam @ (Lam @ M'0 @ N'0) @ P')); elim(IHOR2 Q'); split_all; inv s_red1;
exist (Lam @ N'5 @ (Lam @ N'6 @ N'4 @ x)); auto 20 with TreeHintDb |
elim(IHOR1 M'0); elim(IHOR2 N'0); split_all
].
Qed.
Hint Resolve diamond_s_red1_s_red1: TreeHintDb.
Lemma diamond_s_red1_s_red : diamond s_red1 s_red.
Proof. eapply2 diamond_strip. Qed.
Lemma diamond_s_red : diamond s_red s_red.
Proof. eapply2 diamond_tiling. Qed.
Hint Resolve diamond_s_red: TreeHintDb.
Lemma va_red1_to_s_red1 : implies_red va_red1 s_red1.
Proof. intros M N R; induction R; split_all. Qed.
Lemma va_red_to_s_red: implies_red va_red s_red.
Proof.
eapply2 implies_red_multi_step.
red; split_all; one_step; eapply2 va_red1_to_s_red1.
Qed.
Lemma to_va_red_multi_step: forall red, implies_red red va_red -> implies_red (multi_step red) va_red.
Proof.
red; intros red B M N R; induction R; split_all; red; split_all;
assert(va_red M N) by eapply2 B; apply transitive_red with N; auto; eapply2 IHR.
Qed.
Lemma s_red1_to_va_red: implies_red s_red1 va_red .
Proof.
red; intros M N OR; induction OR; split_all;
try (eapply2 succ_red; eapply2 preserves_app_va_red); auto 10;
repeat eapply2 preserves_app_va_red.
Qed.
Hint Resolve s_red1_to_va_red: TreeHintDb.
Lemma s_red_to_va_red: implies_red s_red va_red.
Proof. eapply2 to_va_red_multi_step. Qed.
Lemma diamond_va_red: diamond va_red va_red.
Proof.
red; intros M N r1 P r2;
assert(s1: s_red M N) by eapply2 va_red_to_s_red;
assert(s2: s_red M P) by eapply2 va_red_to_s_red;
elim(diamond_s_red M N s1 P); auto; intro x; exist x; split_all; eapply2 s_red_to_va_red.
Qed.
Hint Resolve diamond_va_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_va_calculus: confluence VA va_red.
Proof. red; split_all; eapply2 diamond_va_red. Qed.
(* Programs *)
Ltac zerotac := try eapply2 zero_red.
Ltac succtac :=
repeat (eapply transitive_red;
[ eapply2 succ_red ;
match goal with
| |- multi_step va_red1 _ _ => idtac
| _ => fail (*gone too far ! *)
end
| ]); zerotac
.
Ltac aptac := eapply transitive_red; [ eapply preserves_app_va_red |].
Definition reducible M := exists N, va_red1 M N.
Inductive program : VA -> Prop :=
| pr_V0 : program Vop
| pr_S: forall M, program M -> program (Vop @ M)
| pr_H : forall M N, program M -> program N -> program (Vop @ M @ N)
| pr_A : program Lam
| pr_A1 : forall M, program M -> program (Lam @ M)
| pr_A2 : forall M N, program M -> program N -> program (Lam @ M @ N)
.
Hint Constructors program: TreeHintDb.
Ltac program_tac :=
match goal with
| |- program Vop => eapply2 pr_V0; program_tac
| |- program (Vop @ _) => eapply2 pr_S; program_tac
| |- program (Vop @ _ @ _) => eapply2 pr_H; program_tac
| |- program Lam => eapply2 pr_A; program_tac
| |- program (Lam @ _) => eapply2 pr_A1; program_tac
| |- program (Lam @ _ @ _) => eapply2 pr_A2; program_tac
| |- _ => idtac
end.
Definition valuable M := exists N, va_red M N /\ program N.
Lemma programs_are_irreducible: forall M, program M -> forall N, va_red1 M N -> False.
Proof.
intros M p; induction p; intros N0 r; inversion r; subst; inv va_red1; invsub;
((eapply2 IHp; fail) || (eapply2 IHp1; fail) || eapply2 IHp2).
Qed.
Lemma programs_are_stable: forall M, program M -> forall N, s_red1 M N -> N = M.
Proof.
intros M p; induction p; intros N0 r; inversion r; subst; inv s_red1; invsub;
(match goal with H : s_red1 _ ?N |- _ => rewrite (IHp N); auto; fail end) ||
(rewrite (IHp1 M'); auto; rewrite (IHp2 N'); auto; fail) ||
rewrite (IHp1 N'0); auto; rewrite (IHp2 N'); 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 H; intros; eapply H; eauto |
intros red M N r; induction r; split_all; subst;
assert(N = M) by eapply2 programs_are_stable; subst; auto
].
Qed.
Lemma triangle_s_red : forall M N P, s_red M N -> s_red M P -> program P -> s_red N P.
Proof.
intros; assert(d: exists Q, s_red N Q /\ s_red P Q) by eapply2 diamond_s_red;
elim d; auto; intro x; split_all;
assert(x = P) by eapply2 programs_are_stable2; subst; auto.
Qed.
Lemma refs_are_stable: forall x M, s_red (Ref x) M -> M = Ref x.
Proof.
cut (forall red P M, multi_step red P M -> red = s_red1 -> forall x, P = Ref x -> M = Ref x);
[ intro aux; intros; subst; eapply2 aux |
intros red P M r; induction r as [ | ???? r1]; split_all; subst; inversion r1; subst; eapply2 IHr].
Qed.
(* 9.3 : Combinators *)
(* Star Abstraction *)
Fixpoint substitute M x N :=
match M with
| Ref s => if s =? x then N else Ref s
| Vop => Vop
| Lam => Lam
| M1 @ M2 => (substitute M1 x N) @ (substitute M2 x N)
end.
Fixpoint bracket_body M x:=
match M with
Ref s => if s =? x then Vop else Vop @ (Ref s)
| Vop => Vop @ Vop
| Lam => Vop @ Lam
| M1 @ M2 => Vop @ (bracket_body M1 x) @ (bracket_body M2 x)
end.
Definition bracket x M := Lam @ (bracket_body M x) @ (Lam @ Vop @ Lam).
Lemma bracket_beta: forall M x N, va_red (bracket x M @ N) (substitute M x N).
Proof.
induction M; unfold bracket; split_all; succtac;
[caseEq (s=?x); split_all; succtac
| aptac; [ eapply2 IHM1 | eapply2 IHM2 | zerotac]
].
Qed.
Inductive combination : VA -> Prop :=
| is_Vop : combination Vop
| is_Lam : combination Lam
| is_App : forall M N, combination M -> combination N -> combination (M@ N)
.
Hint Constructors combination : TreeHintDb.
Lemma programs_are_combinations: forall p, program p -> combination p.
Proof. intros p pr; induction pr; split_all. Qed.
Fixpoint occurs x M :=
match M with
| Ref y => eqb y x
| Vop | Lam => false
| M1 @ M2 => (occurs x M1) || (occurs x M2)
end.
Lemma occurs_combination: forall M x, combination M -> occurs x M = false.
Proof. induction M; intros x c; inversion c; subst;simpl;auto; rewrite IHM1; auto; rewrite IHM2; auto. Qed.
Lemma substitute_occurs_false: forall M x N, occurs x M = false -> substitute M x N = M.
Proof.
induction M; intros x N occ; simpl in *; auto;
[ rewrite occ; auto
| rewrite orb_false_iff in *; inversion occ; rewrite IHM1; auto; rewrite IHM2; auto
].
Qed.
Fixpoint star_body x M :=
match M with
| Ref s => if s =? x then Vop else Vop @ (Ref s)
| Vop => Vop @ Vop
| Lam => Vop @ Lam
| M1 @ M2 => if occurs x (M1 @ M2)
then Vop @ (star_body x M1) @ (star_body x M2)
else Vop @ (M1 @ M2)
end.
Definition star x M := Lam @ (star_body x M) @ (Lam @ Vop @ Lam).
Lemma star_id: forall x, star x (Ref x) = Lam @ Vop @ (Lam @ Vop @ Lam).
Proof. intro; unfold star, star_body, occurs; rewrite eqb_refl; auto. Qed.
Lemma star_body_occurs_false: forall M x, occurs x M = false -> star_body x M = Vop @ M.
Proof. induction M; intros x occ; unfold star_body; simpl in *; rewrite ? occ; auto. Qed.
Lemma star_occurs_false: forall M x, occurs x M = false -> star x M = Lam @ (Vop @ M) @ (Lam @ Vop @ Lam).
Proof. induction M; intros x occ; unfold star, star_body; simpl in *; rewrite ? occ; auto. Qed.
Notation "\" := star : va_scope.
Theorem star_beta: forall M x N, va_red (\x M @ N) (substitute M x N).
Proof.
induction M; split_all; unfold star, star_body; fold star_body;
[ caseEq (s=? x); split_all; cbv; succtac
| cbv; succtac
| cbv; succtac
| caseEq (occurs x (M1 @ M2)); split_all; succtac;
[ aptac; [ eapply2 IHM1 | eapply2 IHM2 | zerotac]
| succtac; rewrite orb_false_iff in *; split_all;
rewrite ! substitute_occurs_false; auto; cbv; succtac
]].
Qed.
Fixpoint ndx k :=
match k with
| 0 => Vop
| S k1 => Vop @ (ndx k1)
end.
Fixpoint env args :=
match args with
| nil => Lam @ Vop @ Lam
| cons a args1 => Lam @ (Lam @ (env args1)) @ a
end.
(*
Fixpoint env_comb args :=
match args with
| nil => Lam @ Vop @ Lam
| cons a args1 => Vop @ (Vop @ Lam @ (Vop @ Lam @ (env_comb args1))) @ a
end.
*)
Lemma succ_ndx_red : forall M sigma N, va_red (Lam @ (Vop @ M) @ sigma @ N) (sigma @ M).
Proof. intros; cbv; succtac. Qed.
Lemma env_nil_red : forall M, va_red (env nil @ M) M.
Proof. intros; unfold env; fold env; succtac. Qed.
Lemma env_cons_red :
forall a args M, va_red (env(cons a args) @ (Vop @ M)) (env args @ M).
Proof. intros; unfold env; fold env; succtac. Qed.
Lemma env_zero_red : forall a args , va_red (env(cons a args) @ Vop) a.
Proof. intros; unfold env; fold env; succtac. Qed.
Lemma env_succ_red: forall a args x , va_red (env(cons a args) @ (Vop@ x)) (env args @ x).
Proof. intros; unfold env; fold env; succtac. Qed.
(* Combinators *)
Definition Iop := Lam @ Vop @ Lam .
Definition Kop := Lam @ (Lam @ (v Vop) @ (Lam @ (Lam @ (Lam @ Vop @ Lam)) @ Vop)) @ Lam .
Definition Sop := Eval cbv in \"x" (\"y" (\"z" (Ref "x" @ Ref "z" @ (Ref "y" @ Ref "z")))).
Definition KI := Lam @ Iop @ Lam.
Lemma i_red : forall M, va_red (Iop @ M) M.
Proof. intros; cbv; succtac. Qed.
Lemma ki_red : forall M N, va_red (KI @ M @ N) N.
Proof. intros; cbv; succtac. Qed.
Lemma k_red : forall M N, va_red (Kop @ M @ N) M.
Proof. intros; cbv; succtac. Qed.
Lemma sop_red: forall M N P, va_red (Sop @ M @ N @ P) (M@P@(N@P)).
Proof.
intros; cbv; succtac. aptac. succtac. zerotac. aptac. aptac. aptac. aptac. zerotac.
aptac. aptac. succtac. succtac. zerotac. zerotac. zerotac. zerotac. zerotac. succtac. zerotac.
succtac. zerotac. succtac. aptac. succtac. aptac. aptac. aptac. succtac. aptac. aptac. succtac.
succtac. all: zerotac. succtac. apply preserves_app_va_red. 2: zerotac. aptac. aptac. aptac. zerotac.
aptac. aptac. succtac. succtac. all: zerotac. succtac.
Qed.
Definition meaningful_translation_SK_to_VA f :=
(forall M N, sk_red1 M N -> va_red (f M) (f N)) /\ (* strong version *)
(forall M N, f (Incompleteness_of_Combinatory_Logic.App M N) = App (f M) (f N)) /\ (* applications *)
(forall M, Incompleteness_of_Combinatory_Logic.program M -> valuable (f M)) /\ (* programs *)
(forall i, f (Incompleteness_of_Combinatory_Logic.Ref i) = Ref i) . (* variables *)
Fixpoint sk_to_va M :=
match M with
| Incompleteness_of_Combinatory_Logic.Ref i => Ref i
| Incompleteness_of_Combinatory_Logic.Sop => Sop
| Incompleteness_of_Combinatory_Logic.Kop => Kop
| Incompleteness_of_Combinatory_Logic.App M1 M2 => (sk_to_va M1) @ (sk_to_va M2)
end.
Lemma preserves_reduction1_sk_to_va:
forall M N, sk_red1 M N -> va_red (sk_to_va M) (sk_to_va N).
Proof.
intros M N r; induction r; subst; split_all;
[eapply2 sop_red | eapply2 k_red | | ]; eapply2 preserves_app_va_red.
Qed.
Lemma preserves_reduction_sk_to_va:
forall M N, sk_red M N -> va_red (sk_to_va M) (sk_to_va N).
Proof.
cut (forall red M N, Incompleteness_of_Combinatory_Logic.multi_step red M N -> red = sk_red1 ->
va_red (sk_to_va M) (sk_to_va N));
[ intro aux; intros; eapply2 aux
| intros red M N r; induction r; subst; split_all; subst;
eapply transitive_red; [ eapply2 preserves_reduction1_sk_to_va | eapply2 IHr]].
Qed.
Theorem meaningful_translation_from_sk_to_va:
meaningful_translation_SK_to_VA sk_to_va.
Proof.
red; repeat split;
[ eapply2 preserves_reduction1_sk_to_va
| intros M prM; induction prM].
{ exists Sop; split; [ apply zero_red | cbv; auto; program_tac]. }
{
elim IHprM; intro x; split_all. repeat eexists.
unfold Sop. aptac. 2: eassumption. succtac.
eapply transitive_red.
succtac. eapply transitive_red. succtac. aptac. aptac. zerotac. aptac. aptac. zerotac.
succtac. 1-5: zerotac. aptac. aptac. zerotac. aptac. aptac. zerotac. aptac. succtac.
aptac. aptac. zerotac. aptac. zerotac. aptac. succtac. succtac. all: zerotac.
program_tac. }
{
elim IHprM1; intro x; split_all; elim IHprM2; intro x0; split_all.
red. repeat eexists. aptac. aptac. zerotac. eassumption. zerotac. eassumption.
unfold Sop. aptac. succtac. zerotac.
eapply transitive_red. succtac.
aptac. aptac. aptac. aptac. zerotac. succtac. succtac. zerotac. aptac. aptac. zerotac. aptac.
zerotac. aptac. aptac. zerotac. aptac. zerotac. aptac. succtac. succtac. 1-6: zerotac. succtac.
zerotac. succtac. zerotac. succtac. zerotac.
aptac. aptac. zerotac. aptac. zerotac. aptac. aptac. zerotac. aptac. succtac. succtac.
1-8: zerotac. program_tac. }
{exist Kop; unfold Kop, v; cbv; program_tac. }
{
elim IHprM; intro x; split_all; repeat eexists;
[ aptac; [ zerotac | eassumption | unfold Kop, v; succtac] | program_tac].
}
Qed.
(* 9.4: Incompleteness *)
Definition is_equal eq :=
(forall M, program M -> va_red (eq @ M @ M @ (Ref "x") @ (Ref "y")) (Ref "x"))
/\
( forall M N, program M -> program N -> M <> N ->
va_red (eq @ M @ N @ (Ref "x") @ (Ref "y")) (Ref "y")).
Definition identity_program id := program id /\ va_red (App id (Ref "x")) (Ref "x").
Definition identity_value id := valuable id /\ va_red (App id (Ref "x")) (Ref "x").
Inductive id_red : VA -> VA -> Prop :=
| id_id : id_red (Lam @ Vop @ (Lam @ Vop @ Lam)) (Lam @ Vop @ Lam)
| id_ref: forall i, id_red (Ref i) (Ref i)
| id_V : id_red Vop Vop
| id_A : id_red Lam Lam
| id_app : forall M M' N N', id_red M M' -> id_red N N' -> id_red (M @ N) (M' @ N')
.
Hint Constructors id_red: TreeHintDb.
Lemma id_red_refl: forall M, id_red M M.
Proof. induction M; auto_t. Qed.
(* The pentagon for s_red1 *)
Fixpoint term_size M :=
match M with
Ref _ => 1
| Vop => 1
| Lam => 1
| App M1 M2 => term_size M1 + term_size M2
end.
Lemma term_size_positive: forall M, term_size M > 0.
Proof. induction M; split_all; lia. Qed.
Lemma pentagon_id_red_s_red1:
forall M N, id_red M N ->
forall P, s_red1 M P ->
exists Q1 Q2, s_red P Q1 /\ id_red Q1 Q2 /\
s_red N Q2 .
Proof.
Ltac IHtac IHk k :=
match goal with
| H : id_red ?x ?y , H2 : s_red1 ?x ?z |- _ =>
assert(sx: term_size x < k) by lia; elim (IHk x sx y H z); split_all; clear H sx
end.
cut (forall k M, term_size M < k ->
forall N, id_red M N ->
forall P, s_red1 M P ->
exists Q1 Q2, s_red P Q1 /\ id_red Q1 Q2 /\
s_red N Q2 );
[ intro aux; intros; eapply2 aux
| induction k; intros M s N ir P r; simpl in *; [ lia | inversion ir; subst; split_all]].
{ inv s_red1; exist (Lam @ Vop @ Iop); exist Iop; unfold Iop; zerotac. }
{ inv s_red1; exist(Ref i); exist (Ref i); apply zero_red. }
{ inv s_red1; exists Vop; exist Vop; apply zero_red. }
{ inv s_red1; exists Lam; exist Lam; zerotac. }
inversion r; clear r ir; subst; inv id_red; simpl in *; inv s_red1; repeat (IHtac IHk k).
{
exist (v (v x3 @ x1) @ x); exist (v (v x4 @ x2) @ x0); unfold v; auto; zerotac;
[ repeat eapply2 preserves_app_s_red; zerotac
| auto_t
| eapply succ_red; [ eapply2 v_pred | repeat eapply2 preserves_app_s_red; zerotac]].
}
{ exist x; exist x0; eapply2 succ_red. }
{ exist x; exist x0; eapply2 succ_red. }
{
exist (x @ x1); exist (x0 @ x2);
[ eapply2 preserves_app_s_red; eapply2 zero_red |
eapply succ_red; [ eapply2 r_v1_pred | eapply2 preserves_app_s_red]].
}{
exist (Lam @ x5 @ x1 @ x @ (Lam @ x3 @ x1 @ x));
exist (Lam @ x6 @ x2 @ x0 @ (Lam @ x4 @ x2 @ x0));
[ repeat eapply2 preserves_app_s_red; zerotac
| auto 20 with TreeHintDb
| eapply succ_red; [ eapply2 r_v2_pred | repeat eapply2 preserves_app_s_red; zerotac]].
}
{ exist Lam; exist Lam; zerotac; eapply2 succ_red. }
{
exist (Lam @ x @ x3 @ x1); exist (Lam @ x0 @ x4 @ x2);
[ repeat (eapply2 preserves_app_s_red); zerotac
| eapply succ_red; [ eapply2 r_l1_pred | repeat eapply2 preserves_app_s_red; zerotac]].
}
{ exist (Lam @ Vop @ Iop); exist Iop; [ eapply2 preserves_app_s_red ; zerotac | ]; eapply2 succ_red. }
{
exist (Lam @ x5 @ (Lam @ x3 @ x1 @ x)); exist (Lam @ x6 @ (Lam @ x4 @ x2 @ x0));
[ repeat eapply2 preserves_app_s_red; zerotac
| auto 20 with TreeHintDb
| eapply succ_red; [ eapply2 r_l2_pred | repeat eapply2 preserves_app_s_red; zerotac]
].
}{
assert(term_size M0 >0) by eapply2 term_size_positive;
assert(term_size N0 >0) by eapply2 term_size_positive;
repeat (IHtac IHk k); exist (App x1 x); exist (App x2 x0);
eapply2 preserves_app_s_red.
}
Qed.
(*
Generalizing the pentagon from s_red1 to s_red
requires a count of the number of reductions,
so use s_ranked instead.
*)
Inductive s_ranked : nat -> VA -> VA -> Prop :=
| ranked_zero: forall M, s_ranked 0 M M
| ranked_succ: forall n M N P, s_red1 M N -> s_ranked n N P -> s_ranked (S n) M P
.
Hint Constructors s_ranked: TreeHintDb.
Lemma transitive_s_ranked:
forall n M N, s_ranked n M N -> forall p P, s_ranked p N P ->
s_ranked (n+p) M P.
Proof. induction n; intros M N r; inversion r; split_all; subst; eapply2 ranked_succ. Qed.
Lemma s_red_implies_s_ranked:
forall M N, s_red M N -> exists n, s_ranked n M N.
Proof.
cut(forall red M N, multi_step red M N -> red = s_red1 -> exists n, s_ranked n M N);
[ intro aux; intros; eapply aux; eauto
| intros red M N r; induction r; split_all; subst; auto_t;
assert(aux0: exists n, s_ranked n N P) by auto;
elim aux0; auto; intro x; exist (S x); eapply2 ranked_succ].
Qed.
Lemma s_ranked_implies_s_red:
forall n M N, s_ranked n M N -> s_red M N.
Proof.
induction n; intros M N r; inversion r; subst; auto; [apply zero_red | eapply2 succ_red; eapply2 IHn].
Qed.
Lemma diamond_s_ranked_strip :
forall m M N, s_ranked m M N ->
forall P, s_red1 M P ->
exists Q, s_ranked m P Q /\ s_red1 N Q.
Proof.
induction m; intros M N r P rP; inversion r; subst; auto_t;
assert(d: exists Q, s_red1 N0 Q /\ s_red1 P Q) by (eapply diamond_s_red1_s_red1; eauto);
elim d; intro x; split_all;
assert(d2 : exists Q : VA, s_ranked m x Q /\ s_red1 N Q) by eapply2 IHm;
elim d2; auto; intro x1; exist x1; apply ranked_succ with x; auto.
Qed.
Lemma diamond_s_ranked :
forall n M N, s_ranked n M N ->
forall p P, s_ranked p M P ->
exists Q, s_ranked n P Q /\ s_ranked p N Q.
Proof.
induction n; intros M N r p P r2; inversion r; subst; auto_t;
assert(d: exists Q, s_ranked p N0 Q /\ s_red1 P Q) by eapply2 diamond_s_ranked_strip;
elim d; intro x; split_all;
assert(d2 : exists Q : VA, s_ranked n x Q /\ s_ranked p N Q) by eapply2 IHn;
elim d2; intros x1; exist x1; apply ranked_succ with x; auto.
Qed.
Lemma pentagon_id_red_s_ranked:
forall m M P, s_ranked m M P ->
forall N, id_red M N ->
exists p Q1 Q2 n, s_ranked p P Q1 /\ id_red Q1 Q2 /\
s_ranked n N Q2 .
Proof.
induction m; intros M P r N ir; inversion r; clear r; subst; eauto.
(* 2 *)
exists 0; exists P; exist N; exist 0; repeat split; auto.
(* 1 *)
assert(pent: exists Q1 Q2, s_red N0 Q1 /\ id_red Q1 Q2 /\ s_red N Q2) by
eapply2 pentagon_id_red_s_red1.
elim pent; intros x x0; clear pent; split_all.
assert(ppr: exists n, s_ranked n N0 x) by (eapply s_red_implies_s_ranked; eauto).
elim ppr; intro m1; clear ppr; split_all.
assert(d: exists Q, s_ranked m x Q /\ s_ranked m1 P Q) by eapply2 diamond_s_ranked.
elim d; intros x2; clear d; split_all.
assert(pent2: exists p Q1 Q2 n, s_ranked p x2 Q1 /\ id_red Q1 Q2 /\ s_ranked n x1 Q2).
eapply IHm; eauto.
elim(pent2). intros p; clear pent2; intros pent3; split_all.
assert(ppr2: exists q, s_ranked q N x1) by eapply2 s_red_implies_s_ranked.
elim(ppr2); intro q; clear ppr2; split_all.
exists (m1 + p); exists x0; exists x3; exist (q + x4); eapply transitive_s_ranked; eauto.
Qed.
Lemma pentagon_id_red_s_red:
forall M P, s_red M P ->
forall N, id_red M N ->
exists Q1 Q2, s_red P Q1 /\ id_red Q1 Q2 /\
s_red N Q2 .
Proof.
intros M P r N idr.
elim(s_red_implies_s_ranked _ _ r); intros.
assert(pent: exists p Q1 Q2 n, s_ranked p P Q1 /\ id_red Q1 Q2 /\ s_ranked n N Q2)
by eapply2 pentagon_id_red_s_ranked.
elim pent. intros p; clear pent. intro pent2; elim pent2; intros Q1 pent3; clear pent2.
elim pent3; intros Q2 n; clear pent3; split_all.
exists Q1; exist Q2.
all: eapply2 s_ranked_implies_s_red.
Qed.
(*
If parallel reduction is to a variable
then the pentagon becomes a triangle. -- not used in A_Tree!
*)
Lemma triangle_id_red_s_red_ref:
forall M x, s_red M (Ref x) ->
forall N, id_red M N ->
s_red N (Ref x) .
Proof.
intros M x r N ir.
assert(pent: exists Q1 Q2, s_red (Ref x) Q1 /\ id_red Q1 Q2 /\
s_red N Q2) by eapply2 pentagon_id_red_s_red.
elim pent. intros Q1 pent2; clear pent.
elim pent2; intro Q2; clear pent2; split_all.
assert(Q1 = Ref x) by eapply2 refs_are_stable; subst.
inv id_red.
Qed.
Theorem equality_of_programs_is_not_definable_in_va: forall eq, not (is_equal eq).
Proof.
intros; intro premise; inversion premise as (eq0 & eq1).
assert(r: s_red (eq @ Iop @ (Lam @ Vop @ Iop) @ (Ref "x") @ (Ref "y")) (Ref "y")).
eapply va_red_to_s_red. eapply2 premise. cbv; program_tac. cbv; program_tac. discriminate.
elim(pentagon_id_red_s_red (eq @ Iop @ (Lam @ Vop @ Iop) @ (Ref "x") @ (Ref "y")) (Ref "y")
r (eq @ Iop @ Iop @ (Ref "x") @ (Ref "y"))); split_all.
2: repeat eapply2 id_app; eapply2 id_red_refl.
assert(x = Ref "y") by eapply2 refs_are_stable. subst.
inv id_red.
assert (r2: s_red (eq @ Iop @ Iop @ (Ref "x") @ (Ref "y")) (Ref "x")).
eapply2 va_red_to_s_red.
eapply2 eq0. cbv; program_tac.
elim(diamond_s_red _ _ r2 (Ref "y")); auto. intros x (s1&s2).
assert(x = Ref "x") by eapply2 refs_are_stable.
assert(x = Ref "y") by eapply2 refs_are_stable. subst; discriminate.
Qed.
(* 9.5 comes after 9.7, for convenience. *)
(* 9.6: Tagging *)
Definition tag t f :=
Lam
@ (Vop
@ (Vop
@ (v (ndx 2) @ (ndx 1))
@ (v (v (v (v f))) @ Vop)
)
@ (v (v (v t))))
@ (Lam @ (Lam @ (env ((env (Kop :: nil)) :: nil))) @ Vop).
Definition getTag := (* \"" (Lam @ (Ref "") @ Iop @ KI @ Lam). *)