r/ProgrammingLanguages • u/KeyDue7848 • Apr 03 '26
Compiling with sequent calculus
Long time lurker here. I've seen a number of posts about using IRs based on sequent calculus and decided to have a go at it myself. My prototype compiler can be found here, for those of you interested in this niche: https://github.com/August-Alm/sequent
The paper that influenced me the most was https://se.cs.uni-tuebingen.de/publications/schuster25compiling.pdf, that has been highlighted on this reddit before. It defines a low-level IR that corresponds to focused/normalized terms of a depolarised sequent calculus calculus and explains how to transpile it to traditional assembly. I copied this pretty much wholesale.
One abstraction level above it, I have a polarised sequent calculus with generalised algebraic data/codata types, higher kinded types, quantitative type theory-style usage tracking, polymorphism and automatic data kinds in the vein of Haskell's DataKinds extension, and primitive "destination" types for type-safe memory writes in destination passing style (in the style of https://arxiv.org/pdf/2503.07489).
Above that sits functional programming language users are meant to write. It supports the same type-level features, but it is not polarised. It has (generalised algebraic) data and codata types, but they have the same kind "type" ("*") -- polymorphism is higher rank and over all types, not over data or codata separately.
The compilation pipeline was pleasantly easy once. I only really faced two big conundrums:
- The surface functional language is not polarised, but the core sequent calculus is. So, I shift all constructions in the surface language into the positive & producer fragment of the core. Since, e.g., function types in sequent calculus are canonically polarised as ((A:+) -> (B:-)):-, this involves quite a bit of shifting to get right. Similarly, the low-level focused/normalised IR is again unpolarised but still has a chirality division of terms into producers and consumers. The compilation of the core sequent calculus to the focused form is done in such a way that "focused chirality = core chirality + core polarity mod 2". That is, the chirality of terms of negative types flip. Again, the transformations are not really very difficult, the difficulty was realising how it needed to be done. I'm sure it has been written about in the research literature but, not being an academic, I had to reinvent the wheel for myself.
- The low-level, focused IR does not support polymorphism. In fact, the focusing/normalisation of the core sequent calculus into the focused IR does not support polymorphism, or at least I couldn't figure out how to do it. The issue is that this focusing/normalisation relies crucially on eta-expansion of cuts, in a way that depends on knowing the type of the cut. A cut at a polymorphic type variable cannot be eta-expanded. To get around this, I monomorphise the core sequent terms before normalisation, based on https://dl.acm.org/doi/epdf/10.1145/3720472 That paper does not do it in the setting of a sequent calculus, but it translated very nicely into my setting and allowed me to fully monomorphise higher-rank polymorphism with very little effort.
The compiler "frontend" is very underdeveloped. No source code positions in error messages or etc, the syntax is a bit crude and types annotations could be much more inferred. But for what it is -- a prototype of compilation with sequent calculus-based IRs -- I feel it has achieved its goal. It supports high-level, feature-rich functional programming language and emits surprisingly fast Arm64 assembly, with no external dependencies apart from lexx/yacc for parsing.
View all comments
Show parent comment
2
u/shtsoft-dot-eu Apr 05 '26
You did a really great job here!
Regarding the the polarity shifts, I agree that they are not particularly intuitive. As far as I know, they are due to Girard, who introduced them in Locus Solum. I think his memory aid was that
↓looks somewhat like the exponential!, and both change a negative behavior into a positive one.Regarding polymorphism, I personally am in favor of monomorphization. Nevertheless, here is a rough idea, how one might be able keep polymorphic types during the normalization. The situation is somewhat similar to that of base types, where you also cannot simply eta-expand. For example, for critical pairs
<mu a. s1 | mutilde x. s2 >at typeInt, we need a different way to model the consumer. One solution is to use a continuation data typeand model the
mutilde x. s2as a pattern match,(From skimming through your code (sorry, I didn't have the time to read it all), I gather that you did something similar). But now the type of the covariable
ahas changed: it is not a consumer ofIntanymore, but a consumer ofCont. Therefore, whenever a covariable ofIntwas the right-hand side of a cut, it has now become a covariable ofCont, so we have to adapt the left-hand side accordingly. For example,For data types, this does not work so well, because covariables cannot only be bound to
mutilde-bindings, but also to pattern matches (and dually for codata types). But for polymorphic types, there are no (co)pattern matches, so we might try to use a polymorphic continuation type for critical pairs. However, in contrast toIntabove (which I have assumed to be call-by-value), we do not know the evaluation order from the polymorphic type, and hence we do not know which side of the cut to turn into a (co)pattern match. One possibility I see to resolve this problem, is to also polarize type variables. That is, there are positive type variables, which can only be subtituted by positive types, and negative type variables, which can only be subtituted by negative types. Then, if you have a critical pair at a positive type variable, you can use the data typeand model the
mutilde-binding on the right-hand side as a pattern match,And dually, for negative type variables, you can use the codata type
and model the
mu-binding on the left-hand side as a copattern match,This polarization makes type variables of course a bit more restrictive, but maybe it is possible to work around this restriction with polarity shifts. Also, I have not fully thought through this yet, so I'm not completely sure whether it works.