<<

Program Intensional and Comp ositionality

Jeery I Zucker

Departmen t of Computer Science Systems

McMaster University

Ont LS K Canada Hamilton

zuckermaccsdcssmcmasterca

HingKai Hung

Campus Drive

USA Snyder NY

hungcsbuffaloedu

Abstract

We apply to the semantics of an Algollike programming language

asso ciates with expressions their meanings relative to p ossible worlds here This

interpreted as machine states These meanings lie in the semantic domains of a higher

order typ ed intensional logic The great advantage of this approach is that it preserves

comp ositionality of the meaning function even in opaque contexts This approach

assignments Janssen and Van Emde Boas has b een applied in three areas

blo cks and pro cedures with parameters passed by value and by Hung

and assignments to p ointers and parameters passed by name Hung and

Zuc ker It is shown how this approach gives an elegant semantics which is

simple comp ositional and implementation indep endent

Intro duction

Denotational semantics Comp ositionality Assume within some general

framework we have a numb er of syntactic domains and corresp onding semantic domains

D with a meaning function

D

Assume also that the comp ound expressions of the syntax are formed from the atomic expressions

set of syntactic operators by a xed

The meaning function is said to satisfy the Compositionality Principle if for every such

nary syntactic op erator there exists a corresp onding function

1 n n+1

D D D such that for every comp ound expression

1 n 1 n n+1 n+1

1 n 1 n

This research was supp orted by the National Science Foundation under grant no DCR by a grant

the Science Engineering Research Board of McMaster University and by a grant from the National from

Sciences and Engineering Research Council of Canada

Typ eset by A ST X

M E

The Comp ositionality Principle thus says that the meaning of a comp ound expression is a

function of the meanings of its parts The idea can b e found in the writings of Frege It is

Chapter where it is used in the framework of many carefully discussed in Janssen

cit Chapter Janssen gives many arguments for sorted algebras and homomorphisms op

accepting this principle usefulness elegance generality comprehensibility p ower value as a

heuristic to ol and others

Its signicance for the correctness and verication of concurrent systems is discussed in Zwiers

tial comp osition Consider a simple Algollike programming language with assignments sequen

we dene the meaning S of a program S in this language as a state and conditionals Whether

transformer De Bakker or as a transformer forward or backward Dijkstra

it is not hard to see that the resulting semantics is compositional

and reference ad Consider now however the language extended by procedures with value

dress parameters and by blocks For such a language comp ositional semantics is more dif

cult to dene De Bakker for example uses syntactic application which is non

comp ositional

So how can we restore comp ositionality in such a framework The answer is in the use of

intensional logic as we will now see

Sense and Frege distinguished b etween two notions of mean

ing denotation or reference or and sense or We illustrate

rst that compositionality implies the the application of each with a wellknown example Note

which states that substituting one term for another with the principle of substitutivity of equals

same meaning in an expression should not alter the meaning of that expression Consider for

example the sentence

S I see the morning star now

which has a denite denotation truth value relative to a particular valuation of the index

icals I and now Now the phrases the morning star and the evening star have the same

enus so substituting the evening star for the morning star in S denotation the planet V

should result in a sentence with the same truth value

S I see the evening star now

which is the case But now consider the sentence

S I b elieve that the morning star is the morning star

which is obviously true Now however if we substitute the evening star for say the second

o ccurrence of the morning star in S we get a sentence

S I b elieve that the morning star is the evening star

which may very well b e false

The explanation for this apparent violation of the principle of substitutivity of equals is that

the after a verb of attitude expressing a b elief etc is referential ly opaque compared to

a referential ly transparent context such as is visible Freges insight was that the meaning

of an expression in an opaque context is given not by its denotation but by its sense Now the

ent senses since we can easily imagine expressions morning star and evening star have dier

a world in which they refer to dierent ob jects and hence dierent meanings in an opaque

context Hence S and S do not give a counterexample to the principle of substitutivity of

equals

To summarize this discussion taking the senses of expressions to b e their meanings in opaque

contexts restores the principle of substitutivity of equals and hence comp ositionality

Intensional logic In order to reason ab out senses or as opp osed to

or extensions we use intensional logic An early axiomatization of intensional logic

Carnap gave a semantics in which the sense of an expression was given by Church

e gave a more mathematically is a function from states to extensional values Kripk

to develop ed semantics in which the sense of an expression is a function from possible worlds

extensional values

Mon tague gave a comp ositional semantics for a fragment of English including opaque

contexts by dening a translation of it into a system of intensional logic

Van Emde Boas and Janssen applied Montagues approach to an Janssen

imp erative programming language essentially a fragment of Algol Their insight was that

for example the left hand certain contexts in such a language can b e viewed as opaque contexts

side of an assignment

Example Supp ose the lo cations x and y each store the value Compare rst the two

assignments

x x and x y

They b oth have the same eect x y

But compare now the two assignments

x x and y x

They have dierent eects The rst results in x y and the second in x y

So substitutivity of equals works in but not in From this we infer that the rhs

right hand side of an assignment is referential ly transparent but the lhs left hand side is

referential ly opaque

as The solution is to dene the the sense of a program variable or identier of typ e N

a function from p ossible worlds to numb ers interpreting p ossible worlds as computation

states

This amounts to taking the sense of an identier as a memory address or location the contents

of which dep ends on the state of the computation and its denotation as the current contents

Remark In common progam language terminology the lhs of an assignment is said to de

ct The assignment causes the reference to note a reference or lo cation and the rhs an obje

p ossess the ob ject as its value and the ob ject can then b e accessed through the reference by

dereferencing This is quite a dierent concept of reference from the one used in

denotation that we have b een discussing

Janssen and Van Emde Boas pro ceeded by translating the programming language into a

system of higherorder intensional logic The semantics thus inherited by the programming

language turns out to b e comp ositional

An overview of this pap er The approach has b een applied for fragments of Algol in

three areas

Assignments Janssen Van Emde Boas

Blo cks and pro cedures with parameters passed by value and by reference Hung

Assignments to p ointer identiers parameters passed by name Hung Zucker

We will consider each of these three areas in turn in Sections and resp ectively We have

In each case as we will see one gets a semantics which is already touched on the rst in x

compositional and implementation independent

This pap er is intended as a brief survey of these areas details can b e found in the ab ove

and in a forthcoming publication

Garrel Pottinger and Bob Tennent for helpful remarks following We thank Peter Grogono

the talk on which this pap er is based

The formal system I L of intensional logic

Syntax We dene a typ ed system IL of intensional logic The types are generated

according to the denition

j B j j S N

1 2

where N and B are the basic typ es of numbers and booleans or and S is the typ e

of states Note that S is a hidden typ e there are no terms of typ e S For each typ e

there is a countable set V ar of variables of typ e There are also constants of certain typ es

including the constant Typ ed expressions are generated according to the typing rules from

these variables and constants by the arithmetical operations of the relations of and

on the numb ers the prop ositional connectives and existential quantier the op erations of

^ _

application and lambda abstraction at all typ es and the modal operators intension

extension and h i state switcher

Definition Reference variables x are variables of typ e S N There is also a countable

f of reference constants X of typ e S N The reference variables and constants set Re

will represent the numb er references or lo cations

^

Remarks For an expression ie of type the expression gives the sense or

intension of ie its values in all states

_

For S gives the extension of ie its value in the current state This can

in x b e thought of as the dereferencing of see Remark

For reference variable or constant and expression e N the state switcher

expression h ei gives an explicit representation of syntactic substitution See the

in x Substitution Lemma

Semantics Let N b e the set of natural numb ers and B ftt ffg the set of truth values

tate to b e the set of states ie functions Ref N Then dene S

is of ob jects of typ e D N D B D For each typ e there is a domain D

!

N B

1 2

a suitable subset of the set of functions from D to D and D is a suitable subset of

S!

1 2

the set of functions from S tate to D

from V ar to D for all A valuation is a functions

For we dene the meaning of relative to a state and valuation by

structural induction on The full denition can b e found in Hung or Hung Zucker

We give some of the more interesting clauses For k V ar and n N fk ng

N

denotes the variant of at k with respect to n and similarly for fXng

0 0

X X

x x



tt if fk ng for some n N

k

ff otherwise

^ 0 0

_

hXei fXe g

Note that the interpretation of terms is twodimensional Quan tication and abstraction

are interpreted by means of valuations and mo dal op erators by means of states

T ranslation of programming language into IL Let P r og Lang b e the simple

programming language of x It contains

ariables u v consisting of b oth simple variables x y and indexed variables V

ae

A rithmetical expressions e

oleans b Bo

Program statements S generated by

S v e j S S j if b then S else S if

1 2 1 2

Assertions or rst order formulae extending b o oleans by having quantiers

Full details can b e found in Hung or Hung Zucker

We describ e a translation of P r og Lang into IL Again we only give the more interesting

cases A full denition can b e found in the ab ove references

0

First we asso ciate with each simple variable x of typ e N an IL variable x S N and

0

with each array variable a an IL variable a N S N

0

Next for each arithmetical expression e and boolean b we dene their translations e N

0

and b B resp ectively by structural induction on e and b For example the translation of x

_ 0

viewed now as an arithmetical expression is x N its dereferenced value and similarly

_ 0

the translation of ae is a e N

0

Next a statement S is translated as a backward predicate transformer S S B B

Note that a state predicate has typ e S B This is also dened by structural induction

on S For example

(S!N) 0 0 0

q hv e i v e q

note the use of the state switcher and

0 (S!N) 0 ^ 0

S S q S S q

1 2

1 2

The semantics of a program statement is then that induced by its translation Now since

i the translation is a homomorphism of the term algebras and ii the semantics of IL is

comp ositional therefore the induced semantics of P r og Lang is comp ositional See Janssen

Chapter

We have dealt with the rst of the three areas describ ed in x Before moving on to the

second we will show that one can derive more from the translation namely expressibility of the

weakest precondition

y of weakest precondition State switcher elimination Expressibilit

Lemma Substitution Lemma For any assertion reference variable x and arithmetical

expression e

0 0 0 0

hx e i x e

denotes semantic equivalence relative to instantiation of reference variables by refer where

ence constants

The pro of is by structural induction on It uses a numb er of state switcher reductions

This lemma shows that the state switcher functions correctly as an explicit representation of

syntactic substitution The fact that it also correctly represents semantic substitution is shown

by its semantic denition see the last equation in x

Theorem Expressibility of weakest precondition For any statement S and assertion

an assertion such that we can nd

0 ^ 0 0

S

The pro of uses the Substitution Lemma

Discussion It is trivial to apply a transformer asso ciated with a statement S to a p ostcondition

0 ^ 0

to get a precondition in IL That is just S in the statement of the theorem The

interesting question is whether the precondition is expressible in our assertion language by

in the statement of the theorem The signicance of state switcher elimination and the

Substitution Lemma is that in the context of this theorem it provides a state switcher free

0 ^ 0

form of S which is the translation of such a precondition

Blo cks and pro cedures with value and reference parameters

The problem Consider the pro cedure declaration

P h val x ref y i y x end

Supp ose the lo cations u and v each store the value Compare rst the three pro cedure calls

Pu w Pv w P w

They all have the same eect w

But compare now the two calls

P u P v

They have dierent eects The rst results in u v and the second in u v

So a value parameter is referentially transparent and a reference parameter is referentially

opaque

Comp ositional semantics for passbyreference is therefore dicult In fact it also turns out

to b e dicult for passbyvalue For b oth of these De Bakker uses syntactic application

which is noncomp ositional

The solution as we will see is again by means of a translation into IL

There is a corresp onding problem for blo cks and a corresp onding solution as we will also

see

Syntax of pro cedures and blo cks We adjoin to P r og Lang procedure identiers of

r v

and passbyreference P two sorts passbyvalue P

We also have procedure bodies of b oth sorts

v

h val x i S end B

r

h ref x i S end B

and procedure declarations

v v r r

D P B j P B

We also adjoin blocks of two sorts

v

egin new x e S end K b

r

K b egin alias x u S end

Our approach in these denitions is in accordance with the Correspondence Principle of Tennent

x For any parameter mechanism an analogous denition mechanism is p ossible and

vice versa Here denition mechanism can b e interpreted as blo ck mechanism So new

blo cks corresp ond to value parameters and alias blo cks to reference parameters We will

extend the application of this principle in Section

And corresp ondingly the the rhs of the assignment is referentially transparent in the new

blo ck and referentially opaque in the alias blo ck

The syntax of program statements is extended by

v r v r

S j P e j P u j K j K

are dened by and programs R

R h D S i

where D a vector of pro cedure declarations

We assume our programs are closed ie all pro cedures called in S are declared in D We also

forbid for now recursive pro cedures This is just to avoid having to deal with xp oint domain

theory We b elieve there is no diculty in extending our semantic treatment to these

y conversion Before we dene the translation of the extended P r og Lang Rigidit

to IL we need a little of the theory of IL

Definition A term of IL is rigid if its value is the same in all states ie

1 2

for all states

1 2

Versions of the following theorem on conversion in IL can b e found in Dowty Wall

Peters Gallin and Janssen

Theorem conversion For any IL term x if either of the following conditions

^

holds is rigid no free o ccurrence of x in lies in the scop e of or h i then

x x

Here means semantic equivalence

Let us see what go es wrong with conversion when neither condition ab ove holds

^

Example Let k k N S B let b e the current state and let

0

_

x S N b e any variable such that x Consider the term x Now

1 1 0 1

^ _

conversion of this term would lead to x Note that this conversion violates b oth

1

_

restrictions in the ab ove theorem But as can easily b e seen x tt the

1 0



^ _

x constantly true predicate whereas x

1 1 0

Note that this problem is related to the one discussed in x substituting nonrigid terms

in intensional opaque contexts

For later use we also state

_ ^

oposition cancellation For any ILterm Pr

_ ^

Translation of programming language into IL We extend the translation of x

We rst consider blo cks

S N and l N S N for the translation of We have two sp ecial ILvariables j

newblo cks Intuitively l is an array and j is an index which p oints to next available element in

l

The alias blo ck is simple

0 0 0

b egin alias x u S end x S u

the new blo ck less so



0 0

b egin new x e S end q n x S l n



_ 0 _ ^ _

q hj j ihl ne i n j

_

Intuitively this says that there is a numb er n j such that execution of the new blo ck is

equivalent to the execution of the following statement

j ln e Sxln j

where Sxln denotes the replacement of all free x in S by ln

Remark A suggestion for a simpler translation of new blo cks might b e

0 0 0

b egin new x e S end x S e

By simple typ e checking we would discover that this is incorrectly typ ed and come up instead

with

0 0 ^ 0

b egin new x e S end x S e

^ 0

But this is also incorrect b ecause e is rigid and hence p ermits a conversion by the Theorem

_ ^ 0

in x which by cancellation will result in Sxe which is clearly incorrect since this

could result in substituting e for x on the lhs of an assignment in S

The translation of pro cedures with the two sorts of parameters pro ceeds similarly With each

pro cedure identier we asso ciate an IL variable whose value is a function from parameters to

predicate transformers The typ e of value parameters is N and the typ e of reference parameters

is S N The translation thus has the form

v v

P p S N

r r

P p S S N

where S B B the typ e of predicate transformers Then pro cedure calls are

translated by

v _ v 0

P e p e

r _ r 0

P u p e

Pro cedure b o dies are translated by



0 0

hval xi S end m q n x S l n



^ _ _ _

n j q hj j ihl nmi

0 0

href xi S end y S

Note the analogy with the translations of the corresp onding blo cks This gives semantic supp ort

to the Corresp ondence Principle

Finally programs are translated by

0 ^ 0 ^ 0

hP B P B S i p p S B B

1 1 k k 1 k

k 1

The semantics of a program is then that induced by its translation Again since the translation

is a homomorphism of the relevant term algebras and the semantics of IL is comp ositional the

induced semantics of P r og Lang is comp ositional

Lemma for state switchers and expressibility Again as in x we can prove a Substitution

of the weakest precondition

Pointers Pass by name Macro blo cks

We extend P r og Lang with a numb er of concepts involving double intensionality

ointers First we intro duce pointer variables z which can o ccur on the lhs of an P

assignment either in the form z v or in the dereferenced form dref z e Also dref z

may form part of an arithmetical expression

0

With each p ointer variable z we asso ciate an ILvariable z S S N Then the

translation of assignments is extended to the cases

_ 0 0

z v q q hz v i

_ _ 0 0

dref z e q q h z e i

Note that the second case is merely a sp ecial case of our previous rule x for translating

assignments

__

Further the translation of dref z viewed as an arithmetical expression is z N a

doubly dereferenced version of z

Pro cedures with passbyname parameters We intro duce passbyname pro cedure

n

identiers P with pro cedure b o dies

n

h name B z i S end

n n

and declarations P B The formal name parameter z may o ccur in the extensionalized

form expand z inside the b o dy S

For the translation into IL we asso ciate with each formal name parameter z an IL vari

0 n n

able z S S N and with each pro cedure identier P an IL variable p S

S S N where again S B B

_ 0

Then expand z is translated as z and passbyname b o dies by

0 0

h name z i S end z S

blo cks In accordance with the Corresp ondence Principle we intro duce macro Macro

blo cks to match passbyname parameters

n

b egin macro K z v S end

where again S may contain the extensionalized form expand z

The translation of macro blo cks is analogous to that for passbyname parameters

0 0 ^ 0

b egin macro z v S end z S v

Results As b efore we obtain comp ositional semantics for the extended language We

also obtain a Substitution Lemma for state switchers and hence expressibility of the weakest

precondition by nontrivial extensions of the relevant pro ofs for the language of Section

Future work

We plan to extend our work in the following directions

Compare our semantics with operational semantics for the languages considered here

Extend our approach to higher order procedures ie pro cedures with procedure parameters

Apply this metho d also to objectoriented languages for which comp ositional semantics seem

to b e lacking

References

J W de Bakker Mathematical Theory of Program Correctness Prentice Hall

R Carnap Meaning and Necessity nd edition The University of Chicago Press

A Church A formulation of the logic of sense and denotation in Structure Metho d

ys in Honor of Henry M Sheer P Henle et al ed Lib eral Arts and Meaning Essa

ork Press New Y

E W Dijkstra A Discipline of Programming Prentice Hall

D R Dowty R E Wall S Peters Intro duction to Montague Semantics D

Reidel

Frege Ub er Sinn und Bedeutung Zeitschrift fur Philosophie und philosophische G

Kritik translated as On in Translations from the

ritings of Gottlob Frege PT Geach M Black eds Basil Blackwell Philosophical W

Oxford

Mo dal Logic NorthHolland D Gallin Intensional and HigherOrder

H K Hung Application of intensional logic to program semantics in Pro ceedings of

Amsterdam Collo quium Decemb er the th

Comp ositional Semantics and Program Correctness for Pro cedures with Pa

rameters PhD Thesis Computer Science Department SUNYBualo Technical Rep ort

H K Hung J I Zucker Semantics of p ointers referencing and dereferencing

with intensional logic in Pro ceedings of the th Annual IEEE Symp osium on Logic in

Amsterdam Computer Science July

T M V Janssen Foundations and Applications of Part Phi

Centre for Mathematics and losophy Framework Computer Science CWI Tract

Computer Science Amsterdam

T M V Janssen P van Emde Boas On the prop er treatment of referencing

International Collo quium on Automata Lan dereferencing and assignment in th

ersity of Turku Finland A Salomaa M Steinby guages and Programming Univ

eds Lecture Notes in Computer Science SpringerVerlag

S A Kripke Semantical considerations on mo dal logic Acta Philosophica Fennica

R Montague The prop er treatment of quantication in ordinary English in Formal

Philosophy Selected Pap ers of Ric hard Montague RH Thomason ed Yale University

Press

R D Tennent Principles of Programming Languages Prentice Hall

J Zwiers Comp ositionality Concurrency and Partial Correctness Pro of Theories on

Networks of Pro cesses and Their Relationships Lecture Notes in Computer Science

SpringerVerlag