-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathIncompleteness_of_Combinatory_Logic.v
1619 lines (1227 loc) · 54.6 KB
/
Incompleteness_of_Combinatory_Logic.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 8: Incompleteness of Combinatory Logic *)
(* *)
(* Barry Jay *)
(* *)
(**********************************************************************)
Require Import Arith Lia Bool List String.
Require Import Reflective.Rewriting_partI.
Set Default Proof Using "Type".
Ltac invsub :=
match goal with
| H : _ = _ |- _ => injection H; clear H; invsub
| _ => intros; subst
end.
(* 8.2: SK-Calculus *)
Inductive SK: Set :=
| Ref : string -> SK (* variables are indexed by strings *)
| Sop : SK
| Kop : SK
| App : SK -> SK -> SK
.
Hint Constructors SK : TreeHintDb.
Open Scope string_scope.
Declare Scope sk_scope.
Open Scope sk_scope.
Notation "x @ y" := (App x y) (at level 65, left associativity) : sk_scope.
Definition Iop := Sop @ Kop @ Kop.
Inductive combination : SK -> Prop :=
| is_S : combination Sop
| is_K : combination Kop
| is_App : forall M N, combination M -> combination N -> combination (M@ N)
.
Hint Constructors combination : TreeHintDb.
(* SK-reduction *)
Inductive sk_red1 : SK -> SK -> Prop :=
| s__red: forall (M N P : SK), sk_red1 (Sop @M@ N@ P) (M@P@(N@P))
| k_red : forall M N, sk_red1 (Kop @ M@ N) M
| appl_red : forall M M' N, sk_red1 M M' -> sk_red1 (M@ N) (M' @ N)
| appr_red : forall M N N', sk_red1 N N' -> sk_red1 (M@ N) (M@ N')
.
Hint Constructors sk_red1 : TreeHintDb.
(* Multiple reduction steps *)
Inductive multi_step : (SK -> SK -> Prop) -> SK -> SK -> Prop :=
| zero_red : forall red M, multi_step red M M
| succ_red : forall (red: SK-> SK -> 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: SK), red M N -> red N P -> red M P.
Lemma transitive_red : forall red, transitive (multi_step red).
Proof. red; induction 1; intros; simpl; auto. apply succ_red with N; auto. Qed.
Definition preserves_appl (red : SK -> SK -> Prop) :=
forall M N M', red M M' -> red (M@ N) (M' @ N).
Definition preserves_appr (red : SK -> SK -> Prop) :=
forall M N N', red N N' -> red (M@ N) (M@ N').
Definition preserves_app (red : SK -> SK -> 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: SK -> SK -> Prop), preserves_appl red -> preserves_appl (multi_step red).
Proof. red; intros red hy M N M' r; induction r; auto with TreeHintDb; eapply succ_red; eauto. Qed.
Lemma preserves_appr_multi_step :
forall (red: SK -> SK -> Prop), preserves_appr red -> preserves_appr (multi_step red).
Proof. red; intros red hy M N M' r; induction r; auto with TreeHintDb; eapply succ_red; eauto. Qed.
Lemma preserves_app_multi_step :
forall (red: SK -> SK -> Prop),
preserves_appl red -> preserves_appr red ->
preserves_app (multi_step red).
Proof.
red; intros red pl pr M M' rM N N' rN;
apply transitive_red with (M' @ N); [ apply preserves_appl_multi_step | apply preserves_appr_multi_step] ;
auto.
Qed.
(* sk_red *)
Definition sk_red := multi_step sk_red1.
Lemma sk_red_refl: forall M, sk_red M M. Proof. intros; apply zero_red. Qed.
Hint Resolve sk_red_refl : TreeHintDb.
Lemma preserves_appl_sk_red : preserves_appl sk_red.
Proof. apply preserves_appl_multi_step. red; auto_t. Qed.
Lemma preserves_appr_sk_red : preserves_appr sk_red.
Proof. apply preserves_appr_multi_step. red; auto_t. Qed.
Lemma preserves_app_sk_red : preserves_app sk_red.
Proof. apply preserves_app_multi_step; red; auto_t. Qed.
Ltac eval_tac1 :=
match goal with
| |- sk_red ?M _ => red; eval_tac1
(* 4 apps *)
| |- multi_step sk_red1 (_ @ _ @ _ @ _ @ _) _ =>
eapply transitive_red; [eapply preserves_app_sk_red; [eval_tac1|auto_t] |auto_t]
(* 3 apps *)
| |- multi_step sk_red1 (Sop @ _ @ _ @ _) _ => eapply succ_red; auto_t
| |- multi_step sk_red1 (Kop @ _ @ _ @ _) _ =>
eapply transitive_red; [eapply preserves_app_sk_red; [eval_tac1|auto_t] |]; auto_t
| |- multi_step sk_red1 (_ @ _ @ _ @ _) (_ @ _) => eapply preserves_app_sk_red; eval_tac1
(* 2 apps *)
| |- multi_step sk_red1 (Sop @ _ @ _) (Sop @ _ @ _) _ =>
apply preserves_app_sk_red; [ apply preserves_app_sk_red |]; eval_tac1
| |- multi_step sk_red1 (Kop @ _ @ _ ) _ => eapply succ_red; auto_t
| |- multi_step sk_red1 (_ @ _ @ _) (_ @ _) => eapply preserves_app_sk_red; eval_tac1
| |- multi_step sk_red1 (_ @ _) (_ @ _) => apply preserves_app_sk_red; eval_tac1
| _ => auto_t
end.
Ltac eval_tac := intros; cbv; repeat eval_tac1.
Ltac zerotac := try apply zero_red.
Ltac succtac :=
repeat (eapply transitive_red;
[ eapply succ_red; auto_t ;
match goal with | |- multi_step sk_red1 _ _ => idtac | _ => fail end
| ])
.
Ltac aptac := eapply transitive_red; [ eapply preserves_app_sk_red |].
Ltac ap2tac:=
unfold sk_red;
eassumption ||
match goal with
| |- multi_step _ (_ @ _) _ => try aptac; [ap2tac | ap2tac | ]
| _ => idtac
end.
Ltac trtac := unfold Iop; succtac; zerotac.
Lemma i_red: forall M, sk_red (Iop @ M) M.
Proof. eval_tac. Qed.
Lemma i_alt_red: forall y M, sk_red (Sop @Kop@ y@ M) M.
Proof. eval_tac. Qed.
Ltac inv1 prop :=
match goal with
| H: prop (_ @ _) |- _ => inversion H; clear H; inv1 prop
| H: prop Sop |- _ => inversion H; clear H; inv1 prop
| H: prop Kop |- _ => inversion H; clear H; inv1 prop
| H: prop (Ref _) |- _ => inversion H; clear H; inv1 prop
| _ => subst; intros; auto_t
end.
Ltac inv red :=
match goal with
| H: multi_step red (_ @ _) _ |- _ => inversion H; clear H; inv red
| H: multi_step red (Ref _) _ |- _ => inversion H; clear H; inv red
| H: multi_step red Sop _ |- _ => inversion H; clear H; inv red
| H: multi_step red Kop _ |- _ => inversion H; clear H; inv red
| H: red (Ref _) _ |- _ => inversion H; clear H; inv red
| H: red (_ @ _) _ |- _ => inversion H; clear H; inv red
| H: red Sop _ |- _ => inversion H; clear H; inv red
| H: red Kop _ |- _ => inversion H; clear H; inv red
| H: multi_step red _ (Ref _) |- _ => inversion H; clear H; inv red
| H: multi_step red _ (_ @ _) |- _ => inversion H; clear H; inv red
| H: multi_step red _ Sop |- _ => inversion H; clear H; inv red
| H: multi_step red _ Kop |- _ => inversion H; clear H; inv red
| H: red _ (Ref _) |- _ => inversion H; clear H; inv red
| H: red _ (_ @ _) |- _ => inversion H; clear H; inv red
| H: red _ Sop |- _ => inversion H; clear H; inv red
| H: red _ Kop |- _ => inversion H; clear H; inv red
| _ => subst; intros; auto_t
end.
Definition implies_red (red1 red2: SK-> SK-> 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; intros; auto with TreeHintDb.
apply transitive_red with N; auto.
Qed.
Lemma to_sk_red_multi_step: forall red, implies_red red sk_red -> implies_red (multi_step red) sk_red.
Proof.
red. intros red B M N R; induction R; intros.
red; intros; auto with TreeHintDb.
assert(sk_red M N) by (apply B; auto).
apply transitive_red with N; auto.
apply IHR; auto with TreeHintDb.
Qed.
Inductive s_red1 : SK -> SK -> Prop :=
| ref_s_red : forall i, s_red1 (Ref i) (Ref i)
| sop_s_red : s_red1 Sop Sop
| kop_s_red : s_red1 Kop Kop
| app_s_red :
forall M M' ,
s_red1 M M' ->
forall N N' : SK, s_red1 N N' -> s_red1 (M@ N) (M' @ N')
| s_s_red: forall (M M' N N' P P' : SK),
s_red1 M M' -> s_red1 N N' -> s_red1 P P' ->
s_red1 (Sop @M @ N @ P) (M' @ P' @ (N' @ P'))
| k_s_red : forall M M' N, s_red1 M M' -> s_red1 (Kop @ M @ N) M'
.
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_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 sk_red1_to_s_red1 : implies_red sk_red1 s_red1.
Proof. red; intros M N B; induction B; intros; auto with TreeHintDb. Qed.
Lemma sk_red_to_s_red: implies_red sk_red s_red.
Proof.
apply implies_red_multi_step.
red; intros. eapply succ_red. eapply sk_red1_to_s_red1; auto_t. apply zero_red.
Qed.
Lemma s_red1_to_sk_red: implies_red s_red1 sk_red .
Proof.
red; intros M N OR; induction OR; auto_t.
2,3: eapply succ_red; auto_t.
all: ap2tac; zerotac.
Qed.
Hint Resolve s_red1_to_sk_red: TreeHintDb.
Lemma s_red_to_sk_red: implies_red s_red sk_red.
Proof. apply to_sk_red_multi_step; auto with TreeHintDb. Qed.
(* Diamond Lemmas *)
Definition diamond (red1 red2 : SK -> SK -> 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 d1 M N r1 P r2; elim (d1 M P r2 N r1); intros x (?&?);
exists x; tauto.
Qed.
Lemma diamond_strip :
forall red1 red2, diamond red1 red2 -> diamond red1 (multi_step red2).
Proof.
intros red1 red2 d; eapply diamond_flip; intros M N r; induction r;
[intro P; exist P |
intros P0 r0;
elim (d M P0 r0 N); auto_t; intros x (?&?);
elim(IHr d x); auto; intros x0 (?&?); exist x0; eapply2 succ_red
].
Qed.
Definition diamond_star (red1 red2: SK -> SK -> 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;
[ exist P0 |
elim(d M _ r0 N); auto; intros x (r1&r2);
elim(IHr d x); auto; intros x0 (?&?);
exist x0; eapply2 transitive_red
].
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 [| ? ? ? ? r1 r2];
[ intro P; exist P |
intros P0 r0;
elim(diamond_strip red red2 d M N r1 P0); auto;
intros N3 (r3&?);
elim(IHr2 d N3 r3); intros P1 (?&?);
exist P1; eapply2 succ_red
].
Qed.
Hint Resolve diamond_tiling: TreeHIntDb.
Lemma diamond_s_red1 : diamond s_red1 s_red1.
Proof.
red. intros M N r; induction r; intros P0 rP; auto_t.
(* 3 *)
inversion rP; clear rP; subst; inv s_red1; inv s_red1; auto_t.
(* 5 *)
elim(IHr1 M'0); auto; intros x (?&?);
elim(IHr2 N'0); auto; intros x0 (?&?); exists (x@ x0); split; auto_t.
(* 4 *)
elim(IHr1 (Sop @ M'0 @ N'0)); auto_t; intros x (?&?); inv s_red1; invsub.
elim(IHr2 P'); auto_t; intros x (?&?).
exist (N'4 @ x @ (N'3 @ x)).
(* 3 *)
elim(IHr1 (Kop@ P0)); auto_t; intros x (?&?); inv s_red1; invsub; auto_t.
(* 2 *)
inversion rP; clear rP; subst; inv s_red1; invsub.
elim(IHr1 N'2); elim(IHr2 N'1); elim(IHr3 N'0); auto_t; intros x (?&?) x0 (?&?) x1 (?&?).
exist (x1 @ x @ (x0 @ x)).
elim(IHr1 M'0); elim(IHr2 N'0); elim(IHr3 P'0); auto_t; intros x (?&?) x0 (?&?) x1 (?&?).
exist (x1 @ x @ (x0 @ x)).
(* 1 *)
inversion rP; clear rP; subst; inv s_red1; invsub; auto_t.
elim(IHr N'0); auto; intros x (?&?); exist x.
Qed.
Hint Resolve diamond_s_red1: TreeHintDb.
Lemma diamond_s_red1_s_red : diamond s_red1 s_red.
Proof. eapply 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.
(* 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_s_red: confluence SK s_red.
Proof. red; intros; eapply diamond_s_red; auto_t. Qed.
Theorem confluence_sk_red: confluence SK sk_red.
Proof.
red; intros.
match goal with H: sk_red ?x ?y , H1 : sk_red ?x ?z |- _ =>
assert(py: s_red x y) by (apply sk_red_to_s_red; auto_t);
assert(pz: s_red x z) by (apply sk_red_to_s_red; auto_t);
elim(diamond_s_red x y py z); auto_t end.
intros x0 (?&?); exists x0; split; auto_t; apply s_red_to_sk_red; auto_t.
Qed.
Hint Resolve confluence_sk_red: TreeHintDb.
(* programs *)
Inductive program : SK -> Prop :=
| pr_S0 : program Sop
| pr_S1 : forall M, program M -> program (Sop @ M)
| pr_S2 : forall M1 M2, program M1 -> program M2 -> program (Sop @ M1 @ M2)
| pr_K0 : program Kop
| pr_K1 : forall M, program M -> program (Kop @ M) .
Hint Constructors program: TreeHintDb.
Ltac program_tac :=
cbv; repeat (apply pr_S0 || apply pr_S1 || apply pr_S2 || apply pr_K0 || apply pr_K1); auto_t.
(* normal forms *)
Inductive active : SK -> Prop :=
| active_ref : forall i, active (Ref i)
| active_app : forall M N, active M -> active (M@ N)
.
Inductive normal : SK -> Prop :=
| nf_ref : forall i, normal (Ref i)
| nf_S : normal Sop
| nf_K : normal Kop
| nf_app : forall M N, active M -> normal M -> normal N -> normal (M@ N)
| nf_S1 : forall M, normal M -> normal (Sop @ M)
| nf_K1 : forall M, normal M -> normal (Kop @ M)
| nf_S2 : forall M N, normal M -> normal N -> normal (Sop @ M @ N)
.
Hint Constructors active normal : TreeHintDb.
Lemma active_sk_red1 :
forall M, active M -> forall N, sk_red1 M N -> active N.
Proof. intros M a; induction a; intros; inv sk_red1; inv1 active. Qed.
Lemma active_sk_red2:
forall M N P, active M -> sk_red1 (M@ N) P ->
(exists M1, P = M1 @ N /\ sk_red1 M M1)
\/ (exists N1, P = M@ N1 /\ sk_red1 N N1).
Proof. induction M; intros; inv sk_red1; auto_t; inv1 active. Qed.
Lemma active_sk_red:
forall M N P, active M -> sk_red (M@ N) P ->
(exists M1 N1, P = M1 @ N1 /\ sk_red M M1 /\ sk_red N N1).
Proof.
cut(forall red M P, multi_step red M P -> red = sk_red1 ->
forall M1 M2, M = M1 @ M2 -> active M1 ->
(exists P1 P2, P = P1 @ P2 /\ sk_red M1 P1 /\ sk_red M2 P2));
[ intro c; intros; eapply2 c
|
intros red M P r; induction r; intros; subst; auto_t; inv sk_red1 ;
[ inv1 active
| inv1 active
| eelim IHr; [ | auto | auto_t| eapply active_sk_red1; auto_t];
intros x ex; elim ex; intros P2 (?&?&?); subst;
repeat eexists; auto_t; eapply succ_red; auto_t
| eelim IHr; [ | auto | auto_t |]; auto; intros x ex; elim ex; intros x0 (?&?&?); subst;
exists x; exist x0 ; eapply2 succ_red
]].
Qed.
Lemma normal_is_irreducible: forall M, normal M -> forall N, sk_red1 M N -> False.
Proof.
intros M n; induction n; intros; inv sk_red1;
((apply IHn1; fail) || (apply IHn2; fail) || (apply IHn; fail) || inv1 active).
Qed.
Lemma active_is_stable:
forall M N P, active M -> s_red1 (M@ N) P ->
(exists M1 N1, P = M1 @ N1 /\ s_red1 M M1 /\ s_red1 N N1).
Proof.
induction M; intros N P a r; inv s_red1; auto_t; inversion a; subst; eauto 10 with *; inv1 active.
Qed.
Lemma normal_is_stable: forall M, normal M -> forall N, s_red1 M N -> N = M.
Proof. intros M n; induction n; intros; inv s_red1; repeat f_equal; auto; inv1 active. Qed.
Lemma normal_is_stable2: forall M N, s_red M N -> normal M -> N = M.
Proof.
cut(forall red M N, multi_step red M N -> red = s_red1 -> normal M -> N = M);
[ intro c; intros; eapply2 c
| intros red M N r; induction r; intros; subst; auto_t;
assert(N = M) by (eapply normal_is_stable; eauto); subst; auto
].
Qed.
Lemma triangle_s_red : forall M N P, s_red M N -> s_red M P -> normal P -> s_red N P.
Proof.
intros; assert(d: exists Q, s_red N Q /\ s_red P Q) by (eapply diamond_s_red; auto_t);
elim d; intros x (?&?);
assert(x = P) by (eapply normal_is_stable2; eauto); subst; auto.
Qed.
Lemma programs_are_normal: forall M, program M -> normal M.
Proof. intros M pr; induction pr; intros; auto_t. Qed.
Definition normalisable M := exists N, sk_red M N /\ program N.
Ltac normal_tac :=
cbv;
repeat (apply nf_S2 || apply nf_K1 || apply nf_S1 || apply nf_app ||
apply nf_K || apply nf_S || apply nf_ref);
apply programs_are_normal;
auto_t.
Ltac stable_tac :=
match goal with
| H : s_red ?Q ?R |- _ =>
assert(R=Q) by (apply normal_is_stable2; auto_t; cbv; normal_tac); clear H
| H : s_red1 ?Q ?R |- _ =>
assert(R=Q) by (apply normal_is_stable; auto_t; cbv; normal_tac); clear H
| H : sk_red ?Q ?R |- _ => assert(s_red Q R) by (apply sk_red_to_s_red; auto_t); clear H; stable_tac
| _ => auto_t
end; subst; try discriminate.
(* 8.3 Combinators in SK-Calculus *)
Fixpoint bracket x M :=
match M with
| Ref y => if eqb x y then Iop else (Kop @ (Ref y))
| Sop => Kop @ Sop
| Kop => Kop @ Kop
| M1 @ M2 => Sop @ (bracket x M1) @ (bracket x M2)
end
.
(* star abstraction *)
Fixpoint occurs x M :=
match M with
| Ref y => eqb x y
| Sop => false
| Kop => false
| M1 @ M2 => (occurs x M1) || (occurs x M2)
end.
Fixpoint star x M :=
match M with
| Ref y => if eqb x y then Iop else Kop @ (Ref y)
| Sop => Kop @ Sop
| Kop => Kop @ Kop
| M1 @ (Ref y) =>
if eqb x y
then if negb (occurs x M1)
then M1
else Sop @ (star x M1) @ Iop
else if negb (occurs x M1)
then Kop @ (M1 @ (Ref y))
else Sop @ (star x M1) @ (Kop @ (Ref y))
| M1 @ M2 => if occurs x (M1 @ M2)
then Sop @ (star x M1) @ (star x M2)
else Kop @ (M1 @ M2)
end.
Lemma star_id: forall x, star x (Ref x) = Iop.
Proof. intro; unfold star, occurs; rewrite eqb_refl; auto. Qed.
Lemma eta_red: forall M x, occurs x M = false -> star x (M@ (Ref x)) = M.
Proof. intros M x occ; unfold star at 1; fold star; rewrite eqb_refl; rewrite occ; auto. Qed.
Lemma star_occurs_false: forall M x, occurs x M = false -> star x M = Kop @ M.
Proof.
induction M; simpl in *; auto_t; intros x occ; rewrite occ; auto.
rewrite orb_false_iff in occ; elim occ. intros occ1 occ2; rewrite ! occ1.
caseEq M2; simpl; intros; subst; simpl in *; intros; auto_t.
rewrite occ2; auto.
Qed.
Lemma star_occurs_true:
forall M1 M2 x, occurs x (M1 @ M2) = true -> M2 <> Ref x ->
star x (M1 @ M2) = Sop @ (star x M1) @ (star x M2).
Proof.
intros M1 M2 x occ ne; unfold star at 1; fold star.
caseEq M2;
[ intros s e; subst; assert(neq: x=? s = false) by (apply eqb_neq; congruence);
simpl in *; rewrite neq in *; rewrite orb_false_r in *; rewrite occ; auto
| | |]; intros; subst; rewrite ? occ; auto.
Qed.
Ltac star_tac x :=
repeat ( (rewrite (star_occurs_true _ _ x); [| unfold occurs; fold occurs; rewrite ? orb_true_r; simpl; auto; fail| cbv; discriminate]) ||
(rewrite eta_red; [| cbv; auto; fail]) ||
rewrite star_id ||
(rewrite (star_occurs_false _ x); [| unfold occurs; fold occurs; auto; cbv; auto; fail])
).
Notation "\" := star : sk_scope.
(* reprise of Extensional_Programs *)
(* Fixpoints *)
Definition omega := Sop @ (Kop @ (Sop @ Iop)) @ (Sop @ Iop @ Iop).
(*
\(\(App (Ref 0) (App (App (Ref 1) (Ref 1)) (Ref 0)))).
*)
Definition Yop := App omega omega.
Lemma y_red: forall f, sk_red (Yop @ f) (f @ (Yop @ f)).
Proof.
(* delicate proof since rhs is not normal *)
intros; unfold Yop at 1; unfold omega at 1.
aptac. eapply2 succ_red. zerotac.
aptac;
[ aptac;
[ eapply2 succ_red
| instantiate(1:= Yop); eapply2 succ_red; aptac; eval_tac
| zerotac]
| zerotac
| eapply2 succ_red; aptac; [ eval_tac | zerotac | aptac; [ succtac | |]; zerotac]
].
Qed.
(* Waiting *)
Definition wait M N := Sop @ (Sop @ (Kop @ M) @ (Kop @ N)) @ Iop.
Definition Wop := \"x" (\"y" (wait (Ref "x") (Ref "y"))).
Lemma wait_red: forall M N P, sk_red (wait M N @ P) (M@ N @ P).
Proof. intros; cbv; repeat (eapply2 succ_red). Qed.
Lemma w_red1 : forall M N, sk_red (Wop @ M @ N) (wait M N).
Proof. eval_tac. Qed.
Lemma w_red : forall M N P, sk_red (Wop @ M@ N @ P) (M@ N @ P).
Proof. eval_tac. Qed.
(* Fixpoint Functions *)
Definition self_apply := Sop @ Iop @ Iop.
Definition self_apply2 f := Sop @ self_apply @ (Kop @ f).
(* compare to \w. wwf *)
Definition Y2_aux w f := wait (self_apply2 f) w.
(* compare to [x] self_apply2 f w x *)
Definition omega2 := \"w" (\"f" (Ref "f" @ (Y2_aux (Ref "w") (Ref "f")))).
Definition Y2 f := Y2_aux omega2 f.
Lemma self_apply2_red : forall f w, sk_red (self_apply2 f @ w) (w @ w @ f).
Proof. eval_tac. Qed.
Lemma Y2_aux_red: forall w f x, sk_red(Y2_aux w f @ x) (self_apply2 f @ w @ x).
Proof. intros; unfold Y2_aux; apply wait_red. Qed.
Lemma Y2_program: forall f, program f -> program (Y2 f).
Proof. intros; program_tac; auto. Qed.
Theorem fixpoint_function : forall f x, sk_red(Y2 f @ x) (f@ (Y2 f) @ x).
Proof. eval_tac. Qed.
Definition Y2_red := fixpoint_function.
(* Y3 *)
Definition Y3_aux_primer :=
\"x" (bracket "y" ((Ref "sf") @ (Ref "w") @ (Ref "x") @ (Ref "y"))).
Definition Y3_aux w f :=
Sop @ (Sop @ (Kop @ Sop) @ (Sop @ (Kop @ (Sop @ (Sop @ (Kop @ self_apply2 f) @ (Kop @ w)))) @ Kop)) @
(Kop @ (Sop @ Kop @ Kop)).
Definition omega3 := \"w" (\"f" (Ref "f" @ (Y3_aux (Ref "w") (Ref "f")))).
Definition Y3 f := Y3_aux omega3 f.
Lemma Y3_aux_red: forall w f x y, combination w -> sk_red (Y3_aux w f @ x @ y) (self_apply2 f @ w @ x @ y).
Proof. intros; unfold Y3_aux, Y3_aux_primer, bracket. succtac; zerotac. Qed.
Lemma Y3_program: forall f, program f -> program (Y3 f).
Proof. intros; program_tac. Qed.
Theorem Y3_red : forall f x y, sk_red (Y3 f @ x @ y) (f@ (Y3 f) @ x @ y).
Proof. eval_tac. Qed.
(* Y4 *)
Definition Y4_aux_primer :=
\"x" (\"y" (bracket "z" ((Ref "sf") @ (Ref "w") @ (Ref "x") @ (Ref "y") @ (Ref "z")))).
Definition Y4_aux w f := Sop @
(Sop @ (Kop @ Sop) @
(Sop @ (Kop @ (Sop @ (Kop @ Sop))) @
(Sop @
(Sop @ (Kop @ Sop) @
(Sop @ (Kop @ Kop) @
(Sop @ (Kop @ Sop) @ (Sop @ (Kop @ (Sop @ (Sop @ (Kop @ self_apply2 f) @ (Kop @ w)))) @ Kop)))) @
(Kop @ Kop)))) @ (Kop @ (Kop @ (Sop @ Kop @ Kop))).
Definition omega4 := \"w" (\"f" (Ref "f" @ (Y4_aux (Ref "w") (Ref "f")))).
Definition Y4 f := Y4_aux omega4 f.
Lemma Y4_aux_red:
forall w f x y z, combination w -> sk_red(Y4_aux w f @ x @ y @ z) (self_apply2 f @ w @ x @ y @ z).
Proof.
intros; unfold Y4_aux, Y4_aux_primer, bracket.
do 16 (eapply2 succ_red).
aptac;
[ aptac;
[ aptac;
[ aptac;
[ aptac;
[ eval_tac
| eapply2 succ_red
| aptac;
[ zerotac
| aptac; [ eval_tac
| eapply2 succ_red
| aptac;
[ zerotac
| aptac; [ eval_tac | eapply2 succ_red | eapply2 succ_red]
| ]
]
|]
]
| |]
| |]
| |]
| |];
zerotac;
aptac;
[ aptac; [ eapply2 succ_red | zerotac | eapply2 succ_red]
| zerotac
| succtac; zerotac
].
Qed.
Lemma Y4_program: forall f, program f -> program (Y4 f).
Proof. intros; program_tac. Qed.
Theorem Y4_red : forall f x y z, sk_red (Y4 f @ x @ y @ z) (f@ (Y4 f) @ x @ y @ z).
Proof.
eval_tac.
apply preserves_app_sk_red. apply preserves_app_sk_red. apply preserves_app_sk_red.
apply preserves_app_sk_red. all: eval_tac.
Qed.
(* Basic Arithmetic *)
(* Church Numerals *)
Definition church_zero := Kop @ Iop .
Definition church_successor := \"n" (\"f" (\"x" ((Ref "f") @ ((Ref "n") @ (Ref "f") @ (Ref "x"))))).
Lemma church_successor_red :
forall n f x, sk_red (church_successor @ n @ f @ x) (f@ (n @ f @ x)).
Proof. eval_tac. Qed.
Definition church_plus m := App m church_successor.
Lemma church_plus_zero: forall n, sk_red (church_plus church_zero @ n) n.
Proof. eval_tac. Qed.
Lemma church_plus_successor:
forall m n, sk_red (church_plus (church_successor @ m) @ n)
(church_successor @ (church_plus m @ n)).
Proof. eval_tac. Qed.
Definition church_times m n := m@ (church_plus n) @ church_zero.
Lemma church_times_zero: forall n, sk_red (church_times church_zero n) church_zero.
Proof. eval_tac. Qed.
Lemma church_times_successor:
forall m n, sk_red (church_times (church_successor @ m) n)
((church_plus n) @ (church_times m n)).
Proof. eval_tac. Qed.
(*
The combinator corresponding to Kleene's predecessor for the Church numerals in lambda-calculus
does not have the desired properties because star abstraction breaks redexes.
*)
Definition church_predecessor :=
\"n" (\"f" (\"x"
((Ref "n")
@ (\"g" (\"h" (App (Ref "h") (App (Ref "g") (Ref "f")))))
@ (\"u" (Ref "x"))
@ (\"u" (Ref "u"))
))).
(* Scott numerals *)
Definition scott_zero := Kop .
Definition scott_successor := Sop @ (Kop @ Kop) @ (Sop @ (Kop @ (Sop @ Iop)) @ Kop).
Lemma scott_successor_red :
forall n f x, sk_red (scott_successor @ n @ x @ f) (f@ n).
Proof. eval_tac. Qed.
Definition scott_plus :=
Y3 (\"p" (\"m" (\"n"
((Ref "n") @ (Ref "m") @
((Ref "p") @ (scott_successor @ (Ref "m"))))))).
Lemma scott_plus_zero: forall m, sk_red (scott_plus @ m @ scott_zero) m.
Proof. eval_tac. Qed.
Lemma scott_plus_successor:
forall m n, sk_red (scott_plus @ m @ (scott_successor @ n))
(scott_plus @ (scott_successor @ m) @ n).
Proof.
intros; auto_t. unfold scott_plus at 1. eapply transitive_red. apply Y3_red. fold scott_plus.
star_tac "n"; star_tac "m"; star_tac "p"; trtac. unfold scott_successor at 1; trtac.
Qed.
Definition scott_times :=
Y3 (\"p" (\"m" (\"n"
((Ref "n")
@ scott_zero
@ (\"n1" (scott_plus
@ (Ref "m")
@ ((Ref "p") @ (Ref "m") @ (Ref "n1")))))))).
Lemma scott_times_zero: forall m, sk_red (scott_times @ m @ scott_zero) scott_zero.
Proof. eval_tac. Qed.
Lemma scott_times_successor:
forall m n, sk_red (scott_times @ m @ (scott_successor @ n))
(scott_plus @ m @ (scott_times @ m @ n)).
Proof.
intros; auto_t. unfold scott_times at 1. eapply transitive_red. apply Y3_red. fold scott_times.
star_tac "n1"; star_tac "n"; star_tac "m"; star_tac "p".
unfold scott_successor; trtac.
Qed.
Definition scott_predecessor := Sop @ (Sop @ Iop @ (Kop @ Kop)) @ (Kop @ Iop).
Lemma scott_predecessor_zero: sk_red (App scott_predecessor scott_zero) scott_zero.
Proof. eval_tac. Qed.
Lemma scott_predecessor_successor: forall n, sk_red (scott_predecessor @ (scott_successor @ n)) n.
Proof. eval_tac. Qed.
Definition scott_minus :=
Y3 (\"p" (\"m" (\"n"
((Ref "n") @ (Ref "m") @
((Ref "p") @ (scott_predecessor @ (Ref "m"))))))).
Lemma scott_minus_zero: forall m, sk_red (scott_minus @ m @ scott_zero) m.
Proof. eval_tac. Qed.
Lemma scott_minus_successor:
forall m n, sk_red (scott_minus @ (scott_successor @ m) @ (scott_successor @ n))
(scott_minus @ m @ n).
Proof.
intros; auto_t. unfold scott_minus at 1. eapply transitive_red. apply Y3_red. fold scott_minus.
star_tac "n"; star_tac "m"; star_tac "p"; trtac.
unfold scott_predecessor at 1; unfold scott_successor at 1. trtac.
repeat apply preserves_app_sk_red; eval_tac.
Qed.
(* 8.4 Incompleteness of Combinatory Logic *)
(*
Combinatory logic cannot distinguish SKS and SKK.
Hence equality is not definable, so SK-calculus is not complete.
Hence it does not have any tagged identities,
so there is no meaningful translation from tree calculus to SK-calculus
The proofs introduce a new relation id_red that is paramterised by an identity function id.
The main technical result is a pentagon.
id
M ------------> N
| |
p | | p
P - - -> Q - -> R
p id
Here are the details.
*)
(* compounds *)
Inductive compound : SK -> Prop :=
| S1 : forall M, compound (Sop @ M)
| S2 : forall M N, compound (Sop @ M @ N)
| K1 : forall M, compound (Kop @ M)
.
Hint Constructors compound: TreeHintDb.
Definition left_component M :=
match M with
| Ref _ => Iop
| Sop => Iop
| Kop => Iop
| M1 @ _ => M1
end.
Definition right_component M :=
match M with
| Ref _ => M
| Sop => M
| Kop => M
| _ @ M2 => M2
end.
Lemma preserves_compounds_s_red: forall M N, s_red M N -> compound M -> compound N.
Proof.
cut(forall red M N, multi_step red M N -> red = s_red1 -> compound M -> compound N);
[intro c; intros; eapply2 c
| intros red M N r; induction r as [ | ? ? ? ? r1 r2]; split_all; subst; eapply2 IHr2;
inversion r1; subst; inv1 compound; try discriminate; inv s_red1; discriminate].
Qed.
Lemma preserves_components_s_red: