110 releases (10 stable)
1.0.20-beta | Dec 27, 2022 |
---|---|
1.0.11 | Feb 7, 2024 |
1.0.10 | Oct 16, 2023 |
1.0.9 | May 18, 2023 |
0.1.23 | Mar 27, 2022 |
#19 in Programming languages
237 downloads per month
Used in 10 crates
(4 directly)
560KB
6.5K
SLoC
High-order Virtual Machine (HVM)
High-order Virtual Machine (HVM) is a pure functional runtime that is lazy, non-garbage-collected and massively parallel. It is also beta-optimal, meaning that, for higher-order computations, it can be exponentially faster than alternatives, including Haskell's GHC.
That is possible due to a new model of computation, the Interaction Net, which supersedes the Turing Machine and the Lambda Calculus. Previous implementations of this model have been inefficient in practice, however, a recent breakthrough has drastically improved its efficiency, resulting in the HVM. Despite being relatively new, it already beats mature compilers in many cases, and is set to scale towards uncharted levels of performance.
Welcome to the massively parallel future of computers!
Examples
Essentially, HVM is a minimalist functional language that is compiled to a novel runtime based on Interaction Nets. This approach is not only memory-efficient (no GC needed), but also has two significant advantages: automatic parallelism and beta-optimality. The idea is that you write a simple functional program, and HVM will turn it into a massively parallel, beta-optimal executable. The examples below highlight these advantages in action.
Bubble Sort
From: HVM/examples/sort/bubble/main.hvm | From: HVM/examples/sort/bubble/main.hs |
|
|
On this example, we run a simple, recursive Bubble Sort on both HVM and GHC (Haskell's compiler). Notice the algorithms are identical. The chart shows how much time each runtime took to sort a list of given size (the lower, the better). The purple line shows GHC (single-thread), the green lines show HVM (1, 2, 4 and 8 threads). As you can see, both perform similarly, with HVM having a small edge. Sadly, here, its performance doesn't improve with added cores. That's because Bubble Sort is an inherently sequential algorithm, so HVM can't improve it.
Radix Sort
From: HVM/examples/sort/radix/main.hvm | From: HVM/examples/sort/radix/main.hs |
|
|
On this example, we try a Radix Sort, based on merging immutable trees. In this test, for now, single-thread performance was superior on GHC - and this is often the case, since GHC is much older and has astronomically more micro-optimizations - yet, since this algorithm is inherently parallel, HVM was able to outperform GHC given enough cores. With 8 threads, HVM sorted a large list 2.5x faster than GHC.
Keep in mind one could parallelize the Haskell version with par
annotations, but that would demand time-consuming,
expensive refactoring - and, in some cases, it isn't even possible to use all the available parallelism with par
alone. HVM, on the other hands, will automatically distribute parallel workloads through all available cores, achieving
horizontal scalability. As HVM matures, the single-thread gap will decrease significantly.
Lambda Multiplication
From: HVM/examples/lambda/multiplication/main.hvm | From: HVM/examples/lambda/multiplication/main.hs |
|
|
This example implements bitwise multiplication using λ-encodings. Its
purpose is to show yet another important advantage of HVM: beta-optimality. This chart isn't wrong: HVM multiplies
λ-encoded numbers exponentially faster than GHC, since it can deal with very higher-order programs with optimal
asymptotics, while GHC can not. As esoteric as this technique may look, it can actually be very useful to design
efficient functional algorithms. One application, for example, is to implement runtime
deforestation for immutable datatypes. In general,
HVM is capable of applying any fusible function 2^n
times in linear time, which sounds impossible, but is indeed true.
Charts made on plotly.com.
Getting Started
-
Install Rust nightly:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh rustup default nightly
-
Install HVM:
cargo install hvm
-
Run an HVM expression:
hvm run "(@x(+ x 1) 41)"
That's it! For more advanced usage, check the complete guide.
More Information
-
To learn more about the underlying tech, check guide/HOW.md.
-
To ask questions and join our community, check our Discord Server.
-
To contact the author directly, send an email to taelin@kindelia.org.
Dependencies
~6–9.5MB
~183K SLoC