-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFsub_LetSum_Definitions.v
1346 lines (1248 loc) · 48.6 KB
/
Fsub_LetSum_Definitions.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(** Definition of Fsub (System F with subtyping).
Authors: Brian Aydemir and Arthur Chargu\'eraud, with help from
Aaron Bohannon, Jeffrey Vaughan, and Dimitrios Vytiniotis.
Table of contents:
- #<a href="##syntax">Syntax (pre-terms)</a>#
- #<a href="##open">Opening</a>#
- #<a href="##lc">Local closure</a>#
- #<a href="##env">Environments</a>#
- #<a href="##wf">Well-formedness</a>#
- #<a href="##sub">Subtyping</a>#
- #<a href="##typing_doc">Typing</a>#
- #<a href="##values">Values</a>#
- #<a href="##reduction">Reduction</a>#
- #<a href="##auto">Automation</a>#
*)
Require Export Fsub.RefImmut.LabelMap.
Require Export Fsub.RefImmut.Label.
Require Export Fsub.RefImmut.Signatures.
Require Export FqMeta.Metatheory.
Require Export String.
(* ********************************************************************** *)
(** * #<a name="syntax"></a># Syntax (pre-terms) *)
(** We use a locally nameless representation for Fsub, where bound
variables are represented as natural numbers (de Bruijn indices)
and free variables are represented as [atom]s. The type [atom],
defined in the MetatheoryAtom library, represents names: there are
infinitely many atoms, equality is decidable on atoms, and it is
possible to generate an atom fresh for any given finite set of
atoms.
We say that the definitions below define pre-types ([typ]) and
pre-expressions ([exp]), collectively pre-terms, since the
datatypes admit terms, such as [(typ_all typ_top (typ_bvar 3))],
where indices are unbound. A term is locally closed when it
contains no unbound indices.
Similarly, in addition to pre-types and pre-expressions, we define
pre-qualifiers for representing qualifier terms, with possibly
unbound indices.
Note that indices for bound type variables are distinct from
indices for bound expression variables. We make this explicit in
the definitions below of the opening operations. *)
(** A [qua], a qualifier term, consists of either top, bottom,
variables, meets, and joins. *)
Inductive qua : Set :=
| qua_top : qua
| qua_bvar : nat -> qua
| qua_fvar : atom -> qua
| qua_meet : qua -> qua -> qua
| qua_join : qua -> qua -> qua
| qua_bot : qua
.
(** For brevity we use [`readonly] and [`mutable] to denote top/bottom qualifiers
in this calculus. *)
Notation "`readonly" := qua_top.
Notation "`mutable" := qua_bot.
Inductive typ : Set :=
| typ_top : typ
| typ_bvar : nat -> typ
| typ_fvar : atom -> typ
| typ_arrow : qtyp -> qtyp -> typ
| typ_all : typ -> qtyp -> typ
| typ_qall : qua -> qtyp -> typ
| typ_sum : qtyp -> qtyp -> typ
| typ_pair : qtyp -> qtyp -> typ
| typ_ref : qtyp -> typ
with qtyp : Set :=
| qtyp_qtyp : qua -> typ -> qtyp
.
Inductive exp : Set :=
| exp_bvar : nat -> exp
| exp_fvar : atom -> exp
| exp_abs : qua -> qtyp -> exp -> exp
| exp_app : exp -> exp -> exp
| exp_tabs : qua -> typ -> exp -> exp
| exp_tapp : exp -> typ -> exp
| exp_qabs : qua -> qua -> exp -> exp
| exp_qapp : exp -> qua -> exp
| exp_let : exp -> exp -> exp
| exp_inl : qua -> exp -> exp
| exp_inr : qua -> exp -> exp
| exp_case : exp -> exp -> exp -> exp
| exp_pair : qua -> exp -> exp -> exp
| exp_first : exp -> exp
| exp_second : exp -> exp
| exp_ref : qua -> exp -> exp
| exp_ref_label : qua -> label -> exp
| exp_deref : exp -> exp
| exp_set_ref : exp -> exp -> exp
| exp_upqual : qua -> exp -> exp
| exp_check : qua -> exp -> exp
.
(** We declare the constructors for indices and variables to be
coercions. For example, if Coq sees a [nat] where it expects an
[exp], it will implicitly insert an application of [exp_bvar];
similar behavior happens for [atom]s. Thus, we may write
[(exp_abs typ_top (exp_app 0 x))] instead of [(exp_abs typ_top
(exp_app (exp_bvar 0) (exp_fvar x)))]. *)
Coercion typ_bvar : nat >-> typ.
Coercion typ_fvar : atom >-> typ.
Coercion exp_bvar : nat >-> exp.
Coercion exp_fvar : atom >-> exp.
Coercion qua_bvar : nat >-> qua.
Coercion qua_fvar : atom >-> qua.
(* ********************************************************************** *)
(** * #<a name="concrete"></a># Concrete Qualifiers *)
(** A concrete qualifier, at runtime, is either only top or bot. *)
Inductive concrete_qua : Set := cqua_top | cqua_bot.
(** [concretize] partially evaluates a [qua] into a [concrete_qua]. *)
Fixpoint concretize (q : qua) : (option concrete_qua) :=
match q with
| qua_top => Some cqua_top
| qua_fvar X => None
| qua_bvar n => None
| qua_join Q1 Q2 =>
match (concretize Q1) with
| Some cqua_top => Some cqua_top
| Some cqua_bot => (concretize Q2)
| None => None
end
| qua_meet Q1 Q2 =>
match (concretize Q1) with
| Some cqua_top => (concretize Q2)
| Some cqua_bot => Some cqua_bot
| None => None
end
| qua_bot => Some cqua_bot
end.
(** [abstractize] goes the other way, giving us a qualifier term from
a concrete, runtime qualifier. *)
Definition abstractize (s : concrete_qua) :=
match s with
| cqua_top => qua_top
| cqua_bot => qua_bot
end.
(** [cqua_compatible] represents the simple linear order on the two-point
binary lattice: bot <= top. *)
Inductive cqua_compatible : concrete_qua -> concrete_qua -> Prop :=
| cqua_compatible_same : forall s, cqua_compatible s s
| cqua_compatible_up : cqua_compatible cqua_bot cqua_top.
Notation "s ≤ t" := (cqua_compatible s t) (at level 70).
(* ********************************************************************** *)
(** * #<a name="open"></a># Opening terms *)
(** Opening replaces an index with a term. This operation is required
if we wish to work only with locally closed terms when going under
binders (e.g., the typing rule for [exp_abs]). It also
corresponds to informal substitution for a bound variable, which
occurs in the rule for beta reduction.
We need to define three functions for opening due the syntax of
Fsub, and we name them according to the following convention.
- [tt]: Denotes an operation involving types appearing in types.
- [te]: Denotes an operation involving types appearing in
expressions.
- [ee]: Denotes an operation involving expressions appearing in
expressions.
The notation used below for decidable equality on natural numbers
(e.g., [K == J]) is defined in the CoqEqDec library, which is
included by the Metatheory library. The order of arguments to
each "open" function is the same. For example, [(open_tt_rec K U
T)] can be read as "substitute [U] for index [K] in [T]."
Note that we assume [U] is locally closed (and similarly for the
other opening functions). This assumption simplifies the
implementations of opening by letting us avoid shifting. Since
bound variables are indices, there is no need to rename variables
to avoid capture. Finally, we assume that these functions are
initially called with index zero and that zero is the only unbound
index in the term. This eliminates the need to possibly subtract
one in the case of indices. *)
Fixpoint open_qq_rec (K : nat) (R : qua) (Q : qua) {struct Q} : qua :=
match Q with
| qua_top => qua_top
| qua_bvar J => if (K == J) then R else (qua_bvar J)
| qua_fvar X => qua_fvar X
| qua_meet Q1 Q2 => qua_meet (open_qq_rec K R Q1) (open_qq_rec K R Q2)
| qua_join Q1 Q2 => qua_join (open_qq_rec K R Q1) (open_qq_rec K R Q2)
| qua_bot => qua_bot
end.
Fixpoint open_tt_rec (K : nat) (U : typ) (T : typ) {struct T} : typ :=
match T with
| typ_top => typ_top
| typ_bvar J => if K == J then U else (typ_bvar J)
| typ_fvar X => typ_fvar X
| typ_arrow T1 T2 => typ_arrow (open_tqt_rec K U T1) (open_tqt_rec (S K) U T2)
| typ_all T1 T2 => typ_all (open_tt_rec K U T1) (open_tqt_rec (S K) U T2)
| typ_qall Q T => typ_qall Q (open_tqt_rec (S K) U T)
| typ_sum T1 T2 => typ_sum (open_tqt_rec K U T1) (open_tqt_rec K U T2)
| typ_pair T1 T2 => typ_pair (open_tqt_rec K U T1) (open_tqt_rec K U T2)
| typ_ref T1 => typ_ref (open_tqt_rec K U T1)
end
with open_tqt_rec (K : nat) (U : typ) (T : qtyp) {struct T} : qtyp :=
match T with
| qtyp_qtyp Q T => qtyp_qtyp Q (open_tt_rec K U T)
end.
Fixpoint open_qt_rec (K : nat) (R : qua) (T : typ) {struct T} : typ :=
match T with
| typ_top => typ_top
| typ_bvar J => typ_bvar J
| typ_fvar X => typ_fvar X
| typ_arrow T1 T2 => typ_arrow (open_qqt_rec K R T1) (open_qqt_rec (S K) R T2)
| typ_all T1 T2 => typ_all (open_qt_rec K R T1) (open_qqt_rec (S K) R T2)
| typ_qall Q T => typ_qall (open_qq_rec K R Q) (open_qqt_rec (S K) R T)
| typ_sum T1 T2 => typ_sum (open_qqt_rec K R T1) (open_qqt_rec K R T2)
| typ_pair T1 T2 => typ_pair (open_qqt_rec K R T1) (open_qqt_rec K R T2)
| typ_ref T1 => typ_ref (open_qqt_rec K R T1)
end
with open_qqt_rec (K : nat) (R : qua) (T : qtyp) {struct T} : qtyp :=
match T with
| qtyp_qtyp Q T => qtyp_qtyp (open_qq_rec K R Q) (open_qt_rec K R T)
end.
Fixpoint open_te_rec (K : nat) (U : typ) (e : exp) {struct e} : exp :=
match e with
| exp_bvar i => exp_bvar i
| exp_fvar x => exp_fvar x
| exp_abs P V e1 => exp_abs P (open_tqt_rec K U V) (open_te_rec (S K) U e1)
| exp_app e1 e2 => exp_app (open_te_rec K U e1) (open_te_rec K U e2)
| exp_tabs P V e1 => exp_tabs P (open_tt_rec K U V) (open_te_rec (S K) U e1)
| exp_tapp e1 V => exp_tapp (open_te_rec K U e1) (open_tt_rec K U V)
| exp_qabs P Q e1 => exp_qabs P Q (open_te_rec (S K) U e1)
| exp_qapp e1 Q => exp_qapp (open_te_rec K U e1) Q
| exp_let e1 e2 => exp_let (open_te_rec K U e1) (open_te_rec (S K) U e2)
| exp_inl P e1 => exp_inl P (open_te_rec K U e1)
| exp_inr P e2 => exp_inr P (open_te_rec K U e2)
| exp_case e1 e2 e3 =>
exp_case (open_te_rec K U e1) (open_te_rec (S K) U e2) (open_te_rec (S K) U e3)
| exp_pair R e1 e2 => exp_pair R (open_te_rec K U e1) (open_te_rec K U e2)
| exp_first e1 => exp_first (open_te_rec K U e1)
| exp_second e2 => exp_second (open_te_rec K U e2)
| exp_ref P e1 => exp_ref P (open_te_rec K U e1)
| exp_ref_label P l => exp_ref_label P l
| exp_deref e1 => exp_deref (open_te_rec K U e1)
| exp_set_ref e1 e2 => exp_set_ref (open_te_rec K U e1) (open_te_rec K U e2)
| exp_check P e1 => exp_check P (open_te_rec K U e1)
| exp_upqual P e1 => exp_upqual P (open_te_rec K U e1)
end.
Fixpoint open_qe_rec (K : nat) (R : qua) (e : exp) {struct e} : exp :=
match e with
| exp_bvar i => exp_bvar i
| exp_fvar x => exp_fvar x
| exp_abs P V e1 => exp_abs (open_qq_rec K R P) (open_qqt_rec K R V) (open_qe_rec (S K) R e1)
| exp_app e1 e2 => exp_app (open_qe_rec K R e1) (open_qe_rec K R e2)
| exp_tabs P V e1 => exp_tabs (open_qq_rec K R P) (open_qt_rec K R V) (open_qe_rec (S K) R e1)
| exp_tapp e1 V => exp_tapp (open_qe_rec K R e1) (open_qt_rec K R V)
| exp_qabs P Q e1 => exp_qabs (open_qq_rec K R P) (open_qq_rec K R Q) (open_qe_rec (S K) R e1)
| exp_qapp e1 Q => exp_qapp (open_qe_rec K R e1) (open_qq_rec K R Q)
| exp_let e1 e2 => exp_let (open_qe_rec K R e1) (open_qe_rec (S K) R e2)
| exp_inl P e1 => exp_inl (open_qq_rec K R P) (open_qe_rec K R e1)
| exp_inr P e2 => exp_inr (open_qq_rec K R P) (open_qe_rec K R e2)
| exp_case e1 e2 e3 =>
exp_case (open_qe_rec K R e1) (open_qe_rec (S K) R e2) (open_qe_rec (S K) R e3)
| exp_pair P e1 e2 => exp_pair (open_qq_rec K R P) (open_qe_rec K R e1) (open_qe_rec K R e2)
| exp_first e1 => exp_first (open_qe_rec K R e1)
| exp_second e2 => exp_second (open_qe_rec K R e2)
| exp_ref P e1 => exp_ref (open_qq_rec K R P) (open_qe_rec K R e1)
| exp_ref_label P l => exp_ref_label (open_qq_rec K R P) l
| exp_deref e1 => exp_deref (open_qe_rec K R e1)
| exp_set_ref e1 e2 => exp_set_ref (open_qe_rec K R e1) (open_qe_rec K R e2)
| exp_check P e1 => exp_check (open_qq_rec K R P) (open_qe_rec K R e1)
| exp_upqual P e1 => exp_upqual (open_qq_rec K R P) (open_qe_rec K R e1)
end.
Fixpoint open_ee_rec (k : nat) (f : exp) (e : exp) {struct e} : exp :=
match e with
| exp_bvar i => if k == i then f else (exp_bvar i)
| exp_fvar x => exp_fvar x
| exp_abs P V e1 => exp_abs P V (open_ee_rec (S k) f e1)
| exp_app e1 e2 => exp_app (open_ee_rec k f e1) (open_ee_rec k f e2)
| exp_tabs P V e1 => exp_tabs P V (open_ee_rec (S k) f e1)
| exp_tapp e1 V => exp_tapp (open_ee_rec k f e1) V
| exp_qabs P Q e1 => exp_qabs P Q (open_ee_rec (S k) f e1)
| exp_qapp e1 Q => exp_qapp (open_ee_rec k f e1) Q
| exp_let e1 e2 => exp_let (open_ee_rec k f e1) (open_ee_rec (S k) f e2)
| exp_inl P e1 => exp_inl P (open_ee_rec k f e1)
| exp_inr P e2 => exp_inr P (open_ee_rec k f e2)
| exp_case e1 e2 e3 =>
exp_case (open_ee_rec k f e1)
(open_ee_rec (S k) f e2)
(open_ee_rec (S k) f e3)
| exp_pair P e1 e2 => exp_pair P (open_ee_rec k f e1) (open_ee_rec k f e2)
| exp_first e1 => exp_first (open_ee_rec k f e1)
| exp_second e2 => exp_second (open_ee_rec k f e2)
| exp_ref P e1 => exp_ref P (open_ee_rec k f e1)
| exp_ref_label P l => exp_ref_label P l
| exp_deref e1 => exp_deref (open_ee_rec k f e1)
| exp_set_ref e1 e2 => exp_set_ref (open_ee_rec k f e1) (open_ee_rec k f e2)
| exp_check P e1 => exp_check P (open_ee_rec k f e1)
| exp_upqual P e1 => exp_upqual P (open_ee_rec k f e1)
end.
(** Many common applications of opening replace index zero with an
expression or variable. The following definitions provide
convenient shorthands for such uses. Note that the order of
arguments is switched relative to the definitions above. For
example, [(open_tt T X)] can be read as "substitute the variable
[X] for index [0] in [T]" and "open [T] with the variable [X]."
Recall that the coercions above let us write [X] in place of
[(typ_fvar X)], assuming that [X] is an [atom]. *)
Definition open_tt T U := open_tt_rec 0 U T.
Definition open_te e U := open_te_rec 0 U e.
Definition open_ee e1 e2 := open_ee_rec 0 e2 e1.
Definition open_qq Q R := open_qq_rec 0 R Q.
Definition open_qe e R := open_qe_rec 0 R e.
Definition open_qt T R := open_qt_rec 0 R T.
Definition open_qqt T R := open_qqt_rec 0 R T.
Definition open_tqt T R := open_tqt_rec 0 R T.
(* ********************************************************************** *)
(** * #<a name="lc"></a># Local closure *)
(** Recall that [typ] and [exp] define pre-terms; these datatypes
admit terms that contain unbound indices. A term is locally
closed, or syntactically well-formed, when no indices appearing in
it are unbound. The proposition [(type T)] holds when a type [T]
is locally closed, and [(expr e)] holds when an expression [e] is
locally closed.
The inductive definitions below formalize local closure such that
the resulting induction principles serve as structural induction
principles over (locally closed) types and (locally closed)
expressions. In particular, unlike the situation with pre-terms,
there are no cases for indices. Thus, these induction principles
correspond more closely to informal practice than the ones arising
from the definitions of pre-terms.
The interesting cases in the inductive definitions below are those
that involve binding constructs, e.g., [typ_all]. Intuitively, to
check if the pre-term [(typ_all T1 T2)] is locally closed, we must
check that [T1] is locally closed and that [T2] is locally closed
when opened with a variable. However, there is a choice as to how
many variables to quantify over. One possibility is to quantify
over only one variable ("existential" quantification), as in
<<
type_all : forall X T1 T2,
type T1 ->
type (open_tt T2 X) ->
type (typ_all T1 T2) .
>> Or, we could quantify over as many variables as possible ("universal"
quantification), as in
<<
type_all : forall T1 T2,
type T1 ->
(forall X : atom, type (open_tt T2 X)) ->
type (typ_all T1 T2) .
>> It is possible to show that the resulting relations are equivalent.
The former makes it easy to build derivations, while the latter
provides a strong induction principle. McKinna and Pollack used
both forms of this relation in their work on formalizing Pure Type
Systems.
We take a different approach here and use "cofinite"
quantification: we quantify over all but finitely many variables.
This approach provides a convenient middle ground: we can build
derivations reasonably easily and get reasonably strong induction
principles. With some work, one can show that the definitions
below are equivalent to ones that use existential, and hence also
universal, quantification. *)
Inductive qual : qua -> Prop :=
| qual_top :
qual qua_top
| qual_fvar : forall (X : atom),
qual (qua_fvar X)
| qual_meet : forall Q1 Q2,
qual Q1 ->
qual Q2 ->
qual (qua_meet Q1 Q2)
| qual_join : forall Q1 Q2,
qual Q1 ->
qual Q2 ->
qual (qua_join Q1 Q2)
| qual_bot :
qual qua_bot
.
Inductive type : typ -> Prop :=
| type_top :
type typ_top
| type_var : forall X,
type (typ_fvar X)
| type_arrow : forall T1 T2,
qtype T1 ->
qtype T2 ->
type (typ_arrow T1 T2)
| type_all : forall L T1 T2,
type T1 ->
(forall X : atom, X `notin` L -> qtype (open_tqt T2 X)) ->
type (typ_all T1 T2)
| type_qall : forall L Q T,
qual Q ->
(forall X : atom, X `notin` L -> qtype (open_qqt T X)) ->
type (typ_qall Q T)
| type_sum : forall T1 T2,
qtype T1 ->
qtype T2 ->
type (typ_sum T1 T2)
| type_pair : forall T1 T2,
qtype T1 ->
qtype T2 ->
type (typ_pair T1 T2)
| type_ref : forall T1,
qtype T1 ->
type (typ_ref T1)
with qtype : qtyp -> Prop :=
| qtype_qtyp : forall Q T,
qual Q ->
type T ->
qtype (qtyp_qtyp Q T)
.
Inductive expr : exp -> Prop :=
| expr_var : forall x,
expr (exp_fvar x)
| expr_abs : forall L P T e1,
qual P ->
qtype T ->
(forall x : atom, x `notin` L -> expr (open_ee e1 x)) ->
expr (exp_abs P T e1)
| expr_app : forall e1 e2,
expr e1 ->
expr e2 ->
expr (exp_app e1 e2)
| expr_tabs : forall L P T e1,
qual P ->
type T ->
(forall X : atom, X `notin` L -> expr (open_te e1 X)) ->
expr (exp_tabs P T e1)
| expr_tapp : forall e1 V,
expr e1 ->
type V ->
expr (exp_tapp e1 V)
| expr_qabs : forall L P Q e1,
qual P ->
qual Q ->
(forall X : atom, X `notin` L -> expr (open_qe e1 X)) ->
expr (exp_qabs P Q e1)
| expr_qapp : forall e1 Q,
expr e1 ->
qual Q ->
expr (exp_qapp e1 Q)
| expr_let : forall L e1 e2,
expr e1 ->
(forall x : atom, x `notin` L -> expr (open_ee e2 x)) ->
expr (exp_let e1 e2)
| expr_inl : forall P e1,
qual P ->
expr e1 ->
expr (exp_inl P e1)
| expr_inr : forall P e1,
qual P ->
expr e1 ->
expr (exp_inr P e1)
| expr_case : forall L e1 e2 e3,
expr e1 ->
(forall x : atom, x `notin` L -> expr (open_ee e2 x)) ->
(forall x : atom, x `notin` L -> expr (open_ee e3 x)) ->
expr (exp_case e1 e2 e3)
| expr_pair : forall P e1 e2,
qual P ->
expr e1 ->
expr e2 ->
expr (exp_pair P e1 e2)
| expr_first : forall e1,
expr e1 ->
expr (exp_first e1)
| expr_second : forall e1,
expr e1 ->
expr (exp_second e1)
| expr_ref : forall P e1,
qual P ->
expr e1 ->
expr (exp_ref P e1)
| expr_deref : forall e1,
expr e1 ->
expr (exp_deref e1)
| expr_ref_label : forall P l,
qual P ->
expr (exp_ref_label P l)
| expr_set_ref : forall e1 e2,
expr e1 ->
expr e2 ->
expr (exp_set_ref e1 e2)
| expr_upqual : forall P e1,
qual P ->
expr e1 ->
expr (exp_upqual P e1)
| expr_check : forall P e1,
qual P ->
expr e1 ->
expr (exp_check P e1)
.
(** #<a name="body_e_doc"></a># We also define what it means to be the
body of an abstraction, since this simplifies slightly the
definition of reduction and subsequent proofs. It is not strictly
necessary to make this definition in order to complete the
development. *)
Definition body_e (e : exp) :=
exists L, forall x : atom, x `notin` L -> expr (open_ee e x).
(* ********************************************************************** *)
(** * #<a name="env"></a># Environments *)
(** In our presentation of System F with subtyping, we use a single
environment for both typing and subtyping assumptions. We
formalize environments by representing them as association lists
(lists of pairs of keys and values) whose keys are atoms.
The Metatheory and MetatheoryEnv libraries provide functions,
predicates, tactics, notations and lemmas that simplify working
with environments. They treat environments as lists of type [list
(atom * A)]. The notation [(x ~ a)] denotes a list consisting of
a single binding from [x] to [a].
Since environments map [atom]s, the type [A] should encode whether
a particular binding is a typing or subtyping assumption. Thus,
we instantiate [A] with the type [binding], defined below. *)
Inductive binding : Set :=
| bind_sub : typ -> binding
| bind_typ : qtyp -> binding
| bind_qua : qua -> binding.
Inductive signature : Set :=
| bind_sig : qtyp -> signature.
(** A binding [(X ~ bind_sub T)] records that a type variable [X] is a
subtype of [T], and a binding [(x ~ bind_typ U)] records that an
expression variable [x] has type [U].
We define an abbreviation [env] for the type of environments, and
an abbreviation [empty] for the empty environment.
Note: Each instance of [Notation] below defines an abbreviation
since the left-hand side consists of a single identifier that is
not in quotes. These abbreviations are used for both parsing (the
left-hand side is equivalent to the right-hand side in all
contexts) and printing (the right-hand side is pretty-printed as
the left-hand side). Since [nil] is normally a polymorphic
constructor whose type argument is implicit, we prefix the name
with "[@]" to signal to Coq that we are going to supply arguments
to [nil] explicitly. *)
Notation env := (list (atom * binding)).
Notation empty := (@nil (atom * binding)).
Notation sig := (list (label * signature)).
Notation sempty := (@nil (label *signature)).
(** #<b>#Examples:#</b># We use a convention where environments are
never built using a cons operation [((x, a) :: E)] where [E] is
non-[nil]. This makes the shape of environments more uniform and
saves us from excessive fiddling with the shapes of environments.
For example, Coq's tactics sometimes distinguish between consing
on a new binding and prepending a one element list, even though
the two operations are convertible with each other.
Consider the following environments written in informal notation.
<<
1. (empty environment)
2. x : T
3. x : T, Y <: S
4. E, x : T, F
>> In the third example, we have an environment that binds an
expression variable [x] to [T] and a type variable [Y] to [S].
In Coq, we would write these environments as follows.
<<
1. empty
2. x ~ bind_typ T
3. Y ~ bind_sub S ++ x ~ bind_typ T
4. F ++ x ~ bind_typ T ++ E
>> The symbol "[++]" denotes list concatenation and associates to the
right. (That notation is defined in Coq's List library.) Note
that in Coq, environments grow on the left, since that is where
the head of a list is. *)
(* ********************************************************************** *)
(** * #<a name="wf"></a># Well-formedness *)
(** A type [T] is well-formed with respect to an environment [E],
denoted [(wf_typ E T)], when [T] is locally-closed and its free
variables are bound in [E]. We need this relation in order to
restrict the subtyping and typing relations, defined below, to
contain only well-formed types. (This relation is missing in the
original statement of the POPLmark Challenge.)
Note: It is tempting to define the premise of [wf_typ_var] as [(X
`in` dom E)], since that makes the rule easier to apply (no need
to guess an instantiation for [U]). Unfortunately, this is
incorrect. We need to check that [X] is bound as a type-variable,
not an expression-variable; [(dom E)] does not distinguish between
the two kinds of bindings. *)
Inductive wf_qua : env -> qua -> Prop :=
| wf_qua_top : forall E,
wf_qua E qua_top
| wf_qua_fvar : forall R E (X : atom),
binds X (bind_qua R) E ->
wf_qua E (qua_fvar X)
| wf_qua_meet : forall E Q1 Q2,
wf_qua E Q1 ->
wf_qua E Q2 ->
wf_qua E (qua_meet Q1 Q2)
| wf_qua_join : forall E Q1 Q2,
wf_qua E Q1 ->
wf_qua E Q2 ->
wf_qua E (qua_join Q1 Q2)
| wf_qua_bot : forall E,
wf_qua E qua_bot
.
Inductive wf_typ : env -> typ -> Prop :=
| wf_typ_top : forall E,
wf_typ E typ_top
| wf_typ_var : forall U E (X : atom),
binds X (bind_sub U) E ->
wf_typ E (typ_fvar X)
| wf_typ_arrow : forall E T1 T2,
wf_qtyp E T1 ->
wf_qtyp E T2 ->
wf_typ E (typ_arrow T1 T2)
| wf_typ_all : forall L E T1 T2,
wf_typ E T1 ->
(forall X : atom, X `notin` L ->
wf_qtyp (X ~ bind_sub T1 ++ E) (open_tqt T2 X)) ->
wf_typ E (typ_all T1 T2)
| wf_typ_qall : forall L E T1 T2,
wf_qua E T1 ->
(forall X : atom, X `notin` L ->
wf_qtyp (X ~ bind_qua T1 ++ E) (open_qqt T2 X)) ->
wf_typ E (typ_qall T1 T2)
| wf_typ_sum : forall E T1 T2,
wf_qtyp E T1 ->
wf_qtyp E T2 ->
wf_typ E (typ_sum T1 T2)
| wf_typ_pair : forall E T1 T2,
wf_qtyp E T1 ->
wf_qtyp E T2 ->
wf_typ E (typ_pair T1 T2)
| wf_typ_ref : forall E T,
wf_qtyp E T ->
wf_typ E (typ_ref T)
with wf_qtyp : env -> qtyp -> Prop :=
| wf_qtyp_qtyp : forall E Q T,
wf_qua E Q ->
wf_typ E T ->
wf_qtyp E (qtyp_qtyp Q T)
.
(** An environment [E] is well-formed, denoted [(wf_env E)], if each
atom is bound at most at once and if each binding is to a
well-formed type. This is a stronger relation than the [uniq]
relation defined in the MetatheoryEnv library. We need this
relation in order to restrict the subtyping and typing relations,
defined below, to contain only well-formed environments. (This
relation is missing in the original statement of the POPLmark
Challenge.) *)
Inductive wf_env : env -> Prop :=
| wf_env_empty :
wf_env empty
| wf_env_sub : forall (E : env) (X : atom) (T : typ),
wf_env E ->
wf_typ E T ->
X `notin` dom E ->
wf_env (X ~ bind_sub T ++ E)
| wf_env_typ : forall (E : env) (x : atom) (T : qtyp),
wf_env E ->
wf_qtyp E T ->
x `notin` dom E ->
wf_env (x ~ bind_typ T ++ E)
| wf_env_qua : forall (E : env) (X : atom) (Q : qua),
wf_env E ->
wf_qua E Q ->
X `notin` dom E ->
wf_env (X ~ bind_qua Q ++ E).
Inductive wf_sig : env -> sig -> Prop :=
| wf_sig_empty : forall E,
wf_env E ->
wf_sig E sempty
| wf_sig_typ : forall (E: env) (R: sig) (l: label) (T: qtyp),
wf_sig E R ->
wf_qtyp E T ->
LabelSetImpl.notin l (Signatures.dom R) ->
wf_sig E (l ~ bind_sig T ++ R).
(* ********************************************************************** *)
(** * #<a name="sub"></a># Subtyping *)
(** The definition of subtyping is straightforward. It uses the
[binds] relation from the MetatheoryEnv library (in the
[sub_trans_tvar] case) and cofinite quantification (in the
[sub_all] case).
Subqualification is simply adapted from standard subtyping rules,
and shouldn't appear too surprising as well. *)
Inductive subqual : env -> qua -> qua -> Prop :=
| subqual_top : forall E Q,
wf_env E ->
wf_qua E Q ->
subqual E Q qua_top
| subqual_bot : forall E Q,
wf_env E ->
wf_qua E Q ->
subqual E qua_bot Q
| subqual_refl_qvar : forall E X,
wf_env E ->
wf_qua E (qua_fvar X) ->
subqual E (qua_fvar X) (qua_fvar X)
| subqual_trans_qvar : forall R E Q X,
binds X (bind_qua R) E ->
subqual E R Q ->
subqual E (qua_fvar X) Q
| subqual_join_inl : forall E R1 R2 Q,
subqual E Q R1 ->
wf_qua E R2 ->
subqual E Q (qua_join R1 R2)
| subqual_join_inr : forall E R1 R2 Q,
wf_qua E R1 ->
subqual E Q R2 ->
subqual E Q (qua_join R1 R2)
| subqual_join_elim : forall E R1 R2 Q,
subqual E R1 Q ->
subqual E R2 Q ->
subqual E (qua_join R1 R2) Q
| subqual_meet_eliml : forall E R1 R2 Q,
subqual E R1 Q ->
wf_qua E R2 ->
subqual E (qua_meet R1 R2) Q
| subqual_meet_elimr : forall E R1 R2 Q,
wf_qua E R1 ->
subqual E R2 Q ->
subqual E (qua_meet R1 R2) Q
| subqual_meet_intro : forall E R1 R2 Q,
subqual E Q R1 ->
subqual E Q R2 ->
subqual E Q (qua_meet R1 R2)
.
Notation "E |-q Q <: R" := (subqual E Q R) (at level 70).
Inductive sub : env -> typ -> typ -> Prop :=
| sub_top : forall E S,
wf_env E ->
wf_typ E S ->
sub E S typ_top
| sub_refl_tvar : forall E X,
wf_env E ->
wf_typ E (typ_fvar X) ->
sub E (typ_fvar X) (typ_fvar X)
| sub_trans_tvar : forall U E T X,
binds X (bind_sub U) E ->
sub E U T ->
sub E (typ_fvar X) T
| sub_arrow : forall E S1 S2 T1 T2,
subqtype E T1 S1 ->
subqtype E S2 T2 ->
sub E (typ_arrow S1 S2) (typ_arrow T1 T2)
| sub_all : forall L E S1 S2 T1 T2,
sub E T1 S1 ->
(forall X : atom, X `notin` L ->
subqtype (X ~ bind_sub T1 ++ E) (open_tqt S2 X) (open_tqt T2 X)) ->
sub E (typ_all S1 S2) (typ_all T1 T2)
| sub_qall : forall L E S1 S2 T1 T2,
subqual E T1 S1 ->
(forall X : atom, X `notin` L ->
subqtype (X ~ bind_qua T1 ++ E) (open_qqt S2 X) (open_qqt T2 X)) ->
sub E (typ_qall S1 S2) (typ_qall T1 T2)
| sub_sum : forall E S1 S2 T1 T2,
subqtype E S1 T1 ->
subqtype E S2 T2 ->
sub E (typ_sum S1 S2) (typ_sum T1 T2)
| sub_pair : forall E S1 S2 T1 T2,
subqtype E S1 T1 ->
subqtype E S2 T2 ->
sub E (typ_pair S1 S2) (typ_pair T1 T2)
| sub_ref : forall E S T,
subqtype E S T ->
subqtype E T S ->
sub E (typ_ref S) (typ_ref T)
with subqtype : env -> qtyp -> qtyp -> Prop :=
| sub_qtyp_qtyp : forall E Q1 T1 Q2 T2,
subqual E Q1 Q2 ->
sub E T1 T2 ->
subqtype E (qtyp_qtyp Q1 T1) (qtyp_qtyp Q2 T2)
.
Notation "E |-s S <: T" := (sub E S T) (at level 70).
(* ********************************************************************** *)
(** * #<a name="typing_doc"></a># Typing *)
(** The definition of typing is straightforward. It uses the [binds]
relation from the MetatheoryEnv library (in the [typing_var] case)
and cofinite quantification in the cases involving binders (e.g.,
[typing_abs] and [typing_tabs]). *)
Inductive typing : env -> sig -> exp -> qtyp -> Prop :=
| typing_var : forall E W x T,
wf_env E ->
wf_sig E W ->
binds x (bind_typ T) E ->
typing E W (exp_fvar x) T
| typing_abs : forall L E W P V e1 T1,
wf_sig E W ->
wf_qua E P ->
(forall x : atom, x `notin` L ->
typing (x ~ bind_typ V ++ E) W (open_ee e1 x) T1) ->
typing E W (exp_abs P V e1) (qtyp_qtyp P (typ_arrow V T1))
| typing_app : forall Q T1 E W e1 e2 T2,
typing E W e1 (qtyp_qtyp Q (typ_arrow T1 T2)) ->
typing E W e2 T1 ->
typing E W (exp_app e1 e2) T2
| typing_tabs : forall L E W P V e1 T1,
wf_sig E W ->
wf_qua E P ->
(forall X : atom, X `notin` L ->
typing (X ~ bind_sub V ++ E) W (open_te e1 X) (open_tqt T1 X)) ->
typing E W (exp_tabs P V e1) (qtyp_qtyp P (typ_all V T1))
| typing_tapp : forall Q T1 E W e1 T T2,
typing E W e1 (qtyp_qtyp Q (typ_all T1 T2)) ->
sub E T T1 ->
typing E W (exp_tapp e1 T) (open_tqt T2 T)
| typing_qabs : forall L E W P Q e1 T1,
wf_sig E W ->
wf_qua E P ->
(forall X : atom, X `notin` L ->
typing (X ~ bind_qua Q ++ E) W (open_qe e1 X) (open_qqt T1 X)) ->
typing E W (exp_qabs P Q e1) (qtyp_qtyp P (typ_qall Q T1))
| typing_qapp : forall R Q E W e1 Q1 T,
typing E W e1 (qtyp_qtyp R (typ_qall Q1 T)) ->
subqual E Q Q1 ->
typing E W (exp_qapp e1 Q) (open_qqt T Q)
| typing_sub : forall S E W e T,
typing E W e S ->
subqtype E S T ->
typing E W e T
| typing_let : forall L T1 T2 e1 e2 E W,
typing E W e1 T1 ->
(forall x : atom, x `notin` L ->
typing (x ~ bind_typ T1 ++ E) W (open_ee e2 x) T2) ->
typing E W (exp_let e1 e2) T2
| typing_inl : forall P T1 T2 e1 E W,
wf_qua E P ->
typing E W e1 T1 ->
wf_qtyp E T2 ->
typing E W (exp_inl P e1) (qtyp_qtyp P (typ_sum T1 T2))
| typing_inr : forall P T1 T2 e1 E W,
wf_qua E P ->
typing E W e1 T2 ->
wf_qtyp E T1 ->
typing E W (exp_inr P e1) (qtyp_qtyp P (typ_sum T1 T2))
| typing_case : forall L Q Q1 S1 Q2 S2 T e1 e2 e3 E W,
typing E W e1 (qtyp_qtyp Q (typ_sum (qtyp_qtyp Q1 S1) (qtyp_qtyp Q2 S2))) ->
(forall x : atom, x `notin` L ->
typing (x ~ bind_typ (qtyp_qtyp (qua_join Q Q1) S1) ++ E) W (open_ee e2 x) T) ->
(forall x : atom, x `notin` L ->
typing (x ~ bind_typ (qtyp_qtyp (qua_join Q Q2) S2) ++ E) W (open_ee e3 x) T) ->
typing E W (exp_case e1 e2 e3) T
| typing_pair : forall P T1 T2 e1 e2 E W,
wf_qua E P ->
typing E W e1 T1 ->
typing E W e2 T2 ->
typing E W (exp_pair P e1 e2) (qtyp_qtyp P (typ_pair T1 T2))
| typing_first : forall Q Q1 S1 Q2 S2 e E W,
typing E W e (qtyp_qtyp Q
(typ_pair (qtyp_qtyp Q1 S1) (qtyp_qtyp Q2 S2))) ->
typing E W (exp_first e) (qtyp_qtyp (qua_join Q Q1) S1)
| typing_second : forall Q Q1 S1 Q2 S2 e E W,
typing E W e (qtyp_qtyp Q
(typ_pair (qtyp_qtyp Q1 S1) (qtyp_qtyp Q2 S2))) ->
typing E W (exp_second e) (qtyp_qtyp (qua_join Q Q2) S2)
(* Rules for typing references follow: [typing_ref], [typing_deref], [typing_set_ref],
[typing_ref_label]. *)
| typing_ref : forall E W P e T,
wf_sig E W ->
wf_qua E P ->
typing E W e T ->
typing E W (exp_ref P e) (qtyp_qtyp P (typ_ref T))
| typing_deref : forall E W e Q1 Q2 S,
wf_sig E W ->
typing E W e (qtyp_qtyp Q1 (typ_ref (qtyp_qtyp Q2 S))) ->
typing E W (exp_deref e) (qtyp_qtyp (qua_join Q1 Q2) S)
| typing_set_ref : forall E W e1 e2 T,
wf_sig E W ->
typing E W e1 (qtyp_qtyp qua_bot (typ_ref T)) ->
typing E W e2 T ->
typing E W (exp_set_ref e1 e2) T
| typing_ref_label : forall E W P l T,
wf_env E ->
wf_sig E W ->
wf_qua E P ->
Signatures.binds l (bind_sig T) W ->
typing E W (exp_ref_label P l) (qtyp_qtyp P (typ_ref T))
| typing_upqual : forall E W Q T P e,
wf_qua E P ->
typing E W e (qtyp_qtyp Q T) ->
subqual E Q P ->
typing E W (exp_upqual P e) (qtyp_qtyp P T)
| typing_check : forall E W Q T P e,
wf_qua E P ->
typing E W e (qtyp_qtyp Q T) ->
subqual E Q P ->
typing E W (exp_check P e) (qtyp_qtyp Q T)
.
Notation "E @ W |- e : T" := (typing E W e T) (at level 70).
(* ********************************************************************** *)
(** * #<a name="values"></a># Values *)
(** Unlike standard System F-sub, values now carry a qualifier tag. *)
Inductive value : exp -> Prop :=
| value_abs : forall P T e1,
expr (exp_abs P T e1) ->
value (exp_abs P T e1)
| value_tabs : forall P T e1,
expr (exp_tabs P T e1) ->
value (exp_tabs P T e1)
| value_qabs : forall P Q e1,
expr (exp_qabs P Q e1) ->
value (exp_qabs P Q e1)
| value_inl : forall P e1,
qual P ->
value e1 ->
value (exp_inl P e1)
| value_inr : forall P e1,
qual P ->
value e1 ->
value (exp_inr P e1)
| value_pair : forall P e1 e2,
qual P ->
value e1 ->
value e2 ->
value (exp_pair P e1 e2)
| value_ref_label : forall P l1,
qual P ->
value (exp_ref_label P l1)
.
(* ********************************************************************** *)
(** * #<a name="stores"></a> Stores *)
(**
A store is simply just a mapping from labels to values.
*)
Notation store := (LabelMap exp).
Notation empty_store := (LabelMapImpl.empty store).
(**
A well formed store maps labels to values.
*)
Definition well_formed_store (s : store) :=
forall l v, LabelMapImpl.MapsTo l v s -> value v.
(**
To ease proofs on stores, it is useful to have a way to pick
a fresh label not in a store.
*)
Lemma label_map_in_iff_keys : forall l (s : store),
LabelMapImpl.In l s <-> List.In l (List.map fst (LabelMapImpl.elements s)).
Proof with eauto.
intros; split; intro In...
+ destruct In as [e MapsTo].
apply LabelMapImpl.elements_1 in MapsTo.