Skip to content

Stdlib Effect Instances

Each instance carries a different kind of information along with a computation's result: possible failure (Maybe), alternatives (Alternative_), a shared input (Reader), state passed explicitly (State), accumulated output (Writer), a continuation (Continuation), or nothing (Identity). The monad interface sequences each one (Moggi, 1991; Wadler, 1992). Every instance is registered with its type class abstract base classes.

instances

Stdlib effect instances for the typeclass hierarchy.

Each effect is a dx.Model carrying any effect parameters (e.g. Continuation(answer)) plus a concrete V-relation realisation of every typeclass operation it satisfies. Effects compose with arbitrary user-defined effects through the class-driven lifting machinery in quivers.stochastic.effect_lifts.

The constructions live in quivers.core._factories (coproduct injections / case eliminators, product projections / pairings, parallel pairs, distributivity, terminal maps). Each operation is built as a concrete ObservedMorphism over the appropriate SetObject; composition uses the underlying algebra through the >> operator, and the factory helpers use the same composition interface.

Continuation, State, Reader, and Writer carry an extra typed parameter (the answer/state/environment/monoid). Their operations are realised through the standard V-Rel encodings: products for State/Writer, the function-space B^A (encoded as a finite FinSet) for Reader/Continuation. Function-space cardinality grows as |B|^|A|, so large-cardinality instances should be applied at small carriers (typical in compositional-semantics applications).

Identity didactic-model

Identity(*, name: str = 'Identity')

Bases: Model

The trivial monad: Id(A) = A.

All operations reduce to the identity in V-Rel; the laws hold trivially.

Maybe didactic-model

Maybe(*, name: str = 'Maybe')

Bases: Model

The partiality monad: Maybe(A) = A + 1.

A MonadPlus instance: success injects into the left summand, failure into the right.

Alternative_ didactic-model

Alternative_(*, name: str = 'Alternative')

Bases: Model

The Hamblin alternative monad, the powerset monad on sets.

A set of alternatives over A is a subset of A, and the Kleisli category of the powerset monad is Rel, the category of relations [Hofmann, Seal and Tholen (2014)]. The instance works in that Kleisli category over BOOLEAN: the type-level action over A is again A, a relation A → B stands for the Kleisli arrow A → P(B), pure and join are identities, empty is the empty relation and alt is union. Its morphisms are over BOOLEAN, under which composition is associative; a fuzzy variant over an idempotent semiring is quivers.monadic.monads.FuzzyPowersetMonad.

References

Hofmann, D., Seal, G. J., and Tholen, W. (Eds.) (2014). Monoidal Topology: A Categorical Approach to Order, Metric, and Topology. Cambridge University Press. doi:10.1017/CBO9781107517288

Continuation didactic-model

Continuation(*, answer: SetObject, name: str = 'Continuation')

Bases: Model

The continuation monad with a typed answer.

The carrier is the function-space encoding Cont_ρ(A) = [A → ρ] → ρ realised as a finite SetObject of cardinality |ρ|^(|ρ|^|A|). For small A and ρ this is tractable; for larger carriers, use the algebraic-effect handler of quivers.monadic.algebraic with the ContinuationSignature (see ContSignature in this module).

State didactic-model

State(*, state: SetObject, name: str = 'State')

Bases: Model

The state monad.

Carrier is the function-space encoding σ → A × σ. Each element of the carrier corresponds to a deterministic state-transition function pairing a result with an updated state.

Reader didactic-model

Reader(*, env: SetObject, name: str = 'Reader')

Bases: Model

The reader monad: Reader_ρ(A) = [ρ → A] as a finite function-space.

Writer didactic-model

Writer(*, monoid: SetObject, monoid_op_tensor: tuple[float, ...] | None = None, unit_index: int = 0, name: str = 'Writer')

Bases: Model

The writer monad Writer_M(A) = A × M over a finite monoid M.

The accumulator M is a finite set with an associative operation ⊕ and a two-sided unit, the element with flat index unit_index. pure(a) = (a, e) and join((a, m₁), m₂) = (a, m₁ ⊕ m₂); the monad laws are exactly the monoid laws of (M, ⊕, e).

The operation is given as monoid_op_tensor, a flat 0/1 tensor over M × M × M in row-major (m₁, m₂, m) order whose (m₁, m₂) row marks m₁ ⊕ m₂. Without it the operation is max of the flat indices, with unit index 0, the join semilattice of the chain 0 < 1 < ... < |M| - 1.

RAISES DESCRIPTION
ValueError

When an operation is used whose table is not a function, or that is not associative or does not have unit_index as a two-sided unit.

reshape_unit_to_nothing

reshape_unit_to_nothing(nothing: FinSet) -> Morphism

Trivial bijection 1 → 1 viewed as Unit → nothing.

Source code in src/quivers/monadic/instances.py
def reshape_unit_to_nothing(nothing: FinSet) -> Morphism:
    """Trivial bijection ``1 → 1`` viewed as ``Unit → nothing``."""
    data = torch.tensor([[PRODUCT_FUZZY.unit]])
    return observed(Unit, nothing, data)

Cited sources

Eugenio Moggi. Notions of Computation and Monads. Information and Computation, 93(1):55–92, 1991. URL: https://www.sciencedirect.com/science/article/pii/0890540191900524, doi:10.1016/0890-5401(91)90052-4.

Philip Wadler. The Essence of Functional Programming. In Proceedings of the 19th ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, 1–14. ACM, 1992. URL: https://doi.org/10.1145/143165.143169, doi:10.1145/143165.143169.