Algae v1.1.0 Algae.Free View Source

A “free” structure that converts functors into monads by embedding them in a special structure with all of the monadic heavy lifting done for you.

Similar to trees and lists, but with the ability to add a struct “tag”, at each level. Often used for DSLs, interpreters, or building structured data.

For a simple introduction to the “free monad + interpreter” pattern, we recommend Why free monads matter.

Anatomy

Pure

Pure simply holds a plain value.

%Free.Pure{pure: 42}

Roll

Roll resursively containment of more Free structures embedded in a another ADT. For example, with Id:

%Free.Roll{
  roll: %Id{
    id: %Pure{
      pure: 42
    }
  }
}

Link to this section Summary

Functions

Lift a plain functor up into a free monad

Add another layer to a free structure

Create an Algae.Free.Pure wrapping a single, simple value

Wrap a functor in a free structure

Link to this section Types

Link to this section Functions

Lift a plain functor up into a free monad.

Examples

iex> free(%Algae.Id{id: 42})
%Algae.Free.Roll{
  roll: %Algae.Id{
    id: %Algae.Free.Pure{
      pure: 42
    }
  }
}
Link to this function layer(free, mutual) View Source
layer(t, any) :: t

Add another layer to a free structure

Examples

iex> 13
...> |> new()
...> |> layer(%Algae.Id{})
%Algae.Free.Roll{
  roll: %Algae.Id{
    id: %Algae.Free.Pure{
      pure: 13
    }
  }
}

Create an Algae.Free.Pure wrapping a single, simple value

Examples

iex> new(42)
%Algae.Free.Pure{pure: 42}

Wrap a functor in a free structure.

Examples

iex> wrap(%Algae.Id{id: 42})
%Algae.Free.Roll{
  roll: %Algae.Id{
    id: 42
  }
}