NIL. In the case that applying the denotation of a statement to a state results in no "goto", the respective range element will be the result state paired with NIL.
DENOTATIONALSEMANTICSOF GOTO: AN EXIT FORMULATION AND ITS RELATION TO CONTINUATIONS
Cliff B.Jones
Abstract: This paper discusses the problem of providing a defini= tion for the "GOTO" statement within the framework of denotational
semantics.
The accepted approach to the
problem is to use "Continuations".
An alternative "Exit
Formulation" is described in this paper. A small language is introduced which illustrates the difficulties caused by statements which terminate abnormally.
For this lan=
guage definitions based on both approaches are provided. A proof of equivalence of the two definitions is then given. In a closing discussion it is pointed out that continuations can define a wider class of languages than exits, although the latter have been shown to be adequate to define languages as complex as PL/I.
CONTENTS
l°
Introduction
Z80
2.
Notation
281
3.
A Small Language
282
4.
Definition By Exit
285
5.
Definition By Continuations
289
6.
Equivalence of the Two Definitions
292
7.
Discussion
302-304
Fig.l:
The Language
Fig.2:
Definition Using Exit Combinators
Fig.3:
Definition by Exit M e c h a n i s m w i t h Combinators
Fig.4:
Definition
Using Continuations
Fig.5:
Definition
Using Continuations
to be Defined
283 286
Definition Using Continuations Recomputed
Fig.7:
-293 --
at Each C o m p o u n d Statement
Definition Using
(E) Continuations
W i t h o u t Environments
289 291
with Merge of c - c and c - l Fig.6:
Expanded
296
-300
280
i. I N T R O D U C T I O N
There exists by now a c o n s i d e r a b l e body of w o r k on the formal definition of p r o g r a m m i n g languages
(see Lucas 78, in this volume,
for a hi-
storical review). M o s t of the w o r k can be c a t e g o r i s e d as either "abstract interpreters",
"denotational semantics" or "axiomatic"~ This
p a p e r attempts to make a c o n t r i b u t i o n to the u n d e r s t a n d i n g of denotational semantics. This a p p r o a c h is p a r t i c u l a r l y
a s s o c i a t e d w i t h the
P r o g r a m m i n g R e s e a r c h G r o u p at Oxford University.
E v o l v i n g from the
earlier work on abstract interpreters
(see Lucas 69) the m o r e recent
w o r k of the V i e n n a L a b o r a t o r y has also u s e d the d e n o t a t i o n a l style (see Beki6 74).
A l a n g u a g e can be given by the set of its texts. To define the semantics of a l a n g u a g e one m u s t a s s o c i a t e a m e a n i n g or d e n o t a t i o n w i t h each text in the set. Since the set of texts will, languages, be of i n f i n i t e cardinality,
for i n t e r e s t i n g
this link w i l l be shown by de-
fining a function from the set of textstto a set of d e n o t a t i o n s . such a d e f i n i t i o n of a large language to be comprehensible,
For
it is re-
q u i r e d that the d e n o t a t i o n of a c o m p o u n d text should d e p e n d solely on the d e n o t a t i o n s of its c o m p o n e n t parts. (Clearly, this rule m u s t be applied ohly to a sensible level: a s c r i b i n g m e a n i n g to single characters and then t r y i n g to c o n s t r u c t the m e a n i n g of identifiers and k e y w o r d s is u n l i k e l y to prove illuminating.
The limit of sensible d e c o m p o s i t i o n
is u s u a l l y indicated by the a b s t r a c t s y n t ~
of a language.)
In order
for a f u n c t i o n from texts to d e n o t a t i o n s to define the semantics of the texts,
it is o b v i o u s l y a p r e - r e q u i s i t e that the d e n o t a t i o n s
themselves
should be objects w i t h known meanings. One c h a r a c t e r i s t i c of d e n o t a t i o nal semantics is the use of m a t h e m a t i c a l objects
(especially functions)
as the denotations.
This accounts
m a t i c a l semantics".
In fact the functions chosen as d e n o t a t i o n s are
for the a l t e r n a t i v e name of "mathe-
of a very general form and it has b e e n a c o n s i d e r a b l e task to show that such functions do indeed h a v e a c o n s i s t e n t m e a n i n g
(see Scott 71).
T h e r e is, w i t h i n the " d e n o t a t i o n a l school", a g r e e m e n t as to the concepts r e q u i r e d to p r o v i d e d e f i n i t i o n s of simple languages. a number of notatiQnal d i f f e r e n c e s
lead to d i f f e r e n c e s
of c o n c e p t u a l l y similar definitions,
see next section).
(However,
in the a p p e a r a n c e Reme~nbering the
"denotational rule" that d e n o t a t i o n s of c o m p o s i t e objects should be b u i l t from the d e n o t a t i o n s of their components,
the following o b s e r v a -
tions can be made. For a p u r e l y functional l a n g u a g e it is easy to agree a definition;
for a language w h i c h includes a n assignment c o n s t r u c t
281
the c o n c e p t of a store
(i.e. a m a p p i n g from identifiers to values)
per-
mits d e n o t a t i o n s to be d e f i n e d w h i c h are functions from stores to stores;
the g e n e r a l l y a c c e p t e d a p p r o a c h to block s t r u c t u r e d languages
is to i n t r o d u c e locations and e n v i r o n m e n t s . W h i l s t refs Mosses 74 and Beki~ 74 build from this basic list of a g r e e m e n t s and tackle similar large languages, a n i m p o r t a n t d i f f e r e n c e can be found.
The i m p o r t a n t d i f f e r e n c e b e t w e e n the Oxford and V i e n n a groups can be f o u n d in their a p p r o a c h to p r o b l e m s of abnormal termination. The arc h e t y p a l p r o b l e m in this c a t e g o r y is the "goto" statement. A d e f i n i tion c o n f o r m i n g to the d e n o t a t i o n a l rule is d i f f i c u l t to c o n s t r u c t for languages w h i c h include goto statements p r e c i s e l y b e c a u s e their effect is to t r a n s f e r control across the s t r u c t u r e over w h i c h the d e n o t a t i o n s are b e i n g constructed.
(This p o w e r of goto statements has led to a
m o v e m e n t for their elimination. here.
This c o n t r o v e r s y is not e n t e r e d into
Rather, models are e x p l a i n e d w h i c h are general enough to model
goto. On such m o d e l s one can then compare a l t e r n a t i v e language constructs w h i c h m i g h t offer the d e s i r a b l e features, w i t h o u t the danger, of goto statements. Of course,
a language feature may e v e n t u a l l y be
s e l e c t e d for w h i c h simpler models are possible. W h a t the m o d e l s here offer is a basis from w h i c h to work.) tions"
The O x f o r d group use "continua-
(see Strachey 74) to d e f i n e g o t o - l i k e constructs:
tion in section 5 b e l o w is in this guage is d e f i n e d u s i n g the V i e n n a
the d e f i n i -
style. The same small example lan"exit" a p p r o a c h in section 4. The
language itself is introduced i n f o r m a l l y in section 3 after some comments on notation. G i v e n two d e f i n i t i o n s of the same language, q u e s t i o n of their r e l a t i o n s h i p can be posed:
the
e q u i v a l e n c e is p r o v e d in
section 6. W h i l s t both c o n t i n u a t i o n s and exits h a v e been shown to be p o w e r f u l enough to d e f i n e c o m m o n l y used p r o g r a m m i n g languages, two a p p r o a c h e s are not of e q u i v a l e n t power,
the
section 7 contains some
c o n c l u d i n g c o m m e n t s on this point.
2. N O T A T I O N
The basis of the n o t a t i o n to be u s e d in this p a p e r is taken from logic and lambda calculus and will p r o b a b l y be familiar from the literature. The items of special i n t e r e s t w i t h i n this p a p e r are i n t r o d u c e d b e l o w as required. A m o r e c o m p l e t e e x p l a n a t i o n is a v a i l a b l e elsewhere in this v o l u m e
(Jones 78a); Jones 75 p r o v i d e s a s t e p w i s e d e v e l o p m e n t of
the exit concept.
282
One of the m o s t d a n g e r o u s traps
when
c o m p a r i n g two languages is to
let the superficial syntactic d i f f e r e n c e s confuse the real issue w h i c h is that of meaning. The d i f f e r e n c e in a p p e a r a n c e b e t w e e n the O x f o r d and V i e n n a d e f i n i t i o n s is very striking. The former group
has achiev-
ed s u c c i n c t n e s s in o r d e r to f a c i l i t a t e formal r e a s o n i n g about smaller definitions, w h i l s t the tasks tackled by the V i e n n a group have led them to strive for readability.
This paper, b e i n g b a s e d on a very small
language, c o m p r o m i s e s a little for the sake of compactness. Thus short names are u s e d for the syntactic classes and "" is used instead of a named
(tree) c o n s t r u c t o r w h e r e c o n t e x t makes the choice clear
(the con-
vention for d r o p p i n g semantic rules for syntax classes d e f i n e d to be a list of a l t e r n a t i v e s is also followed). O t h e r than this the d e f i n i tions, even that by continuations,
are given in a V i e n n a - s t y l e .
The
issue to be r e v i e w e d is the d i f f e r e n c e s b e t w e e n the d o m a i n s and function types. Choices like the use of d i f f e r e n t b r a c k e t symbols,
expli-
cit versus i m p l i c i t typing of functions and the d e g r e e of a b s t r a c t n e s s for the syntax m i g h t influence the number of c h a r a c t e r s in a d e f i n i t i o n but would,
if u s e d on one of the definitions,
serve only to cloud the
m a i n distinction.
3. A SMALL L A N G U A G E
This section introduces the language w h i c h will be used as the basis for the r e m a i n d e r of the paper. The basic statements of the language are "goto" and an u n a n a l y z e d class of e l e m e n t a r y statements. A b o u t these latter all n e c e s s a r y k n o w l e d g e is g i v e n by the f u n c t i o n "el-sem" which associates a state-transformation class Z) w i t h each m e m b e r of El-stmt.
(i.e. a function over the
Statements can be o p t i o n a l l y
named and lists thereof can be formed into c o m p o m n d statements.
Such
compound statements are also statements and thus can be named and used as elements of other lists. The a b s t r a c t syntax of the language is given,
along w i t h a full name for each class of objects to aid compre-
hension,
in f~g.
I. The s t r u c t u r e of the classes Id and E l - s t m t is not
further defined.
~:
In this paper a s u p e r s c r i p t 0 is to be read as the functional c o m p o s i t i o n operator. It is e l s e w h e r e r e p r e s e n t e d by the fat dot: ,.
283
P
Program
::
C
Compound statement
C
::
s-b:Ns ~
Named s t a t e m e n t
N8
::
s-n:[Id]
statement
s
:
cI
Goto s t a t e m e n t
G
::
Id
Elementary statement
E1
::
El-stmt
Identifiers
Id
IE
A b s t r a c t syntax
Full name
Fig.
s-b:S
I: The L a n g u a g e to be D e f i n e d
In o r d e r to f a c i l i t a t e d i s c u s s i o n of the i d e n t i f i e r p r e f i x e s of statements,
two p r e d i c a t e s w h i c h check for the
(direct and indirect)
con-
t a i n m e n t of identifiers and a f u n c t i o n y i e l d i n g the index of that s t a t e m e n t w h i c h c o n t a i n s an identifier (under the a s s u m p t i o n that it is c o n t a i n e d somewhere)
is~doontCid, nsI)~=> type:
Id Ns*
~
are introduced:
(Bi6{1 -lenr~sl}) Cs-nOnsl(i) = id)
Bool
is-cont(id, ns l) ~
(is-dcont (id, nsl) v (Bi£~l:,,lennsl})(s-bO-nsl(i) E C & is-cont(id, s-bOs-bOnsl(i))))
type:
Id Ns • ~
Bool
ind( id, ns I)= is-doont(id, nsl) T
~-
~
(li) (s-nOnsl(i)=id)
(li)(s-bOnsl(iJ £ C
type:
Id N8~
pre:
i8-eont(id, nsl)
~
Nat
& is-cont(id, s-bOs-bOnsl(i)))
284
It is assumed that w e l l - f o r m e d programs satisfy the context condition that all label identifiers used in goto statements are c o n t a i n e d exactly once w i t h i n the program. With respect to the statement list w i t h i n w h i c h the goto is placed,
the target s t a t e m e n t may be w i t h i n the same
list, w i t h i n a c o n t a i n i n g list or w i t h i n a c o m p o u n d statement w h i c h is a member of the same list~ The local "hop" and the abnormal exit from a list should require no comment. The ability to jump into a p h r a s e s t r u c t u r e is allowed, b e c a u s e it is included in m a n y p r o g r a m m i n g languages
(cf. Algol
60 "goto" into b r a n c h e s of c o n d i t i o n a l state-
ments.)
The choice of features in this language has b e e n m a d e w i t h some care in order to exhibit m o s t of the c o m p l e x i t y of large languages in a framework of reasonable size. Thus the ability to hop b e t w e e n elements of a list has been s u p p l e m e n t e d by p e r m i t t i n g goto statements to enter and leave syntactic units. In fact if the reader compares this language to Algol 60 (see Henhapl 78 in this volume)
only the ability to pass
labels and p r o c e d u r e s as p a r a m e t e r s forces an e x t e n s i o n of the ideas used here. A b n o r m a l t e r m i n a t i o n of a block via a goto s t a t e m e n t is a s t r a i g h t f o r w a r d e x t e n s i o n and the problems a s s o c i a t e d w i t h r e d e f i n i tion of names in a b l o c k - s t r u c t u r e d language can be solved in a uniform way for v a r i a b l e and label identifiers
(see Beki~ 74 for a dis-
c u s s i o n of label variables).
The e l e m e n t a r y statements of the language are a s s u m e d to cause changes to a class of states
el-sem:
Were
(Z):
El-stmt ~
(Z ~ Z)
it not for the inclusion of the "goto" construct,
it w o u l d be
s t r a i g h t f o r w a r d to p r o v i d e a d e f i n i t i o n w h i c h a s s o c i a t e d a t r a n s f o r m a t i o n w i t h any elements of S.
(The d e n o t a t i o n of a list of statements
b e i n g the c o m p o s i t i o n of the d e n o t a t i o n s of the e l e m e n t s of the list.) W h i l s t as a result of the c o n t e x t c o n d i t i o n given above, the d e n o t a t i o n of a w h o l e p r o g r a m w i l l be such a transformation,
it is not p o s s i b l e
to ~scribe such a simple d e n o t a t i o n to "goto" statements. sections offer d i f f e r e n t solutions to this problem.
The next two
285
4. D E F I N I T I O N BY E X I T
The d i f f i c u l t y of finding a suitable d e f i n i t i o n for a language w h i c h includes goto s t a t e m e n t s is that its a b i l i t y to cut across syntactic units forces changes on the semantics of all such units. A m o t i v a t i o n of the exit a p p r o a c h to be defined in this section was to m i n i m i z e the e f f e c t Of these changes on the overall a p p e a r a n c e of a definition.
The
key to a c h i e v i n g the d e s i r e d e f f e c t w i t h o u t w r i t i n g it into all of the semantic equations is to d e f i n e a p p r o p r i a t e combinators. simple language
(i.e. one w i t h o u t goto statements)
Thus in a
a c o m b i n a t o r denot-
ing f u n c t i o n a l c o m p o s i t i o n m i g h t be w r i t t e n ";". If this same symbol is r e i n t e r p r e t e d as the more c o m p l e x c o m b i n a t o r used below,
a defini-
tion for a language w i t h goto statements can p r e s e r v e a simpler app e a r a n c e except, of course,
for those semantic e q u a t i o n s w h i c h deal
s p e c i f i c a l l y w i t h goto statements.
The basic idea of d e f i n i t i o n by exit is to associate a d e n o t a t i o n with e a c h statement w h i c h is a function of type:
E
=
Z~ZA
=
Idl
where:
A
N~L
Thus the d e n o t a t i o n of a s t a t e m e n t is a f u n c t i o n from states to pairs: the first e l e m e n t is a state and the second is either an i d e n t i f i e r or
NIL. In the case that a p p l y i n g the d e n o t a t i o n of a s t a t e m e n t to a state results in no "goto", the r e s p e c t i v e range e l e m e n t will be the result state p a i r e d w i t h NIL. If, however,
a "goto" is e n c o u n t e r e d to a label
not c o n t a i n e d w i t h i n the statement,
the range e l e m e n t w i l l pair the
state r e f l e c t i n g state t r a n s i t i o n up to the time of the "goto" w i t h the target label.
The definition,
using c o m b i n a t o r s w h o s e m e a n i n g is m a d e p r e c i s e belowr
is given in fig 2. The f u n c t i o n names all b e g i n w i t h that they are
part
"x-" to signify
of the exit-style definition. It is not d i f f i c u l t
to p r o v i d e an i n t u i t i v e u n d e r s t a n d i n g of this definition.
The func-
tion x-g w h i c h d e f i n e s the s e m a n t i c s of goto statements uses the "exit" combinator (identifier)
w h i c h simply pairs the a r g u m e n t state w i t h the given value. W h e r e v e r
a simple
(Z~Z) f u n c t i o n is shown it is
i n t e r p r e t e d as y i e l d i n g NIL p a i r e d w i t h w h a t e v e r the o u t p u t state
286
x-p()
x-e(ep)
=
=
x-c(cp)
x-cp(NIL, cp)
x-cp(ido,)
=
tixe [ id~x-l(id, nsl) in
I ia-cont(id, nsl)
]
x-l(ido, nsl)
x-l(ido, nsl) = ido=NIL
x-nsl(nsl, i)
i8-deont(ido, nsl)~
x-nsl(nsl, ind(ido,nsl))
T
(let i = ind(ido, nsl) x-cp (ido, s-b (nsl (i) ) ); x-nsl(nsl, i+l) )
x-nsl(nsl, i) = i)
fox - 1 (ido, ns l)
x-l(ido,nsl)
=
ido=NIL
~ x-ns I (ns l, 1 )
i 8 - d c o n t ( i d o , nsl) ~ x - n s l ( n s l , ind(ido, nsl)) T
~
(let i = ind(ido, nsl) (la, ido '. (ido '=NIL ~ x-nsl(nsl, i+l) (s) , T ~ )) 0
x-s(s-b(nsl(i)))) T
~ h~.
x-g()
x-el()
x-p,
x-c
=
la.
=
h~.
unchanged
F i g 3: Definition by exit mechanism with combinators expanded
5. D E F I N I T I O N
BY C O N T I N U A T I O N S
This section introduces the more widely used continuation approach for the definition of languages which include goto statements. As with exits, this approach recognises that denotations of type Z~Z will not
290
suffice. W h i l e c o n t i n u a t i o n s themselves are:
T = Z ~ Z
the denotations of s t a t e m e n t - l i k e constructs become:
T~T
The q u e s t i o n of the m e a n i n g of goto s t a t e m e n t s is handled by a s s o c i a t ing c o n t i n u a t i o n s w i t h identifiers.
The d e n o t a t i o n of a goto state:
m e n t for any c o n t i n u a t i o n is then the c o n t i n u a t i o n a s s o c i a t e d w i t h the c o n t a i n e d identifier.
In a c o m p l e x l a n g u a g e definition, b l o c k struc-
ture w o u l d anyway force the use of an e x p l i c i t e n v i r o n m e n t a r g u m e n t to the semantic functions and this can be used to record the a s s o c i a t e d c o n t i n u a t i o n for labels. Thus,
Env
=
Id
Intuitively,
~
in the current case:
T
one can consider statement d e n o t a t i o n s as yielding,
a given s u b s e q u e n t c o m p u t a t i o n
(i.e. continuation),
for
the overall compu-
tation s t a r t i n g at this statement. Notice that this is not simply the c o m p o s i t i o n of two functions of type Z~Z b e c a u s e of the p o s s i b i l i t y of "goto". The label d e n o t a t i o n s are the transitions r e s u l t i n g from starting e x e c u t i o n at that label and e x e c u t i n g to the end of the program. Thus a f u n c t i o n of type Z~Z is a s s o c i a t e d w i t h a text given a particular e n v i r o n m e n t and continuation. A m o r e c o m p l e t e d e s c r i p t i o n of the m e t h o d of c o n t i n u a t i o n s
is given in S t r a c h e y
t i n u a t i o n s is given in f i g w h i c h are c o n t i n u a t i o n s
4.
The d e f i n i t i o n by con-
?4.
(The use of braces to b r a c k e t arguments
is adopted for the b e n e f i t of the reader.)
Since there are no c o m b i n a t o r s to be explained in this definition, i n t u i t i v e e x p l a n a t i o n is offered.
this style of d e f i n i t i o n is, however, carefully
no
The reader w h o is u n f a m i l i a r with advised to study this d e f i n i t i o n
(possibly w i t h the aid of an example)
to be sure he has
grasped the rather b a c k - t o - f r o n t c o n s t r u c t i o n of denotations. types of these semantic functions are:
c-p:
P
~
T
c-c:
C
~
(Env
~
(T ~
T))
a-nsl:
Ns ~ Nat
~
(Env
~
(T ~
T))
c-8:
S
~
(Env
~
(T ~
T))
(assumed)
The
291
c-p()= let env 0 = [id~c-l(id, s-b(cp))(enVO){I}
I is-cont(id, s-b(cp))]
c-c(cp) (enVo){I }
c-c6)
=
c-nsl(nsl, 1)
c-nsl(nsl,i) (env) {c}= i } l i s - c o n t ( i d , 0 = (aCdome~r e ( a ) ( s ) , T ~ X c . < s , a > )
nsl)]
let r(~,a)
rOf-l(ido,nsl){hs.}
f-l(ido,nsl){c}= ido=NIL
~ f-nsl(nsl, 1){c}
is-dcont(ido,nsl)
~ f-nsl(nsl, ind(ido, nsl)){c}
T
~
(let i = ind(ido, nsl) f-cp(ido, s-bOnsl(i) ) {f-nsl(nsl, i+l) {c}} J
f-nsl(nslji){c} = i~lennsl
~
f-s(s-bOnsl(i)){f-nsl(nsl,
T
~
e
f-g(){c}
f-el(){c}
fig
=
i+l){c}}
Xs.
=
cOel-sem(eIJ
?: Definition using
(E) continuations
The "f-" functions have the types: f-p:
P
~ E
f-c:
C
~ (E "~ E)
f-cp:
[Id] C
~ (E -~ E)
e:
Id
~ E
without environments.
301
r:
~ [Id]
f-l:
[Id] Ns ~ ~
~ E
f-nsl:
Ns • N a t
~ (E ~ EJ
f-s:
S
~
(E ~ E)
(assumed)
(E ~ E)
This definition presents one in which the earlier differences
(i) and
(ii) have been eliminated and which only requires the equivalence of
the alternative directions
for computing denotations to be established
to complete the equivalence proof to the "x-" functions of section 4.
The approach to this last difference is similar to that taken at the previous stage. Firstly a lenhma is introduced which shows that the "f-" functions are equivalent to a composition of a test and the corresponding
"x-" function. Whereas in the previous stage the test was
a simulation of the "tize" combinator,
this stage is simulating the
".," combinator. Applying this lemma generates a set of functions could be written out as "g-" functions)
(which
which pass the same constant
contlnuation" of lo. to all functions. Once again this constant can be dropped and written directly in the two places where the argument had previously been used. We then have precisely the expanded form of the "x-" functions from section 4. Formally,
the lemma is
define:
t(c) = la, a . ( a = N I L
show for:
ft(xt) is f - s ( x - s ) , f - n s l ( x - n s l ) t(c)Oxt(s) = ft(s){c}
that:
the proof
(not given here)
~ c ( ~ ) , T ~ )
or f - l ( x - 1 )
is by a similar induction to that of Lemma
I.
Using Lemma II:
f-nsl(nsl, i){c}= i