Skip to content

Effect Schema Lifting

These functions lift a base rule schema to values that carry an effect, such as a set of alternatives or a continuation (schema lifting). Which lifted rules exist depends on the type classes the effect inhabits (functor, applicative, monad, alternative) (McBride and Paterson, 2008; Wadler, 1992). class_directed_lifts(base_schema, effect) and lift_rule_set(base_schemas, effects) emit the lifted SchemaDecls, and each generated rule depends on the laws and scope conventions of its effect instance.

effect_lifts

Fixed schema shapes for semantic effect prefixes in chart parsers.

class_directed_lifts emits pure and forward apply for an Applicative, bind for a Monad, and alt for an Alternative. The base schema contributes its name to each generated name; these shapes are fixed rather than derived from the base schema's domain and codomain. A bare Functor and the arrow interfaces add no schemata.

Generated schemata use the chart's ordinary pattern matching. Distributive laws can additionally generate swap schemata through make_swap_schema. These semantic type prefixes are distinct from QIEC effect instances and lexical handlers. Chart execution does not eliminate a prefix by invoking a QIEC handler.

class_directed_lifts

class_directed_lifts(base_schema: SchemaDecl, effect: object) -> tuple[SchemaDecl, ...]

Generate the class-driven lifts for base_schema under effect.

Membership in Applicative emits pure_T and forward apply_T; Monad adds bind_T; Alternative adds alt_T. MonadPlus receives both sets through its parent classes. A bare Functor and arrow-only instances emit nothing.

These are fixed schema shapes. The base schema provides the suffix of their names, not a general transformation of its premises.

PARAMETER DESCRIPTION
base_schema

The base schema being lifted.

TYPE: SchemaDecl

effect

An effect instance (any class registered against the relevant typeclass ABCs in quivers.monadic.typeclasses).

TYPE: object

RETURNS DESCRIPTION
tuple of SchemaDecl

Zero or more schemata in the order pure, apply, bind, alt, retaining only those supported by the effect's classes.

Source code in src/quivers/stochastic/effect_lifts.py
def class_directed_lifts(
    base_schema: SchemaDecl, effect: object
) -> tuple[SchemaDecl, ...]:
    """Generate the class-driven lifts for ``base_schema`` under ``effect``.

    Membership in `Applicative` emits ``pure_T`` and forward ``apply_T``;
    `Monad` adds ``bind_T``; `Alternative` adds ``alt_T``. `MonadPlus`
    receives both sets through its parent classes. A bare `Functor` and
    arrow-only instances emit nothing.

    These are fixed schema shapes. The base schema provides the suffix
    of their names, not a general transformation of its premises.

    Parameters
    ----------
    base_schema : SchemaDecl
        The base schema being lifted.
    effect : object
        An effect instance (any class registered against the relevant
        typeclass ABCs in [`quivers.monadic.typeclasses`][quivers.monadic.typeclasses]).

    Returns
    -------
    tuple of SchemaDecl
        Zero or more schemata in the order ``pure``, ``apply``, ``bind``,
        ``alt``, retaining only those supported by the effect's classes.
    """
    lifts: list[SchemaDecl] = []

    if isinstance(effect, Applicative) and implements(effect, Applicative):
        lifts.append(_make_pure_schema(effect, base_schema))
        lifts.append(_make_apply_schema(effect, base_schema))

    if isinstance(effect, Monad) and implements(effect, Monad):
        lifts.append(_make_bind_schema(effect, base_schema))

    if isinstance(effect, Alternative) and implements(effect, Alternative):
        lifts.append(_make_alt_schema(effect, base_schema))

    return tuple(lifts)

make_swap_schema

make_swap_schema(distributive_law: object, base_schema: SchemaDecl | None = None) -> SchemaDecl

Generate a swap_TU schema from a registered distributive law.

Given a DistributiveLaw λ : S ∘ T ⇒ T ∘ S, the chart parser uses swap_TU to exchange sibling effect orderings at a cell whose effect stack ends in T·U:

swap_TU[X : Cat] : T(U(X)) -> U(T(X))

The schema is consumed by the chart's commutation firing rule of Effects §4.4.

PARAMETER DESCRIPTION
distributive_law

The law whose outer and inner monads the schema exchanges.

TYPE: DistributiveLaw

base_schema

The schema whose name the swap schema's name extends; the name swap is used when omitted.

TYPE: SchemaDecl or None DEFAULT: None

RETURNS DESCRIPTION
SchemaDecl

The swap_TU schema T(U(X)) -> U(T(X)).

RAISES DESCRIPTION
TypeError

If distributive_law is not a DistributiveLaw.

Source code in src/quivers/stochastic/effect_lifts.py
def make_swap_schema(
    distributive_law: object, base_schema: SchemaDecl | None = None
) -> SchemaDecl:
    """Generate a ``swap_TU`` schema from a registered distributive law.

    Given a `DistributiveLaw` ``λ : S ∘ T ⇒ T ∘ S``, the chart
    parser uses ``swap_TU`` to exchange sibling effect orderings at a
    cell whose effect stack ends in ``T·U``:

        swap_TU[X : Cat] : T(U(X)) -> U(T(X))

    The schema is consumed by the chart's *commutation firing* rule
    of [Effects §4.4](../../semantics/effects.md#4-joint-type-and-effect-dispatch).

    Parameters
    ----------
    distributive_law : DistributiveLaw
        The law whose outer and inner monads the schema exchanges.
    base_schema : SchemaDecl or None
        The schema whose name the swap schema's name extends; the
        name ``swap`` is used when omitted.

    Returns
    -------
    SchemaDecl
        The ``swap_TU`` schema ``T(U(X)) -> U(T(X))``.

    Raises
    ------
    TypeError
        If ``distributive_law`` is not a `DistributiveLaw`.
    """
    if not isinstance(distributive_law, DistributiveLaw):
        raise TypeError(
            "make_swap_schema requires a DistributiveLaw instance; "
            f"got {type(distributive_law).__name__}"
        )
    # The naming convention for the swap schema reuses the monad
    # class names; the schema's directionality (S∘T → T∘S) matches
    # the underlying λ : S(T(-)) → T(S(-)).
    outer = type(distributive_law.outer_monad).__name__
    inner = type(distributive_law.inner_monad).__name__
    base_name = base_schema.name if base_schema is not None else "swap"
    return SchemaDecl(
        name=f"swap_{outer}_{inner}_{base_name}",
        parameters=(
            SchemaParameter(
                names=("X",),
                type_expr=TypeName(name="Cat"),
            ),
        ),
        domain=_wrap_with_effect(_wrap_with_effect(TypeName(name="X"), inner), outer),
        codomain=_wrap_with_effect(_wrap_with_effect(TypeName(name="X"), outer), inner),
    )

swap_rule_set

swap_rule_set(distributive_laws: tuple[object, ...]) -> tuple[SchemaDecl, ...]

Generate one swap_TU schema per registered distributive law.

The resulting tuple is appended to the chart parser's rule set alongside the class-driven lifts; the chart's commutation-firing dispatch picks each swap schema up by name.

PARAMETER DESCRIPTION
distributive_laws

The registered distributive laws.

TYPE: tuple of DistributiveLaw

RETURNS DESCRIPTION
tuple of SchemaDecl

One swap schema per law, in the order given.

RAISES DESCRIPTION
TypeError

If an entry is not a DistributiveLaw.

Source code in src/quivers/stochastic/effect_lifts.py
def swap_rule_set(
    distributive_laws: tuple[object, ...],
) -> tuple[SchemaDecl, ...]:
    """Generate one ``swap_TU`` schema per registered distributive law.

    The resulting tuple is appended to the chart parser's rule set
    alongside the class-driven lifts; the chart's commutation-firing
    dispatch picks each swap schema up by name.

    Parameters
    ----------
    distributive_laws : tuple of DistributiveLaw
        The registered distributive laws.

    Returns
    -------
    tuple of SchemaDecl
        One swap schema per law, in the order given.

    Raises
    ------
    TypeError
        If an entry is not a `DistributiveLaw`.
    """
    return tuple(make_swap_schema(law) for law in distributive_laws)

lift_rule_set

lift_rule_set(base_schemas: tuple[SchemaDecl, ...], effects: tuple[object, ...]) -> tuple[SchemaDecl, ...]

Apply class_directed_lifts over a rule-set and effect-stack.

Returns the union of base schemas and all lifts produced for each (base_schema, effect) pair. The chart parser consumes the resulting tuple directly.

PARAMETER DESCRIPTION
base_schemas

The base rule set.

TYPE: tuple of SchemaDecl

effects

The effect instances to lift each base schema under.

TYPE: tuple

RETURNS DESCRIPTION
tuple of SchemaDecl

The base schemas followed by every lift, grouped by base schema and then by effect.

Source code in src/quivers/stochastic/effect_lifts.py
def lift_rule_set(
    base_schemas: tuple[SchemaDecl, ...], effects: tuple[object, ...]
) -> tuple[SchemaDecl, ...]:
    """Apply `class_directed_lifts` over a rule-set and effect-stack.

    Returns the union of base schemas and all lifts produced for each
    (base_schema, effect) pair. The chart parser consumes the
    resulting tuple directly.

    Parameters
    ----------
    base_schemas : tuple of SchemaDecl
        The base rule set.
    effects : tuple
        The effect instances to lift each base schema under.

    Returns
    -------
    tuple of SchemaDecl
        The base schemas followed by every lift, grouped by base
        schema and then by effect.
    """
    out: list[SchemaDecl] = list(base_schemas)
    for base in base_schemas:
        for eff in effects:
            out.extend(class_directed_lifts(base, eff))
    return tuple(out)

Cited sources

Conor McBride and Ross Paterson. Applicative programming with effects. Journal of Functional Programming, 18(1):1–13, 2008. URL: https://www.cambridge.org/core/journals/journal-of-functional-programming/article/applicative-programming-with-effects/C80616ACD5687ABDC86D2B341E83D298, doi:10.1017/S0956796807006326.

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.