Real life and useful examples of the reverse state monad

Reverse-state monastery is a really nice and thoughtful example of Haskell's expressiveness and lazy evaluation. But it is not easy to understand this monad. Moreover, it is very difficult to find any convincing real-life example of what you can do with a reverse state monad than with any other tool in that language.

A monk's reverse state is defined as follows:

newtype RState s a = RState { runRState :: s -> (a,s) }

instance Monad (RState s) where
    return x = RState $ (,) x
    RState sf >>= f = RState $ \s ->
        let (a, past)   = sf future
            (b, future) = runRState (f a) s
        in (b, past)

      

There are already a few examples and customs in it, but I don't think they are entirely practical.

  • Quora's answer : well explained and even has a real use case, but no code, and it's not clear if it's really a good idea to use RState

    .
  • Mindfuck : Introducing this beautiful concept, but the example is not helpful. Thus, no one will write Fibonacci numbers.
  • Kwang Haskell blog : shows how Writer

    you can emulate with RState

    , but let's go. Not a real example of life :)

I also know tardis

, but there is no tutorial on this library, the documentation examples are really abstract, not many people really understand this. The closest to what I want is this tutorial , but it has an example tardis

, not just one RState

. Just like this reference book .

Thus, I am not looking for real life templates tardis

, I am only interested in RState

illustration, if possible. Although I understand that there cannot be samples of pure methods RState

. In this case a minimal example with a transformer RStateT

or tardis

is good enough.

Has anyone used this monad in real life, or a really nice and useful illustration with code?

+3


source to share





All Articles