practical foundations for programming languages || symbol

7
Cambridge Books Online http://ebooks.cambridge.org/ Practical Foundations for Programming Languages Robert Harper Book DOI: http://dx.doi.org/10.1017/CBO9781139342131 Online ISBN: 9781139342131 Hardback ISBN: 9781107029576 Chapter 32 - Symbol pp. 263-268 Chapter DOI: http://dx.doi.org/10.1017/CBO9781139342131.033 Cambridge University Press

Upload: robert

Post on 19-Dec-2016

216 views

Category:

Documents


2 download

TRANSCRIPT

Cambridge Books Online

http://ebooks.cambridge.org/

Practical Foundations for Programming Languages

Robert Harper

Book DOI: http://dx.doi.org/10.1017/CBO9781139342131

Online ISBN: 9781139342131

Hardback ISBN: 9781107029576

Chapter

32 - Symbol pp. 263-268

Chapter DOI: http://dx.doi.org/10.1017/CBO9781139342131.033

Cambridge University Press

32 Symbols

A symbol is an atomic datum with no internal structure. Whereas a variable is given meaningby substitution, a symbol is given meaning by a family of operations indexed by symbols.A symbol is therefore just a name, or index, for an instance of a family of operations.Many different interpretations may be given to symbols according to the operations wechoose to consider, giving rise to concepts such as fluid binding, dynamic classification,mutable storage, and communication channels. With each symbol is associated a typewhose interpretation depends on the particular application. The type of a symbol influencesthe type of its associated operations under each interpretation. For example, in the caseof mutable storage, the type of symbol constrains the contents of the cell named by thatsymbol to values of that type. It is important to bear in mind that a symbol is not a valueof its associated type, but only a constraint on how that symbol may be interpreted by theoperations associated with it.

In this chapter we consider two constructs for computing with symbols. The first isa means of declaring new symbols for use within a specified scope. The expresssionν a:ρ in e introduces a “new” symbol a with associated type ρ for use within e. Thedeclared symbol a is new in the sense that it is bound by the declaration within e, andso may be renamed at will to ensure that it differs from any finite set of active symbols.Whereas the statics determines the scope of a declared symbol, its range of significance,or extent, is determined by the dynamics. There are two different dynamic interpretationsof symbols, the scoped and the free (short for scope–free) dynamics. The scoped dynamicslimits the extent of the symbol to its scope; the lifetime of the symbol is restricted tothe evaluation of its scope. Alternatively, under the free dynamics the extent of a symbolexceeds its scope, extending to the entire computation of which it is a part. We may saythat in the free dynamics a symbol “escapes its scope,” but it is more accurate to say thatits scope widens to encompass the rest of the computation.

The second construct associated with symbols is the concept of a symbolic reference,a form of expression whose sole purpose is to refer to a particular symbol. Symbolicreferences are values of a type ρ sym and have the form & a for some symbol a withassociated type ρ. The eliminatory form for the type ρ sym is a conditional branch thatdetermines whether a symbolic reference refers to a statically specified symbol. Crucially,the statics of the eliminatory form is carefully designed so that, in the positive case, thetype associated with the referenced symbol is made manifest, whereas in the negative case,no type information is gleaned because the referenced symbol could be of any type.

Downloaded from Cambridge Books Online by IP 216.165.126.139 on Sat Nov 30 10:32:06 WET 2013.http://dx.doi.org/10.1017/CBO9781139342131.033

Cambridge Books Online © Cambridge University Press, 2013

264 Symbols

32.1 Symbol Declaration

The ability to declare a new symbol is shared by all applications of symbols in subsequentchapters. The syntax for symbol declaration is given by the following grammar:

Sort Abstract Form Concrete Form DescriptionExp e ::= new[τ](a.e) ν a:τ in e generation

The statics of symbol declaration makes use of a signature, or symbol context, that associatesa type with each of a finite set of symbols. We use the letter � to range over signatures,which are finite sets of pairs a∼τ , where a is a symbol and τ is a type. The typing judgment� �� e : τ is parameterized by a signature �, associating types with symbols.

The statics of symbol declaration makes use of a judgment τ mobile, whose definitiondepends on whether the dynamics is scoped. In a scoped dynamics, mobility is defined sothat the computed value of a mobile type cannot depend on any symbol. By constrainingthe scope of a declaration to have mobile type, we can, under this interpretation, ensurethat the extent of a symbol is confined to its scope. In a free dynamics, every type isdeemed mobile, because the dynamics ensures that the scope of a symbol is widened toaccommodate the possibility that the value returned from the scope of a declaration maydepend on the declared symbol. The term “mobile” reflects the informal idea that symbolsmay or may not be “moved” from the scope of their declaration according to the dynamicsgiven to them. A free dynamics allows symbols to be moved freely, whereas a scopeddynamics limits their range of motion.

The statics of symbol declaration itself is given by the following rule:

� ��,a∼ρ e : τ τ mobile

� �� new[ρ](a.e) : τ. (32.1)

As mentioned, the condition on τ is to be chosen so as to ensure that the returned value ismeaningful in which dynamics we are using.

32.1.1 Scoped Dynamics

The scoped dynamics of symbol declaration is given by a transition judgment of the forme −→�

e′ indexed by a signature �, specifying the active symbols of the transition. Either e

or e′ may involve the symbols declared in �, but no others:

e −−−→�,a∼ρ

e′

new[ρ](a.e) −→�

new[ρ](a.e′)(32.2a)

e val�new[ρ](a.e) −→

�e. (32.2b)

Rule (32.2a) specifies that evaluation takes place within the scope of the declaration of asymbol. Rule (32.2b) specifies that the declared symbol is “forgotten” once its scope hasbeen evaluated.

Downloaded from Cambridge Books Online by IP 216.165.126.139 on Sat Nov 30 10:32:06 WET 2013.http://dx.doi.org/10.1017/CBO9781139342131.033

Cambridge Books Online © Cambridge University Press, 2013

265 32.1 Symbol Declaration

The definition of the judgment τ mobile must be chosen to ensure that the followingmobility condition is satisfied:

If τ mobile, ��,a∼ρ e : τ , and e val�,a∼ρ , then �� e : τ and e val� .

For example, in the presence of symbolic references (see Section 32.2), a function typecannot be deemed mobile, because a function may contain a reference to a local symbol. Thetype nat may be deemed mobile only if the successor is evaluated eagerly, for otherwise asymbolic reference may occur within a value of this type, invalidating the condition.

Theorem 32.1 (Preservation). If �� e : τ and e −→�

e′, then �� e′ : τ .

Proof By induction on the dynamics of symbol declaration. Rule (32.2a) follows directlyby induction, applying Rule (32.1). Rule (32.2b) follows directly from the condition onmobility.

Theorem 32.2 (Progress). If �� e : τ , then either e −→�

e′, or e val� .

Proof There is only one rule to consider, Rule (32.1). By induction we have eithere −−−→

�,a∼ρe′, in which case Rule (32.2a) applies, or e val�,a∼ρ , in which case by the

mobility condition we have e val� , and hence Rule (32.2b) applies.

32.1.2 Scope-Free Dynamics

The scope–free dynamics of symbols is defined by a transition system between states ofthe form ν � { e }, where � is a signature and e is an expression over this signature. Thejudgment ν � { e } → ν �′ { e′ } states that evaluation of e relative to symbols � results inthe expression e′ in the extension �′ of �:

ν � { new[ρ](a.e) } → ν �, a ∼ ρ { e } . (32.3)

Rule (32.3) specifies that symbol generation enriches the signature with the newly intro-duced symbol by extending the signature for all future transitions.

All other rules of the dynamics must be changed accordingly to account for the allocatedsymbols. For example, the dynamics of function application cannot simply be inheritedfrom Chapter 10, but must be reformulated as follows:

ν � { e1 } → ν �′ { e′1 }ν � { e1(e2) } → ν �′ { e′1(e2) } (32.4a)

ν � { λ (x:τ) e(e2) } → ν � { [e2/x]e } . (32.4b)

These rules shuffle around the signature so as to account for symbol declarations withinthe constituent expressions of the application. Similar rules must be given for all otherconstructs of the language.

Downloaded from Cambridge Books Online by IP 216.165.126.139 on Sat Nov 30 10:32:06 WET 2013.http://dx.doi.org/10.1017/CBO9781139342131.033

Cambridge Books Online © Cambridge University Press, 2013

266 Symbols

Theorem 32.3 (Preservation). If ν � { e } → ν �′ { e′ } and �� e : τ , then �′ ⊇ � and��′ e′ : τ .

Proof There is only one rule to consider, Rule (32.3), which is easily handled by inversionof Rule (32.1).

Theorem 32.4 (Progress). If �� e : τ , then either e val� or ν � { e } → ν �′ { e′ } for some�′ and e′.

Proof Immediate, by Rule (32.3).

32.2 Symbolic References

Symbols are not themselves values, but they may be used to form values. One usefulexample is provided by the type τ sym of symbolic references. A value of this type hasthe form & a, where a is a symbol in the signature. To compute with a reference we maybranch according to whether it is a reference to a specified symbol. The syntax of symbolicreferences is given by the following chart:

Sort Abstract Form Concrete Form DescriptionTyp τ ::= sym(τ) τ sym symbolsExp e sym[a] & a reference

is[a][t.τ](e; e1; e2) if e is a then e1 ow e2 comparison

The expression sym[a] is a reference to the symbol a, a value of type sym(τ). Theexpression is[a][t.τ](e; e1; e2) compares the value of e, which must be a referenceto some symbol b with the given symbol a. If b is a, the expression evaluates to e1 andotherwise to e2.

32.2.1 Statics

The typing rules for symbolic references are as follows:

� ��,a∼ρ sym[a] : sym(ρ)(32.5a)

� ��,a∼ρ e : sym(ρ ′) � ��,a∼ρ e1 : [ρ/t]τ � ��,a∼ρ e2 : [ρ ′/t]τ� ��,a∼ρ is[a][t.τ](e; e1; e2) : [ρ ′/t]τ

. (32.5b)

Rule (32.5a) is the introduction rule for the type sym(ρ). It states that if a is a symbolwith associated type ρ, then sym[a] is an expression of type sym(ρ). Rule (32.5b) isthe elimination rule for the type sym(ρ). The type associated with the given symbol ais not required to be the same as the type of the symbol referred to by the expression e.

Downloaded from Cambridge Books Online by IP 216.165.126.139 on Sat Nov 30 10:32:06 WET 2013.http://dx.doi.org/10.1017/CBO9781139342131.033

Cambridge Books Online © Cambridge University Press, 2013

267 32.2 Symbolic References

If e evaluates to a reference to a, then these types will coincide, but if it refers to anothersymbol, b �= a, then these types may well differ.

With this in mind, let us examine carefully Rule (32.5b). A priori there is a discrepancybetween the type ρ of a and the type ρ ′ of the symbol referred to by e. This discrepancyis mediated by the type operator t.τ .1 Regardless of the outcome of the comparison, theoverall type of the expression is [ρ ′/t]τ . To ensure safety, we must ensure that this is a validtype for the result, regardless of whether the comparison succeeds or fails. If e evaluatesto the symbol a, then we “learn” that the types ρ ′ and ρ coincide, because the specifiedand referenced symbol coincide. This is reflected by the type [ρ/t]τ for e1. If e evaluatesto some other symbol, a′ �= a, then the comparison evaluates to e2, which is required tohave type [ρ ′/t]τ ; no further information about the type of the symbol is acquired in thisbranch.

32.2.2 Dynamics

The (scoped) dynamics of symbolic references is given by the following rules:

sym[a] val�,a∼ρ(32.6a)

is[a][t.τ](sym[a]; e1; e2) −−−→�,a∼ρ

e1(32.6b)

(a �= a′)is[a][t.τ](sym[a′]; e1; e2) −−−−−−−→

�,a∼ρ,a′∼ρ ′e2

(32.6c)

e −−−→�,a∼ρ

e′

is[a][t.τ](e; e1; e2) −−−→�,a∼ρ

is[a][t.τ](e′; e1; e2). (32.6d)

Rules (32.6b) and (32.6c) specify that is[a][t.τ](e; e1; e2) branches according towhether the value of e is a reference to the symbol a.

32.2.3 Safety

To ensure that the mobility condition is satisfied, it is important that symbolic referencetypes not be deemed mobile.

Theorem 32.5 (Preservation). If �� e : τ and e −→�

e′, then �� e′ : τ .

Proof By rule induction on Rules (32.6). The most interesting case is Rule (32.6b). Whenthe comparison is positive, the types ρ and ρ ′ must be the same, because each symbol hasat most one associated type. Therefore e1, which has type [ρ ′/t]τ , also has type [ρ/t]τ , asrequired.

1 See Chapter 14 for a discussion of type operators.

Downloaded from Cambridge Books Online by IP 216.165.126.139 on Sat Nov 30 10:32:06 WET 2013.http://dx.doi.org/10.1017/CBO9781139342131.033

Cambridge Books Online © Cambridge University Press, 2013

268 Symbols

Lemma 32.6 (Canonical Forms). If �� e : sym(ρ) and e val� , then e = sym[a] for somea such that � = �′, a ∼ ρ.

Proof By rule induction on Rules (32.5), taking account of the definition of values.

Theorem 32.7 (Progress). Suppose that �� e : τ . Then either e val� or there exists e′ suchthat e −→

�e′.

Proof By rule induction on Rules (32.5). For example, consider Rule (32.5b), in whichwe have that is[a][t.τ](e; e1; e2) has some type τ and that e : sym(ρ) for some ρ.By induction either Rule (32.6d) applies or else we have that e val� , in which case weare assured by Lemma 32.6 that e is sym[a] for some symbol b of type ρ declared in �.But then progress is ensured by Rules (32.6b) and (32.6c), because equality of symbols isdecidable (either a is b or it is not).

32.3 Notes

The concept of a symbol in a programming language was considered by McCarthy inthe original formulation of Lisp (McCarthy, 1965). Unfortunately, symbols were, andoften continue to be, confused with variables, as they were in the original formulationof Lisp. Although symbols are frequently encountered in dynamically typed languages,the formulation given here makes clear that they are equally sensible in statically typedlanguages. The present account was influenced by the work of Pitts and Stark (1993) andon the declaration of names in the π -calculus (Milner, 1999).

Downloaded from Cambridge Books Online by IP 216.165.126.139 on Sat Nov 30 10:32:06 WET 2013.http://dx.doi.org/10.1017/CBO9781139342131.033

Cambridge Books Online © Cambridge University Press, 2013