Wyvern: a Simple, Typed, and Pure Object-Oriented Language
Total Page:16
File Type:pdf, Size:1020Kb
Wyvern: A Simple, Typed, and Pure Object-Oriented Language Ligia Nistor, Darya Kurilova, Stephanie Balzer, Benjamin Chung, Alex Potanin1, and Jonathan Aldrich Carnegie Mellon University flnistor, darya, balzers, bwchung, [email protected], [email protected] Abstract Smalltalk are notable for the following characteristics, many of which are shared by more recent object-oriented languages, such The simplest and purest practical object-oriented language designs as Python, Ruby, Lua, and JavaScript: today are seen in dynamically-typed languages, such as Smalltalk and Self. Static types, however, have potential benefits for produc- • A high-level, pure object-oriented model, in which clients tivity, security, and reasoning about programs. In this paper, we de- can interact with objects only by sending them messages. This scribe the design of Wyvern, a statically typed, pure object-oriented model of interaction enforces Meyer’s uniform access principle language that attempts to retain much of the simplicity and expres- [16], a principle stating that an object’s services should only be siveness of these iconic designs. available through a uniform notation (the object’s methods in Our goals lead us to combine pure object-oriented and func- this case). tional abstractions in a simple, typed setting. We present a foun- • An interactive, value-based language model, which means that dational object-based language that we believe to be as close as everything significant in the language is a first-class value, one can get to simple typed lambda calculus while keeping object- either an object or a function. This contributes to a system that orientation. We show how this foundational language can be trans- is “directly manipulable” in the sense of Smith and Ungar [20]. lated to the typed lambda calculus via standard encodings. We then For example, classes in Smalltalk are not special declarations, define a simple extension to this language that introduces classes but live objects that respond to messages. and show that classes are no more than sugar for the foundational object-based language. Our future intention is to demonstrate that • A high degree of simplicity, with a few constructs sufficing to modules and other object-oriented features can be added to our lan- provide great expressiveness. guage as not more than such syntactical extensions while keeping • Good support for functional programming via blocks or the object-oriented core as pure as possible. lambda expressions and closures. The design of Wyvern closely follows both historical and mod- ern ideas about the essence of object-orientation, suggesting a new Interestingly, both Smalltalk and Self, as well as many lan- way to think about a minimal, practical, typed core language for guages that share some of the characteristics above, are dynam- objects. ically typed. Statically-typed object-oriented languages exist, but many are less pure (e.g., Java classes can expose fields), or less Categories and Subject Descriptors D.3.3 [Programming Lan- interactive (e.g., classes are typically not first-class), or may be guages]: Language Constructs and Features—Classes and objects; considerably more complex (C++ and Scala, despite their many D.3.1 [Programming Languages]: Formal Definitions and Theory virtues, come to mind). General Terms Languages Static types are a potentially interesting addition to the set of features above for a number of reasons. In object-oriented lan- Keywords Object-oriented, first-class classes, static type check- guages, types can eliminate message-not-understood errors [12, 24] ing and be used to optimize programs [10] and to create practical refac- toring tools [22]. Preliminary evidence suggests that, in some cir- 1. Introduction cumstances, static types can enhance productivity as well [15]. Our interest in this question comes from Wyvern, a language we The language designs of Smalltalk [11] and Self [20] are iconic are designing for secure mobile and web programming. To compare in their elegance, their simplicity, and the success with which they with programming languages in this domain, Wyvern must be a capture the essence of object-oriented programming. Although they simple, value-based, pure object-oriented language with good sup- are not among the most common languages used today, their de- port for functional programming—much like the languages men- sign has been an inspiration for many modern languages. Self and tioned above. However, we also want to provide strong reason- ing about security properties. Reasoning is facilitated by a simple, high-level language, but, in addition, we want to use type-based ap- proaches to ensure certain security properties (e.g., using inference Permission to make digital or hard copies of all or part of this work for personal or rules developed for type checking to also check object ownership classroom use is granted without fee provided that copies are not made or distributed and state). Thus, combining static types with a language having the for profit or commercial advantage and that copies bear this notice and the full citation characteristics above is important to meeting this set of goals. on the first page. To copy otherwise, to republish, to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. This paper therefore explores the design of an object-oriented MASPEGHI’13, July 01 2013, Montpellier, France language that is pure, value-based, simple, statically type-safe, and Copyright c 2013 ACM 978-1-4503-2046-7/13/07. $15.00 supports functional programming. We explore variants with and without classes, our motivation being to show that, while classes are 1.2 Mechanisms for Code Reuse convenient, they are not fundamental to the object-oriented model. No discussion of a pure object-oriented programming model would 1.1 What Makes an Object-Oriented Model Pure? be complete without discussion of code reuse mechanisms, such as inheritance. We have not yet investigated adding inheritance or Most of the criteria above are fairly clear. The exception involves delegation to Wyvern. However, Wyvern does support specializa- the purity of the object model. What does it mean, especially in the tion and generalization of types, along with a pure object-oriented context of types, for a language to be purely object-oriented? model that supports reuse via composition mechanisms. Questions To answer the question, we consider both history and theory. of interest to us as we continue the development of Wyvern are: With respect to history, Alan Kay, a pioneer of the object-oriented programming who developed Smalltalk, describes an “objects as • How far can a pure object-oriented model without inheritance server” metaphor in which each “object would be a server offer- go in supporting code reuse? ing services” that clients can use by sending messages to the ob- • What reuse mechanisms would fit most naturally into a system ject [13]. In fact, Kay writes that, in his opinion, “the big idea [of with Wyvern’s design goals? object-oriented programming] is messaging.”1 With respect to theory, in his essay on the nature of objects, We are considering a reuse model for Wyvern based on delega- William Cook defines objects as “a value exporting a [sic] proce- tion, possibly, with slightly simpler semantics compared to delega- dural interface to data or behavior” [9]. The notion of a procedural tion in Self [20]. interface reflects Kay’s focus on messaging: the only way to inter- act with an object is to invoke its methods. This is also consistent 1.3 Contributions with Meyer’s uniform access principle. Cook argues that a key benefit of objects, arising from their pro- This paper makes the following contributions: cedural interface, is that different implementations of an interface can interoperate. For example, in a pure object-oriented model, we • Section 2 presents the design of a simple, typed, value-based, can take the union of a set implemented as a list with a set imple- pure object-oriented language. We closely follow the lambda mented as a hash table. Moreover, different implementations of an calculus, a simple, foundational model of computation, while interface can be treated uniformly: for example, we can store in- enforcing the uniform access principle, supporting object inter- stances of two different implementations of a set within the same operability, and encapsulating state. data structure. These benefits are used in many object-oriented sys- • Section 2 also provides the semantics of this language by trans- tems [5], and we consider them to be critical goals of our language lation into the typed lambda calculus. design. These ideas can also be considered in a comparison between ob- • Section 3 describes a “purely object-oriented” first-class class jects and module systems, particularly, with respect to encapsulat- construct and its definition in terms of pure objects by transla- ing state. Concerning the development of object-oriented program- tion into the language described in the previous section. ming, Kay writes of his motivations, “The large scale one was to find a better module scheme for complex systems involving hiding The design of Wyvern is ongoing. While we believe the design of details, and the small scale one was to find a more flexible ver- described here captures our initial goals well, the design is subject sion of assignment, and then to try to eliminate it altogether” [13]. to change and needs to be extended in many ways. In the conclu- Hiding assignment behind a method interface—via the uniform ac- sion, we discuss areas for future work, including specific potential cess principle—provides a more flexible version of assignment, and extensions of Wyvern. one can go further in using that method interface more abstractly to “replace bindings and assignment with goals” [13]. The procedural 2. The Object-Oriented Core of Wyvern interface of objects also makes them act like first-class modules.2 From these sources, we extract three key requirements that we In this section, we present the syntax and semantics of the object- wish to satisfy in coming up with a typed, pure object-oriented oriented core of Wyvern, which we call Featherweight Wyvern model: (FW).