mize(e
:
t)
=
e
:
{x
:
t|x
⌘
e}
+
where
e
=
let
x
=
⇡
x
in
e
i
i env
Lemma 0.2 (Preservation of Substitution)
` ⇧ x : t. t : Type i
Universes
U
::=
Type
|
+ Set | Prop
i
0
+ (Simulation
+ 0+
Lemma
of
Reduction
Relation)
env
=
hx
.
.
.i
=
fv(e
)
i
?
?
0
?
⌘ t [t 0+
/x]
0 (t[t /x]) +
⇡ e Ifthen
e . + `+e+ B? e0+and e ⌘ e0+
` e B pack h⌃env , env,
`0 e B(xpack
h⌃
,x
,0 env,
: t1 ). e(x
` e, xB: t1e0).2 e1 i
`
env : ⌃
env
env
1 ienv : ⌃env
; `e
e Bx ⇡
e0 then
t and
t : P rop
then eofe,Reduction
⇡
. Relation)
Terms/Types
t ofe::=
x | U | x : t. e, |xe: et |[env/x
⇧ x : ,t.xe: |t]let
x
=
e
:
t
in
e
`
e
:
t
`
⇧
x
:
t.
t
:
U
`
e
:
⇧
x
:
t
.
t
`
e
Lemma
0.2 (Preservation
Substitution)
Lemma
(Simulation
`
e
,
x
[env/x
:
t
[env/x
]
⌘
unpack
]
`
e
[env/x
h↵,
x
,
f
]
i
⌘
=
unpack
e
in
f
x
h↵,
x
x
,
f
i
=
e
+
+
1
env
1
1
env
env
1
te
env
2
te
te
2
0
+
+
0+
0
+
+
?
0+
(t[t
/x])
⌘
t
[t
/x]
Lemma
(Preservation
of
Equivalence)
( xIf : t.
pack `hte env
; e)
` e Bx=
e0 then
B ,e henv,
and e ⌘ e (xenv : tenv , x : t ). eii
0
0
0
0
0 e : {x
+ :+
0+ e}
` x : t. e : ⇧` xe:⌘
t. et 0
` e e : t[e /x]
Environment
::= · | , x : t | , x = e : t
mize(e
t|x
⌘
IfLocal
`:Lemma
et)
⌘
e=
, then
`
e
⌘
e
`
e
⌘
e
(Simulation of Reduction Relation)
Lemma (Preservation
Equivalence)
0
+ of +
?
0+
If ; `0 e Bx e then
`
e
B
e
and
e
⌘
e
+
+
+ of Subtyping)
0+
Lemma
If ` e ⌘(Preservation
e , then
`
e
⌘
e
where
e
=
let
x
=
⇡
x
in
e
i
i
env
0
0
+
+ 0+
+
+
If `tLemma
t:1`Pttrop
`e
t1 of
t,2Equivalence)
`e:t
`t:U
`t t
(Preservation
Type
x
:
t
`
t
:
Type
2:, then
t and
then
⇡
e
.
i
i
+
Lemma
0+ hxi . . .i = fv(e
=
)
If ` e (Preservation
⌘ e0 , then + `env
e++of⌘Subtyping)
e+
·
·
·
Figure
1:
CIC
Syntax
+
+
0, x, env,
+
0
(
x
:
t.
e)
=
pack
ht
(
x
,
:
env,
t.
e)
(x
=
pack
:
t
ht
If ` t1 (Type
t2 , then
`
t
t
Lemma
Preservation)
: env
t : t). ei(xenv : tenv , x : t). e
env
env` eenv
` ⇧ x 1: t. t2 : Type i
Lemma (Preservation of Subtyping)
+ +
+
+
1.
If
`
then
`
Lemma
(Type
Preservation)
If
`
t
t
,
then
`
t
t
1
2
.2 (Preservation
of1 Substitution)
2
Theorem
0
0
0
0
0
+
+
0+
+ x :+
+: U
t
`
⇧
t.
t
`
e
:
t
0 + ` e : ⇧x : t .t
If
`
then
`
(Type
Preservation)
If:/x]
`
e
:
t
then
`
e
:
t
⌘: ttIf`[t2.e1.`Lemma
+
+
+
0+
0+
Type i ` e :,tx : t(where
` t : Type
i
e`: tt :then
denotes
compilation)
0
0
t
[⇡
x
/x
]
t
[⇡
x
/x
]
i
env
i
i
env
i
x
:
t
.
.
.
`
e
:
t
`
⇧
(x
:
t
.
.
.
).
t
:
U
0 +
0
0·
+
i
i
i
i
·
·
+
+
1.
If
`
then
`
` et : t0
`2. Ifx :?`t.e e: t:then
⇧
x
:
t.
`
e
e
:
t[e
/x]
0+
e
:
t
[⇡
x
/x
]
+x : ?with
+ :linking
`
⇧
t.
t
Type
i
env
i
i
0
Correct
compilation
•
If
e
B
v
then
e
B
v
.
+
(Simulation
Reduction
Relation)
`
(x
:
t
.
.
.
).
e
:
⇧
(x
:
t
.
.
.
).
t
2. If ?` e : of
t then
` e + : t+
0+
0
?
i
i
i
i
e : t [⇡ i xenv /xi ]
` e B pack
h⌃+
(xenv : ⌃env , x :0+
t1 ). e1 i
` e B e2
env , env,
0
+
?
0
compilation
Bx• eSecure
then
`
e
B
e
and
e
⌘
e
0
+
0+
,x `
: te1 [env/x
]
`
e
[env/x
]
⌘
unpack
h↵,
x
,
f
i
=
e
in
f
x
x
`
e
:
t
`
t
:
U
`
t
t
?
0
?
env
1
env
te
2
te
0
0
0
0
0
0
0
0 pack h⌃
+env , env,
0+ (xenv : ⌃env , x : t1 ). e1 i
⇧
x
:
t.
t
;
9
↵
:
Type
,
x
:
↵.
(x
)
⇧
x
:
t
.
t
)
B
`
e
B
e
A
specification
for
closure
conversion:
2
i 1:, tx
te
te
e⇧ x⇡:et. t. : U
`
e
:
t
,
x
=
e
`
:
e
t
:
t
.
.
,
.
x
`
=
t
e
:
t
t
,
x
:
t
.
.
.
`
t
t
: t •`Certifying
eIf :et⇡ e then
`
`
e
:
⇧
x
:
t
.
t
`
e
:
t
1
1
1
r
r
1
2
1
r
r
2
0
2
2
?
0
?
compilation
/
Proof-carrying
code
0
00
+
0+
`
e
⌘
e
0 x : th↵,
, x : t`1 [env/x
`env
e1 ,[env/x
] ⌘: ⌃
unpack
x1te
, f i = `e2einBf x
e B pack
env, env
(xenv
,
).
e
i
e
env ]h⌃
te x
env
1
2
functions
must
be
closed
to
be
well-typed
⇧
x
:
t.
t
;
9
↵
:
t
.
(↵
⇥
⇧
(x
:
↵,
x
:
t
).
t
)
`
e
:
t
0
env
0
0
0
0
⇧
(x
:
t
,
x
:
t
.
.
`
.
).
⇧
t
(x
:
e
t
)
,
x
⇧
:
(x
t
.
:
.
t
.
).
.
t
.
.
).
t
e
)
⇧
(x
:
t
.
.
.
).
(Preservation
of
Equivalence)
,
x
:
t
[env/x
]
`
e
[env/x
]
⌘
unpack
h↵, xte , f i = e2`ine
f xete x: `
.
1
env
1
env
`
x
:
t.
e
:
⇧
x
:
t.
t
t[e
/x]
1
1
r
r
2
1
1
r
r
r
r
2
r
r
`
e
⌘
e
2
Type-directed
optimization
0
• optimize(e : t) = e : {x : t|x ⌘ e}0
..
0
+
+
0+
`e⌘e
+
e , then
`
e
⌘
+ e
` (xenv++: tenv , x : t ). e : ⇧
( x : t. e) = pack htenv , env, (xenv : tenv , x : t). ei
00
0
`
(x
:
t
,
x
:
t
).
e
:
⇧
(x
:
t
·
`
x
:
1,
x
:
A
fails
.
env
env
env
env
x
:
t.
e
;
pack
ht
,
henv,
(x
:
t
,
x
:
t
).
eii
+
+
0+
env
env
env
env
`
e
:
t
`
t
:
U
`
t
t
x
:
t
.
.
.
`
e
:
t
`
⇧
(x
:
t
.
.
.
).
t
:
U
..
( i xt: t.
e) rop
= pack
If e : ti and
:P
thenhtenv
e , env,
⇡ e (x. env :i tenv ,ix : t). ei
+
`
(
x
:
t.
e)
=
pack
ht
,
env,
(x
:
t
,
x
:
t).
ei
env
env
env
(Preservation of Subtyping)
`
(x
:
t
0
0
env
env
0+
·,
x
:
1,
x
:
A
`
x
:
A
env
+
+
: ti t. . [⇡
. ).`
⇧
e ::/x
ti ] (xi : ti . . . ). t
i xe
env
++ ` + (xi +
+
0+
+
0+
+
+
`
(x
:
t
,
x
:
t
).
e
:
⇧
(x
:
t
,
x
:
t
`
pack
ht
,
env,
(
env
env
env
env
t2(, then
`
t
t
env
0+
x
:
t
,
x
:
t
`
e
x
:
t
:
t
,
x
:
t
`
e
:
t
where
e
=
let
=
⇡
x
in
e
x : t. e) =1pack2htenv
,
henv,
(x
:
t
,
x
:
t
).
eii
env
env
env
env
i
i `env
env
env
t 0+[⇡ i xenv /xi ]
pack
ht
,
env,
(x
:
t
,
x
env
env
env
A : Set ` (x+env : 1, x : A). x+: ⇧+(xenv
:
1,
x
:
A).
A
t [⇡ i xenv /xi ]
+
+
0+ env : tenv , x : t
`
(x
env
=
hx
.
.
.i
=
fv(e
)
0
i: env
0 +
+
0+
`
(x
:
t
,
x
:
t
`
).
e
(x
⇧
(x
:
t
:
,
t
x
:
t
,
x
).
:
e
t
:
).
⇧
t
(x
:
t
,
x
:
t
env
env
env
env
env
env
env
`
t
:
Type
,
x
:
t
`
t
:
Type
0
(⇧ x : t.
t ) =` e9: ↵
t1 : Type
: t1 ,⇥
xr ⇧
: tr (x
. . . env
` t2 :i ↵,
t2+x 0: t ). t· · · )
(Type
Preservation)
0 ,ix1 =i .e(↵
+
+
00 , env,
+
00, x :+t ).0+
let
x
=
⇡
x
in
xi : ti . where
.`. e``:et:e1t :et`=⇧
`
⇧
(x
:
t
.
.
.
).
t
:
U
`
pack
ht
(x
:
t
e
0
0
0e
0
i
i
i
i
env
env
env
env
0
0
,
x
=
e
:
t
,
x
:
t
.
.
.
`
t
t
0↵,
(xpack
tenv
(x
: 9x:↵
tenv
:At :,.x
: t`
⇥).
⇧
eii
(x
:
9
↵
:
:
t
x
.
:
(↵
t
⇥
).
t
⇧
y:: tt .).eeii
: let
=
t(↵
in
⇧
y
:
t
.
x
t.ett:2t11:, Type
xerr )
: trr⇧
.i. .(x
`rt`
t.2. 2. ). t2htenv , henv, `
` ⇧ (x1 : t11, xr : t,rx1x.1.:=
. ).
env : ht
env, ,xhenv,
env
env
2:2tpack
r+
0
0
e
:
⇧
x
:
t
.
t
t
[e
Lemma
0.2
(Preservation
of
Substitution)
1
1
`
e
:
⇧
x
:
t
.
t
t
[e
/x]
⌘
t
0
0
0t
1
1
+ Type
translation
does
not
know
the
environment
but
term
translation
puts
environment
in
the
type
(due
to
dependency)
0
0
env
=
hx
.
.
.i
=
fv(e
)
`
⇧
(x
:
t
,
x
:
t
.
.
.
).
t
e
)
⇧
(x
:
t
.
.
.
).
`
⇧
(x
:
t
,
x
:
t
.
.
).
t
)
⇧
(x
:
t
.
.
).
t
i
1
1
r
r
2
r
r
1
1
r
r
2
r
r
`
(x
:
t
.
.
.
).
e
:
⇧
(x
:
t
.
.
.
).
t
2
2
then `
y
:
t
.
e
:
⇧
y
:
t
.
x
i
i
i
i
0
+
+
0+
· ` xenv : 1, 0x : A fails(t[t /x])
0
⌘
0
0 t [t /x] 0
00+
0+
`
e
:
e
`
e
:
e
)
t
,x : t ` e : t
` ⇧ x : t. t : U expects e`: et : ⇧
. t /x
`
e
t
[⇡xi:xt env
]
e
:
[⇡
x
/x
]
creates
i
i env
i
0
0
0
`
e
:
⇧
x
:
t
.
t
t
[e
/x]
⌘
t
`
e
+
+
+
1
1
·,
x
:
1,
x
:
A
`
x
:
A
0
0
0
of
Substitution)
0 +` `xe: t. e: :tenv
+
0+
+
0+
`0.2
e(⇧
: tx(Preservation
then
⇧
x
:
t.
t
`
e
e
:
t[e
/x]
xenv : it.env
, x⇥
: t+ ⇧
` e(x
: 0+
tenv
:
t.
t
)
=
9
↵
:
Type
(↵
:
↵,
x
:
t
).
t
)
Lemma
0.3
(Preservation
of
Reductions)
0
x
⌘
⇡
x
x
⌘
⇡
x
+
0+
i
i
env
i
i
env
x
:
t
,
x
:
t
`
e
:
t
+ 0+
env
env
`
e
:
e
)
t
x
:
t
,
x
:
t
`
e
:
t
+
+x :0+
env
env
⌘ t A[t: Set
/x] ` `(x(xenv
:
1,
x
:
A).
x
:
⇧
(x
:
1,
A).
A
tIf
). te B e0 then + ` e+ B? e and e ⌘ e0+
env : tenv , x : t +). e : ⇧ (xenv : env
env , x;+: t `
0+
1
+ ). e : ⇧ (xenv : tenv , x : t ).
+ t 0+
`
(x
:
t
,
x
:
t
x
env
txinenv
env
env
env
Intuitively,
should
only
ever
be
x
0
env
` (xenv 0+
:`tenv
,
x
:
t
).
e
:
⇧
(x
:
t
,
x
:
t
).
t
env
env ` t
e
:
t
`
t
:
U
t
.
2
2
e 0: t [⇡ i xenv /x
.. i ]
x
⌘
⇡
x
so
i
i
env
0
·
`
x
:
1,
x
:
A
fails
.
0
.
env
(Simulation
of
Reduction
Relation)
y
:
t
.
e
:
let
x
=
A
:
t
in
⇧
y
:
t
.
x
.
`
e
:
t
+
00
+0
0+?
Lemma
(Equivalence
Preservation)
? ` pack
+). eii : 9 ↵ : 00t . (↵ ⇥ ⇧ (x 0.4
+
0+
ht
,
henv,
(x
:
t
,
x
:
t
:
↵,
x
:
t
).
t
)
` pack
htenv
, ,henv,
(xenv(x
: env
tenv , x : :t+
). eii : 9
↵
: t: 00.t(↵).
⇥⇧
(xienv env
: ↵, x : t `
).+te ) 00+
env
env
0
+
+
?
0+
B
pack
h⌃
env,
⌃
,
x
e
B
e
+
0+
0
0
env
env
env
1
1
2
htenv , henv,
,yand
x::x
eii⌘
: 9ex
↵ ::t A
. (↵ ⇥ ⇧ (xenv : ⇧
↵,
::t t.).tt ;
) 9↵
0 x
+
+ : Type
0+ i , xte : ↵. (xte ) ⇧ x : t . t )
e Bx e` pack
then
B::tenv
:e1,
`
y`: e·,t(xx
.env
eenv
⇧
tt .:).xeA
x
If ` e ⌘ e , then
`e ⌘e
Toward Type-Preserving Compilation of Coq
William J. Bowman
TYPES in target languages
Calculus of Constructions, Closure Converted
STANDARD CLOSURE CONVERSION FAILS
t1 [env/xenv ] ` e1 [env/xenv ] ⌘0 unpack h↵, xte.. , f i =
e
in
f
x
x
2
te
0A). A
A
:
Set
`
(x
:
1,
x
:
A).
x
:
⇧
(x
:
1,
x
:
xitranslation
: ti . . . ` e must
: t22 refer
`
⇧
(x
:
t
.
.
.
).
t
:
U
env
env
.
trivial,
since
env
=
hx
.
.
.i
i
i
i
Problem:
Type
to
the
environment,
which
doesn’t
exist
until
the
type
is
“used”
to
type
check
a
closure
0
(Preservation
of
Equivalence)
Lemma
0.5
(Preservation
of
Subtyping)
2
`
e
⌘
e
0
0 +
+
0+
+
+
0++
0+
0+
`0+
(x
:
t
.
.
.
).
e
:
⇧
(x
:
t
.
.
.
).
t
Type
,
x
:
↵.
(x
)
⇧
x
:
t
.
t
)
0 (⇧ x : t. t+) =+9`↵ :(x
i
i
i
i
+
+). t [⇡ i xenv /xi ]
, x : t ). te
eIf: ⇧
:ttenv
,
x
:
t
t
[⇡
x
/x
][env/x
]
⌘
t
i
env
i
env
0 e env : ti envte
0 `(xtenv
⌘ e , then
` ey : ⌘
` t1
t2
+
2 , then
t . e : let x = A : t in ⇧ y : t . x 1
x : t. e ;+pack htenv , henv, +(xenv
0+ : tenv , x : t ). eii
00
0
`
(x
:
t
,
x
:
t
).
e
:
env
)
⇧
x
:
t
.
t
00
+
0+
env
env
OUR
TRANSLATION
WORKS
.
yx :: t.t t. e;:9⇧↵y: :tt ..(↵
x ⇥ ⇧ (xenv : ↵, x : t )..t )
⇧
Lemma
0.6
(Type
Preservation)
(Preservation
of
Subtyping)
+
+
0+
.
trivial,
since
env
=
hx
.
.
.i
i
+
1
+
`
pack
ht
,
env,
(x
:
t
,
x
:
t
).
ei
:
9
↵
:
Type
,
x
:
↵.
(x
)
⇧
x
:
t
.
t
)
env
env
env
i
te
te
+
+
(
x
:
t.
e)
=
pack
ht
,
env,
(x
:
t
,
x
:
t).
ei
+
where
e
=
let
x
=
⇡
x
in
e
env
env
env
i
i
env
t2 , then
` t1
t2` (x
+
+
0+
0+
0+
+
:
t
,
x
:
t
).
e
:
⇧
(x
:
t
,
x
:
t
).
t
[⇡
x
/x
]
t
[⇡
x
/x
][env/x
]
⌘
t
+
env
env
env
env
i
env
i
i
env
i
env
1.
If
`
then
`
·
`
x
:
1,
x
:
A
fails
env
0 +
+ 0+
env
=
hx
.
.
.i
=
fv(e
)
i
(⇧ x : t. t ) = 9 ↵ : Type i , xte : ↵. (xte ) ⇧ x : t . t )
+
+ 0+
`
(x
:
t
,
x
:
t
).
e
:
env
)
⇧
x
:
t
.
t
x
:
A
`
x
:
A
env
env
(Type Preservation)·, xenv : 1,
+
+
0
0
0 +0
0
2.
If
`
e
:
t
then
`
e
:
t
`
e
:
⇧
x
:
t
.
t
t
[e
/x]
⌘
t
`
e
:
t
`
e
:
e
)
t
0+
1
1
+
+ 0+
Solution:
Lemma
0.2
(Preservation
of
Substitution)
x
/x
]
A : Set
` t (x[⇡
:
1,
x
:
A).
x
:
⇧
(x
:
1,
x
:
A).
A
i htenv , env,
envi `env
env (xenv : tenv , x : t ). ei : 9 ↵ : Type i , xte : ↵. (xte ) ⇧ x : t . t )
pack
0 +
0
+
0
+
0+
types
environment with free`type
(Morrisett et al. 1998)
`(t[t
e : /x])
e ) t⌘tot unify
e e variables
:t
` then ` 1. Use translucent function
[t /x]
1
0
0
0
+ 0+
y
:
t
.
e
:
let
x
=
A
:
t
in
⇧
y
:
t
.
x
A
function
that
knows
which
term
it
will
be
applied
to,
yielding
additional
type
equalities.
⇧
x
:
t.
t
;
9
↵
:
Type
,
x
:
↵.
(x
)
⇧
x
:
t
.
t
)
0+
i
te
te
e : t [⇡ i xenv
0 env
0
+ /xxi ]i+⌘ ⇡
+
x
?
0
?
i
(Preservation
of
Reductions)
` e : t then ` e : ty : t . e : ⇧ y : t . x 0Lemma 0.3
0
0
0
0
`
e
B
pack
h⌃
,
env,
(x
:
⌃
,
x
:
t
).
e
i
`
e
B
e
env
env
env
1
1
2
0
`
e
:
⇧
x
:
t
.
t
t
[e
/x]
⌘
t
`
e
:
t
`
e
:
e
)
t
1
1
` e : t
1 xenv, xenv
tr . .; . ``te2 Bx te20 then + ` e+ B? e and e ⌘ e0+
1 = e : t1 , xr : If
, x :0t1 [env/xenv ] ` e1 [env/xenv ] ⌘
unpack
h↵,
x
,
f
i
=
e
in
f
x
x
0
te
2
te
+
`
e
:
e
)
t
`
e
e
:
t
0
.
x
:
t.
e
;
pack
ht
,
henv,
(x
:
t
,
x
:
t
).
eii
0
+
0+
env
env
env
` ⇧ (x1 :⇧tx1:,t.xtr ;
: t9
t2.. i , xete)
:⇧
trx. .: t. ).. tt2 )
Type
: ↵.⇧(x(x
0i . . .i
r .↵. .: ).
te r)
trivial,
since
env
=
hx
`e⌘e
Lemma 0.4 (Equivalence Preservation)
+
+
+
0+0 where e =
0+
0+
?
0
?
let
x
=
⇡
x
in
e
+
+
0+
env /xi ][env/xenv ] ⌘ t
(xenvh⌃
: tenv
: t ). e(xNow,
: env
⇧ (x
: tenv
[⇡
/xei ]`Beti e[⇡
e B` pack
env,
:env
⌃intuition
,x
t:`
e⌘1tiewithin
our
closure:
env , x
i xthe
env`
Ifis,:xformalized
, then
⌘
env
1t).e).
2 iiexenv
+
env
=
hx
.
.
.i
=
fv(e
)
i
+
+
0+
1
2. Use
existential
types
quantify
the
`
(x
tenv
, xh↵,
:to
t0+
).tee, :fenv
)
xf:environment.
txte
.t x
: t1 [env/xenv
] ` edependent
[env/x
]
⌘
unpack
x
i =over
e2⇧in
env : +
1
env
fails,The
since
x
:
t
,
x
:
t
`
e
:
t
[⇡
x
/x
]
env
env
i
env
i
Lemma
0.5
(Preservation
of
Subtyping)
+
+ to
0+(x
environment
exists,
but
only
as
a
variable,
until
we
use
the
translated
type
type-check
a
closure.
Lemma
0.2
(Preservation
of
Substitution)
0
x
:
t.
e
;
pack
ht
,
env,
:
t
,
x
:
t).
ei
env
env
env
` pack htenv , env, (x`env
:
t
,
x
:
t
).
ei
:
9
↵
:
Type
,
x
:
↵.
(x
)
⇧
x
:
t
.
t
)
env
i
te
te
e ⌘(t[t
e 0 /x])
+
+
+
+
0+
.
+ If+ ⌘ `
0+
` t1
t2
xenv : tenv , x : t ` e :t ..tt[t1 /x]t2 , then
trivial,
since env = hx . . .i
trivial, since
i
+
+
++
0+
+
0+
0+
0+
x
:
t.
e
;
pack
ht
,
henv,
(x
:
t
,
x
:
t
).
eii
Lemma
0.3
(Preservation
of
Reductions)
env
env
env
(xenv
, xe: :t ⇧
). e(x
: env
⇧ (x0:env
: tenv
, :xt: t ).).ttPreservation)
[⇡ i xenv /xi ] 0+t [⇡ i xenv /xi ][env/xenv ] ⌘ t
` (xenv 0 `: tenv
, x ::ttenv
t
,
x
env
0 ).
0
0
Lemma
0.6
(Type
? : e ) t 0+
` e : ⇧ x : t . t1
t1 [e /x] ⌘Ift ; ` e`Bex e:0 tthen + ` e+`B
e
e and e ⌘ e
t
[⇡
x
/x
]
+
+
0+
i
env
i
+= ⇡ x
00 : tenv , x : t ). e : env ) ⇧ x
+: t .0+
+ (xenv
+
`
t
where
e
=
let
x
in
e
i
i
env
0
0
htenv(, henv,
: tenv
,
x
:
t
).
eii
:
9
↵
:
t
.
(↵
⇥
⇧
(x
:
↵,
x
:
t
).
t
)
+
x : t. e) (x=
pack
ht
,
env,
(x
:
t
,
x
:
t).
ei
+
env
env
env
env
env
x
:
t.
e
;
pack
ht
,
henv,
(x
:
t
,
x
:
t
). eii
`env
e :=
e hx
). t. .iLemma
`
e
e
:
t
env
env
env
+ 0.4
1.
If
`
then
`
+
+ 0+
(Equivalence
Preservation)
=
fv(e
)
i
` pack htenv , env, (xenv
:
t
,
x
:
t
).
ei
:
9
↵
:
Type
,
x
:
↵.
(x
)
⇧
x
:
t
.
t
)
env
i
te
te
If ` e ⌘ e0 , then + ` e+ ⌘ e0+
+
where
e
=
let
x
=
⇡
x
in
e
x
⌘
⇡
x
i + i +
env
+
i
i
env
Lemma 0.2 (Preservation
of Substitution)
0
2. If ` e : t then`
`
e
:
t
+
e
:
t
,
x
=
e
:
t
,
x
:
t
.
.
.
`
t
t
env
=
hx
.
.
.i
=
fv(e
)
i
1
1
1
r
r
2
Lemma
0.5
(Preservation
of
Subtyping)
0xenv
+ env
+ 0+
2
0+
(t[t /x]) ⌘ t [t /x]
0
0
0
0
0
t
[⇡
x
/x
]
+
+
+
i tt11 [e /x]
` e : ⇧ xi: t env
. t1If Lemma
⌘ (Preservation
t
e t2: of
t Substitution)
`e:e )t
0
`
t20.2
, then
` t1` `
⇧
(x
:
t
,
x
:
t
.
.
.
).
t
e
)
⇧
(x
:
t
.
.
.
).
t
1
1
r
r
2
r
r
2
0
+
+
0+
0
0
Lemma (Simulation of Reduction Relation)
Compiler
“for free”
(t[t
/x])
⌘ttcorrectness
[t /x]
`
e
:
e
)
`
e
e
:
t
Because the type system
0
+
+
?
0+
Lemma
0.6
(Type
Preservation)
If
;
`
e
B
e
then
`
e
B
e
and
e
⌘
e
x
?
0
?
Type-preservation proof
2
includes
the
contextual
closure
of
Lemma (Simulation
of
Reduction
Relation)
` e0 B
pack
h⌃
,
env,
(x
:
⌃
,
x
:
t
).
e
i
`
e
B
e
env
env
env
1
1
2
+
+
+
?
0+
1.
If
`
then
`
If
;
`
e
B
e
then
`
e
B
e
and
e
⌘
e
0
the
small-step
reduction
relation.
x
Lemma `
(Preservation
of
Equivalence)
+
0+
,
x
:
t
[env/x
]
`
e
[env/x
]
⌘
unpack
h↵,
x
,
f
i
=
e
in
f
x
x
e
:
t
,
x
=
e
:
t
,
x
:
t
.
.
.
`
t
fails,
since
x
:
t
,
x
:
t
`
e
:
t
[⇡
x
/x
]
1
env
1
env
te
2
te
1
1
1
r
r
2
env
env
i
env
i
2
If ` e ⌘ e0 , then + ` e+ ⌘ e0+
+
+
(Equivalence
2.Lemma
If ` e0.4
: t then
` e+ : 0tPreservation)
0
+
0+
0
+
+
0+
Soon:
`
e
⌘
e
`
⇧
(x
:
t
,
x
:
t
.
.
.
).
t
e
:
t
.
.
t
If)` ⇧
e ⌘(x
e , rthen
`.e). ⌘
e
x
:
t
,
x
:
t
`
e
:
t
1
1
r
r
2
r
env
env
2
Lemma (Preservation of Subtyping)
An
eta
principle
/
normal
form
for
closures
Inductive types
& 0+
+
+
+
+
+
Lemma
0.5
(Preservation
of
Subtyping)
If ` t1 t2 , then
` t1
t2
`
(x
:
t
,
x
:
t
).
e
:
⇧
(x
:
t
,
x
:
t
).
t
env
env
env
env
+
+
+
0
?
guarded
recursion.
If ``et1B? tpack
`
t
t
2 , then
1
2
h⌃
,
env,
(x
:
⌃
,
x
:
t
).
e
i
`
e
B
e
env
env 1. Ifenv
1 ` 1+
2
Lemma
(Type
Preservation)
`
then
+
00
Lemma (Type Preservation)
3
pack
ht
,
henv,
(x
:
t
,
x
:
t
).
eii
:
9
↵
:
t
.
(↵
⇥
⇧
(x
:
↵,
x
, x : t1 `
[env/x
]
`
e
[env/x
]
⌘
unpack
h↵,
x
,
f
i
=
e
in
f
x
x
env
env
env
env
env
1x : t. eenv
te
2
te
(xenv : tenv , x : t). ei
+
+
+; pack htenv , env,
2. If ` e : t then
`e :t
0
+
1. If ` then `
`
e
⌘
e
+
0+
x
:
t
,
x
:
t
`
e
:
t
env
env
Necessary to reason
syntactically
about
closures
in
the
type
system
2. If ` e : t then + ` e+ : t+
?
0
?
3
2
`
e
B
pack
h⌃
,
env,
(x
:
⌃
,
x
:
t
).
e
i
`
e
B
e
+
+
0+
env
env
env 0+ 1
1
2
` (xenv : tenv , x : t ). e : ⇧ (x, xenv
:
t
,
x
:
t
).
t
t
[⇡
x
/x
]
env
i
env
i
: t1 [env/x
]
`
e
[env/x
]
⌘
unpack
h↵,
x
,
f
i
=
e
in
x : t. eenv
; pack
env, (xenv : tenv , te
x : t). ei2 f xte x
1 htenv ,env
0
.
`
e
⌘
e
` e B? pack h⌃env , env, (xenv : ⌃env .., x : t1 ). e1 i
` e0 B ? e2
0+
, x : t [env/x ] ` e [env/x ] ⌘ unpack h↵, x , f i = e in f x x t [⇡ x /x ]
OTHER interesting aspects
:
© Copyright 2025 Paperzz