In functional programming, at the heart of input/output, failure, state, logic, and much more, lies a powerful abstraction called monad. This book provides a journey from the very first concepts, to the myriad of monads available to programmers, down to the categorical foundations.
Monads are key to unlocking many powerful patterns in functional programming. Alas, many developers struggle at first with the concept and the applications of monads. This books provides a full dive into the subject, including thorough explanations of adjacent concepts such as functors, monad transformers, final tagless, or extensible effects.
Author(s): Alejandro Serrano Mena
Publisher: Independently published
Year: 2021
Language: English
Pages: 281
Introduction
Type Classes
Higher-kinded Abstraction
Haskell's Newtype
Language Extensions in Haskell
I What is a Monad?
Discovering Monads
State Contexts
Magical Multiplying Boxes
Both, Maybe? I Don't Think That's an Option
Two for the Price of One
Functors
Better Notation
Block Notation
Pattern Matching and Fail
Lifting Pure Functions
Lift2, Lift3, ..., Ap
Applicatives
Applicative Style
Definition Using Tuples
Utilities for Monadic Code
Lifted Combinators
Traversables
Interlude: Monad Laws
Laws for Functions
Monoids
Monad Laws
II More Monads
Pure Reader-Writer-State Monads
The State Monad
The Reader Monad
The Writer Monad
All at Once: the RWS Monad
Bi-, Contra-, and Profunctors
Failure and Logic
Failure with Fallback
Logic Programming as a Monad
Catching Errors
Monads for Mutability
Mutable References
Interfacing with the Real World
Resource Management and Continuations
The Bracket Idiom
Nicer Code with Continuations
Early Release
III Combining Monads
Functor Composition
Combining Monads by Hand
Many Concepts Go Well Together
But Monads Do Not
A Solution: Monad Transformers
Monadic Stacks
Classes of Monads, MTL-style
Parsing for Free!
Generic Lifting and Unlifting
MonadTrans and Lift
Base Monads: MonadIO and MonadBase
Lifting Functions with Callbacks
More on Manipulating Stacks
IV Rolling Your Own Monads
Defining Custom Monads
Introduction
Final Style
Initial Style
Operational Style and Freer Monads
Transforming and Inspecting Computations
Composing Custom Monads
Final Style
Initial and Operational Style
Extensible Effects
Performance of Free Monads
Left-nested Concatenation
Left-nested Binds
V Diving into Theory
A Roadmap
Just a Monoid!
Quick Summary
Categories, Functors, Natural Transformations
Monoids in Monoidal Categories
The Category of Endofunctors
Adjunctions
Adjoint Functors
Monads from Adjunctions
The Kleisli Category
Free Monads
Bibliography