-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfscd-term.tex
1395 lines (1189 loc) · 57.4 KB
/
fscd-term.tex
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
\documentclass[a4paper, 11pt,titlepage, openright, twoside]{report}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{silence}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{mathtools}
\usepackage{enumitem}
\usepackage{newunicodechar}
\usepackage[margin=3cm,bindingoffset=1cm]{geometry}
\usepackage{stmaryrd}
\SetSymbolFont{stmry}{bold}{U}{stmry}{m}{n}
% https://tex.stackexchange.com/a/106719
\DeclareSymbolFont{sfletters}{OML}{cmbrm}{m}{it}
\DeclareMathSymbol{\slambda}{\mathord}{sfletters}{"15}
%https://tex.stackexchange.com/a/335857
\usepackage{microtype}
\usepackage[dvipsnames]{xcolor}
\usepackage{mathpartir}
\usepackage[style=alphabetic]{biblatex}
\usepackage{hyperref}
\usepackage{cleveref}
\usepackage{tikz}
\usepackage{listings}
\lstset{basicstyle= \footnotesize \ttfamily}
\lstset{language=ml}
\usepackage[nodisplayskipstretch]{setspace}
\setlength{\parskip}{0pt}
\addbibresource{refs.bib}
\title{\textbf{Normalization for algebraic effect handlers}}
\author{Wiktor Kuchta}
\date{29 czerwca 2022} %TODO
\usepackage{titling}
\renewcommand \maketitlehookb {
\begin{center}\large
Normalizacja dla handlerów efektów algebraicznych
\end{center}
\vfil
}
\renewcommand \maketitlehookc {
\vfil
\begin{center}
\large Praca licencjacka \\[0.85em]
\begin{tabular}[t]{rl}
\textbf{Promotor:} & dr hab. Dariusz Biernacki
\end{tabular}\end{center}
\vfil\vfil\vfil\vfil
\begin{center}Uniwersytet Wroc\l{}awski\\
Wydzia\l{} Matematyki i Informatyki\\
Instytut Informatyki
\end{center}
}
\newunicodechar{λ}{\slambda}
\newcommand{\keyword}[1]{\textsf{\textup{#1}}}
\newcommand{\KwDo}{\keyword{do}}
\newcommand{\Do}{\KwDo\;}
\newcommand{\KwHandle}{\keyword{handle}}
\newcommand{\Handle}{\KwHandle\;}
\newcommand{\Lift}[1]{\boldsymbol{[}#1\boldsymbol{]}}
\newcommand{\subst}[2]{\{#1/#2\}}
\newcommand{\E}{\mathcal{E}}
\newcommand{\K}{\mathcal{K}}
\renewcommand{\S}{\mathcal{S}}
\newcommand{\kT}{\mathsf{T}}
\newcommand{\kE}{\mathsf{E}}
\newcommand{\kR}{\mathsf{R}}
\newcommand{\Free}{\textrm{-}\mathrm{free}}
\newcommand{\Obs}{\mathrm{Obs}}
\newcommand{\N}{\mathbb{N}}
\DeclareMathOperator{\dom}{dom}
\newcommand{\+}{\enspace}
\newtheorem{corollary}{Corollary}
\newtheorem{lemma}{Lemma}
\newtheorem{theorem}{Theorem}
\newunicodechar{│}{\mid} % Digr vv
\newunicodechar{╱}{\mathbin{/}} % Digr FD
\newunicodechar{∷}{::} % Digr ::
\newunicodechar{□}{\square} % Digr OS
\newunicodechar{∅}{\emptyset} % Digr /0
\newunicodechar{α}{\alpha}
\newunicodechar{β}{\beta}
\newunicodechar{δ}{\delta} % Digr d*
\newunicodechar{ε}{\varepsilon}
\newunicodechar{γ}{\gamma} % Digr g*
\newunicodechar{ι}{\iota} % Digr i*
\newunicodechar{κ}{\kappa}
\newunicodechar{μ}{\mu}
\newunicodechar{ν}{\nu}
\newunicodechar{ρ}{\rho}
\newunicodechar{σ}{\sigma}
\newunicodechar{τ}{\tau}
\newunicodechar{η}{\eta} % Digr y*
\newunicodechar{Δ}{\Delta}
\newunicodechar{Γ}{\Gamma}
\newunicodechar{Ω}{\Omega} % digr W*
\newunicodechar{ℕ}{\N} % Digr NN 8469 nonstandard
\newunicodechar{⊆}{\subseteq} % Digr (_
\newunicodechar{∪}{\cup} % Digr )U
\newunicodechar{∈}{\in} % Digr (-
\newunicodechar{∃}{\exists} % Digr TE
\newunicodechar{∀}{\forall} % Digr FA
\newunicodechar{∧}{\wedge} % Digr AN
\newunicodechar{∨}{\vee} % Digr OR
\newunicodechar{⊥}{\bot} % Digr -T
\newunicodechar{⊢}{\vdash} % Digr \- 8866 nonstandard
\newunicodechar{⊨}{\models} % Digr \= 8872 nonstandard
\newunicodechar{⊤}{\top} % Digr TO 8868 nonstandard
\newunicodechar{⇒}{\implies} % Digr =>
\newunicodechar{⇔}{\iff} % Digr ==
\newunicodechar{↦}{\mapsto} % Digr T> 8614 nonstandard
\newunicodechar{⟦}{\llbracket} % Digr [[ 10214 nonstandard (needs pkg stmaryrd)
\newunicodechar{⟧}{\rrbracket} % Digr ]] 10215 nonstandard
% cursed
\WarningFilter{newunicodechar}{Redefining Unicode}
\newunicodechar{·}{\ifmmode\cdot\else\textperiodcentered\fi} % Digr .M
\newunicodechar{×}{\ifmmode\times\else\texttimes\fi} % Digr *X
\newunicodechar{→}{\ifmmode\rightarrow\else\textrightarrow\fi} % Digr ->
\newunicodechar{…}{\ifmmode\dots\else\textellipsis\fi} % Digr .,
\begin{document}
\maketitle
\thispagestyle{empty}
\cleardoublepage
\begin{abstract}
Algebraic effects and handlers are gaining popularity in the theory and practice
of programming languages as they provide unparalleled ability to define and combine computational effects.
In this work,
we demonstrate a novel proof of termination of evaluation and type soundness for
a $λ$-calculus with deep effect handlers and a type-and-effect system that
disallows recursive effects.
The key idea is a fixed-point construction of logical relations for the language.
\begin{center} \rule[3pt]{300pt}{1pt} \end{center}
Efekty algebraiczne i ich handlery zyskują na popularności w teorii i praktyce
języków programowania dzięki ich niezrównanym możliwościom definiowania i łączenia efektów obliczeniowych.
W niniejszej pracy
prezentujemy nowy dowód terminacji ewaluacji i poprawności typów
dla rachunku $λ$ z głębokimi handlerami oraz systemem typów i efektów zabraniającym
rekurencyjnych efektów.
Kluczowym pomysłem jest stałopunktowa konstrukcja relacji logicznych dla języka.
\vfil
Wyniki badań powstały w ramach projektu „Szkoła Orłów” realizowanego na Uniwersytecie Wrocławskim
i współfinansowanego ze środków Unii Europejskiej z Europejskiego Funduszu Społecznego w ramach
Programu Operacyjnego Wiedza Edukacja Rozwój.
\end{abstract}
\thispagestyle{empty}
\cleardoublepage
\setcounter{page}{5}
\tableofcontents
\chapter{Introduction}
A programming language needs to be more than just a lambda calculus,
capable only of functional abstraction and evaluation of expressions.
Programs need to have an effect on the outside world and, thinking more locally,
in our programs we would like to have fragments that do not merely reduce to a value in isolation,
but also nontrivially affect the execution of surrounding code.
This is what we call computational effects, the typical examples of which are:
input/output, mutable state, exceptions, nondeterminism, and coroutines.
Today, we are at the mercy of programming language designers
and we can only hope they provide the
effects we would like to use and make sure that they all interact with
each other well.
A traditional way to remedy this is to (purely functionally) model effects using monads \cite{monads}.
This requires the use of monadic style, which among other things causes
stratification of code into two styles and does not always cooperate well with
the language's native facilities.
Moreover, monads do not compose in general, so modular programming with monads has some overheads.
\section{Algebraic effects}
In recent years, a new promising approach has emerged: \textit{algebraic effects and handlers} \cite{Plotkin_2013}.
A language featuring algebraic effects lets programmers express all common computational effects as library code
and use them in the usual direct style.
This is done by decoupling effects into purely syntactic {\em operations} with their type signatures % TODO? awkward
and giving them meaning separately, here by using {\em effect handlers}.
Effect handlers generalize exception handlers.
We can \textit{perform} an operation,
just as we can raise an exception in a typical high-level programming language.
The operation is then handled by the nearest enclosing \textit{handler} for the specific operation.
The handler receives the value given at perform-point and also,
unlike normal exception handlers,
a \textit{resumption} – a first-class functional value representing the rest
of the code to execute inside the handler from the perform-point.
By calling the resumption with a value we can resume at the perform-point
as if performing the operation evaluated to the value.
But more interestingly, we can resume multiple times, or never, or store the resumption for later use.
To prevent crashes because of unhandled operations and aid understanding of effectful code,
languages with algebraic effects typically have powerful
\textit{type-and-effect systems},\footnote{
We will often say just ``type'' when referring to both types and effects.}
not only tracking the type of value an expression might evaluate to, but also
what kind of effects it may perform along the way.
Type-and-effect systems allow us to deduce some basic information about programs,
for example that a pure expression cannot evaluate to two different values,
or, in some systems, that it will {\em always} evaluate to a value.
\section{Contributions}
In this work we demonstrate a (to our knowledge) novel proof of
termination of evaluation (also known as normalization) for a
language \cite{fscd19} equipped with algebraic effects.
We obtain type soundness as a corollary.
We also explain how the proof can carry over to other calculi.
Further, we present code that mirrors the computational content of the proof.
Termination of evaluation might be of interest for several reasons,
for example, it can simplify formal semantics and reasoning, just by virtue of
eliminating nontermination as a case to consider.
It can also be regarded as another safety property, just like ``never crashing''
(though in practice nonterminating programs are indistinguishable from {very long}-running ones).
Although most programming languages support recursion, loops, and other
features allowing for nontermination (e.g., recursive types),
it might be useful to know that fragments that do not use these features cannot be blamed
for it.
Nontermination is sometimes also viewed as a computational effect,
which however does not neatly fit the algebraic effect framework.
Despite that, it can still be added as a built-in effect to be tracked by
the effect system, provided that we have sufficiently identified possible sources
of nontermination.
This is done, for example, in the Koka programming language \cite{koka}.
Normalization also implies that the calculus is sound as a logic,
looking through the lens of the Curry-Howard isomorphism \cite{ch}.
However, it is hard to give logical interpretations to effect annotations \cite{oleg}
and it is unclear how useful it is to treat calculi with algebraic effects as logics.
The primary tool for proving normalization (and many other properties) of
typed formal calculi is {\em logical relations} \cite{lr}.\footnote{
In the context of normalization, logical relations have many names, including:
(Tait's) reducibility predicates, reducibility candidates, and saturated sets.
}
Logical relations are
type-indexed relations on terms that essentially help carry additional
information about {\em local} behavior of terms to make a type derivation-directed
proof of the {\em global} property (such as normalization)
have strong enough induction hypotheses and ultimately go through.
In this work we define direct-style logical relations
to prove termination
of evaluation in our language.
A novelty of our approach
is that the definition of the logical relations
is recursive, more concretely, it is a solution to a fixed-point equation.
%This is unlike any normalization proof we know of.
The fixed-point construction replaces the use of step-indexing
which appears in the logical relation construction we are most directly inspired by \cite{hwc}.
\section{Acknowledgements}
I would like to thank Dariusz Biernacki, Filip Sieczkowski, and Piotr Polesiuk
for their help with this work.
\chapter{The language}\label{chap:language}
We will study the deep handler calculus and type-and-effect system formulated
in \cite{fscd19}.
It is a refreshingly minimal language – the call-by-value lambda calculus with a few extensions
to be able to express the essence of algebraic effects.
There is only the universal operation, performed $\Do v$.
To be able to reach beyond the closest effect handler,
the \textit{lift} operator, written $\Lift{e}$, is introduced,
which makes operations in $e$ skip the nearest handler.
In contrast to much work on algebraic effects, the effect-tracking system here is structural –
signatures of effects do not have to be separately defined and named before their use.
Finally, the type system features polymorphic expressions and polymorphic operations.
%\section{Formal definition}
\section{Untyped syntax and semantics}
The syntax of the calculus is shown in \cref{syntax}.
We use metavariables $x$ and $r$ to refer to expression variables.
As is standard, we work modulo $α$-equivalence and distinguish separate instances
of metavariables by using subscripts, superscripts, or adding primes.
Capture-avoiding substitution of $v$ for $x$ in $e$ is denoted $e\subst{v}{x}$.
Evaluation contexts are expressions with a hole ($□$) and
encode a left-to-right evaluation order.
We use the standard notation $K[x]$ for substituting $x$ for the hole in $K$.
There is a concept of {\em freeness} of evaluation contexts (\cref{freeness}),
a quantity increased by lifts and decreased by handlers.
Intuitively, an evaluation context is $n\Free$
if an operation performed inside the context (in place of the hole) % in the place?
would be handled by the $n$-th handler outside of the context.
Naturally, freeness is used in the operational semantics (\cref{reduction}),
where we can see that effect handling occurs if there
is a $0\Free$ context between the operation and the handler.
The resumption $v_c$ wraps the context $K$
with the same handler, which is why the effect handlers are called {\em deep}
– all operations are handled, not just the first one (as in so-called {\em shallow} handlers).
The other rules are the standard $β$-reduction of the $λ$-calculus
and reductions in cases where an (effect-free) value is wrapped by an effect-related construct.
It is straightforward to see that reduction is deterministic
and compatible with respect to evaluation contexts.
\section{Type-and-effect system}
In \cref{types} the type system is introduced.
We have three kinds: types ($\kT$), effects ($\kE$), and rows ($\kR$).
We use $α$ to range over type variables and
$Δ$ to range over type contexts, which assign kinds to type variables.
Similarly, contexts $Γ$ assign types to term-level variables.
We furthermore have the concept of {\em type-level substitutions}:
we write $Δ ⊢ δ ∷ Δ'$ if $δ$ maps type variables in $Δ'$ to types of the corresponding kinds,
well-formed in $Δ$.
We assume that all types are well-formed (well-kinded) in the appropriate contexts in the sense of \cref{kinding}.
Types can be function types or universal types.
Effects consist of an operation argument type and return type,
possibly with variables bound by the type context.
Rows are lists of effects,
with $ι$ denoting the empty row.
Type-level substitutions are used only in the rule for $\KwDo$
to instantiate the polymorphic variables $Δ'$ of effect $Δ'.\,τ_1 \Rightarrow τ_2$.
Complementarily, the rule for $\KwHandle$ states that the effect handler clause $e_h$
has to be essentially parametric in $Δ'$.
We can intuitively understand effect rows as follows:
if $e : τ ╱ ρ$ and effect $ε$ appears at position $n$ in $ρ$, then
subexpressions performing $ε$ are wrapped by $n$ lifts inside $e$.
Clearly, the order of effects in the row matters.
The type system enables {\em row polymorphism}
since we can have a row ending in a polymorphic type variable.
Such rows are usually called {\em open} (and {\em closed} otherwise).
In \cref{subtyping} the subtyping rules are introduced.
Their main purpose is to allow
effect rows to be subsumed by rows with more effects at the end.
For that to be of use in more places,
we also have subsumption rules for universal types and function types. % polymorphic?
In particular, closed rows are subsumed by open rows.
\begin{figure}
\begin{align*}
%\textrm{Var} ∋ f,r,x,y,… & \\%&\textrm{(variables)}\\
v,u &::= x │ λx.\,e &\textrm{(values)}\\
e &::=
v │ e\;e │ \Lift{e} │ \Do v │ \Handle e\,\{x,r.\,e;\,x.\,e\}
&\textrm{(expressions)}\\
K &::=
□ │ K\;e │ v\;K │ \Lift{K} │ \Handle K\,\{x,r.\,e;\,x.\,e\}
&\textrm{(evaluation contexts)}
\end{align*}
\caption{Syntax.}
\label{syntax}
\end{figure}
\begin{figure}
\begin{mathpar}
\inferrule
{ }
{0\Free(□)}
\inferrule
{n\Free(K)}
{n\Free(K\;e)}
\inferrule
{n\Free(K)}
{n\Free(v\;K)}
\inferrule
{n\Free(K)}
{{n+1}\Free(\Lift{K})}
\inferrule
{{n+1}\Free(K)}
{n\Free(\Handle K\,\{x,r.\,e_h;\,x.\,e_r\})}
\end{mathpar}
\caption{Evaluation context freeness.}
\label{freeness}
\end{figure}
\begin{figure}
\begin{mathpar}
\inferrule
{e_1 ↦ e_2}
{K[e_1] → K[e_2]}
(λx.\,e)\;v ↦ e\subst{v}{x}
\Lift{v} ↦ v
\inferrule
{0\Free(K) \\ v_c = λz.\,\Handle K[z]\,\{x,r.\,e_h;\,x.\,e_r\}}
{\Handle K[\Do v]\,\{x, r.\,e_h;\,x.\,e_r\} ↦ e_h\subst{v}{x}\subst{v_c}{r}}
\Handle v\,\{x,r.\,e_h;\,x.\,e_r\} ↦ e_r\subst{v}{x}
\end{mathpar}
\caption{Contraction ($↦$) and single-step reduction ($→$).}
\label{reduction}
\end{figure}
\clearpage
\begin{figure}
\begin{mathpar}
κ ::= \kT │ \kE │ \kR
%(type variables): α,β,…
%Δ ::= · │ α::κ, Δ
%Γ ::= · │ x:τ, Γ
σ,τ,ε,ρ ::=
α │ τ→_ρτ │ ∀α::κ.\,τ │ ι │ Δ.\,τ \Rightarrow τ │ ε · ρ \\
\inferrule
{x : τ ∈ Γ}
{Δ;Γ ⊢ x : τ ╱ ι}
\inferrule
{Δ ⊢ τ_1 ∷ \kT \\ Δ;Γ, x:τ_1 ⊢ e : τ_2 ╱ ρ}
{Δ;Γ ⊢ λx.\,e : τ_1 →_ρ τ_2 ╱ ι}
\inferrule
{Δ;Γ ⊢ e_1 : τ_1 →_ρ τ_2 ╱ ρ \\ Δ;Γ ⊢ e_2 : τ_1 ╱ ρ}
{Δ;Γ ⊢ e_1\,e_2 : τ_2 ╱ ρ}
\inferrule
{Δ ⊢ ε ∷ \kE \\ Δ;Γ ⊢ e : τ ╱ ρ}
{Δ;Γ ⊢ \Lift{e} : τ ╱ ε·ρ}
\inferrule
{Δ,α∷κ;Γ ⊢ e : τ ╱ ι}
{Δ;Γ ⊢ e : ∀α∷κ.\,τ ╱ ι}
\inferrule
{Δ ⊢ σ ∷ κ \\ Δ;Γ ⊢ e : ∀α∷κ.\, τ ╱ ρ}
{Δ;Γ ⊢ e : τ\subst{σ}{α} ╱ ρ}
\inferrule
{Δ;Γ ⊢ v : δ(τ_1) ╱ ι \\ Δ ⊢ δ ∷ Δ' \\ Δ ⊢ Δ'.\, τ_1 \Rightarrow τ_2 ∷ \kE}
{Δ;Γ ⊢ \Do v : δ(τ_2) ╱ (Δ'.\, τ_1 \Rightarrow τ_2)}
\inferrule
{Δ;Γ ⊢ e : τ ╱ (Δ'.\,τ_1 \Rightarrow τ_2) · ρ \\
Δ,Δ';Γ,x:τ_1,r:τ_2→_ρ τ_r ⊢ e_h : τ_r ╱ ρ \\
Δ;Γ, x:τ ⊢ e_r : τ_r ╱ ρ}
{Δ;Γ ⊢ \Handle e\;\{x,r.\,e_h;x.\,e_r\} : τ_r ╱ ρ}
\inferrule
{Δ ⊢ τ_1 <: τ_2 \\ Δ ⊢ ρ_1 <: ρ_2 \\ Δ;Γ ⊢ e : τ_1 ╱ ρ_1}
{Δ;Γ ⊢ e : τ_2 ╱ ρ_2}
\end{mathpar}
\caption{Type system.}
\label{types}
\end{figure}
\begin{figure}
\begin{mathpar}
\inferrule
{α ∷ κ ∈ Δ}
{Δ ⊢ α ∷ κ}
\inferrule
{Δ ⊢ τ_1 ∷ \kT \\ Δ ⊢ ρ ∷ \kR \\ Δ ⊢ τ_2 ∷ \kT}
{Δ ⊢ τ_1 →_ρ τ_2 ∷ \kT}
\inferrule
{Δ, α :: κ ⊢ τ ∷ \kT}
{Δ ⊢ ∀α::κ.\, τ ∷ \kT}
\inferrule
{Δ,Δ' ⊢ τ_1 ∷ \kT \\ Δ,Δ' ⊢ τ_2 ∷ \kT}
{Δ ⊢ Δ'.\, τ_1 \Rightarrow τ_2 ∷ \kE}
\inferrule
{ }
{Δ ⊢ ι ∷ \kR}
\inferrule
{Δ ⊢ ε ∷ \kE \\ Δ ⊢ ρ ∷ \kR}
{Δ ⊢ ε · ρ ∷ \kR}
%Δ ⊢ δ ∷ Δ' ⇔ \dom(δ) = \dom(Δ') ∧ ∀α∈\dom(δ).\, Δ ⊢ δ(α) ∷ Δ'(α)
\end{mathpar}
%\caption{Well-formedness of types, rows, and type substitution.}
\caption{Well-formedness of types and rows.}
\label{kinding}
\end{figure}
\begin{figure}
\begin{mathpar}
\inferrule
{ }
{Δ ⊢ σ <: σ}
%\textsc{(S-Refl)}
\inferrule
{Δ ⊢ ρ ∷ \kR}
{Δ ⊢ ι <: ρ}
%\textsc{(S-Empty)}
\inferrule
{Δ ⊢ ρ_1 <: ρ_2}
{Δ ⊢ ε·ρ_1 <: ε·ρ_2}
%\textsc{(S-Extend)}
\inferrule
{Δ ⊢ τ_2^1 <: τ_1^1 \\ Δ ⊢ ρ_1 <: ρ_2 \\ Δ ⊢ τ_1^2 <: τ_2^2}
{Δ ⊢ τ_1^1 →_{ρ_1} τ_1^2 <: τ_2^1 →_{ρ_2} τ_2^2}
%\textsc{(S-Arrow)}
\inferrule
{Δ, α ∷ κ ⊢ τ_1 <: τ_2}
{Δ ⊢ ∀α∷κ.\, τ_1 <: ∀α∷κ. \,τ_2}
%\textsc{(S-ForAll)}
\end{mathpar}
\caption{Subtyping.}
\label{subtyping}
\end{figure}
\chapter{The logical relation}
The logical relation is inspired by \cite{hwc}.
Some changes are due to language differences:
we have only one universal operation which simplifies the treatment of effects,
polymorphism does not manifest at the expression level (we do not have type lambdas),
and our operations can be polymorphic.
Instead of a binary step-indexed relation,
our goal is to build a unary relation without step-indexing.
\section{Definition}
We begin with defining the interpretations of kinds.
We call them the spaces of \textit{semantic types} or,
in the specific cases of $\kE$ and $\kR$, \textit{semantic effects}:
\begin{align*}
⟦\kT⟧ &= \mathcal{P}(\textrm{CVal}) = \mathsf{Type}\\
⟦\kE⟧ = ⟦\kR⟧ &= \mathcal{P}(\textrm{CExp}×ℕ×\mathsf{Type}) = \mathsf{Eff}
\end{align*}
We write $\textrm{CVal}$ and $\textrm{CExp}$ for the sets of closed values and closed expressions respectively, so
elements of $\mathsf{Type}$ are simply sets of closed values.
Semantic effects are sets of triples that aim to describe a situation
in which an expression tries to perform an effect but there is no handler for it – it gets {\em control-stuck}.
The components of such a triple are:
the operation with its argument,
the freeness of the enclosing context,
and the semantic type of values we could resume with – the return type of the operation.
Generalizing the preceding,
type contexts are interpreted as mappings from type variables to semantic types:%
$$⟦Δ⟧ = \{ η │ \dom(η) = \dom(Δ) ∧ ∀α∷κ∈Δ.\,η(α) ∈ ⟦κ⟧ \}$$
The logical relations, defined by (mutual) structural induction, appear in \cref{logrel}. % TODO: appear in? ;/
They are parameterized by a mapping $η$ from type variables to
semantic types.
The interpretation of a type variable
is directly retrieved from the environment $η$.
The treatment of function types is standard,
except effect annotations also have to be taken into account.
The definition may not look structurally recursive at first,
but we can consider $τ_2/ρ$ to be a subterm of $τ_1 →_ρ τ_2$,
just rearranged to a different notation.
A universal type is interpreted, as usual,
as the intersection of the interpretations
for all possible choices of the semantic type for the type variable.
\begin{figure}
\begin{align*}
⟦α⟧_η &= η(α) \\
%\\
⟦τ_1 →_ρ τ_2⟧_η
&= \{ λx.\,e \mid ∀v∈⟦τ_1⟧_η.\, e\subst{v}{x} ∈ \E⟦τ_2/ρ⟧_η \} \\
⟦∀α::κ.\,τ⟧_η
&= \{ v \mid ∀μ∈⟦κ⟧.\, v ∈ ⟦τ⟧_{η[α↦μ]} \} \\
⟦Δ.\,τ_1 \Rightarrow τ_2⟧_η &= \{(\Do v,0,⟦τ_2⟧_{ηη'}) \mid η'∈⟦Δ⟧ ∧ v∈⟦τ_1⟧_{ηη'} \} \\
⟦ε · ρ⟧_η &= ⟦ε⟧_η ∪ \{(e, n+1, μ) \mid (e, n, μ)∈⟦ρ⟧_η \} \\
⟦ι⟧_η &= ∅
\end{align*}
\begin{align*}
\E⟦τ/ρ⟧_η &=
\{ e \mid ∃v∈⟦τ⟧_η.\, e →^* v ∨ ∃e'∈\S⟦τ/ρ⟧_η.\, e →^* e' \} \\
\S⟦τ/ρ⟧_η &= \{ K[e] \mid ∃n,μ.\, (e,n,μ)∈⟦ρ⟧_η ∧ n\Free(K) ∧ ∀u∈μ.\, K[u]∈ \E⟦τ/ρ⟧_η \}
\end{align*}
\caption{
Interpretation of types.
Relations on expressions and stuck terms.%
\protect\footnotemark
}
\label{logrel}
\end{figure}
Dually, we can see that a polymorphic effect is interpreted as the {\em union} of the interpretations
of the effect for all possible choices of the semantic types for the type variables.
The interpretation of a single operation is implicitly treated as if it were
at the beginning of a row, so the freeness is $0$.
When we prepend to a row, we increase this freeness for every element of its semantic effect
and combine with the interpretation of the prepended effect.
Naturally, the semantic effect for the empty row is empty.
\footnotetext{
Technically, the terms in $\S⟦τ/ρ⟧_η$ do not have to be control-stuck, since
the $(e,n,μ)$ could come from the interpretation of an effect or row variable,
and the space of semantic effects allows for any $e$.
Such a term {\em is} stuck if the triple comes from the interpretation of an operation.
We leave it that way to make adding multiple operations easier later.
An analogous definition appears in \cite{hwc}.
}
The definition of the set $\E⟦τ/ρ⟧_η$ is recursive and we interpret it inductively,
i.e., as the least solution to the equations (we will elaborate on that soon).
Note the similarity to the interpretation of function types in the definition of
$\S⟦τ/ρ⟧_η$.
The intuition is that an element of $\E⟦τ/ρ⟧_η$
evaluates to a value of type $τ$ while possibly performing an effect in $ρ$ and
getting control-stuck
finitely many times along the way.
When the term is control-stuck, we do not have a handler,
but we do know the return type of the operation,
so we resume with all possible values in parallel.
This is a very local view of effectful computations,
perhaps giving the false impression that all handlers immediately resume with a value and
do nothing more (this is called the {\em reader} effect and is equivalent to dynamically-scoped variables).
In reality, much can happen between
performing the operation and it finally returning.
Nevertheless, this local view suffices for our purposes and
accurately reflects how much of the computation remains in the scope of a handler
throughout evaluation.
\begin{figure}
\begin{tikzpicture}
\node (e) at (0,0.5) {$\textit{times}\;(\Do ())\,\textit{tick}$};
\node (e0) at (0,-1) {$\textit{times}\;0\,\textit{tick}$};
\node (e1) at (4,-1) {$\textit{times}\;1\,\textit{tick}$};
\node (e2) at (8,-1) {$\textit{times}\;2\,\textit{tick}$};
\node (einf) at (12,-1) {$\hdots$};
\draw[dashed,->] (e.south) -- (e0.north);
\draw[dashed,->] (e.south) -- (e1.north);
\draw[dashed,->] (e.south) -- (e2.north);
\draw[dashed,->] (e.south) -- ([yshift=4] einf.north);
\node (e0a) at (0,-2.5) {$()$};
\node (e1a) at (4,-2.5) {$\Lift{\Do ()}; \textit{times}\;0\,\textit{tick}$};
\node (e2a) at (8,-2.5) {$\Lift{\Do ()}; \textit{times}\;1\,\textit{tick}$};
%\node (einfa) at (12,-1.85) {$\ddots$};
\draw[->] (e0.south) -- (e0a.north) node[very near end,right=-3]{\textsubscript{*}};
\draw[->] (e1.south) -- (e1a.north) node[very near end,right=-3]{\textsubscript{*}};
\draw[->] (e2.south) -- (e2a.north) node[very near end,right=-3]{\textsubscript{*}};
\node (e1b) at (4,-4) {$\Lift{()}; \textit{times}\;0\,\textit{tick}$};
\node (e2b) at (8,-4) {$\Lift{()}; \textit{times}\;1\,\textit{tick}$};
\draw[dashed,->] (e1a.south) -- (e1b.north);
\draw[dashed,->] (e2a.south) -- (e2b.north);
\node (e1c) at (4,-5.5) {$()$};
\node (e2c) at (8,-5.5) {$\Lift{\Do ()}; \textit{times}\;0\,\textit{tick}$};
\draw[->] (e1b.south) -- (e1c.north) node[very near end,right=-3]{\textsubscript{*}};
\draw[->] (e2b.south) -- (e2c.north) node[very near end,right=-3]{\textsubscript{*}};
\node (e2d) at (8,-7) {$\Lift{()}; \textit{times}\;0\,\textit{tick}$};
\draw[dashed,->] (e2c.south) -- (e2d.north);
\node (e2e) at (8,-8.5) {$()$};
\draw[->] (e2d.south) -- (e2e.north) node[very near end,right=-3]{\textsubscript{*}};
\node (einfa) at (12,-5.5) {$\vdots$};
\node (einfb) at (12,-10) {$\ddots$};
\end{tikzpicture}
\caption{
An illustration of the reduction trace for $\textit{times}\,({\Do ()})\,\textit{tick} ∈
\E⟦\textsf{unit}/ \textsf{askNat} · \textsf{tick} · ι⟧$,
where $\textsf{askNat} = \textsf{unit}\Rightarrow\textsf{nat}$
and $\textsf{tick} = \textsf{unit}\Rightarrow\textsf{unit}$.
For clarity, we assume the unit type, the inductive naturals, and a function \textit{times} for invoking a thunk $n$ times,
but these can be realized using Church encodings.
We set $\textit{tick} = λx.\,\Lift{\Do ()}$.
Here, the \textsf{tick} effect does not seem to do much, but
a handler for it could, for instance, increment an external counter.
The semicolon is standard syntax sugar:
$e_1;e_2$ is expanded to $(λx.\,e_2)\;e_1$ with $x$ not free in $e_2$.
The solid arrow is the usual reduction, while the dashed one represents substituting
a value for the operation in a stuck term
in $\S⟦\textsf{unit}/ \textsf{askNat} · \textsf{tick} · ι⟧$.
This tree is well-founded despite having infinite depth.
}
\label{trace}
\end{figure}
More graphically (see \cref{trace}),
we can associate with an element in $\E⟦τ/ρ⟧_η$ what we will call a {\em reduction trace}
– a tree where nodes are expressions in $\E⟦τ/ρ⟧_η$,
leaves are values in $⟦τ⟧_η$, and edges are either (maximal) multi-step reductions
or branches from $\S⟦τ/ρ⟧_η$ for each element of $μ$,
which substitute the value for the operation.%
\footnote{
This is also how we could view the structure of values of the inductive type $\E⟦τ/ρ⟧_η$,
had we formalized the definitions in a (dependent) type theory.
}
Different choices of values to resume with can
lead to getting stuck a different number of times,
so the depth of the tree is not uniform, and it can even be infinite.
Still, these trees are well-founded thanks to the choice of the inductive interpretation.
To describe the construction in more detail, we temporarily overload notation and define
operators $\S⟦τ/ρ⟧_η$ and $\E⟦τ/ρ⟧_η$ on sets of expressions (denoted by $X$).
\begin{align*}
\E⟦τ/ρ⟧_η(X) &=
\{ e \mid ∃v∈⟦τ⟧_η.\, e →^* v ∨ ∃e'∈\S⟦τ/ρ⟧_η(X).\, e →^* e' \} \\
\S⟦τ/ρ⟧_η(X) &= \{ K[e] \mid ∃n,μ.\, (e,n,μ)∈⟦ρ⟧_η ∧ n\Free(K) ∧ ∀u∈μ.\, K[u]∈X \}
\end{align*}%
They are clearly monotone,
so by the Knaster-Tarski theorem % citation
the fixed-point equation $\E⟦τ/ρ⟧_η(X) = X$ has a least solution.\footnote{
The operators are not Scott-continuous because of potentially infinite semantic
types $μ$.
This is why the construction from the Kleene fixed-point theorem (union of
iterations of the operator on the empty set)
would fail to be a solution.
With this definition the reduction traces have bounded depth.
The application compatibility lemma would fail:
it grafts different reduction traces to the leaves of a
potentially infinitely branching reduction trace,
so the bounded depth is not preserved.
}
Moreover, it can be characterized as the intersection of all
$\E⟦τ/ρ⟧_η$-closed sets:
\begin{align*}
\E⟦τ/ρ⟧_η &= \bigcap \{ X │ \E⟦τ/ρ⟧_η(X) ⊆ X \} \\
\S⟦τ/ρ⟧_η &= \S⟦τ/ρ⟧_η(\E⟦τ/ρ⟧_η)
\end{align*}
We immediately obtain the following principle:
\begin{lemma}[Tarski induction principle]\label{tarski-induction}
If $\E⟦τ/ρ⟧_η(X) ⊆ X$, then $\E⟦τ/ρ⟧_η ⊆ X$.
\end{lemma}
By expanding out the definition of the function $\E⟦τ/ρ⟧_η$
and treating $X$ as a predicate $P$,
we get the more familiar principle of structural induction on $\E⟦τ/ρ⟧_η$.
\begin{lemma}[Induction principle]\label{induction}
Assume $P$ is a predicate on closed expressions and
\begin{itemize}
\item if $e$ evaluates to a value in $⟦τ⟧_η$, then $P(e)$ holds; and
\item if $e$ reduces to some $K[e']$ such that there exist $(e',n,μ)∈⟦ρ⟧_η$ such that $n\Free(K)$
and $P(K[u])$ holds for all $u∈μ$, then $P(e)$ holds.
\end{itemize}
Then $P(e)$ holds for all $e ∈ \E⟦τ/ρ⟧_η$.
\end{lemma}
We also note some straightforward but useful properties of the relations.
\begin{lemma}[Value inclusion]\label{value-inclusion}
For any $τ$ and $ρ$ we have $⟦τ⟧_η ⊆ \E⟦τ/ρ⟧_η$.
\end{lemma}
\begin{lemma}[Control-stuck inclusion]\label{stuck-inclusion}
For any $τ$ and $ρ$ we have $\S⟦τ/ρ⟧_η ⊆ \E⟦τ/ρ⟧_η$.
\end{lemma}
\begin{lemma}[Closedness under antireduction]\label{antireduction}
If $e →^* e' ∈ \E⟦τ/ρ⟧_η$, then $e∈\E⟦τ/ρ⟧_η$.
\end{lemma}
\begin{lemma}[Weakening]\label{weakening}
If $η'$ extends $η$,
then $\E⟦τ/ρ⟧_η = \E⟦τ/ρ⟧_{η'}$.
\end{lemma}
\begin{lemma}[Monotonicity in types]\label{mono}
If $⟦τ_1⟧_η ⊆ ⟦τ_2⟧_η$ and $⟦ρ_1⟧_η ⊆ ⟦ρ_2⟧_η$,
then $\S⟦τ_1/ρ_1⟧_η ⊆ \S⟦τ_2/ρ_2⟧_η$
and $\E⟦τ_1/ρ_1⟧_η ⊆ \E⟦τ_2/ρ_2⟧_η$.
\end{lemma}
%\begin{proof}
%We will show by induction on $e ∈ \E⟦τ_1/ρ_1⟧_η$ that $e ∈ \E⟦τ_2/ρ_2⟧_η$.
%
%If $e →^* v ∈ ⟦τ_1⟧_η$, then $v ∈ ⟦τ_2⟧_η$ and by \cref{value-inclusion} we have $v ∈ \E⟦τ_2/ρ_2⟧_η$.
%By \cref{antireduction} we conclude $e ∈ \E⟦τ_2/ρ_2⟧_η$.
%
%If $e →^* K[e']$ such that there exist $(e', n, μ) ∈ ⟦ρ_1⟧_η$ such that
%$n\Free(K)$ and $K[u] ∈ \E⟦τ_2/ρ_2⟧_η$ for all $u$ in $μ$,
%then, since $⟦ρ_1⟧_η ⊆ ⟦ρ_2⟧_η$, by definition we have $K[e'] ∈ \S⟦τ_2/ρ_2⟧_η$.
%By \cref{stuck-inclusion} and \cref{antireduction} we have $e∈\E⟦τ_2/ρ_2⟧_η$.
%
%Finally, since the operator $\S⟦τ/ρ⟧_η$ ignores $τ$ and is monotone in $⟦ρ⟧_η$,
%\begin{align*}
%\S⟦τ_1/ρ_1⟧_η = \S⟦τ_1/ρ_1⟧_η(\E⟦τ_1/ρ_1⟧_η) &⊆ \S⟦τ_1/ρ_1⟧_η(\E⟦τ_2/ρ_2⟧_η)\\
%&⊆ \S⟦τ_2/ρ_1⟧_η(\E⟦τ_2/ρ_2⟧_η) = \S⟦τ_2/ρ_2⟧_η.
%\end{align*}
%\end{proof}
\section{Compatibility} % TODO only "Compatibility" or other word? Adequacy? Soundness?
We want to establish that $⊢ e : τ ╱ ι$ implies $e ∈ \E⟦τ/ι⟧$.
For this purpose we will prove a semantic counterpart of each typing rule.
First, we need to define a counterpart to the typing judgment.
Unlike typing judgments, our relations are on closed terms only,
so we get around that by using substitution.
We define semantic entailment as follows:
$$Δ;Γ ⊨ e : τ ╱ ρ ⇔ ∀η∈⟦Δ⟧.\, ∀γ∈⟦Γ⟧_η.\,γ(e) ∈ \E⟦τ/ρ⟧_η,$$
where $⟦Γ⟧_η = \{ γ │ \dom(γ) = \dom(Γ) ∧ ∀x:τ∈Γ.\,γ(x) ∈ ⟦τ⟧_η\}$ contains expression-level
variable substitutions.
\begin{lemma}[Variable compatibility]
$$
\inferrule
{x : τ ∈ Γ}
{Δ;Γ ⊨ x : τ ╱ ι}
$$
\end{lemma}
\begin{proof}
Assume $x : τ ∈ Γ$.
We want to prove $Δ;Γ ⊨ x : τ ╱ ι$.
Take any $η∈⟦Δ⟧$ and $γ∈⟦Γ⟧_η$.
We want to show $γ(x) ∈ \E⟦τ/ι⟧_η$.
From the definition of $⟦Γ⟧_η$
we know that $γ(x) ∈ ⟦τ⟧_η$,
so by \cref{value-inclusion} we have $γ(x) ∈ \E⟦τ/ι⟧_η$.
\end{proof}
\begin{lemma}[Abstraction compatibility]
$$
\inferrule
{Δ ⊢ τ_1 ∷ \kT \\ Δ;Γ, x:τ_1 ⊨ e : τ_2 ╱ ρ}
{Δ;Γ ⊨ λx.\,e : τ_1 →_ρ τ_2 ╱ ι}
$$
\end{lemma}
\begin{proof}
Assume $Δ ⊢ {τ_1 ∷ \kT}$ and $Δ;Γ,{x:τ_1} ⊨ e : τ_2 ╱ ρ$.
% Note: kind assumption necessary so that τ_1 →_ρ τ_2 well formed
We want to prove $Δ;Γ ⊨ λx.\,e : {τ_1 →_ρ τ_2} ╱ ι$.
Take any $η∈⟦Δ⟧$ and $γ∈⟦Γ⟧_η$.
By \cref{value-inclusion} it suffices to show
$γ(λx.\,e) = λx.\,γ(e) ∈ ⟦τ_1 →_ρ τ_2⟧_η$.
So take any $v ∈ ⟦τ_1⟧_η.$
We need to show $γ(e)\subst{v}{x} ∈ \E⟦τ_2/ρ⟧_η$.
Let $γ' = γ'[x↦v]$.
Then $γ' ∈ ⟦Γ,x:τ_1⟧_η$, so $γ(e)\subst{v}{x} = γ'(e) ∈ \E⟦τ_2/ρ⟧_η.$
\end{proof}
For clarity of presentation,
in the following we will assume $Γ$ empty.
The lemmas in full generality can then be proven simply by
substituting an interpretation of $Γ$.
\begin{lemma}[Lift compatibility]
$$
\inferrule
{Δ ⊢ ε ∷ \kE \\ Δ; ⊨ e : τ ╱ ρ}
{Δ; ⊨ \Lift{e} : τ ╱ ε·ρ}
$$
\end{lemma}
\begin{proof}
Assume $Δ ⊢ τ ∷ \kT$, $Δ ⊢ ε ∷ \kE$, and $Δ ⊢ ρ ∷ \kR$.
Take any $η∈⟦Δ⟧$.
We will show by induction on $e∈\E⟦τ/ρ⟧_η$ that $\Lift{e} ∈ \E⟦τ/ε·ρ⟧_η$.
If $e →^* K[e']$ and there exists
$(e', n, μ) ∈ ⟦ρ⟧_η$ such that $n\Free(K)$ and
for all $u∈μ$ the induction hypothesis holds for $K[u]$,
then we have $(e', n+1, μ) ∈ ⟦ε·ρ⟧_η$, $n+1\Free(\Lift{K})$,
and $∀u∈μ.\, \Lift{K[u]} ∈ \E⟦τ/ε·ρ⟧_η$.
So $\Lift{K[e']} ∈ \S⟦τ/ε·ρ⟧_η$ and $\Lift{e} ∈ \E⟦τ/ε·ρ⟧_η$ by antireduction.
If $e →^* v ∈ ⟦τ⟧_η$, then
$\Lift{e} →^* \Lift{v} → v$,
so $\Lift{e} ∈ \E⟦τ/ε·ρ⟧_η$.
\end{proof}
\begin{lemma}[Application compatibility]
$$
\inferrule
{Δ; ⊨ e_1 : τ_1 →_ρ τ_2 ╱ ρ \\ Δ; ⊨ e_2 : τ_1 ╱ ρ}
{Δ; ⊨ e_1\,e_2 : τ_2 ╱ ρ}
$$
\end{lemma}
\begin{proof}
%Assume $Δ;Γ ⊨ e_1 : τ_1 →_ρ τ_2 ╱ ρ$ and $Δ;Γ ⊨ e_2 : τ_1 ╱ ρ$.
Fix any well-formed $Δ$ and $τ_1 →_ρ τ_2$.
Take any $η∈⟦Δ⟧$ and $e_2 ∈ \E⟦τ_1/ρ⟧_η$.
We will show by induction on $e_1∈\E⟦τ_1→_ρ τ_2/ρ⟧_η$ that
$e_1\;e_2 ∈ \E⟦τ_2/ρ⟧_η$.
If $e_1 →^* K_1[e_1']$ and
there exists $(e_1',n,μ)∈⟦ρ⟧_η$ such that $n\Free(K_1)$ and for all $u∈μ$
the inductive hypothesis holds for $K_1[u]$,
then $K_1[e_1']\;e_2 ∈ \S⟦τ_2/ρ⟧_η$, since $n\Free(K_1\;e_2)$.
By antireduction $e_1\;e_2 ∈ \E⟦τ_2/ρ⟧_η$.
Now assume $e_1 →^* (λx.\,e) ∈ ⟦τ_1 →_ρ τ_2/ρ⟧_η$.
We will show by induction on $e_2 ∈ \E⟦τ_1/ρ⟧_η$ that $(λx.\,e)\;e_2 ∈ \E⟦τ_2/ρ⟧_η$
and the claim will follow by antireduction.
If $e_2 →^* K_2[e_2']$ and
there exists $(e_2',n,μ)∈⟦ρ⟧_η$ such that $n\Free(K_2)$ and for all $u∈μ$
the inductive hypothesis holds for $K_2[u]$,
then $(λx.\,e)\;K_2[e_2'] ∈ \S⟦τ_2/ρ⟧_η$, since $n\Free((λx.\,e)\;K_2)$.
By antireduction $(λx.\,e)\;e_2 ∈ \E⟦τ_2/ρ⟧_η$.
If $e_2 →^* v ∈ ⟦τ_1⟧_η$, then
$(λx.\,e)\;e_2 →^* (λx.\,e)\;v → e\subst{v}{x} ∈ \E⟦τ_2/ρ⟧_η$.
\end{proof}
\begin{lemma}[Handle compatibility]
$$
\inferrule
{Δ; ⊨ e : τ ╱ (Δ'.\,τ_1 \Rightarrow τ_2) · ρ \\
Δ,Δ';x:τ_1,r:τ_2→_ρ τ_r ⊨ e_h : τ_r ╱ ρ \\
Δ; x:τ ⊨ e_r : τ_r ╱ ρ}
{Δ; ⊨ \Handle e\;\{x,r.\,e_h;x.\,e_r\} : τ_r ╱ ρ}
$$
\end{lemma}
\begin{proof}
Assume $Δ, Δ' ; x:τ_1, r:τ_2 →_ρ τ_r ⊨ e_h : τ_r ╱ ρ$ and
$Δ; x:τ ⊨ e_r : τ_r ╱ ρ$.
%We want to show
%$Δ;Γ ⊨ \Handle e\,\{x,r.\,e_h;\;x.\,e_r\} : τ_r ╱ ρ$.
Let $h$ stand for $\{x,r.\,e_h;\,x.\,e_r\}$.
Take any $η∈⟦Δ⟧$.
We will show by induction on $e∈\E⟦τ/(Δ'.\,τ_1 \Rightarrow τ_2) · ρ⟧_η$
that $\Handle e\,h ∈ \E⟦τ_r/ρ⟧_η$.
Note that only $τ_1$ and $τ_2$ require $Δ'$ to be in context.
If $e →^* v ∈ ⟦τ⟧_η$,
then $\Handle e\,h →^* e_r\subst{v}{x} ∈ \E⟦τ_r/ρ⟧_η$,
so the claim follows by antireduction.
Now assume $e →^* K[e']$ and
we have $(e',n,μ) ∈ ⟦(Δ'.\,τ_1 \Rightarrow τ_2) · ρ⟧_η$
such that $n\Free(K)$ and
for all $u∈μ$ the induction hypothesis holds for $K[u]$.
If $n=0$, then $(e',n,μ) ∈ ⟦τ_1 \Rightarrow τ_2⟧_{ηη'}$ for some $η'∈⟦Δ'⟧$.
More specifically, $e' = \Do v$, $v∈⟦τ_1⟧_{ηη'}$ and $μ = ⟦τ_2⟧_{ηη'}$.
We have $\Handle e\,h →^* \Handle K[\Do v]\,h → e_h\subst{v}{x}\subst{v_c}{r}$,
where $v_c=λz.\,\Handle K[z]\,h$.
To show $v_c ∈ ⟦τ_2 →_ρ τ_r⟧_{ηη'}$, take any $u ∈ ⟦τ_2⟧_{ηη'}$ and show
$\Handle K[u]\,h ∈ \E⟦τ_r/ρ⟧_{ηη'} = \E⟦τ_r/ρ⟧_η$.
Which holds by induction hypothesis.
Therefore, $e_h\subst{v}{x}\subst{v_c}{r}$ is in $\E⟦τ_r/ρ⟧_η$ and so is $\Handle e\;h$.
If $n>0$, then
$\Handle K[e']\,h ∈ \S⟦τ_r/ρ⟧_η$,
since $n-1\Free(\Handle K\,h)$, $(e',n-1,μ) ∈ ⟦ρ⟧_η$,
and $∀u∈μ.\,\Handle K[u]\,h ∈ \E⟦τ_r/ρ⟧_η$.
Again, the claim follows by antireduction.
\end{proof}
\begin{lemma} \label{subst-comp}
Syntactic type substitutions are compatible with semantic type environments: If
\begin{itemize}
\item $Δ$ and $Δ'$ have disjoint domains; and
\item $Δ,Δ' ⊢ τ ∷ κ$; and
\item $η∈⟦Δ⟧$; and
\item $Δ ⊢ δ ∷ Δ'$; and
\item $η'$ extends $η$ by $α↦⟦δ(α)⟧_η$ for each $α$ in the domain of $Δ'$;
\end{itemize}
then $⟦τ⟧_{η'} = ⟦δ(τ)⟧_η$.
\end{lemma}
\begin{proof}
By induction on the kinding rules.
If $τ=α ∈ Δ$, then both sides are equal to $η(α)$.
If $τ=α ∈ Δ'$, then equality follows from the definition of $η'$.
If $τ=ι$, then both sides are empty.
If $τ=τ_1 →_ρ τ_2$, then
$⟦τ⟧_{η'} = \{λx.\,e │ ∀v ∈ ⟦τ_1⟧_{η'}.\,e\subst{v}{x} ∈ \E⟦τ_2/ρ⟧_{η'}\}$
and
$⟦δ(τ)⟧_η = \{λx.\,e │ ∀v ∈ ⟦δ(τ_1)⟧_η.\,e\subst{v}{x} ∈ \E⟦δ(τ_2)/δ(ρ)⟧_η\}$,
which are equal by the inductive hypothesis and definition of $\E$.
If $τ=∀α∷κ.\,τ'$, then