What is indexed monad?

前端 未结 5 540
猫巷女王i
猫巷女王i 2020-11-27 09:47

What is indexed monad and the motivation for this monad?

I have read that it helps to keep track of the side effects. But the type signature and documentation doesn\

5条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 09:59

    An indexed monad isn't a specific monad like, for example, the state monad but a sort of generalization of the monad concept with extra type parameters.

    Whereas a "standard" monadic value has the type Monad m => m a a value in an indexed monad would be IndexedMonad m => m i j a where i and j are index types so that i is the type of the index at the beginning of the monadic computation and j at the end of the computation. In a way, you can think of i as a sort of input type and j as the output type.

    Using State as an example, a stateful computation State s a maintains a state of type s throughout the computation and returns a result of type a. An indexed version, IndexedState i j a, is a stateful computation where the state can change to a different type during the computation. The initial state has the type i and state and the end of the computation has the type j.

    Using an indexed monad over a normal monad is rarely necessary but it can be used in some cases to encode stricter static guarantees.

提交回复
热议问题