22 releases
Uses new Rust 2021
0.1.25 | Apr 26, 2022 |
---|---|
0.1.24 | Apr 4, 2022 |
0.1.23 | Mar 27, 2022 |
0.1.18 | Feb 28, 2022 |
0.1.0 | Jan 31, 2022 |
#2 in Programming languages
825 downloads per month
Used in kind2
240KB
5.5K
SLoC
High-order Virtual Machine (HVM)
High-order Virtual Machine (HVM) is a pure functional compile target that is lazy, non-garbage-collected and massively parallel. It is also beta-optimal, meaning that, in several cases, it can be exponentially faster than most functional runtimes, including Haskell's GHC.
That is possible due to a new model of computation, the Interaction Net, which combines the Turing Machine with the Lambda Calculus. Previous implementations of this model have been inefficient in practice, however, a recent breakthrough has drastically improved its efficiency, giving birth to the HVM. Despite being a prototype, it already beats mature compilers in many cases, and is set to scale towards uncharted levels of performance.
Welcome to the inevitable parallel, functional future of computers!
Usage
1. Install it
First, install Rust. Then, type:
cargo install hvm
2. Create an HVM file
HVM files look like untyped Haskell. Save the file below as main.hvm
:
// Creates a tree with `2^n` elements
(Gen 0) = (Leaf 1)
(Gen n) = (Node (Gen(- n 1)) (Gen(- n 1)))
// Adds all elements of a tree
(Sum (Leaf x)) = x
(Sum (Node a b)) = (+ (Sum a) (Sum b))
// Performs 2^n additions in parallel
(Main n) = (Sum (Gen n))
The program above creates a perfect binary tree with 2^n
elements and adds
them up. Since it is recursive, HVM will parallelize it automatically.
3. Run and compile
hvm r main 10 # runs it with n=10
hvm c main # compiles HVM to C
clang -O2 main.c -o main -lpthread # compiles C to BIN
./main 30 # runs it with n=30
The program above runs in about 6.4 seconds in a modern 8-core processor, while the identical Haskell code takes about 19.2 seconds in the same machine with GHC. This is HVM: write a functional program, get a parallel C runtime. And that's just the tip of iceberg!
See Nix usage documentation here.
Benchmarks
HVM has two main advantages over GHC: automatic parallelism and beta-optimality.
I've selected 5 common micro-benchmarks to compare them. Keep in mind that HVM
is still an early prototype, so it obviously won't beat GHC in general, but
it does quite well already and should improve steadily as optimizations are
implemented. Tests were compiled with ghc -O2
for Haskell and clang -O2
for
HVM, on an 8-core M1 Max processor. The complete files to replicate these
results are in the bench/
directory.
List Fold (Sequential)
main.hvm | main.hs |
|
|
*the lower the better
In this micro-benchmark, we just build a huge list of numbers, and fold over it to sum them. Since lists are sequential, and since there are no higher-order lambdas, HVM doesn't have any technical advantage over GHC. As such, both runtimes perform very similarly.
Tree Sum (Parallel)
main.hvm | main.hs |
|
|
TreeSum recursively builds and sums all elements of a perfect binary tree. HVM outperforms Haskell by a wide margin because this algorithm is embarassingly parallel, allowing it to fully use the available cores.
QuickSort (Parallel)
main.hvm | main.hs |
|
|
This test modifies QuickSort to return a concatenation tree instead of a flat
list. This makes it embarassingly parallel, allowing HVM to outperform GHC by a
wide margin again. It even beats Haskell's sort from Data.List! Note that
flattening the tree will make the algorithm sequential. That's why we didn't
chose MergeSort, as merge
operates on lists. In general, trees should be
favoured over lists on HVM.
Composition (Optimal)
main.hvm | main.hs |
|
|
This chart isn't wrong: HVM is exponentially faster for function composition,
due to optimality, depending on the target function. There is no parallelism
involved here. In general, if the composition of a function f
has a constant-
size normal form, then f^(2^N)(x)
is linear-time (O(N)
) on HVM, and
exponential-time (O(2^N)
) on GHC. This can be taken advantage of to design
novel functional algorithms. I highly encourage you to try composing different
functions and watching how their complexity behaves. Can you tell if it will be
linear or exponential? Or how recursion will affect it? That's a very
insightful experience!
Lambda Arithmetic (Optimal)
main.hvm | main.hs |
|
|
This example takes advantage of beta-optimality to implement multiplication using lambda-encoded bitstrings. Once again, HVM halts instantly, while GHC struggles to deal with all these lambdas. Lambda encodings have wide practical applications. For example, Haskell's Lists are optimized by converting them to lambdas (foldr/build), its Free Monads library has a faster version based on lambdas, and so on. HVM's optimality open doors for an entire unexplored field of lambda-encoded algorithms that were simply impossible before.
Charts made on plotly.com.
How is that possible?
Check HOW.md.
How can I help?
Most importantly, if you appreciate our work, please help spread word of the project! Sharing on Reddit, HN, and other communities helps more than you think.
Second, I'm looking for partners! I believe HVM's current design is ready to scale and become the fastest runtime in the world, but a lot still needs to be done to get there. We're also building interesting products built on top of it. If you'd like to get involved, please email me, or just send me a personal message on Twitter.
Community
To follow the project, please join our Telegram Chat, the Kindelia community on Discord or Matrix!
Dependencies
~3–4MB
~90K SLoC