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
// sort : List -> List
= Nil
=
// Insert : U60 -> List -> List
=
=
// SwapGT : U60 -> U60 -> U60 -> List -> List
=
=
Nil = Nil
sort' (Cons x xs) = insert x (sort' xs)
insert v Nil = Cons v Nil
insert v (Cons x xs) = swapGT (if v > x then 1 else 0) v x xs
swapGT 0 v x xs = Cons v (Cons x xs)
swapGT 1 v x xs = Cons x (insert v xs)
sort'
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
// Sort : Arr -> Arr
=
// ToMap : Arr -> Map
= Free
=
=
// ToArr : Map -> Arr
= Null
=
=
let a =
let b =
// Merge : Map -> Map -> Map
= Free
= Used
= Used
= Used
=
=
=
= toArr 0 (toMap t)
toMap Null = Free
toMap (Leaf a) = radix a
toMap (Node a b) =
merge (toMap a) (toMap b)
toArr x Free = Null
toArr x Used = Leaf x
toArr x (Both a b) =
let a' = toArr (x * 2 + 0) a
b' = toArr (x * 2 + 1) b
in Node a' b'
merge Free Free = Free
merge Free Used = Used
merge Used Free = Used
merge Used Used = Used
merge Free (Both c d) = (Both c d)
merge (Both a b) Free = (Both a b)
merge (Both a b) (Both c d) =
(Both (merge a c) (merge b d))
sort t
On this example, we try a Radix
Sort, based on merging immutable
trees. This time, HVM's performance improves proportionally to the number of
cores. As such, in this test, it was able to sort large lists 9x faster than GHC! That's because
GHC is locked to a single thread, while HVM exploits the fact that tree-merging
is inherently parallel. Of course, one could parallelize the Haskell version
with par
annotations, but that would require refactoring. Usually, doing so is
very hard and time-consuming. In some cases, it is even impossible to use all
the available parallelism with par
alone. HVM, on the other hands, will
automatically distribute the workload evenly among all available cores, with
no added programmer effort.
Lambda Multiplication
// Increments a Bits by 1
// Inc : Bits -> Bits
= λex λox λix
let e = ex
let o = ix
let i = λ
// Adds two Bits
// Add : Bits -> Bits -> Bits
=
// Multiplies two Bits
// Mul : Bits -> Bits -> Bits
=
let e = End
let o = λ
let i = λ
-- Increments a Bits by 1
inc xs = Bits $ \ex -> \ox -> \ix ->
let e = ex
o = ix
i = \p -> ox (inc p)
in get xs e o i
-- Adds two Bits
add xs ys = app xs (\x -> inc x) ys
-- Muls two Bits
mul xs ys =
let e = end
o = \p -> b0 (mul p ys)
i = \p -> add ys (b0 (mul p ys))
in get xs e o i
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.