Stdlib Effect Instances

Identity, Maybe, Alternative_, Continuation, State, Reader, Writer, List, registered against the appropriate typeclass ABCs.

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 compose seamlessly.

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

Bases: Model

The trivial monad: Id(A) = A.

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

Maybe

Bases: Model

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

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

Alternative_

Bases: Model

The Hamblin alternative monad on the V-algebra.

The type-level action over A is again A: alternatives are encoded as V-weighted multisets in the V-relation tensor, not in the carrier set. Pure injects a value as a singleton (the identity relation); join is the V-relation composition with the noisy-OR aggregation supplied by the underlying algebra.

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

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

Bases: Model

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

Writer

Bases: Model

The writer monad over a chosen accumulator.

The accumulator type M is a SetObject equipped with a user-supplied monoid_op of type M × M → M and a unit_index (the flat index of the monoid unit in M). For the default values, the implementation provides the free commutative monoid structure on M realised as element-wise pairing; bind concatenates the accumulator side via monoid_op.

The monoid operation defaults to the discrete-projection-to-first (the "max" of two elements under the standard order on the flat indices) — a valid monoid structure when one is not supplied; users wanting a different monoid pass an ObservedMorphism of the right shape.

List

Bases: Model

The list monad over a bounded length.

List(A) = ∐_{k=0}^{max_length} A^k realised as a FreeMonoid. Operations: pure builds a singleton; join concatenates two lists; alt is concatenation; the monad-plus structure pairs the empty list as empty.

fmap_obj

fmap_obj(A: SetObject) -> SetObject

List(A) = A*_{≤max_length}.

When A is not a FinSet (e.g. when computing List(List(B))), re-encode it as a flat FinSet of equivalent cardinality. The bijection is given by the row-major flat enumeration of A's underlying state space, so all subsequent morphism constructions on List(A) agree on element identity.

Source code in src/quivers/monadic/instances.py
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
def fmap_obj(self, A: SetObject) -> SetObject:
    """``List(A) = A*_{≤max_length}``.

    When ``A`` is not a `FinSet` (e.g. when computing
    ``List(List(B))``), re-encode it as a flat FinSet of equivalent
    cardinality. The bijection is given by the row-major flat
    enumeration of A's underlying state space, so all subsequent
    morphism constructions on ``List(A)`` agree on element identity.
    """
    from quivers.core.objects import FreeMonoid

    if isinstance(A, FinSet):
        return FreeMonoid(generators=A, max_length=self.max_length)
    encoded = FinSet(name=f"_flat_{A!s}", cardinality=A.size)
    return FreeMonoid(generators=encoded, max_length=self.max_length)

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
326
327
328
329
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)