Records with Rank Polymorphism

Records with Rank Polymorphism

Records with Rank Polymorphism Justin Slepak Olin Shivers Panagiotis Manolios College of Computer Science College of Computer Science College of Computer Science Northeastern University Northeastern University Northeastern University USA USA USA [email protected] [email protected] [email protected] Abstract ACM, New York, NY, USA, 13 pages. https://doi.org/10.1145/3315454. In a rank-polymorphic programming language, all functions 3329961 automatically lift to operate on arbitrarily high-dimensional aggregate data. By adding records to such a language, we can 1 Introduction support computation on data frames, a tabular data struc- In a rank-polymorphic programming language, the primary ture containing heterogeneous data but in which individual control-flow mechanism is implicitly lifting functions toop- columns are homogeneous. In such a setting, a data frame erate on array arguments of arbitrarily many dimensions. is a vector of records, subject to both ordinary array op- Instead of having the programmer write out a loop nest, erations (e.g., filtering, reducing, sorting) and lifted record the programmer effectively writes only the loop body, and operations—projecting a field lifts to projecting a column. the iteration structure is determined automatically based Data frames have become a popular tool for exploratory data on the data being consumed. With iteration over the frame analysis, but fluidity of interacting with data frames via lifted included in the semantics of function application, extracting record operations depends on how the language’s records an individual element is not the common way to consume are designed. aggregate data. Instead, the language favors uniform treat- We investigate three languages with different notions of ment of each element. This encourages the programmer to record data: Racket, Standard ML, and Python. For each, write in a style that depends less on the exact shape of the we examine several common tasks for working with data data, makes opportunities for parallelism more apparent to frames and how the language’s records make these tasks easy a compiler, and facilitates exploratory coding due to a lack or hard. Based on their advantages and disadvantages, we of loop-nest boilerplate. synthesize their ideas to produce a design for record types Record types are also aggregate data, but it is expected that which is flexible for both scalar and lifted computation. different fields in a single record may have different types. CCS Concepts • Software and its engineering → Poly- Heterogeneity calls for the elements of a record to be treated morphism; Control structures; Data types and struc- individually rather than consuming such data through an tures; • Mathematics of computing → Exploratory data analog of rank-polymorphic function lifting. Even when analysis. record fields have the same type, it is not generally appropri- ate to treat them uniformly—e.g., temperature and latitude Keywords array-oriented languages, rank polymorphism, may both be stored as a number of degrees, but only one data frames, records should be convertible between Fahrenheit and Celsius. Libraries and even languages focused on “data frames,” ACM Reference Format: Justin Slepak, Olin Shivers, and Panagiotis Manolios. 2019. Records tabular data structures which are homogeneous within any with Rank Polymorphism. In Proceedings of the 6th ACM SIGPLAN column but potentially heterogeneous along a row, have International Workshop on Libraries, Languages and Compilers for arisen as popular tools for exploratory data analysis. We Array Programming (ARRAY ’19), June 22, 2019, Phoenix, AZ, USA. claim that data frames are the natural result of combining rank polymorphism with records. Permission to make digital or hard copies of all or part of this work for Even with concise theoretical intuition about computing personal or classroom use is granted without fee provided that copies with data frames, ease of use often depends on things a the- are not made or distributed for profit or commercial advantage and that copies bear this notice and the full citation on the first page. Copyrights oretician might dismiss as uninteresting details. Our goal is for components of this work owned by others than the author(s) must to examine several designs for heterogeneous record data be honored. Abstracting with credit is permitted. To copy otherwise, or and how those designs fare when lifted up to data frames. republish, to post on servers or to redistribute to lists, requires prior specific In this work, we explore the interaction of records and rank permission and/or a fee. Request permissions from [email protected]. polymorphism in settings where they are independent fea- ARRAY ’19, June 22, 2019, Phoenix, AZ, USA tures We investigate what we get for free simply for doing © 2019 Copyright held by the owner/author(s). Publication rights licensed to ACM. record computation using rank polymorphism as it already ACM ISBN 978-1-4503-6717-2/19/06...$15.00 exists without extra design work targeted at integrating the https://doi.org/10.1145/3315454.3329961 two features. ARRAY ’19, June 22, 2019, Phoenix, AZ, USA Justin Slepak, Olin Shivers, and Panagiotis Manolios We demonstrate several common tasks using three pro- Suppose instead we gave [5 6 7] as an argument to a gramming systems’ notions of records: dot-prod function which expects two rank-1 arguments. 1. Racket’s structs within #lang remora/dynamic, a We would then have a scalar frame containing a single cell. rank-polymorphic DSL If the other argument’s shape is 2 × 3, we must grow our 2. Standard ML’s records, with aggregate lifting provided scalar frame into a 2-vector frame by replicating the entire by explicit use of map-like functions vector—the cell rank here is 1 instead of 0 in the previous 3. Python’s dictionaries, with a purpose-built library for case. So the replicated form is [[5 6 7] [5 6 7]]. working with data frames The rank which a function expects for that argument After consideration of how each version of records helps effectively determines whether the matrix produced by frame or hinders exploratory analysis, we sketch a design for het- expansion has the original vector as a column or as a row erogeneous record data which combines their respective because the decomposition of the argument shape into its advantages for data frame manipulation but also serves as a frame and cell portions determines where new dimensions sane design independent of our focus on data frames as an are added. Viewing an n-vector as a vector frame containing × ::: application domain. scalar cells means it can be grown to an n m array, whereas viewing it as a scalar frame containing a vector cell 2 Rank Polymorphism means it can be grown to an m ::: × n array. A common way of manipulating the iteration space is In the rank-polymorphic programming model, the universe choosing where in an argument shape new dimensions can of data consists of regular arrays, whose “shape” is a se- be added. Consider a simple vector-matrix addition: quence of natural-number dimensions. A function’s defini- tion includes the “ranks” of its arguments, i.e., the number of (+ [10 20] dimensions each argument is expected to have. A polynomial [[1 2] evaluation function poly-eval would take a rank-1 argu- [3 4]]) ment containing the polynomial’s coefficients and a rank-0 The function + expects scalar arguments. When we split the (i.e., scalar) argument for the number at which to evaluate actual arguments’ shapes—[2] and [2 2] respectively—into the polynomial. For a matrix inversion function minv, there frame and cell portions, they are frame = [2] and cell = [] would be a single argument of rank 2. for the first argument and frame = [2 2] and cell = [] for When a function is applied to arguments of higher rank, the second. We must extend the first argument’s [2] frame those arguments are viewed like nested arrays. Applying to [2 2] by replicating scalar cells. So the 10 cell expands to minv to an array with shape 3 × 4 × 4 treats that argument as [10 10] and 20 to [20 20]. The first argument is therefore a 3-vector whose elements are 4 × 4 matrices. That 3-vector treated like [[10 10] [20 20]]. If we instead used a vec+ forms the implicit iteration space: we must invert each of function which expects vector arguments, the frame shapes the three matrices, producing a 3-vector of result matrices would be [] and [2]. Expanding [] to [2] would replicate which we then reassemble into a 3 × 4 × 4 result. The individ- the vector cell [10 20] rather than each scalar cell. So we ual matrices in this scenario are called the “cells;” they are treat the first argument as [[10 20] [10 20]]. the fundamental unit on which the function operates. The The behavior of vec+ could be described as breaking its “frame” is the sequence of leading dimensions in an argu- arguments into vector cells and then applying + to those vec- ment’s shape which drives the implicit iteration—appending tors (which internally breaks the vectors into scalar cells to the frame and cell shape gives the entire argument shape. add pointwise). So vec+ is a “reranked” version of +. Rerank- Here, our 4 × 4-matrix cells are laid out in a 3-vector frame. ing is a common enough technique in rank-polymorphic We could also use this 3 × 4 × 4 array as the coefficient ar- programming to warrant some language-level support, such gument to poly-eval, in which case it would be viewed as as J’s " operator. 4-vector cells in a 3 × 4-matrix frame. Frames are compatible if and only if one is a prefix of the 2.1 A Rank-Polymorphic Language other.

View Full Text

Details

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