Sequent Calculus As a Compiler Intermediate Language

Sequent Calculus As a Compiler Intermediate Language

Sequent Calculus as a Compiler Intermediate Language Paul Downen Luke Maurer Simon Peyton Jones Zena M. Ariola Microsoft Research Cambridge, UK University of Oregon, USA [email protected] {pdownen,maurerl,ariola}@cs.uoregon.edu Abstract questions, and surprisingly the task was not as routine as we had The λ-calculus is popular as an intermediate language for practical expected. Specifically, our contributions are these: compilers. But in the world of logic it has a lesser-known twin, • We describe a typed sequent calculus called Sequent Core with born at the same time, called the sequent calculus. Perhaps that the same expressiveness as System F!, including let, algebraic would make for a good intermediate language, too? To explore data types, and case (Section 2). this question we designed Sequent Core, a practically-oriented core calculus based on the sequent calculus, and used it to re-implement The broad outline of the language is determined by the logic, a substantial chunk of the Glasgow Haskell Compiler. but we made numerous choices driven by its role as a compiler intermediate representation (Section 2.2). Categories and Subject Descriptors D.3.4 [Programming Lan- • Our language comes equipped with an operational semantics guages]: Processors—Compilers (Section 2.3), a type system (Section 2.4), and standard meta- theoretical properties. We also give direct-style translations to Keywords Intermediate representations; Natural deduction; Se- and from System F! (Section 3).1 quent calculus; Compiler optimizations; Continuations; Haskell • The proof of the pudding is in the eating. We have implemented our intermediate language as a plugin2 for GHC, a state-of- 1. Introduction the-art optimizing compiler for Haskell (Section 4). GHC’s Steele and Sussman’s “Lambda the ultimate” papers [41, 42] per- intermediate language, called Core, is essentially System F!; suasively argued that the λ-calculus is far more than a theoretical our new plugin translates Core programs into Sequent Core, model of computation: it is an incredibly expressive and practical optimizes them, and translates them back. Moreover, we have re- intermediate language for a compiler. The Rabbit compiler [40], implemented some of GHC’s Core-to-Core optimization passes, its successors (e.g. Orbit [21]), and Appel’s book “Compiling with notably the simplifier, to instead use Sequent Core. continuations” [1] all demonstrate the power and utility of the λ- • From the implementation, we found a way that Sequent Core calculus as a compiler’s intermediate language. was qualitatively better than Core for optimization: the treatment The typed λ-calculus arises canonically as the term language of join points. Specifically, join points in Sequent Core are for a logic called natural deduction [14], using the Curry-Howard preserved during simplifications such as the ubiquitous case- isomorphism [45]: the pervasive connection between logic and of-case transformation (Sections 4.2 and 4.3). Further, we show programming languages asserting that propositions are types and how to recover the join points of Sequent Core programs, after proofs are programs. Indeed, for many people, the λ-calculus is the they are lost in translation, using a lightweight version of a living embodiment of Curry-Howard. process known as contification [20] (Section 5). But natural deduction is not the only logic! Conspicuously, natural deduction has a twin, born in the very same paper [14], called So what kind of intermediate language do we get out of the sequent the sequent calculus. Thanks to the Curry-Howard isomorphism, calculus? It turns out that the language resembles continuation- terms of the sequent calculus can also be seen as a programming passing style, a common technique in the λ-calculus for representing language [9, 15, 44] with an emphasis on control flow. control flow inside a program. The division between assumptions This raises an obvious question: does the language of the sequent and conclusions in the logic gives us a divide between programs calculus have merit as a practical compiler intermediate language, that yield results and continuations that observe those results in in the same way that the λ-calculus does? What advantages and the language. Yet despite the surface similarity, Sequent Core is disadvantages might it have, compared to the existing λ-based still quite different from continuation-passing style (Section 6). technology? Curiously, we seem to be the first to address these Perhaps most importantly, Sequent Core brings control flow and continuations to a compiler like GHC without stepping on its toes, allowing its extensive direct-style optimizations to still shine through. In the end, we get an intermediate language that lies somewhere in between direct and continuation-passing styles (Section 7), sharing some advantages of both. In a sense, many of the basic ideas we present here have been This is the author’s version of the work. It is posted here for your personal use. Not for re-discovered over the years as the tradition of Curry-Howard redistribution. The definitive version was published in the following publication: 1 ICFP’16, September 18–24, 2016, Nara, Japan More details of the meta-theory can be found in the appendix: http: ACM. 978-1-4503-4219-3/16/09... //ix.cs.uoregon.edu/~pdownen/publications/scfp_ext.pdf http://dx.doi.org/10.1145/2951913.2951931 2 Available at: http://github.com/lukemaurer/sequent-core 74 Shared syntax of kinds and types Core, namely casts and unboxed types; both are readily accommo- a; b; c 2 TyVar ::= ::: dated in Sequent Core, and our implementation does so, but they distract from our main point. κ 2 Kind ::= ? j κ ! κ Here is a small program written in both representations: # » #» τ; σ 2 Type ::= a j T j σ τ j σ ! τ j 8a:κ.τ j 9a:κ:( τ ) Core Syntax of Sequent Core plusOne : Int ! Int = λx:Int:(+) x 1 x; y; z 2 V ar ::= : : : j 2 Label ::= ::: Sequent Core # » pgm 2 Program ::= bind plusOne : Int ! Int = λx:Int.µret: h(+) jj x · 1 · reti n# »o bind 2 Bind ::= bp j rec bp Referring to Figure 1, we see that # » # » • Just as in Core, a Sequent Core program is a set of top-level bp 2 BindPair ::= x:τ = v j j:τ =µ ~[a:κ; x:σ]:c #» #» bindings; bindings can be non-recursive or (mutually) recursive. v 2 Term ::= λx:τ:v j Λa:κ.v j x j K (σ ; v ) j µret:c • A binding in Sequent Core is either a value binding x:τ = v, #» #» # » # » c 2 Command ::= let bind in c j hv jj ki j jump j σ v or a continuation binding j:τ =µ ~[a:κ; x:τ ]:c.3 We discuss the # » k 2 Kont ::= v · k j τ · k j case of alt j ret latter in Section 2.2.2. # » # » • alt 2 Alternative ::= x:τ ! c j K (a:κ; x:τ ) ! c The right-hand side of a value binding is a term v, which begins with zero or more lambdas followed by a variable, a constructor application, or a computation µret:c. Syntax of Core • The computation term µret:c, where c is a command, means x; y; z 2 V ar ::= ::: “run command c and return whatever is passed to ret as the result # » pgm 2 Program ::= bind of this term.” n# »o • All the interesting work gets done by commands. A command bind 2 Bind ::= bp j rec bp c is a collection of local bindings, wrapping either a cut pair bp 2 BindPair ::= x:τ = v hv jj ki or a jump. We discuss jumps in Section 2.2.2. • e 2 Expression ::= let bind in e Finally, cut pairs do some real computation. They are pairs #» #» hv jj ki of a term v and a continuation k. A continuation k is j λx:τ:e j Λa:κ.e j x j K σ e a call stack containing a sequence of applications (to types or # » j e e j e τ j case e of alt terms) ending with either a case analysis or a return to ret. # » # » alt 2 Alternative ::= x:τ ! e j K a:κ x:τ ! e In plusOne as written in Sequent Core above, the calculation of the function is carried out by the command in its body: the term is the Figure 1. Syntax function (+), while the continuation is x · 1 · ret, meaning “apply to x, then apply to 1, then return.” With this reading, Sequent Core cut pairs closely resemble the states of many abstract machines (e.g., the dictates [45]: first by a logician and later by computer scientists. CEK machine [12]), with a term v in the focus and a continuation Our goal is to put them together in a way that is useful for compilers. or call stack k that describes how it is consumed. Our implementation demonstrates that Sequent Core is certainly up Here is another example program, written in both representations, to the job: in short order, we achieved performance competitive with that drops the last element of a list: a highly mature optimizing compiler. While we are not ready to Core recommend that GHC change its intermediate language, we instead init : 8a:[a] ! [a] see Sequent Core as an illuminating new design point in the space of = Λa.λx:[a]: case reverse a xs of functional programming languages and laboratory for experiments [] ! [] on intermediate representation techniques. We hope that our work (y : ys) ! reverse a xs will bring the sequent calculus forth, Cinderella-like, out of the theory kitchen and into the arms of compiler writers. Sequent Core init : 8a:[a] ! [a] 2. Sequent Core = Λa.λx:[a].µret: hreversejja · xs · case of [] ! [] In this section we present the specifics of our new sequent-style (y : ys) ! hreverse jj a · ys · reti intermediate language for functional programs, along with its type system and operational semantics.

View Full Text

Details

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