Harmonizing Classes, Functions, Tuples, and Type Parameters in Virgil III

Harmonizing Classes, Functions, Tuples, and Type Parameters in Virgil III

Harmonizing Classes, Functions, Tuples, and Type Parameters in Virgil III Ben L. Titzer Google [email protected] Abstract guages like Java first offered object-oriented emulation of func- Languages are becoming increasingly multi-paradigm. Subtype tions in the form of single-method interfaces like Runnable and polymorphism in statically-typed object-oriented languages is be- Comparator. Generics gave rise to even more complex emula- ing supplemented with parametric polymorphism in the form of tions of functions such as java.util.concurrent’s Callable generics. Features like first-class functions and lambdas are ap- and Guava’s Function [13] and Predicate. Now Java has pro- pearing everywhere. Yet existing languages like Java, C#, C++, posals for syntax for function types and closures in JSR 335 [17]. D, and Scala seem to accrete ever more complexity when they Similar developments occurred in both C++ and C#, with libraries reach beyond their original paradigm into another; inevitably older first evolving functional patterns before languages added syntactic features have some rough edges that lead to nonuniformity and pit- support. C# first allowed delegates, which are closures bound to a falls. Given a fresh start, a new language designer is faced with a method of an object, and then full lambdas. Scala set out to blend daunting array of potential features. Where to start? What is impor- object-oriented and functional styles while still primarily targeting tant to get right first, and what can be added later? What features the JVM. Not to be outdone, C++11 has added support for lambdas must work together, and what features are orthogonal? We report and a host of other new features. Of course the relationship between on our experience with Virgil III, a practical language with a care- functions and objects goes back even further in dynamically-typed ful balance of classes, functions, tuples and type parameters. Virgil languages. Smalltalk had blocks [12], popular dynamic languages intentionally lacks many advanced features, yet we find its core such as JavaScript, Python and Ruby have first class functions, and feature set enables new species of design patterns that bridge mul- newcomers such as Newspeak, Groovy and Dart also incorporate tiple paradigms and emulate features not directly supported such as some kind of functional features. interfaces, abstract data types, ad hoc polymorphism, and variant A designer of a new programming language faces a daunting types. Surprisingly, we find variance for function types and tuple lineup of features from which to choose. What features work best? types often replaces the need for other kinds of type variance when How does this feature enhance or hinder another feature? What libraries are designed in a more functional style. feature should be implemented first? Should objects be primary, or functions be primary? What tools should programmers have to Categories and Subject Descriptors D.3.3 [Programming Lan- express polymorphism and reuse, model side-effects or state, and guages]: Language Constructs and Features how can the constructs be implemented efficiently? Of course, these are the tough design questions to which the Keywords Multi-paradigm languages; parametric types; object- whole of language research is directed. This paper explores just oriented programming; functional programming; monomorphiza- one potential combination of four language features and the im- tion; variance; closures; tuples; flattening; unboxing; static compi- plications of that combination. In seeking to find a smaller, more lation cohesive language core made from existing parts, and seeking to implement a practical language in which efficient systems can be 1. Introduction written, we’ve found a number of surprising patterns arising from Mainstream languages are becoming increasingly multi-paradigm just a small set of features. and increasingly complex. Statically-typed object-oriented lan- This paper finds that integrating four particular features in a guages have now ventured beyond the subtype polymorphism of statically-typed language achieves a surprising cohesiveness and class and object inheritance and begun adding parametric polymor- simplicity. Classes provide data abstraction, encapsulation, and in- phism in various forms; erased generics in Java 5.0, reified gener- formation hiding; first-class functions allow fine-grained reuse; tu- ics in C# 2.0, C++ templates, and even more complex generics in ples allow for the uniform treatment of multi-argument and multi- Scala. return function types and simplify composing values, and type pa- Functional programming constructs are now appearing in nearly rameters allow type abstraction and representation independence all active languages, further adding to language complexity. Lan- for more reuse of functions and data structures represented by ob- jects. None of these features are novel, and all of them exhibit implementations in mainstream languages, but their seamless in- tegration is key in Virgil’s design. This integration yields surpris- Permission to make digital or hard copies of all or part of this work for personal or ing power; enough to build new design patterns that emulate other classroom use is granted without fee provided that copies are not made or distributed language features not in the language, including interfaces, abstract for profit or commercial advantage and that copies bear this notice and the full citation data types, ad hoc polymorphism, and variants. An interesting aside on the first page. To copy otherwise, to republish, to post on servers or to redistribute is Virgil’s approach to type variance; a key design difficulty in re- to lists, requires prior specific permission and/or a fee. cent statically typed languages with both subtyping and parametric PLDI’13, June 16–19, 2013, Seattle, WA, USA. Copyright c 2013 ACM 978-1-4503-2014-6/13/06. $10.00 polymorphism. Surprisingly, we find type variance for class types to be far less important when libraries are designed in a more func- design of type parameters and use of first class functions, as we tional style because built-in variance for tuple and function type will see in the next section. Third, the possibility of subsumption constructors is often sufficient. to a universal type weakens confinement [34] properties and may reduce the precision of other static analyses [6][25][31]. 2. Four Pieces of the Puzzle 2.2 First-class Functions Virgil I was originally designed [31] for programming severely Virgil provides support for statically-typed first-class functions. resource-constrained devices with only a few hundred bytes of Function types take the form Tp -> Tr, where Tp is the param- RAM. A staged computation model allows applications to exe- eter type and Tr is the return type. The type void becomes the cute initialization code during compilation, building a heap of ob- parameter type of methods with no parameters and the return type jects that is compiled into a binary that executes directly on the of methods without a declared return type. Function types are unre- hardware. Virgil I supported objects and functions but had no dy- lated to object types, forming a separate universe of values. Func- namic memory allocation, no type parameters, and no tuples, mak- tion types are co-variant in their return type and contra-variant in ing it unsuitable for general-purpose programming. Virgil III is a their parameter type. A function type can legally appear anywhere clean redesign for a more general-purpose setting. It retains the any other type can appear, such as the type of a class field, method staged compilation model of Virgil I but improves classes and func- parameter, local variable, or return type of a method. Unlike most tions, adds tuples, type parameters, dynamic memory allocation, traditional functional languages, tuples are used to model multi- and garbage collection. argument functions instead of currying. Virgil includes primitive types such as int, byte, bool, and 1 To bridge the object-oriented and functional worlds, any method void . All types support four basic operators: equality ==, inequal- of any object can be used as an object method, a function which is ity !=, type cast !, and type query ?. Control structures, basic state- bound to the object as its closure. Similarly, class methods are not ments and expression syntax are similar to most C language descen- bound to an object but are functions that accept the receiver object dants, but declarations and classes look more like Scala. as their first parameter followed by the method parameters. All of the basic primitive operators can be used as first-class functions as 2.1 Classes well, and all three kinds of functions can be used interchangeably. Virgil provides classes that allow encapsulation, data abstraction Examples (b1-7) illustrate the basics, with the types of each and polymorphism through inheritance. Classes resemble those in expression given in comments. Continuing with the class A from Java and C# in that only single inheritance is allowed between (a1-6), we can (b1) create an object of that class and (b2) create a classes and all methods are virtual except those declared private. closure bound to the m method of that object. In (b3) we use the All objects are created by instantiating classes and are always method m of class A as a first-class function that is not bound to an passed by reference, never by value. Unlike most object-oriented object, but accepts the receiver object as the first parameter. In (b4) languages, Virgil has no universal superclass akin to Object from we invoke m directly on the object, in (b5) apply the function m1 to which all classes ultimately inherit. A class declared without a arguments, and in (b6) apply the function m2, supplying the object parent class begins a new hierarchy which is unrelated to other class as the first argument. Notice that we can also use the new operator hierarchies.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us