init::transformers

init/src/transformers.mo

typeStateT#
pub type StateT (S : Type) (M : Type -> Type) (A : Type) {
    mk (run : S -> M (Pair S A))
}

State transformer: StateT S M A = S -> M (Pair S A) threads a state S through the underlying monad M.

A single-constructor type rather than a struct, deliberately: a parameterized struct's constructor does not generalize its type parameters on the Rust host (construction at StateT.mk fails with type mismatch: `StateT` vs. `((StateT ?) ?) ?` while pattern matching works), and no parameterized struct exists anywhere in the corpus to prove otherwise. The single-constructor type is the shape examples/state_monad.mo already uses; construction (StateT.mk), matching, and everything else are spelled identically.

defStateT.run#
pub def StateT.run (m : StateT S M A) (s : S) : M (Pair S A)

Run a StateT computation from an initial state, giving the final state paired with the result.

defStateT.eval#
pub def StateT.eval [Monad M] {S : Type} {A : Type} (m : StateT S M A) (s : S) : M A

Evaluate the stateful computation, returning only the result.

defStateT.exec#
pub def StateT.exec [Monad M] {S : Type} {A : Type} (m : StateT S M A) (s : S) : M S

Execute the stateful computation, returning only the final state.

defStateT.lift#
pub def StateT.lift [Monad M] {S : Type} {A : Type} (m : M A) : StateT S M A

Lift an underlying computation, leaving the state untouched.

defStateT.get#
pub def StateT.get [Monad M, Copy S] {S : Type} : StateT S M S

Read the current state, keeping it threaded. Copy S is what lets the state be both returned and kept; there is no automatic duplication.

defStateT.put#
pub def StateT.put [Monad M] {S : Type} (s : S) : StateT S M Unit

Replace the state.

defStateT.modify#
pub def StateT.modify [Monad M] {S : Type} (f : S -> S) : StateT S M Unit

Modify the state and produce unit.

instance_#
pub instance [Monad M] Monad (StateT S M) {
    def pure (a : A) : StateT S M A :=
        StateT.mk (fn s => Monad.pure (Pair.pair s a))
    def bind (ma : StateT S M A) (f : A -> StateT S M B) : StateT S M B :=
        StateT.mk (StateT.bind_go ma f)
}

bind goes through StateT.bind_go, the same shape ResultT/OptionT use: the helper carries the dict parameter and the instance body's call forwards its own binding, so the body itself names no class.

typeReaderT#
pub type ReaderT (R : Type) (M : Type -> Type) (A : Type) {
    mk (run : R -> M A)
}

Reader transformer: ReaderT R M A = R -> M A threads a read-only environment R through the underlying monad M.

A single-constructor type, not a struct, for StateT's recorded reason: parameterized struct construction does not generalize its type parameters on the Rust host.

defReaderT.run#
pub def ReaderT.run (m : ReaderT R M A) (r : R) : M A

Run a ReaderT computation in an environment.

defReaderT.lift#
pub def ReaderT.lift {R : Type} {M : Type -> Type} {A : Type} (m : M A) : ReaderT R M A

Lift an underlying computation, leaving the environment untouched.

defReaderT.ask#
pub def ReaderT.ask [Monad M] {R : Type} : ReaderT R M R

Read the environment.

defReaderT.asks#
pub def ReaderT.asks [Monad M] {R : Type} {A : Type} (f : R -> A) : ReaderT R M A

Read the environment and apply a pure projection.

The body goes through Monad.bind/Monad.pure rather than the direct ReaderT.mk (fn (r : R) => Monad.pure (f r)): the direct form triggers the compiled constrained-generic dictionary miscompile (plans/implementations/2026-10-18-compiled-constrained-generic-dict-call-miscompiles.md) and crashes the self-hosted test driver (driver exited -1), while ReaderT.ask's variable-argument body is unaffected. Keep this shape until that bug is fixed.

defReaderT.local#
pub def ReaderT.local {R : Type} {R2 : Type} {M : Type -> Type} {A : Type}
(f : R2 -> R) (m : ReaderT R M A) : ReaderT R2 M A

Run m in a locally modified environment.

instance_#
pub instance [Monad M] Monad (ReaderT R M) {
    def pure (a : A) : ReaderT R M A :=
        ReaderT.mk (fn (_r : R) => Monad.pure a)
    def bind (ma : ReaderT R M A) (f : A -> ReaderT R M B) : ReaderT R M B :=
        ReaderT.mk (fn (r : R) => Monad.bind (ReaderT.run ma r) (ReaderT.bind_step f r))
}

The instance body references Monad.bind/Monad.pure DIRECTLY, the StateT/ResultT/OptionT precedent: the promoted method's own [Monad M] dictionary parameter (Phase 3) forwards them (D5), and a helper call in between would be resolved against abstract carriers.

typeResultT#
pub type ResultT (E : Type) (M : Type -> Type) (A : Type) {
    mk (run : M (Result E A))
}

Result transformer: ResultT E M A = M (Result E A) short-circuits the underlying monad's sequencing on Result.err, the same way IO (Result String A) already does by hand everywhere in this codebase (motes/ moose/src/client.mo's continuation chaining, for one) -- this gives that exact shape real do-notation instead.

defResultT.run#
pub def ResultT.run (m : ResultT E M A) : M (Result E A)
defResultT.lift#
pub def ResultT.lift [Monad M] {E : Type} {A : Type} (m : M A) : ResultT E M A

Lift an underlying computation that cannot itself fail.

instance_#
pub instance [Monad M] Monad (ResultT E M) {
    def pure (a : A) : ResultT E M A :=
        ResultT.mk (Monad.pure (Result.ok a))
    def bind (ma : ResultT E M A) (f : A -> ResultT E M B) : ResultT E M B :=
        ResultT.mk (Monad.bind (ResultT.run ma) (ResultT.bind_step f))
}
defResultT.throw#
pub def ResultT.throw [Monad M] {E : Type} {A : Type} (e : E) : ResultT E M A

ExceptT's throwError: fail with e, skipping the rest of the block. ResultT E M A IS ExceptT E M A (M (Result E A)), so this is the error-aware name for a shape this module already provides.

defResultT.catch#
pub def ResultT.catch [Monad M] {E : Type} {A : Type}
(m : ResultT E M A) (on_error : E -> ResultT E M A) : ResultT E M A

ExceptT's catchError / handleError: run on_error on Err.

defResultT.map_error#
pub def ResultT.map_error [Monad M] {E : Type} {E2 : Type} {A : Type}
(f : E -> E2) (m : ResultT E M A) : ResultT E2 M A

Map the error payload, leaving a successful value alone.

The mapping is written as an inline lambda rather than a map_error_step f helper: passing a partially-applied constrained helper whose body transforms the payload crashes the compiled backend (measured: driver exited -1), while the same body in a lambda works.

typeOptionT#
pub type OptionT (M : Type -> Type) (A : Type) {
    mk (run : M (Option A))
}

Option transformer: OptionT M A = M (Option A) short-circuits on Option.none, the ResultT shape with no error payload to carry.

defOptionT.run#
pub def OptionT.run (m : OptionT M A) : M (Option A)
defOptionT.lift#
pub def OptionT.lift [Monad M] {A : Type} (m : M A) : OptionT M A

Lift an underlying computation that always produces a value.

instance_#
pub instance [Monad M] Monad (OptionT M) {
    def pure (a : A) : OptionT M A :=
        OptionT.mk (Monad.pure (Option.some a))
    def bind (ma : OptionT M A) (f : A -> OptionT M B) : OptionT M B :=
        OptionT.mk (Monad.bind (OptionT.run ma) (OptionT.bind_step f))
}
typeWriterT#
pub type WriterT (W : Type) (M : Type -> Type) (A : Type) {
    mk (run : M (Pair W A))
}

Writer transformer: WriterT W M A = M (Pair W A) accumulates a monoid W alongside the result. bind appends the second computation's log after the first, the usual Writer order.

W needs BOTH Semigroup (for combine) and Monoid (for mempty): a superclass constraint gets no dictionary slot in this compiler (the same note Comonad carries), so the two are named explicitly.

defWriterT.run#
pub def WriterT.run (m : WriterT W M A) : M (Pair W A)
defWriterT.lift#
pub def WriterT.lift [Monad M, Monoid W] {A : Type} (m : M A) : WriterT W M A

Lift an underlying computation with an empty log.

defWriterT.tell#
pub def WriterT.tell [Monad M] {W : Type} (w : W) : WriterT W M Unit

Append w to the log, producing unit.

instance_#
pub instance [Monad M, Semigroup W, Monoid W] Monad (WriterT W M) {
    def pure (a : A) : WriterT W M A :=
        WriterT.mk (Monad.pure (Pair.pair (Monoid.mempty unit) a))
    def bind (ma : WriterT W M A) (f : A -> WriterT W M B) : WriterT W M B :=
        WriterT.mk (WriterT.bind_run ma f)
}
defWriterT.listen#
pub def WriterT.listen [Monad M, Copy W] {W : Type} {A : Type}
(m : WriterT W M A) : WriterT W M (Pair A W)

listen: pair the result with the log it wrote. Requires Copy W because the log is returned AND kept as the WriterT's own output.

defWriterT.censor#
pub def WriterT.censor [Monad M] {W : Type} {A : Type}
(f : W -> W) (m : WriterT W M A) : WriterT W M A

censor: transform the log a computation wrote. Inline lambda for the same compiled-backend reason as ResultT.map_error.

typeIdentityT#
pub type IdentityT (M : Type -> Type) (A : Type) {
    mk (run : M A)
}

Identity transformer: IdentityT M A = M A. A pass-through layer for stacks that want to name every level uniformly.

defIdentityT.run#
pub def IdentityT.run (m : IdentityT M A) : M A
defIdentityT.lift#
pub def IdentityT.lift {M : Type -> Type} {A : Type} (m : M A) : IdentityT M A
instance_#
pub instance [Monad M] Monad (IdentityT M) {
    def pure (a : A) : IdentityT M A :=
        IdentityT.mk (Monad.pure a)
    def bind (ma : IdentityT M A) (f : A -> IdentityT M B) : IdentityT M B :=
        IdentityT.mk (Monad.bind (IdentityT.run ma) (fn a => IdentityT.run (f a)))
}