Docsity

Principals in Programming Languages: A Syntactic Proof Technique, Study notes of Programming Languages

Toggle sidebar
bg1
Principals in Programming Languages: A Syntactic Proof Technique
Steve Zdancewic Dan Grossman Greg Morrisett ∗
Department of Computer Science
Cornell University
Abstract
Programs are often structured around the idea that different
pieces of code comprise distinct principals,eachwithaview
of its environment. Typical examples include the modules
of a large program, a host and its clients, or a collection of
interactive agents.
In this paper, we formalize this notion of principal in
the programming language itself. The result is a language
in which intuitive statements such as, “the client must call
open to obtain a file handle,” can be phrased and proven
formally.
We add principals to variants of the simply-typed λ-
calculus and show how we can track the code corresponding
to each principal throughout evaluation. This multiagent
calculus yields syntactic proofs of some type abstraction
properties that traditionally require semantic arguments.
1 Introduction
Programmers often have a notion of principal in mind when
designing the structure of a program. Examples of such
principals include modules of a large system, a host and
its clients, and, in the extreme, individual functions. Di-
viding code into such agents is useful for composing pro-
grams. Moreover, with the increasing use of extensible sys-
tems, such as web browsers, databases [6], and operating
systems [7, 3, 2], this notion of principal becomes critical
for reasoning about potentially untrusted agents interfacing
with host-provided code.
In this paper, we incorporate the idea of principal into
variants of the simply-typed λ-calculus. Doing so allows
us to formalize statements about agent interaction, for in-
stance, a client must call open to obtain a file handle. As
a motivating example, we consider the problem of type ab-
straction in extensible systems.
∗This material is based on work supported in part by the AFOSR
grant F49620-97-1-0013, ARPA/RADC grant F30602-1-0317, and Na-
tional Science Foundation Graduate Fellowships. Any opinions, find-
ings, and conclusions or recommendations expressed in this publica-
tion are those of the authors and do not reflect the views of these
agencies.
(* File handle implemented as int *)
abstype fh
open :string →fh
read :fh →char
Figure 1: Abstract interface for file handles
Consider a host-provided interface for an abstract type
of file handles, fh, and operations to create and use them
(Figure 1). The principals in this scenario are the host im-
plementation of the interface and its clients. Each principal’s
“view of the world” corresponds to its knowledge regarding
fh. In particular, the host knows that fh =int, while clients
do not.
The conventional wisdom is that the use of abstract
datatypes in a type-safe language prevents agents from di-
rectly accessing host data. Instead, a client may only ma-
nipulate such data via a host-provided interface. To for-
malize this wisdom, it is necessary to prove theorems that
say, “agent code cannot violate type abstractions provided
by the host.” For instance, a client should not b e able to
treat an object of type fh as though it were an integer, even
though the host implements it that way.
How do we prove such properties? One way of phrasing
the result is to say that the agent behaves parametrically
with respect to the type fh. Using this observation, we can
encode the agent program in a language like Girard’s Sys-
tem F [5], the polymorphic λ-calculus [18] (see Figure 2).
Here, the type fh is held abstract by encoding the agent as
a polymorphic function. We can then appeal to Reynolds’
parametricity results [19] to conclude that the agent respects
the host’s interface.
Λfh.λhost :{open :string →fh,read :fh →char}.
agent code
Figure 2: Polymorphic λ-calculus agent encoding
Unfortunately, these representation independence results
are proven using semantic arguments based on a model of
the language (see Mitchell’s work [11], for example). We
are unaware of any similar results for languages including
multiple features of modern languages, such as references,
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Principals in Programming Languages: A Syntactic Proof Technique and more Study notes Programming Languages in PDF only on Docsity!

Principals in Programming Languages: A Syntactic Proof Technique

Steve Zdancewic Dan Grossman Greg Morrisett ∗

Department of Computer Science

Cornell University

Abstract

Programs are often structured around the idea that different pieces of code comprise distinct principals, each with a view of its environment. Typical examples include the modules of a large program, a host and its clients, or a collection of interactive agents. In this paper, we formalize this notion of principal in the programming language itself. The result is a language in which intuitive statements such as, “the client must call open to obtain a file handle,” can be phrased and proven formally. We add principals to variants of the simply-typed λ- calculus and show how we can track the code corresponding to each principal throughout evaluation. This multiagent calculus yields syntactic proofs of some type abstraction properties that traditionally require semantic arguments.

1 Introduction

Programmers often have a notion of principal in mind when designing the structure of a program. Examples of such principals include modules of a large system, a host and its clients, and, in the extreme, individual functions. Di- viding code into such agents is useful for composing pro- grams. Moreover, with the increasing use of extensible sys- tems, such as web browsers, databases [6], and operating systems [7, 3, 2], this notion of principal becomes critical for reasoning about potentially untrusted agents interfacing with host-provided code. In this paper, we incorporate the idea of principal into variants of the simply-typed λ-calculus. Doing so allows us to formalize statements about agent interaction, for in- stance, a client must call open to obtain a file handle. As a motivating example, we consider the problem of type ab- straction in extensible systems.

∗This material is based on work supported in part by the AFOSR grant F49620-97-1-0013, ARPA/RADC grant F30602-1-0317, and Na- tional Science Foundation Graduate Fellowships. Any opinions, find- ings, and conclusions or recommendations expressed in this publica- tion are those of the authors and do not reflect the views of these agencies.

(* File handle implemented as int *) abstype fh open : string → fh read : fh → char

Figure 1: Abstract interface for file handles

Consider a host-provided interface for an abstract type of file handles, fh, and operations to create and use them (Figure 1). The principals in this scenario are the host im- plementation of the interface and its clients. Each principal’s “view of the world” corresponds to its knowledge regarding fh. In particular, the host knows that fh = int, while clients do not. The conventional wisdom is that the use of abstract datatypes in a type-safe language prevents agents from di- rectly accessing host data. Instead, a client may only ma- nipulate such data via a host-provided interface. To for- malize this wisdom, it is necessary to prove theorems that say, “agent code cannot violate type abstractions provided by the host.” For instance, a client should not be able to treat an object of type fh as though it were an integer, even though the host implements it that way. How do we prove such properties? One way of phrasing the result is to say that the agent behaves parametrically with respect to the type fh. Using this observation, we can encode the agent program in a language like Girard’s Sys- tem F [5], the polymorphic λ-calculus [18] (see Figure 2). Here, the type fh is held abstract by encoding the agent as a polymorphic function. We can then appeal to Reynolds’ parametricity results [19] to conclude that the agent respects the host’s interface.

Λfh.λhost : {open : string → fh, read : fh → char}. agent code

Figure 2: Polymorphic λ-calculus agent encoding

Unfortunately, these representation independence results are proven using semantic arguments based on a model of the language (see Mitchell’s work [11], for example). We are unaware of any similar results for languages including multiple features of modern languages, such as references,

recursive types, objects, threads, and control operators. Our calculus circumvents this problem by syntactically distinguishing between agents with different type informa- tion. We do this by “coloring” host code and client code with different colors and tracking how these colors inter- mingle during evaluation. By using different semantics for each principal, we force the client to respect the abstract types provided by the host. This separation of principals provides hooks that enable us to prove some type abstrac- tion properties syntactically. To see why these new mechanisms are useful, consider the evaluation of our agent code when “linked” against a host implementation. As Figure 3 shows, linking is encoded as application. In one step of the standard operational se- mantics, the host-type is substituted throughout the agent code. It is impossible to talk about the type fh remaining ab- stract within the client because fh is replaced by int. After a second step, host code is substituted throughout agent code and all distinctions between principals are lost.

τh = {open : string → fh, read : fh → char}

(Λfh.λhost : τh.agent code) int host code 7 −→ (λhost : {int/fh}τh.{int/fh}agent code) host code 7 −→ {host code/host}{int/fh}agent code

Figure 3: “Linking” agent code

The next section describes a two-agent setting sufficient for proving interesting properties about the file handle ex- ample. It then discusses how to include more advanced lan- guage features, such as recursive types and state. Section 3 introduces a multiagent calculus which provides for mul- tiple agents and abstract types. We then revisit the safety properties and language extensions of Section 2. The final sections conclude with related work and other potential uses for principals in programming languages.

2 The Two-agent Language

2.1 Syntax

This section describes a variant of the simply-typed λ- calculus with two principals, an agent and a host. The lan- guage maintains a syntactic distinction between host and agent code throughout evaluation. The host exports one abstract type, t, implemented as type τh.

τ ::= t | b | τ → τ ′

A ::= xa | c | λxa:τ. A | A A′^ | pxHqyτh A^ ˆ ::= c | λxa:τ. A | px Hˆqyt h

H ::= xh | c | λxh:τ. H | H H′^ | pxAqyτa H^ ˆ ::= c | λxh:τ. H

Figure 4: Two-agent Syntax

Figure 4 gives the syntax for the two-agent calculus.

Types, τ , include a base type, b, the host’s abstract type, t, and function types. The terms of the language are agent terms, A, agent values, Aˆ, host terms, H, and host values, Hˆ. The metavariable xa ranges over agent variables which are disjoint from host variables, ranged over by xh. The metavariable c ranges over values of base type. It is helpful to think of terms generated by A and H as having different colors (indicated by the subscripts a and h respectively) that indicate to which principal each belongs. As observed in the introduction, agent and host terms mix during evaluation. To keep track of this intermingling, agent terms contain embedded host terms of the form pxHqyτh. Intu- itively, the brackets delimit a piece of h-colored code, where H is exported to the agent at type τ. Dually, host terms may contain embedded agents. The type annotations on the embeddings keep track of values of type t during execution. In particular, a host term of type τh may be embedded in an agent term. If the an- notation is t, then the agent has no information about the form of the term inside the embedding. Thus, an embedding with annotation t containing a host value is an agent value. A good intuition for the semantics is to imagine two copies of the simply-typed λ-calculus augmented with a new type t. In the agent copy, t is abstract, while in the host copy, we have t = τh. Because the host has more knowledge, there is an asymmetry in the language. In the semantics, this asymmetry manifests itself in rules in which the host refines t to τh.

2.2 Notation

Before describing the semantics, we define some convenient notions. Let e range over both agent and host terms, and let ˆe range over both agent and host values. The color of e is a if e is an A term; otherwise e’s color is h. Note that both terms in a syntactically well-formed application are the same color. Since the host and agent terms share some semantic rules, we use polychromatic rules to range over both agent and host terms. The intention is that all terms mentioned in a polychromatic rule have the same color, and the rule is short-hand for two analogous rules, one for each color. We write {e′/x}e for the capture-avoiding substitution of e′^ for x in e. Terms are equal up to α-conversion, where substituted variables are of the same color. We also define substitution on types, written {τ ′/t}τ. Intuitively, we use the substitution {τh/t}τ to produce the host’s view of τ. We say an agent term is host-free if it contains no em- beddings (and similarly for agent-free host terms).

2.3 Dynamic Semantics Figure 5 describes a small-step operational semantics for the two-agent calculus. The polychromatic rules are the same as for the simply-typed, call-by-value λ-calculus. The other rules handle embeddings. Rules (A1) and (H1) allow evaluation to proceed within embeddings. Inside embeddings, the rules for the opposite color apply. These “context switches” ensure that terms evaluate in the appropriate context for their color. If an embedded value is exported to the outer principal at type b, the outer agent can strip away the embedding and use that value (rules (A2) and (H2)). Rules (A3) and (H3) maintain the distinction between agent and host code. For example, suppose the agent con- tains a host function that is being exported at type τ ′^ → τ ′′.

Polychrome Rules

(var) Γ x : Γ(x) (const) Γ c : b (app)

Γ e : τ ′ → τ Γ e ′ : τ ′

Γ ` e e ′ : τ

Agent Rules

(HinA)

Γ H : {τh/t}τ Γ pxHqyτh : τ (Afn)

Γ[xa : τ ′] A : τ Γ λxa:τ ′. A : τ ′^ → τ

Host Rules

(AinH)

Γ A : τ Γ pxAqy τ a :^ {τh/t}τ^

(Hfn)

Γ[xh : τ ′] H : τ Γ λxh:τ ′. H : τ ′^ → τ

(t 6 ∈ τ ′)

Figure 7: Two-agent Static Semantics

embedded in the host as an agent term. Step (2) is a stan- dard β-reduction. Step (3) is another β-reduction, passing in the string “myf ile”. Step (4) uses (H2) to extract the string from the embedding. At this point, the host function ho is applied to a host value. Repeated use of (A1) allows the host function to proceed. We assume that ho returns 3 when applied to “myf ile”. This result, embedded within the agent code at type fh, is an agent value.

pxλhandleh:int. hr(handleh)qyfhh →char px 3 qyfh h (1) 7 −→ λhandlea:fh. pxhr(pxhandleaqyinta )qycharh px 3 qyfh h (2) 7 −→ pxhr(pxpx 3 qyfh h qyinta )qycharh (3) 7 −→ pxhr(3)qycharh (n) 7 −→∗^ px′A′qycharh 7 −→ ′A′

Figure 8: Agent calling read

The second example (Figure 8) illustrates the agent call-

ing the host’s read function, passing in the file handle px 3 qyfh h. The host code for read is embedded in the agent and ex- ported at the type, fh → char. (We omit the linking steps for brevity.) The body of the host function is hr, a host term taking an integer representing a file handle and returning an integer read from that file. In step (1), the agent extracts the host function via rule (A3). The type of the argument handlea is abstract in the agent, so the type annotation is changed to fh. The second step is a β-reduction. At this point, evaluation continues via (H4), which in step (3) allows the embedded host code to extract the integer 3, held abstract by the agent, as a regular value. The application hr(3) proceeds as usual, until the host computes the character read from the file. At last, since this embedded character is exported to the agent as such, rule (A2) produces the value ′A′.

2.6 Safety Properties

In this section, we explore properties of the two-agent cal- culus including soundness and some type abstraction theo- rems. Sketches of the proofs are deferred as they are corol-

laries to the corresponding proofs in Section 3.5. These properties are not intended to be as general or as “realis- tic” as possible. Rather, they convey the flavor of some statements that are provable using syntactic arguments. The following lemmas establish type soundness:

Lemma 2.1 (Canonical Forms) Assuming ∅ ` eˆ : τ , if τ =

  • b, then ˆe = c for some c.
  • τ ′^ → τ ′′, then eˆ = λx:τ ′. e′^ for some x and e′.
  • t, then ˆe = px Hˆqyt h for some Hˆ of type τh.

Lemma 2.2 (Preservation) If ∅ e : τ and e 7 −→ e′ then ∅ e′^ : τ.

Lemma 2.3 (Progress) If ∅ ` e : τ , then either e is a value or there exists an e′^ such that e 7 −→ e′.

Definition 2.4 (Stuck Terms) A term e is stuck if it is not a value and there is no e′^ such that e 7 −→ e′.

Theorem 2.5 (Type Soundness) If ∅ ` e : τ then there is no stuck e′^ such that e 7 −→∗^ e′.

Given a term, if we ignore the distinction between col- ors, erase the embeddings, and replace t with τh, we have a simply-typed λ-calculus term. Formally, Figure 9 defines the erasure of a two-agent term. (All rules are polychro- matic.) The following lemma states that erasure commutes with evaluation.

Lemma 2.6 (Erasure) Let e be any two-agent term such that ∅ ` e : τ. Then e 7 −→∗^ ˆe iff erase(e) 7 −→∗^ erase(ˆe).

The interesting fact is that the erasure of rule (A3) is basically λx:τ. e 7 −→ λx′:τ. {x′/x}e. The two terms are α- equivalent. With soundness and erasure established, we re-examine the abstraction properties of the introduction. Since the host is always capable of providing information to the agent, we are particularly interested in evaluations where we as- sume the host does not do so. Toward this end, we define host-free to describe an agent term which has no host terms embedded in it. One desirable property of the file handle interface is that the agent never breaks the type abstraction for file han- dles. For example, if ∅ handle : fh, ∅ λf:fh. A : τ , and A

erase(x) = x erase(c) = c erase(λx:τ. e) = λx:{τh/t}τ. erase(e) erase(e e′) = erase(e) erase(e′) erase(pxeqyτi ) = erase(e)

Figure 9: Two-agent erase translation

is host-free, then (λf:fh. A) handle never steps to a context where handle is treated as an integer. This property is a corollary of type soundness. Another property is that the agent is oblivious to the particular choice of integers used by the host to represent a given file handle. More formally:

Theorem 2.7 (Independence of Evaluation)

If px Hˆqyfh h and px Hˆ′qyfh h are well-typed, A is host-free, and ∅ ` λfa:fh. A : fh → b, then:

(λfa:fh. A) px Hˆqyfh h 7 −→∗^ c iff (λfa:fh. A) px Hˆ′qyfh h 7 −→∗^ c

The proof strengthens the claim to a step-by-step evalu- ation correspondence when using Hˆ and Hˆ′:

Lemma 2.8 (Value Abstraction) Let Hˆ and Hˆ′^ be host values of type τh. If A is host-free, [xa : fh] ` A : τ , and

{px Hˆqyfh h /xa}A is not a value, then there exists a host-free term A′^ such that:

  • [xa : fh] ` A′^ : τ
  • {px Hˆqyfh h /xa}A 7 −→ {px Hˆqyfh h /xa}A′
  • {px Hˆ′qyfh h /xa}A 7 −→ {px Hˆ′qyfh h /xa}A′

The embeddings also enable us to track expressions of the abstract type during evaluation, thus allowing us to for- malize a third property: the agent must have called open to obtain a file handle.

Theorem 2.9 (File handles come from open) Suppose (λopena:string → fh. A) is well-typed and A is host-free. If the application of (λopena:string → fh. A) to

pxλsh:string. ho(sh)qystring h →fhsteps to some term A′

containing px Hˆqyfh h as a subterm, then Hˆ was derived from a sequence of the form ho(s) 7 −→∗^ Hˆ.

The proof shows that (after one step) every host embed- ding has as its inside term either an application of ho or an intermediate result of such an application.

2.7 Extending the Language

The two-agent calculus lacks many features found in realistic programming languages, but our embedding approach scales to handle many additions. It is straightforward to add standard type constructors such as products, records, sums, etc. The static semantics

τ ::=... | α | μα.τ H ::=... | rollμα.τ H | unrollμα.τ H H^ ˆ ::=... | rollμα.τ Hˆ

pxrollμα.τ Aˆqyμα.τa 7 −→ rollμα.{τh /t}τ px Aˆqy{aμα.τ /α}τ

Figure 10: Adding recursive types

are standard, while the dynamic semantics, in addition to the usual rules, also include means of propagating embed- dings. For example, px( A,ˆ Aˆ′)qyτ^ ×τ^

′ a 7 −→^ (px^ Aˆqy τ a ,^ px^ Aˆ ′qyτ ′ a ). Recursive types also pose no problems. The necessary additions and an example host dynamic step are shown in Figure 10. (The agent rule is similar.) Our type abstraction results carry through even in this setting. Adding state is more subtle. A rigorous treatment of references appears in the companion technical report [25]. We have proven soundness, as well as stateful analogues of the abstraction theorems presented here. The key issue is how to facilitate the sharing of heap val- ues between the host and agent. Our approach assigns (pos- sibly abstract) types to heap locations and records which agent last assigned to a location. An ensuing dereference by the other agent produces an embedded term. All of this extra information can be erased, so the target language of erasure is a standard language with references. An interest- ing technical issue is that it is unsound for the host to export a t ref value at type τh ref. Therefore, the definition of type substitution is modified so as not to perform substitution under the ref constructor. Restating the type abstraction properties to account for the heap is somewhat trickier, since we must preclude the possibility that the host leaks information to the agent through a shared reference. Essentially, the notion of host-free must be extended to account for terms reachable through references. With references and recursive types, we expect that our results could be extended to handle threads in the style of those in Reppy’s doctoral thesis [17]. Such scalability is due to the similarity of our proofs to standard subject-reduction, as popularized by Wright and Felleisen [24]. Namely, a property of interest is shown to be invariant during eval- uation. Although these syntactic techniques yield weaker results than semantic accounts of parametricity, they are useful in practice because we do not have to build a model containing recursive types, references, and threads.

3 The Multiagent Language

So far, we have described a simple two principal setting in which the host has strictly more information than the agent. Many interesting cases can be modeled in this fashion, but there are times when both principals wish to hide informa- tion or there are more than two agents involved. For exam- ple, we need a multiagent setting to prove safety properties about nested or mutually recursive abstract datatypes. Another natural generalization is to allow an agent to export multiple abstract types, and, once that has been in- troduced, agents should be able to share type information.

ei i 7 −→ e′′ i ei e′ i i 7 −→ e′′ i e′ i

e′ i i 7 −→ e′′ i vi e′ i i 7 −→ vi e′′ i

ej j 7 −→ e′ j pxej qyτj i 7 −→ pxe′ j qyτj

( 4 ) fix fi(xi:τ ).ei i 7 −→ λxi:τ. {fix fi(xi:τ ).ei/fi}ei ( 5 ) λxi:τ. ei vi i 7 −→ {vi/xi}ei

( 6 ) pxcqyb j i 7 −→ c ( 7 ) px ˆvj qyτj i 7 −→ px vˆj qy ∆¯i (τ ) j (τ^6 = ∆¯i(τ )) ( 8 ) pxpx ˆvj qyuj qyτk i 7 −→ px vˆj qyτj :`k (u 6 ∈ Dom(δk), τ = ∆¯i(τ ))

( 9 ) pxλxj :τ. ej qyτ^

′→τ ′′ `j

i 7 −→ λxi:τ ′. px{pxxiqyτi:rev(`j )/xj }ej qyτ^

′′ `j (xi^ fresh, τ^ ′ (^) → τ ′′ (^) = ∆¯i(τ ′ (^) → τ ′′))

Figure 12: Multiagent Dynamic Semantics

(λy:int → int. y 3)pxλx:fh → fh. xqyfh a→fh (7)

h 7 −→ (λy:int → int. y 3)pxλx:fh → fh. xqyinta →int (9) h 7 −→ (λy:int → int. y 3)(λx′:int. pxpxx′qyfh h:aqyinta ) (5) h 7 −→ (λx′:int. pxpxx′qyfh h:aqyinta ) (5) h 7 −→ pxpx 3 qyfh h:a qyinta (8) h 7 −→ px 3 qyinth:a:a (6) h 7 −→ 3

Figure 13: Multiagent Evaluation Example

ment to have. Hence the function body is still abstract to i and when the function is applied, the actual argument will be held abstract from j. The only remaining issue is the agent list on the formal argument embeddings. Since the “inside type” and “outside type” have reversed roles, the agent-list must be in reverse order. Intuitively, the agents which successively provided the function argument type to i must undo their work in the body of the function which is a j-term.

3.3 Example

As an example, we encode our two-agent calculus by letting δh map fh to int and letting δa be undefined everywhere. An evaluation which uses all of the novel rules appears in Figure 13. The numbers in the figure are the reduction rules used to take the step. Notice that under this simple system, rules (7) and (9) encode what was previously “hard-wired” into rule (H3). Similarly, rules (8) and (6) do the work of (H4).

3.4 Static Semantics

Figure 14 shows the multiagent static semantics for agent i. The judgment {∆}; Γ `i ei : τ should be read as, “Under type-maps ∆ 1 ,... , ∆n in context Γ, agent i can show that ei has type τ ”.

All of the rules except (embed) are essentially standard. The rules (abs) and (fix) have additional conditions that force an agent to use the most concrete type available for functions internal to the agent. As alluded to previously, the issue of consistency between agents arises during type checking. For instance, we don’t want an agent to export an int as a function. Likewise, we don’t want an agent, or collection of agents, to violate the type abstractions represented by the ∆i’s. Thus we need some way of relating the type of the expression inside the embedding to the typing annotation on the embedding. We establish an agent-list indexed family of relations on types, τ .i τ ′. Judgments of the form {∆} τ .i τ ′, showing when two types may be related by the listi, are given in Figure 15. These rules say that τ 0 .i 1 :i 2 :...:in τ n if there exist types τ 1 ,... , τ n−^1 such that agent ik is able to show that τ k−^1 = τ k^ for k ∈ { 1 ,... , n}. Informally, the agents are able to chain together their knowledge of type information to show that τ 0 = τ n. The (embed) rule uses the .j :i relation to ensure that the type inside the embedding matches up with the annota- tion on the embedding. The agent i is appended to the list because, as the outermost agent, i is implicitly involved in evaluation of the term. Why is this somewhat complicated mechanism neces- sary? To some extent, it’s not. It is clear that there must be some way of relating the type of a term inside an embedding to the type annotation on the embedding; otherwise, for ex- ample, an agent could export an integer as a string. We could have chosen to allow nested embeddings to be values, so long as each inner embedding is a value with respect to the enclosing agent (for example pxpx 3 qyt i qys j would be a k-value if s 6 ∈ Dom(δk) and t 6 ∈ Dom(δj )). This allows embeddings to “pile up” in a way that is difficult to deal with syntacti- cally and that complicates the dynamic semantics. Instead, we allow rule (8) to collapse two embeddings and push the work of ensuring compatibility onto the .i relation. The lists contain all of the agents that have par- ticipated in the evaluation of the inner value because incon- sistencies might arise otherwise. Consider three agents, i, j, and k such that δi(t) = int, δj (s) = t and δk = ∅. Then collapsing the properly typed k-term pxpx 3 qyt i qys j to either px 3 qys i or px 3 qys j violates the type abstraction properties since nei- ther i nor j knows that s abstracts an int. Alternately, if

(const) {∆}; Γ `i c : b

(var) {∆}; Γ `i xi : Γ(xi)

(app)

{∆}; Γ i ei : τ ′ → τ {∆}; Γi e ′ i :^ τ^

′

{∆}; Γ `i ei e′ i : τ

(abs)

{∆}; Γ[xi : τ ′] i e′ i : τ ∆i(τ ′) = τ ′ {∆}; Γi λxi:τ ′

. e ′ i :^ τ^

′ → τ

(xi 6 ∈ Dom(Γ))

(fix)

{∆}; Γ[fi : τ ′^ → τ ][xi : τ ′] i e′ i : τ ∆i(τ ′^ → τ ) = τ ′^ → τ {∆}; Γi fix fi(xi:τ ′).e′ i : τ ′^ → τ

(fi, xi 6 ∈ Dom(Γ))

(embed)

{∆}; Γ j ej : τ ′ {∆} τ ′ .j :i τ {∆}; Γi pxej qyτ`j : ∆¯i(τ )

Figure 14: Multiagent Static Semantics

(eq)

∆¯i(τ ) = ∆¯i(τ ′) {∆} ` τ .i τ ′

(trans)

{∆} τ ′ .i τ ′′ {∆} τ ′′ .j τ ′

{∆} τ .i:`j τ ′

Figure 15: Type relations: {∆} τ .i τ ′

we were to use sets of agents, instead of (ordered) lists, the reasonable typing rules become too permissive.

3.5 Safety Properties

This section illustrates some of the standard safety theorems of typed programming languages and then discusses abstrac- tion properties that generalize those presented earlier. Rig- orous proofs appear in the companion technical report [25]. We begin with type soundness:

Lemma 3.1 (Preservation)

If {∆}; ∅ `i ei : τ and ei

i 7 −→ e′ i then {∆}; ∅ `i e′ i : τ.

The interesting rules in proving preservation are (7) through (9) since they rely crucially on the. relations. In the Appendix, we state the relevant lemmas and present these cases of the proof.

Lemma 3.2 (Progress) If ei is well-typed then either ei

is a value or there exists an e′ i such that ei i 7 −→ e′ i.

The important point is that rules (6) through (9) guar- antee that pxvj qyτ`j is not stuck unless it is a value.

Theorem 3.3 (Type Safety) If {∆}; ∅ `i ei : τ then

there is no stuck e′ i such that ei i 7 −→∗^ e′ i.

The cost of including embeddings is the addition of sev- eral dynamic rules. Worse yet, with recursion and multiple

erase(xi) = x erase(c) = c erase(λxi:τ. ei) = λx: Φ(¯τ ). erase(ei) erase(fix fi(xi:τ ).ei) = fix f (x: ¯Φ(τ )).erase(ei) erase(ei e′ i) = erase(ei) erase(e′ i) erase(pxej qyτ`j ) = erase(ej)

where Φ =

S

i ∆i

Figure 16: Multiagent erase translation

agents, the lists annotating embeddings might grow arbi- trarily large. The erasure property stated below essentially shows that these syntactic tricks are only a proof technique. For erasure to a typed language, it is necessary to use the combined type information of all of the agents. The multiagent definition of erase is given in Figure 16, where Φ is the map obtained by taking the union of the compatible ∆i maps. Informally, Φ(¯τ ) is the most concrete type for τ that can be found using all n agents’ knowledge. The target language is the simply-typed λ-calculus aug- mented with fix. We have the following lemma:

Lemma 3.4 (Erasure) If ei is well-typed then either ei i 7 −→∗^ vi and erase(ei) 7 −→∗^ erase(vi), or ei and erase(ei) both diverge.

Proof (sketch): By induction on the source derivation. Strengthen the inductive hypothesis to show that for every step of the source language, the erased version takes either zero steps (rules (6) through (9)) or one step (rules (4) and (5)). For divergence, show the contrapositive: any term erasing to a non-diverging term is non-diverging. 2 We use arguments in the style of subject-reduction to prove safety properties that generalize those of the two-agent case.

Definition 3.5 (Agents(ei)) Let Agents(ei) be i and the set of agent subscripts appearing in ei.

5 Related Work

Perhaps the closest work to ours is Leroy and Rouaix’s inves- tigation into the safety properties of typed applets [9]. They use a λ-calculus augmented with state in order to prove the- orems similar to Theorem 2.9. They too distinguish between the execution environment code and applet code, similar to our use of principals, but they consider only the two-agent case and take a less syntactic approach. There has been much work on representation indepen- dence and parametric polymorphism, as pioneered by Stra- chey [20] and Reynolds [19]. Such notions have been in- corporated into programming languages such as SML and Haskell [10, 15] and studied extensively in Girard’s system F [5]. Abadi, Cardelli, and Curien have taken a syntactic ap- proach to parametricity by formalizing the logical relations arguments used in such proofs [1]. More recently, Crary has proposed the use of singleton types as a means of proving parametricity results without resorting to the construction of models [4]. Pierce and Sangiori [16] prove parametricity results for a polymorphic pi-calculus in an operational setting. Rather than add principals to the term language, they use exter- nal substitutions to reason about bisimilarity of polymor- phic processes in which there are both abstract and concrete views of data values. None of the above work (except Leroy and Rouaix’s) explicitly involves the notion of principal. Our syntactic separation of agents is similar to Nielson and Nielson’s Two- Level λ-calculus [14]. There they are concerned with binding time analysis, so the two principals’ code is inherently not mixed during evaluation. A notion of principal also arises in the study of language based security, where privileged agents may not leak information to unprivileged ones. See, for example, Heinze and Riecke’s work on the SLam calculus [8] or Volpano and Smith’s work on type-based security [21]. A more syntactic approach to security is taken by Myers [13].

6 Conclusion

We have created a multiagent calculus in which the notion of principal is made explicit in the language. This syntac- tic distinction allows us to track agent code during evalua- tion, giving syntactic proofs of interesting type abstraction properties. Our hope is that these techniques will scale to realistic, hard to model languages.

7 Acknowledgments

We thank David Walker, Stephanie Weirich, and the anony- mous referees for their helpful comments on earlier drafts of this paper.

References

[1] Mart´in Abadi, Luca Cardelli, and Pierre-Louis Curien. For- mal parametric polymorphism. In Principles of Program- ming Languages, volume 20, pages 157–167, January 1993. [2] Godmar Back, Patrick Tullmann, Leigh Stoller, Wilson C. Hseih, and Jay Lepreau. Java operating systems: Design and implementation. Technical Report UUCS-98-015, University of Utah, August 1998.

[3] Brian N. Bershad, Stefan Savage, Przemyslaw Pardyak, Emin G¨un Sirer, Marc Fiuczynski, David Becker, Susan Eg- gers, and Craig Chambers. Extensibility, safety and perfor- mance in the SPIN operating system. In Proceedings of the Fifteenth ACM Symposium on Operating Systems Princi- ples, pages 267–283, Copper Mountain, CO, December 1995. [4] Karl Crary. A simple proof technique for certain parametric- ity results. In Proceedings of the 4th ACM SIGPLAN In- ternational Conference on Functional Programming, Paris, France, September 1999. This volume. [5] Jean-Yves Girard, Y. Lafont, and P. Taylor. Proofs and Types. Cambridge University Press, 1989. [6] Michael Godfrey, Tobias Mayr, Praveen Seshadri, and Thorsten von Eicken. Secure and portable database exten- sibility. In Proceedings of the 1997 ACM-SIGMOD Confer- ence on the Management of Data, pages 390–401, Seattle, WA, June 1998. [7] Chris Hawblitzel, Chi-Chao Chang, Grzegorz Czajkowski, Deyu Hu, and Thorston von Eiken. Implementing multi- ple protection domains in Java. In 1998 USENIX Annual Technical Conference, New Orleans, LA, June 1998. [8] Nevin Heintze and Jon G. Riecke. The SLam calculus: pro- gramming with secrecy and integrity. In Conference Record of the Twenty-Fifth Annual ACM Symposium on Princi- ples of Programming Languages, pages 365–377. ACM Press,

[9] Xavier Leroy and Fran¸cois Rouaix. Security properties of typed applets. In Principles of Programming Languages, January 1998. [10] Robin Milner, Mads Tofte, Robert Harper, and David Mac- Queen. The Definition of Standard ML (Revised). The MIT Press, 1997. [11] J.C. Mitchell. On the equivalence of data representations. In V. Lifschitz, editor, Artificial Intelligence and Mathemat- ical Theory of Computation: Papers in Honor of John Mc- Carthy, pages 305–330. Academic Press, 1991. [12] John C. Mitchell and Gordon D. Plotkin. Abstract types have existential type. ACM Transactions on Programming Languages and Systems, 10(3):470–502, July 1988. [13] Andrew C. Myers. JFlow: Practical mostly-static informa- tion flow control. In Proceedings of the 26th ACM Sympo- sium on Principles of Programming Languages, pages 228– 241, San Antonio, TX, January 1999. [14] Flemming Nielson and Hanne Riis Nielson. Two-Level Func- tional Languages. Number 34 in Cambridge Tracts in Theo- retical Computer Science. Cambridge University Press, 1992. [15] John Peterson, Kevin Hammond, Lennart Augustsson, Brian Boutel, Warren Burton, Joseph Fasel, Andrew D. Gor- don, John Hughes, Paul Hudak, Thomas Johnsson, Mark Jones, Erik Meijer, Simon Peyton Jones, Alastair Reid, and Philip Wadler. Report on the programming language Haskell, version 1.4. http://www.haskell.org/report. [16] Benjamin C. Pierce and Davide Sangiorgi. Behavioral equiv- alence in the polymorphic pi-calculus. Technical Report MS- CIS-99-10, University of Pennsylvania, April 1999. [17] John Hamilton Reppy. Higher-order Concurrency. PhD the- sis, Cornell University, Ithaca, NY, June 1992. TR 92-1852. [18] John C. Reynolds. Towards a theory of type structure. In Programming Symposium, volume 19 of Lecture Notes in Computer Science, pages 408–425. Springer-Verlag, Paris, France, April 1974. [19] John C. Reynolds. Types, abstraction, and parametric poly- morphism. In R.E.A Mason, editor, Information Processing, pages 513–523. Elsevier Science Publishers B.V., 1983.

[20] C. Strachey. Fundamental concepts in programming lan- guages. Unpublished Lecture Notes, Summer School in Com- puter Programming, August 1967.

[21] Dennis Volpano and Geoffrey Smith. A type-based approach to program security. In Proceedings of TAPSOFT ’97, Collo- quium on Formal Approaches to Software Engineering, Lille, France, April 1997.

[22] Dan S. Wallach and Edward W. Felten. Understanding Java stack inspection. In Proceedings of 1998 IEEE Symposium on Security and Privacy, Oakland, CA, May 1998.

[23] Dan Seth Wallach. A New Approach to Mobile Code Secu- rity. PhD thesis, Princeton University, 1999.

[24] Andrew K. Wright and Mattias Felleisen. A syntactic ap- proach to type soundness. Information and Computation, 115(1):38–94, 1994. Preliminary version in Rice TR 91-160.

[25] Steve Zdancewic and Dan Grossman. Syntax and semantics for multiple agents and abstract types. Technical Report 99-1752, Cornell University, March 1999.

A Appendix

We present the most interesting parts of the proof of the Preservation Lemma for the multiagent calculus. We will use the following lemmas; the proofs are in the companion technical report [25].

Lemma A.1 (Idempotency) For all agents i, and (possibly empty) lists and′, {∆} τ .:i:i:′ τ ′^ iff {∆} τ .:i:′ τ ′

Lemma A.2 (Reversal) If {∆} τ 0 .i τ 1 then {∆} τ 1 .rev(i) τ 0

Lemma A.3 (Arrow Type) If {∆} τ 0 → τ 1 .i τ 2 → τ 3 then {∆} τ 0 .i τ 2 and {∆} τ 1 .i τ 3.

Lemma A.4 (Substitution) For all agents i and j, if {∆}; Γ[xj : τ ′] i ei : τ and {∆}; Γj ej : τ ′, then {∆}; Γ `i {ej /xj }ei : τ.

We recall the statement of Preservation: If {∆}; ∅ `i ei :

τ and ei i 7 −→ e′ i then {∆}; ∅ `i e′ i : τ. The proof is by

induction on the derivation that ei i 7 −→ e′ i and is by cases on the last rule in the derivation. For formatting reasons, we omit the {∆} antecedents throughout. Similarly, an omitted environment should be interpreted as ∅. Here are the cases for the rules peculiar to our calculus:

(6) Trivial.

(7) Since ei typechecks, we must have a derivation as fol- lows:

j vˆj : τ 0 τ 0 .j :i ui px ˆvj qyu`j : ∆¯i(u)

Since ∆¯i(u) equals ∆¯i( ¯∆i(u)), we have via the (eq) rule that u .i ∆¯i(u). Thus by (trans) it follows that τ 0 .`j :i:i ∆¯i(u) and we can remove the second i by Idempotency. We can now derive:

j vˆj : τ 0 τ 0 .j :i ∆¯i(u)i px vˆj qy ∆¯i (u) `j : ¯∆i(u)

The conclusion is the desired result.

(8) Since ei typechecks, we must have a derivation as fol- lows:

j vˆj : τ 0 τ 0 .j :k uk px vˆj qy u `j : ∆¯k(u)^

∆¯k(u) .k :i τ

i pxpx ˆvj qy uj qy

τ `k : ∆¯i(τ^ )

Furthermore, u 6 ∈ ∆k, so ∆¯k (u) = u. Thus by (trans) and the premises, we have τ 0 .j :k:k:i τ. And sincek begins with agent k, Idempotency proves that τ 0 .j :`k :i τ , which yields the following derivation:

j ˆvj : τ 0 τ 0 .j :k :i τ i px ˆvj qy τj :`k : ∆¯i(τ^ )

The conclusion is the desired result.

(9) Since ei typechecks, we must have a derivation as fol- lows:

[xj : τ 0 ] j ej : τ 3 ∆j (τ 0 ) = τ 0j λxj :τ 0. ej : τ 0 → τ 3

j λxj :τ 0. ej : τ 0 → τ 3 τ 0 → τ 3 .`j :i τ 1 → τ 2

`i pxλxj :τ 0. ej qyτ^

(^1) →τ 2 `j : ∆¯i(τ^

(^1) → τ 2 )

Furthermore, ∆i(τ 1 → τ 2 ) = τ 1 → τ 2 , so we know from the definition of ∆i that ∆¯i(τ 1 ) = ∆i(τ 1 ) = τ 1 and similarly for τ 2. From the Arrow Type Lemma and the right hand premise of the bottom step, we conclude that τ 0 .j :i τ 1 and τ 3 .j :i τ 2. Now notice that the reverse of j : i is i : rev(j ) which we shall write ′: j. So by the Reversal Lemma, τ 1 .`′:j τ 0. Thus we can derive:

[xi : τ 1 ] i xi : τ 1 τ 1 .`′:j τ 0

[xi : τ 1 ] j pxxiqy τ 0′^ : ∆¯j (τ^

0 )

In fact, [xi : τ 1 ] `j pxxiqyτ^

0 `′^ :^ τ^

(^0) since the original deriva- tion above provides ∆j (τ 0 ) = τ 0. It also provides [xj : τ 0 ] j ej : τ 3. Since xi is fresh, we may con- clude [xi : τ 1 ][xj : τ 0 ]j ej : τ 3. So by Substitution, [xi : τ 1 ] `j {pxxiqyτ^

0 `′^ /xj^ }ej^ :^ τ^3. Thus we can derive:

[xi : τ 1 ] `j {pxxiqyτ^

0 ′^ /xj^ }ej^ :^ τ^3^ τ^

`j :i τ^ 2

[xi : τ 1 ] `i px{pxxiqyτ^

0 `′^ /xj^ }ej^ qy

τ 2 `j : ∆¯i(τ^

[xi : τ 1 ] `i px{pxxiqyτ^

0 `′^ /xj^ }ej^ qy

τ 2 `j : ∆¯i(τ^

i(τ^ (^1) ) = τ 1

`i λxi:τ 1. px{pxxiqyτ^

0 `′^ /xj^ }ej^ qy

τ 2 `j :^ τ^

i(τ^

Since ∆¯i(τ 2 ) = τ 2 , the conclusion is the desired result.