Pure functions are pretty similar to mathematical functions. They are the reason that Haskell is called a pure functional programming language.
I compare in the table pure and impure functions.
Pure functions have a crucial drawback. They can not communicate with the outside world. Because functions for input and output, functions for building a state, or functions for creating random numbers can not be pure. According to Simon Peyton Jones, the only effect that a pure function can have is to warm up the room. Haskell solves this dead-end by embedding impure, imperative subsystems in the pure functional language. These imperative subsystems are called monads. I will write more about monads in a few seconds.
What is the story of purity in C++? This story is based – similar to Immutable Data – on the programmer’s discipline. I will present a function, a meta-function, and a constexpr function in the following program. All three are pure functions.
Although the three functions give the same result, they are entirely different. powFunc (lines 5 – 8) is a classical function. It will be executed at the program’s run time and can get non-constant arguments. On the contrary, PowMeta (lines 10 – 18) is a meta-function executed at the program’s compile time. Therefore, PowMeta needs constant expressions as arguments. The constexpr function powConst (lines 20 – 24) can run at compile and run times. To be executed at the compile time, powConst needs constant expressions as arguments according to the meta-function PowMeta.
And now to something completely different. I will introduce this in the next section, monads. I will refer to this first definition of monads in later posts, and I will show you examples of monads in future C++: std::optional in C++17, the ranges library from Eric Nieber in C++20, and the extended futures in C++20. But now, I’m talking about the future concept in C++. Or, to say it in the words of Bartosz Milewski: I See a Monad in Your Future. I strongly encourage you the read the post or watch his video.
Haskell, as a pure functional language, has only pure functions. The key to these pure functions is that they will always give the same result when given the same arguments. Thanks to this property, a Haskell function can not have side effects. This property is also called referencial transparency. Therefore, Haskell has a conceptual issue. The world is full of calculations that have side effects. These are calculations that can fail, return an unknown number of results, or are dependent on the environment. To solve this conceptual issue, Haskell uses monads and embeds them in the pure functional langue.
The classical monads encapsulate one side effect:
- I/O monad: Calculations that deal with input and output.
- Maybe monad: Calculations that maybe return a result.
- Error monad: Calculations that can fail.
- List monad: Calculations that can have an arbitrary number of results.
- State monad: Calculations that build a state.
- Reader monad: Calculations that read from the environment.
The concept of the monad is from the category theory. Category theory is a part of mathematics that deals with objects and mapping between objects. Monads are abstract data types (type classes), transforming simple types into enriched ones. Values of these enriched types are called monadic values. Once in a monad, a value can only be transformed by a function composition in another monadic value.
This composition respects the particular structure of a monad. Therefore, the error monad will interrupt its calculation if an error occurs or the state monad builds its state.
To make this happen, a monad consists of three components:
- Type constructor: The type constructor defines how the simple data type becomes a monadic data type.
- Identity function: Introduces the simple value into the monad.
- bind operator Defines how a function is applied to a monadic value to get a new one.
- Rules for the functions:
- The identity function has to be the left and the correct identity element.
- The composition of functions has to be associative.
For the error monad to become an instance of the type class Monad, the error monad has to support the identity function and the bind operator. Both functions define how the error monad deals with an error in the calculation. If you use an error monad, the error is handled in the background.
A monad consists of two control flows. The explicit control for calculating the result and the implicit control flow for dealing with the specific side effect.
A few months ago, after I published this post in German, a reader said: Hey, the definition of a monad is quite simple. “A monad is just a monoid in the category of endofunctors.” I hope you get it.
Pure functional languages have no mutable data. Therefore, they use recursion instead of loops. So you know what the next post will be about.
Thanks a lot to my Patreon Supporters: Matt Braun, Roman Postanciuc, Tobias Zindl, G Prvulovic, Reinhold Dröge, Abernitzke, Frank Grimm, Sakib, Broeserl, António Pina, Sergey Agafyin, Андрей Бурмистров, Jake, GS, Lawton Shoemake, Jozo Leko, John Breland, Venkat Nandam, Jose Francisco, Douglas Tinkham, Kuchlong Kuchlong, Robert Blanch, Truels Wissneth, Kris Kafka, Mario Luoni, Friedrich Huber, lennonli, Pramod Tikare Muralidhara, Peter Ware, Daniel Hufschläger, Alessandro Pezzato, Bob Perry, Satish Vangipuram, Andi Ireland, Richard Ohnemus, Michael Dunsky, Leo Goodstadt, John Wiederhirn, Yacob Cohen-Arazi, Florian Tischler, Robin Furness, Michael Young, Holger Detering, Bernd Mühlhaus, Matthieu Bolt, Stephen Kelley, Kyle Dean, Tusar Palauri, Dmitry Farberov, Juan Dent, George Liao, Daniel Ceperley, Jon T Hess, Stephen Totten, Wolfgang Fütterer, Matthias Grün, Phillip Diekmann, Ben Atakora, Ann Shatoff, Rob North, Bhavith C Achar, Marco Parri Empoli, moon, and Philipp Lenk.
Thanks, in particular, to Jon Hess, Lakshman, Christian Wittenhorst, Sherhy Pyton, Dendi Suhubdy, Sudhakar Belagurusamy, Richard Sargeant, Rusty Fleming, John Nebel, Mipko, Alicja Kaminska, Slavko Radman, and David Poole.
|My special thanks to Embarcadero|
|My special thanks to PVS-Studio|
|My special thanks to Tipi.build|
|My special thanks to Take Up Code|
|My special thanks to SHAVEDYAKS|
I’m happy to give online seminars or face-to-face seminars worldwide. Please call me if you have any questions.
- Embedded Programmierung mit modernem C++ 12.12.2023 – 14.12.2023 (Präsenzschulung, Termingarantie)
Standard Seminars (English/German)
Here is a compilation of my standard seminars. These seminars are only meant to give you a first orientation.
- C++ – The Core Language
- C++ – The Standard Library
- C++ – Compact
- C++11 and C++14
- Concurrency with Modern C++
- Design Pattern and Architectural Pattern with C++
- Embedded Programming with Modern C++
- Generic Programming (Templates) with C++
- Clean Code with Modern C++
- Phone: +49 7472 917441
- Mobil:: +49 176 5506 5086
- Mail: schulung@ModernesCpp.de
- German Seminar Page: www.ModernesCpp.de
- Mentoring Page: www.ModernesCpp.org
Modernes C++ Mentoring,