Module 14: Cardano Programming Languages: Haskell and Plutus
Module 14 will discuss the two programming languages on the Cardano blockchain - Haskell and Plutus. We cover why these languages were chosen for Cardano and their advantages.
Introduction
The last module looked at the history of Cardano, this module will examine Cardano’s programming languages.
If you are familiar with the Cardano ecosystem, you may know that the blockchain is built on the functional programming language "Haskell."
"Plutus," Cardano's smart contract programming language, is built on Haskell, as is Marlowe, Cardano's domain-specific language for financial smart contracts.
In this module, we will be examining both Haskell and Plutus and why they are essential to Cardano.
What is Haskell?
Haskell's official tagline describes it as "an advanced, purely functional programming language." Essentially, Haskell is a programming model based on lambda calculus (a notation for describing mathematical functions and programs) that gives a theoretical framework for describing and evaluating functions. This declarative programming style replaces statements with expressions and focuses on "what to solve" instead of "how to solve."
Haskell is known as a "purely functional" language because every function in Haskell is a function in the mathematical sense and is highly secure. The language was developed by mathematician Haskell Brooks Curry, who spent his career working in combinatorial logic (a notation designed to eliminate the need for variables in mathematical logic), which formed the basis for functional programming.
In Haskell, functions execute low-level tasks and specify what the program should accomplish. It is a statically typed language, which means the code is evaluated during the compilation process. Because the compiler evaluates the code before it runs, type checking ensures that it is concise, clear, and correct. Haskell also ensures that the code is well-structured.
Haskell plays a crucial role in producing high-quality software. Correctness, performance, and maintainability are the three most significant factors for evaluating software quality.
Correctness
Writing code that adheres to specifications requires strong static typing, purity, and immutable data. Haskell-based software is safe, dependable, and bug-free.
Performance
GHC (The Glasgow Haskell Compiler) provides efficient native executables. It has a garbage collector and supports green threads in its runtime system. Haskell is suitable for multi-threaded systems with high demand, such as web backends.
Maintainability
The use of the type system to model the business domain is encouraged and made explicit in Haskell. This makes refactoring code and adapting to changing requirements much more straightforward.
What are the Benefits of Using Haskell?
This language has many benefits, such as:
Memory Safety
Manual memory management is a form of resource management that is used to manage the memory of a computer. Memory management provides mechanisms to dynamically allocate sections of memory to programs when required.
Manual memory management In C and C++, commonly used programming languages, can cause buffer overflows, memory leaks, and other memory-related errors. Memory safety is a common feature of modern languages, including Java, Python, Go, JavaScript, Rust, and others, and it is necessary for writing secure software. However, Haskell software is unlikely to experience such problems because Haskell data is immutable by default.
Native Code
Haskell is compiled ahead of time, directly into native code (programming code that is configured to run on a specific processor), unlike Python, Ruby, JavaScript, Lua, and other interpreted languages. The compiler excels in optimizing and producing efficient executables. As a result, Haskell is a suitable choice for high-performance applications that require high-throughput data processing, such as systems programming, data structures, databases, development, text processing, and more.
Static Types
Haskell, like Java, but unlike JavaScript, features a type checker that validates code as it is written. This means many issues are discovered early in the development cycle, before the product reaches users or the quality assurance department. In addition, the developer can study the code to gain a better understanding of the business domain.
Rich Types
In contrast to Java or Go, where static types (such as Java, C, or C++) are frequently regarded as a hindrance, Haskell's type system is so powerful that it becomes a valuable tool. In addition, Haskell offers a wide range of tools for developing statically typed programs.
Laziness
Haskell was built from the ground up to be a "lazy" language, and its most essential feature is lazy evaluation. Lazy evaluation allows the program to postpone computations until they are required. One of the benefits of this is easier refactoring - code refactoring is defined as the process of restructuring computer code without changing or adding to its functionality.
Purity
Pure functions and immutable data are essential to Haskell's design.
A function is said to be pure if it corresponds to a function in the mathematical sense: it assigns an output value to every possible input value and does nothing else.
Expressions in Haskell are immutable. This means that they cannot change after they have been evaluated. Immutability makes refactoring straightforward and the code much easier to understand.
These properties have been repeatedly demonstrated to be critical for creating correct software. The Haskell programming language provides all of the tools a programmer needs to prevent or reduce these sources of complexity.
Other vital benefits include shorter development time, tight control of side effects, scalability, and more. All this combined makes Haskell an attractive option for a variety of industries. In particular, companies that work with large amounts of data have shown a lot of interest in this language.
Why is Cardano using Haskell?
Wherever possible, functional programming languages emphasize the usage of "pure" functions, which always return the same result for the same input. Haskell, as one of the purest functional programming languages, is said to have influenced many developers' approaches to programming in general and improved their overall programming skills.
As outlined above, a functional programming language is well suited for code and programs that require a high degree of formal verification.
Because Haskell is so secure, programmers can be more confident that the code they develop is correct. Because of this, the Haskell programming language was an excellent choice for programmers to create Cardano. Smart contracts written in Haskell, such as Cardano's Plutus and Marlowe, can be carefully crafted in exact, formally tested code that guarantees a high level of security from the start.
Plutus and Marlowe combine Haskell libraries that leverage existing Haskell documentation, toolkits, and a highly professional community to create a secure and enterprise-ready foundation for smart contracts.
Cardano's code is written in Haskell at both the off-chain and on-chain levels. Thus, rather than reinventing the wheel with an entirely new and unproven smart contract language, Cardano can benefit from the extensive research in the Haskell ecosystem.
What is Plutus?
The Plutus platform is Cardano's smart contract platform. Plutus contracts are composed of on-chain (code that runs on the blockchain) and off-chain (code that runs on the user's computer) components. Cardano's Alonzo upgrade established the infrastructure and added the tools for functional smart contract development using Plutus.
Plutus is a secure, full-featured programming environment based on Haskell, the top functional programming language, that draws on recent language research. Plutus' smart contracts are Haskell programs, and both the on-chain and off-chain code is written in Haskell. GHC compiles the off-chain code, while the Plutus compiler compiles the on-chain code.
Programmers can program directly in Plutus Playground, a lightweight, web-based environment for Plutus exploratory programming, to construct a smart contract utilizing the Plutus platform. Thus, it is possible to write and simulate contracts using Plutus without setting up and managing an entire development environment.
Benefits of Plutus
Below is an an overview of the biggest advantages of Plutus:
Plutus is famed for it's high security and reliability. It also provides superior code review functionality for smart contracts. This ultimately reduces maintenance time and cost in the future. For smart contracts, correctness is absolutely crucial.
Plutus allows a more secure and convenient integrated approach to smart contracts and distributed application development than previous options. The same language is used for both on-chain and off-chain programming. They work with a single code base, which Plutus divides into on-chain and off-chain code, and then packages for deployment.
Understanding Plutus
To better understand Plutus and learn about the environment, it is necessary to first learn about the three concepts below:
The Extended UTXO model (EUTXO)
Plutus Core ‒ the 'on-chain' part of Plutus
The Plutus Application Framework (PAF). This is Plutus's 'off-chain' component that enables interaction with smart contracts.
Extended UTXO Model
The UTXO model is used by Cardano, just like Bitcoin. A transaction in the UTXO design contains inputs and outputs, with the inputs being unspent outputs from prior transactions.
When an output is used as an input in a transaction, it is consumed and cannot be reused. Instead, an address (a public key or public key hash) and a value are used to specify the output (consisting of an ADA amount and optional additional native token amounts). An output's address defines which transactions are permitted to "unlock" the output and utilize it as input.
The EUTXO model expands on the UTXO model by allowing output addresses to contain complicated logic to determine which transactions are allowed to unlock them and adding custom data to all outputs.
Plutus Core
The terms script and data must be specified correctly in order to implement the EUTXO model. Therefore, scripts necessitate a distinct and well-defined scripting language.
Plutus Core is helpful in this situation. Cardano uses the Plutus Core scripting language. Plutus Core scripts can be written in a primary functional language comparable to Haskell, and a substantial subset of Haskell can be used. You do not write Plutus Core as a contract author; the Haskell compiler plugin generates all Plutus Core programs.
The Plutus Application Framework (PAF)
The Plutus Application Framework (PAF) makes it simple to access services that are utilized by Plutus apps. The Plutus application backend, which offers runtime support for accessing the blockchain as well as additional concerns like persistence, logging, and monitoring, can be used to execute applications built with the framework's libraries. In addition, PAF-based applications include an HTTP and WebSocket interface that may be used to communicate with the program from a web browser.
What Can I Do with Plutus?
Plutus Core may be used to define minting guidelines. The Plutus Core policy script will run in the background during minting and burning, approving or disapproving the action.
What is Plutus Playground?
As we know, Plutus Core allows developers to create highly secure Cardano applications. On the other hand, Plutus Playground is a platform for developing and testing smart contracts prior to their deployment on the Cardano blockchain.
The Plutus Playground provides a web-based simulator for building and executing smart contracts and allows access to pre-written smart contracts. The project has grown tremendously and is a very popular resource for developers.
Plutus Playground is aimed at people who want to work with Cardano to create decentralized applications (DApps) and smart contracts. For example, Plutus can develop DApps for supply chains, tracking and tracing, medical records, identity reconciliation, property registration, peer-to-peer payments, and financial systems. As a result, it will appeal to a variety of audiences.
Conclusion
Plutus contracts are made up of code that runs on the blockchain (on-chain) and code that runs on the user's computer (off-chain).
Plutus is a secure, full-featured programming environment based on Haskell, a top functional programming language. Plutus' smart contracts are Haskell programs, and both the on-chain and off-chain code is written in Haskell.
Plutus contracts have many benefits including superior code functionality, a more fine-tuned deployment process compared to previous options, and support fungible and non-fungible tokens
If you are a developer and want to learn more about Plutus, consider joining a future pioneer cohort.
That’s it for this module, see you in Module 15!