-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.v
2154 lines (1955 loc) · 44.9 KB
/
Main.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
Require Import Arith List.
Require Import "Lattice".
Require Import "Environment".
Inductive type : Type :=
| Bool : label -> type
| Arrow : type -> type -> label -> type.
Inductive expr : Type :=
| TT : label -> expr
| FF : label -> expr
| Cond : expr -> expr -> expr -> expr
| Var : nat -> expr
| Abs : nat -> type -> expr -> label -> expr
| App : expr -> expr -> expr.
Inductive value : expr -> Prop :=
| value_true :
forall l,
value (TT l)
| value_false :
forall l,
value (FF l)
| value_abs :
forall x t e l,
value (Abs x t e l).
Definition value_with_label v l :=
value v /\
(v = (TT l) \/ v = (FF l) \/ (exists x t e, v = Abs x t e l)).
Definition stamp expr label :=
match expr with
| TT l => TT (join l label)
| FF l => FF (join l label)
| Abs x t e l => Abs x t e (join l label)
| _ => expr
end.
Lemma stamp_preserves_value :
forall e l,
value (stamp e l) <->
value e.
Proof.
intros e l.
split; intro H.
destruct e; intros; try inversion H.
apply value_true.
apply value_false.
apply value_abs.
destruct e; intros; try inversion H; subst; simpl.
apply value_true.
apply value_false.
apply value_abs.
Qed.
Lemma stamp_value_is_join :
forall v l l',
value_with_label v l ->
value_with_label (stamp v l') (join l l').
Proof.
intros v l l' Hv; unfold value_with_label.
destruct Hv.
destruct H0.
subst.
simpl.
split.
apply value_true.
left.
reflexivity.
destruct H0.
subst.
split.
apply value_false.
right.
left.
reflexivity.
destruct H0.
destruct H0.
destruct H0.
subst.
split.
simpl.
apply value_abs.
right.
right.
exists x.
exists x0.
exists x1.
reflexivity.
Qed.
Lemma stamp_high_is_high :
forall v l,
value_with_label v l ->
value_with_label (stamp v High) High.
Proof.
intros.
apply (stamp_value_is_join v l High) in H.
rewrite join_high_r in H.
apply H.
Qed.
Lemma stamp_low_is_neutral :
forall v l,
value_with_label v l ->
value_with_label (stamp v Low) l.
Proof.
intros.
apply (stamp_value_is_join v l Low) in H.
rewrite join_low_r in H.
apply H.
Qed.
Fixpoint sub (v : expr) (x : nat) (e : expr) :=
match e with
| TT l =>
TT l
| FF l =>
FF l
| Cond e1 e2 e3 =>
Cond (sub v x e1) (sub v x e2) (sub v x e3)
| Var y =>
if beq_nat x y then v else e
| Abs y t f l =>
if beq_nat x y then e else Abs y t (sub v x f) l
| App e1 e2 =>
App (sub v x e1) (sub v x e2)
end.
Inductive big_step : expr -> expr -> Prop :=
| big_step_val :
forall v,
value v ->
big_step v v
| big_step_cond_true :
forall e1 l e2 e3 v,
big_step e1 (TT l) ->
big_step e2 v ->
big_step (Cond e1 e2 e3) (stamp v l)
| big_step_cond_false :
forall e1 l e2 e3 v,
big_step e1 (FF l) ->
big_step e2 v ->
big_step (Cond e1 e2 e3) (stamp v l)
| big_step_app :
forall e1 x s e l e2 v v',
big_step e1 (Abs x s e l) ->
big_step e2 v ->
big_step (sub v x e) v' ->
big_step (App e1 e2) (stamp v' l).
Lemma big_step_deterministic :
forall e v v',
big_step e v ->
big_step e v' ->
v = v'.
Proof.
intros e v v' Hstep.
revert v'.
induction Hstep; intros v'' Hstep'; inversion Hstep'; subst.
reflexivity.
inversion H.
inversion H.
inversion H.
inversion H.
specialize (IHHstep1 _ H3).
inversion IHHstep1; subst.
specialize (IHHstep2 _ H4); subst.
reflexivity.
specialize (IHHstep1 _ H3).
inversion IHHstep1.
inversion H.
specialize (IHHstep1 _ H3).
inversion IHHstep1.
specialize (IHHstep1 _ H3).
inversion IHHstep1; subst.
specialize (IHHstep2 _ H4); subst.
reflexivity.
inversion H.
specialize (IHHstep1 _ H1).
inversion IHHstep1; subst; clear IHHstep1.
specialize (IHHstep2 _ H2); subst.
specialize (IHHstep3 _ H4); subst; auto.
Qed.
Lemma big_steps_to_value :
forall e v,
big_step e v ->
value v.
Proof.
intros e v Hstep.
induction Hstep; subst; auto.
apply stamp_preserves_value with (l := l); auto.
apply stamp_preserves_value; auto.
apply stamp_preserves_value; auto.
Qed.
Inductive subtype : type -> type -> Prop :=
| TFunSub :
forall s1' s1 s2 s2' l l',
subtype s1' s1 ->
subtype s2 s2' ->
flows_to l l' ->
subtype (Arrow s1 s2 l) (Arrow s1' s2' l')
| TBoolSub :
forall l l',
flows_to l l' ->
subtype (Bool l) (Bool l').
Lemma subtype_refl :
forall t,
subtype t t.
Proof.
induction t.
apply TBoolSub.
apply flows_to_refl.
apply TFunSub.
apply IHt1.
apply IHt2.
apply flows_to_refl.
Qed.
Lemma subtype_antisym :
forall t t',
subtype t t' ->
subtype t' t ->
t = t'.
Proof.
intros t t' Hsub.
induction Hsub; intros.
inversion H0.
subst.
rewrite IHHsub1; auto.
rewrite IHHsub2; auto.
rewrite (flows_to_antisym l l'); auto.
inversion H0.
subst.
rewrite (flows_to_antisym l l'); auto.
Qed.
Lemma subtype_bool_left :
forall l s,
subtype (Bool l) s ->
exists l',
s = (Bool l') /\ flows_to l l'.
Proof.
destruct s.
intros.
exists l0.
inversion H; subst.
split.
reflexivity.
apply H2.
intros.
inversion H.
Qed.
Lemma subtype_bool_right :
forall l s,
subtype s (Bool l) ->
exists l',
s = (Bool l') /\ flows_to l' l.
Proof.
destruct s.
intros.
exists l0.
inversion H; subst.
split.
reflexivity.
apply H2.
intros.
inversion H.
Qed.
Lemma subtype_arrow_left :
forall l s1 s2 s,
subtype (Arrow s1 s2 l) s ->
exists s1' s2' l',
s = (Arrow s1' s2' l') /\ flows_to l l' /\ subtype s1' s1 /\ subtype s2 s2'.
Proof.
destruct s.
intros.
inversion H.
intros.
exists s3. exists s4. exists l0.
inversion H; subst.
split.
reflexivity.
split.
assumption.
split; assumption.
Qed.
Lemma subtype_arrow_right :
forall l s1 s2 s,
subtype s (Arrow s1 s2 l) ->
exists s1' s2' l',
s = (Arrow s1' s2' l') /\ flows_to l' l /\ subtype s1 s1' /\ subtype s2' s2.
Proof.
destruct s.
intros.
inversion H.
intros.
exists s3. exists s4. exists l0.
inversion H; subst.
split.
reflexivity.
split.
assumption.
split; assumption.
Qed.
Lemma subtype_trans :
forall t t' t'',
subtype t t' ->
subtype t' t'' ->
subtype t t''.
Proof.
intros t t'.
revert t.
induction t' as [l'| s1' IHs1' s2' IHs2' l'].
intros t t'' H H0.
apply subtype_bool_left in H0.
destruct H0 as [l'' [H0 H0']].
subst.
apply subtype_bool_right in H.
destruct H as [l [H H']].
subst.
apply TBoolSub.
apply flows_to_trans with (l' := l'); assumption.
intros t t'' H0 H.
apply subtype_arrow_left in H.
destruct H as [s1'' [s2'' [l'' [H5 [H6 [H7 H8]]]]]].
subst.
apply subtype_arrow_right in H0.
destruct H0 as [s1 [s2 [l [H5 [H9 [H10 H11]]]]]].
subst.
apply TFunSub.
apply IHs1'; assumption.
apply IHs2'; assumption.
apply flows_to_trans with (l' := l'); assumption.
Qed.
Definition stamp_type t l :=
match t with
| Bool l' => Bool (join l' l)
| Arrow t1 t2 l' => Arrow t1 t2 (join l' l)
end.
Definition type_with_label t l :=
match t with
| Bool l' => l = l'
| Arrow _ _ l' => l = l'
end.
Lemma all_types_have_label :
forall t, exists l,
type_with_label t l.
Proof.
destruct t.
exists l; auto.
reflexivity.
exists l.
reflexivity.
Qed.
Lemma stamp_type_is_join :
forall t l l',
type_with_label t l ->
type_with_label (stamp_type t l') (join l l').
Proof.
intros.
destruct t.
simpl.
simpl in H.
subst.
reflexivity.
simpl.
simpl in H.
subst.
reflexivity.
Qed.
Lemma stamp_preserves_subtype :
forall s l s',
subtype (stamp_type s l) s' ->
subtype s s'.
Proof.
induction s; intros.
simpl in H.
apply subtype_bool_left in H.
destruct H.
destruct H.
subst.
apply TBoolSub.
apply flows_to_trans with (l' := (join l l0)).
apply join_is_upper_bound.
apply H0.
simpl in H.
apply subtype_arrow_left in H.
destruct H.
destruct H.
destruct H.
destruct H.
destruct H0.
destruct H1.
subst.
apply TFunSub.
apply H1.
apply H2.
apply flows_to_trans with (l' := (join l l0)).
apply join_is_upper_bound.
apply H0.
Qed.
Lemma stamp_l_preserves_subtype :
forall s l s',
subtype s s' ->
subtype (stamp_type s l) (stamp_type s' l).
Proof.
induction s; intros.
apply subtype_bool_left in H.
destruct H.
destruct H.
subst.
simpl.
apply TBoolSub.
destruct (join_is_upper_bound x l0).
apply join_is_least_upper_bound; auto.
apply flows_to_trans with (l' := x); auto.
apply subtype_arrow_left in H.
destruct H.
destruct H.
destruct H.
destruct H.
destruct H0.
destruct H1.
subst.
apply TFunSub; auto.
destruct (join_is_upper_bound x1 l0); auto.
apply join_is_least_upper_bound; auto.
apply flows_to_trans with (l' := x1); auto.
Qed.
Inductive typing : environment type -> expr -> type -> Prop :=
| typing_true :
forall c l l',
flows_to l l' ->
typing c (TT l) (Bool l')
| typing_false :
forall c l l',
flows_to l l' ->
typing c (FF l) (Bool l')
| typing_cond :
forall c s s' e1 e2 e3 l,
typing c e1 (Bool l) ->
typing c e2 s ->
typing c e3 s ->
subtype (stamp_type s l) s' ->
typing c (Cond e1 e2 e3) s'
| typing_app :
forall c e1 e2 s2 s2' s s' l,
typing c e1 (Arrow s2 s l) ->
typing c e2 s2' ->
subtype s2' s2 ->
subtype (stamp_type s l) s' ->
typing c (App e1 e2) s'
| typing_abs :
forall c x e s1' s1 s2 s2' l l',
typing (Extend type x s1 c) e s2 ->
subtype s1' s1 ->
subtype s2 s2' ->
flows_to l l' ->
typing c (Abs x s1 e l) (Arrow s1' s2' l')
| typing_var :
forall x c s s',
lookup x c = Some s ->
subtype s s' ->
typing c (Var x) s'.
Lemma typing_inversion_true :
forall c l s,
typing c (TT l) s ->
exists l',
s = (Bool l') /\ flows_to l l'.
Proof.
intros.
inversion H; subst.
exists l'; auto.
Qed.
Lemma typing_inversion_false :
forall c l s,
typing c (FF l) s ->
exists l',
s = (Bool l') /\ flows_to l l'.
Proof.
intros.
inversion H; subst.
exists l'; auto.
Qed.
Lemma typing_inversion_cond :
forall c e1 e2 e3 s l,
typing c (Cond e1 e2 e3) s ->
type_with_label s l ->
exists l' s',
typing c e1 (Bool l') /\
typing c e2 s' /\
typing c e3 s' /\
subtype (stamp_type s' l') s.
Proof.
intros.
inversion H; subst.
exists l0.
exists s0.
split; auto.
Qed.
Lemma typing_inversion_var :
forall c x s,
typing c (Var x) s ->
exists s',
lookup x c = Some s' /\ subtype s' s.
Proof.
intros.
inversion H; subst.
exists s0.
split; auto.
Qed.
Lemma typing_inversion_app :
forall c e1 e2 s l,
typing c (App e1 e2) s ->
type_with_label s l ->
exists l' s1 s1' s2,
typing c e1 (Arrow s1 s2 l') /\
typing c e2 s1' /\
subtype s1' s1 /\
subtype (stamp_type s2 l') s.
Proof.
intros.
inversion H; subst.
exists l0.
exists s2.
exists s2'.
exists s0.
split; auto.
Qed.
Lemma typing_inversion_abs :
forall c x s1' e l' u,
typing c (Abs x s1' e l') u ->
exists s1 s2 s2' l,
u = (Arrow s1 s2 l) /\
typing (Extend _ x s1' c) e s2' /\
subtype s1 s1' /\
subtype s2' s2 /\
flows_to l' l.
Proof.
intros.
inversion H; subst.
exists s1'0.
exists s2'.
exists s2.
exists l'0; auto.
Qed.
Lemma typing_is_context_invariant :
forall c c' e s,
env_equiv c c' ->
typing c e s ->
typing c' e s.
Proof.
intros c c' e s Hequiv Htype.
revert c' Hequiv.
induction Htype; intros c' Hequiv.
apply typing_true; auto.
apply typing_false; auto.
apply typing_cond with (s := s)(l := l); auto.
apply typing_app with (s2 := s2)(s := s)(l := l)(s2' := s2'); auto.
apply typing_abs with (s2 := s2); auto.
apply IHHtype.
apply env_equiv_extend_eq.
apply Hequiv.
apply typing_var with (s := s); auto.
rewrite <- H.
symmetry.
apply Hequiv.
Qed.
Inductive free_in : nat -> expr -> Prop :=
| free_in_cond :
forall x e1 e2 e3,
(free_in x e1 \/ free_in x e2 \/ free_in x e3) ->
free_in x (Cond e1 e2 e3)
| free_in_app :
forall x e1 e2,
(free_in x e1 \/ free_in x e2) ->
free_in x (App e1 e2)
| free_in_var :
forall x,
free_in x (Var x)
| free_in_abs :
forall x y s e l,
x <> y ->
free_in x e ->
free_in x (Abs y s e l).
Lemma free_in_dec :
forall x e,
{ free_in x e} + { ~ free_in x e }.
Proof.
unfold not.
induction e.
right.
intros.
inversion H.
right.
intros.
inversion H.
destruct IHe1.
left.
apply free_in_cond.
left.
assumption.
destruct IHe2.
left.
apply free_in_cond.
right.
left.
assumption.
destruct IHe3.
left.
apply free_in_cond.
right.
right.
apply f1.
right.
intros.
inversion H; subst.
destruct H2.
auto.
destruct H0; auto.
case_eq (beq_nat x n).
intros.
apply beq_nat_true in H.
subst.
left.
apply free_in_var.
right.
intros.
inversion H0.
subst.
rewrite <- beq_nat_refl in H.
inversion H.
destruct IHe.
case_eq (beq_nat x n); intros.
apply beq_nat_true in H.
subst.
right.
intros.
inversion H.
auto.
left.
apply beq_nat_false in H.
apply free_in_abs; assumption.
right.
intros.
inversion H; auto.
destruct IHe1.
left.
apply free_in_app; auto.
destruct IHe2.
left.
apply free_in_app; auto.
right.
intros.
inversion H.
destruct H2; auto.
Qed.
Definition closed e :=
forall x, ~ free_in x e.
Definition ctxt_approx c c' :=
forall x t,
(lookup x c = Some t ->
exists t',
lookup x c' = Some t' /\ subtype t t').
Definition ctxt_equiv c c' :=
ctxt_approx c c' /\ ctxt_approx c' c.
Lemma ctxt_equiv_sound :
forall e c c',
ctxt_equiv c c' ->
(forall x, free_in x e ->
lookup x c = lookup x c').
Proof.
unfold ctxt_equiv.
unfold ctxt_approx.
intros.
destruct H.
case_eq (lookup x c); intros.
case_eq (lookup x c'); intros.
specialize (H x t H2).
specialize (H1 x t0 H3).
destruct H.
destruct H.
destruct H1.
destruct H1.
rewrite H1 in H2.
inversion H2.
subst.
clear H2.
rewrite H in H3.
inversion H3.
subst.
clear H3.
assert (t = t0).
apply subtype_antisym; auto.
subst.
reflexivity.
specialize (H x t H2).
destruct H.
destruct H.
rewrite H in H3.
inversion H3.
case_eq (lookup x c'); intros.
specialize (H1 x t H3).
destruct H1.
destruct H1.
rewrite H2 in H1.
inversion H1.
reflexivity.
Qed.
Lemma env_approx :
forall e c c' s s' l l',
typing c e s ->
type_with_label s l ->
ctxt_approx c c' ->
typing c' e s' ->
type_with_label s' l' ->
subtype s (stamp_type s' l).
Proof.
intros e c c' s s' l l' Htyp Hlab Happx Htyp'.
generalize dependent c.
generalize dependent s.
revert l.
revert l'.
induction Htyp'; intros l'' l''' s'' Htwl c' Htype Happx Htwl'.
simpl in Htwl.
apply typing_inversion_true in Htype.
destruct Htype.
destruct H0.
subst.
simpl in Htwl.
subst.
apply TBoolSub.
apply join_is_upper_bound.
simpl in Htwl.
apply typing_inversion_false in Htype.
destruct Htype.
destruct H0.
subst.
simpl in Htwl.
subst.
apply TBoolSub.
apply join_is_upper_bound.
apply typing_inversion_cond with (l := l) in Htype.
destruct Htype as [l' H0].
destruct H0 as [t H0].
destruct H0.
destruct H1.
destruct H2.
Abort.
Lemma env_invariance :
forall e c c' s,
typing c e s ->
(forall x, free_in x e -> lookup x c = lookup x c') ->
typing c' e s.
Proof.
intros e c c' s Htype.
revert c'.
induction Htype; intros.
apply typing_true.
assumption.
apply typing_false.
assumption.
apply typing_cond with (s := s)(l := l); auto.
apply (IHHtype1 c'); auto.
intros.
apply H0.
apply free_in_cond; auto.
apply (IHHtype2 c'); auto; intros.
apply H0.
apply free_in_cond; auto.
apply (IHHtype3 c'); auto.
intros.
apply H0.
apply free_in_cond; auto.
apply typing_app with (s2 := s2)(s := s)(l := l)(s2' := s2'); auto.
apply IHHtype1; intros.
apply H1.
apply free_in_app; auto.
apply IHHtype2; intros.
apply H1.
apply free_in_app; auto.
apply typing_abs with (s2 := s2); auto.
apply IHHtype.
intros.
simpl.
case_eq (beq_nat x0 x); intro Heq.
reflexivity.
apply H2.
apply free_in_abs.
apply beq_nat_false in Heq.
assumption.
apply H3.
apply typing_var with (s := s); auto.
rewrite <- H.
symmetry.
apply H1.
apply free_in_var.
Qed.
Lemma free_in_env :
forall x e c s,
typing c e s ->
free_in x e ->
exists s', lookup x c = Some s'.
Proof.
intros x e c s Htype.
revert x.
induction Htype; intros x' Hfree.
inversion Hfree.
inversion Hfree.
inversion Hfree.
destruct H2.
apply IHHtype1.
assumption.
destruct H2.
apply IHHtype2.
assumption.
apply IHHtype3.
assumption.
inversion Hfree.
destruct H3.
apply IHHtype1.
assumption.
apply IHHtype2.
assumption.
inversion Hfree; subst.
specialize (IHHtype x' H8).
destruct IHHtype.
apply beq_nat_false_iff in H5.
simpl in H2.
rewrite H5 in H2.
exists x0.
assumption.
inversion Hfree; subst.
exists s.
assumption.
Qed.
Lemma typeable_closed :
forall e s,
typing (Empty _) e s ->
closed e.
Proof.
intros.
unfold closed.
intros.
unfold not.
intros.
apply free_in_env with (x := x) in H.
destruct H.
inversion H.
assumption.
Qed.
Lemma subsumption :
forall c e s s',
typing c e s ->
subtype s s' ->
typing c e s'.
Proof.
intros c e s s' Htyp.
revert s'.
induction Htyp.
intros s' Htyp.
apply subtype_bool_left in Htyp.
destruct Htyp.
destruct H0.
subst.
apply typing_true.
apply flows_to_trans with (l' := l'); assumption.
intros s' Htyp.
apply subtype_bool_left in Htyp.
destruct Htyp.
destruct H0.
subst.
apply typing_false.
apply flows_to_trans with (l' := l'); assumption.
intros s'' Hsub.
apply typing_cond with (s := s)(l := l).
apply Htyp1.
apply Htyp2.
apply Htyp3.
apply subtype_trans with (t' := s').
apply H.
apply Hsub.
intros s'' Hsub.
apply typing_app with (s2 := s2)(s := s) (l := l)(s2' := s2'); auto.
apply subtype_trans with (t' := s'); auto.
intros s' Hsub.
apply subtype_arrow_left in Hsub.
destruct Hsub.
destruct H2.
destruct H2.
destruct H2.
destruct H3.
destruct H4.
subst.
apply typing_abs with (s2 := s2).
apply Htyp.
apply subtype_trans with (t' := s1'); assumption.
apply subtype_trans with (t' := s2'); assumption.
apply flows_to_trans with (l' := l'); assumption.
intros s'' Hsub.
apply typing_var with (s := s).
assumption.
apply subtype_trans with (t' := s'); assumption.
Qed.
Lemma stamp_idemp :
forall s l,
type_with_label s l ->
s = (stamp_type s l).
Proof.
destruct s; intros; simpl in H; subst; simpl; rewrite <- join_idempotent; reflexivity.
Qed.
Lemma canonical_form_bool :
forall c v l,
value v ->
typing c v (Bool l) ->
exists l',
(v = (TT l') \/ v = (FF l')) /\ flows_to l' l.
Proof.
intros c e l' Hval.
inversion Hval; subst.
intros Htype.
inversion Htype; subst.