Objects, Interference, and the Yoneda Embedding
Total Page:16
File Type:pdf, Size:1020Kb
Syracuse University SURFACE College of Engineering and Computer Science - Former Departments, Centers, Institutes and College of Engineering and Computer Science Projects 1995 Objects, Interference, and the Yoneda Embedding Peter W. O'Hearn Syracuse University Uday S. Reddy University of Illinois at Urbana-Champaign Follow this and additional works at: https://surface.syr.edu/lcsmith_other Part of the Programming Languages and Compilers Commons Recommended Citation O'Hearn, Peter W. and Reddy, Uday S., "Objects, Interference, and the Yoneda Embedding" (1995). College of Engineering and Computer Science - Former Departments, Centers, Institutes and Projects. 14. https://surface.syr.edu/lcsmith_other/14 This Article is brought to you for free and open access by the College of Engineering and Computer Science at SURFACE. It has been accepted for inclusion in College of Engineering and Computer Science - Former Departments, Centers, Institutes and Projects by an authorized administrator of SURFACE. For more information, please contact [email protected]. Electronic Notes in Theoretical Computer Science to app ear Ob jects Interference and the Yoneda Emb edding Peter W OHearn Syracuse University Uday S Reddy University of Il linois at UrbanaChampaign Dedicated to John C Reynolds in honor of his th birthday Abstract We present a new semantics for Algollike languages that combines metho ds from two prior lines of development the ob jectbased approach of where the meaning of an imp erative program is describ ed in terms of sequences of observable actions and the functorcategory approach initiated by Reynolds where the varying na ture of the runtime stack is explained using functors from a category of store shap es to a category of cp os The semantics gives an account of b oth the phemomena of lo cal state and irre versibility of state change As an indication of the accuracy obtained we present a full abstraction result for closed terms of secondorder typ e in a language containing active expressions ie valuereturning commands Intro duction In his inuential Turing award lecture John Backus criticized imp erative programming languages for promoting a view of programming as wordata time pro cessing John Reynolds expressed his resp onse to this criticism in a meeting of IFIP working group in around which he rep eated to several p eople privately including the second author The view put forward by Backus Reynolds said is that imp erative programming is like working with 1 Supp orted by NSF grant CCR 2 Supp orted by NSF grant CCR c Elsevier Science B V OHearn and Reddy pigeon holes All that one do es is to take a pigeon out from a hole or to put a new pigeon in a hole But with ob jectoriented programming he said one works with turkey holes rather than pigeon holes Instead of taking out a pigeon or putting in a pigeon one do es more sophisticated manipulations such as rotate a turkey or tilt a turkey The turkey holes that Reynolds sp oke of are what programmers call ob jects They incorp orate some physical resources such as memory and provide op erations for the manipulation of these resources Programs are built by putting such ob jects together and letting them invoke each others op era tions The state of an ob ject cannot b e manipulated by other ob jects without the co op eration of the ob ject itself in terms of the op erations it provides Such an ob jectbased view we nd is implicit throughout Reynoldss work on imp erative programming In his seminal pap er on Algollike languages Reynolds treats pro ce dures not as actions on the global state but as actions on the state at the p oint of their denitions To elab orate a pro cedure can only access the ob jects that have b een allo cated when the pro cedure is dened Other ob jects created after the p oint of denition but b efore the pro cedure is called do not aect its b ehavior Every pro cedure lives in its own turkey hole so to sp eak Reynolds also shows how to treat variables pigeon holes as a sp ecial case of turkey holes ob jects with op erations for setting and reading values stored in them This essentially frees imp erative programming from the limitations suggested by Backus and sets up a truly ob jectbased paradigm for thinking ab out imp erative programs Reynoldss program for the semantics of imp erative languages was further develop ed by Oles and Tennent and continued and expanded in a numb er of works In a separate line of development a mo del based more explicitly on a notion of ob jects has b een formulated in Reynoldss conception of imp erative programming expressed ab ove formed an imp ortant pretheoretic motivation for this work though its theo retical development also draws inspiration from linear logic syntactic control of interference and the relation b etween them In this pap er we obtain a new semantics for Algollike languages via a synthesis of these two lines the ob jectbased approach of where the meaning of an imp erative program is describ ed in terms of sequences of observable actions and the functorcategory approach initiated by Reynolds where the varying nature of the runtime stack is explained using functors from a category of store shap es to a category of cp os In the remainder of this intro ductory section we give an informal overview of the construction and discuss the sp ecic semantic issues addressed by it Semantic issues Locality and irreversibility In imp erative computation there is an idea of destroying information by over writing parts of computer memory This is clearly imp ortant for implementa OHearn and Reddy tion But supplying direct access to assignment in the programming language also results in p ositive information that programmers make use of Consider a parameterless pro cedure gensym that returns a dierent integer each time it is called In reasoning ab out a program using gensym for instance gen erating fresh names when implementing substitution in calculus we would use the prop erty that any call to gensym returns an integer that was not returned by it previously This prop erty exemplies one of the most basic intuitions ab out state the general irreversibility of state change By this we mean not only that p ortions of the store are destructively up dated during the course of a compu tation but that in the presence of abstraction or lo cal state this irreversibility manifests itself in observable prop erties of programs A typical implementation of gensym would use a lo cal integer variable that is incremented on each call When we say that gensym returns a dierent integer each time it is called it is crucial that other pro cedures or ob jects do not access the lo cal state of gensym directly and reset the value to a previouslyencountered one This statement ab out the gensym pro cedure implicitly involves interactions b etween the pro cedure and any other pieces of a program The following co de illustrates the kind of prop erty of such interactions we have in mind b egin integer x integer pro cedure gensym f x x returnx g x P gensym if gensym then diverge end A client pro cedure P is passed a parameterless pro cedure gensym for generating new names P can use its argument a numb er of times we are assuming callbyname though the eect can obviously b e simulated in call byvalue and if it uses its argument at least once then we exp ect that the whole blo ck will diverge Since the nonlo cal pro cedure P cannot access the lo cal variable x if x is up dated by calling gensym then pro cedure P has no way of resetting its value to zero It follows by intuitive reasoning that this blo ck should have terminationnontermination b ehavior equivalent to P diverge This co de is not a realistic program but it is interesting for the reasoning principle it illustrates Generally when we have an ob ject consisting of some internal state and observable op erations it is not p ossible for a client program to cause the internal state of the ob ject to backtrack to previous states This is b ecause the only changes to the internal state that the client can p ossibly eect come ab out by using the provided op erations The observable ramications of irreversibility of state change are inextricably b ound up with lo cality Irreversibility has proven dicult to capture in semantics b ecause most mo dels allow for snapback op erations These op erations work by accepting a OHearn and Reddy pro cedure as an argument running the pro cedure and then restoring the state to the value it had b efore the argument was executed this would contradict the reasoning ab out gensym ab ove The snapback eect requires restoration of even lo cal state The phenomenon of irreversibility is not so clear cut in languages that violate the abstractness of lo cal state such as C or when programming on a system level where one might want access to the entire computer memory One could in some instances achieve the eect of snapback by a series of incremental state changes But on the level of programmable ob jects where abstraction is central irreversibility is a familiar phenomenon one that arises in Scheme ML Algol and most ob jectoriented languages These intertwined notions of irreversibility and lo cality are fundamental and should b e accounted for by a satisfactory theory of state Overview of Approach Objects plus Yoneda The mo del presented here builds up on the work rep orted in where a semantics is presented based on identifying an imp erative computation with a stream of observations For example commands are mo delled not as state tostate functions but as sequences