r/ProgrammingLanguages • u/FedericoBruzzone • May 21 '26
Mutable Value Semantics (MVS) or Ownership & Borrowing: A Trade-off Analysis
I'm continuing the research on semantics for a new language. After studying Mutable Value Semantics (MVS) in the first post (reddit discussion), I wrote a follow-up that examines the trade-offs between MVS and the Ownership & Borrowing model.
The post covers:
- Friction points in Rust's borrow checker
- Where Hylo's MVS solves them and where it introduces new trade-offs
- Swift's hybrid approach and its runtime exclusivity checks
- Open questions I'm exploring for my own language design
I'd love to hear your thoughts.
Link: https://federicobruzzone.github.io/posts/eter/MVS-or-ownership&borrowing.html
21
Upvotes
6
u/RedCrafter_LP May 21 '26
I faced the life time issue as well in my experience. I'm also writing my own language and based it in its core on rust and many of its principles. Something my language completely lacks are explicit lifetime annotations.
I took inspiration from a pattern every c developer knows
What's happening here is that the read function has some data that outlives it's scope storing the data in a local variable in read would have erased the data on return. To fix it read "forward declared" it's need for a buffer and the calling function (main) provided it.
Using this forward declaring system and some static analysis a function can determine which size of a buffer it needs to return the largest possible local struct that gets returned as a reference from a function. This conservative approach makes things rather easy. Check every path for possible referenced data in the returned value and create a state machine like struct for the forward declared buffer. This buffer is taken by reference implicitly by the function and every calling function is either making space for it or again forward declares it part of its own forwarding struct to the next caller. This is 100% compile time solvable. In reality you likely need a forward declare struct size limit to not potentially blow the stack up the stack exponentially. Recursive programming that is not tail resurrection folded and contains a forward declaration struct is especially bad. I'm not supporting recursion therefore this is not an issue in my language.
This completely eliminates the guess work of which reference is returned because both references are marked as "potentially contained in this function return structure" therefore the calling function conservably extends the lifetime of both references (and their owner somewhere up the stack) to the most conservative point necessary to fulfill the "either could be returned" scenario.
6
u/SkiFire13 May 22 '26
Note that the compiler is pretty clear to which references are missing lifetime annotations:
At no point it's highlighting the
eargument of the return type ofcall, all mentions are for theFn(&u8, &u8) -> &u8trait instead. Following the compiler suggestion leads to another error, and after following its suggestions again you end up with code that compiles and is less restrictive for the caller (albeit this might not be the case in a more realistic scenario).I would wager that most issues people have with lifetimes are due to randomly sprinkling lifetime annotations around (often the same lifetime, which has important consequences!) in the hope that it fixes the compiler error.
Note that catching panics is not required for that issue, having destructors is also enough because they make the same kind of observation after the panic happened.
If you language performs unwinding then it likely suffers from this issue unless it preverts "borrowing" from struct fields and leaks borrowed locals on unwinding.
I'm not an expert of Hylo but looking at its website I can see an example using
do-catch, although that's not explained anywhere. I wonder if that's an actual feature or a leftover from an earlier iteration.Regarding Hylo, the approach looks very cool, but I wonder if it's really simplier than Rust. Yes, lifetimes are complicated, but they are just one concept in the end. Hylo introduces so many new concepts and keywords that it pretty overwhelming.
2
u/Express_Job_5731 May 24 '26
You should definitely check out Mojo. It learned quite a lot from both Hylo and Rust and took a step further. Reference safety without the pain and suffering:
-Chris Lattner (have no idea why reddit messed up my name, whatever)
2
u/pranabekka May 24 '26
`split_at` is interesting. I wonder how expensive a general solution would be to compute. I was thinking that when a variable index is encountered in a loop, like `arr[j]`, it goes back to the source of the variable, and if it only increments, then each index will be distinct at runtime. If the index is a range, like `arr[j..j+1]`, then we'd need to look at the amount by which the variable increments. For example, if the increment is `j += 3`, and indexing uses an offset less than 3, like `arr[j..j+2]`, then that can be guaranteed not overlap as well. I haven't though much about complex indexes like `arr[j+n]` with conditional increments like `if cond { n += 3 } else { n += 5 }`, but even just the simpler optimisations should be useful.
2
u/neel-krishnaswami May 31 '26
Hi,
This post series has been really helpful for me in understanding what MVS actually is.
However, one thing that would really help me (and maybe you?) is to actually work out what the ownership patterns in actual algorithms and architectures are like, and how they can be expressed.
For example, what does the union-find algorithm look like? The backwards-pointing parent pointers in it are a challenge to most ownership systems. I think Rust implementations usually just punt and use an indices into an array based arena. Would you have to do the same in Hylo?
As another example, how would a callback-based event loop (like the JS DOM, or MVC in general) be expressed? This is the boss fight for ownership systems, because you're dealing with mutable collections of imperative callbacks, which run in dynamically-determined cascades implicitly triggered by the event loop. A lot of people these days just give up and suggest sticking to immediate mode, but maybe ownership can actually help here? (Eg, what happens if we restrict every onFoo() event handler to taking Hylo-style subscripts?)