如何很好地评估嵌套的StateT和ErrorT monad?

Ric*_* T. 3 monads haskell monad-transformers

我在程序的不同级别有两个类型的控件结构声明.最下面的是Agent一个StateTIO能力的.第二个是StateT具有Agent功能的另一个,第三个是(Plan)是一个ErrorT.

type Agent = StateT AgentState IO
type Plan = ErrorT PlanError (StateT PlanState Agent)
Run Code Online (Sandbox Code Playgroud)

评估a的最佳方法是Plan什么?我写了下面的代码,但它不是很少,因为有大量的嵌套runStateTrunErrorT调用.

foo :: Plan ()
defaultAgentState :: AgentState
runStateT (runStateT (runErrorT foo) (PlanState 0)) defaultAgentState
Run Code Online (Sandbox Code Playgroud)

有更简单/更好的东西吗?

Dan*_*her 6

如果你有一个monad变换器堆栈,那么必须runXyzT在某个时刻调用各个变换器的每个函数,遗憾的是没有捷径.

但是,如果您多次使用特定堆栈,则定义一个特殊runMyStack函数是值得的,因此堆叠的混乱runXyzT只出现在一个点上.