lisa.monad.State#
- class lisa.monad.State(f)[source]#
Bases:
MonadTrans
Monad transformer analogous to Haskell’s
StateT
transformer.It manipulates state-transforming functions of type
state -> (value, new_state)
. This allows simulating a global state, without actually requiring one.Attributes
Properties
State-transforming function of type
state -> (value, new_state)
.Methods
Allow calling monadic values to run the state-transforming function, with the initial state provided by
State.make_state()
.Takes a monadic value Monad[A], a function that takes an A and returns Monad[B], and returns a Monad[B].
Build a monadic value out of a state modifying function of type
state -> (value, new_state)
.Returns a monadic value returning the current state.
Lift a monadic value
m
by 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
m
by 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)
.Create an initial state. All the parameters of
State.__call__()
are passed toState.make_state()
.Returns a monadic value applying
f
on the current state, setting the new state and then returning it.Returns a monadic value setting the current state and returning the old one.
__await__()
inheriteddo()
inheritedDecorate a coroutine function so that
awaits
gains the powers of the monad.join()
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].
pure()
inheritedTurn a regular value of type
A
into a monadic value of typeMonad[A]
.
Attributes#
- State.__slots__ = ('_f',)#
Properties#
Methods#
- State.__call__(*args, **kwargs)[source]#
Allow calling monadic values to run the state-transforming function, with the initial state provided by
State.make_state()
.
- State.bind(continuation)[source]#
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
_TailCall
instance.
- classmethod State.from_f(f)[source]#
Build a monadic value out of a state modifying function of type
state -> (value, new_state)
.
- State.hoist(self, nat)[source]#
Lift a monadic value
m
by 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
lift
adds 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
hoist
as defined in https://hackage.haskell.org/package/mmorphNote
Note for implementers: A monad transformers
t m a
(t
is the transformer HKT,m
is the base monad anda
is the “contained type) usually ends up containing an “m (f a)” (f
being some kind of functor). For example,MaybeT
in Haskell (Option
here) is more or less defined asdata MaybeT m a = MaybeT (m (Maybe a))
. What thehoist
implementation 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)))
.
- classmethod State.lift(m)[source]#
Lift a monadic value
m
by 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
lift
as defined in https://hackage.haskell.org/package/transformers
- classmethod State.make_state(x)[source]#
Create an initial state. All the parameters of
State.__call__()
are passed toState.make_state()
.
- classmethod State.modify_state(f)[source]#
Returns a monadic value applying
f
on the current state, setting the new state and then returning it.
- classmethod State.set_state(new)[source]#
Returns a monadic value setting the current state and returning the old one.
- State.__await__()#
Inherited method, see
lisa.monad.Monad.__await__()
- classmethod State.do(f)#
Inherited method, see
lisa.monad.MonadTrans.do()
Decorate a coroutine function so that
awaits
gains the powers of the monad.
- classmethod State.join(self)#
Inherited method, see
lisa.monad.Monad.join()
Takes a monadic value Monad[Monad[A]], and returns a Monad[A].
- classmethod State.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].
- classmethod State.pure(x)#
Inherited method, see
lisa.monad.MonadTrans.pure()
Turn a regular value of type
A
into a monadic value of typeMonad[A]
.