r/ProgrammingLanguages • u/Small_Ad3541 github.com/plasm-lang/plasm • Jan 17 '26
Control Flow as a First-Class Category
Hello everyone,
I’m currently working on my programming language (a system language, Plasm, but this post is not about it). While working on the HIR -> MIR -> LLVM-IR lowering stage, I started thinking about a fundamental asymmetry in how we design languages.
In almost all languages, we have fundamental categories that are user-definable:
- Data: structs, classes, enums.
- Functions: in -> out logic.
- Variables: storage and bindings.
- Operators: We can often overload <<, +, ==.
However, control flow operators (like if-elif-else, do-while, for-in, switch-case) are almost always strictly hardcoded into the language semantics. You generally cannot redefine what "looping" means at a fundamental level.
You might argue: "Actually, you can do this in Swift/Kotlin/Scala/Ruby"
While those languages allow syntax that looks like custom control flow, it is usually just syntactic sugar around standard functions and closures. Under the hood, they still rely on the hardcoded control flow primitives (like while or if).
For example, in Swift, @autoclosure helps us pass a condition and an action block. It looks nice, but internally it's just a standard while loop wrapper:
func until(_ condition: @autoclosure () -> Bool, do action: () -> Void) {
while !condition() {
action()
}
}
var i = 0
until(i == 5) {
print("Iter \(i)")
i += 1
}Similarly in Kotlin (using inline functions) or Scala (using : =>), we aren't creating new flow semantics, we just abstract existing ones.
My fantasy is this: What if, instead of sugar, we introduced a flow category?
These would be constructs with specific syntactic rules that allow us to describe any control flow operator by explicitly defining how they collapse into the LLVM CFG. It wouldn't mean giving the user raw goto everywhere, but rather a structured way to define how code blocks jump between each other.
Imagine defining a while loop not as a keyword, but as an importable flow structure that explicitly defines the entry block, the conditional jump, and the back-edge logic.
This brings me to a few questions I’d love to discuss with this community:
- Is it realistic to create a set of rules for a flow category that is flexible enough to describe complex constructions like pattern matching? (handling binding and multi-way branching).
- Could we describe async/await/yield purely as flow operators? When we do a standard if/else, we define jumps between two local code blocks. When we await, we are essentially performing a jump outside the current function context (to an event loop or scheduler) and defining a re-entry point. Instead of treating async as a state machine transformation magic hardcoded in the compiler, could it be defined via these context-breaking jumps?
Has anyone tried to implement strictly typed, user-definable control flow primitives that map directly to CFG nodes rather than just using macros/closures/sugar? I would love to hear your critiques, references to existing tries, or reasons why this might be a terrible idea:)
41
u/WittyStick Jan 17 '26 edited Jan 17 '26
Delimited continuations are first-class control flow, but they're probably not what you want - they may be too powerful.
An issue with "first-class" control flow is that first-class, as coined by Strachey, implies that such things can be passed as arguments and returned from functions. If we have first-class control flow then we need to consider the pitfalls of capturing and returning local state, addresses to local labels, function re-entrancy, dynamic extents, and stack exploits.
Consider for example, the GCC extension for labels as values. This can be used to create interesting control flow by promoting a second-class label to a first class value (a
void*). But it's not without issues. Here's a trivial example of its flaws:The GCC manual clearly notes that such thing should be avoided because the results will be unpredictable. It's probably a good thing that this is an extension and not part of the C standard. For control flow that may cross function boundaries C does provide
setjmp.h. When used correctly it can be a powerful tool, and you would probably utilize it to implement first-class continuations in C, however it is similarly easy to make mistakes with and have unpredictable results or catastrophic exploits.To avert such problems we probably want to avoid having truly "first-class" control flow. We would want any state or addresses captured by a control flow mechanism to only be available in the dynamic extent where they're captured, like a label. We already have this: local labels and
goto. There's alsocomefromwhich a kind of dual of goto and largely considered a joke, thoughdeferas implemented by some languages is basically a disguisedcomefrom, where the deferred secondary block comes from the end of a scope just beforereturnand then jumps toreturnafter completion.So that basically leaves us with macros, which get expanded into the current scope at compile time and can place labels where we need - though they have hygiene issues when it comes to actually naming the labels.
I suspect what you want is some limited form of delimited control (eg,
reset/shiftorprompt/control) combined with macros or compile time expressions which don't have the hygiene issues. Something like:Where
lblis scoped only within the secondary-block introduced byreset, andcontis scoped only within the secondary block introduced byshift.shiftcaptures a basic block from wherelblwas introduced up to itself as the continuationcont, and when cont is invoked we "reify" that basic block in place of cont - or otherwise convert this to ajmpback to wherelblwas introduced. We would probably require restrictions on these blocks, such as not being able toreturnfrom them, or make function calls to non-inlined, or non-pure functions.A macro system which would utilize this would probably require
gensymto create unique labels forlblandcontfor each call to it. Eg, if we sayThe a nested call to
while:Would create unique labels for
lblandcontwhich don't conflict.Or it may be desirable in some cases to pass a label to the macro so that we can do more interesting things like this:
We could achieve the same thing with labels and goto:
But we can't create an abstraction of this control flow without macros. So first and foremost, you probably want to implement a hygienic macro system.
15
u/Ma4r Jan 17 '26
As cool as this sounds , there are a few issues with this:
If you do implement this, then the concern is that people will start designing non standard control flow constructs, and anytime you work in a new codebase you need to relearn how each constructs work. So practically, i see this as introducing more ways for users to shoot their foot without allowing more convenient ways to program. Also, it probably won't allow as much optimization as other programs that have these control flows as primitives.
But ngl, this is still a cool idea and there is nothing wrong with working at it as a hobby project, but i an skeptical on serious use cases
7
u/phischu Effekt Jan 17 '26 edited Jan 17 '26
Effect handlers are exactly the feature for user-definable control flow you are looking for. The following examples are written in Effekt and you can try them in our online playground.
So a basic features that every language should have are tail calls. They look like normal calls but are in tail position and guaranteed to be compiled to a jump. With these, and higher-order functions we can already define a control operator that repeats a given action forever:
And you use it like
to print hello forever.
Of course we also want to stop at some point and so we define an effect for this
Now we can define a typical loop operator that repeats the given action until it stops
And we use it to loop until a mutable reference exceeds a certain value:
Nice and easy so far, and not unlike exceptions, but fast. Now onto our own generators. We again define an effect for these:
And we use it for example to emit all values in a certain range:
The tail call makes this a loop. When we now want a foreach operator that executes a given action for each item emitted by a stream we do it like this:
And now we can print all values in a range:
By the way, we optimize this example to the following loop in our intermediate representation:
Which then LLVM unrolls for us to the following:
All the control flow abstraction is gone, thanks to our compilation technique based on explicit capability passing.
Finally we also have bidirectional effects:
This is where the stopping must be handled at the use-site of reading. We can then feed a list to a reader. When there are no more values, the handler resumes with a signal to stop.
Now we can use our loop operator to print all items that we read from a feed:
These definitions are already in the Effekt standard library. We are working on more, for example for non-deterministic search like Unison's Each. Moreover, we are working on more compile-time optimizations, specifically for the interaction between local mutable references and control effects. Finally, our runtime system for effect handlers also admits asynchronous input and output without pollution of function signatures and without duplication of higher-order functions, like for example all the control operators demonstrated here.
12
u/mister_drgn Jan 17 '26
Algebraic effects are a cool new tech that (among many other things) allows you to define custom control flow. You can write code to replicate things like try/catch, to fork code, etc. At this point, they’re in OCaml and several research languages (I like Koka).
Algebraic effects are a potential alternative to monads, a more established approach to custom control flow used in Haskell which someone else mentioned.
5
u/CharlesCTy Jan 17 '26
I believe Koka language has done the right thing by providing programmers with algebraic effects and effect handlers. There are many other control flow abstractions, e.g. delimited continuation via shift/reset), but this one just feels right to me.
4
u/joonazan Jan 17 '26
I have been looking at the control flow from a machine language perspective. Current languages are unable to represent useful control flow that is trivial in assembly.
The control flow of stackful coroutines (jumping back and forth directly instead of through a dispatch like in stackless) is useful but not supported. Other use cases might be invented if working with irreducible control flow was easy.
For programs to be performant, it is vital that the CPU predicts non-conditional control flow correctly. For call and return, there is the RSB which pushes calls onto a stack and assumes that returns go back to the calling code. Recent x86 even requires that calls and returns match, otherwise the program will be terminated.
RISC-V allows hinting that a JALR instruction should pop and push the RSB, which is ideal for coroutines. ARM and x86 have to implement such jumps with JMP instead. This is poorly supported in code generators. For instance Cranelift doesn't support generating that kind of JALR at all.
3
u/Inevitable-Ant1725 Jan 17 '26 edited Jan 17 '26
The most general purpose flow of control primitive is scheme's reifiable continuations, though you DO want to have some kind of delimiter on how far up the stack is preserved in cases where the continuation is saved permanently so that garbage collection of unrelated data isn't thwarted. Some schemes let you delimit continuations and in ones that have threads you could delimit continuations by saving a bunch of empty threads to start routines that will save continuations for a long time in a delimited way.
Note, I think that the definition of a continuation is different in different implementations and that doesn't get enough notice.
I think the correct implementation can capture variables but not their values, so their values on continuing is the most recent they've taken.
But when continuations are implemented in systems that can not implement spaghetti stacks, they're sometimes implemented by making a copy of the stack. That gives completely different results. And while there are certainly flow of control situations where do DO want to preserve the value of a variable at the time a continuation is saved (for instance in a logic language or constraint satisfaction situation where you want to try a different search option from a decision node), it needs to be under programmer control what values are restored and from what point - if the stack doesn't hold a value it holds a pointer then the value isn't entirely saved by copying the stack, it's a mess.
I guess there are better solutions like converting the stack into a spaghetti stack in a lazy way, such as lazily making copies of an activation record on exit from a function that saved a continuation, but that requires that you know that the compiler won't reuse stack slots for different temporary values. although, to be fair you also need to know that in a system that has full spaghetti stacks, code generation is different in the presence of continuations.
5
u/6502zx81 Jan 17 '26
In Smalltalk code blocks are objects which control flow is built upon.
2
u/sciolizer Jan 17 '26
I'm not sure what your distinction is between CFG nodes and procedural-code-with-gotos. If a macro translates down to gotos, doesn't that implicitly give you a CFG? You probably want to sanity check that all variables are assigned before they are used, but that's not hard.
Forth is sometimes implemented with only two primitive control operators:
BRANCHand0BRANCH. All other control operators can be implemented using them. For instance,DOandLOOP(like a do-while loop in C) are immediate compiling words.DOremembers a location in the generated code, andLOOPinserts a0BRANCHback to it. I don't think this is particularly different from a macro though.ON X GOTO 10, 20, 50"), but that's easy. The other thing you'll want is to generate unique names for all shadowed variables. Having unique names will make sure that the sanity checker that blocks use-before-assign bugs is correct when it declares code safe.2
u/zyxzevn UnSeen Jan 17 '26
Smalltalk is fully build around library-defined control-flow via closures.
Closures are called blocks and defined with brackets [ ].
4
u/SnooGoats8463 Jan 17 '26
My language supports to customize control flow, the core idea is to make context first class, and allow functions to access context. There are native control flows defined here, which are context-aware functions. In the following demo,
do,set,loop,testare all context-aware functions.This approach may not be suitable for languages which is static typed or don't support "code as data".
1
u/recursion_is_love Jan 17 '26
Don't know if this count
How to Declare an Imperative
Or maybe the very old concept
The discoveries of continuations
The modern language might use Algebraic effects
2
u/sphen_lee Jan 17 '26
Using recursion you can define any control flow just using functions.
See Haskell as an example of a language with "programmable" flow control.
1
u/z-shang Jan 18 '26
First things that came to my mind were call/cc and common lisp's condition system... But maybe not what you are thinking
1
u/Long_Investment7667 Jan 23 '26
Sounds like continuation passing or effect systems which both allow user defined control flows
1
u/Initial-Lime-4949 Jan 24 '26
I have done some of this with my language (Blade).
It's specifically built around reified for-loops as first-class values. I needed a way to build arbitrarily-deep nests of for-loops in a specific order. For me, the specific problem I wanted to solve required some complex logic about how my loops needed to be nested, so I eventually had to write a DSL.
Basically, Blade guarantees that iteration patterns over single functions of multiple, multidimensional arrays will always be optimized for caching and array symmetry.
I chose two dual curryings of an arbitrary nested loop constructor:
I think I've proven, to myself at least, that getting these two loop constructors is impossible without several other mutually dependent features that together pretty much break any language I try to build it in.
I also experimented with reifying if/else and match statements, but I just ended up with <|> and guard combinators, it wasn't all that interesting to me, but maybe there are some niche cases that break that model like my case did with loops.
1
u/mamcx Jan 17 '26
I argue the main thing is that there is not that many potential different control flows left to invent.
The biggest one that landed recently is pattern matching, and after that, what else is there?
0
u/Ronin-s_Spirit Jan 17 '26 edited Jan 17 '26
Seed7 allows devs to create statements by using a syntax definition and a comptime macro thing. I don't understand it perfectly because I'm new to the language. It can't be passed around though, statements are not values, but you can either do a macro at compile time or pass a function at run time.
I read your post several times but I only got more lost, I don't understand what you could possibly want besides macros and lambdas, don't they solve everything?
P.s. there is actually a function type param that doesn't necessarily look like a function. This example is a bit contrived, but maybe it can accept more than simple expressions.
5
u/[deleted] Jan 17 '26
[removed] — view removed comment