
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 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 • A high-level, pure object-oriented model, in which clients and Self. Static types, however, have potential benefits for produc- can interact with objects only by sending them messages. This tivity, security, and reasoning about programs. In this paper, we de- model of interaction enforces Meyer’s uniform access principle scribe the design of Wyvern, a statically typed, pure object-oriented [16], a principle stating that an object’s services should only be language that attempts to retain much of the simplicity and expres- available through a uniform notation (the object’s methods in siveness of these iconic designs. this case). Our goals lead us to combine pure object-oriented and func- 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 • A high degree of simplicity, with a few constructs sufficing to object-based language. Our future intention is to demonstrate that provide great expressiveness. modules and other object-oriented features can be added to our lan- 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). Static types are a potentially interesting addition to the set of General Terms Languages features above for a number of reasons. In object-oriented lan- guages, types can eliminate message-not-understood errors [12, 24] Keywords Object-oriented, first-class classes, static type check- and be used to optimize programs [10] and to create practical refac- ing toring tools [22]. Preliminary evidence suggests that, in some cir- cumstances, static types can enhance productivity as well [15]. 1. Introduction 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- Smalltalk are notable for the following characteristics, many of 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 rules developed for type checking to also check object ownership and state). Thus, combining static types with a language having the Permission to make digital or hard copies of all or part of this work for personal or characteristics above is important to meeting this set of goals. classroom use is granted without fee provided that copies are not made or distributed This paper therefore explores the design of an object-oriented for profit or commercial advantage and that copies bear this notice and the full citation on the first page. To copy otherwise, to republish, to post on servers or to redistribute language that is pure, value-based, simple, statically type-safe, and to lists, requires prior specific permission and/or a fee. supports functional programming. We explore variants with and MASPEGHI’13, July 01 2013, Montpellier, France without classes, our motivation being to show that, while classes are Copyright c 2013 ACM 978-1-4503-2046-7/13/07. $15.00 convenient, they are not fundamental to the object-oriented model. 1.1 What Makes an Object-Oriented Model Pure? 1 type Lot = Most of the criteria above are fairly clear. The exception involves 2 meth value : int the purity of the object model. What does it mean, especially in the 3 meth compare(other : Lot) : int context of types, for a language to be purely object-oriented? 4 meth sell : int To answer the question, we consider both history and theory. 5 With respect to history, Alan Kay, a pioneer of the object-oriented 6 meth purchase(q : int, p : int) : Lot = 7 new programming who developed Smalltalk, describes an “objects as 8 var qty : int = q server” metaphor in which each “object would be a server offer- 9 var price : int = p ing services” that clients can use by sending messages to the ob- 10 meth value : int = ject [13]. In fact, Kay writes that, in his opinion, “the big idea [of 11 this.qty * this.price 1 object-oriented programming] is messaging.” 12 meth compare(other : Lot) : int = With respect to theory, in his essay on the nature of objects, 13 this.value - other.value William Cook defines objects as “a value exporting a [sic] proce- 14 meth sell : int = dural interface to data or behavior” [9]. The notion of a procedural 15 val qtySold = this.qty 16 this.qty = 0 interface reflects Kay’s focus on messaging: the only way to inter- 17 qtySold act with an object is to invoke its methods. This is also consistent 18 with Meyer’s uniform access principle. 19 val aLot = purchase(100,100) Cook argues that a key benefit of objects, arising from their pro- 20 val d = aLot.compare(purchase(50,90)) cedural interface, is that different implementations of an interface 21 val value = aLot.value can interoperate. For example, in a pure object-oriented model, we 22 val qtySold = aLot.sell can take the union of a set implemented as a list with a set imple- mented as a hash table. Moreover, different implementations of an Figure 1. A Lot Object in the Object-Oriented Core of Wyvern interface can be treated uniformly: for example, we can store in- (Featherweight Wyvern) stances of two different implementations of a set within the same data structure. These benefits are used in many object-oriented sys- tems [5], and we consider them to be critical goals of our language as inheritance. We have not yet investigated adding inheritance or design. delegation to Wyvern. However, Wyvern does support specializa- These ideas can also be considered in a comparison between ob- tion and generalization of types, along with a pure object-oriented jects and module systems, particularly, with respect to encapsulat- model that supports reuse via composition mechanisms. Questions ing state. Concerning the development of object-oriented program- of interest to us as we continue the development of Wyvern are: ming, Kay writes of his motivations, “The large scale one was to • How far can a pure object-oriented model without inheritance find a better module scheme for complex systems involving hiding go in supporting code reuse? of details, and the small scale one was to find a more flexible ver- sion of assignment, and then to try to eliminate it altogether” [13]. • What reuse mechanisms would fit most naturally into a system Hiding assignment behind a method interface—via the uniform ac- with Wyvern’s design goals? cess principle—provides a more flexible version of assignment, and one can go further in using that method interface more abstractly to We are considering a reuse model for Wyvern based on delega- “replace bindings and assignment with goals” [13]. The procedural tion, possibly, with slightly simpler semantics compared to delega- interface of objects also makes them act like first-class modules.2 tion in Self [20]. From these sources, we extract three key requirements that we wish to satisfy in coming up with a typed, pure object-oriented 1.3 Contributions model: This paper makes the following contributions: • Uniform access principle. Following Meyer, Cook, and Kay, • Section 2 presents the design of a simple, typed, value-based, it should be possible to access objects only by invoking their pure object-oriented language.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages8 Page
-
File Size-