lisa.monad.MonadTrans#
- class lisa.monad.MonadTrans[source]#
-
Base class for monad transformers.
Heavily inspired by transformers as defined by: https://hackage.haskell.org/package/transformers
And stack manipulation inspired by: https://hackage.haskell.org/package/mmorph
Methods
Takes a monadic value Monad[A], a function that takes an A and returns Monad[B], and returns a Monad[B].
Decorate a coroutine function so that
awaitsgains the powers of the monad.Lift a monadic value
mby one level in the stack, i.e.: Given a stack for 3 transformersT1(T2(T3(Identity))), a valuem = T2(Identity).pure(42). we haveT2.hoist(m, T3.pure) == T2(T3(Identity)).pure(42).Lift a monadic value
mby one level in the stack, i.e.: Given a stack for 3 transformersT1(T2(T3(Identity))), a valuem = T3(Identity).pure(42). we haveT2.lift(m) == T2(T3(Identity)).pure(42).Turn a regular value of type
Ainto a monadic value of typeMonad[A].__await__()inheritedjoin()inheritedTakes a monadic value Monad[Monad[A]], and returns a Monad[A].
map()inheritedTakes a monadic value Monad[A], a function that takes an A and returns B, and returns a Monad[B].
Methods#
- abstract MonadTrans.bind(continuation)#
Takes a monadic value Monad[A], a function that takes an A and returns Monad[B], and returns a Monad[B].
Note
It is allowed to return a
_TailCallinstance.
- classmethod MonadTrans.do(f)[source]#
Decorate a coroutine function so that
awaitsgains the powers of the monad.See also
This decorator is very similar to the do-notation in Haskell.
- abstract classmethod MonadTrans.hoist(self, nat)[source]#
Lift a monadic value
mby one level in the stack, i.e.: Given a stack for 3 transformersT1(T2(T3(Identity))), a valuem = T2(Identity).pure(42). we haveT2.hoist(m, T3.pure) == T2(T3(Identity)).pure(42).In other words, it allows adding a level “from below”, whereas
liftadds a level “from above”. It’s similar tomap, except that instead of traversing all the nested functor layers, it stops at the first one.- Parameters:
self (lisa.monad.Monad) – Monadic value to hoist.
nat (collections.abc.Callable) – Natural transform. i.e. a morphism from
Monad1[A]toMonad2[A]that obeys certain laws.
See also
hoistas defined in https://hackage.haskell.org/package/mmorphNote
Note for implementers: A monad transformers
t m a(tis the transformer HKT,mis the base monad andais the “contained type) usually ends up containing an “m (f a)” (fbeing some kind of functor). For example,MaybeTin Haskell (Optionhere) is more or less defined asdata MaybeT m a = MaybeT (m (Maybe a)). What thehoistimplementation must do is to “rebuild” a value with a call tonat()around them (...)part. ForMaybeT, this giveshoist nat (MaybeT (m (Maybe a))) = MaybeT(nat(m (Maybe a))).
- abstract classmethod MonadTrans.lift(m)[source]#
Lift a monadic value
mby one level in the stack, i.e.: Given a stack for 3 transformersT1(T2(T3(Identity))), a valuem = T3(Identity).pure(42). we haveT2.lift(m) == T2(T3(Identity)).pure(42).See also
liftas defined in https://hackage.haskell.org/package/transformers
- classmethod MonadTrans.pure(x)[source]#
Turn a regular value of type
Ainto a monadic value of typeMonad[A].
- MonadTrans.__await__()#
Inherited method, see
lisa.monad.Monad.__await__()
- classmethod MonadTrans.join(self)#
Inherited method, see
lisa.monad.Monad.join()Takes a monadic value Monad[Monad[A]], and returns a Monad[A].
- classmethod MonadTrans.map(self, f)#
Inherited method, see
lisa.monad.Monad.map()Takes a monadic value Monad[A], a function that takes an A and returns B, and returns a Monad[B].