liveBook · Manning

Mzero

As explained in Fabulous Adventures in Data Structures and Algorithms

MZero is the identity value for the addition-like operation of an additive monad.

MZero is the identity value for an additive monad: adding it changes nothing, and binding any computation to it produces MZero again. It behaves like zero for the monad’s addition-like operation. section 11.4 section 11.5

An additive monad supplies three related behaviors:

  • Adds two monadic values through an operation such as sequence concatenation or probability-distribution merging. section 14.2 section 11.4
  • Provides MZero as the additive identity: the empty sequence leaves concatenation unchanged. section 11.4
  • Uses MZero to express filtering: retain a value with Unit or reject it with MZero. section 11.4

Empty sequences and distributions

For the sequence monad, MZero is the empty sequence: Select, SelectMany, or Where applied to it remains empty. For a discrete probability distribution, MZero is an empty distribution in which every outcome is impossible. Its implementation returns no support and zero weight; constructors and joint-distribution calculations must account for zero total weight. section 14.2 section 11.4

    static M<T> Where<T>(M<T> x, Func<T, bool> predicate) =>
        Bind(x, t => predicate(t) ? Unit(t) : MZero);

Expresses generic filtering by binding each value to Unit when the predicate succeeds and MZero otherwise. section 14.2

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Defines the algebraic role and implements it for sequences and probability distributions, including zero-weight edge cases.
Examples
Uses empty sequences, generic Unit-or-MZero filtering, and empty discrete probability distributions.
What it assumes
Assumes monads, Unit, Bind, and sequence operations; develops additive identity and zero-like Bind behavior.
What you can do after
Apply via Bind and Where by representing rejected values with MZero.
How it connects
Connects additive monads with Bind, Unit, Select, SelectMany, Where, concatenation, and Cartesian products.

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.