liveBook · Manning

Additive monad

As explained in Fabulous Adventures in Data Structures and Algorithms

An additive monad is a monad equipped with an operation for combining computations or values according to an additive structure.

An additive monad is a monad with an operation for combining monadic values and an identity value, conventionally called MZero. Binding a computation to MZero produces MZero, so additive structure can represent alternatives, impossibility, or absence. section 11.4 section 14.2

The pattern requires three related capabilities:

Sequences and probability distributions

For sequences, concatenation acts as addition and the empty sequence is MZero. A predicate can therefore retain an item by returning a singleton sequence or discard it by returning an empty sequence; the direct implementation is clearer and more efficient, but the monadic version exposes the general pattern. Discrete probability distributions provide a less obvious example: distributions can be merged, while an empty distribution represents an outcome that is impossible. Supporting that value requires constructors and joint-distribution logic to handle zero total weight. section 11.4 section 14.2

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Defines addition, MZero, and the Bind law, then works through sequence filtering and probability-distribution edge cases.
Examples
Uses empty and singleton sequences, sequence filtering, merged marble distributions, and an empty discrete distribution.
What it assumes
Assumes monadic Unit and Bind, while treating additive laws and sequence operators as the immediate foundation.
What you can do after
Build it from scratch by implementing MZero and additive Bind behavior, then derive a generic Where operation.
How it connects
Connects monads, Bind, Unit, Where, sequence concatenation, Cartesian products, and probability distributions.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fabulous Adventures in Data Structures and Algorithms. The section links point to the parts of the book it draws on.