# Do colored subprograms exist in Ada?

**URL:** <https://forum.ada-lang.io/t/do-colored-subprograms-exist-in-ada/343>\
**Category:** General\
**Created:** [February 24, 2023, 5:08pm UTC](https://forum.ada-lang.io/t/do-colored-subprograms-exist-in-ada/343 "2023-02-24T17:08:11Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cantanima](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cantanima/32/121_2.png) [@cantanima](https://forum.ada-lang.io/u/cantanima)\
**Post date:** [February 24, 2023, 5:08pm UTC](https://forum.ada-lang.io/t/do-colored-subprograms-exist-in-ada/343/1 "2023-02-24T17:08:11Z")

</div>

I was reading today about a proposal regarding the Rust language’s use of `async` and `await`. which are related to colored subprograms. If you’re familiar with them, read on; if not, click the blurred text at bottom for a hopefully-correct explanation.

I naturally wondered how this issue affects Ada. I’m not as familiar with `task` types as I should be, even though I used them a few years ago, and Ada 2022 brings the new `parallel` keyword, which I have yet to use. After refreshing my memory:

Is my impression correct that Ada’s approach to concurrency and parallelism circumvents “colored” subprograms altogether? After all, task entries are called just like regular subprograms, and subprograms with `parallel` blocks are called just like other subprograms.

My apologies if I’m being unclear; please don’t hesitate to correct or scold me if I’m babbling nonsense. I know what I’m trying to say, but I’m not used to talking about these particular issues.

I think [this page](https://journal.stuffwithstuff.com/2015/02/01/what-color-is-your-function/) is the origin of the term, and hopefully I can summarize the issue correctly as: subprograms have to be called differently depending on their “color”: `async` functions have to be called with a trailing `.await`, which means they can’t be used in places where non-`async` functions can. The use of `await` somewhere “taints” a subprogram (I don’t quite get why) so that every subprogram that wants to invoke it must also be declared `async`, chaining the requirements of `async`’s and `.await`’s to an unpleasant level. I’ve felt it most painfully when I want to use an `async` function in an iterator: no can do.

---

<div class="post-metadata">

**Author:** ![stephe-ada-guru](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/stephe-ada-guru/32/77_2.png) [@stephe-ada-guru](https://forum.ada-lang.io/u/stephe-ada-guru)\
**Post date:** [February 24, 2023, 10:05pm UTC](https://forum.ada-lang.io/t/do-colored-subprograms-exist-in-ada/343/2 "2023-02-24T22:05:38Z")

</div>

async and await can be implemented using Ada tasks, just as Rust and other languages implement them using tasking primitives; an async subprogram would call an entry in the task that starts a computation, and await would call an entry that receives the results. There’s more to it; the code that calls the async subprogram has to arrange to use the result somehow.

This is similar to “futures” implemented by other languages.

All such constructs are an attempt to make tasking easier to use. I prefer to be more explicit about what a task is, so I like the Ada model better. It might be interesting to see an AI ([Ada Rapporteur Group - Ada Interpretations](https://arg.adaic.org/ada-interpretations)) for this, if someone has an application where it is clearly better/simpler than Ada tasks.

---

<div class="post-metadata">

**Author:** ![cantanima](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/cantanima/32/121_2.png) [@cantanima](https://forum.ada-lang.io/u/cantanima)\
**Post date:** [February 25, 2023, 1:23am UTC](https://forum.ada-lang.io/t/do-colored-subprograms-exist-in-ada/343/3 "2023-02-25T01:23:35Z")

</div>

Thank you for the reply. I guess my question was unclear, no thanks to the headline:

> Do colored subprograms exist in Ada?

That makes it seem I was concerned with whether `async` and `.await` _were_ implented in Ada, and if not, _could_ they be.

I was actually more interested in the question stated in the body:

> [Does] Ada’s approach to concurrency and parallelism [circumvent] “colored” subprograms altogether?

That is, are the `task` and `parallel` “colored” the way `async` and `.await` are, tainting any subprogram that uses them into being called in a special fashion?

It seems, based both on what I’ve been reading and on your answer, _and_ on a review of how I used `task` 5 years ago, that the answer to the question I _meant_ to ask is, _ **No** _, and the answer to the question I _actually_ asked is, _ **You can implement them** _.

If that’s incorrect, I’d appreciate correction, but otherwise I appreciate the clarification.

---

<div class="post-metadata">

**Author:** ![zetah11](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/z/58f4c7/32.png) [@zetah11](https://forum.ada-lang.io/u/zetah11)\
**Post date:** [March 7, 2023, 10:18am UTC](https://forum.ada-lang.io/t/do-colored-subprograms-exist-in-ada/343/4 "2023-03-07T10:18:08Z")

</div>

To be a little bit theoretical about this, the reason `async`/`await` ends up coloring functions is because asynchronicity is a kind of “effect”, similar to exceptions: if some function `foo` raises an exception, then any other function `bar` which calls `foo` essentially also “raises” that exception, unless they explicitly catch it. Similarly, if some function `foo` pauses execution by `await`ing a future, then any function `bar` which calls `foo` effectively also pauses execution.

For various reasons (backwards compatibility, compiler transformations), some languages like to explicitly mark which functions may end up pausing execution like this, which is where the whole function coloring issue comes from. Imagine if you had to mark every function that might raise an exception with `excepts` - you’d run into the exact same issue. An `excepts` function can call any function, but a non-`excepts` function cannot call one marked `excepts`.

Actually, that last part isn’t quite true since you can actually catch exceptions, and therefore stop them from bubbling up. Most languages with `async` often do let you “catch” the asynchronicity - most Rust executors like Tokio give you a function `block_on` which lets you block on the result of a future - but it’s often a bit inconvenient.

AFAIK, Ada does not have any way of “marking” a function like with `async` or `excepts`, and so it doesn’t have this issue where you’re limited in what functions you can call. Still, it has exceptions, and so in a way, you still have function colors - they’re just not tracked by the compiler.

Anyways, one of the big reasons I really like Ada’s `task`s and the new `parallel` features is that they are examples of structured concurrency, which means that a function won’t return until all of the tasks it spawned have finished. It’s a bit like if you put `block_on` around the body of every `async` function, and it makes for a very predictable and composable way of multitasking.

---

<div class="post-metadata">

**Author:** ![simonjwright](https://forum.ada-lang.io/user_avatar/forum.ada-lang.io/simonjwright/32/11_2.png) [@simonjwright](https://forum.ada-lang.io/u/simonjwright)\
**Post date:** [March 7, 2023, 1:40pm UTC](https://forum.ada-lang.io/t/do-colored-subprograms-exist-in-ada/343/5 "2023-03-07T13:40:24Z")

</div>

> [@zetah11](#):
>
> Anyways, one of the big reasons I really like Ada’s `task`s and the new `parallel` features is that they are examples of structured concurrency, which means that a function won’t return until all of the tasks it spawned have finished.

Don’t know about others, but I’d think that for a **sub** program to create even one task would be very unusual indeed. Of course, I’m used to the Ravenscar profile, where tasks have to be created at library level.

No opinion on `parallel` features.

---

<div class="post-metadata">

**Author:** ![zetah11](https://forum.ada-lang.io/letter_avatar_proxy/v4/letter/z/58f4c7/32.png) [@zetah11](https://forum.ada-lang.io/u/zetah11)\
**Post date:** [March 7, 2023, 6:06pm UTC](https://forum.ada-lang.io/t/do-colored-subprograms-exist-in-ada/343/6 "2023-03-07T18:06:58Z")

</div>

That’s fair. Spawning tasks inside subprograms is something that should be done with a bit of care since the overhead can sometimes offset any gains, and the use of entries can easily create hard-to-debug deadlocks. Still, there are some situations where I’ve found it to be a useful tool, such as reading multiple large files at once. That’s a situation where you’d typically reach for `async`/`await` in another language, hitting the function coloring problem. Having tasks inside subprograms let’s you neatly encapsulate that.

That said, I’m not a terribly experienced Ada programmer, so it’s quite possible it is an anti-pattern.
