haskell - 如何使用 StateT、ContT 和 ReaderT 创建 monad?

标签 haskell monads monad-transformers continuations

如何创建一个使用 State、Cont 和 Reader 转换器的 monad?我想阅读一个环境,并更新/使用状态。但是,我也想暂停/中断操作。例如,如果满足某个条件,则状态保持不变。

到目前为止,我有一个使用 ReaderT 和 StateT 的 monad,但我不知道如何包含 ContT:

{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Test where
-- monads
import   Data.Functor.Identity (Identity, runIdentity)
import   Control.Monad.State
import   Control.Monad.Reader
import   Control.Monad.Cont

-- reader environment
type In = Integer

-- cont: if true then pause, else continue 
type Pause = Bool

-- state environment:
newtype StateType = StateType { s :: Integer }

newtype M r = M {_unM :: ReaderT In (ContT Pause (StateT StateType Identity)) r}
  deriving ( Functor, Applicative, Monad
           , MonadReader In
           , MonadCont   Pause
           , MonadState  StateType
           )

-- run monadic action
runM :: In -> Pause -> StateType -> M r -> StateType
runM inp pause initial act
  = runIdentity             -- unwrap identity
  $ flip execStateT initial -- unwrap state
  $ flip runContT   pause   -- unwrap cont
  $ flip runReaderT inp     -- unwrap reader
  $ _unM act                -- unwrap action

这给出了错误:

* Expected kind `* -> *', but `Pause' has kind `*'
* In the first argument of `MonadCont', namely `Pause'
  In the newtype declaration for `M'
  |
24|         , MonadCont  Pause
  |

好的,但是为什么 Pause 需要 kind * -> *?...我淹没在类型中,需要解释。 Pause 必须采用什么形式,一个函数? ContT如何整合?最终,我打算将 Cont 用于控制结构。

最佳答案

MonadReaderMonadState 不同,MonadCont 类型类takes only one parameter .由于该参数 m 必须是 Monad,因此它必须具有类型 * -> *

在派生子句中,您需要 MonadCont, 而不是 MonadCont Pause

在回答后续问题时添加:

继续 is defined as:

newtype ContT r m a = ContT { runContT :: (a -> m r) -> m r }

请注意,newtype M r 定义中的 r 作为最终 (a) 参数传递给 ContT。插入变量,你有

ContT Bool (State StateType) a = ContT { 
    runContT :: (a -> State StateType Bool) -> (State StateType Bool)
  }

这提供了一个计算上下文,您可以在其中操作 StateType,并使用定界延续。最终,您将构造一个 ContT Bool (State StateType) Bool。然后您可以运行延续(使用 evalContT ),并返回到更简单的 State StateType 上下文。 (实际上,您可以在程序的同一部分解包所有 3 个 monad 转换器。)

关于haskell - 如何使用 StateT、ContT 和 ReaderT 创建 monad?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53403001/

相关文章:

scala - 为什么我们需要 Scala 中的 Monad Transformers?

haskell - 如何用递归定义遍历 Monad 并收集结果?

haskell - 为什么简单地使用 State monad 会导致堆栈溢出?

performance - 如何在 Haskell 中有效地字节交换二进制数据

haskell - 如何在Haskell中创建包含有限长度字符串的类型

haskell - 在 Haskell 中执行一系列操作时的异常处理

haskell - Haskell 中的大规模设计?

haskell - Monad 变形金刚文档 : eval1 doesn't typecheck

haskell - 从 State 切换到 StateT 后,如何恢复单子(monad)构造列表的惰性求值?

haskell - 了解 `dropWhile`