Why Cardano chose Haskell—and why you should care
(Written by @ElliotHill of the Cardano Foundation)
If you have been involved in the Cardano ecosystem for a while, you might have learned that our blockchain is built using a functional programming language called ‘Haskell’.
Haskell is the basis for Plutus, Cardano’s smart contract programming language, and it also powers Marlowe, our domain-specific language for financial smart contracts, both of which you can learn more about in our previous article that introduces decentralized applications.
However, Haskell is not one of the best-known programming languages. Nor is it widely picked up by those just starting out on their journey as a developer. In fact, the ‘Popularity of Programming Languages Index’ ranks Haskell as the 27th most-searched-for language on Google as of November 2020, with the likes of Python and Java taking the top spots.
So, with this in mind, many may wonder why Cardano was implemented in Haskell in the first place? The reason is that Haskell, particularly in conjunction with formal methods, is well-suited to write code that is robust and correct. But to understand why this is important for Cardano, we first need to explore what Haskell is and examine its role as a functional programming language.
What is Haskell?
Named after the late American mathematician Haskell Curry, Haskell has its roots in both academia and the study of logic, and also in earlier iterations of functional programming languages, such as Miranda.
These early influences, which prompted more widespread interest in functional programming languages in general, led to Haskell being formally defined in 1990. However, several researchers, such as Professor Phillip Wadler, had already begun working on proto-Haskell components, such as type classes, as early as 1987—before Haskell’s version 1.0 release.
Functional programming languages put an emphasis on using “pure” functions where possible: functions that always give the same result for the same input. While that is a given for mathematicians, computer programmers are used to the idea that “functions” in a computer program also depend on internal state, data on a disk, or the current time. Separating those “side effects” from the main logic wherever possible, Haskell programmers can make it much easier to reason about their code (“equational reasoning”). Not only does that make it easier to write correct code, it is also invaluable for testing, or even proving its correctness.
Evolving over time, Haskell has been through several iterations, such as Haskell 98 and Haskell 2010, with dedicated community members adding extensions, type classes, and building Haskell compilers to support the ecosystem. There now exists a vast body of work and comprehensive libraries and tools for Haskell developers to utilize.
As one of the purest functional programming languages, Haskell has reportedly led many developers to transform the way they approach programming in general and helped improve their overall abilities as programmers.
According to Aaron Contorer, CEO of Haskell programming company FP Complete, this has led to Haskell being voted as the most recommended language to learn even if developers have no specific need for it, which is why we encourage all developers to explore what Haskell has to offer.
Why was Cardano implemented in Haskell?
We have learned that Haskell is a functional programming language, which is well-suited for high-assurance code and programs that require a high degree of formal verification.
As we have discussed above, this allows programmers to have a larger degree of certainty that the code they have implemented is correct. For the future that we envisage for Cardano—namely becoming the next global social and financial operating system—assurance and reliability are essential.
With the arrival of decentralized applications, tokenization, and smart contracts on Cardano, we expect that an entire ecosystem of decentralized financial services will arise. This will bring billions of dollars of value or more on-chain, locked through smart contracts, or managed via decentralized autonomous organizations (DAOs).
However, current smart contract infrastructure has failed to secure this on-chain value, mostly implementing new, custom-designed programming languages, each of which requires its own toolchains, libraries, and educational resources—making it difficult to formally reason about the behavior of smart contracts and applications built atop them.
We know from experience that the resultant vulnerabilities, code failures, or smart contract exploits on other blockchains and smart contract languages have resulted in disastrous consequences and led to considerable financial losses, often in the billions of dollars. This has led some to question whether smart contracts will ever be viable from a risk perspective to institutional users.
This is where Haskell comes into its own. Through Haskell, Cardano’s Plutus and Marlowe smart contracts can be carefully implemented in a precise, formally verified code that offers a high level of assurance from the beginning.
Plutus and Marlowe are offered as a set of libraries for Haskell, leveraging existing Haskell documentation, toolkits, and a highly-professional community to provide a base from which to build secure and enterprise-grade smart contracts.
Both off-chain and on-chain code for Cardano is written in Haskell, which allows Cardano to inherit the rich body of research that already exists within the Haskell ecosystem, instead of reinventing the wheel with a totally new and unproven smart contract language.
To provide support to the wider Haskell community, Cardano’s engineering partners IOHK have recently donated to the Haskell Foundation, an initiative which supports Haskell development and growth. You can learn more about the Haskell Foundation from Simon Peyton Jones keynote address at the Haskell eXchange 2020 virtual event here.
Where can I learn more about Haskell?
Cardano, and by extension the Plutus and Marlowe platforms, are open-source and available for exploration. You can discover more of our codebase via Cardano Docs, or by visiting the Cardano GitHub directly.
If you are looking for the best guide to Haskell and you are unsure where to start, we recommend you check out the book or website ‘Learn You a Haskell for Great Good’ by Miran Lipovača. Learning Haskell is made easy with this illustrated guide, one of the most engaging ways to learn this fascinating programming language.
Other resources for Haskell developers include the Glasgow Haskell Compiler (GHC), led by Simon Peyton Jones and Simon Marlow, an open-source native code compiler for Haskell.
If you would like to join the vibrant Haskell for developers community, we would also recommend checking out the Haskell Reddit. If you are interested in developing on Cardano, why not join our developer portal waiting list? Here, you will get access to the latest developer news from the Cardano ecosystem.
Have fun and keep building!