POLYA: TRUE TYPE POLYMORPHISM
FOR MOBILE AMBIENTS∗
Torben Amtoft†
Kansas State University

Henning Makholm
Heriot-Watt University

J. B. Wells
Heriot-Watt University

Abstract1

Previous type systems for mobility calculi (the original Mobile Ambients, itsvariants and descendants, e.g., Boxed Ambients and Safe Ambients, and otherrelated systems) offer little support for generic mobile agents. Previous systems
either do not handle communication at all or globally assign fixed communication types to ambient names that do not change as an ambient moves around orinteracts with other ambients. This makes it hard to type examples such as amessenger ambient that uses communication primitives to collect a message of
non-predetermined type and deliver it to a non-predetermined destination.
In contrast, we present our new type system PolyA. Instead of assigning communication types to ambient names, PolyA assigns a type to each process P thatgives upper bounds on (1) the possible ambient nesting shapes of any process P 0
to which P can evolve, (2) the values that may be communicated at each location,and (3) the capabilities that can be used at each location. Because PolyA can typegeneric mobile agents, we believe PolyA is the first type system for a mobility
calculus that provides type polymorphism comparable in power to polymorphic
type systems for the λ-calculus. PolyA is easily extended to ambient calculus
variants. A restriction of PolyA has principal typings.

Introduction

Whereas the π-calculus [16] is probably the most widely known calculus for communicating processes, the ambient calculus [7] has recently become important, because it adds reasoning about locations and mobility. In the ambient calculus, processes are located in ambients, locations which can be nested, forming a tree. Ambients can move, making the tree dynamic. Furthermore, only processes that are “close”
to each other can exchange values.

1.1

The problem with ambient calculus type systems

Consider this process:m[in s.0 | open t.(p, v).p.hvi.0] | s[t[in m.hin r, di.0] | r[open m.(v).out v.0]]
The example ambient named m is perhaps the simplest kind of generic mobile agent,
namely a messenger. That is, m first goes somewhere looking for messages to deliver,∗ Partially supported by EC FP5 grant IST-2001-33477, EPSRC grant GR/R41545/01, NSF grants 9806745(EIA), 9988529 (CCR), and 0113193 (ITR), and Sun Microsystems equipment grant EDUD-7826-990410US.
† Much of the work was done while Amtoft was at Heriot-Watt University paid by EC FP5 grant IST-200133477.

then m collects a destination and a payload, and then m goes to that destination anddelivers that payload.
Nearly all type systems for ambient calculi follow the example of the seminal system of Cardelli and Gordon [8] and assign to each ambient name a a description of
the communication that can happen within ambients named a. Unfortunately, typesystems based on this principle are inflexible about generic functionality. Consider
the example process extended to have two possible execution paths, in that m can enter
either of two senders:||m[in s.0 | open t.(p, v).p.hvi.0]
s[t[in m.hin r, di.0] | r[open m.(v).out v.0]] (v must be a name)s[t[in m.hin q, out di.0] | q[open m.(v).v.0]] (v must be a capability)

Here, the messenger m must be able to deliver two different types of payloads, both anambient name and a capability. None of the previous type systems for ambient calculi
allow this. In general, the previous type systems do not support the possibility that amobile agent may carry non-predetermined types of data from location to location anddeliver this data using communication primitives.
In previous type systems for ambient calculi, generic mobile agents can be encodedby using extra ambient wrappers, one for each type of data to be delivered. However,this encoding is awkward and also loses the ability to predict whether the correct typeof data is being delivered to each location, avoiding stuck states.
In solving this problem, a key observation is that the possible communication
within m depends on which of the s’s the ambient m is found inside.

1.2

Our solution – overview

To overcome the weaknesses of previous type systems for generic functionality, we
present a new type system, PolyA. Types indicate the possible positions of capabilities,
inputs, and outputs, and also represent upper bounds on the possible ambient nesting
tree into which a process can evolve. Thus they look much like processes, as is also
the case, e.g., for the types of [10].
Our type system’s basic concept is the shape predicate. The actual definition issomewhat involved, partly due to the need of handling communication, so let us introduce the concept gently with a toy system where the only capability is “in”:¯¯¯
Toy shape predicates: σ ::= 0 ¯ (σ | σ) ¯ a[σ] ¯ in a
A shape predicate’s meaning is a set of terms, given by this matching relation:`P : σ` a[P] : (· · · | a[σ] | · · ·)`P : σ`Q : σ`P|Q : σ` in a.0 : (· · · | in a | · · ·)`0 : σWith these rules we can derive the judgement ` P0 : σ0 , whereP0
σ0= a[in b.0 | in c.0] | b[d[in a.0]] | c[e[in a.0]]= a[in b | in c] | b[d[in a]] | c[e[in a]]

But we can also derive, say,` a[in b.0] | a[in c.0] : σ0
— the matching rules do not care that the b and c on the top level are missing, nor thatthe a[in b | in c] part of the shape predicate is used twice.
PolyA types are shape predicates such that the set of terms matching a type is closed
under reduction. The shape predicate σ0 above is not a type, because
P0 ,→ P1 = b[a[in c.0] | d[in a.0]] | c[· · ·]yet 6 ` P1 : σ0 . One type that P0 does have is
σ1 = a[in b | in c] | b[a[in b | in c | d[in a]] | d[in a]]| c[a[in b | in c | e[in a]] | e[in a]]
The a[· · ·] predicate inside b still allows the in b. This must be so because shapepredicates do not care about the number of identical items (unlike what is the case in[21]), so one of the terms matched by σ1 is a[in b.0 | in b.0] | b[0], which reduces
to b[a[in b]].
A more subtle point about σ1 is that it disallows having an e inside an a insidea b, or a d inside an a inside a c. This example therefore illustrates the most basic
kind of polymorphism possible: The same initial a ambient can evolve differently in
different possible futures, and the type system can prove that those different futures
do not interfere with each other.
This is the way polymorphism works in PolyA: An ambient can start out with a verysmall type such as a[in b | in c], and then when it chooses to move into b rather thanc, it may change its type to a supertype, a[in b | in c | · · ·]. Seen from the viewpointof the moving ambient, its type has evolved — though of course the new type has beenpresent from the beginning somewhere in the overall type of the entire computation.
PolyA lets any supertype (i.e., a type that is matched by a larger set of terms) be
used as a polymorphic variant if it appears in the right place of the overall typing. The
overall typing contains all of the polymorphic variants that will ever be needed foreach ambient in the particular context it is being typed in.
Some readers might think that this does not look like type polymorphism, because
the various types for a are not substitution instances of a parameterised type. However, how one technically expresses the relation between the type for some genericcode and the types for its concrete uses is not essential to the concept of genericity or
polymorphism. What is important is that the type system supports reasoning about distinct uses of the same generic code. We achieve what Cardelli and Wegner [9] called“the purest form of polymorphism: the same object or function can be used uniformly
in different type context without changes, coercions or any kind of run-time tests or
special encodings of representations”.
PolyA can optionally track the sequencing of actions, a possibility pioneered by
Amtoft et al. [1, 2]. For example, a[in b.in c.0] | b[c[0]] | c[open a.0] has a PolyA
type proving that a will never be opened.
PolyA can assign the type shown in Figure 1 to the example containing the genericmessenger and two clients. This type proves that the example process has only welldefined behaviour, something which no previous type system for ambients can do.
The type may still appear complex compared to the term it types. This is partly
because we constructed it with the help of a type inference algorithm [15] which strives
to create a very precise (and thus information-rich) type. It is possible to constructvisually smaller but less precise types that also prove well defined behaviour for themessenger example.

1.3

Other related work

Although not type-based, several papers have explored letting the analysis of anambient subprocess depend on its possible contexts — a task which requires an estimate of the possible shapes of the ambient tree structure. None of these handle
communication, however, so none can prove the safety of our example polymorphic
messenger. With shape grammars [18], a set of grammars is returned such that at any
step, the current process can be described by one of these grammars. The analysis isvery precise, but potentially also very expensive. In Kleene analysis [17], a 3-valued
logic is used to estimate the possible shapes. The framework allows for trade-offs
w.r.t. precision versus costs. The abstract interpretation system of [12] keeps track ofletrec Xm8 = in s.0 | open t.(p, v).p.h{v}i.0
in m[Xm8]| s[letrec Xm7 = (Xm8) | (Xt3) | h{d}i.0 | (p, v).p.h{v}i.0| in r.h{d}i.0 | m[Xm7] | r[Xr1] | t[Xt3]
Xr1= (Xm7) | (v).out v.0 | out d.0 | open m.(v).out v.0
Xt3= h<in r>, {d}i.0 | in m.h<in r>, {d}i.0
in m[Xm7] | r[Xr1] | t[in m.h<in r>, {d}i.0] end ]| s[letrec Xm3 = (Xm8) | (Xt1) | h<out d>i.0 | (p, v).p.h{v}i.0| in q.h<out d>i.0 | m[Xm3] | q[Xq1] | t[Xt1]
Xq1= (Xm3) | (v).v.0 | out d.0 | open m.(v).v.0
Xt1= h<in q>, <out d>i.0 | in m.h<in q>, <out d>i.0
in m[Xm3] | q[Xq1] | t[in m.h<in q>, <out d>i.0] end ]end

Figure 1. A type for for the polymorphic messenger example, shown in the “shape expression
syntax from Figure 3, as well as a shape graph as defined in Section 3.1. For readability, not all0πedges are shown. Instead, special edges of the form X →Y signify that for each edge Y →Zπthere must also be an edge X → Z. These zero edges work transitively; thus one of the impliedtedges in the figure is Xq1 open→
Xopen2.
The graph has been produced from a machine-generated source, plus some manual layout hints,
using the VCG graph layout tool [20].

the context “one level up”. This is sufficient to achieve a quite precise analysis, yet is“only” polynomial (n7 ).
Polymorphic type systems already exist for the π-calculus [22, 19], but do not generalise easily to the spatial nature of our messenger example.

1.4

Summary of contributions (conclusion)
We present PolyA, the first type system for the ambient calculus that is flexible
enough to type generic mobile agents.
We explain how PolyA types can be used not just to check basic type safety
but also to give precise answers to various questions about process behaviour ofinterest for other reasons, e.g., security.
We prove subject reduction (Thm. 21(1)) and the decidability of type checking(Prop. 6) for PolyA.
We prove principal typings (Thm. 27) for a useful restriction of PolyA.
We illustrate how to extend PolyA to support the cross-ambient communication of Boxed Ambients [4], the co-capabilities of Safe Ambients [13], and theprocess (not ambient) mobility capability of M3 [11].

The proofs of most propositions and theorems have been omitted here for space reasons. They can be found in an extended online version of this paper [3].
In other work [15] we have developed a type inference algorithm for a useful restriction of PolyA. Space limitations prevent including a further description here.
Acknowledgements The design of PolyA benefited from helpful discussions with
Mario Coppo, Mariangiola Dezani, and Elio Giovannetti.

2

The ambient calculus

For space reasons, we present the system for a calculus without name restriction. In[3] we present a straightforward way to handle name restriction. In later work it may
be possible to combine PolyA with more advanced treatments of name restriction, suchas the “abstract names” of Lhoussaine and Sassone [14].
Fig. 2 defines the syntax and semantics of our base calculus. Whenever it has been
defined that some (meta)variable letter, say “x”, ranges over a given set of objects, thenotation x shall mean that set of objects.
The syntactic category of prefixes is not in traditional ambient calculus formulations. Our calculus treats ambient boundaries as capabilities; “amb a” is the capability
that creates an ambient named a when executed. In our formulation, an ambient with
contents P is written “amb a.P”. The traditional notation “a[P]” is syntactic sugar
for amb a.P; we use this whenever convenient. The capability amb a can in principle
be passed in a message. We allow this more because it is syntactically convenient thanbecause we expect processes to actually do it. Our main results do not fully support
programs that use this possibility.
The special capability “•” is not supposed to be found in the initial term. It signifies a substitution result that would otherwise be syntactically invalid. For example,
the term hin ci | (b).in a.open b.0 reduces to in a.•.0 instead of the (hypothetical)“in a.open (in c).0”. Traditional ambient calculus accounts usually leave such a communication result undefined, implicitly understanding that the system would crasheither at the communication time or when the ill-formed capability executes after thein a capability has fired. PolyA supports both views, according to how one interprets•. In either case, it is technically convenient to reify the failure as a special term.
The symbol • does not have any reduction rules associated with it. As far as ourtheory is concerned it just sits there. Likewise, there are no reduction rules for placeholder capabilities of the form “a”. In applications, one may or may not want to treat
it as an error if such a capability shows up in a place where it wants to be executed. A
PolyA type conservatively approximates whether one of these capabilities may occur,
but the type system user must decide what to do if this happens.

Syntax:
Names:a, b ::= a | b | c | · · ·Opcodes:
O ::= in | out | open | amb
Capabilities: C ::= a | O a | •Messages: M, N ::= C | M.N | ε~ | (~a)Prefixes:
p ::= M | hMi
Processes: P, Q, R ::= p.P | !P | (P | Q) | 0
See main text for further syntactic restrictions (scoping).
Process equivalence:
P|Q ≡ Q|P

P | (Q | R) ≡ (P | Q) | R

0|P ≡ P!P ≡ P | !P

!0 ≡ 0(M.N).P ≡ M.(N.P)ε.P ≡ PP≡PP≡Q
P|R ≡ Q|RP≡Q
p.P ≡ p.Q

P≡Q
!P ≡ !Q

Q≡P
P≡Q

P≡Q
Q≡R
P≡R

Substitution:
A term substitution S is a (total) function from names to messages such that
S (a) 6= a for only finitely many a’s. We often notate it S = [a1 7→ M1 , . . . , ak 7→
Mk ], understanding implicitly that S (a) = a when a is not one of the ai ’s. Shorter
notations are [ai 7→ Mi ]1≤i≤k or [a 7→ Ma ]a∈A .

S (M.N) = (S M).(S N)
Sε=ε
S a = S (a)S•=•n
S (O a) = O S (a) if S (a) is a name

For messages:•

For other prefixes:

otherwise

S hM1 , . . . , Mk i = hS M1 , . . . , S Mk i

S (a1 , . . . , ak ) = (a1 , . . . , ak ) if S (ai ) = ai for all in•otherwise

S (p.P) = (S p).(S P)
S (P | Q) = (S P) | (S Q)

For terms:

S (!P) = !(S P)
S0=0

Reduction rules:
a[in b.P | Q] | b[R] ,→ b[a[P | Q] | R]b[a[out b.P | Q] | R] ,→ a[P | Q] | b[R]a[P] | open a.Q ,→ P | Q

hM1 , . . . , Mn i.P | (a1 , . . . , an ).Q ,→ P | [ai 7→ Mi ]1≤i≤n Q
P ,→ Qa[P] ,→ a[Q]Figure 2.

P ,→ QP | R ,→ Q | R

P ≡ P0P0 ,→ Q0
P ,→ Q

Q0 ≡ Q

Syntax and semantics of the ambient calculus

Convention 1 A term P is well formed iff its free names are distinct from the names
bound by any “(~a)” within the term and it does not contain any nested bindings of thesame name. We consider only well formed terms.
Conv. 1 does not limit expressiveness. Any program (term) in a more conventional
ambient calculus formulation that allows α-conversion has a well formed α-variant
which can be used in our type system.
The convention ensures that our reduction rules will never perform a substitution
where there is a risk of name capture by (~a) bindings. Reductions preserve wellformedness, because it is syntactically impossible for a substitution to inject a (~a)
within the body of another (~a). (This is in contrast to the λ-calculus, where substi-

tutions routinely insert λ-abstractions into other abstractions). Because of this, we
do not need to recognise α-equivalence for (~a).P. This is a significant technical simplification, because for many purposes we can treat (~a) as any other action, without
needing special machinery for α-equivalence of the bound names.
Fig. 2 contains no provisions for avoiding name capture in S (~a) — this is handled
by Convention 1. The • possibility for S (~a) is never supposed to be used; substitutions
leading to it will not arise by our rules.

3

Shape predicates
The following pseudo-grammar defines the (abstract) syntax of our type system:µ ::= {C1 ,C2 , · · · ,Ck }*| <C1 .C2 . · · · .Ck >
| {a}
¯
¯
Prefix types:
π ::= C ¯ (~a) ¯ h~µi
Shape predicates: σ ::= (π1 .σ1 | · · · | πk .σk )| 0(Ci ’s all different, k ≥ 1)
(Ci ’s all different, k ≥ 0)

Message types:(k ≥ 1)

Definition 2 (matching of shape predicates) These rules define the relations ` M : µ, ` p : π, and ` P : σ:M 6∈ aM.0 ≡ C10 . · · · .Cn0 .0{C10 , ...,Cn0 } ⊆ {C1 , ...,Ck }KleeneStar` M : {C1 , . . . ,Ck }*M.0 ≡ C1 . · · · .Ck .0
M 6∈ aSequenced` M : <C1 . · · · .Ck >`C : CCap` (~a) : (~a)

Recv

` a : {a}Name

` M 1 : µ1···
` M k : µk
Send
` hM1 , . . . , Mk i : hµ1 , . . . , µk i`p:π`P : σPfx` p.P : (· · · | π.σ | · · ·)` M.(N.P) : σSeq` (M.N).P : σ`P : σ`Q : σPar`P|Q : σNull`0 : σ`P : σNop` ε.P : σ`P : σBang` !P : σ

The side conditions M 6∈ a and M.0 ≡ C1 . · · · .Ck .0 on rules KleeneStar and Sequenced amount to specifying that these two forms of message types are matched
modulo associativity of “.” and neutrality of “ε” — with the exception that messages
that are raw names (i.e., “a” as opposed to “a.ε” or “in a”) are handled specially. They
are matched only by the message type {a}.
Our language of message types is rather restricted, but it has been carefully constructed to allow the proofs to work — in particular, for principal types to exist, weneed to ensure that each message type has only finitely many subtypes.
Theorem 3 If P ≡ Q then ` P : σ ⇔ ` Q : σ for all σ.
Definition 4 The meaning of a shape predicate (message type, prefix type) is theset of terms (messages, prefixes) that match it:[[µ]] = { M | ` M : µ }[[π]] = { p | ` p : π }[[σ]] = { P | ` P : σ }Definition 5 Define the following containment relations:µ ≤ µ0 ⇐⇒ [[µ]] ⊆ [[µ0 ]]π ≤ π0 ⇐⇒ [[π]] ⊆ [[π0 ]]σ ≤ σ0 ⇐⇒ [[σ]] ⊆ [[σ0 ]]Each of the three containment relations is a preorder (transitive and reflexive). Containment of shape predicates is not antisymmetric, however. For example, the shapepredicates amb a.amb b.0 and amb a.amb b.0 | amb a.0 have the same meaning, but itwould be technically inconvenient (and not give any real benefit) to insist on equating
shape predicates with equal meanings.

3.1

Recursive shape predicates

Our strategy in analysing a term is to look for a shape predicate describing all of its
possible computational futures. Because many terms can create arbitrarily deep nestings of ambients (e.g., !a[!in a.0]), the finite trees we have used for shape predicatesso far are not up to the task1 . We need infinite shape predicates. We should, however,restrict ourselves to infinite shape predicates with finite representations — in otherwords, regular trees.
There are several regular tree representations that we could have used. We believe
it is technically most convenient (and intuitive) to view regular trees as graphs. Therefore, we retroactively replace the abstract syntax for shape predicates with:¯¯¯Node identifiers: X,Y, Z ::= X1 ¯ X2 ¯ X3 ¯ · · ·πYEdges:
e ::= X →Shape graphs:
G ∈ Pfin ( e )Shape predicates:
σ ::= hX | Gi
A shape predicate is now a shape graph together with a pointer to a distinguished root
node. The version of the Pfx rule that works with this notation isπ
Y ∈G` P : hY | Gi
`p:π
X→
Pfx
` p.P : hX | Gi
Thm. 3 is still true with this formulation, because it was proven by induction on term
equivalence rather than shape-predicate structure.
In general, defining some property for shape graphs implicitly defines it for shapepredicates: The shape predicate hX | Gi has the property iff G has.
This graph-based formulation is the basis for our formal development. However,even though graphs are an intuitive way of thinking about regularly infinite shapepredicates, they are less convenient for writing down shape predicates, at least in ahuman-friendly form. Figure 3 defines a more tree-like textual notation for shapegraphs for use in examples.
Proposition 6 The relations of Defn. 2 are effectively (and efficiently) decidable
when shape predicates are given as graphs.
Definition 7 Two shape graphs G1 and G2 are equivalent, written G1 ≈ G2 , iff[[hX | G1 i]] = [[hX | G2 i]] for all X.

3.2

Effective characterisation of containment

Definition 8 Let R be a relation between shape predicates. R is a shape simulation
0π
Y ∈ G imply that there is π0 ≥ π and Y 0 such that X 0 π→
iff hX | Gi R hX 0 | G0 i and X →0000
Y ∈ G and hY | Gi R hY | G i.
Theorem 9 Shape containment ≤ is the largest shape simulation; it is the union of
all shape simulations.
Thus, to prove that σ ≤ σ0 it is sufficient to find a shape simulation R such that σ R σ0 .
This strategy leads directly to:
Proposition 10 The relation hX | Gi ≤ hX 0 | G0 i can be decided effectively (actually, in polynomial time).
It is worth noticing that shape simulations treat (~a) just like any other prefix type. Thus
≤ treats the “result” type covariantly (like [24]), whereas the input position in PolyA
is a list of names and thus essentially invariant.
1 This happens even for terminating terms such as b[in a.0]|a[open b.0], because shape predicates cannot
distinguish them from !b[!in a.0] | !a[open b.0]. Thus, nearly every nontrivial use of open will needrecursive σ’s. As already observed by Cardelli et al. [6], open often complicates analysis significantly.

This is the syntax of shape expressions:
Shape expressions: V ::= U | X | letrec X1 = U1 ; . . . ; Xn = Un in Xi
Shape summands: U ::= 0 | (U |U) | π | (π1 |· · ·|πn ).V | (X)As additional syntactic sugar, letrec · · · in U stands for letrec · · · ; X = U in X
where X is fresh. π.V stands for (π).V , and a[V ] stands for (amb a).V .
To convert a shape expression to a graph-shaped shape predicate, first replaceeach U of the form (X) with the right-hand side of the innermost in-scope letrec
binding for X. It is an error if no such binding exist, of if the unfolding does
not terminate. (V ’s of the form X are not touched at this stage). Then α-rename
the entire shape expression such that no X is bound by two different letrec’s, andapply the function (·)∗ defined by:
V ∗ is a shape predicate:U ∗ = hX |UX∗ i where X is freshX ∗ = hX | ∅i(letrec X1 = U1 ; . . . ; Xn = Un in Xi )∗ = hXi |U1 ∗X1 ∪ · · · ∪Un ∗Xn i
UX∗ is a shape graph:0∗X = ∅
(U1 |U2 )∗X = (U1 )∗X ∪ (U2 )∗XπX}π∗X = {X →πi∗((π1 |· · ·|πn ).V )X = { X → X 0 | 1 ≤ i ≤ n } ∪ G
where hX 0 | Gi = V ∗Note that the parentheses in U ::= (X) are important; they distinguish between “π.X”, which says toinsert an edge going to node X itself, and “π.(X)”, which says to insert an edge to a fresh node that
happens to behave like X. This can make a difference for whether the shape graph satisfies certain
criteria that we’ll define later.

Figure 3.

3.3

Shape expressions: a tree-like notation for recursive shape predicatesType substitutions

Definition 11 A type substitution T is a function from names to message types
such that T (a) 6= {a} for only finitely many a’s. Like term substitutions, type substitutions may be written as [a1 7→ µ1 , . . . , ak 7→ µk ] or [a 7→ µa ]a∈A .
A type substitution can be applied to capabilities, message types, shape graphs, andshape predicates as follows:
Type substitution for capabilities: T C is a message type, not a capability.
nT • = <•>
T a = T (a)T (O a) = <O b> if T (a) = {b}<•>otherwise
Substitution for message types: T µ is a message type given by:
To compute T {C1 , . . . ,Ck }*, let µi = T Ci for 1 ≤ i ≤ k. If µi = <> for all i, then the
result is also <>. Otherwise, the result is {C10 , . . . ,Cn0 }*, where the C0j s are allcapabilities that occur in any of the µi ’s, with duplicates removed (and in some
canonical order).
To compute T <C1 . · · · .Ck >, let µi = T Ci for 1 ≤ i ≤ k. If any µi has the form { · · · }*,or if any C appears in more than one µi , then the result is the same as the resultof T {C1 , . . . ,Ck }*. Otherwise, each µi has the form < · · · >. Concatenate all of
the capability lists (in the order of the i’s) and return <the concatenated list>.
Finally, T {a} is simply T (a).

Substitution for shape graphs: T G is a shape graph. To construct T G, first constructεY . Gεan intermediate graph Gε which can contain special null edges written X →πcontains contributions from each edge Y1 → Y2 ∈ G:
1 When π = a and T (a) = {C1 , . . . ,Ck }*, choose a fresh node Z, and add to Gε
the following edges:
ε
ε
k
1
2
Y1 →
Z C→
Z C→
· · · C→Z→
Y2

2 When π = a and T (a) = <C1 . · · · .Ck >, choose fresh nodes Z0 through Zk , andadd to Gε the edges
ε
ε
1
2
Z0 C→
Z1 C→
· · · Zk →
Y2
Y1 →
b
3 When π = a and T (a) = {b}, add to Gε the edge Y1 →Y2 .
0
4 When π = O a, T (O a) will always have the form <C >. Add to Gε the edge
0
Y1 C→ Y2 .
5 When π = (a1 , . . . , ak ), check that T ai = {ai } for all i, and then add the edgeπ•
Y1 →Y2 to Gε . Otherwise, add Y1 →
Y2 .
li
Y2 .
6 When π = hµ1 , . . . , µk i, add to Gε the edge Y1 hT µ1 ,...,T µ→
π
ε
ε
π
ε
Now set T G = { Xk →Y | (Xk →Xk−1 →··· →X0 →Y ) ∈ Gε , k ≥ 0 }.

Substitution for shape predicates: T σ is a shape predicate given by:

T hX | Gi = hX | T Gi
Definition 12 Extend matching of message types pointwise to type substitutions:` S : T iff ` S (a) : T (a) for all a.
Theorem 13 Given any σ and T , let P = { S P | ` P : σ ∧ ` S : T }. Then P ⊆ [[T σ]].

4 Shape predicates as types
4.1

Closed shape predicatesDefinition 14 The shape predicate σ is semantically closed iff its meaning is closed
under reduction, i.e., if ` P : σ and P ,→ Q imply ` Q : σ.
This definition is intuitively appealing, but it is not immediately clear how to decideit. However, we have local rules that imply semantic closure:
Definition 15 The shape graph G is locally closed at X0 iffbbaX), (X in→
Y ), (X0 amb→Z)} ⊆ G
1 {(X0 amb→
amb a
0
0
⇒ ∃X : Z→ X ∈ G ∧ hX | Gi ≤ hX 0 | Gi ∧ hY | Gi ≤ hX 0 | Gi,aba
2 {(X0 amb→
X), (X amb→
Y ), (Y out→Z)} ⊆ Gamb b
00
→ Y ∈ G ∧ hY | Gi ≤ hY 0 | Gi ∧ hZ | Gi ≤ hY 0 | Gi,⇒ ∃Y : X0aa
3 {(X0 amb→
X), (X0 open→Y )} ⊆ G⇒ hX | Gi ≤ hX0 | Gi ∧ hY | Gi ≤ hX0 | Gi, and)iY ), (X0 (a1 ,...,ak→Z)} ⊆ G
4 {(X0 hµ1 ,...,µk→⇒ hY | Gi ≤ hX0 | Gi ∧ [ai 7→ µi ]1≤i≤k hZ | Gi ≤ hX0 | Gi.

These rules can also be expressed in diagram form:

≤

≤
(a1 ,
...,a
k)

a

a
≤

am
b

am

am
b

≤

≤

Y

i
...,µ k
hµ 1,

a

b
am

b

b
in

≤

≤

ba

am

Y

X

en

Y0

X

X0
op

bb

X
X0

am

ba

Z

X0
≤

X0
am

bb

X0

Y

out a

Z

[ai 7→ µi ]1≤i≤k Z

ZYDefinition 16 Let σ = hX | Gi be a shape predicate. The active nodes in σ, written
active(σ), is the least set of node names such thataZ ∈ G }.
active(σ) = {X} ∪ { Z | ∃Y ∈ active(σ) : ∃a : Y amb→

Definition 17 The shape predicate hX | Gi is syntactically closed iff G is locallyclosed at every X ∈ active(hX | Gi).
Definition 18 The shape graph G is trim iffπX→
Y ∈ G ∧ X π→ Z ∈ G ∧ π ≤ π0 ∧ hY | Gi ≤ hZ | Gi =⇒ Y = Z.
0

Thus a trim graph is one where no edges can be taken away without changing its
meaning. Trimness is not a significant restriction:
Lemma 19 Every shape graph G has a trim equivalent subgraph: For any G there
exists a trim G0 ⊆ G such that G ≈ G0 .
Definition 20 The shape graph G is discrete iff both of these hold:
1 For each capability C that is not amb a for some a, whenever G contains a chain
C
C
C
X0 →
X1 →
··· →
Xk of edges all decorated with C and any two of the Xi ’s are
identical, then X0 = X1 = · · · = Xk .
2 G does not contain any message type of the shape {C1 , . . . ,Ck }* such that oneof the Ci ’s is amb a.
Theorem 21 Let σ be a shape predicate.
1 If σ is syntactically closed, then it is semantically closed.
2 If σ is trim and discrete, then the reverse implication also holds: σ is syntactically closed if and only if it is semantically closed.
The conditions in Thm. 21(2) are necessary. A shape predicate that is semanticallyclosed but neither discrete nor syntactically closed isletrec X = ((a).a.a.c.0 | h{b}*i.0 | b.b.X | c.0) in X.
One that is semantically closed but neither trim nor syntactically closed isa[in b.0] | b[a[in b.0]] | b[0].

4.2

Types

Definition 22 A type τ is a syntactically closed shape predicate. Given a type τ,
the term P has type τ iff ` P : τ.
This notion of types has the basic properties expected of any type system: It enjoys
subject reduction (Thm. 21(1)), it can be effectively decided whether a given term has
a given type (Prop. 6), and types can be distinguished from non-types (using Prop. 10).
Given an algorithm to compute precise types (such as the one we present in [15]),
one can approximate various properties of a term’s computational behaviour:•
If P has the type σ = hX | Gi and G contains no edge Y →Z with Y ∈ active(σ),
then executing P will never execute a malformed substitution result such as [a 7→M.N](in a).
•
If P has the type hX | Gi and G contains no edge Y →Z, then executing P will
never create a malformed substitution result.
Any security policy can be checked if it can be stated as a condition on configurations that must not arise. For example, the policy “no ambient a must everdirectly contain an ambient named b” is satisfied by P if it has a type hX | Giabsuch that G does not contain a sequence X1 amb→
X2 amb→X3 .

Proposition 23 Every term P has a type (although the type may contain • and thus
not prove that the term “cannot go wrong”).
Our notion of types is very expressive — it allows a very fine-grained approximation to important questions. However, it is not known whether principal types always
exist; we have neither proved nor disproved this. Thus, we now define a syntactically
restricted type system for which we do prove that principal types exist.

4.3

Modest types; existence of principal types

Definition 24 Define the relation =(≤) on prefix types as the least equivalence
relation that contains ≤.
Definition 25 Define the stratification function S by
S((~a)) = S(h~µi) = 3

S(amb a) = 2

S(C) = 1 when C 6= amb a

Definition 26 The shape graph G is modest iff for each π, one of the following
conditions hold:
1 Finite depth. There is a number nπ such that whenever G contains a chain
k
1
2
X0 π→
X1 π→
· · · π→Xk with every S(πi ) ≤ S(π), there are at most nπ different
i’s such that πi =(≤) π.
k1
2
2 Monomorphic recursion. Whenever G contains a chain X0 π→
X1 π→· · · π→Xkwith every S(πi ) ≤ S(π) and π1 =(≤) π =(≤) πk , then X1 = Xk .

Modesty is a rather technical concept; indeed it has been designed specifically to allow
Theorem 27 below to hold. Here are some comments that may help getting an intuition
about what modesty means. The basic idea is to restrict cycles so that they cannot be
arbitrarily long without mentioning arbitrarily many different capabilities. A graphwithout any cycles will always be modest, because it satisfies finite depth for all π.
However, as remarked in Sect. 3.1, acyclic shape predicates are not enough to typeall interesting terms. We therefore allow cycles, as long as they satisfy “monomorphic
recursion”. This condition says that if we need cycles containing some prefix — say,the ambient boundary amb a — then two nested ambients of that name must matchthe same node in the graph. This means that a[(b).in b.0 | a[hεi.0 | in a.0]] cannot
be typed with a •-free modest type, because the monomorphic recursion requirement
forces the type inside the two a ambients to be the same node in the graph. Therefore
the type system wrongly thinks that hεi may communicate with (b).in b.0.
The stratification function S eases the modesty requirements. It says that a cycle
only “counts as a cycle” for the prefix type in it at the highest stratum. Thus the chainbaa
X2 amb→
X3 out→X4 does not violate monomorphic recursion for out a.
X1 out→C
Because ambient boundaries are in their own stratum, there can be local X →X edgeswithin each ambient without forcing all nodes in the graph to collapse.
It is always possible to satisfy finite depth for stratum-3 prefix types, because noneof the reduction rules increase the nesting depth of communication actions. Therefore, one needs only consider shape graphs consisting of clusters of capability-marked
edges, linked together by stratum-3 edges in a tree.
The flexibility offered by stratification is restricted because one must choose globally between finite depth and monomorphic recursion for each π, rather than in eachisolated cluster of stratum-n-and-lower edges. This restriction is not intuitive, but isneeded for technical reasons in the proof.
While it is easy to construct terms where non-modest types allow a more precise
analysis, they do not seem to correspond to natural programming styles. We conjecturethat restriction of expressive power entailed by requiring modesty does not seriously
impede PolyA’s ability to analyse real-world software designs.
Allowing only modest and discrete types yields principal typings (defined in [23]):

Theorem 27 For every term P which has at least one modest discrete type, there isa modest discrete type τ that is minimal among P’s modest discrete types.
The proof of Theorem 27 is non-constructive and does not point to an effective
procedure for finding a principal type. In [15] we have defined (and implemented) apractical type inference algorithm for a yet more restricted version of PolyA, but its
principality properties are not yet well understood.
Requiring discreteness of types loses Prop. 23: There exist terms having no discrete
type. However, all terms (without name restriction) of the original ambient calculus
have types:~ has a modest
Proposition 28 Any term P that does not contain amb a inside hMi
discrete type, and so also a principal such.

5

Extended and modified ambient calculi

Our framework is strong enough to handle many ambient calculus variants withdifferent reduction rules. In most cases, PolyA can be extended to deal with suchvariation simply by adjusting Defn. 15 with conditions systematically derived fromthe changed or new reduction rules. If this is done correctly and the new or changed
rules are straightforward rewriting steps, then it is simple to construct new cases for the
proof of Thm. 21. The rest of our theory will then carry through unchanged, including
the existence of principal types.
We illustrate this principle with examples of such extensions.
Boxed Ambients [4] removes the open capability; instead processes can communicate
across ambient boundaries with directional communication actions:
¯
¯
¯
¯
¯
¯
~ ↑ ¯ hMi
~ ? ¯ hMi
~ ↓a ¯ (~a)↑ ¯ (~a)? ¯ (~a)↓aPrefixes: p ::= M ¯ hMiThere are corresponding reduction rules such as:~ ↓b .P | b[Q | (~a)? .R] ,→ P | b[Q | [ai 7→ Mi ]i R]
hMi
Our prefix type syntax is easily extended to include the new actions. The new reduction rules can be used to derive local closure conditions such as:↓b?

b{(X0 hµ1 ,...,µk i → X), (X0 amb→
Y ), (Y (a1 ,...,ak )→ Z)} ⊆ G⇒ hX | Gi ≤ hX0 | Gi ∧ [ai 7→ µi ]1≤i≤k hZ | Gi ≤ hY | Gi

Safe Ambients [13] introduces co-capabilities (also added to BA by [5]), where bothinteraction parties must present a capability. This can improve analysis precision andavoid unwanted behaviours. The reduction rules are amended to require this, e.g.:a[open a.P | Q] | open a.R ,→ P | Q | R
It is straightforward to extend PolyA to systems with co-capabilities. For example,
condition 3 of Defn. 15 would be replaced by:aaa{(X0 amb→
X), (X0 open→Y ), (X open→Z)} ⊆ G
⇒ hX | Gi ≤ hX0 | Gi ∧ hY | Gi ≤ hX0 | Gi ∧ hZ | Gi ≤ hX0 | Gi.

The M3 calculus [11] introduces a new method of inter-ambient communication; a
new capability to can move a process into a neighbour ambient:a[P | to b.Q] | b[R] ,→ a[P] | b[Q | R]
This, too, is easily expressed as a closure condition:abb{(X0 amb→
X), (X0 amb→
Y ), (Y to→Z)} ⊆ G
⇒ hZ | Gi ≤ hX | Gi

References[1] T. Amtoft, A. J. Kfoury, and S. M. Pericas-Geertsen. What are polymorphically-typed ambients? In D. Sands, editor, ESOP 2001, Genova, volume 2028 of LNCS, pages 206–220.
Springer-Verlag, Apr. 2001. An extended version appears as Technical Report BUCS-TR2000-021, Comp.Sci. Department, Boston University, 2000.
[2] T. Amtoft, A. J. Kfoury, and S. M. Pericas-Geertsen. Orderly communication in theambient calculus. Computer Languages, 28:29–60, 2002.
[3] T. Amtoft, H. Makholm, and J. B. Wells. PolyA: True type polymorphism for Mobile
Ambients. Technical Report HW-MACS-TR-0015, Heriot-Watt Univ., School of Math.
& Comput. Sci., Feb. 2004.
[4] M. Bugliesi, G. Castagna, and S. Crafa. Boxed ambients. In 4th International Conferenceon Theoretical Aspects of Computer Science (TACS’01), volume 2215 of LNCS, pages
38–63. Springer-Verlag, 2001.
[5] M. Bugliesi, S. Crafa, M. Merro, and V. Sassone. Communication interference in mobile
boxed ambients. In FST & TCS 2002, 2002.
[6] L. Cardelli, G. Ghelli, and A. D. Gordon. Mobility types for mobile ambients. In J. Wiedermann, P. van Emde Boas, and M. Nielsen, editors, ICALP’99, volume 1644 of LNCS,
pages 230–239. Springer-Verlag, July 1999. Extended version appears as Microsoft Research Technical Report MSR-TR-99-32, 1999.
[7] L. Cardelli and A. D. Gordon. Mobile ambients. In M. Nivat, editor, FoSSaCS’98, volume1378 of LNCS, pages 140–155. Springer-Verlag, 1998.
[8] L. Cardelli and A. D. Gordon. Types for mobile ambients. In POPL’99, San Antonio,
Texas, pages 79–92. ACM Press, Jan. 1999.
[9] L. Cardelli and P. Wegner. On understanding types, data abstraction, and polymorphism.
Computing Surveys, 17(4):471–522, 1985.
[10] M. Coppo and M. Dezani-Ciancaglini. A fully abstract model for higher-order mobile
ambients. In VMCAI 2002, volume 2294 of LNCS, pages 255–271, 2002.
[11] M. Coppo, M. Dezani-Ciancaglini, E. Giovannetti, and I. Salvo. M3: Mobility types formobile processes in mobile ambients. In CATS 2003, volume 78 of ENTCS, 2003.
[12] F. Levi and S. Maffeis. An abstract interpretation framework for analysing mobile ambients. In SAS’01, volume 2126 of LNCS, pages 395–411. Springer-Verlag, 2001.
[13] F. Levi and D. Sangiorgi. Controlling interference in ambients. In POPL’00, Boston,
Massachusetts, pages 352–364. ACM Press, Jan. 2000.
[14] C. Lhoussaine and V. Sassone. A dependently typed ambient calculus. In Programming Languages & Systems, 13th European Symp. Programming, volume 2986 of LNCS.
Springer-Verlag, 2004.
[15] H. Makholm and J. B. Wells. Type inference for PolyA. Technical Report HW-MACSTR-0013, Heriot-Watt Univ., School of Math. & Comput. Sci., Jan. 2004.
[16] R. Milner. Communicating and Mobile Systems: The π-Calculus. Cambridge Press, 1999.
[17] F. Nielson, H. R. Nielson, and M. Sagiv. A Kleene analysis of mobile ambients. In
Programming Languages & Systems, 9th European Symp. Programming, volume 1782 of
LNCS, pages 305–319. Springer-Verlag, 2000.
[18] H. R. Nielson and F. Nielson. Shape analysis for mobile ambients. In POPL’00, Boston,
Massachusetts, pages 142–154. ACM Press, 2000. A revised and extended version has
appeared in Nordic Journal of Computing, 8:233–275, 2001.
[19] B. C. Pierce and D. Sangiorgi. Behavioral equivalence in the polymorphic pi-calculus.
Journal of the ACM, 47(3):531–584, May 2000.
[20] G. Sander. Graph layout through the VCG tool. In R. Tamassia and I. G. Tollis, editors,
Graph Drawing: DIMACS International Workshop, GD ’94, volume 894 of LNCS, pages
194–205. Springer-Verlag, 1994.
[21] D. Teller, P. Zimmer, and D. Hirschkoff. Using ambients to control resources. In CONCUR’02, volume 2421 of LNCS, pages 288–303. Springer-Verlag, 2002.
[22] D. N. Turner. The Polymorphic Pi-Calculus: Theory and Implementation. PhD thesis,
University of Edinburgh, 1995. Report no ECS-LFCS-96-345.
[23] J. B. Wells. The essence of principal typings. In Proc. 29th Int’l Coll. Automata,
Languages, and Programming, volume 2380 of LNCS, pages 913–925. Springer-Verlag,
2002.
[24] P. Zimmer. Subtyping and typing algorithms for mobile ambients. In FOSSACS 2000,
Berlin, volume 1784 of LNCS, pages 375–390. Springer-Verlag, 2000.