我想在Haskell程序中有一些更高级别的函数来调用其他函数,这些函数最终会调用使用某些状态或配置的函数,而不必在所有这些函数调用中传递状态。我知道这是状态monad(或可能是Reader monad?)的经典用法。
(我也不确定是否应该使用StateT(如下面的示例所示)来启用IO,或者是否应该以某种方式分别输出结果。)
在此阶段,我对这里的所有教程,博客文章和类似问题感到非常困惑,无法选择解决方案。还是我误解了隐藏的东西?
这是一个小例子:
import Control.Monad.State
-- Here's a simple configuration type:
data Config = MkConfig {
name :: String
, num :: Int
} deriving Show
-- Here's a couple of configurations.
-- (They're hard coded and pre-defined.)
c1 = MkConfig "low" 7
c2 = MkConfig "high" 10
-- Here's a lower level function that explicitly uses the config.
-- (The String is ignored here for simplicity, but it could be used.)
fun :: Config -> Int -> Int
fun (MkConfig _ i) j = i*j
-- testA and GoA work fine as expected.
-- fun uses the different configs c1,c2 in the right way.
testA = do
a <- get
lift (print (fun a 2))
put c2
a <- get
lift (print (fun a 4))
goA = evalStateT testA c1
-- (c1 could be put at the start of testA instead.)
-- But what I really want is to use fun2 that calls fun,
-- and not explicitly need state.
-- But this function definition does not compile:
fun2 :: Int -> Int
fun2 j = 3 * fun cf j
-- fun needs a config arg cf, but where from?
-- I would like a similar way of using fun2 as in testB and goB here.
testB = do
a <- get
lift (print (fun2 3)) -- but fun2 doesn't take the state in a
put c2
a <- get
lift (print (fun2 42)) -- but fun2 doesn't take the state in a
goB = evalStateT testB c1
我想使配置远离程序中的fun2之类的高级功能,同时仍保留更改配置并使用新配置运行那些功能的能力。这是一个“怎么做的问题”(除非我完全弄错了主意)。
答案 0 :(得分:2)
您当然不能在类型签名中完全“隐藏配置”:普通的旧函数Int -> Int
必须是参照透明的,因此它也不能依赖或接受某些{{ 1}}值。
您可能想做的事情是这样的:
Config
然后只要有fun2 :: Int -> State Config Int -- An `Int -> Int` that depends on `Config` state.
-- Compare to how `Int -> IO Int` is like an
-- `Int -> Int` function that depends on IO.
fun2 j = do
c1 <- get
return (3 * fun c1 j)
,就可以通过类似
c :: Config
另请参阅Combining StateT IO with State:
let result = evalState (fun2 42) c -- An Int.
然后您可以编写类似的内容
hoistState :: Monad m => State s a -> StateT s m a
hoistState = StateT . (return .) . runState