Submitted for publication in Information and Computation Academic Press

The Typ e and Eect Discipline

JeanPierre Talpin

And

Pierre Jouvelot

Centre de Recherche en Informatique

Ecole des Mines de Paris

rue Saint Honore F Fontainebleau

EMail talpinensm p fr and jouveloten smp f r

Novemb er th

Current Address JeanPierre Talpin Europ ean ComputerIndustry ResearchCenter ECRCGmbH

Arab ella Strae D Munc hen Email jpecrcde

Abstract

The type and eect discipline is a new framework for reconstructing the principal typ e and

the minimal eect of expressions in implicitly typ ed p olymorphic functional languages that

supp ort imp erative constructs The typ e and eect discipline outp erforms other p olymorphic

typ e systems Just as typ es abstract collections of concrete values eects denote imp erative

op erations on regions Regions abstract sets of p ossibly aliased memory lo cations

Eects are used to control typ e generalization in the presence of imp erative constructs

while regions delimit observable sideeects The observable eects of an expression range

over the regions that are free in its typ e environmentanditstyp e eects related to lo cal

data structures can b e discarded during typ e reconstruction The typ e of an expression can

b e generalized with resp ect to the typ e variables that are not free in the typ e environment

or in the observable eect

Intro ducing the typ e and eect discipline we dene b oth a dynamic and a static se

mantics for an MLlike language and prove that they are consistently related We presenta

reconstruction algorithm that computes the principal typ e and the minimal observable eect

of expressions Weprove its correctness with resp ect to the static semantics

Contents

Intro duction

Related Work

A Core Language and its Semantics

Syntax

Store op erations

Formulation of the Dynamic Semantics

Semantic Ob jects

Axioms and Rules of the Dynamic Semantics

Dynamic Semantics of Store Op erations

Static Semantics

Free Variables and Substitutions

Typ e Schemes and Environments

Typ e Generalization

Rules of the Static Semantics

Static Semantics of Store Op erations

Observation Criterion

Formal Prop erties of the Static Semantics

Consistency of Dynamic and Static Semantics

The Reconstruction Algorithm

Constrained Typ e Schemes

Constrained Typ e Schemes of Store Op erations

The Reconstruction Algorithm

Constraint Resolution

WellFormed Constraint Sets

Unication Algorithm

Correctness of the Reconstruction Algorithm

Examples

Comparison with the Related Work

Comparative Examples

Benchmarks

Extensions

Conclusion

Intro duction

Typ e inference Milner is the pro cess that automatically reconstructs the typ e of

expressions in programming languages Polymorphic typ e inference in functional languages

b ecomes problematic in the presence of imp erative constructs and muchinvestigations have

b een devoted to this issue Tofte Leroy Weis Wright

Eect systems Giord al Lucassen Lucassen Giord aim at a

safe integration of imp erative programming features in functional languages Just as typ es

describ e what expressions compute eects describ e how expressions compute and b oth can

b e statically reconstructed Jouvelot Giord Talpin Jouvelot Sept

Weintro duce the type and eect discipline a new framework for reconstructing the prin

cipal typ e and the minimal eect of expressions in implicitly typ ed p olymorphic functional

languages that supp ort imp erative constructs Just as typ es structurally abstract collections

of concrete values regions abstract sets of p ossibly aliased memory lo cations while eects

denote imp erative op erations on regions Eects control typ e generalization in the presence

of imp erative constructs while regions are used to rep ort their only observable sideeects

The observable eects of an expression range over the regions that are free in its typ e

environment and its typ e Eects related to lo cal data structures can b e discarded during

typ e reconstruction The typ e of an expression can b e generalized with resp ect to the

variables that are neither free in the typ e environment nor in the observable eect

In this pap er section presents the related work Then we describ e the dynamic sec

tion and static sections and semantics of the language We state that the static

and dynamic semantics are consistent section and that our reconstruction algorithm

presented in sections and is correct with resp ect to the static semantics section We

e some examples sections and that show that our approach surpasses previous giv

techniques b efore suggesting some extensions section and concluding section

Related Work

Short of the adho c techniques used in the rst typ e inference systems the imp erativetyp e

discipline Tofte is the classical way to deal with the problem of typ e generalization for

p olymorphic functional languages in the presence of non referentially transparent constructs

Its extension based on weak typ e variables is used in the implementation of Standard

ML App el Mac Queen A dierent approach suggested in LeroyWeis

consists in lab eling the typeofeach function with the set of the typ es of the value identiers

that o ccur in its b o dy and then to trackthedangerous typ e variables of references

All those approaches build conservative approximations of value typ es that may b e ac

cessible from the global store by relating the typing of references to syntactic information

A more intuitive and integrated approach is to infer a more semantically meaningful infor

mation by recording and and keeping track of the typ es of values referenced in the store as

so on as the store is expanded and as long as its lo cations are used The quest for sucha typ e

system has for long b een the sub ject of manyinvestigations Damas OTo ole

Wright Eect inference allows us to approximate the store by regions and typ es and

as such can b e used to decide when to p erform typ e generalization

The FX system Lucassen Giord Giord al suggests a static seman

tics for p olymorphic typ e and eect checking In Jouvelot Giord the authors

show that eect reconstruction can b e seen as a constraint satisfaction problem However

the exact matching of eects required by the static semantics together with the use of explicit

p olymorphism imply the nonexistence of syntactic principal typ es it also somewhat limits

the kind of accepted programs We presentinTalpin Jouvelot Sept an algorithm

that computes the maximal typ e and the minimal eect of expressions using subsumption

on eects to overcome this particular problem of eect matching

In the type and eect disciplinewe apply the technique of typ e region and eect inference

to the problem of typing references in MLlike languages in the presence of p olymorphic let

constructs We determine the principal typ e and the minimal observable eects of expres

sions We use eect information to p erform typ e generalization By using eect information

together with an observation criterion our typ e system is able to precisely delimit the scop e

let expres of sideeecting op erations thus allowing typ e generalization to b e p erformed in

sions in a more ecient and uniform way than previous systems It is shown with some simple

examples section that our system improves over earlier typ e generalization p olicies for

MLlike languages

A Core Language and its Semantics

Reasoning on the complete denition of a functional language such as Standard ML or FX

would have b een complex and tedious In order to simplify the presentation and to ease

the formal reasoning this section intro duces a core language It is an attempt to trade

between integrating the principal features of functional and imp erative programming and

b eing simple This section intro duces its syntax and its dynamic semantics together with a

series of conventions and notations that are used in this pap er

Syntax

The expressions of the language written e p ossibly with a prime or a subscript are the ele

ments of the term algebra Exp generated by the grammar describ ed b elow It uses enclosing

parentheses in the reminiscence of Scheme Rees al

e x j value identier

op e j op eration

e e j application

lambda x e j abstraction

let x e e lexical value binding

op new j get j set op erations on references

Syntax

In this grammar x and f range over a countable set of identiers The form e e stands

for the application of a function e to an argument e The form op e applies the primitive

op eration op to the argument e The expression lambda x e is the socalled lamb da

abstraction that denes the rstclass function whose parameter is x and whose result is the

value of e

Store op erations

The arithmetic op erations over integers and the b o olean op erations and and or

or even the if construct are typically represented by op erators op b ecause their meaning

cannot b e explained easily by abstractions and applications Store op erations can also b e

dened by op erators They op erate on reference values which are indirection cells that can

b e dynamically allo cated read and written in place

The op eration new e initializes a fresh reference to the value of the expression eThe

op eration get e gets the value referenced by the p ointer returned by e The op eration

set e e mo dies the content of the reference returned by e and sets it to the value of

e We use the convention that set returns the unit value u

Formulation of the Dynamic Semantics

In this section we dene the dynamic semantics of our language The dynamic semantics

sp ecies the meaning of expressions It is dened byanevaluation mechanism that relates

expressions to values To express this relation we use the formalism of relational semantics

Plotkin Kahn It consists of a predicate b etween expressions and values dened

by a set of axioms and inference rules called evaluation judgements An evaluation judgment

tells whether an expression evaluates to a given result

Semantic Ob jects

We present the semantics ob jects on which the predicate of evaluation is dened These

semantic ob jects include values environments stores and traces

Values are either the command value u reference values l or closures c A closure x eE

is comp osed of a value identier x its formal parameter an expression e its b o dy and the

environment E where it is dened

An environment E is represented by a nite map from identiers to values In an envi

ronment E we assume that all identiers are distinct The empty mapping is written fg

The domain of the mapping E is written DomE anditsrange Im E If x b elongs to

DomE we write E x for the value asso ciated with x in E Finallywe write E for the

x

exclusion of x from E and E fx v g for the extension of E to the mapping of x to v

x

v Value fugRefClosure values

c Closure IdExpEnv closures

l Ref lo cations

n

E Env Id Value environments

n

s Store Ref Value stores

Values Environments Stores and Traces

The presence of references requires the intro duction of a notion of state in the dynamic

semantics the store The store changes during the evaluation of a program and it tells the

currentcontents of all initialized references We assume that we are given a countable set

Ref of lo cations l Then a store s is represented by a nite map from references or lo cations

in Ref to values Thus we use for stores the same notations than for environments

Axioms and Rules of the Dynamic Semantics

We present in the vein of Tofte Milner the set of rules that inductively denes

the predicate of evaluation s E e v s on the structure of expressions Given a store

s and an environment E the predicate s E e v s asso ciates each expression e with a

value v and a new store s

s E x E xs var

s E lambda x e x eE s abs

x

s E e v s s E fx v g e v s

x

let

s E let x e e v s

s E e x e E s s E e v s s E fx v ge v s

app

s E e e v s

Dynamic Semantics

The axiom var states that an identier x evaluates to the value E x b ound to it in

the environment E provided that this identier x b elongs to the domain of E Otherwise

the expression x has no meaning By the axiom abs a function denition evaluates to a

closure

As stated by the rule let a let binding evaluates the rst argument e toavalue v

binds it to the identier x and then evaluates its second argument e in the environment E

extended with fx v g The result v is the result of the let expression In the case that the

evaluation of the rst argument do es not succeed the evaluation of the let expression is not

dened

The rule of application app is more complex First the expression e must evaluate

to a closure x e E Then the argument e must evaluate to a value v Finallythe

function b o dy e must evaluate to a value v with the environmen t E captured in the

closure extended with the formal parameter x b ound to v

Dynamic Semantics of Store Op erations

Now we can give the relational semantics for the op erations on references The semantics

describ es how the store is mo died bytheevaluation of expressions

s E e v s l Doms

new

s E new e l s fl v g

s E e l s l Doms

get

s E get e s l s

s E e l s s E e v s

set

s E set e e u s fl v g

l

Dynamic Semantics of Store Op erations

The rule new of reference initialization rst evaluates the initial value v of the reference

and then picks a fresh lo cation l Thisvery step is nondeterministic but all choices of l are

equivalent mo dulo a renaming of the lo cations in s The second step is then to extend the

store with the binding of l to v and to return l as the value of the expression The rule get

evaluates its argument e to a lo cation l then returns the value v stored at this lo cation in

the store s Finallyby the rule set the assignment op erator evaluates its rst argument

e to a lo cation l and its second argument e toavalue v Then it up dates the store at the

lo cation l substituting the previous value by v Note that by denition of the rule new l

must b e in s when e evaluates to l

Static Semantics

In this section we present the static semantics of our language We are rst going to equip

the language with a typ e system Then we will give the inference rules of the static semantics

The rules of the static semantics asso ciate the expressions of the language with their typ e

and eect in the same way as the rules of the dynamic semantics asso ciate expressions with

values We b egin by dening the term algebra for the three kinds of semantic ob jects

regions eects and typ es

r j regions

jinit j read j write j j eects

unit j j ref j typ es

Static Semantics Ob jects

The domain of regions is the disjoint union of a countable set of constants r and

variables Every lo cation corresp onds to a given region in the static semantics A region

abstracts the memory lo cations that will b e initialized at a given program p ointatruntime

Eects can either b e the constant that represents the absence of eects eect

variables or store eects init read andwrite that approximate memory

sideeects on the region of references to values of typ e

The eect init which could also b e named al loc statically records the allo cation

of a reference in a region and its initialization to a value of typ e The eects read

and write keep trackofhow and when the references of a given region are used

We dene the range of an eect written Rng is the set of pairs such that

ead orwrite isin We write Regs the set of regions such either init r

that is in the range of

Eects can b e gathered together with the inx op erator that denotes the union of ef

fects eects dene a set algebra The equality on eects is thus dened mo dulo asso ciativity

commutativity and idemp otence with as the neutral element We dene the setinclusive

relation of subsumption on eects or if and only if there exists an eect

such that

The domain of typ es is comp osed of the constant unit which denotes the typeofthe

trivial value of commands in ML likethetyp e named comm in Algol typ e variables

function typ es from to reference typ es ref in region to values of typ e

with a latent eect The latent eect of a function encapsulates the sideeects of its b o dy

and is the eect incurred when the function is applied

Free Variables and Substitutions

Wehave dened three kinds of variables typ e variables region variables and eect variables

When it is not necessary to sp ecify if a variable represents a typ e a region or an eect we

note it v Also we adopt the vector notation v to represent sequences of terms suchas

sequences of variables v

We write fv for the set of free typ e region and eect variables in This denition

extends p ointwise to regions and eects The function fr is dened in a similar manner and

computes the set of region constants and variables free in typ e and eect terms

fv unit fv

fv fg

fv init fv fv

fvref fv fv

fvread fv fv

fv fv fv fv

fv write fv fv

fvr

fv fv fv

fv fg

Free Variables

Substitutions map typ e variables to typ es region variables to regions and eect

variables to eects We write for the comp osition of the substitution and so

that The identity is written Id

Typ e Schemes and Environments

We use typ e schemes intro duced by Milner to generically represent the dierent

typ es of an expression A typ e scheme v consists of a typ e whichisuniversally quantied

over a sequence v of typ e variables region variables and eect variables A typ e is an

instance of a typ e scheme v written v if the variables v can b e substituted by

some substitution so that

In the sequence v the variables are assumed to b e distinct and their order of o ccurrence

is not signicant When that sequence is emptywe do not distinguish from We

identify typ e schemes that dier only by a renaming of their quantied variables or that

dier bytheintro duction or elimination of quantied variables that are not free in the b o dy

of the typ e scheme

The context in which an expression is asso ciated with a typ e and an eect is represented

bya typ e environment E which maps value identiers to typ e schemes The denitions of

free variables and free regions are extended to typ e schemes by fv v fv n v and to

environments E by considering that a typ e variable is free in E ifandonlyifitisfreeinE x

for some value identier x in DomE Substitutions are also extended to typ e schemes v

by using alpharenaming of quantied variables in typ e schemes to avoid capture of b ound

variables

We extend substitutions to typ e schemes v v using the renaming

fv v g of the b ound variables v by fresh variables v which are not free in and The

substitution v of a variable v by is dened as v if v v and as v otherwise The image

E ofatyp e environment E by a substitution is dened by E x E x for every

x in DomE Our extension of substitution on typ e schemes and environments satises the

following lemma

Lemma Substitution and Instantiation If v then there exist and



v such that

  

Pro of By hyp othesis v By denition v v where the v

are neither free in nor where fv v g and where is dened by v for every

v fv n v By denition of there exists dened on v suchthat Let

fv v g and Wehave v Let the restriction of on fv n v

   

 

It veries that

 

Typ e Generalization

The generalization Gen E ofa typ e is p erformed at let b oundaries on some of the

typ e region and eect variables v that o ccur free in Avariable cannot b e generalized

when it is either free in the typ e environment E or present in the eect

GenE letv fv n fvE fv in v

The rst condition is common for purely functional languages Gordon al As

for the second just as typ es are b ound to identiers in the typ e environment typ es are

b ound to regions in the reconstructed eects Thus when these regions are observable from

the context ie in the typ e environment E or the typ e of the returned value those typ es

cannot b e generalized

Rules of the Static Semantics

The next gure summarizes the rules of our static semantics We formulate typ e and eect

inference byadeductive pro of system that assigns a typ e and an eect to every expression

of the language The con text in which an expressions is asso ciated with a typ e and an eect

is represented byatyp e environment E which maps value identiers to typ es Deductions

pro duce conclusions of the form Ee which are called typing judgments and read in

the typ e environment E the expression e has typ e and eect

Ex

var

Ex

Ee E fx Gen E ge

x

let

Elet x e e

E fx ge

x

abs

Elambda x e

Ee Ee

app

Ee e

Static Semantics

In the rules var and let the static semantics manipulates typ e schemes by using the

mechanism of generalization and instantiation sp ecied in the previous section The com

munication of the eects from a function denition to a function application is b est viewed

in the rules of abstraction abs and application app whichshow the interesting inter

playbetween typ es and eects Via the abstraction rule the eect of a lamb da abstraction

b o dy is put inside the function typ e while with the application rule this emb edded eect

is extracted from the function typ e to b e exercised at the p ointofcalleectsow from the

p oints where functions are dened to the p oints where they are used

Static Semantics of Store Op erations

The store op erations new get and set have b een dened by appropriate rules in the dynamic

semantics In the static semantics they are b est dened by using axioms

init  

Enew ref

read  

Eget ref

write  

Eset ref unit

Static Semantics for Imp erative Op erations

These three axioms sp ecify the typ es that can b e assigned to the identiers new get and

set that implement store op erations For instance the axiom for the identier new reads

for anyenvironment E typ e region and eect thetyp e of new is a function from

ob jects of typ e to references ref that has at least the eect init of initializing a

reference in the region The additional eects and are used here to allow p ossible

co ercions to b e p erformed

Observation Criterion

The latent eect of a function derives statically from the state transforming op erations that

the expression it abstracts p erforms when it is executed For example store op erations

comprise the initialization reading and writing of references which are approximated by

regions

Even if a value expression p erforms certain op erations on the store one may b e able to

detect that those op erations cannot interfere with other expressions This is the case when

the regions over which the eect ranges are unreferenced in the rest of the program If this

is the case then we shall mask eects which derive from those op erations

In our typ e and eect inference system the static determination of the lexical scop e of

data regions is implemented by an observation criterion The observation of eects consists

of selecting an eect that ranges over regions that refer to data accessible in the environment

of an expression or in its value The accessible data are abstracted by the free regions of the

yp e environmentandofthevalue typ e of the expression t

In the static semantics only sideeects that can aect the typing context of an expres

sion ie its typ e environment E and its value typ e are worth rep orting The other eects

of refer to lo cal references that are freshly created and not exp orted from the expression

e

Ee Observe E

sub

Ee

Observation and Subsumption Rule

The observation criterion is sp ecied by the rule sub which tells that an expression e has

any eect bigger than the observable eects that can b e inferred for it Observe E

is the set of observable eects of

Observe E finit read write j frE fr g

f j fvE fv g

Observable Eects

The observable eects are the eect variables that o ccur free in or E and the eects

of the form init read and write where o ccurs free in or in E We write

Observe for Observe fg and Observe for Observe unit unit The

function Observe has the following formal prop ertywhich is widely used in the rest of this

pap er

Lemma Observe and free variables If Observe then fr Regs n

and fr Regs n

Pro of By hyp othesis Observe By denition of Observe and for any

Regs n wehavethat fr fr Thus fr fr Regs n as

exp ected

With the lemmaweshow that observation is conserved under substitution in that

combinations of substitutions to the function Observe can b e compared with the application

of the function Observe to substituted terms

Lemma Observe and substitution Observe Observe for any

Pro of Let us write Observe We pro ceed by case analysis For every

and by denition of Observe wehave fv and thus For every init

wehave that fr resp ectively read and write and by denition of Observe

and thus fr This implies that init This proves that

Note however that the containment is not prop er An example where we do not have

int init int init unit and Observe Observe is ref



f g



Formal Prop erties of the Static Semantics

The lemma of substitution is used b oth in the pro of of consistency and in the pro ofs of

correctness for the reconstruction algorithm

Lemma Substitution If Ee then Ee for any substitution

Pro of The pro of is by induction on the typing derivation

Case of var By hyp othesis wehave Ex By denition of the rule var Ex

By the lemma there exists E x and suchthat Thus by denition of

the rule var Ex

Case of abs By hyp othesis wehave Elambda x e By denition of

the rule abs E fx ge By induction hyp othesis on e E fx g

x x

e for any substitution By denition of the rule abs Elambda x e

Case of app By hyp othesis Ee e By denition of the rule app

Ee and Ee By induction hyp othesis on e and e Ee

and Ee for any substitution By the denition of the rule app we

conclude that Ee e for any substitution

Case of let By hyp othesis Elet x e e By denition of the rule

 

let wehave

Ee and E fx Gen E ge

x  

Let v be Gen E For any substitution let us consider fresh v and dene

v v g By the denition of Genwehavethat E E and as the extension of with f

v

By denition of v v Thus

E fx Gen E g E fx Gen E g

x x

Using the induction hyp othesis on e with we get that Ee and thus

by denition of Ee By induction hyp othesis on e weget

E fx Gen E ge

x  

which is equivalentto E fx Gen E ge By denition of the

x  

rule let we can then conclude that Elet x e e



Case of sub By h yp othesis Ee By denition of the rule sub this requires

that there exists suchthat

Ee and Observe E

Let be any substitution By induction hyp othesis on the derivation wehave that

Ee

Let Observe E and n Let us dene the substitution that maps



eect variables in to and regions in Regs to fresh regions not free in E and

 

Using the lemma Regs fr frE fr Thus for the substitution



wehave

Ee



By denition of the rule sub

Ee Observe E



By denition of Observe Observe E Observe E Thus

 

Ee Observe E

By the rule sub Ee By denition of Observe Observe E

Since wehave and by the rule sub

Ee

Consistency of Dynamic and Static Semantics

We dene a consistency judgment s Sj v which relates values and typ es according to

a given store mo del S a store s and observable eects The notion of store mo del S is

dened b elow

Definition Store Model Astoremodel S is a nite mapping from locations l to

pairs of regions and types We say that S extends S written SvS if and only

if DomS DomS and for every l DomS S l S l

In the dynamic semantics when an expression is evaluated its initial store s p ossibly

mutates to another s Similarly in the static semantics the observable eects that

corresp ond to the construction of the initial store s maybeaugmented with the eect

inferred for the evaluated expression Similarly the store mo del S must b e up dated to S

However S must agree with S on the lo cations l of its domain which refer to observable

regions in These considerations are formalized by the following denition

Definition Extension S extends S noted S v S or S w

S ifandonlyif DomS DomS and for al l l DomS if S l Rng

S and S areequivalent noted S S or S l Rng then S l S l

if and only if S v S and S v S

The relation presented in the denition b elow sp ecies the consistency b etween values

and typ es according to observable eects It refers to an eect which represents the eect of

evaluating an expression to a value and the history of the eects that p ermitted the evaluation

of its environment Unobservable eects mayhowever b e needed to show the consistency

between unused values captured within closures and the typ es assigned to them

Definition Consistent values and types Given the store s the eect and the

model S the c onsistency relation between a value v and a type written s Sj v

satises the fol lowing properties

s Sj uunit

s Sj l ref Rng S l and s Sj sl

s Sjx eE there exist E such that Elambda x e

such that Observe and Observe

S such that S S and s S j E E

We write s Sj v v if and only if s Sj v for any substitution dened

xE x for any on v and s Sj E E if and only if DomE DomE and s Sj E

x DomE

It is shown in Tofte that such a structural prop ertybetween values and typ es

do es not uniquely dene a relation Because functions can b e recursively dened through

references it must b e regarded as a xed p oint equation Wemust dene the typing con

sistency relation as the maximal xed p oint of the prop erty dened in This is done by

considering the appropriate function F below

Definition F Let R be the set of al l tuples s S v The function F is dened over

the elements Q of P R The greatest xedpoint of F gfpF fQ R j Q F Qg

denes our relation we write s Sj v if and only if s S v gfp F

F Qfs S v j

if v u then unit

if v l then there exists such that ref S l Rng

and s S sl Q

if v x eE then

there exist E such that Elambda x e and S such

that Observe Observe and S S

and s S Ex Q for any x DomE and Exg

To admit a maximal xed p oint gfpF the function F must b e monotonic This is the

rst prop erty that wethus havetoverify

Lemma Monotony of F If Q Q then F Q FQ

Pro of Let Q and Q be two subsets of S such that QQLetq be s S vin F Q

Weprovethat q FQ

If v u then by the denition unit so that q FQ

Rng S l and If v l then by the denition ref

s S sl Q Since QQs S sl Q By the denition

q FQ

If v x eE then by the denition there exist E such that Elambda x e

such that Observe and Observe S suchthat S

S ands S Ex Qfor any x DomE and Ex Since

Q Qs S Ex Q By the denition q FQ

The lemma relates the denition of the relation v with the consistency relation given

in the denition according to the following resp ects

Lemma Extension If s Sj v and S v S then s S j v

Pro of We consider the set Qfs S v j s Sj v and S v S gWe

Q Thus show by case analysis on the structure of v thatq s S visin F

QFQ showing that Q is F consistent

If v u then by the denition unit so that q FQ

If v l then by the denition ref Rng S l and

Q s Sj sl Since s Sj sl and by denition of Qs S sl

Since Rng S l and by denition Rng and S l

By the denition q FQ

If v x eE then by the denition there exist E such that Elambda x e

such that Observe and Observe S suchthat S

S and s S j E x for any x DomE and Ex

egs are not free in Let us dene on Regs in suchaway that the regions R

and also ImS Let us write E E and S S By the lemma

E lambda x e By the lemma for all E x there exists Ex

such that the restriction of on the free variables of E veries Bythe

lemma s S j E x for any x DomE and Ex Let us dene

S as follows



l Rng l if l DomS andS S

l DomS S l



S l otherwise

Ex Q it remains to showthat Toprovethats S v S

S This requires that for any l DomS if S l Rng or

l l S then S l Rng S

If S l Rng then by denition of S S l S l

If S l Rng we pro ceed by case analysis on the denition of S

First if l DomS and S l Rng then S l S l Otherwise

or either l DomS This is imp ossible since we supp ose that l DomS

S l Rng We show that this is imp ossible as well

By h yp othesis wehavethat S l S l Rng Since by denition of

frImS Regs wemust have S l S l Rng However by

hyp othesis wehavethat S v S This imp oses that if S l or S l isin

then S l S l Since S S and do es not aect wemust Rng

have S l S l S l S l However we cannot have b oth S l Rng

l Rng and S

S Wehave shown by the denition that Since for any S v

x DomE and Ex s S j E x by denition of Qs

Ex Q S

Toshowthatq FQ it remains to provethat S S Since DomS

S this reduces to showing that if S l Rng orS l Rng then Dom

S l S l We pro ceed by case analysis on and n

If S l Rng or S l Rng then either l DomS or not If l DomS

then by denition of S S l S l If l DomS then since S S

S l S l Since is not dened on S l S l Since byhyp othesis

l v S S l S l Now we knowthatS l S l S l S S

Toprove that S l S l it remains to show that S l S l By hyp othesis

l DomS and either S l Rng or S l Rng If S l Rng then

S l S l If S l Rng then since S l S l S l Rng Thus

by denition of S S l S l It follows that S l S l We conclude

that if S l Rng then S l S l

If S l Rng n then since Regs fr S l Rng

Thus by denition of S S l S l Similarlyif S l Rng n then

fr S l Rng Thus either l DomS andthen since Regs

S l S l or l DomS and then since S v S S l Rng

Thus S l Rng andby denition of S S l S l

ehaveproved that S S By the denition of Observe and W

Observe Since E lambda x e and s S Ex Q

for every x DomE and every E x by the denition q FQ

The lemma relates the denition of the relation with the consistency relation

dened in

Lemma Equivalence If S S then s Sj v if and only if s S j v

Pro of By hyp othesis S S By denition S v S and S w

S If s Sj v then since S v S andby the lemma s S j v If

s S j v then since S w S and by the lemma s Sj v

The lemma states that the typing judgment s Sj v is stable under substitution

Lemma Substitution If s Sj v then sSj v for any substitution

Pro of We consider the set Qfs S v j s Sj v gWeshow by case analysis

on the structure of v that q s S visin F Q Thus QFQ showing that

Q is F consistent

If v u then by the denition unit so that q FQ

If v l then by the denition ref Rng S l and

s Sj sl Since s Sj sl by denition of Qs S sl Q

S l Rng By the denition q FQ

If v x eE then by the denition there exist E such that Elambda x e

such that Observe and Observe S suchthat S

S and s S j E x for any x DomE and Ex Let us dene the

substitution of Regs by the regions Regs not free in or and let be

same as elsewhere

Let us write E E S S and By the lemma for all Ex

there exists Exsuch that the restriction of on the free variables of E veries

By denition of Qs S Ex Qfor every x DomE

x By denition of Observe and Observe and and every E

Observe By the denition S S By the lemma with

lambda x e By the denition q FQ E

The lemma is a renement of lemma and states that the judgment s Sj v

reduces to s Sj v for any substitution that aects but not It is used in the

pro of of consistency theorem to showthatourtyp e generalization criterion is correct

Lemma Instantiation If s Sj v and is denedonfv n fv then s Sj

v

Pro of By hyp othesis s Sj v and is dened on fv n fv By the lemma

sSj v Since by the denition S S By the lemma

s Sj v

During the evaluation of an expression the store is extended and up dated in an organized

way The denition sp ecies the requirements for preserving consistency b etween typ es

and values in the presence of sideeects

Definition Succession s S becomes s S noted s S v s S if

and only if Doms Doms S v S and s Sj v implies s S j v for

any v and

The lemma represents the situation that arises when a reference is initialized In the

lemma we address the situation arising when a value is assigned to a reference

Lemma Initialization Let init s s fl v g l Doms S

S fl g l DomS and s Sj v Ifs Sj v then s S j v

S v j s Sj v gWeshow by case Pro of We consider the set Qfs

analysis on the structure of v thatq s S v isinF Q Thus QFQ

showing that Q is F consistent

If v u then by the denition unit so that q FQ

If v l then by the denition ref Rng S l and

s Sj sl Since s Sj sl by denition of Qs S sl Q

Rng S l If l l since Rng and S l then

s l sl and s S sl Q If l l then byhyp othesis

v s l and s Sj v Thus by denition of Qs S s l Q

By the denition q FQ

If v x eE then by the denition there exist E suchthat Elambda x e

such that Observe and Observe S suchthat S

S and s S j E x for any x DomE and E x Let be

dened on Regs and such that the regions Regs are not free in and

Let S S and E E Since by the lemma Regs fv by

the lemma with E lambda x e

By the lemma for all E x there exists Exsuch that the restriction

of on E veries Thus by the lemma s S j E x for any

x DomE and E x By denition of Qs S Ex Q for

suchthatObserve every x DomE and every Ex Wehave dened

and Observe and S suchthat S S By the

denition q FQ

write s s fl v g s Sj l ref and Lemma Assignment Let

l

s Sj v Ifs Sj v then s Sj v

Pro of We consider the set Qfs S v j s Sj v g Weshow by case

analysis on the structure of v that q s S v isinF Q Thus QFQ

showing that Q is F consistent

If v u then by the denition unit so that q FQ

If v l then by the denition ref Rng S l and

s Sj sl By denition of Qs S sl Q

If l l then Since Rng by denition of Rng

v s l ands Sj v Thus by denition of Qs S v Q Otherwise

s l sl and s S s l Q By the denition q FQ

If v x eE then by the denition there exist E suchthat Elambda x e

Observe and Observe S suchthat S such that

S and s S j E x for any x DomE and Ex

Since s Sj l ref by denition Rng and then Rng Rng

Thus Observe By denition of Qs S Ex Q for

every x DomE andevery Ex By the denition q FQ

The lemma is used in the inner pro of case of the theorem in the case the rule

sub is used It tells that the consistency of the rest of the computation is not aected by

unobservable eects

Lemma Observability If s Sj v and Observe then s Sj

v

Pro of We consider the set Qfs S v j s Sj v g for any suchthat and

Observe We show by case analysis on the structure of v that q s S v

Q Thus Q FQ showing that Q is F consistent is in F

If v u then by the denition unit so that q FQ

If v l then by the denition ref Rng S l

and s Sj sl Since s Sj sl by denition of Qs S sl

Q Since Rng and ref by denition of Observe

RngObserve Since Observe Rng Thus by the deni

tion q FQ

If v x eE then by the denition there exist E such that Elambda x e

Observe S suchthat S such that Observe and

S and s S j E E

By the denition for any x DomE and Ex s S j E x By

denition of Qs S Ex Q

Let n Since Observe and fr Regs n wehave

Observe Similarly since Observe and fr Regs n

wehave Observe

and nally since S S and S Wehave that

S By the denition q FQ

The consistency theorem app ears b elow The eect corresp onds to the eect of evalu

ating the environment of the expression eLetE and E b e consistent with resp ect to this

initial eect and such that the initial store s and a store mo del S satisfy s Sj E E

If e is such that s E e v s and Ee then there exists a store mo del S suchthat

s S b ecomes s S and that the value v is consistent with its typ e according to

the mo del S satisfying s S j v

Theorem Consistency of dynamic and static semantics If s Sj E E E

e and s E e v s then there exists S such that s S v s S and

s S j v

Pro of The pro of is by induction on the length of the dynamic evaluation Before detailling

the case analysis that corresp onds to eachsyntactic form we detail the inner pro of case that

corresp onds to the application of the rule sub in the static semantics The situation is that

was inferred Ee s E e v s and s Sj E E The judgment Ee

from the rule of observation

Ee Observe E

Ee

Let us write Observe E and n Let us dene the substitution



on Regs such that the regions Regs are not free in E and Let us write

 

Since Observe E by the lemma Regs frE fr By

   

the lemma

E e



S j v and that By hyp othesis on the inner pro of there exists S such that s



s S v s S Since Observe E and of course Observe

  

Observe E by the lemma



s S j v

Let us consider any v and such that s Sj v Since s S v s S



by the denition s S j v We can freely cho ose inthe

  

judgment Ee so that Regs fr By the denition of Observe

 

Observe and thus by the lemma s S j v This holds for any



judgment s Sj v Thus by the denition

s S v s S

Since by the lemma

and s S j v s S v s S

Case of var By hyp othesis s Sj E E s E x v s and Ex By denition

of the rule var this requires that E xv and that Ex By the denition

s Sj v We conclude taking s s and S S that

S j v s S v s S and s

Case of abs By hyp othesis s Sj E E The rules abs of the dynamic and static

semantics imp ose that s E lambda x e x eE s and Elambda x e

x

By the denition taking s s and S S we conclude that

s S v s S ands S jx eE

x

Case of let By hyp othesis s Sj E E The rule let of the dynamic semantics

imp oses that

s E e v s s E fx v ge v s

x  

s E let x e e v s

 

In the static semantics writing wehave



Ee E fx Gen E ge

x   

Elet x e e

  

Let us write v fv n fv fvE Let us dene the substitution on v suchthat

the variables v v are distinct and not free in and E Let us write By

the lemma

Ee

By induction hyp othesis on e there exists S suchthat

s S v s S and s S j v

Since s S v s S by the denition s S j E E Since v

fv n fv fv E wehave v fv Let be any substitution dened on v Since

v fv and v fv by the lemma s S j v By denition of

and denition

s S j v v

Let us write E E fx v g and E E fx v g By induction hyp othesis

x x

there exists S suchthats S v s S and that s S j v on e

  

Since s S v s S and s S v s S by the denition

s S v s S and s S j v

 

Case of app By hyp othesis s Sj E E The rule app of the dynamic semantics

imp oses that

s E e x e E s s E e v s s E fx v ge v s

        

s E e e v s

  

Let us write In the static semantics wehave

 



Ee Ee

    

Ee e

   

By induction hyp othesis on e there exists S suchthat



s S v s S and s S jx e E

  

E By induction Since s S v s S by the denition s S j E

hyp othesis on e there exists S suchthat

 

s S v s S ands S j v

       



and by the denition s S j Since s S jx e E

     



x e E By the denition this requires that there exist E S and suchthat

  

 



S S Observe and Observe

     

  

verifying



and s S j E E E lambda x e

    

 

Let us write Regs and dene on such that the regions are not free in E



 

and Since Observe and Observe

     

 

by the lemma



Regs Regs and fv fv

  

 

Let us write and S S wehave S S

  

    



Observe and Observe By the lemmas and E

  

  

and S verify





and s S E lambda x e j E E

    

 

Let us write E E fx v g and E E fx g By the denition the

 

x x

E E By induction on e there exists S such lemmas and s S j

  

 

that

s S v s S andthats S j v

   

   

Since s S j v and Observe by the lemma

  

 

s S j v

 

In the same manner let us consider any v and suchthats Sj v Since s S v

s S by the denition s S j v Since we can freely cho ose

 

fr by the denition of Observe Observe so that Regs

   

Thus by the lemma s S j v This holds for any judgment s Sj v

Thus by the denition

s S v s S

Case of new By hyp othesis s Sj E E The rule new of the dynamic semantics

imp oses that

s E e v s l Dom s

s E new e l s fl v g

In the static semantics the situation is

Ee

Enew e ref init

Let us write init By induction hyp othesis on e there exists S such

that s S v s S and that s S j v Let us write s S

g Since l Doms ands S j v bythe s fl v g S fl

lemma s S v s S Thus by the denitions and

s S v s S and s S j l ref

Case of get By hyp othesis s Sj E E The rule get of the dynamic semantics

imp oses that

s E e l s l Doms

s E get e s l s

In the static semantics the rule get reads

Ee ref

Eget e read

Let us write read By induction hyp othesis on e there exists S suchthat

s S v s S and that s S j l ref By the denitions and

s S v s S and s S j s l

Case of set By hyp othesis s Sj E E The rule set of the dynamic semantics

imp oses that

s E e l s s E e v s

 

s E set e e u s fl v g

 

l

In the static semantics wehavethat

Ee ref Ee

 

Eset e e unit write

 

Let us write write By induction hyp othesis on e there exists S



such that

s S v s S and that s S j l ref

By the denition s S j E E By induction hyp othesis on e there exists S

 

h that suc

s S v s S and that s S j v

     

By the denition s S j l ref Let us write s S s fl

   

l

S v s S By the denitions v g S By the lemma s

   

and we conclude that

s S v s S and s S j uunit

The Reconstruction Algorithm

We present the inference algorithm I that reconstructs the principal typ e and eect of

expressions with resp ect to the static semantics The inference algorithm I uses a double

recursion scheme that separates the reconstruction of typ es and eects from the pro cess of

restricting eects with regard to the observation criterion The next section explains the

notions intro duced in the algorithm

Constrained Typ e Schemes

Weuseconstrained type schemes to generically represent the p ossible typ es and constraint

sets of letb ound expressions In the static semantics typ e schemes wereoftheformv

But now since eect variables o ccur in function typ es constraintsetsinvolving these eect

variables havetobekept within typ e schemes In the algorithm the typ e environment E

binds value identiers to such constrained typ e schemes

Constrained typ e schemes written v or are comp osed of a typ e

er typ e eect and region variables The and a set of inequalities universally quantied ov

typ e and constraint set asso ciated with e only dep end on the free variables of e and thereby

on the typ e environment E We write v v

In order to relate the constrained typ e schemes and environments of the algorithm to the

static semantics we dene a relation from the former to the latter by using the principal

of a constraintset as dened in the section We write v v and mo del

E xE x for all x DomE

Gen E let fv g fv n fvE fv in v n

v v

Inst v let v new and fv v g in

Generalization and Instantiation

For a given constraint set the function Gen generalizes the typ e of an expression

up on the variables that are neither free in its environment E nor present in its observed

eect We write for the restriction f j v g of on the eect variables

v v

v We write n the complementof in The instantiation of typ e schemes for value

v v

identiers and op erators is done by using the function Inst

Constrained Typ e Schemes of Store Op erations

In the reconstruction algorithm the store op eration new get and set are viewed as op era

tors They are related with appropriate constrained typ e schemes by the function TypeOf

unit f write g TypeOf set ref

TypeOf get ref f read g

TypeOf new ref f init g

Constrained Typ e Schemes for Store Op erations

The Reconstruction Algorithm

In the rst phase of the reconstruction the algorithm I given an environment E and a

constraintset reconstructs the typ e and the eect of an expression e together with

a substitution that ranges over the free variables of the environment E and an up dated

constraint set In its second phase the algorithm I takes into account the observation

criterion Observe in order to restrict the eect computed by the algorithm I

I E elet I E ein Observe E

I E e case e of

op let Inst TypeOf opinId

x if x DomE

then let InstE x in Id

else fail

lambda x e let new

e I E fx g

x

in f g

Reconstruction Algorithm I

e e let I E e



I E e

    

new

U

  



and

   

in

   

let x e e let I E e



v Gen E

I E fx v g e

  x 

in

  

Reconstruction Algorithm I continued

An imp ortantinvariant of the algorithm I is that latent eects of functions are al

ways represented by eect variables in the algorithm This technique was intro duced in

Talpin Jouvelot Sept and makes the problem of solving equations tractable by

a simple extension to a unication algorithm on free algebras Robinson over eect

variables The unication algorithm U presented in section solves the equations

on the typ es built by the algorithm I We note U for U curried with its rst

argument the constraintset in order to simplify the notation

Constraint Resolution

We view the inference of typ es and eects of an expression as a constraint satisfaction

problem The algorithm builds equations on typ es and inequations on eects In the algo

rithm indirections b etween typ es and eects are intro duced by the notion of constraint sets

dened b elow satises Among the solutions of a constraint set the principal mo del

the lemma

Definition Model of Constraints A substitution isamodel of a constraint set

written j if and only if for every constraint in The principal model

of is inductively denedby

f g

Id and f g where

However recall that weintro duced typ es in eects as well as eects in typ es

Consequently some expressions maynowhave recursively dened typ es and eects and shall

thus b e rejected by the static semantics

Example The static semantics might constrain some expressions to have an eect con

taining init itself Leroy The simplest known example pro ducing suchan

illformed constraint set is

lambda f

let x new new lambda x x

if true f lambda y set x new f y

In this program the typ e of the function f has to matchthetyp e of the lamb daexpression

lambda y set x new f y that initializes an observable reference to f Note that

the incriminated eect must b e observable for this situation to app ear Giving typ e to x

the typ e of f is recursively dened by

f

ef  init   write  r

f f

f

For our algorithm to b e eectively implemented constraint sets must b e checked for well

formedness It must b e enforced that no indirect cycles are intro duced through init eects

WellFormed ConstraintSets

Our solution is to only use wellformed constraint sets within the algorithm I They corre

sp ond to sound assignments of eect variables in the static semantics

Definition WellFormed Constraint Sets Aconstraint set is wel lformed

written wf if and only if for every such that f g we have

fv Rng

The notation wf is extendedtotype schemes by wfv i wf and to type

environments by wf E i wfE x for every x in DomE

The denition of wellformed constraint sets comes here with the following lemmas that

state that wellformed constraint sets are solvable by nite substitutions

Lemma WellFormed Constraint Sets wf if and only if j

Pro of If wfthen j We pro ceed by induction on the numb er of constraints in If

then Id solves Consider f g where nf g By denition

wehave f g and by induction hyp othesis on solves For every

f g and f g constraint in

If then n Since wehave

By induction wehave If then

n so that Otherwise so

thus

and Since and wehave Otherwise

so Since solves wehave that sothat

in b oth cases For every constraint in so solves It follows that

It remains to showthat solves f g By denition f g Since

n Also f g

then otherwise In b oth cases wehavethat If

solves f gWehavethus proved that solves

Pro of If j then wf Let us assume that j and rewrite as

f n j g

  

If j then by denition of j for every constraint By

Thus for every constraint n construction of

  

n Finally j

  

Consider any constraint in and dene nf gSince j then

by denition of j Since by construction of this is equivalent

Since by construction of this is equivalentto to

Since by construction of this is equivalentto

Since by denition of f g this implies that for every pair

f g This implies that fv and by denition of wf

that wf

Now if wf then writing every constraintin as n wehavethat

  

fv for anypair of n This implies that it is not in anyofthe

  

Thus we conclude that wf

Lemma Principal Model If solves then

Pro of By induction on the numb er of constraints in If then Idso

Now let us consider f g where nf g and let b e a solution of

Note that solves soby induction Let be any eect variable we wish to

By denition f g Then there are two show

cases

If then by induction

Otherwise so that f g

n as

as

by induction

as solves

Now implies that Thus Thus

inthiscaseaswell

We state that illformed constraint sets cannot b e satised by substitutions of eect

variables by nite eect terms

Lemma IllFormed Constraint Sets If is il lformed then theredoes not exist

a substitution satisfying

Pro of We show that in order to satisfy an illformed constraint set any substitution

must substitute at least one eect variable by a non nite eect term

We assume that wf By denition this implies that there exists a constraint f g

in suchthat Rng and fv where nf g Supp ose that there

exists a substitution suchthat j

Bydenitionof weknow that f By the lemma we knowthat

g Then from j and by denition must verify

must b e in Rng Since Rng the substitution Thus Rng

must verify However

f g by denition of

f gf g since and

g since f g f

Since Rng f g and fv

the term must satisfy But since Rng and fv

o ccurs in Thus the substitution satisfying cannot b e dened since the term

must b e dened recursively

Unication Algorithm

In the reconstruction algorithm I instead of checking the wellformedness of the constructed

constraint set after each expression is typ echecked we implement an extended o ccurrence

check test rep orting the construction of illformed constraint at the p oint of unifying eect

variables together

U case of

unit unit Id

f g

j if fv thenfailelsef g

ref ref let f g in U

let U

i f i i

i f i

U

f i f i

i f

g f

f i f i f i

in if wf then else fail

otherwise fail

Unication Algorithm

The unication algorithm U presented ab ove solves the equations on

typ es built by the algorithm I and checks the constraint set generated by the algorithm

for wellformedness It either fails or returns a substitution standing for the most general

unier of the two given typ e terms and alsochecking that the substitution preserves

the wellformedness of the given constraint set The following soundness and completeness

lemma givetheinvariants of the unication algorithm U We note U for U curried with its

rst argument the constraint set in order to lighten the notation

Lemma Soundness of U If is wel lformedandU then is wel l

formedand

Pro of The algorithm U unies the terms of a free algebra and its soundness pro of only

departs from Robinson in the case that enforces the wellformedness of the constraint

set Byhyp othesis is well formed and U By denition of U this

and f gThus and it remains to prove that requires that fv

is well formed

By hyp othesis wehave that is wellformed By denition this requires that for every

constraint in considering nf gwehave

Rng fv

Wewanttoshow that is wellformed By denition this requires to show that for

every in considering nf gwehave

Rng fv

If fv then By denition f gThus wehave to show that fv

so that wehave fv Otherwise fv and since fv and

it remains to show that fv

Assume that there exists Rng such that fv By denition of this

requires that fv Wehaveshown that supp osing fv implies fv which

contradicts the hyp othesis fv

Wehaveproved that for every in considering nf gwehave

that fv forevery inRng By denition this proves that is

wellformed

Lemma Completeness of U Let be wel lformed Whenever for a sub

stitution satisfying then U then is wel lformed and there exists a substi

tution model satisfying such that

Pro of By hyp othesis is wellformed and there exists a substitution satisfying such

that The cases that dier from the completeness pro of of Robinson

are those which require the wellformedness of the constraint set to b e checked or

i f

i f

In the case of thehyp othesis is that By the lemma wehavethat

so that This implies that is not in fv so that we get that

f g U by denition Consider dened by and v v

otherwise Wehave that and that satises since satises By the

lemma this implies that is wellformed

the hyp othesis requires that In the case of

i f i

f i i

and

f

f

then U for By induction hyp othesis on and wf and

i i i i i

i i i

some j Since wf and since there exists a substitution satisfying suchthat

i i i

i i

then by induction hyp othesis on and wehave U

i i f i f i f

i

i i f f

then wf and for some j

f i f f i

i f f

Thus there exists a substitution satisfying suchthat

f i f i f i

f f f

But requires that Let us write

f i f i f i f i

f f f f f

and f g

f i f i f i

Wehave Since the substitution satises then

f i f i

f f

satises so that satises By the lemma this implies that is well formed

suchthat As a conclusion weget U wf and the substitution satises

Correctness of the Reconstruction Algorithm

In this section weprove the correctness of the algorithm with resp ect to the static semantics

The soundness theorem states that the typ e and eect computed by I are provable in the

static semantics assuming any solution of the inferred constraints

Theorem Soundness Let E and be wel lformed If I E e then

E e

Pro of The pro of is by induction on the structure of expressions We assume that the

constrained typ e schemes in the environment E are constructed with the function Genso

that for every x in DomE wehave that E x v with restricted to the v Then

E x E x ignoring capture of a consequence is that for any substitution wehavethat

b ound variables Also note that by denition of the reconstruction algorithm the constraint

set extends every mo del of is thus a mo del of

Case of var By hyp othesis I E xId By the denition of the

algorithm this requires that E xv that fv v g and that the v are fresh

Since and are only Since renames the v with fresh v wehave that

dened on v wehave E E By denition of E x By denition of the

rule var

Ex

Because substitutes v with fresh v wehave that By the lemma

used with we can conclude that

E

x

Case of let By hyp othesis I E let x e e By the denition of



the algorithm I this requires that

E I E let x e e and Observe



By the denition of I this requires that there exist suchthat and

 

such that satisfying

  

I E e and I E fx v g e

  x 

E v By induction hyp othesis on e weget where Gen

E e

By the denition of Gen wehavethat b ecause is the restriction

of on v and its complementin We also have that E E and that

since the v are neither free in E nor in Thus wehave that

E e

Since extends satises satises Thus By the lemma we

  

By the lemma used with have that

  

E e

  

and E fx v gweget By induction hyp othesis on e with

x 

E v

fx g e

  

x

gThus Since E E then E fx v g E fx v

x x x

x

E

fx v g e

x   

Since and by the denition of the rule let this implies that



E let x e e

  

E so that using the rule sub we get We know that Observe

E

let x e e



and since we conclude that By the lemma used with

E let x e e



Case of abs By hyp othesis I E lambda x e f g By

the denition of the algorithm I this requires that I E fx ge By

x

induction hyp othesis on e

E

fx g e

x

Since is fresh Thus by the rule sub

E fx g e

x

f g Since is fresh Let us write f g By denition

we get

E fx g e

x

By the rule abs we conclude that

E

lambda x e

Case of app By hyp othesis I E e e By denition of the algo

 

rithm I this requires that

I E e I E e and U

       



where Observe and By induction E

       

hyp othesis on e we get

E e

Since extends and U is wellformed Since

      



satises Thus satises By the lemmaon extends

     

wehave By the lemma used with we get

     

E e

   

Since U then is wellformed and bythe

        



correctness lemma on unication yielding

 



E e

    

Since E E and by induction hyp othesis on e with E



E e

      

Since satises satises By the lemma using wehave

     

Bylemma used with we get

  

E

e

    

By denition of and the rule app



E

e e

  

E andby the rule sub Since Observe

 

E

e e

 

we get By the lemma used with

E

e e

 

The completeness theorem states that the inferred typ e is principal with resp ect to substi

tutions on variables and that the reconstructed eect is minimal with resp ect to subsumption

on eects

Theorem Completeness Let E and be wel lformed and beamodel of If

Ee then I E e and there exists j such that E E

and

Pro of The pro of is by induction on the structure of expressions

Case of var By hyp othesis Ex This requires that E xby denition

of the rule var and that E xv by denition of E By denition of the algorithm

I we get

v

I E xId and fv g

By hyp othesis wehave that E x By denition of this requires that there

exists a substitution dened on v such that

By denition of the algorithm I substitutes v with fresh v Let b e dened on v



by v v v Avoiding capture the substitution satises

  



Since renames v with fresh v wehave As a consequence



satises and is such that

E E

and and

Elet x e e By denition of the Case of let By hyp othesis





rule let this requires that

Ee and E fx Gen E g e

x 



By induction hyp othesis on e I E e and there exists a substitution

satisfying such that

E E and

Thus by denition of Gen

E Gen E Gen

As a consequence given that E E

x x

E E

fx Gen ge

x





elsewhere by Id on v and by Gen E and dene Let v

By denition of

E E and

Let us consider any such that Gen By denition of there E

fv n fv exists a substitution dened on fv E suchthat

Let us dene by on v Since is dened on fv n fv fv E any

variable v in fv v satises v v Thus

Since satises by the lemma Thus

By denition of this implies that

v

E v Wehaveshown that for any such that Gen

Thus since there exists a derivation ge E fx Gen E

 x



of

E v

fx g e



x



E E since satises and by induction hyp othesis on e with and Since

x x 

E fx v g

x

e g I E fx v

   x

and there exists a substitution satisfying such that and



   

g g E fx v E fx v



x x



E fx v g

 x



E By denition Let us write and Observe

    

of the algorithm we get that

I E let x e e



Let V bethefreevariables of E and Dene by on V and otherwise

 



By denition satises and satises

E E

and





Since Observe E and then by the lemma



E E

Observe Observe

 

We conclude that Wehave proved that I E let x e e





and that there exists a substitution satisfying such that

E E

and



Case of abs By hyp othesis Elambda x e By denition of the

i f

rule abs this requires that

E

fx ge

x i f

With a fresh variable this is equivalent to

E fx g e f g

x f i

By induction hyp othesis on e since E fx g is wellformed wehavethat

x

I E fx ge

x

and there exists a substitution satisfying such that

E E

f g fx g fx g and

i x x f

By denition of the algorithm we get that

f g I E lambda x e

where is new Let us consider the mo del f g of f gWe get

and

E E

f i

Case of app The hyp othesis is By the denition of the Ee e



 

rule app this requires that



and Ee Ee



  

satisfying By induction hyp othesis on e I E e and there exists

such that



and E E



Since E E and E is wellformed by induction hyp othesis on e weget



I E e

    

and there exists satisfying suchthat





E E and

  

    

Let V b e the set of free variables in E and Take and new and

     

dene as follows







v v V













v

v







v













v otherwise

By this denition satises and we get







E E and

   

    

is either fresh or in fv E Since E By denition of I every v in





E E for every v in fv E wehave v v v For every

  

  

fresh v since v v wehave v v v Thus

 

 



 

  

Since satises and by the lemma there exists a substitu

  

  

tion such that U verifying

   



   

and such that is wellformed By the denition of the algorithm we get

 

I E e e

 

where and By the lemma there

        

exists a substitution satisfying such that We get

  



E E

and

   

 

By denition of I wehave that

I E e e and Observe E

 

and by the lemma By denition



 

E Observe E Observe



 

then by denition of we get that j Bythe j and Since

 

 

lemma By the lemma this implies

Observe E



 

We can conclude that I E e e E E and







Examples

By intro ducing some wellknown examples of list pro cessing pro cedures weshow that our

typ e and eect system p ermits the assignment of the same typ e and eect to its functional

and imp erative implementations

nil list

cons list list

car list

null list bool

cdr list list

List Pro cessing Functions

Weintro duce the typ e list ofimmutable lists together with the following constantand

functions for manipulating them nil is the empty list The predicate null tests if a list is

empty The constructor cons pairs up an elementoftyp e with a list of typ e list The

pro cedure car returns the rst element of a list and cdr the rest

Example Our rst example is the function fold that successively applies function f over

every element of a list l and its intermediate result i

define fold lambda f i

lambda l rec loop l i

if null l i

loop cdr l f car l i

li

By considering observable eects implementing the function fold recursively or bya

lo op using temp orary lo catives do es not aect its typing

define fold lambda f i

lambda l

let result new i

let data new l

until null get data

set result f car get data get result

set data cdr get data

get result

Both implementations of the function fold havetyp e



list Then if one denes the function reverse for reversing the elements of a

list by lambda l fold cons nil l its p olymorphic typ e is list list

with b oth implementations of fold

Example In the same vein we consider the typing of two implementations of the function

map First we implement map byusing reverse and imp erative constructs

define map lambda f l

let r new nil

let x new l

until null get x

set r cons f car get x get r

set x cdr get x

reverse get r

The details of the imp erative implementation of the function map are similar to those

of the function fold and we can also implement map by reusing the function fold dened

ab ove

define map lambda f l

fold lambda x r cons f x r nil l

The same p olymorphic typ e is assigned to b oth of them by using our typ e and eect

discipline So for instance the application of map to the identity function and the empty

list has the p olymorphic typ e listofnil

define nil map lambda x x nil

The application of map to new and nil has a monomorphic typ e listref accounting

for the use of the function new on a region with an observable eect init

define nil map new nil

Comparison with the Related Work

Togive a detailed comparison of our system with the related work the criterion of expres

siveness seems at very rst sight b est suited b ecause it is a formal criterion The relative

expressiveness of a typ e system with resp ect to another denes itself as the capabilityof

strictly accepting more programs Such a prop osition requires a formal pro of and in most

cases it is thus easier to show the contrary by giving a counter example

In practice it turns out that the criterion of expressiveness is not appropriate and many

examples among which some are presented in the next section show that there usually

is no prop er inclusion b etween typ e systems Unfortunately it app ears that our system

do es not p ermit to recognize some sophisticated examples as sound for example the func

tion f section or the function id section although those examples could b e

recognized as such using for example the typing discipline describ ed in Tofte or

App el Mac Queen

Nonetheless in order to argue in favor of our system wehave already carried out the

comparison on practical situations which aimed at demonstrating that the problem of inte

grating imp erative features to a functional language is b est viewed in terms of a typ e and

eect system b ecause it p ermits reasoning at a more intuitivelevel ab out the problem of

typing references in a language suchasMLThegoalofintegrating imp erative and func

tional paradigms which is a fundamental asp ect in our system is particularly well suited

within a programming environment that supp orts separate compilation features and mo dular

programming paradigms

Comparative Examples

This section present a series of more or less sophisticated examples adapted from a sur

vey pap er on this sub ject OTo ole and from Leroy that establishes fron

tiers b etween the related typ e systems Note that the results of the system presented in

Jouvelot Giord are given for programs with explicit p olymorphic typ es

define id let x id rid

define id lambda y rid id y

define id nop rid id

e write rid the imp erative version of the identity function In these three examples w

lambda x get new x and id for the usual denition of the identity functional We

also use the function nop dened as b elow

define nop lambda f

lambda x

let g lambda y f x

x

The next example quoted from Leroyshows that the observation of eects can

b e useful for removing typ e dep endencies intro duced by otherwise deadco de Our inference

system generalizes the typ e of id below contrarily to the ones dened in Leroy or

Wright which are not able to deal with the spurious allo cation new x

lambda z

let id lambda x

if true z lambda y begin new x y

x

id id

Nonetheless one can force such an eect to b e observable as is shown in this other

contrived example An o ccurrence of the typ e of y app ears on the arrow of the function typ e

for f and is constrained to matchthetyp e of f which o ccurs in the context of idAsa

consequence the typ e of id cannot b e generalized

lambda f

let id lambda y

let r new y

if true lambda z if true r new y z f y

id id

In addition to this comparison the example of recursivetyping presented in section

the function etaref below requires such a recursivetyping is well typ ed using the system

of Leroy Note that it would have b een in ours if we had chosen to make indirections

between typ es and eects explicit in the static semantics using constraint sets as is done in

the inference algorithm

define etaref lambda f

let r new f

if true f lambda x

get if true r new f x

Benchmarks

The following b enchmark summarizes the discussion ab ove and suggests that our typ e and

eect discipline favorably comp etes with some earlier p olymorphic typ e generalization p oli

cies

Example Tofte App el Jouvelot Wright Leroy Leroy

id no yes yes yes yes yes yes

id yes no yes yes yes yes yes

id no no yes yes yes yes yes

id yes yes no no no yes yes

id yes yes no no no no no

id id no no no no yes yes yes

id id no no no no yes yes yes

id id no no yes yes yes yes yes

id id no no no no no yes yes

id id no no no no no no no

Leroys system based on closure typing essentially diers from ours based on eect

inference in that it asso ciates functions with a static information the typ e of their free

variables Typ e generalization then relies on a chasing of dangerous typ es This resembles

the pro cess of garbage collection whichchases referenced values and marks them b efore

sweeping the rest

In our approach typ e generalization is expressed in a much more natural way b ecause

it states what information is imp ortant accessible reference values Instead of chasing for

every p ossibly referenced value at any time as in a closuretyping system we dene a notion

of accessibility in the static semantics which is represented by an observation criterion

This explains the dierences arising in the examples etaref dened in the ab ovesec

tion or fakerefbelow adapted from Leroy

define refid lambda x get x x

define fakeref refid rec loop x loop x

Atyp e system based on closure typing cannot detect the fakecharacter of the reference

intro duced in this example A typ e system based on eect inference can It detects that no

initialization eects o ccur

Extensions

An app ealing direction for further extensions would b e the treatment of rstclass contin

uations are ob jects that allow programs to capture current state of their

evaluation using the higherorder callcc op eration call with currentcontinuation and

to manipulate it using the throw construct These twovery simple and general op erations

dene sophisticated control structures that may b e needed to implementinterleaving or

backtracking mechanisms for instance

Continuation ob jects were originally intro duced in Scheme Rees al and then

but not without trouble in the implementation of Standard ML Duba al Con

tinuation values where rst prop osed by Duba al as an extension of Standard ML

and also implemented by an abstract data typ e Latter the implementation of continua

tions in Standard ML of New Jersey App el Mac Queen was shown unsound in the

presence of typ e p olymorphism Harp er Lillibridge

An accessible value allows the evaluation of an expression to b e restarted

in a context dierent from the context in whichitwas typ ed Thus it app ears very natural

to restrict p olymorphism for continuations in the same vein as for references accessible

continuations shall remain monomorphic

In our typing discipline with eects the typing of continuations can b e integrated in a

manner very similar to reference values by the following static seman tics

  comefrom 



TypeOf callcc   cont  

   



TypeOf throw   cont  

Static Semantics for Continuations

We dene cont  tobethetyp e of continuation values and the eects comefrom 

for capturing the currentcontinuation and goto  for invoking a continuation as in

Jouvelot Giord

The addition of continuations to our language do es not however showupasavery

natural extension as far as the dynamic semantics and the pro ofs of consistency are con

cerned The dynamic semantics needs to b e completely reformulated in terms of a continu

ation based semantics as in Duba al or in terms of a reduction semantics as in

Wright Felleisen

In Leroy following along the lines of Duba al the author gives a weak

soundness result for the extension of its typing discipline to continuation ob jects which

states that a welltyp ed expressions cannot go wrong Reduction semantics p ermits the

formulation of a strong soundness result for continuations in the typing discipline of Standard

ML Felleisen Friedman Wright Felleisen

Conclusion

Imp erative features are required to make functional programming realistic but integrating

p olymorphic typing in an imp erative language app ears problematic Our claim is that solving

this tension is b est tackled by using an eect systems Eect systems answer the lack

of sp ecication for p olymorphic typing in the presence of eects by approximating state

transformations

Using eects our typing discipline reconstructs the principal typ e and the minimal ob

servable eects of expressions We use eect information to control typ e generalization We

use an observation criterion to precisely delimit the scop e of sideeecting op erations Our

observation criterion generalizes the abstraction prop erties of functions in the presence of a

state A function can use a mutable ob ject lo cally in a given region without making mention

of this region outside Altogether this allows typ e generalization to b e p erformed in let

expressions in a more ecient and uniform way than previous systems

The initial design goal of p olymorphic eect systems Lucassen Giord was to

safely integrate functional and imp erative constructs Weshowed how eect systems can also

b e put to work for solving the problem of p olymorphic typ e reconstruction in the presence

of imp erative constructs Our typing discipline p ermits full integration of an imp erative

programming st yle in a p olymorphic functional language

Acknowledgements

We are grateful to Fritz Henglein and Mads Tofte DIKU Xavier Leroy and Didier Remy

INRIA David K Giord MIT and Andrew K Wright Rice University for insightful

comments and valuable feedbackonthework presented in this pap er

References

App el Mac Queen Appel A W and Mac Queen D B Standard ML Ref

erence Manual ATT Bell Lab oratories and Princeton University Octob er

Damas Damas L Typ e Assignment in Programming Languages PhD Thesis

University of Edinburgh April

Duba al Duba B F Harper R and MacQueen D Typing FirstClass

Continuations in ML In Proceedings of the ACM Conference on Principles of Pro

gramming LanguagesACM NewYork

Felleisen Friedman Felleisen M and Friedman D P A Syntactic Theory

of Sequential State In Theoretical Computer Sciencevolume numb er pages

Gordon al Gordon MJ Milner R and Wadsworth CP Edinburgh

LCF Lectures Notes in Computer Science No Springer Verlag Berlin

Harp er Lillibridge Harper R and Lillibridge M Polymorphic typ e assign

ment and CPS conversion In SIGPLAN Continuations Workshop

Giord al Gifford D K Jouvelot P Lucassen J M and Sheldon

M A FX Reference Manual MITLCSTR MIT Lab oratory for Computer

Science Septemb er

Jouvelot Giord Jouvelot P and Gifford D K Reasoning ab out Con

tinuations with Control Eects In ACM SIGPLAN International Conferenceon

Programming Language Design and ImplementationPortland June

Jouvelot Giord Jouvelot P and Gifford D K Algebraic Reconstruc

tion of Typ es and Eects In Proceedings of the ACM Conference on Principles of

Programming LanguagesACM NewYork

Kahn Kahn G Natural Semantics In K Fuchi and M Nivat editors Programming

of FutureGeneration Computers pages Elsevier

Leroy Leroy X The ZINC Exp eriment an Economical Implementation of the ML

Language Technical rep ort INRIA

LeroyWeis Leroy X and Weis P Polymorphic Typ e Inference and As

signment In Proceedings of the ACM Conference on Principles of Programming

LanguagesACM NewYork

Leroy Leroy X Private Communication

Leroy Leroy X Typage polymorphe dun langage algorithmique Do ctoral disserta

tion UniversiteParis VI I

Lucassen Lucassen J M Typ es and Eects Towards the Integration of Functional

and Imp erative Programming MITLCSTR Ph D Thesis MIT Lab oratory for

Computer Science August

Lucassen Giord Lucassen J M and Gifford D K Polymorphic Eect

Systems In Proceedings of the ACM Conference on Principles of Programming

LanguagesACM NewYork

Milner Milner R A Theory for Typ e Polymorphism in Programming In Journal

of Computer and Systems SciencesVol pages

Milner R Tofte M Harper R The denition of Standard Milner al

ML The MIT PressCambridge

Milner Milner R and Tofte M CoInduction in Relational Semantics Theo

retical Computer Science

OTo ole OToole J W Polymorphic Typ e Reconstruction Master ThesisMIT

Lab oratory for Computer Science May

OTo ole OToole J W Typ e Abstraction Rules for References a Comparison

of Four WhichHaveAchieved NotorietyTechnical Rep ort MIT Lab oratory for

Computer Science

Plotkin Plotkin G A structural Approach to Op erational Semantics Technical

report DAIMIFNAarhus University

Robinson Robinson J A A Machine Oriented Logic Based on the Resolution

Principle In Journal of the ACMVol pages ACM NewYork

Rees al Rees J and Clinger W Editors Fourth Rep ort on the Algorith

mic Language Scheme September

Talpin Jouvelot Sept Talpin J P and Jouvelot P Polymorphic Typ e

Region and Eect Inference In the Journal of Functional Programmingvolume number

Cambridge University Press

Talpin Jouvelot June Talpin J P and Jouvelot P The Typ e and Eect

Discipline In the proceedings of the s IEEE ConferenceonLogic in Computer Sci

ence Santa Cruz California June

Tofte Tofte M Op erational Semantics and Polymorphic Typ e Inference PhD

Thesis and Technical Rep ort ECSLFCSUniversity of Edinburgh

Wright Felleisen Wright A K and Felleisen M ASyntactic Approachto

Typ e Soundness Technical Rep ort TR Rice University

Wright Wright A K Typing References by Eect Inference In the proceedings

of the s European Symposium on Programmingvolume of the Lectures Notes

in Computer Science pages Springer Verlag