r/ProgrammingLanguages • u/Vovandosy • Aug 07 '26
Aren't rust's lifetimes basically just coeffects?
I was talking with a LLM, discussing effects and coeffects and how they may be interestingly used in language design
So, in one moment after I understood coeffects and effects are often used in pairs (like async/async ctx , io/world, ect.), i thought that coeffect scopes sometime should be labeled somehow to avoid shadowing
for example in my syntax :
some_fn :=
## capturing the scope coeffect and assigning it into a label
().use 'label := ().use FnScope
## some function
longjump () ? :=
().use _ := ().use 'label
return () ?
##...
()
## so, somewhere in some inner call we may write
another_fn :=
...
longjump() ?
...
## and the execution will be returned into the function where `longjump` were declared
but for this to be valid, it is important for the label not to outlive the scope where it was declared
then, i also thought : it would be good to have an ability to write these labels in the effect's declaration
## somewhere outside
().use 'ctx := ...
## a function that is async for both contexts — don't know for which cases it may be useful but why not. instead of async, it may be some another effect that uses some context/scope/world/coeffect
f() use AsyncCtx do Async '_ do Async 'ctx := ...
so, then i thought : effects and coeffects in my system are declared just like type constructors without the last type (aka citizens of *→* kind), so it would be logical for any type to be able to take a context label as a polymorphic (or depending) parameter.. and rust's references do exactly this.
so rust's
fn f<'b, 'a : 'b>(smth : &'a mut &'b Smth2, smth2 : &'b Smth2) {
*smth = ...
}
just takes some coeffects 'a and 'b.
it is equivalent to that like for smth it raises an effect to overwrite the world 'a and for smth2 it just returns the value into scope, where 'b is active (and 'a : 'b means that the scope containing world 'a is located inside scope containg world 'b)
so, am i thinking right about it? may it have some practical uses in pl design? any more ideas?
p.s.: sorry for my english not being perfect.. don't be humble to re-ask something if you did not understand.
31
u/onlyrealcuzzo Aug 07 '26 edited Aug 07 '26
I'd recommend trying to describe the problem in terms of Rust, as more people are likely familiar with that. I can't even figure out what you're trying to solve or ask (and I'm building a language in this space).
Your last two examples in (presumably) your prototype language and Rust appear to be completely different, so it's hard to tell what you're proposing.
If you're asking if you could just rename lifetimes as "coeffects", the answer is obviously yes. If you're asking if lifetimes could be better, there's a lot of people that have shown ways this can be done - see Group Borrowing: https://verdagon.dev/blog/group-borrowing.
13
u/initial-algebra Aug 07 '26 edited Aug 07 '26
Strictly speaking, lifetimes themselves are not coeffects, but grades of an overarching "is valid for" coeffect. This comment from Neel Krishnaswami may help to understand exactly what a coeffect is. Interestingly, and maybe confusingly, you can think of sets of effect markers like
asyncandioas grades for a "has effect" coeffect. So...lifetimes are actually maybe more like sets of effect markers, and this lines up nicely with the interpretation of lifetimes as sets of loans used in Rust's new borrow checker, Polonius. Lifetimes are also very similar to environment classifiers (also called support variables or context variables) from staged computation, which are sets of variable names, or grades of a "has free variable" coeffect for safely handling quoted/future-stage code.EDIT: This is not quite right. If lifetimes are coeffect grades, then loans cannot correspond to effect markers, because the order is wrong! If a lifetime is a set of loans, then Rust's subtyping rule for references is that
&'a Tis a subtype of&'b Tiff'ais a subset of'b. However, the "has effect" coeffect actually enriches values with capabilities to perform the effects, so subtyping must decrease the set of effect markers. Therefore, loans are...anti-effect markers? I guess that kind of makes sense, actually, since the only valid way to access something a reference has borrowed is through that reference, meaning the corresponding capability has been removed from the context. Fascinating!3
u/probabilityzero Aug 07 '26
You can think of lexical region types as coeffects. Not sure if anyone has written that up exactly before.
Rather than digging into the nitty gritty details of Rust lifetimes, if you want to explore this, I'd start with the Tofte and Talpin region calculus and try to encode it with coeffects. Could you think of region annotations as grades, so a region annotated type is a graded modal type?
3
u/Aaron1924 Aug 07 '26
The way Rust represents its lifetimes is not really related to effects, they're part of the reference types and subject to subtyping, but you can simulate Rust-style ownership and borrowing using a sufficiently expressive effect system
There was a talk at IWACO 2024 by Lionel Parreaux about how to do this; the audio in the recording is a little fucked but otherwise very enjoyable talk
1
u/riz0id Aug 07 '26
Your question is incomplete so it can’t be answered. Normally a question like this would be answered by inspecting the semantics of rust lifetimes and the semantics of a “coeffect”. Once you have those two definitions, then you can test for equivalence by implementing coeffects with lifetimes, and then implementing lifetimes with coeffects.
Nobody can faithfully answer this question without a definition of coeffects though. I think the similarity between the two that you’re seeing is they’re both coalgebraic, which is really abstract: a record/struct is a “coalgebra” in the same sense that lifetimes, or any contextual programming language feature would be. It may be worth pointing out that a records/structs are not equivalent to lifetimes or a hypothetical “coeffect” system.