-
Notifications
You must be signed in to change notification settings - Fork 2
/
skiy.v
1696 lines (1527 loc) · 47.8 KB
/
skiy.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 List.
Require Import Arith.
Require Import Lia.
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 profinite_adj.
Require Import strict_utils.
Require Import fixes.
Require Import flat.
(** * Soundness and adequacy simply-typed SKI with booleans and fixpoints.
The adequacy proof goes via a standard logical-relations argument.
As a corollary of the main logical-relations lemma, we show that
nonvalues are denoted by ⊥.
*)
(** We have arrow types and a single base type of booleans. *)
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 ski_scope with ski.
Open Scope ski_scope.
(** Terms are boolean constants, the standard combinators S, K and I,
and an IF/THEN/ELSE combinator; and applications. We also have
the call-by-value fixpoint combinator Y. As usual for the CBV
fixpoint operator, it only calculates fixpoints at function types.
*)
Inductive term : ty -> Type :=
| tbool : forall b:bool,
term 2
| tapp : forall σ₁ σ₂,
term (σ₁ ⇒ σ₂) ->
term σ₁ ->
term σ₂
| tI : forall σ,
term (σ ⇒ σ)
| tK : forall σ₁ σ₂,
term (σ₁ ⇒ σ₂ ⇒ σ₁)
| tS : forall σ₁ σ₂ σ₃,
term ((σ₁ ⇒ σ₂ ⇒ σ₃) ⇒ (σ₁ ⇒ σ₂) ⇒ σ₁ ⇒ σ₃)
| tIF : forall σ,
term (2 ⇒ σ ⇒ σ ⇒ σ)
| tY : forall σ₁ σ₂,
term ( ((σ₁ ⇒ σ₂) ⇒ (σ₁ ⇒ σ₂)) ⇒ (σ₁ ⇒ σ₂) ).
Arguments tapp [_ _] _ _.
Notation "x • y" := (tapp x y)
(at level 52, left associativity, format "x • y") : ski_scope.
(** The operational semantics is given in a big-step style, with the specification
of redexes split out into a separate relation.
*)
Inductive redex : forall σ₁ σ₂, term (σ₁ ⇒ σ₂) -> term σ₁ -> term σ₂ -> Prop :=
| redex_I : forall σ x,
redex _ _ (tI σ) x x
| redex_K : forall σ₁ σ₂ x y,
redex σ₂ σ₁ (tK σ₁ σ₂ • y) x y
| redex_S : forall σ₁ σ₂ σ₃ f g x,
redex _ _ (tS σ₁ σ₂ σ₃ • f • g)
x
((f•x)•(g•x))
| redex_IFtrue : forall σ th el,
redex _ _ (tIF σ • tbool true • th) el th
| redex_IFfalse : forall σ th el,
redex _ _ (tIF σ • tbool false • th) el el
| redex_Y : forall σ₁ σ₂ (f:term ((σ₁ ⇒ σ₂) ⇒ (σ₁ ⇒ σ₂))) x,
redex _ _ (tY σ₁ σ₂ • f) x
(f•(tY σ₁ σ₂ • f)•x).
Inductive eval : forall τ, term τ -> term τ -> Prop :=
| ebool : forall b, eval 2 (tbool b) (tbool b)
| eI : forall σ, eval _ (tI σ) (tI _)
| eK : forall σ₁ σ₂, eval _ (tK σ₁ σ₂) (tK _ _)
| eS : forall σ₁ σ₂ σ₃, eval _ (tS σ₁ σ₂ σ₃) (tS _ _ _)
| eIF : forall σ, eval _ (tIF σ) (tIF σ)
| eY : forall σ₁ σ₂, eval _ (tY σ₁ σ₂) (tY _ _)
| eapp1 : forall σ₁ σ₂ m₁ m₂ n₁ n₂ r z,
eval (σ₁ ⇒ σ₂) m₁ n₁ ->
eval σ₁ m₂ n₂ ->
redex σ₁ σ₂ n₁ n₂ r ->
eval σ₂ r z ->
eval σ₂ (m₁ • m₂) z
| eapp2 : forall σ₁ σ₂ m₁ m₂ n₁ n₂,
eval (σ₁ ⇒ σ₂) m₁ n₁ ->
eval σ₁ m₂ n₂ ->
~(exists r, redex σ₁ σ₂ n₁ n₂ r) ->
eval σ₂ (m₁ • m₂) (n₁ • n₂).
(** 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.
Ltac inj_ty :=
repeat match goal with
[ H : existT _ _ ?x = existT _ _ ?y |- _ ] =>
apply inj_pair2_ty in H; subst
end.
Ltac inv H :=
inversion H; subst; inj_ty; subst.
(** Values are terms that evaluate to themselves.
*)
Definition value σ (t:term σ) := eval _ t t.
Arguments value [σ] t.
(** Here are some basic techincal results
about the operational semantics.
*)
Lemma eval_value τ x y :
eval τ x y -> value y.
Proof.
intro H. induction H.
apply ebool.
apply eI.
apply eK.
apply eS.
apply eIF.
apply eY.
auto.
apply eapp2; auto.
Qed.
Lemma redex_eq τ₁ τ₂ x y z1 z2 :
redex τ₁ τ₂ x y z1 ->
redex τ₁ τ₂ x y z2 ->
z1 = z2.
Proof.
intros; inv H; inv H; inv H0; 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 H. auto.
intros. inv H. auto.
intros. inv H. auto.
intros. inv H. auto.
intros. inv H. auto.
intros. inv H3.
apply IHeval1 in H9.
apply IHeval2 in H10.
subst n₁0 n₂0.
assert (r = r0).
eapply redex_eq; eauto.
subst r0.
apply IHeval3; auto.
apply IHeval1 in H9.
apply IHeval2 in H10.
subst n₁0 n₂0.
elim H11; eauto.
intros. inv H2.
apply IHeval1 in H8.
apply IHeval2 in H9.
subst n₁0 n₂0.
elim H1. eauto.
f_equal; 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.
Lemma eval_app_congruence σ₁ σ₂ : forall x x' y y' z,
(forall q, eval _ x q -> eval _ x' q) ->
(forall q, eval _ y q -> eval _ y' q) ->
eval _ (@tapp σ₁ σ₂ x y) z ->
eval _ (@tapp σ₁ σ₂ x' y') z.
Proof.
intros.
inv H1.
apply H in H7.
apply H0 in H8.
eapply eapp1; eauto.
apply eapp2; auto.
Qed.
Lemma eval_no_redex : forall σ₁ σ₂ x x',
eval σ₂ x x' ->
forall m₁ m₂ n₁ n₂ r,
x' = @tapp σ₁ σ₂ m₁ m₂ ->
eval _ m₁ n₁ -> eval _ m₂ n₂ -> redex _ _ n₁ n₂ r -> False.
Proof.
do 5 intro. induction H; intros; try discriminate; subst.
eapply IHeval3; eauto.
inv H2.
assert (m₂0 = n₂0).
eapply eval_eq; eauto.
apply eval_trans with m₂0; auto.
assert (m₁0 = n₁0).
eapply eval_eq; eauto.
apply eval_trans with m₁0; auto.
subst.
apply H1. eauto.
Qed.
Lemma value_app_inv σ₁ σ₂ x y :
value (@tapp σ₁ σ₂ x y) ->
value x /\ value y.
Proof.
intros. inv H.
elimtype False.
eapply eval_no_redex.
apply H8. reflexivity. eauto. eauto. eauto.
split; auto.
Qed.
(*
Lemma eval_app_inv σ₁ σ₂ x y z :
eval _ (@tapp σ₁ σ₂ x y) z ->
exists x', exists y',
eval _ x x' /\ eval _ y y' /\
eval _ (x' • y') z.
Proof.
intros. inv H.
exists n₁. exists n₂.
intuition.
eapply eapp1.
eapply eval_value; eauto.
eapply eval_value; eauto.
eauto. auto.
exists n₁. exists n₂.
intuition.
apply eapp2.
eapply eval_value; eauto.
eapply eval_value; eauto.
auto.
Qed.
*)
(** "Inert" terms are those that will not evaluate if applied to
one more argument. We prove that every term at function type
is either intert or forms a redex if applied to another term.
*)
Inductive inert : forall σ₁ σ₂, term (σ₁ ⇒ σ₂) -> Prop :=
| inert_K : forall σ₁ σ₂,
inert _ _ (tK σ₁ σ₂)
| inert_S1 : forall σ₁ σ₂ σ₃,
inert _ _ (tS σ₁ σ₂ σ₃)
| inert_S2 : forall σ₁ σ₂ σ₃ x,
inert _ _ (tS σ₁ σ₂ σ₃ • x)
| inert_IF1 : forall σ,
inert _ _ (tIF σ)
| inert_IF2 : forall σ x,
inert _ _ (tIF σ • x)
| inert_Y : forall σ₁ σ₂,
inert _ _ (tY σ₁ σ₂).
Fixpoint tmsize τ (x:term τ) : nat :=
match x with
| tapp a b => (1 + tmsize _ a + tmsize _ b)%nat
| _ => 1%nat
end.
Lemma redex_inert_false : forall σ₁ σ₂ f g r,
redex σ₁ σ₂ f g r ->
inert σ₁ σ₂ f ->
False.
Proof.
intros. inv H; inv H0.
Qed.
Lemma redex_or_inert' n :
forall τ (x:term τ) σ₁ σ₂ (f:term (σ₁ ⇒ σ₂))
(Hτ : τ = (σ₁ ⇒ σ₂)%ty)
(Hx : eq_rect τ term x _ Hτ = f)
(Hsz : tmsize τ x = n),
value f ->
(forall g, exists r, redex σ₁ σ₂ f g r) \/ inert σ₁ σ₂ f.
Proof.
induction n using (well_founded_induction lt_wf).
intros τ x. rename H into Hind.
destruct x; intros; try discriminate.
subst σ₂. simpl in *. subst n f.
destruct (value_app_inv _ _ _ _ H).
assert (Hx1:tmsize _ x1 < S (tmsize _ x1 + tmsize _ x2)).
lia.
generalize (Hind (tmsize _ x1) Hx1 _ _ _ _ x1
(refl_equal _) (refl_equal _) (refl_equal _) H0).
intros. destruct H2.
destruct (H2 x2).
elimtype False. eapply eval_no_redex.
apply H. reflexivity. apply H0. apply H1. eauto.
inv H2.
left; intros. econstructor. econstructor.
right. constructor.
left; intros. econstructor. econstructor.
right. constructor.
destruct (value_app_inv _ _ _ _ H0).
inv H4.
left; intros. destruct b.
econstructor. econstructor.
econstructor. econstructor.
simpl in *.
inv H13.
elimtype False. eapply eval_no_redex.
apply H13. reflexivity. eauto. eauto. eauto.
assert (Hm₁ : tmsize _ m₁ <
S (S (S (S (tmsize (σ₁ ⇒ ty_bool) m₁ + tmsize σ₁ m₂ + tmsize σ₂0 x2))))).
lia.
destruct (value_app_inv _ _ _ _ H4).
generalize (Hind _ Hm₁ _ _ _ _ m₁
(refl_equal _) (refl_equal _) (refl_equal _) H5).
intros. destruct H11. destruct (H11 m₂).
elimtype False. eapply eval_no_redex.
apply H4. reflexivity. eauto. eauto. eauto.
inv H11.
assert (Hn₁ : tmsize _ n₁ <
S (S (S (S (tmsize (σ₁ ⇒ ty_bool) n₁ + tmsize σ₁ n₂ + tmsize σ₂0 x2))))).
lia.
destruct (value_app_inv _ _ _ _ H4). simpl in Hind.
generalize (Hind _ Hn₁ _ _ _ _ n₁
(refl_equal _) (refl_equal _) (refl_equal _) H6).
intros. destruct H13. destruct (H13 n₂).
elimtype False. eapply eval_no_redex.
apply H4. reflexivity. eauto. eauto. eauto.
inv H13.
left; intros. econstructor. econstructor.
inv Hτ.
replace Hτ with (refl_equal (σ₂ ⇒ σ₂)%ty). simpl.
left; intros. econstructor. econstructor.
apply Eqdep_dec.UIP_dec. decide equality.
inv Hτ.
replace Hτ with (refl_equal (σ₁0 ⇒ σ₂ ⇒ σ₁0)%ty). simpl.
right. constructor.
apply Eqdep_dec.UIP_dec. decide equality.
inv Hτ.
replace Hτ with (refl_equal ((σ₁ ⇒ σ₂ ⇒ σ₃) ⇒ (σ₁ ⇒ σ₂) ⇒ σ₁ ⇒ σ₃)%ty).
simpl.
right. constructor.
apply Eqdep_dec.UIP_dec. decide equality.
inv Hτ.
replace Hτ with (refl_equal (ty_bool ⇒ σ ⇒ σ ⇒ σ)%ty).
simpl.
right. constructor.
apply Eqdep_dec.UIP_dec. decide equality.
inv Hτ.
replace Hτ with (refl_equal (((σ₁ ⇒ σ₂) ⇒ σ₁ ⇒ σ₂) ⇒ σ₁ ⇒ σ₂)%ty).
simpl.
right. constructor.
apply Eqdep_dec.UIP_dec. decide equality.
Qed.
Lemma redex_or_inert :
forall σ₁ σ₂ (f:term (σ₁ ⇒ σ₂)),
value f ->
(forall g, exists r, redex σ₁ σ₂ f g r) \/ inert σ₁ σ₂ f.
Proof.
intros.
apply (redex_or_inert' (tmsize _ f) _ f _ _ f (refl_equal _));
simpl; auto.
Qed.
Lemma canonical_bool : forall x,
eval 2 x x ->
x = tbool true \/ x = tbool false.
Proof.
intros. inv H.
destruct b; auto.
elimtype False.
eapply eval_no_redex.
apply H6. reflexivity. eauto. eauto. eauto.
inv H0. clear H0.
destruct (redex_or_inert _ _ n₁); auto.
elim H5; apply H0.
inv H0.
Qed.
(** Types are interpreted as pointed domains. Booleans
are the flat domain over booleans and the arrow type
is the lifted strict function space.
*)
Fixpoint tydom (τ:ty) : ∂PLT :=
match τ with
| ty_bool => flat enumbool
| ty_arrow τ₁ τ₂ => colift (tydom τ₁ ⊸ tydom τ₂)
end.
(** Here we define the semantics of the Y combinator.
*)
Section Ydefn.
Variables σ₁ σ₂:ty.
Definition Ybody
: U (colift (tydom (σ₁ ⇒ σ₂) ⊸ tydom (σ₁ ⇒ σ₂)))
→ PLT.exp (U (tydom (σ₁ ⇒ σ₂))) (U (tydom (σ₁ ⇒ σ₂)))
(*w : U (colift (tydom (σ₁ ⇒ σ₂) ⊸ tydom (σ₁ ⇒ σ₂))) *)
:= PLT.curry (*x:U (tydom (σ₁ ⇒ σ₂)))*) (strict_curry' (*y:U tydom σ₁ *)
(* w *) (* x *) (*y*)
(strict_app' ∘ 〈strict_app' ∘ 〈π₁ ∘ π₁, π₂ ∘ π₁〉, π₂〉)
).
Lemma Ybody_unroll : forall Γ
(f:Γ → U (tydom ((σ₁ ⇒ σ₂) ⇒ (σ₁ ⇒ σ₂))))
(x:Γ → U (tydom σ₁)),
semvalue x ->
let Yf := (fixes Ybody) ∘ f in
strict_app' ∘ 〈Yf, x〉 ≈
strict_app' ∘ 〈strict_app' ∘ 〈f,Yf〉 , x〉.
Proof.
intros. unfold Yf at 1.
rewrite fixes_unroll. unfold Ybody at 1.
rewrite PLT.curry_apply2.
rewrite <- (cat_assoc PLT).
rewrite strict_curry_app2'.
rewrite (PLT.pair_compose_commute false).
rewrite <- (cat_assoc PLT).
apply cat_respects. auto.
rewrite (PLT.pair_compose_commute false).
rewrite <- (cat_assoc PLT).
rewrite (PLT.pair_compose_commute false).
rewrite <- (cat_assoc PLT).
rewrite PLT.pair_commute1.
rewrite PLT.pair_commute2.
rewrite PLT.pair_commute1.
rewrite <- (cat_assoc PLT).
rewrite PLT.pair_commute1.
rewrite PLT.pair_commute2.
rewrite (cat_ident2 PLT).
apply PLT.pair_eq. auto. auto.
auto.
Qed.
Definition Ysem Γ
: Γ → U (tydom (((σ₁ ⇒ σ₂) ⇒ (σ₁ ⇒ σ₂)) ⇒ (σ₁ ⇒ σ₂)))
:= strict_curry' (fixes Ybody ∘ π₂).
End Ydefn.
Notation "'Λ' f" := (strict_curry' f) : ski_scope.
(** The denotation of terms. The denotation of
the S, K and I combinators a straightforward interpretation of the
usual lambda term into the strict lambda and strict application
denotation functions.
*)
Fixpoint denote (τ:ty) (m:term τ) : 1 → U (tydom τ) :=
match m in term τ return 1 → U (tydom τ) with
| tbool b => flat_elem' b
| tapp m₁ m₂ => strict_app' ∘ 〈〚m₁〛,〚m₂〛〉
| tI σ => Λ(π₂)
| tK σ₁ σ₂ => Λ(Λ(π₂ ∘ π₁))
| tS σ₁ σ₂ σ₃ => Λ(Λ(Λ(
strict_app' ∘
〈 strict_app' ∘ 〈π₂ ∘ π₁ ∘ π₁, π₂〉
, strict_app' ∘ 〈π₂ ∘ π₁, π₂〉
〉
)))
| tIF σ => Λ(flat_cases' (fun b:bool =>
if b then Λ(Λ(π₂ ∘ π₁))
else Λ(Λ(π₂))
))
| tY σ₁ σ₂ => Ysem σ₁ σ₂ 1
end
where "〚 m 〛" := (denote _ m) : ski_scope.
(** This mutual induction shows that operational
values have semantic values as denotations,
and that inert operational values yield
semantic values when applied to any semantic value.
*)
Lemma value_inert_semvalue : forall n,
(forall σ x,
tmsize _ x = n ->
eval σ x x -> semvalue 〚x〛) /\
(forall σ₁ σ₂ x (y:1 → U (tydom σ₁)),
tmsize _ x = n ->
value x ->
inert σ₁ σ₂ x ->
semvalue y ->
semvalue (strict_app' ∘ 〈〚x〛, y〉)).
Proof.
intro n. induction n using (well_founded_induction lt_wf).
split; intros.
inv H1; simpl.
apply flat_elem'_semvalue.
apply strict_curry'_semvalue.
apply strict_curry'_semvalue.
apply strict_curry'_semvalue.
apply strict_curry'_semvalue.
unfold Ysem. apply strict_curry'_semvalue.
elimtype False.
eapply eval_no_redex.
apply H8. reflexivity. eauto. eauto. eauto.
inv H2. clear H2.
destruct (redex_or_inert _ _ n₁); auto.
elim H7; auto.
simpl in H.
assert (Hm1 : (tmsize _ n₁) < S (tmsize _ n₁ + tmsize _ n₂)).
lia.
destruct (H _ Hm1).
apply H3; auto.
clear H2 H3.
assert (Hm2 : (tmsize _ n₂) < S (tmsize _ n₁ + tmsize _ n₂)).
lia.
destruct (H _ Hm2).
apply H2; auto.
inv H2; simpl.
rewrite strict_curry_app'; auto.
apply strict_curry'_semvalue2.
rewrite strict_curry_app'; auto.
apply strict_curry'_semvalue2.
rewrite strict_curry_app'; auto.
rewrite strict_curry_app2'; auto.
apply strict_curry'_semvalue2.
destruct (value_app_inv _ _ _ _ H1); auto.
simpl in H.
assert (tmsize _ x0 < S (S (tmsize _ x0))). lia.
destruct (H _ H5). apply (H6 _ x0); auto.
rewrite strict_curry_app'; auto.
destruct (flat_elem_canon enumbool y H3) as [b ?].
rewrite H0.
simpl.
rewrite flat_cases_elem'.
destruct b.
apply strict_curry'_semvalue2.
apply strict_curry'_semvalue2.
rewrite strict_curry_app'; auto.
destruct (value_app_inv _ _ _ _ H1); auto.
destruct (canonical_bool x0); auto; subst x0; simpl.
rewrite flat_cases_elem'.
rewrite strict_curry_app2'; auto.
apply strict_curry'_semvalue2.
rewrite flat_cases_elem'.
rewrite strict_curry_app2'; auto.
apply strict_curry'_semvalue2.
destruct (value_app_inv _ _ _ _ H1); auto.
simpl in H.
assert (tmsize _ x0 < S (S (tmsize _ x0))). lia.
destruct (H _ H5). apply (H6 _ x0); auto.
unfold Ysem.
rewrite strict_curry_app'; auto.
rewrite (fixes_unroll _ _ (Ybody σ₁0 σ₂0)).
unfold Ybody at 1.
rewrite PLT.curry_apply2.
rewrite <- (cat_assoc PLT).
rewrite <- (cat_assoc PLT).
apply strict_curry'_semvalue2.
Qed.
Lemma value_semvalue : forall σ (x:term σ),
value x -> semvalue 〚x〛.
Proof.
intros. destruct (value_inert_semvalue (tmsize _ x)); auto.
Qed.
Lemma inert_semvalue σ₁ σ₂ x y :
value x -> inert σ₁ σ₂ x -> semvalue y ->
semvalue (strict_app' ∘ 〈〚x〛, y 〉).
Proof.
intros.
destruct (value_inert_semvalue (tmsize _ x)).
apply H3; auto.
Qed.
Hint Resolve value_semvalue.
(** Now we can show the soundness of redexes.
*)
Lemma redex_soundness : forall σ₁ σ₂ x y z,
value x ->
value y ->
redex σ₁ σ₂ x y z ->
strict_app' ∘ 〈〚x〛,〚y〛〉 ≈ 〚z〛.
Proof.
intros. inv H1.
inv H1. simpl.
rewrite strict_curry_app'; auto.
rewrite PLT.pair_commute2. auto.
simpl.
rewrite strict_curry_app'; auto.
rewrite strict_curry_app2'; auto.
rewrite <- (cat_assoc PLT).
rewrite PLT.pair_commute1.
rewrite PLT.pair_commute2. auto.
destruct (value_app_inv _ _ _ _ H); auto.
destruct (value_app_inv _ _ _ _ H).
destruct (value_app_inv _ _ _ _ H2). clear H4.
simpl.
rewrite strict_curry_app'; auto.
rewrite strict_curry_app2'; auto.
rewrite strict_curry_app2'; auto.
repeat rewrite <- (cat_assoc PLT).
rewrite (PLT.pair_compose_commute false).
repeat rewrite <- (cat_assoc PLT).
rewrite (PLT.pair_compose_commute false).
repeat rewrite <- (cat_assoc PLT).
repeat rewrite PLT.pair_commute1.
repeat rewrite PLT.pair_commute2.
rewrite (PLT.pair_compose_commute false).
repeat rewrite <- (cat_assoc PLT).
repeat rewrite PLT.pair_commute1.
repeat rewrite PLT.pair_commute2.
auto.
apply (value_semvalue _ g); auto.
apply (value_semvalue _ f); auto.
destruct (value_app_inv _ _ _ _ H). clear H2.
simpl.
rewrite strict_curry_app'; auto.
rewrite flat_cases_elem'.
rewrite strict_curry_app2'; auto.
rewrite strict_curry_app2'; auto.
repeat rewrite <- (cat_assoc PLT).
repeat rewrite PLT.pair_commute1.
repeat rewrite PLT.pair_commute2. auto.
apply flat_elem'_semvalue.
destruct (value_app_inv _ _ _ _ H). clear H2.
inv H1.
simpl.
rewrite strict_curry_app'; auto.
rewrite flat_cases_elem'.
rewrite strict_curry_app2'; auto.
rewrite strict_curry_app2'; auto.
repeat rewrite PLT.pair_commute2. auto.
apply flat_elem'_semvalue.
destruct (value_app_inv _ _ _ _ H). clear H1 H2.
simpl.
unfold Ysem.
rewrite strict_curry_app'; auto.
rewrite fixes_unroll at 1. unfold Ybody at 1.
rewrite PLT.curry_apply2.
rewrite <- (cat_assoc PLT).
rewrite PLT.pair_commute2.
rewrite <- (cat_assoc PLT).
rewrite strict_curry_app2'; auto.
rewrite <- (cat_assoc PLT).
apply cat_respects. auto.
rewrite (PLT.pair_compose_commute false).
rewrite PLT.pair_commute2.
apply PLT.pair_eq. 2: auto.
rewrite <- (cat_assoc PLT).
apply cat_respects. auto.
rewrite (PLT.pair_compose_commute false).
apply PLT.pair_eq.
rewrite <- (cat_assoc PLT).
rewrite PLT.pair_commute1.
rewrite (cat_assoc PLT).
rewrite PLT.pair_commute1.
apply cat_ident2.
rewrite <- (cat_assoc PLT).
rewrite PLT.pair_commute1.
rewrite (cat_assoc PLT).
rewrite PLT.pair_commute2.
rewrite <- (cat_assoc PLT).
rewrite PLT.pair_commute2.
auto.
apply (value_semvalue _ f). auto.
Qed.
(** This leads easily into the soundness of evaluation.
*)
Lemma soundness : forall τ (m z:term τ),
eval τ m z -> 〚m〛≈〚z〛.
Proof.
intros. induction H; simpl; auto.
rewrite IHeval1.
rewrite IHeval2.
rewrite <- IHeval3.
apply redex_soundness.
eapply eval_value; eauto.
eapply eval_value; eauto.
auto.
rewrite IHeval1.
rewrite IHeval2.
auto.
Qed.
(** * The logical relations lemma
Now define the logical relation for adequacy. It is defined
by induction on the structure of types, in a standard way.
*)
Fixpoint LR (τ:ty) :
term τ -> (1 → U (tydom τ)) -> Prop :=
match τ as τ' return
term τ' -> (1 → U (tydom τ')) -> Prop
with
| ty_bool => fun m h => exists b:bool,
m = tbool b /\ h ≈ flat_elem' b
| ty_arrow σ₁ σ₂ => fun m h =>
forall n h',
LR σ₁ n h' -> value n -> semvalue h' ->
semvalue (strict_app' ∘ 〈h, h'〉) ->
exists z,
eval _ (m • n) z /\
LR σ₂ z (strict_app' ∘ 〈h, h'〉)
end.
Lemma LR_equiv τ : forall m h h',
h ≈ h' -> LR τ m h -> LR τ m h'.
Proof.
induction τ; simpl. intros.
destruct H0 as [b [??]]. exists b; split; auto.
rewrite <- H; auto.
simpl; intros.
destruct (H0 n h'0 H1 H2 H3) as [z [??]]; auto.
revert H4. apply semvalue_equiv.
apply cat_respects; auto.
apply PLT.pair_eq; auto.
exists z.
split; auto.
revert H6. apply IHτ2.
apply cat_respects; auto.
apply PLT.pair_eq; auto.
Qed.
(** Now we need a host of auxilary definitions to state
the main lemmas regarding the logical relation. These
definitions allow us to apply an arbitrary number of
arguments to a syntactic term and to the denotation of terms.
*)
Fixpoint lrtys (ts:list ty) (z:ty) :=
match ts with
| nil => z
| t::ts' => (t ⇒ (lrtys ts' z))%ty
end.
Fixpoint lrsyn (ts:list ty) : Type :=
match ts with
| nil => unit
| t::ts' => prod (lrsyn ts') (term t)
end.
Fixpoint lrsem (ts:list ty) : Type :=
match ts with
| nil => unit
| t::ts' => prod (lrsem ts') (1 → U (tydom t))
end.
Fixpoint lrhyps (ls:list ty) : lrsyn ls -> lrsem ls -> Prop :=
match ls with
| nil => fun _ _ => True
| t::ts => fun xs ys =>
(eval _ (snd xs) (snd xs) /\ semvalue (snd ys)) /\
LR t (snd xs) (snd ys) /\ lrhyps ts (fst xs) (fst ys)
end.
Fixpoint lrapp (ls:list ty) z : lrsyn ls -> term (lrtys ls z) -> term z :=
match ls as ls' return lrsyn ls' -> term (lrtys ls' z) -> term z with
| nil => fun _ m => m
| t::ts => fun xs m => lrapp ts _ (fst xs) (m • (snd xs))
end.
Fixpoint lrsemapp (ls:list ty) z :
lrsem ls -> (1 → U (tydom (lrtys ls z))) -> (1 → U (tydom z)) :=
match ls as ls' return
lrsem ls' -> (1 → U (tydom (lrtys ls' z))) -> (1 → U (tydom z))
with
| nil => fun _ h => h
| t::ts => fun ys h => lrsemapp ts _ (fst ys) (strict_app' ∘ 〈h, snd ys〉)
end.
Lemma eval_lrapp_congruence ls : forall xs τ m m' z,
(forall q, eval _ m q -> eval _ m' q) ->
eval τ (lrapp ls τ xs m) z ->
eval τ (lrapp ls τ xs m') z.
Proof.
induction ls; simpl; intros.
apply H. auto.
fold lrtys in *.
revert H0. apply IHls.
intros.
inv H0.
apply H in H6.
eapply eapp1; eauto.
eapply eapp2; eauto.
Qed.
Lemma lrsemapp_equiv ls : forall τ ys h h',
h ≈ h' -> lrsemapp ls τ ys h ≈ lrsemapp ls τ ys h'.
Proof.
induction ls; simpl; intros; auto.
apply IHls.
apply cat_respects; auto.
apply PLT.pair_eq; auto.
Qed.
Lemma semvalue_lrsemapp_out ls : forall τ ys h,
semvalue (lrsemapp ls τ ys h) -> semvalue h.
Proof.
induction ls; simpl; intros; auto.
apply IHls in H.
apply semvalue_app_out1' in H. auto.
Qed.
(** This fact is important in the base cases of the fundamental lemma; it allows
unwind a stack of applications.
*)
Lemma LR_under_apply ls :
forall (τ : ty) (m z0 : term (lrtys ls τ)) (xs : lrsyn ls)
(ys : lrsem ls) (h : 1 → U (tydom (lrtys ls τ))),
eval (lrtys ls τ) m z0 ->
lrhyps ls xs ys ->
semvalue (lrsemapp ls τ ys h) ->
LR (lrtys ls τ) z0 h ->
exists z : term τ,
eval τ (lrapp ls τ xs m) z /\ LR τ z (lrsemapp ls τ ys h).
Proof.
induction ls; simpl; intros.
exists z0. split; auto.
destruct xs as [xs x].
destruct ys as [ys y]. simpl in *.
destruct H0 as [[??][??]].
destruct (H2 x y) as [z1 [??]]; auto.
apply semvalue_lrsemapp_out in H1. auto.
generalize (IHls τ (tapp z0 x) z1 xs ys (strict_app' ∘ PLT.pair h y)
H6 H5 H1 H7).
intros [q [??]].
exists q; split; auto.
revert H8.
apply eval_lrapp_congruence. intro.
apply eval_app_congruence; auto.
fold lrtys. intros.
apply eval_trans with z0; auto.
Qed.
(** If a sup is a semantic value, then there is some element of the
set is a semantic value.
*)
Lemma semvalue_sup (B:∂PLT) (XS:dirset (PLT.homset_cpo _ 1 (U B))) :
semvalue (∐XS) -> exists x, x ∈ XS /\ semvalue x.
Proof.
intros.
destruct (H tt) as [q ?].
simpl in H0.
apply union_axiom in H0.
destruct H0 as [q' [??]].
apply image_axiom2 in H0.
destruct H0 as [q'' [??]].
simpl in *.
exists q''. split; auto.
red; intro. destruct g.
exists q. rewrite <- H2; auto.
Qed.
(** The logical relation is admissible. This is key to the
fundamental lemma case for Y.
*)
Lemma LR_admissible τ :
forall m (XS:dirset (PLT.homset_cpo _ 1 (U (tydom τ)))),
semvalue (∐XS) ->
(forall x, x ∈ XS -> semvalue x -> LR τ m x) -> LR τ m (∐XS).
Proof.
induction τ; simpl. intros.
apply semvalue_sup in H. destruct H as [x [??]].
destruct (H0 x) as [b [??]]; auto.
subst m. exists b. split; auto.
split.
apply CPO.sup_is_least.
hnf; simpl; intros.
destruct (proj2_sig XS (x::x0::nil)). hnf; auto.
hnf; intros. apply cons_elem in H4.
destruct H4. rewrite H4. auto.
rewrite (cons_elem _ x0 nil a) in H4.
destruct H4. rewrite H4. auto.
apply nil_elem in H4. elim H4.
destruct H4.
assert (x1 ≈ x).
assert (x ≤ x1). apply H4. apply cons_elem; auto.
split; auto.
hnf; intros.
rewrite H3 in H6.
assert ((tt,Some b : U (flat enumbool)) ∈ PLT.hom_rel x1).
apply H6.
unfold flat_elem'.
apply PLT.compose_hom_rel. exists (Some tt).
split. simpl. apply adj_unit_rel_elem. auto.
apply U_hom_rel. right.
exists tt. exists b. split; auto.
apply PLT.compose_hom_rel; auto.
exists tt. split.
simpl. apply eprod_elem. split; simpl.
apply eff_complete. apply single_axiom; auto.
simpl. apply single_axiom; auto.
destruct H3. apply H9.
destruct a. unfold flat_elem'.
apply PLT.compose_hom_rel.
exists (Some tt). split.
simpl. apply adj_unit_rel_elem; simpl; auto.
destruct c; auto.
apply U_hom_rel.
destruct c0; auto. right.
exists tt. exists c0. split; auto.
apply PLT.compose_hom_rel.
exists tt.
split. simpl.
apply eprod_elem. split.
apply eff_complete. apply single_axiom; auto.
simpl. apply single_axiom; auto.
cut (c0 = b). intros. subst c0; auto.
destruct c.
destruct (PLT.hom_directed _ _ _ x1 tt ((Some c0::Some b::nil))).
hnf; auto.
red; intros.
rewrite (cons_elem _ _ _ a) in H10. destruct H10. rewrite H10.
apply erel_image_elem. auto.
apply cons_elem in H10. destruct H10. rewrite H10.
apply erel_image_elem. auto.
apply nil_elem in H10. elim H10.
destruct H10.
assert (Some c0 ≤ x2).
apply H10. apply cons_elem; auto.
assert (Some (b:enumbool) ≤ x2).
apply H10. apply cons_elem. right. apply cons_elem; auto.
destruct x2. hnf in H12. hnf in H13.
subst c0. subst b. auto.
elim H12.