-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFrameCompatibility.v
More file actions
1515 lines (1473 loc) · 53.5 KB
/
Copy pathFrameCompatibility.v
File metadata and controls
1515 lines (1473 loc) · 53.5 KB
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
From Stdlib Require Import Arith Bool Lia Relations.Relation_Operators.
Require Import AdministrativeReduction.
Require Import AdministrativeAlgebra.
Require Import AdministrativeResiduals.
Require Import SharedPrompts.CoreAlgebra.
Require Import SharedPrompts.ScopedFusion.
Import SharedPrompts.
(* Canonical right-associated spines are derived gadgets for the mode
relation. They are useful when a continuation replacement swallows the
delimiter at the head of a tail: first expose all ordinary LetLet redexes,
then contract the bottom LetS. *)
Fixpoint admin_let_assoc_spine (l : lctx) (q t : expr) : expr :=
match l with
| LNil => ELet q t
| LLet a l' => ELet a (admin_let_assoc_spine l' q (olift_e 1 1 t))
end.
Lemma admin_let_assoc_spine_star (l : lctx) (q t : expr) :
star (admin_step)
(ELet (plug_l l q) t)
(admin_let_assoc_spine l q t).
Proof.
revert q t.
induction l as [|a l IH]; intros q t.
- apply star_refl.
- simpl.
eapply star_step.
+ apply AS_LetAssoc.
+ apply (admin_star_map (admin_step) (admin_step)
(fun z => ELet a z)).
* intros x y Hxy. apply AS_LetBody. exact Hxy.
* apply IH.
Qed.
Fixpoint admin_let_shift_spine (l : lctx) (e t : expr) : expr :=
match l with
| LNil => EShift (krepl_e 0 (frame t) e)
| LLet a l' => ELet a (admin_let_shift_spine l' e (olift_e 1 1 t))
end.
Lemma admin_let_shift_spine_star (l : lctx) (e t : expr) :
star (admin_step)
(ELet (plug_l l (EShift e)) t)
(admin_let_shift_spine l e t).
Proof.
revert e t.
induction l as [|a l IH]; intros e t.
- apply star_one. apply AS_LetShift.
- simpl.
eapply star_step.
+ apply AS_LetAssoc.
+ apply (admin_star_map (admin_step) (admin_step)
(fun z => ELet a z)).
* intros x y Hxy. apply AS_LetBody. exact Hxy.
* apply IH.
Qed.
Fixpoint admin_spine_tail_lift (l : lctx) (q : expr) : expr :=
match l with
| LNil => q
| LLet _ l' => admin_spine_tail_lift l' (olift_e 1 1 q)
end.
Lemma admin_let_assoc_spine_tail_shape (l : lctx) (e q : expr) :
admin_let_assoc_spine l e q =
plug_l l (ELet e (admin_spine_tail_lift l q)).
Proof.
revert e q.
induction l as [|a l IH]; intros e q; simpl.
- reflexivity.
- rewrite (IH e (olift_e 1 1 q)). reflexivity.
Qed.
Lemma admin_let_shift_spine_abort_shape (l : lctx) (e q : expr) :
admin_let_shift_spine l (klift_e 0 1 e) q =
plug_l l (abort e).
Proof.
revert e q.
induction l as [|a l IH]; intros e q.
- simpl.
rewrite (proj2 admin_krepl_no_cocc
(klift_e 0 1 e) 0 (frame q)).
+ reflexivity.
+ apply cocc_klift_fresh_e.
- simpl. rewrite (IH e (olift_e 1 1 q)). reflexivity.
Qed.
Lemma admin_abort_tail_spine (h : nat) (l : lctx) (e q : expr) :
joinable (admin_step)
(ECont h
(ELet (plug_l l
(abort (ECont h (ELet e (admin_spine_tail_lift l q))))) q))
(ECont h (ELet (plug_l l e) q)).
Proof.
exists (ECont h (plug_l l (ELet e (admin_spine_tail_lift l q)))); split.
- eapply star_trans.
+ apply (admin_star_map (admin_step) (admin_step) (ECont h)).
* intros x y Hxy. apply AS_ContBody. exact Hxy.
* apply (admin_let_shift_spine_star l
(klift_e 0 1 (ECont h
(ELet e (admin_spine_tail_lift l q)))) q).
+ rewrite (admin_let_shift_spine_abort_shape l
(ECont h (ELet e (admin_spine_tail_lift l q))) q).
apply star_one. apply AS_ContTail.
apply tail_step_plug_abort.
- eapply (admin_star_map (admin_step) (admin_step) (ECont h)).
+ intros x y Hxy. apply AS_ContBody. exact Hxy.
+ rewrite <- (admin_let_assoc_spine_tail_shape l e q).
apply (admin_let_assoc_spine_star l e q).
Qed.
Lemma admin_krepl_installed_frame_abort_shape (h : nat) (q e : expr) :
krepl_e h (installed_let_frame h q) (abort (ECont h e)) =
abort (plug_d (installed_let_frame h q)
(krepl_e h (installed_let_frame h q) e)).
Proof.
unfold abort. simpl.
replace (h + 1) with (S h) by lia.
rewrite Nat.eqb_refl.
assert (Hframe :
klift_d 0 1 (installed_let_frame h q) =
installed_let_frame (S h) (klift_e 0 1 q)).
{ unfold installed_let_frame, klift_d. simpl.
destruct (Nat.leb 0 h); f_equal; lia. }
repeat f_equal.
assert (Hinst : ICont (S h) (CLet CHole (klift_e 0 1 q)) =
klift_d 0 1 (installed_let_frame h q)).
{ change (installed_let_frame (S h) (klift_e 0 1 q) =
klift_d 0 1 (installed_let_frame h q)).
symmetry. exact Hframe. }
rewrite Hinst.
symmetry. apply admin_klift_krepl_e.
Qed.
Lemma admin_krepl_installed_frame_tail_abort_shape (h : nat) (q : expr)
(l : lctx) (e : expr) :
exists l' ep,
krepl_e h (installed_let_frame h q)
(plug_l l (abort (ECont h e))) =
plug_l l'
(abort (ECont h
(ELet ep (admin_spine_tail_lift l' q)))) /\
krepl_e h (installed_let_frame h q) (plug_l l e) = plug_l l' ep.
Proof.
revert q e.
induction l as [|a l IH]; intros q e.
- exists LNil, (krepl_e h (installed_let_frame h q) e). split.
+ change (krepl_e h (installed_let_frame h q)
(abort (ECont h e)) =
abort (ECont h
(ELet (krepl_e h (installed_let_frame h q) e) q))).
rewrite admin_krepl_installed_frame_abort_shape.
reflexivity.
+ reflexivity.
- destruct (IH (olift_e 1 1 q) e)
as [l' [ep [Ha Hb]]].
exists (LLet (krepl_e h (installed_let_frame h q) a) l'), ep. split.
+ simpl.
change (ELet (krepl_e h (installed_let_frame h q) a)
(krepl_e h (olift_d 0 1 (installed_let_frame h q))
(plug_l l (abort (ECont h e)))) =
ELet (krepl_e h (installed_let_frame h q) a)
(plug_l l'
(abort (ECont h
(ELet ep (admin_spine_tail_lift l'
(olift_e 1 1 q))))))).
rewrite (admin_olift_installed_let_frame 0 1 h q), Ha.
reflexivity.
+ simpl.
change (ELet (krepl_e h (installed_let_frame h q) a)
(krepl_e h (olift_d 0 1 (installed_let_frame h q))
(plug_l l e)) =
ELet (krepl_e h (installed_let_frame h q) a)
(plug_l l' ep)).
rewrite (admin_olift_installed_let_frame 0 1 h q), Hb.
reflexivity.
Qed.
Lemma admin_krepl_frame_abort_hit (h : nat) (q : expr)
(l : lctx) (e : expr) :
joinable (admin_step)
(krepl_e h (installed_let_frame h q)
(ECont h (plug_l l (abort (ECont h e)))))
(krepl_e h (installed_let_frame h q)
(ECont h (plug_l l e))).
Proof.
destruct (admin_krepl_installed_frame_tail_abort_shape h q l e)
as [l' [ep [Hsrc Hdst]]].
simpl. rewrite Nat.eqb_refl, Hsrc, Hdst.
apply admin_abort_tail_spine.
Qed.
Lemma admin_krepl_abort_shape_nonhit (h : nat) (q : expr)
(k : nat) (e : expr) :
h <> k ->
krepl_e h (installed_let_frame h q) (abort (ECont k e)) =
abort (ECont k (krepl_e h (installed_let_frame h q) e)).
Proof.
intro Hhk.
assert (Hkh : Nat.eqb k h = false).
{ apply Nat.eqb_neq. intro Heq. apply Hhk. symmetry. exact Heq. }
assert (Hskh : Nat.eqb (S k) (S h) = false).
{ apply Nat.eqb_neq. intro Heq. apply Hhk. lia. }
assert (H0k : Nat.leb 0 k = true) by (apply Nat.leb_le; lia).
assert (H0h : Nat.leb 0 h = true) by (apply Nat.leb_le; lia).
assert (Hplus : Nat.eqb (k + 1) (S h) = false).
{ apply Nat.eqb_neq. intro Heq. apply Hhk. lia. }
unfold abort. cbn [krepl_e]. cbn [klift_e].
try rewrite H0k.
try rewrite H0h.
try rewrite Hplus.
cbn [krepl_e].
try rewrite H0h.
try rewrite Hplus.
f_equal.
rewrite (admin_klift_krepl_e 0 h (installed_let_frame h q) e).
reflexivity.
Qed.
Lemma admin_krepl_frame_tail_abort_shape_nonhit (h : nat) (q : expr)
(k : nat) (l : lctx) (e : expr) :
h <> k ->
exists l' ep,
krepl_e h (installed_let_frame h q)
(plug_l l (abort (ECont k e))) =
plug_l l' (abort (ECont k ep)) /\
krepl_e h (installed_let_frame h q) (plug_l l e) = plug_l l' ep.
Proof.
intro Hhk.
revert q e.
induction l as [|a l IH]; intros q e.
- exists LNil, (krepl_e h (installed_let_frame h q) e). split.
+ apply admin_krepl_abort_shape_nonhit. exact Hhk.
+ reflexivity.
- destruct (IH (olift_e 1 1 q) e)
as [l' [ep [Ha Hb]]].
exists (LLet (krepl_e h (installed_let_frame h q) a) l'), ep. split.
+ simpl.
change (ELet (krepl_e h (installed_let_frame h q) a)
(krepl_e h (olift_d 0 1 (installed_let_frame h q))
(plug_l l (abort (ECont k e)))) =
ELet (krepl_e h (installed_let_frame h q) a)
(plug_l l' (abort (ECont k ep)))).
rewrite (admin_olift_installed_let_frame 0 1 h q), Ha.
reflexivity.
+ simpl.
change (ELet (krepl_e h (installed_let_frame h q) a)
(krepl_e h (olift_d 0 1 (installed_let_frame h q))
(plug_l l e)) =
ELet (krepl_e h (installed_let_frame h q) a) (plug_l l' ep)).
rewrite (admin_olift_installed_let_frame 0 1 h q), Hb.
reflexivity.
Qed.
Lemma admin_krepl_frame_abort_nonhit (h : nat) (q : expr)
(k : nat) (l : lctx) (e : expr) :
h <> k ->
star (admin_step)
(krepl_e h (installed_let_frame h q)
(ECont k (plug_l l (abort (ECont k e)))))
(krepl_e h (installed_let_frame h q)
(ECont k (plug_l l e))).
Proof.
intro Hhk.
destruct (admin_krepl_frame_tail_abort_shape_nonhit h q k l e Hhk)
as [l' [ep [Hsrc Hdst]]].
assert (Hkh : Nat.eqb k h = false).
{ apply Nat.eqb_neq. intro Heq. apply Hhk. symmetry. exact Heq. }
simpl. rewrite Hkh, Hsrc, Hdst.
apply star_one. apply AS_ContTail.
apply tail_step_plug_abort.
Qed.
Lemma admin_krepl_frame_abort_residual (h : nat) (q : expr)
(k : nat) (l : lctx) (e : expr) :
joinable (admin_step)
(krepl_e h (installed_let_frame h q)
(ECont k (plug_l l (abort (ECont k e)))))
(krepl_e h (installed_let_frame h q)
(ECont k (plug_l l e))).
Proof.
destruct (Nat.eqb k h) eqn:Hkh.
- apply Nat.eqb_eq in Hkh. subst k.
apply admin_krepl_frame_abort_hit.
- exists (krepl_e h (installed_let_frame h q)
(ECont k (plug_l l e))); split.
+ apply admin_krepl_frame_abort_nonhit.
intro Heq. subst k. rewrite Nat.eqb_refl in Hkh. discriminate.
+ apply star_refl.
Qed.
(**
Naturality of the [Let.S] substitution.
The continuation bound by the inner [EShift] is fresh for the ambient
replacement. The statement below keeps those two binders separate: the
ambient replacement is at [k], while the body is mapped at
[S (k + cut)]. This is the equation needed when a [Let.S] root is
transported through a continuation replacement.
*)
Definition klift0_d (k : nat) (d : installed) : installed :=
klift_d 0 k d.
Definition klift0_e (k : nat) (e : expr) : expr :=
klift_e 0 k e.
Lemma admin_klift_add_d (a b : nat) (d : installed) :
klift_d 0 a (klift_d 0 b d) = klift_d 0 (b + a) d.
Proof.
destruct d as [j c]. simpl. f_equal.
- destruct (Nat.leb 0 j); simpl; lia.
- induction c as [|c IH tail]; simpl.
+ reflexivity.
+ rewrite IH. f_equal. apply fs_klift_comp_e.
Qed.
Lemma admin_klift_zero_d (cut : nat) (d : installed) :
klift_d cut 0 d = d.
Proof.
destruct d as [j c]. simpl. f_equal.
- destruct (Nat.leb cut j); lia.
- induction c as [|c IH tail]; simpl.
+ reflexivity.
+ rewrite IH, klift_zero_e. reflexivity.
Qed.
(* Removing a continuation binder from a continuation lifted above that
binder is just the corresponding lift with the binder removed. This is
the small arithmetic identity used by the non-hit dL.S fusion below. *)
Lemma admin_ksubst_lift_prefix (cut k : nat) (q : expr) :
SharedPrompts.ksubst_e cut (ICont (k + cut) CHole)
(klift_e 0 (S cut) q) = klift_e 0 cut q.
Proof.
rewrite <- (SharedPrompts.MapComposition.klift_prefix_e 0 cut q).
replace (0 + cut) with cut by lia.
apply ksubst_klift_e.
Qed.
(* Non-hit dL.S fusion. The replacement head [h] and the dL.S delimiter
[k] are distinct, so continuation removal commutes with the replacement;
the shift raises both heads and the ordinary-let case raises the payload.
This is an equality, rather than a reduction transport statement: it is
the algebraic endpoint used by the corresponding residual gadget. *)
Lemma admin_ksubst_krepl_frame_nonhit :
(forall v cut h k q,
h <> k ->
SharedPrompts.ksubst_v cut (ICont (k + cut) CHole)
(krepl_v (S (h + cut))
(klift_d 0 (S cut) (installed_let_frame h q)) v) =
krepl_v (h + cut) (klift_d 0 cut (installed_let_frame h q))
(SharedPrompts.ksubst_v cut (ICont (k + cut) CHole) v)) /\
(forall e cut h k q,
h <> k ->
SharedPrompts.ksubst_e cut (ICont (k + cut) CHole)
(krepl_e (S (h + cut))
(klift_d 0 (S cut) (installed_let_frame h q)) e) =
krepl_e (h + cut) (klift_d 0 cut (installed_let_frame h q))
(SharedPrompts.ksubst_e cut (ICont (k + cut) CHole) e)).
Proof.
apply (syntax_ind
(fun v => forall cut h k q, h <> k ->
SharedPrompts.ksubst_v cut (ICont (k + cut) CHole)
(krepl_v (S (h + cut))
(klift_d 0 (S cut) (installed_let_frame h q)) v) =
krepl_v (h + cut) (klift_d 0 cut (installed_let_frame h q))
(SharedPrompts.ksubst_v cut (ICont (k + cut) CHole) v))
(fun e => forall cut h k q, h <> k ->
SharedPrompts.ksubst_e cut (ICont (k + cut) CHole)
(krepl_e (S (h + cut))
(klift_d 0 (S cut) (installed_let_frame h q)) e) =
krepl_e (h + cut) (klift_d 0 cut (installed_let_frame h q))
(SharedPrompts.ksubst_e cut (ICont (k + cut) CHole) e))).
- intros n cut h k q Hhk. reflexivity.
- intros e IH cut h k q Hhk. simpl. f_equal.
specialize (IH cut h k (olift_e 1 1 q) Hhk).
simpl in IH.
rewrite (klift_olift_e_comm 0 1 (S cut) 1 q) in IH.
rewrite (klift_olift_e_comm 0 1 cut 1 q) in IH.
exact IH.
- intros v IH cut h k q Hhk. simpl. f_equal. apply IH; exact Hhk.
- intros v IHv u IHu cut h k q Hhk. simpl. f_equal;
[apply IHv | apply IHu]; exact Hhk.
- intros a IHa t IHt cut h k q Hhk. simpl. f_equal.
+ apply IHa. exact Hhk.
+ specialize (IHt cut h k (olift_e 1 1 q) Hhk).
simpl in IHt.
rewrite (klift_olift_e_comm 0 1 (S cut) 1 q) in IHt.
rewrite (klift_olift_e_comm 0 1 cut 1 q) in IHt.
exact IHt.
- intros e IH cut h k q Hhk. simpl. f_equal.
specialize (IH (S cut) h k q Hhk).
simpl in IH.
assert (Hq1 : klift_e 0 1 (klift_e 0 (S cut) q) =
klift_e 0 (S (S cut)) q).
{ rewrite (fs_klift_comp_e 0 1 (S cut) q).
replace (S cut + 1) with (S (S cut)) by lia. reflexivity. }
assert (Hq0 : klift_e 0 1 (klift_e 0 cut q) =
klift_e 0 (S cut) q).
{ rewrite (fs_klift_comp_e 0 1 cut q).
replace (cut + 1) with (S cut) by lia. reflexivity. }
rewrite Hq1, Hq0.
replace (S (S (h + cut))) with (S (h + S cut)) by lia.
replace (h + S cut + 1) with (h + S (S cut)) by lia.
replace (h + cut + 1) with (h + S cut) by lia.
replace (k + cut + 1) with (k + S cut) by lia.
replace (S (h + cut)) with (h + S cut) by lia.
exact IH.
- intros n e IH cut h k q Hhk. simpl.
destruct (Nat.eqb n (S (h + cut))) eqn:Hnr.
+ apply Nat.eqb_eq in Hnr. subst n.
replace (h + S cut) with (S (h + cut)) by lia.
assert (Hremhit : remove_index cut (S (h + cut)) = Some (h + cut)).
{ apply remove_index_shift. lia. }
cbn [SharedPrompts.ksubst_e]. rewrite Hremhit. simpl.
assert (Hframe1 :
ICont (S (h + cut)) (CLet CHole (klift_e 0 (S cut) q)) =
klift_d 0 (S cut) (installed_let_frame h q)).
{ rewrite (admin_klift_installed_let_frame 0 (S cut) h q).
unfold installed_let_frame. simpl.
destruct (Nat.leb 0 h); f_equal; lia. }
assert (Hframe0 :
ICont (h + cut) (CLet CHole (klift_e 0 cut q)) =
klift_d 0 cut (installed_let_frame h q)).
{ rewrite (admin_klift_installed_let_frame 0 cut h q).
unfold installed_let_frame. simpl.
destruct (Nat.leb 0 h); f_equal; lia. }
rewrite Hframe1, Hframe0.
rewrite Nat.eqb_refl. f_equal. f_equal.
* rewrite (IH cut h k q Hhk). reflexivity.
* apply admin_ksubst_lift_prefix.
+ destruct (remove_index cut n) as [j|] eqn:Hrem.
* cbn [SharedPrompts.ksubst_e]. rewrite Hrem. simpl.
replace (h + S cut) with (S (h + cut)) by lia.
assert (Hnc : Nat.eqb n cut = false).
{ unfold remove_index in Hrem.
destruct (Nat.eqb n cut); discriminate || reflexivity. }
assert (Hj : Nat.eqb j (h + cut) = false).
{ pose proof (@remove_index_eqb_above cut n (h + cut)
ltac:(lia) Hnc) as Hmap.
rewrite Hrem in Hmap. simpl in Hmap.
rewrite Hnr in Hmap. exact Hmap. }
assert (Hframe1 :
ICont (S (h + cut)) (CLet CHole (klift_e 0 (S cut) q)) =
klift_d 0 (S cut) (installed_let_frame h q)).
{ rewrite (admin_klift_installed_let_frame 0 (S cut) h q).
unfold installed_let_frame. simpl.
destruct (Nat.leb 0 h); f_equal; lia. }
assert (Hframe0 :
ICont (h + cut) (CLet CHole (klift_e 0 cut q)) =
klift_d 0 cut (installed_let_frame h q)).
{ rewrite (admin_klift_installed_let_frame 0 cut h q).
unfold installed_let_frame. simpl.
destruct (Nat.leb 0 h); f_equal; lia. }
rewrite Hframe1, Hframe0, Hj. f_equal.
apply IH. exact Hhk.
* cbn [SharedPrompts.ksubst_e]. rewrite Hrem. simpl.
replace (h + S cut) with (S (h + cut)) by lia.
assert (Hkh : Nat.eqb (k + cut) (h + cut) = false).
{ apply Nat.eqb_neq. intro Heq. apply Hhk. lia. }
assert (Hframe1 :
ICont (S (h + cut)) (CLet CHole (klift_e 0 (S cut) q)) =
klift_d 0 (S cut) (installed_let_frame h q)).
{ rewrite (admin_klift_installed_let_frame 0 (S cut) h q).
unfold installed_let_frame. simpl.
destruct (Nat.leb 0 h); f_equal; lia. }
assert (Hframe0 :
ICont (h + cut) (CLet CHole (klift_e 0 cut q)) =
klift_d 0 cut (installed_let_frame h q)).
{ rewrite (admin_klift_installed_let_frame 0 cut h q).
unfold installed_let_frame. simpl.
destruct (Nat.leb 0 h); f_equal; lia. }
rewrite Hframe1, Hframe0, Hkh. f_equal.
apply IH. exact Hhk.
- intros n cut h k q Hhk. simpl.
destruct (Nat.eqb n (S (h + cut))) eqn:Hnr.
+ apply Nat.eqb_eq in Hnr. subst n.
cbn [SharedPrompts.ksubst_e].
replace (h + S cut) with (S (h + cut)) by lia.
assert (Hremhit : remove_index cut (S (h + cut)) = Some (h + cut)).
{ apply remove_index_shift. lia. }
rewrite Hremhit.
simpl. rewrite Nat.eqb_refl. reflexivity.
+ destruct (remove_index cut n) as [j|] eqn:Hrem.
* cbn [SharedPrompts.ksubst_e]. rewrite Hrem. simpl.
assert (Hnc : Nat.eqb n cut = false).
{ unfold remove_index in Hrem.
destruct (Nat.eqb n cut); discriminate || reflexivity. }
assert (Hj : Nat.eqb j (h + cut) = false).
{ pose proof (@remove_index_eqb_above cut n (h + cut)
ltac:(lia) Hnc) as Hmap.
rewrite Hrem in Hmap. simpl in Hmap.
rewrite Hnr in Hmap. exact Hmap. }
rewrite Hj. reflexivity.
* cbn [SharedPrompts.ksubst_e]. rewrite Hrem. simpl.
assert (Hkh : Nat.eqb (k + cut) (h + cut) = false).
{ apply Nat.eqb_neq. intro Heq. apply Hhk. lia. }
rewrite Hkh. reflexivity.
Qed.
(* With a non-hit outer frame, the dL.S residual is now a direct instance of
the fusion equation above. The tail induction only lifts the same
joinability through ordinary let frames. *)
Lemma admin_krepl_frame_tail_shift_nonhit_tail (h : nat) (q : expr)
(k : nat) (l : lctx) (e : expr) :
h <> k -> cocc_e 0 e = true ->
joinable (delimited_step k)
(krepl_e h (installed_let_frame h q) (plug_l l (EShift e)))
(krepl_e h (installed_let_frame h q)
(plug_l l (abort (SharedPrompts.ksubst_e 0 (ICont k CHole) e)))).
Proof.
intros Hhk Hocc.
revert q e Hocc.
induction l as [|a l IH]; intros q e Hocc.
- assert (Hocc_m :
cocc_e 0
(krepl_e (S h) (klift_d 0 1 (installed_let_frame h q)) e) = true).
{ rewrite (admin_klift_installed_let_frame 0 1 h q).
destruct (Nat.leb 0 h) eqn:Hh; simpl.
- replace (h + 1) with (S h) by lia.
apply (proj2 admin_cocc_krepl_installed_frame e 0 (S h)
(klift_e 0 1 q)); [lia | exact Hocc].
- exfalso. apply Nat.leb_gt in Hh. lia. }
assert (Hfus := (proj2 admin_ksubst_krepl_frame_nonhit
e 0 h k q Hhk)).
assert (Hlift := admin_klift_krepl_e 0 h (installed_let_frame h q)
(SharedPrompts.ksubst_e 0 (ICont k CHole) e)).
idtac.
idtac.
simpl.
assert (Hframe1 :
ICont (h + 1) (CLet CHole (klift_e 0 1 q)) =
klift_d 0 1 (installed_let_frame h q)).
{ rewrite (admin_klift_installed_let_frame 0 1 h q).
destruct (Nat.leb 0 h) eqn:Hh; simpl.
- unfold installed_let_frame. reflexivity.
- exfalso. apply Nat.leb_gt in Hh. lia. }
rewrite Hframe1.
replace (k + 0) with k in Hfus by lia.
replace (h + 0) with h in Hfus by lia.
replace (S (h + 0)) with (S h) in Hfus by lia.
rewrite (admin_klift_zero_d 0 (installed_let_frame h q)) in Hfus.
exists (abort (SharedPrompts.ksubst_e 0 (ICont k CHole)
(krepl_e (S h) (klift_d 0 1 (installed_let_frame h q)) e))).
split.
+ apply star_one. right. apply TS_Shift. exact Hocc_m.
+ unfold abort. simpl.
rewrite Hframe1.
rewrite Hfus.
rewrite Hlift.
destruct (Nat.leb 0 h) eqn:Hh; simpl.
* apply star_refl.
* exfalso. apply Nat.leb_gt in Hh. lia.
- simpl.
assert (Hframe :
ICont h (CLet CHole (olift_e 1 1 q)) =
installed_let_frame h (olift_e 1 1 q)) by reflexivity.
rewrite Hframe.
apply (admin_joinable_map (delimited_step k) (delimited_step k)
(ELet (krepl_e h (installed_let_frame h q) a))).
+ intros x y Hxy. apply admin_tail_step_let_r. exact Hxy.
+ apply IH. exact Hocc.
Qed.
Lemma admin_krepl_frame_tail_shift_nonhit (h : nat) (q : expr)
(k : nat) (l : lctx) (e : expr) :
h <> k -> cocc_e 0 e = true ->
joinable (admin_step)
(krepl_e h (installed_let_frame h q)
(ECont k (plug_l l (EShift e))))
(krepl_e h (installed_let_frame h q)
(ECont k (plug_l l
(abort (SharedPrompts.ksubst_e 0 (ICont k CHole) e))))).
Proof.
intros Hhk Hocc.
assert (Hbody := admin_krepl_frame_tail_shift_nonhit_tail h q k l e
Hhk Hocc).
destruct Hbody as [w [Hw1 Hw2]].
simpl.
assert (Hkh : Nat.eqb k h = false).
{ apply Nat.eqb_neq. intro Heq. apply Hhk. symmetry. exact Heq. }
rewrite Hkh.
exists (ECont k w); split.
- apply admin_tail_star_lift. exact Hw1.
- apply admin_tail_star_lift. exact Hw2.
Qed.
(* The complete dL.S frame transport, isolating only the genuine hit case
as a premise. The non-hit branch is the fusion lemma above; AbortD is
already handled by the explicit abort/spine residual. *)
Lemma admin_frame_tail_compatibility_from_shift_hit
(Hshift : forall h q l e,
cocc_e 0 e = true ->
joinable (admin_step)
(krepl_e h (installed_let_frame h q)
(ECont h (plug_l l (EShift e))))
(krepl_e h (installed_let_frame h q)
(ECont h (plug_l l
(abort (SharedPrompts.ksubst_e 0 (ICont h CHole) e)))))) :
forall h q k e e',
tail_step k e e' ->
joinable (admin_step)
(krepl_e h (installed_let_frame h q) (ECont k e))
(krepl_e h (installed_let_frame h q) (ECont k e')).
Proof.
intros h q k e e' Hd.
pose proof (tail_step_shape k e e' Hd) as Hshape.
destruct Hshape as [Hshape|Hshape].
- destruct Hshape as [l [r [Hsrc [Hdst Hocc]]]].
rewrite Hsrc, Hdst.
destruct (Nat.eqb k h) eqn:Hkh.
+ apply Nat.eqb_eq in Hkh. subst k. apply Hshift. exact Hocc.
+ apply admin_krepl_frame_tail_shift_nonhit; [|exact Hocc].
intro Heq. subst k. rewrite Nat.eqb_refl in Hkh. discriminate.
- destruct Hshape as [l [r [Hsrc Hdst]]].
rewrite Hsrc, Hdst. apply admin_krepl_frame_abort_residual.
Qed.
(* A continuation replacement at [k+b] cannot hit an occurrence lifted by
[b] above the fresh continuation [k]. *)
Lemma admin_krepl_klift_nohit :
(forall v b k f,
krepl_v (k + b) f (klift_v b (S k) v) =
klift_v b (S k) v) /\
(forall e b k f,
krepl_e (k + b) f (klift_e b (S k) e) =
klift_e b (S k) e).
Proof.
apply (syntax_ind
(fun v => forall b k f,
krepl_v (k + b) f (klift_v b (S k) v) =
klift_v b (S k) v)
(fun e => forall b k f,
krepl_e (k + b) f (klift_e b (S k) e) =
klift_e b (S k) e)).
- intros n b k f. reflexivity.
- intros e IH b k f. simpl. f_equal.
rewrite (IH b k (olift_d 0 1 f)). reflexivity.
- intros v IH b k f. simpl. f_equal. apply IH.
- intros v IHv u IHu b k f. simpl. f_equal; [apply IHv|apply IHu].
- intros a IHa t IHt b k f. simpl. f_equal.
+ apply IHa.
+ apply IHt.
- intros e IH b k f. simpl. f_equal.
replace (S (k + b)) with (k + S b) by lia.
apply IH.
- intros n e IH b k f. simpl.
destruct (Nat.leb b n) eqn:Hbn; simpl.
+ replace (k + b + (S k)) with (S k + (k + b)) by lia.
apply Nat.leb_le in Hbn.
assert (Hneq : Nat.eqb (n + S k) (k + b) = false).
{ apply Nat.eqb_neq. lia. }
rewrite Hneq. f_equal. apply IH.
+ assert (Hbn' : n < b) by (apply Nat.leb_gt; exact Hbn).
assert (Hneq : Nat.eqb n (k + b) = false).
{ apply Nat.eqb_neq. lia. }
rewrite Hneq. f_equal. apply IH.
- intros n b k f. simpl.
destruct (Nat.leb b n) eqn:Hbn; simpl.
+ replace (k + b + (S k)) with (S k + (k + b)) by lia.
apply Nat.leb_le in Hbn.
assert (Hneq : Nat.eqb (n + S k) (k + b) = false).
{ apply Nat.eqb_neq. lia. }
rewrite Hneq. reflexivity.
+ assert (Hbn' : n < b) by (apply Nat.leb_gt; exact Hbn).
assert (Hneq : Nat.eqb n (k + b) = false).
{ apply Nat.eqb_neq. lia. }
rewrite Hneq. reflexivity.
Qed.
(* Hit-case fusion for the two replacements generated by a Let.S residual.
The outer continuation removal meets the fresh frame head exactly at
[h+cut]; the equation is the algebraic core of the frame-hit gadget. *)
Lemma admin_ksubst_krepl_frame_hit :
(forall v cut h q,
SharedPrompts.ksubst_v cut (ICont (h + cut) CHole)
(krepl_v cut (installed_let_frame cut (klift_e 0 (S cut) q))
(krepl_v (S (h + cut))
(klift_d 0 (S cut) (installed_let_frame h q)) v)) =
krepl_v (h + cut) (klift_d 0 cut (installed_let_frame h q))
(SharedPrompts.ksubst_v cut (ICont (h + cut) CHole) v)) /\
(forall e cut h q,
SharedPrompts.ksubst_e cut (ICont (h + cut) CHole)
(krepl_e cut (installed_let_frame cut (klift_e 0 (S cut) q))
(krepl_e (S (h + cut))
(klift_d 0 (S cut) (installed_let_frame h q)) e)) =
krepl_e (h + cut) (klift_d 0 cut (installed_let_frame h q))
(SharedPrompts.ksubst_e cut (ICont (h + cut) CHole) e)).
Proof.
apply (syntax_ind
(fun v => forall cut h q,
SharedPrompts.ksubst_v cut (ICont (h + cut) CHole)
(krepl_v cut (installed_let_frame cut (klift_e 0 (S cut) q))
(krepl_v (S (h + cut))
(klift_d 0 (S cut) (installed_let_frame h q)) v)) =
krepl_v (h + cut) (klift_d 0 cut (installed_let_frame h q))
(SharedPrompts.ksubst_v cut (ICont (h + cut) CHole) v))
(fun e => forall cut h q,
SharedPrompts.ksubst_e cut (ICont (h + cut) CHole)
(krepl_e cut (installed_let_frame cut (klift_e 0 (S cut) q))
(krepl_e (S (h + cut))
(klift_d 0 (S cut) (installed_let_frame h q)) e)) =
krepl_e (h + cut) (klift_d 0 cut (installed_let_frame h q))
(SharedPrompts.ksubst_e cut (ICont (h + cut) CHole) e))).
- intros n cut h q. reflexivity.
- intros e IH cut h q. simpl. f_equal.
specialize (IH cut h (olift_e 1 1 q)).
simpl in IH.
rewrite (klift_olift_e_comm 0 1 (S cut) 1 q) in IH.
rewrite (klift_olift_e_comm 0 1 cut 1 q) in IH.
exact IH.
- intros v IH cut h q. simpl. f_equal. apply IH.
- intros v IHv u IHu cut h q. simpl. f_equal;
[apply IHv | apply IHu].
- intros a IHa t IHt cut h q. simpl. f_equal.
+ apply IHa.
+ specialize (IHt cut h (olift_e 1 1 q)).
simpl in IHt.
rewrite (klift_olift_e_comm 0 1 (S cut) 1 q) in IHt.
rewrite (klift_olift_e_comm 0 1 cut 1 q) in IHt.
exact IHt.
- intros e IH cut h q. simpl. f_equal.
specialize (IH (S cut) h q).
simpl in IH.
replace (h + cut + 1) with (h + S cut) by lia.
replace (cut + 1) with (S cut) by lia.
replace (S (S (h + cut))) with (S (h + S cut)) by lia.
replace (h + S cut + 1) with (h + S (S cut)) by lia.
replace (h + cut + 1) with (h + S cut) by lia.
rewrite (fs_klift_comp_e 0 1 (S cut) q).
rewrite (fs_klift_comp_e 0 1 cut q).
replace (S cut + 1) with (S (S cut)) by lia.
replace (cut + 1) with (S cut) by lia.
unfold installed_let_frame in IH |- *.
replace (S (h + cut)) with (h + S cut) by lia.
exact IH.
- intros n e IH cut h q. simpl.
destruct (Nat.eqb n (S (h + cut))) eqn:Hinner.
+ destruct (Nat.eqb n (h + cut)) eqn:Houter; simpl.
* apply Nat.eqb_eq in Hinner.
apply Nat.eqb_eq in Houter. lia.
* apply Nat.eqb_eq in Hinner. subst n.
assert (Hneq : Nat.eqb (S (h + cut)) cut = false) by
(apply Nat.eqb_neq; lia).
assert (Hneq2 : Nat.eqb (h + S cut) cut = false) by
(apply Nat.eqb_neq; lia).
rewrite Hneq2.
cbn [SharedPrompts.ksubst_e].
cbn [SharedPrompts.krepl_e].
cbn [SharedPrompts.remove_index].
assert (Hrem : remove_index cut (h + S cut) = Some (h + cut)).
{ replace (h + S cut) with (S (h + cut)) by lia.
apply remove_index_shift. lia. }
rewrite Hrem.
assert (Hrem2 : remove_index cut (S (h + cut)) = Some (h + cut)).
{ apply remove_index_shift. lia. }
rewrite Hrem2.
cbn [SharedPrompts.krepl_e].
rewrite Nat.eqb_refl.
cbn [plug_d].
unfold plug, installed_let_frame. simpl.
assert (Hframe0 :
ICont (h + cut) (CLet CHole (klift_e 0 cut q)) =
klift_d 0 cut (installed_let_frame h q)).
{ rewrite (admin_klift_installed_let_frame 0 cut h q).
unfold installed_let_frame. simpl.
destruct (Nat.leb 0 h); f_equal; lia. }
assert (Hframe1 :
ICont (h + S cut) (CLet CHole (klift_e 0 (S cut) q)) =
klift_d 0 (S cut) (installed_let_frame h q)).
{ rewrite (admin_klift_installed_let_frame 0 (S cut) h q).
unfold installed_let_frame. simpl.
destruct (Nat.leb 0 h); f_equal; lia. }
f_equal.
f_equal.
{ rewrite Hframe1.
unfold installed_let_frame in IH |- *.
rewrite IH. rewrite Hframe0. reflexivity. }
{ pose proof (proj2 admin_krepl_klift_nohit q 0 cut
(ICont cut (CLet CHole (olift_e 1 1 (klift_e 0 (S cut) q)))))
as Hno.
replace (cut + 0) with cut in Hno by lia.
rewrite Hno.
apply admin_ksubst_lift_prefix. }
+ destruct (Nat.eqb n (h + cut)) eqn:Houter; simpl.
* apply Nat.eqb_eq in Houter. subst n.
destruct h as [|h].
(* The outer hit is also the cut-zero case. *)
{ rewrite Nat.eqb_refl.
cbn [SharedPrompts.ksubst_e].
rewrite remove_index_eq. simpl.
cbn [SharedPrompts.krepl_e].
cbn [plug_d].
rewrite remove_index_eq. simpl.
cbn [SharedPrompts.krepl_e].
cbn [plug_d].
rewrite Nat.eqb_refl.
assert (Hframe1 :
ICont (S cut) (CLet CHole (klift_e 0 (S cut) q)) =
klift_d 0 (S cut) (installed_let_frame 0 q)).
{ rewrite (admin_klift_installed_let_frame 0 (S cut) 0 q).
unfold installed_let_frame. simpl. reflexivity. }
f_equal. f_equal.
{ rewrite Hframe1.
replace (0 + cut) with cut by lia.
replace (S (0 + cut)) with (S cut) by lia.
exact (IH cut 0 q). }
{ exact (admin_ksubst_lift_prefix cut 0 q). } }
(* For a positive offset, removal decrements the head and neither
replacement hits it. *)
{ assert (Hneq : Nat.eqb (S h + cut) (S (S h + cut)) = false) by
(apply Nat.eqb_neq; lia).
assert (Hneq2 : Nat.eqb (S h + cut) cut = false) by
(apply Nat.eqb_neq; lia).
rewrite Hneq2.
assert (Hrem : remove_index cut (S h + cut) = Some (h + cut)).
{ replace (S h + cut) with (S (h + cut)) by lia.
apply remove_index_shift. lia. }
cbn [SharedPrompts.ksubst_e]. rewrite Hrem. simpl.
assert (Hhead : Nat.eqb (h + cut) (S h + cut) = false) by
(apply Nat.eqb_neq; lia).
assert (Hhead2 : Nat.eqb (h + cut) (S (h + cut)) = false) by
(apply Nat.eqb_neq; lia).
rewrite Hhead2.
cbn [SharedPrompts.krepl_e].
f_equal.
replace (S (h + cut)) with (S h + cut) by lia.
replace (S (S (h + cut))) with (S (S h + cut)) by lia.
replace (S (h + S cut)) with (S (S h + cut)) by lia.
assert (Hframe1 :
ICont (S (S h + cut))
(CLet CHole (klift_e 0 (S cut) q)) =
klift_d 0 (S cut) (installed_let_frame (S h) q)).
{ rewrite (admin_klift_installed_let_frame 0 (S cut) (S h) q).
unfold installed_let_frame. simpl.
destruct (Nat.leb 0 (S h)); f_equal; lia. }
assert (Hframe0 :
ICont (S h + cut) (CLet CHole (klift_e 0 cut q)) =
klift_d 0 cut (installed_let_frame (S h) q)).
{ rewrite (admin_klift_installed_let_frame 0 cut (S h) q).
unfold installed_let_frame. simpl.
destruct (Nat.leb 0 (S h)); f_equal; lia. }
rewrite Hframe1, Hframe0.
exact (IH cut (S h) q).
}
* destruct (Nat.eqb n cut) eqn:Hcut; simpl.
{ apply Nat.eqb_eq in Hcut. subst n.
rewrite remove_index_eq. simpl.
cbn [SharedPrompts.krepl_e].
rewrite Nat.eqb_refl.
cbn [plug_d].
assert (Hframe1 :
ICont (h + S cut) (CLet CHole (klift_e 0 (S cut) q)) =
klift_d 0 (S cut) (installed_let_frame h q)).
{ rewrite (admin_klift_installed_let_frame 0 (S cut) h q).
unfold installed_let_frame. simpl.
destruct (Nat.leb 0 h); f_equal; lia. }
assert (Hframe0 :
ICont (h + cut) (CLet CHole (klift_e 0 cut q)) =
klift_d 0 cut (installed_let_frame h q)).
{ rewrite (admin_klift_installed_let_frame 0 cut h q).
unfold installed_let_frame. simpl.
destruct (Nat.leb 0 h); f_equal; lia. }
f_equal. f_equal.
{ rewrite Hframe1. rewrite (IH cut h q). rewrite Hframe0. reflexivity. }
{ exact (admin_ksubst_lift_prefix cut h q). } }
{ destruct (remove_index cut n) as [j|] eqn:Hrem.
- cbn [SharedPrompts.ksubst_e]. simpl.
assert (Hnc : Nat.eqb n cut = false). exact Hcut.
assert (Hj : Nat.eqb j (h + cut) = false).
{ pose proof (@remove_index_eqb_above cut n (h + cut)
ltac:(lia) Hnc) as Hmap.
rewrite Hrem in Hmap. simpl in Hmap.
rewrite Hinner in Hmap. exact Hmap. }
rewrite Hj.
cbn [SharedPrompts.krepl_e].
f_equal.
assert (Hframe1 :
ICont (h + S cut) (CLet CHole (klift_e 0 (S cut) q)) =
klift_d 0 (S cut) (installed_let_frame h q)).
{ rewrite (admin_klift_installed_let_frame 0 (S cut) h q).
unfold installed_let_frame. simpl.
destruct (Nat.leb 0 h); f_equal; lia. }
rewrite Hframe1.
assert (Hframe0 :
ICont (h + cut) (CLet CHole (klift_e 0 cut q)) =
klift_d 0 cut (installed_let_frame h q)).
{ rewrite (admin_klift_installed_let_frame 0 cut h q).
unfold installed_let_frame. simpl.
destruct (Nat.leb 0 h); f_equal; lia. }
rewrite Hframe0.
exact (IH cut h q).
- exfalso.
unfold remove_index in Hrem.
rewrite Hcut in Hrem.
destruct (Nat.leb (S cut) n); discriminate. }
- intros n cut h q. simpl.
destruct (Nat.eqb n (S (h + cut))) eqn:Hinner.
+ destruct (Nat.eqb n (h + cut)) eqn:Houter.
* apply Nat.eqb_eq in Hinner.
apply Nat.eqb_eq in Houter. lia.
* destruct (remove_index cut n) as [j|] eqn:Hrem; simpl.
{ apply Nat.eqb_eq in Hinner. subst n.
assert (Hneq : Nat.eqb (h + S cut) cut = false) by
(apply Nat.eqb_neq; lia).
rewrite Hneq.
cbn [SharedPrompts.ksubst_e].
assert (Hrem2 : remove_index cut (h + S cut) = Some (h + cut)).
{ replace (h + S cut) with (S (h + cut)) by lia.
apply remove_index_shift. lia. }
rewrite Hrem2.
assert (Hnc : Nat.eqb (S (h + cut)) cut = false).
{ unfold remove_index in Hrem.
destruct (Nat.eqb (S (h + cut)) cut); discriminate || reflexivity. }
assert (Hmap : Nat.eqb j (h + cut) = true).
{ pose proof (@remove_index_eqb_above cut (S (h + cut)) (h + cut)
ltac:(lia) Hnc) as H0.
rewrite Hrem in H0. simpl in H0.
destruct (Nat.eqb (h + cut) (h + cut)) eqn:Hself in H0;
simpl in H0.
- exact H0.
- rewrite Nat.eqb_refl in Hself. discriminate. }
rewrite Hmap.
cbn [SharedPrompts.ksubst_e].
reflexivity. }
{ apply Nat.eqb_eq in Hinner. subst n.
assert (Hneq : Nat.eqb (h + S cut) cut = false) by
(apply Nat.eqb_neq; lia).
rewrite Hneq.
assert (Hrem2 : remove_index cut (h + S cut) = Some (h + cut)).
{ replace (h + S cut) with (S (h + cut)) by lia.
apply remove_index_shift. lia. }
cbn [SharedPrompts.ksubst_e]. rewrite Hrem2.
rewrite Nat.eqb_refl. reflexivity. }
+ destruct (Nat.eqb n (h + cut)) eqn:Houter.
* destruct (remove_index cut n) as [j|] eqn:Hrem; simpl.
{ apply Nat.eqb_eq in Houter. subst n.
assert (Hnc : Nat.eqb (h + cut) cut = false).
{ unfold remove_index in Hrem.
destruct (Nat.eqb (h + cut) cut); discriminate || reflexivity. }
cbn [SharedPrompts.ksubst_e].
rewrite Hnc.
cbn [remove_index].
assert (Hmap : Nat.eqb j (h + cut) = false).
{ pose proof (@remove_index_eqb_above cut (h + cut) (h + cut)
ltac:(lia) Hnc) as H0.
rewrite Hrem in H0. simpl in H0.
rewrite Hinner in H0. exact H0. }
rewrite Hmap.
cbn [SharedPrompts.ksubst_e].
rewrite Hrem. reflexivity. }
{ apply Nat.eqb_eq in Houter. subst n.
destruct h as [|h].
- cbn [SharedPrompts.ksubst_e].
replace (0 + cut) with cut by lia.
rewrite Nat.eqb_refl. simpl.
rewrite remove_index_eq. reflexivity.
- exfalso.
unfold remove_index in Hrem.
assert (Hneq : Nat.eqb (S h + cut) cut = false) by
(apply Nat.eqb_neq; lia).
rewrite Hneq in Hrem.
destruct (Nat.leb (S cut) (S h + cut)); discriminate. }
* destruct (remove_index cut n) as [j|] eqn:Hrem; simpl.
{ cbn [SharedPrompts.ksubst_e].
assert (Hnc : Nat.eqb n cut = false).
{ unfold remove_index in Hrem.
destruct (Nat.eqb n cut); discriminate || reflexivity. }
assert (Hmap : Nat.eqb j (h + cut) = false).
{ pose proof (@remove_index_eqb_above cut n (h + cut)
ltac:(lia) Hnc) as H0.
rewrite Hrem in H0. simpl in H0.
rewrite Hinner in H0. exact H0. }
rewrite Hmap.
rewrite Hnc.
cbn [SharedPrompts.ksubst_e].
rewrite Hrem. reflexivity. }
{ destruct (Nat.eqb n cut) eqn:Hcut; simpl.
- apply Nat.eqb_eq in Hcut. subst n.
cbn [SharedPrompts.ksubst_e].
rewrite Nat.eqb_refl.
rewrite remove_index_eq. reflexivity.
- cbn [SharedPrompts.ksubst_e].
rewrite Hrem. simpl. rewrite Nat.eqb_refl. reflexivity. }
Qed.
(* Empty-tail hit residual. The mapped source first exposes a Let.S redex;
the resulting tail shift is then contracted by dL.S. On the target the
same Let.S step is a stutter after [krepl] meets the freshly lifted body.
The two endpoints are identified by [admin_ksubst_krepl_frame_hit]. *)
Lemma admin_krepl_frame_tail_shift_hit_empty (h : nat) (q e : expr) :
cocc_e 0 e = true ->
joinable (admin_step)
(krepl_e h (installed_let_frame h q) (ECont h (EShift e)))
(krepl_e h (installed_let_frame h q)
(ECont h (abort (SharedPrompts.ksubst_e 0 (ICont h CHole) e)))).
Proof.
intro Hocc.