r/ProgrammingLanguages • • Oct 15 '26

Truly Colorless Functions in Sabre

https://sabre.rroessler.io/blog/design-01-truly-colorless-functions
13 Upvotes

25 comments sorted by

15

u/initial-algebra Oct 15 '26

Should the spawn policy attach the custom scheduler to the Future created and cause all subsequent calls to async to use the custom scheduler, or should it only be used in that instance? My current through process is that spawning a future with a custom scheduler should be pervasive and that all subsequent calls should use that scheduler unless specified otherwise.

This is pretty much the same problem all effect systems face. "await" is an effect, and what you call a policy is a handler for the "await" effect. I assume the default policy is to block, while the async policy passes through the caller's policy (usually, this would be the default, and blocking would be an explicit policy, but that's more a matter of taste).

11

u/Positive_Total_4414 Oct 15 '26

Yeah, exactly. I appreciate the research effort and exploration, but this is exactly what function color is?

13

u/initial-algebra Oct 15 '26 edited Oct 15 '26

It does solve the colouring problem for first-order functions, but that's the easy part. It's the general case of higher-order functions that is tricky, which is also true for effect systems, so the can has merely been kicked down the road. (This is the case 9 times out of 10 when someone has claimed they have solved function colouring.)

2

u/Vraliance Oct 16 '26

I am looking into options to handle these higher-order functions, though I will start by providing simple functions such as Future.all and others (similar to JS) before I transition them to a syntactical solution

3

u/_choam_ Oct 15 '26

How dies koka solve it? When i tried it did have higher order functions, but kind of not

7

u/initial-algebra Oct 16 '26

It's not too dissimilar from how it looks in Haskell, where e.g. map takes a function with a polymorphic effect and has the same effect on its result. It's just specialized to algebraic effects vs. monads in general.

1

u/tending Oct 17 '26

And that doesn't count as kicking the can down the road?

5

u/initial-algebra Oct 18 '26

No, because you don't need to duplicate the definition of each higher-order combinator for each combination of effects. You just instantiate the polymorphic effect variable with something different. Also, unlike some more simplistic systems (e.g. second-class values), you aren't locked into the same effects and capturing the handlers from the caller if you don't want that.

2

u/Vraliance Oct 16 '26 edited Oct 16 '26

I will have to do some more research into how this problem arises with effect systems as it would be really useful to improve this design internally. Otherwise, execution policies are different as they are applied when a function is called and agnostic of the function they are executed in.

Realistically I designed it this way to simplify the programming experience when dealing with concurrency. By being explicitly at the call-site of a function, it is obvious how the function is being executed, what the outputs are based on the policy, and simplifies the overall experience of using async/await in a dynamic programming language. Many newer developers and even more experienced ones want a language to help them, and I think this solution does just that. In future, I hope that it can then also be used in an extensible way to provide other concurrency solutions

3

u/initial-algebra Oct 16 '26

execution policies are different as they are applied when a function is called and agnostic of the function they are executed in

That is how all effect handlers work.

2

u/Vraliance Oct 16 '26

This may be the case however effects themselves are a type of function coloring as they explicitly declare a potential restriction on a function that makes it harder to call by requiring an effect handler. This is exactly criterion 4 of being a “red” function.

When instead talking of non-effect based languages, that syntactic change may be similar to an effect handler but without an effect system it instead achieves an alternative goal, hence a different name. Better yet I choose the name execution policy from C++ usage of “std::async(std::launch::async, …)” where “std::launch” is named a policy.

3

u/initial-algebra Oct 16 '26 edited Oct 16 '26

Your language is effect-based, no matter what names you use. My point is that, to find inspiration to solve the problem you mentioned, you can look into existing research into effect systems.

I mean, in your docs folder in your repository, you have a page that mentions:

With these combined features, Sabre has removed function coloring entirely, whilst also exposing syntax for additional execution policies (such as error handling).

Error handling is just another kind of effect...

This may be the case however effects themselves are a type of function coloring as they explicitly declare a potential restriction on a function that makes it harder to call by requiring an effect handler. This is exactly criterion 4 of being a “red” function.

This is kind of silly, because by this logic, functions that require arguments are coloured because they're "harder to call". The colouring problem specifically has to do with needing duplicate definitions with incompatible (coloured) signatures, instead of a single definition with a unified signature.

2

u/Vraliance Oct 16 '26

I appreciate that and it has been a lot of help to get more inspiration. But I think we are still glossing over the core problem that function coloring originally presented. That being overhead to a developer by imposing restrictions in calling functions.

It isn’t silly at all and my example with Zig requiring the “std.Io” argument showcases this perfectly. Again being painful to the developer with needing to pass that argument around.

Currently Sabre does allow for singular function definitions whilst being able to have these be called synchronously, asynchronously or eventually otherwise. Looking only at the developer experience this is far simpler, concise and painless.

Exceptions are effects, no matter the language. But not all developers perceive them that way. I think as language designers we should be thinking not just of those with a high level of knowledge in PL design, but also those that don’t.

2

u/initial-algebra Oct 16 '26

Point #4 in the original article is about continuation-passing style in Node. The author goes on to say that features like "await" solve this subproblem. Yeah, it's slightly more typing than just calling the function, but it doesn't lead to a significant restructuring and incompatibility with other syntax elements (e.g. if/else, loops) like CPS does. Anyway, no matter how you bikeshed function call syntax, it doesn't address the coloured definition problem.

It isn’t silly at all and my example with Zig requiring the “std.Io” argument showcases this perfectly. Again being painful to the developer with needing to pass that argument around.

Currently Sabre does allow for singular function definitions whilst being able to have these be called synchronously, asynchronously or eventually otherwise. Looking only at the developer experience this is far simpler, concise and painless.

Well, it may be very easy to call a function with the default blocking policy, but func::async(args).await() is quite a bit more verbose than just having to pass an extra argument...

2

u/Vraliance Oct 17 '26

4. Red functions are more painful to call

I should've been more specific. There are explicitly 5 items that Bob originally proposes for function coloring so addressing this I think is important.

Using func::async(args).await() is no different or even simpler than io.async(func, args).await(io) for Zig at least. But each to their own.

3

u/initial-algebra Oct 17 '26 edited Oct 17 '26

Oh, I didn't realize it was also that verbose in Zig. I was expecting it to just be something like func(args, io), maybe with an await tacked on somewhere, with your choice of io determining normal vs. async execution.


Still, the real problem with calling "red functions", as originally proposed by the article, is that the CPS equivalent of e.g.

for (const x of xs) {
  await foo(x)
}

Is extremely difficult/annoying to write. Keep in mind, this was written more than ten years ago, before async/await was more widely available. The point of bringing up "painful to call red functions" is that it precludes the obvious workaround of "just make every function red", therefore essentially colourless, which is really what you've done, and what other effect-based languages also do. It's not a problem when you have async/await, unless you're a language like Rust, where you can't always pay the cost of slicing up what would otherwise be a native machine code subroutine into a state machine with lots of extra instructions.

The only real difference is that your default function call behaviour is to execute with a blocking policy instead of passing through the caller's policy and awaiting, whereas most other effect-based languages assume you want to pass through the caller's effect handlers and do the equivalent of awaiting all the time, unless you specify otherwise (e.g. with Effekt's boxing).

2

u/Vraliance Oct 17 '26

CPS is on my list to tackle, I’ve got a couple different ideas for it but they need a little extra though to them. I will have updates as I learn more and see what I can produce. In terms of every function being red and therefore colorless, this stems from using fibers for concurrency. So every function call has access to the current fiber “Isolate” (attached to the function frame) that they can use for async/await. For Sabre this works well, but for Rust you’re right in that it could muddy compiled outputs.

Internally default function calls simply jump to the bytecode of the next function. Otherwise an async call will instead create a new fiber “Isolate” that invokes the function. Do any effect-system languages use fibers or something else for async/await?

→ More replies

2

u/pthierry Oct 16 '26

Wait, if functions don't have a color, what happens if you synchronously call a function that does things asynchronously?

2

u/Vraliance Oct 17 '26

The async policy simple creates a fiber for executing that function called. Similar to goroutines, though I want to have an alternative execution policy to allow setting the scheduler for further async calls or perhaps forcing all calls to be synchronous

1

u/pthierry Oct 17 '26

If the developer can make a call and not know that it might run things asynchronously, that seems pretty unsafe…

Concurrency is hard enough, but surprise concurrency? Ouch.

1

u/Vraliance Oct 17 '26

Surprisingly this occurs in other languages and is called unstructured concurrency. It is slightly unsafe, unless we incorporate some structuring. There’s a great article for this in Go: Structured Concurrency in Go

The goal would be to have library functions that help structure places where developers would use concurrency. Additionally, all internal library methods will adhere to using no async unless absolutely necessary. Realistically any program could have unknowable artifacts, suppose Python or Node JS had a class spawn a hidden process without the developer having access or knowledge. This violates the same principle.

Sabre already uses “Future” values for structuring, and I hope to include others such as stop tokens, to help improve developer access to potentially hidden fiber instances.

1

u/pthierry Oct 18 '26

No, not all programs have significant unknowable artifacts, and some programming languages work hard at reducing drastically their mere possibility.

It's a huge part of what I love in Haskell.