LanguagesArchitecture
Note: Regions are still a work-in-progress. Part 1 has been successfully prototyped, but parts 2-5 are only a preview describing how we expect them to work in practice, to show where we're headed and what we're aiming for. They could surpass our wildest expectations, or they could shatter and implode into a glorious fireball, who knows! Follow along as we implement all this, and reach out if anything isn't clear! 0 1

Vale has an ambitious goal: to be fast, memory safe, and most importantly, easy. There are a lot of stellar languages that have two, and we suspect it's possible to really maximize all three.

To do this, we're harnessing a new concept called regions.

In Part 1 we saw how we can use pure functions to easily immutably borrow data to make it faster to access.

Part 2 showed us how we could more precisely create regions via isolates, and immutably borrow them too.

Part 3 showed us how we can get the benefit of isolates with many more kinds of data.

Then in Part 4, we saw how we could immutably borrow only part of an object, so that we could have more precise control over what parts of our data were immutable.

In this final part, we'll show how we can limit some data to never outlive a certain region, and how that enables useful optimizations like zero-cost iteration.

Cells and Iteration

In Part 4, we saw a basic linked list.

Here it is again, renamed to LinkedListNode to be consistent with Vale's standard library.

Now, we're going to add a LinkedList class which holds the head of the linked list.

The '' in ''?^LinkedListNode<T> means we're holding the head of the list in a cell, like we saw in Part 2.

struct LinkedListNode<T> {
  ship T;
  next priv vary ?^LinkedListNode<T>;
}

struct LinkedList<T> {
  head ''?^LinkedListNode<T>;
}

Here's a basic use case, where we're turning the list immutable to iterate over it faster, and subtracting from each ship's fuel.

exported func main() {
  list = LinkedList<Ship>();
  list.add(Ship("Serenity", 10));
  list.add(Ship("Raza", 22));

  foreach x in list.iter() {
    set x.fuel -= 5;
    println("Reduced {x.name}'s fuel");
  }
}
stdout
Reduced Serenity's fuel
Reduced Raza's fuel

There are three surprising things here:

  • There's no change in the List's API, the usage of a cell is entirely encapsulated within LinkedList and its functions.
  • Even though the list is immutable, we can still modify the ships inside. This is because of Vale's "multi-region data" described back in Part 4.
  • We get these benefits even when iterating!

Let's talk about that last one, as it involves some concepts we haven't really covered before.

Side Notes
(interesting tangential thoughts)
0

If anything isn't clear, feel free to reach out via discord, twitter, or the subreddit! We love answering questions, and it helps us know how to improve our explanations.

1

We're aiming to complete regions by early 2024, check out the roadmap for more details.

foreach is just while with an iterator

Under the hood, this is using an iterator, a small struct that keeps track of where we are in the list.

foreach is just syntactic sugar for using iterators.

Here's the above program, without the foreach syntactic sugar.

First, it takes the expression after in (here, list.iter()) and puts it in a local, iterable. (1)

Then, it makes an iterator for it using its Begin function. (2)

Every iteration, it will first call HasNext to see if there's a next element (3), and then call Next to get it. (4)

exported func main() {
  list = ...;

  iterable = list.imm; // 1
  iterator = iterable.Begin(); // 2
  while iterator.HasNext() { // 3
    x = iterator.Next(); // 4

    println(x);
  }
}

There are two interesting things happening here, regarding regions.

First, list.iter() is borrowing a cell immutably, and producing a cell guard like we saw in Part 2. Here we're returning that cell guard. 2

Second, we're using Begin to make an iterator that is scoped to the lifetime of that cell guard. The compiler then makes sure the iterator won't outlive the cell guard.

Let's talk about that a bit more!

2

Draft TODO: Talk about how this requires an RC, or scope tether uprooting.

Limiting Structs

When we say iterable.Begin(), we're giving Begin a ?&i'LinkedListNode<Ship>, where i' is the implicit hidden region tied to the lifetime of the iterable cell guard, like we saw in Part 2.

LinkedList.vale has a function which accepts that type:

func Begin<x', T>(head ?&x'LinkedListNode<T>) LinkedListIter<x', T> {
  return LinkedListIter<x', T>(head); 3
}

The call will return a LinkedListIter<i', Ship> which we can later call .HasNext() and .Next() on.

Note the i' in LinkedListIter<i', Ship>.

When a struct has a region generic parameter, the compiler makes sure that the struct doesn't outlive that region.

So here, we know that LinkedListIter<i', Ship> won't outlive i'. i' came from the cell guard, so the iterator won't outlive the cell guard.

That's what it means to "limit" a struct; if a struct has a region parameter, the compiler makes sure it does not outlive that region.

3

The <x', T> is just included for clarity, the compiler can infer it if we leave it out.

Other Collections, Classes, and Architectures

We can use this pattern for any data structure, not just LinkedList.

Vale's List is an array list with a cell.

This lets us iterate over the array with zero generation checks.

struct List<T> {
  array priv vary ''[]E;
}

HashSet, HashMap, and all the collections have cells under the hood. These are private implementation details that the user doesn't have to worry about.

We can use this for any class; Part 2 showed us how a Ship can have a private Engine in a cell.

These techniques can even make entire architectures fast. Entity-component-system is an architecture that holds all of its state in List<T>s, and iterates through them a lot. Since iterating is now zero-cost, architectures like this become much faster in Vale.

Conclusion

As you can see, structs can be "scoped" to the lifetime of an existing region, such as one that's produced by opening a cell. This lets us more flexibly access data from a region.

This is especially useful for iterators, which like to immutably open a collection's contents and read them much faster.

4

That's all for now! We hope you enjoyed this article. Stay tuned for the next article, which shows how one-way isolation works.

If you're impressed with our track record and believe in the direction we're heading, please consider sponsoring us on GitHub!

With your support, we can bring regions to programmers worldwide.

See you next time!

- Evan Ovadia

4

Draft TODO: talk about how all these techniques come together to give us a ton of optimization power. kind of like a super-powered blend of borrow checking, shared mutability, and other stuff.

We're looking for sponsors!

With your help, we can launch a language with speed, safety, flexibility, and ease of use.

We’re a very small team of passionate individuals, working on this on our own and not backed by any corporation.

If you want to support our work, please consider sponsoring us on GitHub!

Those who sponsor us also get extra benefits, including:

  • Early access to all of our articles!
  • A sneak peek at some of our more ambitious designs, such as memory-safe allocators based on algebraic effects, an async/await/goroutine hybrid that works without data coloring or function coloring, and more.
  • Your name on the vale.dev home page!

With enough sponsorship, we can:

  • Start a a 501(c)(3) non-profit organization to hold ownership of Vale. 5
  • Buy the necessary computers to support more architectures.
  • Work on this full-time.
  • Make Vale into a production-ready language, and push it into the mainstream!

We have a strong track record, and during this quest we've discovered and implemented a lot of completely new techniques:

  • The Linear-Aliasing Model that lets us use linear types where we need speed, and generational references where we need the flexibility of shared mutability.
  • Region Borrowing, which makes it easier to write efficient code by composing shared mutability with the ability to temporarily freeze data.
  • Higher RAII, where the language adds logic safety by enforcing that we eventually perform a specific future operation.
  • Perfect Replayability makes debugging race conditions obsolete by recording all inputs and replaying execution exactly.

These have been successfully prototyped. With your sponsorship we can polish them, integrate them, and bring these techniques into the mainstream. 6

Our next steps are focused on making Vale more user-friendly by:

  1. Finalizing the compiler's error messages and improving compile speeds.
  2. Polishing interop with other languages.
  3. Growing the standard library and ecosystem!

We aim to combine and add to the benefits of our favorite languages:

We need your help to make this happen!

If you're impressed by our track record and believe in the direction we're heading, please consider sponsoring us:

If you have any questions, always feel free to reach out via email, twitter, discord, or the subreddit. Cheers!

5

Tentatively named the Vale Software Foundation.

6

Generational references, the linear-aliasing model, and higher RAII are all complete, and region borrowing, fearless FFI, and perfect replayability have been successfully prototyped. Be sure to check out the experimental version of the compiler!