A Modular Monadic Action Semantics
Total Page:16
File Type:pdf, Size:1020Kb
The following paper was originally published in the Proceedings of the Conference on Domain-Specific Languages Santa Barbara, California, October 1997 A Modular Monadic Action Semantics Keith Wansbrough and John Hamer University of Auckland For more information about USENIX Association contact: 1. Phone: 510 528-8649 2. FAX: 510 548-5738 3. Email: [email protected] 4. WWW URL:http://www.usenix.org A Modular Monadic Action Semantics Keith Wansbrough John Hamer Department of Computer Science University of Auckland Auckland, New Zealand g fkeith-w,j-hamer @cs.auckland.ac.nz Abstract clearly and precisely defined; modular enough to change A domain-specific language (DSL) is a framework incrementally; and amenable to formal reasoning. Differ- which is designed to precisely meet the needs of a particu- ent tools exist that address these varied requirements, but lar application. Domain-specific languages exist for a vari- few make any attempt to address them all. Compiler gener- ety of reasons. As productivity tools, they are used to make ators provide efficient implementations, but they typically application prototyping and development faster and more have weak formal properties. We do not consider such sys- robust in the presence of evolving requirements. Further- tems further in this paper. Semantic formalisms, such as more, by bridging the “semantic gap” between an applica- denotational semantics and structural operational seman- tion domain and program code, DSLs increase the oppor- tics, have richly developed theories but are difficult to use tunity to apply formal methods in proving properties of an and lack modularity. application. The conceptual distance between the high-level language In this paper, we contribute a synthesis of two existing and established semantic formalisms is huge. Writing such systems that address the problem of providing sound se- specifications is a tedious and difficult task. Natural con- mantic descriptions of realistic programming languages: cepts in the high-level language must be accurately simu- action semantics and modular monadic semantics. The lated by constructs built from the small range of primitives resulting synthesis, modular monadic action semantics, is in the semantic formalism. The probability of introducing compatible with action semantics yet adds true modularity errors is large, and the maintainability of the specification and allows domain specific specifications to be made at a is poor. variety of levels. Three attempts at solving the problem of providing sound semantic descriptions of realistic programming lan- 1 Introduction guages are presented here. The first is action seman- tics [Mos92], a highly readable notation with formal foun- “I’d rather write programs to write programs than dations in Peter Mosses’s unified algebra and Plotkin’s write programs.” (Programming proverb). structural operational semantics [Plo83]. This system has proven quite popular, and has been used to specify a num- A domain-specific language (DSL) is a framework ber of existing and evolving languages. which is designed to precisely meet the needs of a particu- The second is Hudak, Liang and Jones’ modular lar application. Domain-specific languages exist for a vari- monadic semantics [LH96]. Modular monadic semantics ety of reasons. As productivity tools, they are used to make is a structured form of denotational semantics, embedded application prototyping and development faster and more in the Haskell language. As a relatively new system it is robust in the presence of evolving requirements. Further- yet to gain widespread use, but it has a number of highly more, by bridging the “semantic gap” between an applica- attractive features, foremost its excellent modularity prop- tion domain and program code, DSLs increase the oppor- erties. tunity to apply formal methods in proving properties of an These two systems can be seen as competing for the application. same market (Figure 1(c) and (d)): both are attempts to Designing and implementing a domain specific language reduce the conceptual distance that must be bridged when is, however, problematic. Putting aside the requirement formally specifying a high-level language (compare with for an efficient implementation, the language needs to be: Figure 1(a) and (b)). Both appear to do so successfully; This work was supported in part by a scholarship from the University they are, however, different DSLs and are based in differ- of Auckland. ent semantic formalisms. HLL HLL HLL HLL HLL The primary aim of action semantics is to al- low useful semantic descriptions of realistic pro- mo d. gramming languages. [Mos92, p. xv] mo d. mo d. AS MMS AS It aims at being simultaneously formal and readable, and mo d. mono. mono. achieves this goal admirably. An action semantic descrip- MMS tion is entirely formal, yet it can be intuitively understood mo d. mono. to a surprising degree by someone with no prior knowledge mo d. -calculus SOS -calculus SOS SOS of the system. However, action semantics has two significant limita- b c d e a tions. Firstly, it is incomplete. Not all programming lan- guage concepts can be represented directly within action Figure 1: Formal specification techniques. semantics—only those which Mosses has chosen to build into the system. There is no provision for the extension of action semantics. Secondly, action semantics theory has The third approach arises our observation that action not progressed very far: it is difficult to use an action se- semantics and modular monadic semantics have much to mantics of a language to prove properties of that language offer each other. Action semantics provides a friendly or of programs written within it. syntax to the language specifier, and encapsulates a sub- stantial but fixed range of language concepts. Modular Despite these disadvantages, action semantics has monadic semantics, on the other hand, provides a rather proven quite popular and rather successful. A num- less friendly syntax, but with complete flexibility and ex- ber of compiler generators based on action semantics tensibility afforded by its internal modularity. By combin- exist [Ørb94, BMW92, Pal92], along with at least one ing these two systems, we obtain a tiered system containing tool [vDM96] for assistance in developing new action se- two domain-specific semantic notations, featuring all the mantic descriptions. Action semantic descriptions exist for key features of action semantics along with the underlying a number of real languages [Mos96a, x7.1], and others are flexibility and extensibility of modular monadic semantics being developed. An action semantics for an intermedi- (Figure 1(e)). This system we call modular monadic action ate language for compilers, ANDF-FS [NT94, HT94], is semantics (MMAS). currently in use in the ‘real world’, by the Open Software The user of modular monadic action semantics specifies Foundation. Work is progressing in a number of directions. the high-level language using the rich set of primitives pro- In the following sections the essential features of ac- vided by action semantics. If the language requires a con- tion semantics are briefly summarised. More detail can be cept that is not present in action semantics, the specifier found in Mosses’ book [Mos92], or his tutorial [Mos96b]. simply drops down one level, and uses the underlying mod- ular monadic semantics to specify the new feature. If this is not sufficient, the modular monadic layer itself may be 2.1 Structure of action semantics modified or extended as required. All this takes place in a An action semantic description (ASD) of a language spec- highly modular fashion, with little or no change to existing ifies the semantics of the language by means of actions, parts of the specification. Furthermore, theories developed representing computations. The action corresponding to a at each layer can also be developed in a modular fashion. given program phrase is built up from primitive actions and New features can be introduced without undermining the action combinators. Data referred to by the actions may be validity of existing proofs. accessed by means of yielders, and new types of data may In the remainder of this paper we describe action seman- be readily defined in an algebraic fashion. tics, modular monadic semantics, and modular monadic Action semantics divides program behaviour into a num- action semantics in more detail, and we investigate some ber of facets, corresponding to the types of information of the applications of MMAS. We conclude with a look dealt with. The basic facet refers to control flow. The func- at some related work, some concluding comments, and tional facet refers to transient information, i.e., data passed directions for future work. Full details on modular between successive actions. The declarative facet refers to monadic action semantics can be found in Wansbrough’s scoped information, i.e., bindings of tokens to data. The thesis [Wan97]. imperative facet refers to stable information, i.e., the stor- age of data in cells. Finally, the communicative facet refers 2 Action semantics to permanent information, i.e., the communication of data between distributed actions. Two further facets, the reflec- Action semantics [Mos92, Mos96a] is a notation devel- tive and directive facets, refer to reflection and indirection. oped over some years by Mosses at Aarhus University. As Action semantics has these seven computational concepts Mosses puts it, built in, and they can be used directly by specifications. The primitive actions, yielders and data of action seman- 2.2 Semantics tics are separated into these categories, and are intended to Action semantics is intended to be a formal notation, and act on only one facet at a time. This automatically lends a in order to achieve this its own semantics must be precisely degree of modularity to an action semantic description: ac- and formally defined. Mosses chose to do this by providing tions involving only, say, basic and functional behaviour a low-level definition of action semantics as a structural (such as expression evaluation) need not concern them- operational semantics (SOS) [Plo81, Plo83], using a slight selves with behaviour in other facets (such as the declar- variation on Plotkin’s original approach.