-
Notifications
You must be signed in to change notification settings - Fork 2
/
st_lam.v
1676 lines (1490 loc) · 48.1 KB
/
st_lam.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
(* Copyright (c) 2014, Robert Dockins *)
Require Import String.
Require Import atoms.
Require Import permutations.
Require Import basics.
Require Import preord.
Require Import categories.
Require Import sets.
Require Import finsets.
Require Import esets.
Require Import effective.
Require Import directed.
Require Import plotkin.
Require Import joinable.
Require Import approx_rels.
Require Import cpo.
Require Import profinite.
Require Import finprod.
Require Import discrete.
Require Import List.
(** * The simply-typed λ-calculus with booleans.
This file develops the simply-typed λ-calculus
with named variables. Types are interpreted
as unpointed domains in PLT.
Soundness and adequacy of the denotational semantics
are proved with respect to a standard big-step operational
semantics. This uses the standard logical relation
approach. As a corollary, we obtain strong normalization
for the calculus.
*)
(** ** Types and type denotations
We have a boolean base type and functions.
*)
Inductive ty :=
| ty_bool
| ty_arrow : ty -> ty -> ty.
Delimit Scope ty_scope with ty.
Notation "2" := ty_bool : ty_scope.
Notation "x ⇒ y" := (ty_arrow (x)%ty (y)%ty) : ty_scope.
Bind Scope ty_scope with ty.
Delimit Scope lam_scope with lam.
Open Scope lam_scope.
(** Types are interpreted via a straightforward
translation into PLT domains.
*)
Fixpoint tydom (τ:ty) : PLT :=
match τ with
| 2%ty => disc finbool
| (τ₁ ⇒ τ₂)%ty => tydom τ₁ ⇒ tydom τ₂
end.
(** The syntax of types has decidable equality. This is
important because it allows us to work around some
problems that arise with dependent types.
*)
Lemma ty_dec : forall x y:ty, {x=y}+{x<>y}.
Proof.
decide equality.
Qed.
(** ** Type contexts
Now we instantiate a module for finite products.
This gives us a domain in PLT for representing
type contexts, and provides operations and lemmas
we need for working with them.
*)
Module env_input <: FINPROD_INPUT.
Definition A := ty.
Definition Adec := ty_dec.
Definition F := tydom.
End env_input.
Module ENV := finprod.finprod(env_input).
Notation env := ENV.env.
Canonical Structure ENV.env_supported.
Notation inenv := ENV.inenv.
Notation cxt := ENV.finprod.
Notation castty := (cast ENV.ty).
Notation proj := ENV.proj.
Notation bind := ENV.bind.
(** ** Terms and term denotations
Terms are intrinsicly-typed, carrying both
a type environment for their free variables
and the final type of the term.
Variables carry a name (atom) and a proof
that (x,σ) appears in the type environment.
Lambdas extend the type environment in the standard way.
*)
Inductive term (Γ:env) : ty -> Type :=
| tvar : forall (x:atom) (σ:ty),
inenv Γ x σ ->
term Γ σ
| tbool : forall n:bool,
term Γ 2
| tapp : forall σ₁ σ₂,
term Γ (σ₁ ⇒ σ₂) ->
term Γ σ₁ ->
term Γ σ₂
| tif : forall σ,
term Γ 2 ->
term Γ σ ->
term Γ σ ->
term Γ σ
| tlam : forall x σ₁ σ₂,
term ((x,σ₁)::Γ) σ₂ ->
term Γ (σ₁ ⇒ σ₂).
Arguments tapp [_ _ _] _ _.
Notation "x • y" := (tapp x y)
(at level 52, left associativity, format "x • y") : lam_scope.
Notation subst := (ENV.subst term).
Notation term_wk := (ENV.tm_wk term).
Notation term_subst := (ENV.tm_subst term).
(** The terms in environment [Γ] with type [τ] are interpreted
as PLT-homs from [cxt Γ] to [tydom τ].
*)
Definition dom (Γ:env) (τ:ty) : Type := cxt Γ → tydom τ.
Fixpoint denote (Γ:env) (τ:ty) (m:term Γ τ) : dom Γ τ :=
match m in term _ τ' return dom Γ τ' with
| tvar _ x σ IN => castty IN ∘ proj Γ x
| tbool _ b => disc_elem b ∘ PLT.terminate false (cxt Γ)
| tif _ σ x y z => disc_cases (fun b:bool => if b then 〚y〛 else 〚z〛)
∘ 〈 id, 〚x〛 〉
| tapp m₁ m₂ => apply ∘ 〈 〚m₁〛, 〚m₂〛 〉
| tlam _ x σ₁ σ₂ m' => Λ(〚m'〛 ∘ bind Γ x σ₁)
end
where "〚 m 〛" := (denote _ _ m) : lam_scope.
(** Here we define a generic traversal function. This traversal
is uniformly used to define both weakening and substitution
by exploiting the finprod library. Defining this traversal
and its correctness proof is sufficent to get out a
definition of substitution and a proof of correctness.
*)
Section traverse.
Variable thingy:env -> atom -> ty -> Type.
Variable thingy_term : forall Γ x σ, thingy Γ x σ -> term Γ σ.
Variable rename_var : env -> atom -> atom.
Variable weaken_vars : forall Γ₁ Γ₂ y τ,
(forall x σ, inenv Γ₁ x σ -> thingy Γ₂ x σ) ->
(forall x σ, inenv ((y,τ)::Γ₁) x σ -> thingy ((rename_var Γ₂ y,τ)::Γ₂) x σ).
Fixpoint traverse
(Γ₁ Γ₂:env) (σ:ty)
(VAR : forall x σ, inenv Γ₁ x σ -> thingy Γ₂ x σ)
(m:term Γ₁ σ) : term Γ₂ σ :=
match m with
| tvar _ x σ IN => thingy_term Γ₂ x σ (VAR x σ IN)
| tbool _ b => tbool Γ₂ b
| @tapp _ σ₁ σ₂ m₁ m₂ =>
@tapp Γ₂ σ₁ σ₂ (traverse Γ₁ Γ₂ (σ₁ ⇒ σ₂) VAR m₁)
(traverse Γ₁ Γ₂ σ₁ VAR m₂)
| tif _ σ x y z =>
tif Γ₂ σ (traverse Γ₁ Γ₂ 2 VAR x)
(traverse Γ₁ Γ₂ σ VAR y)
(traverse Γ₁ Γ₂ σ VAR z)
| tlam _ x σ₁ σ₂ m' =>
let x' := rename_var Γ₂ x in
tlam Γ₂ x' σ₁ σ₂
(traverse ((x,σ₁)::Γ₁) ((x',σ₁)::Γ₂) σ₂
(weaken_vars Γ₁ Γ₂ x σ₁ VAR)
m')
end.
Hypothesis weaken_sem_bind : forall Γ₁ Γ₂ x σ VAR,
bind Γ₁ x σ ∘ PLT.pair_map (ENV.varmap_denote term denote thingy thingy_term Γ₁ Γ₂ VAR) id
≈ ENV.varmap_denote term denote thingy thingy_term ((x,σ)::Γ₁) ((rename_var Γ₂ x,σ)::Γ₂)
(weaken_vars Γ₁ Γ₂ x σ VAR) ∘ bind Γ₂ (rename_var Γ₂ x) σ.
Hypothesis varmap_denote_proj : forall Γ₁ Γ₂ VAR x σ i,
〚 thingy_term Γ₂ x σ (VAR x σ i) 〛
≈ castty i ∘ proj Γ₁ x ∘ ENV.varmap_denote term denote thingy thingy_term Γ₁ Γ₂ VAR.
Lemma traverse_correct
(Γ₁ Γ₂:env) (σ:ty)
(m:term Γ₁ σ) : forall
(VAR : forall x σ, inenv Γ₁ x σ -> thingy Γ₂ x σ),
denote _ _ (traverse Γ₁ Γ₂ σ VAR m) ≈
denote _ _ m ∘ ENV.varmap_denote term denote thingy thingy_term Γ₁ Γ₂ VAR.
Proof.
revert Γ₂. induction m; simpl; intros.
apply varmap_denote_proj.
rewrite <- (cat_assoc PLT). apply cat_respects; auto.
symmetry. apply PLT.terminate_univ.
rewrite <- (cat_assoc PLT). apply cat_respects; auto.
rewrite (PLT.pair_compose_commute false).
apply PLT.pair_eq.
apply IHm1; auto.
apply IHm2; auto.
rewrite <- (cat_assoc PLT).
rewrite (PLT.pair_compose_commute false).
rewrite (cat_ident2 PLT).
symmetry.
rewrite (disc_cases_commute _ _ _ _ _ (ENV.varmap_denote _ _ _ _ _ _ _)).
apply cat_respects; auto.
symmetry.
apply disc_cases_univ.
intros. rewrite disc_cases_elem'.
rewrite (cat_ident1 PLT).
destruct x; auto.
rewrite IHm1. auto.
symmetry.
rewrite (PLT.curry_compose_commute _ _ _ _ _ (〚 m 〛∘ bind Γ x σ₁)).
apply PLT.curry_eq.
rewrite <- (cat_assoc PLT).
rewrite IHm.
rewrite <- (cat_assoc PLT). apply cat_respects; auto.
rewrite weaken_sem_bind.
apply cat_respects; auto.
Qed.
End traverse.
(** Register terms together with the denotation and traversal functions
as a term model. This gives us access to the generic substitution
definition in finprod.
*)
Program Definition lam_termmodel :=
ENV.TermModel term tvar traverse denote traverse_correct _.
Next Obligation.
simpl. auto.
Qed.
Existing Instance lam_termmodel.
(** Restate the substitution correctness lemma. *)
Lemma subst_soundness Γ x σ₁ σ₂ n₁ n₂ :
〚 n₁ 〛 ∘ bind Γ x σ₁ ∘ 〈id, 〚 n₂ 〛〉 ≈ 〚 subst Γ σ₂ σ₁ x n₁ n₂ 〛.
Proof.
generalize (ENV.subst_soundness term). simpl. auto.
Qed.
(** ** Operational semantics and soundness
This is a standard call-by-value operational semantics. As this
calculus is strongly-normalizing, we could just as well use a
call-by-need strategy.
Notation: [m⇓z] means that [m] evaluates to [z].
[m↓] means that [m] evaluates to itself; i.e., [m] is a value.
*)
Reserved Notation "m ⇓ z" (at level 82, left associativity).
Reserved Notation "m ↓" (at level 82, left associativity).
Inductive eval (Γ:env) : forall τ, term Γ τ -> term Γ τ -> Prop :=
| ebool : forall b,
tbool Γ b ↓
| eif : forall σ x y z b q,
x ⇓ (tbool Γ b) ->
(if b then y else z) ⇓ q ->
(tif Γ σ x y z) ⇓ q
| elam : forall x σ₁ σ₂ m,
tlam Γ x σ₁ σ₂ m ↓
| eapp : forall x σ₁ σ₂ m₁ m₂ n₁ n₂ z,
m₁ ⇓ (tlam Γ x σ₁ σ₂ n₁) ->
m₂ ⇓ n₂ ->
subst Γ σ₂ σ₁ x n₁ n₂ ⇓ z ->
m₁ • m₂ ⇓ z
where "m ⇓ z" := (eval _ _ m z)
and "m ↓" := (eval _ _ m m).
(** Evaluation preserves the denotation of terms. *)
Theorem soundness : forall Γ τ (m z:term Γ τ),
m ⇓ z -> 〚m〛 ≈ 〚z〛.
Proof.
intros. induction H; simpl; auto.
rewrite IHeval1.
simpl.
rewrite disc_cases_elem'.
rewrite (cat_ident1 PLT).
destruct b; auto.
rewrite IHeval1.
rewrite IHeval2.
rewrite <- IHeval3.
simpl.
rewrite PLT.curry_apply2.
apply subst_soundness.
Qed.
(** ** Misc technical lemmas
*)
(** Syntactic types have decicable equality, which
implies injectivity for dependent pairs with
(syntactic) types as the type being depended upon.
*)
Lemma inj_pair2_ty : forall (F:ty -> Type) τ x y,
existT F τ x = existT F τ y -> x = y.
Proof.
intros.
apply Eqdep_dec.inj_pair2_eq_dec in H. auto.
decide equality.
Qed.
Lemma env_dec : forall a b:env, {a=b}+{a<>b}.
Proof.
decide equality.
decide equality.
decide equality.
apply string_dec.
Qed.
Ltac inj_ty :=
repeat match goal with
[ H : existT _ _ _ = existT _ _ _ |- _ ] =>
apply inj_pair2_ty in H ||
apply (Eqdep_dec.inj_pair2_eq_dec _ string_dec) in H ||
apply (Eqdep_dec.inj_pair2_eq_dec _ env_dec) in H
end.
Ltac inv H :=
inversion H; subst; inj_ty; repeat subst.
(** We will need a variety of technical results about the operational semantics.
*)
Lemma eval_value Γ τ x y :
eval Γ τ x y -> eval Γ τ y y.
Proof.
intro H. induction H.
apply ebool.
auto.
apply elam.
auto.
Qed.
Lemma eval_eq Γ τ x y1 y2 :
eval Γ τ x y1 -> eval Γ τ x y2 -> y1 = y2.
Proof.
intro H. revert y2.
induction H.
intros. inv H. auto.
intros. inv H1.
assert (tbool Γ b = tbool Γ b0).
apply IHeval1. auto.
inv H2.
apply IHeval2; auto.
intros. inv H. auto.
intros. inv H2.
apply IHeval1 in H8.
apply IHeval2 in H9.
inv H8.
apply IHeval3; auto.
Qed.
Lemma eval_trans Γ τ x y z :
eval Γ τ x y -> eval Γ τ y z -> eval Γ τ x z.
Proof.
intros.
replace z with y; auto.
eapply eval_eq with y; auto.
eapply eval_value; eauto.
Qed.
(** ** Alpha congruence
Here we define alpha congruence of terms.
*)
Inductive var_cong : env -> env -> atom -> atom -> Prop :=
| vcong_here : forall Γ₁ Γ₂ x₁ x₂ y₁ y₂ τ,
x₁ = y₁ -> x₂ = y₂ ->
var_cong ((x₁,τ)::Γ₁) ((x₂,τ)::Γ₂) y₁ y₂
| vcong_there : forall Γ₁ Γ₂ x₁ x₂ y₁ y₂ τ,
x₁ <> y₁ -> x₂ <> y₂ ->
var_cong Γ₁ Γ₂ y₁ y₂ ->
var_cong ((x₁,τ)::Γ₁) ((x₂,τ)::Γ₂) y₁ y₂.
Inductive alpha_cong : forall Γ Γ' (τ:ty), term Γ τ -> term Γ' τ -> Prop :=
| acong_var : forall Γ Γ' τ x₁ x₂ H₁ H₂,
var_cong Γ Γ' x₁ x₂ ->
alpha_cong Γ Γ' τ (tvar Γ x₁ τ H₁) (tvar Γ' x₂ τ H₂)
| acong_bool : forall Γ Γ' b,
alpha_cong Γ Γ' 2 (tbool Γ b) (tbool Γ' b)
| acong_app : forall Γ Γ' σ₁ σ₂ m₁ m₂ n₁ n₂,
alpha_cong Γ Γ' (σ₁ ⇒ σ₂) m₁ n₁ ->
alpha_cong Γ Γ' σ₁ m₂ n₂ ->
alpha_cong Γ Γ' σ₂ (m₁ • m₂) (n₁ • n₂)
| acong_if : forall Γ Γ' σ x1 x2 y1 y2 z1 z2,
alpha_cong Γ Γ' 2 x1 x2 ->
alpha_cong Γ Γ' σ y1 y2 ->
alpha_cong Γ Γ' σ z1 z2 ->
alpha_cong Γ Γ' σ (tif Γ σ x1 y1 z1) (tif Γ' σ x2 y2 z2)
| acong_lam : forall (Γ Γ':env) (x₁ x₂:atom) σ₁ σ₂ m₁ m₂,
alpha_cong ((x₁,σ₁)::Γ) ((x₂,σ₁)::Γ') σ₂ m₁ m₂ ->
alpha_cong Γ Γ' (σ₁ ⇒ σ₂) (tlam Γ x₁ σ₁ σ₂ m₁) (tlam Γ' x₂ σ₁ σ₂ m₂).
(** Alpha congruence is reflexive, transitive and symmetric.
*)
Lemma var_cong_refl Γ x τ:
inenv Γ x τ ->
var_cong Γ Γ x x.
Proof.
induction Γ; intro H.
inv H.
hnf in H. simpl in H.
destruct a.
destruct (string_dec s x). inv H.
apply vcong_here; auto.
apply vcong_there; auto.
Qed.
Lemma var_cong_sym Γ₁ Γ₂ x y :
var_cong Γ₁ Γ₂ x y ->
var_cong Γ₂ Γ₁ y x.
Proof.
intro H. induction H.
apply vcong_here; auto.
apply vcong_there; auto.
Qed.
Lemma var_cong_trans Γ₁ Γ₂ x y z :
var_cong Γ₁ Γ₂ x y ->
forall Γ₃,
var_cong Γ₂ Γ₃ y z ->
var_cong Γ₁ Γ₃ x z.
Proof.
intro H; induction H; intros.
subst. inv H1.
apply vcong_here; auto.
elim H3; auto.
inv H2.
elim H0. auto.
apply vcong_there; auto.
Qed.
Lemma alpha_eq_refl Γ σ (m:term Γ σ) :
alpha_cong Γ Γ σ m m.
Proof.
induction m.
apply acong_var.
eapply var_cong_refl; eauto.
apply acong_bool.
apply acong_app; auto.
apply acong_if; auto.
apply acong_lam; auto.
Qed.
Lemma alpha_eq_sym Γ₁ Γ₂ τ m n :
alpha_cong Γ₁ Γ₂ τ m n ->
alpha_cong Γ₂ Γ₁ τ n m.
Proof.
intro H; induction H.
apply acong_var. apply var_cong_sym. auto.
apply acong_bool.
apply acong_app; auto.
apply acong_if; auto.
apply acong_lam; auto.
Qed.
Lemma alpha_eq_trans Γ₁ τ (m:term Γ₁ τ) :
forall Γ₂ Γ₃ (n:term Γ₂ τ) (o:term Γ₃ τ),
alpha_cong Γ₁ Γ₂ τ m n ->
alpha_cong Γ₂ Γ₃ τ n o ->
alpha_cong Γ₁ Γ₃ τ m o.
Proof.
induction m; intros; inv H; inv H0.
apply acong_var.
eapply var_cong_trans; eauto.
apply acong_bool.
apply acong_app; eauto.
apply acong_if; eauto.
apply acong_lam; eauto.
Qed.
(** Alpha congruent terms have equal denotations.
*)
Lemma alpha_cong_denote (Γ₁ Γ₂:env) τ (m:term Γ₁ τ) (n:term Γ₂ τ) :
alpha_cong Γ₁ Γ₂ τ m n ->
forall A (h₁:A → cxt Γ₁) (h₂:A → cxt Γ₂),
(forall a b τ (IN1:inenv Γ₁ a τ) (IN2:inenv Γ₂ b τ),
var_cong Γ₁ Γ₂ a b ->
castty IN1 ∘ proj Γ₁ a ∘ h₁ ≈ castty IN2 ∘ proj Γ₂ b ∘ h₂) ->
〚m〛 ∘ h₁ ≈ 〚n〛 ∘ h₂.
Proof.
intro H. induction H.
simpl; intros. apply H0. auto.
simpl; intros.
do 2 rewrite <- (cat_assoc PLT).
apply cat_respects; auto.
transitivity (PLT.terminate false A).
apply PLT.terminate_univ.
symmetry.
apply PLT.terminate_univ.
simpl; intros.
do 2 rewrite <- (cat_assoc PLT).
apply cat_respects; auto.
do 2 rewrite (PLT.pair_compose_commute false).
apply PLT.pair_eq.
apply IHalpha_cong1. auto.
apply IHalpha_cong2. auto.
simpl; intros.
rewrite <- (cat_assoc PLT).
rewrite <- (cat_assoc PLT).
rewrite (PLT.pair_compose_commute false).
rewrite (PLT.pair_compose_commute false).
rewrite (cat_ident2 PLT).
rewrite (cat_ident2 PLT).
rewrite (IHalpha_cong1 _ h₁ h₂ H2).
rewrite disc_cases_commute.
rewrite (disc_cases_commute _ _ _ _ _ h₂).
apply cat_respects; auto.
apply disc_cases_univ.
intros.
rewrite disc_cases_elem'.
rewrite (cat_ident1 PLT).
destruct x.
apply IHalpha_cong2; auto.
apply IHalpha_cong3; auto.
simpl; intros.
rewrite (PLT.curry_compose_commute false _ _ _ _ (〚 m₁ 〛 ∘ bind Γ x₁ σ₁)).
rewrite (PLT.curry_compose_commute false _ _ _ _ (〚 m₂ 〛 ∘ bind Γ' x₂ σ₁)).
apply PLT.curry_eq.
do 2 rewrite <- (cat_assoc PLT).
apply IHalpha_cong.
intros. inv H1.
do 2 rewrite <- (cat_assoc PLT).
rewrite (cat_assoc PLT _ _ _ _ (proj ((a,σ₁)::Γ) a)).
rewrite (ENV.proj_bind_eq _ _ _ _ (refl_equal a)).
rewrite <- (cat_assoc PLT).
unfold PLT.pair_map.
rewrite PLT.pair_commute2.
rewrite (cat_ident2 PLT).
symmetry.
rewrite (cat_assoc PLT _ _ _ _ (proj ((b,σ₁)::Γ') b)).
rewrite (ENV.proj_bind_eq _ _ _ _ (refl_equal b)).
rewrite <- (cat_assoc PLT).
rewrite PLT.pair_commute2.
do 2 rewrite (cat_assoc PLT).
apply cat_respects; auto.
etransitivity.
apply cast_compose.
symmetry.
etransitivity.
apply cast_compose.
match goal with [ |- castty ?X ≈ castty ?Y ] => generalize X Y end.
hnf in IN1. simpl in *.
destruct (string_dec a a).
inv IN1. intros.
replace e0 with e1. auto.
apply Eqdep_dec.UIP_dec. decide equality. decide equality.
elim n; auto.
do 2 rewrite <- (cat_assoc PLT).
rewrite (cat_assoc PLT _ _ _ _ (proj ((x₁,σ₁)::Γ) a)).
rewrite (ENV.proj_bind_neq x₁ σ₁ a Γ H9); auto.
unfold PLT.pair_map.
rewrite <- (cat_assoc PLT).
rewrite PLT.pair_commute1.
symmetry.
rewrite (cat_assoc PLT _ _ _ _ (proj ((x₂,σ₁)::Γ') b)).
rewrite (ENV.proj_bind_neq x₂ σ₁ b Γ' H10); auto.
rewrite <- (cat_assoc PLT).
rewrite PLT.pair_commute1.
repeat rewrite (cat_assoc PLT).
apply cat_respects; auto.
rewrite (cast_compose false).
rewrite (cast_compose false).
symmetry. apply H0. auto.
Qed.
Lemma alpha_cong_denote' Γ τ (m:term Γ τ) (n:term Γ τ) :
alpha_cong Γ Γ τ m n -> 〚m〛 ≈ 〚n〛.
Proof.
intros.
cut (〚m〛∘ id ≈ 〚n〛∘ id ).
intro. do 2 rewrite (cat_ident1 PLT) in H0. auto.
apply alpha_cong_denote; auto.
intros. cut (a = b). intro. subst b.
replace IN2 with IN1; auto.
apply Eqdep_dec.UIP_dec. decide equality. decide equality.
cut (forall Γ₁ Γ₂ a b, var_cong Γ₁ Γ₂ a b -> Γ₁ = Γ₂ -> a = b).
intros. eapply H1; eauto.
clear.
intros. induction H.
inv H0; auto.
inv H0; auto.
Qed.
(** We'll end up needing quite a few facts about alpha congruence.
Here we collect them together before defining the logical relation
and tackling the fundamental lemma.
*)
(** Congruence is preserved by weakening.
*)
Lemma alpha_cong_wk : forall (Γm Γn Γm' Γn':env) τ m n H₁ H₂,
(forall a b, var_cong Γm Γn a b -> var_cong Γm' Γn' a b) ->
alpha_cong Γm Γn τ m n ->
alpha_cong _ _ τ (term_wk Γm Γm' τ H₁ m)
(term_wk Γn Γn' τ H₂ n).
Proof.
intros. revert Γm' Γn' H₁ H₂ H.
induction H0; simpl; intros.
apply acong_var. apply H0. auto.
apply acong_bool.
apply acong_app; auto.
apply IHalpha_cong1. auto.
apply IHalpha_cong2. auto.
apply acong_if; auto.
apply IHalpha_cong1. auto.
apply IHalpha_cong2. auto.
apply IHalpha_cong3. auto.
apply acong_lam. apply IHalpha_cong.
intros. inv H1.
apply vcong_here; auto.
apply vcong_there; auto.
Qed.
(** Variable congruence is closely related the [inenv] relation.
*)
Lemma varcong_inenv1 Γ₁ Γ₂ a b :
var_cong Γ₁ Γ₂ a b -> exists τ, inenv Γ₁ a τ.
Proof.
intro H. induction H. unfold inenv.
simpl. destruct (string_dec x₁ y₁); eauto. contradiction.
unfold inenv. simpl.
destruct (string_dec x₁ y₁); eauto.
Qed.
Lemma varcong_inenv2 Γ₁ Γ₂ a b :
var_cong Γ₁ Γ₂ a b -> exists τ, inenv Γ₂ b τ.
Proof.
intro H. induction H. unfold inenv.
simpl. destruct (string_dec x₂ y₂); eauto. contradiction.
unfold inenv. simpl.
destruct (string_dec x₂ y₂); eauto.
Qed.
Lemma varcong_eq Γ₁ Γ₂ a b :
var_cong Γ₁ Γ₂ a b -> Γ₁ = Γ₂ -> a = b.
Proof.
intro H. induction H; simpl; intros.
inv H1; auto. inv H2; auto.
Qed.
Lemma inenv_varcong Γ a τ :
inenv Γ a τ -> var_cong Γ Γ a a.
Proof.
unfold inenv.
induction Γ; simpl; intros.
discriminate. destruct a0.
destruct (string_dec s a). subst.
apply vcong_here; auto.
apply vcong_there; auto.
Qed.
Lemma env_supp_inenv (Γ:env) a :
a ∈ ‖Γ‖ <-> exists τ, inenv Γ a τ.
Proof.
induction Γ; simpl; split; intros.
apply nil_elem in H. elim H.
destruct H. inv H.
unfold Support.support in H. simpl in H.
unfold inenv. simpl. destruct a0.
simpl in H.
destruct (string_dec c a); eauto.
apply cons_elem in H. destruct H.
apply atom_strong_eq in H.
elim n; auto.
apply IHΓ in H.
auto.
unfold inenv in H.
simpl in H.
destruct a0.
destruct (string_dec c a).
unfold Support.support. simpl.
apply cons_elem; auto.
left; auto. subst c; auto.
apply IHΓ in H.
unfold Support.support. simpl.
apply cons_elem; auto.
Qed.
(** When congruent substitutions are applied to congruence terms,
the resulting terms are congruent.
*)
Lemma term_subst_cong : forall Γ τ (m:term Γ τ) Γ' (n:term Γ' τ) Γ₁ Γ₂
(VAR1 : ENV.varmap term Γ Γ₁) (VAR2 : ENV.varmap term Γ' Γ₂),
(forall a1 a2 σ IN1 IN2,
var_cong Γ Γ' a1 a2 ->
alpha_cong Γ₁ Γ₂ σ (VAR1 a1 σ IN1) (VAR2 a2 σ IN2)) ->
alpha_cong Γ Γ' τ m n ->
alpha_cong Γ₁ Γ₂ τ
(term_subst Γ Γ₁ τ VAR1 m)
(term_subst Γ' Γ₂ τ VAR2 n).
Proof.
intros until m; induction m; simpl; intros; auto.
inv H0. simpl.
apply H. auto.
inv H0.
apply acong_bool.
inv H0.
apply acong_app; auto.
apply IHm1; auto.
apply IHm2; auto.
inv H0. simpl.
apply acong_if; auto.
apply IHm1; auto.
apply IHm2; auto.
apply IHm3; auto.
inv H0. simpl.
apply acong_lam; auto.
apply IHm. intros.
unfold ENV.shift_vars', ENV.shift_vars, ENV.extend_map, ENV.weaken_map.
hnf in IN1. hnf in IN2. simpl in IN1. simpl in IN2.
revert IN1 IN2.
destruct (string_dec x a1); simpl; intros.
destruct (string_dec x₂ a2); simpl; intros.
subst x. subst x₂. unfold eq_rect_r.
inv IN1.
replace IN1 with (Logic.eq_refl (Some σ)). simpl.
replace IN2 with (Logic.eq_refl (Some σ)). simpl.
unfold ENV.newestvar. simpl.
apply acong_var.
apply vcong_here; auto.
apply Eqdep_dec.UIP_dec. decide equality. decide equality.
apply Eqdep_dec.UIP_dec. decide equality. decide equality.
inv H1. elim n; auto.
elim H10; auto.
destruct (string_dec x₂ a2); simpl; intros.
subst x₂. inv H1.
elim n; auto. elim H11; auto.
apply alpha_cong_wk; auto.
intros.
apply vcong_there; auto.
apply varcong_inenv1 in H2.
apply env_supp_inenv in H2.
intro. subst a. revert H2.
apply fresh_atom_is_fresh'.
red; intros. apply app_elem; auto.
apply varcong_inenv2 in H2.
apply env_supp_inenv in H2.
intro. subst b. revert H2.
apply fresh_atom_is_fresh'.
red; intros. apply app_elem; auto.
apply H. inv H1. elim n; auto. auto.
auto.
Qed.
(** Evaluation commutes with alpha congruence.
*)
Lemma eval_alpha Γ τ (m z:term Γ τ) :
(m ⇓ z) -> forall Γ' (n:term Γ' τ),
alpha_cong Γ Γ' τ m n ->
exists z', (n ⇓ z') /\ alpha_cong Γ Γ' τ z z'.
Proof.
intro H. induction H; intros.
inv H. exists (tbool Γ' b). split.
apply ebool. apply acong_bool.
inv H1.
destruct (IHeval1 Γ' x2) as [m [??]]; auto.
inv H3.
destruct (IHeval2 Γ' (if b then y2 else z2)) as [n [??]]; auto.
destruct b; auto.
exists n.
split; auto.
eapply eif. eauto. auto.
inv H. exists (tlam Γ' x₂ σ₁ σ₂ m₂).
split. apply elam.
apply acong_lam. auto.
inv H2.
destruct (IHeval1 Γ' n₁0 H8) as [z1' [??]].
destruct (IHeval2 Γ' n₂0 H11) as [z2' [??]].
inv H4.
destruct (IHeval3 Γ' (subst Γ' σ₂ σ₁ x₂ m₂0 z2')) as [z' [??]].
unfold ENV.subst.
apply term_subst_cong.
intros.
inv H7.
unfold ENV.extend_map. simpl.
revert IN1 IN2. unfold inenv; simpl.
destruct (string_dec a1 a1).
destruct (string_dec a2 a2).
intros. inv IN1.
replace IN1 with (Logic.eq_refl (Some σ)). simpl.
replace IN2 with (Logic.eq_refl (Some σ)). simpl.
unfold eq_rect_r; simpl. auto.
apply Eqdep_dec.UIP_dec. decide equality. decide equality.
apply Eqdep_dec.UIP_dec. decide equality. decide equality.
elim n; auto. elim n; auto.
unfold ENV.extend_map. simpl.
revert IN1 IN2. unfold inenv; simpl.
destruct (string_dec x a1).
elim H18; auto.
destruct (string_dec x₂ a2).
elim H19; auto.
intros.
apply acong_var. auto.
auto.
exists z'; split; auto.
eapply eapp; eauto.
Qed.
(* FIXME, move earlier *)
Lemma app_not_value Γ σ (x y:term Γ σ) :
x⇓y -> forall σ₂ (m:term Γ (σ₂ ⇒ σ)) n, y = m•n -> False.
Proof.
intro H. induction H; intros; try discriminate.
eapply IHeval2; eauto.
subst z.
eapply IHeval3; eauto.
Qed.
Lemma if_not_value Γ σ (x y:term Γ σ) :
x⇓y -> forall a b c, y = tif Γ σ a b c -> False.
Proof.
intro H. induction H; intros; try discriminate.
eapply IHeval2; eauto.
subst z.
eapply IHeval3; eauto.
Qed.
(** The property of being a value is preserved
by alpha congruence.
*)
Lemma alpha_cong_value Γ Γ' σ x y :
alpha_cong Γ Γ' σ x y -> x↓ -> y↓.
Proof.
intro H. induction H; intros.
inv H0.
apply ebool.
inv H1.
eapply app_not_value in H9; eauto. elim H9.
eapply if_not_value in H2. elim H2. eauto.
apply elam.
Qed.
Lemma alpha_cong_eq Γ σ x y :
x = y ->
alpha_cong Γ Γ σ x y.
Proof.
intro. subst y. apply alpha_eq_refl.
Qed.
(* FIXME, can these lemmas be pushed into finprod somehow? *)
Lemma term_wk_ident : forall Γ σ m H,
term_wk Γ Γ σ H m = m.
Proof.
intros until m; induction m; simpl; intros; auto.
f_equal.
apply Eqdep_dec.UIP_dec. decide equality. decide equality.
f_equal; auto. apply IHm1. apply IHm2.
f_equal; auto. apply IHm1. apply IHm2. apply IHm3.
f_equal; auto. apply IHm.
Qed.
Lemma term_wk_compose : forall Γ₁ σ m Γ₂ Γ₃ H1 H2 H3,
term_wk Γ₂ Γ₃ σ H2 (term_wk Γ₁ Γ₂ σ H1 m) = term_wk Γ₁ Γ₃ σ H3 m.
Proof.
intros until m. induction m; simpl; intros; auto.
f_equal.
apply Eqdep_dec.UIP_dec. decide equality. decide equality.
f_equal.
apply (IHm1 Γ₂ Γ₃ H1 H2 H3).
apply (IHm2 Γ₂ Γ₃ H1 H2 H3).
f_equal.
apply IHm1.
apply IHm2.
apply IHm3.
f_equal.
apply IHm.
Qed.
Lemma term_wk_compose' : forall Γ₁ σ m Γ₂ Γ₃ H1 H2,
term_wk Γ₂ Γ₃ σ H2 (term_wk Γ₁ Γ₂ σ H1 m) =
term_wk Γ₁ Γ₃ σ (fun x τ H => H2 x τ (H1 x τ H)) m.
Proof.
intros. eapply term_wk_compose; eauto.
Qed.
(** Weakening commutes with substition, up to alpha congruence.
*)
Lemma term_subst_wk_cong : forall Γ τ (m:term Γ τ) Γ₁ Γ₂ Γ₃ Γ₄
(VAR1 : ENV.varmap term Γ Γ₁) (VAR2:ENV.varmap term Γ₃ Γ₄) H₁ H₂,
(forall a σ Ha1 Ha2 H,
alpha_cong _ _ σ (term_wk Γ₁ Γ₂ σ H (VAR1 a σ Ha1)) (VAR2 a σ Ha2)) ->
alpha_cong _ _ τ
(term_wk Γ₁ Γ₂ τ H₁ (term_subst Γ Γ₁ τ VAR1 m))
(term_subst Γ₃ Γ₄ τ VAR2 (term_wk Γ Γ₃ τ H₂ m)).
Proof.
intros until m. induction m; simpl; intros; auto.
apply acong_bool.
apply acong_app; auto.
apply IHm1; auto.
apply IHm2; auto.
apply acong_if; auto.
apply IHm1; auto.
apply IHm2; auto.
apply IHm3; auto.
apply acong_lam.
apply IHm; clear IHm.
intros. unfold ENV.shift_vars'. unfold ENV.shift_vars.
unfold ENV.extend_map. simpl. unfold ENV.weaken_map. simpl.
unfold ENV.newestvar. simpl. unfold ENV.newestvar_obligation_1. simpl.
generalize Ha1 Ha2. unfold inenv; simpl.
destruct (string_dec x a); simpl.
subst a. intros.
inv Ha0. unfold eq_rect_r.
replace Ha0 with (Logic.eq_refl (Some σ)).
replace Ha3 with (Logic.eq_refl (Some σ)). simpl.
apply acong_var.
apply vcong_here; auto.
apply Eqdep_dec.UIP_dec. decide equality. decide equality.
apply Eqdep_dec.UIP_dec. decide equality. decide equality.
intros.
eapply alpha_eq_trans.
apply alpha_cong_eq.
apply term_wk_compose'.
unfold ENV.tm_wk. simpl.
match goal with [ |- alpha_cong _ _ _
(traverse _ _ _ _ _ _ _ ?Q1 _)
(traverse _ _ _ _ _ _ _ ?Q2 _) ] =>
generalize Q1 Q2; intros
end.
assert (forall x τ, inenv Γ₂ x τ -> inenv ((fresh[Γ₂],σ₁)::Γ₂) x τ).
intros.
hnf. hnf in H1. simpl. simpl in H1.
rewrite H1.
set (q := fresh [Γ₂]).
simpl in q. fold q.
destruct (string_dec q x0).
subst q.
elimtype False.
clear -H1 e.
assert (x0 ∈ ‖Γ₂‖).
apply env_supp_inenv. eauto.
subst x0. revert H.
apply fresh_atom_is_fresh'.
red; intros. apply app_elem; auto.