init::transformers
init/src/transformers.mo
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.
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.
pub def StateT.eval [Monad M] {S : Type} {A : Type} (m : StateT S M A) (s : S) : M AEvaluate the stateful computation, returning only the result.
pub def StateT.exec [Monad M] {S : Type} {A : Type} (m : StateT S M A) (s : S) : M SExecute the stateful computation, returning only the final state.
pub def StateT.lift [Monad M] {S : Type} {A : Type} (m : M A) : StateT S M ALift an underlying computation, leaving the state untouched.
pub def StateT.get [Monad M, Copy S] {S : Type} : StateT S M SRead the current state, keeping it threaded. Copy S is what lets the
state be both returned and kept; there is no automatic duplication.
pub def StateT.put [Monad M] {S : Type} (s : S) : StateT S M UnitReplace the state.
pub def StateT.modify [Monad M] {S : Type} (f : S -> S) : StateT S M UnitModify the state and produce unit.
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.
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.
pub def ReaderT.run (m : ReaderT R M A) (r : R) : M ARun a ReaderT computation in an environment.
pub def ReaderT.lift {R : Type} {M : Type -> Type} {A : Type} (m : M A) : ReaderT R M ALift an underlying computation, leaving the environment untouched.
pub def ReaderT.ask [Monad M] {R : Type} : ReaderT R M RRead the environment.
pub def ReaderT.asks [Monad M] {R : Type} {A : Type} (f : R -> A) : ReaderT R M ARead 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.
pub def ReaderT.local {R : Type} {R2 : Type} {M : Type -> Type} {A : Type}
(f : R2 -> R) (m : ReaderT R M A) : ReaderT R2 M ARun m in a locally modified environment.
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.
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.
pub def ResultT.run (m : ResultT E M A) : M (Result E A)pub def ResultT.lift [Monad M] {E : Type} {A : Type} (m : M A) : ResultT E M ALift an underlying computation that cannot itself fail.
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))
}pub def ResultT.throw [Monad M] {E : Type} {A : Type} (e : E) : ResultT E M AExceptT'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.
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 AExceptT's catchError / handleError: run on_error on Err.
pub def ResultT.map_error [Monad M] {E : Type} {E2 : Type} {A : Type}
(f : E -> E2) (m : ResultT E M A) : ResultT E2 M AMap 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.
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.
pub def OptionT.run (m : OptionT M A) : M (Option A)pub def OptionT.lift [Monad M] {A : Type} (m : M A) : OptionT M ALift an underlying computation that always produces a value.
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))
}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.
pub def WriterT.run (m : WriterT W M A) : M (Pair W A)pub def WriterT.lift [Monad M, Monoid W] {A : Type} (m : M A) : WriterT W M ALift an underlying computation with an empty log.
pub def WriterT.tell [Monad M] {W : Type} (w : W) : WriterT W M UnitAppend w to the log, producing unit.
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)
}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.
pub def WriterT.censor [Monad M] {W : Type} {A : Type}
(f : W -> W) (m : WriterT W M A) : WriterT W M Acensor: transform the log a computation wrote. Inline lambda for the
same compiled-backend reason as ResultT.map_error.
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.
pub def IdentityT.run (m : IdentityT M A) : M Apub def IdentityT.lift {M : Type -> Type} {A : Type} (m : M A) : IdentityT M Apub 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)))
}