Why Compiler Writers Care About Case-of-Case

This article examines the deep duality between let-bindings and control operators like call/cc in functional programming. It showcases how constructs like label and goto provide clearer semantics for continuation capture than traditional methods, simplifying translations into Core calculus. Additionally, it explains how the ฮปฮผฮผหœ-calculus handles case-of-case transformations natively, making certain compiler optimizations implicit rather than explicit.


This content originally appeared on HackerNoon and was authored by Abstraction: Computer Science Discourse

  1. Introduction

  2. Translating To Sequent Calculus

    2.1 Arithmetic Expressions

    2.2 Let Bindings

    2.3 Top-level Definitions

    2.4 Algebraic Data and Codata Types

    2.5 First-Class Functions

    2.6 Control Operators

  3. Evaluation Within a Context

    3.1 Evaluation Contexts for Fun

    3.2 Focusing on Evaluation in Core

  4. Typing Rules

    4.1 Typing Rules for Fun

    4.2 Typing Rules for Core

    4.3 Type Soundness

  5. Insights

    5.1 Evaluation Contexts are First Class

    5.2 Data is Dual to Codata

    5.3 Let-Bindings are Dual to Control Operators

    5.4 The Case-of-Case Transformation

    5.5 Direct and Indirect Consumers

    5.6 Call-By-Value, Call-By-Name and Eta-Laws

    5.7 Linear Logic and the Duality of Exceptions

  6. Related Work

  7. Conclusion, Data Availability Statement, and Acknowledgments

A. The Relationship to the Sequent Calculus

B. Typing Rules for Fun

C. Operational Semantics of label/goto

References

5.3 Let-Bindings are Dual to Control Operators

The label construct in Fun is translated to a ๐œ‡-binding in Core. Also, when considering the typing rule for label ๐›ผ {๐‘ก } in Section 4.1, we can see that it directly corresponds to typing a ๐œ‡-binding with the label ๐›ผ being the bound covariable. Similarly, a let-binding is translated to a ๐œ‡หœ-binding and typing a let-binding in Fun closely corresponds to typing a ๐œ‡หœ-term in Core. This way, labels and let-bindings are dual to each other, the same way ๐œ‡ and ๐œ‡หœ are. The duality can be extended to other control operators such as call/cc.

\ As it turns out, the label construct is very closely related to call/cc. There are in fact only two differences. First, label ๐›ผ {๐‘ก } has the binder ๐›ผ for the continuation built into the construct, just as the variation of call/cc named let/cc (which Reynolds [1972] called escape). The second, and more important difference is that the invocation of the continuation captured by label ๐›ผ {๐‘ก } happens through an explicit language construct goto(๐‘ก; ๐›ผ). This makes it easy to give a translation to Core as we can simply insert another ๐œ‡-binding to discard the remaining continuation at exactly the place where the captured continuation is invoked. In contrast, with call/cc and let/cc the continuation is applied in the same way as a normal function, making it necessary to redefine the variable the captured continuation is bound to when translating to Core. This obscures the duality to let-bindings which is so evident for label and goto.

\ To see this, here is a translation of let/cc ๐‘˜ ๐‘ก to Core

\ [let/cc ๐‘˜ ๐‘ก] โ‰” ๐œ‡๐›ผ.โŸจcocase {ap(๐‘ฅ, ๐›ฝ) โ‡’ โŸจ๐‘ฅ | ๐›ผโŸฉ} | ๐œ‡๐‘˜. หœ โŸจ[๐‘ก] | ๐›ผโŸฉโŸฉ

\ The essence of the translation still is that the current continuation is captured by the outer ๐œ‡ and bound to ๐›ผ. But now we also have to transform this ๐›ผ into a function (the cocase here) which discards its context (here bound to ๐›ฝ) and bind this function to ๐‘˜, which is done using ๐œ‡หœ. For call/cc, the duality is even more obscured, as there the binder for the continuation is hidden in the function which call/cc is applied to. For the translation, this function must then be applied to the above cocase and the captured continuation ๐›ผ, resulting in the following term (cf. also [Miquey 2019]).

\ [call/cc ๐‘“] โ‰” ๐œ‡๐›ผ.โŸจ[๐‘“ ] | ap(cocase {ap(๐‘ฅ, ๐›ฝ) โ‡’ โŸจ๐‘ฅ | ๐›ผโŸฉ}, ๐›ผ)โŸฉ

\ Other control operators for undelimited continuations can be translated in a similar way. For example, consider Felleisenโ€™s C [Felleisen et al. 1987]. The difference to call/cc is that C discards the current continuation if it is not invoked somewhere in the term C is applied to, whereas call/cc leaves it in place and thus behaves as a no-op if the captured continuation is never invoked. The only change that needs to be made in the translation to Core is that the top-level continuation โ‹† has to be used for the outer cut instead of using the captured continuation. This is most easily seen for a variation of C which has the binder for the continuation built into the operator and where the invocation of the continuation is explicit, similar to label/goto. Calling this variation labelC, we obtain the following translation

\ [labelC ๐›ผ {๐‘ก }] โ‰” ๐œ‡๐›ผ.โŸจ[๐‘ก] | โ‹†โŸฉ

\ Here the duality to let-bindings is evident again. The translation for C itself is then obtained in the same way as for call/cc

\ [C ๐‘“ ] โ‰” ๐œ‡๐›ผ.โŸจ[๐‘“ ] | ap(cocase {ap(๐‘ฅ, ๐›ฝ) โ‡’ โŸจ๐‘ฅ | ๐›ผโŸฉ}, โ‹†)โŸฉ

5.4 The Case-of-Case Transformation

One important transformation in functional compilers is the case-of-case transformation. Maurer et al. [2017] give the following example of this transformation. The term

\n

if (if ๐‘’1 then ๐‘’2 else ๐‘’3) then ๐‘’4 else ๐‘’5

\n

can be replaced by the term

\n

if ๐‘’1 then (if ๐‘’2 then ๐‘’4 else ๐‘’5) else (if ๐‘’3 then ๐‘’4 else ๐‘’5).

\n

Logicians call these kinds of transformations commutative conversions, and they play an important role in the study of the sequent calculus. But as Maurer et al. [2017] show, they are also important for compiler writers who want to generate efficient code.

\n

In the ๐œ†๐œ‡๐œ‡หœ-calculus, commuting conversions donโ€™t have to be implemented as a special compiler pass. They fall out for free as a special instance of ๐œ‡-reductions! Let us illustrate this point by translating Maurer et al.โ€™s example into the ๐œ†๐œ‡๐œ‡หœ-calculus. First, let us translate the two examples using pattern-matching syntax:

\n

\ image

\ Let us now translate these two terms into the ๐œ†๐œ‡๐œ‡หœ-calculus:

\n

\ image

\ \n

We can see that just by reducing all of the underlined redexes we reduce both of these examples to the same term.

:::info Authors:

(1) David Binder, University of Tรผbingen, Germany;

(2) Marco Tzschentke, University of Tรผbingen, Germany;

(3) Marius Muller, University of Tรผbingen, Germany;

(4) Klaus Ostermann, University of Tรผbingen, Germany.

:::


:::info This paper is available on arxiv under CC BY 4.0 license.

:::

\


This content originally appeared on HackerNoon and was authored by Abstraction: Computer Science Discourse


Print Share Comment Cite Upload Translate Updates
APA

Abstraction: Computer Science Discourse | Sciencx (2025-07-09T06:12:42+00:00) Why Compiler Writers Care About Case-of-Case. Retrieved from https://www.scien.cx/2025/07/09/why-compiler-writers-care-about-case-of-case/

MLA
" » Why Compiler Writers Care About Case-of-Case." Abstraction: Computer Science Discourse | Sciencx - Wednesday July 9, 2025, https://www.scien.cx/2025/07/09/why-compiler-writers-care-about-case-of-case/
HARVARD
Abstraction: Computer Science Discourse | Sciencx Wednesday July 9, 2025 » Why Compiler Writers Care About Case-of-Case., viewed ,<https://www.scien.cx/2025/07/09/why-compiler-writers-care-about-case-of-case/>
VANCOUVER
Abstraction: Computer Science Discourse | Sciencx - » Why Compiler Writers Care About Case-of-Case. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/07/09/why-compiler-writers-care-about-case-of-case/
CHICAGO
" » Why Compiler Writers Care About Case-of-Case." Abstraction: Computer Science Discourse | Sciencx - Accessed . https://www.scien.cx/2025/07/09/why-compiler-writers-care-about-case-of-case/
IEEE
" » Why Compiler Writers Care About Case-of-Case." Abstraction: Computer Science Discourse | Sciencx [Online]. Available: https://www.scien.cx/2025/07/09/why-compiler-writers-care-about-case-of-case/. [Accessed: ]
rf:citation
» Why Compiler Writers Care About Case-of-Case | Abstraction: Computer Science Discourse | Sciencx | https://www.scien.cx/2025/07/09/why-compiler-writers-care-about-case-of-case/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.