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

f

State-transforming function of type state -> (value, new_state).

Methods

__call__()

Allow calling monadic values to run the state-transforming function, with the initial state provided by State.make_state().

bind()

Takes a monadic value Monad[A], a function that takes an A and returns Monad[B], and returns a Monad[B].

from_f()

Build a monadic value out of a state modifying function of type state -> (value, new_state).

get_state()

Returns a monadic value returning the current state.

hoist()

Lift a monadic value m by one level in the stack, i.e.: Given a stack for 3 transformers T1(T2(T3(Identity))), a value m = T2(Identity).pure(42). we have T2.hoist(m, T3.pure) == T2(T3(Identity)).pure(42).

lift()

Lift a monadic value m by one level in the stack, i.e.: Given a stack for 3 transformers T1(T2(T3(Identity))), a value m = T3(Identity).pure(42). we have T2.lift(m) == T2(T3(Identity)).pure(42).

make_state()

Create an initial state. All the parameters of State.__call__() are passed to State.make_state().

modify_state()

Returns a monadic value applying f on the current state, setting the new state and then returning it.

set_state()

Returns a monadic value setting the current state and returning the old one.

__await__() inherited

do() inherited

Decorate a coroutine function so that awaits gains the powers of the monad.

join() inherited

Takes a monadic value Monad[Monad[A]], and returns a Monad[A].

map() inherited

Takes a monadic value Monad[A], a function that takes an A and returns B, and returns a Monad[B].

pure() inherited

Turn a regular value of type A into a monadic value of type Monad[A].

Attributes#

State.__slots__ = ('_f',)#

Properties#

property State.f[source]#

State-transforming function of type state -> (value, new_state)

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).

classmethod State.get_state()[source]#

Returns a monadic value returning the current state.

State.hoist(self, nat)[source]#

Lift a monadic value m by one level in the stack, i.e.: Given a stack for 3 transformers T1(T2(T3(Identity))), a value m = T2(Identity).pure(42). we have T2.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 to map, except that instead of traversing all the nested functor layers, it stops at the first one.

Parameters:

See also

hoist as defined in https://hackage.haskell.org/package/mmorph

Note

Note for implementers: A monad transformers t m a (t is the transformer HKT, m is the base monad and a 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 as data MaybeT m a = MaybeT (m (Maybe a)). What the hoist implementation must do is to “rebuild” a value with a call to nat() around the m (...) part. For MaybeT, this gives hoist 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 transformers T1(T2(T3(Identity))), a value m = T3(Identity).pure(42). we have T2.lift(m) == T2(T3(Identity)).pure(42).

classmethod State.make_state(x)[source]#

Create an initial state. All the parameters of State.__call__() are passed to State.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 type Monad[A].