these two terms behave dierently, be
ause,
ontrary to if 2*3 = 0 then 1 else (5+6) * (pow (2*31) (5+6))
the usual assumption about CBV variables, x is repla
ed
by a nonvalue. To retain the established reasoning prin
iples of CBV languages, we restri
t all uses of ma
ros in whi
h itself expands into :
Ma
roML to be synta
ti
ally nonvalues (e.g., appli
ations,
letexpressions, et
). if 2*3 = 0 then 1
else (5+6) * (if (2*31) = 0 then 1
2.2 Functional Inlining else (5+6) * (pow ((2*31)1) (5+6)))
Ma
ros that take arguments are just as useful if not more
useful than simple inlining. Their form (as appli
ations) also
provides the added advantage that they do not interfere with and the expansion goes on indenitely. Ma
ro expansion
the established semanti
s of CBV languages. Consider:
an only terminate if the ifexpression is evaluated dur
ing expansion, and not re
onstru
ted as part of the result.
To require the exe
ution of the if expression during ma
ro
ma
$ e = fn x => e expansion, we must expli
itly indi
ate that n is an early pa
ma
? e = e () rameter, rather than a regular ma
ro parameter (whi
h we
all late ), and annotate the term to distinguish among early Semanti
s: A key insight behind this aspe
t of our proposal
and late
omputations. The intended pow ma
ro
an now is to allow the user to only dene new binding
onstru
ts
be written as: that follow the patterns of existing binding
onstru
ts, su
h
as lambda abstra
tions, value de
larations, and re
ursive
de
larations. In these binding
onstru
ts every o
urren
e of
let ma
pow ~n x = a variable
an be immediately identied as either a binding
~(if n = 0 then <1> else <x * ~(pow (n1) x)>) o
urren
e or a bound o
urren
e. The semanti
s of our
in pow ~(2*3) (5+6) end proposal is designed to re
e
t this distin
tion. But even
when this distin
tion is taken into a
ount, equivalen
e
The two
onstru
ts es
ape ~e and bra
kets hei are borrowed is still subtle. For example, the denition above
annot be
dire
tly from MetaML, and work as follows: the rst
on written into:
stru
t \es
apes" from the \ma
ro expansion mode" to reg
ular ML evaluation to perform a
omputation; the se
ond ma
(let seq x = e1 in e2 end) =
onstru
t interrupts regular evaluation to return a result to $(let val y = ?e1 in ?e2 end)
the \ma
ro expansion mode." The expansion of the ma
ro
all above now yields:
with the justi
ation that \y is a binding o
urren
e, and
(5+6) * (5+6) * (5+6) * (5+6) * (5+6) * (5+6) * 1
it does not o
ur in the body ?e2." The problem here is
that there are in fa
t two dierent binding o
urren
es of
the variable x, and ea
h one of them is of a dierent nature.
Semanti
s: The need for introdu
ing the bra
kets and es In essen
e, the rst one (in the parameter of the ma
ro)
ape
onstru
ts is dire
tly related to the need to have a says that \there is a variable, let's
all it x, whi
h
an o
ur
wellspe
ied order for evaluating various subexpressions. free in the expression bound to e2". Be
ause of this, the
In parti
ular, with re
ursion, it be
omes
lear that there use of the variable name x in the se
ond de
laration now
are two dierent kinds of
omputations: early ones and late has spe
ial meaning. In essen
e, the se
ond de
laration now
ones. The need to intermix these two kinds of
omputation says \use x lo
ally as a normally variable name, but make
is what requires a more substantial type system than usual. sure that it is treated in the output of the ma
ro as the
In parti
ular, we must enfor
e what is
alled
ongruen
e in binding o
urren
e for the x in e2."
the partial evaluation literature: a wellformed multistage
omputation should not
ontain an early
omputation that The type system for Ma
roML addresses this issue through
depends on the result of a late
omputation [18℄. A simple two me
hanisms: rst, spe
ial type environments are used
example of the kind of program that the type system has to to keep tra
k of the de
larations of ma
ro parameters, and
reje
t is: most importantly, the bindee parameters like e2 above. Se

ond the type of these bindee parameters will expli
itly
arry
around the name of the binder parameter x, whi
h
omes
let ma
f b n = ~(if b=0 then <n> else <n+1>) from the rst de
laration of x. The se
ond de
laration of
in fn a => fn m => f a m end x itself is in fa
t a
ompletely normal de
laration. With
this typing information, it is possibly to reje
t the lo
al re
The ma
ro argument a is needed during ma
ro expansion pla
ement of x into y as above. It is important to note the
but, being a runtime variable, it is unbound at that time. dieren
e between this me
hanism and the
lassi
\a
iden
tal dynami
s
oping:" the dependen
y on a \free" variable
2.4 Defining New Binding Constructs is re
e
ted expli
itly in the type. Essentially the same prin
i
We now
ome to one of the most novel features of Ma
roML: ple underlies the re
ent proposal for impli
it parameters [20℄.
the ability to dene new binding
onstru
ts in the typed We know that the type system provides an adequate solution
setting. Let us say that we are using the ma
ros $ and ? for to the problem of
onversion in the sour
e program be
suspensions to implement a notion of a
omputation [23℄.
ause the type system guarantees that welltyped Ma
roML
We dene a suitable monadi
let for this setting as follows: programs
an be translated to MetaML programs, and in
the latter, renaming is
ompletely standard, even in the
untyped setting.
ma
(let seq x = e1 in e2 end) =
$(let val x = ?e1 in ?e2 end) At this point, the reader may wonder \how are variables
passed around in Ma
roML?" We return shortly to this ques
tion in Se
tion 4.
The denition introdu
es a new binding
onstru
t let seq
whi
h expands to the
ore binding
onstru
t let val. For
example, 3. MULTISTAGE LANGUAGES
Ma
ro systems introdu
e a stage of
omputation that should
happen before the traditional stage of program exe
ution.
let seq y = f $7 in g y end Early
omputations during this new stage in
lude ma
ro
expansion as well as various other traditional
omputations
expands to: (e.g.,
onditionals, appli
ations, et
).
As mentioned in the introdu
tion, multilevel languages have
$(let val x = ?(f $7) in ?(g x) end) been developed to model su
h kinds of staged
omputation.
x : tn 2 ; x : tn1 `n e : t2 `n e1 : t2 ! t `n e2 : t2
`n x : t `n x:e : t1 ! t2 `n e1 e2 : t
; f : (t1 ! t2 ! t3 ! t)n ; x1 : tn1 ; x2 : tn2 ; x3 : tn3 `n e1 : t
; f : (t1 ! t2 ! t3 ! t)n `n e2 : t4 `n+1 e : t `n e : hti + `n e : hti
`n letre
f x1 x2 x3 = e1 in e2 : t4 `n hei : hti `n+1 ~e : t `n run e:t
Multilevel languages oer
onstru
ts for building and
om the same variable name. We write x : tn 2 when x : tn is
bining
ode, often in a typed setting. Multistage languages a subterm of a valid .
are multilevel languages that provide the user with a means
of exe
uting the generated
ode. The notion of \
ode" de The rules of the type system are presented in Figure 1. The
s
ribed here is an abstra
t one. For example, it has been rst four rules of the type system are standard, ex
ept that
proven that betaredu
tions are sound inside this notion of the level n of ea
h term is passed around everywhere. Note
ode [39℄. This means that
ode in su
h systems is never in that in the rule for lambda (and re
ursive fun
tions), we
spe
ted (nor observed) synta
ti
ally. An alternative, equally take the
urrent level and use it as the level of the bound
valid way of thinking about these languages, therefore, is variable when we add it to the environment.
that they provide ne
ontrol over the evaluation order [41,
40, 37℄. The rule for bra
kets says hei
an have type hti when e has
type t. In addition, e must be typed at level n + 1, where n
A premier example of a stati
allytyped, fun
tional, multi is the level for hei. The level parameter is therefore
ounting
stage language is MetaML. In addition to the normal
on the number of \surrounding" bra
kets. The rule for es
ape
stru
ts of a fun
tional language, MetaML also has three does basi
ally the
onverse. Note, therefore, that es
apes
onstru
ts for building, eÆ
iently
ombining, and exe
ut
an only o
ur at level 1 and higher. Es
apes are supposed
ing
ode. These three
onstru
ts are bra
kets hei,es
ape ~e, to \undo" the ee
t of bra
kets.
and run run e. For the purposes of our study here, we use
the following small MetaML language as our multistage lan Finally, the rule for run e is rather subtle: we
an run a term
guage: of type hti to get a value of type t. However, we must be
areful to note that the term being run must be typed under
e 2 EMeta ::= x j x:e j e e the environment + , rather than simply . We dene +
j letre
f x1 x2 x3 = e1 in e2 as having the same variables and
orresponding types as ,
j hei j ~e j run e but with ea
h level in
remented by 1. Without this (rather
We have restri
ted the number of arguments of re
ursive subtle) adjustment the type system is unsafe [40, 26, 38℄.
fun
tions to three, only for simpli
ity.
Figure 2 denes the bigstep semanti
s for MetaML. There
We present a type system for this language with the follow are a number of reasons why the bigstep semanti
s for
ing types: MetaML [26, 40℄ is an instru
tive model for the formal study
of multistage
omputation: rst, by making \evaluation un
t 2 TMeta ::= nat j t ! t j hti der lambda" expli
it, this semanti
s makes it easy to illus
Here nat is the type for natural numbers, used as an example trate how a multistage
omputation often violates one of the
of various base types. In this paper, we have omitted giving basi
assumptions of many works on programming language
onstru
ts that work on this type be
ause their treatment semanti
s, namely, the restri
tion to
losed terms. Se
ond,
is trivial, and spa
e is limited. Fun
tion types as usual have by using just the standard notion of substitution [2℄, this se
the form t ! t. The MetaML
ode type is denoted by hti. manti
s
aptures the essen
e of stati
s
oping, and there is
In this se
tion we present a sound type system for MetaML. no need for using additional ma
hinery to perform renaming
The soundness of this type system is studied and estab at runtime.
lished elsewhere [40, 26, 38℄. While this type system is not The bigstep semanti
s for MetaML is a family of partial
the most expressive type system available for MetaML (see n
fun
tions ,! : EMeta ! EMeta from expressions to \an
for example [26, 38, 5℄), it is simple and suÆ
ient for our
purposes here. swers," indexed by a natural number n (the level). Taking
n to be 0, we
an see that the rst two rules
orrespond to
The judgments of the type system have the form `n e : the rules of a CBV lambda
al
ulus. The rule for run at
t, where n is a natural number
alled the level of a term. level 0 says that an expression is run by rst evaluating it
The role of the notion of level is explained below where we to get an expression in bra
kets, and then evaluating that
onsider the type rules and the semanti
s for bra
kets and expression. The rule for bra
kets at level 0 says that they
es
ape. The
ontext is a map from identiers to types and are evaluated by rebuilding the expression they surround at
levels, and is represented by the following term language: level 1. Rebuilding, or \evaluating at levels higher than 0,"
is intended to eliminate level 1 es
apes. Rebuilding is per
::= [℄ j ; x : tn formed by traversing the expression while
orre
tly keeping
tra
k of levels. Thus rebuilding simply traverses a term until
In any valid , there should be no repeating o
urren
es of
0
e1 ,! x:e
0
e2 ,! e3 F x1 :x2 :x3 :e1 [f := letre
f x1 x2 x3 = e1 in f℄
0 0
e[x := e3 ℄ ,! e4 e2 [f := F ℄ ,! e3
0 0 0
x:e ,! x:e e1 e2 ,! e4 letre
f x1 x2 x3 = e1 in e2 ,! e
0 0
e1 ,! he2 i e2 ,! e3
n+1
e1 ,! e2
n+1
e1 ,! e3 e2 ,! e4
n+1
0
run e1 ,! e3
n+1
x ,! x
n+1
x:e1 ,! x:e2
n+1
e1 e2 ,! e3 e4
n+1 n+1
e1 ,! e3 e2 ,! e4
n+1
letre
f x1 x2 x3 = e1 in e2 ,! letre
f x1 x2 x3 = e3 in e4
n+1
e1 ,! e2
n+1
e1 ,! e2
n+1
e1 ,! e2
0
e1 ,! he2 i
n
he1 i ,! he2 i
n+1 n+2 1
run e1 ,! run e2 ~e1 ,! ~e2 ~e1 ,! e2
a level 1 es
ape is en
ountered, at whi
h point normal (level We are now at a point where we
an pre
isely dene and
0) evaluation fun
tion is invoked. The es
aped expression interpret our ma
ro language. We present the syntax, type
must yield a bra
keted expression, and then the expression system, and semanti
s of the
ore of Ma
roML (
alled Core
itself is returned. Ma
roML). The language has the usual expressions for a
CBV language, augmented with the previouslymotivated
Interesting examples of MetaML programs
an be found in let ma

onstru
t for dening ma
ros, and the ~e and hei
the literature [41, 38, 37℄. For the purposes of this paper, used to
ontrol re
ursive inlining:
we fo
us on illustrating how three kinds of
omputation
an
be a
hieved using MetaML: e 2 EMa
ro ::= x j x:e j e e
j letre
f x1 x2 x3 = e1 in e2
Evaluation Under Lambda: Consider the term xy:(z:z ) x j letma
f (~x0 ; x1 ; x2 :x3 ) = e1 in e2
and let us say that we are interested in eliminating the inner j f (e1 ; e2 ; x:e) j hei j ~e
appli
ation. In both CBV and CBN, evaluation only works
on
losed terms, and therefore, never \goes under lambda." For
larity, all ma
ros in Core Ma
roML have exa
tly three
With MetaML it is possible to for
e the inner
omputation parameters representative of the three kinds of possible pa
by rewriting the expression as run hxy:~((z:z ) hxi)i, and rameters in Ma
roML. Restri
ting ourselves to ma
ros with
then evaluating it. The result is the desired term: xy:x. exa
tly three parameters allows us to avoid substantial ad
We use su
h a pattern of run, bra
kets, and es
ape in our ministrative detail in Core Ma
roML. The three kinds of
interpretation of the ma
ro language to ensure that
ompu ma
ro arguments are as follows:
tations are performed in the desired order.
Substitution: Consider the term (x:f x x) (g y ). Can we 1. x0 , as indi
ated by the pre
eding ~, is an early param
for
e the appli
ation to be done rst, produ
ing f (g y ) (g y )? eter, whi
h
an be used during ma
ro expansion,
This operation is not expressible in CBV or CBN evaluation
semanti
s, but is expressible in MetaML by annotating the 2. x1 is a late parameter, whi
h might appear in the out
term as follows: run ((x:hf ~x ~xi) hg y i). put of the expansion, and
3. x2 :x3 denes a binder/bindee pair of parameters. The
Renaming: While it may be obvious to experts, it seems two variables x2 and x3 are bound variables in the
not widely known that simply using the standard notion s
ope of the ma
ro denition but they
an only be used
of substitution in dening the semanti
s of a language like in rather spe
ial ways enfor
ed by the type system.
MetaML is suÆ
ient for providing the
orre
t treatment of The variable x2 must be bound again using a regular
free and bound variables everywhere. In MetaML, there is binding
onstru
t before x3
an be used. The variable
never any a
idental variable
apture, and there is never any x3
an only be used in the s
ope of x2 . A legal use of
need to express any freshness
onditions or to use a gensym su
h a binder/bindee pair is:
like operation. Our semanti
s for Ma
roML is simple, be
ause we build on the fa
t that using the standard notion letma
f ( ; ; x:y ) = a:x:a + x + y in : : :
of substitution in the MetaML semanti
s really means that
everything is taken
are of. The x in the ma
ro de
laration binds the two o
ur
ren
es of x in the ma
ro denition! All three o
ur
ren
es of x
ould be renamed to z without
hanging the
meaning. The semanti
s would, however, be
hanged
4. CORE MACROML if we only
hange the inner term to z:a + z + y ,
x : tm 2 x:t2 x2 : [x1 : t1 ℄t2 2 and x1 : t11 2
; ; ; `m x : t ; ; ; `1 x : t ; ; ; `1 x2 : t2
; ; ; ; x : tm 1 ` m e : t2 ; ; ; `m e1 : t2 ! t ; ; ; `m e2 : t2
m
; ; ; ` x:e : t1 ! t2 ; ; ; `m e1 e2 : t
; ; ; ; f : (t1 ! t2 ! t3 ! t)m ; x1 : tm 1 ; x2 : tm
2 ; x3 : t m
3 `m e1 : t
; ; ; ; f : (t1 ! t2 ! t3 ! t)m `m e2 : t4
; ; ; `m letre
f x1 x2 x3 = e1 in e2 : t4
; f : (t1 ; t2 ; [t3 ℄t4 ) ) t5 ; ; x2 : [x : t3 ℄t4 ; ; x1 : t2 ; ; x0 : t01 `1 e1 : t5
; f : (t1 ; t2 ; [t3 ℄t4 ) ) t5 ; ; ; `1 e2 : t
; ; ; `1 letma
f (~x0 ; x1 ; x:x2 ) = e1 in e2 : t
f : (t1 ; t2 ; [t3 ℄t4 ) ) t5 2
; ; ; `0 e1 : t1
; ; ; `1 e2 : t2
; ; ; x : t3 ; `1 e3 : t4 ; ; ; `1 e : t ; ; ; `0 e : hti
1
; ; ; ` f (e1 ; e2 ; x:e3 ) : t5 0
; ; ; ` hei : hti ; ; ; `1 ~e : t
be
ause we would be returning a result that
ould binder/bindee pair is a pair of a \bound" variable de
have an unbound variable (that was bound to x) in
lared to be of type t3 and an expression of type t4 ,
a subterm (that was bound to y ). The binder/bindee whi
h
ould
ontain a free o
urren
e of the \bound"
parameter illustrates how dening new binding
on variable. This notation is inspired by types in FreshML
stru
ts works in Ma
roML. For Core Ma
roML we [34℄.
have pi
ked the simplest binding
onstru
t in the lan
guage, namely lambda abstra
tion x:e. The other the bindee parameter environment. It
arries bindings
binding
onstru
ts follow naturally. of the form [x : t1 ℄t2 . This environment is needed
for type
he
king the body of a ma
ro that uses a
binder/bindee parameter of the form x:x1 , so that
The appli
ation of a ma
ro f (e1 ; e2 ; x:e) also takes exa
tly we know that we
an only use x1 at type t2 in a
on
three arguments: the rst is an early argument, the se
 text where x is bound (with type t1 ).
ond is a late argument, and the third is a binder/bindee
argument. The binder/bindee argument explains the
ore the late parameter environment. It
arries bindings
of our treatment of new binding
onstru
ts: it must be
lear of the form t. This environment is needed for type
what variables are free in what subexpressions, and both
he
king the body of a ma
ro that uses a late param
must always be passed together. Note that a binder/bindee eter.
argument must have the right form for the binding stru
 the regular environment. Be
ause we are in a twostage
ture (in this
ase, lambda). For example, the appli
ation setting, it
arries bindings of the form tm .
f ((); (); z:z + a) of the ma
ro dened above expands to
a0 :x:a0 + x + (x + a): The rules of the Ma
roML type system are presented in
Figure 3. The rst three rules deal with variable lookup.
4.1 Typing Core MacroML The rst rule is the variable (proje
tion) rule from MetaML.
The types of Ma
roML are the same as MetaML: The next rule is similar, although it re
e
ts the fa
t that late
ma
ro parameters
an only be used at level one. The third
t 2 TMa
ro ::= nat j t ! t j hti rule is similar but it
he
ks that the bindee variable is used in
The type system, however, is more involved. Typing judg a
ontext where its binder variable has already been bound.
ments have the form ; ; ; `m e : t where m is the level
of a term. We restri
t the levels here to 0 (representing early The next three rules are standard for a multilevel language.
omputations) and 1 (representing late
omputations). The All the usual rules of SML would be lifted in the same man
environments have the following roles (and denitions): ner (although some
are is required with ee
ts. See Se

tion 7.)
the ma
ro environment. It keeps tra
k of the vari The next four rules are spe
i
to ma
ros. The rst rule
ous ma
ros that have been de
lared. These bindings is for a ma
ro denition. Be
ause we allow ma
ro deni
are of the form (t1 ; t2 ; [t3 ℄t4 ) ) t5 . In this binding, tions to be re
ursive, the body of the ma
ro de
laration is
the tuple provides information about the three stan
he
ked under the assumption that the ma
ro being dened
dardized parameters. Note that we write [t3 ℄t4 to de is already de
lared. We also add the appropriate assump
s
ribe the binder/bindee argument. Intuitively, the tions about the binder/bindee parameters, the late param
eter, and the early parameter to the appropriate environ is, in fa
t, that variables are never passed around! Dur
ments. The rest of the rule is standard. Ma
ro appli
ation ing ma
roexpansion time, the binder/bindee parameter is
is also analogous to appli
ation, although one should note passed in what
an be
onsidered its HOAS representation.
that e1 and e2 are
he
ked at dierent levels. The rules for The type of a binder/bindee parameter is also translated
bra
kets and es
ape are spe
ial
ases of the same rules in to a fun
tion type. Naturally, this is
onsistent with the
MetaML. external type of this parameter. The type of a late param
eter is simply a \delayed" or
ode version of the Ma
roML
4.2 The Semantics of Core MacroML type of that parameter. No translation is shown for regu
In this se
tion, we present the denition of the semanti
s lar environments, as the translation is simply the identity
of Core Ma
roML via an interpretation into MetaML. For embedding.
any welltyped Core MetaML program [℄; [℄; [℄; [℄ `1 e : t the
interpretation [ [℄; [℄; [℄; [℄ `1 e : t℄ will dene a MetaML term The translation on judgments pro
eeds as follows. Terms
(
all it e00 ). To get the nal result of running the Ma
roML that do not involve ma
ros are translated homomorphi
ally.
program, we simply evaluate the MetaML term run he00 i. To Late parameters are translated by putting an es
ape around
get a more negrained view of the evaluation of e00 , we
an them. The intuition here is that late parameters only o
ur
view it as pro
eeding into two distin
t steps: inside the denition of a ma
ro, and when they o
ur, they
are supposed to be \spli
ed into" the
ontext where there
are used in order to appear in the output of ma
ro expansion
Ma
ro expansion: the Ma
roML program e expands as expe
ted.
to a MetaML program e01 if:
0 The key rule in the translations is the one for the bindee
he00 i ,! e01 parameters: when a bindee parameter x2 is used in the
where e00 is the interpretation of the Ma
roML program body of a ma
ro denition, its translation
orresponds to
into MetaML des
ribed above (and dened below). an appli
ation of x2 to a pie
e of
ode
arrying the
orre
sponding binder parameter x1 ! All of this is es
aped so that
Regular exe
ution: The expansion e01 of e then evalu the appli
ation is performed at ma
ro expansion time. To
ates to the answer e02 if: understand what is going on here, keep in mind the trans
0 lation of the environment , and note that it introdu
es an
run e01 ,! e02 arrow type \out of nowhere" during the translation. Thus,
in the target of the interpretation, x2 has an arrow type.
Note that the only new part in the above semanti
s is the This is be
ause, as we said earlier, the translation produ
es
translation from Ma
roML to MetaML. The two stages of
ode that is passing around a HOAS representation of the
MetaML evaluation are then just standard MetaML rebuild binder/bindee pair.
ing and evaluation, respe
tively. Whenever the original term
does not have any
ode types in its Ma
roML type, the latter Ma
ro de
larations are translated to es
aped fun
tion de

step would
oin
ide with standard ML evaluation. larations, i.e., fun
tion de
larations that are to be exe
uted
during expansion. Note however that the body of the fun

Figure 4 presents the translation, rst dened on environ tion being dened and the
ontext where it is used are both
ments, and then dened on judgments. Although the trans in bra
kets. This is be
ause we want to treat both as
ode,
lation
an be made to map untyped terms to untyped terms, ex
ept in pla
es where the translation has added other es
it is
ontextsensitive, and it is therefore easier to dene it
apes.
on judgments of welltyped Core Ma
roML programs. To
avoid the risk of potentially
onfusing notation, the trans A ma
ro appli
ation is translated into an es
aped appli
a
lation maps judgments to terms (rather than judgments to tion, for similar reasons. Note that the rst (early) argu
judgments), as the full MetaML judgments are easy to re ment to the appli
ation is not bra
keted, but the se
ond
onstru
t. (late) argument is. As hinted earlier by the translation of
the types, the binder/bindee argument is translated into a
Empty environments are mapped to empty environments. fun
tion whose body is itself a pie
e of
ode. Intuitively,
The binding for a ma
ro is translated into a MetaML type making the body a pie
e of
ode delays its evaluation. It
that, in essen
e, re
e
ts the semanti
s of the spe
ial notation is worth noting that the HOAS entities only exist during
we have used; the (level 0) MetaML type: the rst stage (ma
ro expansion), and not during the exe
ution of the program proper. The translation of bra
kets
t1 ! ht2 i ! (ht3 i ! ht4 i) ! ht5 i and es
ape is straightforward.
orresponds to a fun
tion that takes three (
urried) pa We illustrate the translation on the example at the begin
rameters. The rst parameter is a \true" value of type ning of the se
tion, with a bound in to the type nat1 :
t1
orresponding to the early parameter. The se
ond pa
rameter, however, is a \delayed" or
ode value of type t2
orresponding to the late parameter. The third parame
ter (the binder/bindee parameter) is in fa
t translated to [ [℄;[℄; [℄; a : nat1 `1
a fun
tion. It is at this point that we
an start to ex letma
f ( ; ; x:y ) = a:x:a + x + y
plain how the interpretation of the binder/bindee parame in f ((); (); z:z + a)℄℄ =
ters works. Re
all that in the examples se
tion we promised ~(letre
f y = ha:x:a + x + ~(y hxi)i
to explain how \variables are passed around." The answer in h~(f () () (z:h~z + ai))i)
Environments
[ ;℄ = ;
[ ; f : (t1 ; t2 ; [t3 ℄t4 ) ) t5 ℄ = [ ℄℄; f : (t1 ! ht2 i ! (ht3 i ! ht4 i) ! ht5 i)0
[ ; x2 : [x1 : t1 ℄t2 ℄ = [ ℄℄; x2 : (ht1 i ! ht2 i)0
[ ; x : t ℄ = [ ℄℄; x : hti0
Lambda Terms
x : tm 2 [ ; ; ; ; x : tm 1 `m e : t2 ℄ = e0
m
[ ; ; ; ` x : t℄ = x m
[ ; ; ; ` x:e : t1 ! t2 ℄ = x:e0
[ ; ; ; `m e1 : t2 ! t℄ = e01 [ ; ; ; `m e2 : t2 ℄ = e02
[ ; ; ; `m e1 e2 : t℄ = e01 e02
[ ; ; ; ; f : (t1 ! t2 ! t3 ! t)m ; x1 : tm1 ; x2 : t m
2 ; x3 : tm
3 `m e1 : t℄ = e01
[ ; ; ; ; f : (t1 ! t2 ! t3 ! t)m `m e2 : t4 ℄ = e02
[ ; ; ; `m letre
f x1 x2 x3 = e1 in e2 : t4 ℄ = letre
f x1 x2 x3 = e01 in e02
Ma
ros
x:t2 x2 : [x1 : t1 ℄t2 2 and x1 : t11 2
[ ; ; ; `1 x : t℄ = ~x [ ; ; ; `1 x2 : t2 ℄ = ~(x2 hx1 i)
[ ; f : (t1 ; t2 ; [t3 ℄t4 ) ) t5 ; ; x2 : [x : t3 ℄t4 ; ; x1 : t2 ; ; x0 : t01 `1 e1 : t5 ℄ = e01
[ ; f : (t1 ; t2 ; [t3 ℄t4 ) ) t5 ; ; ; `1 e2 : t℄ = e02
[ ; ; ; `1 letma
f (~x0 ; x1 ; x:x2 ) = e1 in e2 : t℄ = ~(letre
f x0 x1 x2 = he01 i in he02 i)
[ ; ; ; `0 e1 : t1 ℄ = e01
[ ; ; ; `1 e2 : t2 ℄ = e02
[ ; ; ; x : t3 ; `1 e3 : t4 ℄ = e03
f : (t1 ; t2 ; [t3 ℄t4 ) ) t5 2
[ ; ; ; `1 f (e1 ; e2 ; x:e3 ) : t5 ℄ = ~(f e01 he02 i x:he03 i)
Code Obje
ts
[ ; ; ; `1 e : t℄ = e0 [ ; ; ; `0 e : hti℄ = e0
[ ; ; ; `0 hei : hti℄ = he0 i [ ; ; ; `1 ~e : t℄ = ~e0
As we said at the start of this se
tion, the output of the 4.3 Type Safety
translation is wrapped in the
ontext run h: : :i and then As mentioned in the introdu
tion, dening the semanti
s of
evaluated in MetaML to produ
e the result of ma
ro ex Core Ma
roML by interpretation into MetaML makes prov
pansion. Be
ause bigstep semanti
s derivations are hard to ing type safety fairly dire
t. In what follows, we state and
draw (and maybe to read), we
an \animate" the
ompu outline the proof of this result.
tation using the following sequen
e of sound MetaML left
redu
tions [38℄.
Theorem 4.1 (Type Safety). If [℄; [℄; [℄; [℄ `m e : t is
a valid Ma
roML judgment, then translating it to MetaML
run h~(letre
f y = ha:x:a + x + ~(y hxi)i yields a welltyped MetaML program, and exe
uting that pro
in h~(f () () (z:h~z + ai))i)i gram does not generate any MetaML runtime errors.
! (
all f , rename a when doing substitution)
run h~h~ha0 :x:a0 + x + ~((z:h~z + ai) hxi)iii
! (
an
el es
ape with bra
kets) Proof. The rst part is by Lemma 4.2, and the se
ond
run ha0 :x:a0 + x + ~((z:h~z + ai) hxi)i part follows from the type safety property of the MetaML
! (
all inner ) type system presented in previous work [40, 26℄.
run ha0 :x:a0 + x + ~h~hxi + aii
! (
an
el es
ape with bra
kets)
run ha0 :x:a0 + x + (x + a)i In the statement of the theorem, MetaML runtime errors in
! (run)
lude both errors that might o
ur at ma
ro expansion and
a0 :x:a0 + x + (x + a) runtime errors (dened pre
isely in [40, 26, 38℄). The ne

essary auxiliary lemma states that our translation preserves
typing.
whi
h is the expe
ted result of ma
ro expansion.
Lemma 4.2 (Type Preservation). If ; ; ; `m Re
ursive Bindings: A simple but still important issue is
e : t is a valid Ma
roML judgment, then [ ℄℄; [ ℄℄; [ ℄℄; `m how re
ursive binding
onstru
ts should be treated. In par
[ ; ; ; `m e : t℄ : t is a valid MetaML judgment. ti
ular, when a ma
ro is dening a new binding
onstru
t
in terms of an existing re
ursive binding
onstru
t, this in
Proof. Routine indu
tion on the height of the derivation formation should be maintained in the type of the ma
ro.
of ; ; ; `m e : t.
Consider the following de
laration:
allowing the user to dene new binding
onstru
ts. In try Intuitionisti
Modal Logi
s and Appli
ations (IMLA)
(1999).
ing to a
hieve this, we have used ideas from both HOAS
(to implement our proposal in a multistage setting) [33℄ [5℄ Cal
agno, C., Moggi, E., and Taha, W. Closed types as
and FreshML (to provide the surfa
e syntax and ideas in a simple approa
h to safe imperative multistage
programming. In 27th International Colloquium on
the sour
e language) [34℄. It may well be that our language Automata, Languages, and Programming (ICALP)
provides some new insights on the link between the two ap (Geneva, 2000), vol. 1853 of Le
ture Notes in Computer
proa
hes to treating binding
onstru
ts. S
ien
e, ACM Press, pp. 25{36.
egant way of avoiding binding issues, dening new binding Computer So
iety Press, pp. 184{195.
onstru
ts, and provides a sound basis for developing type [8℄ Davies, R., and Pfenning, F. A modal analysis of staged
systems for ma
ro languages.
omputation. In 23rd Annual ACM Symposium on
Prin
iples of Programming Languages (POPL) (St.
In this paper, we have not
onsidered type safety in the pres Petersburg Bea
h, 1996), pp. 258{270.
en
e of imperative features (referen
es, ex
eptions) during [9℄ Dybvig, R. K., Hieb, R., and Bruggeman, C. Synta
ti
expansion time. In this setting, we expe
t the work on im abstra
tion in S
heme. Lisp and Symboli
Computation 5,
4 (De
. 1992), 295{326.
perative multilevel languages to be of dire
t relevan
e [42,
5℄. We have also not
onsidered a multilevel ma
ro sys [10℄ Erko k, L., and Laun
hbury, J. Re
ursive monadi
tem primarily for the reason of simpli
ity. We would like bindings. In Pro
eedings of the Fifth ACM SIGPLAN
to
onsider su
h an extension in future work. But there International Conferen
e on Fun
tional Programming,
ICFP'00 (September 2000), ACM Press, pp. 174{185.
are restri
tions on the system that may be a bit more
hal
lenging to alleviate. For example, we have not
onsidered [11℄ Felleisen, M. On the expressive power of programming
languages. In S
ien
e of Computer Programming (1991),
higherorder ma
ros (ma
ros that take other ma
ros as pa vol. 17, pp. 35{75. Preliminary version in: Pro
. European
rameters) and we have not
onsidered ma
ros that generate Symposium on Programming, Le
ture Notes in Computer
other ma
ros. For su
h expressiveness, however, we expe
t S
ien
e, 432. SpringerVerlag (1990), 134{151.
[12℄ Fiore, M., Plotkin, G., and Turi, D. Abstra
t syntax Symposium on Prin
iples of Programming Languages, San
and variable binding. In Pro
eedings of the 14th Annual Antonio, Texas (New York, NY, Jan. 1999), ACM,
Symposium on Logi
in Computer S
ien
e (LICS'99) pp. 43{56.
(Trento, Italy, July 1999), G. Longo, Ed., IEEE Computer [28℄ Nielson, F., and Nielson, H. R. Twolevel semanti
s and
So
iety Press, pp. 193{202.
ode generation. Theoreti
al Computer S
ien
e 56, 1
[13℄ Glu
k, R., and Jrgensen, J. EÆ
ient multilevel (1988), 59{133.
generating extensions for program spe
ialization. In [29℄ Okasaki, C. Purely Fun
tional Data Stru
tures. PhD
Programming Languages: Implementations, Logi
s and
thesis, S
hool of Computer S
ien
e, Carnegie Mellon
Programs (PLILP'95) (1995), S. D. Swierstra and University, 1996.
M. Hermenegildo, Eds., vol. 982 of Le
ture Notes in
Computer S
ien
e, SpringerVerlag, pp. 259{278. [30℄ Oliva, D. P., Ramsdell, J. D., and Wand, M. The
k, R., and Jrgensen, J. Fast bindingtime analysis
VLISP veried pres
heme
ompiler. Lisp and Symboli
[14℄ Glu Computation 8, 1/2 (1995), 111{182.
for multilevel spe
ialization. In Perspe
tives of System
Informati
s (1996), D. Bjrner, M. Broy, and I. V. [31℄ Oregon Graduate Institute Te
hni
al Reports. P.O. Box
Pottosin, Eds., vol. 1181 of Le
ture Notes in Computer 91000, Portland, OR 972911000,USA. Available online
S
ien
e, SpringerVerlag, pp. 261{272. from ftp://
se.ogi.edu/pub/te
hreports/README.html.
k, R., and Jrgensen, J. An automati
program
Last viewed August 1999.
[15℄ Glu
generator for multilevel spe
ialization. LISP and Symboli
[32℄ Pasali
, E., Sheard, T., and Taha, W. DALI: An
Computation 10, 2 (1997), 113{158. untyped, CBV fun
tional language supporting rstorder
datatypes with binders (te
hni
al development). Te
h. Rep.
[16℄ Gomard, C. K., and Jones, N. D. A partial evaluator for CSE00007, OGI, 2000. Available from [31℄.
untyped lambda
al
ulus. Journal of Fun
tional
Programming 1, 1 (1991), 21{69. [33℄ Pfenning, F., and Elliott, C. Higherorder abstra
t
syntax. In Pro
eedings of the Symposium on Language
[17℄ Hofmann, M. Semanti
al analysis of higherorder abstra
t Design and Implementation (Atlanta, 1988), pp. 199{208.
syntax. In Pro
eedings of the 14th Annual Symposium on
Logi
in Computer S
ien
e (LICS'99) (Trento, Italy, July [34℄ Pitts, A. M., and Gabbay, M. J. A metalanguage for
1999), G. Longo, Ed., IEEE Computer So
iety Press. programming with bound names modulo renaming. In
Mathemati
s of Programme Constru
tion (2000), vol. 1837
[18℄ Jones, N. D., Gomard, C. K., and Sestoft, P. Partial of Le
ture Notes in Computer S
ien
e, SpringerVerlag,
Evaluation and Automati
Program Generation.
pp. 230{255.
Prenti
eHall, 1993.
[35℄ Plotkin, G. D. Callbyname,
allbyvalue and the
[19℄ Kohlbe
ker, E., Friedman, D. P., Felleisen, M., and lambda
al
ulus. Theoreti
al Computer S
ien
e 1 (1975),
Duba, B. Hygieni
ma
ro expansion. In Pro
eedings of the 125{159.
ACM Conferen
e on LISP and Fun
tional Programming
(Cambridge, MA, Aug. 1986), R. P. Gabriel, Ed., ACM [36℄ Sabry, A. What is a purely fun
tional language? Journal
Press, pp. 151{181. of Fun
tional Programming 8, 1 (Jan. 1998), 1{22.
[20℄ Lewis, J. R., Laun
hbury, J., Meijer, E., and Shields, [37℄ Sheard, T. Using MetaML: A staged programming
M. Impli
it parameters: Dynami
s
oping with stati
types. language. Le
ture Notes in Computer S
ien
e 1608 (1999),
In Pro
eedings of the 27th ACM SIGPLANSIGACT 207{??
Symposium on Prin
iples of Programming Languages [38℄ Taha, W. MultiStage Programming: Its Theory and
(POLP00) (N.Y., Jan. 19{21 2000), ACM Press, Appli
ations. PhD thesis, Oregon Graduate Institute of
pp. 108{118. S
ien
e and Te
hnology, 1999. Available from [31℄.
[21℄ Miller, D. An extension to ML to handle bound variables [39℄ Taha, W. A sound redu
tion semanti
s for untyped CBN
in data stru
tures: Preliminary report. In Informal multistage
omputation. Or, the theory of MetaML is
Pro
eedings of the Logi
al Frameworks BRA Workshop nontrivial. In Pro
eedings of the Workshop on Partial
(June 1990). Available as UPenn CIS te
hni
al report Evaluation and Semanti
sBased Program Maniplation
MSCIS9059. (PEPM) (Boston, 2000), ACM Press.
[22℄ Mit
hell, J. C. On abstra
tion and the expressive power [40℄ Taha, W., Benaissa, Z.E.A., and Sheard, T.
of programming languages. In Theoreti
al Aspe
ts of Multistage programming: Axiomatization and typesafety.
Computer Software (1991), T. Ito and A. R. Meyer, Eds., In 25th International Colloquium on Automata, Languages,
vol. 526 of Le
ture Notes in Computer S
ien
e, and Programming (ICALP) (Aalborg, 1998), vol. 1443 of
SpringerVerlag, pp. 290{310. Le
ture Notes in Computer S
ien
e, pp. 918{929.
[23℄ Moggi, E. Notions of
omputation and monads. [41℄ Taha, W., and Sheard, T. Multistage programming with
Information and Computation 93, 1 (1991). expli
it annotations. In Pro
eedings of the Symposium on
[24℄ Moggi, E. A
ategori
al a
ount of twolevel languages. In Partial Evaluation and Semanti
Based Program
Mathemati
s Foundations of Program Semanti
s (1997), Manipulation (PEPM) (Amsterdam, 1997), ACM Press,
Elsevier S
ien
e. pp. 203{217.
[25℄ Moggi, E. Fun
tor
ategories and twolevel languages. In [42℄ Thiemann, P., and Dussart, D. Partial evaluation for
Foundations of Software S
ien
e and Computation
higherorder languages with state. Available online from
Stru
tures (FoSSaCS) (1998), vol. 1378 of Le
ture Notes in http://www.informatik.unifreiburg.de/~thiemann/papers/
[26℄ Moggi, E., Taha, W., Benaissa, Z. E.A., and Sheard, [43℄ Wand, M. Embedding type stru
ture in semanti
s. In
T. An idealized MetaML: Simpler, and more expressive. In Conferen
e Re
ord of the Prin
iples of Programming
Much more than documents.
Discover everything Scribd has to offer, including books and audiobooks from major publishers.
Cancel anytime.