Languages the Racket Way 2016 Language Workbench Challenge

Languages the Racket Way 2016 Language Workbench Challenge

Languages the Racket Way 2016 Language Workbench Challenge Daniel Feltey Spencer P. Florence Tim Knutson Northwestern University Northwestern University University of Utah [email protected] spencer.fl[email protected] [email protected] Vincent St-Amour Ryan Culpepper Matthew Flatt Northwestern University Northeastern University University of Utah [email protected] [email protected] mfl[email protected] Robert Bruce Findler Matthias Felleisen Northwestern University Northeastern University [email protected] [email protected] Abstract After explaining the linguistic elements of language de- Racket espouses the view that full-fledged problem solving velopment in Racket (section 2), this paper illustrates Racket almost always calls for language design. In support of this as a language workbench starting with (section 3) an imple- view, it implements a notion of linguistic reuse, which al- mentation of MiniJava (Roberts 2001). lows programmers to rapidly develop and deploy new pro- Based on the MiniJava implementation, we present the gramming languages. Together with DrRacket, its IDE, the results of tackling three benchmark problems from the 2016 Racket ecosystem thus makes up a true language workbench. language workbench challenge. First, we demonstrate a so- This paper demonstrates Racket’s capabilities with an im- lution to the Tabular Notation problem in the Editing cate- plementation of the 2016 Language Workbench Challenge. gory by adding notation to MiniJava so that programmers Building on a concise implementation of MiniJava, it shows can express finite-state machines as Unicode-based tables how it is easy it is to add new notation, constrain constructs, (section 4). Second, we solve the Beyond-Grammar Restric- and create IDE tools. tions benchmark from the Evolution and Reuse category by showing how to constrain a break construct in MiniJava so 1. The Racket Manifesto in a Nutshell that is is valid only within the scope of while (section 5). Third, we explain how to connect an implemented language, Racket really is a programming-language programming lan- such as MiniJava, with DrRacket; specifically, we show how guage (Felleisen et al. 2015). It provides both unique linguis- to add tools for program Restructuring, consistent with the tic support (Flatt and PLT 2010) for the rapid prototyping Editing benchmark category (section 6). of languages as well as an ecosystem with unmatched ele- Our MiniJava implementation with all of the extensions ments (Findler et al. 2002). As such, it is a first step toward is available as a Racket package.1 Using the current version the idea of a language workbench (Erdweg et al. 2015) as of Racket2, run originally imagined by Bill Scherlis and Dana Scott in the early 1980s (Scherlis and Scott 1983). raco pkg install lwc2016 to install it. To view a selection of sample MiniJava pro- Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed grams and experiment with the language extensions, select for profit or commercial advantage and that copies bear this notice and the full citation the "Open Require Path. " entry under the "File" menu in on the first page. Copyrights for components of this work owned by others than ACM lwc2016/examples/programs/ must be honored. Abstracting with credit is permitted. To copy otherwise, or republish, DrRacket and type . to post on servers or to redistribute to lists, requires prior specific permission and/or a fee. Request permissions from [email protected]. ,. 1 Copyright © ACM [to be supplied]. $15.00. http://pkgs.racket-lang.org http://dx.doi.org/10.1145/ 2 https://download.racket-lang.org/ 2. The Racket Language Workbench Technically, the derivation works as follows. A language Racket promotes a language-oriented view of problem solv- module may export a subset of the constructs and func- ing. Using Racket, programmers can quickly build lan- tions of some base language, which implicitly subtracts fea- guages to solve each aspect of a programming problem on tures from that language; it may export additional features its own linguistic terms. As such, Racket programs are com- and functions, which adds new capabilities; and it may re- posed of a number of modules, each implemented in the lan- interpret existing features, say, function applications or con- guage that is best suited for the module’s task. To represent ditionals. The re-interpretation is accomplished by defining a this module language mapping, the first line of each mod- new construct or function in a module and exporting it under Ñ ule specifies the language in which it is written. The Racket the name of a feature that already exists in the base language. ecosystem relies on this mapping for linguistic dispatch:a A Racket programmer uses the syntax object system to mechanism by which a language publishes its implementa- create new linguistic constructs. This system is a descendent tion and linguistic meta-information—syntax coloring, in- of Scheme and Lisp’s hygienic macro system (Clinger and dentation, static analysis, etc.—to customize execution and Rees 1991; Hart 1963; Kohlbecker et al. 1986). The system user experience for programs written in that language. represents syntactic terms via syntax objects, which include To support this language-development idiom, Racket properties of the source syntax as well as those specified by fully embraces the idea of linguistic reuse (Krishnamurthi a language implementor (Dybvig et al. 1992). 2000). According to this view, the development of a new Like the Lisp macro system of lore, Racket’s syntax ob- language consists of adding, subtracting, and re-interpreting ject system allows the specification of rewriting rules on constructs and run-time facilities from a base language. Even syntax objects. An elaborator uses these rules to translate the installation of a new language takes place within the a module from any language into Racket core syntax on an Racket ecosystem: a language is itself a Racket component incremental basis. Unlike Lisp or Scheme macros, Racket’s that provides certain services, and each module’s language rewriting rules provide sophisticated services. For example, specification (i.e., the initial #lang line) simply refers to they automatically propagate source information so that they another module that implements the language (Flatt 2002). can report error in terms of the original source notation. Sim- A language implementation can be as simple as a plain ilarly, the rules almost automatically enforce context-free Racket module that exports specific constructs. A more so- constraints so that error messages use the concepts of the sur- phisticated language variant consists of modules that im- face language (Culpepper and Felleisen 2010). Lastly, these plement a reader—a lexer and parser for any imaginable rewriting rules can also articulate transformations on com- Unicode-based notation—and a semantics module. By using plete modules and on existing linguistic constructs—giving specific tools and following certain conventions, program- them the expressive power to track context-sensitive con- mers can produce languages that work well together. straints and to assign new meaning to old words. More broadly, a programmer can implement a Racket language in several different ways: 3. MiniJava via Racket The Racket language proper consists of a module-oriented, • as a plain interpreter that repeatedly traverses the code of untyped functional language in the spirit of Lisp and Scheme. a client program; In contrast, MiniJava (section 3.1) is a class-based, object- • as a compiler that maps a client program to a target lan- oriented programming language in the spirit of Java. Using guage, either within or outside of the Racket ecosystem; Racket’s syntax system, it is nevertheless possible to create a realistic implementation of MiniJava with a relatively small • as a Redex (Felleisen et al. 2010) reduction semantics; or effort—thanks to linguistic reuse. • as a linguistic derivative of an existing Racket language. This section provides an overview of the MiniJava im- plementation (section 3.2) followed by a presentation of the Racket strongly encourages this last approach, because it individual building blocks. Where necessary, the section also delivers results more quickly while remaining as general as explains Racket features on a technical basis. any of the others. But, all of these approaches are useful in certain situations, and on occasion, as in the case of 3.1 MiniJava MiniJava, an implementation may borrow elements from Figure 1 displays a MiniJava program. Like any Java pro- several approaches. gram, it consists of a series of class definitions, one of them Deriving one language from another means creating a designated as the “main” class. Classes have public methods translation of new linguistic constructs into those of the and fields. Each of these comes with a type signature, where base (or “parent”) language and a run-time library. Such types are the names of classes plus the usual primitive types derivations inherit other elements of the run-time system (e.g., int). The body of a method may use the familiar state- (the VM, the JIT compiler, the garbage collector, etc.). We ments of an imperative language: assignments, conditionals, consider translation the critical part of language derivation. and loops. MiniJava expressions are also the familiar ones. #lang mini-java a prefix variant of MiniJava in a conventional manner. The choice of type elaboration over checking allows the injec- class Main { public static void main(String [] args) { tion of type annotations that help implement efficient method System.out.println((new Runner()).run(10)); calls (Flatt et al. 1998). The prefix variant of MiniJava is an } untyped, parenthesized version of MiniJava. } class Runner { MiniJava Parity check; public int run(int n){ 1. Lexing + Parsing int current; check = new Parity(); Abstract Syntax Tree current = 0; while (current < n) { 2.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    12 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