-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemantics_L1.v
More file actions
2310 lines (2106 loc) · 100 KB
/
Copy pathSemantics_L1.v
File metadata and controls
2310 lines (2106 loc) · 100 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
(* SPDX-License-Identifier: PMPL-1.0-or-later *)
(* SPDX-FileCopyrightText: 2026 Jonathan D.A. Jewell *)
(**
*********************************************************************
*** ✅ ACTIVE -- L1 semantics. Modality-indexed. ***
*** ***
*** This is the post-counterexample L1 redesign. Extend HERE. ***
*** ***
*** 3 `Admitted` lemmas remain (down from 7 mid-chain, 9 pre- ***
*** bullet-restoration). L2-β closures so far: ***
*** - typing_preserves_bindings_l1 (now m-polymorphic) ***
*** - unrestricted_flag_unchanged_l1 ***
*** - shift_typing_gen_l1 (via shift_typing_gen_l1_m + wrapper) ***
*** - subst_typing_gen_l1 (via subst_typing_gen_l1_m + wrapper) ***
*** - count_occ_le_l1 (via count_occ_le_l1_m + wrapper) ***
*** - region_shrink_preserves_typing_l1_gen (via ***
*** region_shrink_preserves_typing_l1_gen_m + wrapper) ***
*** - typing_preserves_length_l1, output_shape_at_l1, ***
*** loc_retype_at_R_l1 all generalised to m-polymorphic ***
*** ***
*** Residual admits (L2-β follow-up): ***
*** 1. region_shrink_preserves_typing_l1_gen_m — list-vs- ***
*** multiset structural mismatch in T_Region_Active_L1 ***
*** shadowed case (1 internal admit). Bullet structure ***
*** restored 2026-05-27; only the structural sub-case ***
*** remains. ***
*** 2. region_liveness_at_split_l1_gen — 1 narrow admit in the ***
*** T_Region_Active_L1 [r = rv] sub-case (genuinely false ***
*** per documented counterexample ERegion rv (EI32 5)) ***
*** 3. preservation_l1 — capstone; depends on closing (1)+(2) ***
*** under L2 dispatch + lambda-rigidity gap resolution ***
*** ***
*** DO NOT close them by: ***
*** - introducing new `Axiom` declarations ***
*** - ad-hoc side conditions on compound rules ***
*** - strengthened lemma signatures dodging the L2 dispatch ***
*** ***
*** Cross-layer dependencies (L1's lambda-rigidity gap closes at ***
*** L2) are documented in `formal/PRESERVATION-DESIGN.md §5.1`. ***
*** ***
*** See `STATUS.adoc`, `PROOF-NEEDS.md`, and ***
*** `formal/PRESERVATION-DESIGN.md`. ***
*********************************************************************
*)
(** * Ephapax Preservation under the L1 judgment (R-threaded typing)
This file states [preservation_l1] for the new [has_type_l1]
judgment in [TypingL1.v]. The operational semantics [step] from
[Semantics.v] is unchanged.
Per PRESERVATION-DESIGN.md §4.5, preservation under L1 is:
[step (mu, R, e) (mu', R', e')] /\
[has_type_l1 R G e T R_final G']
->
[has_type_l1 R' G e' T R_final G']
The [R_final] and [G'] outputs are invariant under stepping —
they describe the state after the entire expression has fully
evaluated, which the operational step does not change.
Current status (post-swarm A+B+C, sequenced multi-PR closure):
- [remove_first_eq_l1] — Qed (trivial).
- [value_R_G_preserving_l1] — Qed (L1.A, PR #158). Induction on
[is_value] with nested IH for EInl, EInr, EPair, EBorrow.
- [region_shrink_preserves_typing_l1] — Qed (L1.B, PR #159) via
auxiliary [region_shrink_preserves_typing_l1_gen] which itself
is Admitted; 22/24 typing-rule cases close, 2 residual admits
in T_Region_L1 / T_Region_Active_L1 sub-cases blocked on L1
analogs of legacy [region_env_perm_typing] / [region_add_typing]
(tasks #25 / #26).
- [subst_preserves_typing_l1] — Qed (L1.C) with strengthened
statement (the original signature was demonstrably unsound — see
the lemma's header). Depended on a single isolated sub-Axiom
[loc_retype_at_R_l1] (task #27); the L1.F PR replaces that
unsound axiom with (i) a Qed-able [loc_retype_at_R_l1] requiring
[In r R_inner] and (ii) a narrower [region_liveness_at_split_l1]
axiom capturing only the genuine structural obligation that
remains. Two compound-rule cases (T_Lam_L1_Linear, T_Region_L1) are
discharged directly with no axiom; nine residual sites cite the
narrower axiom. See its header for closure approaches.
- [preservation_l1] — Admitted; the three swarm helpers above
unblock the per-case proof, but the bullet structure (per
design doc §4.5 + §12.16) is itself follow-up work (task #24).
Per-case proof sketches were validated experimentally during this
file's authoring. The cases that close without any of the three
Admitted helpers are: S_StringNew (apply T_Loc_L1), S_StringConcat
(invert both T_Loc_L1 children, then apply T_Loc_L1), S_StringLen
(invert the borrow, apply T_I32_L1), S_If_True / S_If_False
(invert T_Bool_L1 on the condition, then assumption), S_Region_
Enter (re-apply T_Region_Active_L1 with the same R_body), and
S_Drop (apply T_Unit_L1). These can be inlined once the bullet
structure accounts for the per-region typing cross-cases
(ERegion inverts to both T_Region_L1 and T_Region_Active_L1,
doubling subgoals for the three region step rules).
Cases requiring helpers:
- S_Region_Exit needs [region_shrink_preserves_typing_l1].
- Congruence (S_X_Step) cases need [value_R_G_preserving_l1].
- β-reduction (S_Let_Val, S_LetLin_Val, S_App_Fun, S_Case_Inl,
S_Case_Inr) cases need [subst_preserves_typing_l1].
Vacuous: S_Borrow_Step (both typing sub-cases — the inner cannot
step under either typing rule).
Once the three helpers are Qed, the per-case proofs are
mechanical; the full theorem closure is sequenced as task #19's
continuation. *)
Require Import Coq.Strings.String.
Require Import Coq.Lists.List.
Require Import Coq.Arith.Arith.
Require Import Coq.Bool.Bool.
Require Import Lia.
Import ListNotations.
From Ephapax Require Import Syntax.
From Ephapax Require Import Typing.
From Ephapax Require Import Modality.
From Ephapax Require Import TypingL1.
From Ephapax Require Import Semantics.
(** ** Trivial: the operational [remove_first] and the L1
[remove_first_L1] coincide pointwise. *)
Lemma remove_first_eq_l1 :
forall r R,
remove_first_L1 r R = remove_first r R.
Proof.
intros r R. induction R as [| r' R' IH]; simpl.
- reflexivity.
- rewrite IH. reflexivity.
Qed.
(** ** Helper: values preserve both R and G under the L1 judgment.
Inductive on [is_value v]. Atomic value rules (T_Unit_L1, T_Bool_L1,
T_I32_L1, T_Loc_L1, T_StringNew_L1, T_Lam_L1_Linear) give R_out = R_in,
G_out = G_in directly. Compound value forms (EInl, EInr, EPair,
EBorrow of value) propagate via IH on the value-shaped sub-
expression. Detailed proof deferred to L1 follow-up PR. *)
Lemma value_R_G_preserving_l1 :
forall m R G v T R' G',
is_value v ->
has_type_l1 m R G v T R' G' ->
R' = R /\ G' = G.
Proof.
intros m R G v T R' G' Hv. revert m R G T R' G'.
induction Hv; intros m0 R0 G0 T0 R0' G0' Ht.
- (* VUnit *) inversion Ht; subst; split; reflexivity.
- (* VBool *) inversion Ht; subst; split; reflexivity.
- (* VI32 *) inversion Ht; subst; split; reflexivity.
- (* VLam *) inversion Ht; subst; split; reflexivity.
- (* VPair v1 v2 *)
inversion Ht; subst.
match goal with
| [ H1 : has_type_l1 _ _ _ v1 _ _ _,
H2 : has_type_l1 _ _ _ v2 _ _ _ |- _ ] =>
specialize (IHHv1 _ _ _ _ _ _ H1) as [HR1 HG1]; subst;
specialize (IHHv2 _ _ _ _ _ _ H2) as [HR2 HG2]; subst
end.
split; reflexivity.
- (* VInl T v *)
inversion Ht; subst.
match goal with
| [ H : has_type_l1 _ _ _ v _ _ _ |- _ ] =>
specialize (IHHv _ _ _ _ _ _ H) as [HR HG]; subst
end.
split; reflexivity.
- (* VInr T v *)
inversion Ht; subst.
match goal with
| [ H : has_type_l1 _ _ _ v _ _ _ |- _ ] =>
specialize (IHHv _ _ _ _ _ _ H) as [HR HG]; subst
end.
split; reflexivity.
- (* VLoc *) inversion Ht; subst; split; reflexivity.
- (* VBorrow v *)
inversion Ht; subst.
+ (* T_Borrow_L1: EBorrow (EVar i) — impossible since v is a value, not EVar *)
inversion Hv.
+ (* T_Borrow_Val_L1 *)
split; reflexivity.
- (* VEcho T v — T_Echo_L1 (slice 3a) types this at [TEcho T]
with R_out = R and G_out = G by construction; both invariants
hold immediately. *)
inversion Ht; subst; split; reflexivity.
Qed.
(** ** Helper: region-environment shrinkage for value typings.
Mirrors [Semantics.region_shrink_preserves_typing] under the L1
judgment. Used by the [S_Region_Exit] case of [preservation_l1].
Detailed proof deferred to L1 follow-up PR. *)
(** Small commutation/idempotence facts about [remove_first]. *)
Lemma remove_first_comm :
forall r1 r2 R,
remove_first r1 (remove_first r2 R) = remove_first r2 (remove_first r1 R).
Proof.
intros r1 r2. induction R as [| r' R' IH]; [reflexivity|].
simpl.
destruct (String.eqb r2 r') eqn:Heq2; destruct (String.eqb r1 r') eqn:Heq1.
- (* r2 = r' = r1; both pop r' *)
simpl. apply String.eqb_eq in Heq1, Heq2. subst.
reflexivity.
- simpl. rewrite Heq2. reflexivity.
- simpl. rewrite Heq1. reflexivity.
- simpl. rewrite Heq1, Heq2. f_equal. apply IH.
Qed.
(** ** Region-count monotonicity for L1 typing.
Every typing rule either preserves the count of region [r] in
the threaded environment ([R = R'] for all atomic / variable /
value rules; [R1 = R2] threading via children for compound
rules), or decreases it by exactly one (via [remove_first_L1 r]
in [T_Region_L1] / [T_Region_Active_L1]).
Consequently, [count_occ string_dec r R' <= count_occ string_dec r R]
for any well-typed expression. This is the L1 analog of the
structural fact that legacy [has_type] has [R] unchanged: the
"drop" is bounded by what the operational [S_Region_Exit] can
do.
Used to discharge the [T_Region_L1] shadowed sub-case of
[region_shrink_preserves_typing_l1_gen] as vacuous (the inner
region's output cannot contain >1 copies of the freshly-pushed
region). *)
(** Local notation: [cnt r R] = number of occurrences of region [r]
in environment [R]. Wraps stdlib [count_occ] with the (list,
element) argument order. *)
Definition cnt (r : region_name) (R : region_env) : nat :=
count_occ string_dec R r.
Lemma remove_first_L1_count_eq_self :
forall (r : region_name) (R : region_env),
cnt r (remove_first_L1 r R) = cnt r R - 1.
Proof.
intros r R. unfold cnt.
induction R as [|r' R' IH]; simpl; [reflexivity|].
destruct (String.eqb r r') eqn:Heq.
- apply String.eqb_eq in Heq. subst r'.
destruct (string_dec r r) as [_|Hne]; [|exfalso; apply Hne; reflexivity].
simpl. lia.
- apply String.eqb_neq in Heq.
simpl. destruct (string_dec r' r) as [Heq'|_].
+ exfalso. apply Heq. symmetry. exact Heq'.
+ exact IH.
Qed.
Lemma remove_first_L1_count_other :
forall (r r0 : region_name) (R : region_env),
r <> r0 ->
cnt r0 (remove_first_L1 r R) = cnt r0 R.
Proof.
intros r r0 R Hne. unfold cnt.
induction R as [|r' R' IH]; simpl; [reflexivity|].
destruct (String.eqb r r') eqn:Heq.
- apply String.eqb_eq in Heq. subst r'.
destruct (string_dec r r0) as [Heq'|_]; [exfalso; apply Hne; exact Heq'|].
reflexivity.
- simpl. destruct (string_dec r' r0) as [->|_].
+ simpl. rewrite IH. reflexivity.
+ exact IH.
Qed.
(** Count monotonicity: every L1 typing rule has [cnt r R'
<= cnt r R] for every region [r]. *)
Lemma count_occ_le_l1_m :
forall m R G e T R' G',
R ; G |=L1[m] e : T -| R' ; G' ->
forall r, cnt r R' <= cnt r R.
Proof.
intros m R G e T R' G' Ht.
induction Ht; intros r0; unfold cnt in *; simpl in *;
try lia;
try (specialize (IHHt r0); lia);
try (specialize (IHHt1 r0); specialize (IHHt2 r0); lia);
try (specialize (IHHt1 r0); specialize (IHHt2 r0); specialize (IHHt3 r0); lia).
- (* T_Region_L1: R' = remove_first_L1 r R_body, body input r::R *)
specialize (IHHt r0).
pose proof (remove_first_L1_count_eq_self r R_body) as Hself.
unfold cnt in Hself.
destruct (string_dec r r0) as [<-|Hne].
+ rewrite Hself.
destruct (string_dec r r) as [_|Hbad]; [|exfalso; apply Hbad; reflexivity].
lia.
+ pose proof (remove_first_L1_count_other r r0 R_body) as Hoth.
assert (Hne' : r <> r0) by (intro Hbad; apply Hne; exact Hbad).
specialize (Hoth Hne').
unfold cnt in Hoth. rewrite Hoth.
destruct (string_dec r r0) as [Heq|_]; [exfalso; apply Hne; exact Heq|].
lia.
- (* T_Region_Active_L1 *)
specialize (IHHt r0).
pose proof (remove_first_L1_count_eq_self r R_body) as Hself.
unfold cnt in Hself.
destruct (string_dec r r0) as [<-|Hne].
+ rewrite Hself. lia.
+ pose proof (remove_first_L1_count_other r r0 R_body) as Hoth.
assert (Hne' : r <> r0) by (intro Hbad; apply Hne; exact Hbad).
specialize (Hoth Hne').
unfold cnt in Hoth. rewrite Hoth.
lia.
- (* T_Region_L1_Echo — same remove_first_L1 shape as T_Region_L1 *)
specialize (IHHt r0).
pose proof (remove_first_L1_count_eq_self r R_body) as Hself.
unfold cnt in Hself.
destruct (string_dec r r0) as [<-|Hne].
+ rewrite Hself.
destruct (string_dec r r) as [_|Hbad]; [|exfalso; apply Hbad; reflexivity].
lia.
+ pose proof (remove_first_L1_count_other r r0 R_body) as Hoth.
assert (Hne' : r <> r0) by (intro Hbad; apply Hne; exact Hbad).
specialize (Hoth Hne').
unfold cnt in Hoth. rewrite Hoth.
destruct (string_dec r r0) as [Heq|_]; [exfalso; apply Hne; exact Heq|].
lia.
- (* T_Region_Active_L1_Echo — same shape as T_Region_Active_L1 *)
specialize (IHHt r0).
pose proof (remove_first_L1_count_eq_self r R_body) as Hself.
unfold cnt in Hself.
destruct (string_dec r r0) as [<-|Hne].
+ rewrite Hself. lia.
+ pose proof (remove_first_L1_count_other r r0 R_body) as Hoth.
assert (Hne' : r <> r0) by (intro Hbad; apply Hne; exact Hbad).
specialize (Hoth Hne').
unfold cnt in Hoth. rewrite Hoth.
lia.
Qed.
(** Linear specialisation — preserves the legacy call sites. *)
Lemma count_occ_le_l1 :
forall R G e T R' G',
R; G |=L1 e : T -| R'; G' ->
forall r, cnt r R' <= cnt r R.
Proof.
intros R G e T R' G' Ht r.
exact (count_occ_le_l1_m Linear R G e T R' G' Ht r).
Qed.
(** Corollary: if [r] appears in the output, it appeared at least
that many times in the input. *)
Lemma count_occ_in_input_l1 :
forall R G e T R' G',
R; G |=L1 e : T -| R'; G' ->
forall r, In r R' -> In r R.
Proof.
intros R G e T R' G' Ht r Hin.
apply (count_occ_In string_dec) in Hin.
pose proof (count_occ_le_l1 _ _ _ _ _ _ Ht r) as Hle.
unfold cnt in Hle.
apply (count_occ_In string_dec). lia.
Qed.
(** ** L1 region-environment set/multiset-equivalence — design note.
Legacy [region_env_perm_typing] (in [Semantics.v]) uses set-
equivalence between region environments to transport typings.
The L1 analog would say: if [forall r, In r R1 <-> In r R2] and
[R1; G |=L1 e : T -| R1'; G'], then there exists [R2'] with
[R2; G |=L1 e : T -| R2'; G'].
The L1 version is fundamentally weaker than legacy because L1's
[T_Region_L1] and [T_Region_Active_L1] rules pop a *specific
list occurrence* (the FIRST one) of the named region via
[remove_first_L1]. So the output [R2'] depends on the *list
structure* of [R2], not just its membership.
Concretely: legacy outputs are not threaded ([R_out] = [R_in]
typing-wise), so legacy never needs to transport an *output* —
only inputs. L1's R-threading exposes this gap.
The [T_Region_Active_L1]-shadowed sub-case of
[region_shrink_preserves_typing_l1_gen] below requires bridging
a body derivation from [R] to [remove_first r R] (in some
sub-case where [In r (remove_first r R)], i.e., R has [r] twice
or more). The bridge would need to preserve outputs *with
list-structure agreement*, which set-equivalence (and even
multiset-equivalence in some sub-sub-cases) does not provide.
Resolution: this sub-case remains an internal [admit] in
[region_shrink_preserves_typing_l1_gen_m] (the m-polymorphic
helper). The Linear wrapper [_gen] and the value-restricted
wrapper [region_shrink_preserves_typing_l1] (used by
[preservation_l1]'s S_Region_Exit case) both still depend on
it: an earlier note here suggested the value-wrapper could
bypass [_gen_m] by induction on [is_value], but that fails on
[VLam] — the lambda body is a non-value whose internal
[ERegion r' e'] subterm may shadow the outer [r], hitting the
same structural residual. Closure options are listed in the
case's own comment within [_gen_m]. *)
(** Auxiliary general L1 region-shrinkage lemma — no [is_value] or
[~ In r (free_regions T)] premises, mirroring the legacy
[Semantics.region_shrink_preserves_typing]. The L1
region-permutation infrastructure (analog of [region_env_perm_typing]
and [region_add_typing]) is not yet built; the [T_Region_L1] and
[T_Region_Active_L1] sub-cases need them. The two [admit]s inside
this helper are the genuine residual blocking the [T_Lam_L1_Linear] case
of the targeted lemma below.
Once the L1 permutation infrastructure lands, this helper closes to
Qed and the targeted lemma follows in one line. *)
(** ** Modality-polymorphic generalisation.
Mirrors the L2-β pattern used by [subst_typing_gen_l1_m] and
[shift_typing_gen_l1_m]: the structural shrinkage lemma is
mode-blind (region operations don't depend on the modality),
so we prove it once at arbitrary [m] and derive the Linear
specialisation as a wrapper.
One genuine residual [admit] remains — the [T_Region_Active_L1]
[rr = r] shadowed sub-case, documented as the list-vs-multiset
bridge in PROOF-NEEDS.md §2 and in the design-note comment
above this lemma block. The L1 region-permutation infrastructure
(analog of [region_env_perm_typing] and [region_add_typing])
is needed to close it; bridging options are listed in the
case's own comment. *)
(** Migrated to [expr_strictly_free_of_region] as the precondition
(blocker 5 reformulation, 2026-05-28). The strict predicate gives
the body strictly MORE information than the weak one — in
particular, the [T_Region_*_L1] cases no longer rely on a
shadow-short-circuited [True] in the [rr = r] subcase. The
residual [admit] at the [T_Region_Active_L1] [rr = r] sub-case
is blocked by a list-vs-multiset structural mismatch (Phase D
work, NOT by predicate weakness — strengthening the predicate
does not close it). The [admit] remains; see the case's own
comment for resolution options. *)
Lemma region_shrink_preserves_typing_l1_gen_m :
forall m R G e T R' G',
R ; G |=L1[m] e : T -| R' ; G' ->
forall r,
expr_strictly_free_of_region r e ->
remove_first r R ; G |=L1[m] e : T -| remove_first r R' ; G'.
Proof.
intros m R G e T R' G' Ht.
induction Ht; intros rr Hfree; simpl in Hfree.
- (* T_Unit_L1 *) apply T_Unit_L1.
- (* T_Bool_L1 *) apply T_Bool_L1.
- (* T_I32_L1 *) apply T_I32_L1.
- (* T_Var_Lin_L1 *) eapply T_Var_Lin_L1; eauto.
- (* T_Var_Unr_L1 *) eapply T_Var_Unr_L1; eauto.
- (* T_Loc_L1 *)
apply T_Loc_L1.
apply remove_first_preserves_other; [exact Hfree | exact H].
- (* T_StringNew_L1 *)
apply T_StringNew_L1.
apply remove_first_preserves_other; [exact Hfree | exact H].
- (* T_StringConcat_L1 *)
destruct Hfree as [Hf1 Hf2].
eapply T_StringConcat_L1; [eapply IHHt1; auto | eapply IHHt2; auto].
- (* T_StringLen_L1 *)
eapply T_StringLen_L1. eapply IHHt; auto.
- (* T_Let_L1 *)
destruct Hfree as [Hf1 Hf2].
eapply T_Let_L1; [eapply IHHt1; auto | eapply IHHt2; auto].
- (* T_LetLin_L1 *)
destruct Hfree as [Hf1 Hf2].
eapply T_LetLin_L1; [exact H | eapply IHHt1; auto | eapply IHHt2; auto].
- (* T_Lam_L1_Linear *)
apply T_Lam_L1_Linear. eapply IHHt; auto.
- (* T_Lam_L1_Affine *)
eapply T_Lam_L1_Affine. eapply IHHt; auto.
- (* T_Lam_L1_Linear_Eff — body's R_in is separate from outer R, but
the side condition [forall r, In r R -> In r R_in] survives
outer shrinkage (smaller outer R ⊆ original R ⊆ R_in). The body
typing at R_in is unchanged. *)
eapply T_Lam_L1_Linear_Eff; [|eassumption].
intros r0 Hin0. apply H. eapply remove_first_subset. exact Hin0.
- (* T_Lam_L1_Affine_Eff — same pattern. *)
eapply T_Lam_L1_Affine_Eff; [|eassumption].
intros r0 Hin0. apply H. eapply remove_first_subset. exact Hin0.
- (* T_App_L1 *)
destruct Hfree as [Hf1 Hf2].
eapply T_App_L1; [eapply IHHt1; auto | eapply IHHt2; auto].
- (* T_Pair_L1 *)
destruct Hfree as [Hf1 Hf2].
eapply T_Pair_L1; [eapply IHHt1; auto | eapply IHHt2; auto].
- (* T_Fst_L1 *)
eapply T_Fst_L1; [eapply IHHt; auto | exact H].
- (* T_Snd_L1 *)
eapply T_Snd_L1; [eapply IHHt; auto | exact H].
- (* T_Inl_L1 *)
eapply T_Inl_L1. eapply IHHt; auto.
- (* T_Inr_L1 *)
eapply T_Inr_L1. eapply IHHt; auto.
- (* T_Case_L1_Linear *)
destruct Hfree as [Hf1 [Hf2 Hf3]].
eapply T_Case_L1_Linear;
[eapply IHHt1; auto | eapply IHHt2; auto | eapply IHHt3; auto].
- (* T_Case_L1_Affine *)
destruct Hfree as [Hf1 [Hf2 Hf3]].
eapply T_Case_L1_Affine;
[eapply IHHt1; auto | eapply IHHt2; auto | eapply IHHt3; auto].
- (* T_If_L1_Linear *)
destruct Hfree as [Hf1 [Hf2 Hf3]].
eapply T_If_L1_Linear;
[eapply IHHt1; auto | eapply IHHt2; auto | eapply IHHt3; auto].
- (* T_If_L1_Affine *)
destruct Hfree as [Hf1 [Hf2 Hf3]].
eapply T_If_L1_Affine;
[eapply IHHt1; auto | eapply IHHt2; auto | eapply IHHt3; auto].
- (* T_Region_L1: ERegion r e (fresh r).
Shadowed sub-case (rr = r) closes via count-monotonicity
vacuity; descend sub-case (rr <> r) via the
remove_first/remove_first_L1 commutation rewrite. *)
destruct (String.eqb rr r) eqn:Heq.
+ (* rr = r: shadowed. Hfree is True (irrelevant). *)
apply String.eqb_eq in Heq. subst r.
rewrite (remove_first_not_in_id _ _ H).
destruct (in_dec string_dec rr (remove_first_L1 rr R_body)) as [Hin | Hnotin].
* (* Multiple rr's in R_body — VACUOUS by count monotonicity.
Body is typed at (rr :: R) with ~In rr R, so the body input
has count_occ rr = 1. By count_occ_le_l1_m, count_occ rr R_body
<= 1. But Hin says count_occ rr R_body >= 2. Contradiction. *)
exfalso.
pose proof (count_occ_le_l1_m _ _ _ _ _ _ _ Ht rr) as Hle.
unfold cnt in Hle. simpl in Hle.
destruct (string_dec rr rr) as [_|Hbad]; [|apply Hbad; reflexivity].
apply (count_occ_In string_dec) in Hin.
pose proof (remove_first_L1_count_eq_self rr R_body) as Hself.
unfold cnt in Hself. rewrite Hself in Hin.
apply (count_occ_not_In string_dec) in H.
lia.
* (* At most one rr in R_body. Then
remove_first rr (remove_first_L1 rr R_body) =
remove_first_L1 rr R_body. *)
rewrite (remove_first_not_in_id _ _ Hnotin).
eapply T_Region_L1; eauto.
+ (* rr <> r: descend. *)
apply String.eqb_neq in Heq.
assert (Hgoal_eq : remove_first rr (remove_first_L1 r R_body) =
remove_first_L1 r (remove_first rr R_body)).
{ rewrite (remove_first_eq_l1 r R_body).
rewrite (remove_first_eq_l1 r (remove_first rr R_body)).
apply remove_first_comm. }
rewrite Hgoal_eq.
eapply T_Region_L1.
* intro Hin. apply H. eapply remove_first_subset; exact Hin.
* exact H0.
* apply remove_first_preserves_other; [intro Hbad; apply Heq; exact Hbad | exact H1].
* (* Strict-predicate migration: [Hfree] is uniformly the body's
strict-freedom (no conditional to discharge). Still need
[simpl in IHHt] to unfold [remove_first rr (r :: R)] into
[r :: remove_first rr R] via the [rr <> r] guard. *)
specialize (IHHt rr Hfree).
simpl in IHHt.
destruct (String.eqb rr r) eqn:Heq2.
-- exfalso. apply String.eqb_eq in Heq2. apply Heq. exact Heq2.
-- exact IHHt.
- (* T_Region_Active_L1: ERegion r e (r currently active). *)
destruct (String.eqb rr r) eqn:Heq.
+ (* rr = r: shadowed; Hfree True. RESIDUAL — structural
list-vs-multiset mismatch documented in PROOF-NEEDS.md §2
and in the design-note comment above this lemma block.
Bridging requires either:
(a) a stronger L1 perm lemma that produces specifically-shaped
outputs (requires reformulating output as a function of
input + derivation shape), or
(b) a reformulation of [remove_first_L1] to be multiset-based
(lose ordering), or
(c) a redesign of the L1 [T_Region_*_L1] rules to not depend on
first-occurrence semantics. *)
admit.
+ (* rr <> r: descend. *)
apply String.eqb_neq in Heq.
assert (Hgoal_eq : remove_first rr (remove_first_L1 r R_body) =
remove_first_L1 r (remove_first rr R_body)).
{ rewrite (remove_first_eq_l1 r R_body).
rewrite (remove_first_eq_l1 r (remove_first rr R_body)).
apply remove_first_comm. }
rewrite Hgoal_eq.
eapply T_Region_Active_L1.
* apply remove_first_preserves_other; [intro Hbad; apply Heq; exact Hbad | exact H].
* exact H0.
* apply remove_first_preserves_other; [intro Hbad; apply Heq; exact Hbad | exact H1].
* eapply IHHt. exact Hfree.
- (* T_Region_L1_Echo — structurally identical to T_Region_L1
modulo the output-type [TEcho T] vs [T]. Both subcases close
directly (shadowed via count-monotonicity vacuity, descend
via remove_first/remove_first_L1 commutation). Closed in
slice 4 to honor the "no parallel-rule admit-debt" seam-audit
directive — the original T_Region_L1 case has zero admits,
so its mirror has zero admits too. *)
destruct (String.eqb rr r) eqn:Heq.
+ (* rr = r: shadowed. *)
apply String.eqb_eq in Heq. subst r.
rewrite (remove_first_not_in_id _ _ H).
destruct (in_dec string_dec rr (remove_first_L1 rr R_body)) as [Hin | Hnotin].
* (* Multiple rr's in R_body — VACUOUS by count monotonicity. *)
exfalso.
pose proof (count_occ_le_l1_m _ _ _ _ _ _ _ Ht rr) as Hle.
unfold cnt in Hle. simpl in Hle.
destruct (string_dec rr rr) as [_|Hbad]; [|apply Hbad; reflexivity].
apply (count_occ_In string_dec) in Hin.
pose proof (remove_first_L1_count_eq_self rr R_body) as Hself.
unfold cnt in Hself. rewrite Hself in Hin.
apply (count_occ_not_In string_dec) in H.
lia.
* (* At most one rr in R_body. *)
rewrite (remove_first_not_in_id _ _ Hnotin).
eapply T_Region_L1_Echo; eauto.
+ (* rr <> r: descend. *)
apply String.eqb_neq in Heq.
assert (Hgoal_eq : remove_first rr (remove_first_L1 r R_body) =
remove_first_L1 r (remove_first rr R_body)).
{ rewrite (remove_first_eq_l1 r R_body).
rewrite (remove_first_eq_l1 r (remove_first rr R_body)).
apply remove_first_comm. }
rewrite Hgoal_eq.
eapply T_Region_L1_Echo.
* intro Hin. apply H. eapply remove_first_subset; exact Hin.
* exact H0.
* apply remove_first_preserves_other; [intro Hbad; apply Heq; exact Hbad | exact H1].
* (* Strict-predicate migration: same as T_Region_L1 above —
[simpl in IHHt] still needed to unfold [remove_first]. *)
specialize (IHHt rr Hfree).
simpl in IHHt.
destruct (String.eqb rr r) eqn:Heq2.
-- exfalso. apply String.eqb_eq in Heq2. apply Heq. exact Heq2.
-- exact IHHt.
- (* T_Region_Active_L1_Echo — parallels T_Region_Active_L1.
The descend sub-case closes directly; the shadowed sub-case
(rr = r) is the same list-vs-multiset structural residual as
in T_Region_Active_L1 above. Per slice 4 seam audit: the
shadowed sub-case admit is a TRUE MIRROR of the pre-existing
L1 structural admit at line 553. The descend sub-case is
closed Qed-style in slice 4 (not an admit). *)
destruct (String.eqb rr r) eqn:Heq.
+ (* rr = r: shadowed — list-vs-multiset mirror of line 553.
Resolution path is identical to its non-Echo counterpart:
option (a) L1 perm lemma, (b) multiset reformulation, or
(c) T_Region_*_L1 redesign. See line 542-552 design note. *)
admit.
+ (* rr <> r: descend. *)
apply String.eqb_neq in Heq.
assert (Hgoal_eq : remove_first rr (remove_first_L1 r R_body) =
remove_first_L1 r (remove_first rr R_body)).
{ rewrite (remove_first_eq_l1 r R_body).
rewrite (remove_first_eq_l1 r (remove_first rr R_body)).
apply remove_first_comm. }
rewrite Hgoal_eq.
eapply T_Region_Active_L1_Echo.
* apply remove_first_preserves_other; [intro Hbad; apply Heq; exact Hbad | exact H].
* exact H0.
* apply remove_first_preserves_other; [intro Hbad; apply Heq; exact Hbad | exact H1].
* eapply IHHt. exact Hfree.
- (* T_Borrow_L1 *)
eapply T_Borrow_L1. exact H.
- (* T_Borrow_Val_L1 *)
eapply T_Borrow_Val_L1; [exact H | eapply IHHt; auto].
- (* T_Drop_L1 *)
eapply T_Drop_L1; [exact H | eapply IHHt; auto].
- (* T_Drop_L1_Echo — parallel rule; identical proof shape (output
type is [TEcho T] instead of [TBase TUnit], otherwise the same). *)
eapply T_Drop_L1_Echo; [exact H | eapply IHHt; auto].
- (* T_Copy_L1 *)
eapply T_Copy_L1; [exact H | eapply IHHt; auto].
- (* T_Echo_L1 — region shrink under an echo value preserves the
value structure (region-free witness implies a region-free echo). *)
eapply T_Echo_L1.
+ exact H.
+ eapply IHHt; auto.
- (* T_Observe_L1 — region shrink commutes with the witness sub-expression *)
eapply T_Observe_L1. eapply IHHt; auto.
Admitted.
Lemma region_shrink_preserves_typing_l1_gen :
forall R G e T R' G',
has_type_l1_linear R G e T R' G' ->
forall r,
expr_strictly_free_of_region r e ->
has_type_l1_linear (remove_first r R) G e T (remove_first r R') G'.
Proof.
intros R G e T R' G' Ht r Hfree.
apply (region_shrink_preserves_typing_l1_gen_m Linear); auto.
Qed.
Lemma region_shrink_preserves_typing_l1 :
forall R G v T R' G' r,
is_value v ->
has_type_l1_linear R G v T R' G' ->
~ In r (free_regions T) ->
expr_strictly_free_of_region r v ->
has_type_l1_linear (remove_first r R) G v T (remove_first r R') G'.
Proof.
intros R G v T R' G' r Hv Ht HnotT Hfree.
eapply region_shrink_preserves_typing_l1_gen; eauto.
Qed.
(** ** Helper: substitution preserves the L1 typing.
Mirrors [Semantics.subst_preserves_typing] under the L1 judgment.
Used by the β-reduction cases ([S_Let_Val], [S_LetLin_Val],
[S_App_Fun], [S_Case_Inl], [S_Case_Inr]) of [preservation_l1].
STATEMENT NOTE: The original drafted statement quantified [v]'s
typing at a separate [R1] independent of the body's [R2]. That
statement is unsound: with [v = ELoc l r : TString r], [R1 = [r]],
[R2 = []], and an [e2] that consumes the bound variable, the
conclusion would need to type [ELoc l r] at [R2 = []] via
[T_Loc_L1] (which requires [In r R2 = False]). The statement here
is strengthened to type [v] at the same [R2; G2] as the body's
input. This is exactly what is needed by the β-reduction cases of
[preservation_l1]: after [value_R_G_preserving_l1] establishes
R/G invariance of values, [v] typed at the outer (R, G) coincides
with the body's intermediate (R2, G2). *)
(** ===== L1 infrastructure lemmas =====
These mirror analogous lemmas in [Semantics.v] for the legacy
judgment but are stated and proved for [has_type_l1]. *)
(** Length preserved by every typing rule.
L2-β: generalised to modality-polymorphic [has_type_l1 m]. The
length invariant is modality-independent (every constructor
threads ctx length identically). Linear-specific callers
continue to work via instantiation. *)
Lemma typing_preserves_length_l1 :
forall m R G e T R' G',
has_type_l1 m R G e T R' G' ->
length G' = length G.
Proof.
intros m R G e T R' G' H.
induction H; simpl in *; try reflexivity; try lia;
try (rewrite ctx_mark_used_length; reflexivity).
Qed.
(** Output-context lookup preserves type at the same index.
L2-β restoration 2026-05-27: body ported from commit 56f592f.
Generalised in subst_typing_gen_l1 PR to modality-polymorphic
so sub-derivations at variable [m] from a polymorphic compound
rule (e.g. T_App_L1) can be threaded through. The body is
unchanged — every constructor's case discharges identically
regardless of modality; the explicit bullets cover the seven
compound cases that need IH composition. *)
Lemma typing_preserves_bindings_l1 :
forall m R G e T R' G',
has_type_l1 m R G e T R' G' ->
forall i T0 u0,
ctx_lookup G' i = Some (T0, u0) ->
exists u1, ctx_lookup G i = Some (T0, u1).
Proof.
intros m R G e T R' G' Htype.
induction Htype; intros idx Ty uf Hlk; simpl in *;
try (eexists; exact Hlk);
try (eapply IHHtype; exact Hlk).
- (* T_Var_Lin_L1: G' = ctx_mark_used G i *)
eapply ctx_mark_used_lookup_type. exact Hlk.
- (* T_StringConcat_L1 *)
destruct (IHHtype2 _ _ _ Hlk) as [u_mid Hu_mid].
eapply IHHtype1. exact Hu_mid.
- (* T_Let_L1 *)
destruct (IHHtype2 (S idx) Ty uf) as [u_mid Hu_mid]; [exact Hlk|].
eapply IHHtype1. exact Hu_mid.
- (* T_LetLin_L1 *)
destruct (IHHtype2 (S idx) Ty uf) as [u_mid Hu_mid]; [exact Hlk|].
eapply IHHtype1. exact Hu_mid.
- (* T_App_L1 *)
destruct (IHHtype2 _ _ _ Hlk) as [u_mid Hu_mid].
eapply IHHtype1. exact Hu_mid.
- (* T_Pair_L1 *)
destruct (IHHtype2 _ _ _ Hlk) as [u_mid Hu_mid].
eapply IHHtype1. exact Hu_mid.
- (* T_Case_L1_Linear *)
destruct (IHHtype2 (S idx) Ty uf) as [u_mid Hu_mid]; [exact Hlk|].
eapply IHHtype1. exact Hu_mid.
- (* T_Case_L1_Affine: same shape — branch 2 ignored; branch 1 carries through *)
destruct (IHHtype2 (S idx) Ty uf) as [u_mid Hu_mid]; [exact Hlk|].
eapply IHHtype1. exact Hu_mid.
- (* T_If_L1_Linear *)
destruct (IHHtype2 _ _ _ Hlk) as [u_mid Hu_mid].
eapply IHHtype1. exact Hu_mid.
- (* T_If_L1_Affine *)
destruct (IHHtype2 _ _ _ Hlk) as [u_mid Hu_mid].
eapply IHHtype1. exact Hu_mid.
Qed.
(** Unrestricted (non-linear) bindings are unchanged through typing.
L2-β restoration 2026-05-27: body ported from commit 56f592f with
new bullets for T_Case_L1_Affine + T_If_L1_Affine. T_Lam_L1_Affine
auto-discharges through [try exact Hlk] (R/G unchanged at the
rule's conclusion). *)
Lemma unrestricted_flag_unchanged_l1 :
forall R G e T R' G',
R; G |=L1 e : T -| R' ; G' ->
forall j T0 u,
is_linear_ty T0 = false ->
ctx_lookup G j = Some (T0, u) ->
ctx_lookup G' j = Some (T0, u).
Proof.
intros R G e T R' G' Htype.
induction Htype; intros idx T0 u0 Hnlin Hlk; simpl in *;
try exact Hlk;
try (eapply IHHtype; eassumption).
- (* T_Var_Lin_L1 *)
destruct (Nat.eq_dec i idx) as [->|Hne].
+ unfold ctx_lookup in *. rewrite H in Hlk. injection Hlk as <- <-.
rewrite Hnlin in H0. discriminate.
+ rewrite ctx_mark_used_lookup_other by exact Hne. exact Hlk.
- (* T_StringConcat_L1 *)
eapply IHHtype2; [exact Hnlin|]. eapply IHHtype1; eassumption.
- (* T_Let_L1 *)
apply (IHHtype1 idx T0 u0 Hnlin) in Hlk.
assert (HlkS: ctx_lookup (ctx_extend G' T1) (S idx) = Some (T0, u0)) by exact Hlk.
apply (IHHtype2 (S idx) T0 u0 Hnlin) in HlkS.
unfold ctx_lookup, ctx_extend in HlkS. simpl in HlkS. exact HlkS.
- (* T_LetLin_L1 *)
apply (IHHtype1 idx T0 u0 Hnlin) in Hlk.
assert (HlkS: ctx_lookup (ctx_extend G' T1) (S idx) = Some (T0, u0)) by exact Hlk.
apply (IHHtype2 (S idx) T0 u0 Hnlin) in HlkS.
unfold ctx_lookup, ctx_extend in HlkS. simpl in HlkS. exact HlkS.
- (* T_App_L1 *)
eapply IHHtype2; [exact Hnlin|]. eapply IHHtype1; eassumption.
- (* T_Pair_L1 *)
eapply IHHtype2; [exact Hnlin|]. eapply IHHtype1; eassumption.
- (* T_Case_L1_Linear *)
apply (IHHtype1 idx T0 u0 Hnlin) in Hlk.
assert (HlkS: ctx_lookup (ctx_extend G' T1) (S idx) = Some (T0, u0)) by exact Hlk.
apply (IHHtype2 (S idx) T0 u0 Hnlin) in HlkS.
unfold ctx_lookup, ctx_extend in HlkS. simpl in HlkS. exact HlkS.
- (* T_Case_L1_Affine — same shape *)
apply (IHHtype1 idx T0 u0 Hnlin) in Hlk.
assert (HlkS: ctx_lookup (ctx_extend G' T1) (S idx) = Some (T0, u0)) by exact Hlk.
apply (IHHtype2 (S idx) T0 u0 Hnlin) in HlkS.
unfold ctx_lookup, ctx_extend in HlkS. simpl in HlkS. exact HlkS.
- (* T_If_L1_Linear *)
eapply IHHtype2; [exact Hnlin|]. eapply IHHtype1; eassumption.
- (* T_If_L1_Affine *)
eapply IHHtype2; [exact Hnlin|]. eapply IHHtype1; eassumption.
Qed.
(** If a false-flag binding becomes true, the type must be linear. *)
Lemma flag_false_to_true_implies_linear_l1 :
forall R G e T R' G' k T1,
R; G |=L1 e : T -| R' ; G' ->
nth_error G k = Some (T1, false) ->
nth_error G' k = Some (T1, true) ->
is_linear_ty T1 = true.
Proof.
intros R G e T R' G' k T1 Htype Hin Hout.
destruct (is_linear_ty T1) eqn:Hlin; [reflexivity | exfalso].
pose proof (unrestricted_flag_unchanged_l1 _ _ _ _ _ _ Htype k T1 false Hlin
ltac:(unfold ctx_lookup; exact Hin)) as H.
unfold ctx_lookup in H. rewrite H in Hout. discriminate.
Qed.
(** Output context shape at arbitrary position. *)
Lemma output_shape_at_l1 :
forall m R Gin e T R' Gout k T1 u_in,
has_type_l1 m R Gin e T R' Gout ->
nth_error Gin k = Some (T1, u_in) ->
exists u_out, nth_error Gout k = Some (T1, u_out).
Proof.
intros m R Gin e T R' Gout k T1 u_in Htype Hin.
assert (Hlen := typing_preserves_length_l1 _ _ _ _ _ _ _ Htype).
assert (Hlt: k < length Gout).
{ rewrite Hlen. apply nth_error_Some. congruence. }
destruct (nth_error Gout k) as [[T1' u']|] eqn:E.
- destruct (typing_preserves_bindings_l1 _ _ _ _ _ _ _ Htype k T1' u') as [u1 Hu1].
{ unfold ctx_lookup. exact E. }
unfold ctx_lookup in Hu1. rewrite Hin in Hu1.
assert (T1' = T1) by congruence. subst T1'.
eexists. reflexivity.
- apply nth_error_None in E. lia.
Qed.
(** Values preserve R and G under the L1 judgment.
Inductive on [is_value]; closes via direct inversion of each
[T_*_L1] value rule and IH composition for compound values. *)
Lemma value_R_G_invariant_l1 :
forall v, is_value v ->
forall R G T R' G',
R; G |=L1 v : T -| R' ; G' ->
R' = R /\ G' = G.
Proof.
intros v Hval. induction Hval; intros R0 G0 Tx R'x G'x Htype;
inversion Htype; subst; try (split; reflexivity).
- (* VPair *)
match goal with
| [ H1 : _; _ |=L1 v1 : _ -| _ ; _,
H2 : _; _ |=L1 v2 : _ -| _ ; _ |- _ ] =>
destruct (IHHval1 _ _ _ _ _ H1) as [-> ->];
destruct (IHHval2 _ _ _ _ _ H2) as [-> ->]
end. split; reflexivity.
- (* VInl *) eapply IHHval. eassumption.
- (* VInr *) eapply IHHval. eassumption.
(* VBorrow: both T_Borrow_L1 and T_Borrow_Val_L1 have output = input,
so [try (split; reflexivity)] above closes both cases. *)
Qed.
(** Canonical form: a value of [TString r] is a location. *)
Lemma canonical_string_l1 :
forall R G v r R' G',
R; G |=L1 v : TString r -| R' ; G' ->
is_value v ->
exists l, v = ELoc l r.
Proof. intros; inversion H0; subst; inversion H; subst; eauto. Qed.
(** Linear values are exactly locations, with the region in R. *)
Lemma linear_value_is_loc_l1 :
forall R G v T,
R; G |=L1 v : T -| R ; G ->
is_value v ->
is_linear_ty T = true ->
exists l r, v = ELoc l r /\ T = TString r /\ In r R.
Proof.
intros R G v T Htype Hval Hlin.
destruct T; simpl in Hlin; try discriminate.
- (* TString *)
destruct (canonical_string_l1 _ _ _ _ _ _ Htype Hval) as [l ->].
inversion Htype; subst. exists l, r. auto.
- (* TRef Lin _ — no value has this type at L1 *)
destruct l; [|discriminate].
exfalso. inversion Hval; subst; inversion Htype.
- (* TRegion — no value has this type *)
exfalso. inversion Hval; subst; inversion Htype.
Qed.
(** ===== remove_at / insert_at (re-using Semantics.v definitions) ===== *)
(** insert_at and remove_at are already defined in Semantics.v; we import them
transparently via [From Ephapax Require Import Semantics] above. The
relevant lemmas — [nth_error_insert_at_*], [nth_error_remove_at_*],
[remove_at_ctx_mark_used_*], [remove_at_mark_used_self], [insert_at_*],
[remove_insert_cancel] — are reused below. *)
(** Shift typing for L1: shifting [e] up by 1 at cutoff [k] in the
context corresponds to inserting a fresh (T_new, false) at position
[k] in both input and output. R is unchanged at every rule.
L2-β restoration 2026-05-27: generalised to modality-polymorphic
[has_type_l1 m] mirroring [region_liveness_at_split_l1_gen]'s
shape. The Linear-only wrapper [shift_typing_gen_l1] preserves
the original signature for callers. Body ported from commit
56f592f with new bullets for T_Lam_L1_Affine, T_Case_L1_Affine,
T_If_L1_Affine — each Affine case mirrors its Linear counterpart
since R and `insert_at` threading are modality-independent. *)
Lemma shift_typing_gen_l1_m :
forall m R G e T R' G',
has_type_l1 m R G e T R' G' ->
forall k T_new,
k <= length G ->
has_type_l1 m R (insert_at k (T_new, false) G) (shift k 1 e) T R' (insert_at k (T_new, false) G').
Proof.
intros m R G e T R' G' Htype.
induction Htype; intros k T_new Hk; simpl.
- apply T_Unit_L1.
- apply T_Bool_L1.
- apply T_I32_L1.
(* T_Var_Lin_L1 *)
- destruct (Nat.leb_spec k i).
+ rewrite (insert_at_ctx_mark_used_ge G k i (T_new, false) H1 Hk).
replace (i + 1) with (S i) by lia.
eapply T_Var_Lin_L1.
* unfold ctx_lookup. rewrite nth_error_insert_at_gt by (try assumption; try lia).
unfold ctx_lookup in H. exact H.
* exact H0.
+ rewrite (insert_at_ctx_mark_used_lt G k i (T_new, false) H1 Hk).
eapply T_Var_Lin_L1.
* unfold ctx_lookup. rewrite nth_error_insert_at_lt by (try assumption; try lia).
unfold ctx_lookup in H. exact H.
* exact H0.
(* T_Var_Unr_L1 *)
- destruct (Nat.leb_spec k i).
+ replace (i + 1) with (S i) by lia. eapply T_Var_Unr_L1.
* unfold ctx_lookup. rewrite nth_error_insert_at_gt by (try assumption; try lia).
unfold ctx_lookup in H. exact H.
* exact H0.
+ eapply T_Var_Unr_L1.
* unfold ctx_lookup. rewrite nth_error_insert_at_lt by (try assumption; try lia).
unfold ctx_lookup in H. exact H.
* exact H0.
(* T_Loc_L1 *)
- apply T_Loc_L1. exact H.
(* T_StringNew_L1 *)
- apply T_StringNew_L1. exact H.
(* T_StringConcat_L1 *)
- eapply T_StringConcat_L1; [apply IHHtype1; assumption|].
apply IHHtype2. assert (Hlen := typing_preserves_length_l1 _ _ _ _ _ _ _ Htype1). lia.
(* T_StringLen_L1 *)
- eapply T_StringLen_L1. apply IHHtype. assumption.
(* T_Let_L1 *)
- assert (IH1 := IHHtype1 k T_new Hk).
assert (Hlen := typing_preserves_length_l1 _ _ _ _ _ _ _ Htype1).