r/ProgrammingLanguages • • Apr 30 '26

The Mutable Value Semantics (MVS): A Non-superficial Study

I’m researching a new semantics for a new language. During this process, I studied the Mutable Value Semantics (MVS) and I wrote a post about it. I simply want to share this study with you all and I'd love to hear your thoughts.

A new post will be coming soon where I'll discuss the limitations I’m finding in current models, and what I’d like to have for my own.

Link: https://federicobruzzone.github.io/posts/MVS.html

28 Upvotes

28 comments sorted by

View all comments

Show parent comment

3

u/tmzem May 02 '26

Thanks for this reply, lots of interesting points. Could you explain these a bit further?

One can represent positions with independent values (e.g., indices) and use inversion of control to access the contents of a collection [...] makes the APIs of generic algorithms dramatically simpler

Using indices into a collection is also used by ParaSail language. While this works well with array-based collections, I wonder how well (performant) it would word with Tree-based collection like BTreeMap's where accessing and index inside a loop would require (the often same) chain of dereferences.

Also I wonder how you would easily represent lazy algorithms which are usually modeled with iterator adapters that hold references internally (map, filter, zip), and how index-based access would make APIs cleaner given that you'd have to pass/thread the collection explicitly. Could you give an example?

Remote parts are only for the closures and they are not strictly necessary

How could one implement the ArraySlice<E> type mentioned in the Language Tour's Concurrency section without remote parts?

3

u/l33n44 May 02 '26

> Could you explain these a bit further?

AFAICT performance does not suffer from this kind of representation. Intuitively, an index into some large data structure is similar to a pointer and thus accessing an array at index i is not more expensive than dereferencing. If you have to follow n references then you'll access an array n times instead. Both operations are essentially the same, except that the array can have even better cache locality if you design your data structure carefully.

In fact, one can treat an array like a small memory region and indices into this array as pointers into the region. From there, one can build any abstraction they like to expose a nice API. You can look at the implementation of abstract syntax trees in Hylo's compiler for a real-world example in a performance-critical application.

> Also I wonder how you would easily represent lazy algorithms which are usually modeled with iterator adapters [...]

The first thing to note is that an operation that would typically return a reference in Rust can be expressed as an operation that runs a lambda instead. That is, one can turn

struct S { x: i32 }
impl S {
  fn data(&self) -> &i32 { &self.x }
}

into

struct S { x: i32 }
impl S {
  fn with_data<T, F>(&self, f: F) -> T
    where F: FnOnce(&i32) -> T
  { f(&self.data) }
}

The goal of this transformation is to avoid having to track the lifetime of the field reference. In a language where references are first class, you know that one cannot escape the "reference" that is passed as argument and therefore it cannot possibly live longer than a call to the lambda, which itself cannot live longer than a call to with_data. Add some fancy sugaring around this and you get Hylo's subscripts.

But there's more! This transformation gives you an opportunity to run any code you want after the lambda returned, which can be understood as a way to "cleanup" whatever was necessary to setup the argument. So one could write something like this:

struct Array { xs: Vec<i32> }
struct Slice { xs: Vec<i32>, start: isize, end: isize }
impl Array {
  fn with_mut_slice<T, F>(&mut self, start: isize, end: isize, f: F) -> T
    where F: FnOnce(&mut Slice) -> T
  {
    let mut s = Slice{ xs: self.xs, start: start, end: end };
    let r = f(&mut s);
    self.xs = s.xs;
    r
  }
}

Rust won't let you move something out of a mutable borrow on `Self` but Hylo will. And so that's one way to get slices. Essentially you are "temporarily" moving the contents of a data structure into some wrapper that will expose only part of it.

If you don't want to move things around then you could use pointers under the covers, wrapped into some safe API. But again, not every programming language has to care about every last bit of operation so if paying for two moves is acceptable there's no need to venture into unsafe land.