liveBook · Manning

Type-lifting function

As explained in Fabulous Adventures in Data Structures and Algorithms

Unit is the conventional monadic operation that wraps a value of type T into the corresponding monadic type M<T>.

A type-lifting function wraps a value of type T in a corresponding monadic type M<T>. Conventionally called Unit, it creates the simplest workflow that carries the monad’s added capability without changing the underlying value. section 11.1

static Nullable<T> NullableUnit<T> where T : struct (T t) => 
    new Nullable<T>(t);
static Lazy<T> LazyUnit<T>(T t) => new Lazy<T>(t);
static Task<T> TaskUnit<T>(T t) => Task.FromResult(t);
static IEnumerable<T> SeqUnit<T>(T t) => [ t ];

These C# definitions lift one value into nullable, lazy, asynchronous, or sequence workflows. section 11.1

Typical lifts preserve the value while choosing a workflow representation:

  • NullableUnit turns a struct value into Nullable<T>, adding the possibility of no value. section 11.1
  • LazyUnit creates Lazy<T>, deferring computation represented by the wrapped value. section 11.1
  • TaskUnit creates an already-completed Task<T>, placing a value into an asynchronous workflow. section 11.1
  • SeqUnit creates a one-element IEnumerable<T>, representing the value as a sequence. section 11.1

Unit is necessary but not sufficient for useful composition. Bind combines an M<T> with a function from T to M<R>; Unit and Bind together support workflows that retain ordinary function meanings while adding nullability, laziness, asynchrony, or sequencing. Map can then be defined by binding a function and lifting its result. section 11.3

A probability wrapper

A discrete distribution can provide a Unit wrapper for a single value and Bind for combining it with likelihood-producing functions. This supports principled manipulation of finite categorical distributions rather than ad hoc use of Random. section 14.4

How Fabulous Adventures in Data Structures and Algorithms covers it

How deep it goes
Defines Unit, gives C# implementations, and connects it to Bind, Map, and Join without developing monad laws.
Examples
Uses Nullable<T>, Lazy<T>, Task<T>, IEnumerable<T>, and discrete distributions as concrete lifting targets.
What it assumes
Assumes familiarity with generic types, functions, and C# wrappers while defining monadic lifting from the underlying value type.
What you can do after
Builds Unit functions that place values into nullable, lazy, asynchronous, sequence, or probability workflows.
How it connects
Connects Unit to monads, Bind, Map, Join, nullable types, laziness, asynchronous computation, sequences, and probability distributions.

Also explained in

Compare how 4 books explain type-lifting function →

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.