This is a short and non-technical blog post demonstrating why the Haskell programming language is a good choice for building cryptocurrencies.
Cryptocurrencies are different from other programs in one key way: One mistake in the code can result in the instantaneous loss of all value of all users.
This is not often the case with other software. In conventional applications and services, mistakes in the code lead to downtime, unsatisfied users, or a successful hack of the service provider. Only in the rarest cases does this mean the complete and permanent destruction of the service and all invested money being gone for good. Recovery is usually possible for ordinary business software. In cryptocurrencies code is money, so extra caution is needed when writing it.
While other financial software also needs lots of caution, mistakes in cryptocurrencies can be especially severe due to their decentralised nature. Traditional banking has safety mechanisms like chargebacks and contracts between banks that make it possible to undo transactions correct mistakes after they happened. In cryptocurrencies, money that's gone is usually gone for good.
How do you avoid mistakes in your code? One way is to hire excellent programmers. However, all programmers are humans and they will eventually make mistakes. It's safer to have human-written code cross-checked by a machine to the fullest extent possible. It is crucial to use a programming language that makes mandatory automated checks easy to write.
Haskell is a programming language that was designed for safety. Haskell provides hard guarantees that certain kinds of mistakes cannot be in the code. How does it do this? Let's look at some features that make this possible.
Memory safety. This means that you cannot accidentally modify values in your computer's memory that you did not intend to modify. Haskell prevents mistakes where garbage data is written into memory and the program is exhibiting arbitrary behaviour from that point. It also makes one of the most dangerous security vulnerabilities, remote code execution by an attacker, impossible.
Static types.. Haskell enforces that the operations you do with your data are sound. Any function that processes data must declare what types of data (such as numbers, user names, cryptographic hashes) are consumed or produced by it. When you try to feed a function data of the wrong type Haskell will complain and refuse to build your code. This means the problem is detected long before the code ever ships, and in fact it's impossible to ship the code until the mistake is corrected. The types of all things being explicitly clear also makes it easier to understand the code. The ease of understanding enables programmers on your team to work faster and makes the job of external experts easier.
Purity. Purity is the ability to prove the
absence of unwanted side-effects. In Haskell you can
indicate (usually done with the
IO type) whether a
function in the code has any interaction with the outside world. If
you indicate that code doesn't interact with the outside world,
Haskell guarantees that it will not do so. If a function
with the type
checkSufficientBalanceForTransaction :: User
-> Amount -> Result does not carry an
label you know it cannot access the Internet, that it cannot read
any files, and that it cannot by itself have any visible effect on
the world. You are guaranteed that all it can do is access the data
that is passed in (the
User whose balance is to be
checked, and the
Amount the user would like to send)
and return a
Result that says whether the user's
balance is sufficient. This dramatically reduces the amount of code
you have to vet against malicious inputs. Pure functions are
guaranteed to always return the same output value given the same
input values. This property allows the Haskell compiler to produce
programs that run faster and use all available CPU cores. It also
eases testing and quality assurance.
Haskell forces you to write code that is explicit, covers all corner cases, and is hard to misunderstand.
Raw technical features are not the only reason why Haskell is becoming popular for writing cryptocurrencies. It is also the ecosystem that surrounds the language, and the mind set of Haskell programmers that make it an attractive choice.
Haskell tends to select for engineers who are particularly interested in creating quality focused, high reliability software. Some Haskellers started their programming career with Haskell and stuck to it, but this is still rare. Most Haskellers chose Haskell after a long journey in search for better tools and improved productivity. They are often versed in many programming languages and have explored myriad techniques.
Haskell programmers tend to be well trained and enjoy understanding the problem domain before starting to code. This discipline is crucial when working on high-risk projects like cryptocurrencies.
The design and implementation of new programming language features often happens in Haskell before they are adapted into other languages. This entices software developers that wish to work outside of current boundaries. The diversity, passion, and experience makes the Haskell community a great place to build teams for creating new technology.
While Haskell and the surrounding tools and libraries are sophisticated and require effort to get into, those that do master them find themselves equipped with an unexpected power: being able to own their entire programming toolchain. An expert Haskell programmer can modify the code their team writes, the libraries that the project depends on, down to the Haskell compiler that creates the final machine code. This “hackability” of the Haskell toolchain brings various benefits:
stackmake this straightforward, allowing you to easily swap out upstream projects with your own forks. This reduces the risk relying on dependencies exposes to your project.
This is different compared to many other programming languages. Even an expert C++ programmer often does not feel equipped to make changes to the C++ compiler due to the complexities and sheer amount of code involved. The GCC compiler has over 7 million lines of code.
The Haskell toolchain is clean and consists of relatively small amounts of code. GHC totals around 0.5 million lines, including all of its Haskell dependencies. This makes it possible for a single programmer to be familiar with Haskell's compiler, runtime, and libraries in their entirety.
A number of technical and non-technical points come together in making Haskell a great choice for cryptocurrency implementations. This is especially true when it comes to safety and programming ecosystems.
One of the biggest strengths of Haskell for cryptocurrencies is its advertisability:
Haskell has a reputation for excelling in these areas. It is already known and respected for providing strong guarantees and for being made and used by experts. At time of writing coinmarketcap.com listed 1595 existing cryptocurrencies. A new cryptocurrency can set itself apart from that competition by drawing attention to the improved safety and quality derived from being built in Haskell.
Do you like this blog post and need help with industrial Haskell, Rust or DevOps? Contact us.