-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeSystem.v
More file actions
792 lines (737 loc) · 19.2 KB
/
TypeSystem.v
File metadata and controls
792 lines (737 loc) · 19.2 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
(******************************************************************************)
(* Copyright (c) 2019--2026 - Paulo Torrens <paulotorrens AT gnu DOT org> *)
(******************************************************************************)
Require Import Lia.
Require Import Arith.
Require Import Relations.
Require Import Equality.
Require Import Local.Prelude.
Require Import Local.Substitution.
Require Import Local.Syntax.
Require Import Local.Context.
Require Import Local.Metatheory.
Require Import Local.Equational.
Require Import Local.Reduction.
Require Import Local.Observational.
Import ListNotations.
(** ** Type system *)
Definition env: Set :=
list pseudoterm.
Inductive simple: pseudoterm -> Prop :=
| simple_base:
simple base
| simple_negation:
forall ts,
Forall simple ts -> simple (negation ts).
Notation valid_env g :=
(Forall simple g).
(* We are free to take a simpler definition here since we're only dealing with
simple types. Reasoning about dependent types will require much more care! *)
Inductive typing: env -> relation pseudoterm :=
| typing_bound:
forall g n t,
valid_env g ->
item t g n ->
typing g n t
| typing_jump:
forall g k xs ts,
typing g k (negation ts) ->
Forall2 (typing g) xs ts ->
typing g (jump k xs) void
| typing_bind:
forall g b ts c,
typing (negation ts :: g) b void ->
typing (ts ++ g) c void ->
typing g (bind b ts c) void.
Global Hint Constructors typing: cps.
Local Goal
let G := [base; base; base; negation [base; base]] in
typing G ex1 void.
Proof.
compute.
repeat econstructor.
Qed.
Local Goal
let G := [base; base; base; negation [base; base]] in
typing G ex2 void.
Proof.
compute.
repeat econstructor.
Qed.
Local Goal
let G := [base; base; base; negation [base; base]] in
typing G ex3 void.
Proof.
compute.
repeat econstructor.
Qed.
Lemma valid_env_typing:
forall g e t,
typing g e t ->
valid_env g.
Proof.
induction 1.
- assumption.
- assumption.
- dependent destruction IHtyping1.
assumption.
Qed.
Lemma typing_bound_is_simple:
forall g n t,
typing g (bound n) t ->
simple t.
Proof.
intros.
dependent destruction H.
replace t with (nth n g void).
- apply Forall_nth.
+ assumption.
+ apply item_valid_index with t.
assumption.
- apply nth_item.
assumption.
Qed.
Lemma typing_bound_cant_be_void:
forall g n,
~typing g (bound n) void.
Proof.
intros g n ?.
apply typing_bound_is_simple in H.
inversion H.
Qed.
Lemma simple_types_ignore_substitution:
forall t,
simple t ->
forall s,
inst s t = t.
Proof.
induction t using pseudoterm_deepind; intros.
- inversion H.
- dependent destruction H0.
+ reflexivity.
+ sigma; f_equal.
induction H; auto.
dependent destruction H0.
simpl; f_equal.
* sigma; auto.
* auto.
- exfalso.
inversion H0.
- exfalso.
inversion H0.
Qed.
Lemma simple_env_ignores_lift:
forall ts,
valid_env ts ->
forall i k,
traverse_list (lift i) k ts = ts.
Proof.
induction 1; simpl; intros.
- reflexivity.
- f_equal; auto.
rewrite traverse_list_length.
sigma.
now apply simple_types_ignore_substitution.
Qed.
Lemma simple_env_ignores_subst:
forall ts,
valid_env ts ->
forall y k,
traverse_list (subst y) k ts = ts.
Proof.
induction 1; simpl; intros.
- reflexivity.
- f_equal; auto.
rewrite traverse_list_length.
sigma.
now apply simple_types_ignore_substitution.
Qed.
Lemma simple_types_are_closed:
forall t,
simple t ->
forall k,
not_free k t.
Proof.
induction t using pseudoterm_deepind; intros.
- exfalso.
inversion H.
- dependent destruction H0.
+ clear H.
repeat constructor.
+ constructor.
generalize dependent k.
induction ts; intros.
* constructor.
* dependent destruction H.
dependent destruction H1.
constructor; auto.
- exfalso.
inversion H0.
- exfalso.
inversion H0.
Qed.
Lemma not_free_list_negation:
forall ts,
valid_env ts ->
forall k,
not_free_list NEGATION k ts.
Proof.
intros.
enough (not_free k (negation ts)).
- dependent destruction H0.
assumption.
- apply simple_types_are_closed.
now constructor.
Qed.
Lemma not_free_typing:
forall b g t,
typing g b t ->
forall k,
k >= length g ->
not_free k b.
Proof.
induction b using pseudoterm_deepind; intros.
- dependent destruction H.
apply item_valid_index in H0.
constructor.
lia.
- inversion H0.
- dependent destruction H0.
constructor.
+ eapply IHb; eauto.
+ clear H0 IHb.
generalize dependent ts.
induction H; intros.
* constructor.
* dependent destruction H1.
constructor; eauto.
- dependent destruction H0.
constructor.
+ eapply IHb1; eauto.
simpl; lia.
+ apply valid_env_typing in H0_.
dependent destruction H0_.
dependent destruction H0.
clear H IHb1 IHb2 H0_ H0_0 H1 b1 b2 g.
(* Of course, simple types never have free variables! *)
now apply not_free_list_negation.
+ eapply IHb2; eauto.
rewrite app_length; lia.
Qed.
(* -------------------------------------------------------------------------- *)
Section Structural.
Variable R: env -> pseudoterm -> Prop.
Definition WEAKENING: Prop :=
forall g c,
R g c ->
forall t,
simple t ->
R (t :: g) (lift 1 0 c).
Definition EXCHANGE: Prop :=
forall g c,
R g c ->
forall n h,
switch n g h ->
R h (switch_bindings n c).
Definition CONTRACTION: Prop :=
forall g c t,
R (t :: t :: g) c ->
R (t :: g) (subst (bound 0) 0 c).
End Structural.
(* -------------------------------------------------------------------------- *)
Lemma valid_env_insert:
forall ts,
Forall simple ts ->
forall n g h,
insert ts n g h ->
valid_env g <-> valid_env h.
Proof.
induction 2; split; intros.
- apply Forall_app.
now split.
- clear H.
induction ts; simpl in H0.
+ assumption.
+ dependent destruction H0.
now apply IHts.
- dependent destruction H1.
constructor.
+ assumption.
+ now apply IHinsert.
- dependent destruction H1.
constructor.
+ assumption.
+ now apply IHinsert.
Qed.
Local Hint Resolve -> valid_env_insert: cps.
Lemma typing_lift:
forall e g t,
typing g e t ->
forall us k,
valid_env us ->
forall h,
insert us k g h ->
typing h (lift (length us) k e) t.
Proof.
induction e using pseudoterm_deepind; intros.
- dependent destruction H.
destruct (le_gt_dec k n).
+ rewrite lift_bound_ge; try lia.
constructor; eauto with cps.
eapply item_insert_ge; eauto.
+ rewrite lift_bound_lt; try lia.
constructor; eauto with cps.
eapply item_insert_lt; eauto.
- inversion H0.
- dependent destruction H0.
rewrite lift_distributes_over_jump.
econstructor.
+ eapply IHe; eauto.
+ clear IHe H0.
generalize dependent ts.
induction xs; intros.
* simpl in H, H1 |- *.
dependent destruction H1.
constructor.
* simpl in H, H1 |- *.
dependent destruction H.
dependent destruction H1.
constructor; eauto.
- dependent destruction H0.
rewrite lift_distributes_over_bind.
constructor.
+ replace (negation (traverse_list (lift (length us)) k ts)) with
(negation ts).
* eapply IHe1; eauto.
now constructor.
* f_equal.
apply valid_env_typing in H0_.
dependent destruction H0_.
dependent destruction H0.
now rewrite simple_env_ignores_lift.
+ replace (traverse_list (lift (length us)) k ts) with ts.
* eapply IHe2; eauto.
rewrite Nat.add_comm.
now apply insert_app.
* apply valid_env_typing in H0_.
dependent destruction H0_.
dependent destruction H0.
now rewrite simple_env_ignores_lift.
Qed.
Theorem weakening:
WEAKENING (fun g c => typing g c void).
Proof.
intros g c ? t ?.
replace 1 with (length [t]) by auto.
apply typing_lift with g.
- assumption.
- auto.
- constructor.
Qed.
(* -------------------------------------------------------------------------- *)
Lemma valid_env_switch_bindings:
forall n g h,
switch n g h ->
valid_env g <-> valid_env h.
Proof.
induction 1; split; intros.
- dependent destruction H.
dependent destruction H0.
firstorder.
- dependent destruction H.
dependent destruction H0.
firstorder.
- dependent destruction H0.
firstorder.
- dependent destruction H0.
firstorder.
Qed.
Local Hint Resolve -> valid_env_switch_bindings: cps.
Lemma typing_switch_bindings:
forall e g t,
typing g e t ->
forall n h,
switch n g h ->
typing h (switch_bindings n e) t.
Proof.
induction e using pseudoterm_deepind; intros.
- rename n0 into m.
dependent destruction H.
(* TODO: there's gotta be a better way... *)
assert ((m > n) \/ (m = n) \/ (S m = n) \/ (S m < n))
as [ ? | [ ? | [ ? | ? ] ] ] by lia.
+ unfold switch_bindings.
sigma.
constructor; eauto with cps.
admit.
+ dependent destruction H2.
unfold switch_bindings.
sigma.
constructor; eauto with cps.
admit.
+ dependent destruction H2.
unfold switch_bindings.
sigma.
constructor; eauto with cps.
admit.
+ unfold switch_bindings.
sigma.
replace (S (S (m + n)) - m - 2) with n by lia.
constructor; eauto with cps.
admit.
- inversion H0.
- dependent destruction H0.
rewrite switch_bindings_distributes_over_jump.
econstructor.
+ apply IHe with g; eauto.
+ clear IHe H0.
generalize dependent ts.
induction xs; intros.
* simpl in H, H1 |- *.
dependent destruction H1.
constructor.
* simpl in H, H1 |- *.
dependent destruction H.
dependent destruction H1.
constructor; eauto.
- dependent destruction H0.
rewrite switch_bindings_distributes_over_bind.
constructor.
+ apply IHe1 with (negation ts :: g); eauto.
replace (negation (traverse_list switch_bindings n ts))
with (negation ts).
* constructor.
assumption.
* f_equal.
apply valid_env_typing in H0_.
dependent destruction H0_.
dependent destruction H0.
(* From H0. *)
admit.
+ apply IHe2 with (ts ++ g); eauto.
rewrite Nat.add_comm.
replace (traverse_list switch_bindings n ts) with ts.
* apply switch_app.
assumption.
* apply valid_env_typing in H0_.
dependent destruction H0_.
dependent destruction H0.
(* From H0. *)
admit.
Admitted.
Theorem exchange:
EXCHANGE (fun g c => typing g c void).
Proof.
intros g c ? n h ?.
apply typing_switch_bindings with g.
- assumption.
- assumption.
Qed.
(* -------------------------------------------------------------------------- *)
Lemma valid_env_join:
forall n g h,
join n g h ->
valid_env g <-> valid_env h.
Proof.
induction 1; split; intros.
- dependent destruction H.
assumption.
- dependent destruction H.
firstorder.
- dependent destruction H0.
firstorder.
- dependent destruction H0.
firstorder.
Qed.
Local Hint Resolve -> valid_env_join: cps.
Lemma typing_subst0:
forall e g t,
typing g e t ->
forall n h,
join n g h ->
typing h (subst (bound 0) n e) t.
Proof.
induction e using pseudoterm_deepind; intros.
- rename n0 into m.
dependent destruction H.
destruct (lt_eq_lt_dec m n) as [ [ ? | ? ] | ? ].
+ rewrite subst_bound_gt; try lia.
constructor; eauto with cps.
admit.
+ dependent destruction e.
rewrite subst_bound_eq; try lia.
rewrite lift_bound_ge; try lia.
replace (m + 0) with m; try lia.
constructor; eauto with cps.
admit.
+ rewrite subst_bound_lt; try lia.
constructor; eauto with cps.
admit.
- inversion H0.
- dependent destruction H0.
rewrite subst_distributes_over_jump.
econstructor.
+ apply IHe with g; eauto.
+ clear IHe H0.
generalize dependent ts.
induction xs; intros.
* simpl in H, H1 |- *.
dependent destruction H1.
constructor.
* simpl in H, H1 |- *.
dependent destruction H.
dependent destruction H1.
constructor; eauto.
- dependent destruction H0.
rewrite subst_distributes_over_bind.
constructor.
+ apply IHe1 with (negation ts :: g); eauto.
replace (negation (traverse_list (subst (bound 0)) n ts)) with
(negation ts).
* constructor.
assumption.
* f_equal.
apply valid_env_typing in H0_.
dependent destruction H0_.
dependent destruction H0.
now rewrite simple_env_ignores_subst.
+ apply IHe2 with (ts ++ g); eauto.
rewrite Nat.add_comm.
replace (traverse_list (subst (bound 0)) n ts) with ts.
* apply join_app.
assumption.
* apply valid_env_typing in H0_.
dependent destruction H0_.
dependent destruction H0.
now rewrite simple_env_ignores_subst.
Admitted.
Theorem contraction:
CONTRACTION (fun g c => typing g c void).
Proof.
intros g c t ?.
apply typing_subst0 with (t :: t :: g).
- assumption.
- constructor.
Qed.
(* -------------------------------------------------------------------------- *)
Lemma typing_implies_correct_arity:
forall (h: context) ts g xs,
typing (negation ts :: g) (h (jump #h xs)) void ->
length xs = length ts.
Proof.
(* Of course we have to generalize our induction a bit. *)
assert (forall (h: context) n ts g xs,
item (negation ts) g n ->
typing g (h (jump (n + #h) xs)) void ->
length xs = length ts).
- induction h; simpl; intros.
+ rewrite Nat.add_comm in H0; simpl in H0.
dependent destruction H0.
dependent destruction H0.
rename ts0 into us.
assert (negation ts = negation us).
* eapply item_unique; eauto.
* dependent destruction H3; clear H1.
apply Forall2_length in H2.
assumption.
+ rename ts0 into us.
dependent destruction H0.
apply IHh with (S n) (negation ts :: g).
* auto with cps.
* replace (S n + #h) with (n + S #h); try lia.
assumption.
+ rename ts0 into us.
dependent destruction H0.
apply IHh with (length ts + n) (ts ++ g).
* rewrite Nat.add_comm.
apply item_insert_head.
assumption.
* replace (length ts + n + #h) with (n + (#h + length ts)); try lia.
assumption.
(* There we go, now we can show it. *)
- intros.
eapply H with (g := negation ts :: g) (n := 0).
+ constructor.
+ eassumption.
Qed.
Theorem progress:
forall g b,
typing g b void ->
(exists k, converges b k) \/ (exists c, head b c).
Proof.
intros.
dependent induction H.
- exfalso.
eapply typing_bound_cant_be_void with g n.
constructor; auto.
- clear IHtyping.
dependent destruction H.
left; exists n.
constructor.
- clear IHtyping2.
destruct IHtyping1 as [ (k, ?) | (d, ?) ]; auto.
+ destruct k.
* dependent destruction H1 using converges_inv; simpl.
right; eexists.
apply head_longjmp with (r := context_hole); auto with cps.
eapply typing_implies_correct_arity; eauto.
* left; exists k.
constructor; auto.
+ right; eexists.
apply head_bind_left; eauto.
Qed.
(* -------------------------------------------------------------------------- *)
Lemma valid_env_drop:
forall k n g h,
drop k n g h ->
valid_env g -> valid_env h.
Proof.
induction 1; intros.
- assumption.
- dependent destruction H0.
firstorder.
- dependent destruction H0.
firstorder.
Qed.
Local Hint Resolve valid_env_drop: cps.
Lemma typing_remove_binding:
forall e g t,
typing g e t ->
forall k h,
drop k 1 g h ->
not_free k e ->
typing h (remove_binding k e) t.
Proof.
induction e using pseudoterm_deepind; intros.
- dependent destruction H.
unfold remove_binding.
destruct (lt_eq_lt_dec k n) as [ [ ? | ? ] | ? ].
+ rewrite subst_bound_gt; try lia.
constructor; eauto with cps.
admit.
+ exfalso.
dependent destruction H2.
contradiction.
+ rewrite subst_bound_lt; try lia.
constructor; eauto with cps.
admit.
- inversion H0.
- unfold remove_binding.
dependent destruction H0.
dependent destruction H3.
rewrite subst_distributes_over_jump.
econstructor.
+ apply IHe with g; eauto.
+ clear IHe H0 H3.
generalize dependent ts.
induction xs; intros.
* simpl in H, H1, H4 |- *.
dependent destruction H1.
constructor.
* simpl in H, H1, H4 |- *.
dependent destruction H.
dependent destruction H1.
dependent destruction H4.
constructor; eauto.
- unfold remove_binding.
dependent destruction H0.
rewrite subst_distributes_over_bind.
constructor.
+ apply IHe1 with (negation ts :: g); eauto.
replace (negation (traverse_list (subst (bound 0)) k ts)) with
(negation ts).
* constructor.
assumption.
* f_equal.
apply valid_env_typing in H0_.
dependent destruction H0_.
dependent destruction H0.
now rewrite simple_env_ignores_subst.
* dependent destruction H2.
assumption.
+ apply IHe2 with (ts ++ g); eauto.
rewrite Nat.add_comm.
replace (traverse_list (subst (bound 0)) k ts) with ts.
* apply drop_app.
assumption.
* apply valid_env_typing in H0_.
dependent destruction H0_.
dependent destruction H0.
now rewrite simple_env_ignores_subst.
* dependent destruction H2.
rewrite Nat.add_comm.
assumption.
Admitted.
Lemma typing_smol:
forall b c,
smol b c ->
forall g,
typing g b void ->
typing g c void.
Proof.
induction 1; intros.
- dependent destruction H0.
eapply typing_remove_binding.
+ eassumption.
+ repeat constructor.
+ assumption.
- dependent destruction H0.
constructor; auto.
- dependent destruction H0.
constructor; auto.
Qed.
Lemma typing_beta:
forall b c,
beta b c ->
forall g,
typing g b void ->
typing g c void.
Proof.
induction 1; intros.
- dependent destruction H0.
constructor; auto.
(* Hmmm... *)
admit.
- dependent destruction H0.
constructor; auto.
- dependent destruction H0.
constructor; auto.
Admitted.
Theorem subject_reduction:
forall b c,
step b c ->
forall g,
typing g b void ->
typing g c void.
Proof.
intros.
apply step_characterization in H.
destruct H.
- apply typing_beta with b; auto.
- apply typing_smol with b; auto.
Qed.
(* -------------------------------------------------------------------------- *)
Example subject_expansion_failure:
(* We do NOT have subject expansion! *)
exists g b c,
beta b c /\ typing g c void /\ ~typing g b void.
Proof.
(*
k: ~() |- j<j> { j<x> = k<> } ---> k: ~() |- k<> { j<x> = k<> }
*)
exists [negation []].
exists (bind (jump 0 [bound 0]) [base] (jump 1 [])).
exists (bind (jump 1 []) [base] (jump 1 [])).
repeat split.
- apply beta_ctxjmp with (h := context_hole).
reflexivity.
- repeat econstructor.
- intros ?.
dependent destruction H.
dependent destruction H.
dependent destruction H.
dependent destruction H0.
dependent destruction H1.
dependent destruction H0.
dependent destruction H1.
Qed.