derived-krivine-machine-agda swierstra dk · ru. nl this paper presents the derivation of an...
TRANSCRIPT
From
Mat
hem
atic
sto
Abs
tract
Mac
hine
Afo
rma
lderivation
of
an
exe
cu
tab
leK
rivin
em
ach
ine
Wou
ter
Sw
iers
tra
Radb
oud
Unive
rsity
Nijm
egen
w.s
wie
rstr
a@
cs
.ru
.nl
This
pape
rpr
esen
tsth
ede
rivat
ion
ofan
exec
utab
leK
rivin
eab
stra
ctm
achi
nefro
ma
smal
lst
epin
ter-
pret
erfo
rthe
sim
ply
type
dla
mbd
aca
lcul
usin
the
depe
nden
tlyty
ped
p ro
gram
min
gla
ngua
geAg
da.
1In
tro
du
ctio
n
Ther
eis
acl
ose
rela
tions
hip
betw
een
lam
bda
calc
uli
with
expl
icit
subs
titut
ions
and
abst
ract
mac
hine
s.B
iern
acka
and
Dan
vy[7
]ha
vesh
own
how
tode
rive
seve
ral
wel
l-kno
wn
abst
ract
mac
hine
sin
clud
ing
the
Kriv
ine
mac
hine
[14,
15,
21,
22],
the
CE
Km
achi
ne[1
9],
and
the
Zin
cm
achi
ne[2
3].
Sta
rting
with
afu
nctio
nal
prog
ram
that
eval
uate
sb
yre
peat
edhe
adr e
duct
ion,
each
of
thes
eab
stra
ctm
achi
nes
may
bede
rived
b y
ase
ries
of
prog
ram
trans
form
atio
ns.
Eve
rytra
nsfo
rmat
ion
isca
refu
llym
otiv
ated
inth
eac
com
pany
ing
text
.Th
ispa
per
aim
sto
nail
dow
nth
eco
rrect
ness
of
thes
ede
rivat
ions
furth
eran
d,in
the
proc
ess,
unco
ver
even
mor
est
ruct
ure.
Inth
isp
aper
we
show
how
the
deriv
atio
np
rese
nted
byB
iern
acka
and
Dan
vyca
nbe
form
aliz
edin
the
depe
nden
tlyty
ped
prog
ram
min
gla
ngua
geA
gda
[25]
.W
hat
dow
eh
ope
toga
inb
ydo
ing
so?
Inth
eir
stud
yre
latin
gev
alua
tors
and
abst
ract
mac
hine
s,A
ger
etal
.[1
]st
ate
inth
ein
trodu
ctio
n:
Mos
tof
our
impl
emen
tatio
nsof
the
abst
ract
mac
hine
sr a
iseco
mpi
ler
war
ning
sab
out
non-
exha
ustiv
em
atch
es.
Thes
ear
ein
here
ntto
p ro
gram
min
gab
stra
ctm
achi
nes
inan
ML-
like
lang
uage
.
This
pape
rde
mon
stra
tes
that
thes
eno
n-ex
haus
tive
mat
ches
are
not
inhe
rent
toa
depe
nden
tlyty
ped
pro-
gram
min
gla
ngua
gesu
chas
Agd
a.A
llth
efu
nctio
nsw
epr
esen
the
rear
est
ruct
ural
lyre
curs
ive
and
prov
ide
alte
rnat
ives
for
ever
yca
sebr
anch
.Th
issh
iftto
ade
pend
ently
type
dla
ngua
gegi
ves
usm
any
prop
ertie
sof
eval
uatio
n‘f
orfre
e.’
For
exam
ple,
from
the
type
sal
one
we
lear
nth
atev
alua
tion
isty
pepr
eser
ving
and
that
ever
yte
rmca
nb
ede
com
pose
dun
ique
lyin
toa
rede
xan
dev
alua
tion
cont
ext.
Fin
ally
,us
ing
Agd
aen
able
sus
topr
ovid
ea
mac
hine
-che
cked
p ro
ofof
the
corre
ctne
ssof
ever
ytra
nsfo
rmat
ion.
Mor
esp
ecifi
cally
,th
isp
aper
mak
esth
efo
llow
ing
conc
rete
cont
ribut
ions
:
•W
ede
scrib
eth
eim
plem
enta
tion
of
asm
all
step
eval
uato
rin
Agd
ath
atno
rmal
izes
byre
peat
edhe
adre
duct
ion
(Sec
tion
3).
Toco
nvin
ceAg
da’s
term
inat
ion
chec
ker
that
our
defin
ition
isso
und,
we
prov
ide
ano
rmal
izat
ion
proo
fin
the
styl
eof
Tait
[30]
,or
igin
ally
sket
ched
b y
Coq
uand
[13]
(Sec
tion
4).
•A
pply
ing
the
refo
cusi
ngtra
nsfo
rmat
ion
[18]
,yi
elds
asm
all-s
tep
abst
ract
mac
hine
that
isno
tye
tta
il-re
curs
ive
(Sec
tion
5).
We
prov
eth
atth
istra
nsfo
rmat
ion
p re
serv
esth
ese
man
tics
and
term
ina-
tion
p ro
perti
esoft
hesm
all-s
tep
eval
uato
rfr
omS
ectio
n4.
J.Ch
apm
anan
dP.
B.L
evy
(Eds
.):Fo
urth
Wor
ksho
pon
Mat
hem
atica
llySt
ructu
red
Func
tiona
lP
rogr
amm
ing(M
SFP
2012
).
EPTC
S76
,201
2,pp
.16
3–17
7,do
i:10
.420
4/E
PTC
S.7
6.10
164
Fro
mM
ath
emat
ics
t oA
bstra
ctM
achi
ne
•Th
issm
all-s
tep
abst
ract
mac
hine
can
b etra
nsfo
rmed
furth
erto
deriv
eth
eK
rivin
em
achi
ne(S
ec-
tion
6).
Onc
eag
ain,
we
show
that
the
trans
form
atio
npr
eser
ves
the
sem
antic
san
dte
rmin
atio
npr
oper
ties
oft
hesm
all-s
tep
abst
ract
mac
hine
from
Sec
tion
5.
This
p ap
eris
alit
erat
eA
gda
prog
ram
.R
athe
rth
ansp
ellin
gou
tth
ede
tails
of e
very
proo
f,w
ew
illon
lysk
etch
the
nece
ssar
yle
mm
asan
dde
finiti
ons.
The
com
plet
eso
urce
code
,in
clud
ing
proo
fs,
isav
aila
ble
onlin
e.1
Eve
ryse
ctio
nin
this
pape
rde
fines
ase
para
tem
odul
e,al
low
ing
usto
reus
eth
esa
me
nam
esfo
rth
efu
nctio
nsan
dda
taty
pes
pres
ente
din
indi
vidu
alse
ctio
ns.
Fin
ally
,th
eco
dein
this
pape
rus
esa
shor
tA
gda
Pre
lude
that
isin
clud
edin
anap
pend
ix.
Rea
ders
unfa
mili
arw
ithA
gda
may
wan
tto
cons
ult
one
of
the
man
ytu
toria
lsan
din
trodu
ctio
nsth
atar
eav
aila
ble
[10,
26,2
7].
2T
yp
es
an
dte
rms
Bef
ore
we
can
deve
lop
the
serie
sof
eva
luat
ors,
we
need
tode
fine
the
term
san
dty
pes
oft
hesi
mpl
yty
ped
lam
bda
calc
ulus
.
data
Ty:
Set
whe
reO
:T
y_
⇒_
:T
y→
Ty
→T
y
Con
text
:S
etC
onte
xt=
List
Ty
The
data
type
Ty
repr
esen
tsth
ety
pes
of
the
sim
ply
type
dla
mbd
aca
lcul
usw
ithon
eba
sety
peO
.A
cont
ext
isde
fined
tobe
alis
to
fty
pes.
Typi
cally
the
varia
bles
σan
dτ
rang
eov
erty
pes;
the
varia
bles
Γan
d∆
rang
eov
erco
ntex
ts.
Nex
tw
ede
fine
the
data
type
so
fw
ell-t
yped
,w
ell-s
cope
dva
riabl
esan
dla
mbd
ate
rms:
data
Ref
:C
onte
xt→
Ty
→Se
tw
here
Top
:R
ef(C
ons
σΓ
)σ
Pop
:R
efΓ
σ→
Ref
(Con
sτ
Γ)
σ
data
Term
:C
onte
xt→
Ty
→Se
tw
here
Lam
:Te
rm(C
ons
σΓ
)τ
→Te
rmΓ
(σ⇒
τ)A
pp:
Term
Γ(σ
⇒τ)
→Te
rmΓ
σ→
Term
Γτ
Var
:R
efΓ
σ→
Term
Γσ
Thes
ede
finiti
ons
are
entir
ely
stan
dard
.Th
ere
are
thre
eco
nstru
ctor
sfo
rth
esi
mpl
yty
ped
lam
bda
calc
ulus
:La
min
trodu
ces
ala
mbd
a,ex
tend
ing
the
cont
ext;
the
App
cons
truct
orap
plie
sa
term
of
type
σ⇒
τto
anar
gum
ent
of
type
σ;
the
Var
cons
truct
orr e
fere
nces
ava
riabl
eb
ound
inth
eco
ntex
t.N
ote
that
inth
ety
pese
tco
depr
esen
ted
inth
ispa
per,
any
unbo
und
varia
bles
inty
pesi
gnat
ures
are
impl
icitl
yun
iver
sally
quan
tifie
d,as
isth
eco
nven
tion
inH
aske
ll[2
4]an
dE
pigr
am[2
8].
W h
enw
ew
ish
tobe
mor
eex
plic
itab
out
impl
icit
argu
men
ts,
we
will
adhe
reto
Agda
’sno
tatio
nof
encl
osin
gsu
char
gum
ents
incu
rlybr
aces
.N
ext,
we
can
defin
eth
eda
taty
pes
repr
esen
ting
clos
edte
rms.
Acl
osur
eis
ate
rmt
paire
dw
ithan
envi
ronm
ent
cont
aini
ngcl
osed
term
sfo
ra
llth
efre
eva
riabl
esin
t.Fu
rther
mor
e,cl
osed
term
sar
ecl
osed
unde
rap
plic
atio
n.Th
isyi
elds
the
two
mut
ually
recu
rsiv
eda
taty
pes
defin
edbe
low
.
1The
sourc
eco
de,
com
patib
lew
ithAg
dave
rsio
n2.
3,is
avail
able
from
http
://w
ww.c
s.r
u.n
l/~w
oute
rs.
W.S
wie
rstra
165
data
Clo
sed
:T
y→
Set
whe
reC
losu
re:
Term
Γσ
→En
vΓ
→C
lose
dσ
Cla
pp:
Clo
sed
(σ⇒
τ)→
Clo
sed
σ→
Clo
sed
τ
data
Env
:C
onte
xt→
Set
whe
reN
il:
Env
Nil
_·_
:C
lose
dσ
→En
vΓ
→En
v(C
ons
σΓ)
This
isa
varia
tion
of
Cur
ien’
sλρ
-cal
culu
s,pr
opos
edby
Bie
rnac
kaan
dD
anvy
[7].
Asi
mila
rch
oice
of
clos
edte
rms
was
inde
pend
ently
prop
osed
byC
oqua
nd[1
3].
The
aim
of
eval
uatio
nis
toco
mpu
tea
valu
efo
rev
ery
clos
edte
rm.
Clo
sed
lam
bda
expr
essi
ons
are
the
only
valu
esin
our
lang
uage
.Th
efin
alde
finiti
ons
inth
isse
ctio
nca
ptur
eth
is:
isVa
l:
Clo
sed
σ→
Set
isVa
l(C
losu
re(L
ambo
dy)
env)
=U
nit
isVa
l=
Em
pty
data
Val
ue(σ
:Ty
):
Set
whe
reV
al:
(c:
Clo
sed
σ)
→is
Val
c→
Valu
eσ
With
thes
ety
pes
inpl
ace,
we
can
spec
ifyth
ety
peo
fth
eev
alua
tion
func
tion
we
will
defin
ein
the
com
ing
sect
ions
:
eval
uate
:C
lose
dσ
→Va
lue
σ
3R
ed
uctio
n
Writ
ing
t[en
v]to
deno
teth
ecl
osur
eco
nsis
ting
of
ate
rmta
ndan
envi
ronm
ent
env,
the
four
rule
sin
belo
wsp
ecify
ano
rmal
-ord
ersm
all
step
redu
ctio
nre
latio
nfo
rth
ecl
osed
term
s.In
this
sect
ion,
we
will
star
tto
impl
emen
tth
ese
rule
sin
Agd
a.
LOO
KU
Pi[
c1,c
2,..
.cn]
→ci
AP
P(t0
t1)
[env
]→
(t0[e
nv])
(t1
[env
])
BETA
((λt)
[env
])x
→t[
x·en
v]
LEFT
ifc0→
c0′th
enc0
c1→
c0′c1
Inth
est
yle
of
Dan
vyan
dN
iels
en[1
8],
we
defin
ea
sing
lere
duct
ion
step
inth
ree
parts
.F
irst,
we
deco
mpo
sea
clos
edte
rmin
toa
rede
xan
dan
eval
uatio
nco
ntex
t.S
econ
d,w
eco
ntra
ctth
ere
dex
tofo
rma
new
clos
edte
rm.
Fin
ally
,w
ep
lug
the
resu
lting
clos
edte
rmba
ckin
toth
eev
alua
tion
cont
ext.
To
defin
esu
cha
thre
e-st
epre
duct
ion
step
,w
est
art
b y
defin
ing
the
Red
exty
pe,
corr
espo
ndin
gto
the
left-
hand
side
soft
hefir
stth
ree
rule
sab
ove.
data
Red
ex:
Ty
→S
etw
here
Look
up:
Ref
Γσ
→E
nvΓ
→R
edex
σR
app
:Te
rmΓ
(σ⇒
τ)→
Term
Γσ
→En
vΓ
→R
edex
τB
eta
:Te
rm(C
ons
σΓ
)τ
→E
nvΓ
→C
lose
dσ
→R
edex
τ
166
Fro
mM
ath
emat
ics
t oA
bstra
ctM
achi
ne
Of
cour
se,
ever
yr e
dex
can
b em
appe
db
ack
toth
ecl
osed
term
that
itre
pres
ents
.
from
Red
ex:
Red
exσ
→C
lose
dσ
from
Red
ex(L
ooku
pie
nv)
=C
losu
re(V
ari)
env
from
Red
ex(R
app
fxen
v)=
Clo
sure
(App
fx)
env
from
Red
ex(B
eta
body
env
arg)
=C
lapp
(Clo
sure
(Lam
body
)en
v)ar
g
Nex
t,w
ede
fine
the
cont
ract
func
tion
that
com
pute
sth
er e
sult
ofc
ontra
ctin
ga
sing
ler e
dex:
!:
Env
Γ→
Ref
Γσ
→C
lose
dσ
__ Nil
!()
(x··)
!To
p=
x(x
·xs
)!
Pop
r=
xs!
r
cont
ract
:R
edex
σ→
Clo
sed
σco
ntra
ct(L
ooku
pie
nv)
=en
v!i
cont
ract
(Rap
pfx
env)
=C
lapp
(Clo
sure
fenv
)(C
losu
rex
env)
cont
ract
(Bet
abo
dyen
var
g)=
Clo
sure
body
(arg
·en
v)
Inth
eLo
okup
case
,w
elo
okup
the
varia
ble
from
the
envi
ronm
ent
usin
gth
e_
!_o
pera
tor.
The
Rap
pca
sedi
strib
utes
the
envi
ronm
ent
over
the
two
term
s.F
inal
ly,
Beta
r edu
ctio
nex
tend
sth
een
viro
nmen
tw
ithth
ear
gum
ent
arg,
and
u se
sth
eex
tend
eden
viro
nmen
tto
crea
tea
new
clos
ure
from
the
body
of
a
lam
bda.
Onc
eag
ain,
the
defin
ition
oft
heco
ntra
ctfu
nctio
ncl
osel
yfo
llow
sth
efir
stth
ree
redu
ctio
nru
les
that
we
form
ulat
edab
ove.
Whi
leth
isde
scrib
esho
wto
cont
ract
asi
ngle
rede
x,w
est
illne
edto
defin
eth
ede
com
posi
tion
of
ate
rmin
toa
r ede
xan
da
redu
ctio
nco
ntex
t.W
ebe
gin
b y
defin
ing
anev
alua
tion
cont
ext
asth
elis
to
far
gum
ents
enco
unte
red
alon
gth
esp
ine
of
ate
rm:
data
Eva
lCon
text
:T
y→
Ty
→Se
tw
here
MT
:E
valC
onte
xtσ
σ
AR
G:
Clo
sed
σ→
Eva
lCon
text
τρ
→E
valC
onte
xt(σ
⇒τ)
ρ
Igno
ring
the
Ty
indi
ces
for
the
mom
ent,
anev
alua
tion
cont
ext
issi
mpl
ya
list
of
clos
edte
rms.
Giv
enan
yev
alua
tion
cont
ext
ctx
and
term
t,w
ew
ould
like
top
lug
tin
the
cont
ext
byite
rativ
ely
appl
ying
tto
all
the
argu
men
tsin
ctx.
For
this
toty
pech
eck,
the
term
tsh
ould
abst
ract
over
all
the
varia
bles
inth
eev
alua
tion
cont
ext.
We
enfo
rce
this
b y
inde
the
Eva
lCon
text
type
b y
the
‘sou
rce’
and
‘des
tinat
ion’
type
sin
the
styl
eo
fAtk
ey[3
].T
hepl
ugop
erat
ion
itsel
fth
enap
plie
san
yar
gum
ents
from
the
eval
uatio
nco
ntex
tto
itsar
gum
ent
term
:
plug
:E
valC
onte
xtσ
τ→
Clo
sed
σ→
Clo
sed
τpl
ugM
Tf
=f
plug
(AR
Gx
ctx)
f=
plug
ctx
(Cla
ppfx
)
Fin
ally
,w
ede
fine
the
deco
mpo
sitio
nof
acl
osed
t erm
into
are
dex
and
eval
uatio
nco
ntex
tas
avi
ew[2
4,31
]on
clos
edte
rms.
Def
inin
gsu
cha
view
cons
ists
of
two
parts
:a
data
type
Dec
ompo
sitio
nin
dexe
dby
acl
osed
term
,an
da
func
tion
deco
mpo
seth
atm
aps
ever
ycl
osed
term
toits
Dec
ompo
sitio
n.W
ew
illst
art
byde
finin
ga
data
type
Dec
ompo
sitio
n.T
here
are
two
cons
truct
ors,
corr
espo
ndin
gto
the
two
poss
ible
outc
omes
of
deco
mpo
sing
acl
osed
t erm
c:ei
ther
cis
ava
lue,
inw
hich
case
we
h av
e
W.S
wie
rstra
167
the
clos
ure
of
aLa
m-te
rman
dan
envi
ronm
ent;
alte
rnat
ivel
y,c
can
bede
com
pose
din
toa
rede
xr
and
anev
alua
tion
cont
ext
ctx,
such
that
plug
ging
the
term
corr
espo
ndin
gto
rin
the
eval
uatio
nco
ntex
tct
xis
equa
lto
the
orig
inal
term
c:
data
Dec
ompo
sitio
n:
Clo
sed
σ→
Set
whe
reV
al:
(bod
y:
Term
(Con
sσ
Γ)
τ)→
(env
:En
vΓ
)→
Dec
ompo
sitio
n(C
losu
re(L
ambo
dy)
env)
Dec
ompo
se:
(r:
Red
exσ)
→(c
tx:
Eva
lCon
text
στ
)→
Dec
ompo
sitio
n(p
lug
ctx
(fro
mR
edex
r))
Nex
tw
esh
owho
wev
ery
clos
edte
rmc
can
b ede
com
pose
din
toa
Dec
ompo
sitio
nc.
We
doso
byde
finin
ga
p ai
rof
func
tions
,lo
adan
dun
load
.Th
elo
adfu
nctio
ntra
vers
esth
esp
ine
of
c,ac
cum
ulat
ing
any
argu
men
tsw
een
coun
ter
inan
eval
uatio
nco
ntex
tun
tilw
efin
da
rede
xor
acl
osur
eco
ntai
ning
aLa
m.
The
unlo
adfu
nctio
nin
spec
tsth
eev
alua
tion
cont
ext
that
load
has
accu
mul
ated
inor
der
tode
cide
ifa
lam
bda
isin
deed
ava
lue,
orw
heth
erit
still
has
furth
erar
gum
ents
,an
dhe
nce
corre
spon
dsto
aBe
tare
dex:
load
:(c
tx:
Eva
lCon
text
στ )
(c:
Clo
sed
σ)
→D
ecom
posi
tion
(plu
gct
xc)
load
ctx
(Clo
sure
(Lam
body
)en
v)=
unlo
adct
xbo
dyen
vlo
adct
x(C
losu
re(A
ppfx
)en
v)=
Dec
ompo
se(R
app
fxen
v)ct
x
load
ctx
(Clo
sure
(Var
i)en
v)=
Dec
ompo
se(L
ooku
pie
nv)
ctx
load
ctx
(Cla
ppfx
)=
load
(AR
Gx
ctx)
f
unlo
ad:
(ctx
:E
valC
onte
xt(σ
⇒τ)
ρ)
(bod
y:
Term
(Con
sσ
Γ)τ)
(env
:En
vΓ)
→D
ecom
posi
tion
(plu
gct
x(C
losu
re(L
ambo
dy)
env)
)un
load
MT
body
env
=V
albo
dyen
vun
load
(AR
Gar
gct
x)bo
dyen
v=
Dec
ompo
se(B
eta
body
env
arg)
ctx
The
deco
mpo
sefu
nctio
nits
elf
sim
ply
kick
soff
load
with
anin
itial
lyem
pty
eval
uatio
nco
ntex
t.
deco
mpo
se:
(c:
Clo
sed
σ)→
Dec
ompo
sitio
nc
deco
mpo
sec
=lo
adM
Tc
Tope
rform
asi
ngle
r edu
ctio
nst
ep,
we
deco
mpo
sea
clos
edte
rm.
Ifth
isyi
elds
ava
lue,
ther
eis
nofu
rther
r edu
ctio
nto
b edo
ne.
I fde
com
posi
tion
yiel
dsa
r ede
xan
dev
alua
tion
cont
ext,
we
cont
ract
the
rede
xan
dp
lug
the
resu
ltb
ack
into
the
eval
uatio
nco
ntex
t:
head
Red
uce
:C
lose
dσ
→C
lose
dσ
head
Red
uce
cw
ithde
com
pose
che
adR
educ
e⌊C
losu
re(L
ambo
dy)
env⌋
|V
albo
dyen
v=
Clo
sure
(Lam
body
)en
vhe
adR
educ
e⌊p
lug
ctx
(from
Red
exre
dex)
⌋
|
Dec
ompo
sere
dex
ctx
=pl
ugct
x(c
ontra
ctre
dex)
Not
eth
atpa
ttern
mat
chin
gon
the
Dec
ompo
sitio
npr
oduc
esm
ore
info
rmat
ion
abou
tth
ete
rmth
atha
sbe
ende
com
pose
d.Th
isis
appa
rent
inth
ef o
rced
patte
rns
[25]
,⌊C
losu
re(L
ambo
dy)
env⌋
inth
eV
albr
anch
and
⌊plu
gct
x(fr
omR
edex
rede
x)⌋
inth
eD
ecom
pose
bran
ch,
that
appe
aron
the
left-
hand
side
of
the
func
tion
defin
ition
.
This
com
plet
esou
rde
finiti
onof
asi
ngle
head
r edu
ctio
nst
ep.
168
Fro
mM
ath
emat
ics
t oA
bstra
ctM
achi
ne
4It
era
ted
he
ad
redu
ction
Inth
epr
evio
usse
ctio
nw
ees
tabl
ishe
dho
wto
perfo
rma
sing
lere
duct
ion
step
.N
owit
shou
ldb e
stra
ight
-fo
rwar
dto
defin
ean
eval
uatio
nfu
nctio
nby
itera
tivel
yre
duci
ngb
ya
sing
lest
epun
tilw
ere
ach
ava
lue:
eval
uate
:C
lose
dσ
→Va
lue
σev
alua
tec
=ite
rate
(dec
ompo
sec)
whe
reite
rate
:D
ecom
posi
tion
c→
Valu
eσ
itera
te(V
alva
lp)
=V
alva
lp
itera
te(D
ecom
pose
rctx
)=
itera
te(d
ecom
pose
(plu
gct
x(c
ontra
ctr)
))
Ther
eis
one
prob
lem
with
this
defin
ition
:it
isno
tst
ruct
ural
lyre
curs
ive.
Itis
reje
cted
byA
gda.
Yet
we
know
that
the
sim
ply
type
dla
mbd
aca
lcul
usis
stro
ngly
norm
aliz
ing—
soite
rativ
ely
perfo
rmin
ga
sing
leh
ead
redu
ctio
nw
illal
way
spr
oduc
ea
valu
eev
entu
ally
.H
owca
nw
eco
nvin
ceA
gda
oft
his
fact
?Th
eB
ove-
Cap
retta
met
hod
ison
ete
chni
que
totra
nsfo
rma
defin
ition
that
isno
tst
ruct
ural
lyre
curs
ive
into
aneq
uiva
lent
defin
ition
that
isst
ruct
ural
lyre
curs
ive
over
ane
war
gum
ent
[9].
Ess
entia
lly,
itdo
esst
ruct
ural
r ecu
rsio
nov
erth
eca
llgr
aph
of
afu
nctio
n.In
our
case
,w
ew
ould
like
toha
vean
inha
bita
nto
fth
efo
llow
ing
data
type
:
data
Trac
e:{c
:C
lose
dσ}
→D
ecom
posi
tion
c→
Set
whe
reD
one
:(b
ody
:Te
rm(C
ons
σΓ)
τ)→
(env
:En
vΓ
)→
Trac
e(V
albo
dyen
v)
Ste
p:
Trac
e(d
ecom
pose
(plu
gct
x(c
ontra
ctr)
))→
Trac
e(D
ecom
pose
rctx
)
We
coul
dth
ende
fine
the
itera
tefu
nctio
nby
stru
ctur
alin
duct
ion
over
the
trace
:
itera
te:{
c:
Clo
sed
σ}
→(d
:D
ecom
posi
tion
c)→
Trac
ed
→Va
lue
σ
itera
te(V
albo
dyen
v)(D
one
⌊bo
dy⌋
⌊env
⌋)
=V
al(C
losu
re(L
ambo
dy)
env)
unit
itera
te(D
ecom
pose
rctx
)(S
tep
step
)=
itera
te(d
ecom
pose
(plu
gct
x(c
ontra
ctr)
))st
ep
Alth
ough
this
defin
ition
does
pass
Agda
’ste
rmin
atio
nch
ecke
r,th
equ
estio
nre
mai
nsho
wto
prov
ide
the
requ
ired
Trac
ear
gum
ent
toou
rite
rate
func
tion.
That
isw
ew
ould
like
tode
fine
afu
nctio
nof
type
:
(t:
Clo
sed
σ)→
Trac
et
Ast
raig
htfo
rwar
dat
tem
ptto
defin
esu
cha
func
tion
fails
imm
edia
tely
.In
stea
d,w
ene
edto
defin
eth
efo
llow
ing
logi
cal
rela
tion
that
stre
ngth
ens
our
indu
ctio
nhy
poth
esis
:
Red
ucib
le:{σ
:T
y}→
(t:
Clo
sed
σ)
→S
etR
educ
ible
{O}t
=Tr
ace
(dec
ompo
set)
Red
ucib
le{σ
⇒τ
}t
=P
air
(Tra
ce(d
ecom
pose
t))
((x
:C
lose
dσ
)→
Red
ucib
lex
→R
educ
ible
(Cla
ppt
x))
Red
ucib
leEn
v:
Env
Γ→
Set
Red
ucib
leE
nvN
il=
Uni
tR
educ
ible
Env
(x·en
v)=
Pai
r(R
educ
ible
x )(R
educ
ible
Env
env)
Top
rove
that
all
clos
edte
rms
are
r edu
cibl
e,w
efo
llow
the
proo
fsk
etch
edby
Coq
uand
[13]
and
p ro
veth
efo
llow
ing
two
lem
mas
.
W.S
wie
rstra
169
lem
ma1
:(c
:C
lose
dσ)
→R
educ
ible
(hea
dRed
uce
c)→
Red
ucib
lec
lem
ma2
:(t
:Te
rmΓ
σ)(e
nv:
Env
Γ)
→R
educ
ible
Env
env
→R
educ
ible
(Clo
sure
ten
v)
The
proo
fof
lem
ma2
perfo
rms
indu
ctio
non
the
term
t.In
each
oft
hebr
anch
es,
we
appe
alto
lem
ma1
inor
der
topr
ove
that
Clo
sure
ten
vis
also
redu
cibl
e.Th
ep
roof
of
lem
ma1
isdo
neb
yin
duct
ion
onσ
and
c.Th
eon
lydi
fficu
ltca
seis
that
for
clos
edap
plic
atio
ns,
Cla
ppfx
.In
that
bran
ch,
we
n ee
dto
show
that
Cla
pp(h
eadR
educ
e(C
lapp
fx))
yis
equa
lto
head
Red
uce
(Cla
pp(C
lapp
fx )
y).
Top
rove
the
desi
red
equa
lity
we
obse
rve
t hat
ifde
com
posi
ngC
lapp
fxyi
elds
ar e
dex
rand
eval
uatio
nco
ntex
tct
x,th
enth
ede
com
posi
tion
of
Cla
pp(C
lapp
fx )
ym
ust
yiel
dth
esa
me
r ede
xw
ithth
eev
alua
tion
cont
ext
obta
ined
b y
addi
ngy
toth
een
dof
ctx.
Toco
mpl
ete
the
p ro
ofw
ede
fine
anau
xilia
ry‘b
ackw
ards
view’
onev
alua
tion
cont
exts
that
stat
esth
atev
ery
eval
uatio
nco
ntex
tis
eith
erem
pty
orar
ises
byad
ding
acl
osed
term
toth
een
dof
anev
alua
tion
cont
ext.
Usi
ngth
isvi
ew,
the
requ
ired
equa
lity
isea
syto
p ro
ve.
Usi
ngle
mm
a1an
dle
mm
a2,
we
can
p ro
veou
rm
ain
theo
rem
:ev
ery
clos
edte
rmis
redu
cibl
e.To
doso
,we
defin
eth
efo
llow
ing
two
mut
ually
r ecu
rsiv
eth
eore
ms:
mut
ual
theo
rem
:(c
:C
lose
dσ
)→
Red
ucib
lec
theo
rem
(Clo
sure
ten
v)=
lem
ma2
ten
v(e
nvTh
eore
men
v)th
eore
m(C
lapp
fx)
=sn
d(th
eore
mf)
x(th
eore
mx)
envT
heor
em:
(env
:En
vΓ
)→
Red
ucib
leE
nven
ven
vThe
orem
Nil
=un
it
envT
heor
em(t
·ts
)=
(theo
rem
t,en
vThe
orem
ts)
Top
rove
that
ever
ycl
osur
eis
r edu
cibl
e,w
eap
peal
tole
mm
a2an
dpr
ove
that
ever
ycl
osed
term
inth
een
viro
nmen
tis
also
redu
cibl
e.Th
epr
oof
that
ever
ycl
osed
appl
icat
ion
isre
duci
ble
recu
rses
over
b ot
har
gum
ents
fand
x.Th
ere
curs
ive
call
tof
yiel
dsa
p ai
rof
atra
cean
da
func
tion
oft
ype:
((x
:C
lose
dσ
)→
Red
ucib
lex
→R
educ
ible
(Cla
ppfx
))
App
lyin
gth
isfu
nctio
nto
xan
dth
eore
mx,
yiel
dsth
ede
sire
dp
roof
.O
neim
porta
ntco
rolla
ryofo
urth
eore
mis
that
for
ever
ycl
osed
term
c,w
eca
nco
mpu
tean
eval
uatio
ntra
ceof
c:
term
inat
ion
:{
σ:
Ty}
→(c
:C
lose
dσ
)→
Trac
e(d
ecom
pose
c)te
rmin
atio
n{
O}
c=
theo
rem
cte
rmin
atio
n{
σ⇒
τ}
c=
fst
(theo
rem
c)
Now
we
can
final
lyco
mpl
ete
the
defin
ition
ofo
ursm
all
step
eval
uatio
nfu
nctio
n:
eval
uate
:C
lose
dσ
→Va
lue
σev
alua
tet
=ite
rate
(dec
ompo
set)
(term
inat
ion
t)
The
eval
uate
func
tion
itera
tivel
ype
rform
sa
sing
lest
epof
head
redu
ctio
n,p
erfo
rmin
gst
ruct
ural
indu
c-tio
nov
erth
etra
ceth
atw
eco
mpu
teus
ing
the
r edu
cibi
lity
proo
fsk
etch
edab
ove.
5R
efo
cusin
g
The
smal
lst
epev
alua
tor
pres
ente
din
the
prev
ious
sect
ion
r epe
ated
lyde
com
pose
sa
clos
edte
rmin
toan
eval
uatio
nco
ntex
tan
da
r ede
x,co
ntra
cts
the
r ede
x,an
dpl
ugs
the
cont
ract
umb
ack
into
the
eval
uatio
n
170
From
M a
them
atic
st o
Abs
tract
Mac
hine
cont
ext.
Bef
ore
trans
form
ing
this
eval
uato
rin
toth
eK
rivin
em
achi
ne,
we
will
show
how
toap
ply
the
re-
focu
sing
trans
form
atio
nto
prod
uce
asm
all-s
tep
abst
ract
mac
hine
[17]
.Th
issm
all-s
tep
abst
ract
mac
hine
form
sa
conv
enie
ntha
lfway
p oi
ntbe
twee
nth
esm
all
step
eval
uato
ran
dth
eK
rivin
em
achi
ne.
The
key
idea
ofr
efo
cusi
ngis
toco
mpo
seth
epl
uggi
ngan
dde
com
posi
tion
step
sin
toa
sing
lere
focu
sop
erat
ion.
Inst
ead
of
r epe
ated
lypl
uggi
ngan
dde
com
posi
ng,
the
refo
cus
func
tion
navi
gate
sdi
rect
lyto
the
next
rede
x,if
itex
ists
:
refo
cus
:(c
tx:
Eva
lCon
text
στ )
(c:
Clo
sed
σ)
→D
ecom
posi
tion
(plu
gct
xc)
refo
cus
MT
(Clo
sure
(Lam
body
)en
v)=
Val
body
env
refo
cus
(AR
Gx
ctx)
(Clo
sure
(Lam
body
)en
v)=
Dec
ompo
se(B
eta
body
env
x)ct
xre
focu
sct
x(C
losu
re(V
ari)
env)
=D
ecom
pose
(Loo
kup
ienv
)ct
xre
focu
sct
x(C
losu
re(A
ppfx
)en
v)=
Dec
ompo
se(R
app
fxen
v)ct
xre
focu
sct
x(C
lapp
fx)
=re
focu
s(A
RG
xct
x)f
We
can
form
aliz
eth
isin
tuiti
onab
out
the
beha
viou
rofr
efoc
usin
gby
prov
ing
the
follo
win
gle
mm
a:
refo
cusC
orre
ct:
(ctx
:E
valC
onte
xtσ
τ )(c
:C
lose
dσ
)→
refo
cus
ctx
c≡
deco
mpo
se(p
lug
ctx
c)
The
proo
fb
yin
duct
ion
onct
xan
dc
r elie
son
anea
syle
mm
a:
deco
mpo
seP
lug
:(c
tx:
Eva
lCon
text
στ )
(c:
Clo
sed
σ)
→de
com
pose
(plu
gct
xc)
≡lo
adct
xc
The
proo
foft
hede
com
pose
Plu
gle
mm
apr
ocee
dsb
ysi
mpl
ein
duct
ion
onth
eev
alua
tion
cont
ext.
Tore
writ
eou
rev
alua
tor
tous
eth
ere
focu
sop
erat
ion,
we
will
need
toad
apt
the
Trac
eda
taty
pefr
omth
epr
evio
usse
ctio
n.Ite
rate
dr e
curs
ive
calls
will
nolo
nger
call
deco
mpo
sean
dpl
ug,
but
inst
ead
navi
gate
toth
ene
xtr e
dex
usin
gth
ere
focu
sfu
nctio
n.Th
ene
wTr
ace
data
type
refle
cts
j ust
that
:
data
Trac
e:
Dec
ompo
sitio
nc
→Se
tw
here
Don
e:
(bod
y:
Term
(Con
sσ
Γ)τ)
→(e
nv:
Env
Γ)→
Trac
e(V
albo
dyen
v)S
tep
:Tr
ace
(ref
ocus
ctx
(con
tract
r))
→Tr
ace
(Dec
ompo
serc
tx)
Topr
ove
that
this
new
Trac
eda
taty
peis
inha
bite
d,w
eca
llth
ete
rmin
atio
nle
mm
afr
omth
epr
evio
usse
ctio
n.U
sing
the
refo
cusC
orre
ctle
mm
a,w
epe
rform
indu
ctio
non
the
Trac
eda
taty
pefr
omth
epr
evio
usse
ctio
nto
cons
truct
aw
itnes
sof
term
inat
ion.
All
this
isdo
neby
the
follo
win
gte
rmin
atio
nfu
nctio
n:
term
inat
ion
:(c
:C
lose
dσ
)→
Trac
e(r
efoc
usM
Tc)
The
defin
ition
ofo
urev
alua
tor
isno
wst
raig
htfo
rwar
d.Th
eite
rate
func
tion
repe
ated
lyre
focu
ses
and
cont
ract
su
ntil
ava
lue
has
been
reac
hed:
itera
te:
(d:
Dec
ompo
sitio
nc)
→Tr
ace
d→
Valu
eσ
itera
te(V
albo
dyen
v)(D
one
⌊bod
y⌋⌊e
nv⌋
)=
Val
(Clo
sure
(Lam
body
)en
v)un
itite
rate
(Dec
ompo
serc
tx)
(Ste
pst
ep)
=ite
rate
(ref
ocus
ctx
(con
tract
r))
step
eval
uate
:C
lose
dσ
→Va
lue
σ
eval
uate
c=
itera
te(re
focu
sM
Tc)
(term
inat
ion
c)
W.S
wie
rstra
171
The
eval
uate
func
tion
kick
so
ffth
eite
rate
func
tion
with
anem
pty
eval
uatio
nco
ntex
tan
da
proo
fo
fte
rmin
atio
n.F
inal
ly,
we
can
also
show
that
our
new
eval
uato
rbe
have
sth
esa
me
asth
eev
alua
tion
func
tion
pre-
sent
edin
the
prev
ious
sect
ion.
Todo
so,w
epr
ove
the
follo
win
gle
mm
aby
indu
ctio
non
the
deco
mpo
si-
tion
oft: co
rrect
ness
:{t
:C
lose
dσ
}→
(trac
e:
Trac
e(r
efoc
usM
Tt)
)→
(trac
e’:
Sec
tion4
.Tra
ce(d
ecom
pose
t))
→
itera
te(r
efoc
usM
Tt)
trace
≡S
ectio
n4.it
erat
e(d
ecom
pose
t)tra
ce’
An
impo
rtant
coro
llary
of
this
corre
ctne
sspr
oper
tyis
that
our
new
eval
uatio
nfu
nctio
nbe
have
sid
enti-
cally
t oth
eev
alua
tefu
nctio
nfr
omth
epr
evio
usse
ctio
n:
coro
llary
:(t
:C
lose
dσ)
→ev
alua
tet
≡S
ectio
n4.e
valu
ate
tco
rolla
ryt
=co
rrect
ness
(ter
min
atio
nt)
(Sec
tion4
.term
inat
ion
t)
This
com
plet
esth
ede
finiti
onan
dve
rific
atio
no
fth
eev
alua
tor
that
aris
esb
yap
plyi
ngth
ere
focu
sing
trans
form
atio
non
the
smal
lst
epev
alua
tor
from
Sec
tion
4.
6T
he
Krivi
nem
achin
e
Inth
isse
ctio
nw
ew
illde
rive
the
Kriv
ine
mac
hine
from
the
eval
uatio
nfu
nctio
nw
esa
wp
revi
ousl
y.To
com
plet
eou
rde
rivat
ion,
we
perfo
rma
few
furth
erpr
ogra
mtra
nsfo
rmat
ions
onth
epr
evio
usev
alua
tion
func
tion.
We
star
tb
yin
linin
gth
eite
rate
func
tion,
mak
ing
our
refo
cus
func
tion
recu
rsiv
e.Fu
rther
mor
e,th
eev
alua
tefu
nctio
nin
the
prev
ious
sect
ion
map
ped
App
term
sin
tocl
osed
Cla
ppte
rms,
and
subs
eque
ntly
eval
uate
dth
efir
star
gum
ent
of
the
resu
lting
Cla
ppco
nstru
ctor
,ad
ding
the
seco
ndar
gum
ent
toth
eev
alu-
atio
nco
ntex
t.In
this
sect
ion,
we
will
com
bine
thes
etw
ost
eps
into
asi
ngle
tran
sitio
n—a
trans
form
atio
nso
met
imes
refe
rred
toas
com
pres
sing
corr
idor
trans
ition
s[1
6].
As
are
sult,
we
will
nolo
nger
add
clos
edap
plic
atio
nsto
the
envi
ronm
ent
or
eval
uatio
nco
ntex
t.W
ein
trodu
ceth
efo
llow
ing
p re
dica
tes
enfo
rcin
gth
eab
senc
eo
fC
lapp
cons
truct
ors
oncl
osed
term
s,en
viro
nmen
ts,
and
eval
uatio
nco
ntex
tsr e
spec
tivel
y:
mutu
al
isV
alid
Clo
sure
:C
lose
dσ
→Se
tis
Val
idC
losu
re(C
losu
ret
env)
=is
Val
idE
nven
vis
Val
idC
losu
re(C
lapp
fx )
=E
mpt
y
isV
alid
Env
:En
v∆
→Se
tis
Val
idE
nvN
il=
Uni
tis
Val
idE
nv(c
·en
v)=
Pai
r(is
Val
idC
losu
rec)
(isV
alid
Env
env)
isV
alid
Con
text
:E
valC
onte
xtσ
τ→
Set
isV
alid
Con
text
MT
=U
nit
isV
alid
Con
text
(AR
G(C
losu
ret
env)
ctx)
=P
air
(isV
alid
Env
env)
(isV
alid
Con
text
ctx)
isV
alid
Con
text
(AR
G(C
lapp
fx)
env)
=E
mpt
y
172
Fro
mM
ath
emat
ics
t oA
bstra
ctM
achi
ne
Giv
enth
atth
eon
lyva
lidcl
osed
term
sar
ecl
osur
es,
we
can
defin
efu
nctio
nsth
atpr
ojec
tth
eun
derly
ing
envi
ronm
ent
and
term
from
any
valid
clos
edte
rm:
getC
onte
xt:
Exis
ts(C
lose
dσ)
isV
alid
Clo
sure
→C
onte
xtge
tCon
text
(Witn
ess
(Clo
sure
{Γ}
ten
v)))
=Γ
getC
onte
xt(W
itnes
s(C
lapp
fx )
())
getE
nv:
(c:
Exis
ts(C
lose
dσ
)is
Val
idC
losu
re)
→En
v(g
etC
onte
xtc)
getE
nv(W
itnes
s(C
losu
ret
env)
p)=
env
getE
nv(W
itnes
s(C
lapp
fx)
())
getT
erm
:(c
:Ex
ists
(Clo
sed
σ)
isV
alid
Clo
sure
)→
Term
(get
Con
text
c)σ
getT
erm
(Witn
ess
(Clo
sure
ten
v)p)
=t
getT
erm
(Witn
ess
(Cla
ppfx
)()
)
Fin
ally
,w
eca
nde
fine
ane
wlo
okup
oper
atio
nth
atgu
aran
tees
that
look
ing
upa
varia
ble
ina
valid
envi
ronm
ent
will
alw
ays
r etu
rna
clos
ure:
look
up:
Ref
Γσ
→(e
nv:
Env
Γ)
→is
Val
idE
nven
v→
Exis
ts(C
lose
dσ)
isV
alid
Clo
sure
look
upTo
p(C
losu
ret
env
··)
(p1,
p2)
=W
itnes
s(C
losu
ret
env)
p1
look
upTo
p(C
lapp
··)
((),
,)lo
okup
(Pop
i)(
·en
v)(,
p)=
look
upie
nvp
Ifth
ear
gum
ent
refe
renc
eis
Top,
we
patte
rnm
atch
onth
een
viro
nmen
t,w
hich
mus
tco
ntai
na
clos
ure.
We
use
the
p ro
ofth
atth
een
viro
nmen
tco
ntai
nsex
clus
ivel
ycl
osur
esto
disc
harg
eth
eC
lapp
bran
ch.
Ifth
ear
gum
ent
r efe
renc
eis
Pop
i,w
ere
curs
eov
eria
ndth
eta
iloft
heen
viro
nmen
t.O
nce
agai
n,w
ede
fine
aTr
ace
data
type
,de
scrib
ing
the
call-
grap
hof
the
Kriv
ine
mac
hine
.Th
eTr
ace
data
type
isin
dexe
dby
the
thre
ear
gum
ents
toth
eK
rivin
em
achi
ne:
ate
rm,
anen
viro
nmen
t,an
dan
eval
uatio
nco
ntex
t.Th
eda
taty
peha
sa
cons
truct
orfo
rev
ery
trans
ition
;re
curs
ive
calls
toth
eab
stra
ctm
achi
neco
rresp
ond
tor e
curs
ive
argu
men
tsto
aco
nstru
ctor
:
data
Trac
e:
Term
Γσ
→En
vΓ
→E
valC
onte
xtσ
τ→
Set
whe
reLo
okup
:(i
:R
efΓ
σ)
(p:
isV
alid
Env
env)
→le
tc
=lo
okup
ienv
pin
Trac
e(g
etTe
rmc)
(get
Env
c)ct
x→
Trac
e(V
ari)
env
ctx
App
:(f
:Te
rmΓ
(σ⇒
τ))
(x:
Term
Γσ
)→
Trac
efe
nv(A
RG
(Clo
sure
xen
v)ct
x)→
Trac
e(A
ppfx
)en
vct
xB
eta
:(c
tx:
Eva
lCon
text
σρ
)→
(arg
:Te
rmH
τ)→
(arg
Env
:En
vH
)→
(bod
y:
Term
(Con
sτ
Γ)σ)
→Tr
ace
body
(Clo
sure
arg
argE
nv·
env)
ctx
→Tr
ace
(Lam
body
)en
v(A
RG
(Clo
sure
arg
argE
nv)
ctx)
Don
e:
(bod
y:
Term
(Con
sτ
Γ)σ
)→
Trac
e(L
ambo
dy)
env
MT
Usi
ngth
isTr
ace,
we
can
now
defin
eth
efin
alve
rsio
nof
the
refo
cus
func
tion,
corr
espo
ndin
gto
the
Kriv
ine
abst
ract
mac
hine
,by
stru
ctur
alre
curs
ion
onth
isTr
ace.
The
resu
lting
mac
hine
corre
spon
dsto
W.S
wie
rstra
173
the
Kriv
ine
mac
hine
asis
usua
llypr
esen
ted
inth
elit
erat
ure
[14,
15,
21].
Bie
rnac
kaan
dD
anvy
[7]
also
cons
ider
the
deriv
atio
nof
Kriv
ine’
sor
igin
alm
achi
ne[2
2]th
atco
ntra
cts
nest
edβ-
redu
ctio
nsin
one
step
.
refo
cus
:(c
tx:
Eva
lCon
text
στ )
(t:
Term
Γσ)
(env
:En
vΓ)
→Tr
ace
ten
vct
x→
Valu
eτ
refo
cus
ctx
⌊Var
i⌋en
v(L
ooku
piq
step
)=
let
c=
look
upie
nvq
inre
focu
sct
x(g
etTe
rmc)
(get
Env
c)st
epre
focu
sct
x⌊A
ppfx
⌋en
v(A
ppfx
step
)=
refo
cus
(AR
G(C
losu
rex
env)
ctx)
fen
vst
epre
focu
s⌊A
RG(C
losu
rear
gen
v’)
ctx⌋
⌊Lam
body
⌋en
v(B
eta
ctx
arg
env’
body
step
)=
refo
cus
ctx
body
((C
losu
rear
gen
v’)
·en
v)st
epre
focu
s⌊M
T⌋⌊L
ambo
dy⌋
env
(Don
ebo
dy)
=V
al(C
losu
re(L
ambo
dy)
env)
unit
Inth
eca
sefo
rva
riabl
es,
we
look
upth
ecl
osur
eth
atth
eva
riabl
er e
fers
toin
the
envi
ronm
ent,
and
con-
tinue
eval
uatio
nw
ithth
atclo
sure
’ste
rman
den
viro
nmen
t.In
the
case
for
App
fx,w
ead
dth
ear
gum
ent
and
curre
nten
viro
nmen
tto
the
appl
icat
ion
cont
ext,
and
cont
inue
eval
uatin
gth
ete
rmf.
We
dist
ingu
ish
two
furth
erca
ses
for
lam
bda
term
s:if
the
eval
uatio
nco
ntex
tis
not
empt
y,w
eca
npe
rform
ab
eta
redu
c-tio
nst
ep;
othe
rwis
eev
alua
tion
isfin
ishe
d.W
est
illne
edt o
p ro
veth
atth
eTr
ace
data
type
isi n
habi
ted.
Dur
ing
exec
utio
n,th
eK
rivin
em
achi
neon
lyad
dscl
osur
esto
the
envi
ronm
ent
and
eval
uatio
nco
ntex
t.D
urin
gth
ete
rmin
atio
np
roof
,w
ew
illne
edto
keep
track
of
the
follo
win
gin
varia
nton
eval
uatio
nco
ntex
tsan
den
viro
nmen
ts:
inva
riant
:E
valC
onte
xtσ
τ→
Env
Γ→
Set
inva
riant
ctx
env
=P
air
(isV
alid
Env
env)
(isV
alid
Con
text
ctx)
The
p ro
ofof
term
inat
ion
once
agai
nca
llsth
ete
rmin
atio
np
roof
from
the
prev
ious
sect
ion.
An
auxi
liary
lem
ma
show
sth
atan
yw
itnes
sof
term
inat
ion
for
the
smal
l-ste
pab
stra
ctm
achi
nein
Sec
tion
5w
illal
sosu
ffice
asa
proo
fof
term
inat
ion
oft
heK
rivin
em
achi
ne.
term
inat
ion
:(t
:Te
rmN
ilσ)
→Tr
ace
tN
ilM
Tte
rmin
atio
nt
=le
mm
aM
Tt
Nil
(uni
t,un
it)(S
ectio
n5.te
rmin
atio
n(C
losu
ret
Nil)
)w
here
lem
ma
:(c
tx:
Eva
lCon
text
στ )
(t:
Term
Γσ)
(env
:En
vΓ)
→in
varia
ntct
xen
v→
Sec
tion5
.Tra
ce(S
ectio
n5.re
focu
sct
x(C
losu
ret
env)
)→
Trac
et
env
ctx
The
lem
ma
ispr
oven
byst
raig
htfo
rwar
din
duct
ion
onth
eev
alua
tion
cont
ext,
the
term
,an
dth
eTr
ace
data
type
from
the
prev
ious
sect
ion.
Onc
ew
epa
ttern
mat
chon
the
term
and
the
eval
uatio
nco
ntex
t,w
ekn
oww
hich
trans
ition
we
wis
hto
mak
e,an
dhe
nce
whi
chco
nstru
ctor
of
the
Trac
eda
tat y
peis
requ
ired.
Any
recu
rsiv
eoc
curre
nces
oft
heTr
ace
data
type
can
b e
prod
uced
b y
r ecu
rsiv
eca
llsto
the
lem
ma.
The
only
othe
rre
sult
nece
ssar
yst
ates
that
the
look
upfu
nctio
nan
dth
e_!_
o pe
ratio
nw
esa
wp
revi
ousl
yr e
turn
the
sam
ecl
osed
term
from
anen
viro
nmen
t.F
inal
ly,
we
can
defin
eth
eev
alua
tion
func
tion
that
calls
refo
cus
with
asu
itabl
ech
oice
for
itsin
itia
lar
gum
ents
:
eval
uate
:Te
rmN
ilσ
→V
alue
σev
alua
tet
=re
focu
sM
Tt
Nil
(ter
min
atio
nt)
174
From
M a
them
atic
st o
Abs
tract
Mac
hine
Toco
nclu
de,
we
show
that
this
final
vers
ion
of
the
refo
cus
func
tion
beha
ves
equi
vale
ntly
toth
ere
focu
sfu
nctio
nfr
omth
epr
evio
usse
ctio
n.To
p ro
veth
is,
we
form
ulat
eth
eco
rrect
ness
prop
erty
belo
w.
corre
ctne
ss:
(ctx
:E
valC
onte
xtσ
τ )(t
:Te
rmΓ
σ)(e
nv:
Env
Γ)
→(t
1:
Trac
et
env
ctx)
→
(t2:
Sec
tion5
.Tra
ce(S
ectio
n5.re
focu
sct
x(C
losu
ret
env)
))→
refo
cus
ctx
ten
vt1
≡S
ectio
n5.it
erat
e(S
ectio
n5.re
focu
sct
x(C
losu
ret
env)
)t2
Onc
eag
ain,
the
p ro
ofpr
ocee
dsb
yst
raig
htfo
rwar
din
duct
ion
onth
etra
ces.
As
are
sult
oft
his
corre
ctne
sspr
oper
ty,
we
can
prov
eth
atou
rev
alua
tion
func
tion
beha
ves
the
sam
eas
the
func
tion
pres
ente
din
the
prev
ious
sect
ion:
coro
llary
:(t
:Te
rmN
ilσ)
→ev
alua
tet
≡S
ectio
n5.e
valu
ate
(Clo
sure
tN
il)co
rolla
ryt
=le
ttra
ce=
term
inat
ion
tin
let
trace
’=
Sec
tion5
.term
inat
ion
(Clo
sure
tN
il)in
corre
ctne
ssM
Tt
Nil
trace
trace
’
By
chai
ning
toge
ther
our
corre
ctne
ssre
sults
,w
eca
nsh
owth
atou
rK
rivin
em
achi
nepr
oduc
esth
esa
me
valu
eas
our
orig
inal
eval
uato
rba
sed
onre
peat
edhe
adr e
duct
ion,
ther
eby
com
plet
ing
the
form
alde
rivat
ion
of
the
Kriv
ine
mac
hine
from
asm
all
step
eval
uato
r.
7Di
scus
sion
Ther
eha
sb
een
prev
ious
wor
kon
form
aliz
ing
the
deriv
atio
nso
fab
stra
ctm
achi
nes
inC
oq[6
,29
].In
cont
rast
toth
ede
velo
pmen
the
re,
thes
efo
rmal
izat
ions
are
not
exec
utab
lebu
tin
stea
dde
fine
the
r edu
ctio
nbe
havi
our
asin
duct
ive
r ela
tions
betw
een
term
san
dva
lues
.Th
eex
ecut
abili
tyo
fou
rab
stra
ctm
achi
nes
com
esat
apr
ice:
we
need
top
rove
that
the
eval
uato
rste
rmin
ate,
whi
chre
quire
sa
clev
erlo
gica
lr e
latio
n.O
nth
eot
her
h an
d,it
isea
sier
tore
ason
abou
tex
ecut
able
func
tions
.In
type
theo
ry,
defin
ition
aleq
ualit
ies
are
alw
ays
triv
ially
t rue
—afa
ctyo
uca
non
lyex
ploi
tif
your
func
tions
com
pute
.Th
isp
aper
u se
sth
eB
ove-
Cap
retta
met
hod
top
rove
term
inat
ion
of
ever
yev
alua
tor.
Cha
pman
and
Alte
nkirc
hus
ea
sim
ilar
logi
cal
rela
tion
topr
oduc
ein
habi
tant
so
fB o
ve-C
apre
ttapr
edic
ates
whe
nw
ritin
ga
big-
step
norm
aliz
atio
nal
gorit
hm[2
].Th
ere
are,
of
cour
se,
alte
rnat
ive
met
hods
tosh
owth
ata
non-
stru
ctur
ally
recu
rsiv
efu
nctio
ndo
este
rmin
ate.
For
exam
ple,
itm
aybe
inte
rest
ing
toin
vest
igat
eho
wto
adap
tth
eno
rmal
izat
ion
p ro
ofto
use
anor
der
onla
mbd
ate
rms
prop
osed
b y
Gan
dy[2
0]to
defin
ea
suita
ble
acce
ssib
ility
r ela
tion.
Fin
ally
,yo
um
ayw
onde
rif
the
usag
eof
logi
cal
rela
tions
topr
ove
term
inat
ion
is‘c
heat
ing.’
Afte
rall,
the
com
puta
tiona
lco
nten
to
fno
rmal
izat
ion
proo
fsus
ing
logi
cal
rela
tions
isits
elf
ano
rmal
izat
ion
algo
rithm
[4,5
,8]—
sois
our
smal
l-ste
pev
alua
tor
not
j ust
read
ing
off
the
valu
efr
omth
etra
ceth
atou
rpr
oof
com
pute
s?N
otat
all!
Infa
ct,
the
beha
viou
ro
fth
eite
rate
func
tion
from
Sec
tion
4is
inde
pend
ent
of
the
trace
we
p ro
vide
—onc
eth
eite
rate
func
tion
mat
ches
onth
ear
gum
ent
deco
mpo
sitio
n,th
etra
cepa
ssed
asan
argu
men
tto
the
itera
tefu
nctio
nis
uniq
uely
dete
rmin
ed.
The
follo
win
gst
atem
ent
isea
syto
prov
e:
colla
psib
le:
(d:
Dec
ompo
sitio
nc)
(t1
t2:
Trac
ed)
→t1
≡t2
Inot
her
wor
ds,
the
trace
sth
emse
lves
carr
yno
com
puta
tiona
lco
nten
t.S
uch
colla
psib
leda
taty
pes
may
beer
ased
b y
asu
itabl
ecl
ever
com
pile
r[1
1,12
].Th
ispa
per
focu
ses
onth
ede
rivat
ion
of
the
Kriv
ine
abst
ract
mac
hine
.Th
ere
isno
r eas
onto
belie
ve
that
the
othe
rde
rivat
ions
of
abst
ract
mac
hine
s[1
,7]
may
not
befo
rmal
ized
ina
sim
ilar
fash
ion.
W.
S w
iers
tra17
5
Ack
no
wle
dg
em
en
ts
Iw
ould
like
toth
ank
Jam
esM
cKin
nafo
rou
ren
terta
inin
gan
ded
ucat
iona
ldi
scus
sion
s.M
ałg
orza
taB
iern
acka
,P
ierr
e-E
varis
teD
agan
d,O
livie
rD
anvy
,Ily
aSe
rgey
,Th
omas
van
Noo
rtan
dfo
uran
onym
ous
revi
ewer
sa
llpr
ovid
edin
valu
able
feed
back
ona
draf
tve
rsio
no
fth
ispa
per,
for
whi
chI
amgr
atef
ul.
Refer
ence
s[1
]M
ads
Sig
Age
r,D
ariu
szB
iern
acki
,O
livie
rD
anvy
&Ja
nM
idtg
aard
(200
3):
Af u
nctio
nal
corr
e-sp
onde
nce
betw
een
eval
uato
rsan
dab
stra
ctm
achi
nes.
In:
Pro
ceed
ings
o ft
he5t
hA
CM
SIG
PLA
NIn
tern
atio
nal
C o
nfer
ence
o n
P rin
cipl
esan
dP
ract
ice
ofD
ecl
ariti
veP
rogr
amm
ing,
AC
M,
pp.
8–19
,do
i:10.
114
5/88
825
1.88
8254
.
[2]
Thor
sten
A lt
enki
rch
&Ja
mes
Cha
pman
(200
9):
Big
-ste
pno
rmal
isat
ion.
Jour
nal
o fF
unc
tiona
lP
rogr
amm
ing
19(3
-4),
pp.
311
–333
,do
i:10
.101
7/S
0956
7968
0900
7278
.
[3]
Rob
ert
Atk
ey(2
009)
:P
ara
met
eris
edno
tions
of c
ompu
tatio
n.Jo
urna
lof
F u
nctio
nal
P ro
gram
min
g19
(3&
4),
pp.
335–
376,
doi:
10.1
017/
S09
5679
6809
0072
8X.
[4]
Ulric
hB
erge
r(1
993)
:P
rogr
amex
tract
ion
f rom
norm
aliz
atio
npr
oofs
.Ty
ped
L am
bda
Cal
culi
and
App
licat
ions
,pp
.91
–106
,do
i:10
.100
7/B
Fb00
3710
0.
[5]
Ulric
hB
erge
r,S
tefa
nB
ergh
ofer
,P
ierr
eLe
touz
ey&
Hel
mut
Sch
wic
hten
berg
(200
6):
Pro
gram
ex-
trac
tionf
rom
norm
aliz
atio
npr
oofs
.S
tudi
aL
ogic
a82
(1),
do
i:10
.100
7/s1
1225
-006
-660
4-5
.
[6]
Mał
gorz
ata
Bie
rnac
ka&
Dar
iusz
Bie
rnac
ki(2
007)
:Fo
rmal
izin
gC
onst
ruct
ions
ofA
bstra
ctM
a-ch
ines
for
Fun
ctio
nal
L an
guag
esin
Coq
.In
:7t
hI n
tern
atio
nal
Wor
ksho
po
nR
edu
ctio
nS
trate
gies
inR
ew
ritin
gan
dP
rogr
amm
ing,
pp.
84–9
9.
[7]
Mał
gorz
ata
Bie
rnac
ka&
Oliv
ier
Dan
vy(2
007)
:A
conc
rete
f ram
ewor
kf o
ren
viro
nmen
tm
achi
nes.
AC
MT
rans
actio
nso
nC
om
puta
tiona
lL
ogic
9(1)
,pp
.6:
1–6:
30,
doi:
10.1
145/
1297
658.
1297
664.
[8]
M a
łgor
zata
Bie
rnac
ka,
Oliv
ier
Dan
vy&
Kris
tian
Stø
vrin
g(2
006)
:P
rogr
amex
trac
tionf
rom
p ro
ofs
of w
eak
head
norm
aliz
atio
n.E
lect
roni
cN
ote
si n
T he
oret
ical
Com
pute
rSc
ienc
e15
5,pp
.16
9–18
9,d
oi:
10.1
016/
j.ent
cs.2
005.
11.0
56.
[9]
Ana
Bov
e&
Ven
anzi
oC
apre
tta(2
005)
:M
ode
lling
gene
ral
recu
rsio
nin
type
theo
ry.
Mat
hem
atic
alS
truct
ures
i nC
ompu
ter
Sci
ence
15(4
),pp
.67
1–70
8,do
i:10
.101
7/S
0960
1295
0500
4822
.
[10]
Ana
Bov
e&
Pet
erD
ybje
r(2
009)
:D
epe
nden
tTy
pes
atW
ork.
InA
naB
ove,
Luís
Bar
bosa
,A
lber
toP
ardo
&Jo
rge
P in
to,
edito
rs:
Lang
uage
E n
gine
erin
gan
dR
igor
ous
Sof
twar
eD
eve
lopm
ent,
Lect
ure
Not
esi n
Com
pute
rSc
ienc
e55
20,
Spr
inge
r,pp
.57
–99,
doi:
10.1
007/
978-
3-64
2-03
153-
3.
[11]
E d
win
Bra
dy(2
005)
:P
ract
ical
I mpl
emen
tatio
nof
aD
epe
nden
tlyTy
ped
Fun
ctio
nal
P ro
gram
min
gLa
ngua
ge.
Ph.
D.
thes
is,
Uni
vers
ityo
fDur
ham
.
[12]
E d
win
Bra
dy,
Con
orM
cBrid
e&
Jam
esM
cKin
na(2
003)
:I n
duct
ive
Fam
ilies
N e
edN
otS
tore
The
irIn
dice
s.In
:TY
PE
S,
pp.
115–
129,
doi:
10.1
007/
978-
3-54
0-24
849-
1_8.
[13]
Thi
erry
Coq
uand
(199
9):
Indu
ctiv
eD
efin
ition
san
dTy
peTh
eory
:an
intr
oduc
tion.
In:
Pro
ceed
ings
of th
eT
YPES
S um
mer
Sch
ool.
[14]
Pie
rre
Cré
gut
(200
7):
Stro
ngly
Red
ucin
gV
aria
nts
of th
eK
rivin
eA
bstra
ctM
achi
ne.
Hig
her-
Ord
er
and
Sym
bolic
C o
mpu
tatio
n20
(3),
pp.
209–
230,
doi:
10.1
007/
s109
90-0
07-9
015-
z.
176
Fro
mM
ath
emat
ics
t oA
bstra
ctM
achi
ne
[15]
Pie
rre-
Loui
sC
urie
n(1
991)
:A
nab
stra
ctf r
amew
orkf
or
envi
ronm
ent
mac
hine
s.Th
eore
tical
C o
m-
pute
rSc
ienc
e82
(2),
pp.
389–
402,
doi:
10.1
016/
0304
-397
5(91
)902
30-Y
.
[16]
Oliv
ier
Dan
vy(2
008)
:Fr
omre
duct
ion-
base
dto
redu
ctio
n-fre
eno
rmal
izat
ion.
InP
iete
rK
oop-
man
,R
inus
Pla
smei
jer
&D
oaits
eS
wie
rstra
,ed
itors
:P
roce
edin
gso
fth
e6t
hI n
tern
atio
nal
Sch
ool
o n
Adv
ance
dF
unct
iona
lP
rogr
amm
ing,
LNC
S53
82,
Spr
inge
r-V
erla
g,pp
.66
–164
,do
i:10
.100
7/97
8-3-
642-
0465
2-0_
3.
[17]
Oliv
ier
Dan
vy&
Kev
inM
illik
in(2
008)
:O
nth
eeq
uiva
lenc
ebe
twee
nsm
all-s
tep
and
big-
step
ab-
stra
ctm
achi
nes:
asi
mpl
eap
plic
atio
nof
ligh
twei
ght f
usio
n.In
form
atio
nP
roce
ssin
gLe
tters
106(
3),
pp.
100–
109,
doi:
10.1
016/
j.ipl
.200
7.10
.010
.
[18]
Oliv
ier
Dan
vy&
Lass
eR
.N
iels
en(2
004)
:R
efo
cusi
ngin
R e
duct
ion
Sem
antic
s.Te
chni
cal
Rep
ort
RS
-04-
26,
BR
ICS
.
[19]
Mat
thia
sFe
lleis
en&
Dan
iel
P.Fr
iedm
an(2
005)
:C
ontr
olop
erat
ors,
the
SE
CD
-mac
hine
and
the
lam
bda-
calc
ulus
.F
orm
alD
esc
riptio
nof
P r
ogra
mm
ing
Con
cept
sII
I.
[20]
Rob
inG
andy
(198
0):
P ro
ofs
of S
trong
N o
rmal
izat
ion.
InJo
nath
anS
eldi
n&
Rog
erH
indl
ey,
edito
rs:
ToH
.B.
C u
rry:
E ss
ays
o n
C o
mbi
nato
ryL
ogic
,L
ambd
aC
alcu
lus
and
F or
mal
ism
,A
cade
mic
Pres
s,pp
.457
–490
.
[21]
Chr
isH
anki
n(1
994)
:L
ambd
aC
alcu
li,a
guid
ef o
rco
mpu
ter
scie
ntis
ts.
Gra
duat
eT
exts
inC
ompu
ter
Scie
nce
1,O
xfor
dU
nive
rsity
Pres
s.
[22]
Jean
-Lou
isK
rivin
e(2
007)
:A
call-
by-n
ame
lam
bda-
calc
ulus
mac
hine
.H
ighe
rO
rder
and
Sym
bolic
Com
puta
tion
20(3
),pp
.19
9–20
7,do
i:10
.100
7/s1
0990
-007
-901
8-9.
[23]
Xav
ier
Lero
y(1
990)
:Th
eZ
INC
expe
rimen
t:an
econ
omic
alim
plem
enta
tion
oft
heM
Lla
ngua
ge.
Tech
nica
lR
epor
t,IN
RIA
Roc
quen
cour
t.
[24]
Con
orM
cBrid
e&
Jam
esM
cKin
na(2
004)
:Th
evi
ewf r
omth
ele
ft.Jo
urna
lof
F u
nctio
nal
Pro
gram
-m
ing
14(1
),pp
.69
–111
,doi
:10.
1017
/S09
5679
6803
0048
29.
[25]
Ulf
Nor
ell
(200
7):
Tow
ards
ap
ract
ical
p ro
gram
min
gla
ngua
geba
sed
onde
pend
ent
type
theo
ry.
Ph.
D.
thes
is,
Cha
lmer
sU
nive
rsity
ofT
echn
olog
y.
[26]
UlfN
ore
ll(2
008)
:D
epe
nden
tlyTy
ped
Pro
gram
min
gin
A gd
a.In
Pie
ter
Koo
pman
,R
inus
Pla
smei
jer
&D
oaits
eS
wie
rstra
,ed
itors
:A
dvan
ced
F un
ctio
nal
P ro
gram
min
g,LN
CS
-Tut
oria
l58
32,
Spr
inge
r-V
erla
g,pp
.23
0–26
6,do
i:10
.100
7/97
8-3-
642-
0465
2-0_
5.
[27]
Nic
olas
Our
y&
W o
uter
Sw
iers
tra(2
008)
:Th
eP
ower
of
Pi.
In:
ICF
P’0
8:P
roce
edin
gso
fth
eT
hirte
enth
AC
MS
IGP
LAN
I nte
rnat
iona
lC
onf
eren
ceo
nF
unct
iona
lP
rogr
amm
ing,
pp.
39–5
0,do
i:10
.114
5/14
112
04.1
4112
13.
[28]
Sim
onP
eyto
nJo
nes,
edito
r(2
003)
:H
aske
ll98
Lang
uage
and
L ib
rarie
s:Th
eR
evis
edR
epor
t.C
ambr
idge
Uni
vers
ityPr
ess.
[29]
Fili
pS
iecz
kow
ski,
Mal
gorz
ata
Bie
rnac
ka&
Dar
iusz
Bie
rnac
ki(2
010)
:A
uto
mat
ing
D e
rivat
ions
of
Abs
tract
Mac
hine
sf ro
mR
edu
ctio
nS
eman
tics:
AG
ener
icFo
rmal
izat
ion
of R
efoc
usin
gin
Coq
.In
:22
ndS
ympo
sium
o n
I mpl
emen
tatio
nan
dA
ppl
icat
ion
ofF
unc
tiona
lL
angu
ages
,pp
.72
–88.
[30]
Will
iam
W .
Tait
(196
7):
I nte
nsio
nal
inte
rpre
tatio
nsof
func
tiona
lsof
fini
tety
peI .
Jour
nal
o fS
ym
-bo
licL
ogic
32(2
),pp
.19
8–21
2,do
i:10.
2307
/227
1658
.
[31]
Phi
lipW
adl
er(1
987)
:V
iew
s:A
way
for
patte
rnm
atch
ing
toco
habi
tw
ithda
taab
stra
ctio
n.In
:P
roce
edin
gsof
t he
1 4t
hA
CM
S IG
AC
T-S
IGP
LAN
Sym
posi
umo
nP
rinci
ples
ofP
rogr
amm
ing
L an
-
guag
es,
pp.
307–
313,
doi:
10.1
145/
4162
5.41
653
.
W.S
wie
rstra
AA
nA
gd
aP
relu
de
mod
ule
Prel
ude
whe
re
id:
fora
ll{a
:Se
t}→
a→
aid
x=
x
data
Em
pty
:Se
tw
here
mmaag
giicc( :
)f orall
{a: S e
t}→Em
pty→ a
reco
rdU
nit
:Se
tw
here
unit
:U
nit
unit
=re
cord
{}
data
Pai
r(a
b:
Set
):
Set
whe
re,
:a
→b
→Pa
ira
b
fst
:fo
rall
{ab}
→P
air
ab
→a
fst
(x,,
)=
x
snd
:fo
rall
{ab}
→P
air
ab
→b
snd(,y
)=
y
data
List
(a:
Set
):
Set
whe
reN
il:
List
aC
ons
:a
→Li
sta
→Li
sta
data
_≡
_{a
:Se
t}(x
:a)
:a
→Se
tw
here
Ref
l:x
≡x
infix
6_
≡_
sym
:{a
:Se
t}{x
y:
a}
→x
≡y
→y
≡x
sym
Ref
l=
Ref
l
cong
:{a
b:
Set}
{xy
:a}
→(f
:a
→b)
→x
≡
cong
fR
efl
=R
efl
data
Exis
ts(a
:S
et)
(b:
a→
Set
):
Set
whe
reW
itnes
s:
(x:
a)→
bx
→Ex
ists
ab
fsts
:fo
rall
{ab}
→Ex
ists
ab
→a
fsts
(Witn
ess
xx)
=x
snds
:fo
rall
{a
b}
→(x
:Ex
ists
ab)
→(b
(fsts
x))
snds
(Witn
ess
y)=
y
17
7