Is a state monad with two state variable types (in and out) still a monad?

Haskell's state monad State sa forces me to keep the same type of s during the whole do block. But since the state monad is really just a function, what if I define it as State ioa = State (i -> (o, a)) ?. The return and bind functions would look exactly the same as in the standard state monad, but with the types changed:

return :: a -> State st st a
bind :: (State i o a) -> (a -> (State o o' b)) -> (State i o' b)

I don't think is possible to implement Monad in Haskell using this definition because it expects a single State io type in bind (only a can change). But this question is not about Haskell but about whether this would technically be a monad or not. Or if not, would it be some kind of superset of a monad (such that all the monad laws still apply but has some extra features)?

This is something I found would be useful in another language I'm working on, which is based on lambda calculus, so I'm using Haskell as a reference. I just don't want this to break other stuff later on where I'd expect the monad laws to apply.


What you are looking for is an indexed Monad. See eg the definition in category-extras :

The definition of an indexed Monad:

class IxApplicative m => IxMonad m where
  ibind :: (a -> m j k b) -> m i j a -> m i k b

The State indexed Monad:

class IxMonad m => IxMonadState m where
  iget :: m i i i
  iput :: j -> m i j ()
链接地址: http://www.djcxy.com/p/42918.html

上一篇: Haskell中的Monadic类型检查器

下一篇: 是一个状态monad,有两个状态变量类型(进出)还是单子吗?