-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlotkinCBV.v
1566 lines (1424 loc) · 42.1 KB
/
PlotkinCBV.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 (c) 2019--2025 - Paulo Torrens <paulotorrens AT gnu DOT org> *)
(******************************************************************************)
Require Import Lia.
Require Import Arith.
Require Import Equality.
Require Import Local.Prelude.
Require Import Local.AbstractRewriting.
Require Import Local.Substitution.
Require Import Local.Syntax.
Require Import Local.Context.
(* TODO: remove this one. *)
Require Import Local.Equational.
Require Import Local.Reduction.
Require Import Local.Metatheory.
Require Import Local.Observational.
Require Import Local.Conservation.
(* TODO: will we need this one...? *)
Require Import Local.Structural.
Require Import Local.Shrinking.
Require Import Local.TypeSystem.
Require Import Local.Normalization.
(* Require Export Local.Lambda.Calculus. *)
Import ListNotations.
(* -------------------------------------------------------------------------- *)
(* S K E T C H ! TODO: if correct, move to somewhere else, please. I'm tired. *)
(* -------------------------------------------------------------------------- *)
Inductive essential: relation pseudoterm :=
| essential_headjmp:
forall h xs ts c,
static h ->
length xs = length ts ->
essential (bind (h (jump #h xs)) ts c)
(bind (h (apply_parameters xs 0 (lift (S #h) (length ts) c))) ts c)
| essential_link:
forall h xs ts c1 c2,
static h ->
length xs = length ts ->
essential c1 c2 ->
essential (bind (h (jump #h xs)) ts c1) (bind (h (jump #h xs)) ts c2)
| essential_bind_left:
LEFT essential.
Lemma essential_head:
inclusion head essential.
Proof.
induction 1.
induction H0; simpl.
- now constructor.
- now apply essential_bind_left.
Qed.
Inductive essential_subterm: relation pseudoterm :=
| essential_subterm_head:
forall h xs ts b,
static h ->
length xs = length ts ->
essential_subterm b (bind (h (jump #h xs)) ts b)
| essential_subterm_copy:
forall h b c ts xs k y,
static h ->
length xs = length ts ->
head b y ->
c = apply_parameters xs k (lift (S #h) (length ts) y) ->
essential_subterm b (bind (h c) ts b).
Lemma head_lift:
forall b c,
head b c ->
forall i k,
head (lift i k b) (lift i k c).
Proof.
admit.
Admitted.
Lemma head_subst:
forall b c,
head b c ->
forall y k,
head (subst y k b) (subst y k c).
Proof.
admit.
Admitted.
Lemma head_apply_parameters:
forall b c,
head b c ->
forall xs k,
head (apply_parameters xs k b) (apply_parameters xs k c).
Proof.
induction xs; simpl; intros.
- now repeat rewrite apply_parameters_nil.
- repeat rewrite apply_parameters_cons.
apply head_subst.
apply IHxs.
Qed.
Lemma head_static_context:
forall h,
static h ->
forall b c,
head b c ->
head (h b) (h c).
Proof.
induction 1; simpl; intros.
- assumption.
- apply head_bind_left.
now apply IHstatic.
Qed.
Lemma sn_head_essential_subterm:
forall b c,
essential_subterm b c ->
SN head c ->
SN head b.
Proof.
intros.
(* Since we gotta move an essential subterm b into the head of c, which might
do some substitution and unlock some extra steps, we split this into two
steps. Notice that due to the nature of substitution, head steps that will
happen in b have to happen at c as well, all of them, so b has a subset of
head redexes from c, which we can always do by a bisimulation. *)
induction H0 using SN_ind.
constructor; intros.
fold (SN head); unfold transp in H1.
destruct H; subst.
- (* Step 1: get a smaller reduction length. *)
eapply H2.
+ eapply t_trans.
* apply t_step.
apply head_longjmp with (r := context_hole) (h := h); auto with cps.
* simpl.
apply t_step.
apply head_bind_left.
apply head_static_context; auto.
apply head_apply_parameters.
apply head_lift.
eassumption.
+ econstructor; eauto.
+ assumption.
- (* Clearly, as head reduction is deterministic although I didn't prove this
annoying lemma yet... *)
assert (y0 = y) by admit; subst.
(* Step 2: keep a bisimulation to show that we'll match a prefix of the now
new head. *)
clear H2.
assert (SN head (apply_parameters xs k (lift (S #h) (length ts) y))).
+ apply SN_preimage with (fun c => bind (h c) ts b); intros.
* apply head_bind_left.
now apply head_static_context.
* assumption.
+ apply SN_preimage with (fun c => apply_parameters xs k
(lift (S #h) (length ts) c)); intros.
* apply head_apply_parameters.
now apply head_lift.
* assumption.
Admitted.
Lemma sn_essential_sn_head:
forall b,
SN head b ->
SN essential b.
Proof.
intros.
induction H using SN_ind.
constructor; intros.
fold (SN essential); unfold transp in H0.
admit.
Admitted.
Require Export Local.Lambda.Calculus.
(* -------------------------------------------------------------------------- *)
(* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- *)
(* -------------------------------------------------------------------------- *)
Include Lambda.Calculus.
Module CPS := Local.Syntax.
Inductive cbv: relation term :=
| cbv_betav:
forall t b x,
value x ->
cbv
(application (abstraction t b) x)
(subst x 0 b)
| cbv_app1:
forall f1 f2 x,
cbv f1 f2 ->
cbv (application f1 x) (application f2 x)
| cbv_app2:
forall f x1 x2,
value f ->
cbv x1 x2 ->
cbv (application f x1) (application f x2).
Local Hint Constructors cbv: cps.
Lemma full_cbv:
inclusion cbv full.
Proof.
induction 1.
- constructor.
- constructor; auto.
- constructor; auto.
Qed.
Lemma cbv_implies_nonvalue:
forall a b,
cbv a b -> ~value a.
Proof.
induction 1; inversion 1.
Qed.
Lemma cbv_is_a_function:
forall a b1,
cbv a b1 ->
forall b2,
cbv a b2 -> b1 = b2.
Proof.
induction 1; intros.
- dependent destruction H0.
+ reflexivity.
+ exfalso.
inversion H0.
+ exfalso.
apply cbv_implies_nonvalue with x x2; auto.
- dependent destruction H0.
+ exfalso.
apply cbv_implies_nonvalue in H.
auto with cps.
+ f_equal; auto.
+ exfalso.
apply cbv_implies_nonvalue in H.
auto with cps.
- dependent destruction H1.
+ exfalso.
apply cbv_implies_nonvalue in H0.
auto with cps.
+ exfalso.
apply cbv_implies_nonvalue in H1.
auto with cps.
+ f_equal; auto.
Qed.
Lemma cbv_is_decidable:
forall e,
{ normal cbv e } + { exists f, cbv e f }.
Proof.
induction e; simpl.
- left.
inversion 1.
- left.
inversion 1.
- destruct e1.
+ clear IHe1.
destruct IHe2.
* left.
(* TODO: damn OCD... *)
inversion_clear 1; [ easy | firstorder ].
* right.
destruct e as (x, ?).
exists (application n x).
constructor 3; auto.
constructor.
+ destruct value_dec with e2.
* right; eexists.
now constructor.
* destruct IHe2.
(* TODO: refactor me, please? *)
left.
inversion_clear 1.
contradiction.
inversion H0.
firstorder.
right.
destruct e as (x, ?).
eexists.
constructor 3.
constructor.
eassumption.
+ destruct IHe1.
* left; intros x ?.
dependent destruction H.
(* TODO: refactor... *)
firstorder.
inversion H.
* right.
destruct e as (x, ?).
eexists; eauto with cps.
(* TODO: once we add reduction under pairs and thunks, review below. As of
now, this isn't even true. *)
+ admit.
+ admit.
+ admit.
+ admit.
+ admit.
- admit.
- admit.
- admit.
- admit.
- admit.
Admitted.
Lemma closed_normal_cbv_implies_value:
forall e,
closed e ->
normal cbv e ->
value e.
Proof.
intros.
destruct value_dec with e as [ ?H | ?H ].
- assumption.
- exfalso.
induction e.
+ apply H1.
constructor.
+ apply H1.
constructor.
+ clear H1.
destruct e1.
* specialize (H n).
dependent destruction H.
dependent destruction H.
contradiction.
* eapply H0.
constructor.
destruct value_dec with e2 as [ ?H | ?H ]; auto.
exfalso.
apply IHe2.
(* TODO: refactor me, please... *)
intros n.
specialize (H n).
now dependent destruction H.
intros x ?.
eapply H0.
constructor 3; eauto.
constructor.
assumption.
* apply IHe1.
(* TODO: refactor... *)
intro.
specialize (H n).
dependent destruction H.
assumption.
intros x ?.
eapply H0.
constructor.
eassumption.
inversion 1.
* (* TODO: not true yet, as we can't reduce inside a pair. *)
admit.
* admit.
* admit.
* admit.
* admit.
+ admit.
+ admit.
+ admit.
+ admit.
+ admit.
Admitted.
(* TODO: fix typing on the following! *)
Local Notation VAR n :=
(* [x] = k<x> *)
(jump 0 [CPS.bound (S n)]).
Local Notation ABS b t1 t2 :=
(* [\x.e] = k<f> { f<x, k> = [e] } *)
(bind (jump 1 [CPS.bound 0]) [t1; t2] b).
Local Notation APP b c t1 t2 :=
(* [e f] = [e] { k<f> = [f] { k<v> = f<v, k> } } *)
(bind b [t1] (bind c [t2] (jump 1 [CPS.bound 2; CPS.bound 0]))).
(* TODO: these lifts should be moved from source to target! *)
Inductive cbv_cps: term -> pseudoterm -> Prop :=
| cbv_cps_bound:
forall n: nat,
cbv_cps n (VAR n)
| cbv_cps_abstraction:
forall t e b,
cbv_cps (lift 1 1 e) b ->
cbv_cps (abstraction t e) (ABS b void void)
| cbv_cps_application:
forall f x b c,
cbv_cps (lift 1 0 f) b ->
cbv_cps (lift 2 0 x) c ->
cbv_cps (application f x) (APP b c void void).
Local Hint Constructors cbv_cps: cps.
Lemma cbv_cps_is_a_function:
forall e c1,
cbv_cps e c1 ->
forall c2,
cbv_cps e c2 -> c1 = c2.
Proof.
induction 1; intros.
- dependent destruction H; auto.
- dependent destruction H0.
f_equal; auto.
- dependent destruction H1.
f_equal; auto.
f_equal; auto.
Qed.
Local Hint Resolve cbv_cps_is_a_function: cps.
Lemma cbv_cps_lift:
forall e c,
cbv_cps e c ->
forall i k,
cbv_cps (lift i k e) (lift i (S k) c).
Proof.
induction 1; intros.
- destruct (le_gt_dec k n).
+ sigma; constructor.
+ sigma; constructor.
- rewrite lift_distributes_over_bind.
rewrite lift_distributes_over_jump; simpl.
rewrite lift_bound_lt; try lia.
rewrite lift_bound_lt; try lia.
replace (lift i k (abstraction t e)) with
(abstraction t (lift i (S k) e)) by now sigma.
constructor.
rewrite lift_lift_permutation; try lia.
replace (k + 2) with (2 + k); simpl; try lia.
apply IHcbv_cps; lia.
- rewrite lift_distributes_over_bind.
rewrite lift_distributes_over_bind.
rewrite lift_distributes_over_jump; simpl.
rewrite lift_bound_lt; try lia.
rewrite lift_bound_lt; try lia.
rewrite lift_bound_lt; try lia.
replace (lift i k (application f x)) with
(application (lift i k f) (lift i k x)) by now sigma.
constructor.
+ rewrite lift_lift_permutation; try lia.
apply IHcbv_cps1; lia.
+ rewrite lift_lift_permutation; try lia.
replace (k + 1) with (1 + k); try lia.
apply IHcbv_cps2; lia.
Qed.
Local Hint Resolve cbv_cps_lift: cps.
Lemma cbv_cps_is_total:
forall e,
exists c,
cbv_cps e c.
Proof.
induction e.
- eauto with cps.
- destruct IHe as (c, ?).
eauto with cps.
- destruct IHe1 as (b, ?).
destruct IHe2 as (c, ?).
eauto with cps.
(* TODO: not yet true, we didn't define the CPS translations for pairs and
thunks. *)
- admit.
- admit.
- admit.
- admit.
- admit.
Admitted.
Local Hint Resolve cbv_cps_is_total: cps.
Lemma cbv_cps_lift_inversion:
forall i k e b,
cbv_cps (lift i k e) b ->
exists2 c,
cbv_cps e c & b = lift i (S k) c.
Proof.
intros.
assert (exists c, cbv_cps e c) as (c, ?).
- eauto with cps.
- eauto with cps.
Qed.
Local Hint Resolve cbv_cps_lift_inversion: cps.
Lemma cbv_cps_not_free:
forall e c,
cbv_cps e c ->
forall n,
not_free n e <-> CPS.not_free (S n) c.
Proof.
induction 1; split; intros.
- dependent destruction H.
rename n0 into m.
constructor.
+ constructor; lia.
+ do 2 constructor; lia.
- dependent destruction H.
dependent destruction H0.
dependent destruction H0.
rename n0 into m.
constructor; lia.
- constructor; simpl.
+ constructor.
* constructor; lia.
* do 2 constructor; lia.
+ repeat constructor.
+ dependent destruction H0.
apply IHcbv_cps; try lia.
replace (S n) with (n + 1) in H0; try lia.
apply not_free_lift with (k := 1) in H0.
replace (n + 1 + 1) with (2 + n) in H0; try lia.
assumption.
- constructor.
dependent destruction H0.
simpl in H0_0.
apply IHcbv_cps in H0_0; auto.
replace (S (S n)) with (n + 1 + 1) in H0_0; try lia.
apply not_free_lift in H0_0.
replace (n + 1) with (1 + n) in H0_0; try lia.
assumption.
- dependent destruction H1.
constructor; simpl.
+ apply IHcbv_cps1; auto.
replace (S n) with (n + 1 + 0); try lia.
apply not_free_lift.
rewrite Nat.add_comm.
assumption.
+ repeat constructor.
+ repeat (simpl; try constructor; try lia).
simpl; eapply IHcbv_cps2; auto.
replace (S (S n)) with (n + 2 + 0); try lia.
apply not_free_lift.
rewrite Nat.add_comm.
assumption.
- dependent destruction H1.
dependent destruction H1_0.
simpl in H2, H1_0_1, H1_0_2.
constructor.
+ apply IHcbv_cps1 in H1_; auto.
replace (S n) with (n + 1 + 0) in H1_; try lia.
apply not_free_lift in H1_.
rewrite Nat.add_comm in H1_.
assumption.
+ apply IHcbv_cps2 in H1_0_1; auto.
replace (S (S n)) with (n + 2 + 0) in H1_0_1; try lia.
apply not_free_lift in H1_0_1.
rewrite Nat.add_comm in H1_0_1.
assumption.
Qed.
(* -------------------------------------------------------------------------- *)
(*
Let's try to reason about simulation. The proof should follow in a similar way
from the call-by-name one. Recall the call-by-value translation:
1) [x] = k<x>
2) [\x.M] = k<f> { f<x, k> = [M] }
3) [M N](k) = [M] { k<f> = [N] { k<v> = f<v, k> } }
Again, we have a term as [(\x.a) b], which will translate into:
k<f>
{ f<x, k> =
[a] }
{ k<f> =
[b]
{ k<v> =
f<v, k> } }
We immediately have two linear jump redexes (only the first at head position):
[b]
{ k<x> =
[a] }
This is the opposite of the call-by-name! Of course, I should have expected
that. If [a] contains a free occurrence of x and is thus equal to C[x], we
will then have:
[b]
{ k<x> =
D[k<x>] }
This is way more problematic. Does Plotkin prove simulation for the full beta
reduction in here, or just for the call-by-value beta reduction? AAAAAAAAAAA.
It seems this simply isn't true for the full beta... could we think of a
counter example? Anyways, let's consider, thus, that b is a value. We have two
cases then. The first one, where b is a variable:
k<y>
{ k<x> =
D[k<x>] }
This will simplify in one linear head reduction to:
D[k<y>]
Ok, this seems fine. I've replaced one variable by the other. Now, the other
case is when b is an abstraction. We should then have:
k<f>
{ f<y, k> =
[c] }
{ k<x> =
D[k<x>] }
This will simply reduce to:
D[k<f>]
{ f<y, k> =
[c] }
As we'd have the reduction be from [(\x.a) (\y.c)] to [a[\y.c/x]], if for
simplicity we assume there's a single x in there, we'd want to have:
D[k<f> { f<y, k> = [c] }]
This is just floating! However, the problem is that f can appear free multiple
times, so we can't just float this in there. We can duplicate it, of course,
if we are not trying to reduce but rather show that the terms are equal. This
is enough to show adequacy, but we don't have one-step simulation anymore. We
would still have it if we allowed for specialization, just like it's done in
linear logic! But we'd like to have contraction instead for the CPS-calculus.
Other notions of reduction: though the call-by-name translation can't validate
eta (we don't want it to!), the call-by-value translation should validate some
extra notions of reduction. The call-by-value eta reduction can be simulated,
but it does need the (ETA) rule. It seems that the id-reduction from Moggi's
calculus, (\x.x) e, can also be simulated, but it requires floating.
*)
Axiom R: relation pseudoterm.
Conjecture R_bind_left:
LEFT R.
Conjecture R_bind_right:
RIGHT R.
Conjecture R_lift:
forall b c,
R b c ->
forall i k,
R (lift i k b) (lift i k c).
Lemma rt_R_bind_left:
LEFT rt(R).
Proof.
induction 1.
- apply rt_step.
now apply R_bind_left.
- apply rt_refl.
- now apply rt_trans with (bind y ts c).
Qed.
Lemma rt_R_bind_right:
RIGHT rt(R).
Proof.
induction 1.
- apply rt_step.
now apply R_bind_right.
- apply rt_refl.
- now apply rt_trans with (bind b ts y).
Qed.
Lemma rt_R_lift:
forall b c,
rt(R) b c ->
forall i k,
rt(R) (lift i k b) (lift i k c).
Proof.
induction 1; intros.
- apply rt_step.
now apply R_lift.
- apply rt_refl.
- now apply rt_trans with (lift i k y).
Qed.
Lemma cbv_simulates_betav:
forall t e x b c,
value x ->
cbv_cps (application (abstraction t e) x) b ->
cbv_cps (subst x 0 e) c ->
rt(R) b c.
Proof.
(* This should follow the description above. *)
admit.
Admitted.
Lemma cbv_simulates_cbv:
forall e f,
cbv e f ->
forall b c,
cbv_cps e b ->
cbv_cps f c ->
rt(R) b c.
Proof.
induction 1; intros.
- rename b into e, b0 into b.
now apply cbv_simulates_betav with t e x.
- dependent destruction H0.
dependent destruction H1.
assert (c0 = c) by eauto with cps.
clear H1_0; subst.
apply cbv_cps_lift_inversion in H0_ as (b1, ?, ?).
apply cbv_cps_lift_inversion in H1_ as (b2, ?, ?).
specialize (IHcbv b1 b2); subst.
apply rt_R_bind_left.
apply rt_R_lift.
now apply IHcbv.
- dependent destruction H1.
dependent destruction H2.
assert (b0 = b) by eauto with cps.
clear H1_; subst.
apply cbv_cps_lift_inversion in H1_0 as (c1, ?, ?).
apply cbv_cps_lift_inversion in H2_0 as (c2, ?, ?).
specialize (IHcbv c1 c2); subst.
apply rt_R_bind_right.
apply rt_R_bind_left.
apply rt_R_lift.
now apply IHcbv.
Qed.
Lemma cbv_simulation:
forall e f,
compatible cbv e f ->
forall b c,
cbv_cps e b ->
cbv_cps f c ->
rt(R) b c.
Proof.
induction 1; intros.
- now apply cbv_simulates_cbv with e f.
- dependent destruction H0.
dependent destruction H1.
apply cbv_cps_lift_inversion in H0 as (c1, ?, ?).
apply cbv_cps_lift_inversion in H1 as (c2, ?, ?).
specialize (IHcompatible c1 c2); subst.
apply rt_R_bind_right.
apply rt_R_lift.
now apply IHcompatible.
- dependent destruction H0.
dependent destruction H1.
assert (c0 = c) by eauto with cps.
clear H1_0; subst.
apply cbv_cps_lift_inversion in H0_ as (b1, ?, ?).
apply cbv_cps_lift_inversion in H1_ as (b2, ?, ?).
specialize (IHcompatible b1 b2); subst.
apply rt_R_bind_left.
apply rt_R_lift.
now apply IHcompatible.
- dependent destruction H0.
dependent destruction H1.
assert (b0 = b) by eauto with cps.
clear H0_; subst.
apply cbv_cps_lift_inversion in H0_0 as (c1, ?, ?).
apply cbv_cps_lift_inversion in H1_0 as (c2, ?, ?).
specialize (IHcompatible c1 c2); subst.
apply rt_R_bind_right.
apply rt_R_bind_left.
apply rt_R_lift.
now apply IHcompatible.
Qed.
Local Lemma technical1:
forall n m,
weakly_converges
(bind (jump 0 [CPS.bound (2 + n)]) [void]
(bind (jump 0 [CPS.bound (3 + m)]) [void]
(jump 1 [CPS.bound 2; CPS.bound 0])))
(1 + n).
Proof.
intros.
(* Here our term is of the form:
k<x> { k<f> = k<y> { k<v> = f<v, k> } }
This will reduce to:
k<y> { k<v> = x<v, k> } { k<f> = k<y> { k<v> = f<v, k> } }
Then to:
x<y, k> { k<v> = x<v, k> } { k<f> = k<y> { k<v> = f<v, k> } }
Which will then halt at x.
*)
eexists; [ eapply star_trans |].
- (* TODO: we need to automate this... *)
set (c := jump 1 [CPS.bound 2; CPS.bound 0]).
set (b := bind (jump 0 [CPS.bound (3 + m)]) [void] c).
replace (bind (jump 0 [CPS.bound (2 + n)]) [void] b) with
(context_left Context.context_hole [void] b (jump 0 [CPS.bound (2 + n)]))
by auto.
apply star_ctxjmp.
reflexivity.
- simpl.
(* TODO: I think there's an issue with sigma; change this once I fix it! *)
rewrite lift_distributes_over_bind.
rewrite lift_distributes_over_jump.
rewrite apply_parameters_distributes_over_bind.
rewrite apply_parameters_distributes_over_jump.
unfold apply_parameters.
simpl; sigma.
(* Back to the proof... *)
apply star_bind_left.
replace (jump (var 0) [var (S (S (S m)))]) with
(Context.context_hole (jump 0 [CPS.bound (S (S (S m)))])) by auto.
apply star_ctxjmp.
reflexivity.
- simpl.
unfold apply_parameters.
sigma.
repeat constructor.
Qed.
Local Lemma technical2:
forall n b,
weakly_converges
(bind (jump 0 [CPS.bound (S (S n))]) [void]
(bind
(lift 2 1 (bind (jump 1 [CPS.bound 0])
[void; void] (lift 1 2 b)))
[void] (jump 1 [CPS.bound 2; CPS.bound 0]))) (1 + n).
Proof.
intros.
(* Here our term is of the form:
k<x> { k<f> = k<f> { f<x, k> = [e] } { k<v> = f<v, k> } }
This will reduce to:
k<f> { f<x, k> = [e] } { k<v> = x<v, k> } { ... }
Then to:
x<f, k> { f<x, k> = [e] } { k<v> = x<v, k> } { ... }
Which will then halt at x.
*)
eexists; [ eapply star_trans |].
- replace (jump 0 [CPS.bound (S (S n))]) with
(Context.context_hole (jump 0 [CPS.bound (S (S n))])) by auto.
apply star_ctxjmp.
reflexivity.
- simpl.
rewrite apply_parameters_cons.
rewrite apply_parameters_nil.
apply star_bind_left.
rewrite lift_distributes_over_bind; simpl.
rewrite Metatheory.lift_lift_simplification by lia; simpl.
rewrite subst_distributes_over_bind; simpl.
rewrite Metatheory.subst_lift_simplification by lia.
rewrite lift_distributes_over_bind; simpl.
rewrite lift_distributes_over_jump; simpl.
rewrite lift_bound_lt by lia.
rewrite lift_bound_lt by lia.
replace (subst (CPS.bound (S (S n))) 0 (lift 1 1 void))
with void by auto.
replace (lift 2 2 void) with void by auto.
replace (lift 2 1 void) with void by auto.
rewrite Metatheory.lift_lift_simplification by lia; simpl.
rewrite lift_distributes_over_jump; simpl.
rewrite lift_bound_lt by lia.
rewrite lift_bound_ge by lia; simpl.
rewrite lift_bound_lt by lia; simpl.
rewrite subst_distributes_over_jump; simpl.
rewrite subst_bound_eq by lia.
rewrite lift_bound_ge by lia; simpl.
rewrite subst_bound_gt by lia; simpl.
rewrite subst_bound_lt by lia; simpl.
set (c := (jump (S (S (S n))) [CPS.bound 2; CPS.bound 0])).
replace (bind (jump 1 [CPS.bound 0]) [void; void] (lift 3 2 b)) with
(context_left Context.context_hole [void; void] (lift 3 2 b)
(jump 1 [CPS.bound 0])) by auto.
apply star_ctxjmp.
reflexivity.
- simpl.
rewrite apply_parameters_cons.
rewrite apply_parameters_nil.
sigma.
do 4 constructor.
Qed.
Local Lemma technical3:
forall b k n,
weakly_converges b (1 + k) ->
weakly_converges
(bind (jump 0 [CPS.bound (S (S n))]) [void]
(bind (lift 2 1 b) [void]
(jump 1 [CPS.bound 2; CPS.bound 0])))
(1 + k).
Proof.
intros.
(* Here our term is of the form:
k<x> { k<f> = [e] { k<v> = f<v, k> } }
Where we know that [e] halts at some variable free y. This reduces to:
[e] { k<v> = x<v, k> } { ... }
Which we know, by the renaming conventions, will still jump to y.
*)
destruct H as (c, ?, ?).
eexists; [ eapply star_trans |].
- apply star_bind_right.
apply star_bind_left.
apply star_lift.
eassumption.
- replace (jump 0 [CPS.bound (S (S n))]) with
(Context.context_hole (jump 0 [CPS.bound (S (S n))])) by auto.
apply star_ctxjmp.
reflexivity.
- constructor; simpl.
rewrite apply_parameters_cons.
rewrite apply_parameters_nil.
rewrite lift_distributes_over_bind.
rewrite subst_distributes_over_bind.
constructor.
rewrite Metatheory.lift_lift_simplification by lia; simpl.
eapply converges_subst.
+ eapply converges_lift.
* eassumption.
* rewrite lift_bound_ge by lia; simpl.
reflexivity.
+ now rewrite subst_bound_gt by lia.
Qed.
Local Lemma technical4:
forall b c k,
weakly_converges c (1 + k) ->
weakly_converges
(bind (bind (jump 1 [CPS.bound 0]) [void; void] b)
[void]
(bind (lift 2 1 c) [void]
(jump 1 [CPS.bound 2; CPS.bound 0])))
(1 + k).
Proof.
intros.
(* Here our term is of the form:
k<f> { f<x, k> = [e] } { k<f> = [f] { k<v> = f<v, k> } }
And we know that [f] halts to a free variable y. This will reduce to:
[f] { k<v> = f<v, k> } { f<x, k> = [e] } { ... }
Immediately we know that this halts to y due to the renaming conventions.
*)
destruct H as (d, ?, ?).
eexists; [ eapply star_trans |].
- apply star_bind_right.
apply star_bind_left.
apply star_lift.
eassumption.
- replace (bind (jump 1 [CPS.bound 0]) [void; void] b) with
(context_left Context.context_hole [void; void] b (jump 1 [CPS.bound 0]))
by auto.
apply star_ctxjmp.
reflexivity.
- simpl.
rewrite apply_parameters_cons.
rewrite apply_parameters_nil.
do 2 constructor.
rewrite lift_distributes_over_bind.
rewrite Metatheory.lift_lift_simplification by lia; simpl.
rewrite subst_distributes_over_bind; simpl.
rewrite Metatheory.subst_lift_simplification by lia.
constructor.
eapply converges_lift.
+ eassumption.
+ now rewrite lift_bound_ge by lia.
Qed.
Local Lemma technical5:
forall b c k,
weakly_converges b (1 + k) ->
weakly_converges
(bind (lift 1 1 b) [void]
(bind c [void]
(jump 1 [CPS.bound 2; CPS.bound 0])))
(1 + k).
Proof.
intros.
(* Here our term is simply of the form:
[e f](k) = [e] { k<f> = [f] { k<v> = f<v, k> } }
And we know that [e] halts to a free variable y. Thus we know that by the